-
Notifications
You must be signed in to change notification settings - Fork 851
improvement: lazy backend #7523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
d31fa2d to
fa804c9
Compare
fa804c9 to
8d2c360
Compare
8d2c360 to
fa3b6d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements lazy backend initialization for marimo, allowing users to view and edit notebooks before the kernel connects. The kernel now only starts when explicitly triggered by user actions like clicking Run or Connect, improving initial load times and enabling offline-first editing workflows.
Key Changes:
- Introduced a new
NOT_STARTEDWebSocket state for pre-connection UI - Created
createLazyRequests()wrapper to intercept requests and trigger kernel initialization on first use - Added
useConnectToRuntime()hook and UI components for manual connection triggering - Modified runtime configuration to support lazy initialization with a new
lazyboolean field
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
frontend/src/mount.tsx |
Updated Zod schemas to use looseObject() and added lazy flag to runtime config initialization |
frontend/src/core/websocket/useMarimoKernelConnection.tsx |
Enabled lazy kernel support and removed TODO comment for existing cells handling |
frontend/src/core/websocket/types.ts |
Added NOT_STARTED state to WebSocket state enum |
frontend/src/core/websocket/connection-utils.ts |
Added isAppNotStarted() helper and NOT_STARTED connection tooltip |
frontend/src/core/runtime/types.ts |
Added lazy boolean field to RuntimeConfig interface with documentation |
frontend/src/core/runtime/runtime.ts |
Added debug logging for runtime initialization |
frontend/src/core/runtime/config.ts |
Implemented useConnectToRuntime() hook for manual connection triggering, set lazy default to true |
frontend/src/core/runtime/__tests__/runtime.test.ts |
Updated all test cases to include lazy: true in mock RuntimeConfig objects |
frontend/src/core/runtime/__tests__/createWsUrl.test.ts |
Updated all test cases to include lazy: true in mock RuntimeConfig objects |
frontend/src/core/network/resolve.ts |
Wrapped network requests with lazy initialization wrapper for non-WASM/non-static notebooks |
frontend/src/core/network/requests-lazy.ts |
New file implementing lazy request interception logic with memoized initialization |
frontend/src/core/network/requests-lazy.test.ts |
New comprehensive test suite for lazy requests functionality |
frontend/src/core/network/connection.ts |
Changed default connection state to NOT_STARTED, added canInteractWithAppAtom and isNotStartedAtom |
frontend/src/core/kernel/state.ts |
New file defining kernel instantiation state atom and wait helper |
frontend/src/core/kernel/handlers.ts |
Updated to set kernel state after instantiation, added error logging for config parsing |
frontend/src/core/islands/main.ts |
Added setKernelState NOOP to kernel ready handler options |
frontend/src/core/edit-app.tsx |
Added NotStartedConnectionAlert component for empty notebooks |
frontend/src/core/codemirror/cm.ts |
Removed dynamic readonly extension setup |
frontend/src/components/editor/renderers/cell-array.tsx |
Replaced isConnectedAtom with canInteractWithAppAtom for button states, added NotStartedConnectionAlert |
frontend/src/components/editor/notebook-cell.tsx |
Simplified cell action logic to remove connection checks for cell creation |
frontend/src/components/editor/controls/Controls.tsx |
Replaced isConnectedAtom with canInteractWithAppAtom for run button state |
frontend/src/components/editor/chrome/wrapper/footer-items/backend-status.tsx |
Added click handler for connecting when not started, updated to use connection helper functions |
frontend/src/components/editor/cell/code/cell-editor.tsx |
Replaced WebSocketState check with isAppConnecting() helper |
frontend/src/components/editor/app-container.tsx |
Replaced WebSocketState check with isAppClosed() helper |
frontend/src/components/editor/alerts/connecting-alert.tsx |
Refactored ConnectingAlert to use proper conditional rendering, added NotStartedConnectionAlert component |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,65 @@ | |||
| /* Copyright 2024 Marimo. All rights reserved. */ | |||
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The copyright year is 2024, but other files in this PR use 2026. This should be updated to 2026 for consistency.
| /* Copyright 2024 Marimo. All rights reserved. */ | |
| /* Copyright 2026 Marimo. All rights reserved. */ |
| @@ -0,0 +1,310 @@ | |||
| /* Copyright 2024 Marimo. All rights reserved. */ | |||
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The copyright year is 2024, but other files in this PR use 2026. This should be updated to 2026 for consistency.
| /* Copyright 2024 Marimo. All rights reserved. */ | |
| /* Copyright 2026 Marimo. All rights reserved. */ |
| @@ -0,0 +1,16 @@ | |||
| import { atom } from "jotai"; | |||
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing copyright header. This file should include a copyright notice at the top, like "/* Copyright 2026 Marimo. All rights reserved. */".
| return useEvent(async () => { | ||
| if (isAppNotStarted(connection.state)) { | ||
| setConnection({ state: WebSocketState.CONNECTING }); | ||
| await runtimeManager.init(); |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The connection state is set to CONNECTING before calling runtimeManager.init(), but if init() fails or throws an error, the connection state remains stuck in CONNECTING. Consider wrapping this in a try-catch to handle initialization failures and set the connection state to CLOSED on error.
| await runtimeManager.init(); | |
| try { | |
| await runtimeManager.init(); | |
| } catch (error) { | |
| Logger.log("Failed to initialize runtime", error); | |
| setConnection({ state: WebSocketState.CLOSED }); | |
| throw error; | |
| } |
dmadisetti
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't get the preview to work, but the dynamic connection seems fine.
Only caveat was that after the 25 failed attempts, I needed to refresh (connect button didn't seem to work)
| @@ -0,0 +1,310 @@ | |||
| /* Copyright 2024 Marimo. All rights reserved. */ | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frontend/src/core/network/__tests__/requests-lazy.test.ts ?
Allows marimo to start with the kernel in a "lazy" state - users can view and edit cells before the kernel connects. The kernel only starts when explicitly triggered (clicking Run or Connect).
Flow
handleKernelReadysends edited codes to backend viasendInstantiateauto_instantiatesetting)Key Changes
Frontend:
NOT_STARTEDWebSocket state for pre-connection UIcreateLazyRequests()wrapper intercepts all requests and triggers kernel init on first useuseConnectToRuntime()hook for manual connection triggerNotStartedConnectionAlertcomponent with "Click to connect" buttoncanInteractWithAppAtomenables UI interaction before connection