Skip to content

Introduce host API extensions - #12224

Open
manzt wants to merge 2 commits into
mainfrom
notebook-extensions/foundation
Open

manzt wants to merge 2 commits into
mainfrom
notebook-extensions/foundation

Conversation

@manzt

@manzt manzt commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

This description is longer than usual because I want to give anyone working on networking in marimo a practical way to think about the host model.


The goal of this PR is to let marimo build new and/or migrate existing editor features to the host architecture without making each feature part of the core protocol.

Why use the host architecture for editor-specific features?

Today, marimo splits ownership of some shared notebook state between the server and an "editing" client. That creates two problems.

  • For users, a second tab has to “take over” from the first before it can edit the same notebook (since state is split between server and client).
  • For developers, it’s hard to reason about shared state. When the server and a client disagree, which one has the current value? When another client connects, where should it read that value?

The host architecture gives us one source of truth and is designed to support multiple clients. A host (server) is the sole owner of shared state, and each client requests changes through operations and observes through an event stream.

Why keep some features outside the core protocol?

The host protocol is intentionally narrow. It defines the core objects and operations that clients should agree on when working with the same reactive program. marimo’s editor can build on that shared core while keeping features specific to the editor when they make sense there.

Most importantly, each feature we add to the core becomes a public API that other hosts and clients can rely on and that we commit to maintaining (and not breaking).

The host model

The host protocol uses a client-server architecture with HTTP operations and a publish-subscribe event stream. The host owns notebook state as a collection of immutable records. Clients connect to the stream, receive a snapshot, and stay subscribed to changes.

Event stream and operations

To illustrate, here is an example of a "host" with one immutable record, a cell. When the client connects, it opens an event stream GET /notebook/{id}/events and receives the sole record as an event on the stream:

The client is now in sync with the host.

While the client is connected, a cell can change on the host. When this happens, the host does not describe what changed (i.e., a diff) but instead sends a replacement for the record (with the same ID) over the event stream.

The client replaces its record, and again is in sync.

Since the host is the sole owner of state, a client cannot alter the cell directly. Instead, it makes a request to change the record (i.e., make an edit).

An important nuance here is that the request says what the client wants done, but the host decides the result. If the host accepts, it applies the change to its own records (the source of truth) and sends the new record(s) on the event stream to every connected client, including the one that asked.

If the host denies the request, the client that dispatched the request is informed but no clients receive an update.

Notice how the clients observe changes the same way whether they come from their own requests, another client, or the host itself. Each client just subscribes to the event stream and updates its local state as the host publishes new or replacement records.

The cell illustrates the general pattern for defining a feature on the host. You define a record that represents its state and the operations that can change it. Connected clients observe the resulting records on the event stream.

Opening the event stream

In the one-cell example above, I said the client was in sync, but I left out one detail. How does the client know it’s up to date?

The host protocol has two special events for this, ready and reset. When a client first opens the stream, the host sends the current records, followed by ready. In our example, there’s just one cell.

GET /notebooks/n1/events

event: cell
data: {"cell":{"id":"c3","notebook_id":"n1","code":"x = 21","name":"_","config":{"column":null,"disabled":false,"hide_code":false,"expand_output":false},"etag":"5cf78587dcf22d8d","code_fingerprint":"0f71456db2ea83f4","version":1}}

id: 95
event: ready
data: {}

At ready, the client knows it’s up to date. In a real notebook, the host sends all the records that match the subscription before ready, and the stream stays open for changes:

I said all records, but the protocol also lets clients filter what they receive and resume where they left off after a disconnect. Following state

This PR

This PR gives marimo a way to define editor-specific records and operations outside the core protocol. A definition names the record and declares the operations that can change it. The marimo.* namespace marks it as an editor extension.

Here’s what a definition looks like, using marimo.breakpoints from #12225.

// packages/host-protocol/src/marimo/breakpoints.ts
export default defineNotebookExtension({
  name: "marimo.breakpoints",
  record: Breakpoints,
  endpoints: [Replace],
});

// packages/host-protocol/src/marimo.ts
export const extensions = [breakpoints] as const;

Each operation gets a route under the extension’s name, and the client gets a typed method for it.

PUT /extensions/marimo.breakpoints/notebooks/{notebook_id}
await notebook.extensions.breakpoints.replace({
  body: { cells: { setup: [3, 8] } },
});

Clients read the record from the existing notebook stream, so snapshots, live updates, and resume work as they do for core records. The list of extensions is empty in this PR.

@manzt
manzt added this pull request to stack #12227 December 2, 2026 00:27
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report for ./frontend

Status Category Percentage Covered / Total
🔵 Lines 69.97% 29191 / 41718
🔵 Statements 69.92% 29766 / 42566
🔵 Functions 65.49% 7050 / 10765
🔵 Branches 65.42% 17135 / 26192
File CoverageNo changed files found.
Generated in workflow #26586 for commit a63ed9d by the Vitest Coverage Report Action

@manzt
manzt marked this pull request as ready for review December 2, 2026 02:36
Copilot AI balanced review requested due to automatic review settings December 2, 2026 02:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Extension endpoint identifiers can collide with core or other extension endpoints, causing contract generation to throw.

4 open issues
What changed in this PR

Introduces infrastructure for marimo-specific notebook extensions outside the core host protocol, using the existing HTTP operations and notebook event stream.

Changes:

  • Adds extension definitions and Python/TypeScript binding generation.
  • Adds notebook-scoped extension lifecycles and snapshot delivery.
  • Updates documentation, tests, and generation checks.
File Description
tests/​_server/​host/​test_notebook_extensions.py Tests extension lifetime across sessions.
protocols/​host/​README.md Documents marimo OpenAPI output.
packages/​host-protocol/​src/​model/​Events.ts Names dotted extension events.
packages/​host-protocol/​src/​marimo/​defineNotebookExtension.ts Defines notebook extension contracts.
packages/​host-protocol/​src/​marimo.ts Adds the marimo contract entrypoint.
packages/​host-protocol/​src/​__tests__/​python.test.ts Tests extension binding generation.
packages/​host-protocol/​src/​__tests__/​openapi.test.ts Tests extension contract isolation.
packages/​host-protocol/​src/​__tests__/​notebook-tags.ts Adds a synthetic extension fixture.
packages/​host-protocol/​scripts/​python/​models.ts Supports dotted fields and reusable definitions.
packages/​host-protocol/​scripts/​python/​extension.ts Generates Python extension modules.
packages/​host-protocol/​scripts/​python/​calls.ts Shares operation generation with extensions.
packages/​host-protocol/​scripts/​python.ts Writes generated extension bindings.
packages/​host-protocol/​scripts/​openapi.ts Adds the marimo OpenAPI option.
packages/​host-protocol/​README.md Explains extension versus protocol scope.
packages/​host-protocol/​package.json Exports the marimo entrypoint.
packages/​client/​src/​request.ts Binds notebook extension requests.
packages/​client/​src/​notebook/​client.ts Exposes notebook extensions.
packages/​client/​src/​extensions.ts Adds the generated extension accessor.
packages/​client/​src/​__tests__/​operations.test.ts Separates portable operation assertions.
packages/​client/​src/​__tests__/​generation.test.ts Excludes tests from generated fixtures.
packages/​client/​scripts/​generate-protocol.ts Generates extension methods and typed dictionary keys.
packages/​client/​package.json Generates from the marimo contract.
marimo/​_server/​host/​session.py Includes extensions in initial streams.
marimo/​_server/​host/​bridge.py Manages extension lifetimes and snapshots.
marimo/​_server/​host/​backend.py Connects session attachment to extensions.
marimo/​_server/​host/​api.py Delegates initial events to the bridge.
marimo/​_server/​extensions/​scope.py Provides session bindings and event publishing.
marimo/​_server/​extensions/​notebook.py Manages installed extensions and cleanup.
marimo/​_server/​extensions/​__init__.py Introduces the extensions package.
marimo/​_core/​stream.py Accepts extension classes for event names.
.github/​workflows/​test_host_protocol.yaml Watches generated extension bindings.

Review effort: Balanced

Comment thread marimo/_server/host/bridge.py Resolved
@manzt
manzt force-pushed the notebook-extensions/foundation branch from a02a7ef to 16afe3a Compare December 3, 2026 01:10
@manzt
manzt removed this pull request from stack #12227 December 3, 2026 01:11
@manzt
manzt added this pull request to stack #12252 December 3, 2026 01:11
@manzt manzt added enhancement New feature or request host-protocol Related to the marimo host protocol or it's implementation. labels Dec 3, 2026
@manzt
manzt force-pushed the notebook-extensions/foundation branch 2 times, most recently from 090f793 to 73721c7 Compare December 3, 2026 19:56
@manzt manzt changed the title Introduce marimo notebook extensions Introduce host extensions Dec 3, 2026
@manzt manzt changed the title Introduce host extensions Introduce host API extensions Dec 3, 2026
@manzt manzt added internal A refactor or improvement that is not user facing and removed enhancement New feature or request labels Dec 3, 2026
@manzt
manzt force-pushed the notebook-extensions/foundation branch from 73721c7 to a63ed9d Compare December 3, 2026 20:56
@marthacryan

marthacryan commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

YAY this is looking so good. One question I have is what is your definition of "editor extensions" versus "notebook extensions". The function defineNotebookExtension implies that we're creating a notebook extension, but the marimo.* namespace marks it as an editor extension. My guess is that notebook extensions are accessing the notebook event stream and eventually there will be a "catalog extension"? And "editor extension" is a type of notebook extension that is not part of the core host protocol? Let me know if I'm misunderstanding!

Also, when you say "editor" above is that synonymous with "client" in the host protocol?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

host-protocol Related to the marimo host protocol or it's implementation. internal A refactor or improvement that is not user facing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants