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.

Retain variables as part of execution results (#11479)

Trevor Manz3e97b89a9b77
diff --git a/marimo/_core/backend.py b/marimo/_core/backend.py
index 70521815cc..3bb822761e 100644
--- a/marimo/_core/backend.py
+++ b/marimo/_core/backend.py
@@ -18,7 +18,7 @@ unnecessary if marimo adopts the core model directly.
from __future__ import annotations
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Protocol
from marimo._core import protocol
@@ -133,21 +133,31 @@ class Planned:
@dataclass(frozen=True)
-class Status:
- """An execution changed status to `running` or `completed`."""
+class Started:
+ """An execution started evaluating code."""
execution_id: protocol.ExecutionId
- status: protocol.Lifecycle
+
+
+@dataclass(frozen=True)
+class Completed:
+ """An execution finished with its final outcome and input revisions."""
+
+ execution_id: protocol.ExecutionId
+ outcome: protocol.Outcome
+ depends_on: dict[str, protocol.Revision | None] = field(
+ default_factory=dict
+ )
@dataclass(frozen=True)
class Produced:
- """Console or display output from an execution.
+ """An item produced by an execution.
Reusing an item ID replaces its previous value.
"""
- item: protocol.Console | protocol.Display
+ item: protocol.Console | protocol.Display | protocol.Variable
@dataclass(frozen=True)
@@ -177,7 +187,8 @@ Message = (
| Exited
| Scheduled
| Planned
- | Status
+ | Started
+ | Completed
| Produced
| Concluded
| Done
diff --git a/marimo/_core/executions.py b/marimo/_core/executions.py
index 0a9a785cd1..2000eb9cee 100644
--- a/marimo/_core/executions.py
+++ b/marimo/_core/executions.py
@@ -21,13 +21,14 @@ if TYPE_CHECKING:
@dataclass(frozen=True)
class CellExecution:
- """A completed cell execution with its console and display outputs.
+ """A completed cell execution with its outputs and bound variables.
Its source may be older than the cell's current source.
"""
execution: protocol.Execution
outputs: tuple[protocol.Console | protocol.Display, ...] = ()
+ variables: tuple[protocol.Variable, ...] = ()
@dataclass
@@ -44,19 +45,27 @@ class _Run:
@dataclass
class _Result:
- """A current execution and its outputs."""
+ """A current execution and its items."""
execution: protocol.Execution
outputs: dict[protocol.ItemId, protocol.Console | protocol.Display] = (
field(default_factory=dict)
)
+ variables: dict[protocol.ItemId, protocol.Variable] = field(
+ default_factory=dict
+ )
@dataclass(frozen=True)
class Removed:
"""A current record that a transition removed."""
- record_type: type[protocol.Execution | protocol.Console | protocol.Display]
+ record_type: type[
+ protocol.Execution
+ | protocol.Console
+ | protocol.Display
+ | protocol.Variable
+ ]
id: str
run_id: protocol.RunId | None
@@ -72,7 +81,11 @@ class Released:
Record = (
- protocol.Run | protocol.Execution | protocol.Console | protocol.Display
+ protocol.Run
+ | protocol.Execution
+ | protocol.Console
+ | protocol.Display
+ | protocol.Variable
)
Change = Record | Removed | Released
@@ -98,12 +111,12 @@ class Executions:
) -> Executions:
"""Restore completed executions for cells in the document.
- Execution and output IDs must be unique. Each output must belong
+ Execution and item IDs must be unique. Each item must belong
to its containing execution.
"""
known = {cell.id for cell in cells}
restored = cls()
- output_ids: set[protocol.ItemId] = set()
+ item_ids: set[protocol.ItemId] = set()
for cell_id, retained in deepcopy(dict(executions)).items():
execution = retained.execution
if execution.cell_id != cell_id:
@@ -115,6 +128,7 @@ class Executions:
f"result for cell {cell_id} not in the document"
)
outputs = sorted(retained.outputs, key=lambda item: item.seq)
+ items = [*outputs, *retained.variables]
if execution.status != "completed":
raise ValueError(
"initial results must be completed executions"
@@ -123,19 +137,17 @@ class Executions:
raise ValueError("initial results must have an outcome")
if execution.id in restored._results:
raise ValueError("initial execution ids must be unique")
- if any(item.execution_id != execution.id for item in outputs):
- raise ValueError(
- "result outputs must belong to their execution"
- )
- if len({item.id for item in outputs}) != len(outputs):
- raise ValueError("result output ids must be unique")
- if output_ids.intersection(item.id for item in outputs):
- raise ValueError(
- "result output ids must be unique across cells"
- )
- output_ids.update(item.id for item in outputs)
+ if any(item.execution_id != execution.id for item in items):
+ raise ValueError("result items must belong to their execution")
+ if len({item.id for item in items}) != len(items):
+ raise ValueError("result item ids must be unique")
+ if item_ids.intersection(item.id for item in items):
+ raise ValueError("result item ids must be unique across cells")
+ item_ids.update(item.id for item in items)
restored._results[execution.id] = _Result(
- execution, {item.id: item for item in outputs}
+ execution,
+ {item.id: item for item in outputs},
+ {item.id: item for item in retained.variables},
)
restored._cells[cell_id] = execution.id
return restored
@@ -153,7 +165,11 @@ class Executions:
if current is None or current.execution.status != "completed":
return None
return deepcopy(
- CellExecution(current.execution, tuple(current.outputs.values()))
+ CellExecution(
+ current.execution,
+ tuple(current.outputs.values()),
+ tuple(current.variables.values()),
+ )
)
def run(self, run_id: protocol.RunId) -> protocol.Run | None:
@@ -180,7 +196,10 @@ class Executions:
*(
item
for result in self._results.values()
- for item in result.outputs.values()
+ for item in (
+ *result.outputs.values(),
+ *result.variables.values(),
+ )
),
]
)
@@ -253,13 +272,37 @@ class Executions:
)
case be.Planned(run_id=run_id, executions=planned):
changes.extend(self._observe_planned(run_id, planned, runtime))
- case be.Status(execution_id=execution_id, status=status):
- changes.extend(self._observe_status(execution_id, status))
+ case be.Started(execution_id=execution_id):
+ changes.extend(self._observe_started(execution_id))
+ case be.Completed(
+ execution_id=execution_id,
+ outcome=outcome,
+ depends_on=depends_on,
+ ):
+ if (
+ result := self._results.get(execution_id)
+ ) is not None and result.execution.status != "completed":
+ changes.append(
+ self._set_execution(
+ replace(
+ result.execution,
+ status="completed",
+ outcome=outcome,
+ depends_on=dict(depends_on),
+ completed_at=utcnow(),
+ )
+ )
+ )
case be.Produced(item=item):
if (
result := self._results.get(item.execution_id)
) is not None:
- result.outputs[item.id] = item
+ if isinstance(item, protocol.Variable):
+ if result.execution.cell_id is None:
+ return []
+ result.variables[item.id] = item
+ else:
+ result.outputs[item.id] = item
changes.append(item)
case be.Concluded(execution_id=execution_id, outcome=outcome):
if (
@@ -402,33 +445,24 @@ class Executions:
)
return changes
- def _observe_status(
- self, execution_id: protocol.ExecutionId, status: protocol.Lifecycle
+ def _observe_started(
+ self, execution_id: protocol.ExecutionId
) -> list[Change]:
changes: list[Change] = []
result = self._results.get(execution_id)
if result is None:
return []
execution = result.execution
- if execution.status in (status, "completed"):
+ if execution.status != "queued":
return []
now = utcnow()
- if status == "running":
- execution = replace(execution, status=status, started_at=now)
- assert execution.run_id is not None
- run = self._runs[execution.run_id].run
- if run.status == "queued":
- changes.append(
- self._set_run(
- replace(run, status="running", started_at=now)
- )
- )
- elif status == "completed":
- if execution.outcome is None:
- raise ValueError("a completed execution must have an outcome")
- execution = replace(execution, status=status, completed_at=now)
- else:
- execution = replace(execution, status=status)
+ execution = replace(execution, status="running", started_at=now)
+ assert execution.run_id is not None
+ run = self._runs[execution.run_id].run
+ if run.status == "queued":
+ changes.append(
+ self._set_run(replace(run, status="running", started_at=now))
+ )
changes.append(self._set_execution(execution))
return changes
@@ -492,7 +526,7 @@ class Executions:
def _remove_execution(
self, execution_id: protocol.ExecutionId
) -> list[Removed]:
- """Remove an execution and its outputs, outputs first."""
+ """Remove an execution and its items, items first."""
result = self._results[execution_id]
run_id = result.execution.run_id
changes: list[Removed] = [
@@ -501,7 +535,7 @@ class Executions:
item.id,
run_id,
)
- for item in result.outputs.values()
+ for item in (*result.outputs.values(), *result.variables.values())
]
changes.append(Removed(protocol.Execution, execution_id, run_id))
self._forget_result(execution_id)
diff --git a/marimo/_server/host/translate.py b/marimo/_server/host/translate.py
index 16c5f0caff..e9413c4916 100644
--- a/marimo/_server/host/translate.py
+++ b/marimo/_server/host/translate.py
@@ -34,7 +34,6 @@ class _Execution:
id: protocol.ExecutionId
cell_id: str
ended: bool = False
- reported: bool = False
outcome: protocol.Outcome = field(
default_factory=lambda: protocol.ValueOutcome(None)
)
@@ -86,7 +85,7 @@ class RunTranslator:
if execution is None or execution.ended:
return []
if notification.phase == "running":
- return [be.Status(execution.id, "running")]
+ return [be.Started(execution.id)]
return self._conclude(execution)
execution = self._executions.get(notification.execution_id or "")
@@ -114,13 +113,12 @@ class RunTranslator:
mimetype=str(output.mimetype), data=text
)
)
- execution.reported = True
out.append(be.Concluded(execution.id, execution.outcome))
return out
def _plan(self, notification: ExecutionNotification) -> be.Planned:
execution = _Execution(
- protocol.ExecutionId(f"{self.run_id}-{len(self._executions) + 1}"),
+ protocol.ExecutionId(notification.execution_id),
notification.cell_id,
)
self._executions[notification.execution_id] = execution
@@ -150,7 +148,6 @@ class RunTranslator:
) -> list[be.Message]:
if execution.ended:
return []
- previous = execution.outcome
exceptions = [
e
for e in execution.errors
@@ -163,10 +160,7 @@ class RunTranslator:
elif interrupted or execution.errors:
execution.outcome = protocol.InterruptedOutcome()
execution.ended = True
- out: list[be.Message] = []
- if not execution.reported or execution.outcome != previous:
- out.append(be.Concluded(execution.id, execution.outcome))
- out.append(be.Status(execution.id, "completed"))
+ out: list[be.Message] = [be.Completed(execution.id, execution.outcome)]
self._failed |= isinstance(execution.outcome, protocol.ErrorOutcome)
self.interrupted |= isinstance(
execution.outcome, protocol.InterruptedOutcome
@@ -190,7 +184,7 @@ class RunTranslator:
out.append(
self._plan(
ExecutionNotification(
- execution_id="unsubmitted",
+ execution_id=f"{self.run_id}-unsubmitted",
cell_id=SCRATCH_CELL_ID,
run_id=self.run_id,
phase="planned",
diff --git a/tests/_core/conftest.py b/tests/_core/conftest.py
index e1241c41bf..824f048312 100644
--- a/tests/_core/conftest.py
+++ b/tests/_core/conftest.py
@@ -275,20 +275,19 @@ def plan(
),
)
)
- notebook.observe(be.Status(execution, "running"))
+ notebook.observe(be.Started(execution))
return execution
def finish(notebook: Notebook, execution: protocol.ExecutionId) -> None:
notebook.observe(
- be.Concluded(
+ be.Completed(
execution,
protocol.ValueOutcome(
protocol.DisplayData(mimetype="text/plain", data="value")
),
)
)
- notebook.observe(be.Status(execution, "completed"))
def fold(events: list[RoutedEvent], initial: dict | None = None) -> dict:
diff --git a/tests/_core/test_executions.py b/tests/_core/test_executions.py
index b61e2a2b80..e50e6eb63d 100644
--- a/tests/_core/test_executions.py
+++ b/tests/_core/test_executions.py
@@ -3,7 +3,10 @@
from __future__ import annotations
+import dataclasses
+
import pytest
+from dirty_equals import IsDatetime
from msgspec.structs import replace
from marimo._core import backend as be, protocol
@@ -66,9 +69,8 @@ def complete(
execution = protocol.ExecutionId(eid)
changes: list[Change] = []
for msg in (
- be.Status(execution, "running"),
- be.Concluded(execution, outcome or protocol.ValueOutcome(None)),
- be.Status(execution, "completed"),
+ be.Started(execution),
+ be.Completed(execution, outcome or protocol.ValueOutcome(None)),
):
changes.extend(executions.observe(msg, RUNTIME))
return changes
@@ -89,23 +91,92 @@ def describe(changes: list[Change]) -> list[str]:
return out
+def variable(execution_id: str = "e1") -> protocol.Variable:
+ return protocol.Variable(
+ execution_id=protocol.ExecutionId(execution_id),
+ id=protocol.ItemId("v1"),
+ name="x",
+ preview="1",
+ revision=protocol.Revision(protocol.ExecutionId(execution_id), 0),
+ type="int",
+ )
+
+
[email protected]("started", [False, True])
+def test_completion_freezes_the_outcome_and_input_revisions(
+ started: bool,
+) -> None:
+ executions = Executions()
+ executions.begin(run("r1"), "request")
+ [queued] = executions.observe(planned("r1", ("e1", "a")), RUNTIME)
+ assert isinstance(queued, protocol.Execution)
+ eid = queued.id
+ if started:
+ executions.observe(be.Started(eid), RUNTIME)
+ executions.observe(
+ be.Concluded(eid, protocol.ValueOutcome(None)), RUNTIME
+ )
+ inputs = {
+ "x": protocol.Revision(protocol.ExecutionId("producer"), 2),
+ "print": None,
+ }
+ completed = be.Completed(eid, protocol.InterruptedOutcome(), inputs)
+ assert executions.observe(completed, RUNTIME) == [
+ replace(
+ queued,
+ status="completed",
+ outcome=protocol.InterruptedOutcome(),
+ depends_on=inputs,
+ started_at=IsDatetime() if started else None,
+ completed_at=IsDatetime(),
+ )
+ ]
+ retained = executions.current(protocol.CellId("a"))
+ inputs.clear()
+ for message in (
+ be.Started(eid),
+ be.Concluded(eid, protocol.ValueOutcome(None)),
+ be.Completed(eid, protocol.ValueOutcome(None)),
+ ):
+ assert executions.observe(message, RUNTIME) == []
+ assert executions.current(protocol.CellId("a")) == retained
+
+
def test_a_cell_result_outlives_its_run_until_the_next_replaces_it() -> None:
executions = Executions()
executions.begin(run("r1"), "first")
executions.observe(planned("r1", ("e1", "a")), RUNTIME)
+ bound = variable()
+ assert executions.observe(be.Produced(bound), RUNTIME) == [bound]
complete(executions, "e1")
done = executions.observe(
be.Done(protocol.RunId("r1"), "succeeded"), RUNTIME
)
assert describe(done) == ["run r1"]
assert executions.active() is None
- assert describe(executions._records()) == ["run r1", "execution e1"]
+ assert describe(executions._records()) == [
+ "run r1",
+ "execution e1",
+ "variable v1",
+ ]
assert executions.request_id(protocol.RunId("r1")) == "first"
assert executions.snapshot_request(protocol.RunId("r1")) is None
+ before = executions.current(protocol.CellId("a"))
+ assert before is not None
+ updated = replace(
+ bound, preview="2", revision=replace(bound.revision, update=1)
+ )
+ assert executions.observe(be.Produced(updated), RUNTIME) == [updated]
+ assert executions.current(protocol.CellId("a")) == dataclasses.replace(
+ before, variables=(updated,)
+ )
+ assert before.variables == (bound,)
+
executions.begin(run("r2"), "second")
replaced = executions.observe(planned("r2", ("e2", "a")), RUNTIME)
assert describe(replaced) == [
+ "variable.removed v1",
"execution.removed e1",
"released r1",
"execution e2",
@@ -113,12 +184,14 @@ def test_a_cell_result_outlives_its_run_until_the_next_replaces_it() -> None:
executions.release(protocol.RunId("r1"))
assert executions.run(protocol.RunId("r1")) is None
assert describe(executions._records()) == ["run r2", "execution e2"]
+ assert executions.observe(be.Produced(updated), RUNTIME) == []
def test_scratch_work_leaves_with_its_run() -> None:
executions = Executions()
executions.begin(run("r1"), "request")
executions.observe(planned("r1", ("e1", None)), RUNTIME)
+ assert executions.observe(be.Produced(variable()), RUNTIME) == []
produced = executions.observe(
be.Produced(
protocol.Console(
@@ -148,9 +221,7 @@ def test_a_run_completes_only_after_its_executions_and_reports_failures() -> (
executions = Executions()
executions.begin(run("r1"), "request")
executions.observe(planned("r1", ("e1", "a")), RUNTIME)
- executions.observe(
- be.Status(protocol.ExecutionId("e1"), "running"), RUNTIME
- )
+ executions.observe(be.Started(protocol.ExecutionId("e1")), RUNTIME)
with pytest.raises(ValueError, match="unfinished"):
executions.observe(be.Done(protocol.RunId("r1"), "succeeded"), RUNTIME)
complete(executions, "e1", protocol.InterruptedOutcome())
@@ -167,9 +238,7 @@ def test_interrupting_completes_everything_in_flight() -> None:
executions.begin(run("r1"), "request")
executions.observe(planned("r1", ("e1", "a"), ("e2", None)), RUNTIME)
for eid in ("e1", "e2"):
- executions.observe(
- be.Status(protocol.ExecutionId(eid), "running"), RUNTIME
- )
+ executions.observe(be.Started(protocol.ExecutionId(eid)), RUNTIME)
changes = executions.interrupt()
assert describe(changes) == ["execution e1", "execution e2", "run r1"]
assert all(
@@ -211,10 +280,7 @@ def test_scheduling_needs_a_runtime_but_stray_updates_do_not() -> None:
with pytest.raises(NoRuntime):
executions.observe(be.Scheduled(protocol.RunId("r1")), None)
assert (
- executions.observe(
- be.Status(protocol.ExecutionId("e1"), "running"), None
- )
- == []
+ executions.observe(be.Started(protocol.ExecutionId("e1")), None) == []
)
scheduled = executions.observe(be.Scheduled(protocol.RunId("r1")), RUNTIME)
assert describe(scheduled) == ["run r1"]
@@ -249,9 +315,7 @@ def test_removing_a_cell_drops_its_result_or_disowns_active_work() -> None:
executions.begin(run("r1"), "request")
executions.observe(planned("r1", ("e1", "a"), ("e2", "b")), RUNTIME)
complete(executions, "e1")
- executions.observe(
- be.Status(protocol.ExecutionId("e2"), "running"), RUNTIME
- )
+ executions.observe(be.Started(protocol.ExecutionId("e2")), RUNTIME)
assert describe(executions.remove_cell(protocol.CellId("a"))) == [
"execution.removed e1"
]
@@ -268,7 +332,9 @@ def test_removing_a_cell_drops_its_result_or_disowns_active_work() -> None:
def test_restored_executions_are_current_until_replaced() -> None:
document = Document([DocumentCell("c1", "42")], {})
- retained = retained_execution(run_id="old-run")
+ retained = dataclasses.replace(
+ retained_execution(run_id="old-run"), variables=(variable("restored"),)
+ )
executions = Executions.restore(
{protocol.CellId("c1"): retained}, cells=document
)
@@ -276,11 +342,13 @@ def test_restored_executions_are_current_until_replaced() -> None:
assert describe(executions._records()) == [
"execution restored",
"console restored-output",
+ "variable v1",
]
executions.begin(run("r1"), "request")
changes = executions.observe(planned("r1", ("e1", "c1")), RUNTIME)
assert describe(changes) == [
"console.removed restored-output",
+ "variable.removed v1",
"execution.removed restored",
"execution e1",
]
@@ -297,6 +365,8 @@ def test_restored_executions_are_current_until_replaced() -> None:
("no-outcome", "outcome"),
("output-owner", "belong to their execution"),
("duplicate-output", "unique"),
+ ("variable-owner", "belong to their execution"),
+ ("duplicate-item", "unique"),
],
)
def test_restore_rejects_inconsistent_executions(
@@ -324,6 +394,15 @@ def test_restore_rejects_inconsistent_executions(
execution,
(replace(outputs[0], execution_id=protocol.ExecutionId("other")),),
)
+ elif invalid == "variable-owner":
+ retained = dataclasses.replace(
+ retained, variables=(variable("other"),)
+ )
+ elif invalid == "duplicate-item":
+ retained = dataclasses.replace(
+ retained,
+ variables=(replace(variable("restored"), id=outputs[0].id),),
+ )
else:
retained = CellExecution(execution, outputs * 2)
with pytest.raises(ValueError, match=message):
diff --git a/tests/_core/test_host.py b/tests/_core/test_host.py
index abf69684bb..c6059928c4 100644
--- a/tests/_core/test_host.py
+++ b/tests/_core/test_host.py
@@ -560,7 +560,7 @@ async def test_expiration_converges_for_live_replay_and_snapshot(
)
live.clear()
notebook.observe(be.Produced(item))
- notebook.observe(be.Status(execution, "running"))
+ notebook.observe(be.Started(execution))
notebook.observe(be.Done(run.id, "succeeded"))
assert live == []
@@ -749,7 +749,7 @@ async def test_completed_scratch_runs_belong_to_the_host() -> None:
notebook.observe(be.Done(run.id, "succeeded"))
# The notebook lets go without a word; the host still shows the run.
assert fold(notebook._snapshot()) == {}
- assert names(live) == ["execution", "execution", "run"]
+ assert names(live) == ["execution", "run"]
assert set(fold(host.events(notebook_id=notebook.id))) == {
("run", run.id),
("execution", execution),
diff --git a/tests/_core/test_notebook.py b/tests/_core/test_notebook.py
index fe7a0e23b1..08e11d28d5 100644
--- a/tests/_core/test_notebook.py
+++ b/tests/_core/test_notebook.py
@@ -97,7 +97,7 @@ def scratch(notebook: Notebook, run_id: str = "run-1") -> Run:
(be.PlannedExecution(ExecutionId("e1"), None, "9f", "3 * 3"),),
)
)
- notebook.observe(be.Status(ExecutionId("e1"), "running"))
+ notebook.observe(be.Started(ExecutionId("e1")))
return run
@@ -288,8 +288,7 @@ class TestStop:
running(notebook, backend)
run = scratch(notebook)
notebook.observe(be.Produced(console("o1", "e1", "x")))
- notebook.observe(be.Concluded(ExecutionId("e1"), value("")))
- notebook.observe(be.Status(ExecutionId("e1"), "completed"))
+ notebook.observe(be.Completed(ExecutionId("e1"), value("")))
notebook.observe(be.Done(run.id, "succeeded"))
log.clear()
@@ -510,8 +509,7 @@ class TestRun:
) -> None:
running(notebook, backend)
run = scratch(notebook)
- notebook.observe(be.Concluded(ExecutionId("e1"), value("9")))
- notebook.observe(be.Status(ExecutionId("e1"), "completed"))
+ notebook.observe(be.Completed(ExecutionId("e1"), value("9")))
notebook.observe(be.Done(run.id, "succeeded"))
cursor = host.events()[-1].event.id
live: list[RoutedEvent] = []
@@ -564,13 +562,12 @@ class TestRun:
(be.PlannedExecution(ExecutionId("e7"), None, "9f", "3 * 3"),),
)
)
- notebook.observe(be.Status(ExecutionId("e7"), "running"))
+ notebook.observe(be.Started(ExecutionId("e7")))
notebook.observe(be.Concluded(ExecutionId("e7"), value("working")))
notebook.observe(be.Produced(console("o3", "e7", "2\n")))
notebook.observe(
- be.Concluded(ExecutionId("e7"), value("working<pre>9</pre>"))
+ be.Completed(ExecutionId("e7"), value("working<pre>9</pre>"))
)
- notebook.observe(be.Status(ExecutionId("e7"), "completed"))
notebook.observe(be.Done(run.id, "succeeded"))
assert notebook.runtime is not None
@@ -583,7 +580,6 @@ class TestRun:
"execution",
"console",
"execution",
- "execution",
"run",
]
assert set(request_ids(log)) == {key}
@@ -611,7 +607,6 @@ class TestRun:
("queued", None),
("running", None),
("running", value("working")),
- ("running", value("working<pre>9</pre>")),
("completed", value("working<pre>9</pre>")),
]
retained = [
@@ -739,7 +734,7 @@ class TestRun:
(be.PlannedExecution(ExecutionId("e9"), None, "x", "3 * 3"),),
)
)
- notebook.observe(be.Status(ExecutionId("e9"), "running"))
+ notebook.observe(be.Started(ExecutionId("e9")))
notebook.observe(be.Produced(console("o9", "e9", "boo")))
notebook.observe(be.Done(RunId("ghost"), "succeeded"))
assert log == []
@@ -754,8 +749,7 @@ class TestRunCompletion:
) -> None:
running(notebook, backend)
original = scratch(notebook)
- notebook.observe(be.Concluded(ExecutionId("e1"), value("9")))
- notebook.observe(be.Status(ExecutionId("e1"), "completed"))
+ notebook.observe(be.Completed(ExecutionId("e1"), value("9")))
notebook.observe(be.Done(original.id, "succeeded"))
log.clear()
notebook.observe(be.Scheduled(RunId("later"), ExecutionId("e1")))
@@ -806,8 +800,7 @@ class TestRunCompletion:
run = scratch(notebook)
with pytest.raises(ValueError, match="unfinished"):
notebook.observe(be.Done(run.id, "succeeded"))
- notebook.observe(be.Concluded(ExecutionId("e1"), value("9")))
- notebook.observe(be.Status(ExecutionId("e1"), "completed"))
+ notebook.observe(be.Completed(ExecutionId("e1"), value("9")))
notebook.observe(
be.Planned(
run.id,
@@ -822,9 +815,9 @@ class TestRunCompletion:
),
)
)
- notebook.observe(be.Status(ExecutionId("child"), "running"))
+ notebook.observe(be.Started(ExecutionId("child")))
notebook.observe(
- be.Concluded(
+ be.Completed(
ExecutionId("child"),
ErrorOutcome(
Error(
@@ -840,7 +833,6 @@ class TestRunCompletion:
),
)
)
- notebook.observe(be.Status(ExecutionId("child"), "completed"))
# Even an empty execution queue is not an authoritative end to scheduling.
current = notebook.executions.run(run.id)
assert current is not None
@@ -859,7 +851,7 @@ class TestRunCompletion:
assert final.run.status == "completed"
log.clear()
# Released scratch records cannot reopen or acquire more output.
- notebook.observe(be.Status(ExecutionId("e1"), "running"))
+ notebook.observe(be.Started(ExecutionId("e1")))
notebook.observe(be.Concluded(ExecutionId("e1"), value("changed")))
notebook.observe(be.Done(run.id, "succeeded"))
notebook.observe(
@@ -898,8 +890,7 @@ class TestInterrupt:
with pytest.raises(NotFound):
notebook.interrupt("nope")
run = scratch(notebook)
- notebook.observe(be.Concluded(ExecutionId("e1"), value("9")))
- notebook.observe(be.Status(ExecutionId("e1"), "completed"))
+ notebook.observe(be.Completed(ExecutionId("e1"), value("9")))
notebook.observe(be.Done(run.id, "succeeded"))
with pytest.raises(NotFound):
notebook.interrupt(run.id)
@@ -922,7 +913,7 @@ class TestCellResults:
first.id, (be.PlannedExecution(old, "cell", "h1", None),)
)
)
- notebook.observe(be.Status(old, "running"))
+ notebook.observe(be.Started(old))
failed = ErrorOutcome(
Error(
kind="exception",
@@ -935,8 +926,7 @@ class TestCellResults:
related=(),
)
)
- notebook.observe(be.Concluded(old, failed))
- notebook.observe(be.Status(old, "completed"))
+ notebook.observe(be.Completed(old, failed))
item = Console(
id=ItemId("o1"),
execution_id=old,
@@ -968,8 +958,7 @@ class TestCellResults:
if isinstance(event.event.data, ExecutionMessage)
]
assert visible == [new]
- notebook.observe(be.Concluded(new, ValueOutcome(None)))
- notebook.observe(be.Status(new, "completed"))
+ notebook.observe(be.Completed(new, ValueOutcome(None)))
if same_run:
with pytest.raises(ValueError, match="successful run"):
notebook.observe(be.Done(second.id, "succeeded"))
diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.py
index 404a1a844d..2df6b1b45c 100644
--- a/tests/_server/host/test_host_api.py
+++ b/tests/_server/host/test_host_api.py
@@ -1000,9 +1000,8 @@ async def test_run_filter_over_http(harness: Harness) -> None:
)
)
notebook.observe(
- be.Concluded(protocol.ExecutionId("e1"), protocol.ValueOutcome(None))
+ be.Completed(protocol.ExecutionId("e1"), protocol.ValueOutcome(None))
)
- notebook.observe(be.Status(protocol.ExecutionId("e1"), "completed"))
notebook.observe(be.Done(first.id, "succeeded"))
notebook.run("2", request_id="second")
response, task, disconnect = request(
@@ -1088,7 +1087,7 @@ async def test_scratch_runs_over_http(harness: Harness) -> None:
notebook.observe(
be.Planned(run_id, (be.PlannedExecution("e1", None, "hash", "3 * 3"),))
)
- notebook.observe(be.Status("e1", "running"))
+ notebook.observe(be.Started("e1"))
running = await finished(app, "POST", runs, headers=key("r1"), body=body)
assert running.json()["status"] == "running"
reused = await finished(
@@ -1110,8 +1109,7 @@ async def test_scratch_runs_over_http(harness: Harness) -> None:
assert interrupted.status == 202
assert harness.backend().calls[-1] == ("interrupt", run_id)
- notebook.observe(be.Concluded("e1", protocol.InterruptedOutcome()))
- notebook.observe(be.Status("e1", "completed"))
+ notebook.observe(be.Completed("e1", protocol.InterruptedOutcome()))
notebook.observe(be.Done(run_id, "interrupted"))
completed = await finished(app, "POST", runs, headers=key("r1"), body=body)
assert completed.json()["status"] == "completed"
@@ -1152,15 +1150,14 @@ async def test_request_receipts_expire_independently_of_results(
notebook.observe(
be.Planned(run_id, (be.PlannedExecution(eid, cell_id, "hash", "42"),))
)
- notebook.observe(be.Status(eid, "running"))
+ notebook.observe(be.Started(eid))
harness.now = 100
active = await finished(
harness.app, "POST", path, headers=key("run"), body=body
)
assert active.json()["id"] == run_id
assert active.json()["status"] == "running"
- notebook.observe(be.Concluded(eid, protocol.ValueOutcome(None)))
- notebook.observe(be.Status(eid, "completed"))
+ notebook.observe(be.Completed(eid, protocol.ValueOutcome(None)))
notebook.observe(be.Done(run_id, "succeeded"))
harness.now = 129
harness.host.expire()
diff --git a/tests/_server/host/test_host_backend.py b/tests/_server/host/test_host_backend.py
index 1decfb1f34..12155db15f 100644
--- a/tests/_server/host/test_host_backend.py
+++ b/tests/_server/host/test_host_backend.py
@@ -208,7 +208,7 @@ async def test_interrupt_waits_for_the_kernel_result(
await observed.wait_for(be.Done)
assert session.interrupts == 1
- concluded = [m for m in observed.messages if isinstance(m, be.Concluded)]
+ concluded = [m for m in observed.messages if isinstance(m, be.Completed)]
assert concluded[-1].outcome == (
protocol.InterruptedOutcome()
if takes_effect
@@ -296,8 +296,8 @@ async def test_external_scratch_is_adopted_before_compilation(
run_id="browser",
)
)
- assert observed.messages[-1] == be.Status(execution.id, "running")
- assert observed.kinds() == ["Scheduled", "Planned", "Status"]
+ assert observed.messages[-1] == be.Started(execution.id)
+ assert observed.kinds() == ["Scheduled", "Planned", "Started"]
session.broadcast(CompletedRunNotification(run_id="browser"))
assert observed.messages[-1] == be.Done(
protocol.RunId("browser"), "succeeded"
@@ -331,7 +331,7 @@ async def test_canceling_a_queued_run_preserves_other_execution(
await observed.wait_for(be.Done)
assert session.interrupts == 0
[outcome] = [
- m.outcome for m in observed.messages if isinstance(m, be.Concluded)
+ m.outcome for m in observed.messages if isinstance(m, be.Completed)
]
assert outcome == protocol.InterruptedOutcome()
@@ -357,7 +357,7 @@ async def test_submission_failure_is_an_error(
b.run(protocol.RunId("failed"), "42")
await observed.wait_for(be.Done)
[outcome] = [
- m.outcome for m in observed.messages if isinstance(m, be.Concluded)
+ m.outcome for m in observed.messages if isinstance(m, be.Completed)
]
assert outcome == protocol.ErrorOutcome(
protocol.Error(
diff --git a/tests/_server/host/test_host_execution.py b/tests/_server/host/test_host_execution.py
index 03f1ea6101..a4df7380e7 100644
--- a/tests/_server/host/test_host_execution.py
+++ b/tests/_server/host/test_host_execution.py
@@ -747,7 +747,7 @@ async def test_nested_evaluations_keep_the_scratch_result_and_skipped_child(
assert root["outcome"]["kind"] == "value"
assert "42" in root["outcome"]["display"]["data"]
assert all(
- e["outcome"]["kind"] == "error"
+ e["outcome"]["kind"] == "error" and e["caused_by"] == root["id"]
for e in executions
if e["cell_id"] is not None
)
diff --git a/tests/_server/host/test_host_record.py b/tests/_server/host/test_host_record.py
index 1824a03b7f..200fa5641a 100644
--- a/tests/_server/host/test_host_record.py
+++ b/tests/_server/host/test_host_record.py
@@ -184,8 +184,7 @@ async def test_idle_host_expires_results_and_stops_maintenance(
notebook.observe(
be.Planned(run_id, (be.PlannedExecution(eid, None, "hash", "42"),))
)
- notebook.observe(be.Concluded(eid, protocol.ValueOutcome(None)))
- notebook.observe(be.Status(eid, "completed"))
+ notebook.observe(be.Completed(eid, protocol.ValueOutcome(None)))
notebook.observe(be.Done(run_id, "succeeded"))
removed = asyncio.Event()
host.subscribe(
diff --git a/tests/_server/host/test_host_translate.py b/tests/_server/host/test_host_translate.py
index 1d285ecaf2..4bdb921cde 100644
--- a/tests/_server/host/test_host_translate.py
+++ b/tests/_server/host/test_host_translate.py
@@ -8,12 +8,13 @@ from dirty_equals import IsStr
from inline_snapshot import snapshot
from marimo._core.backend import (
+ Completed,
Concluded,
Done,
Planned,
PlannedExecution,
Produced,
- Status,
+ Started,
)
from marimo._core.document import code_hash
from marimo._core.protocol import (
@@ -88,7 +89,7 @@ def test_a_run_that_prints_and_shows_a_value() -> None:
run_id=RunId("r1"),
executions=(
PlannedExecution(
- id=ExecutionId("r1-1"),
+ id=ExecutionId("scratch"),
cell_id=None,
code_hash=code_hash("3 * 3"),
code="3 * 3",
@@ -96,10 +97,10 @@ def test_a_run_that_prints_and_shows_a_value() -> None:
),
),
),
- Status(execution_id=ExecutionId("r1-1"), status="running"),
+ Started(execution_id=ExecutionId("scratch")),
Produced(
item=Console(
- execution_id=ExecutionId("r1-1"),
+ execution_id=ExecutionId("scratch"),
id=IsStr(),
name="stdout",
seq=0,
@@ -107,14 +108,19 @@ def test_a_run_that_prints_and_shows_a_value() -> None:
)
),
Concluded(
- execution_id=ExecutionId("r1-1"),
+ execution_id=ExecutionId("scratch"),
outcome=ValueOutcome(
display=DisplayData(
data="<pre>9</pre>", mimetype="text/html"
)
),
),
- Status(execution_id=ExecutionId("r1-1"), status="completed"),
+ Completed(
+ execution_id=ExecutionId("scratch"),
+ outcome=ValueOutcome(
+ DisplayData(data="<pre>9</pre>", mimetype="text/html")
+ ),
+ ),
Done(run_id=RunId("r1"), outcome="succeeded"),
]
)
@@ -155,8 +161,8 @@ def test_an_exception_ends_the_run_in_error() -> None:
assert t.finish() == snapshot(
[
- Concluded(
- execution_id=ExecutionId("r1-1"),
+ Completed(
+ execution_id=ExecutionId("scratch"),
outcome=ErrorOutcome(
error=Error(
source="marimo",
@@ -176,7 +182,6 @@ ZeroDivisionError: division by zero
)
),
),
- Status(execution_id=ExecutionId("r1-1"), status="completed"),
Done(run_id=RunId("r1"), outcome="failed"),
]
)
@@ -196,11 +201,10 @@ def test_an_interruption_ends_the_run_interrupted(
assert t.finish() == snapshot(
[
- Concluded(
- execution_id=ExecutionId("r1-1"),
+ Completed(
+ execution_id=ExecutionId("scratch"),
outcome=InterruptedOutcome(),
),
- Status(execution_id=ExecutionId("r1-1"), status="completed"),
Done(run_id=RunId("r1"), outcome="interrupted"),
]
)
@@ -224,7 +228,7 @@ def test_console_items_number_in_order_and_lose_highlighting() -> None:
[
Produced(
item=Console(
- execution_id=ExecutionId("r1-1"),
+ execution_id=ExecutionId("scratch"),
id=IsStr(),
name="stdout",
seq=0,
@@ -233,7 +237,7 @@ def test_console_items_number_in_order_and_lose_highlighting() -> None:
),
Produced(
item=Console(
- execution_id=ExecutionId("r1-1"),
+ execution_id=ExecutionId("scratch"),
id=IsStr(),
name="stderr",
seq=1,
@@ -267,7 +271,7 @@ def test_late_output_keeps_its_evaluation_when_a_cell_runs_again() -> None:
execution_id=identity,
)
)
- assert ids[0] != ids[1]
+ assert ids == ["first", "second"]
assert translator.finish() == [Done(RunId("r1"), "succeeded")]
[output] = translator.feed(
CellNotification(
@@ -281,6 +285,22 @@ def test_late_output_keeps_its_evaluation_when_a_cell_runs_again() -> None:
assert output.item.execution_id == ids[0]
+def test_unsubmitted_runs_have_distinct_executions() -> None:
+ executions = []
+ for run_id in ("r1", "r2"):
+ translator = RunTranslator(RunId(run_id), "print('never ran')")
+ translator.interrupted = True
+ planned, completed, done = translator.finish()
+ assert isinstance(planned, Planned)
+ [execution] = planned.executions
+ executions.append(execution.id)
+ assert (completed, done) == (
+ Completed(execution.id, InterruptedOutcome()),
+ Done(RunId(run_id), "interrupted"),
+ )
+ assert len(set(executions)) == 2
+
+
@pytest.mark.parametrize(
("error", "outcome_type", "run_outcome"),
[
@@ -327,7 +347,6 @@ def test_skipped_cells_report_their_error_after_becoming_idle(
)
)
messages.extend(translator.finish())
- assert isinstance(messages[0], Concluded)
+ assert isinstance(messages[0], Completed)
assert isinstance(messages[0].outcome, outcome_type)
- assert messages[1] == Status(ExecutionId("r1-1"), "completed")
- assert messages[2] == Done(RunId("r1"), run_outcome) # type: ignore[arg-type]
+ assert messages[1] == Done(RunId("r1"), run_outcome) # type: ignore[arg-type]