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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Follow island notebooks through the host stream (#11518)
Trevor Manz066bf094bf51
diff --git a/frontend/src/core/islands/__tests__/bootstrap.test.ts b/frontend/src/core/islands/__tests__/bootstrap.test.tsindex fb21cf3f2f..8eae7a0b01 100644--- a/frontend/src/core/islands/__tests__/bootstrap.test.ts+++ b/frontend/src/core/islands/__tests__/bootstrap.test.ts@@ -6,17 +6,17 @@ import { RuntimeState } from "@/core/kernel/RuntimeState";import { initializeIslands } from "../bootstrap";import type { IslandsPyodideBridge } from "../bridge";import { islandsHydratedAtom, islandsPendingInitialRunsAtom } from "../state";-import type { IslandsKernelMessage } from "../worker/worker";--function completedRun(sessionGeneration: number): IslandsKernelMessage {- return {- sessionGeneration,- message: JSON.stringify({- op: "completed-run",- data: { op: "completed-run", run_id: null },- }) as IslandsKernelMessage["message"],- };-}+import type { NotificationMessage } from "@/core/kernel/messages";++type Consumer = (+ notification: NotificationMessage,+ sessionGeneration: number,+) => void;++const completedRun = {+ op: "completed-run",+ run_id: null,+} as NotificationMessage;describe("initializeIslands", () => {afterEach(vi.restoreAllMocks);@@ -26,9 +26,9 @@ describe("initializeIslands", () => {root.innerHTML ='<marimo-island data-app-id="app-1" data-reactive="true"></marimo-island>';- let consumeMessage: ((message: IslandsKernelMessage) => void) | undefined;+ let consumeMessage: Consumer | undefined;const bridge = {- consumeMessages: (consumer: (message: IslandsKernelMessage) => void) => {+ consumeNotifications: (consumer: Consumer) => {consumeMessage = consumer;},sendComponentValues: vi.fn(),@@ -48,14 +48,14 @@ describe("initializeIslands", () => {expect(store.get(islandsHydratedAtom)).toBe(false);store.set(islandsPendingInitialRunsAtom, new Set([1, 2]));- consumeMessage?.(completedRun(1));+ consumeMessage?.(completedRun, 1);expect(store.get(islandsHydratedAtom)).toBe(false);- consumeMessage?.(completedRun(1));- consumeMessage?.(completedRun(0));+ consumeMessage?.(completedRun, 1);+ consumeMessage?.(completedRun, 0);expect(store.get(islandsHydratedAtom)).toBe(false);- consumeMessage?.(completedRun(2));+ consumeMessage?.(completedRun, 2);expect(store.get(islandsHydratedAtom)).toBe(true);});});diff --git a/frontend/src/core/islands/__tests__/bridge.test.ts b/frontend/src/core/islands/__tests__/bridge.test.tsindex 96328c05ac..5ab1e288ec 100644--- a/frontend/src/core/islands/__tests__/bridge.test.ts+++ b/frontend/src/core/islands/__tests__/bridge.test.ts@@ -10,7 +10,6 @@ import {} from "@/__tests__/branded";import { notebookAtom } from "@/core/cells/cells";import { islandsPendingInitialRunsAtom } from "@/core/islands/state";-import type { IslandsKernelMessage } from "../worker/worker";type Base64String = components["schemas"]["Base64String"];interface TestIslandApp {@@ -89,9 +88,6 @@ vi.mock("@/core/wasm/rpc", () => ({startSession: mockStartSessionRequest,stopSession: mockStopSessionRequest,},- send: {- consumerReady: vi.fn(),- },},addMessageListener: vi.fn((name: string, listener: (payload: never) => void) => {@@ -177,17 +173,22 @@ describe("IslandsPyodideBridge", () => {});}- function sendKernelMessage(sessionGeneration: number) {- const listener = mockMessageListeners.get("kernelMessage");+ function sendNotebookEvent(sessionGeneration: number, frame: unknown) {+ const listener = mockMessageListeners.get("notebookEvent");if (!listener) {- throw new Error("Missing kernel message listener");+ throw new Error("Missing notebook event listener");}- const message = {- message: "" as IslandsKernelMessage["message"],- sessionGeneration,- };- listener(message as never);- return message;+ listener({ event: JSON.stringify(frame), sessionGeneration } as never);+ }++ /** Send a notification and return the call the consumer should receive. */+ function sendKernelMessage(sessionGeneration: number) {+ const notification = { op: "interrupted" };+ sendNotebookEvent(sessionGeneration, {+ event: "marimo",+ data: { marimo: notification },+ });+ return [notification, sessionGeneration];}async function initializeSingleApp() {@@ -300,7 +301,7 @@ describe("IslandsPyodideBridge", () => {describe("app lifecycle", () => {it("ignores messages after a session yields ownership", async () => {const consumeMessage = vi.fn();- bridge.consumeMessages(consumeMessage);+ bridge.consumeNotifications(consumeMessage);mockSingleApp();await bridge.initializeApps();@@ -319,10 +320,7 @@ describe("IslandsPyodideBridge", () => {sendKernelMessage(1);const currentMessage = sendKernelMessage(2);- expect(consumeMessage.mock.calls).toEqual([- [firstMessage],- [currentMessage],- ]);+ expect(consumeMessage.mock.calls).toEqual([firstMessage, currentMessage]);finishReplacement();await replacement;@@ -330,7 +328,7 @@ describe("IslandsPyodideBridge", () => {it("restores message ownership when replacement fails", async () => {const consumeMessage = vi.fn();- bridge.consumeMessages(consumeMessage);+ bridge.consumeNotifications(consumeMessage);mockSingleApp();await bridge.initializeApps();mockReplaceSessionRequest.mockRejectedValueOnce(@@ -344,12 +342,12 @@ describe("IslandsPyodideBridge", () => {const previousMessage = sendKernelMessage(1);sendKernelMessage(2);- expect(consumeMessage.mock.calls).toEqual([[previousMessage]]);+ expect(consumeMessage.mock.calls).toEqual([previousMessage]);});it("ignores messages while their session is stopping", async () => {const consumeMessage = vi.fn();- bridge.consumeMessages(consumeMessage);+ bridge.consumeNotifications(consumeMessage);mockSingleApp();await bridge.initializeApps();let finishStop!: () => void;@@ -373,7 +371,7 @@ describe("IslandsPyodideBridge", () => {it("forwards messages from every app in a multi-app document", async () => {const consumeMessage = vi.fn();- bridge.consumeMessages(consumeMessage);+ bridge.consumeNotifications(consumeMessage);mockParseMarimoIslandApps.mockReturnValue([app(), app("app-2")]);mockCreateMarimoFile.mockReturnValueOnce("generated app 1")@@ -385,10 +383,17 @@ describe("IslandsPyodideBridge", () => {const secondMessage = sendKernelMessage(2);sendKernelMessage(3);- expect(consumeMessage.mock.calls).toEqual([- [firstMessage],- [secondMessage],- ]);+ expect(consumeMessage.mock.calls).toEqual([firstMessage, secondMessage]);+ });++ it("delivers only the notebook stream's marimo notifications", async () => {+ const consumeMessage = vi.fn();+ bridge.consumeNotifications(consumeMessage);+ await initializeSingleApp();+ sendNotebookEvent(1, { event: "ready", id: "3", data: {} });+ const notification = sendKernelMessage(1);++ expect(consumeMessage.mock.calls).toEqual([notification]);});it("holds controls until the first app session is ready", async () => {diff --git a/frontend/src/core/islands/bootstrap.ts b/frontend/src/core/islands/bootstrap.tsindex 0267d9c9f5..818a144ce4 100644--- a/frontend/src/core/islands/bootstrap.ts+++ b/frontend/src/core/islands/bootstrap.ts@@ -9,11 +9,7 @@ import {} from "@/plugins/impl/anywidget/registry";import { initializePlugins } from "@/plugins/plugins";import { Functions } from "@/utils/functions";-import {- safeExtractSetUIElementMessageBuffers,- type JsonString,-} from "@/utils/json/base64";-import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";+import { safeExtractSetUIElementMessageBuffers } from "@/utils/json/base64";import { Logger } from "@/utils/Logger";import { logNever } from "@/utils/assertNever";import {@@ -30,10 +26,7 @@ import {handleKernelReady,handleRemoveUIElements,} from "../kernel/handlers";-import type {- NotificationMessage,- NotificationMessageType,-} from "../kernel/messages";+import type { NotificationMessage } from "../kernel/messages";import { queryParamHandlers } from "../kernel/queryParamHandlers";import { RuntimeState } from "../kernel/RuntimeState";import { initialModeAtom } from "../mode";@@ -126,19 +119,9 @@ export async function initializeIslands(}});- // Wire up kernel message handling- // The wire format is {"op": "...", "data": {"op": "...", ...}} — both- // the envelope and the payload carry the op. The bridge types the message- // as NotificationPayload (just {data}), but the actual wire format- // includes the outer op too.- // Worker metadata identifies the session that emitted the message.- bridge.consumeMessages(({ message, sessionGeneration }) => {- handleMessage(- message as unknown as JsonString<IslandsNotificationMessage>,- actions,- store,- sessionGeneration,- );+ // Each notification names the session that sent it.+ bridge.consumeNotifications((notification, sessionGeneration) => {+ handleNotification(notification, actions, store, sessionGeneration);});// Track first user interaction before initialization completes@@ -154,27 +137,15 @@ export async function initializeIslands(RuntimeState.INSTANCE.start(bridge.sendComponentValues);}-type IslandsNotificationMessage = {- [K in NotificationMessageType]: {- data: Extract<NotificationMessage, { op: K }>;- op: K;- };-}[NotificationMessageType];--/**- * Handles a single message from the kernel.- *- * Wire format from Python: {"op": "<name>", "data": {"op": "<name>", ...}}- */+/** Handles a single notification from the kernel. */// oxlint-disable-next-line marimo/prefer-object-params-function handleMessage(- message: JsonString<IslandsNotificationMessage>,+function handleNotification(+ msg: NotificationMessage,actions: NotebookActions,store: Store,sessionGeneration: number,): void {try {- const msg = jsonParseWithSpecialChar(message);Logger.debug("Islands received message:", msg.op);switch (msg.op) {@@ -223,7 +194,7 @@ function handleMessage(return;case "kernel-ready":- handleKernelReady(msg.data, {+ handleKernelReady(msg, {autoInstantiate: true,setCells: actions.setCells,setLayoutData: Functions.NOOP,@@ -239,47 +210,47 @@ function handleMessage(case "send-ui-element-message":UI_ELEMENT_REGISTRY.broadcastMessage(- msg.data.ui_element,- msg.data.message,- safeExtractSetUIElementMessageBuffers(msg.data),+ msg.ui_element,+ msg.message,+ safeExtractSetUIElementMessageBuffers(msg),);return;case "remove-ui-elements":- handleRemoveUIElements(msg.data);+ handleRemoveUIElements(msg);return;case "function-call-result":- FUNCTIONS_REGISTRY.resolve(msg.data.function_call_id, msg.data);+ FUNCTIONS_REGISTRY.resolve(msg.function_call_id, msg);return;case "cell-op":- handleCellNotificationeration(msg.data, actions.handleCellMessage);+ handleCellNotificationeration(msg, actions.handleCellMessage);return;case "alert":toast({- title: msg.data.title,- description: renderHTML({ html: msg.data.description }),- variant: msg.data.variant,+ title: msg.title,+ description: renderHTML({ html: msg.description }),+ variant: msg.variant,});return;case "query-params-append":- queryParamHandlers.append(msg.data);+ queryParamHandlers.append(msg);return;case "query-params-set":- queryParamHandlers.set(msg.data);+ queryParamHandlers.set(msg);return;case "query-params-delete":- queryParamHandlers.delete(msg.data);+ queryParamHandlers.delete(msg);return;case "query-params-clear":queryParamHandlers.clear();return;case "model-lifecycle":- handleWidgetMessage(WIDGET_REGISTRY, msg.data);+ handleWidgetMessage(WIDGET_REGISTRY, msg);return;case "consumer-capabilities":diff --git a/frontend/src/core/islands/bridge.ts b/frontend/src/core/islands/bridge.tsindex dcf2a7d996..23287010c1 100644--- a/frontend/src/core/islands/bridge.ts+++ b/frontend/src/core/islands/bridge.ts@@ -5,15 +5,16 @@ import { getWorkerRPC } from "@/core/wasm/rpc";import { Deferred } from "@/utils/Deferred";import { throwNotImplemented } from "@/utils/functions";import { Logger } from "@/utils/Logger";+import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";import { generateUUID } from "@/utils/uuid";import { initialNotebookState, notebookAtom } from "../cells/cells";-import type { ClientCommand } from "../kernel/messages";+import type { ClientCommand, NotificationMessage } from "../kernel/messages";import type { EditRequests, RunRequests } from "../network/types";import { store as defaultStore } from "../state/jotai";import { getMarimoExportContext } from "../static/export-context";import { createMarimoFile, parseMarimoIslandApps } from "./parse";import { islandsInitializedAtom, islandsPendingInitialRunsAtom } from "./state";-import type { IslandsKernelMessage, WorkerSchema } from "./worker/worker";+import type { WorkerSchema } from "./worker/worker";import type { WorkerFactory } from "./worker-factory";import { DefaultWorkerFactory } from "./worker-factory";@@ -52,14 +53,14 @@ interface AppSession {* @example* ```ts* const bridge = new IslandsPyodideBridge();- * bridge.consumeMessages(message => console.log(message));+ * bridge.consumeNotifications((notification) => console.log(notification.op));* await bridge.initializeApps();* ```*/export class IslandsPyodideBridge implements RunRequests, EditRequests {private rpc: ReturnType<typeof getWorkerRPC<WorkerSchema>>;- private messageConsumer:- | ((message: IslandsKernelMessage) => void)+ private notificationConsumer:+ | ((notification: NotificationMessage, sessionGeneration: number) => void)| undefined;private readonly store: typeof defaultStore;private readonly root: Document | Element;@@ -110,14 +111,24 @@ export class IslandsPyodideBridge implements RunRequests, EditRequests {},);- this.rpc.addMessageListener("kernelMessage", (message) => {- // A stopped session can still have messages in transit after the next- // app takes ownership of the shared frontend state.- if (!this.activeSessionGenerations.has(message.sessionGeneration)) {- return;- }- this.messageConsumer?.(message);- });+ this.rpc.addMessageListener(+ "notebookEvent",+ ({ event, sessionGeneration }) => {+ // A stopped session can still have events in transit after the next+ // app takes ownership of the shared frontend state.+ if (!this.activeSessionGenerations.has(sessionGeneration)) {+ return;+ }+ // Islands follow only the notebook's marimo notifications.+ const frame = jsonParseWithSpecialChar(event) as {+ event: string;+ data: { marimo?: NotificationMessage };+ };+ if (frame.event === "marimo" && frame.data.marimo) {+ this.notificationConsumer?.(frame.data.marimo, sessionGeneration);+ }+ },+ );}public async initializeApps(): Promise<void> {@@ -246,14 +257,14 @@ export class IslandsPyodideBridge implements RunRequests, EditRequests {await this.rpc.proxy.request.startSession(opts);}- /**- * Sets up a consumer for kernel messages- */- public consumeMessages(- consumer: (message: IslandsKernelMessage) => void,+ /** Receive each running session's marimo notifications. */+ public consumeNotifications(+ consumer: (+ notification: NotificationMessage,+ sessionGeneration: number,+ ) => void,): void {- this.messageConsumer = consumer;- this.rpc.proxy.send.consumerReady({});+ this.notificationConsumer = consumer;}// ============================================================================diff --git a/frontend/src/core/islands/worker/__tests__/controller.test.ts b/frontend/src/core/islands/worker/__tests__/controller.test.tsindex 5f6e71d6dc..a5d669c9ca 100644--- a/frontend/src/core/islands/worker/__tests__/controller.test.ts+++ b/frontend/src/core/islands/worker/__tests__/controller.test.ts@@ -54,7 +54,6 @@ function startSession(controller: TestController, code: string) {return controller.startSession({code,filename: `${code}.py`,- onMessage: vi.fn(),});}diff --git a/frontend/src/core/islands/worker/controller.ts b/frontend/src/core/islands/worker/controller.tsindex acbdd0c73c..496ab5b4ca 100644--- a/frontend/src/core/islands/worker/controller.ts+++ b/frontend/src/core/islands/worker/controller.ts@@ -1,11 +1,9 @@/* Copyright 2026 Marimo. All rights reserved. */import type { PyodideInterface } from "pyodide";import { defaultUserConfig } from "@/core/config/config-schema";-import type { NotificationPayload } from "@/core/kernel/messages";import { DefaultWasmController } from "@/core/wasm/worker/bootstrap";import { WasmFileSystem } from "@/core/wasm/worker/fs";import type { SerializedBridge } from "@/core/wasm/worker/types";-import type { JsonString } from "@/utils/json/base64";import { Logger } from "../../../utils/Logger";export class ReadonlyWasmController extends DefaultWasmController {@@ -40,13 +38,11 @@ export class ReadonlyWasmController extends DefaultWasmController {public override async startSession(opts: {code: string;filename: string | null;- onMessage?: (message: JsonString<NotificationPayload>) => void;}): Promise<SerializedBridge> {const bridge = super.startSession({queryParameters: {},code: opts.code,filename: opts.filename,- onMessage: opts.onMessage,userConfig: defaultUserConfig(),});diff --git a/frontend/src/core/islands/worker/worker.tsx b/frontend/src/core/islands/worker/worker.tsxindex ea0bec7954..667e2f57b3 100644--- a/frontend/src/core/islands/worker/worker.tsx+++ b/frontend/src/core/islands/worker/worker.tsx@@ -8,14 +8,11 @@ import {createWorkerParentTransport,type RPCSchema,} from "rpc-anywhere";-import type { NotificationPayload } from "@/core/kernel/messages";import type { ParentSchema } from "@/core/wasm/rpc";import { shouldLoadDuckDBPackages } from "@/core/wasm/utils";import { TRANSPORT_ID } from "@/core/wasm/worker/constants";import { getPyodideVersion } from "@/core/wasm/worker/getPyodideVersion";-import { MessageBuffer } from "@/core/wasm/worker/message-buffer";import type { RawBridge, SerializedBridge } from "@/core/wasm/worker/types";-import type { JsonString } from "@/utils/json/base64";import { Logger } from "@/utils/Logger";import { prettyError } from "../../../utils/errors";import { invariant } from "../../../utils/invariant";@@ -45,14 +42,11 @@ async function loadPyodideAndPackages() {}const pyodideReadyPromise = loadPyodideAndPackages();-export interface IslandsKernelMessage {- message: JsonString<NotificationPayload>;+/** An event of one session's notebook stream. */+export interface IslandsNotebookEvent {+ event: string;sessionGeneration: number;}--const messageBuffer = new MessageBuffer<IslandsKernelMessage>((payload) => {- rpc.send.kernelMessage(payload);-});interface SessionRequest {code: string;appId: string;@@ -62,6 +56,8 @@ interface SessionRequest {let activeSession:| (Omit<SessionRequest, "code"> & { bridge: SerializedBridge })| undefined;+/** Every running session's bridge, kept while the worker follows its notebook. */+const sessions = new Set<SerializedBridge>();let sessionQueue = Promise.resolve();async function startSession(@@ -79,18 +75,12 @@ async function startSession(code: opts.code,filename: `app-${opts.appId}.py`,});- const nextBridge = await self.controller.startSession({- ...notebook,- onMessage: (message) => {- messageBuffer.push({- message,- sessionGeneration: opts.sessionGeneration,- });- },- });- if (activeSession) {- (nextBridge as unknown as PyProxy).destroy();- } else {+ const nextBridge = await self.controller.startSession(notebook);+ sessions.add(nextBridge);+ // Follow before the session instantiates. Each subscription begins with+ // its own kernel-ready and the session's retained notifications.+ await follow(nextBridge, opts.sessionGeneration);+ if (!activeSession) {activeSession = {appId: opts.appId,bridge: nextBridge,@@ -107,10 +97,27 @@ async function startSession(}async function stopActiveSession(): Promise<void> {- const session = activeSession;+ // Stopping closes every session and its notebook subscriptions.await self.controller.stopSession();activeSession = undefined;- (session?.bridge as unknown as PyProxy | undefined)?.destroy();+ for (const bridge of sessions) {+ (bridge as unknown as PyProxy).destroy();+ }+ sessions.clear();+}++async function follow(+ bridge: SerializedBridge,+ sessionGeneration: number,+): Promise<void> {+ const notebook = bridge.notebook;+ try {+ await notebook.subscribe(`island-${sessionGeneration}`, null, (event) => {+ rpc.send.notebookEvent({ event, sessionGeneration });+ });+ } finally {+ notebook.destroy();+ }}function enqueueSession<T>(operation: () => Promise<T>): Promise<T> {@@ -243,8 +250,8 @@ export type WorkerSchema = RPCSchema<messages: {// Emitted when the worker is readyready: {};- // Emitted when the kernel sends a message- kernelMessage: IslandsKernelMessage;+ // Emitted for each event of a session's notebook stream+ notebookEvent: IslandsNotebookEvent;// Emitted when the Pyodide is initializedinitialized: {};// Emitted when the Pyodide fails to initialize@@ -263,13 +270,6 @@ const rpc = createRPC<WorkerSchema, ParentSchema>({rpc.send("ready", {});-/// Listeners-// When the consumer is ready, start the message buffer-rpc.addMessageListener("consumerReady", async () => {- await pyodideReadyPromise; // Make sure loading is done- messageBuffer.start();-});-function getMarimoVersion() {return self.name; // We store the version in the worker name}diff --git a/frontend/src/core/wasm/rpc.ts b/frontend/src/core/wasm/rpc.tsindex 924cad17ff..a4794e9bb9 100644--- a/frontend/src/core/wasm/rpc.ts+++ b/frontend/src/core/wasm/rpc.ts@@ -11,7 +11,6 @@ import { TRANSPORT_ID } from "./worker/constants";export type ParentSchema = RPCSchema<{messages: {- consumerReady: {};bootstrap: WasmRuntimeConfig;};}>;diff --git a/frontend/src/core/wasm/worker/bootstrap.ts b/frontend/src/core/wasm/worker/bootstrap.tsindex 357293e7d8..e69f1dfbea 100644--- a/frontend/src/core/wasm/worker/bootstrap.ts+++ b/frontend/src/core/wasm/worker/bootstrap.ts@@ -2,8 +2,6 @@import { loadPyodide, type PyodideInterface } from "pyodide";import type { PyCallable, PyProxy } from "pyodide/ffi";import type { UserConfig } from "@/core/config/config-schema";-import type { NotificationPayload } from "@/core/kernel/messages";-import type { JsonString } from "@/utils/json/base64";import { invariant } from "../../../utils/invariant";import { Logger } from "../../../utils/Logger";import { WasmFileSystem } from "./fs";@@ -142,21 +140,10 @@ export class DefaultWasmController implements WasmController {queryParameters: Record<string, string | string[]>;code: string;filename: string | null;- /** Receives every notification, for callers that do not follow the notebook's events. */- onMessage?: (message: JsonString<NotificationPayload>) => void;userConfig: UserConfig;}): Promise<SerializedBridge> {const sessionGeneration = this.sessionGeneration;- const { code, filename, onMessage, queryParameters, userConfig } = opts;- // We pass down a messenger object to the code- // This is used to have synchronous communication between the JS and Python code- // Previously, we used a queue, but this would not properly flush the queue- // during processing of a cell's code.- //- // This adds a messenger object to the global scope (import js; js.messenger.callback)- self.messenger = {- callback: onMessage,- };+ const { code, filename, queryParameters, userConfig } = opts;self.query_params = queryParameters;self.user_config = userConfig;@@ -170,14 +157,12 @@ export class DefaultWasmController implements WasmController {import jsfrom marimo._pyodide.bootstrap import create_session, instantiate- assert js.messenger, "messenger is not defined"assert js.query_params, "query_params is not defined"def create_session_resources():session, bridge = create_session(filename="${nbFilename}",query_params=js.query_params.to_py(),- message_callback=js.messenger.callback,user_config=js.user_config.to_py(),)session_task = Nonediff --git a/frontend/src/core/wasm/worker/message-buffer.ts b/frontend/src/core/wasm/worker/message-buffer.tsdeleted file mode 100644index 985e316fce..0000000000--- a/frontend/src/core/wasm/worker/message-buffer.ts+++ /dev/null@@ -1,33 +0,0 @@-/* Copyright 2026 Marimo. All rights reserved. */--/**- * A buffer for messages that are received before the worker is ready to process them.- * Once the worker is ready, the buffer is flushed.- */-export class MessageBuffer<T> {- private buffer: T[];- private started = false;- private onMessage: (data: T) => void;- public constructor(onMessage: (data: T) => void) {- this.onMessage = onMessage;- this.buffer = [];- }-- public push = (data: T) => {- if (this.started) {- this.onMessage(data);- } else {- this.buffer.push(data);- }- };-- /**- * Start processing messages- */- public start = () => {- this.started = true;- // Flush the buffer- this.buffer.forEach((data) => this.onMessage(data));- this.buffer = [];- };-}diff --git a/frontend/src/core/wasm/worker/types.ts b/frontend/src/core/wasm/worker/types.tsindex fa731e87bd..271e5d9586 100644--- a/frontend/src/core/wasm/worker/types.ts+++ b/frontend/src/core/wasm/worker/types.ts@@ -2,11 +2,7 @@import type { PyodideInterface } from "pyodide";import type { WasmRuntimeConfig } from "../runtime-config";import type { UserConfig } from "@/core/config/config-schema";-import type {- ClientCommand,- NotificationPayload,-} from "@/core/kernel/messages";-import type { JsonString } from "@/utils/json/base64";+import type { ClientCommand } from "@/core/kernel/messages";import type {CodeCompletionRequest,CopyNotebookRequest,@@ -69,8 +65,6 @@ export interface WasmController {code: string;filename: string | null;userConfig: UserConfig;- /** Receives every notification, for callers that do not follow the notebook's events. */- onMessage?: (message: JsonString<NotificationPayload>) => void;}): Promise<SerializedBridge>;}diff --git a/marimo/_pyodide/bootstrap.py b/marimo/_pyodide/bootstrap.pyindex 4ab2acab03..0a65e44f96 100644--- a/marimo/_pyodide/bootstrap.py+++ b/marimo/_pyodide/bootstrap.py@@ -10,11 +10,6 @@ from marimo._runtime._wasm import ensure_wasm_runtime_bootstrappedensure_wasm_runtime_bootstrapped()from marimo._config.config import merge_config-from marimo._messaging.serde import (- deserialize_kernel_message,- serialize_kernel_message,-)-from marimo._pyodide.kernel_ready import kernel_readyfrom marimo._runtime.commands import (AppMetadata,CreateNotebookCommand,@@ -30,10 +25,7 @@ from marimo._session.notebook import AppFileManagerfrom marimo._utils.parse_dataclass import parse_rawif TYPE_CHECKING:- from collections.abc import Callable-from marimo._config.config import MarimoConfig- from marimo._messaging.types import KernelMessagefrom marimo._pyodide.pyodide_session import PyodideBridge, PyodideSession@@ -72,7 +64,6 @@ def instantiate(def create_session(filename: str,query_params: SerializedQueryParams,- message_callback: Callable[[str], None] | None,user_config: MarimoConfig,) -> tuple[PyodideSession, PyodideBridge]:"""@@ -81,9 +72,6 @@ def create_session(Args:filename: The filename of the app.query_params: The query parameters from the URL.- message_callback: Receives every notification as JSON, starting with- kernel-ready, or None when the caller follows the notebook's- event stream instead.user_config: The user configuration.Returns:@@ -93,13 +81,6 @@ def create_session("""ensure_wasm_runtime_bootstrapped()- def write_kernel_message(notification: KernelMessage) -> None:- assert message_callback is not None- data_json_str = notification.decode("utf-8")- name = deserialize_kernel_message(notification).name- text = f'{{"op": "{name}", "data": {data_json_str}}}'- message_callback(text)-# Lazy import to decrease startup timefrom marimo._config.config import merge_default_configfrom marimo._config.manager import ScriptConfigReader@@ -119,13 +100,6 @@ def create_session()app = app_file_manager.app- if message_callback is not None:- # We want this message to be performant, so any expensive operations- # should be after this message is sent- write_kernel_message(- serialize_kernel_message(kernel_ready(app_file_manager, None))- )-from marimo._pyodide.pyodide_session import PyodideBridge, PyodideSessionsession = PyodideSession(@@ -144,8 +118,6 @@ def create_session(user_config,)- if message_callback is not None:- session.consumers.append(write_kernel_message)bridge = PyodideBridge(session)return session, bridgediff --git a/tests/_pyodide/test_bootstrap.py b/tests/_pyodide/test_bootstrap.pyindex 7eaaa26e1f..16de658f85 100644--- a/tests/_pyodide/test_bootstrap.py+++ b/tests/_pyodide/test_bootstrap.py@@ -1,15 +1,12 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations-import jsonfrom typing import TYPE_CHECKING, Anyimport pytestfrom marimo._config.config import DEFAULT_CONFIGfrom marimo._messaging.msgspec_encoder import encode_json_str-from marimo._messaging.notification import VariablesNotification-from marimo._messaging.serde import serialize_kernel_messagefrom marimo._pyodide.bootstrap import create_session, save_filefrom marimo._pyodide.pyodide_session import PyodideSessionfrom marimo._server.models.models import SaveNotebookRequest@@ -17,18 +14,9 @@ from marimo._session.model import SessionModefrom marimo._types.ids import CellId_tif TYPE_CHECKING:- from collections.abc import Callablefrom pathlib import Path-def mock_message_callback() -> Callable[[str], None]:- def callback(message: str) -> None:- pass-- return callback--FILE_CONTENTS = """import marimo@@ -66,13 +54,11 @@ def mock_app_file_with_script_config(tmp_path: Path) -> Path:async def test_create_session_with_default_config(- mock_message_callback: Callable[[str], None],mock_app_file: Path,) -> None:session, _ = create_session(filename=str(mock_app_file),query_params={},- message_callback=mock_message_callback,user_config=DEFAULT_CONFIG,)@@ -81,13 +67,11 @@ async def test_create_session_with_default_config(async def test_create_session_with_script_config(- mock_message_callback: Callable[[str], None],mock_app_file_with_script_config: Path,) -> None:session, _ = create_session(filename=str(mock_app_file_with_script_config),query_params={},- message_callback=mock_message_callback,user_config=DEFAULT_CONFIG,)@@ -96,7 +80,6 @@ async def test_create_session_with_script_config(async def test_create_session_with_invalid_script_config(- mock_message_callback: Callable[[str], None],tmp_path: Path,) -> None:# Create a file with invalid config@@ -113,7 +96,6 @@ async def test_create_session_with_invalid_script_config(session, _ = create_session(filename=str(filename),query_params={},- message_callback=mock_message_callback,user_config=DEFAULT_CONFIG,)@@ -125,13 +107,11 @@ async def test_create_session_with_invalid_script_config(async def test_instantiate(- mock_message_callback: Callable[[str], None],mock_app_file: Path,) -> None:session, _ = create_session(filename=str(mock_app_file),query_params={},- message_callback=mock_message_callback,user_config=DEFAULT_CONFIG,)@@ -180,53 +160,3 @@ def test_save_file(# Verify the file was saved correctlysaved_content = mock_app_file.read_text()assert "print('hello')" in saved_content---async def test_message_callback_format(- mock_app_file: Path,-) -> None:- """Test that message_callback receives properly formatted JSON."""- received_messages: list[str] = []-- _session, _ = create_session(- filename=str(mock_app_file),- query_params={},- message_callback=lambda text: received_messages.append(text),- user_config=DEFAULT_CONFIG,- )-- assert len(received_messages) >= 1- parsed = json.loads(received_messages[0])- assert parsed["op"] == "kernel-ready"- assert isinstance(parsed["data"], dict)---async def test_message_callback_receives_later_notifications(- mock_app_file: Path,-) -> None:- received: list[str] = []- session, _ = create_session(- filename=str(mock_app_file),- query_params={},- message_callback=received.append,- user_config=DEFAULT_CONFIG,- )- session._on_message(- serialize_kernel_message(VariablesNotification(variables=[]))- )- assert [json.loads(message)["op"] for message in received] == [- "kernel-ready",- "variables",- ]---async def test_create_session_without_message_callback(- mock_app_file: Path,-) -> None:- session, _ = create_session(- filename=str(mock_app_file),- query_params={},- message_callback=None,- user_config=DEFAULT_CONFIG,- )- assert session.consumers == [session.session_view.add_raw_notification]diff --git a/tests/_pyodide/test_pyodide_acceptance.mjs b/tests/_pyodide/test_pyodide_acceptance.mjsindex 13be8b12da..62f53448d7 100644--- a/tests/_pyodide/test_pyodide_acceptance.mjs+++ b/tests/_pyodide/test_pyodide_acceptance.mjs@@ -210,11 +210,13 @@ print("Test notebook created at /home/pyodide/test_notebook.py")import jsonimport asyncio-# Collect messages sent by the session+# Collect the notifications the session sends on its notebook streammessages = []-def message_callback(msg):- messages.append(msg)+def receive(raw):+ event = json.loads(raw)+ if event["event"] == "marimo":+ messages.append(event["data"]["marimo"])# Import and create sessionfrom marimo._pyodide.bootstrap import create_session, instantiate@@ -226,9 +228,9 @@ def init(auto_instantiate=True):session, bridge = create_session(filename="/home/pyodide/test_notebook.py",query_params={},- message_callback=message_callback,user_config={},)+await bridge.notebook.subscribe("messages", None, receive)# Run the sessioninit(session)@@ -278,7 +280,7 @@ if missing_methods:# Sleep until we get two messages (one after kernel-ready)while len(messages) < 2:await asyncio.sleep(0.1)-message_ops = [json.loads(msg)["op"] for msg in messages]+message_ops = [msg["op"] for msg in messages]print("Messages received:", message_ops);kernel_ready_received = "kernel-ready" in message_opsdiff --git a/tests/_pyodide/test_wasm_concurrency_matrix.mjs b/tests/_pyodide/test_wasm_concurrency_matrix.mjsindex 412ff064d6..65c58a7ef0 100644--- a/tests/_pyodide/test_wasm_concurrency_matrix.mjs+++ b/tests/_pyodide/test_wasm_concurrency_matrix.mjs@@ -345,7 +345,7 @@ async def create_wasm_concurrency_session(notebook_path, callback_name):with open(notebook_path, "w") as f:f.write(WASM_CONCURRENCY_NOTEBOOK)- session, bridge = create_session(notebook_path, {}, None, {})+ session, bridge = create_session(notebook_path, {}, {})# Notifications arrive on the notebook stream, after its snapshot.await bridge.notebook.subscribe("concurrency-matrix", None, getattr(js, callback_name)