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 cells stuck as "needs run" after backend-initiated execution (#8794)

Trevor Manz825296ce2f7e
diff --git a/marimo/_code_mode/_context.py b/marimo/_code_mode/_context.py
index ab47a3d920..773ab57d58 100644
--- a/marimo/_code_mode/_context.py
+++ b/marimo/_code_mode/_context.py
@@ -341,6 +341,18 @@ class AsyncCodeModeContext:
await self._apply_ops(ops, cells_to_run)
elif cells_to_run:
# run_cell was called without any structural ops — just re-run.
+ # Notify the frontend that these cells are no longer stale so
+ # it clears edited/lastCodeRun (covers the two-step pattern:
+ # edit_cell in one flush, run_cell in a separate flush).
+ valid = cells_to_run & set(self.graph.cells.keys())
+ if valid:
+ self.notify(
+ UpdateCellCodesNotification(
+ cell_ids=list(valid),
+ codes=[self.graph.cells[cid].code for cid in valid],
+ code_is_stale=False,
+ )
+ )
await self._kernel._run_cells(cells_to_run)
# Flush queued UI updates as a single batch.
@@ -988,7 +1000,17 @@ class AsyncCodeModeContext:
# Notify frontend of all changes (code and config-only).
# Cells not queued for execution are marked as stale.
# Config-only changes are never stale (the code didn't change).
+ #
+ # In autorun mode, when the caller explicitly ran at least one
+ # cell, also include cells_to_run from mutate_graph (reactive
+ # descendants) so the agent sees their execution synchronously
+ # and the frontend isn't left showing stale for cells that are
+ # about to execute. We only expand when explicit_run is
+ # non-empty to preserve the "suggestion" behavior: creating
+ # cells without run_cell leaves them stale/unexecuted.
_run_set = explicit_run or set()
+ if _run_set and self._kernel.reactive_execution_mode == "autorun":
+ _run_set = _run_set | cells_to_run
_code_entry_ids = {e.cell_id for e in code_entries}
all_entries = code_entries + config_entries
by_stale: dict[bool, list[_PlanEntry]] = {}
@@ -1044,8 +1066,8 @@ class AsyncCodeModeContext:
self.notify(UpdateCellIdsNotification(cell_ids=target_order))
- # Only run cells explicitly queued via run_cell(), filtered to
- # cells that still exist after structural ops have been applied.
+ # Run queued cells (explicit run_cell + autorun descendants),
+ # filtered to cells that still exist after structural ops.
if _run_set:
cells_to_run = _run_set & set(self.graph.cells.keys())
if cells_to_run:
diff --git a/tests/_code_mode/test_context.py b/tests/_code_mode/test_context.py
index e6ff97a588..6c9c644039 100644
--- a/tests/_code_mode/test_context.py
+++ b/tests/_code_mode/test_context.py
@@ -577,3 +577,88 @@ class TestInstallPackages:
assert installed["pandas"] == ""
assert installed["polars>=0.20"] == ""
assert installed["numpy==1.26"] == ""
+
+
+class TestAutorunStaleState:
+ """In autorun mode, downstream cells should not be marked stale."""
+
+ async def test_dependent_chain_only_root_run(self, k: Kernel) -> None:
+ """Running the root cell should mark reactive descendants as
+ non-stale in autorun mode so the frontend doesn't show them
+ as 'needs run'."""
+ ctx = AsyncCodeModeContext(k)
+ _clear_messages(k)
+
+ async with ctx as nb:
+ a = nb.create_cell("x = 1")
+ b = nb.create_cell("y = x + 1")
+ nb.run_cell(a)
+
+ # Both cells should have executed.
+ assert k.globals["x"] == 1
+ assert k.globals["y"] == 2
+
+ # The notification for b should be code_is_stale=False because
+ # autorun will execute it.
+ code_notifs = msgspec.to_builtins(_code_notifs(k))
+ stale_flags = {
+ cid: n["code_is_stale"]
+ for n in code_notifs
+ for cid in n["cell_ids"]
+ }
+ a_id = list(k.graph.cells.keys())[0]
+ b_id = list(k.graph.cells.keys())[1]
+ assert stale_flags[str(a_id)] is False
+ assert stale_flags[str(b_id)] is False
+
+ async def test_dependent_chain_lazy_mode(
+ self, lazy_kernel: Kernel
+ ) -> None:
+ """In lazy mode, only the explicitly run cell should be non-stale.
+ Downstream cells stay stale."""
+ k = lazy_kernel
+ ctx = AsyncCodeModeContext(k)
+ _clear_messages(k)
+
+ async with ctx as nb:
+ a = nb.create_cell("x = 1")
+ b = nb.create_cell("y = x + 1")
+ nb.run_cell(a)
+
+ assert k.globals["x"] == 1
+ # b should NOT have executed in lazy mode.
+ assert "y" not in k.globals
+
+ code_notifs = msgspec.to_builtins(_code_notifs(k))
+ stale_flags = {
+ cid: n["code_is_stale"]
+ for n in code_notifs
+ for cid in n["cell_ids"]
+ }
+ a_id = list(k.graph.cells.keys())[0]
+ b_id = list(k.graph.cells.keys())[1]
+ assert stale_flags[str(a_id)] is False
+ assert stale_flags[str(b_id)] is True
+
+ async def test_two_step_edit_then_run(self, k: Kernel) -> None:
+ """edit_cell in one flush, run_cell in a separate flush should
+ send code_is_stale=false so the frontend clears the stale state."""
+ await k.run([ExecuteCellCommand(cell_id="0", code="x = 1")])
+ ctx = AsyncCodeModeContext(k)
+
+ # Flush 1: edit only
+ async with ctx as nb:
+ nb.edit_cell("0", code="x = 42")
+
+ _clear_messages(k)
+
+ # Flush 2: run only
+ ctx2 = AsyncCodeModeContext(k)
+ async with ctx2 as nb:
+ nb.run_cell("0")
+
+ assert k.globals["x"] == 42
+
+ code_notifs = msgspec.to_builtins(_code_notifs(k))
+ assert len(code_notifs) == 1
+ assert code_notifs[0]["code_is_stale"] is False