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.

Remove legacy session stream framing (#11378)

Trevor Manz9e4075f10623
diff --git a/frontend/src/core/runtime/__tests__/createWsUrl.test.ts b/frontend/src/core/runtime/__tests__/createWsUrl.test.ts
index 91e196992a..6159eeadc4 100644
--- a/frontend/src/core/runtime/__tests__/createWsUrl.test.ts
+++ b/frontend/src/core/runtime/__tests__/createWsUrl.test.ts
@@ -13,9 +13,7 @@ describe("RuntimeManager.getWsURL", () => {
});
const sessionId = "1234" as SessionId;
const url = runtime.getWsURL(sessionId);
- expect(url.toString()).toBe(
- "wss://marimo.app/ws?session_id=1234&message_format=host",
- );
+ expect(url.toString()).toBe("wss://marimo.app/ws?session_id=1234");
expect(url.searchParams.get(KnownQueryParams.sessionId)).toBe(sessionId);
});
@@ -26,9 +24,7 @@ describe("RuntimeManager.getWsURL", () => {
});
const sessionId = "1234" as SessionId;
const url = runtime.getWsURL(sessionId);
- expect(url.toString()).toBe(
- "ws://marimo.app/ws?session_id=1234&message_format=host",
- );
+ expect(url.toString()).toBe("ws://marimo.app/ws?session_id=1234");
expect(url.searchParams.get(KnownQueryParams.sessionId)).toBe(sessionId);
});
@@ -39,9 +35,7 @@ describe("RuntimeManager.getWsURL", () => {
});
const sessionId = "1234" as SessionId;
const url = runtime.getWsURL(sessionId);
- expect(url.toString()).toBe(
- "ws://marimo.app/nested/ws?session_id=1234&message_format=host",
- );
+ expect(url.toString()).toBe("ws://marimo.app/nested/ws?session_id=1234");
expect(url.searchParams.get(KnownQueryParams.sessionId)).toBe(sessionId);
});
@@ -54,7 +48,7 @@ describe("RuntimeManager.getWsURL", () => {
const sessionId = "1234" as SessionId;
const url = runtime.getWsURL(sessionId);
expect(url.toString()).toBe(
- "ws://marimo.app/nested/ws?foo=bar&file=test.py&session_id=1234&message_format=host",
+ "ws://marimo.app/nested/ws?foo=bar&file=test.py&session_id=1234",
);
expect(url.searchParams.get(KnownQueryParams.sessionId)).toBe(sessionId);
});
@@ -68,7 +62,7 @@ describe("RuntimeManager.getWsURL", () => {
const sessionId = "1234" as SessionId;
const url = runtime.getWsURL(sessionId);
expect(url.toString()).toBe(
- "ws://marimo.app/ws?file=test.py&search=test&other=last&session_id=1234&message_format=host",
+ "ws://marimo.app/ws?file=test.py&search=test&other=last&session_id=1234",
);
expect(url.searchParams.get("file")).toBe("test.py");
expect(url.searchParams.get("search")).toBe("test");
diff --git a/frontend/src/core/runtime/__tests__/runtime.test.ts b/frontend/src/core/runtime/__tests__/runtime.test.ts
index ad63b9e815..831d3537ed 100644
--- a/frontend/src/core/runtime/__tests__/runtime.test.ts
+++ b/frontend/src/core/runtime/__tests__/runtime.test.ts
@@ -60,7 +60,6 @@ describe("RuntimeManager", () => {
expect(url.protocol).toBe("wss:");
expect(url.hostname).toBe("example.com");
expect(url.pathname).toBe("/ws");
- expect(url.searchParams.get("message_format")).toBe("host");
expect(url.searchParams.has("types")).toBe(false);
expect(url.searchParams.get("session_id")).toBe(sessionId);
});
@@ -236,7 +235,6 @@ describe("RuntimeManager", () => {
);
const url = runtime.getSseURL("s_123" as SessionId);
- expect(url.searchParams.get("message_format")).toBe("host");
expect(url.searchParams.get("access_token")).toBeNull();
expect(url.toString()).not.toContain("my-secret-token");
});
@@ -740,7 +738,7 @@ describe("RuntimeManager", () => {
const sessionId = "test" as SessionId;
expect(runtime.getWsURL(sessionId).toString()).toMatchInlineSnapshot(
- `"blob:https://example.com/12345678-1234-1234-1234-123456789abc?session_id=test&message_format=host"`,
+ `"blob:https://example.com/12345678-1234-1234-1234-123456789abc?session_id=test"`,
);
expect(Logger.warn).toHaveBeenCalledOnce();
});
diff --git a/frontend/src/core/runtime/runtime.ts b/frontend/src/core/runtime/runtime.ts
index 5daf064ad9..a986960c11 100644
--- a/frontend/src/core/runtime/runtime.ts
+++ b/frontend/src/core/runtime/runtime.ts
@@ -154,9 +154,7 @@ export class RuntimeManager {
*/
public getWsURL(sessionId: SessionId): URL {
const searchParams = this.getSessionSearchParams(sessionId);
- const url = this.formatWsURL("/ws", searchParams);
- url.searchParams.set("message_format", "host");
- return url;
+ return this.formatWsURL("/ws", searchParams);
}
/**
@@ -171,7 +169,6 @@ export class RuntimeManager {
searchParams: this.getSessionSearchParams(sessionId),
restrictToKnownQueryParams: false,
});
- url.searchParams.set("message_format", "host");
// The page URL can still carry access_token (merged in from the
// current page's params, before auth cleanup strips it); never
// forward it — a token in the URL leaks into proxy/server logs.
diff --git a/marimo/_runtime/params.py b/marimo/_runtime/params.py
index c0741c003f..50be6cf658 100644
--- a/marimo/_runtime/params.py
+++ b/marimo/_runtime/params.py
@@ -38,7 +38,6 @@ class QueryParams(State[SerializedQueryParams]):
"refresh_token",
"session_id",
"cursor",
- "message_format",
"types",
}
diff --git a/marimo/_server/api/endpoints/ws_endpoint.py b/marimo/_server/api/endpoints/ws_endpoint.py
index 6c073d94e1..09f385fc7c 100644
--- a/marimo/_server/api/endpoints/ws_endpoint.py
+++ b/marimo/_server/api/endpoints/ws_endpoint.py
@@ -77,9 +77,6 @@ async def websocket_endpoint(
async def _session_events(
connection: WebSocket | Request, params: ConnectionParams
) -> SessionEventStream:
- message_format = connection.query_params.get("message_format", "session")
- if message_format not in ("host", "session"):
- raise ValueError("message_format must be host or session")
cursor = stream_options.cursor(connection)
types = stream_options.types(connection)
if "run_id" in connection.query_params:
@@ -94,7 +91,6 @@ async def _session_events(
doc_manager=DOC_MANAGER,
types=types,
cursor=cursor,
- message_format="host" if message_format == "host" else "session",
)
diff --git a/marimo/_server/host/session.py b/marimo/_server/host/session.py
index 6c04921d06..14ac487174 100644
--- a/marimo/_server/host/session.py
+++ b/marimo/_server/host/session.py
@@ -6,17 +6,14 @@ from __future__ import annotations
import asyncio
from contextlib import aclosing
from dataclasses import dataclass
-from typing import TYPE_CHECKING, Literal
+from typing import TYPE_CHECKING
from starlette.websockets import WebSocket, WebSocketDisconnect, WebSocketState
from marimo._core import protocol
from marimo._core.stream import RoutedEvent
from marimo._messaging.notification import KernelStartupErrorNotification
-from marimo._messaging.serde import (
- deserialize_kernel_message,
- serialize_kernel_message,
-)
+from marimo._messaging.serde import deserialize_kernel_message
from marimo._server.api.endpoints.ws.notification_filter import (
should_filter_operation,
)
@@ -32,7 +29,6 @@ from marimo._server.host.events import (
from marimo._server.sse import (
HEARTBEAT_EVENT,
format_close_event,
- format_sse_event,
wait_for_http_disconnect,
)
from marimo._session.managers.ipc import KernelStartupError
@@ -75,7 +71,6 @@ class SessionEventStream(SessionHandler):
types: set[str] | None,
cursor: str | None,
heartbeat_seconds: float = 20.0,
- message_format: Literal["host", "session"] = "host",
) -> None:
super().__init__(
manager=manager,
@@ -83,7 +78,6 @@ class SessionEventStream(SessionHandler):
mode=manager.mode,
doc_manager=doc_manager,
)
- self.message_format = message_format
self.connection = connection
self.heartbeat_seconds = heartbeat_seconds
self._stream_finished = False
@@ -218,15 +212,7 @@ class SessionEventStream(SessionHandler):
if self.notebook is not None and self._attachment is not None:
self.notebook.detach(self._attachment.id)
- def _serialize(self, event: RoutedEvent) -> str | None:
- # Temporary wire compatibility; lifecycle and recipient filtering are
- # shared with host delivery. Remove after the frontend cutover.
- if self.message_format == "session":
- if not isinstance(event.event, MarimoEvent):
- return None
- notification = event.event.data.marimo
- data = serialize_kernel_message(notification).decode()
- return f'{{"op": "{notification.name}", "data": {data}}}'
+ def _serialize(self, event: RoutedEvent) -> str:
return serialize_event(
event, include_code=self.manager.should_send_code_to_frontend()
)
@@ -246,9 +232,7 @@ class SessionEventStream(SessionHandler):
):
await websocket.close(event.code, event.reason)
elif event is not None:
- payload = self._serialize(event)
- if payload is not None:
- await websocket.send_text(payload)
+ await websocket.send_text(self._serialize(event))
except WebSocketDisconnect:
pass
@@ -260,10 +244,6 @@ class SessionEventStream(SessionHandler):
yield format_close_event(event.code, event.reason)
elif event is None:
yield HEARTBEAT_EVENT
- elif self.message_format == "session":
- payload = self._serialize(event)
- if payload is not None:
- yield format_sse_event(payload)
else:
yield serialize_sse_event(
event,
diff --git a/tests/_cli/_pair_server.py b/tests/_cli/_pair_server.py
index 5172be7e0b..a7482995a5 100644
--- a/tests/_cli/_pair_server.py
+++ b/tests/_cli/_pair_server.py
@@ -147,8 +147,11 @@ def _wait_for_kernel_ready(
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("kernel did not become ready")
- message = json.loads(websocket.recv(timeout=remaining))
- if message.get("op") == "kernel-ready":
+ event = json.loads(websocket.recv(timeout=remaining))
+ if event["event"] != "marimo":
+ continue
+ message = event["data"]["marimo"]
+ if message["op"] == "kernel-ready":
return
diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.py
index faa1b801e0..9ffc9ab156 100644
--- a/tests/_cli/test_cli.py
+++ b/tests/_cli/test_cli.py
@@ -1532,7 +1532,10 @@ def test_editor_sandbox_supplies_server_tools(
f"ws://{DEFAULT_HOST}:{port}/ws?{query}"
) as websocket:
while True:
- message = json.loads(websocket.recv(timeout=60))
+ event = json.loads(websocket.recv(timeout=60))
+ if event["event"] != "marimo":
+ continue
+ message = event["data"]["marimo"]
assert message["op"] != "kernel-startup-error", message
if message["op"] == "kernel-ready":
break
diff --git a/tests/_server/api/endpoints/test_health.py b/tests/_server/api/endpoints/test_health.py
index 47eb3ce974..5774a5d077 100644
--- a/tests/_server/api/endpoints/test_health.py
+++ b/tests/_server/api/endpoints/test_health.py
@@ -78,8 +78,7 @@ def test_sessions_stable_id_survives_reconnect(
client: TestClient, connection_id: str
) -> None:
with client.websocket_connect(
- f"/ws?message_format=host&session_id={SESSION_ID}",
- headers=token_header(),
+ f"/ws?session_id={SESSION_ID}", headers=token_header()
) as websocket:
snapshot = socket_snapshot(websocket)
notebook_id = next(
@@ -103,8 +102,7 @@ def test_sessions_stable_id_survives_reconnect(
assert response.json() == {SESSION_ID: expected}
with client.websocket_connect(
- f"/ws?message_format=host&session_id={connection_id}",
- headers=token_header(),
+ f"/ws?session_id={connection_id}", headers=token_header()
) as websocket:
assert receive_notification(websocket)["op"] == "reconnected"
response = client.get("/api/sessions", headers=token_header())
@@ -114,8 +112,7 @@ def test_sessions_stable_id_survives_reconnect(
def test_sessions_stable_id_changes_after_restart(client: TestClient) -> None:
with client.websocket_connect(
- f"/ws?message_format=host&session_id={SESSION_ID}",
- headers=token_header(),
+ f"/ws?session_id={SESSION_ID}", headers=token_header()
) as websocket:
assert receive_notification(websocket)["op"] == "kernel-ready"
response = client.get("/api/sessions", headers=token_header())
@@ -129,8 +126,7 @@ def test_sessions_stable_id_changes_after_restart(client: TestClient) -> None:
assert response.json() == {}
with client.websocket_connect(
- f"/ws?message_format=host&session_id={SESSION_ID}",
- headers=token_header(),
+ f"/ws?session_id={SESSION_ID}", headers=token_header()
) as websocket:
assert receive_notification(websocket)["op"] == "kernel-ready"
response = client.get("/api/sessions", headers=token_header())
diff --git a/tests/_server/api/endpoints/test_home.py b/tests/_server/api/endpoints/test_home.py
index bffe57a27e..669a26d743 100644
--- a/tests/_server/api/endpoints/test_home.py
+++ b/tests/_server/api/endpoints/test_home.py
@@ -641,7 +641,6 @@ def test_open_notebook_connects_and_reconnects_to_host(
"session_id": SESSION_ID,
"file": path,
"access_token": "fake-token",
- "message_format": "host",
}
)
notebook = None
diff --git a/tests/_server/api/endpoints/test_sse.py b/tests/_server/api/endpoints/test_sse.py
index 67a371c6c0..f9e2923f1a 100644
--- a/tests/_server/api/endpoints/test_sse.py
+++ b/tests/_server/api/endpoints/test_sse.py
@@ -113,7 +113,7 @@ class SSETestConnection:
"""Skip host model events when asserting session lifecycle notifications."""
while True:
record = await self.next_event()
- if record["event"] in ("close", "comment", "message"):
+ if record["event"] in ("close", "comment"):
return record
if record["event"] == "marimo":
notification = json.loads(record["data"])["marimo"]
@@ -186,10 +186,9 @@ async def _expect_close(
assert json.loads(event["data"]) == {"code": code, "reason": reason}
[email protected]("format_query", ["", "&message_format=session"])
-async def test_sse_kernel_ready(client: TestClient, format_query: str) -> None:
- async with _connect(client, SSE_QUERY + format_query) as connection:
- event = await connection.next_event()
+async def test_sse_kernel_ready(client: TestClient) -> None:
+ async with _connect(client) as connection:
+ event = await connection.next_notification()
assert connection.status == 200
assert connection.headers["content-type"].startswith(
"text/event-stream"
@@ -703,9 +702,7 @@ async def test_sse_host_snapshot_and_cursor_replay(
manager = get_session_manager(client)
manager.mode = mode
manager.include_code = False
- query = (
- SSE_QUERY + "&message_format=host&types=notebook,cell,custom,marimo"
- )
+ query = SSE_QUERY + "&types=notebook,cell,custom,marimo"
async with _connect(client, query) as connection:
snapshot = []
while True:
@@ -744,8 +741,7 @@ async def test_sse_host_snapshot_and_cursor_replay(
@pytest.mark.parametrize(
- "options",
- ["cursor=", "types=invalid", "run_id=unknown", "message_format=invalid"],
+ "options", ["cursor=", "types=invalid", "run_id=unknown"]
)
async def test_sse_rejects_invalid_subscription_before_startup(
client: TestClient, options: str
diff --git a/tests/_server/api/endpoints/test_ws.py b/tests/_server/api/endpoints/test_ws.py
index b7df000101..73b5246a87 100644
--- a/tests/_server/api/endpoints/test_ws.py
+++ b/tests/_server/api/endpoints/test_ws.py
@@ -55,11 +55,9 @@ WS_URL = _create_ws_url("123")
OTHER_WS_URL = _create_ws_url("456")
[email protected]("format_query", ["", "&message_format=session"])
-def test_ws(client: TestClient, format_query: str) -> None:
- with client.websocket_connect(WS_URL + format_query) as websocket:
- # Legacy clients must see their notification first, with no host frames.
- data = websocket.receive_json()
+def test_ws(client: TestClient) -> None:
+ with client.websocket_connect(WS_URL) as websocket:
+ data = receive_notification(websocket)
assert_kernel_ready_response(data)
diff --git a/tests/_server/api/endpoints/ws_helpers.py b/tests/_server/api/endpoints/ws_helpers.py
index 76d5a1910e..8f77364064 100644
--- a/tests/_server/api/endpoints/ws_helpers.py
+++ b/tests/_server/api/endpoints/ws_helpers.py
@@ -20,8 +20,6 @@ def receive_notification(websocket: WebSocketTestSession) -> dict[str, Any]:
"""Read a private session notification from the host event stream."""
while True:
event = websocket.receive_json()
- if "op" in event:
- return event
assert "event" in event
if event["event"] == "marimo":
notification = event["data"]["marimo"]
diff --git a/tests/_server/host/test_app_websocket.py b/tests/_server/host/test_app_websocket.py
index 316e718e99..a06119d6e5 100644
--- a/tests/_server/host/test_app_websocket.py
+++ b/tests/_server/host/test_app_websocket.py
@@ -46,7 +46,7 @@ def app_host(client: TestClient) -> Iterator[SessionBridge]:
def url(session_id: str) -> str:
- return f"/ws?message_format=host&session_id={session_id}&access_token=fake-token"
+ return f"/ws?session_id={session_id}&access_token=fake-token"
@pytest.mark.parametrize("include_code", [False, True])
@@ -156,9 +156,7 @@ def test_app_host_keeps_authentication(
client: TestClient, app_host: SessionBridge
) -> None:
with pytest.raises(WebSocketDisconnect) as exc:
- with client.websocket_connect(
- "/ws?message_format=host&session_id=alice"
- ):
+ with client.websocket_connect("/ws?session_id=alice"):
pass
assert exc.value.code == WebSocketCodes.UNAUTHORIZED
assert not app_host.host.notebooks
diff --git a/tests/_server/host/test_editor_websocket.py b/tests/_server/host/test_editor_websocket.py
index 335c0a73f3..63d8bf5a4c 100644
--- a/tests/_server/host/test_editor_websocket.py
+++ b/tests/_server/host/test_editor_websocket.py
@@ -51,7 +51,7 @@ def editor_host(client: TestClient) -> Iterator[SessionBridge]:
def socket_url(bridge: SessionBridge, session_id: str = "s1") -> str:
(notebook_id,) = bridge.host.notebooks
- return f"{HOST_API_PATH}/notebooks/{notebook_id}/events/ws?message_format=host&session_id={session_id}&access_token=fake-token"
+ return f"{HOST_API_PATH}/notebooks/{notebook_id}/events/ws?session_id={session_id}&access_token=fake-token"
def receive_notification(
@@ -420,7 +420,7 @@ def test_editor_resumes_host_events_without_replaying_private_notifications(
client: TestClient, editor_host: SessionBridge, session_route: bool
) -> None:
endpoint = (
- "/ws?message_format=host&session_id=s1&access_token=fake-token"
+ "/ws?session_id=s1&access_token=fake-token"
if session_route
else socket_url(editor_host)
)
@@ -477,7 +477,7 @@ def test_untitled_editor_bootstraps_and_reconnects_with_host_events(
client.app.state.host_context = HostContext(
bridge=bridge, token="host-token", port=1234
)
- url = "/ws?message_format=host&session_id=new&access_token=fake-token"
+ url = "/ws?session_id=new&access_token=fake-token"
with client:
assert client.portal is not None
client.portal.call(bridge.open)
diff --git a/tests/_server/test_asgi.py b/tests/_server/test_asgi.py
index e306cfadaa..4ffb7bee20 100644
--- a/tests/_server/test_asgi.py
+++ b/tests/_server/test_asgi.py
@@ -87,10 +87,10 @@ class TestASGIAppBuilder(unittest.TestCase):
try:
with (
client.websocket_connect(
- "/one/ws?message_format=host&session_id=visitor"
+ "/one/ws?session_id=visitor"
) as one,
client.websocket_connect(
- "/two/ws?message_format=host&session_id=visitor"
+ "/two/ws?session_id=visitor"
) as two,
):
snapshots = [socket_snapshot(ws) for ws in (one, two)]
@@ -472,9 +472,7 @@ class TestASGIAppBuilder(unittest.TestCase):
assert "http" in invoked_for
# WebSocket should also have user/meta in scope.
- with client.websocket_connect(
- "/ws?message_format=host&session_id=test123"
- ) as ws:
+ with client.websocket_connect("/ws?session_id=test123") as ws:
data = ws.receive_text()
assert data # kernel-ready message
finally:
diff --git a/tests/_server/test_scratchpad_integration.py b/tests/_server/test_scratchpad_integration.py
index 87982ebffd..6f4cf07f39 100644
--- a/tests/_server/test_scratchpad_integration.py
+++ b/tests/_server/test_scratchpad_integration.py
@@ -188,7 +188,10 @@ class _Session:
if remaining <= 0:
raise TimeoutError("websocket predicate never satisfied")
raw = self.ws.recv(timeout=remaining)
- msg: dict[str, Any] = json.loads(raw)
+ event = json.loads(raw)
+ if event["event"] != "marimo":
+ continue
+ msg: dict[str, Any] = event["data"]["marimo"]
if predicate(msg):
return msg