A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(cache): mo.cache raises KeyError: '__scratch__' in scratchpad (#9664)
Everett Roeth5c1bb8089988
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 7b21537a45..f0623356d2 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -189,12 +189,13 @@ def defs() -> tuple[str, ...]:return ()if ctx.execution_context is not None:- return tuple(- sorted(- defn- for defn in ctx.graph.cells[ctx.execution_context.cell_id].defs- )- )+ cell_id = ctx.execution_context.cell_id+ # The scratchpad cell lives in a Runner-local graph, not in+ # ctx.graph (which always returns the kernel's main graph). It+ # also has no meaningful defs in any case.+ if cell_id not in ctx.graph.cells:+ return ()+ return tuple(sorted(defn for defn in ctx.graph.cells[cell_id].defs))return ()@@ -216,10 +217,15 @@ def refs() -> tuple[str, ...]:)if ctx.execution_context is not None:+ cell_id = ctx.execution_context.cell_id+ # Scratchpad cell isn't registered in the main graph; same as+ # defs() above.+ if cell_id not in ctx.graph.cells:+ return ()return tuple(sorted(defn- for defn in ctx.graph.cells[ctx.execution_context.cell_id].refs+ for defn in ctx.graph.cells[cell_id].refs# exclude builtins that have not been shadowedif defn not in unshadowed_builtins)diff --git a/marimo/_save/save.py b/marimo/_save/save.pyindex 6a59cb6fe3..342931a52d 100644--- a/marimo/_save/save.py+++ b/marimo/_save/save.py@@ -6,6 +6,7 @@ import asyncioimport functoolsimport inspectimport io+import linecacheimport sysimport threadingimport time@@ -38,6 +39,8 @@ from marimo._ast.transformers import (from marimo._ast.variables import is_mangled_local, unmangle_localfrom marimo._messaging.tracebacks import write_tracebackfrom marimo._runtime.context import get_context, safe_get_context+from marimo._runtime.dataflow import DirectedGraph+from marimo._runtime.scratch import SCRATCH_CELL_IDfrom marimo._runtime.side_effect import SideEffectfrom marimo._runtime.state import Statefrom marimo._save.cache import (@@ -71,7 +74,6 @@ if TYPE_CHECKING:from typing_extensions import Self- from marimo._runtime.dataflow import DirectedGraphfrom marimo._save.stores import StoreP = ParamSpec("P")@@ -169,8 +171,15 @@ class _cache_call(CacheContext, Generic[P, R]):ctx.cell_id or ctx.execution_context.cell_id or CellId_t(""))# If the cell ID is "external", that means it's not from the main- # graph but rather from an embedded graph.- self._external = is_external_cell_id(maybe_cell_id)+ # graph but rather from an embedded graph. The scratchpad cell+ # also lives outside the main graph (it's only registered in a+ # Runner-local DirectedGraph during run_scratchpad), so it+ # follows the same code path: defer to graph_from_scope at call+ # time, no main-graph lookup.+ self._external = (+ is_external_cell_id(maybe_cell_id)+ or maybe_cell_id == SCRATCH_CELL_ID+ )if not self._external:graph = ctx.graphglbls = ctx.globals@@ -710,6 +719,23 @@ class _cache_context(SkipContext, CacheContext):code = cell.codeelif cell_id in graph.cells:code = graph.cells[cell_id].code+ elif cell_id == SCRATCH_CELL_ID:+ # The scratchpad cell lives in a Runner-local graph,+ # not in ctx.graph (the kernel's main graph). Pull+ # source from linecache, which compile_cell populated+ # when the scratchpad was compiled. Use an empty+ # graph + empty cell_id for the BlockHasher call+ # below, mirroring how `@mo.cache._set_context`+ # handles cells outside the main graph.+ lines = linecache.getlines(filename)+ if not lines:+ raise CacheException(+ "Could not resolve cell for cache."+ f"{UNEXPECTED_FAILURE_BOILERPLATE}"+ )+ code = "".join(lines)+ graph = DirectedGraph()+ cell_id = CellId_t("")else:raise CacheException("Could not resolve cell for cache."diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 0e9793a2dd..93f734e907 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -1677,6 +1677,71 @@ except NameError:# Does not pollute globals, reverts back to 10assert k.globals["z"] == 10+ @staticmethod+ async def test_run_scratch_with_mo_cache_decorator(+ mocked_kernel: MockedKernel,+ ) -> None:+ """Regression test: @mo.cache decoration inside the scratchpad must+ not raise `KeyError: '__scratch__'`.++ Before the fix that registers SCRATCH_CELL_ID in the kernel's main+ graph during run_scratchpad, the cache decorator's `_set_context`+ crashed at `graph.cells[cell_id]` because `__scratch__` lived only+ in the Runner's local graph, never in `self.graph`.+ """+ k = mocked_kernel.k+ await k.run_scratchpad(+ "import marimo as mo\[email protected]\ndef f(x): return x * 2\nf(3)"+ )+ # No KeyError leaked+ assert not any(+ "__scratch__" in m for m in mocked_kernel.stderr.messages+ )+ # __scratch__ does not linger in the main graph after teardown+ assert SCRATCH_CELL_ID not in k.graph.cells+ # Scratchpad does not pollute globals+ assert "f" not in k.globals++ @staticmethod+ async def test_run_scratch_with_persistent_cache_context(+ mocked_kernel: MockedKernel,+ tmp_path: pathlib.Path,+ ) -> None:+ """Regression test: `with mo.persistent_cache(...)` in scratchpad+ must not raise CacheException via the parallel+ `_cache_context.trace` code path.+ """+ k = mocked_kernel.k+ await k.run_scratchpad(+ "import marimo as mo\n"+ "from pathlib import Path\n"+ f"with mo.persistent_cache('scratch_test', save_path=Path({str(tmp_path)!r})):\n"+ " x = sum(range(100))\n"+ "x"+ )+ assert not any(+ "CacheException" in m or "Could not resolve cell" in m+ for m in mocked_kernel.stderr.messages+ )+ assert SCRATCH_CELL_ID not in k.graph.cells++ @staticmethod+ async def test_run_scratch_with_mo_cache_cleans_up_after_crash(+ mocked_kernel: MockedKernel,+ ) -> None:+ """Regression test: if the scratchpad raises AFTER decorating,+ `__scratch__` is still unregistered from the kernel graph (the+ `try/finally` correctness guard).+ """+ k = mocked_kernel.k+ await k.run_scratchpad(+ "import marimo as mo\n"+ "@mo.cache\n"+ "def f(x): return x * 2\n"+ "raise RuntimeError('intentional')"+ )+ assert SCRATCH_CELL_ID not in k.graph.cells+async def test_rename(self, any_kernel: Kernel, exec_req: ExecReqProvider) -> None: