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.

Track binding revisions in the kernel (#11480)

Trevor Manzda94270dcca0
diff --git a/marimo/_ast/cell.py b/marimo/_ast/cell.py
index 2e486df6ec..b838cf2158 100644
--- a/marimo/_ast/cell.py
+++ b/marimo/_ast/cell.py
@@ -27,6 +27,7 @@ if TYPE_CHECKING:
from types import CodeType
from marimo._ast.app import InternalApp
+ from marimo._core.protocol import Revision
from marimo._messaging.types import Stream
from marimo._output.hypertext import Html
@@ -140,6 +141,14 @@ class RunResultStatus:
exception: Exception | None = None
[email protected](frozen=True)
+class ImportedValue:
+ """An imported value whose revision may survive reevaluation."""
+
+ value: object
+ revision: Revision
+
+
@dataclasses.dataclass
class ImportWorkspace:
"""A workspace for runtimes to use to manage a cell's imports."""
@@ -148,6 +157,10 @@ class ImportWorkspace:
is_import_block: bool = False
# Defs that have been imported by the runtime
imported_defs: set[Name] = dataclasses.field(default_factory=set)
+ # Candidates carried across invalidation, consumed by the next evaluation.
+ carried_values: dict[Name, ImportedValue] = dataclasses.field(
+ default_factory=dict
+ )
def _is_coroutine(code: CodeType | None) -> bool:
diff --git a/marimo/_messaging/context.py b/marimo/_messaging/context.py
index 071043c7d4..667c9c0cad 100644
--- a/marimo/_messaging/context.py
+++ b/marimo/_messaging/context.py
@@ -3,11 +3,14 @@ from __future__ import annotations
import uuid
from contextvars import ContextVar
-from dataclasses import dataclass
-from typing import cast
+from dataclasses import dataclass, field
+from typing import TYPE_CHECKING, cast
from marimo._runtime.commands import HTTPRequest
+if TYPE_CHECKING:
+ from marimo._runtime.execution import ExecutionAttempt
+
RunId_t = str
RUN_ID_CTX = ContextVar[RunId_t | None]("run_id")
@@ -19,6 +22,9 @@ class ExecutionTag:
id: str
cell_id: str
run_id: str | None = None
+ attempt: ExecutionAttempt | None = field(
+ default=None, compare=False, repr=False
+ )
EXECUTION_CTX = ContextVar[ExecutionTag | None]("execution", default=None)
@@ -54,6 +60,7 @@ class execution_id_context:
cell_id: str
id: str | None = None
+ attempt: ExecutionAttempt | None = None
def __enter__(self) -> None:
self.token = EXECUTION_CTX.set(
@@ -61,6 +68,7 @@ class execution_id_context:
self.id or str(uuid.uuid4()),
self.cell_id,
RUN_ID_CTX.get(None),
+ self.attempt,
)
)
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.py
index 5f31cb422a..e6537c970c 100644
--- a/marimo/_messaging/notification.py
+++ b/marimo/_messaging/notification.py
@@ -17,6 +17,7 @@ import msgspec
from marimo import _loggers as loggers
from marimo._ast.app_config import _AppConfig
from marimo._ast.cell import CellConfig, RuntimeStateType
+from marimo._core.protocol import Revision
from marimo._data._external_storage.models import (
StorageEntry,
StorageNamespace,
@@ -101,6 +102,7 @@ class ExecutionNotification(Notification, tag="execution"):
phase: Literal["planned", "running", "completed"]
code: str | None = None
caused_by: str | None = None
+ depends_on: dict[str, Revision | None] | None = None
class CellNotification(Notification, tag="cell-op"):
@@ -714,6 +716,8 @@ class VariableValue(BaseStruct):
name: str
value: str | None
datatype: str | None
+ execution_id: str | None = None
+ revision: Revision | None = None
class VariablesNotification(Notification, tag="variables"):
diff --git a/marimo/_runtime/dataflow/graph.py b/marimo/_runtime/dataflow/graph.py
index 942e46ad0f..b4576f34e0 100644
--- a/marimo/_runtime/dataflow/graph.py
+++ b/marimo/_runtime/dataflow/graph.py
@@ -4,7 +4,7 @@
from __future__ import annotations
import threading
-from dataclasses import dataclass, field
+from dataclasses import dataclass, field, replace
from typing import TYPE_CHECKING, Literal
from marimo import _loggers
@@ -364,7 +364,7 @@ class DirectedGraph(GraphTopology):
return processed - refs
def copy(self, filename: str | None = None) -> DirectedGraph:
- """Return a deep copy of the graph by recompiling all cells.
+ """Recompile the graph while preserving runtime import state.
This is mainly useful in the case where recompilation must be done
due to a dynamically changing notebook, where the line cache must be
@@ -380,11 +380,10 @@ class DirectedGraph(GraphTopology):
cell_id=cid,
filename=filename,
)
- # Carry over import data manually
- imported_defs = old_cell.import_workspace.imported_defs
- is_import_block = old_cell.import_workspace.is_import_block
- cell.import_workspace.imported_defs = imported_defs
- cell.import_workspace.is_import_block = is_import_block
+ # Recompilation changes source locations, not runtime imports.
+ cell = replace(
+ cell, import_workspace=old_cell.import_workspace
+ )
# Reregister
graph.register_cell(cid, cell)
return graph
diff --git a/marimo/_runtime/execution.py b/marimo/_runtime/execution.py
index 261b87d3b5..7d59dc82e5 100644
--- a/marimo/_runtime/execution.py
+++ b/marimo/_runtime/execution.py
@@ -9,9 +9,11 @@ completion, so a background thread cannot write into a later attempt.
from __future__ import annotations
from dataclasses import dataclass, field
+from threading import RLock
from typing import Literal
from uuid import uuid4
+from marimo._core.protocol import Revision
from marimo._messaging.context import (
EXECUTION_CTX,
RUN_ID_CTX,
@@ -31,6 +33,10 @@ class ExecutionAttempt:
id: str = field(default_factory=lambda: str(uuid4()))
run_id: str | None = field(default_factory=lambda: RUN_ID_CTX.get(None))
completed: bool = False
+ depends_on: dict[str, Revision | None] | None = None
+ _lock: RLock = field(
+ default_factory=RLock, init=False, repr=False, compare=False
+ )
@classmethod
def plan(cls, cell_id: CellId_t, code: str) -> ExecutionAttempt:
@@ -54,16 +60,29 @@ class ExecutionAttempt:
def activate(self) -> execution_id_context:
"""Attribute output in this context to the attempt."""
- return execution_id_context(self.cell_id, self.id)
+ return execution_id_context(self.cell_id, self.id, self)
def start(self) -> None:
self._notify("running")
def finish(self) -> None:
"""Complete the attempt after its output and errors are reported."""
- if not self.completed:
+ with self._lock:
+ if self.completed:
+ return
self.completed = True
- self._notify("completed")
+ self._notify("completed")
+
+ def advance_inputs(self, revisions: dict[str, Revision]) -> None:
+ """Keep this attempt current against the values it updated itself."""
+ with self._lock:
+ if self.completed or self.depends_on is None:
+ return
+ self.depends_on.update(
+ (name, revision)
+ for name, revision in revisions.items()
+ if name in self.depends_on
+ )
def _notify(self, phase: Literal["running", "completed"]) -> None:
broadcast_notification(
@@ -72,5 +91,6 @@ class ExecutionAttempt:
cell_id=self.cell_id,
run_id=self.run_id,
phase=phase,
+ depends_on=self.depends_on if phase == "completed" else None,
)
)
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.py
index 6bda79b641..e233edc3a7 100644
--- a/marimo/_runtime/runner/cell_runner.py
+++ b/marimo/_runtime/runner/cell_runner.py
@@ -64,8 +64,10 @@ LOGGER = marimo_logger()
if TYPE_CHECKING:
from collections import deque
+ from collections.abc import Callable
from marimo._ast.cell import CellImpl
+ from marimo._core.protocol import Revision
from marimo._runtime.runner.hook_context import PostExecutionHookContext
from marimo._runtime.runner.hooks import NotebookCellHooks
from marimo._runtime.state import State
@@ -156,6 +158,8 @@ class Runner:
execution_context: ExecutionContextManager | None = None,
user_config: MarimoConfig | None = None,
executions: dict[CellId_t, ExecutionAttempt] | None = None,
+ get_inputs: Callable[[CellImpl], dict[str, Revision | None] | None]
+ | None = None,
):
self.graph = graph
self.debugger = debugger
@@ -164,6 +168,7 @@ class Runner:
self._hooks = hooks
self.user_config = user_config
self._executions = executions if executions is not None else {}
+ self._get_inputs = get_inputs
# runtime globals
self.glbls = glbls
@@ -808,6 +813,8 @@ class Runner:
execution = self._executions.pop(
cell_id, None
) or ExecutionAttempt.plan(cell_id, self.graph.cells[cell_id].code)
+ if self._get_inputs is not None:
+ execution.depends_on = self._get_inputs(self.graph.cells[cell_id])
with execution.activate():
execution.start()
try:
@@ -932,6 +939,10 @@ class Runner:
for finish_hook in self._hooks.on_finish_hooks:
finish_hook(finish_ctx)
for execution in self._executions.values():
+ if self._get_inputs is not None:
+ execution.depends_on = self._get_inputs(
+ self.graph.cells[execution.cell_id]
+ )
execution.finish()
async def _dispatch_runnable(
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index 43251befa7..915a6cf877 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -27,7 +27,6 @@ from marimo._messaging.notification import (
DatasetsNotification,
DataSourceConnectionsNotification,
StorageNamespacesNotification,
- VariableValuesNotification,
)
from marimo._messaging.notification_utils import (
CellNotificationUtils,
@@ -38,7 +37,6 @@ from marimo._messaging.tracebacks import (
format_exception_message,
write_traceback,
)
-from marimo._messaging.variables import create_variable_value
from marimo._output import formatting
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._runtime.context.types import (
@@ -122,27 +120,6 @@ def _set_run_result_status(
cell.set_run_result_status("success")
-@kernel_tracer.start_as_current_span("broadcast_variables")
-def _broadcast_variables(
- cell: CellImpl,
- ctx: PostExecutionHookContext,
- run_result: cell_runner.RunResult,
-) -> None:
- if not ctx.should_broadcast_data:
- return
-
- del run_result
- values = [
- create_variable_value(
- name=variable,
- value=(ctx.glbls.get(variable, None)),
- )
- for variable in cell.defs
- ]
- if values:
- broadcast_notification(VariableValuesNotification(variables=values))
-
-
@kernel_tracer.start_as_current_span("broadcast_datasets")
def _broadcast_datasets(
cell: CellImpl,
@@ -559,7 +536,6 @@ POST_EXECUTION_HOOKS: list[PostExecutionHook] = [
_store_reference_to_output,
_store_state_reference,
_issue_exception_side_effect,
- _broadcast_variables,
_broadcast_datasets,
_broadcast_data_source_connection,
_broadcast_duckdb_datasource,
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 0e56d3d1ce..58c5e72c90 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -24,7 +24,13 @@ from typing import (
from uuid import uuid4
from marimo import _loggers
-from marimo._ast.cell import CellConfig, CellImpl, RuntimeStateType
+from marimo._ast.cell import (
+ CellConfig,
+ CellImpl,
+ ImportedValue,
+ ImportWorkspace,
+ RuntimeStateType,
+)
from marimo._ast.compiler import _build_source_position_map, compile_cell
from marimo._ast.errors import ImportStarError
from marimo._ast.names import SETUP_CELL_NAME
@@ -35,11 +41,13 @@ from marimo._config.config import (
MarimoConfig,
OnCellChangeType,
)
+from marimo._core.protocol import ExecutionId, Revision
from marimo._dependencies.dependencies import DependencyManager
from marimo._entrypoints.registry import EntryPointRegistry
from marimo._lint.validate_graph import check_for_errors
from marimo._messaging.cell_output import CellChannel
from marimo._messaging.context import (
+ EXECUTION_CTX,
RUN_ID_CTX,
run_id_context,
)
@@ -155,6 +163,11 @@ from marimo._secrets.load_dotenv import (
)
from marimo._session.model import SessionMode
from marimo._session.queue import QueueType
+from marimo._sql.bindings import (
+ SQLBinding,
+ existing_sql_bindings,
+ get_sql_bindings,
+)
from marimo._sql.engines.types import (
EngineCatalog,
QueryEngine,
@@ -178,13 +191,14 @@ from marimo._utils.signals import restore_signals
from marimo._utils.typed_connection import TypedConnection
if TYPE_CHECKING:
- from collections.abc import Callable, Iterator, Sequence
+ from collections.abc import Callable, Iterable, Iterator, Sequence
from types import ModuleType
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._runtime.virtual_file import VirtualFileStorageType
LOGGER = _loggers.marimo_logger()
+_NOT_IMPORTED = object()
_KERNEL_LIFESPAN_REGISTRY = EntryPointRegistry[Lifespan[None]](
"marimo.kernel.lifespan",
@@ -454,6 +468,17 @@ class CellMetadata:
code: str | None = None
[email protected](frozen=True)
+class Binding:
+ """The execution that owns a notebook name and its value's revision."""
+
+ cell_id: CellId_t
+ execution_id: str
+ revision: Revision
+ imported: object = _NOT_IMPORTED
+ sql: SQLBinding | None = None
+
+
def _get_attached_catalogs() -> set[str]:
"""Fetch the names of all catalogs currently attached to DuckDB."""
import duckdb
@@ -581,10 +606,12 @@ class Kernel:
if path not in sys.path:
sys.path.insert(0, path)
- self._hooks = hooks
+ self._hooks = hooks.copy()
+ self._hooks.add_post_execution(self._bind_variables)
self._globals_lock = threading.RLock()
self._state_lock = threading.RLock()
+ self._bindings: dict[str, Binding] = {}
self._out_of_band_worker_started = False
self.debugger = debugger_override
@@ -1011,13 +1038,26 @@ class Kernel:
else:
carried_imports = []
- if previous_cell is not None:
- LOGGER.debug("Deleting cell %s", cell_id)
- previous_children = self._deactivate_cell(cell_id)
+ with self._state_lock:
+ carried_values = (
+ self._imported_values(previous_cell)
+ if previous_cell is not None and carried_imports
+ else {}
+ )
+ if previous_cell is not None:
+ LOGGER.debug("Deleting cell %s", cell_id)
+ previous_children = self._deactivate_cell(cell_id)
error = self._try_registering_cell(
cell_id, code, carried_imports=carried_imports, stale=stale
)
+ if error is None:
+ workspace = self.graph.cells[cell_id].import_workspace
+ workspace.carried_values = {
+ name: carried_values[name]
+ for name in workspace.imported_defs
+ if name in carried_values
+ }
if error is not None and previous_cell is not None:
# The frontend keeps the cell around in the case of a
# registration error; let the FE know the cell is no longer
@@ -1071,62 +1111,83 @@ class Kernel:
attached_catalogs = _get_attached_catalogs()
return attached_catalogs
- for name, variable_data in variables.items():
- # Take the last definition of the variable
- variable = variable_data[-1]
- if name in exclude_defs:
- continue
-
- if variable.kind == "table" and DependencyManager.duckdb.has():
- import duckdb
+ with self._state_lock:
+ for name, variable_data in variables.items():
+ # Take the last definition of the variable
+ variable = variable_data[-1]
+ if name in exclude_defs:
+ continue
- # We only drop in-memory tables: we don't want to drop tables
- # on databases!
- qualified = _in_memory_qualified_name(
- variable, name, get_attached_catalogs
+ binding_name = (
+ variable.sql_ref.qualified_name
+ if variable.sql_ref is not None
+ else name
)
- if qualified is not None:
- try:
- duckdb.execute(f"DROP TABLE IF EXISTS {qualified}")
- except Exception as e:
- LOGGER.warning("Failed to drop table %s: %s", name, e)
- elif variable.kind == "view" and DependencyManager.duckdb.has():
- import duckdb
+ self._bindings.pop(binding_name, None)
- # We only drop in-memory views for the same reason.
- qualified = _in_memory_qualified_name(
- variable, name, get_attached_catalogs
- )
- if qualified is not None:
- try:
- duckdb.execute(f"DROP VIEW IF EXISTS {qualified}")
- except Exception as e:
- LOGGER.warning("Failed to drop view %s: %s", name, e)
- elif variable.kind == "catalog" and DependencyManager.duckdb.has():
- import duckdb
+ if variable.kind == "table" and DependencyManager.duckdb.has():
+ import duckdb
- try:
- identifier = quote_sql_identifier(name)
- duckdb.execute(f"DETACH DATABASE IF EXISTS {identifier}")
- except Exception as e:
- LOGGER.warning("Failed to detach catalog %s: %s", name, e)
- else:
- if name in self.globals:
- del self.globals[name]
-
- # Restore module-level __doc__ so it doesn't fall
- # through to builtins.__doc__
- if name == "__doc__":
- self.globals["__doc__"] = (
- self.app_metadata.docstring
- or "Created for the marimo kernel."
+ # We only drop in-memory tables: we don't want to drop tables
+ # on databases!
+ qualified = _in_memory_qualified_name(
+ variable, name, get_attached_catalogs
)
+ if qualified is not None:
+ try:
+ duckdb.execute(f"DROP TABLE IF EXISTS {qualified}")
+ except Exception as e:
+ LOGGER.warning(
+ "Failed to drop table %s: %s", name, e
+ )
+ elif (
+ variable.kind == "view" and DependencyManager.duckdb.has()
+ ):
+ import duckdb
- if (
- "__annotations__" in self.globals
- and name in self.globals["__annotations__"]
+ # We only drop in-memory views for the same reason.
+ qualified = _in_memory_qualified_name(
+ variable, name, get_attached_catalogs
+ )
+ if qualified is not None:
+ try:
+ duckdb.execute(f"DROP VIEW IF EXISTS {qualified}")
+ except Exception as e:
+ LOGGER.warning(
+ "Failed to drop view %s: %s", name, e
+ )
+ elif (
+ variable.kind == "catalog"
+ and DependencyManager.duckdb.has()
):
- del self.globals["__annotations__"][name]
+ import duckdb
+
+ try:
+ identifier = quote_sql_identifier(name)
+ duckdb.execute(
+ f"DETACH DATABASE IF EXISTS {identifier}"
+ )
+ except Exception as e:
+ LOGGER.warning(
+ "Failed to detach catalog %s: %s", name, e
+ )
+ else:
+ if name in self.globals:
+ del self.globals[name]
+
+ # Restore module-level __doc__ so it doesn't fall
+ # through to builtins.__doc__
+ if name == "__doc__":
+ self.globals["__doc__"] = (
+ self.app_metadata.docstring
+ or "Created for the marimo kernel."
+ )
+
+ if (
+ "__annotations__" in self.globals
+ and name in self.globals["__annotations__"]
+ ):
+ del self.globals["__annotations__"][name]
def _invalidate_cell_state(
self,
@@ -1142,6 +1203,7 @@ class Kernel:
"""
cell = self.graph.cells[cell_id]
cell.import_workspace.imported_defs = set()
+ cell.import_workspace.carried_values.clear()
missing_modules_before_deletion = (
self.module_registry.missing_modules()
)
@@ -1520,6 +1582,202 @@ class Kernel:
# modules are marked as stale
self.graph.set_stale(cell_ids, prune_imports=True)
+ def _read_inputs(
+ self, cell: CellImpl
+ ) -> dict[str, Revision | None] | None:
+ if get_context().is_embedded() or get_mode() != "edit":
+ return None
+ with self._state_lock:
+ sql_bindings = (
+ [
+ binding.sql
+ for binding in self._bindings.values()
+ if binding.sql is not None
+ ]
+ if cell.sql_refs
+ else []
+ )
+ existing_sql = set(existing_sql_bindings(sql_bindings))
+ with self._state_lock:
+ inputs: dict[str, Revision | None] = {}
+ for name in sorted(cell.refs):
+ sql_ref = cell.sql_refs.get(name)
+ matches = (
+ {
+ bound_name: binding
+ for bound_name, binding in self._bindings.items()
+ if binding.sql is not None
+ and binding.sql in existing_sql
+ and binding.sql.matches(sql_ref)
+ }
+ if sql_ref is not None
+ else {}
+ )
+ inputs.update(
+ (name, binding.revision)
+ for name, binding in matches.items()
+ )
+ if not any(
+ binding.sql is not None
+ and binding.sql.kind in ("table", "view")
+ for binding in matches.values()
+ ):
+ binding = self._bindings.get(name)
+ inputs[name] = (
+ binding.revision
+ if binding is not None
+ and binding.sql is None
+ and name in self.globals
+ else None
+ )
+ return inputs
+
+ def _imported_values(self, cell: CellImpl) -> dict[str, ImportedValue]:
+ with self._state_lock:
+ return {
+ name: ImportedValue(binding.imported, binding.revision)
+ for name in cell.import_workspace.imported_defs
+ if name in self.globals
+ and (binding := self._bindings.get(name)) is not None
+ and binding.cell_id == cell.cell_id
+ and binding.imported is not _NOT_IMPORTED
+ }
+
+ def _bind_variables(
+ self,
+ cell: CellImpl,
+ ctx: hook_context.PostExecutionHookContext,
+ result: cell_runner.RunResult,
+ ) -> None:
+ del result
+ carried = cell.import_workspace.carried_values
+ cell.import_workspace.carried_values = {}
+ if not ctx.should_broadcast_data:
+ return
+ current = EXECUTION_CTX.get()
+ assert current is not None
+ sql_bindings = get_sql_bindings(cell)
+ python_names = {
+ name
+ for name in cell.defs
+ if cell.variable_data[name][-1].language == "python"
+ }
+ with self._state_lock:
+ # A nested run may have produced a newer import while this cell
+ # was queued. Its live binding takes precedence over the carry.
+ previous_imports = carried | self._imported_values(cell)
+ imports = (
+ {item.definition for item in cell.imports}
+ if cell.import_workspace.is_import_block
+ else set()
+ )
+ for name in cell.deleted_refs - self.globals.keys():
+ self._bindings.pop(name, None)
+ for name in python_names.intersection(self.globals):
+ value = self.globals[name]
+ previous = (
+ previous_imports.get(name) if name in imports else None
+ )
+ revision = (
+ previous.revision
+ if previous is not None and previous.value is value
+ else Revision(ExecutionId(current.id), 0)
+ )
+ self._bindings[name] = Binding(
+ cell.cell_id,
+ current.id,
+ revision,
+ value if name in imports else _NOT_IMPORTED,
+ )
+ for sql_binding in sql_bindings:
+ self._bindings[sql_binding.name] = Binding(
+ cell.cell_id,
+ current.id,
+ Revision(ExecutionId(current.id), 0),
+ sql=sql_binding,
+ )
+ previews = self._publish_bindings(
+ python_names | {binding.name for binding in sql_bindings}
+ )
+ self._publish_variable_previews(previews)
+
+ def _publish_bindings(
+ self, names: Iterable[str]
+ ) -> dict[str, tuple[object, Binding | None]]:
+ # Called under _state_lock: readers must not see an unpublished revision.
+ previews = {
+ name: (self.globals.get(name), self._bindings.get(name))
+ for name in sorted(names)
+ }
+ values = [
+ VariableValue(
+ name=name,
+ value=None,
+ datatype=binding.sql.kind if binding.sql else None,
+ execution_id=binding.execution_id,
+ revision=binding.revision,
+ )
+ for name, (_, binding) in previews.items()
+ if binding is not None
+ ]
+ if values:
+ broadcast_notification(
+ VariableValuesNotification(variables=values), self.stream
+ )
+ return previews
+
+ def _publish_variable_previews(
+ self, previews: dict[str, tuple[object, Binding | None]]
+ ) -> None:
+ # Formatting can run user code, including waiting for a state setter
+ # on another thread. Never hold the state lock while rendering.
+ values = {
+ name: create_variable_value(name, value)
+ for name, (value, binding) in previews.items()
+ if binding is None or binding.sql is None
+ }
+ with self._state_lock:
+ current = []
+ for name, value in values.items():
+ obj, binding = previews[name]
+ if (
+ self._bindings.get(name) is not binding
+ or self.globals.get(name) is not obj
+ ):
+ continue
+ if binding is not None:
+ value.execution_id = binding.execution_id
+ value.revision = binding.revision
+ current.append(value)
+ if current:
+ broadcast_notification(
+ VariableValuesNotification(variables=current), self.stream
+ )
+
+ def _update_variables(self, names: Iterable[str]) -> None:
+ if get_context().is_embedded() or get_mode() != "edit":
+ return
+ with self._state_lock:
+ revisions = {}
+ for name in sorted(set(names)):
+ if (
+ name not in self.globals
+ or (binding := self._bindings.get(name)) is None
+ ):
+ continue
+ revision = Revision(
+ binding.revision.execution, binding.revision.update + 1
+ )
+ self._bindings[name] = dataclasses.replace(
+ binding, revision=revision
+ )
+ revisions[name] = revision
+ previews = self._publish_bindings(revisions)
+ current = EXECUTION_CTX.get()
+ if current is not None and current.attempt is not None:
+ current.attempt.advance_inputs(revisions)
+ self._publish_variable_previews(previews)
+
def _propagate_kernel_errors(
self,
ctx: hook_context.OnFinishHookContext,
@@ -1541,9 +1799,21 @@ class Kernel:
# descendants (cancelled == cells that raise exceptions), whereas
# eager kernels do (since we clear all state ahead of time, and
# have the closure of the roots in cells to run)
+ workspaces: list[ImportWorkspace] = []
+
def invalidate_state(ctx: hook_context.PreparationHookContext) -> None:
for cid in ctx.cells_to_run:
- self._invalidate_cell_state(cid)
+ source = self.graph.cells[cid]
+ workspace = ctx.graph.cells[cid].import_workspace
+ workspaces.append(workspace)
+ with self._state_lock:
+ carried = (
+ self._imported_values(source)
+ or source.import_workspace.carried_values
+ )
+ source.import_workspace.carried_values = {}
+ self._invalidate_cell_state(cid)
+ workspace.carried_values = carried
def note_time_of_interruption(
cell_impl: CellImpl,
@@ -1578,6 +1848,7 @@ class Kernel:
execution_type=self.execution_type,
execution_context=self._install_execution_context,
hooks=run_hooks,
+ get_inputs=self._read_inputs,
user_config=self.user_config,
)
@@ -1586,7 +1857,11 @@ class Kernel:
# TODO(akshayka): when no logger is configured, log output is not
# redirected to frontend (it's printed to console),
# which is incorrect
- await runner.run_all()
+ try:
+ await runner.run_all()
+ finally:
+ for workspace in workspaces:
+ workspace.carried_values.clear()
with self._state_lock:
cells_with_stale_state = runner.resolve_state_updates(
self.state_updates
@@ -1602,6 +1877,9 @@ class Kernel:
from marimo._runtime.threads import is_marimo_thread
ctx = get_context()
+ self._update_variables(
+ name for name in self._bindings if self.globals.get(name) is state
+ )
if ctx.execution_context is not None:
setter_cell_id = ctx.execution_context.cell_id
else:
@@ -1954,6 +2232,7 @@ class Kernel:
execution_type=self.execution_type,
execution_context=self._install_execution_context,
hooks=scratchpad_hooks,
+ get_inputs=self._read_inputs,
executions={SCRATCH_CELL_ID: execution},
user_config=self.user_config,
)
@@ -2168,14 +2447,8 @@ class Kernel:
for name in ctx.ui_element_registry.bound_names(object_id)
if not is_local(name)
}
- variable_values: list[VariableValue] = []
+ self._update_variables(bound_names)
for name in bound_names:
- if name in self.globals:
- variable_values.append(
- create_variable_value(
- name=name, value=self.globals[name]
- )
- )
try:
# subtracting self.graph.definitions[name]: never rerun the
# cell that created the name
@@ -2200,12 +2473,6 @@ class Kernel:
# Entering undefined behavior territory ...
continue
- if variable_values and not ctx.is_embedded():
- broadcast_notification(
- VariableValuesNotification(variables=variable_values),
- self.stream,
- )
-
if self.reactive_execution_mode == "autorun":
await self._run_cells(referring_cells)
else:
diff --git a/marimo/_server/host/backend.py b/marimo/_server/host/backend.py
index 65081242c4..a9770cfda9 100644
--- a/marimo/_server/host/backend.py
+++ b/marimo/_server/host/backend.py
@@ -19,6 +19,7 @@ from marimo._messaging.notification import (
InterruptedNotification,
NotebookDocumentTransactionNotification,
StartupProgressNotification,
+ VariableValuesNotification,
)
from marimo._messaging.serde import deserialize_kernel_message
from marimo._runtime.commands import (
@@ -392,6 +393,27 @@ class SessionBackend(SessionConsumer):
self._handle_notification(message)
def _handle_notification(self, message: NotificationMessage) -> None:
+ if isinstance(message, VariableValuesNotification):
+ for value in message.variables:
+ if value.execution_id is None or value.revision is None:
+ continue
+ self._observe(
+ be.Produced(
+ protocol.Variable(
+ execution_id=protocol.ExecutionId(
+ value.execution_id
+ ),
+ id=protocol.ItemId(
+ f"{value.execution_id}-variable-{value.name}"
+ ),
+ name=value.name,
+ preview=value.value,
+ revision=value.revision,
+ type=value.datatype,
+ )
+ )
+ )
+ return
if isinstance(message, NotebookDocumentTransactionNotification):
if self._document_changed is not None and self.session is not None:
self._document_changed()
diff --git a/marimo/_server/host/translate.py b/marimo/_server/host/translate.py
index e9413c4916..6d5e882358 100644
--- a/marimo/_server/host/translate.py
+++ b/marimo/_server/host/translate.py
@@ -86,7 +86,9 @@ class RunTranslator:
return []
if notification.phase == "running":
return [be.Started(execution.id)]
- return self._conclude(execution)
+ return self._conclude(
+ execution, depends_on=notification.depends_on
+ )
execution = self._executions.get(notification.execution_id or "")
if execution is None:
@@ -145,6 +147,7 @@ class RunTranslator:
*,
error: protocol.Error | None = None,
interrupted: bool = False,
+ depends_on: dict[str, protocol.Revision | None] | None = None,
) -> list[be.Message]:
if execution.ended:
return []
@@ -160,7 +163,9 @@ class RunTranslator:
elif interrupted or execution.errors:
execution.outcome = protocol.InterruptedOutcome()
execution.ended = True
- out: list[be.Message] = [be.Completed(execution.id, execution.outcome)]
+ out: list[be.Message] = [
+ be.Completed(execution.id, execution.outcome, depends_on or {})
+ ]
self._failed |= isinstance(execution.outcome, protocol.ErrorOutcome)
self.interrupted |= isinstance(
execution.outcome, protocol.InterruptedOutcome
diff --git a/marimo/_sql/bindings.py b/marimo/_sql/bindings.py
new file mode 100644
index 0000000000..eb7c544285
--- /dev/null
+++ b/marimo/_sql/bindings.py
@@ -0,0 +1,125 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, cast
+
+from marimo._ast.sql_visitor import SQLKind, SQLRef
+from marimo._dependencies.dependencies import DependencyManager
+
+if TYPE_CHECKING:
+ from collections.abc import Sequence
+
+ from marimo._ast.cell import CellImpl
+
+
+@dataclass(frozen=True)
+class SQLBinding:
+ """A SQL definition, named as declared in the notebook."""
+
+ name: str
+ kind: SQLKind
+ ref: SQLRef | None = None
+
+ def matches(self, ref: SQLRef) -> bool:
+ return ref.matches_hierarchical_ref(
+ self.ref.table if self.ref is not None else self.name,
+ self.name,
+ self.kind,
+ )
+
+
+def get_sql_bindings(cell: CellImpl) -> list[SQLBinding]:
+ """Resolve declared SQL objects against the live default DuckDB catalog.
+
+ Static definitions alone cannot distinguish a successful CREATE from one
+ skipped by a conditional or an exception earlier in the cell.
+ """
+ candidates = []
+ for name, definitions in cell.variable_data.items():
+ variable = definitions[-1]
+ if variable.kind not in ("table", "view", "schema", "catalog"):
+ continue
+ candidates.append(
+ SQLBinding(
+ variable.sql_ref.qualified_name if variable.sql_ref else name,
+ cast(SQLKind, variable.kind),
+ variable.sql_ref,
+ )
+ )
+ return existing_sql_bindings(candidates)
+
+
+def existing_sql_bindings(
+ candidates: Sequence[SQLBinding],
+) -> list[SQLBinding]:
+ """Return the SQL bindings whose objects still exist in this runtime."""
+ if not candidates or not DependencyManager.duckdb.has():
+ return []
+
+ import duckdb
+
+ try:
+ database, schema = duckdb.sql(
+ "SELECT current_database(), current_schema()"
+ ).fetchone() or ("memory", "main")
+ database, schema = database.lower(), schema.lower()
+ catalogs = {
+ row[0].lower()
+ for row in duckdb.sql(
+ "SELECT database_name FROM duckdb_databases()"
+ ).fetchall()
+ }
+ schemas = {
+ tuple(part.lower() for part in row)
+ for row in duckdb.sql(
+ "SELECT database_name, schema_name FROM duckdb_schemas()"
+ ).fetchall()
+ }
+ relations = {
+ tuple(part.lower() for part in row)
+ for row in duckdb.sql(
+ "SELECT database_name, schema_name, table_name, 'table' "
+ "FROM duckdb_tables() UNION ALL "
+ "SELECT database_name, schema_name, view_name, 'view' "
+ "FROM duckdb_views()"
+ ).fetchall()
+ }
+ except duckdb.Error:
+ # An aborted transaction can make catalog inspection unavailable.
+ return []
+
+ bound = []
+ for candidate in candidates:
+ if candidate.kind == "catalog":
+ exists = candidate.name.lower() in catalogs
+ elif candidate.kind == "schema":
+ parts = candidate.name.lower().split(".")
+ qualified = (
+ tuple(parts) if len(parts) == 2 else (database, parts[0])
+ )
+ exists = qualified in schemas
+ else:
+ ref = candidate.ref
+ assert ref is not None
+ catalog = ref.catalog or database
+ namespace = ref.schema or schema
+ if ref.catalog is None and ref.schema in catalogs:
+ catalog, namespace = ref.schema, "main"
+ exists = (
+ catalog,
+ namespace,
+ ref.table,
+ candidate.kind,
+ ) in relations
+ if ref.catalog is None and ref.schema in (None, "main"):
+ # Unqualified and main-qualified names also resolve temp tables.
+ exists |= (
+ "temp",
+ "main",
+ ref.table,
+ candidate.kind,
+ ) in relations
+ if exists:
+ bound.append(candidate)
+ return bound
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index d9945cbc27..f59792e026 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -2062,6 +2062,15 @@ components:
- type: string
- type: 'null'
default: null
+ depends_on:
+ anyOf:
+ - additionalProperties:
+ anyOf:
+ - $ref: '#/components/schemas/Revision'
+ - type: 'null'
+ type: object
+ - type: 'null'
+ default: null
execution_id:
type: string
op:
@@ -4789,6 +4798,22 @@ components:
RequestId:
format: request-id
type: string
+ Revision:
+ properties:
+ execution:
+ description: Identifies an execution. Unique within the host.
+ minLength: 1
+ type: string
+ update:
+ description: How many times the value has been updated since it was produced.
+ Zero at binding.
+ minimum: 0
+ type: integer
+ required:
+ - execution
+ - update
+ title: Revision
+ type: object
RunningNotebooksResponse:
properties:
files:
@@ -6290,8 +6315,18 @@ components:
anyOf:
- type: string
- type: 'null'
+ execution_id:
+ anyOf:
+ - type: string
+ - type: 'null'
+ default: null
name:
type: string
+ revision:
+ anyOf:
+ - $ref: '#/components/schemas/Revision'
+ - type: 'null'
+ default: null
value:
anyOf:
- type: string
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index caf5112fc7..3fa0cef537 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -5057,6 +5057,10 @@ export interface components {
cell_id: components["schemas"]["CellId"];
/** @default null */
code?: string | null;
+ /** @default null */
+ depends_on?: {
+ [key: string]: components["schemas"]["Revision"] | null;
+ } | null;
execution_id: string;
/** @enum {unknown} */
op: "execution";
@@ -6669,6 +6673,13 @@ export interface components {
};
/** Format: request-id */
RequestId: TypedString<"RequestId">;
+ /** Revision */
+ Revision: {
+ /** @description Identifies an execution. Unique within the host. */
+ execution: string;
+ /** @description How many times the value has been updated since it was produced. Zero at binding. */
+ update: number;
+ };
/** RunningNotebooksResponse */
RunningNotebooksResponse: {
files: components["schemas"]["MarimoFile"][];
@@ -7607,7 +7618,11 @@ export interface components {
*/
VariableValue: {
datatype: string | null;
+ /** @default null */
+ execution_id?: string | null;
name: string;
+ /** @default null */
+ revision?: components["schemas"]["Revision"] | null;
value: string | null;
};
/**
diff --git a/tests/_runtime/test_bindings.py b/tests/_runtime/test_bindings.py
new file mode 100644
index 0000000000..0005fc2fd7
--- /dev/null
+++ b/tests/_runtime/test_bindings.py
@@ -0,0 +1,682 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import asyncio
+import sys
+from types import ModuleType
+from typing import TYPE_CHECKING
+
+import pytest
+
+from marimo._core.protocol import ExecutionId, Revision
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._messaging.notification import (
+ ExecutionNotification,
+ VariableValue,
+ VariableValuesNotification,
+)
+from marimo._runtime import runtime
+from marimo._runtime.commands import (
+ DeleteCellCommand,
+ ExecuteCellsCommand,
+ ExecuteScratchpadCommand,
+ UpdateCellConfigCommand,
+ UpdateUIElementCommand,
+)
+from marimo._session.model import SessionMode
+from marimo._types.ids import CellId_t
+from tests._runtime._helpers.session import mocked_kernel_session
+
+if TYPE_CHECKING:
+ from collections.abc import Iterator
+
+ from marimo._config.config import ExecutionType
+ from marimo._messaging.notification import NotificationMessage
+ from marimo._runtime.runtime import Kernel
+ from tests.conftest import MockedKernel
+
+
+async def run(kernel: Kernel, **cells: str) -> None:
+ await kernel.router.dispatch(
+ ExecuteCellsCommand(
+ cell_ids=[CellId_t(cid) for cid in cells],
+ codes=list(cells.values()),
+ )
+ )
+
+
+def completed(
+ messages: list[NotificationMessage],
+) -> dict[CellId_t, ExecutionNotification]:
+ return {
+ m.cell_id: m
+ for m in messages
+ if isinstance(m, ExecutionNotification) and m.phase == "completed"
+ }
+
+
+def variables(messages: list[NotificationMessage]) -> dict[str, VariableValue]:
+ return {
+ v.name: v
+ for m in messages
+ if isinstance(m, VariableValuesNotification)
+ for v in m.variables
+ }
+
+
+def bound(messages: list[NotificationMessage]) -> dict[str, VariableValue]:
+ return {
+ name: v
+ for name, v in variables(messages).items()
+ if v.revision is not None
+ }
+
+
[email protected]("execution_type", ["relaxed", "strict"])
+async def test_conditional_definitions_bind_only_defined_names(
+ mocked_kernel: MockedKernel, execution_type: ExecutionType
+) -> None:
+ mocked_kernel.k.execution_type = execution_type
+ await run(
+ mocked_kernel.k,
+ a="flag = False",
+ b="if flag:\n x = 1\ny = None",
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ values = variables(messages)
+ assert set(bound(messages)) == {"flag", "y"}
+ assert values["x"].execution_id is None
+ assert completed(messages)["b"].depends_on == {
+ "flag": values["flag"].revision
+ }
+
+
+async def test_run_mode_reports_no_variables() -> None:
+ with mocked_kernel_session(mode=SessionMode.RUN) as session:
+ await run(session.kernel, a="secret = 'private'", b="len(secret)")
+ messages = session.stream.parsed_operations
+ assert not variables(messages)
+ assert all(
+ m.depends_on is None
+ for m in messages
+ if isinstance(m, ExecutionNotification)
+ )
+
+
+async def test_bindings_are_reported_before_completion(
+ mocked_kernel: MockedKernel,
+) -> None:
+ await run(
+ mocked_kernel.k,
+ a="x = 1",
+ b="y = print(x + 1)",
+ c="z = missing",
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ executions = completed(messages)
+ values = bound(messages)
+ assert set(values) == {"x", "y"}
+ assert executions["b"].depends_on == {
+ "print": None,
+ "x": values["x"].revision,
+ }
+ assert executions["c"].depends_on == {"missing": None}
+ assert values["x"].value == "1"
+ assert values["x"].execution_id == executions["a"].execution_id
+ for value in values.values():
+ reported = next(
+ i
+ for i, m in enumerate(messages)
+ if isinstance(m, VariableValuesNotification)
+ and value in m.variables
+ )
+ completion = next(
+ i
+ for i, m in enumerate(messages)
+ if isinstance(m, ExecutionNotification)
+ and m.execution_id == value.execution_id
+ and m.phase == "completed"
+ )
+ assert reported < completion
+
+
+async def test_lazy_rerun_replaces_a_binding_without_rewriting_old_inputs(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(kernel, a="x = 1", b="y = x + 1")
+ messages = mocked_kernel.stream.parsed_operations
+ before = completed(messages)["b"]
+ revision = bound(messages)["x"].revision
+
+ await run(kernel, a="x = 2")
+ messages = mocked_kernel.stream.parsed_operations
+ assert completed(messages)["b"] == before
+ assert before.depends_on == {"x": revision}
+ assert bound(messages)["x"].revision != revision
+
+ await run(kernel, b="y = x + 1")
+ messages = mocked_kernel.stream.parsed_operations
+ assert completed(messages)["b"].depends_on == {
+ "x": bound(messages)["x"].revision
+ }
+
+
[email protected]("deletion", ["cell", "python"])
+async def test_deleted_bindings_are_unbound_for_later_readers(
+ mocked_kernel: MockedKernel,
+ deletion: str,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(kernel, a="x = 1", b="y = x + 1")
+ if deletion == "cell":
+ await kernel.delete_cell(DeleteCellCommand(cell_id=CellId_t("a")))
+ else:
+ await run(kernel, d="del x")
+ mocked_kernel.stream.messages.clear()
+ await run(kernel, b="y = x + 1")
+ messages = mocked_kernel.stream.parsed_operations
+ assert completed(messages)["b"].depends_on == {"x": None}
+ assert not bound(messages)
+
+
[email protected]("failure", ["1 / 0", "raise KeyboardInterrupt"])
+async def test_failed_and_skipped_executions_only_bind_existing_names(
+ mocked_kernel: MockedKernel,
+ failure: str,
+) -> None:
+ await run(mocked_kernel.k, a=f"x = 1\n{failure}\ny = 2", b="z = y")
+ messages = mocked_kernel.stream.parsed_operations
+ executions = completed(messages)
+ assert set(executions) == {"a", "b"}
+ assert set(bound(messages)) == {"x"}
+ assert variables(messages)["y"].revision is None
+ assert executions["b"].depends_on == {"y": None}
+
+
+async def test_state_updates_advance_the_setters_own_input_revision(
+ mocked_kernel: MockedKernel,
+) -> None:
+ await run(
+ mocked_kernel.k,
+ a="import marimo as mo",
+ b="get, set_value = mo.state(0)",
+ c="value = get()",
+ )
+ old = bound(mocked_kernel.stream.parsed_operations)["get"].revision
+ mocked_kernel.stream.messages.clear()
+
+ await run(
+ mocked_kernel.k,
+ d="for i in range(3):\n set_value(get() + 1)",
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ new = bound(messages)["get"].revision
+ assert new.execution == old.execution
+ assert new.update == old.update + 3
+ executions = completed(messages)
+ assert executions["d"].depends_on["get"] == new
+ assert executions["c"].depends_on == {"get": new}
+ assert mocked_kernel.k.globals["value"] == 3
+
+
+async def test_ui_updates_publish_revisions_without_rerunning_lazy_cells(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel,
+ a="import marimo as mo",
+ b="slider = mo.ui.slider(0, 10, value=1)",
+ c="value = slider.value",
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ before = completed(messages)["c"]
+ old = bound(messages)["slider"].revision
+
+ await kernel.router.dispatch(
+ UpdateUIElementCommand.from_ids_and_values(
+ [(kernel.globals["slider"]._id, 5)]
+ )
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ assert completed(messages)["c"] == before
+ slider = bound(messages)["slider"]
+ assert (slider.revision.execution, slider.revision.update) == (
+ old.execution,
+ old.update + 1,
+ )
+ assert slider.value == "5"
+ assert before.depends_on == {"slider": old}
+
+
+async def test_scratch_updates_reach_nested_readers_from_the_notebook_namespace(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel,
+ a="import marimo as mo",
+ b="get, set_value = mo.state(1)",
+ c="value = get()",
+ alias="alias = get",
+ )
+ before = bound(mocked_kernel.stream.parsed_operations)["get"]
+
+ async def run_reader() -> None:
+ await run(kernel, c="value = get()")
+
+ kernel.globals["run_reader"] = run_reader
+ mocked_kernel.stream.messages.clear()
+ await kernel.router.dispatch(
+ ExecuteScratchpadCommand(
+ code="""
+get = "scratch-local"
+set_value(2)
+await run_reader()
+"""
+ )
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ updated = bound(messages)
+ after = updated["get"]
+ assert updated["alias"].revision.update == 1
+ assert after.datatype == "State"
+ assert after.execution_id == before.execution_id
+ assert after.revision.update == before.revision.update + 1
+ child = completed(messages)["c"]
+ assert child.depends_on == {"get": after.revision}
+ update_index = next(
+ i
+ for i, m in enumerate(messages)
+ if isinstance(m, VariableValuesNotification) and after in m.variables
+ )
+ child_start = next(
+ i
+ for i, m in enumerate(messages)
+ if isinstance(m, ExecutionNotification)
+ and m.execution_id == child.execution_id
+ and m.phase == "running"
+ )
+ assert update_index < child_start
+ assert kernel.globals["value"] == 2
+
+
+async def test_background_updates_do_not_rewrite_completed_inputs(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel,
+ a="import marimo as mo\nimport threading",
+ b="get, set_value = mo.state(1)",
+ c="""
+gate = threading.Event()
+done = threading.Event()
+def _update():
+ gate.wait()
+ set_value(2)
+ done.set()
+worker = mo.Thread(target=_update)
+worker.start()
+get()
+""",
+ )
+ before = completed(mocked_kernel.stream.parsed_operations)["c"]
+ try:
+ kernel.globals["gate"].set()
+ assert await asyncio.to_thread(kernel.globals["done"].wait, 5)
+ finally:
+ kernel.globals["gate"].set()
+ kernel.globals["worker"].join(timeout=5)
+ messages = mocked_kernel.stream.parsed_operations
+ assert completed(messages)["c"] == before
+ previous = before.depends_on["get"]
+ current = bound(messages)["get"].revision
+ assert (current.execution, current.update) == (
+ previous.execution,
+ previous.update + 1,
+ )
+
+
[email protected]("debugging", [False, True])
+async def test_reimport_in_the_same_cell_keeps_its_revision(
+ mocked_kernel: MockedKernel,
+ monkeypatch: pytest.MonkeyPatch,
+ debugging: bool,
+) -> None:
+ if debugging:
+ monkeypatch.setenv("DEBUGPY_RUNNING", "1")
+ await run(mocked_kernel.k, a="import math", b="value = math.pi")
+ messages = mocked_kernel.stream.parsed_operations
+ before = completed(messages)["b"]
+ old = bound(messages)["math"]
+
+ await run(mocked_kernel.k, a="import math\nimport os")
+ messages = mocked_kernel.stream.parsed_operations
+ current = bound(messages)["math"]
+ assert current.execution_id != old.execution_id
+ assert current.revision == old.revision
+ assert completed(messages)["b"].depends_on == before.depends_on
+
+
+async def test_rebinding_none_in_the_same_cell_advances_its_revision(
+ mocked_kernel: MockedKernel,
+) -> None:
+ await run(mocked_kernel.k, a="x = None")
+ old = bound(mocked_kernel.stream.parsed_operations)["x"].revision
+ await run(mocked_kernel.k, a="x = None\ny = 1")
+ new = bound(mocked_kernel.stream.parsed_operations)["x"].revision
+ assert new != old
+
+
+ "replacement", ["pass", "import missing_revision_test_module\nimport math"]
+)
+async def test_removed_or_failed_imports_do_not_reuse_their_old_revision(
+ mocked_kernel: MockedKernel,
+ replacement: str,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ kernel = mocked_kernel.k
+ await run(kernel, a="import math")
+ old = bound(mocked_kernel.stream.parsed_operations)["math"].revision
+ await run(kernel, a=replacement)
+ if replacement == "pass":
+ await run(kernel, a="import math")
+ else:
+ module = ModuleType("missing_revision_test_module")
+ monkeypatch.setitem(sys.modules, module.__name__, module)
+ await run(kernel, a=replacement)
+ assert (
+ bound(mocked_kernel.stream.parsed_operations)["math"].revision != old
+ )
+
+
[email protected]("changed", [False, True])
+async def test_queued_imports_use_the_latest_nested_result(
+ mocked_kernel: MockedKernel,
+ monkeypatch: pytest.MonkeyPatch,
+ changed: bool,
+) -> None:
+ module = ModuleType("revision_test_module")
+ module.value = None
+ monkeypatch.setitem(sys.modules, module.__name__, module)
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ code = "from revision_test_module import value"
+ nested = False
+
+ async def rerun_import() -> None:
+ if nested:
+ if changed:
+ module.value = object()
+ await run(kernel, b=code)
+
+ kernel.globals["rerun_import"] = rerun_import
+ await run(kernel, a="await rerun_import()", b=code, c="result = value")
+ initial = bound(mocked_kernel.stream.parsed_operations)["value"].revision
+ reader = completed(mocked_kernel.stream.parsed_operations)["c"]
+ nested = True
+ mocked_kernel.stream.messages.clear()
+ await run(kernel, a="await rerun_import()", b=code)
+ messages = mocked_kernel.stream.parsed_operations
+ values = [
+ v
+ for m in messages
+ if isinstance(m, VariableValuesNotification)
+ for v in m.variables
+ if v.name == "value" and v.value is not None
+ ]
+ assert len(values) == 2
+ assert values[0].execution_id != values[1].execution_id
+ assert values[0].revision == values[1].revision
+ assert (values[1].revision != initial) is changed
+ assert "c" not in completed(messages)
+ assert reader.depends_on == {"value": initial}
+
+
+async def test_disabled_imports_discard_their_carried_revision(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(kernel, a="import math")
+ old = bound(mocked_kernel.stream.parsed_operations)["math"].revision
+ await kernel.router.dispatch(
+ UpdateCellConfigCommand(configs={CellId_t("a"): {"disabled": True}})
+ )
+ await run(kernel, a="import math\nimport os")
+ await kernel.router.dispatch(
+ UpdateCellConfigCommand(configs={CellId_t("a"): {"disabled": False}})
+ )
+ await run(kernel, a="import math\nimport os")
+ assert (
+ bound(mocked_kernel.stream.parsed_operations)["math"].revision != old
+ )
+
+
+async def test_rejected_imports_discard_their_carried_revision(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ await run(kernel, a="import math")
+ old = bound(mocked_kernel.stream.parsed_operations)["math"].revision
+ await run(kernel, a="import math\nimport os", b="math = 1")
+ await kernel.delete_cell(DeleteCellCommand(cell_id=CellId_t("b")))
+ await run(kernel, a="import math\nimport os")
+ assert (
+ bound(mocked_kernel.stream.parsed_operations)["math"].revision != old
+ )
+
+
+async def test_preview_can_wait_for_a_state_setter(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel,
+ a="import marimo as mo",
+ b="get, set_value = mo.state(0)",
+ c="""
+class _Preview:
+ __slots__ = ()
+
+ def __str__(self):
+ thread = mo.Thread(target=lambda: set_value(1))
+ thread.start()
+ thread.join(timeout=1)
+ return "blocked" if thread.is_alive() else "done"
+
+x = _Preview()
+""",
+ )
+ assert (
+ variables(mocked_kernel.stream.parsed_operations)["x"].value == "done"
+ )
+
+
+async def test_preview_cannot_overwrite_an_update_during_rendering(
+ mocked_kernel: MockedKernel,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel, a="import marimo as mo", b="get, set_value = mo.state(0)"
+ )
+ old = bound(mocked_kernel.stream.parsed_operations)["get"]
+ mocked_kernel.stream.messages.clear()
+ create_preview = runtime.create_variable_value
+ rendered = False
+
+ def render(name: str, value: object) -> VariableValue:
+ nonlocal rendered
+ preview = create_preview(name, value)
+ if name == "get":
+ preview.value = str(kernel.globals["get"]())
+ if not rendered:
+ rendered = True
+ # The update must be visible before formatting can run user code.
+ assert bound(mocked_kernel.stream.parsed_operations)[
+ "get"
+ ].revision == Revision(ExecutionId(old.execution_id), 1)
+ kernel.globals["set_value"](2)
+ return preview
+
+ monkeypatch.setattr(runtime, "create_variable_value", render)
+ kernel.globals["set_value"](1)
+ assert variables(mocked_kernel.stream.parsed_operations)[
+ "get"
+ ] == VariableValue(
+ name="get",
+ value="2",
+ datatype="State",
+ execution_id=old.execution_id,
+ revision=Revision(ExecutionId(old.execution_id), 2),
+ )
+
+
+def isolated_duckdb() -> Iterator[None]:
+ import duckdb
+
+ previous = duckdb.default_connection()
+ with duckdb.connect() as connection:
+ duckdb.set_default_connection(connection)
+ try:
+ yield
+ finally:
+ duckdb.set_default_connection(previous)
+
+
[email protected]("isolated_duckdb")
+ not DependencyManager.duckdb.has() or not DependencyManager.sqlglot.has(),
+ reason="SQL dependencies not installed",
+)
[email protected]("kind", ["TABLE", "VIEW", "TEMP TABLE"])
[email protected]("qualified", [False, True])
+async def test_sql_bindings_record_inputs_and_replacement(
+ mocked_kernel: MockedKernel, kind: str, qualified: bool
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(kernel, a="import duckdb")
+ leaf = f"revision_{kind.lower().replace(' ', '_')}"
+ name = f"main.{leaf}" if qualified else leaf
+ producer = (
+ f"duckdb.sql('CREATE OR REPLACE {kind} {name} AS SELECT 1 AS x')"
+ )
+ reader = f"df = duckdb.sql('SELECT * FROM {name}').fetchall()"
+ await run(kernel, b=producer, c=reader)
+ messages = mocked_kernel.stream.parsed_operations
+ values = bound(messages)
+ table = values[name]
+ assert table == VariableValue(
+ name=name,
+ value=None,
+ datatype=kind.rsplit(maxsplit=1)[-1].lower(),
+ execution_id=completed(messages)["b"].execution_id,
+ revision=Revision(
+ ExecutionId(completed(messages)["b"].execution_id), 0
+ ),
+ )
+ assert completed(messages)["c"].depends_on == {
+ "duckdb": values["duckdb"].revision,
+ name: table.revision,
+ }
+ await run(kernel, b=producer.replace("SELECT 1", "SELECT 2"))
+ assert (
+ bound(mocked_kernel.stream.parsed_operations)[name].revision
+ != table.revision
+ )
+ assert kernel.globals["df"] == [(1,)]
+
+
[email protected]("isolated_duckdb")
+ not DependencyManager.duckdb.has() or not DependencyManager.sqlglot.has(),
+ reason="SQL dependencies not installed",
+)
+async def test_sql_bindings_only_include_existing_objects(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel,
+ a="import duckdb",
+ b="""
+duckdb.sql('CREATE TABLE revision_exists AS SELECT 1 AS x')
+if False:
+ duckdb.sql('CREATE TABLE revision_skipped AS SELECT 1 AS x')
+raise RuntimeError('partial execution')
+duckdb.sql('CREATE TABLE revision_failed AS SELECT 1 AS x')
+""",
+ )
+ assert set(bound(mocked_kernel.stream.parsed_operations)) == {
+ "duckdb",
+ "revision_exists",
+ }
+ await kernel.delete_cell(DeleteCellCommand(cell_id=CellId_t("b")))
+ mocked_kernel.stream.messages.clear()
+ await run(kernel, c="duckdb.sql('SELECT * FROM revision_exists')")
+ assert (
+ completed(mocked_kernel.stream.parsed_operations)["c"].depends_on[
+ "revision_exists"
+ ]
+ is None
+ )
+
+
[email protected]("isolated_duckdb")
+ not DependencyManager.duckdb.has() or not DependencyManager.sqlglot.has(),
+ reason="SQL dependencies not installed",
+)
+async def test_sql_qualified_bindings_do_not_collide(
+ mocked_kernel: MockedKernel,
+) -> None:
+ kernel = mocked_kernel.k
+ kernel.reactive_execution_mode = "lazy"
+ await run(
+ kernel,
+ a="import duckdb",
+ b="duckdb.sql('CREATE SCHEMA IF NOT EXISTS revision_left')",
+ c="duckdb.sql('CREATE SCHEMA IF NOT EXISTS revision_right')",
+ d="duckdb.sql('CREATE TABLE revision_left.t AS SELECT 1 AS x')",
+ e="duckdb.sql('CREATE TABLE revision_right.t AS SELECT 2 AS x')",
+ f="rows = duckdb.sql('SELECT * FROM revision_left.t').fetchall()",
+ )
+ messages = mocked_kernel.stream.parsed_operations
+ values = bound(messages)
+ assert completed(messages)["f"].depends_on == {
+ name: values[name].revision
+ for name in ("duckdb", "revision_left", "revision_left.t")
+ }
+ assert kernel.globals["rows"] == [(1,)]
+ assert (
+ values["revision_right.t"].execution_id
+ == completed(messages)["e"].execution_id
+ )
+
+ await run(kernel, g="duckdb.sql('DROP TABLE revision_left.t')")
+ mocked_kernel.stream.messages.clear()
+ await run(
+ kernel,
+ f="rows = duckdb.sql('SELECT * FROM revision_left.t').fetchall()",
+ )
+ assert completed(mocked_kernel.stream.parsed_operations)[
+ "f"
+ ].depends_on == {
+ "duckdb": values["duckdb"].revision,
+ "revision_left": values["revision_left"].revision,
+ "revision_left.t": None,
+ }
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index aa1a68936c..49f4e47fff 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -324,12 +324,12 @@ class TestExecution:
notify_frontend=False,
)
- [notification] = [
+ notifications = [
m
for m in mocked_kernel.stream.parsed_operations
if isinstance(m, VariableValuesNotification)
]
- assert [(v.name, v.value) for v in notification.variables] == [
+ assert [(v.name, v.value) for v in notifications[-1].variables] == [
("array", "[5]")
]
diff --git a/tests/_server/api/endpoints/test_files.py b/tests/_server/api/endpoints/test_files.py
index 8f219ecfd4..027b001c58 100644
--- a/tests/_server/api/endpoints/test_files.py
+++ b/tests/_server/api/endpoints/test_files.py
@@ -560,7 +560,9 @@ def test_rename_propagates(
data = receive_notification(websocket)
if data["op"] == "variable-values":
for var in data["data"]["variables"]:
- variables[var["name"]] = var["value"]
+ # Revisions arrive before their rendered previews.
+ if var["value"] is not None:
+ variables[var["name"]] = var["value"]
# Variable outputs are truncated to 50 characters
# current_filename can exceed this count on windows and OSX.
@@ -586,7 +588,9 @@ def test_rename_propagates(
data = receive_notification(websocket)
if data["op"] == "variable-values":
for var in data["data"]["variables"]:
- variables[var["name"]] = var["value"]
+ # Revisions arrive before their rendered previews.
+ if var["value"] is not None:
+ variables[var["name"]] = var["value"]
assert ("x" + new_filename).startswith(variables["a"])
assert new_filename.startswith(variables["b"])
diff --git a/tests/_server/host/test_host_staleness.py b/tests/_server/host/test_host_staleness.py
index f8350f1a58..5dc7f2b482 100644
--- a/tests/_server/host/test_host_staleness.py
+++ b/tests/_server/host/test_host_staleness.py
@@ -9,8 +9,6 @@ import json
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any
-import pytest
-
from tests._server.host.drive import finished, wait_for
from tests._server.host.live import key, open_host
from tests._server.host.staleness import snapshot
@@ -128,13 +126,6 @@ async def client(
yield Client(app, notebook_id, events, ids)
-# Passes once the host publishes variables and dependencies.
-unpublished = pytest.mark.xfail(
- strict=True, reason="the host does not publish variables yet"
-)
-
-
-@unpublished
async def test_edits_and_reruns_outdate_dependents(tmp_path: Path) -> None:
async with client(
tmp_path, on_cell_change="lazy", x="x = 1", y="y = x + 1"
@@ -162,7 +153,6 @@ async def test_autorun_leaves_every_result_current(tmp_path: Path) -> None:
assert await c.outdated() == {}
-@unpublished
async def test_state_updates_outdate_readers(tmp_path: Path) -> None:
async with client(
tmp_path,
@@ -225,7 +215,6 @@ async def test_reimporting_keeps_dependents_current(tmp_path: Path) -> None:
assert (await c.snapshot()).latest(c.cells["value"]) == before
-@unpublished
async def test_binding_a_missing_name_outdates_its_readers(
tmp_path: Path,
) -> None:
diff --git a/tests/_server/host/test_host_translate.py b/tests/_server/host/test_host_translate.py
index 4bdb921cde..fff4aa09e1 100644
--- a/tests/_server/host/test_host_translate.py
+++ b/tests/_server/host/test_host_translate.py
@@ -24,6 +24,7 @@ from marimo._core.protocol import (
ErrorOutcome,
ExecutionId,
InterruptedOutcome,
+ Revision,
RunId,
ValueOutcome,
)
@@ -78,7 +79,8 @@ def test_a_run_that_prints_and_shows_a_value() -> None:
)
assert t.feed(CompletedRunNotification(run_id="other")) == []
assert not t.completed
- messages.extend(t.feed(attempt("completed")))
+ inputs = {"x": Revision(ExecutionId("producer"), 2), "print": None}
+ messages.extend(t.feed(attempt("completed", depends_on=inputs)))
messages.extend(t.feed(CompletedRunNotification(run_id="r1")))
assert t.completed
messages.extend(t.finish())
@@ -120,6 +122,7 @@ def test_a_run_that_prints_and_shows_a_value() -> None:
outcome=ValueOutcome(
DisplayData(data="<pre>9</pre>", mimetype="text/html")
),
+ depends_on=inputs,
),
Done(run_id=RunId("r1"), outcome="succeeded"),
]
diff --git a/tests/_session/state/test_session_view.py b/tests/_session/state/test_session_view.py
index 135b047c2a..ea09f2f426 100644
--- a/tests/_session/state/test_session_view.py
+++ b/tests/_session/state/test_session_view.py
@@ -8,6 +8,7 @@ import msgspec
import pytest
from marimo._ast.cell import RuntimeStateType
+from marimo._core.protocol import ExecutionId, Revision
from marimo._data.models import (
Database,
DataSourceConnection,
@@ -725,10 +726,23 @@ def test_last_run_code(session_view: SessionView) -> None:
assert session_view.last_executed_code[cell_id] == "print('hello')"
-def test_serialize_parse_variable_value() -> None:
[email protected]("execution_id", [None, "execution-1"])
+def test_serialize_parse_variable_value(execution_id: str | None) -> None:
original = create_variable_value(name="var1", value=1)
+ original.execution_id = execution_id
+ original.revision = (
+ Revision(ExecutionId(execution_id), 2) if execution_id else None
+ )
serialized = serialize(original)
- assert serialized == {"datatype": "int", "name": "var1", "value": "1"}
+ assert serialized == {
+ "datatype": "int",
+ "name": "var1",
+ "value": "1",
+ "execution_id": execution_id,
+ "revision": (
+ {"execution": execution_id, "update": 2} if execution_id else None
+ ),
+ }
parsed = parse_raw(serialized, VariableValue)
assert parsed == original