@marimo-team/islands 0.23.7-dev14 → 0.23.7-dev16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25691,7 +25691,7 @@ ${_}`,
25691
25691
  return Logger.warn("Failed to get version from mount config"), null;
25692
25692
  }
25693
25693
  }
25694
- marimoVersionAtom = atom(getVersionFromMountConfig() || "0.23.7-dev14");
25694
+ marimoVersionAtom = atom(getVersionFromMountConfig() || "0.23.7-dev16");
25695
25695
  showCodeInRunModeAtom = atom(true);
25696
25696
  atom(null);
25697
25697
  var import_compiler_runtime = require_compiler_runtime();
package/dist/main.js CHANGED
@@ -26,7 +26,7 @@ import { $ as useCellActions, At as DeferredRequestRegistry, B as safeExtractSet
26
26
  import { __tla as __tla_1 } from "./chunk-5FQGJX7Z-BOg95xG5.js";
27
27
  import { o as useSize, s as Root$2, u as createLucideIcon } from "./dist-ESg7xyoD.js";
28
28
  import { A as SquareFunction, C as DEFAULT_COLOR_SCHEME, D as SCALE_TYPE_DESCRIPTIONS, E as EMPTY_VALUE$1, O as TIME_UNIT_DESCRIPTIONS, S as DEFAULT_AGGREGATION, T as DEFAULT_TIME_UNIT, _ as AGGREGATION_TYPE_DESCRIPTIONS, a as AGGREGATION_FNS$1, b as COLOR_SCHEMES, c as COLOR_BY_FIELDS, d as NONE_VALUE, f as SELECTABLE_DATA_TYPES, g as TIME_UNITS, h as STRING_AGGREGATION_FNS, i as convertDataTypeToSelectable, j as ChartColumn, k as escapeFieldName, l as COMBINED_TIME_UNITS, m as SORT_TYPES, n as createSpecWithoutData, o as BIN_AGGREGATION, p as SINGLE_TIME_UNITS, r as isFieldSet, s as CHART_TYPES, t as augmentSpecWithData, u as ChartType, v as AGGREGATION_TYPE_ICON, w as DEFAULT_MAX_BINS_FACET, x as COUNT_FIELD, y as CHART_TYPE_ICON } from "./spec-DSIuqd3f.js";
29
- import { $ as filtersToFilterGroup, A as contextAwarePanelOwner, At as Funnel, B as TableCell, Bt as ChevronLeft, C as prettifyRowCount, Ct as useOverflowDetection, D as ContextAwarePanelItem, Dt as EmotionCacheProvider, E as ComboboxItem, Et as HtmlOutput, F as Toggle, Ft as Code, G as generateColumns, H as TableHeader, I as Fill, It as ChevronsUpDown, J as ColumnChartContext, K as inferFieldTypes, L as Provider$1, Lt as ChevronsRight, M as isCellAwareAtom, N as SlotNames, Nt as Ellipsis, O as PANEL_TYPES, Ot as TextWrap, P as slotsController, Pt as Download, Q as usePrevious$1, R as Table, Rt as ChevronsLeft, S as prettifyRowColumnCount, St as LazyVegaEmbed, T as Combobox, Tt as Kbd, U as TableRow, V as TableHead, Vt as ArrowDownWideNarrow, W as NAMELESS_COLUMN_PREFIX, X as DelayMount, Y as ColumnChartSpecModel, Z as useIntersectionObserver, _ as downloadBlob, _t as TabsList, at as TOO_MANY_ROWS, b as Progress, bt as ChartInfoState, c as Slide, ct as Command, d as JsonOutput, dt as CommandItem, et as getPageIndexForRow, f as OutputArea, ft as CommandList, g as ADD_PRINTING_CLASS, gt as TabsContent, h as InstallPackageButton, ht as Tabs, it as SELECT_COLUMN_ID, j as contextAwarePanelType, jt as EyeOff, k as contextAwarePanelOpen, kt as GripHorizontal, l as RadioGroup, lt as CommandEmpty, m as DataTable, mt as Maps, n as marimoVersionAtom, nt as loadTableData, o as SLIDE_TYPE_OPTIONS_BY_VALUE, ot as toFieldTypes, p as OutputRenderer, pt as CommandSeparator, q as renderCellValue, r as showCodeInRunModeAtom, rt as INDEX_COLUMN_NAME, st as getMimeValues, t as useNotebookCodeAvailable, tt as loadTableAndRawData, u as RadioGroupItem, ut as CommandInput, v as downloadByURL, vt as TabsTrigger, w as useInternalStateWithSync, wt as RenderTextWithLinks, x as Filenames, xt as ChartLoadingState, y as downloadHTMLAsImage, yt as ChartErrorState, z as TableBody, zt as ChevronsDownUp, __tla as __tla_2 } from "./code-visibility-fNUza_4s.js";
29
+ import { $ as filtersToFilterGroup, A as contextAwarePanelOwner, At as Funnel, B as TableCell, Bt as ChevronLeft, C as prettifyRowCount, Ct as useOverflowDetection, D as ContextAwarePanelItem, Dt as EmotionCacheProvider, E as ComboboxItem, Et as HtmlOutput, F as Toggle, Ft as Code, G as generateColumns, H as TableHeader, I as Fill, It as ChevronsUpDown, J as ColumnChartContext, K as inferFieldTypes, L as Provider$1, Lt as ChevronsRight, M as isCellAwareAtom, N as SlotNames, Nt as Ellipsis, O as PANEL_TYPES, Ot as TextWrap, P as slotsController, Pt as Download, Q as usePrevious$1, R as Table, Rt as ChevronsLeft, S as prettifyRowColumnCount, St as LazyVegaEmbed, T as Combobox, Tt as Kbd, U as TableRow, V as TableHead, Vt as ArrowDownWideNarrow, W as NAMELESS_COLUMN_PREFIX, X as DelayMount, Y as ColumnChartSpecModel, Z as useIntersectionObserver, _ as downloadBlob, _t as TabsList, at as TOO_MANY_ROWS, b as Progress, bt as ChartInfoState, c as Slide, ct as Command, d as JsonOutput, dt as CommandItem, et as getPageIndexForRow, f as OutputArea, ft as CommandList, g as ADD_PRINTING_CLASS, gt as TabsContent, h as InstallPackageButton, ht as Tabs, it as SELECT_COLUMN_ID, j as contextAwarePanelType, jt as EyeOff, k as contextAwarePanelOpen, kt as GripHorizontal, l as RadioGroup, lt as CommandEmpty, m as DataTable, mt as Maps, n as marimoVersionAtom, nt as loadTableData, o as SLIDE_TYPE_OPTIONS_BY_VALUE, ot as toFieldTypes, p as OutputRenderer, pt as CommandSeparator, q as renderCellValue, r as showCodeInRunModeAtom, rt as INDEX_COLUMN_NAME, st as getMimeValues, t as useNotebookCodeAvailable, tt as loadTableAndRawData, u as RadioGroupItem, ut as CommandInput, v as downloadByURL, vt as TabsTrigger, w as useInternalStateWithSync, wt as RenderTextWithLinks, x as Filenames, xt as ChartLoadingState, y as downloadHTMLAsImage, yt as ChartErrorState, z as TableBody, zt as ChevronsDownUp, __tla as __tla_2 } from "./code-visibility-BTdq0PKn.js";
30
30
  import { c as Calendar, i as createReducerAndAtoms, n as useOnUnmount, o as ToggleLeft, t as useOnMount } from "./useLifecycle-CjMjllqy.js";
31
31
  import { n as $fb18d541ea1ad717$export$ad991b66133851cf, r as $5a387cc49350e6db$export$722debc0e56fea39, t as $896ba0a80a8f4d36$export$85fd5fdf27bacc79 } from "./useDateFormatter-B3mCQMP3.js";
32
32
  import { t as Check } from "./check-CFM2mVDr.js";
@@ -44600,7 +44600,7 @@ ${c}
44600
44600
  if (l && l !== "slide") return l;
44601
44601
  if (c == null ? void 0 : c.has(e)) return "skip";
44602
44602
  }
44603
- var LazySlidesComponent = import_react.lazy(() => import("./reveal-component-CRR2Ac1v.js"));
44603
+ var LazySlidesComponent = import_react.lazy(() => import("./reveal-component-CMK1wxUM.js"));
44604
44604
  const SlidesLayoutPlugin = {
44605
44605
  type: "slides",
44606
44606
  name: "Slides",
@@ -8,7 +8,7 @@ import { t as require_react } from "./react-DA-nE2FX.js";
8
8
  import { t as require_compiler_runtime } from "./compiler-runtime-CEbnTgxf.js";
9
9
  import "./html-to-image-BwZL1Pkk.js";
10
10
  import "./chunk-5FQGJX7Z-BOg95xG5.js";
11
- import { Ft as Code, Mt as Expand, a as DEFAULT_SLIDE_TYPE, c as Slide, i as DEFAULT_DECK_TRANSITION, jt as EyeOff, s as SlideSidebar, t as useNotebookCodeAvailable } from "./code-visibility-fNUza_4s.js";
11
+ import { Ft as Code, Mt as Expand, a as DEFAULT_SLIDE_TYPE, c as Slide, i as DEFAULT_DECK_TRANSITION, jt as EyeOff, s as SlideSidebar, t as useNotebookCodeAvailable } from "./code-visibility-BTdq0PKn.js";
12
12
  import "./input-BAOe64zx.js";
13
13
  import "./toDate-CHtl9vts.js";
14
14
  import "./react-dom-BWRJ_g_k.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.23.7-dev14",
3
+ "version": "0.23.7-dev16",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -120,7 +120,7 @@
120
120
  "lz-string": "^1.5.0",
121
121
  "marked": "^15.0.12",
122
122
  "mermaid": "^11.12.3",
123
- "partysocket": "1.1.10",
123
+ "partysocket": "1.1.13",
124
124
  "path-to-regexp": "^8.4.0",
125
125
  "plotly.js": "^3.3.1",
126
126
  "pyodide": "0.27.7",
@@ -15,6 +15,7 @@ interface Props {
15
15
  connection: ConnectionStatus;
16
16
  isRunning: boolean;
17
17
  width: AppConfig["width"];
18
+ onReconnect?: () => void;
18
19
  }
19
20
 
20
21
  export const AppContainer: React.FC<PropsWithChildren<Props>> = ({
@@ -22,13 +23,18 @@ export const AppContainer: React.FC<PropsWithChildren<Props>> = ({
22
23
  connection,
23
24
  isRunning,
24
25
  children,
26
+ onReconnect,
25
27
  }) => {
26
28
  const connectionState = connection.state;
27
29
 
28
30
  return (
29
31
  <>
30
32
  <DynamicFavicon isRunning={isRunning} />
31
- <StatusOverlay connection={connection} isRunning={isRunning} />
33
+ <StatusOverlay
34
+ connection={connection}
35
+ isRunning={isRunning}
36
+ onReconnect={onReconnect}
37
+ />
32
38
  <PyodideLoader>
33
39
  <WrappedWithSidebar>
34
40
  {/** oxlint-ignore-next-line -- ID is used by other components to grab the DOM element */}
@@ -0,0 +1,108 @@
1
+ /* Copyright 2026 Marimo. All rights reserved. */
2
+ // @vitest-environment jsdom
3
+
4
+ import { fireEvent, render } from "@testing-library/react";
5
+ import { createStore, Provider as JotaiProvider } from "jotai";
6
+ import type React from "react";
7
+ import { describe, expect, it, vi } from "vitest";
8
+ import { TooltipProvider } from "@/components/ui/tooltip";
9
+ import { viewStateAtom } from "@/core/mode";
10
+ import {
11
+ type ConnectionStatus,
12
+ WebSocketClosedReason,
13
+ WebSocketState,
14
+ } from "@/core/websocket/types";
15
+ import { StatusOverlay } from "../status";
16
+
17
+ function renderOverlay(
18
+ connection: ConnectionStatus,
19
+ onReconnect?: () => void,
20
+ ): ReturnType<typeof render> {
21
+ const store = createStore();
22
+ store.set(viewStateAtom, { mode: "edit", cellAnchor: null });
23
+ const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
24
+ <JotaiProvider store={store}>
25
+ <TooltipProvider>{children}</TooltipProvider>
26
+ </JotaiProvider>
27
+ );
28
+ return render(
29
+ <StatusOverlay
30
+ connection={connection}
31
+ isRunning={false}
32
+ onReconnect={onReconnect}
33
+ />,
34
+ { wrapper },
35
+ );
36
+ }
37
+
38
+ describe("StatusOverlay disconnect indicator", () => {
39
+ it("invokes onReconnect when the disconnect icon is clicked", () => {
40
+ const onReconnect = vi.fn();
41
+ const { getByTestId } = renderOverlay(
42
+ {
43
+ state: WebSocketState.CLOSED,
44
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
45
+ reason: "kernel not found",
46
+ },
47
+ onReconnect,
48
+ );
49
+
50
+ const icon = getByTestId("disconnected-indicator") as HTMLButtonElement;
51
+ expect(icon.tagName).toBe("BUTTON");
52
+ expect(icon.disabled).toBe(false);
53
+ expect(icon.getAttribute("aria-label")).toBe("Reconnect to app");
54
+ fireEvent.click(icon);
55
+ expect(onReconnect).toHaveBeenCalledTimes(1);
56
+ });
57
+
58
+ it("renders a disabled button when no onReconnect is provided", () => {
59
+ const { getByTestId } = renderOverlay({
60
+ state: WebSocketState.CLOSED,
61
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
62
+ reason: "kernel not found",
63
+ });
64
+
65
+ const button = getByTestId("disconnected-indicator");
66
+ expect((button as HTMLButtonElement).disabled).toBe(true);
67
+ });
68
+
69
+ it.each([
70
+ [
71
+ WebSocketClosedReason.MALFORMED_QUERY,
72
+ "the kernel did not recognize a request; please file a bug with marimo",
73
+ ],
74
+ [
75
+ WebSocketClosedReason.KERNEL_STARTUP_ERROR,
76
+ "Failed to start kernel sandbox",
77
+ ],
78
+ ])(
79
+ "renders a disabled button for non-recoverable close reason %s",
80
+ (code, reason) => {
81
+ const onReconnect = vi.fn();
82
+ const { getByTestId } = renderOverlay(
83
+ { state: WebSocketState.CLOSED, code, reason },
84
+ onReconnect,
85
+ );
86
+
87
+ const button = getByTestId("disconnected-indicator") as HTMLButtonElement;
88
+ expect(button.disabled).toBe(true);
89
+ fireEvent.click(button);
90
+ expect(onReconnect).not.toHaveBeenCalled();
91
+ },
92
+ );
93
+
94
+ it("does not render the disconnect icon when another tab has taken over", () => {
95
+ const onReconnect = vi.fn();
96
+ const { queryByTestId } = renderOverlay(
97
+ {
98
+ state: WebSocketState.CLOSED,
99
+ code: WebSocketClosedReason.ALREADY_RUNNING,
100
+ reason: "another browser tab is already connected to the kernel",
101
+ canTakeover: true,
102
+ },
103
+ onReconnect,
104
+ );
105
+
106
+ expect(queryByTestId("disconnected-indicator")).toBeNull();
107
+ });
108
+ });
@@ -7,16 +7,26 @@ import { Tooltip } from "@/components/ui/tooltip";
7
7
  import { notebookScrollToRunning } from "@/core/cells/actions";
8
8
  import { onlyScratchpadIsRunningAtom } from "@/core/cells/cells";
9
9
  import { viewStateAtom } from "@/core/mode";
10
- import { type ConnectionStatus, WebSocketState } from "@/core/websocket/types";
10
+ import {
11
+ type ConnectionStatus,
12
+ WebSocketClosedReason,
13
+ WebSocketState,
14
+ } from "@/core/websocket/types";
11
15
  import { cn } from "@/utils/cn";
12
16
 
13
17
  export const StatusOverlay: React.FC<{
14
18
  connection: ConnectionStatus;
15
19
  isRunning: boolean;
16
- }> = ({ connection, isRunning }) => {
20
+ onReconnect?: () => void;
21
+ }> = ({ connection, isRunning, onReconnect }) => {
17
22
  const { mode } = useAtomValue(viewStateAtom);
18
23
  const isClosed = connection.state === WebSocketState.CLOSED;
19
24
  const isOpen = connection.state === WebSocketState.OPEN;
25
+ // Only KERNEL_DISCONNECTED is recoverable by a retry. Other terminal
26
+ // reasons (MALFORMED_QUERY, KERNEL_STARTUP_ERROR) would deterministically
27
+ // fail the same way; ALREADY_RUNNING is handled by `LockedIcon` below.
28
+ const canReconnect =
29
+ isClosed && connection.code === WebSocketClosedReason.KERNEL_DISCONNECTED;
20
30
 
21
31
  return (
22
32
  <>
@@ -28,7 +38,11 @@ export const StatusOverlay: React.FC<{
28
38
  )}
29
39
  >
30
40
  {isOpen && isRunning && <RunningIcon />}
31
- {isClosed && !connection.canTakeover && <DisconnectedIcon />}
41
+ {isClosed && !connection.canTakeover && (
42
+ <DisconnectedIcon
43
+ onReconnect={canReconnect ? onReconnect : undefined}
44
+ />
45
+ )}
32
46
  {isClosed && connection.canTakeover && <LockedIcon />}
33
47
  </div>
34
48
  </>
@@ -37,13 +51,33 @@ export const StatusOverlay: React.FC<{
37
51
 
38
52
  const topLeftStatus = "print:hidden pointer-events-auto hover:cursor-pointer";
39
53
 
40
- const DisconnectedIcon = () => (
41
- <Tooltip content="App disconnected">
42
- <div className={topLeftStatus}>
43
- <UnlinkIcon className="w-[25px] h-[25px] text-(--red-11)" />
44
- </div>
45
- </Tooltip>
46
- );
54
+ const DisconnectedIcon: React.FC<{ onReconnect?: () => void }> = ({
55
+ onReconnect,
56
+ }) => {
57
+ const disabled = !onReconnect;
58
+ return (
59
+ <Tooltip
60
+ content={
61
+ disabled ? "App disconnected" : "App disconnected — click to reconnect"
62
+ }
63
+ >
64
+ {/* Wrapper span keeps the tooltip reachable when the button is
65
+ disabled — a disabled <button> swallows pointer events. */}
66
+ <span tabIndex={disabled ? 0 : -1}>
67
+ <button
68
+ type="button"
69
+ className={cn(topLeftStatus, "bg-transparent border-0 p-0")}
70
+ aria-label={disabled ? "App disconnected" : "Reconnect to app"}
71
+ data-testid="disconnected-indicator"
72
+ onClick={onReconnect}
73
+ disabled={disabled}
74
+ >
75
+ <UnlinkIcon className="w-[25px] h-[25px] text-(--red-11)" />
76
+ </button>
77
+ </span>
78
+ </Tooltip>
79
+ );
80
+ };
47
81
 
48
82
  const LockedIcon = () => (
49
83
  <Tooltip content="Notebook locked">
@@ -34,7 +34,10 @@ const RunPage = (props: Props) => {
34
34
 
35
35
  const Watermark = () => {
36
36
  return (
37
- <div className="fixed bottom-0 right-0 z-50" data-testid="watermark">
37
+ <div
38
+ className="fixed bottom-0 right-0 z-50 print:hidden"
39
+ data-testid="watermark"
40
+ >
38
41
  <a
39
42
  href={Constants.githubPage}
40
43
  target="_blank"
@@ -79,7 +79,7 @@ export const EditApp: React.FC<AppProps> = ({
79
79
  };
80
80
  }, []);
81
81
 
82
- const { connection } = useMarimoKernelConnection({
82
+ const { connection, reconnect } = useMarimoKernelConnection({
83
83
  autoInstantiate: userConfig.runtime.auto_instantiate,
84
84
  setCells: (cells, layout) => {
85
85
  setCells(cells);
@@ -147,6 +147,7 @@ export const EditApp: React.FC<AppProps> = ({
147
147
  connection={connection}
148
148
  isRunning={isRunning}
149
149
  width={appConfig.width}
150
+ onReconnect={reconnect}
150
151
  >
151
152
  <AppHeader
152
153
  connection={connection}
@@ -38,7 +38,7 @@ export const RunApp: React.FC<AppProps> = ({ appConfig }) => {
38
38
  };
39
39
  }, []);
40
40
 
41
- const { connection } = useMarimoKernelConnection({
41
+ const { connection, reconnect } = useMarimoKernelConnection({
42
42
  autoInstantiate: true,
43
43
  setCells: setCells,
44
44
  sessionId: getSessionId(),
@@ -84,6 +84,7 @@ export const RunApp: React.FC<AppProps> = ({ appConfig }) => {
84
84
  connection={connection}
85
85
  isRunning={isRunning}
86
86
  width={appConfig.width}
87
+ onReconnect={reconnect}
87
88
  >
88
89
  <AppHeader connection={connection} className="sm:pt-8">
89
90
  {galleryHref && (
@@ -0,0 +1,155 @@
1
+ /* Copyright 2026 Marimo. All rights reserved. */
2
+ // @vitest-environment jsdom
3
+
4
+ import { act, renderHook } from "@testing-library/react";
5
+ import { createStore, Provider as JotaiProvider } from "jotai";
6
+ import type React from "react";
7
+ import { ErrorBoundary } from "react-error-boundary";
8
+ import { beforeEach, describe, expect, it, vi } from "vitest";
9
+
10
+ vi.mock("@/core/websocket/useWebSocket", async () => {
11
+ const actual =
12
+ await vi.importActual<typeof import("../useWebSocket")>("../useWebSocket");
13
+ return {
14
+ ...actual,
15
+ useConnectionTransport: vi.fn(),
16
+ };
17
+ });
18
+
19
+ vi.mock("@/core/runtime/config", async () => {
20
+ const actual = await vi.importActual<typeof import("@/core/runtime/config")>(
21
+ "@/core/runtime/config",
22
+ );
23
+ return {
24
+ ...actual,
25
+ useRuntimeManager: vi.fn(),
26
+ };
27
+ });
28
+
29
+ import { useRuntimeManager } from "@/core/runtime/config";
30
+ import { connectionAtom } from "../../network/connection";
31
+ import type { SessionId } from "../../kernel/session";
32
+ import { WebSocketClosedReason, WebSocketState } from "../types";
33
+ import { useMarimoKernelConnection } from "../useMarimoKernelConnection";
34
+ import { useConnectionTransport } from "../useWebSocket";
35
+
36
+ interface MockTransport {
37
+ readyState: 0 | 1 | 2 | 3;
38
+ retryCount: number;
39
+ reconnect: ReturnType<typeof vi.fn>;
40
+ close: ReturnType<typeof vi.fn>;
41
+ send: ReturnType<typeof vi.fn>;
42
+ addEventListener: ReturnType<typeof vi.fn>;
43
+ removeEventListener: ReturnType<typeof vi.fn>;
44
+ }
45
+
46
+ function makeTransport(
47
+ readyState: 0 | 1 | 2 | 3 = WebSocket.CLOSED,
48
+ ): MockTransport {
49
+ return {
50
+ readyState,
51
+ retryCount: 0,
52
+ reconnect: vi.fn(),
53
+ close: vi.fn(),
54
+ send: vi.fn(),
55
+ addEventListener: vi.fn(),
56
+ removeEventListener: vi.fn(),
57
+ };
58
+ }
59
+
60
+ function makeRuntimeManager(isHealthy = vi.fn().mockResolvedValue(true)) {
61
+ return {
62
+ isHealthy,
63
+ getWsURL: () => new URL("ws://localhost/ws"),
64
+ waitForHealthy: vi.fn().mockResolvedValue(undefined),
65
+ isSameOrigin: true,
66
+ };
67
+ }
68
+
69
+ describe("useMarimoKernelConnection.reconnect()", () => {
70
+ let transport: MockTransport;
71
+ let isHealthy: ReturnType<typeof vi.fn>;
72
+ let store: ReturnType<typeof createStore>;
73
+
74
+ beforeEach(() => {
75
+ transport = makeTransport(WebSocket.CLOSED);
76
+ isHealthy = vi.fn().mockResolvedValue(true);
77
+ store = createStore();
78
+ store.set(connectionAtom, {
79
+ state: WebSocketState.CLOSED,
80
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
81
+ reason: "kernel not found",
82
+ });
83
+ vi.mocked(useConnectionTransport).mockReturnValue(transport);
84
+ vi.mocked(useRuntimeManager).mockReturnValue(
85
+ makeRuntimeManager(isHealthy) as unknown as ReturnType<
86
+ typeof useRuntimeManager
87
+ >,
88
+ );
89
+ });
90
+
91
+ function renderUseHook() {
92
+ const wrapper: React.FC<React.PropsWithChildren> = ({ children }) => (
93
+ <JotaiProvider store={store}>
94
+ <ErrorBoundary fallback={null}>{children}</ErrorBoundary>
95
+ </JotaiProvider>
96
+ );
97
+ return renderHook(
98
+ () =>
99
+ useMarimoKernelConnection({
100
+ sessionId: "test-session" as SessionId,
101
+ autoInstantiate: false,
102
+ setCells: () => {},
103
+ }),
104
+ { wrapper },
105
+ );
106
+ }
107
+
108
+ it("is a no-op when the transport is already OPEN", async () => {
109
+ transport.readyState = WebSocket.OPEN;
110
+ const { result } = renderUseHook();
111
+ await act(async () => {
112
+ await result.current.reconnect();
113
+ });
114
+ expect(isHealthy).not.toHaveBeenCalled();
115
+ expect(transport.reconnect).not.toHaveBeenCalled();
116
+ });
117
+
118
+ it("is a no-op when the transport is already CONNECTING", async () => {
119
+ transport.readyState = WebSocket.CONNECTING;
120
+ const { result } = renderUseHook();
121
+ await act(async () => {
122
+ await result.current.reconnect();
123
+ });
124
+ expect(isHealthy).not.toHaveBeenCalled();
125
+ expect(transport.reconnect).not.toHaveBeenCalled();
126
+ });
127
+
128
+ it("probes /health and reconnects when the runtime is healthy", async () => {
129
+ isHealthy.mockResolvedValue(true);
130
+ const { result } = renderUseHook();
131
+ await act(async () => {
132
+ await result.current.reconnect();
133
+ });
134
+ expect(isHealthy).toHaveBeenCalledOnce();
135
+ expect(transport.reconnect).toHaveBeenCalledOnce();
136
+ expect(store.get(connectionAtom)).toEqual({
137
+ state: WebSocketState.CONNECTING,
138
+ });
139
+ });
140
+
141
+ it("transitions to CLOSED and does not call ws.reconnect when the probe fails", async () => {
142
+ isHealthy.mockResolvedValue(false);
143
+ const { result } = renderUseHook();
144
+ await act(async () => {
145
+ await result.current.reconnect();
146
+ });
147
+ expect(isHealthy).toHaveBeenCalledOnce();
148
+ expect(transport.reconnect).not.toHaveBeenCalled();
149
+ expect(store.get(connectionAtom)).toEqual({
150
+ state: WebSocketState.CLOSED,
151
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
152
+ reason: "kernel not found",
153
+ });
154
+ });
155
+ });
@@ -0,0 +1,137 @@
1
+ /* Copyright 2026 Marimo. All rights reserved. */
2
+
3
+ import { afterEach, describe, expect, it, vi } from "vitest";
4
+ import { Logger } from "@/utils/Logger";
5
+ import { WebSocketClosedReason, WebSocketState } from "../types";
6
+ import { classifyCloseEvent } from "../useMarimoKernelConnection";
7
+ import { MAX_RETRIES } from "../useWebSocket";
8
+
9
+ function classify(
10
+ reason: string | undefined,
11
+ retryCount = 0,
12
+ maxRetries = MAX_RETRIES,
13
+ ) {
14
+ return classifyCloseEvent({ reason }, { retryCount, maxRetries });
15
+ }
16
+
17
+ describe("classifyCloseEvent", () => {
18
+ describe("transient closes (default branch)", () => {
19
+ it("retries when retryCount < maxRetries", () => {
20
+ const decision = classify(undefined, 0);
21
+ expect(decision.kind).toBe("retry");
22
+ expect(decision.status).toEqual({ state: WebSocketState.CONNECTING });
23
+ });
24
+
25
+ it("retries on each intermediate close event during a retry storm", () => {
26
+ for (let n = 0; n < MAX_RETRIES; n++) {
27
+ const decision = classify(undefined, n);
28
+ expect(decision.kind).toBe("retry");
29
+ expect(decision.status).toEqual({ state: WebSocketState.CONNECTING });
30
+ }
31
+ });
32
+
33
+ it("transitions to CLOSED when retryCount reaches maxRetries", () => {
34
+ const decision = classify(undefined, MAX_RETRIES);
35
+ expect(decision.kind).toBe("gave-up");
36
+ expect(decision.status).toEqual({
37
+ state: WebSocketState.CLOSED,
38
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
39
+ reason: "kernel not found",
40
+ });
41
+ });
42
+
43
+ it("transitions to CLOSED when retryCount exceeds maxRetries", () => {
44
+ const decision = classify(undefined, MAX_RETRIES + 5);
45
+ expect(decision.kind).toBe("gave-up");
46
+ });
47
+
48
+ it("treats unknown reason strings as transient and logs a warning", () => {
49
+ const logger = vi.spyOn(Logger, "warn").mockImplementation(() => {});
50
+ const decision = classify("something-else", 3);
51
+ expect(decision.kind).toBe("retry");
52
+ expect(logger).toHaveBeenCalled();
53
+ logger.mockRestore();
54
+ });
55
+ });
56
+
57
+ afterEach(() => {
58
+ vi.restoreAllMocks();
59
+ });
60
+
61
+ describe("terminal closes (server-initiated)", () => {
62
+ it("MARIMO_ALREADY_CONNECTED → terminal + closeTransport, with takeover", () => {
63
+ const decision = classify("MARIMO_ALREADY_CONNECTED", 0);
64
+ expect(decision.kind).toBe("terminal");
65
+ expect(decision.status).toMatchObject({
66
+ state: WebSocketState.CLOSED,
67
+ code: WebSocketClosedReason.ALREADY_RUNNING,
68
+ canTakeover: true,
69
+ });
70
+ if (decision.kind === "terminal") {
71
+ expect(decision.closeTransport).toBe(true);
72
+ }
73
+ });
74
+
75
+ it.each([
76
+ "MARIMO_WRONG_KERNEL_ID",
77
+ "MARIMO_NO_FILE_KEY",
78
+ "MARIMO_NO_SESSION_ID",
79
+ "MARIMO_NO_SESSION",
80
+ "MARIMO_SHUTDOWN",
81
+ ])("%s → terminal with KERNEL_DISCONNECTED, closes transport", (reason) => {
82
+ const decision = classify(reason, 0);
83
+ expect(decision.kind).toBe("terminal");
84
+ expect(decision.status).toMatchObject({
85
+ state: WebSocketState.CLOSED,
86
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
87
+ });
88
+ if (decision.kind === "terminal") {
89
+ expect(decision.closeTransport).toBe(true);
90
+ }
91
+ });
92
+
93
+ it("MARIMO_MALFORMED_QUERY → terminal but does NOT close transport", () => {
94
+ const decision = classify("MARIMO_MALFORMED_QUERY", 0);
95
+ expect(decision.kind).toBe("terminal");
96
+ expect(decision.status).toMatchObject({
97
+ state: WebSocketState.CLOSED,
98
+ code: WebSocketClosedReason.MALFORMED_QUERY,
99
+ });
100
+ if (decision.kind === "terminal") {
101
+ expect(decision.closeTransport).toBe(false);
102
+ }
103
+ });
104
+
105
+ it("MARIMO_KERNEL_STARTUP_ERROR → terminal + closeTransport", () => {
106
+ const decision = classify("MARIMO_KERNEL_STARTUP_ERROR", 0);
107
+ expect(decision.kind).toBe("terminal");
108
+ expect(decision.status).toMatchObject({
109
+ state: WebSocketState.CLOSED,
110
+ code: WebSocketClosedReason.KERNEL_STARTUP_ERROR,
111
+ });
112
+ if (decision.kind === "terminal") {
113
+ expect(decision.closeTransport).toBe(true);
114
+ }
115
+ });
116
+
117
+ it("terminal closes ignore retryCount entirely", () => {
118
+ const decision = classify("MARIMO_SHUTDOWN", 99);
119
+ expect(decision.kind).toBe("terminal");
120
+ });
121
+ });
122
+
123
+ describe("retry budget exhaustion", () => {
124
+ it("yields retry on attempts 1..maxRetries-1 and gave-up on the final close", () => {
125
+ const states: string[] = [];
126
+ for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
127
+ states.push(classify(undefined, attempt - 1).kind);
128
+ }
129
+ states.push(classify(undefined, MAX_RETRIES).kind);
130
+
131
+ expect(states).toEqual([
132
+ ...Array.from({ length: MAX_RETRIES }, () => "retry"),
133
+ "gave-up",
134
+ ]);
135
+ });
136
+ });
137
+ });
@@ -43,6 +43,8 @@ export class BasicTransport implements IConnectionTransport {
43
43
  return WebSocket.OPEN;
44
44
  }
45
45
 
46
+ readonly retryCount = 0;
47
+
46
48
  reconnect(code?: number | undefined, reason?: string | undefined): void {
47
49
  this.close();
48
50
  this.connect();
@@ -25,6 +25,7 @@ export interface IConnectionTransport {
25
25
  callback: ConnectionTransportCallback<T>,
26
26
  ): void;
27
27
  readyState: WebSocket["readyState"];
28
+ readonly retryCount: number;
28
29
  }
29
30
 
30
31
  export class ConnectionSubscriptions {
@@ -11,7 +11,10 @@ import type {
11
11
  NotificationMessageData,
12
12
  NotificationPayload,
13
13
  } from "@/core/kernel/messages";
14
- import { useConnectionTransport } from "@/core/websocket/useWebSocket";
14
+ import {
15
+ MAX_RETRIES,
16
+ useConnectionTransport,
17
+ } from "@/core/websocket/useWebSocket";
15
18
  import { renderHTML } from "@/plugins/core/RenderHTML";
16
19
  import {
17
20
  handleWidgetMessage,
@@ -69,10 +72,96 @@ import { useStorageActions } from "../storage/state";
69
72
  import { useVariablesActions } from "../variables/state";
70
73
  import type { VariableName } from "../variables/types";
71
74
  import { isWasm } from "../wasm/utils";
72
- import { WebSocketClosedReason, WebSocketState } from "./types";
75
+ import {
76
+ type ConnectionStatus,
77
+ WebSocketClosedReason,
78
+ WebSocketState,
79
+ } from "./types";
73
80
 
74
81
  const SUPPORTS_LAZY_KERNELS = true;
75
82
 
83
+ export type CloseDecision =
84
+ | { kind: "terminal"; status: ConnectionStatus; closeTransport: boolean }
85
+ | { kind: "gave-up"; status: ConnectionStatus }
86
+ | { kind: "retry"; status: ConnectionStatus };
87
+
88
+ export function classifyCloseEvent(
89
+ event: { reason?: string },
90
+ context: { retryCount: number; maxRetries: number },
91
+ ): CloseDecision {
92
+ switch (event.reason) {
93
+ case "MARIMO_ALREADY_CONNECTED":
94
+ return {
95
+ kind: "terminal",
96
+ status: {
97
+ state: WebSocketState.CLOSED,
98
+ code: WebSocketClosedReason.ALREADY_RUNNING,
99
+ reason: "another browser tab is already connected to the kernel",
100
+ canTakeover: true,
101
+ },
102
+ closeTransport: true,
103
+ };
104
+ case "MARIMO_WRONG_KERNEL_ID":
105
+ case "MARIMO_NO_FILE_KEY":
106
+ case "MARIMO_NO_SESSION_ID":
107
+ case "MARIMO_NO_SESSION":
108
+ case "MARIMO_SHUTDOWN":
109
+ return {
110
+ kind: "terminal",
111
+ status: {
112
+ state: WebSocketState.CLOSED,
113
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
114
+ reason: "kernel not found",
115
+ },
116
+ closeTransport: true,
117
+ };
118
+ case "MARIMO_MALFORMED_QUERY":
119
+ return {
120
+ kind: "terminal",
121
+ status: {
122
+ state: WebSocketState.CLOSED,
123
+ code: WebSocketClosedReason.MALFORMED_QUERY,
124
+ reason:
125
+ "the kernel did not recognize a request; please file a bug with marimo",
126
+ },
127
+ closeTransport: false,
128
+ };
129
+ case "MARIMO_KERNEL_STARTUP_ERROR":
130
+ return {
131
+ kind: "terminal",
132
+ status: {
133
+ state: WebSocketState.CLOSED,
134
+ code: WebSocketClosedReason.KERNEL_STARTUP_ERROR,
135
+ reason: "Failed to start kernel sandbox",
136
+ },
137
+ closeTransport: true,
138
+ };
139
+ default:
140
+ // Empty/undefined reasons are normal transient closes. Anything else is
141
+ // an unknown server reason; warn so a new MARIMO_* reason doesn't fall
142
+ // silently into the retry path.
143
+ if (event.reason) {
144
+ logNever(event.reason as never);
145
+ }
146
+ }
147
+ // partysocket stops retrying silently once `maxRetries` is hit; surface
148
+ // CLOSED so callers can detect the give-up.
149
+ if (context.retryCount >= context.maxRetries) {
150
+ return {
151
+ kind: "gave-up",
152
+ status: {
153
+ state: WebSocketState.CLOSED,
154
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
155
+ reason: "kernel not found",
156
+ },
157
+ };
158
+ }
159
+ return {
160
+ kind: "retry",
161
+ status: { state: WebSocketState.CONNECTING },
162
+ };
163
+ }
164
+
76
165
  function getExistingCells(): CellData[] | undefined {
77
166
  if (!SUPPORTS_LAZY_KERNELS) {
78
167
  return undefined;
@@ -340,6 +429,30 @@ export function useMarimoKernelConnection(opts: {
340
429
  }
341
430
  };
342
431
 
432
+ // Manual reconnect. Probes /health first to fail fast when the runtime
433
+ // is unreachable, instead of waiting on partysocket's retry budget.
434
+ const reconnect = async () => {
435
+ if (
436
+ ws.readyState === WebSocket.OPEN ||
437
+ ws.readyState === WebSocket.CONNECTING
438
+ ) {
439
+ return;
440
+ }
441
+ shouldTryReconnecting.current = true;
442
+ setConnection({ state: WebSocketState.CONNECTING });
443
+ const healthy = await runtimeManager.isHealthy();
444
+ if (!healthy) {
445
+ shouldTryReconnecting.current = false;
446
+ setConnection({
447
+ state: WebSocketState.CLOSED,
448
+ code: WebSocketClosedReason.KERNEL_DISCONNECTED,
449
+ reason: "kernel not found",
450
+ });
451
+ return;
452
+ }
453
+ ws.reconnect();
454
+ };
455
+
343
456
  const ws = useConnectionTransport({
344
457
  static: isStaticNotebook(),
345
458
  /**
@@ -399,58 +512,20 @@ export function useMarimoKernelConnection(opts: {
399
512
  */
400
513
  onClose: (e) => {
401
514
  Logger.warn("WebSocket closed", e.code, e.reason);
402
- switch (e.reason) {
403
- case "MARIMO_ALREADY_CONNECTED":
404
- setConnection({
405
- state: WebSocketState.CLOSED,
406
- code: WebSocketClosedReason.ALREADY_RUNNING,
407
- reason: "another browser tab is already connected to the kernel",
408
- canTakeover: true,
409
- });
410
- ws.close(); // close to prevent reconnecting
411
- return;
412
-
413
- case "MARIMO_WRONG_KERNEL_ID":
414
- case "MARIMO_NO_FILE_KEY":
415
- case "MARIMO_NO_SESSION_ID":
416
- case "MARIMO_NO_SESSION":
417
- case "MARIMO_SHUTDOWN":
418
- setConnection({
419
- state: WebSocketState.CLOSED,
420
- code: WebSocketClosedReason.KERNEL_DISCONNECTED,
421
- reason: "kernel not found",
422
- });
423
- ws.close(); // close to prevent reconnecting
424
- return;
425
-
426
- case "MARIMO_MALFORMED_QUERY":
427
- setConnection({
428
- state: WebSocketState.CLOSED,
429
- code: WebSocketClosedReason.MALFORMED_QUERY,
430
- reason:
431
- "the kernel did not recognize a request; please file a bug with marimo",
432
- });
433
- return;
434
-
435
- default:
436
- // Check for kernel startup error (full error already received via message)
437
- if (e.reason === "MARIMO_KERNEL_STARTUP_ERROR") {
438
- setConnection({
439
- state: WebSocketState.CLOSED,
440
- code: WebSocketClosedReason.KERNEL_STARTUP_ERROR,
441
- reason: "Failed to start kernel sandbox",
442
- });
443
- ws.close(); // prevent reconnecting
444
- return;
445
- }
446
-
447
- // Session should be valid
448
- // - browser tab might have been closed or re-opened
449
- // - computer might have just woken from sleep
450
- //
451
- // so try reconnecting.
452
- setConnection({ state: WebSocketState.CONNECTING });
453
- tryReconnecting(e.code, e.reason);
515
+ const decision = classifyCloseEvent(e, {
516
+ retryCount: ws.retryCount,
517
+ maxRetries: MAX_RETRIES,
518
+ });
519
+ setConnection(decision.status);
520
+ if (decision.kind === "terminal" && decision.closeTransport) {
521
+ ws.close(); // close to prevent reconnecting
522
+ return;
523
+ }
524
+ if (decision.kind === "retry") {
525
+ // Session should be valid
526
+ // - browser tab might have been closed or re-opened
527
+ // - computer might have just woken from sleep
528
+ tryReconnecting(e.code, e.reason);
454
529
  }
455
530
  },
456
531
 
@@ -468,5 +543,5 @@ export function useMarimoKernelConnection(opts: {
468
543
  },
469
544
  });
470
545
 
471
- return { connection };
546
+ return { connection, reconnect };
472
547
  }
@@ -18,6 +18,10 @@ interface UseConnectionTransportOptions {
18
18
  onError: (event: WebSocketEventMap["error"]) => void;
19
19
  }
20
20
 
21
+ // Per-`reconnect()` retry budget for partysocket. After exhaustion, partysocket
22
+ // stops silently; treat `retryCount >= MAX_RETRIES` as the give-up signal.
23
+ export const MAX_RETRIES = 10;
24
+
21
25
  function createConnectionTransport(
22
26
  options: Pick<UseConnectionTransportOptions, "url" | "static">,
23
27
  ): IConnectionTransport {
@@ -33,8 +37,7 @@ function createConnectionTransport(
33
37
  // Cast needed: ReconnectingWebSocket types readyState as `number`
34
38
  // but IConnectionTransport expects `0 | 1 | 2 | 3`
35
39
  return new ReconnectingWebSocket(urlProvider, undefined, {
36
- // We don't want Infinity retries
37
- maxRetries: 10,
40
+ maxRetries: MAX_RETRIES,
38
41
  debug: false,
39
42
  startClosed: true,
40
43
  // long timeout -- the server can become slow when many notebooks