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
Propagate deleted bindings to host snapshots (#11516)
Trevor Manzcfdbb7145677
diff --git a/frontend/src/core/websocket/useNotebookSession.tsx b/frontend/src/core/websocket/useNotebookSession.tsxindex e5464d91da..6b66552bd8 100644--- a/frontend/src/core/websocket/useNotebookSession.tsx+++ b/frontend/src/core/websocket/useNotebookSession.tsx@@ -263,13 +263,18 @@ export function useNotebookSession(opts: {filterStorageFromVariables(variableNames);return;case "variable-values":- setMetadata(- msg.data.variables.map((v) => ({+ setMetadata([+ ...(msg.data.removed ?? []).map((v) => ({+ name: v.name as VariableName,+ dataType: undefined,+ value: undefined,+ })),+ ...msg.data.variables.map((v) => ({name: v.name as VariableName,dataType: v.datatype,value: v.value,})),- );+ ]);return;case "alert":toast({diff --git a/marimo/_core/backend.py b/marimo/_core/backend.pyindex 3bb822761e..86fe196659 100644--- a/marimo/_core/backend.py+++ b/marimo/_core/backend.py@@ -160,6 +160,14 @@ class Produced:item: protocol.Console | protocol.Display | protocol.Variable+@dataclass(frozen=True)+class Unbound:+ """A variable removed from the namespace, identified by its producer."""++ execution_id: protocol.ExecutionId+ name: str++@dataclass(frozen=True)class Concluded:"""An execution outcome, final when the execution completes."""@@ -190,6 +198,7 @@ Message = (| Started| Completed| Produced+ | Unbound| Concluded| Done)diff --git a/marimo/_core/executions.py b/marimo/_core/executions.pyindex 2000eb9cee..31eae1b29b 100644--- a/marimo/_core/executions.py+++ b/marimo/_core/executions.py@@ -304,6 +304,18 @@ class Executions:else:result.outputs[item.id] = itemchanges.append(item)+ case be.Unbound(execution_id=execution_id, name=name):+ if (result := self._results.get(execution_id)) is not None:+ for item in tuple(result.variables.values()):+ if item.name == name:+ del result.variables[item.id]+ changes.append(+ Removed(+ protocol.Variable,+ item.id,+ result.execution.run_id,+ )+ )case be.Concluded(execution_id=execution_id, outcome=outcome):if (result := self._results.get(execution_id)diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.pyindex 94fd05595f..42755c35db 100644--- a/marimo/_messaging/notification.py+++ b/marimo/_messaging/notification.py@@ -732,15 +732,24 @@ class VariablesNotification(Notification, tag="variables"):variables: list[VariableDeclarationNotification]+class VariableRemoval(BaseStruct):+ """A binding removed from the runtime's namespace."""++ name: str+ execution_id: str++class VariableValuesNotification(Notification, tag="variable-values"):"""Current variable values.Attributes:variables: Variables with current values and types.+ removed: Bindings that no longer exist, identified by their producer."""name: ClassVar[str] = "variable-values"variables: list[VariableValue]+ removed: list[VariableRemoval] = msgspec.field(default_factory=list)class DatasetsNotification(Notification, tag="datasets"):diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex da3a743a2e..74eb5d7bab 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -66,6 +66,7 @@ from marimo._messaging.notification import (RemoveUIElementsNotification,UIElementMessageNotification,VariableDeclarationNotification,+ VariableRemoval,VariablesNotification,VariableValue,VariableValuesNotification,@@ -1670,8 +1671,7 @@ class Kernel:if cell.import_workspace.is_import_blockelse set())- for name in cell.deleted_refs - self.globals.keys():- self._bindings.pop(name, None)+ self._unbind_variables(cell.deleted_refs - self.globals.keys())for name in python_names.intersection(self.globals):value = self.globals[name]previous = (@@ -1700,6 +1700,19 @@ class Kernel:)self._publish_variable_previews(previews)+ def _unbind_variables(self, names: Iterable[str]) -> None:+ # Called under _state_lock, like _publish_bindings.+ removed = [+ VariableRemoval(name=name, execution_id=binding.execution_id)+ for name in sorted(names)+ if (binding := self._bindings.pop(name, None)) is not None+ ]+ if removed:+ broadcast_notification(+ VariableValuesNotification(variables=[], removed=removed),+ self.stream,+ )+def _publish_bindings(self, names: Iterable[str]) -> dict[str, tuple[object, Binding | None]]:diff --git a/marimo/_server/host/backend.py b/marimo/_server/host/backend.pyindex a9770cfda9..65201dabfb 100644--- a/marimo/_server/host/backend.py+++ b/marimo/_server/host/backend.py@@ -394,6 +394,13 @@ class SessionBackend(SessionConsumer):def _handle_notification(self, message: NotificationMessage) -> None:if isinstance(message, VariableValuesNotification):+ for removed in message.removed:+ self._observe(+ be.Unbound(+ protocol.ExecutionId(removed.execution_id),+ removed.name,+ )+ )for value in message.variables:if value.execution_id is None or value.revision is None:continuediff --git a/marimo/_session/state/session_view.py b/marimo/_session/state/session_view.pyindex ff58e6744a..65413adf38 100644--- a/marimo/_session/state/session_view.py+++ b/marimo/_session/state/session_view.py@@ -376,6 +376,13 @@ class SessionView:)elif isinstance(notification, VariableValuesNotification):+ for removed in notification.removed:+ value = self.variable_values.get(removed.name)+ if (+ value is not None+ and value.execution_id == removed.execution_id+ ):+ del self.variable_values[removed.name]for value in notification.variables:self.variable_values[value.name] = valuediff --git a/packages/openapi/api.yaml b/packages/openapi/api.yamlindex f59792e026..3055ca6684 100644--- a/packages/openapi/api.yaml+++ b/packages/openapi/api.yaml@@ -6306,6 +6306,18 @@ components:VariableName:format: variable-nametype: string+ VariableRemoval:+ description: A binding removed from the runtime's namespace.+ properties:+ execution_id:+ type: string+ name:+ type: string+ required:+ - name+ - execution_id+ title: VariableRemoval+ type: objectVariableValue:description: "Variable value and type for variables panel.\n\n Attributes:\n\\ name: Variable name.\n value: String representation of value.\n\@@ -6339,11 +6351,17 @@ components:type: objectVariableValuesNotification:description: "Current variable values.\n\n Attributes:\n variables:\- \ Variables with current values and types."+ \ Variables with current values and types.\n removed: Bindings that\+ \ no longer exist, identified by their producer."properties:op:enum:- variable-values+ removed:+ default: []+ items:+ $ref: '#/components/schemas/VariableRemoval'+ type: arrayvariables:items:$ref: '#/components/schemas/VariableValue'diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.tsindex 3fa0cef537..f0c639cdec 100644--- a/packages/openapi/src/api.ts+++ b/packages/openapi/src/api.ts@@ -7607,6 +7607,14 @@ export interface components {};/** Format: variable-name */VariableName: TypedString<"VariableName">;+ /**+ * VariableRemoval+ * @description A binding removed from the runtime's namespace.+ */+ VariableRemoval: {+ execution_id: string;+ name: string;+ };/*** VariableValue* @description Variable value and type for variables panel.@@ -7631,10 +7639,13 @@ export interface components {** Attributes:* variables: Variables with current values and types.+ * removed: Bindings that no longer exist, identified by their producer.*/VariableValuesNotification: {/** @enum {unknown} */op: "variable-values";+ /** @default [] */+ removed?: components["schemas"]["VariableRemoval"][];variables: components["schemas"]["VariableValue"][];};/**diff --git a/tests/_server/host/test_host_staleness.py b/tests/_server/host/test_host_staleness.pyindex 741d8dc477..f312a4c5e6 100644--- a/tests/_server/host/test_host_staleness.py+++ b/tests/_server/host/test_host_staleness.py@@ -262,6 +262,51 @@ async def test_sql_results_follow_their_binding_revisions(assert await c.outdated() == {}+ "deletion", ["del x", "del x\nraise ValueError('failed')"]+)+async def test_deleted_bindings_outdate_existing_readers(+ tmp_path: Path, deletion: str+) -> None:+ async with client(+ tmp_path,+ on_cell_change="lazy",+ x="x = 1\nkept = 2\nx",+ y="y = x + 1",+ delete=deletion,+ ) as c:+ await c.run("x", "y")+ before = await c.snapshot()+ x = next(v for v in before.variables.values() if v.name == "x")+ assert await c.outdated() == {}++ await c.run(+ "delete", outcome="failed" if "raise" in deletion else "succeeded"+ )+ after = await c.snapshot()+ assert after.variables == {+ key: value+ for key, value in before.variables.items()+ if key != x.id+ }+ assert after.latest(c.cells["x"]) == before.latest(c.cells["x"])+ assert after.latest(c.cells["y"]) == before.latest(c.cells["y"])+ assert (await c.outdated())["y"] == "replaced"+ removed = next(+ i+ for i, r in enumerate(c.events.records)+ if r.get("event") == "variable.removed" and r["data"]["id"] == x.id+ )+ completed = next(+ i+ for i, r in enumerate(c.events.records)+ if r.get("event") == "execution"+ and r["data"]["execution"]["cell_id"] == c.cells["delete"]+ and r["data"]["execution"]["status"] == "completed"+ )+ assert removed < completed++async def test_autorun_leaves_every_result_current(tmp_path: Path) -> None:async with client(tmp_path, on_cell_change="autorun", x="x = 1", y="y = x + 1"diff --git a/tests/_session/state/test_session_view.py b/tests/_session/state/test_session_view.pyindex ea09f2f426..5c5cb83d23 100644--- a/tests/_session/state/test_session_view.py+++ b/tests/_session/state/test_session_view.py@@ -48,6 +48,7 @@ from marimo._messaging.notification import (StartupLogsNotification,StartupProgressNotification,VariableDeclarationNotification,+ VariableRemoval,VariablesNotification,VariableValue,VariableValuesNotification,@@ -215,6 +216,31 @@ def test_session_view_variable_values(session_view: SessionView) -> None:assert list(variables_names) == ["var2"]+def test_variable_removals_only_remove_the_named_binding(+ session_view: SessionView,+) -> None:+ value = VariableValue(+ name="x", value="1", datatype="int", execution_id="new"+ )+ session_view.add_notification(+ VariableValuesNotification(variables=[value])+ )+ for execution_id in ("old", "new", "new"):+ session_view.add_raw_notification(+ serialize_kernel_message(+ VariableValuesNotification(+ variables=[],+ removed=[+ VariableRemoval(name="x", execution_id=execution_id)+ ],+ )+ )+ )+ assert session_view.variable_values == (+ {"x": value} if execution_id == "old" else {}+ )++def test_ui_values(session_view: SessionView) -> None:session_view.add_control_request(UpdateUIElementCommand.from_ids_and_values([("test_ui", 123)])