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.

Support cell runs in the host protocol (#11429)

Trevor Manz1025a0dc1794
diff --git a/marimo/_core/backend.py b/marimo/_core/backend.py
index 7b1c28b7cd..70521815cc 100644
--- a/marimo/_core/backend.py
+++ b/marimo/_core/backend.py
@@ -19,10 +19,15 @@ unnecessary if marimo adopts the core model directly.
from __future__ import annotations
from dataclasses import dataclass
-from typing import Protocol
+from typing import TYPE_CHECKING, Protocol
from marimo._core import protocol
+if TYPE_CHECKING:
+ from collections.abc import Sequence
+
+ from marimo._core.document import Document
+
class Backend(Protocol):
"""Commands for managing a notebook's kernel.
@@ -46,6 +51,19 @@ class Backend(Protocol):
def run(self, run_id: protocol.RunId, code: str) -> None:
"""Evaluate scratch code and report the run's progress and output."""
+ def run_cells(
+ self,
+ run_id: protocol.RunId,
+ document: Document,
+ cell_ids: Sequence[protocol.CellId],
+ ) -> None:
+ """Run cells of `document` and report the run's progress and output.
+
+ The kernel registers the requested cells and any pending ancestors
+ from initialization before running them. Dependents run as the
+ runtime's configuration says.
+ """
+
def interrupt(self, run_id: protocol.RunId) -> None:
"""Interrupt a run and report an `interrupted` execution outcome."""
diff --git a/marimo/_core/notebook.py b/marimo/_core/notebook.py
index 3a1456a893..87e6e04c6f 100644
--- a/marimo/_core/notebook.py
+++ b/marimo/_core/notebook.py
@@ -6,14 +6,21 @@ from __future__ import annotations
import asyncio
import logging
from copy import deepcopy
-from typing import TYPE_CHECKING, Any
+from typing import TYPE_CHECKING, Any, overload
import msgspec
+from msgspec import UNSET
from msgspec.structs import replace
from marimo._core import backend as be, protocol
from marimo._core.document import Stale
-from marimo._core.errors import Conflict, EditConflict, NoRuntime, NotFound
+from marimo._core.errors import (
+ Conflict,
+ EditConflict,
+ Invalid,
+ NoRuntime,
+ NotFound,
+)
from marimo._core.executions import Executions, Released, Removed
from marimo._core.ids import mint
from marimo._core.stream import RoutedEvent, snapshot_event, utcnow
@@ -427,8 +434,40 @@ class Notebook:
# ---- runs ----------------------------------------------------------
- def run(self, code: str, *, request_id: str) -> protocol.Run:
- """Queue scratch code on a running, idle runtime."""
+ @overload
+ def run(self, code: str, *, request_id: str) -> protocol.Run: ...
+
+ @overload
+ def run(
+ self,
+ *,
+ cells: Sequence[protocol.RequestedCell],
+ request_id: str,
+ ) -> protocol.Run: ...
+
+ def run(
+ self,
+ code: str | None = None,
+ *,
+ cells: Sequence[protocol.RequestedCell] | None = None,
+ request_id: str,
+ ) -> protocol.Run:
+ """Queue scratch code, or cells of the document, on an idle runtime.
+
+ Each requested cell must be in the document. One that names a
+ `code_hash` the cell no longer has is a conflict, and nothing runs.
+
+ Raises:
+ Invalid: If the request names neither code nor cells, or both,
+ an empty cell sequence, or a cell the document does not have.
+ NoRuntime: If the notebook has no runtime.
+ Conflict: If the runtime is not running, a run is in progress,
+ or a requested cell's code has changed.
+ """
+ if (code is None) == (cells is None):
+ raise Invalid("a run names code or cells, not both")
+ if cells is not None and not cells:
+ raise Invalid("a cell run names at least one cell")
with self.sink.deferred():
self._require_open()
runtime = self._require_runtime()
@@ -437,6 +476,21 @@ class Notebook:
active = self.executions.active()
if active is not None:
raise Conflict(f"run {active.id} is in progress.")
+ targets: list[protocol.CellId] = []
+ for requested in cells or ():
+ cell = self._document.cell(requested.id)
+ if cell is None:
+ raise Invalid(
+ f"cell {requested.id} is not in the document."
+ )
+ if (
+ requested.code_hash is not UNSET
+ and requested.code_hash != cell.code_hash
+ ):
+ raise Conflict(
+ f"cell {requested.id} has changed since it was read."
+ )
+ targets.append(cell.id)
run = protocol.Run(
id=mint(protocol.RunId, "run"),
notebook_id=self.id,
@@ -447,7 +501,10 @@ class Notebook:
completed_at=None,
)
self._apply(self.executions.begin(run, request_id))
- self.backend.run(run.id, code)
+ if code is not None:
+ self.backend.run(run.id, code)
+ else:
+ self.backend.run_cells(run.id, self._document, targets)
return run
def interrupt(self, run_id: str) -> protocol.Run:
diff --git a/marimo/_runtime/commands.py b/marimo/_runtime/commands.py
index 161ebc1979..ce5d8539ef 100644
--- a/marimo/_runtime/commands.py
+++ b/marimo/_runtime/commands.py
@@ -296,6 +296,9 @@ class ExecuteCellsCommand(Command):
codes: Python code for each cell. Must match length of cell_ids.
request: HTTP request context if available.
timestamp: Unix timestamp when command was created.
+ run_id: Optional correlation ID. When set, the run's notifications
+ and its `CompletedRunNotification` carry it.
+ allow_stdin: Whether cells may request interactive input.
"""
# ids of cells to run
@@ -306,6 +309,8 @@ class ExecuteCellsCommand(Command):
request: HTTPRequest | None = None
# time at which the request was received
timestamp: float = msgspec.field(default_factory=time.time)
+ run_id: str | None = None
+ allow_stdin: bool = True
def __repr__(self) -> str:
return f"ExecuteCellsCommand(cells={len(self.cell_ids)})"
diff --git a/marimo/_runtime/kernel_request_handlers.py b/marimo/_runtime/kernel_request_handlers.py
index 9d02005e71..758669e0b3 100644
--- a/marimo/_runtime/kernel_request_handlers.py
+++ b/marimo/_runtime/kernel_request_handlers.py
@@ -177,7 +177,11 @@ class KernelRequestHandlers:
async def _handle_execute_multiple(
self, request: ExecuteCellsCommand
) -> None:
- with self._run_context(), http_request_context(request.request):
+ with (
+ self._run_context(request.run_id),
+ stdin_context(request.allow_stdin),
+ http_request_context(request.request),
+ ):
await self._kernel.run(request.execution_requests)
async def _handle_sync_graph(self, request: SyncGraphCommand) -> None:
diff --git a/marimo/_server/host/api.py b/marimo/_server/host/api.py
index 1b383a435e..bb191a6025 100644
--- a/marimo/_server/host/api.py
+++ b/marimo/_server/host/api.py
@@ -943,11 +943,14 @@ async def _run(request: Request) -> Response:
key = request.headers["Idempotency-Key"]
notebook = await _notebook(request)
body = await _decode(request, protocol.RunRequest)
- if body.code is msgspec.UNSET or body.cells is not msgspec.UNSET:
+ if isinstance(body.code, str) and body.cells is msgspec.UNSET:
+ started = notebook.run(body.code, request_id=key)
+ elif body.code is msgspec.UNSET and isinstance(body.cells, tuple):
+ started = notebook.run(cells=body.cells, request_id=key)
+ else:
return problem(
- 400, "invalid-request", "This host requires scratch code"
+ 400, "invalid-request", "A run names code or cells, not both"
)
- started = notebook.run(body.code, request_id=key)
return _reply(202, started)
diff --git a/marimo/_server/host/backend.py b/marimo/_server/host/backend.py
index 42a4496664..65081242c4 100644
--- a/marimo/_server/host/backend.py
+++ b/marimo/_server/host/backend.py
@@ -21,7 +21,11 @@ from marimo._messaging.notification import (
StartupProgressNotification,
)
from marimo._messaging.serde import deserialize_kernel_message
-from marimo._runtime.commands import ExecuteScratchpadCommand, HTTPRequest
+from marimo._runtime.commands import (
+ ExecuteCellsCommand,
+ ExecuteScratchpadCommand,
+ HTTPRequest,
+)
from marimo._runtime.scratch import SCRATCH_CELL_ID
from marimo._server.host.events import notification_event
from marimo._server.host.translate import RunTranslator
@@ -30,13 +34,17 @@ from marimo._session.consumer import SessionConsumer
from marimo._session.model import ConnectionState, SessionMode
from marimo._session.requests import InstantiateNotebookRequest
from marimo._session.types import KernelState
-from marimo._types.ids import ConsumerId, SessionId
+from marimo._types.ids import CellId_t, ConsumerId, SessionId
from marimo._version import __version__
if TYPE_CHECKING:
+ from collections.abc import Coroutine, Sequence
+
+ from marimo._core.document import Document
from marimo._core.stream import Sink
from marimo._messaging.notification import NotificationMessage
from marimo._messaging.types import KernelMessage
+ from marimo._runtime.commands import CommandMessage
from marimo._server.session_manager import SessionManager
from marimo._session.events import SessionEventBus
from marimo._session.session import Session
@@ -67,7 +75,7 @@ class _Run:
session: Session
translator: RunTranslator
done: asyncio.Event = field(default_factory=asyncio.Event)
- task: asyncio.Task[None] | None = None
+ task: asyncio.Task[object] | None = None
submitted: bool = False
@@ -172,11 +180,64 @@ class SessionBackend(SessionConsumer):
return
run = _Run(session, RunTranslator(run_id, code))
self._run = run
+ self._watch_run(run, self._run_code(run, code))
+
+ def run_cells(
+ self,
+ run_id: protocol.RunId,
+ document: Document,
+ cell_ids: Sequence[protocol.CellId],
+ ) -> None:
+ session = self.session
+ if session is None:
+ return
+ run = _Run(session, RunTranslator(run_id, None))
+ self._run = run
+ codes = {CellId_t(cell.id): cell.code for cell in document}
+ targets = [CellId_t(cell_id) for cell_id in cell_ids]
+ try:
+ # Enqueue before returning so later document edits follow the run.
+ session.instantiate(
+ InstantiateNotebookRequest(
+ object_ids=[], values=[], auto_run=False, codes=codes
+ ),
+ http_request=None,
+ )
+ self._submit(
+ run,
+ ExecuteCellsCommand(
+ cell_ids=targets,
+ codes=[codes[cell_id] for cell_id in targets],
+ request=self._http_request(session),
+ run_id=run_id,
+ allow_stdin=False,
+ ),
+ )
+ except Exception as exception:
+ self._finish_run(run, exception)
+ return
+ self._watch_run(run, run.done.wait())
+
+ def _watch_run(
+ self, run: _Run, coroutine: Coroutine[object, object, object]
+ ) -> None:
run.task = asyncio.get_running_loop().create_task(
- self._run_code(run, code), name=f"host.run.{run_id}"
+ coroutine, name=f"host.run.{run.translator.run_id}"
)
# A callback also finalizes cancellation before the coroutine starts.
- run.task.add_done_callback(lambda task: self._finish_run(run, task))
+ run.task.add_done_callback(
+ lambda task: self._finish_run(
+ run,
+ asyncio.CancelledError()
+ if task.cancelled()
+ else task.exception(),
+ )
+ )
+
+ def _submit(self, run: _Run, command: CommandMessage) -> None:
+ run.submitted = True
+ self._translators[run.translator.run_id] = run.translator
+ run.session.put_control_request(command, from_consumer_id=None)
async def _run_code(self, run: _Run, code: str) -> None:
session = run.session
@@ -189,51 +250,51 @@ class SessionBackend(SessionConsumer):
http_request=None,
)
cells, outputs = snapshot_for_scratchpad(session)
- request = None
- if self._request_metadata is not None:
- # Host runs have no HTTP origin. Give the URL fields the same
- # keys a real request has so notebook code can read them.
- empty_url: dict[str, Encodable] = {
- "path": "",
- "port": None,
- "scheme": "",
- "netloc": "",
- "query": "",
- "hostname": None,
- }
- request = HTTPRequest(
- url=dict(empty_url),
- base_url=dict(empty_url),
- headers={},
- query_params={},
- path_params={},
- cookies={},
- user={},
- meta=self._request_metadata(session),
- )
- run.submitted = True
- self._translators[run.translator.run_id] = run.translator
- session.put_control_request(
+ self._submit(
+ run,
ExecuteScratchpadCommand(
code=code,
- request=request,
+ request=self._http_request(session),
notebook_cells=cells,
cell_outputs=outputs,
run_id=run.translator.run_id,
allow_stdin=False,
),
- from_consumer_id=None,
)
await run.done.wait()
- def _finish_run(self, run: _Run, task: asyncio.Task[None]) -> None:
+ def _http_request(self, session: Session) -> HTTPRequest | None:
+ if self._request_metadata is None:
+ return None
+ # Host runs have no HTTP origin. Give the URL fields the same
+ # keys a real request has so notebook code can read them.
+ empty_url: dict[str, Encodable] = {
+ "path": "",
+ "port": None,
+ "scheme": "",
+ "netloc": "",
+ "query": "",
+ "hostname": None,
+ }
+ return HTTPRequest(
+ url=dict(empty_url),
+ base_url=dict(empty_url),
+ headers={},
+ query_params={},
+ path_params={},
+ cookies={},
+ user={},
+ meta=self._request_metadata(session),
+ )
+
+ def _finish_run(self, run: _Run, exception: BaseException | None) -> None:
if self._run is run:
self._run = None
error = None
- if task.cancelled():
+ if isinstance(exception, asyncio.CancelledError):
run.translator.interrupted = True
- elif (exception := task.exception()) is not None:
- LOGGER.error("Scratch run failed in the host: %s", exception)
+ elif exception is not None:
+ LOGGER.error("Run failed in the host: %s", exception)
error = protocol.Error(
kind="internal",
name=None,
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 4bae5cfb49..d9945cbc27 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -1898,8 +1898,13 @@ components:
\ and reactive execution.\n\n Attributes:\n cell_ids: Cells to execute.\n\
\ codes: Python code for each cell. Must match length of cell_ids.\n\
\ request: HTTP request context if available.\n timestamp: Unix\
- \ timestamp when command was created."
+ \ timestamp when command was created.\n run_id: Optional correlation\
+ \ ID. When set, the run's notifications\n and its `CompletedRunNotification`\
+ \ carry it.\n allow_stdin: Whether cells may request interactive input."
properties:
+ allowStdin:
+ default: true
+ type: boolean
cellIds:
items:
$ref: '#/components/schemas/CellId'
@@ -1913,6 +1918,11 @@ components:
- $ref: '#/components/schemas/HTTPRequest'
- type: 'null'
default: null
+ runId:
+ anyOf:
+ - type: string
+ - type: 'null'
+ default: null
timestamp:
type: number
type:
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index dde8bea74a..caf5112fc7 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -4961,12 +4961,19 @@ export interface components {
* codes: Python code for each cell. Must match length of cell_ids.
* request: HTTP request context if available.
* timestamp: Unix timestamp when command was created.
+ * run_id: Optional correlation ID. When set, the run's notifications
+ * and its `CompletedRunNotification` carry it.
+ * allow_stdin: Whether cells may request interactive input.
*/
ExecuteCellsCommand: {
+ /** @default true */
+ allowStdin?: boolean;
cellIds: components["schemas"]["CellId"][];
codes: string[];
/** @default null */
request?: components["schemas"]["HTTPRequest"] | null;
+ /** @default null */
+ runId?: string | null;
timestamp?: number;
/** @enum {unknown} */
type: "execute-cells";
diff --git a/tests/_core/conftest.py b/tests/_core/conftest.py
index 4e96a8fb58..92397a0c86 100644
--- a/tests/_core/conftest.py
+++ b/tests/_core/conftest.py
@@ -4,6 +4,7 @@
from __future__ import annotations
import asyncio
+from typing import TYPE_CHECKING
import pytest
@@ -15,6 +16,9 @@ from marimo._core.notebook import Notebook
from marimo._core.run_log import RetentionPolicy
from marimo._core.stream import RoutedEvent, Sink, event_name
+if TYPE_CHECKING:
+ from collections.abc import Sequence
+
OPERATIONS = ["notebook.create", "runtime.start", "runtime.run"]
@@ -33,6 +37,13 @@ class ScriptedBackend:
def run(self, run_id: str, code: str) -> None:
self.calls.append(("run", run_id, code))
+ def run_cells(
+ self, run_id: str, document: Document, cell_ids: Sequence[str]
+ ) -> None:
+ self.calls.append(
+ ("run_cells", run_id, document.etag, tuple(cell_ids))
+ )
+
def interrupt(self, run_id: str) -> None:
self.calls.append(("interrupt", run_id))
diff --git a/tests/_core/test_notebook.py b/tests/_core/test_notebook.py
index f6f95ffea4..fe7a0e23b1 100644
--- a/tests/_core/test_notebook.py
+++ b/tests/_core/test_notebook.py
@@ -14,7 +14,7 @@ from msgspec.structs import replace
from marimo._core import backend as be, protocol, protocol_events
from marimo._core.document import Document, DocumentCell
-from marimo._core.errors import Conflict, NoRuntime, NotFound
+from marimo._core.errors import Conflict, Invalid, NoRuntime, NotFound
from marimo._core.executions import CellExecution, Executions
from marimo._core.host import Host
from marimo._core.notebook import Notebook
@@ -1451,3 +1451,74 @@ async def test_deleting_an_active_cell_releases_its_result_when_the_run_ends() -
assert list(notebook.document) == []
assert fold(notebook._snapshot()) == {}
assert fold(host.events(notebook_id=notebook.id)) == {}
+
+
+async def test_running_cells_hands_the_backend_the_document() -> None:
+ _, notebook = await host_at(Clock())
+ notebook.replace(
+ [DocumentCell(CellId("c1"), "x = 1"), DocumentCell(CellId("c2"), "x")],
+ {},
+ )
+ c1 = notebook.document.cell(CellId("c1"))
+ assert c1 is not None
+
+ run = notebook.run(
+ cells=[
+ protocol.RequestedCell(id=CellId("c1"), code_hash=c1.code_hash)
+ ],
+ request_id="run-1",
+ )
+
+ assert run.status == "queued"
+ assert notebook.backend.calls[-1] == ( # type: ignore[attr-defined]
+ "run_cells",
+ run.id,
+ notebook.document.etag,
+ ("c1",),
+ )
+
+
+async def test_running_a_cell_whose_code_changed_is_a_conflict() -> None:
+ _, notebook = await host_at(Clock())
+ notebook.replace([DocumentCell(CellId("c1"), "x = 1")], {})
+ calls = len(notebook.backend.calls) # type: ignore[attr-defined]
+
+ with pytest.raises(Conflict, match="changed"):
+ notebook.run(
+ cells=[
+ protocol.RequestedCell(
+ id=CellId("c1"), code_hash=protocol.Etag("0" * 16)
+ )
+ ],
+ request_id="run-1",
+ )
+ with pytest.raises(Invalid, match="not in the document"):
+ notebook.run(
+ cells=[protocol.RequestedCell(id=CellId("ghost"))],
+ request_id="run-2",
+ )
+
+ assert len(notebook.backend.calls) == calls # type: ignore[attr-defined]
+ assert notebook.executions.active() is None
+
+
+async def test_a_run_names_code_or_cells() -> None:
+ _, notebook = await host_at(Clock())
+ with pytest.raises(Invalid):
+ notebook.run(request_id="run-1") # type: ignore[call-overload]
+ with pytest.raises(Invalid):
+ notebook.run("1", cells=[], request_id="run-2") # type: ignore[call-overload]
+
+
[email protected]("cells", [[], ()])
+async def test_a_cell_run_needs_at_least_one_target(
+ cells: list[protocol.RequestedCell] | tuple[protocol.RequestedCell, ...],
+) -> None:
+ _, notebook = await host_at(Clock())
+ calls = list(notebook.backend.calls) # type: ignore[attr-defined]
+
+ with pytest.raises(Invalid, match="at least one cell"):
+ notebook.run(cells=cells, request_id="empty")
+
+ assert notebook.backend.calls == calls # type: ignore[attr-defined]
+ assert notebook.executions.active() is None
diff --git a/tests/_runtime/test_kernel_request_handlers.py b/tests/_runtime/test_kernel_request_handlers.py
index 1b091d0b62..bb6cd7eb9a 100644
--- a/tests/_runtime/test_kernel_request_handlers.py
+++ b/tests/_runtime/test_kernel_request_handlers.py
@@ -13,7 +13,7 @@ from marimo._runtime.commands import (
ModelUpdateMessage,
)
from marimo._runtime.kernel_request_handlers import KernelRequestHandlers
-from marimo._types.ids import WidgetModelId
+from marimo._types.ids import CellId_t, WidgetModelId
from tests._messaging.mocks import MockStream
if TYPE_CHECKING:
@@ -43,6 +43,21 @@ async def test_nested_commands_complete_the_run_once(
assert messages[-1]["op"] == "completed-run"
+async def test_cell_runs_carry_the_requested_run_id(
+ mocked_kernel: MockedKernel,
+) -> None:
+ await KernelRequestHandlers(mocked_kernel.k)._handle_execute_multiple(
+ ExecuteCellsCommand(
+ cell_ids=[CellId_t("a")], codes=["x = 1"], run_id="run-1"
+ )
+ )
+ messages = MockStream(mocked_kernel.stream).operations
+ assert [m["run_id"] for m in messages if m["op"] == "completed-run"] == [
+ "run-1"
+ ]
+ assert {m["run_id"] for m in messages if m["op"] == "cell-op"} == {"run-1"}
+
+
async def test_a_failed_command_flushes_before_completing(
mocked_kernel: MockedKernel,
) -> None:
@@ -189,3 +204,35 @@ async def test_noninteractive_scratch_input_is_scoped_to_the_command(
ExecuteScratchpadCommand(code="observed.append(input('interactive'))")
)
assert kernel.globals["observed"] == ["EOF", "interactive"]
+
+
+ "read",
+ [
+ "input('prompt')",
+ "__import__('builtins').input()",
+ "__import__('getpass').getpass()",
+ ],
+)
+async def test_noninteractive_cell_input_is_scoped_to_the_command(
+ mocked_kernel: MockedKernel, read: str
+) -> None:
+ kernel = mocked_kernel.k
+ handlers = KernelRequestHandlers(kernel)
+ kernel.globals["observed"] = []
+ await handlers._handle_execute_multiple(
+ ExecuteCellsCommand(
+ cell_ids=[CellId_t("a")],
+ codes=[
+ f"try:\n {read}\nexcept EOFError:\n observed.append('EOF')"
+ ],
+ allow_stdin=False,
+ )
+ )
+ await handlers._handle_execute_multiple(
+ ExecuteCellsCommand(
+ cell_ids=[CellId_t("a")],
+ codes=["observed.append(input('interactive'))"],
+ )
+ )
+ assert kernel.globals["observed"] == ["EOF", "interactive"]
diff --git a/tests/_server/host/conftest.py b/tests/_server/host/conftest.py
index 7debe8db80..0bb9ee06a9 100644
--- a/tests/_server/host/conftest.py
+++ b/tests/_server/host/conftest.py
@@ -28,6 +28,7 @@ class FakeSession:
self.event_bus = SessionEventBus()
self.consumers: list[tuple[SessionConsumer, bool]] = []
self.commands: list[Any] = []
+ self.instantiations: list[Any] = []
self.notifications: list[NotificationMessage] = []
self.submitted = asyncio.Event()
self.interrupts = 0
@@ -58,7 +59,8 @@ class FakeSession:
return
def instantiate(self, request: Any, *, http_request: Any) -> None:
- del request, http_request
+ del http_request
+ self.instantiations.append(request)
def put_control_request(self, request: Any, from_consumer_id: Any) -> None:
del from_consumer_id
diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.py
index 2b63c642e4..61475b95bc 100644
--- a/tests/_server/host/test_host_api.py
+++ b/tests/_server/host/test_host_api.py
@@ -2327,3 +2327,40 @@ def test_websocket_snapshot_failure_releases_subscription(
assert caught.value.code == 1008
assert notebook._attachments == {}
assert len(harness.host._stream._listeners) == listeners
+
+
+async def test_cell_runs_over_http(harness: Harness) -> None:
+ nid = await harness.running()
+ notebook = await harness.host.notebook(nid)
+ (cell,) = notebook.document
+ runs = f"/notebooks/{nid}/runtime/runs"
+
+ body = json.dumps(
+ {"cells": [{"id": cell.id, "code_hash": cell.code_hash}]}
+ ).encode()
+ accepted = await finished(
+ harness.app, "POST", runs, headers=key("r1"), body=body
+ )
+ assert accepted.status == 202, accepted.body
+ run_id = accepted.json()["id"]
+ assert harness.backend().calls[-1] == (
+ "run_cells",
+ run_id,
+ notebook.document.etag,
+ (cell.id,),
+ )
+
+ notebook.observe(be.Done(protocol.RunId(run_id), "succeeded"))
+ stale = json.dumps({"cells": [{"id": cell.id, "code_hash": "0" * 16}]})
+ conflict = await finished(
+ harness.app, "POST", runs, headers=key("r2"), body=stale.encode()
+ )
+ assert conflict.status == 409
+ assert conflict.json()["type"].endswith("/conflict")
+ assert "changed since it was read" in conflict.json()["detail"]
+
+ both = json.dumps({"code": "1", "cells": [{"id": cell.id}]}).encode()
+ invalid = await finished(
+ harness.app, "POST", runs, headers=key("r3"), body=both
+ )
+ assert invalid.status == 400
diff --git a/tests/_server/host/test_host_backend.py b/tests/_server/host/test_host_backend.py
index 7ffc7d8925..1decfb1f34 100644
--- a/tests/_server/host/test_host_backend.py
+++ b/tests/_server/host/test_host_backend.py
@@ -13,6 +13,7 @@ from marimo._code_mode.screenshot_meta import (
SCREENSHOT_SERVER_URL_KEY,
)
from marimo._core import backend as be, protocol
+from marimo._core.document import Document, DocumentCell
from marimo._core.stream import Stream
from marimo._messaging.notification import (
CellNotification,
@@ -21,6 +22,7 @@ from marimo._messaging.notification import (
StartupProgressNotification,
)
from marimo._messaging.serde import serialize_kernel_message
+from marimo._runtime.commands import ExecuteCellsCommand
from marimo._runtime.scratch import SCRATCH_CELL_ID
from marimo._server.host.backend import SessionBackend
from marimo._server.scratchpad import scratchpad_request_metadata
@@ -371,8 +373,9 @@ async def test_submission_failure_is_an_error(
)
[email protected]("cell_run", [False, True])
async def test_host_run_carries_screenshot_metadata(
- manager: FakeManager, observed: Observed
+ manager: FakeManager, observed: Observed, cell_run: bool
) -> None:
b = SessionBackend(
protocol.NotebookId("n1"),
@@ -391,7 +394,14 @@ async def test_host_run_carries_screenshot_metadata(
)
b.adopt(RT1, manager.next_session)
run_id = protocol.RunId("run-screenshot")
- b.run(run_id, "ctx.screenshot()")
+ if cell_run:
+ b.run_cells(
+ run_id,
+ Document([DocumentCell(protocol.CellId("a"), "1")], {}),
+ [protocol.CellId("a")],
+ )
+ else:
+ b.run(run_id, "ctx.screenshot()")
await asyncio.wait_for(manager.next_session.submitted.wait(), 5)
command = manager.next_session.commands[-1]
assert command.request.meta == {
@@ -408,3 +418,65 @@ async def test_host_run_carries_screenshot_metadata(
assert b._run is not None
assert b._run.task is not None
await b._run.task
+
+
+async def test_running_cells_instantiates_from_the_host_document(
+ manager: FakeManager, observed: Observed
+) -> None:
+ b = backend(manager, observed)
+ b.start(RT1)
+ await observed.wait_for(be.Running)
+ session = manager.next_session
+ document = Document(
+ [
+ DocumentCell(protocol.CellId("a"), "x = 1"),
+ DocumentCell(protocol.CellId("b"), "y = x"),
+ ],
+ {},
+ )
+
+ b.run_cells(protocol.RunId("run-1"), document, [protocol.CellId("b")])
+ await asyncio.wait_for(session.submitted.wait(), 5)
+
+ (instantiation,) = session.instantiations
+ assert instantiation.auto_run is False
+ assert instantiation.codes == {"a": "x = 1", "b": "y = x"}
+ (command,) = session.commands
+ assert isinstance(command, ExecuteCellsCommand)
+ assert command.cell_ids == ["b"]
+ assert command.codes == ["y = x"]
+ assert command.run_id == "run-1"
+
+ session.broadcast(
+ CompletedRunNotification(run_id="run-1", outcome="succeeded")
+ )
+ await observed.wait_for(be.Done)
+ assert observed.messages[-1] == be.Done(
+ protocol.RunId("run-1"), "succeeded"
+ )
+
+
[email protected]("method", ["instantiate", "put_control_request"])
+async def test_cell_submission_failure_ends_the_run(
+ manager: FakeManager,
+ observed: Observed,
+ monkeypatch: pytest.MonkeyPatch,
+ method: str,
+) -> None:
+ b = backend(manager, observed)
+ session = manager.next_session
+ b.adopt(RT1, session)
+ observed.messages.clear()
+
+ def failed(*_args: object, **_kwargs: object) -> None:
+ raise RuntimeError("Kernel channel closed")
+
+ monkeypatch.setattr(session, method, failed)
+ b.run_cells(
+ protocol.RunId("failed"),
+ Document([DocumentCell(protocol.CellId("a"), "42")], {}),
+ [protocol.CellId("a")],
+ )
+
+ assert observed.messages == [be.Done(protocol.RunId("failed"), "failed")]
+ assert b._run is None
diff --git a/tests/_server/host/test_host_execution.py b/tests/_server/host/test_host_execution.py
index 1160412cd3..03e420e45a 100644
--- a/tests/_server/host/test_host_execution.py
+++ b/tests/_server/host/test_host_execution.py
@@ -221,6 +221,133 @@ async def test_scratch_outcomes_from_the_kernel(
assert outcome["error"]["message"]
[email protected]("delete", [False, True], ids=["edit", "delete"])
+async def test_cell_runs_preserve_order_with_later_edits(
+ live_host: tuple[Starlette, str, Response], delete: bool
+) -> None:
+ app, notebook_id, events = live_host
+ notebook = await app.state.host_context.bridge.host.notebook(notebook_id)
+ cell = next(iter(notebook.document))
+ session = next(iter(app.state.session_manager.sessions.values()))
+ runs = f"/notebooks/{notebook_id}/runtime/runs"
+ body = json.dumps(
+ {"cells": [{"id": cell.id, "code_hash": cell.code_hash}]}
+ ).encode()
+
+ async def completed(response: Response) -> None:
+ assert response.status == 202, response.body
+ await wait_for(
+ events,
+ lambda stream: any(
+ r.get("data", {}).get("run", {}).get("id")
+ == response.json()["id"]
+ and r["data"]["run"]["status"] == "completed"
+ for r in stream.records
+ ),
+ )
+
+ await completed(
+ await finished(app, "POST", runs, headers=key("warm"), body=body)
+ )
+ # A scratch client must not let a later edit overtake an accepted cell run.
+ async with session.scratchpad_lock:
+ accepted = await finished(
+ app, "POST", runs, headers=key("run"), body=body
+ )
+ assert accepted.status == 202, accepted.body
+ change = (
+ {"kind": "delete_cell", "id": cell.id, "base_etag": "*"}
+ if delete
+ else {
+ "kind": "put_cell",
+ "base_etag": "*",
+ "cell": {"id": cell.id, "code": "example = 99"},
+ }
+ )
+ edited = await finished(
+ app,
+ "POST",
+ f"/notebooks/{notebook_id}/edits",
+ headers=key("edit"),
+ body=json.dumps({"changes": [change]}).encode(),
+ )
+ assert edited.status == 200, edited.body
+ await completed(accepted)
+
+ assertion = (
+ f"assert {cell.id!r} not in _graph.cells"
+ if delete
+ else f"assert _graph.cells[{cell.id!r}].code == 'example = 99'"
+ )
+ checked = await finished(
+ app,
+ "POST",
+ runs,
+ headers=key("check-graph"),
+ body=json.dumps(
+ {
+ "code": "from marimo._runtime.context import get_context as _ctx\n"
+ "_graph = _ctx().graph\n" + assertion
+ }
+ ).encode(),
+ )
+ await completed(checked)
+ assert (
+ next(
+ r["data"]["run"]["outcome"]
+ for r in reversed(events.records)
+ if r.get("data", {}).get("run", {}).get("id")
+ == checked.json()["id"]
+ )
+ == "succeeded"
+ )
+ current = notebook.document.cell(cell.id)
+ assert (None if current is None else current.code) == (
+ None if delete else "example = 99"
+ )
+
+
+ "notebook_source",
+ [
+ (
+ "import marimo\napp = marimo.App()\n"
+ "@app.cell\ndef _():\n answer = input('prompt')\n return (answer,)\n"
+ )
+ ],
+ indirect=True,
+)
+async def test_host_cell_input_fails_without_waiting_for_stdin(
+ live_host: tuple[Starlette, str, Response],
+) -> None:
+ app, notebook_id, events = live_host
+ notebook = await app.state.host_context.bridge.host.notebook(notebook_id)
+ cell = next(iter(notebook.document))
+ accepted = await finished(
+ app,
+ "POST",
+ f"/notebooks/{notebook_id}/runtime/runs",
+ headers=key("run"),
+ body=json.dumps({"cells": [{"id": cell.id}]}).encode(),
+ )
+ assert accepted.status == 202, accepted.body
+ await wait_for(
+ events,
+ lambda stream: any(
+ r.get("data", {}).get("run", {}).get("id") == accepted.json()["id"]
+ and r["data"]["run"]["status"] == "completed"
+ for r in stream.records
+ ),
+ )
+ [execution] = [
+ r["data"]["execution"]
+ for r in events.records
+ if r.get("data", {}).get("execution", {}).get("status") == "completed"
+ ]
+ assert execution["outcome"]["error"]["name"] == "EOFError"
+ assert notebook.executions.active() is None
+
+
async def test_background_output_after_a_scratch_run_is_dropped(
live_host: tuple[Starlette, str, Response],
) -> None: