Skip to content

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.

fix(code-mode): run unexecuted dependencies in edit transactions (#11115)

Shahmir Varqha93f5b52fa8a8
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.py
index 9602d1f2ba..f297ac7909 100644
--- a/marimo/_code_mode/_context.py
+++ b/marimo/_code_mode/_context.py
@@ -1175,7 +1175,9 @@ class AsyncCodeModeContext:
if op.config is not None:
return op.config
meta = self._kernel.cell_metadata.get(cell_id)
- return meta.config if meta else CellConfig()
+ if meta is not None:
+ return meta.config
+ return self._document.get_cell(cell_id).config
def edit_cell(
self,
@@ -1760,18 +1762,41 @@ class AsyncCodeModeContext:
resolved_configs[entry.cell_id] = CellConfig(hide_code=True)
else:
existing_meta = self._kernel.cell_metadata.get(entry.cell_id)
- resolved_configs[entry.cell_id] = (
- existing_meta.config if existing_meta else CellConfig()
- )
+ if existing_meta is not None:
+ resolved_configs[entry.cell_id] = existing_meta.config
+ else:
+ resolved_configs[entry.cell_id] = self._document.get_cell(
+ entry.cell_id
+ ).config
# Let mutate_graph handle all graph mutations: it properly
# cleans up globals, UI elements, and lifecycle hooks for
# deleted/replaced cells via _delete_cell / _deactivate_cell.
+ _run_set = explicit_run or set()
execution_requests = [
ExecuteCellCommand(cell_id=e.cell_id, code=e.code)
for e in code_entries
if e.code is not None
]
+ requested_ids = {request.cell_id for request in execution_requests}
+ # Requested cells may exist only in the document. Register them in
+ # the same mutation so dependency sorting sees the whole run batch.
+ for entry in plan:
+ if (
+ entry.cell_id in _run_set
+ and entry.cell_id not in self.graph.cells
+ and entry.cell_id not in requested_ids
+ ):
+ execution_requests.append(
+ ExecuteCellCommand(
+ cell_id=entry.cell_id,
+ code=existing_code[entry.cell_id],
+ )
+ )
+ resolved_configs.setdefault(
+ entry.cell_id,
+ self._document.get_cell(entry.cell_id).config,
+ )
deletion_requests = [
DeleteCellCommand(cell_id=cid)
for cid in existing_id_set - plan_ids
@@ -1826,7 +1851,6 @@ class AsyncCodeModeContext:
# Run queued cells (explicit run_cell + autorun descendants),
# filtered to cells that still exist after structural ops.
- _run_set = explicit_run or set()
if _run_set and self._kernel.reactive_execution_mode == "autorun":
_run_set = _run_set | cells_to_run
if _run_set:
diff --git a/tests/_code_mode/test_code_mode_context.py b/tests/_code_mode/test_code_mode_context.py
index 1099ad56e6..30ef436ffa 100644
--- a/tests/_code_mode/test_code_mode_context.py
+++ b/tests/_code_mode/test_code_mode_context.py
@@ -1329,6 +1329,43 @@ class TestAutorunStaleState:
class TestDocumentKernelDivergence:
"""Tests for cells that exist in the document but not in the kernel graph."""
+ @pytest.mark.parametrize("operation", ["run", "edit", "configure"])
+ async def test_run_doc_only_dependencies(
+ self, k: Kernel, operation: str
+ ) -> None:
+ imports = NotebookCell(
+ id=CellId_t("imports"),
+ code="import math",
+ name="",
+ config=CellConfig(hide_code=True, expand_output=True),
+ )
+ calculation = NotebookCell(
+ id=CellId_t("calculation"),
+ code="result = math.sqrt(4)",
+ name="",
+ config=CellConfig(),
+ )
+ unrelated = NotebookCell(
+ id=CellId_t("unrelated"),
+ code="raise RuntimeError('must not run')",
+ name="",
+ config=CellConfig(),
+ )
+ with _ctx(k, extra_doc_cells=[calculation, imports, unrelated]) as ctx:
+ async with ctx as nb:
+ if operation == "edit":
+ nb.edit_cell("calculation", code="result = math.sqrt(9)")
+ elif operation == "configure":
+ nb.edit_cell("calculation", hide_code=True)
+ nb.run_cell("calculation")
+ nb.run_cell("imports")
+
+ assert k.globals["result"] == (3 if operation == "edit" else 2)
+ assert set(k.graph.cells) == {"imports", "calculation"}
+ if operation != "run":
+ assert k.graph.cells["imports"].config == imports.config
+ assert "unrelated" not in k.cell_metadata
+
async def test_delete_doc_only_cell(self, k: Kernel) -> None:
"""Deleting a cell that is in the document but not the kernel
graph should succeed without KeyError."""
@@ -1342,18 +1379,42 @@ class TestDocumentKernelDivergence:
# The ghost cell should not appear in the graph.
assert "ghost" not in k.graph.cells
- async def test_edit_and_run_doc_only_cell(self, k: Kernel) -> None:
+ @pytest.mark.parametrize("disabled", [False, True])
+ @pytest.mark.parametrize("edit", ["code", "config", "both"])
+ async def test_edit_and_run_doc_only_cell(
+ self, k: Kernel, disabled: bool, edit: str
+ ) -> None:
"""A cell present only in the document can be edited and run,
bringing it into the kernel graph."""
ghost = NotebookCell(
- id=CellId_t("ghost"), code="z = 0", name="", config=CellConfig()
+ id=CellId_t("ghost"),
+ code="z = 0",
+ name="",
+ config=CellConfig(
+ disabled=disabled, hide_code=True, expand_output=True, column=2
+ ),
)
with _ctx(k, extra_doc_cells=[ghost]) as ctx:
async with ctx as nb:
- nb.edit_cell("ghost", code="z = 42")
+ nb.edit_cell(
+ "ghost",
+ code=None if edit == "config" else "z = 42",
+ hide_code=None if edit == "code" else False,
+ )
nb.run_cell("ghost")
- assert k.globals["z"] == 42
+ expected_config = CellConfig(
+ disabled=disabled,
+ hide_code=edit == "code",
+ expand_output=True,
+ column=2,
+ )
+ assert k.graph.cells["ghost"].config == expected_config
+ assert k.cell_metadata["ghost"].config == expected_config
+ if disabled:
+ assert "z" not in k.globals
+ else:
+ assert k.globals["z"] == (0 if edit == "config" else 42)
async def test_create_cell_no_collision_with_doc_only_ids(
self, k: Kernel