Skip to content

Commit cdca36e

Browse files
committed
Define the runtime breakpoint record and replace operation
1 parent a201d0b commit cdca36e

9 files changed

Lines changed: 323 additions & 3 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright 2026 Marimo. All rights reserved.
2+
"""The live runtime’s debugger breakpoints."""
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# Copyright 2026 Marimo. All rights reserved.
2+
# AUTO-GENERATED FILE — DO NOT EDIT.
3+
# Regenerate with `make host-protocol-codegen`.
4+
# ruff: noqa: TC003 - msgspec resolves annotations at runtime
5+
"""Python bindings for the `marimo.breakpoints` notebook extension.
6+
7+
Each operation defines a `Call` type for its decoded request and a
8+
`Result` type for its handler's response. The host uses `event`
9+
to publish the extension's record and `handle` to dispatch a call to
10+
the handlers for one notebook.
11+
"""
12+
13+
from __future__ import annotations
14+
15+
from datetime import datetime
16+
from typing import Annotated, Final, Protocol, TypeAlias, assert_never
17+
18+
import msgspec
19+
from msgspec import UNSET, Meta, Struct, UnsetType
20+
21+
from marimo._core import protocol, result
22+
from marimo._core.protocol import RuntimeId, Timestamp
23+
from marimo._core.protocol.api import MutationProblem
24+
from marimo._core.stream import ExtensionEvent
25+
26+
BreakpointLine: TypeAlias = Annotated[int, Meta(ge=1)]
27+
28+
29+
class Breakpoints(Struct, frozen=True):
30+
cells: Annotated[
31+
dict[str, tuple[BreakpointLine, ...]],
32+
Meta(description="One-based breakpoint lines by cell id."),
33+
]
34+
runtime_id: RuntimeId
35+
36+
37+
class MarimoBreakpointsMessage(Struct, frozen=True):
38+
marimo_breakpoints: Breakpoints = msgspec.field(name="marimo.breakpoints")
39+
request_id: (
40+
Annotated[
41+
str,
42+
Meta(
43+
description="The `Idempotency-Key` of the request that caused this event. Omitted when no request did, and on snapshots unless that request's work is still in progress."
44+
),
45+
]
46+
| UnsetType
47+
) = UNSET
48+
time: (
49+
Annotated[
50+
Timestamp,
51+
Meta(
52+
description="When the host published this event. Present on live and replayed events; omitted on snapshots."
53+
),
54+
]
55+
| UnsetType
56+
) = UNSET
57+
58+
59+
class ReplaceBreakpointsRequest(
60+
Struct, frozen=True, forbid_unknown_fields=True
61+
):
62+
cells: Annotated[
63+
dict[str, tuple[BreakpointLine, ...]],
64+
Meta(description="One-based breakpoint lines by cell id."),
65+
]
66+
67+
68+
class MarimoBreakpointsEvent(
69+
ExtensionEvent, frozen=True, tag="marimo.breakpoints"
70+
):
71+
data: MarimoBreakpointsMessage
72+
73+
74+
def event(
75+
record: Breakpoints,
76+
*,
77+
request_id: str | UnsetType = UNSET,
78+
time: datetime | UnsetType = UNSET,
79+
) -> MarimoBreakpointsEvent:
80+
return MarimoBreakpointsEvent(
81+
MarimoBreakpointsMessage(
82+
marimo_breakpoints=record, request_id=request_id, time=time
83+
)
84+
)
85+
86+
87+
class Replace:
88+
"""Replace the current runtime's debugger breakpoints."""
89+
90+
ID: Final = "notebook.extensions.breakpoints.replace"
91+
METHOD: Final = "PUT"
92+
PATH: Final = "/extensions/marimo.breakpoints/notebooks/{notebook_id}"
93+
94+
class Params(msgspec.Struct, frozen=True):
95+
notebook_id: protocol.NotebookId
96+
97+
class Headers(msgspec.Struct, frozen=True):
98+
idempotency_key: str = msgspec.field(name="idempotency-key")
99+
100+
class Call(
101+
msgspec.Struct,
102+
frozen=True,
103+
tag_field="operation",
104+
tag="notebook.extensions.breakpoints.replace",
105+
):
106+
params: Replace.Params
107+
headers: Replace.Headers
108+
body: ReplaceBreakpointsRequest
109+
110+
Result: TypeAlias = result.Result[
111+
Breakpoints, MutationProblem | protocol.NoRuntimeProblem
112+
]
113+
114+
115+
class Handlers(Protocol):
116+
"""Operations an implementation of `marimo.breakpoints` must provide."""
117+
118+
async def replace(self, call: Replace.Call, /) -> Replace.Result: ...
119+
120+
121+
OPERATIONS = (Replace,)
122+
Call: TypeAlias = Replace.Call
123+
124+
125+
async def handle(
126+
handlers: Handlers, call: Call
127+
) -> result.Result[
128+
tuple[int, msgspec.Struct | None],
129+
MutationProblem | protocol.NoRuntimeProblem,
130+
]:
131+
match call:
132+
case Replace.Call():
133+
outcome_replace = await handlers.replace(call)
134+
if outcome_replace.ok:
135+
return result.Ok((200, outcome_replace.value))
136+
return outcome_replace
137+
assert_never(call)

‎packages/client/src/__tests__/notebook-client.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ afterEach(() => {
5252
});
5353

5454
describe("NotebookClient", () => {
55+
it("replaces breakpoints for its notebook below the configured host base", async () => {
56+
const cells = { c1: [2, 5] };
57+
const record = { runtime_id: "r1", cells };
58+
const { notebook, sent } = connect(
59+
() => json(200, record),
60+
"https://example.com/prefix/api/marimo/v1/notebooks/n%201/?workspace=demo",
61+
);
62+
const result = await notebook.extensions.breakpoints.replace({
63+
body: { cells },
64+
options: { idempotencyKey: "set-lines" },
65+
});
66+
expect(result).toEqual({ ok: true, status: 200, data: record });
67+
const request = sent();
68+
expect(request.method).toBe("PUT");
69+
expect(request.url).toBe(
70+
"https://example.com/prefix/api/marimo/v1/extensions/marimo.breakpoints/notebooks/n%201?workspace=demo",
71+
);
72+
expect(request.headers.get("Authorization")).toBe("Bearer secret");
73+
expect(request.headers.get("Idempotency-Key")).toBe("set-lines");
74+
expect(await request.json()).toEqual({ cells });
75+
});
76+
5577
it("rejects non-JSON event data before sending it", async () => {
5678
const { notebook, fetch } = connect(
5779
() => new Response(null, { status: 204 }),

‎packages/client/src/__tests__/protocol.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ describe("host protocol bindings", () => {
100100
expectTypeOf<Protocol.RequestedCell["id"]>().toEqualTypeOf<string>();
101101
});
102102

103+
it("decodes breakpoint keys as cell IDs", () => {
104+
const set = Protocol.Breakpoints.parse({
105+
runtime_id: "rt",
106+
cells: { a: [1, 3] },
107+
});
108+
expectTypeOf<keyof typeof set.cells>().toEqualTypeOf<Protocol.CellId>();
109+
expect(set.cells).toEqual({ a: [1, 3] });
110+
expect(
111+
Protocol.ReplaceBreakpointsRequest.safeParse({ cells: { a: [0] } })
112+
.success,
113+
).toBe(false);
114+
});
115+
103116
it("decodes the same run ID on records and removal events", () => {
104117
const removed = Protocol.RunRemovedEvent.parse({
105118
event: "run.removed",

‎packages/client/src/extensions.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ import type { bindNotebookExtension } from "./request.ts";
66
export function notebookExtensions(
77
bind: ReturnType<typeof bindNotebookExtension>,
88
) {
9-
return {};
9+
return {
10+
breakpoints: { replace: bind("notebook.extensions.breakpoints.replace") },
11+
};
1012
}

‎packages/client/src/operations.ts‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/client/src/protocol.ts‎

Lines changed: 77 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/host-protocol/src/marimo.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import { makeHostContract } from "./contract.ts";
44
import * as Experimental from "./experimental/index.ts";
5+
import breakpoints from "./marimo/breakpoints.ts";
56

67
export { defineNotebookExtension } from "./marimo/defineNotebookExtension.ts";
7-
export const extensions = [] as const;
8+
export const extensions = [breakpoints] as const;
89
export const features = [...Experimental.features, ...extensions] as const;
910
export const { Api, events } = makeHostContract(features);

0 commit comments

Comments
 (0)