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.

Merge model messages into a single replay snapshot (#8163)

Trevor Manz16a61d74c1d4
diff --git a/marimo/_session/state/session_view.py b/marimo/_session/state/session_view.py
index eb51a4b7be..c91d99fbf7 100644
--- a/marimo/_session/state/session_view.py
+++ b/marimo/_session/state/session_view.py
@@ -3,7 +3,7 @@ from __future__ import annotations
import time
from dataclasses import dataclass
-from typing import Any, Literal, Optional, cast
+from typing import Any, Literal, Optional, Union, cast
from marimo import _loggers
from marimo._data.models import DataSourceConnection, DataTable
@@ -15,7 +15,10 @@ from marimo._messaging.notification import (
DataSourceConnectionsNotification,
InstallingPackageAlertNotification,
InterruptedNotification,
+ ModelClose,
ModelLifecycleNotification,
+ ModelOpen,
+ ModelUpdate,
NotificationMessage,
SQLTableListPreviewNotification,
SQLTablePreviewNotification,
@@ -51,6 +54,59 @@ ExportType = Literal["html", "md", "ipynb", "session"]
MIMEBUNDLE_TYPE: KnownMimeType = "application/vnd.marimo+mimebundle"
+BufferPath = tuple[Union[str, int], ...]
+
+
+@dataclass
+class ModelReplayState:
+ """Aggregated snapshot of a widget model's current state.
+
+ Internally uses a dict for buffers (path → bytes) so merging
+ updates is a simple dict operation. Converted back to the wire
+ format (parallel lists) on replay via ``to_notification()``.
+ """
+
+ model_id: WidgetModelId
+ state: dict[str, Any]
+ buffers: dict[BufferPath, bytes]
+
+ @staticmethod
+ def from_open(model_id: WidgetModelId, msg: ModelOpen) -> ModelReplayState:
+ buffers = {tuple(p): b for p, b in zip(msg.buffer_paths, msg.buffers)}
+ return ModelReplayState(
+ model_id=model_id,
+ state=dict(msg.state),
+ buffers=buffers,
+ )
+
+ def apply_update(self, msg: ModelUpdate) -> None:
+ """Merge an update into this snapshot (mutates in place)."""
+ # Drop buffers whose root key is being overridden
+ updated_keys = set(msg.state.keys())
+ self.buffers = {
+ path: buf
+ for path, buf in self.buffers.items()
+ if path[0] not in updated_keys
+ }
+ # Merge new state and buffers
+ self.state.update(msg.state)
+ for path, buf in zip(msg.buffer_paths, msg.buffers):
+ self.buffers[tuple(path)] = buf
+
+ def to_notification(self) -> ModelLifecycleNotification:
+ """Convert back to a ModelOpen notification for replay."""
+ paths = list(self.buffers.keys())
+ bufs = list(self.buffers.values())
+ return ModelLifecycleNotification(
+ model_id=self.model_id,
+ message=ModelOpen(
+ state=dict(self.state),
+ buffer_paths=[list(p) for p in paths],
+ buffers=bufs,
+ ),
+ )
+
+
@dataclass
class AutoExportState:
html: bool = False
@@ -110,10 +166,9 @@ class SessionView:
self.last_execution_time: dict[CellId_t, float] = {}
# Any stale code that was read from a file-watcher
self.stale_code: Optional[UpdateCellCodesNotification] = None
- # Model messages
- self.model_messages: dict[
- WidgetModelId, list[ModelLifecycleNotification]
- ] = {}
+ # Aggregated model state — one snapshot per live model.
+ # Updates merge in; close removes the entry.
+ self.model_states: dict[WidgetModelId, ModelReplayState] = {}
# UI element messages
self.ui_element_messages: dict[
UIElementId, list[UIElementMessageNotification]
@@ -304,10 +359,11 @@ class SessionView:
self.stale_code = notification
elif isinstance(notification, UIElementMessageNotification):
- # TODO(perf): Consider merging consecutive 'update' messages
- # to reduce replay size. Could keep only the latest state for
- # each model_id instead of the full message history.
- # TODO: cleanup old UI messages
+ # TODO: Consider reducing to a single message per element
+ # (similar to ModelReplayState) instead of keeping the full
+ # history. Currently the only user is chat streaming
+ # ("stream_chunk" messages) which are ephemeral and don't
+ # need replay at all.
ui_element_id = notification.ui_element
if ui_element_id not in self.ui_element_messages:
self.ui_element_messages[ui_element_id] = []
@@ -315,13 +371,18 @@ class SessionView:
elif isinstance(notification, ModelLifecycleNotification):
model_id = notification.model_id
- if model_id not in self.model_messages:
- self.model_messages[model_id] = []
- # TODO(perf): Consider merging consecutive 'update' messages
- # to reduce replay size. Could keep only the latest state for
- # each model_id instead of the full message history.
- # TODO: cleanup old UI messages
- self.model_messages[model_id].append(notification)
+ msg = notification.message
+ if isinstance(msg, ModelOpen):
+ self.model_states[model_id] = ModelReplayState.from_open(
+ model_id, msg
+ )
+ elif isinstance(msg, ModelUpdate):
+ view = self.model_states.get(model_id)
+ if view is not None:
+ view.apply_update(msg)
+ elif isinstance(msg, ModelClose):
+ self.model_states.pop(model_id, None)
+ # ModelCustom is ephemeral — skip for replay
elif isinstance(notification, StartupLogsNotification):
prev = self.startup_logs.content if self.startup_logs else ""
@@ -460,9 +521,8 @@ class SessionView:
# Model messages must come before cell notifications to ensure
# the model exists before the view tries to use it.
- if self.model_messages:
- for model_messages in self.model_messages.values():
- all_notifications.extend(model_messages)
+ for view in self.model_states.values():
+ all_notifications.append(view.to_notification())
if self.ui_element_messages:
for ui_messages in self.ui_element_messages.values():
diff --git a/tests/_session/state/test_session_view.py b/tests/_session/state/test_session_view.py
index 33c3b88383..e575a42bb8 100644
--- a/tests/_session/state/test_session_view.py
+++ b/tests/_session/state/test_session_view.py
@@ -20,7 +20,10 @@ from marimo._messaging.notification import (
DatasetsNotification,
DataSourceConnectionsNotification,
InstallingPackageAlertNotification,
+ ModelClose,
+ ModelCustom,
ModelLifecycleNotification,
+ ModelOpen,
ModelUpdate,
SQLMetadata,
SQLTableListPreviewNotification,
@@ -41,7 +44,7 @@ from marimo._runtime.commands import (
ExecuteCellsCommand,
UpdateUIElementCommand,
)
-from marimo._session.state.session_view import SessionView
+from marimo._session.state.session_view import ModelReplayState, SessionView
from marimo._sql.engines.duckdb import INTERNAL_DUCKDB_ENGINE
from marimo._types.ids import CellId_t, RequestId, VariableName, WidgetModelId
from marimo._utils.parse_dataclass import parse_raw
@@ -186,56 +189,231 @@ def test_ui_values(session_view: SessionView) -> None:
assert "test_ui3" in session_view.ui_values
-def test_model_message_values(session_view: SessionView) -> None:
+def test_model_open_stored(session_view: SessionView) -> None:
model_id = WidgetModelId("test_model")
- model_id2 = WidgetModelId("test_model2")
session_view.add_notification(
ModelLifecycleNotification(
model_id=model_id,
- message=ModelUpdate(
+ message=ModelOpen(
state={"key": "value"},
buffer_paths=[],
buffers=[],
),
)
)
- assert model_id in session_view.model_messages
- assert session_view.model_messages[model_id][0].message == ModelUpdate(
- state={"key": "value"}, buffer_paths=[], buffers=[]
- )
+ assert model_id in session_view.model_states
+ assert session_view.model_states[model_id].state == {"key": "value"}
+
+
+def test_model_update_merges_into_open(session_view: SessionView) -> None:
+ model_id = WidgetModelId("test_model")
- # Can add to existing model
+ session_view.add_notification(
+ ModelLifecycleNotification(
+ model_id=model_id,
+ message=ModelOpen(
+ state={"a": 1, "b": 2},
+ buffer_paths=[],
+ buffers=[],
+ ),
+ )
+ )
session_view.add_notification(
ModelLifecycleNotification(
model_id=model_id,
message=ModelUpdate(
- state={"key": "new_value"},
+ state={"b": 99, "c": 3},
buffer_paths=[],
buffers=[],
),
)
)
- assert len(session_view.model_messages[model_id]) == 2
- assert session_view.model_messages[model_id][1].message == ModelUpdate(
- state={"key": "new_value"}, buffer_paths=[], buffers=[]
+ assert session_view.model_states[model_id].state == {
+ "a": 1,
+ "b": 99,
+ "c": 3,
+ }
+
+
+def test_model_close_removes(session_view: SessionView) -> None:
+ model_id = WidgetModelId("test_model")
+
+ session_view.add_notification(
+ ModelLifecycleNotification(
+ model_id=model_id,
+ message=ModelOpen(
+ state={"key": "value"},
+ buffer_paths=[],
+ buffers=[],
+ ),
+ )
+ )
+ assert model_id in session_view.model_states
+
+ session_view.add_notification(
+ ModelLifecycleNotification(
+ model_id=model_id,
+ message=ModelClose(),
+ )
+ )
+ assert model_id not in session_view.model_states
+
+
+def test_model_custom_skipped(session_view: SessionView) -> None:
+ model_id = WidgetModelId("test_model")
+
+ session_view.add_notification(
+ ModelLifecycleNotification(
+ model_id=model_id,
+ message=ModelCustom(content={"foo": "bar"}, buffers=[]),
+ )
)
+ assert model_id not in session_view.model_states
+
+
+def test_model_update_without_open_ignored(
+ session_view: SessionView,
+) -> None:
+ model_id = WidgetModelId("test_model")
- # Can add multiple models
session_view.add_notification(
ModelLifecycleNotification(
- model_id=model_id2,
+ model_id=model_id,
message=ModelUpdate(
- state={"key2": "value2"},
+ state={"key": "value"},
buffer_paths=[],
buffers=[],
),
)
)
- assert model_id2 in session_view.model_messages
- assert session_view.model_messages[model_id2][0].message == ModelUpdate(
- state={"key2": "value2"}, buffer_paths=[], buffers=[]
- )
+ assert model_id not in session_view.model_states
+
+
+def test_model_multiple_models(session_view: SessionView) -> None:
+ model_id1 = WidgetModelId("model1")
+ model_id2 = WidgetModelId("model2")
+
+ for mid, val in [(model_id1, "v1"), (model_id2, "v2")]:
+ session_view.add_notification(
+ ModelLifecycleNotification(
+ model_id=mid,
+ message=ModelOpen(
+ state={"key": val},
+ buffer_paths=[],
+ buffers=[],
+ ),
+ )
+ )
+ assert session_view.model_states[model_id1].state == {"key": "v1"}
+ assert session_view.model_states[model_id2].state == {"key": "v2"}
+
+
+class TestModelReplayState:
+ """Unit tests for ModelReplayState buffer merging."""
+
+ def test_from_open(self) -> None:
+ model_id = WidgetModelId("m")
+ view = ModelReplayState.from_open(
+ model_id,
+ ModelOpen(
+ state={"img": None, "label": "hi"},
+ buffer_paths=[["img"]],
+ buffers=[b"png"],
+ ),
+ )
+ assert view.state == {"img": None, "label": "hi"}
+ assert view.buffers == {("img",): b"png"}
+
+ def test_apply_update_merges_state(self) -> None:
+ view = ModelReplayState(
+ model_id=WidgetModelId("m"),
+ state={"a": 1, "b": 2},
+ buffers={},
+ )
+ view.apply_update(
+ ModelUpdate(state={"b": 99, "c": 3}, buffer_paths=[], buffers=[])
+ )
+ assert view.state == {"a": 1, "b": 99, "c": 3}
+
+ def test_apply_update_replaces_buffer_for_updated_key(self) -> None:
+ view = ModelReplayState(
+ model_id=WidgetModelId("m"),
+ state={"img": None, "data": None},
+ buffers={("img",): b"png", ("data",): b"old_csv"},
+ )
+ view.apply_update(
+ ModelUpdate(
+ state={"data": None},
+ buffer_paths=[["data"]],
+ buffers=[b"new_csv"],
+ )
+ )
+ assert view.buffers == {("img",): b"png", ("data",): b"new_csv"}
+
+ def test_apply_update_removes_nested_buffer_paths(self) -> None:
+ """When a state key is updated, all buffers nested under it
+ should be dropped."""
+ view = ModelReplayState(
+ model_id=WidgetModelId("m"),
+ state={"widget": {"a": None, "b": None}},
+ buffers={
+ ("widget", "a"): b"buf_a",
+ ("widget", "b"): b"buf_b",
+ ("other",): b"keep",
+ },
+ )
+ view.apply_update(
+ ModelUpdate(
+ state={"widget": {"c": None}},
+ buffer_paths=[["widget", "c"]],
+ buffers=[b"buf_c"],
+ )
+ )
+ assert view.buffers == {
+ ("other",): b"keep",
+ ("widget", "c"): b"buf_c",
+ }
+
+ def test_apply_update_adds_new_buffer(self) -> None:
+ view = ModelReplayState(
+ model_id=WidgetModelId("m"),
+ state={"label": "hi"},
+ buffers={},
+ )
+ view.apply_update(
+ ModelUpdate(
+ state={"img": None},
+ buffer_paths=[["img"]],
+ buffers=[b"png"],
+ )
+ )
+ assert view.buffers == {("img",): b"png"}
+ assert view.state == {"label": "hi", "img": None}
+
+ def test_to_notification_round_trips(self) -> None:
+ model_id = WidgetModelId("m")
+ view = ModelReplayState(
+ model_id=model_id,
+ state={"img": None, "data": None, "label": "hi"},
+ buffers={("img",): b"png", ("data",): b"csv"},
+ )
+ notif = view.to_notification()
+ assert notif.model_id == model_id
+ assert isinstance(notif.message, ModelOpen)
+ assert notif.message.state == {
+ "img": None,
+ "data": None,
+ "label": "hi",
+ }
+ # buffer_paths and buffers should be parallel and match
+ path_buf = dict(
+ zip(
+ [tuple(p) for p in notif.message.buffer_paths],
+ notif.message.buffers,
+ )
+ )
+ assert path_buf == {("img",): b"png", ("data",): b"csv"}
def test_last_run_code(session_view: SessionView) -> None: