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.

Require notebook identity before yielding pair test servers (#11422)

Trevor Manze84c14dbc734
diff --git a/tests/_cli/_pair_server.py b/tests/_cli/_pair_server.py
index a7482995a5..56a4ff71c7 100644
--- a/tests/_cli/_pair_server.py
+++ b/tests/_cli/_pair_server.py
@@ -26,7 +26,7 @@ class PairTestServer:
url: str
session_id: str
stable_session_id: str
- notebook_id: str | None
+ notebook_id: str
_process: subprocess.Popen[bytes]
_websocket: ClientConnection
_stderr_path: Path
@@ -141,18 +141,23 @@ def _wait_for_kernel_ready(
websocket: ClientConnection,
*,
timeout: float = 10.0,
-) -> None:
+) -> str:
deadline = time.monotonic() + timeout
+ notebook_id: str | None = None
while True:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError("kernel did not become ready")
event = json.loads(websocket.recv(timeout=remaining))
+ if event["event"] == "notebook":
+ notebook_id = event["data"]["notebook"]["id"]
if event["event"] != "marimo":
continue
message = event["data"]["marimo"]
if message["op"] == "kernel-ready":
- return
+ # The host publishes notebook identity before kernel-ready.
+ assert isinstance(notebook_id, str), "Missing notebook snapshot"
+ return notebook_id
def _stop_process(process: subprocess.Popen[bytes]) -> None:
@@ -227,19 +232,20 @@ def pair_test_server(
if token
else {},
)
- _wait_for_kernel_ready(websocket)
+ notebook_id = _wait_for_kernel_ready(websocket)
request = urllib.request.Request(
f"{url}/api/sessions",
headers={"Authorization": f"Bearer {token}"} if token else {},
)
with urllib.request.urlopen(request, timeout=5) as response:
sessions = json.load(response)
+ assert sessions[session_id]["notebook_id"] == notebook_id
stable_session_id = sessions[session_id]["session_id"]
server = PairTestServer(
url=url,
session_id=session_id,
stable_session_id=stable_session_id,
- notebook_id=sessions[session_id].get("notebook_id"),
+ notebook_id=notebook_id,
_process=process,
_websocket=websocket,
_stderr_path=stderr_path,
diff --git a/tests/_cli/test_pair_integration.py b/tests/_cli/test_pair_integration.py
index a61e401863..31d463cf3e 100644
--- a/tests/_cli/test_pair_integration.py
+++ b/tests/_cli/test_pair_integration.py
@@ -33,7 +33,6 @@ def _command(
) -> list[str]:
target = []
if selector == "notebook":
- assert server.notebook_id is not None
target = ["--notebook", server.notebook_id]
elif selector == "session":
target = ["--session", server.stable_session_id]
@@ -137,7 +136,6 @@ def test_authenticated_execution_preserves_session_contract(
) -> None:
token_file = tmp_path / "token"
token_file.write_text("pair-editor-private-token")
- assert editor_server.notebook_id is not None
result = _run(
editor_server,
"--token-file",