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.

Clarify edit response revisions and reset stale cursors (#11360)

Trevor Manzf9b8e10dfc3f
diff --git a/frontend/src/core/host/protocol.ts b/frontend/src/core/host/protocol.ts
index 8b0343aded..f2d643f585 100644
--- a/frontend/src/core/host/protocol.ts
+++ b/frontend/src/core/host/protocol.ts
@@ -1892,7 +1892,9 @@ export const EditResponse = zod
"The version of each cell the request created or changed, by id, and null for each cell it deleted.",
),
})
- .describe("The document after an edit or a save, and the cells it touched.");
+ .describe(
+ "The document as an edit or save left it, or at a later revision, and the cells it touched.",
+ );
export type EditResponse = zod.input<typeof EditResponse>;
export type EditResponseOutput = zod.output<typeof EditResponse>;
diff --git a/marimo/_core/host.py b/marimo/_core/host.py
index 910c4cc029..de940bf5ef 100644
--- a/marimo/_core/host.py
+++ b/marimo/_core/host.py
@@ -295,7 +295,7 @@ class Host:
def events(
self,
- cursor: int | None = None,
+ cursor: str | None = None,
*,
notebook_id: str | None = None,
types: Collection[str] | None = None,
@@ -303,8 +303,9 @@ class Host:
) -> list[RoutedEvent]:
"""Return a snapshot or replay followed by `ready`.
- A cursor requests the events after it. An unavailable cursor returns
- `reset` and a snapshot.
+ A cursor requests the events after it. A cursor this stream did not
+ issue, or whose events it no longer has, returns `reset` and a
+ snapshot.
"""
scope = None
if notebook_id is not None:
@@ -312,12 +313,13 @@ class Host:
raise NotFound(f"no notebook {notebook_id}")
scope = self._records[notebook_id].id
ready = RoutedEvent(
- protocol.ReadyEvent(data={}, id=str(self._stream.last_id))
+ protocol.ReadyEvent(data={}, id=self._stream.cursor)
)
if cursor is None:
events = self._snapshot(scope)
else:
- missed = self._stream.since(cursor)
+ position = self._stream.position(cursor)
+ missed = None if position is None else self._stream.since(position)
if missed is None:
events = [
RoutedEvent(protocol.ResetEvent(data={})),
diff --git a/marimo/_core/stream.py b/marimo/_core/stream.py
index 706c23cf8b..805ef8e45e 100644
--- a/marimo/_core/stream.py
+++ b/marimo/_core/stream.py
@@ -8,6 +8,7 @@ stream from the last event id received on that stream.
from __future__ import annotations
import logging
+import secrets
from abc import ABC, abstractmethod
from collections import deque
from collections.abc import Callable, Collection, Iterator
@@ -93,9 +94,11 @@ class RoutedEvent:
run_id: protocol.RunId | None = None
@property
- def id(self) -> int | None:
- """The stream's integer cursor, absent on snapshots and resets."""
- return None if self.event.id is UNSET else int(self.event.id)
+ def position(self) -> int | None:
+ """The event's position in its stream, absent on snapshots and resets."""
+ if self.event.id is UNSET:
+ return None
+ return int(self.event.id.rpartition("-")[2])
def visible_in(self, notebook_id: str | None) -> bool:
"""Whether the event belongs in the catalog or notebook stream."""
@@ -216,6 +219,9 @@ class Stream(Sink):
"""
def __init__(self, *, history: int = 1000) -> None:
+ # Cursors name the stream that issued them, so a cursor from before
+ # a restart resets instead of replaying another history.
+ self._epoch = secrets.token_hex(4)
self._last_id = 0
self._history: deque[RoutedEvent] = deque(maxlen=history)
self._listeners: list[Listener] = []
@@ -228,18 +234,46 @@ class Stream(Sink):
def last_id(self) -> int:
return self._last_id
- def since(self, cursor: int) -> list[RoutedEvent] | None:
- """Return events after `cursor`, or `None` for a stale or future cursor."""
- if cursor == self._last_id:
+ @property
+ def cursor(self) -> str:
+ """The cursor of the last event, as `ready` carries it."""
+ return self.cursor_at(self._last_id)
+
+ def cursor_at(self, position: int) -> str:
+ """Return the cursor this stream issues for `position`."""
+ return f"{self._epoch}-{position}"
+
+ def position(self, cursor: str) -> int | None:
+ """Return the position `cursor` names, or `None` if another stream issued it."""
+ epoch, _, position = cursor.partition("-")
+ # Header text is latin-1, where superscripts count as digits.
+ if epoch != self._epoch or not (
+ position.isascii() and position.isdigit()
+ ):
+ return None
+ try:
+ return int(position)
+ except ValueError:
+ return None
+
+ def since(self, position: int) -> list[RoutedEvent] | None:
+ """Return events after `position`, or `None` for a stale or future one."""
+ if position == self._last_id:
return []
- if cursor > self._last_id:
+ if position > self._last_id:
return None
- oldest = self._history[0].id if self._history else self._last_id + 1
+ oldest = (
+ self._history[0].position if self._history else self._last_id + 1
+ )
assert oldest is not None
- if cursor < oldest - 1:
+ if position < oldest - 1:
return None
return deepcopy(
- [e for e in self._history if e.id is not None and e.id > cursor]
+ [
+ e
+ for e in self._history
+ if e.position is not None and e.position > position
+ ]
)
def subscribe(self, listener: Listener) -> Callable[[], None]:
@@ -277,7 +311,7 @@ class Stream(Sink):
self._last_id += 1
stamped = replace(
event,
- event=msgspec.structs.replace(event.event, id=str(self._last_id)),
+ event=msgspec.structs.replace(event.event, id=self.cursor),
)
self._history.append(stamped)
self._pending.append((stamped, tuple(self._listeners)))
diff --git a/marimo/_server/host/api.py b/marimo/_server/host/api.py
index 2674ca4e83..7e9eca352d 100644
--- a/marimo/_server/host/api.py
+++ b/marimo/_server/host/api.py
@@ -622,7 +622,7 @@ async def _notebook_events_ws(websocket: WebSocket) -> None:
notebook.detach(attachment.id)
-def _cursor(request: HTTPConnection) -> int | Response | None:
+def _cursor(request: HTTPConnection) -> str | Response | None:
raw = request.headers.get("Last-Event-ID")
if request.scope["type"] == "websocket":
query = request.query_params.get("cursor")
@@ -630,15 +630,10 @@ def _cursor(request: HTTPConnection) -> int | Response | None:
if raw is not None and raw != query:
return problem(400, "invalid-request", "Conflicting cursors")
raw = query
- if raw is None:
- return None
- # Header text is latin-1, where superscripts count as digits.
- if not (raw.isascii() and raw.isdigit()):
- return problem(400, "invalid-request", "Last-Event-ID must be an id")
- try:
- return int(raw)
- except ValueError:
- return problem(400, "invalid-request", "Last-Event-ID is too long")
+ if raw == "":
+ return problem(400, "invalid-request", "Last-Event-ID is empty")
+ # Cursors are opaque; one the host did not issue resets the stream.
+ return raw
def _types(request: HTTPConnection) -> set[str] | Response | None:
@@ -667,7 +662,7 @@ def _attachment_problem(error: msgspec.ValidationError) -> str:
def _initial_events(
bridge: SessionBridge,
- cursor: int | None,
+ cursor: str | None,
types: set[str] | None,
*,
notebook_id: protocol.NotebookId | None,
@@ -698,20 +693,20 @@ class _EventSubscription:
) -> None:
self.initial = initial
self._queue = queue
- self._ready_id = initial[-1].id or 0
+ self._ready_id = initial[-1].position or 0
async def next(self) -> RoutedEvent:
"""Wait for a live event not already covered by the initial state."""
while True:
event = await self._queue.get()
- if event.id is None or event.id > self._ready_id:
+ if event.position is None or event.position > self._ready_id:
return event
@contextmanager
def _subscribe_events(
bridge: SessionBridge,
- cursor: int | None,
+ cursor: str | None,
types: set[str] | None,
*,
notebook_id: protocol.NotebookId | None,
@@ -742,7 +737,7 @@ def _subscribe_events(
def _stream(
context: HostContext,
- cursor: int | None,
+ cursor: str | None,
types: set[str] | None,
*,
notebook_id: protocol.NotebookId | None,
@@ -781,8 +776,8 @@ def _stream(
def _frame(event: RoutedEvent) -> bytes:
"""Encode an event as an SSE frame."""
lines = []
- if event.id is not None:
- lines.append(f"id: {event.id}")
+ if event.event.id is not msgspec.UNSET:
+ lines.append(f"id: {event.event.id}")
lines.append(f"event: {event_name(event.event)}")
data = msgspec.json.encode(event.event.data)
lines.append(f"data: {data.decode()}")
diff --git a/marimo/_server/host/websocket.py b/marimo/_server/host/websocket.py
index 268a9a01bf..5d4ba283cc 100644
--- a/marimo/_server/host/websocket.py
+++ b/marimo/_server/host/websocket.py
@@ -61,7 +61,7 @@ class EditorEventSocket(SessionHandler):
params: ConnectionParams,
doc_manager: LoroDocManager,
types: set[str] | None,
- cursor: int | None,
+ cursor: str | None,
) -> None:
super().__init__(
manager=manager,
@@ -93,7 +93,7 @@ class EditorEventSocket(SessionHandler):
# The session delivers marimo notifications with recipient filtering,
# including edits that must not echo to the client that sent them.
if not isinstance(event.event, MarimoEvent) and (
- event.id is None or event.id > self._ready_id
+ event.position is None or event.position > self._ready_id
):
self._events.put_nowait(event)
@@ -108,7 +108,7 @@ class EditorEventSocket(SessionHandler):
initial = self.bridge.host.events(
self.cursor, notebook_id=self.notebook.id, types=self.types
)
- self._ready_id = initial[-1].id or 0
+ self._ready_id = initial[-1].position or 0
for event in initial:
# Session notifications must retain their recipient filtering,
# including on reconnect; only protocol records use replay.
diff --git a/protocols/host/openapi.yaml b/protocols/host/openapi.yaml
index 155feb2835..be131c403b 100644
--- a/protocols/host/openapi.yaml
+++ b/protocols/host/openapi.yaml
@@ -40,8 +40,9 @@ paths:
sends that position in the `Last-Event-ID` header. If the host
still has the events after that point, it sends them with their
original ids and timestamps, even before the new `ready`, and then
- `ready`. If it does not, it sends `reset`, and the client discards
- what it holds and receives the full state again. A client also
+ `ready`. If it does not, or the cursor predates a loss of host
+ state, it sends `reset`, and the client discards what it holds and
+ receives the full state again. A client also
discards an incomplete snapshot before reconnecting without a
cursor. On `reset` it clears its cursor until the next `ready`.
@@ -295,8 +296,8 @@ paths:
cells that changed, `cell.removed` for each deleted cell, and then
the document, with nothing in between. The analysis of the new
revision follows with the same key once the host has it. The
- response carries the
- new etags, so a client keeps editing without reading again. A
+ response carries the new etags, as of this edit or a later
+ revision, so a client keeps editing without reading again. A
change that changes nothing keeps its etags and sends no events.
Whether the result has reached the store shows in the document's
`saved_etag`.
@@ -2060,7 +2061,7 @@ components:
description: The cell's version after the edit.
EditResponse:
type: object
- description: The document after an edit or a save, and the cells it touched.
+ description: The document as an edit or save left it, or at a later revision, and the cells it touched.
required: [document, cells]
properties:
document: { $ref: '#/components/schemas/Document' }
diff --git a/protocols/host/spec.md b/protocols/host/spec.md
index 95ece448a1..c355c9cbac 100644
--- a/protocols/host/spec.md
+++ b/protocols/host/spec.md
@@ -247,10 +247,12 @@ each document change as a group without interleaving unrelated events, so
clients can apply an edit together with its effects on the rest of the
document.
-Each **cursor** marks a position in the stream. After disconnecting, a client
-can reconnect with its last cursor to request the events it missed. If the host
-no longer has those events, it requests a reset. Clients MUST then discard
-their previous view and rebuild it from a fresh snapshot.
+Each **cursor** marks a position in the stream. Clients treat cursors as
+opaque. A client sends back the last cursor it received and does not compare
+or construct cursors. After disconnecting, a client can reconnect with its last
+cursor to request the events it missed. If the host no longer has those events,
+or the cursor predates a loss of host state, it requests a reset. Clients MUST
+then discard their previous view and rebuild it from a fresh snapshot.
Event history can expire while results remain stored. Those results still
appear in a fresh snapshot, even when their original events are unavailable.
@@ -319,9 +321,10 @@ Preconditions use **etags**, identifiers derived from content. They let clients
edit different parts of a document independently while detecting competing
changes to the same part. For example, two clients can update different cells,
but an edit based on an older version of the same cell produces a conflict.
-An accepted edit answers with the document and the version of each cell it
-touched. The client already has the content it sent, and the versions let it
-recognize the edit when its events arrive on the stream.
+An accepted edit answers with the version of each cell it touched and the
+document at the edit's revision or a later one. The client already has the
+content it sent, and the versions let it recognize the edit when its events
+arrive on the stream.
Accepting an edit and saving it are separate actions. The host reports whether
the current document has reached storage, whether it saves automatically or
diff --git a/tests/_cli/test_pair_delivery_integration.py b/tests/_cli/test_pair_delivery_integration.py
index 3961f0ddac..95d60b39e5 100644
--- a/tests/_cli/test_pair_delivery_integration.py
+++ b/tests/_cli/test_pair_delivery_integration.py
@@ -300,7 +300,7 @@ def test_subprocess_reconnect_replays_messages_and_server_detach(
"cursor"
]
assert isinstance(cursor, str)
- assert cursor.isdigit()
+ assert cursor
# The observer keeps the producer guard satisfied during the gap.
send("during gap")
(tmp_path / "resume").touch()
diff --git a/tests/_core/test_host.py b/tests/_core/test_host.py
index a4ded7bb12..abf69684bb 100644
--- a/tests/_core/test_host.py
+++ b/tests/_core/test_host.py
@@ -103,8 +103,10 @@ async def test_catalog_snapshot_and_replay_exclude_runs(host: Host) -> None:
notebook.start()
notebook.observe(be.Running(runtime_id_of(notebook)))
run = notebook.run("1", request_id="r1")
- cursor = host.events()[-1].id
- assert cursor is not None
+ ready = host.events()[-1]
+ cursor = ready.event.id
+ assert isinstance(cursor, str)
+ assert ready.position is not None
notebook.observe(
be.Planned(
@@ -123,8 +125,8 @@ async def test_catalog_snapshot_and_replay_exclude_runs(host: Host) -> None:
"attachment",
"ready",
]
- assert all(e.id is None for e in events[:-1])
- assert events[-1].id == cursor + 2
+ assert all(e.position is None for e in events[:-1])
+ assert events[-1].position == ready.position + 2
assert all(
getattr(e.event.data, "time", None) is UNSET for e in events[:-1]
)
@@ -158,7 +160,7 @@ async def test_a_notebook_stream_sees_only_its_notebook(host: Host) -> None:
assert all(e.notebook_id == "n1" for e in events[:-1])
# Replay includes both starting and running states for n1.
- live = host.events(0, notebook_id="n1")
+ live = host.events(host._stream.cursor_at(0), notebook_id="n1")
assert names(live) == [
"notebook",
"document",
@@ -168,7 +170,7 @@ async def test_a_notebook_stream_sees_only_its_notebook(host: Host) -> None:
"execution",
"ready",
]
- assert all(e.id is not None for e in live)
+ assert all(e.position is not None for e in live)
with pytest.raises(NotFound):
host.events(notebook_id="n3")
@@ -178,8 +180,8 @@ async def test_resume_replays_or_resets() -> None:
small = Host(operations=[], history=2)
small.update_project(project())
await add_notebook(small, record(), ScriptedBackend())
- cursor = small.events()[-1].id
- assert cursor is not None
+ cursor = small.events()[-1].event.id
+ assert isinstance(cursor, str)
assert names(small.events(cursor)) == ["ready"]
small.update_project(project("p2"))
@@ -187,14 +189,21 @@ async def test_resume_replays_or_resets() -> None:
small.update_project(project("p3"))
small.update_project(project("p4"))
assert names(small.events(cursor))[0] == "reset"
- assert names(small.events(10**6))[0] == "reset"
+ assert names(small.events(small._stream.cursor_at(10**6)))[0] == "reset"
+ oversized = small._stream.cursor_at(0) + "1" * 5000
+ assert names(small.events(oversized))[0] == "reset"
+ # A cursor from before a restart names another stream's history.
+ restarted = Host(operations=[]).events()[-1].event.id
+ assert isinstance(restarted, str)
+ assert names(small.events(restarted))[0] == "reset"
async def test_types_filter_keeps_ready_and_reset(host: Host) -> None:
host.update_project(project())
await add_notebook(host, record(), ScriptedBackend())
assert names(host.events(types={"notebook"})) == ["notebook", "ready"]
- assert names(host.events(10**6, types={"notebook"})) == [
+ future = host._stream.cursor_at(10**6)
+ assert names(host.events(future, types={"notebook"})) == [
"reset",
"notebook",
"ready",
@@ -350,7 +359,7 @@ async def test_catalog_updates_do_not_share_mutable_records(
source = replace(source, name="changed", operations=())
host.update_project(source)
- replay = host.events(0, types={"project"})
+ replay = host.events(host._stream.cursor_at(0), types={"project"})
projects = [
e.event.data.project
for e in replay
@@ -540,7 +549,7 @@ async def test_expiration_converges_for_live_replay_and_snapshot(
]
assert all(event.event.data.request_id == "request" for event in live)
replay = host.events(
- snapshot[-1].id, notebook_id=notebook.id, run_id=run.id
+ snapshot[-1].event.id, notebook_id=notebook.id, run_id=run.id
)
assert (isinstance(replay[0].event, protocol.ResetEvent)) == (history == 2)
assert (
diff --git a/tests/_core/test_notebook.py b/tests/_core/test_notebook.py
index 686a13e2f2..f6f95ffea4 100644
--- a/tests/_core/test_notebook.py
+++ b/tests/_core/test_notebook.py
@@ -115,7 +115,7 @@ class TestStart:
for events in (
host.events(notebook_id=notebook.id),
- host.events(0, notebook_id=notebook.id),
+ host.events(host._stream.cursor_at(0), notebook_id=notebook.id),
):
assert [
event.event.data.runtime
@@ -513,7 +513,7 @@ class TestRun:
notebook.observe(be.Concluded(ExecutionId("e1"), value("9")))
notebook.observe(be.Status(ExecutionId("e1"), "completed"))
notebook.observe(be.Done(run.id, "succeeded"))
- cursor = host.events()[-1].id
+ cursor = host.events()[-1].event.id
live: list[RoutedEvent] = []
unsubscribe = host.subscribe(
live.append, notebook_id=notebook.id, run_id=run.id
diff --git a/tests/_core/test_stream.py b/tests/_core/test_stream.py
index e3c0c05962..7a8a713d0c 100644
--- a/tests/_core/test_stream.py
+++ b/tests/_core/test_stream.py
@@ -45,7 +45,7 @@ def test_nested_publications_follow_the_current_event() -> None:
stream = Stream()
def publish_another(event: RoutedEvent) -> None:
- if event.id == 1:
+ if event.position == 1:
stream.publish(project("p2"))
stream.subscribe(publish_another)
@@ -54,7 +54,7 @@ def test_nested_publications_follow_the_current_event() -> None:
stream.publish(project())
- assert [e.id for e in delivered] == [1, 2]
+ assert [e.position for e in delivered] == [1, 2]
assert delivered == stream.since(0)
@@ -143,10 +143,10 @@ def test_deferred_events_join_history_before_delivery() -> None:
with stream.deferred():
stream.publish(project())
- assert [e.id for e in stream.since(0) or []] == [1]
+ assert [e.position for e in stream.since(0) or []] == [1]
assert delivered == []
- assert [e.id for e in delivered] == [1]
+ assert [e.position for e in delivered] == [1]
def test_subscribers_act_after_the_command_reaches_the_backend(
@@ -213,7 +213,9 @@ def test_subscribers_cannot_start_a_runtime_during_adoption(
def test_custom_events_are_on_the_notebook_stream_only(
host: Host, notebook: Notebook
) -> None:
- cursor = host.events(notebook_id=notebook.id)[-1].id
+ ready = host.events(notebook_id=notebook.id)[-1]
+ cursor = ready.event.id
+ assert isinstance(cursor, str)
assert cursor is not None
notebook.emit("marimo.test", {"hello": 1})
@@ -228,7 +230,7 @@ def test_custom_events_are_on_the_notebook_stream_only(
assert names(
host.events(cursor, notebook_id=notebook.id, types={"attachment"})
) == ["ready"]
- replayed = host._stream.since(cursor)
+ replayed = host._stream.since(ready.position or 0)
assert replayed is not None
assert names(replayed) == ["custom"]
diff --git a/tests/_server/host/drive.py b/tests/_server/host/drive.py
index 64a9e13aa6..1bc4c02624 100644
--- a/tests/_server/host/drive.py
+++ b/tests/_server/host/drive.py
@@ -19,6 +19,8 @@ if TYPE_CHECKING:
from starlette.testclient import WebSocketTestSession
from starlette.types import ASGIApp, Receive, Scope, Send
+ from marimo._core.host import Host
+
PORT = 1234
"""Server port used to validate the request's Host header."""
@@ -39,6 +41,13 @@ def with_client_address(
return wrapped
+def position(host: Host, cursor: str) -> int:
+ """Return where a cursor the host issued falls in its stream."""
+ found = host._stream.position(cursor)
+ assert found is not None, f"{cursor} is not from this host"
+ return found
+
+
def socket_snapshot(websocket: WebSocketTestSession) -> list[dict[str, Any]]:
"""Read the opening snapshot or replay, including its ready boundary."""
events = []
diff --git a/tests/_server/host/test_editor_websocket.py b/tests/_server/host/test_editor_websocket.py
index da2a652ca8..fb7c839565 100644
--- a/tests/_server/host/test_editor_websocket.py
+++ b/tests/_server/host/test_editor_websocket.py
@@ -245,6 +245,7 @@ def test_reconnect_recovers_offline_notifications(
if replay == "cursor":
url += f"&cursor={cursor}"
elif replay == "reset":
+ # A bare integer is a cursor from a host before this one.
url += "&cursor=999999999"
with client.websocket_connect(url) as socket:
opening = socket_snapshot(socket)
@@ -449,14 +450,11 @@ def test_editor_resumes_host_events_without_replaying_private_notifications(
assert receive_notification(socket, "alert")["title"] == "Reconnected"
[email protected]("cursor", ["bad", "-1", ""])
-def test_invalid_cursor_is_rejected_before_starting_a_session(
- client: TestClient, editor_host: SessionBridge, cursor: str
+def test_an_empty_cursor_is_rejected_before_starting_a_session(
+ client: TestClient, editor_host: SessionBridge
) -> None:
with pytest.raises(WebSocketDisconnect) as exc:
- with client.websocket_connect(
- f"{socket_url(editor_host)}&cursor={cursor}"
- ):
+ with client.websocket_connect(f"{socket_url(editor_host)}&cursor="):
pytest.fail("Invalid cursor was accepted")
assert (exc.value.code, exc.value.reason) == (1008, "Invalid event cursor")
assert not get_session_manager(client).sessions
diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.py
index 4392660082..63671489e2 100644
--- a/tests/_server/host/test_host_api.py
+++ b/tests/_server/host/test_host_api.py
@@ -39,6 +39,7 @@ from tests._server.host.drive import (
PORT,
Response,
finished,
+ position,
request,
socket_snapshot,
until_ready,
@@ -311,16 +312,32 @@ async def test_malformed_bearers_are_401_problems(
assert response.headers["content-type"] == "application/problem+json"
[email protected]("cursor", ["x", "-1", "1" * 5000, "1 "])
-async def test_a_malformed_cursor_is_a_400_problem(
+# A bare integer is a cursor from a host before this one.
[email protected]("cursor", ["1", "x", "-1", "1" * 5000, "1 "])
+async def test_a_cursor_the_host_did_not_issue_resets_the_stream(
harness: Harness, cursor: str
) -> None:
- response = await finished(
+ response, _, disconnect = request(
harness.app,
"GET",
"/events",
headers={**AUTH, "Last-Event-ID": cursor},
)
+ await wait_for(response, until_ready)
+ assert response.names() == [
+ "reset",
+ "host",
+ "project",
+ "notebook",
+ "ready",
+ ]
+ disconnect()
+
+
+async def test_an_empty_cursor_is_a_400_problem(harness: Harness) -> None:
+ response = await finished(
+ harness.app, "GET", "/events", headers={**AUTH, "Last-Event-ID": ""}
+ )
assert response.status == 400
assert response.json()["type"].endswith("/invalid-request")
@@ -448,13 +465,13 @@ async def test_the_catalog_stream_sends_a_snapshot_then_changes(
await wait_for(response, until_ready)
assert response.names() == ["host", "project", "notebook", "ready"]
assert "id" not in response.records[0]
- cursor = int(response.records[-1]["id"])
+ cursor = response.records[-1]["id"]
notebook = await harness.host.notebook(harness.notebook_id)
notebook.start()
await wait_for(response, lambda r: r.names()[-1] == "runtime")
live = response.records[-1]
- assert int(live["id"]) > cursor
+ assert position(harness.host, live["id"]) > position(harness.host, cursor)
assert live["data"]["runtime"]["status"] == "starting"
assert "time" in live["data"]
disconnect()
@@ -523,11 +540,6 @@ async def test_types_and_cursors_apply_to_the_stream(harness: Harness) -> None:
assert resumed.names() == ["runtime", "ready"]
disconnect()
- bad = await finished(
- harness.app, "GET", "/events", headers={**AUTH, "Last-Event-ID": "x"}
- )
- assert bad.status == 400
-
async def test_a_notebook_stream_attaches_the_client(harness: Harness) -> None:
notebook = await harness.host.notebook(harness.notebook_id)
@@ -1971,7 +1983,7 @@ async def test_marimo_notifications_use_their_own_sse_kind(
await wait_for(response, lambda r: "marimo" in r.names())
assert [r for r in response.records if r.get("event") == "marimo"] == [
{
- "id": str(published.id),
+ "id": published.event.id,
"event": "marimo",
"data": {
"marimo": {
@@ -2043,7 +2055,7 @@ def test_websocket_delivers_live_events(harness: Harness, query: str) -> None:
},
}
assert websocket.receive_json() == {
- "id": str(first.id),
+ "id": first.event.id,
**expected,
}
if not query:
@@ -2054,7 +2066,7 @@ def test_websocket_delivers_live_events(harness: Harness, query: str) -> None:
"data": {"text": "hello"},
}
assert websocket.receive_json() == {
- "id": str(second.id),
+ "id": second.event.id,
**expected,
}
@@ -2195,7 +2207,7 @@ def test_editor_authentication_is_limited_to_the_event_socket(
RoutedEvent(MarimoEvent(payload), notebook_id=notebook.id),
)
assert websocket.receive_json() == {
- "id": str(published.id),
+ "id": published.event.id,
"event": "marimo",
"data": {
"marimo": {
@@ -2258,9 +2270,7 @@ def test_event_socket_respects_disabled_editor_authentication(
[
("?types=bogus", AUTH, "Invalid event types"),
("?run_id=", AUTH, "run_id must not be empty"),
- ("?cursor=bad", AUTH, "Invalid event cursor"),
("?cursor=", AUTH, "Invalid event cursor"),
- ("", {**AUTH, "Last-Event-ID": "-1"}, "Invalid event cursor"),
("?cursor=1", {**AUTH, "Last-Event-ID": "2"}, "Invalid event cursor"),
(
"",
diff --git a/tests/_server/host/test_host_bridge.py b/tests/_server/host/test_host_bridge.py
index d55b8f0c7a..83fd4329fb 100644
--- a/tests/_server/host/test_host_bridge.py
+++ b/tests/_server/host/test_host_bridge.py
@@ -297,8 +297,8 @@ async def test_session_updates_after_catalog_removal_are_ignored(
assert record is not None
notebook = await bridge.host.notebook(record.id)
bridge.index.forget(record.id)
- cursor = bridge.host.events()[-1].id
- assert cursor is not None
+ cursor = bridge.host.events()[-1].event.id
+ assert isinstance(cursor, str)
session.broadcast(
StartupProgressNotification(
diff --git a/tests/_server/host/test_host_events.py b/tests/_server/host/test_host_events.py
index 1eee7ec79b..3fbbd5b412 100644
--- a/tests/_server/host/test_host_events.py
+++ b/tests/_server/host/test_host_events.py
@@ -84,7 +84,8 @@ def test_extension_routing_and_control_filters() -> None:
stream = Stream()
recorded = stream.append(event)
assert recorded.event == MarimoEvent(
- data=MarimoMessage(CompletedRunNotification(run_id="r1")), id="1"
+ data=MarimoMessage(CompletedRunNotification(run_id="r1")),
+ id=stream.cursor_at(1),
)
assert stream.since(0) == [recorded]
diff --git a/tests/_server/host/test_host_websocket.py b/tests/_server/host/test_host_websocket.py
index 1d04e2fd57..b4e52824e0 100644
--- a/tests/_server/host/test_host_websocket.py
+++ b/tests/_server/host/test_host_websocket.py
@@ -28,6 +28,7 @@ from tests._server.host.conftest import FakeManager, FakeSession
from tests._server.host.drive import (
AUTHORITY,
PORT,
+ position,
socket_snapshot,
with_client_address,
)
@@ -282,7 +283,9 @@ def test_reconnect_replays_original_events_then_continues_live(
)
live = socket.receive_json()
assert live["data"]["marimo"]["run_id"] == "live"
- assert int(live["id"]) > int(replay[-1]["id"])
+ assert position(bridge.host, live["id"]) > position(
+ bridge.host, replay[-1]["id"]
+ )
finally:
client.portal.call(bridge.close)
@@ -312,11 +315,15 @@ def test_expired_cursor_resets_to_current_state(
reset = socket_snapshot(socket)
assert reset[0] == {"event": "reset", "data": {}}
assert reset[1:-1] == initial[:-1]
- assert int(reset[-1]["id"]) > int(cursor)
+ assert position(bridge.host, reset[-1]["id"]) > position(
+ bridge.host, cursor
+ )
event = client.portal.call(notebook.emit, "test.live", {})
live = socket.receive_json()
assert live["event"] == "custom"
assert live["data"]["custom"] == msgspec.to_builtins(event)
- assert int(live["id"]) > int(reset[-1]["id"])
+ assert position(bridge.host, live["id"]) > position(
+ bridge.host, reset[-1]["id"]
+ )
finally:
client.portal.call(bridge.close)