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.

Use document as source of truth in code_mode _apply_ops (#8944)

Trevor Manz6a4c99bb0f41
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.py
index 638f7dce34..f18e8a5b40 100644
--- a/marimo/_code_mode/_context.py
+++ b/marimo/_code_mode/_context.py
@@ -870,17 +870,15 @@ class AsyncCodeModeContext:
self, ops: list[_Op], explicit_run: set[CellId_t] | None = None
) -> None:
"""Validate, plan, format, and apply a batch of operations."""
- existing_ids = list(self.graph.cells.keys())
+ existing_ids = list(self._document)
plan = _build_plan(existing_ids, ops)
# Auto-format new/changed code.
plan = await self._format_plan(plan)
- # Diff the plan against the current graph.
- existing_id_set = set(self.graph.cells.keys())
- existing_code = {
- cid: self.graph.cells[cid].code for cid in existing_id_set
- }
+ # Diff the plan against the current document.
+ existing_id_set = set(self._document)
+ existing_code = {cell.id: cell.code for cell in self._document.cells}
plan_ids = {e.cell_id for e in plan}
# Classify each entry.
@@ -922,6 +920,7 @@ class AsyncCodeModeContext:
deletion_requests = [
DeleteCellCommand(cell_id=cid)
for cid in existing_id_set - plan_ids
+ if cid in self.graph.cells
]
cells_to_run = self._kernel.mutate_graph(
execution_requests, deletion_requests
@@ -972,9 +971,7 @@ class AsyncCodeModeContext:
async def _format_plan(self, plan: list[_PlanEntry]) -> list[_PlanEntry]:
"""Format new/changed code in the plan with the default formatter."""
- existing_code = {
- cid: self.graph.cells[cid].code for cid in self.graph.cells
- }
+ existing_code = {cell.id: cell.code for cell in self._document.cells}
to_format: dict[CellId_t, str] = {}
for entry in plan:
diff --git a/tests/_code_mode/test_context.py b/tests/_code_mode/test_context.py
index 164f39882d..7d49f57af8 100644
--- a/tests/_code_mode/test_context.py
+++ b/tests/_code_mode/test_context.py
@@ -12,6 +12,7 @@ import msgspec
import pytest
from inline_snapshot import snapshot
+from marimo._ast.cell import CellConfig
from marimo._code_mode._context import AsyncCodeModeContext
from marimo._messaging.notebook.document import (
NotebookCell,
@@ -23,17 +24,26 @@ from marimo._messaging.notification import (
)
from marimo._runtime.commands import ExecuteCellCommand
from marimo._runtime.runtime import Kernel
+from marimo._types.ids import CellId_t
@contextmanager
-def _ctx(k: Kernel) -> Generator[AsyncCodeModeContext, None, None]:
- """Build an AsyncCodeModeContext with a document snapshot from the kernel."""
- doc = NotebookDocument(
- [
- NotebookCell(id=cid, code=cell.code, name="", config=cell.config)
- for cid, cell in k.graph.cells.items()
- ]
- )
+def _ctx(
+ k: Kernel,
+ extra_doc_cells: list[NotebookCell] | None = None,
+) -> Generator[AsyncCodeModeContext, None, None]:
+ """Build an AsyncCodeModeContext with a document snapshot from the kernel.
+
+ ``extra_doc_cells`` adds cells to the document that are *not* in the
+ kernel graph, simulating cells that exist on disk but were never run.
+ """
+ cells = [
+ NotebookCell(id=cid, code=cell.code, name="", config=cell.config)
+ for cid, cell in k.graph.cells.items()
+ ]
+ if extra_doc_cells:
+ cells.extend(extra_doc_cells)
+ doc = NotebookDocument(cells)
with notebook_document_context(doc):
yield AsyncCodeModeContext(k)
@@ -65,7 +75,7 @@ class TestAddCell:
nb.run_cell(cid)
assert len(k.graph.cells) == 1
- cell = list(k.graph.cells.values())[0]
+ cell = next(iter(k.graph.cells.values()))
assert cell.code == "x = 1"
assert k.globals["x"] == 1
@@ -91,8 +101,8 @@ class TestAddCell:
async def test_add_appends_by_default(self, k: Kernel) -> None:
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 10"),
- ExecuteCellCommand(cell_id="1", code="b = 20"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 10"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 20"),
]
)
with _ctx(k) as ctx:
@@ -115,8 +125,8 @@ class TestAddCell:
async def test_add_with_after(self, k: Kernel) -> None:
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 10"),
- ExecuteCellCommand(cell_id="1", code="b = 20"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 10"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 20"),
]
)
with _ctx(k) as ctx:
@@ -126,7 +136,7 @@ class TestAddCell:
nb.create_cell("c = a + b", after="0")
ops = _tx_ops(k)
- reorder = [o for o in ops if o["type"] == "reorder-cells"][0]
+ reorder = next(o for o in ops if o["type"] == "reorder-cells")
assert reorder["cellIds"][0] == "0"
# New cell should be after "0", before "1".
assert reorder["cellIds"][2] == "1"
@@ -171,9 +181,9 @@ class TestDeleteCell:
async def test_delete_cell(self, k: Kernel) -> None:
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = 2"),
- ExecuteCellCommand(cell_id="2", code="c = 3"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 2"),
+ ExecuteCellCommand(cell_id=CellId_t("2"), code="c = 3"),
]
)
assert len(k.graph.cells) == 3
@@ -197,8 +207,8 @@ class TestDeleteCell:
"""Deleting a cell removes its defs from kernel globals."""
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = 2"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 2"),
]
)
assert k.globals["a"] == 1
@@ -214,9 +224,9 @@ class TestDeleteCell:
async def test_delete_multiple(self, k: Kernel) -> None:
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = 2"),
- ExecuteCellCommand(cell_id="2", code="c = 3"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 2"),
+ ExecuteCellCommand(cell_id=CellId_t("2"), code="c = 3"),
]
)
with _ctx(k) as ctx:
@@ -231,7 +241,7 @@ class TestDeleteCell:
class TestUpdateCell:
async def test_update_code(self, k: Kernel) -> None:
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
assert k.globals["x"] == 1
with _ctx(k) as ctx:
@@ -253,7 +263,9 @@ class TestUpdateCell:
async def test_update_cleans_stale_globals(self, k: Kernel) -> None:
"""Updating code removes old defs that are no longer defined."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1\ny = 2")])
+ await k.run(
+ [ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1\ny = 2")]
+ )
assert k.globals["x"] == 1
assert k.globals["y"] == 2
@@ -267,7 +279,7 @@ class TestUpdateCell:
async def test_update_preserves_config(self, k: Kernel) -> None:
"""Updating only code preserves the cell's existing config."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
# Set hide_code=True on the cell.
with _ctx(k) as ctx:
@@ -286,7 +298,7 @@ class TestUpdateCell:
assert k.cell_metadata["0"].config.hide_code is True
async def test_update_config_only(self, k: Kernel) -> None:
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
_clear_messages(k)
@@ -314,9 +326,9 @@ class TestCombined:
async def test_delete_and_add(self, k: Kernel) -> None:
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = 2"),
- ExecuteCellCommand(cell_id="2", code="c = 3"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 2"),
+ ExecuteCellCommand(cell_id=CellId_t("2"), code="c = 3"),
]
)
@@ -336,8 +348,8 @@ class TestCombined:
"""Delete a cell and add a replacement defining the same names."""
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = a + 1"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = a + 1"),
]
)
assert k.globals["b"] == 2
@@ -357,18 +369,18 @@ class TestCombined:
async def test_noop_batch(self, k: Kernel) -> None:
"""An empty context manager does nothing."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
_clear_messages(k)
- async with ctx as nb: # noqa: B018
+ async with ctx as nb:
pass
assert _graph_codes(k) == snapshot({"0": "x = 1"})
async def test_exception_discards_ops(self, k: Kernel) -> None:
"""If an exception occurs, queued ops are discarded."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
try:
async with ctx as nb:
@@ -383,7 +395,7 @@ class TestCombined:
async def test_rerun_without_structural_ops(self, k: Kernel) -> None:
"""run_cell without any create/edit/delete still executes."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
# Mutate the global so we can detect re-execution.
k.globals["x"] = 0
@@ -396,8 +408,8 @@ class TestCombined:
"""run_cell on an unchanged cell works even with other structural ops."""
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="x = 1"),
- ExecuteCellCommand(cell_id="1", code="y = x + 1"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="y = x + 1"),
]
)
with _ctx(k) as ctx:
@@ -411,7 +423,7 @@ class TestCombined:
async def test_run_deleted_cell_raises(self, k: Kernel) -> None:
"""Calling run_cell on a cell queued for deletion raises."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
async with ctx as nb:
nb.delete_cell("0")
@@ -433,7 +445,7 @@ class TestSummary:
async def test_edit_prints_summary(
self, k: Kernel, capsys: object
) -> None:
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
async with ctx as nb:
nb.edit_cell("0", code="x = 2")
@@ -444,7 +456,7 @@ class TestSummary:
async def test_delete_prints_summary(
self, k: Kernel, capsys: object
) -> None:
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
with _ctx(k) as ctx:
async with ctx as nb:
nb.delete_cell("0")
@@ -456,7 +468,7 @@ class TestSummary:
self, k: Kernel, capsys: object
) -> None:
with _ctx(k) as ctx:
- async with ctx as nb: # noqa: B018
+ async with ctx as nb:
pass
captured = capsys.readouterr() # type: ignore[attr-defined]
@@ -466,9 +478,9 @@ class TestSummary:
"""Full batch: create+run, edit+run, delete, create (staged), re-run."""
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = 2"),
- ExecuteCellCommand(cell_id="2", code="c = 3"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 2"),
+ ExecuteCellCommand(cell_id=CellId_t("2"), code="c = 3"),
]
)
with _ctx(k) as ctx:
@@ -511,15 +523,15 @@ class TestResolveTarget:
# "first" should come before the second cell in ordering.
ops = _tx_ops(k)
- reorder = [o for o in ops if o["type"] == "reorder-cells"][0]
+ reorder = next(o for o in ops if o["type"] == "reorder-cells")
assert len(reorder["cellIds"]) == 2
async def test_create_after_renamed_cell(self, k: Kernel) -> None:
"""Can reference a cell by its new name after edit_cell renames it."""
await k.run(
[
- ExecuteCellCommand(cell_id="0", code="a = 1"),
- ExecuteCellCommand(cell_id="1", code="b = 2"),
+ ExecuteCellCommand(cell_id=CellId_t("0"), code="a = 1"),
+ ExecuteCellCommand(cell_id=CellId_t("1"), code="b = 2"),
]
)
with _ctx(k) as ctx:
@@ -534,7 +546,7 @@ class TestResolveTarget:
# New cell should be after "0" (renamed), before "1".
ops = _tx_ops(k)
- reorder = [o for o in ops if o["type"] == "reorder-cells"][0]
+ reorder = next(o for o in ops if o["type"] == "reorder-cells")
assert reorder["cellIds"][0] == "0"
assert reorder["cellIds"][2] == "1"
@@ -634,7 +646,7 @@ class TestAutorunStaleState:
async def test_two_step_edit_then_run(self, k: Kernel) -> None:
"""edit_cell in one flush, run_cell in a separate flush should
execute the updated code."""
- await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
# Flush 1: edit only
with _ctx(k) as ctx:
@@ -649,3 +661,33 @@ class TestAutorunStaleState:
nb.run_cell("0")
assert k.globals["x"] == 42
+
+
+class TestDocumentKernelDivergence:
+ """Tests for cells that exist in the document but not in the kernel graph."""
+
+ 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."""
+ ghost = NotebookCell(
+ id=CellId_t("ghost"), code="y = 99", name="", config=CellConfig()
+ )
+ with _ctx(k, extra_doc_cells=[ghost]) as ctx:
+ async with ctx as nb:
+ nb.delete_cell("ghost")
+
+ # 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:
+ """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()
+ )
+ with _ctx(k, extra_doc_cells=[ghost]) as ctx:
+ async with ctx as nb:
+ nb.edit_cell("ghost", code="z = 42")
+ nb.run_cell("ghost")
+
+ assert k.globals["z"] == 42