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
Drop dangling @file URLs from the session cache (#9278)
Trevor Manz970d0399b8d5
diff --git a/marimo/_cli/development/commands.py b/marimo/_cli/development/commands.pyindex 32364a942e..5103065800 100644--- a/marimo/_cli/development/commands.py+++ b/marimo/_cli/development/commands.py@@ -790,6 +790,7 @@ def preview(file_path: Path, port: int, host: str, headless: bool) -> None:session_snapshot = serialize_session_view(session_view,cell_ids=list(file_manager.app.cell_manager.cell_ids()),+ drop_virtual_file_outputs=False,)# Get notebook snapshot from file managerdiff --git a/marimo/_server/export/_session_cache.py b/marimo/_server/export/_session_cache.pyindex a3911e6638..0babcf1775 100644--- a/marimo/_server/export/_session_cache.py+++ b/marimo/_server/export/_session_cache.py@@ -65,6 +65,7 @@ def serialize_session_snapshot(view,cell_ids=cell_ids,script_metadata_hash=get_script_metadata_hash(notebook_path),+ drop_virtual_file_outputs=True,)diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.pyindex 7f730a565e..c39bbef951 100644--- a/marimo/_server/export/exporter.py+++ b/marimo/_server/export/exporter.py@@ -88,7 +88,9 @@ class Exporter:# Serialize notebook statesession_snapshot = serialize_session_view(- session_view, cell_ids=app.cell_manager.cell_ids()+ session_view,+ cell_ids=app.cell_manager.cell_ids(),+ drop_virtual_file_outputs=False,)notebook_snapshot = serialize_notebook(session_view, app.cell_manager)diff --git a/marimo/_session/state/serialize.py b/marimo/_session/state/serialize.pyindex 5db465ca0b..4572161e68 100644--- a/marimo/_session/state/serialize.py+++ b/marimo/_session/state/serialize.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport asyncioimport jsonimport os+import refrom dataclasses import dataclassfrom pathlib import Pathfrom typing import TYPE_CHECKING, Any, cast@@ -52,6 +53,30 @@ if TYPE_CHECKING:LOGGER = _loggers.marimo_logger()+# Matches the runtime's virtual-file URL shape: `./@file/<bytes>-<name>`+# (see `marimo/_runtime/virtual_file/virtual_file.py`). Anchored to the+# byte-length digits so a literal "./@file/" mention in user content+# doesn't trip the check.+_VIRTUAL_FILE_URL_RE = re.compile(r"\./@file/\d+-")+++def _references_virtual_file(data: Any) -> bool:+ """Return True if `data` references a virtual-file URL.++ Virtual files (`./@file/<bytes>-<name>`) are backed by per-process+ storage that disappears on kernel restart, so any cached output that+ embeds one would 404 on replay (e.g. anywidget HTML losing its binary+ state — see #9273).+ """+ if isinstance(data, str):+ return _VIRTUAL_FILE_URL_RE.search(data) is not None+ if isinstance(data, dict):+ return any(_references_virtual_file(v) for v in data.values())+ if isinstance(data, list):+ return any(_references_virtual_file(v) for v in data)+ return False++def _normalize_error(error: MarimoError | dict[str, Any]) -> ErrorOutput:"""Normalize error to consistent format."""if isinstance(error, dict):@@ -80,6 +105,8 @@ def _normalize_error(error: MarimoError | dict[str, Any]) -> ErrorOutput:def serialize_session_view(view: SessionView,cell_ids: Iterable[CellId_t],+ *,+ drop_virtual_file_outputs: bool,script_metadata_hash: str | None = None,) -> NotebookSessionV1:"""Convert a SessionView to a NotebookSession schema.@@ -88,6 +115,20 @@ def serialize_session_view(the NotebookSession schema (and only these cells will be saved to theschema). When not provided, this method attempts to recover the notebookorder from the SessionView object, but this is not always possible.++ `./@file/...` URLs are backed by per-process buffers, so+ `drop_virtual_file_outputs` depends on where the snapshot will be+ consumed:++ - `True` when the snapshot will be replayed in a *different*+ process from the one that produced it. The buffers are gone, so+ surviving URLs would 404 on replay (#9273); dropping them leaves+ the cell un-run until the kernel re-executes it. Used by the+ on-disk session cache.+ - `False` when the snapshot will be consumed in the *same* process+ while buffers are still live, and the caller resolves the URLs+ itself before they're released — e.g. HTML export inlining them+ as `data:` URLs."""cells: list[Cell] = []@@ -110,7 +151,10 @@ def serialize_session_view(cell_notif.output.data,):outputs.append(_normalize_error(error))- else:+ elif not (+ drop_virtual_file_outputs+ and _references_virtual_file(cell_notif.output.data)+ ):outputs.append(DataOutput(type="data",@@ -413,6 +457,7 @@ class SessionCacheWriter(AsyncBackgroundTask):script_metadata_hash=_script_metadata_hash(self.notebook_path),+ drop_virtual_file_outputs=True,)if isinstance(self.path, AsyncPath):await self.path.write_text(json.dumps(data, indent=2))diff --git a/tests/_session/state/test_serialize_session.py b/tests/_session/state/test_serialize_session.pyindex 364e61b3ef..fc2d824c72 100644--- a/tests/_session/state/test_serialize_session.py+++ b/tests/_session/state/test_serialize_session.py@@ -88,7 +88,9 @@ def test_serialize_basic_session(session_view: SessionView):)view.last_executed_code[CELL1] = "print('Hello, world!')"- result = serialize_session_view(view, cell_ids=[CELL1])+ result = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )snapshot("basic_session.json", json.dumps(result, indent=2))@@ -107,7 +109,9 @@ def test_serialize_session_with_error(session_view: SessionView):"raise RuntimeError('Something went wrong')")- result = serialize_session_view(view, cell_ids=[CELL1])+ result = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )snapshot("error_session.json", json.dumps(result, indent=2))@@ -131,7 +135,9 @@ def test_serialize_session_with_console(session_view: SessionView):)view.last_executed_code[CELL1] = "print('test')"- result = serialize_session_view(view, cell_ids=[CELL1])+ result = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )snapshot("console_session.json", json.dumps(result, indent=2))@@ -151,7 +157,9 @@ def test_serialize_session_with_mime_bundle(session_view: SessionView):)view.last_executed_code[CELL1] = "HTML('Hello')"- result = serialize_session_view(view, cell_ids=[CELL1])+ result = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )snapshot("mime_bundle_session.json", json.dumps(result, indent=2))@@ -382,6 +390,124 @@ def test_serialize_notebook_missing_cell_data(session_view: SessionView):assert cell["config"]["hide_code"] is None+def test_session_round_trip_drops_dangling_virtual_file_urls(+ session_view: SessionView,+):+ # Regression: https://github.com/marimo-team/marimo/issues/9273.+ # A `./@file/...` URL in cached HTML output points at a per-process+ # buffer that won't exist on the next kernel; the URL must not+ # survive the on-disk cache round-trip.+ html = (+ '<marimo-anywidget data-name="Counter">'+ '<img src="./@file/4-abcd1234.png">'+ "</marimo-anywidget>"+ )+ view = session_view+ view.cell_notifications[CELL1] = _make_cell_notification(+ CELL1,+ output=CellOutput(+ channel=CellChannel.OUTPUT,+ mimetype="text/html",+ data=html,+ ),+ )+ view.last_executed_code[CELL1] = "widget"++ serialized = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=True+ )+ code_hash_to_cell_id = _build_code_hash_to_cell_id_mapping(serialized)+ restored = deserialize_session(serialized, code_hash_to_cell_id)++ assert restored.cell_notifications[CELL1].output is None+++def test_session_round_trip_drops_nested_virtual_file_urls(+ session_view: SessionView,+):+ # `_references_virtual_file` recurses through dicts and lists — a mime+ # bundle where only one alternative carries the `./@file/` URL must+ # still trigger the drop.+ view = session_view+ view.cell_notifications[CELL1] = _make_cell_notification(+ CELL1,+ output=CellOutput(+ channel=CellChannel.OUTPUT,+ mimetype="application/vnd.marimo+mimebundle",+ data={+ "text/plain": "widget",+ "text/html": '<img src="./@file/4-abcd1234.png">',+ },+ ),+ )+ view.last_executed_code[CELL1] = "widget"++ serialized = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=True+ )+ code_hash_to_cell_id = _build_code_hash_to_cell_id_mapping(serialized)+ restored = deserialize_session(serialized, code_hash_to_cell_id)++ assert restored.cell_notifications[CELL1].output is None+++def test_drop_virtual_file_outputs_ignores_literal_prefix_in_text(+ session_view: SessionView,+):+ # The check is anchored to the full URL shape (`./@file/<digits>-`),+ # so plain text that happens to mention the prefix must not trigger+ # the drop.+ view = session_view+ view.cell_notifications[CELL1] = _make_cell_notification(+ CELL1,+ output=CellOutput(+ channel=CellChannel.OUTPUT,+ mimetype="text/plain",+ data="marimo stores virtual files under ./@file/ on the server",+ ),+ )+ view.last_executed_code[CELL1] = "print('docs')"++ serialized = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=True+ )+ code_hash_to_cell_id = _build_code_hash_to_cell_id_mapping(serialized)+ restored = deserialize_session(serialized, code_hash_to_cell_id)++ restored_output = restored.cell_notifications[CELL1].output+ assert restored_output is not None+ assert restored_output.data == (+ "marimo stores virtual files under ./@file/ on the server"+ )+++def test_drop_virtual_file_outputs_preserves_unrelated_outputs(+ session_view: SessionView,+):+ # The drop is targeted: outputs without a virtual-file URL must pass+ # through even when `drop_virtual_file_outputs=True`.+ view = session_view+ view.cell_notifications[CELL1] = _make_cell_notification(+ CELL1,+ output=CellOutput(+ channel=CellChannel.OUTPUT,+ mimetype="text/html",+ data="<b>Hello</b>",+ ),+ )+ view.last_executed_code[CELL1] = "HTML('Hello')"++ serialized = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=True+ )+ code_hash_to_cell_id = _build_code_hash_to_cell_id_mapping(serialized)+ restored = deserialize_session(serialized, code_hash_to_cell_id)++ restored_output = restored.cell_notifications[CELL1].output+ assert restored_output is not None+ assert restored_output.data == "<b>Hello</b>"++def test_deserialize_basic_session():"""Test deserialization of a basic session"""session = NotebookSessionV1(@@ -762,7 +888,9 @@ def test_serialize_session_with_dict_error():"raise RuntimeError('Something went wrong')")- result = serialize_session_view(view, cell_ids=[CELL1])+ result = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )assert len(result["cells"]) == 1assert len(result["cells"][0]["outputs"]) == 1assert result["cells"][0]["outputs"][0]["type"] == "error"@@ -798,7 +926,9 @@ def test_serialize_session_with_mixed_error_formats(session_view: SessionView):)view.last_executed_code[CELL1] = "# code that causes mixed errors"- result = serialize_session_view(view, cell_ids=[CELL1])+ result = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )# Verify the error normalization worked correctlyassert len(result["cells"]) == 1@@ -900,7 +1030,9 @@ class TestSessionCacheManager:cache_file.parent.mkdir(parents=True)# Write cache file- data = serialize_session_view(view, cell_ids=[CELL1])+ data = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )cache_file.write_text(json.dumps(data))# Read back@@ -938,7 +1070,9 @@ class TestSessionCacheManager:cache_file.parent.mkdir(parents=True)# Write cache file- data = serialize_session_view(view, cell_ids=[CELL1])+ data = serialize_session_view(+ view, cell_ids=[CELL1], drop_virtual_file_outputs=False+ )cache_file.write_text(json.dumps(data))# Read back@@ -970,7 +1104,9 @@ class TestSessionCacheManager:cache_file.parent.mkdir(parents=True)# Write cache file- data = serialize_session_view(view, cell_ids=[id1, id2])+ data = serialize_session_view(+ view, cell_ids=[id1, id2], drop_virtual_file_outputs=False+ )cache_file.write_text(json.dumps(data))# Read back@@ -1004,7 +1140,10 @@ class TestSessionCacheManager:cache_file.parent.mkdir(parents=True)data = serialize_session_view(- view, cell_ids=[id1, id2], script_metadata_hash="old"+ view,+ cell_ids=[id1, id2],+ script_metadata_hash="old",+ drop_virtual_file_outputs=False,)cache_file.write_text(json.dumps(data))@@ -1050,7 +1189,9 @@ class TestSessionCacheManager:cache_file.parent.mkdir(parents=True)# Write cache file- data = serialize_session_view(view, cell_ids=[CELL1, CELL2])+ data = serialize_session_view(+ view, cell_ids=[CELL1, CELL2], drop_virtual_file_outputs=False+ )cache_file.write_text(json.dumps(data))# Read backdiff --git a/tests/_session/state/test_serialize_session_missing_type.py b/tests/_session/state/test_serialize_session_missing_type.pyindex ae96d2aa28..896aad6ec9 100644--- a/tests/_session/state/test_serialize_session_missing_type.py+++ b/tests/_session/state/test_serialize_session_missing_type.py@@ -29,7 +29,9 @@ def test_serialize_session_with_dict_error_missing_type():"raise RuntimeError('Something went wrong')")- result = serialize_session_view(view, cell_ids=[CELL_ID])+ result = serialize_session_view(+ view, cell_ids=[CELL_ID], drop_virtual_file_outputs=False+ )assert len(result["cells"]) == 1assert len(result["cells"][0]["outputs"]) == 1assert result["cells"][0]["outputs"][0]["type"] == "error"