Skip to content

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.

fix(lsp): upgrade codemirror-languageserver to v2 (#10355)

Myles Scolnick518bdc962383
diff --git a/frontend/package.json b/frontend/package.json
index 2742b347a7..da2ce68358 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -55,7 +55,7 @@
"@lezer/markdown": "^1.6.3",
"@lezer/python": "^1.1.18",
"@marimo-team/codemirror-ai": "^0.3.7",
- "@marimo-team/codemirror-languageserver": "^1.16.12",
+ "@marimo-team/codemirror-languageserver": "^2.0.0",
"@marimo-team/codemirror-mcp": "^0.1.7",
"@marimo-team/codemirror-sql": "^0.3.0",
"@marimo-team/llm-info": "workspace:*",
@@ -63,7 +63,6 @@
"@marimo-team/react-slotz": "^0.2.0",
"@marimo-team/smart-cells": "workspace:*",
"@mui/material": "^6.5.0",
- "@open-rpc/client-js": "^1.8.1",
"@paralleldrive/cuid2": "^2.3.1",
"@radix-ui/colors": "^3.0.0",
"@radix-ui/react-icons": "~1.3.2",
diff --git a/frontend/src/components/editor/chrome/panels/__tests__/logs-panel.test.tsx b/frontend/src/components/editor/chrome/panels/__tests__/logs-panel.test.tsx
new file mode 100644
index 0000000000..5aa9cb03b6
--- /dev/null
+++ b/frontend/src/components/editor/chrome/panels/__tests__/logs-panel.test.tsx
@@ -0,0 +1,25 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { render, screen } from "@testing-library/react";
+import { describe, expect, it } from "vitest";
+import type { CellLog } from "@/core/cells/logs";
+import { LogViewer } from "../logs-panel";
+
+describe("LogViewer", () => {
+ it("labels logs that came from a language server instead of a cell", () => {
+ const logs: CellLog[] = [
+ {
+ timestamp: 0,
+ level: "stderr",
+ message: "pylsp failed to start",
+ source: { type: "lsp", name: "pylsp" },
+ },
+ ];
+
+ render(<LogViewer logs={logs} />);
+
+ expect(screen.getByText("(lsp:pylsp)")).toBeInTheDocument();
+ expect(screen.getByText("pylsp failed to start")).toBeInTheDocument();
+ expect(screen.getByText("STDERR")).toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/components/editor/chrome/panels/logs-panel.tsx b/frontend/src/components/editor/chrome/panels/logs-panel.tsx
index 3c6e514fba..33aa87f16f 100644
--- a/frontend/src/components/editor/chrome/panels/logs-panel.tsx
+++ b/frontend/src/components/editor/chrome/panels/logs-panel.tsx
@@ -4,7 +4,11 @@ import { FileTextIcon } from "lucide-react";
import React from "react";
import { ClearButton } from "@/components/buttons/clear-button";
import { useCellActions, useCellLogs } from "@/core/cells/cells";
-import { type CellLog, formatLogTimestamp } from "@/core/cells/logs";
+import {
+ type CellLog,
+ formatLogTimestamp,
+ logSourceLabel,
+} from "@/core/cells/logs";
import { cn } from "@/utils/cn";
import { CellLink } from "../../links/cell-link";
import { PanelEmptyState } from "./empty-state";
@@ -84,7 +88,13 @@ function formatLog(log: CellLog) {
</span>
<span className={cn("shrink-0", color)}>{level}</span>
<span className="shrink-0 text-(--gray-10)">
- (<CellLink cellId={log.cellId} />)
+ (
+ {log.source.type === "cell" ? (
+ <CellLink cellId={log.source.cellId} />
+ ) : (
+ logSourceLabel(log.source)
+ )}
+ )
</span>
{log.message}
</>
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.ts
index 0f7133613c..6d2816ae4f 100644
--- a/frontend/src/core/cells/__tests__/cells.test.ts
+++ b/frontend/src/core/cells/__tests__/cells.test.ts
@@ -1870,8 +1870,18 @@ describe("cell reducer", () => {
it("can clear logs", () => {
state.cellLogs = [
- { level: "stderr", message: "log1", timestamp: 0, cellId: firstCellId },
- { level: "stderr", message: "log1", timestamp: 0, cellId: firstCellId },
+ {
+ level: "stderr",
+ message: "log1",
+ timestamp: 0,
+ source: { type: "cell", cellId: firstCellId },
+ },
+ {
+ level: "stderr",
+ message: "log1",
+ timestamp: 0,
+ source: { type: "cell", cellId: firstCellId },
+ },
];
actions.clearLogs();
expect(state.cellLogs).toEqual([]);
diff --git a/frontend/src/core/cells/__tests__/logs.test.ts b/frontend/src/core/cells/__tests__/logs.test.ts
index 4e4da10d2a..0b23fa06f0 100644
--- a/frontend/src/core/cells/__tests__/logs.test.ts
+++ b/frontend/src/core/cells/__tests__/logs.test.ts
@@ -46,7 +46,7 @@ describe("getCellLogsForMessage", () => {
timestamp: 1_234_567_890,
level: "stdout",
message: "Hello, World!",
- cellId: "cell-1",
+ source: { type: "cell", cellId: "cell-1" },
});
});
@@ -74,7 +74,7 @@ describe("getCellLogsForMessage", () => {
timestamp: 1_234_567_890,
level: "stderr",
message: "Error occurred",
- cellId: "cell-2",
+ source: { type: "cell", cellId: "cell-2" },
});
});
@@ -102,7 +102,7 @@ describe("getCellLogsForMessage", () => {
timestamp: 1_234_567_890,
level: "stdout",
message: "Error: Something went wrong",
- cellId: "cell-3",
+ source: { type: "cell", cellId: "cell-3" },
});
});
@@ -130,7 +130,7 @@ describe("getCellLogsForMessage", () => {
timestamp: 1_234_567_890,
level: "stderr",
message: "Critical Error: System failure",
- cellId: "cell-4",
+ source: { type: "cell", cellId: "cell-4" },
});
});
@@ -157,7 +157,7 @@ describe("getCellLogsForMessage", () => {
expect(logs[0].level).toBe("stderr"); // marimo-error should be treated as stderr
expect(logs[0].message).toContain("Traceback (most recent call last):");
expect(logs[0].message).toContain('File "test.py", line 1');
- expect(logs[0].cellId).toBe("cell-5");
+ expect(logs[0].source).toEqual({ type: "cell", cellId: "cell-5" });
});
test("handles multiple console outputs with different MIME types", () => {
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index e32c7900b2..e2a82f5454 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -927,6 +927,12 @@ const {
cellLogs: [...nextState.cellLogs, ...getCellLogsForMessage(message)],
};
},
+ addLogs: (state, action: { logs: CellLog[] }) => {
+ return {
+ ...state,
+ cellLogs: [...state.cellLogs, ...action.logs],
+ };
+ },
setCellIds: (state, action: { cellIds: CellId[] }) => {
const isTheSame = isEqual(state.cellIds.inOrderIds, action.cellIds);
if (isTheSame) {
@@ -1698,6 +1704,13 @@ export {
notebookAtom,
};
+/** Actions for dispatching outside of React. */
+const imperativeActions = createActions((action) => {
+ store.set(notebookAtom, (state) => reducer(state, action));
+});
+
+export const addLogs = imperativeActions.addLogs;
+
/// ATOMS
export const cellIdsAtom = atom((get) => get(notebookAtom).cellIds);
diff --git a/frontend/src/core/cells/document-changes.ts b/frontend/src/core/cells/document-changes.ts
index 5d22437aae..8c39127d88 100644
--- a/frontend/src/core/cells/document-changes.ts
+++ b/frontend/src/core/cells/document-changes.ts
@@ -369,6 +369,7 @@ export function toDocumentChanges(
case "clearCellConsoleOutput":
case "clearAllCellOutputs":
case "clearLogs":
+ case "addLogs":
return [];
// Editor UI state — no document changes.
diff --git a/frontend/src/core/cells/logs.ts b/frontend/src/core/cells/logs.ts
index 5561014713..f726461e4d 100644
--- a/frontend/src/core/cells/logs.ts
+++ b/frontend/src/core/cells/logs.ts
@@ -13,11 +13,21 @@ import { tracebackModalAtom } from "../errors/traceback-atom";
import React from "react";
import { ToastAction } from "@/components/ui/toast";
+/** Logs come from a cell, or from a language server, which has no cell. */
+export type LogSource =
+ | { type: "cell"; cellId: CellId }
+ | { type: "lsp"; name: string };
+
export interface CellLog {
+ /** Unix timestamp, in seconds. */
timestamp: number;
level: "stdout" | "stderr";
message: string;
- cellId: CellId;
+ source: LogSource;
+}
+
+export function logSourceLabel(source: LogSource): string {
+ return source.type === "cell" ? source.cellId : `lsp:${source.name}`;
}
let didAlreadyToastError = false;
@@ -48,7 +58,7 @@ export function getCellLogsForMessage(cell: CellMessage): CellLog[] {
timestamp: output.timestamp || Date.now(),
level: isError ? "stderr" : "stdout",
message: message,
- cellId: cell.cell_id,
+ source: { type: "cell", cellId: cell.cell_id },
});
break;
}
@@ -71,7 +81,7 @@ export function getCellLogsForMessage(cell: CellMessage): CellLog[] {
cell.output.data.forEach((error) => {
CellLogLogger.log({
level: "stderr",
- cellId: cell.cell_id,
+ source: { type: "cell", cellId: cell.cell_id },
timestamp: cell.timestamp ?? 0,
message: JSON.stringify(error),
});
@@ -150,7 +160,7 @@ const CellLogLogger = {
`%c[${status}]`,
`color:${color}; padding:2px 0; border-radius:2px; font-weight:bold`,
`[${formatLogTimestamp(payload.timestamp)}]`,
- `(${payload.cellId}) ${payload.message}`,
+ `(${logSourceLabel(payload.source)}) ${payload.message}`,
);
},
};
diff --git a/frontend/src/core/codemirror/copilot/__tests__/language-server.test.ts b/frontend/src/core/codemirror/copilot/__tests__/language-server.test.ts
index 24e8d0b857..0ef862832f 100644
--- a/frontend/src/core/codemirror/copilot/__tests__/language-server.test.ts
+++ b/frontend/src/core/codemirror/copilot/__tests__/language-server.test.ts
@@ -1,28 +1,81 @@
/* Copyright 2026 Marimo. All rights reserved. */
-/* oxlint-disable typescript/no-explicit-any */
-
-import { Transport } from "@open-rpc/client-js/build/transports/Transport";
+import type {
+ JSONRPCMessage,
+ JSONRPCNotification,
+ JSONRPCRequest,
+ Transport,
+} from "@marimo-team/codemirror-languageserver";
import { beforeEach, describe, expect, it, vi } from "vitest";
+import { store } from "@/core/state/jotai";
import { CopilotLanguageServerClient } from "../language-server";
+import { copilotStatusState } from "../state";
+
+const copilotState = vi.hoisted(() => ({ isEnabled: true }));
+
+vi.mock("../state", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("../state")>();
+ return {
+ ...actual,
+ isCopilotEnabled: () => copilotState.isEnabled,
+ };
+});
+
+class MockTransport implements Transport {
+ private readonly handlers = new Set<(message: JSONRPCMessage) => void>();
+ readonly sent: JSONRPCMessage[] = [];
+
+ connect = vi.fn().mockResolvedValue(undefined);
+ close = vi.fn();
+ send = vi.fn((message: JSONRPCMessage) => {
+ this.sent.push(message);
+ if (isRequest(message) && message.method === "initialize") {
+ queueMicrotask(() => {
+ this.emit({
+ jsonrpc: "2.0",
+ id: message.id,
+ result: { capabilities: {} },
+ });
+ });
+ }
+ });
+
+ onMessage(handler: (message: JSONRPCMessage) => void): () => void {
+ this.handlers.add(handler);
+ return () => {
+ this.handlers.delete(handler);
+ };
+ }
+
+ emit(message: JSONRPCMessage) {
+ for (const handler of this.handlers) {
+ handler(message);
+ }
+ }
+}
-// Mock transport
-class MockTransport extends Transport {
- override connect = vi.fn().mockResolvedValue(undefined);
- override close = vi.fn();
- override sendData = vi.fn().mockResolvedValue({});
- override subscribe = vi.fn();
- override unsubscribe = vi.fn();
- override parseData = vi.fn();
+function isRequest(message: JSONRPCMessage): message is JSONRPCRequest {
+ return "method" in message && "id" in message;
+}
+
+function notifications(
+ transport: MockTransport,
+ method: string,
+): JSONRPCNotification[] {
+ return transport.sent.filter(
+ (message): message is JSONRPCNotification =>
+ "method" in message && !("id" in message) && message.method === method,
+ );
}
describe("CopilotLanguageServerClient", () => {
let mockTransport: MockTransport;
beforeEach(() => {
+ copilotState.isEnabled = true;
mockTransport = new MockTransport();
});
- it("should initialize without copilot settings", () => {
+ it("initializes without copilot settings", () => {
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
@@ -32,178 +85,421 @@ describe("CopilotLanguageServerClient", () => {
expect(client).toBeDefined();
});
- it("should initialize with empty copilot settings", () => {
+ it("initializes with copilot settings", () => {
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
- copilotSettings: {},
+ copilotSettings: {
+ http: {
+ proxy: "http://proxy.example.com:8888",
+ proxyStrictSSL: true,
+ },
+ telemetry: {
+ telemetryLevel: "off",
+ },
+ },
});
expect(client).toBeDefined();
});
- it("should initialize with copilot settings", () => {
+ it("sends configuration after initialization", async () => {
const copilotSettings = {
http: {
proxy: "http://proxy.example.com:8888",
proxyStrictSSL: true,
},
- telemetry: {
- telemetryLevel: "off",
- },
};
-
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
copilotSettings,
});
-
- expect(client).toBeDefined();
+ await client.initializePromise;
+ await vi.waitFor(() => {
+ expect(
+ notifications(mockTransport, "workspace/didChangeConfiguration"),
+ ).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "workspace/didChangeConfiguration",
+ params: { settings: copilotSettings },
+ },
+ ]);
+ });
});
- it("should send configuration after initialization when settings are provided", async () => {
+ it.each([
+ ["empty", {}],
+ ["omitted", undefined],
+ ])(
+ "does not send configuration when settings are %s",
+ async (_, copilotSettings) => {
+ const client = new CopilotLanguageServerClient({
+ rootUri: "file:///test",
+ workspaceFolders: null,
+ transport: mockTransport,
+ copilotSettings,
+ });
+ await client.initializePromise;
+ await new Promise((resolve) => setTimeout(resolve, 0));
+
+ expect(
+ notifications(mockTransport, "workspace/didChangeConfiguration"),
+ ).toEqual([]);
+ },
+ );
+
+ it("accepts enterprise, proxy, and telemetry settings together", async () => {
const copilotSettings = {
http: {
proxy: "http://proxy.example.com:8888",
proxyStrictSSL: true,
+ proxyKerberosServicePrincipal: "HTTP/proxy.example.com",
+ },
+ telemetry: {
+ telemetryLevel: "all",
+ },
+ "github-enterprise": {
+ uri: "https://github.enterprise.com",
},
};
-
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
copilotSettings,
});
-
- // Spy on the notify method
- const notifySpy = vi.spyOn(client as any, "notify");
-
- // Wait for initialization
await client.initializePromise;
-
- // Wait for next tick to let the configuration be sent
- await new Promise((resolve) => setTimeout(resolve, 0));
-
- // Verify that notify was called with the correct parameters
- expect(notifySpy).toHaveBeenCalledWith("workspace/didChangeConfiguration", {
- settings: copilotSettings,
+ await vi.waitFor(() => {
+ expect(
+ notifications(mockTransport, "workspace/didChangeConfiguration"),
+ ).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "workspace/didChangeConfiguration",
+ params: { settings: copilotSettings },
+ },
+ ]);
});
});
- it("should not send configuration after initialization when settings are empty", async () => {
+ it("reopens the latest Copilot document after re-initialization", async () => {
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
- copilotSettings: {},
});
+ await client.initializePromise;
+ await client.textDocumentDidOpen({
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "python",
+ version: 0,
+ text: "initial",
+ },
+ });
+ await client.textDocumentDidChange({
+ textDocument: { uri: "file:///test.py", version: 1 },
+ contentChanges: [{ text: "updated" }],
+ });
+ mockTransport.sent.length = 0;
+
+ await client.reInitialize();
+
+ expect(notifications(mockTransport, "textDocument/didOpen")).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "textDocument/didOpen",
+ params: {
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "python",
+ version: 1,
+ text: "updated",
+ },
+ },
+ },
+ ]);
+ });
- // Spy on the notify method
- const notifySpy = vi.spyOn(client as any, "notify");
+ it("reopens the document with the languageId it was opened with", async () => {
+ const client = new CopilotLanguageServerClient({
+ rootUri: "file:///test",
+ workspaceFolders: null,
+ transport: mockTransport,
+ });
+ await client.initializePromise;
+ // The Copilot plugin opens as "copilot", not "python"
+ await client.textDocumentDidOpen({
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "copilot",
+ version: 0,
+ text: "initial",
+ },
+ });
+ await client.textDocumentDidChange({
+ textDocument: { uri: "file:///test.py", version: 1 },
+ contentChanges: [{ text: "updated" }],
+ });
+ mockTransport.sent.length = 0;
+
+ await client.reInitialize();
+
+ expect(notifications(mockTransport, "textDocument/didOpen")).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "textDocument/didOpen",
+ params: {
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "copilot",
+ version: 1,
+ text: "updated",
+ },
+ },
+ },
+ ]);
+ });
- // Wait for initialization
+ it("ignores an empty didChange without corrupting document state", async () => {
+ const client = new CopilotLanguageServerClient({
+ rootUri: "file:///test",
+ workspaceFolders: null,
+ transport: mockTransport,
+ });
await client.initializePromise;
+ mockTransport.sent.length = 0;
- // Wait for next tick
- await new Promise((resolve) => setTimeout(resolve, 0));
+ await expect(
+ client.textDocumentDidChange({
+ textDocument: { uri: "file:///test.py", version: 1 },
+ contentChanges: [],
+ }),
+ ).resolves.toBeUndefined();
- // Verify that notify was NOT called with didChangeConfiguration
- expect(notifySpy).not.toHaveBeenCalledWith(
- "workspace/didChangeConfiguration",
- expect.anything(),
- );
+ expect(notifications(mockTransport, "textDocument/didOpen")).toEqual([]);
+ expect(notifications(mockTransport, "textDocument/didChange")).toEqual([]);
});
- it("should not send configuration after initialization when settings are not provided", async () => {
+ it("balances document references when Copilot is disabled before close", async () => {
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
});
-
- // Spy on the notify method
- const notifySpy = vi.spyOn(client as any, "notify");
-
- // Wait for initialization
await client.initializePromise;
+ await client.textDocumentDidOpen({
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "python",
+ version: 7,
+ text: "value = 1",
+ },
+ });
- // Wait for next tick
- await new Promise((resolve) => setTimeout(resolve, 0));
+ copilotState.isEnabled = false;
+ await client.textDocumentDidClose({
+ textDocument: { uri: "file:///test.py" },
+ });
+ copilotState.isEnabled = true;
+ mockTransport.sent.length = 0;
+ await client.reInitialize();
- // Verify that notify was NOT called with didChangeConfiguration
- expect(notifySpy).not.toHaveBeenCalledWith(
- "workspace/didChangeConfiguration",
- expect.anything(),
- );
+ expect(notifications(mockTransport, "textDocument/didOpen")).toEqual([]);
});
- it("should accept github-enterprise settings", async () => {
- const copilotSettings = {
- "github-enterprise": {
- uri: "https://github.enterprise.com",
- },
- };
-
+ it("does not close the shared document until the last mounted editor closes it (relies on base client's own refcounting)", async () => {
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
- copilotSettings,
});
-
- // Spy on the notify method
- const notifySpy = vi.spyOn(client as any, "notify");
-
- // Wait for initialization
await client.initializePromise;
- // Wait for next tick to let the configuration be sent
- await new Promise((resolve) => setTimeout(resolve, 0));
+ // Two editors (e.g. two visible cells) share this client and both open
+ // the same document.
+ await client.textDocumentDidOpen({
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ });
+ await client.textDocumentDidOpen({
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ });
+ mockTransport.sent.length = 0;
- // Verify that notify was called with the enterprise settings
- expect(notifySpy).toHaveBeenCalledWith("workspace/didChangeConfiguration", {
- settings: copilotSettings,
+ // Closing the first editor must not close the shared document while the
+ // second editor is still mounted.
+ await client.textDocumentDidClose({
+ textDocument: { uri: "file:///test.py" },
});
- });
+ expect(notifications(mockTransport, "textDocument/didClose")).toEqual([]);
- it("should accept all supported settings at once", async () => {
- const copilotSettings = {
- http: {
- proxy: "http://proxy.example.com:8888",
- proxyStrictSSL: true,
- proxyKerberosServicePrincipal: "HTTP/proxy.example.com",
+ // Closing the last editor closes the document for real.
+ await client.textDocumentDidClose({
+ textDocument: { uri: "file:///test.py" },
+ });
+ expect(notifications(mockTransport, "textDocument/didClose")).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "textDocument/didClose",
+ params: { textDocument: { uri: "file:///test.py" } },
},
- telemetry: {
- telemetryLevel: "all",
+ ]);
+ });
+
+ it("keeps document versions monotonic after opening a versioned document", async () => {
+ const client = new CopilotLanguageServerClient({
+ rootUri: "file:///test",
+ workspaceFolders: null,
+ transport: mockTransport,
+ });
+ await client.initializePromise;
+ await client.textDocumentDidOpen({
+ textDocument: {
+ uri: "file:///test.py",
+ languageId: "python",
+ version: 7,
+ text: "value = 1",
},
- "github-enterprise": {
- uri: "https://github.enterprise.com",
+ });
+ mockTransport.sent.length = 0;
+
+ await client.textDocumentDidChange({
+ textDocument: { uri: "file:///test.py", version: 8 },
+ contentChanges: [{ text: "value = 2" }],
+ });
+
+ expect(notifications(mockTransport, "textDocument/didChange")).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "textDocument/didChange",
+ params: {
+ textDocument: { uri: "file:///test.py", version: 8 },
+ contentChanges: [{ text: "value = 2" }],
+ },
},
- };
+ ]);
+ });
+ it("ignores malformed custom notifications", async () => {
const client = new CopilotLanguageServerClient({
rootUri: "file:///test",
workspaceFolders: null,
transport: mockTransport,
- copilotSettings,
});
+ await client.initializePromise;
- // Spy on the notify method
- const notifySpy = vi.spyOn(client as any, "notify");
+ expect(() => {
+ mockTransport.emit({
+ jsonrpc: "2.0",
+ method: "statusNotification",
+ params: { busy: "not-a-boolean" },
+ });
+ mockTransport.emit({
+ jsonrpc: "2.0",
+ method: "window/logMessage",
+ params: null,
+ });
+ }).not.toThrow();
+ });
- // Wait for initialization
- await client.initializePromise;
+ describe("status notifications", () => {
+ beforeEach(() => {
+ store.set(copilotStatusState, {
+ busy: false,
+ kind: null,
+ message: null,
+ });
+ });
- // Wait for next tick to let the configuration be sent
- await new Promise((resolve) => setTimeout(resolve, 0));
+ it.each([
+ [
+ "omitted message and unknown kind",
+ { kind: "Inactive", busy: false },
+ { busy: false, kind: "Inactive", message: null, status: undefined },
+ ],
+ [
+ "omitted busy",
+ { kind: "Error", message: "Not signed in" },
+ {
+ busy: false,
+ kind: "Error",
+ message: "Not signed in",
+ status: undefined,
+ },
+ ],
+ [
+ "a fully populated payload",
+ {
+ busy: true,
+ kind: "Normal",
+ message: "Working",
+ status: "InProgress",
+ },
+ {
+ busy: true,
+ kind: "Normal",
+ message: "Working",
+ status: "InProgress",
+ },
+ ],
+ [
+ "unusable field types",
+ { busy: 1, kind: 2, message: 3, status: 4 },
+ { busy: false, kind: null, message: null, status: undefined },
+ ],
+ ])(
+ "normalizes rather than discards a status update with %s",
+ async (_label, params, expected) => {
+ const client = new CopilotLanguageServerClient({
+ rootUri: "file:///test",
+ workspaceFolders: null,
+ transport: mockTransport,
+ });
+ await client.initializePromise;
+
+ mockTransport.emit({
+ jsonrpc: "2.0",
+ method: "didChangeStatus",
+ params,
+ });
+
+ expect(store.get(copilotStatusState)).toEqual(expected);
+ },
+ );
- // Verify that notify was called with all settings
- expect(notifySpy).toHaveBeenCalledWith("workspace/didChangeConfiguration", {
- settings: copilotSettings,
+ it("ignores a status update with non-object params", async () => {
+ const client = new CopilotLanguageServerClient({
+ rootUri: "file:///test",
+ workspaceFolders: null,
+ transport: mockTransport,
+ });
+ await client.initializePromise;
+
+ mockTransport.emit({
+ jsonrpc: "2.0",
+ method: "statusNotification",
+ params: "not-an-object",
+ });
+
+ expect(store.get(copilotStatusState)).toEqual({
+ busy: false,
+ kind: null,
+ message: null,
+ });
});
});
});
diff --git a/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts b/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts
index 6a0a33138b..4077b028be 100644
--- a/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts
+++ b/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts
@@ -1,565 +1,153 @@
/* Copyright 2026 Marimo. All rights reserved. */
-/* oxlint-disable typescript/no-explicit-any */
-
-import { WebSocketTransport } from "@open-rpc/client-js";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Mocks } from "@/__mocks__/common";
import { LazyWebsocketTransport } from "../transport";
-// Mock the Logger
vi.mock("@/utils/Logger", () => ({
Logger: Mocks.logger(),
}));
-// Mock the WebSocketTransport
-vi.mock("@open-rpc/client-js", () => {
- const mockWebSocketTransport = vi.fn();
- mockWebSocketTransport.prototype.connect = vi.fn();
- mockWebSocketTransport.prototype.close = vi.fn();
- mockWebSocketTransport.prototype.sendData = vi.fn();
- mockWebSocketTransport.prototype.subscribe = vi.fn();
- mockWebSocketTransport.prototype.unsubscribe = vi.fn();
-
- return {
- WebSocketTransport: mockWebSocketTransport,
- };
-});
+class FakeWebSocket {
+ static readonly OPEN = 1;
+ static readonly CLOSED = 3;
+ static readonly instances: FakeWebSocket[] = [];
+
+ readonly sent: string[] = [];
+ readyState = 0;
+ readonly url: string;
+ private readonly listeners = new Map<
+ string,
+ Set<(event: Record<string, never>) => void>
+ >();
+
+ constructor(url: string) {
+ this.url = url;
+ FakeWebSocket.instances.push(this);
+ }
+
+ addEventListener(
+ type: string,
+ listener: (event: Record<string, never>) => void,
+ ) {
+ const listeners = this.listeners.get(type) ?? new Set();
+ listeners.add(listener);
+ this.listeners.set(type, listeners);
+ }
+
+ send(frame: string) {
+ this.sent.push(frame);
+ }
+
+ close() {
+ this.readyState = FakeWebSocket.CLOSED;
+ this.emit("close");
+ }
+
+ open() {
+ this.readyState = FakeWebSocket.OPEN;
+ this.emit("open");
+ }
+
+ fail() {
+ this.emit("error");
+ }
+
+ private emit(type: string) {
+ for (const listener of this.listeners.get(type) ?? []) {
+ listener({});
+ }
+ }
+}
+
+async function getSocket(index: number): Promise<FakeWebSocket> {
+ await vi.waitFor(() => {
+ expect(FakeWebSocket.instances.length).toBeGreaterThan(index);
+ });
+ return FakeWebSocket.instances[index];
+}
describe("LazyWebsocketTransport", () => {
const mockWsUrl = "ws://localhost:8080/copilot";
- let mockGetWsUrl: ReturnType<typeof vi.fn>;
- let mockWaitForReady: ReturnType<typeof vi.fn>;
- let mockShowError: ReturnType<typeof vi.fn>;
beforeEach(() => {
- vi.clearAllMocks();
-
- mockGetWsUrl = vi.fn(() => mockWsUrl);
- mockWaitForReady = vi.fn().mockResolvedValue(undefined);
- mockShowError = vi.fn();
-
- // Mock the WebSocketTransport constructor
- (WebSocketTransport as any).mockImplementation(function (this: any) {
- this.connect = vi.fn().mockResolvedValue(undefined);
- this.close = vi.fn();
- this.sendData = vi.fn().mockResolvedValue({ result: "success" });
- this.subscribe = vi.fn();
- this.unsubscribe = vi.fn();
- });
+ FakeWebSocket.instances.length = 0;
+ vi.stubGlobal("WebSocket", FakeWebSocket);
});
- describe("constructor", () => {
- it("should create a transport with default options", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- expect(transport).toBeDefined();
- expect(mockGetWsUrl).not.toHaveBeenCalled();
- });
-
- it("should accept custom retry options", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- retries: 5,
- retryDelayMs: 2000,
- maxTimeoutMs: 10_000,
- });
-
- expect(transport).toBeDefined();
- });
+ afterEach(() => {
+ vi.unstubAllGlobals();
});
- describe("connect", () => {
- it("should wait for prerequisites before connecting", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await transport.connect();
-
- expect(mockWaitForReady).toHaveBeenCalledTimes(1);
- expect(mockGetWsUrl).toHaveBeenCalledTimes(1);
- expect(WebSocketTransport).toHaveBeenCalledWith(mockWsUrl);
- });
-
- it("should create delegate and connect", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await transport.connect();
-
- const delegate = (transport as any).delegate;
- expect(delegate).toBeDefined();
- expect(delegate.connect).toHaveBeenCalledTimes(1);
- });
-
- it("should retry on connection failure", async () => {
- let attemptCount = 0;
- (WebSocketTransport as any).mockImplementation(function (this: any) {
- this.connect = vi.fn().mockImplementation(() => {
- attemptCount++;
- if (attemptCount < 2) {
- return Promise.reject(new Error("Connection failed"));
- }
- return Promise.resolve(undefined);
- });
- this.close = vi.fn();
- this.sendData = vi.fn();
- this.subscribe = vi.fn();
- this.unsubscribe = vi.fn();
- });
-
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- retries: 3,
- retryDelayMs: 10,
- });
-
- await transport.connect();
-
- expect(attemptCount).toBe(2);
- });
-
- it("should show error toast on final retry failure", async () => {
- const connectionError = new Error("Connection failed");
- (WebSocketTransport as any).mockImplementation(function (this: any) {
- this.connect = vi.fn().mockRejectedValue(connectionError);
- this.close = vi.fn();
- this.sendData = vi.fn();
- this.subscribe = vi.fn();
- this.unsubscribe = vi.fn();
- });
-
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- retries: 2,
- retryDelayMs: 10,
- });
-
- await expect(transport.connect()).rejects.toThrow("Connection failed");
-
- expect(mockShowError).toHaveBeenCalledTimes(1);
- expect(mockShowError).toHaveBeenCalledWith(
- "GitHub Copilot Connection Error",
- "Failed to connect to GitHub Copilot. Please check your settings and try again.\n\nConnection failed",
- );
- expect((transport as any).delegate).toBeUndefined();
- });
-
- it("should handle waitForReady failure", async () => {
- const waitError = new Error("Prerequisites not ready");
- mockWaitForReady.mockRejectedValue(waitError);
-
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await expect(transport.connect()).rejects.toThrow(
- "Prerequisites not ready",
- );
- expect(WebSocketTransport).not.toHaveBeenCalled();
- });
+ it("waits for Copilot prerequisites before connecting", async () => {
+ let ready: (() => void) | undefined;
+ const waitForReady = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ ready = resolve;
+ }),
+ );
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: () => mockWsUrl,
+ waitForReady,
+ showError: vi.fn(),
+ retryDelayMs: 0,
+ });
+
+ const connection = transport.connect();
+ expect(waitForReady).toHaveBeenCalledTimes(1);
+ expect(FakeWebSocket.instances).toHaveLength(0);
+
+ ready?.();
+ const socket = await getSocket(0);
+ socket.open();
+ await connection;
+
+ expect(socket.url).toBe(mockWsUrl);
});
- describe("subscribe", () => {
- it("should register handler on parent and track subscription", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
-
- expect((transport as any).pendingSubscriptions).toHaveLength(1);
- expect((transport as any).pendingSubscriptions[0]).toEqual({
- event: "notification",
- handler,
- });
- });
-
- it("should register handler on delegate if it exists", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await transport.connect();
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
-
- const delegate = (transport as any).delegate;
- expect(delegate.subscribe).toHaveBeenCalledWith("notification", handler);
- });
-
- it("should register pending subscriptions when delegate is created", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const handler1 = vi.fn();
- const handler2 = vi.fn();
- transport.subscribe("notification", handler1);
- transport.subscribe("response", handler2);
-
- await transport.connect();
-
- const delegate = (transport as any).delegate;
- expect(delegate.subscribe).toHaveBeenCalledWith("notification", handler1);
- expect(delegate.subscribe).toHaveBeenCalledWith("response", handler2);
- });
+ it("shows a user-facing error after the final retry", async () => {
+ const showError = vi.fn();
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: () => mockWsUrl,
+ waitForReady: vi.fn().mockResolvedValue(undefined),
+ showError,
+ retries: 2,
+ retryDelayMs: 0,
+ });
+
+ const connection = transport.connect();
+ (await getSocket(0)).fail();
+ (await getSocket(1)).fail();
+
+ await expect(connection).rejects.toThrow(
+ "WebSocket connection to ws://localhost:8080/copilot failed",
+ );
+ expect(showError).toHaveBeenCalledWith(
+ "GitHub Copilot Connection Error",
+ "Failed to connect to GitHub Copilot. Please check your settings and try again.\n\nWebSocket connection to ws://localhost:8080/copilot failed",
+ );
});
- describe("unsubscribe", () => {
- it("should remove specific handler for specific event", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const handler1 = vi.fn();
- const handler2 = vi.fn();
- transport.subscribe("notification", handler1);
- transport.subscribe("notification", handler2);
-
- expect((transport as any).pendingSubscriptions).toHaveLength(2);
-
- transport.unsubscribe("notification", handler1);
-
- expect((transport as any).pendingSubscriptions).toHaveLength(1);
- expect((transport as any).pendingSubscriptions[0].handler).toBe(handler2);
- });
-
- it("should remove handler from all events when event not specified", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
- transport.subscribe("response", handler);
-
- expect((transport as any).pendingSubscriptions).toHaveLength(2);
-
- transport.unsubscribe(undefined, handler);
-
- expect((transport as any).pendingSubscriptions).toHaveLength(0);
- });
-
- it("should remove all handlers for event when handler not specified", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const handler1 = vi.fn();
- const handler2 = vi.fn();
- transport.subscribe("notification", handler1);
- transport.subscribe("notification", handler2);
- transport.subscribe("response", handler1);
-
- expect((transport as any).pendingSubscriptions).toHaveLength(3);
-
- transport.unsubscribe("notification");
-
- expect((transport as any).pendingSubscriptions).toHaveLength(1);
- expect((transport as any).pendingSubscriptions[0].event).toBe("response");
- });
-
- it("should remove all subscriptions when neither event nor handler specified", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- transport.subscribe("notification", vi.fn());
- transport.subscribe("response", vi.fn());
-
- expect((transport as any).pendingSubscriptions).toHaveLength(2);
-
- transport.unsubscribe();
-
- expect((transport as any).pendingSubscriptions).toHaveLength(0);
- });
-
- it("should unsubscribe from delegate if it exists", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await transport.connect();
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
- transport.unsubscribe("notification", handler);
-
- const delegate = (transport as any).delegate;
- expect(delegate.unsubscribe).toHaveBeenCalledWith(
- "notification",
- handler,
- );
- });
- });
-
- describe("sendData", () => {
- it("should send data through delegate when connected", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await transport.connect();
-
- const data: any = { method: "test", params: [] };
- const result = await transport.sendData(data, 5000);
-
- const delegate = (transport as any).delegate;
- expect(delegate.sendData).toHaveBeenCalledWith(data, 5000);
- expect(result).toEqual({ result: "success" });
- });
-
- it("should reconnect if delegate is undefined", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
-
- // sendData calls tryConnect directly, not connect, so wait functions aren't called
- // But it should still create and connect the delegate
- expect(WebSocketTransport).toHaveBeenCalled();
- const delegate = (transport as any).delegate;
- expect(delegate).toBeDefined();
- expect(delegate.connect).toHaveBeenCalled();
- });
-
- it("should use maxTimeoutMs as default when no timeout is provided", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- maxTimeoutMs: 5000,
- });
-
- await transport.connect();
-
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, undefined);
-
- const delegate = (transport as any).delegate;
- expect(delegate.sendData).toHaveBeenCalledWith(data, 5000);
- });
-
- it("should respect caller-provided timeout without clamping", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- maxTimeoutMs: 5000,
- });
-
- await transport.connect();
-
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 30_000);
-
- const delegate = (transport as any).delegate;
- expect(delegate.sendData).toHaveBeenCalledWith(data, 30_000);
- });
-
- it("should throw error if reconnection fails", async () => {
- const connectionError = new Error("Connection failed");
- (WebSocketTransport as any).mockImplementation(function (this: any) {
- this.connect = vi.fn().mockRejectedValue(connectionError);
- this.close = vi.fn();
- this.sendData = vi.fn();
- this.subscribe = vi.fn();
- this.unsubscribe = vi.fn();
- });
-
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- retries: 1,
- retryDelayMs: 10,
- });
-
- const data: any = { method: "test", params: [] };
- await expect(transport.sendData(data, 5000)).rejects.toThrow(
- "Unable to connect to GitHub Copilot",
- );
- });
- });
-
- describe("onReconnect", () => {
- it("should call onReconnect after close + sendData reconnection", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const onReconnect = vi.fn().mockResolvedValue(undefined);
- transport.onReconnect = onReconnect;
-
- // Initial connect
- await transport.connect();
- expect(onReconnect).not.toHaveBeenCalled();
-
- // Close the transport (simulates failure handling)
- transport.close();
-
- // sendData should reconnect and call onReconnect
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
-
+ it("inherits reconnect callbacks for Copilot re-initialization", async () => {
+ const transport = new LazyWebsocketTransport({
+ getWsUrl: () => mockWsUrl,
+ waitForReady: vi.fn().mockResolvedValue(undefined),
+ showError: vi.fn(),
+ retryDelayMs: 0,
+ });
+ const onReconnect = vi.fn().mockResolvedValue(undefined);
+ transport.onReconnect = onReconnect;
+
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.close();
+
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
expect(onReconnect).toHaveBeenCalledTimes(1);
});
-
- it("should NOT call onReconnect on initial sendData connection", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const onReconnect = vi.fn().mockResolvedValue(undefined);
- transport.onReconnect = onReconnect;
-
- // sendData without prior connect — this is the initial connection
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
-
- expect(onReconnect).not.toHaveBeenCalled();
- });
-
- it("should call onReconnect after tryConnect failure + retry via sendData", async () => {
- let connectAttempt = 0;
- (WebSocketTransport as any).mockImplementation(function (this: any) {
- this.connect = vi.fn().mockImplementation(() => {
- connectAttempt++;
- // First 2 attempts fail (retries=2 exhausted), then succeed on next sendData
- if (connectAttempt <= 2) {
- return Promise.reject(new Error("Connection failed"));
- }
- return Promise.resolve(undefined);
- });
- this.close = vi.fn();
- this.sendData = vi.fn().mockResolvedValue({ result: "success" });
- this.subscribe = vi.fn();
- this.unsubscribe = vi.fn();
- });
-
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- retries: 2,
- retryDelayMs: 10,
- });
-
- const onReconnect = vi.fn().mockResolvedValue(undefined);
- transport.onReconnect = onReconnect;
-
- // Initial connect fails (all retries exhausted)
- await expect(transport.connect()).rejects.toThrow("Connection failed");
- expect(onReconnect).not.toHaveBeenCalled();
-
- // Now sendData reconnects successfully
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
-
- expect(onReconnect).toHaveBeenCalledTimes(1);
- });
-
- it("should propagate onReconnect rejection and allow retry", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- const reconnectError = new Error("Re-initialization failed");
- const onReconnect = vi.fn().mockRejectedValueOnce(reconnectError);
- transport.onReconnect = onReconnect;
-
- await transport.connect();
- transport.close();
-
- const data: any = { method: "test", params: [] };
- await expect(transport.sendData(data, 5000)).rejects.toThrow(
- "Re-initialization failed",
- );
- expect(onReconnect).toHaveBeenCalledTimes(1);
-
- // needsReInitialization should still be true, so a subsequent retry
- // will attempt onReconnect again
- onReconnect.mockResolvedValueOnce(undefined);
- await transport.sendData(data, 5000);
- expect(onReconnect).toHaveBeenCalledTimes(2);
- });
- });
-
- describe("close", () => {
- it("should close delegate and clear it", async () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- await transport.connect();
-
- const delegate = (transport as any).delegate;
- expect(delegate).toBeDefined();
-
- transport.close();
-
- expect(delegate.close).toHaveBeenCalledTimes(1);
- expect((transport as any).delegate).toBeUndefined();
- });
-
- it("should handle close when delegate is undefined", () => {
- const transport = new LazyWebsocketTransport({
- getWsUrl: mockGetWsUrl,
- waitForReady: mockWaitForReady,
- showError: mockShowError,
- });
-
- expect(() => transport.close()).not.toThrow();
- });
});
});
diff --git a/frontend/src/core/codemirror/copilot/language-server.ts b/frontend/src/core/codemirror/copilot/language-server.ts
index f4152ad13b..fe6d8e981c 100644
--- a/frontend/src/core/codemirror/copilot/language-server.ts
+++ b/frontend/src/core/codemirror/copilot/language-server.ts
@@ -7,6 +7,7 @@ import type {
CompletionList,
CompletionParams,
DidChangeTextDocumentParams,
+ DidCloseTextDocumentParams,
DidOpenTextDocumentParams,
Hover,
HoverParams,
@@ -16,7 +17,9 @@ import type {
} from "vscode-languageserver-protocol";
import { VersionedTextDocumentIdentifier } from "vscode-languageserver-protocol";
import { store } from "@/core/state/jotai";
+import { invariant } from "@/utils/invariant";
import { Logger } from "@/utils/Logger";
+import { hasFunctionProperty, isRecord } from "@/utils/records";
import { getCodes } from "./getCodes";
import {
clearGitHubCopilotLoadingVersion,
@@ -30,42 +33,88 @@ import type {
GitHubCopilotStatusNotificationParams,
GitHubCopilotStatusResult,
} from "./types";
+import type { LanguageAdapterType } from "../language/types";
const logger = Logger.get("@github/copilot-language-server");
+const REQUEST_TIMEOUT_MS = 10_000;
+// Only used for the synthetic didOpen sent when a change arrives before any open
+const DEFAULT_LANGUAGE_ID: LanguageAdapterType = "python";
+type NoParams = Record<string, never>;
// A map of request methods and their parameters and return types
export interface LSPRequestMap {
- checkStatus: [{}, GitHubCopilotStatusResult];
- signIn: [{}, GitHubCopilotSignInInitiateResult];
+ checkStatus: [NoParams, GitHubCopilotStatusResult];
+ signIn: [NoParams, GitHubCopilotSignInInitiateResult];
signInConfirm: [GitHubCopilotSignInConfirmParams, GitHubCopilotStatusResult];
- signOut: [{}, GitHubCopilotStatusResult];
+ signOut: [NoParams, GitHubCopilotStatusResult];
"textDocument/inlineCompletion": [
InlineCompletionParams,
InlineCompletionList | InlineCompletionItem[] | null,
];
}
-export interface LSPEventMap {
- statusNotification: GitHubCopilotStatusNotificationParams;
- didChangeStatus: GitHubCopilotStatusNotificationParams;
- "window/logMessage": { type: number; message: string };
+interface UntypedLanguageServerMethods {
+ request: (
+ method: string,
+ params: unknown,
+ timeout: number,
+ ) => Promise<unknown>;
+ notify: (method: string, params: unknown) => Promise<void>;
}
-export type EnhancedNotification = {
- [key in keyof LSPEventMap]: {
- jsonrpc: "2.0";
- id?: null | undefined;
- method: key;
- params: LSPEventMap[key];
+/**
+ * `LanguageServerClient#request`/`#notify` are protected and generic over the
+ * library's own `LSPRequestMap`/`LSPNotifyMap`
+ * This asserts the two inherited methods we need are present.
+ */
+function assertHasLanguageServerRpc(
+ value: unknown,
+): asserts value is UntypedLanguageServerMethods {
+ invariant(
+ isRecord(value) &&
+ hasFunctionProperty(value, "request") &&
+ hasFunctionProperty(value, "notify"),
+ "LanguageServerClient is missing request/notify",
+ );
+}
+
+/**
+ * Normalizes rather than validates: Copilot omits `message`/`busy` on most
+ * notifications and ships `kind` values we don't know about, so rejecting on an
+ * exact shape match would silently freeze the status indicator.
+ */
+function parseCopilotStatusNotificationParams(
+ value: unknown,
+): GitHubCopilotStatusNotificationParams | null {
+ if (!isRecord(value)) {
+ return null;
+ }
+ const { busy, kind, message, status } = value;
+ return {
+ busy: typeof busy === "boolean" ? busy : false,
+ kind: typeof kind === "string" ? kind : null,
+ message: typeof message === "string" ? message : null,
+ status: typeof status === "string" ? status : undefined,
};
-}[keyof LSPEventMap];
+}
+
+function isLogMessageParams(
+ value: unknown,
+): value is { type: number; message: string } {
+ return (
+ isRecord(value) &&
+ typeof value.type === "number" &&
+ typeof value.message === "string"
+ );
+}
/**
* A client for the Copilot language server.
*/
export class CopilotLanguageServerClient extends LanguageServerClient {
private documentVersion = 0;
- private hasOpenedDocument = false;
+ private openDocumentCount = 0;
+ private lastOpenedDocument: DidOpenTextDocumentParams | undefined;
private copilotSettings: Record<string, unknown> = {};
constructor(
@@ -80,10 +129,11 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
}
private attachInitializeListener() {
- // Send configuration after initialization
- this.initializePromise.then(() => {
- this.sendConfiguration();
- });
+ void this.initializePromise
+ .then(() => this.sendConfiguration())
+ .catch((error: unknown) => {
+ logger.warn("#initialize: Failed to send configuration", error);
+ });
}
/**
@@ -95,6 +145,12 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
this.initializePromise = this.initialize();
await this.initializePromise;
await this.sendConfiguration();
+ if (this.openDocumentCount > 0 && this.lastOpenedDocument) {
+ await this.sendNotification(
+ "textDocument/didOpen",
+ this.lastOpenedDocument,
+ );
+ }
}
private async sendConfiguration() {
@@ -103,7 +159,9 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
if (!settings || Object.keys(settings).length === 0) {
return;
}
- await this.notify("workspace/didChangeConfiguration", { settings });
+ await this.sendNotification("workspace/didChangeConfiguration", {
+ settings,
+ });
logger.debug("#sendConfiguration: Configuration sent", settings);
}
@@ -111,20 +169,21 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
method: Method,
params: LSPRequestMap[Method][0],
): Promise<LSPRequestMap[Method][1]> {
- return await (
- this as unknown as {
- request: (
- method: Method,
- params: LSPRequestMap[Method][0],
- ) => Promise<LSPRequestMap[Method][1]>;
- }
- ).request(method, params);
+ assertHasLanguageServerRpc(this);
+ return (await this.request(
+ method,
+ params,
+ REQUEST_TIMEOUT_MS,
+ )) as LSPRequestMap[Method][1];
}
- // oxlint-disable-next-line typescript/no-explicit-any
- override async notify(method: any, params: any): Promise<any> {
+ private async sendNotification(
+ method: string,
+ params: unknown,
+ ): Promise<void> {
logger.debug("#notify", method, params);
- return super.notify(method, params);
+ assertHasLanguageServerRpc(this);
+ return this.notify(method, params);
}
override getInitializationOptions() {
@@ -151,14 +210,29 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
override async textDocumentDidOpen(
params: DidOpenTextDocumentParams,
- ): Promise<DidOpenTextDocumentParams> {
+ ): Promise<boolean> {
if (this.isDisabled()) {
- return params;
+ return false;
}
- this.hasOpenedDocument = true;
+ this.openDocumentCount++;
+ this.lastOpenedDocument = params;
+ this.documentVersion = Math.max(
+ this.documentVersion,
+ params.textDocument.version,
+ );
return super.textDocumentDidOpen(params);
}
+ override async textDocumentDidClose(
+ params: DidCloseTextDocumentParams,
+ ): Promise<void> {
+ if (this.openDocumentCount === 0) {
+ return;
+ }
+ this.openDocumentCount--;
+ return super.textDocumentDidClose(params);
+ }
+
override async textDocumentCompletion(
params: CompletionParams,
): Promise<CompletionList | CompletionItem[]> {
@@ -168,18 +242,27 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
override async textDocumentDidChange(
params: DidChangeTextDocumentParams,
- ): Promise<DidChangeTextDocumentParams> {
+ ): Promise<void> {
if (this.isDisabled()) {
- return params;
+ return;
+ }
+
+ const change = params.contentChanges[0];
+ if (!change) {
+ logger.warn(
+ "#textDocumentDidChange: Ignoring an update with no content changes.",
+ params,
+ );
+ return;
}
- if (!this.hasOpenedDocument) {
+ if (this.openDocumentCount === 0) {
await this.textDocumentDidOpen({
textDocument: {
uri: params.textDocument.uri,
- languageId: "python",
+ languageId: DEFAULT_LANGUAGE_ID,
version: params.textDocument.version,
- text: params.contentChanges[0].text,
+ text: change.text,
},
});
}
@@ -191,7 +274,6 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
changes,
);
}
- const change = changes[0];
if ("range" in change) {
logger.warn(
"#textDocumentDidChange: Copilot doesn't support range changes.",
@@ -200,12 +282,24 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
}
const text = getCodes(change.text);
+ const version = ++this.documentVersion;
+ // `reInitialize` replays this as a didOpen, so keep the original languageId
+ this.lastOpenedDocument = {
+ textDocument: {
+ uri: params.textDocument.uri,
+ languageId:
+ this.lastOpenedDocument?.textDocument.languageId ??
+ DEFAULT_LANGUAGE_ID,
+ version,
+ text,
+ },
+ };
return super.textDocumentDidChange({
...params,
contentChanges: [{ text: text }],
textDocument: VersionedTextDocumentIdentifier.create(
params.textDocument.uri,
- ++this.documentVersion,
+ version,
),
});
}
@@ -264,9 +358,11 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
return await this._request("textDocument/inlineCompletion", {
...params,
textDocument: {
- ...params.textDocument,
- version: version,
- } as VersionedTextDocumentIdentifier,
+ ...VersionedTextDocumentIdentifier.create(
+ params.textDocument.uri,
+ version,
+ ),
+ },
});
};
@@ -287,8 +383,6 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
}
const requestVersion = this.documentVersion;
- (params.textDocument as VersionedTextDocumentIdentifier).version =
- requestVersion;
// If version is 0, it means the document hasn't been opened yet
if (requestVersion === 0) {
@@ -297,12 +391,27 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
// Start a loading indicator
setGitHubCopilotLoadingVersion(requestVersion);
- const response = await this.throttledGetCompletionInternal(
- params,
- requestVersion,
- );
- // Stop the loading indicator (only if the version hasn't changed)
- clearGitHubCopilotLoadingVersion(requestVersion);
+ let response: InlineCompletionList | InlineCompletionItem[] | null;
+ try {
+ response = await this.throttledGetCompletionInternal(
+ {
+ ...params,
+ textDocument: VersionedTextDocumentIdentifier.create(
+ params.textDocument.uri,
+ requestVersion,
+ ),
+ },
+ requestVersion,
+ );
+ } catch (error) {
+ // A suggestion that times out should fail quietly rather than surface as
+ // an unhandled rejection out of the inline-completion fetcher.
+ logger.warn("#getCompletion: Failed to fetch completions", error);
+ return null;
+ } finally {
+ // Stop the loading indicator (only if the version hasn't changed)
+ clearGitHubCopilotLoadingVersion(requestVersion);
+ }
// If the document version has changed since the request was made, return an empty response
if (requestVersion !== this.documentVersion) {
@@ -316,29 +425,32 @@ export class CopilotLanguageServerClient extends LanguageServerClient {
* Handle notifications from the Copilot language server.
* Uses onNotification to listen for statusNotification, didChangeStatus, and window/logMessage.
*/
- private handleNotification: Parameters<
- LanguageServerClient["onNotification"]
- >[0] = (notif) => {
- if (!notif.params) {
+ private handleNotification = (notification: unknown): void => {
+ if (!isRecord(notification) || typeof notification.method !== "string") {
return;
}
- const notification = notif as unknown as EnhancedNotification;
-
- // Handle statusNotification
- if (notification.method === "statusNotification") {
- store.set(copilotStatusState, notification.params);
- }
-
- // Handle didChangeStatus
- if (notification.method === "didChangeStatus") {
- store.set(copilotStatusState, notification.params);
+ if (
+ notification.method === "statusNotification" ||
+ notification.method === "didChangeStatus"
+ ) {
+ const status = parseCopilotStatusNotificationParams(notification.params);
+ if (status) {
+ store.set(copilotStatusState, status);
+ } else {
+ logger.warn(
+ "#statusNotification: Ignoring a status update with no params",
+ notification.params,
+ );
+ }
+ return;
}
- // Handle window/logMessage
- if (notification.method === "window/logMessage") {
- const params = notification.params as { type: number; message: string };
- const { type, message } = params;
+ if (
+ notification.method === "window/logMessage" &&
+ isLogMessageParams(notification.params)
+ ) {
+ const { type, message } = notification.params;
// Map LSP log types to console methods
// type: 1 = Error, 2 = Warning, 3 = Info, 4 = Log
switch (type) {
diff --git a/frontend/src/core/codemirror/copilot/transport.ts b/frontend/src/core/codemirror/copilot/transport.ts
index 8ea846fc76..ea384b0a5f 100644
--- a/frontend/src/core/codemirror/copilot/transport.ts
+++ b/frontend/src/core/codemirror/copilot/transport.ts
@@ -1,10 +1,7 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { WebSocketTransport } from "@open-rpc/client-js";
-import type { JSONRPCRequestData } from "@open-rpc/client-js/build/Request";
-import { Transport } from "@open-rpc/client-js/build/transports/Transport";
import { prettyError } from "@/utils/errors";
-import { Logger } from "@/utils/Logger";
+import { ReconnectingWebSocketTransport } from "@/core/lsp/transport";
export interface LazyWebsocketTransportOptions {
/**
@@ -24,224 +21,35 @@ export interface LazyWebsocketTransportOptions {
showError: (title: string, description: string | React.ReactNode) => void;
/**
- * Number of retry attempts for connection.
+ * Number of connection attempts in each retry cycle.
* @default 3
*/
retries?: number;
/**
- * Delay between retry attempts in milliseconds.
+ * Delay between connection attempts in milliseconds.
* @default 1000
*/
retryDelayMs?: number;
-
- /**
- * Default timeout for sendData operations in milliseconds.
- * Used when the caller does not provide an explicit timeout.
- * @default 5000
- */
- maxTimeoutMs?: number;
-}
-
-interface Subscription {
- event: "pending" | "notification" | "response" | "error";
- handler: Parameters<Transport["subscribe"]>[1];
}
/**
- * A WebSocket transport that lazily connects after waiting for prerequisites.
- *
- * This transport:
- * - Waits for copilot to be enabled before connecting
- * - Waits for the websocket to be available before connecting
- * - Forwards subscriptions/unsubscriptions to the delegate
- * - Handles reconnection automatically
+ * Copilot's lazy transport is the shared reconnecting LSP transport with
+ * Copilot-specific readiness checks, retries, and user-facing errors.
*/
-export class LazyWebsocketTransport extends Transport {
- private delegate: WebSocketTransport | undefined;
- private pendingSubscriptions: Subscription[] = [];
- private readonly options: Required<LazyWebsocketTransportOptions>;
- private needsReInitialization = false;
-
- /**
- * Callback invoked after the transport reconnects following a close or connection failure.
- * Used by the LSP client to re-run the initialize handshake on the new connection.
- */
- onReconnect?: () => Promise<void>;
-
+export class LazyWebsocketTransport extends ReconnectingWebSocketTransport {
constructor(options: LazyWebsocketTransportOptions) {
- super();
- this.delegate = undefined;
- this.pendingSubscriptions = [];
- this.options = {
+ super({
+ getWsUrl: options.getWsUrl,
+ waitForConnection: options.waitForReady,
retries: options.retries ?? 3,
retryDelayMs: options.retryDelayMs ?? 1000,
- maxTimeoutMs: options.maxTimeoutMs ?? 5000,
- getWsUrl: options.getWsUrl,
- waitForReady: options.waitForReady,
- showError: options.showError,
- };
- }
-
- override subscribe(...args: Parameters<Transport["subscribe"]>): void {
- // Register handler on parent Transport
- super.subscribe(...args);
-
- const [event, handler] = args;
-
- // Track the subscription
- this.pendingSubscriptions.push({ event, handler });
-
- // Also register on delegate if it exists
- if (this.delegate) {
- this.delegate.subscribe(event, handler);
- }
- }
-
- override unsubscribe(
- ...args: Parameters<Transport["unsubscribe"]>
- ): import("events").EventEmitter | undefined {
- // Unsubscribe from parent Transport
- const result = super.unsubscribe(...args);
-
- const [event, handler] = args;
- // Also unsubscribe from delegate if it exists
- if (this.delegate) {
- this.delegate.unsubscribe(event, handler);
- }
-
- // Remove from pending subscriptions if handler is provided
- if (handler) {
- if (event) {
- // Remove specific handler for specific event
- const index = this.pendingSubscriptions.findIndex(
- (sub) => sub.event === event && sub.handler === handler,
+ onConnectionFailure: (error) => {
+ options.showError(
+ "GitHub Copilot Connection Error",
+ `Failed to connect to GitHub Copilot. Please check your settings and try again.\n\n${prettyError(error)}`,
);
- if (index > -1) {
- this.pendingSubscriptions.splice(index, 1);
- }
- } else {
- // Remove handler from all events
- this.pendingSubscriptions = this.pendingSubscriptions.filter(
- (sub) => sub.handler !== handler,
- );
- }
- } else if (event) {
- // Remove all handlers for specific event
- this.pendingSubscriptions = this.pendingSubscriptions.filter(
- (sub) => sub.event !== event,
- );
- } else {
- // Remove all subscriptions
- this.pendingSubscriptions = [];
- }
-
- return result;
- }
-
- private createDelegate(): WebSocketTransport {
- const delegate = new WebSocketTransport(this.options.getWsUrl());
- // Register all pending subscriptions on the delegate
- for (const { event, handler } of this.pendingSubscriptions) {
- delegate.subscribe(event, handler);
- }
- return delegate;
- }
-
- private async tryConnect(): Promise<void> {
- for (let attempt = 1; attempt <= this.options.retries; attempt++) {
- try {
- // Create delegate, if it doesn't exist
- if (!this.delegate) {
- this.delegate = this.createDelegate();
- }
- await this.delegate.connect();
- Logger.log("Copilot#connect: Connected successfully");
- return;
- } catch (error) {
- Logger.warn(
- `Copilot#connect: Connection attempt ${attempt}/${this.options.retries} failed`,
- error,
- );
- if (attempt === this.options.retries) {
- this.delegate = undefined;
- this.needsReInitialization = true;
- // Show error toast on final retry
- this.options.showError(
- "GitHub Copilot Connection Error",
- `Failed to connect to GitHub Copilot. Please check your settings and try again.\n\n${prettyError(error)}`,
- );
- throw error;
- }
- await new Promise((resolve) =>
- setTimeout(resolve, this.options.retryDelayMs),
- );
- }
- }
- }
-
- override async connect(): Promise<void> {
- // Wait for all prerequisites to be ready
- await this.options.waitForReady();
-
- // Try connecting with retries
- return this.tryConnect();
- }
-
- override close(): void {
- this.delegate?.close();
- this.delegate = undefined;
- this.needsReInitialization = true;
- }
-
- override async sendData(
- data: JSONRPCRequestData,
- timeout: number | null | undefined,
- ): Promise<unknown> {
- // If delegate is undefined, try to reconnect
- if (!this.delegate) {
- Logger.log("Copilot#sendData: Delegate not initialized, reconnecting...");
- try {
- // Ensure prerequisites are ready before attempting connection
- await this.options.waitForReady();
- await this.tryConnect();
- } catch (error) {
- Logger.error("Copilot#sendData: Failed to reconnect transport", error);
- throw new Error(
- "Unable to connect to GitHub Copilot. Please check your settings and try again.",
- { cause: error },
- );
- }
-
- // Re-run LSP initialization handshake after reconnecting
- if (this.needsReInitialization && this.onReconnect) {
- Logger.log(
- "Copilot#sendData: Re-initializing LSP after reconnection...",
- );
- try {
- await this.onReconnect();
- this.needsReInitialization = false;
- } catch (error) {
- // Close the uninitialized connection so the next attempt starts fresh
- this.close();
- Logger.error(
- "Copilot#sendData: LSP re-initialization after reconnection failed",
- error,
- );
- throw error;
- }
- }
- }
-
- // After reconnection, delegate should be initialized
- if (!this.delegate) {
- throw new Error(
- "Failed to initialize GitHub Copilot connection. Please try again.",
- );
- }
-
- // Use maxTimeoutMs as default when no timeout is provided
- timeout = timeout ?? this.options.maxTimeoutMs;
- return this.delegate.sendData(data, timeout);
+ },
+ });
}
}
diff --git a/frontend/src/core/codemirror/copilot/types.ts b/frontend/src/core/codemirror/copilot/types.ts
index c377a0fc5f..339b38a204 100644
--- a/frontend/src/core/codemirror/copilot/types.ts
+++ b/frontend/src/core/codemirror/copilot/types.ts
@@ -35,11 +35,18 @@ export type GitHubCopilotStatus =
*/
export type GitHubCopilotRequestStatus = "InProgress" | "Warning" | "Normal";
+/**
+ * Payload of Copilot's `statusNotification`/`didChangeStatus`. `kind` and
+ * `status` are open strings, not unions: this is a vendor shape we don't
+ * control, and an unrecognized value must not discard the notification.
+ */
export interface GitHubCopilotStatusNotificationParams {
busy: boolean;
- kind: "Warning" | "Normal" | "Error" | null;
+ /** Known values: "Warning" | "Normal" | "Error" | "Inactive" */
+ kind: string | null;
message: string | null;
- status?: "InProgress" | "Warning" | "Normal";
+ /** Known values: "InProgress" | "Warning" | "Normal" */
+ status?: string;
}
export interface GitHubCopilotStatusResult {
diff --git a/frontend/src/core/codemirror/language/languages/python.ts b/frontend/src/core/codemirror/language/languages/python.ts
index 1366055f4d..7376f4deed 100644
--- a/frontend/src/core/codemirror/language/languages/python.ts
+++ b/frontend/src/core/codemirror/language/languages/python.ts
@@ -38,7 +38,11 @@ import { createLspMarkdownRenderer } from "../../lsp/markdown-renderer";
import { NotebookLanguageServerClient } from "../../lsp/notebook-lsp";
import { createTransport } from "../../lsp/transports";
import { CellDocumentUri, type ILanguageServerClient } from "../../lsp/types";
-import { getLspRootUri, getLspWorkspaceFolders } from "../../lsp/utils";
+import {
+ getLspRootUri,
+ getLspWorkspaceFolders,
+ isKnownCellDocumentUri,
+} from "../../lsp/utils";
import {
clickablePlaceholderExtension,
smartPlaceholderExtension,
@@ -338,6 +342,21 @@ export class PythonLanguageAdapter implements LanguageAdapter<{}> {
clients.length === 1
? (clients[0] as NotebookLanguageServerClient)
: new FederatedLanguageServerClient(clients);
+ const openExternalLocation = (result: { uri: string }) => {
+ Logger.debug("openExternalLocation", result);
+ // Neither the merged document nor a cell is a file on disk. The
+ // plugin handles same-cell locations itself; revealing a location in
+ // another cell isn't supported yet.
+ if (
+ client.documentUri === result.uri ||
+ isKnownCellDocumentUri(result.uri)
+ ) {
+ return;
+ }
+ getRequestClient().openFile({
+ path: result.uri.replace("file://", ""),
+ });
+ };
return [
languageServerWithClient({
@@ -346,6 +365,7 @@ export class PythonLanguageAdapter implements LanguageAdapter<{}> {
allowHTMLContent: true,
markdownRenderer: createLspMarkdownRenderer(),
useSnippetOnCompletion: true,
+ clientSideFiltering: true,
hoverConfig: hoverOptions,
completionConfig: autocompleteOptions,
// Default to false
@@ -362,16 +382,8 @@ export class PythonLanguageAdapter implements LanguageAdapter<{}> {
rename: hotkeys.getHotkey("cell.renameSymbol").key,
},
completionMatchBefore,
- onGoToDefinition: (result) => {
- Logger.debug("onGoToDefinition", result);
- if (client.documentUri === result.uri) {
- // Local definition
- return;
- }
- getRequestClient().openFile({
- path: result.uri.replace("file://", ""),
- });
- },
+ onGoToDefinition: openExternalLocation,
+ onShowLocation: openExternalLocation,
}),
documentUri.of(CellDocumentUri.of(cellId)),
];
diff --git a/frontend/src/core/codemirror/lsp/__tests__/federated-lsp.test.ts b/frontend/src/core/codemirror/lsp/__tests__/federated-lsp.test.ts
new file mode 100644
index 0000000000..1cf1e0aab6
--- /dev/null
+++ b/frontend/src/core/codemirror/lsp/__tests__/federated-lsp.test.ts
@@ -0,0 +1,161 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it, type Mocked, vi } from "vitest";
+import type * as LSP from "vscode-languageserver-protocol";
+import { FederatedLanguageServerClient } from "../federated-lsp";
+import type { ILanguageServerClient } from "../types";
+
+function createClient(
+ supportedMethods: string[] = [],
+): Mocked<ILanguageServerClient> {
+ const client: Mocked<ILanguageServerClient> = {
+ ready: true,
+ capabilities: {},
+ initializePromise: Promise.resolve(),
+ clientCapabilities: {},
+ initialize: vi.fn(),
+ close: vi.fn(),
+ hasCapability: vi.fn((method) => supportedMethods.includes(method)),
+ textDocumentDidOpen: vi.fn().mockResolvedValue(false),
+ textDocumentDidChange: vi.fn(),
+ textDocumentDidClose: vi.fn(),
+ textDocumentWillSave: vi.fn(),
+ textDocumentWillSaveWaitUntil: vi.fn().mockResolvedValue(null),
+ textDocumentDidSave: vi.fn(),
+ textDocumentHover: vi.fn().mockResolvedValue({ contents: [] }),
+ textDocumentCompletion: vi.fn().mockResolvedValue(null),
+ completionItemResolve: vi.fn(),
+ textDocumentDefinition: vi.fn().mockResolvedValue(null),
+ textDocumentCodeAction: vi.fn().mockResolvedValue(null),
+ codeActionResolve: vi.fn(),
+ textDocumentRename: vi.fn().mockResolvedValue(null),
+ textDocumentPrepareRename: vi.fn().mockResolvedValue(null),
+ textDocumentSignatureHelp: vi.fn().mockResolvedValue(null),
+ onNotification: vi.fn().mockReturnValue(() => true),
+ };
+ return client;
+}
+
+describe("FederatedLanguageServerClient", () => {
+ it("routes requests using dynamic method capabilities", async () => {
+ const staticOnlyClient = createClient();
+ const dynamicClient = createClient(["textDocument/definition"]);
+ const definition: LSP.Location = {
+ uri: "file:///definition.py",
+ range: {
+ start: { line: 1, character: 2 },
+ end: { line: 1, character: 5 },
+ },
+ };
+ dynamicClient.textDocumentDefinition.mockResolvedValue(definition);
+ const client = new FederatedLanguageServerClient([
+ staticOnlyClient,
+ dynamicClient,
+ ]);
+ const params: LSP.DefinitionParams = {
+ textDocument: { uri: "file:///notebook.py" },
+ position: { line: 0, character: 0 },
+ };
+
+ await expect(client.textDocumentDefinition(params)).resolves.toEqual(
+ definition,
+ );
+ expect(client.hasCapability("textDocument/definition")).toBe(true);
+ expect(staticOnlyClient.textDocumentDefinition).not.toHaveBeenCalled();
+ expect(dynamicClient.textDocumentDefinition).toHaveBeenCalledWith(params);
+ });
+
+ it("prefers the first client's willSaveWaitUntil edits regardless of latency", async () => {
+ const slowEdits: LSP.TextEdit[] = [
+ {
+ range: {
+ start: { line: 0, character: 0 },
+ end: { line: 0, character: 1 },
+ },
+ newText: "first",
+ },
+ ];
+ const first = createClient();
+ const second = createClient();
+ // Answers last, but still wins
+ first.textDocumentWillSaveWaitUntil.mockImplementation(
+ () => new Promise((resolve) => setTimeout(() => resolve(slowEdits), 5)),
+ );
+ second.textDocumentWillSaveWaitUntil.mockResolvedValue([
+ {
+ range: {
+ start: { line: 1, character: 0 },
+ end: { line: 1, character: 1 },
+ },
+ newText: "second",
+ },
+ ]);
+ const client = new FederatedLanguageServerClient([first, second]);
+
+ await expect(
+ client.textDocumentWillSaveWaitUntil({
+ textDocument: { uri: "file:///notebook.py" },
+ reason: 1,
+ }),
+ ).resolves.toEqual(slowEdits);
+ expect(second.textDocumentWillSaveWaitUntil).toHaveBeenCalled();
+ });
+
+ it("skips clients that reject willSaveWaitUntil", async () => {
+ const failing = createClient();
+ const working = createClient();
+ const edits: LSP.TextEdit[] = [
+ {
+ range: {
+ start: { line: 0, character: 0 },
+ end: { line: 0, character: 1 },
+ },
+ newText: "ok",
+ },
+ ];
+ failing.textDocumentWillSaveWaitUntil.mockRejectedValue(
+ new Error("server exploded"),
+ );
+ working.textDocumentWillSaveWaitUntil.mockResolvedValue(edits);
+ const client = new FederatedLanguageServerClient([failing, working]);
+
+ await expect(
+ client.textDocumentWillSaveWaitUntil({
+ textDocument: { uri: "file:///notebook.py" },
+ reason: 1,
+ }),
+ ).resolves.toEqual(edits);
+ });
+
+ it("fans out document lifecycle and save notifications", async () => {
+ const first = createClient();
+ const second = createClient();
+ second.textDocumentDidOpen.mockResolvedValue(true);
+ const client = new FederatedLanguageServerClient([first, second]);
+ const openParams: LSP.DidOpenTextDocumentParams = {
+ textDocument: {
+ uri: "file:///notebook.py",
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ };
+ const closeParams: LSP.DidCloseTextDocumentParams = {
+ textDocument: { uri: "file:///notebook.py" },
+ };
+ const saveParams: LSP.DidSaveTextDocumentParams = {
+ textDocument: { uri: "file:///notebook.py" },
+ text: "value = 1",
+ };
+
+ await expect(client.textDocumentDidOpen(openParams)).resolves.toBe(true);
+ await client.textDocumentDidClose(closeParams);
+ await client.textDocumentDidSave(saveParams);
+
+ for (const child of [first, second]) {
+ expect(child.textDocumentDidOpen).toHaveBeenCalledWith(openParams);
+ expect(child.textDocumentDidClose).toHaveBeenCalledWith(closeParams);
+ expect(child.textDocumentDidSave).toHaveBeenCalledWith(saveParams);
+ }
+ });
+});
diff --git a/frontend/src/core/codemirror/lsp/__tests__/log-messages.test.ts b/frontend/src/core/codemirror/lsp/__tests__/log-messages.test.ts
new file mode 100644
index 0000000000..bac278d427
--- /dev/null
+++ b/frontend/src/core/codemirror/lsp/__tests__/log-messages.test.ts
@@ -0,0 +1,53 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type { JSONRPCMessage } from "@marimo-team/codemirror-languageserver";
+import { beforeEach, describe, expect, it } from "vitest";
+import { initialNotebookState, notebookAtom } from "@/core/cells/cells";
+import { store } from "@/core/state/jotai";
+import { handleLogMessage } from "../log-messages";
+
+function logMessage(type: number, message: string): JSONRPCMessage {
+ return {
+ jsonrpc: "2.0",
+ method: "window/logMessage",
+ params: { type, message },
+ } as JSONRPCMessage;
+}
+
+describe("handleLogMessage", () => {
+ beforeEach(() => {
+ store.set(notebookAtom, initialNotebookState());
+ });
+
+ it("adds a log for each message, erroring on Error and Warning", () => {
+ handleLogMessage("pylsp", logMessage(1, "failed to start"));
+ handleLogMessage("pylsp", logMessage(2, "plugin not installed"));
+ handleLogMessage("pylsp", logMessage(3, "indexing"));
+
+ expect(store.get(notebookAtom).cellLogs).toMatchObject([
+ { level: "stderr", message: "failed to start" },
+ { level: "stderr", message: "plugin not installed" },
+ {
+ level: "stdout",
+ message: "indexing",
+ source: { type: "lsp", name: "pylsp" },
+ },
+ ]);
+ });
+
+ it("ignores messages that aren't logs", () => {
+ handleLogMessage("pylsp", { jsonrpc: "2.0", id: 1 } as JSONRPCMessage);
+ handleLogMessage("pylsp", {
+ jsonrpc: "2.0",
+ method: "textDocument/publishDiagnostics",
+ params: { uri: "file:///a", diagnostics: [] },
+ } as JSONRPCMessage);
+ handleLogMessage("pylsp", {
+ jsonrpc: "2.0",
+ method: "window/logMessage",
+ params: { type: 1 },
+ } as JSONRPCMessage);
+
+ expect(store.get(notebookAtom).cellLogs).toEqual([]);
+ });
+});
diff --git a/frontend/src/core/codemirror/lsp/__tests__/normalize-markdown-math.test.ts b/frontend/src/core/codemirror/lsp/__tests__/normalize-markdown-math.test.ts
index 6edea94cf0..19af6b63b2 100644
--- a/frontend/src/core/codemirror/lsp/__tests__/normalize-markdown-math.test.ts
+++ b/frontend/src/core/codemirror/lsp/__tests__/normalize-markdown-math.test.ts
@@ -90,6 +90,31 @@ Use \`:math:\` as text and :math:\`z\` as math.
expect(normalized).not.toContain(":math:`z`");
expect(normalized).toContain("||(z||)");
});
+
+ it("leaves markdown-escaped brackets in type annotations alone", () => {
+ const markdown =
+ 'on\\_change (Optional\\[Callable\\]) and editable\\_columns (Union\\[list\\[str\\], Literal\\["all"\\]\\])';
+
+ expect(normalizeMarkdownMath(markdown)).toBe(markdown);
+ });
+
+ it("keeps the newlines around fenced code blocks", () => {
+ const markdown = `
+\`\`\`python
+class data_editor()
+\`\`\`
+---
+Inline \\[z^2\\] math forces normalization.
+
+Done.
+`.trim();
+
+ // A closing fence glued to the next line stops closing the block, and the
+ // rest of the document is swallowed into it.
+ expect(normalizeMarkdownMath(markdown)).toContain(
+ "class data_editor()\n```\n---\n",
+ );
+ });
});
describe("normalizeLspDocumentation", () => {
diff --git a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
index f437ce8a49..f7495cb4fd 100644
--- a/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
+++ b/frontend/src/core/codemirror/lsp/__tests__/notebook-lsp.test.ts
@@ -1,6 +1,4 @@
/* Copyright 2026 Marimo. All rights reserved. */
-/* oxlint-disable typescript/no-explicit-any */
-
import { EditorView } from "@codemirror/view";
import {
type LanguageServerClient,
@@ -18,7 +16,11 @@ import { PythonLanguageAdapter } from "../../language/languages/python";
import { languageMetadataField } from "../../language/metadata";
import { createNotebookLens } from "../lens";
import { NotebookLanguageServerClient } from "../notebook-lsp";
-import { CellDocumentUri, type ILanguageServerClient } from "../types";
+import {
+ CellDocumentUri,
+ type ClientNotification,
+ type ILanguageServerClient,
+} from "../types";
const Cells = {
cell1: cellId("cell1"),
@@ -26,6 +28,40 @@ const Cells = {
cell3: cellId("cell3"),
};
+type TestLanguageServerClient = ILanguageServerClient & {
+ notify: (method: string, params: unknown) => Promise<void>;
+ processNotification: (notification: ClientNotification) => void;
+};
+
+type GetNotebookEditors = () => Record<CellId, EditorView | null | undefined>;
+
+function replaceNotebookEditors(
+ client: NotebookLanguageServerClient,
+ getNotebookEditors: GetNotebookEditors,
+): void {
+ const mutableClient = client as unknown as {
+ getNotebookEditors: GetNotebookEditors;
+ };
+ mutableClient.getNotebookEditors = getNotebookEditors;
+}
+
+function isPublishDiagnosticsNotification(
+ notification: ClientNotification,
+): notification is {
+ method: "textDocument/publishDiagnostics";
+ params: LSP.PublishDiagnosticsParams;
+} {
+ return (
+ notification.method === "textDocument/publishDiagnostics" &&
+ typeof notification.params === "object" &&
+ notification.params !== null &&
+ "uri" in notification.params &&
+ typeof notification.params.uri === "string" &&
+ "diagnostics" in notification.params &&
+ Array.isArray(notification.params.diagnostics)
+ );
+}
+
describe("createNotebookLens", () => {
it("should produce correct lens for same inputs", () => {
// Use unique content for this test
@@ -243,8 +279,9 @@ describe("createNotebookLens", () => {
});
describe("NotebookLanguageServerClient", () => {
- let mockClient: Mocked<ILanguageServerClient>;
+ let mockClient: Mocked<TestLanguageServerClient>;
let notebookClient: NotebookLanguageServerClient;
+ let notifyMock: Mocked<TestLanguageServerClient>["notify"];
beforeEach(() => {
mockClient = {
@@ -252,12 +289,18 @@ describe("NotebookLanguageServerClient", () => {
capabilities: {},
initializePromise: Promise.resolve(),
clientCapabilities: {},
+ hasCapability: vi.fn().mockReturnValue(false),
completionItemResolve: vi.fn(),
+ codeActionResolve: vi.fn(),
initialize: vi.fn(),
close: vi.fn(),
onNotification: vi.fn(),
textDocumentDidOpen: vi.fn(),
textDocumentDidChange: vi.fn(),
+ textDocumentDidClose: vi.fn(),
+ textDocumentWillSave: vi.fn(),
+ textDocumentWillSaveWaitUntil: vi.fn(),
+ textDocumentDidSave: vi.fn(),
textDocumentHover: vi.fn(),
textDocumentCompletion: vi.fn(),
textDocumentDefinition: vi.fn(),
@@ -265,14 +308,10 @@ describe("NotebookLanguageServerClient", () => {
textDocumentCodeAction: vi.fn(),
textDocumentSignatureHelp: vi.fn(),
textDocumentRename: vi.fn(),
+ processNotification: vi.fn(),
+ notify: vi.fn(),
};
- (mockClient as any).processNotification = vi.fn();
- (mockClient as any).notify = vi.fn();
- notebookClient = new NotebookLanguageServerClient(mockClient, {}, () => ({
- [Cells.cell1]: new EditorView({ doc: "# this is a comment" }),
- [Cells.cell2]: new EditorView({ doc: "import math\nimport numpy" }),
- [Cells.cell3]: new EditorView({ doc: "print(math.sqrt(4))" }),
- }));
+ notifyMock = mockClient.notify;
// Mock the atom instead of the instance method
vi.spyOn(store, "get").mockImplementation((atom) => {
@@ -295,7 +334,331 @@ describe("NotebookLanguageServerClient", () => {
return undefined;
});
- (NotebookLanguageServerClient as any).SEEN_CELL_DOCUMENT_URIS.clear();
+ notebookClient = new NotebookLanguageServerClient(mockClient, {}, () => ({
+ [Cells.cell1]: new EditorView({ doc: "# this is a comment" }),
+ [Cells.cell2]: new EditorView({ doc: "import math\nimport numpy" }),
+ [Cells.cell3]: new EditorView({ doc: "print(math.sqrt(4))" }),
+ }));
+ });
+
+ describe("document lifecycle", () => {
+ it("maps cell opens and closes to the merged notebook document", async () => {
+ mockClient.textDocumentDidOpen.mockResolvedValue(true);
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+
+ await expect(
+ notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "print('cell')",
+ },
+ }),
+ ).resolves.toBe(true);
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri },
+ });
+
+ expect(mockClient.textDocumentDidOpen).toHaveBeenCalledWith({
+ textDocument: {
+ uri: "file:///project/__marimo_notebook__.py",
+ languageId: "python",
+ version: 1,
+ text: [
+ "# this is a comment",
+ "import math",
+ "import numpy",
+ "print(math.sqrt(4))",
+ ].join("\n"),
+ },
+ });
+ expect(mockClient.textDocumentDidClose).toHaveBeenCalledWith({
+ textDocument: {
+ uri: "file:///project/__marimo_notebook__.py",
+ },
+ });
+ });
+
+ it("reopens the merged document directly after reconnection", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ await notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "print('cell')",
+ },
+ });
+ notifyMock.mockClear();
+
+ await notebookClient.resyncAllDocuments();
+
+ expect(mockClient.initialize).toHaveBeenCalledTimes(1);
+ expect(notifyMock).toHaveBeenNthCalledWith(
+ 1,
+ "workspace/didChangeConfiguration",
+ { settings: {} },
+ );
+ expect(notifyMock).toHaveBeenNthCalledWith(2, "textDocument/didOpen", {
+ textDocument: {
+ uri: "file:///project/__marimo_notebook__.py",
+ languageId: "python",
+ version: 1,
+ text: [
+ "# this is a comment",
+ "import math",
+ "import numpy",
+ "print(math.sqrt(4))",
+ ].join("\n"),
+ },
+ });
+ expect(mockClient.textDocumentDidOpen).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not reopen a notebook with no active editor documents", async () => {
+ notifyMock.mockClear();
+
+ await notebookClient.resyncAllDocuments();
+
+ expect(notifyMock).toHaveBeenCalledTimes(1);
+ expect(notifyMock).not.toHaveBeenCalledWith(
+ "textDocument/didOpen",
+ expect.anything(),
+ );
+ });
+
+ it("keeps the merged document open until its last cell view closes", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ const openParams: LSP.DidOpenTextDocumentParams = {
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ };
+ await notebookClient.textDocumentDidOpen(openParams);
+ await notebookClient.textDocumentDidOpen(openParams);
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri },
+ });
+ notifyMock.mockClear();
+
+ await notebookClient.resyncAllDocuments();
+ expect(notifyMock).toHaveBeenCalledWith(
+ "textDocument/didOpen",
+ expect.anything(),
+ );
+
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri },
+ });
+ notifyMock.mockClear();
+ await notebookClient.resyncAllDocuments();
+ expect(notifyMock).not.toHaveBeenCalledWith(
+ "textDocument/didOpen",
+ expect.anything(),
+ );
+ });
+
+ it("ignores an unmatched close instead of forwarding it to the underlying client", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+
+ // No matching textDocumentDidOpen was ever recorded for this cell.
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri },
+ });
+
+ expect(mockClient.textDocumentDidClose).not.toHaveBeenCalled();
+ });
+
+ it("does not let an unmatched close of one cell close the document for others", async () => {
+ const cellUri1 = CellDocumentUri.of(Cells.cell1);
+ const cellUri2 = CellDocumentUri.of(Cells.cell2);
+ await notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: cellUri2,
+ languageId: "python",
+ version: 1,
+ text: "import math",
+ },
+ });
+
+ // A spurious close for a cell that was never opened must not forward a
+ // close for the merged document while cell2's view is still open.
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri1 },
+ });
+
+ expect(mockClient.textDocumentDidClose).not.toHaveBeenCalled();
+ });
+
+ it("rolls back the open count when the underlying client rejects textDocumentDidOpen", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ mockClient.textDocumentDidOpen.mockRejectedValueOnce(
+ new Error("transport failure"),
+ );
+
+ await expect(
+ notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ }),
+ ).rejects.toThrow("transport failure");
+
+ // The failed open must not leave a phantom reference behind: a
+ // subsequent resync should not believe this cell is open.
+ notifyMock.mockClear();
+ await notebookClient.resyncAllDocuments();
+ expect(notifyMock).not.toHaveBeenCalledWith(
+ "textDocument/didOpen",
+ expect.anything(),
+ );
+ });
+
+ it("keeps the reference and marks the cell seen when the underlying client reports no fresh open (result === false)", async () => {
+ // The vendor client resolves `false` when another view already has the
+ // shared merged document open: it did not need to send its own
+ // `didOpen`, but the document is genuinely open. This is the common
+ // case for every cell after the first one in a notebook, since all
+ // cells share the same underlying merged-document URI. It must not be
+ // treated like a rejection.
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ mockClient.textDocumentDidOpen.mockResolvedValueOnce(false);
+
+ await expect(
+ notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ }),
+ ).resolves.toBe(false);
+
+ const seenCellDocumentUris = (
+ notebookClient as unknown as { seenCellDocumentUris: Set<string> }
+ ).seenCellDocumentUris;
+ expect(seenCellDocumentUris.has(cellUri)).toBe(true);
+
+ // The reference must still be tracked: closing it forwards a real
+ // close instead of being treated as unmatched.
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri },
+ });
+ expect(mockClient.textDocumentDidClose).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not mark a cell as seen when the underlying client rejects textDocumentDidOpen", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ mockClient.textDocumentDidOpen.mockRejectedValueOnce(
+ new Error("transport failure"),
+ );
+
+ await expect(
+ notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ }),
+ ).rejects.toThrow("transport failure");
+
+ // `seenCellDocumentUris` drives diagnostic clearing; a cell that never
+ // successfully opened has no diagnostics to clear and must not be
+ // tracked as seen.
+ const seenCellDocumentUris = (
+ notebookClient as unknown as { seenCellDocumentUris: Set<string> }
+ ).seenCellDocumentUris;
+ expect(seenCellDocumentUris.has(cellUri)).toBe(false);
+ });
+
+ it("does not drop a concurrent open's reference when an earlier open for the same cell rejects", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ const openParams: LSP.DidOpenTextDocumentParams = {
+ textDocument: {
+ uri: cellUri,
+ languageId: "python",
+ version: 1,
+ text: "value = 1",
+ },
+ };
+
+ let rejectFirst!: (error: Error) => void;
+ const firstUnderlyingOpen = new Promise<boolean>((_, reject) => {
+ rejectFirst = reject;
+ });
+ let resolveSecond!: (value: boolean) => void;
+ const secondUnderlyingOpen = new Promise<boolean>((resolve) => {
+ resolveSecond = resolve;
+ });
+ mockClient.textDocumentDidOpen
+ .mockReturnValueOnce(firstUnderlyingOpen)
+ .mockReturnValueOnce(secondUnderlyingOpen);
+
+ // Two concurrent opens for the same cell (e.g. a fast remount) both
+ // increment the shared count before either underlying call settles.
+ const firstCall = notebookClient.textDocumentDidOpen(openParams);
+ const secondCall = notebookClient.textDocumentDidOpen(openParams);
+ const firstCallOutcome = firstCall.catch((error: Error) => error);
+
+ // The first (stale) open fails; its rollback must not erase the
+ // second, still-live open's reference.
+ rejectFirst(new Error("transport failure"));
+ await firstCallOutcome;
+
+ resolveSecond(true);
+ await secondCall;
+
+ await notebookClient.textDocumentDidClose({
+ textDocument: { uri: cellUri },
+ });
+ expect(mockClient.textDocumentDidClose).toHaveBeenCalledTimes(1);
+ });
+
+ it("maps safe save notifications and rejects merged save edits", async () => {
+ const cellUri = CellDocumentUri.of(Cells.cell1);
+ const willSaveParams: LSP.WillSaveTextDocumentParams = {
+ textDocument: { uri: cellUri },
+ reason: LSP.TextDocumentSaveReason.Manual,
+ };
+
+ await notebookClient.textDocumentWillSave(willSaveParams);
+ await expect(
+ notebookClient.textDocumentWillSaveWaitUntil(willSaveParams),
+ ).resolves.toBeNull();
+ await notebookClient.textDocumentDidSave({
+ textDocument: { uri: cellUri },
+ text: "cell text",
+ });
+
+ expect(mockClient.textDocumentWillSave).toHaveBeenCalledWith({
+ textDocument: {
+ uri: "file:///project/__marimo_notebook__.py",
+ },
+ reason: LSP.TextDocumentSaveReason.Manual,
+ });
+ expect(mockClient.textDocumentWillSaveWaitUntil).not.toHaveBeenCalled();
+ expect(mockClient.textDocumentDidSave).toHaveBeenCalledWith({
+ textDocument: {
+ uri: "file:///project/__marimo_notebook__.py",
+ },
+ text: [
+ "# this is a comment",
+ "import math",
+ "import numpy",
+ "print(math.sqrt(4))",
+ ].join("\n"),
+ });
+ });
});
describe("textDocumentHover", () => {
@@ -598,26 +961,6 @@ describe("NotebookLanguageServerClient", () => {
describe("textDocumentRename", () => {
it("should transform rename position and apply edits to editor views", async () => {
- const props = {
- workspaceFolders: null,
- capabilities: {
- textDocument: {
- rename: {
- prepareSupport: true,
- },
- },
- },
- languageId: "python",
- transport: {
- sendData: vi.fn(),
- subscribe: vi.fn(),
- connect: vi.fn(),
- transportRequestManager: {
- send: vi.fn(),
- },
- } as any,
- };
-
// Setup mock plugins with editor views
const mockView1 = new EditorView({
doc: "# this is a comment",
@@ -627,7 +970,6 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell1),
- ...props,
}),
],
});
@@ -641,7 +983,6 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell2),
- ...props,
}),
],
});
@@ -655,17 +996,16 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell3),
- ...props,
}),
],
});
expect(mockView3.state.doc.toString()).toBe("print(math.sqrt(4))");
- (notebookClient as any).getNotebookEditors = () => ({
+ replaceNotebookEditors(notebookClient, () => ({
[Cells.cell1]: mockView1,
[Cells.cell2]: mockView2,
[Cells.cell3]: mockView3,
- });
+ }));
// Setup rename params
const renameParams: LSP.RenameParams = {
@@ -825,7 +1165,7 @@ describe("NotebookLanguageServerClient", () => {
end: { line: 0, character: 5 },
},
// Missing newText property
- } as any,
+ } as unknown as LSP.TextEdit,
],
},
],
@@ -858,26 +1198,6 @@ describe("NotebookLanguageServerClient", () => {
});
it("should handle raw strings with markdown content during rename (issue #7377)", async () => {
- const props = {
- workspaceFolders: null,
- capabilities: {
- textDocument: {
- rename: {
- prepareSupport: true,
- },
- },
- },
- languageId: "python",
- transport: {
- sendData: vi.fn(),
- subscribe: vi.fn(),
- connect: vi.fn(),
- transportRequestManager: {
- send: vi.fn(),
- },
- } as any,
- };
-
// Setup mock plugins with editor views
const markdownCell = 'mo.md(r"""\n# Header\n""")';
const variableCell = "a = 'Test'";
@@ -890,7 +1210,6 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell1),
- ...props,
}),
],
});
@@ -903,7 +1222,6 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell2),
- ...props,
}),
],
});
@@ -916,16 +1234,15 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell3),
- ...props,
}),
],
});
- (notebookClient as any).getNotebookEditors = () => ({
+ replaceNotebookEditors(notebookClient, () => ({
[Cells.cell1]: mockView1,
[Cells.cell2]: mockView2,
[Cells.cell3]: mockView3,
- });
+ }));
// Update the mock to return the correct codes
vi.spyOn(store, "get").mockImplementation((atom) => {
@@ -1005,26 +1322,6 @@ describe("NotebookLanguageServerClient", () => {
});
it("should only rename private variables in the current cell (issue #7810)", async () => {
- const props = {
- workspaceFolders: null,
- capabilities: {
- textDocument: {
- rename: {
- prepareSupport: true,
- },
- },
- },
- languageId: "python",
- transport: {
- sendData: vi.fn(),
- subscribe: vi.fn(),
- connect: vi.fn(),
- transportRequestManager: {
- send: vi.fn(),
- },
- } as any,
- };
-
// Setup editor views - both cells have a private variable _x
const cell1Code = "_x = 1\nprint(_x)";
const cell2Code = "_x = 2\nprint(_x)";
@@ -1037,7 +1334,6 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell1),
- ...props,
}),
],
});
@@ -1050,15 +1346,14 @@ describe("NotebookLanguageServerClient", () => {
languageServerWithClient({
client: mockClient as unknown as LanguageServerClient,
documentUri: CellDocumentUri.of(Cells.cell2),
- ...props,
}),
],
});
- (notebookClient as any).getNotebookEditors = () => ({
+ replaceNotebookEditors(notebookClient, () => ({
[Cells.cell1]: mockView1,
[Cells.cell2]: mockView2,
- });
+ }));
// Update the mock to return the correct codes
vi.spyOn(store, "get").mockImplementation((atom) => {
@@ -1129,22 +1424,38 @@ describe("NotebookLanguageServerClient", () => {
});
describe("diagnostics handling", () => {
+ it("forwards malformed diagnostic notifications without throwing", () => {
+ const forwardedNotification = vi.fn();
+ mockClient.processNotification = forwardedNotification;
+ notebookClient.patchProcessNotification();
+
+ const malformedNotification: ClientNotification = {
+ method: "textDocument/publishDiagnostics",
+ params: {
+ uri: "file:///__marimo__.py",
+ diagnostics: [{ message: "Missing a range" }],
+ },
+ };
+
+ expect(() => {
+ mockClient.processNotification(malformedNotification);
+ }).not.toThrow();
+ expect(forwardedNotification).toHaveBeenCalledWith(malformedNotification);
+ });
+
it("should transform diagnostic ranges and filter out-of-bounds diagnostics", async () => {
// Mock processNotification to capture the transformed diagnostics
const capturedDiagnostics: LSP.PublishDiagnosticsParams[] = [];
- // @ts-expect-error: processNotification is private
mockClient.processNotification = vi
.fn()
- .mockImplementation((notification: any) => {
- if (notification.method === "textDocument/publishDiagnostics") {
+ .mockImplementation((notification: ClientNotification) => {
+ if (isPublishDiagnosticsNotification(notification)) {
capturedDiagnostics.push(notification.params);
}
});
// Call patch since we changed processNotification
notebookClient.patchProcessNotification();
- // Start the document version at 1
- (notebookClient as any).documentVersion = 1;
// Open documents for multiple cells so they get tracked
await notebookClient.textDocumentDidOpen({
@@ -1193,8 +1504,7 @@ describe("NotebookLanguageServerClient", () => {
},
};
- // @ts-expect-error: processNotification is private
- notebookClient.client.processNotification(diagnosticsNotification);
+ mockClient.processNotification(diagnosticsNotification);
expect(capturedDiagnostics).toHaveLength(2);
expect(capturedDiagnostics[0].diagnostics).toHaveLength(1);
@@ -1207,21 +1517,160 @@ describe("NotebookLanguageServerClient", () => {
expect(capturedDiagnostics[1].diagnostics).toHaveLength(0);
});
+ it("should map relatedInformation out of merged-document coordinates", async () => {
+ const capturedDiagnostics: LSP.PublishDiagnosticsParams[] = [];
+ mockClient.processNotification = vi
+ .fn()
+ .mockImplementation((notification: ClientNotification) => {
+ if (isPublishDiagnosticsNotification(notification)) {
+ capturedDiagnostics.push(notification.params);
+ }
+ });
+ notebookClient.patchProcessNotification();
+
+ await notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: CellDocumentUri.of(Cells.cell3),
+ languageId: "python",
+ version: 1,
+ text: "print(math.sqrt(4))",
+ },
+ });
+
+ // Merged doc: line 0 -> cell1, lines 1-2 -> cell2, line 3 -> cell3
+ mockClient.processNotification({
+ method: "textDocument/publishDiagnostics",
+ params: {
+ uri: notebookClient.documentUri,
+ version: 1,
+ diagnostics: [
+ {
+ range: {
+ start: { line: 3, character: 0 },
+ end: { line: 3, character: 5 },
+ },
+ message: "Shadowed import",
+ severity: LSP.DiagnosticSeverity.Warning,
+ relatedInformation: [
+ {
+ location: {
+ uri: notebookClient.documentUri,
+ range: {
+ start: { line: 1, character: 7 },
+ end: { line: 1, character: 11 },
+ },
+ },
+ message: "declared here",
+ },
+ {
+ location: {
+ uri: "file:///site-packages/math.pyi",
+ range: {
+ start: { line: 9, character: 0 },
+ end: { line: 9, character: 4 },
+ },
+ },
+ message: "stub definition",
+ },
+ ],
+ },
+ ],
+ },
+ });
+
+ const forwarded = capturedDiagnostics.find(
+ (params) => params.diagnostics.length > 0,
+ );
+ expect(forwarded?.uri).toBe(CellDocumentUri.of(Cells.cell3));
+ expect(forwarded?.diagnostics[0].relatedInformation).toEqual([
+ {
+ location: {
+ uri: CellDocumentUri.of(Cells.cell2),
+ range: {
+ start: { line: 0, character: 7 },
+ end: { line: 0, character: 11 },
+ },
+ },
+ message: "declared here",
+ },
+ {
+ location: {
+ uri: "file:///site-packages/math.pyi",
+ range: {
+ start: { line: 9, character: 0 },
+ end: { line: 9, character: 4 },
+ },
+ },
+ message: "stub definition",
+ },
+ ]);
+ });
+
+ it("should keep diagnostics with unusable relatedInformation entries", async () => {
+ const capturedDiagnostics: LSP.PublishDiagnosticsParams[] = [];
+ mockClient.processNotification = vi
+ .fn()
+ .mockImplementation((notification: ClientNotification) => {
+ if (isPublishDiagnosticsNotification(notification)) {
+ capturedDiagnostics.push(notification.params);
+ }
+ });
+ notebookClient.patchProcessNotification();
+
+ await notebookClient.textDocumentDidOpen({
+ textDocument: {
+ uri: CellDocumentUri.of(Cells.cell3),
+ languageId: "python",
+ version: 1,
+ text: "print(math.sqrt(4))",
+ },
+ });
+
+ expect(() => {
+ mockClient.processNotification({
+ method: "textDocument/publishDiagnostics",
+ params: {
+ uri: notebookClient.documentUri,
+ version: 1,
+ diagnostics: [
+ {
+ range: {
+ start: { line: 3, character: 0 },
+ end: { line: 3, character: 5 },
+ },
+ message: "Shadowed import",
+ // `isDiagnostic` doesn't reach these, so a server can hand us
+ // entries that aren't objects at all.
+ relatedInformation: [
+ null,
+ { message: "no location" },
+ ] as unknown as LSP.DiagnosticRelatedInformation[],
+ },
+ ],
+ },
+ });
+ }).not.toThrow();
+
+ const forwarded = capturedDiagnostics.find(
+ (params) => params.diagnostics.length > 0,
+ );
+ expect(forwarded?.uri).toBe(CellDocumentUri.of(Cells.cell3));
+ expect(forwarded?.diagnostics[0].message).toBe("Shadowed import");
+ });
+
it("should clear diagnostics for all cells when receiving empty diagnostics", async () => {
await notebookClient.sync();
const seenNotifications: LSP.PublishDiagnosticsParams[] = [];
- (mockClient as any).processNotification = vi
+ mockClient.processNotification = vi
.fn()
- .mockImplementation((notification: any) => {
- if (notification.method === "textDocument/publishDiagnostics") {
+ .mockImplementation((notification: ClientNotification) => {
+ if (isPublishDiagnosticsNotification(notification)) {
seenNotifications.push(notification.params);
}
});
notebookClient.patchProcessNotification();
- // Start the document version at 1
- (notebookClient as any).documentVersion = 1;
// Open documents so they get tracked
await notebookClient.textDocumentDidOpen({
@@ -1252,8 +1701,7 @@ describe("NotebookLanguageServerClient", () => {
},
};
- // @ts-expect-error: processNotification is private
- notebookClient.client.processNotification(emptyDiagnosticsNotification);
+ mockClient.processNotification(emptyDiagnosticsNotification);
expect(seenNotifications.length).toBeGreaterThan(0);
seenNotifications.forEach((notification) => {
@@ -1263,17 +1711,15 @@ describe("NotebookLanguageServerClient", () => {
it("should handle diagnostics across multiple cells", async () => {
const seenNotifications = new Map<string, LSP.PublishDiagnosticsParams>();
- (mockClient as any).processNotification = vi
+ mockClient.processNotification = vi
.fn()
- .mockImplementation((notification: any) => {
- if (notification.method === "textDocument/publishDiagnostics") {
+ .mockImplementation((notification: ClientNotification) => {
+ if (isPublishDiagnosticsNotification(notification)) {
seenNotifications.set(notification.params.uri, notification.params);
}
});
notebookClient.patchProcessNotification();
- // Start the document version at 1
- (notebookClient as any).documentVersion = 1;
// Open documents for multiple cells so they get tracked
await notebookClient.textDocumentDidOpen({
@@ -1321,10 +1767,7 @@ describe("NotebookLanguageServerClient", () => {
},
};
- // @ts-expect-error: processNotification is private
- notebookClient.client.processNotification(
- multiCellDiagnosticsNotification,
- );
+ mockClient.processNotification(multiCellDiagnosticsNotification);
expect(seenNotifications.size).toBe(2);
const cell1Uri = CellDocumentUri.of(Cells.cell1);
@@ -1341,13 +1784,7 @@ describe("NotebookLanguageServerClient", () => {
});
it("should handle version updates in textDocumentDidChange", async () => {
- mockClient.textDocumentDidChange = vi
- .fn()
- .mockImplementation((params) => {
- return params;
- });
-
- const result = await notebookClient.textDocumentDidChange({
+ await notebookClient.textDocumentDidChange({
textDocument: {
uri: CellDocumentUri.of(Cells.cell1),
version: 5,
@@ -1355,15 +1792,22 @@ describe("NotebookLanguageServerClient", () => {
contentChanges: [{ text: "new code" }],
});
- expect(result).toBeDefined();
- expect(result.textDocument.version).toBeGreaterThan(0);
- expect(result.contentChanges).toHaveLength(1);
- expect(result.contentChanges[0].text).toMatchInlineSnapshot(`
- "# this is a comment
- import math
- import numpy
- print(math.sqrt(4))"
- `);
+ expect(mockClient.textDocumentDidChange).toHaveBeenCalledWith({
+ textDocument: {
+ uri: "file:///project/__marimo_notebook__.py",
+ version: expect.any(Number),
+ },
+ contentChanges: [
+ {
+ text: [
+ "# this is a comment",
+ "import math",
+ "import numpy",
+ "print(math.sqrt(4))",
+ ].join("\n"),
+ },
+ ],
+ });
});
});
@@ -1404,14 +1848,8 @@ describe("NotebookLanguageServerClient", () => {
});
});
- describe("SEEN_CELL_DOCUMENT_URIS memory management", () => {
- it("should track opened cells in SEEN_CELL_DOCUMENT_URIS", async () => {
- // Clear any existing state
- const seenUris = (NotebookLanguageServerClient as any)
- .SEEN_CELL_DOCUMENT_URIS;
- seenUris.clear();
-
- // Open some cells to add them to SEEN_CELL_DOCUMENT_URIS
+ describe("diagnostic tracking isolation", () => {
+ it("does not leak opened cell state between notebook clients", async () => {
await notebookClient.textDocumentDidOpen({
textDocument: {
uri: CellDocumentUri.of(Cells.cell1),
@@ -1421,37 +1859,41 @@ describe("NotebookLanguageServerClient", () => {
},
});
- await notebookClient.textDocumentDidOpen({
- textDocument: {
- uri: CellDocumentUri.of(Cells.cell2),
- languageId: "python",
+ const forwardedNotification = vi.fn();
+ const isolatedClient: Mocked<TestLanguageServerClient> = {
+ ...mockClient,
+ processNotification: forwardedNotification,
+ notify: vi.fn(),
+ };
+ const isolatedNotebookClient = new NotebookLanguageServerClient(
+ isolatedClient,
+ {},
+ );
+ await isolatedNotebookClient.sync();
+ isolatedNotebookClient.patchProcessNotification();
+
+ isolatedClient.processNotification({
+ method: "textDocument/publishDiagnostics",
+ params: {
+ uri: "file:///project/__marimo_notebook__.py",
version: 1,
- text: "code2",
+ diagnostics: [],
},
});
- // Verify cells were added
- expect(seenUris.has(CellDocumentUri.of(Cells.cell1))).toBe(true);
- expect(seenUris.has(CellDocumentUri.of(Cells.cell2))).toBe(true);
- expect(seenUris.size).toBe(2);
- });
-
- it("should have pruneSeenCellUris static method", () => {
- // Verify the method exists and is callable
- expect(
- typeof (NotebookLanguageServerClient as any).pruneSeenCellUris,
- ).toBe("function");
+ expect(forwardedNotification).not.toHaveBeenCalled();
});
});
describe("initialization and configuration", () => {
it("should send configuration after initialization", async () => {
- const configNotifications: any[] = [];
- (mockClient as any).notify = vi
- .fn()
- .mockImplementation((method, params) => {
- configNotifications.push({ method, params });
- });
+ const configNotifications: Array<{
+ method: string;
+ params: unknown;
+ }> = [];
+ mockClient.notify = vi.fn().mockImplementation(async (method, params) => {
+ configNotifications.push({ method, params });
+ });
// Create a new client to trigger initialization
const client = new NotebookLanguageServerClient(mockClient, {});
diff --git a/frontend/src/core/codemirror/lsp/__tests__/transports.test.ts b/frontend/src/core/codemirror/lsp/__tests__/transports.test.ts
new file mode 100644
index 0000000000..7335b1bbc3
--- /dev/null
+++ b/frontend/src/core/codemirror/lsp/__tests__/transports.test.ts
@@ -0,0 +1,112 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { initialNotebookState, notebookAtom } from "@/core/cells/cells";
+import { store } from "@/core/state/jotai";
+import { createTransport } from "../transports";
+
+vi.mock("../../../network/connection", () => ({
+ waitForConnectionOpen: vi.fn(() => Promise.resolve()),
+}));
+
+vi.mock("../../../runtime/config", () => ({
+ getRuntimeManager: () => ({
+ getLSPURL: (server: string) => new URL(`ws://localhost:2718/lsp/${server}`),
+ }),
+}));
+
+/** Minimal stand-in for the browser WebSocket used by `WebSocketTransport`. */
+class FakeWebSocket {
+ static readonly instances: FakeWebSocket[] = [];
+ static readonly OPEN = 1;
+
+ readyState = 0;
+ readonly url: string;
+ private readonly listeners = new Map<string, Set<(event: unknown) => void>>();
+
+ constructor(url: string) {
+ this.url = url;
+ FakeWebSocket.instances.push(this);
+ }
+
+ addEventListener(type: string, listener: (event: unknown) => void) {
+ const listeners = this.listeners.get(type) ?? new Set();
+ listeners.add(listener);
+ this.listeners.set(type, listeners);
+ }
+
+ send() {
+ // no-op
+ }
+
+ close() {
+ this.readyState = 3;
+ }
+
+ open() {
+ this.readyState = FakeWebSocket.OPEN;
+ this.emit("open", {});
+ }
+
+ receive(message: unknown) {
+ this.emit("message", { data: JSON.stringify(message) });
+ }
+
+ private emit(type: string, event: unknown) {
+ for (const listener of this.listeners.get(type) ?? []) {
+ listener(event);
+ }
+ }
+}
+
+describe("createTransport", () => {
+ beforeEach(() => {
+ FakeWebSocket.instances.length = 0;
+ vi.stubGlobal("WebSocket", FakeWebSocket);
+ vi.spyOn(console, "log").mockImplementation(() => undefined);
+ store.set(notebookAtom, initialNotebookState());
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it("forwards window/logMessage notifications to the logs panel", async () => {
+ const transport = createTransport("pylsp");
+ const connected = transport.connect();
+ await vi.waitFor(() => expect(FakeWebSocket.instances).toHaveLength(1));
+ const socket = FakeWebSocket.instances[0];
+ socket.open();
+ await connected;
+
+ socket.receive({
+ jsonrpc: "2.0",
+ method: "window/logMessage",
+ params: { type: 1, message: "pylsp failed to start" },
+ });
+ socket.receive({
+ jsonrpc: "2.0",
+ method: "window/logMessage",
+ params: { type: 3, message: "indexing workspace" },
+ });
+ // Unrelated notifications are left alone.
+ socket.receive({
+ jsonrpc: "2.0",
+ method: "textDocument/publishDiagnostics",
+ params: { uri: "file:///a", diagnostics: [] },
+ });
+
+ expect(store.get(notebookAtom).cellLogs).toMatchObject([
+ {
+ level: "stderr",
+ message: "pylsp failed to start",
+ source: { type: "lsp", name: "pylsp" },
+ },
+ {
+ level: "stdout",
+ message: "indexing workspace",
+ source: { type: "lsp", name: "pylsp" },
+ },
+ ]);
+ });
+});
diff --git a/frontend/src/core/codemirror/lsp/federated-lsp.ts b/frontend/src/core/codemirror/lsp/federated-lsp.ts
index 8aa9cea493..d4ff924c17 100644
--- a/frontend/src/core/codemirror/lsp/federated-lsp.ts
+++ b/frontend/src/core/codemirror/lsp/federated-lsp.ts
@@ -2,7 +2,7 @@
import type * as LSP from "vscode-languageserver-protocol";
import { Objects } from "@/utils/objects";
-import type { ILanguageServerClient } from "./types";
+import type { ILanguageServerClient, RoutableLspMethod } from "./types";
import { getLspDocumentUri } from "./utils";
function removeFalseyValues<T extends object>(obj: T): T {
@@ -54,7 +54,7 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
}
return undefined;
})
- .filter((c) => c != null);
+ .filter((c): c is LSP.ClientCapabilities => c != null);
return mergeDictsIgnoreFalsey<LSP.ClientCapabilities>(capabilities);
}
@@ -72,7 +72,7 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
get capabilities(): LSP.ServerCapabilities | null {
const capabilities = this.clients
.map((client) => client.capabilities)
- .filter((c) => c !== null);
+ .filter((c): c is LSP.ServerCapabilities => c !== null);
return mergeDictsIgnoreFalsey<LSP.ServerCapabilities>(capabilities);
}
@@ -92,16 +92,20 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
});
}
- private firstWithCapability(
- capability: keyof LSP.ServerCapabilities,
+ private firstWithMethod(
+ method: RoutableLspMethod,
): ILanguageServerClient | undefined {
- return this.clients.find((client) => client.capabilities?.[capability]);
+ return this.clients.find((client) => client.hasCapability(method));
}
- private clientsWithCapability(
- capability: keyof LSP.ServerCapabilities,
+ private clientsWithMethod(
+ method: RoutableLspMethod,
): ILanguageServerClient[] {
- return this.clients.filter((client) => client.capabilities?.[capability]);
+ return this.clients.filter((client) => client.hasCapability(method));
+ }
+
+ hasCapability(method: string): boolean {
+ return this.clients.some((client) => client.hasCapability(method));
}
async initialize(): Promise<void> {
@@ -114,18 +118,16 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
async textDocumentDidChange(
params: LSP.DidChangeTextDocumentParams,
- ): Promise<LSP.DidChangeTextDocumentParams> {
+ ): Promise<void> {
await Promise.all(
this.clients.map((client) => client.textDocumentDidChange(params)),
);
-
- return params;
}
async completionItemResolve(
item: LSP.CompletionItem,
): Promise<LSP.CompletionItem> {
- const client = this.firstWithCapability("completionProvider");
+ const client = this.firstWithMethod("textDocument/completion");
if (client) {
return client.completionItemResolve(item);
}
@@ -135,17 +137,22 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
async textDocumentCodeAction(
params: LSP.CodeActionParams,
): Promise<(LSP.Command | LSP.CodeAction)[] | null> {
- const client = this.firstWithCapability("codeActionProvider");
+ const client = this.firstWithMethod("textDocument/codeAction");
if (client) {
return client.textDocumentCodeAction(params);
}
return null;
}
+ async codeActionResolve(action: LSP.CodeAction): Promise<LSP.CodeAction> {
+ const client = this.firstWithMethod("textDocument/codeAction");
+ return client ? client.codeActionResolve(action) : action;
+ }
+
async textDocumentRename(
params: LSP.RenameParams,
): Promise<LSP.WorkspaceEdit | null> {
- const client = this.firstWithCapability("renameProvider");
+ const client = this.firstWithMethod("textDocument/rename");
if (client) {
return client.textDocumentRename(params);
}
@@ -155,7 +162,7 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
async textDocumentPrepareRename(
params: LSP.PrepareRenameParams,
): Promise<LSP.PrepareRenameResult | null> {
- const client = this.firstWithCapability("renameProvider");
+ const client = this.firstWithMethod("textDocument/prepareRename");
if (client) {
return client.textDocumentPrepareRename(params);
}
@@ -165,7 +172,7 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
async textDocumentSignatureHelp(
params: LSP.SignatureHelpParams,
): Promise<LSP.SignatureHelp | null> {
- const client = this.firstWithCapability("signatureHelpProvider");
+ const client = this.firstWithMethod("textDocument/signatureHelp");
if (client) {
return client.textDocumentSignatureHelp(params);
}
@@ -176,7 +183,7 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
async textDocumentCompletion(
params: LSP.CompletionParams,
): Promise<LSP.CompletionList | LSP.CompletionItem[] | null> {
- const clients = this.clientsWithCapability("completionProvider");
+ const clients = this.clientsWithMethod("textDocument/completion");
const results = await Promise.allSettled(
clients.map((client) => client.textDocumentCompletion(params)),
);
@@ -187,24 +194,70 @@ export class FederatedLanguageServerClient implements ILanguageServerClient {
async textDocumentDefinition(
params: LSP.DefinitionParams,
): Promise<LSP.Definition | LSP.LocationLink[] | null> {
- const client = this.firstWithCapability("definitionProvider");
+ const client = this.firstWithMethod("textDocument/definition");
if (client) {
return client.textDocumentDefinition(params);
}
return null;
}
- async textDocumentDidOpen(params: LSP.DidOpenTextDocumentParams) {
- await Promise.all(
+ async textDocumentDidOpen(
+ params: LSP.DidOpenTextDocumentParams,
+ ): Promise<boolean> {
+ const results = await Promise.all(
this.clients.map((client) => client.textDocumentDidOpen(params)),
);
- return params;
+ return results.some((result) => result !== false);
+ }
+
+ async textDocumentDidClose(
+ params: LSP.DidCloseTextDocumentParams,
+ ): Promise<void> {
+ await Promise.all(
+ this.clients.map((client) => client.textDocumentDidClose(params)),
+ );
+ }
+
+ async textDocumentWillSave(
+ params: LSP.WillSaveTextDocumentParams,
+ ): Promise<void> {
+ await Promise.all(
+ this.clients.map((client) => client.textDocumentWillSave(params)),
+ );
+ }
+
+ async textDocumentWillSaveWaitUntil(
+ params: LSP.WillSaveTextDocumentParams,
+ ): Promise<LSP.TextEdit[] | null> {
+ // This blocks the save, so query concurrently rather than paying the sum of
+ // the servers' latencies. The earliest client that returned edits still
+ // wins, so the result doesn't depend on who answers first.
+ const results = await Promise.allSettled(
+ this.clients.map((client) =>
+ client.textDocumentWillSaveWaitUntil(params),
+ ),
+ );
+
+ for (const result of results) {
+ if (result.status === "fulfilled" && result.value?.length) {
+ return result.value;
+ }
+ }
+ return null;
+ }
+
+ async textDocumentDidSave(
+ params: LSP.DidSaveTextDocumentParams,
+ ): Promise<void> {
+ await Promise.all(
+ this.clients.map((client) => client.textDocumentDidSave(params)),
+ );
}
async textDocumentHover(params: LSP.HoverParams): Promise<LSP.Hover> {
for (const client of this.clients) {
- if (!client.capabilities?.hoverProvider) {
+ if (!client.hasCapability("textDocument/hover")) {
continue;
}
const result = await client.textDocumentHover(params);
diff --git a/frontend/src/core/codemirror/lsp/log-messages.ts b/frontend/src/core/codemirror/lsp/log-messages.ts
new file mode 100644
index 0000000000..700fc93302
--- /dev/null
+++ b/frontend/src/core/codemirror/lsp/log-messages.ts
@@ -0,0 +1,37 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type { JSONRPCMessage } from "@marimo-team/codemirror-languageserver";
+import { addLogs } from "@/core/cells/cells";
+import { isRecord } from "@/utils/records";
+
+// LSP MessageType: 1 = Error, 2 = Warning, 3 = Info, 4 = Log, 5 = Debug
+const ERROR_MESSAGE_TYPES = new Set<unknown>([1, 2]);
+
+/**
+ * Forward a server's `window/logMessage` notifications to the Logs panel.
+ * Servers report failures they can't surface as diagnostics this way, such as
+ * a missing interpreter or a plugin that failed to load.
+ */
+export function handleLogMessage(
+ serverName: string,
+ message: JSONRPCMessage,
+): void {
+ if (!("method" in message) || message.method !== "window/logMessage") {
+ return;
+ }
+ const params: unknown = message.params;
+ if (!isRecord(params) || typeof params.message !== "string") {
+ return;
+ }
+
+ addLogs({
+ logs: [
+ {
+ timestamp: Date.now() / 1000,
+ level: ERROR_MESSAGE_TYPES.has(params.type) ? "stderr" : "stdout",
+ message: params.message,
+ source: { type: "lsp", name: serverName },
+ },
+ ],
+ });
+}
diff --git a/frontend/src/core/codemirror/lsp/normalize-markdown-math.ts b/frontend/src/core/codemirror/lsp/normalize-markdown-math.ts
index 7d16229360..c27c3b711f 100644
--- a/frontend/src/core/codemirror/lsp/normalize-markdown-math.ts
+++ b/frontend/src/core/codemirror/lsp/normalize-markdown-math.ts
@@ -6,8 +6,10 @@ import type * as LSP from "vscode-languageserver-protocol";
const INLINE_MATH_ROLE_REGEX = /(?<!`):math:`([^\n`]+)`/g;
// Matches role variant with HTML code tag: :math:<code>...</code>
const INLINE_MATH_ROLE_CODE_REGEX = /:math:\s*<code>(.+?)<\/code>/gs;
-// Matches display bracket delimiters: \[...\]
-const DISPLAY_BRACKET_REGEX = /\\\[(.+?)\\]/gs;
+// Matches display bracket delimiters: \[...\]. Must start a line or follow
+// whitespace, so escaped brackets in type annotations (`Optional\[Callable\]`)
+// are not read as math.
+const DISPLAY_BRACKET_REGEX = /(?<!\S)\\\[(.+?)\\]/gs;
// Matches inline paren delimiters: \(...\)
const INLINE_PAREN_REGEX = /\\\((.+?)\\\)/gs;
// Matches display dollar delimiters: $$...$$
@@ -36,6 +38,8 @@ export function normalizeMarkdownMath(markdown: string): string {
return markdown;
}
+ // Segments are split on line boundaries, so the newline between two of them
+ // belongs to neither and has to be restored on join.
return splitByFencedCodeBlocks(markdown)
.map((segment) => {
if (segment.isFenced) {
@@ -55,7 +59,7 @@ export function normalizeMarkdownMath(markdown: string): string {
})
.join("");
})
- .join("");
+ .join("\n");
}
/**
@@ -132,7 +136,7 @@ function containsMathSyntax(markdown: string): boolean {
markdown.includes(".. math::") ||
markdown.includes(":math:`") ||
// Detects display bracket delimiters.
- /\\\[[\S\s]+?\\]/.test(markdown) ||
+ /(?<!\S)\\\[[\S\s]+?\\]/.test(markdown) ||
// Detects inline paren delimiters.
/\\\([\S\s]+?\\\)/.test(markdown) ||
// Detects display dollar delimiters.
diff --git a/frontend/src/core/codemirror/lsp/notebook-lsp.ts b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
index 00ca4ea2fc..fdf31f018e 100644
--- a/frontend/src/core/codemirror/lsp/notebook-lsp.ts
+++ b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
@@ -9,6 +9,7 @@ import { invariant } from "@/utils/invariant";
import { Logger } from "@/utils/Logger";
import { LRUCache } from "@/utils/lru";
import { Objects } from "@/utils/objects";
+import { isRecord } from "@/utils/records";
import { getPositionAtWordBounds } from "../completion/hints";
import { topologicalCodesAtom } from "../copilot/getCodes";
import {
@@ -19,8 +20,10 @@ import { createNotebookLens, type NotebookLens } from "./lens";
import { normalizeLspDocumentation } from "./normalize-markdown-math";
import {
CellDocumentUri,
+ type ClientNotification,
type ILanguageServerClient,
isClientWithNotify,
+ isClientWithProcessNotification,
} from "./types";
import { getLspDocumentUri } from "./utils";
@@ -140,6 +143,47 @@ function normalizeSignatureHelpResponse(
};
}
+interface PublishDiagnosticsNotification {
+ method: "textDocument/publishDiagnostics";
+ params: LSP.PublishDiagnosticsParams;
+}
+
+function isPosition(value: unknown): value is LSP.Position {
+ return (
+ isRecord(value) &&
+ typeof value.line === "number" &&
+ typeof value.character === "number"
+ );
+}
+
+function isRange(value: unknown): value is LSP.Range {
+ return isRecord(value) && isPosition(value.start) && isPosition(value.end);
+}
+
+function isDiagnostic(value: unknown): value is LSP.Diagnostic {
+ return (
+ isRecord(value) && typeof value.message === "string" && isRange(value.range)
+ );
+}
+
+function isPublishDiagnosticsNotification(
+ notification: ClientNotification,
+): notification is PublishDiagnosticsNotification {
+ if (
+ notification.method !== "textDocument/publishDiagnostics" ||
+ !isRecord(notification.params)
+ ) {
+ return false;
+ }
+ const { uri, version, diagnostics } = notification.params;
+ return (
+ typeof uri === "string" &&
+ (version === undefined || typeof version === "number") &&
+ Array.isArray(diagnostics) &&
+ diagnostics.every(isDiagnostic)
+ );
+}
+
export class NotebookLanguageServerClient implements ILanguageServerClient {
public readonly documentUri: LSP.DocumentUri;
private readonly client: ILanguageServerClient;
@@ -156,17 +200,17 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
* This set is pruned when diagnostics are processed to only include
* cells that exist in the current notebook snapshot.
*/
- private static readonly SEEN_CELL_DOCUMENT_URIS = new Set<CellDocumentUri>();
+ private readonly seenCellDocumentUris = new Set<CellDocumentUri>();
/**
* Remove cell URIs that are no longer in the notebook.
* Called during diagnostic processing to prevent memory leaks.
*/
- private static pruneSeenCellUris(currentCellIds: Set<CellId>): void {
- for (const uri of NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS) {
+ private pruneSeenCellUris(currentCellIds: Set<CellId>): void {
+ for (const uri of this.seenCellDocumentUris) {
const cellId = CellDocumentUri.parse(uri);
if (!currentCellIds.has(cellId)) {
- NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS.delete(uri);
+ this.seenCellDocumentUris.delete(uri);
}
}
}
@@ -178,6 +222,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
string,
Promise<LSP.CompletionItem>
>(10);
+ private readonly openCellDocumentCounts = new Map<CellDocumentUri, number>();
private latestDiagnosticsVersion: number | null = null;
private forwardedDiagnosticsVersion = 0;
@@ -201,7 +246,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
isClientWithNotify(this.client),
"notify is not a method on the client",
);
- this.client.notify("workspace/didChangeConfiguration", {
+ void this.client.notify("workspace/didChangeConfiguration", {
settings: initialSettings,
});
});
@@ -253,9 +298,14 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
}
close(): void {
+ this.openCellDocumentCounts.clear();
this.client.close();
}
+ hasCapability(method: string): boolean {
+ return this.client.hasCapability(method);
+ }
+
/**
* Re-synchronize all open documents with the LSP server.
* This is called after a WebSocket reconnection to restore document state.
@@ -266,7 +316,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
"notify is not a method on the client",
);
await this.client.initialize();
- this.client.notify("workspace/didChangeConfiguration", {
+ await this.client.notify("workspace/didChangeConfiguration", {
settings: this.initialSettings,
});
@@ -275,16 +325,19 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
this.latestDiagnosticsVersion = null;
this.forwardedDiagnosticsVersion = 0;
- // Re-open the merged document with the LSP server
- // This sends a textDocument/didOpen for the entire notebook
- await this.client.textDocumentDidOpen({
- textDocument: {
- languageId: "python", // Default to Python for marimo notebooks
- text: lens.mergedText,
- uri: this.documentUri,
- version: version,
- },
- });
+ // The upstream client preserves its didOpen reference counts across a
+ // transport reconnect. Send the re-open notification directly so the new
+ // server session receives the document without incrementing those counts.
+ if (this.openCellDocumentCounts.size > 0) {
+ await this.client.notify("textDocument/didOpen", {
+ textDocument: {
+ languageId: "python",
+ text: lens.mergedText,
+ uri: this.documentUri,
+ version: version,
+ },
+ });
+ }
Logger.log("[lsp] Document re-synchronization complete");
}
@@ -309,29 +362,67 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
const { lens, version } = this.snapshotter.snapshot();
- NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS.add(cellDocumentUri);
+ const previousOpenCount =
+ this.openCellDocumentCounts.get(cellDocumentUri) ?? 0;
+ this.openCellDocumentCounts.set(cellDocumentUri, previousOpenCount + 1);
// Pass merged doc to LSP
- const result = await this.client.textDocumentDidOpen({
- textDocument: {
- languageId: params.textDocument.languageId,
- text: lens.mergedText,
- uri: this.documentUri,
- version: version,
- },
- });
+ try {
+ const result = await this.client.textDocumentDidOpen({
+ textDocument: {
+ languageId: params.textDocument.languageId,
+ text: lens.mergedText,
+ uri: this.documentUri,
+ version: version,
+ },
+ });
- if (!result) {
- return params;
+ // Only mark the cell as seen once the open actually succeeds.
+ this.seenCellDocumentUris.add(cellDocumentUri);
+ return result !== false;
+ } catch (error) {
+ // Roll back this call's own increment. Use the *current* count rather
+ // than `previousOpenCount`: a concurrent open for the same cell may
+ // have incremented it while this call was in flight, and resetting to
+ // the stale snapshot would erase that other, possibly-successful
+ // reference.
+ const currentOpenCount =
+ this.openCellDocumentCounts.get(cellDocumentUri) ?? 0;
+ if (currentOpenCount <= 1) {
+ this.openCellDocumentCounts.delete(cellDocumentUri);
+ } else {
+ this.openCellDocumentCounts.set(cellDocumentUri, currentOpenCount - 1);
+ }
+ throw error;
}
+ }
- return {
- ...result,
- textDocument: {
- ...result.textDocument,
- text: lens.mergedText,
- },
- };
+ public async textDocumentDidClose(
+ params: LSP.DidCloseTextDocumentParams,
+ ): Promise<void> {
+ const cellDocumentUri = params.textDocument.uri;
+ this.assertCellDocumentUri(cellDocumentUri);
+
+ const previousOpenCount =
+ this.openCellDocumentCounts.get(cellDocumentUri) ?? 0;
+ if (previousOpenCount === 0) {
+ // No matching open was ever recorded for this cell; ignore rather
+ // than forwarding a close that isn't ours to send.
+ Logger.warn(
+ "[lsp] textDocumentDidClose with no tracked open",
+ cellDocumentUri,
+ );
+ return;
+ }
+ if (previousOpenCount <= 1) {
+ this.openCellDocumentCounts.delete(cellDocumentUri);
+ } else {
+ this.openCellDocumentCounts.set(cellDocumentUri, previousOpenCount - 1);
+ }
+
+ await this.client.textDocumentDidClose({
+ textDocument: { uri: this.documentUri },
+ });
}
/**
@@ -360,18 +451,52 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
return { params, lens };
}
- public async textDocumentDidChange(params: LSP.DidChangeTextDocumentParams) {
+ public async textDocumentDidChange(
+ params: LSP.DidChangeTextDocumentParams,
+ ): Promise<void> {
Logger.debug("[lsp] textDocumentDidChange", params);
// We know how to only handle single content changes
// But that is all we expect to receive
if (params.contentChanges.length === 1) {
- const { params: syncParams } = await this.sync();
- return syncParams;
+ await this.sync();
+ return;
}
Logger.warn("[lsp] Unhandled textDocumentDidChange", params);
- return this.client.textDocumentDidChange(params);
+ await this.client.textDocumentDidChange(params);
+ }
+
+ public async textDocumentWillSave(
+ params: LSP.WillSaveTextDocumentParams,
+ ): Promise<void> {
+ this.assertCellDocumentUri(params.textDocument.uri);
+ await this.client.textDocumentWillSave({
+ ...params,
+ textDocument: { uri: this.documentUri },
+ });
+ }
+
+ public async textDocumentWillSaveWaitUntil(
+ params: LSP.WillSaveTextDocumentParams,
+ ): Promise<LSP.TextEdit[] | null> {
+ this.assertCellDocumentUri(params.textDocument.uri);
+ // See textDocumentCodeAction for why merged-document edits are unsafe
+ Logger.debug(
+ "[lsp] willSaveWaitUntil edits are disabled for notebook documents",
+ );
+ return null;
+ }
+
+ public async textDocumentDidSave(
+ params: LSP.DidSaveTextDocumentParams,
+ ): Promise<void> {
+ this.assertCellDocumentUri(params.textDocument.uri);
+ const { lens } = this.snapshotter.snapshot();
+ await this.client.textDocumentDidSave({
+ textDocument: { uri: this.documentUri },
+ text: params.text === undefined ? undefined : lens.mergedText,
+ });
}
async textDocumentDefinition(
@@ -457,6 +582,51 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
return this.client.textDocumentCodeAction(params);
}
+ codeActionResolve(action: LSP.CodeAction): Promise<LSP.CodeAction> {
+ return Promise.resolve(action);
+ }
+
+ /**
+ * Map a diagnostic's related-information entries ("declared here", ...) out
+ * of merged-document coordinates. Entries pointing into the notebook are
+ * rewritten to the owning cell; entries pointing at files on disk are left
+ * for the host to open.
+ */
+ private reverseRelatedInformation(
+ relatedInformation: LSP.DiagnosticRelatedInformation[] | undefined,
+ lens: NotebookLens,
+ ): LSP.DiagnosticRelatedInformation[] | undefined {
+ if (!relatedInformation) {
+ return undefined;
+ }
+
+ return relatedInformation.map((related) => {
+ // `isDiagnostic` does not validate related information, so a server may
+ // hand us an entry that isn't an object or has no usable location.
+ if (
+ !isRecord(related) ||
+ !isRecord(related.location) ||
+ related.location.uri !== this.documentUri ||
+ !isRange(related.location.range)
+ ) {
+ return related;
+ }
+ const cellId = lens.cellIds.find((id) =>
+ lens.isInRange(related.location.range, id),
+ );
+ if (cellId === undefined) {
+ return related;
+ }
+ return {
+ ...related,
+ location: {
+ uri: CellDocumentUri.of(cellId),
+ range: lens.reverseRange(related.location.range, cellId),
+ },
+ };
+ });
+ }
+
/**
* Rename implementation with manual edit application.
*
@@ -754,24 +924,16 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
*/
public patchProcessNotification() {
invariant(
- "processNotification" in this.client,
+ isClientWithProcessNotification(this.client),
"processNotification is not a method on the client",
);
- // @ts-expect-error: processNotification is private
const previousProcessNotification = this.client.processNotification.bind(
this.client,
);
- const processNotification = (
- notification:
- | {
- method: "textDocument/publishDiagnostics";
- params: LSP.PublishDiagnosticsParams;
- }
- | { method: "other"; params: unknown },
- ) => {
- if (notification.method === "textDocument/publishDiagnostics") {
+ const processNotification = (notification: ClientNotification) => {
+ if (isPublishDiagnosticsNotification(notification)) {
const incomingVersion = notification.params.version;
if (incomingVersion != null) {
const latestVersion = this.latestDiagnosticsVersion;
@@ -811,12 +973,16 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
if (!diagnosticsByCellId.has(cellId)) {
diagnosticsByCellId.set(cellId, []);
}
- const cellDiag = {
+ const relatedInformation = this.reverseRelatedInformation(
+ diag.relatedInformation,
+ lens,
+ );
+ const cellDiag: LSP.Diagnostic = {
...diag,
range: lens.reverseRange(diag.range, cellId),
+ ...(relatedInformation ? { relatedInformation } : {}),
};
- // oxlint-disable-next-line typescript/no-non-null-assertion
- diagnosticsByCellId.get(cellId)!.push(cellDiag);
+ diagnosticsByCellId.get(cellId)?.push(cellDiag);
break; // Exit inner loop once we find the matching cell
}
}
@@ -824,11 +990,9 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
// Prune any cell URIs for cells that no longer exist
const currentCellIds = new Set(lens.cellIds);
- NotebookLanguageServerClient.pruneSeenCellUris(currentCellIds);
+ this.pruneSeenCellUris(currentCellIds);
- const cellsToClear = new Set(
- NotebookLanguageServerClient.SEEN_CELL_DOCUMENT_URIS,
- );
+ const cellsToClear = new Set(this.seenCellDocumentUris);
// Process each cell's diagnostics
for (const [cellId, cellDiagnostics] of diagnosticsByCellId.entries()) {
diff --git a/frontend/src/core/codemirror/lsp/transports.ts b/frontend/src/core/codemirror/lsp/transports.ts
index 77f357158e..8d76eb7700 100644
--- a/frontend/src/core/codemirror/lsp/transports.ts
+++ b/frontend/src/core/codemirror/lsp/transports.ts
@@ -2,6 +2,7 @@
import { ReconnectingWebSocketTransport } from "@/core/lsp/transport";
import { waitForConnectionOpen } from "../../network/connection";
import { getRuntimeManager } from "../../runtime/config";
+import { handleLogMessage } from "./log-messages";
/**
* Create a transport for a given LSP server.
@@ -18,11 +19,17 @@ export function createTransport(
onReconnect?: () => Promise<void>,
) {
const runtimeManager = getRuntimeManager();
- return new ReconnectingWebSocketTransport({
+ const transport = new ReconnectingWebSocketTransport({
getWsUrl: () => runtimeManager.getLSPURL(serverName).toString(),
waitForConnection: async () => {
await waitForConnectionOpen();
},
onReconnect,
});
+
+ // Handled on the transport, not the client, so that logs are captured before
+ // the client initializes — which is when startup failures are reported.
+ transport.onMessage((message) => handleLogMessage(serverName, message));
+
+ return transport;
}
diff --git a/frontend/src/core/codemirror/lsp/types.ts b/frontend/src/core/codemirror/lsp/types.ts
index eb7bc0fa1f..3fbcfc46c3 100644
--- a/frontend/src/core/codemirror/lsp/types.ts
+++ b/frontend/src/core/codemirror/lsp/types.ts
@@ -6,9 +6,56 @@ import type { CellId } from "@/core/cells/ids";
import { invariant } from "@/utils/invariant";
import type { TypedString } from "@/utils/typed";
-export type ILanguageServerClient = {
- [key in keyof LanguageServerClient]: LanguageServerClient[key];
-};
+type LanguageServerClientMember =
+ | "ready"
+ | "capabilities"
+ | "initializePromise"
+ | "clientCapabilities"
+ | "initialize"
+ | "close"
+ | "hasCapability"
+ | "textDocumentDidOpen"
+ | "textDocumentDidChange"
+ | "textDocumentDidClose"
+ | "textDocumentWillSave"
+ | "textDocumentWillSaveWaitUntil"
+ | "textDocumentDidSave"
+ | "textDocumentHover"
+ | "textDocumentCompletion"
+ | "completionItemResolve"
+ | "textDocumentDefinition"
+ | "textDocumentCodeAction"
+ | "codeActionResolve"
+ | "textDocumentRename"
+ | "textDocumentPrepareRename"
+ | "textDocumentSignatureHelp"
+ | "onNotification";
+
+/**
+ * The stable client surface used by marimo's notebook and federated adapters.
+ * Keeping this explicit prevents unrelated additions to the upstream client
+ * from becoming required adapter methods.
+ */
+export type ILanguageServerClient = Pick<
+ LanguageServerClient,
+ LanguageServerClientMember
+>;
+
+/**
+ * LSP request methods that `FederatedLanguageServerClient` routes to whichever
+ * child client declares support.
+ *
+ * This is the subset of `ILanguageServerClient`'s request methods
+ * https://github.com/marimo-team/codemirror-languageserver/blob/v2.0.0/src/lsp.ts#L88-L113
+ */
+export type RoutableLspMethod =
+ | "textDocument/hover"
+ | "textDocument/completion"
+ | "textDocument/definition"
+ | "textDocument/codeAction"
+ | "textDocument/rename"
+ | "textDocument/prepareRename"
+ | "textDocument/signatureHelp";
export type CellDocumentUri = DocumentUri & TypedString<"CellDocumentUri">;
@@ -33,10 +80,23 @@ export const CellDocumentUri = {
export function isClientWithNotify(
client: ILanguageServerClient,
): client is ILanguageServerClient & {
- notify: (
- kind: string,
- options: { settings: Record<string, unknown> },
- ) => void;
+ notify: (method: string, params: unknown) => Promise<void>;
+} {
+ return "notify" in client && typeof client.notify === "function";
+}
+
+export interface ClientNotification {
+ method: string;
+ params: unknown;
+}
+
+export function isClientWithProcessNotification(
+ client: ILanguageServerClient,
+): client is ILanguageServerClient & {
+ processNotification: (notification: ClientNotification) => void;
} {
- return "notify" in client;
+ return (
+ "processNotification" in client &&
+ typeof client.processNotification === "function"
+ );
}
diff --git a/frontend/src/core/codemirror/lsp/utils.ts b/frontend/src/core/codemirror/lsp/utils.ts
index fa7e74897c..78a123c9f3 100644
--- a/frontend/src/core/codemirror/lsp/utils.ts
+++ b/frontend/src/core/codemirror/lsp/utils.ts
@@ -1,6 +1,20 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import { cellIdsAtom } from "@/core/cells/cells";
import { lspWorkspaceAtom } from "@/core/saving/file-state";
import { store } from "@/core/state/jotai";
+import { CellDocumentUri } from "./types";
+
+/**
+ * `CellDocumentUri.is` only tests the `file:///` prefix, which every absolute
+ * file URI shares, so on its own it cannot tell a cell from a real file on
+ * disk. Resolve the URI against the open notebook instead.
+ */
+export function isKnownCellDocumentUri(uri: string): boolean {
+ if (!CellDocumentUri.is(uri)) {
+ return false;
+ }
+ return store.get(cellIdsAtom).inOrderIds.includes(CellDocumentUri.parse(uri));
+}
export function getLspRootUri() {
const lspWorkspace = store.get(lspWorkspaceAtom);
diff --git a/frontend/src/core/lsp/__tests__/transport.test.ts b/frontend/src/core/lsp/__tests__/transport.test.ts
index df6cba6741..d726eb1b00 100644
--- a/frontend/src/core/lsp/__tests__/transport.test.ts
+++ b/frontend/src/core/lsp/__tests__/transport.test.ts
@@ -1,439 +1,611 @@
/* Copyright 2026 Marimo. All rights reserved. */
-/* oxlint-disable typescript/no-explicit-any */
-
-import { WebSocketTransport } from "@open-rpc/client-js";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { JSONRPCMessage } from "@marimo-team/codemirror-languageserver";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Mocks } from "@/__mocks__/common";
+import { isRecord } from "@/utils/records";
import { ReconnectingWebSocketTransport } from "../transport";
-// Mock the Logger
vi.mock("@/utils/Logger", () => ({
Logger: Mocks.logger(),
}));
-// Mock the WebSocketTransport
-vi.mock("@open-rpc/client-js", () => {
- const mockWebSocketTransport = vi.fn();
- mockWebSocketTransport.prototype.connect = vi.fn();
- mockWebSocketTransport.prototype.close = vi.fn();
- mockWebSocketTransport.prototype.sendData = vi.fn();
-
- return {
- WebSocketTransport: mockWebSocketTransport,
- };
-});
+class FakeWebSocket {
+ static readonly CONNECTING = 0;
+ static readonly OPEN = 1;
+ static readonly CLOSING = 2;
+ static readonly CLOSED = 3;
+ static readonly instances: FakeWebSocket[] = [];
+
+ readonly sent: string[] = [];
+ readonly url: string;
+ readonly protocols: string | string[] | undefined;
+ readyState = FakeWebSocket.CONNECTING;
+ private readonly listeners = new Map<
+ string,
+ Set<(event: { data?: unknown }) => void>
+ >();
+
+ constructor(url: string, protocols?: string | string[]) {
+ this.url = url;
+ this.protocols = protocols;
+ FakeWebSocket.instances.push(this);
+ }
+
+ addEventListener(
+ type: string,
+ listener: (event: { data?: unknown }) => void,
+ ) {
+ const listeners = this.listeners.get(type) ?? new Set();
+ listeners.add(listener);
+ this.listeners.set(type, listeners);
+ }
+
+ send(frame: string) {
+ this.sent.push(frame);
+ }
+
+ close() {
+ this.readyState = FakeWebSocket.CLOSED;
+ this.emit("close", {});
+ }
+
+ open() {
+ this.readyState = FakeWebSocket.OPEN;
+ this.emit("open", {});
+ }
+
+ fail() {
+ this.emit("error", {});
+ }
+
+ disconnect() {
+ this.readyState = FakeWebSocket.CLOSED;
+ this.emit("close", {});
+ }
+
+ receive(message: JSONRPCMessage) {
+ this.emit("message", { data: JSON.stringify(message) });
+ }
+
+ messages(): JSONRPCMessage[] {
+ return this.sent.map((frame) => {
+ const message: unknown = JSON.parse(frame);
+ if (!isJSONRPCMessage(message)) {
+ throw new Error("Fake WebSocket received an invalid JSON-RPC message");
+ }
+ return message;
+ });
+ }
+
+ private emit(type: string, event: { data?: unknown }) {
+ for (const listener of this.listeners.get(type) ?? []) {
+ listener(event);
+ }
+ }
+}
+
+function isJSONRPCMessage(value: unknown): value is JSONRPCMessage {
+ if (!isRecord(value) || value.jsonrpc !== "2.0") {
+ return false;
+ }
+ if (typeof value.method === "string") {
+ return true;
+ }
+ return (
+ (typeof value.id === "string" || typeof value.id === "number") &&
+ ("result" in value || "error" in value)
+ );
+}
+
+async function getSocket(index: number): Promise<FakeWebSocket> {
+ await vi.waitFor(() => {
+ expect(FakeWebSocket.instances.length).toBeGreaterThan(index);
+ });
+ return FakeWebSocket.instances[index];
+}
describe("ReconnectingWebSocketTransport", () => {
const mockWsUrl = "ws://localhost:8080/lsp";
- let mockConnection: any;
beforeEach(() => {
- vi.clearAllMocks();
-
- // Create a mock WebSocket connection with readyState
- mockConnection = {
- readyState: WebSocket.OPEN,
- };
-
- // Mock the WebSocketTransport constructor to set the connection
- (WebSocketTransport as any).mockImplementation(function (this: any) {
- this.connection = mockConnection;
- this.connect = vi.fn().mockResolvedValue(undefined);
- this.close = vi.fn();
- this.sendData = vi.fn().mockResolvedValue({ result: "success" });
- this.subscribe = vi.fn();
- this.unsubscribe = vi.fn();
- });
+ FakeWebSocket.instances.length = 0;
+ vi.stubGlobal("WebSocket", FakeWebSocket);
});
- it("should create a transport with the provided URL function", () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- expect(transport).toBeDefined();
- expect(getWsUrl).not.toHaveBeenCalled(); // URL function not called until connect
+ afterEach(() => {
+ vi.unstubAllGlobals();
});
- it("should connect successfully", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- await transport.connect();
-
- expect(getWsUrl).toHaveBeenCalledTimes(1);
- expect(WebSocketTransport).toHaveBeenCalledWith(mockWsUrl);
- });
-
- it("should wait for connection before connecting", async () => {
+ it("waits for prerequisites and shares concurrent connection attempts", async () => {
+ let releaseConnection: (() => void) | undefined;
+ const waitForConnection = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ releaseConnection = resolve;
+ }),
+ );
const getWsUrl = vi.fn(() => mockWsUrl);
- const waitForConnection = vi.fn().mockResolvedValue(undefined);
const transport = new ReconnectingWebSocketTransport({
getWsUrl,
waitForConnection,
});
- await transport.connect();
-
+ const firstConnection = transport.connect();
+ const secondConnection = transport.connect();
expect(waitForConnection).toHaveBeenCalledTimes(1);
+ expect(FakeWebSocket.instances).toHaveLength(0);
+
+ releaseConnection?.();
+ const socket = await getSocket(0);
+ socket.open();
+ await Promise.all([firstConnection, secondConnection]);
+
+ expect(socket.url).toBe(mockWsUrl);
expect(getWsUrl).toHaveBeenCalledTimes(1);
});
- it("should reuse the same connection promise if already connecting", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const waitForConnection = vi
- .fn()
- .mockImplementation(
- () => new Promise((resolve) => setTimeout(resolve, 100)),
- );
+ it("forwards messages in both directions and supports unsubscribe", async () => {
const transport = new ReconnectingWebSocketTransport({
- getWsUrl,
- waitForConnection,
+ getWsUrl: () => mockWsUrl,
});
+ const inbound = vi.fn();
+ const unsubscribe = transport.onMessage(inbound);
+ const connection = transport.connect();
+ const socket = await getSocket(0);
+ socket.open();
+ await connection;
+
+ const outbound: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ method: "initialized",
+ params: {},
+ };
+ transport.send(outbound);
+ expect(socket.messages()).toEqual([outbound]);
- // Start two connections concurrently
- const promise1 = transport.connect();
- const promise2 = transport.connect();
-
- await Promise.all([promise1, promise2]);
-
- // Should only create one delegate
- expect(WebSocketTransport).toHaveBeenCalledTimes(1);
- expect(waitForConnection).toHaveBeenCalledTimes(1);
- });
-
- it("should send data successfully when connected", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- await transport.connect();
-
- const data: any = { method: "test", params: [] };
- const result = await transport.sendData(data, 5000);
+ const notification: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ method: "textDocument/publishDiagnostics",
+ params: { uri: "file:///test.py", diagnostics: [] },
+ };
+ socket.receive(notification);
+ expect(inbound).toHaveBeenCalledWith(notification);
- expect(result).toEqual({ result: "success" });
+ unsubscribe();
+ socket.receive(notification);
+ expect(inbound).toHaveBeenCalledTimes(1);
});
- it("should reconnect when WebSocket is in CLOSED state", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
+ it("restores LSP state before replaying queued requests", async () => {
+ const onReconnect = vi.fn(async () => {
+ transport.send({
+ jsonrpc: "2.0",
+ method: "initialized",
+ params: {},
+ });
+ transport.send({
+ jsonrpc: "2.0",
+ method: "textDocument/didOpen",
+ params: { textDocument: { uri: "file:///test.py" } },
+ });
+ });
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
+ onReconnect,
+ });
- // First connection
- await transport.connect();
- expect(WebSocketTransport).toHaveBeenCalledTimes(1);
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
- // Simulate WebSocket closing
- mockConnection.readyState = WebSocket.CLOSED;
+ transport.send({
+ jsonrpc: "2.0",
+ method: "textDocument/didChange",
+ params: { textDocument: { uri: "file:///test.py", version: 2 } },
+ });
+ const completionRequest: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ id: 1,
+ method: "textDocument/completion",
+ params: { textDocument: { uri: "file:///test.py" } },
+ };
+ transport.send(completionRequest);
- // Send data should trigger reconnection
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(1);
+ expect(secondSocket.messages()).toHaveLength(3);
+ });
- // Should have created a new WebSocketTransport
- expect(WebSocketTransport).toHaveBeenCalledTimes(2);
+ expect(secondSocket.messages()).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "initialized",
+ params: {},
+ },
+ {
+ jsonrpc: "2.0",
+ method: "textDocument/didOpen",
+ params: { textDocument: { uri: "file:///test.py" } },
+ },
+ completionRequest,
+ ]);
});
- it("should reconnect when WebSocket is in CLOSING state", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- // First connection
- await transport.connect();
- expect(WebSocketTransport).toHaveBeenCalledTimes(1);
-
- // Simulate WebSocket closing
- mockConnection.readyState = WebSocket.CLOSING;
-
- // Send data should trigger reconnection
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
-
- // Should have created a new WebSocketTransport
- expect(WebSocketTransport).toHaveBeenCalledTimes(2);
- });
+ it("holds live requests until asynchronous state restoration finishes", async () => {
+ let finishRestoration: (() => void) | undefined;
+ const restorationGate = new Promise<void>((resolve) => {
+ finishRestoration = resolve;
+ });
+ const onReconnect = vi.fn(async () => {
+ transport.send({
+ jsonrpc: "2.0",
+ id: 10,
+ method: "initialize",
+ params: {},
+ });
+ await restorationGate;
+ transport.send({
+ jsonrpc: "2.0",
+ method: "initialized",
+ params: {},
+ });
+ });
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
+ onReconnect,
+ });
- it("should close the transport and prevent reconnection", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
+
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
+ expect(secondSocket.messages()).toEqual([
+ {
+ jsonrpc: "2.0",
+ id: 10,
+ method: "initialize",
+ params: {},
+ },
+ ]);
+ });
- await transport.connect();
- transport.close();
+ const hoverRequest: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ id: 11,
+ method: "textDocument/hover",
+ params: { textDocument: { uri: "file:///test.py" } },
+ };
+ transport.send(hoverRequest);
+ expect(secondSocket.messages()).toHaveLength(1);
- // Attempting to connect again should throw
- await expect(transport.connect()).rejects.toThrow("Transport is closed");
+ finishRestoration?.();
+ await vi.waitFor(() => {
+ expect(secondSocket.messages()).toHaveLength(3);
+ });
+ expect(secondSocket.messages()).toEqual([
+ {
+ jsonrpc: "2.0",
+ id: 10,
+ method: "initialize",
+ params: {},
+ },
+ {
+ jsonrpc: "2.0",
+ method: "initialized",
+ params: {},
+ },
+ hoverRequest,
+ ]);
});
- it("should close old delegate when creating a new one", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- // First connection
- await transport.connect();
- const firstDelegate = (transport as any).delegate;
- expect(firstDelegate).toBeDefined();
-
- // Simulate connection closed
- mockConnection.readyState = WebSocket.CLOSED;
-
- // Reconnect by sending data
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
+ it("replays document changes made while state restoration is in flight", async () => {
+ let finishRestoration: (() => void) | undefined;
+ const restorationGate = new Promise<void>((resolve) => {
+ finishRestoration = resolve;
+ });
+ const onReconnect = vi.fn(async () => {
+ transport.send({
+ jsonrpc: "2.0",
+ method: "textDocument/didOpen",
+ params: { textDocument: { uri: "file:///test.py", version: 3 } },
+ });
+ await restorationGate;
+ });
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
+ onReconnect,
+ });
- // Old delegate should have been closed
- expect(firstDelegate.close).toHaveBeenCalled();
- });
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
- it("should handle connection failures gracefully", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const connectionError = new Error("Connection failed");
+ const staleChange: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ method: "textDocument/didChange",
+ params: { textDocument: { uri: "file:///test.py", version: 2 } },
+ };
+ transport.send(staleChange);
- // Mock connect to fail
- (WebSocketTransport as any).mockImplementationOnce(function (this: any) {
- this.connection = mockConnection;
- this.connect = vi.fn().mockRejectedValue(connectionError);
- this.close = vi.fn();
- this.sendData = vi.fn();
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(1);
});
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- await expect(transport.connect()).rejects.toThrow("Connection failed");
+ // The user keeps typing while the resync is still awaiting.
+ const liveChange: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ method: "textDocument/didChange",
+ params: { textDocument: { uri: "file:///test.py", version: 4 } },
+ };
+ transport.send(liveChange);
- // Delegate should be cleared after failure
- expect((transport as any).delegate).toBeUndefined();
+ finishRestoration?.();
+ await vi.waitFor(() => {
+ expect(secondSocket.messages()).toHaveLength(2);
+ });
+ expect(secondSocket.messages()).toEqual([
+ {
+ jsonrpc: "2.0",
+ method: "textDocument/didOpen",
+ params: { textDocument: { uri: "file:///test.py", version: 3 } },
+ },
+ liveChange,
+ ]);
+ expect(secondSocket.messages()).not.toContainEqual(staleChange);
});
- it("should handle waitForConnection failures", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const waitError = new Error("Wait failed");
- const waitForConnection = vi.fn().mockRejectedValue(waitError);
-
+ it("retries failed state restoration without losing queued requests", async () => {
+ const onReconnect = vi
+ .fn<() => Promise<void>>()
+ .mockRejectedValueOnce(new Error("State restoration failed"))
+ .mockResolvedValue(undefined);
const transport = new ReconnectingWebSocketTransport({
- getWsUrl,
- waitForConnection,
+ getWsUrl: () => mockWsUrl,
+ onReconnect,
+ retries: 2,
+ retryDelayMs: 0,
});
- await expect(transport.connect()).rejects.toThrow("Wait failed");
-
- // Should not have created a delegate
- expect(WebSocketTransport).not.toHaveBeenCalled();
- });
-
- it("should automatically reconnect on sendData after connection loss", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- // Don't connect initially
- // Simulate WebSocket in closed state (no delegate exists)
- expect((transport as any).delegate).toBeUndefined();
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
+
+ const queuedRequest: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ id: 12,
+ method: "textDocument/hover",
+ params: { textDocument: { uri: "file:///test.py" } },
+ };
+ transport.send(queuedRequest);
- // Send data should trigger automatic connection
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
+ const failedRestorationSocket = await getSocket(1);
+ failedRestorationSocket.open();
+ const recoveredSocket = await getSocket(2);
+ recoveredSocket.open();
- expect(WebSocketTransport).toHaveBeenCalledTimes(1);
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(2);
+ expect(recoveredSocket.messages()).toEqual([queuedRequest]);
+ });
+ expect(failedRestorationSocket.messages()).toEqual([]);
});
- it("should call onReconnect callback after reconnection", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const onReconnect = vi.fn().mockResolvedValue(undefined);
+ it("abandons a restoration whose socket died instead of waiting for it", async () => {
+ // A restoration awaiting a response on a dead socket only settles when the
+ // LSP request times out, so recovery must not be chained to it.
+ const strandedRestoration = new Promise<void>(() => {
+ // never settles
+ });
+ const onReconnect = vi
+ .fn<() => Promise<void>>()
+ .mockImplementationOnce(() => strandedRestoration)
+ .mockResolvedValue(undefined);
const transport = new ReconnectingWebSocketTransport({
- getWsUrl,
+ getWsUrl: () => mockWsUrl,
onReconnect,
});
- // First connection - callback should not be called
- await transport.connect();
- expect(onReconnect).not.toHaveBeenCalled();
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
- // Simulate connection loss
- mockConnection.readyState = WebSocket.CLOSED;
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(1);
+ });
- // Reconnect - callback should be called this time
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
+ secondSocket.disconnect();
- expect(onReconnect).toHaveBeenCalledTimes(1);
+ const thirdSocket = await getSocket(2);
+ thirdSocket.open();
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(2);
+ });
+ expect(thirdSocket.readyState).toBe(FakeWebSocket.OPEN);
});
- it("should not call onReconnect callback on first connection", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const onReconnect = vi.fn().mockResolvedValue(undefined);
+ it("keeps the queued backlog when a restoration is abandoned", async () => {
+ const strandedRestoration = new Promise<void>(() => {
+ // never settles
+ });
+ const onReconnect = vi
+ .fn<() => Promise<void>>()
+ .mockImplementationOnce(() => strandedRestoration)
+ .mockResolvedValue(undefined);
const transport = new ReconnectingWebSocketTransport({
- getWsUrl,
+ getWsUrl: () => mockWsUrl,
onReconnect,
});
- await transport.connect();
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
+
+ const queuedRequest: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ id: 21,
+ method: "textDocument/hover",
+ params: { textDocument: { uri: "file:///test.py" } },
+ };
+ transport.send(queuedRequest);
- expect(onReconnect).not.toHaveBeenCalled();
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(1);
+ });
+ // The doomed attempt detached the backlog; abandoning it must hand the
+ // messages back rather than strand them.
+ secondSocket.disconnect();
+
+ const thirdSocket = await getSocket(2);
+ thirdSocket.open();
+ await vi.waitFor(() => {
+ expect(thirdSocket.messages()).toEqual([queuedRequest]);
+ });
});
- it("should handle onReconnect callback errors gracefully", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const reconnectError = new Error("Reconnect callback failed");
- const onReconnect = vi.fn().mockRejectedValue(reconnectError);
+ it("clears the in-flight backlog when closed during restoration", async () => {
+ const strandedRestoration = new Promise<void>(() => {
+ // never settles
+ });
+ const onReconnect = vi.fn<() => Promise<void>>(() => strandedRestoration);
const transport = new ReconnectingWebSocketTransport({
- getWsUrl,
+ getWsUrl: () => mockWsUrl,
onReconnect,
});
- // First connection
- await transport.connect();
-
- // Simulate connection loss
- mockConnection.readyState = WebSocket.CLOSED;
-
- // Reconnect - should propagate the error from onReconnect
- const data: any = { method: "test", params: [] };
- await expect(transport.sendData(data, 5000)).rejects.toThrow(
- "Reconnect callback failed",
- );
- });
-
- describe("subscribe", () => {
- it("should track subscriptions", () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+ firstSocket.disconnect();
- expect((transport as any).pendingSubscriptions).toHaveLength(1);
- expect((transport as any).pendingSubscriptions[0]).toEqual({
- event: "notification",
- handler,
- });
+ transport.send({
+ jsonrpc: "2.0",
+ method: "textDocument/didChange",
+ params: {},
});
- it("should register handler on delegate if it exists", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- await transport.connect();
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
-
- const delegate = (transport as any).delegate;
- expect(delegate.subscribe).toHaveBeenCalledWith("notification", handler);
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await vi.waitFor(() => {
+ expect(onReconnect).toHaveBeenCalledTimes(1);
});
+ // Restoration is now stranded and holding the backlog it detached.
+ expect(
+ (transport as unknown as { inFlightBacklog?: unknown[] }).inFlightBacklog,
+ ).toHaveLength(1);
- it("should register pending subscriptions when delegate is created", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- const handler1 = vi.fn();
- const handler2 = vi.fn();
- transport.subscribe("notification", handler1);
- transport.subscribe("response", handler2);
+ transport.close();
- await transport.connect();
+ expect(
+ (transport as unknown as { inFlightBacklog?: unknown[] }).inFlightBacklog,
+ ).toBeUndefined();
+ });
- const delegate = (transport as any).delegate;
- expect(delegate.subscribe).toHaveBeenCalledWith("notification", handler1);
- expect(delegate.subscribe).toHaveBeenCalledWith("response", handler2);
+ it("keeps inbound subscriptions active after reconnection", async () => {
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
});
+ const inbound = vi.fn();
+ transport.onMessage(inbound);
+ const initialConnection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.open();
+ await initialConnection;
+
+ firstSocket.disconnect();
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ const notification: JSONRPCMessage = {
+ jsonrpc: "2.0",
+ method: "window/logMessage",
+ params: { type: 3, message: "reconnected" },
+ };
+ secondSocket.receive(notification);
- it("should re-register subscriptions on reconnection", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- // Add subscription before connection
- const handler = vi.fn();
- transport.subscribe("notification", handler);
-
- // First connection
- await transport.connect();
- const firstDelegate = (transport as any).delegate;
- expect(firstDelegate.subscribe).toHaveBeenCalledWith(
- "notification",
- handler,
- );
-
- // Clear mock calls
- firstDelegate.subscribe.mockClear();
-
- // Simulate connection loss
- mockConnection.readyState = WebSocket.CLOSED;
-
- // Reconnect by sending data
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
-
- // New delegate should have been created
- const secondDelegate = (transport as any).delegate;
- expect(secondDelegate).not.toBe(firstDelegate);
-
- // Subscription should be re-registered on new delegate
- expect(secondDelegate.subscribe).toHaveBeenCalledWith(
- "notification",
- handler,
- );
- });
+ expect(inbound).toHaveBeenCalledWith(notification);
});
- describe("unsubscribe", () => {
- it("should remove subscription from tracking", () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
- expect((transport as any).pendingSubscriptions).toHaveLength(1);
-
- transport.unsubscribe("notification", handler);
- expect((transport as any).pendingSubscriptions).toHaveLength(0);
+ it("retries failed connection attempts", async () => {
+ const onConnectionFailure = vi.fn();
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
+ retries: 2,
+ retryDelayMs: 0,
+ onConnectionFailure,
});
- it("should unregister from delegate if it exists", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- await transport.connect();
-
- const handler = vi.fn();
- transport.subscribe("notification", handler);
+ const connection = transport.connect();
+ const firstSocket = await getSocket(0);
+ firstSocket.fail();
+ const secondSocket = await getSocket(1);
+ secondSocket.open();
+ await connection;
- const delegate = (transport as any).delegate;
- delegate.unsubscribe.mockClear();
-
- transport.unsubscribe("notification", handler);
+ expect(secondSocket.readyState).toBe(FakeWebSocket.OPEN);
+ expect(onConnectionFailure).not.toHaveBeenCalled();
+ });
- expect(delegate.unsubscribe).toHaveBeenCalledWith(
- "notification",
- handler,
- );
+ it("reports the final connection failure", async () => {
+ const onConnectionFailure = vi.fn();
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
+ retries: 1,
+ onConnectionFailure,
});
- it("should not re-register unsubscribed handlers on reconnection", async () => {
- const getWsUrl = vi.fn(() => mockWsUrl);
- const transport = new ReconnectingWebSocketTransport({ getWsUrl });
-
- const handler1 = vi.fn();
- const handler2 = vi.fn();
- transport.subscribe("notification", handler1);
- transport.subscribe("response", handler2);
-
- await transport.connect();
-
- // Unsubscribe handler1
- transport.unsubscribe("notification", handler1);
+ const connection = transport.connect();
+ const socket = await getSocket(0);
+ socket.fail();
- // Simulate connection loss
- mockConnection.readyState = WebSocket.CLOSED;
-
- // Reconnect by sending data
- const data: any = { method: "test", params: [] };
- await transport.sendData(data, 5000);
+ await expect(connection).rejects.toThrow(
+ "WebSocket connection to ws://localhost:8080/lsp failed",
+ );
+ expect(onConnectionFailure).toHaveBeenCalledWith(
+ expect.objectContaining({ message: expect.stringContaining("failed") }),
+ );
+ });
- const newDelegate = (transport as any).delegate;
+ it("closes permanently and ignores later sends", async () => {
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl: () => mockWsUrl,
+ });
+ const connection = transport.connect();
+ const socket = await getSocket(0);
+ socket.open();
+ await connection;
- // Only handler2 should be registered on the new delegate
- expect(newDelegate.subscribe).not.toHaveBeenCalledWith(
- "notification",
- handler1,
- );
- expect(newDelegate.subscribe).toHaveBeenCalledWith("response", handler2);
+ transport.close();
+ transport.send({
+ jsonrpc: "2.0",
+ method: "initialized",
+ params: {},
});
+
+ expect(socket.messages()).toEqual([]);
+ await expect(transport.connect()).rejects.toThrow("Transport is closed");
+ expect(FakeWebSocket.instances).toHaveLength(1);
});
});
diff --git a/frontend/src/core/lsp/transport.ts b/frontend/src/core/lsp/transport.ts
index 061ead8531..b556a472c4 100644
--- a/frontend/src/core/lsp/transport.ts
+++ b/frontend/src/core/lsp/transport.ts
@@ -1,9 +1,25 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { WebSocketTransport } from "@open-rpc/client-js";
-import type { JSONRPCRequestData } from "@open-rpc/client-js/build/Request";
-import { Transport } from "@open-rpc/client-js/build/transports/Transport";
+
+import {
+ type JSONRPCMessage,
+ type Transport,
+ WebSocketTransport,
+} from "@marimo-team/codemirror-languageserver";
import { Logger } from "@/utils/Logger";
+const MAX_PENDING_MESSAGES = 1000;
+const RESYNCED_NOTIFICATION_METHODS = new Set([
+ "textDocument/didOpen",
+ "textDocument/didChange",
+ "textDocument/didClose",
+]);
+const RESTORATION_METHODS = new Set([
+ "initialize",
+ "initialized",
+ "workspace/didChangeConfiguration",
+ "textDocument/didOpen",
+]);
+
export interface ReconnectingWebSocketTransportOptions {
/**
* Function that returns the WebSocket URL to connect to.
@@ -21,183 +37,339 @@ export interface ReconnectingWebSocketTransportOptions {
* This allows the LSP client to re-synchronize state (e.g., re-send document open notifications).
*/
onReconnect?: () => Promise<void>;
-}
-interface Subscription {
- event: "pending" | "notification" | "response" | "error";
- handler: Parameters<Transport["subscribe"]>[1];
+ /**
+ * Number of connection attempts in each retry cycle.
+ * @default 1
+ */
+ retries?: number;
+
+ /**
+ * Delay between connection attempts.
+ * @default 1000
+ */
+ retryDelayMs?: number;
+
+ /**
+ * Called when every connection attempt in a retry cycle fails.
+ */
+ onConnectionFailure?: (error: Error) => void;
}
/**
- * A WebSocket transport that automatically reconnects when the connection is lost.
- * This handles cases like computer sleep/wake or network interruptions.
+ * A WebSocket transport that automatically reconnects when the connection is
+ * lost. Messages produced while reconnecting are replayed only after the LSP
+ * client has re-initialized and restored its document state.
+ *
+ * Deliberately does not implement the optional `Transport.onClose`: JSONRPCClient
+ * latches the first close error and never clears it, so forwarding drops would
+ * fail every later request instead of surviving a reconnect.
*/
-export class ReconnectingWebSocketTransport extends Transport {
+export class ReconnectingWebSocketTransport implements Transport {
private delegate: WebSocketTransport | undefined;
- private readonly options: ReconnectingWebSocketTransportOptions;
private connectionPromise: Promise<void> | undefined;
+ private isDelegateConnected = false;
+ /**
+ * Identifies the current connection attempt. Bumping it abandons whatever is
+ * in flight: an attempt whose socket has already died may stay parked on an
+ * LSP request timeout, and it must neither delay recovery nor mutate state
+ * belonging to the attempt that replaced it.
+ */
+ private connectionGeneration = 0;
+ /** Generation currently restoring LSP state, if any. */
+ private restoringGeneration: number | undefined;
private isClosed = false;
private hasConnectedBefore = false;
- private pendingSubscriptions: Subscription[] = [];
+ private readonly messageHandlers = new Set<
+ (message: JSONRPCMessage) => void
+ >();
+ private pendingMessages: JSONRPCMessage[] = [];
+ /**
+ * Backlog detached by the in-flight attempt. Held here rather than in a local
+ * so that abandoning that attempt can hand the messages straight back to the
+ * queue instead of stranding them until it eventually settles.
+ */
+ private inFlightBacklog: JSONRPCMessage[] | undefined;
+ private hasLoggedQueueOverflow = false;
+ private disposeDelegateMessage: (() => void) | undefined;
+ private disposeDelegateClose: (() => void) | undefined;
+ private readonly options: Required<
+ Pick<ReconnectingWebSocketTransportOptions, "retries" | "retryDelayMs">
+ > &
+ Omit<ReconnectingWebSocketTransportOptions, "retries" | "retryDelayMs">;
+
+ public onReconnect: (() => Promise<void>) | undefined;
constructor(options: ReconnectingWebSocketTransportOptions) {
- super();
- this.options = options;
- this.delegate = undefined;
+ this.options = {
+ ...options,
+ retries: options.retries ?? 1,
+ retryDelayMs: options.retryDelayMs ?? 1000,
+ };
+ this.onReconnect = options.onReconnect;
}
- /**
- * Create a new WebSocket delegate, replacing any existing one.
- */
- private createDelegate(): WebSocketTransport {
- // Close the old delegate if it exists
- if (this.delegate) {
- try {
- this.delegate.close();
- } catch (error) {
- Logger.warn("Error closing old WebSocket delegate", error);
- }
+ async connect(): Promise<void> {
+ if (this.isClosed) {
+ throw new Error("Transport is closed");
}
-
- // Create a new delegate
- this.delegate = new WebSocketTransport(this.options.getWsUrl());
-
- // Re-register all pending subscriptions on the new delegate
- for (const { event, handler } of this.pendingSubscriptions) {
- this.delegate.subscribe(event, handler);
+ if (this.isDelegateConnected) {
+ return;
+ }
+ if (this.connectionPromise) {
+ return this.connectionPromise;
}
- return this.delegate;
+ const generation = ++this.connectionGeneration;
+ const promise = this.connectWithRetries(generation).finally(() => {
+ // An abandoned attempt must not clear its successor's promise.
+ if (this.connectionPromise === promise) {
+ this.connectionPromise = undefined;
+ }
+ });
+ this.connectionPromise = promise;
+ return promise;
}
- /**
- * Check if the current delegate's WebSocket is in a closed or closing state.
- */
- private isDelegateClosedOrClosing(): boolean {
- if (!this.delegate) {
- return true;
+ /** Whether `generation` has been superseded or the transport was closed. */
+ private isStale(generation: number): boolean {
+ return this.isClosed || generation !== this.connectionGeneration;
+ }
+
+ send(message: JSONRPCMessage): void {
+ if (this.isClosed) {
+ return;
+ }
+ if (
+ this.delegate &&
+ this.isDelegateConnected &&
+ (this.restoringGeneration === undefined ||
+ this.canSendWhileRestoring(message))
+ ) {
+ this.delegate.send(message);
+ return;
}
- // Access the internal connection to check its readyState
- const ws = this.delegate.connection;
- if (!ws) {
- return true;
+ if (this.pendingMessages.length < MAX_PENDING_MESSAGES) {
+ this.pendingMessages.push(message);
+ } else if (!this.hasLoggedQueueOverflow) {
+ // Log once per episode; the queue fills one keystroke at a time.
+ this.hasLoggedQueueOverflow = true;
+ Logger.error(
+ `LSP reconnect queue is full (${MAX_PENDING_MESSAGES}); dropping messages until it drains`,
+ );
}
+ this.reconnect();
+ }
- // WebSocket.CLOSING = 2, WebSocket.CLOSED = 3
- return (
- ws.readyState === WebSocket.CLOSING || ws.readyState === WebSocket.CLOSED
- );
+ onMessage(handler: (message: JSONRPCMessage) => void): () => void {
+ this.messageHandlers.add(handler);
+ return () => {
+ this.messageHandlers.delete(handler);
+ };
}
- override async connect() {
- // Don't reconnect if explicitly closed
+ close(): void {
if (this.isClosed) {
- throw new Error("Transport is closed");
+ return;
}
+ this.isClosed = true;
+ this.isDelegateConnected = false;
+ this.restoringGeneration = undefined;
+ // Abandon anything in flight so it cannot touch state after teardown.
+ this.connectionGeneration++;
+ this.connectionPromise = undefined;
+ this.pendingMessages = [];
+ this.inFlightBacklog = undefined;
+ this.messageHandlers.clear();
+ this.disposeDelegate();
+ this.delegate?.close();
+ this.delegate = undefined;
+ }
- // If already connecting, wait for that connection
- if (this.connectionPromise) {
- return this.connectionPromise;
- }
+ private async connectWithRetries(generation: number): Promise<void> {
+ let lastError: Error | undefined;
- this.connectionPromise = (async () => {
+ for (let attempt = 1; attempt <= this.options.retries; attempt++) {
try {
- // Wait for dependencies to be ready (e.g., runtime connection)
- if (this.options.waitForConnection) {
- await this.options.waitForConnection();
- }
-
- // Create a new delegate if needed
- let delegate = this.delegate;
- if (!delegate || this.isDelegateClosedOrClosing()) {
- delegate = this.createDelegate();
+ await this.connectDelegate(generation);
+ return;
+ } catch (error) {
+ lastError = error instanceof Error ? error : new Error(String(error));
+ if (this.isStale(generation)) {
+ // Superseded while we were failing; the live attempt owns the
+ // delegate now, so tear down nothing and retry nothing.
+ throw lastError;
}
-
- // Connect the delegate
- await delegate.connect();
- Logger.log("WebSocket transport connected successfully");
-
- // If this is a reconnection, call the onReconnect callback
- const isReconnection = this.hasConnectedBefore;
- this.hasConnectedBefore = true;
-
- if (isReconnection && this.options.onReconnect) {
- Logger.log("Calling onReconnect callback to re-synchronize state");
- await this.options.onReconnect();
+ this.invalidateDelegate();
+ Logger.warn(
+ `WebSocket connection attempt ${attempt}/${this.options.retries} failed`,
+ lastError,
+ );
+
+ if (attempt < this.options.retries) {
+ await new Promise((resolve) =>
+ setTimeout(resolve, this.options.retryDelayMs),
+ );
}
- } catch (error) {
- Logger.error("WebSocket transport connection failed", error);
- // Clear the delegate on failure so we create a new one on retry
- this.delegate = undefined;
- throw error;
- } finally {
- this.connectionPromise = undefined;
}
- })();
+ }
- return this.connectionPromise;
+ const error = lastError ?? new Error("WebSocket connection failed");
+ this.options.onConnectionFailure?.(error);
+ throw error;
}
- override close() {
- this.isClosed = true;
- this.delegate?.close();
- this.delegate = undefined;
- this.connectionPromise = undefined;
- }
-
- override subscribe(...args: Parameters<Transport["subscribe"]>): void {
- // Register handler on parent Transport
- super.subscribe(...args);
+ private async connectDelegate(generation: number): Promise<void> {
+ await this.options.waitForConnection?.();
+ if (this.isStale(generation)) {
+ throw new Error("Connection attempt abandoned");
+ }
- const [event, handler] = args;
+ const delegate = this.createDelegate();
+ await delegate.connect();
+ if (this.isStale(generation) || this.delegate !== delegate) {
+ delegate.close();
+ throw new Error("Transport closed while connecting");
+ }
- // Track the subscription
- this.pendingSubscriptions.push({ event, handler });
+ this.isDelegateConnected = true;
+ const isReconnection = this.hasConnectedBefore;
+ this.hasConnectedBefore = true;
+
+ // Detach before restoring: notifications queued while the socket was down
+ // are superseded by what `onReconnect` re-sends, but ones produced *during*
+ // restoration are not — they carry edits the restore snapshot never saw,
+ // and the notebook snapshotter will not re-emit them.
+ const queuedWhileDisconnected = this.detachPendingMessages();
+ this.inFlightBacklog = queuedWhileDisconnected;
+
+ try {
+ if (isReconnection) {
+ this.restoringGeneration = generation;
+ try {
+ await this.onReconnect?.();
+ } finally {
+ // Only clear the gate if a newer attempt hasn't taken it over.
+ if (this.restoringGeneration === generation) {
+ this.restoringGeneration = undefined;
+ }
+ }
+ }
- // Also register on delegate if it exists
- if (this.delegate) {
- this.delegate.subscribe(event, handler);
+ if (
+ this.isStale(generation) ||
+ this.delegate !== delegate ||
+ !this.isDelegateConnected
+ ) {
+ throw new Error("WebSocket closed while restoring LSP state");
+ }
+ } catch (error) {
+ // Nothing superseded the backlog, so restore it ahead of anything queued
+ // since. If this attempt was abandoned, whoever abandoned it already
+ // reclaimed the backlog and owns the queue now.
+ if (!this.isStale(generation)) {
+ this.reclaimInFlightBacklog();
+ }
+ throw error;
}
- }
- override unsubscribe(
- ...args: Parameters<Transport["unsubscribe"]>
- ): import("events").EventEmitter | undefined {
- // Unregister from parent
- const result = super.unsubscribe(...args);
+ const replayable =
+ isReconnection && this.onReconnect
+ ? queuedWhileDisconnected.filter(
+ (message) =>
+ !(
+ "method" in message &&
+ RESYNCED_NOTIFICATION_METHODS.has(message.method)
+ ),
+ )
+ : queuedWhileDisconnected;
+
+ this.inFlightBacklog = undefined;
+ for (const message of [...replayable, ...this.detachPendingMessages()]) {
+ delegate.send(message);
+ }
- const [event, handler] = args;
+ Logger.log("WebSocket transport connected successfully");
+ }
- // Remove from pending subscriptions
- this.pendingSubscriptions = this.pendingSubscriptions.filter(
- (sub) => !(sub.event === event && sub.handler === handler),
- );
+ private detachPendingMessages(): JSONRPCMessage[] {
+ const messages = this.pendingMessages;
+ this.pendingMessages = [];
+ this.hasLoggedQueueOverflow = false;
+ return messages;
+ }
- // Also unregister from delegate if it exists
- if (this.delegate) {
- this.delegate.unsubscribe(event, handler);
+ /** Return the in-flight attempt's backlog to the front of the queue. */
+ private reclaimInFlightBacklog(): void {
+ if (!this.inFlightBacklog) {
+ return;
}
+ this.pendingMessages = [
+ ...this.inFlightBacklog,
+ ...this.pendingMessages,
+ ].slice(0, MAX_PENDING_MESSAGES);
+ this.inFlightBacklog = undefined;
+ }
- return result;
+ private canSendWhileRestoring(message: JSONRPCMessage): boolean {
+ // Responses to server-initiated requests must not be held or the
+ // initialization handshake could deadlock.
+ return !("method" in message) || RESTORATION_METHODS.has(message.method);
}
- override async sendData(
- data: JSONRPCRequestData,
- timeout: number | null | undefined,
- ) {
- // If the delegate is closed or closing, try to reconnect
- if (this.isDelegateClosedOrClosing()) {
- Logger.warn("WebSocket is closed or closing, attempting to reconnect");
- try {
- await this.connect();
- } catch (error) {
- Logger.error("Failed to reconnect WebSocket", error);
- throw error;
+ private createDelegate(): WebSocketTransport {
+ this.invalidateDelegate();
+
+ const delegate = new WebSocketTransport(this.options.getWsUrl());
+ this.delegate = delegate;
+ this.disposeDelegateMessage = delegate.onMessage((message) => {
+ for (const handler of this.messageHandlers) {
+ try {
+ handler(message);
+ } catch (error) {
+ Logger.error("LSP message handler failed", error);
+ }
}
- }
+ });
+ this.disposeDelegateClose = delegate.onClose((error) => {
+ if (this.delegate !== delegate || this.isClosed) {
+ return;
+ }
+ Logger.warn("WebSocket transport connection closed", error);
+ this.isDelegateConnected = false;
+ this.disposeDelegate();
+ this.delegate = undefined;
+ // Abandon any attempt still in flight: it is bound to the socket that
+ // just died and may sit parked on an LSP request timeout. Take its
+ // backlog and drop the promise so the next connect() starts immediately
+ // rather than joining a doomed one.
+ this.restoringGeneration = undefined;
+ this.reclaimInFlightBacklog();
+ this.connectionPromise = undefined;
+ queueMicrotask(() => this.reconnect());
+ });
+ return delegate;
+ }
+
+ private reconnect(): void {
+ void this.connect().catch((error) => {
+ Logger.error("WebSocket transport reconnection failed", error);
+ });
+ }
+
+ private invalidateDelegate(): void {
+ this.isDelegateConnected = false;
+ this.restoringGeneration = undefined;
+ this.disposeDelegate();
+ this.delegate?.close();
+ this.delegate = undefined;
+ }
- // Send the data using the delegate
- return this.delegate?.sendData(data, timeout);
+ private disposeDelegate(): void {
+ this.disposeDelegateMessage?.();
+ this.disposeDelegateMessage = undefined;
+ this.disposeDelegateClose?.();
+ this.disposeDelegateClose = undefined;
}
}
diff --git a/frontend/src/css/app/codemirror.css b/frontend/src/css/app/codemirror.css
index c918e90cc5..3209536e2f 100644
--- a/frontend/src/css/app/codemirror.css
+++ b/frontend/src/css/app/codemirror.css
@@ -90,8 +90,22 @@
@apply bg-muted border border-border rounded-md px-1;
}
- .cm-diagnosticText > div {
- display: inline-flex;
+ /* The message, the docs link, and related info are siblings; stack them. */
+ .cm-lsp-diagnostic-message {
+ @apply flex flex-col items-start gap-1;
+
+ /* Rendered markdown, so the `pre-wrap` on `.cm-diagnostic` would only turn
+ the whitespace between block tags into blank lines. */
+ white-space: normal;
+ }
+
+ .cm-diagnostic-code-link {
+ @apply text-xs;
+ }
+
+ /* The docs link already reads `source(code)`; don't repeat it below. */
+ .cm-diagnostic:has(.cm-diagnostic-code-link) .cm-diagnosticSource {
+ @apply hidden;
}
}
diff --git a/frontend/src/stories/log-viewer.stories.tsx b/frontend/src/stories/log-viewer.stories.tsx
index b770462dc8..b0ad6e6af8 100644
--- a/frontend/src/stories/log-viewer.stories.tsx
+++ b/frontend/src/stories/log-viewer.stories.tsx
@@ -23,37 +23,43 @@ export const Primary: Story = {
{
timestamp: Date.now(),
level: "stdout",
- cellId: cellId("cell1"),
+ source: { type: "cell", cellId: cellId("cell1") },
message: "Hello world!",
},
{
timestamp: Date.now(),
level: "stdout",
- cellId: cellId("cell1"),
+ source: { type: "cell", cellId: cellId("cell1") },
message: "Running cell...",
},
{
timestamp: Date.now(),
level: "stdout",
- cellId: cellId("cell1"),
+ source: { type: "cell", cellId: cellId("cell1") },
message: "Done!",
},
{
timestamp: Date.now(),
level: "stderr",
- cellId: cellId("cell2"),
+ source: { type: "cell", cellId: cellId("cell2") },
message: "Output is too large!",
},
{
timestamp: Date.now(),
level: "stderr",
- cellId: cellId("cell2"),
+ source: { type: "cell", cellId: cellId("cell2") },
message: "String length is too short.".repeat(100),
},
+ {
+ timestamp: Date.now(),
+ level: "stderr",
+ source: { type: "lsp", name: "pylsp" },
+ message: "Failed to load plugin 'ruff': not installed",
+ },
...Array.from({ length: 100 }).map(() => ({
timestamp: Date.now(),
level: "stdout" as const,
- cellId: cellId("cell1"),
+ source: { type: "cell", cellId: cellId("cell1") } as const,
message: "Running cell...",
})),
]}
diff --git a/marimo/third_party.txt b/marimo/third_party.txt
index 8c9adb7b0d..8f250a3097 100644
--- a/marimo/third_party.txt
+++ b/marimo/third_party.txt
@@ -132,7 +132,7 @@ See third_party_licenses.txt for license texts.
├── package.json: Apache-2.0
└── license files: Apache
-@marimo-team/[email protected] [license(s): BSD, BSD-3-Clause]
+@marimo-team/[email protected] [license(s): BSD, BSD-3-Clause]
├── package.json: BSD-3-Clause
└── readme files: BSD
@@ -149,10 +149,6 @@ See third_party_licenses.txt for license texts.
├── package.json: MIT
└── license files: MIT
-@open-rpc/[email protected] [license(s): Apache, Apache-2.0]
-├── package.json: Apache-2.0
-└── license files: Apache
-
@paralleldrive/[email protected] [license(s): MIT]
├── package.json: MIT
└── license files: MIT
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index cc2f0d87eb..e4988bfc46 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -140,8 +140,8 @@ importers:
specifier: ^0.3.7
version: 0.3.7(@codemirror/[email protected])(@codemirror/[email protected])
'@marimo-team/codemirror-languageserver':
- specifier: ^1.16.12
- version: 1.16.12(@codemirror/[email protected])(@codemirror/[email protected])
+ specifier: ^2.0.0
+ version: 2.0.0(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])
'@marimo-team/codemirror-mcp':
specifier: ^0.1.7
version: 0.1.7(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@modelcontextprotocol/[email protected])
@@ -163,9 +163,6 @@ importers:
'@mui/material':
specifier: ^6.5.0
- '@open-rpc/client-js':
- specifier: ^1.8.1
- version: 1.8.1
'@paralleldrive/cuid2':
specifier: ^2.3.1
version: 2.3.1
@@ -1685,9 +1682,11 @@ packages:
'@codemirror/state': ^6
'@codemirror/view': ^6
- '@marimo-team/[email protected]':
- resolution: {integrity: sha512-GZ6blOQYf5Xl5wlKl024gN4C4ZXv6BIS1/19fuFhuDh06TO24ueNUqlSA3S0Un8II9wvbqqAmYz/yFdSMiqeYw==}
+ '@marimo-team/[email protected]':
+ resolution: {integrity: sha512-ch/jeSnyfufQsycttZk/wmqQ5MSz1Ofe9iP1Lye4eJMSd0zUcDthqHnUWxPqyV4HMK3ns6bIS8pKQYQ7VkiauA==}
peerDependencies:
+ '@codemirror/autocomplete': ^6
+ '@codemirror/lint': ^6
'@codemirror/state': ^6
'@codemirror/view': ^6
@@ -1885,9 +1884,6 @@ packages:
'@open-draft/[email protected]':
resolution: {integrity: sha512-U69T3ItWHvLwGg5eJ0n3I62nWuE6ilHlmz7zM0npLBRvPRd7e6NYmg54vvRtP5mZG7kZqZCFVdsTWo7BPtBujg==}
- '@open-rpc/[email protected]':
- resolution: {integrity: sha512-vV+Hetl688nY/oWI9IFY0iKDrWuLdYhf7OIKI6U1DcnJV7r4gAgwRJjEr1QVYszUc0gjkHoQJzqevmXMGLyA0g==}
-
'@opentelemetry/[email protected]':
resolution: {integrity: sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg==}
engines: {node: '>=8.0.0'}
@@ -6687,14 +6683,6 @@ packages:
resolution: {integrity: sha512-LpB/54B+/2J5hqQ7imZHfdU31OlgQqx7ZicVlkm9kzg9/w8GKLEcFfJl/t7DCEDueOyBAD6zCCwTO6Fzs0NoEQ==}
engines: {node: '>=16'}
- resolution: {integrity: sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==}
-
- resolution: {integrity: sha512-muId7Zzn9ywDsyXgTIafTry2sV3nySZeUDe6YedVd1Hvuuep5AsIlqK+XefWpYTyJG5e503F2xIuT2lcU6rCSw==}
- peerDependencies:
- ws: '*'
-
resolution: {integrity: sha512-Yljz7ffyPbrLpLngrMtZ7NduUgVvi6wG9RJ9IUcyCd59YQ911PBJphODUcbOVbqYfxe1wuYf/LJ8PauMRwsM/g==}
@@ -7403,15 +7391,6 @@ packages:
resolution: {integrity: sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==}
- resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==}
- engines: {node: 4.x || >=6.0.0}
- peerDependencies:
- encoding: ^0.1.0
- peerDependenciesMeta:
- encoding:
- optional: true
-
resolution: {integrity: sha512-J6l92tKHX6w8Jy5nO1Vuc01NoIiRGi/d6qBKVxh+IQ8Cr3b6HbVNfKiF8ZpFKufTwpwxMmce2W3iQZ861ZRyTg==}
engines: {node: '>=18'}
@@ -8723,9 +8702,6 @@ packages:
peerDependencies:
react: ^19.2.4
- resolution: {integrity: sha512-Nk/brWYpD85WlOgzw5h173aci0Teyv8YdIAEtV+N88nDB0dLlazZyJMIsN6eo1/AR61l+p6CJTG1JIyFaoNEEA==}
-
resolution: {integrity: sha512-vMgjE/GGEPEFnhFub6pa4FmJBRBVOLpIII2hvCZ8Kzb7K0hlHo7mQv6xYrBvCL2LtAIBwFUK8wvuJgTVSQ5MFQ==}
@@ -9106,9 +9082,6 @@ packages:
resolution: {integrity: sha512-kXuRi1mtaKMrsLUxz3sQYvVl37B0Ns6MzfrtV5DvJceE9bPyspOqk9xxv7XbZWcfLWbFmm997vl83qUWVJA64w==}
engines: {node: '>=16'}
- resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==}
-
resolution: {integrity: sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==}
engines: {node: '>=18'}
@@ -9657,9 +9630,6 @@ packages:
resolution: {integrity: sha512-q/fGIivtqTT7PEoF07axFIlHNk/XCPaYpq64btnepopSWvKNFkoORlQYgqDigBIuGA1ExnFd/GnSUnBNEPQY7Q==}
- resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}
-
resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==}
engines: {node: '>=12'}
@@ -9686,9 +9656,6 @@ packages:
engines: {node: '>=18'}
deprecated: Use @exodus/bytes instead for a more spec-conformant and faster implementation
- resolution: {integrity: sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==}
-
resolution: {integrity: sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg==}
engines: {node: '>=18'}
@@ -9697,9 +9664,6 @@ packages:
resolution: {integrity: sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==}
engines: {node: '>=18'}
- resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==}
-
resolution: {integrity: sha512-F6+WgncZi/mJDrammbTuHe1q0R5hOXv/mBaiNA2TCNT/LTHusX0V+CJnj9XT8ki5ln2UZyyddDgHfCzyrOH7MQ==}
@@ -9783,18 +9747,6 @@ packages:
utf-8-validate:
optional: true
- resolution: {integrity: sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==}
- engines: {node: '>=8.3.0'}
- peerDependencies:
- bufferutil: ^4.0.1
- utf-8-validate: ^5.0.2
- peerDependenciesMeta:
- bufferutil:
- optional: true
- utf-8-validate:
- optional: true
-
resolution: {integrity: sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==}
engines: {node: '>=10.0.0'}
@@ -11220,19 +11172,14 @@ snapshots:
'@codemirror/view': 6.43.6
diff: 8.0.2
- '@marimo-team/[email protected](@codemirror/[email protected])(@codemirror/[email protected])':
+ '@marimo-team/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])':
dependencies:
'@codemirror/autocomplete': 6.20.3
'@codemirror/lint': 6.9.7
'@codemirror/state': 6.7.1
'@codemirror/view': 6.43.6
- '@open-rpc/client-js': 1.8.1
marked: 15.0.12
vscode-languageserver-protocol: 3.17.5
- transitivePeerDependencies:
- - bufferutil
- - encoding
- - utf-8-validate
'@marimo-team/[email protected](@codemirror/[email protected])(@codemirror/[email protected])(@codemirror/[email protected])(@modelcontextprotocol/[email protected])':
dependencies:
@@ -11455,17 +11402,6 @@ snapshots:
'@open-draft/[email protected]': {}
- '@open-rpc/[email protected]':
- dependencies:
- isomorphic-fetch: 3.0.0
- isomorphic-ws: 5.0.0([email protected])
- strict-event-emitter-types: 2.0.0
- ws: 7.5.10
- transitivePeerDependencies:
- - bufferutil
- - encoding
- - utf-8-validate
-
'@opentelemetry/[email protected]': {}
'@oxc-project/[email protected]': {}
@@ -16903,17 +16839,6 @@ snapshots:
- dependencies:
- node-fetch: 2.7.0
- whatwg-fetch: 3.6.20
- transitivePeerDependencies:
- - encoding
-
- dependencies:
- ws: 7.5.10
-
@@ -17895,10 +17820,6 @@ snapshots:
- dependencies:
- whatwg-url: 5.0.0
-
@@ -19636,8 +19557,6 @@ snapshots:
transitivePeerDependencies:
- supports-color
-
@@ -20045,8 +19964,6 @@ snapshots:
dependencies:
tldts: 7.0.12
-
dependencies:
punycode: 2.3.1
@@ -20842,8 +20759,6 @@ snapshots:
dependencies:
get-canvas-context: 1.0.2
-
@@ -20897,8 +20812,6 @@ snapshots:
dependencies:
iconv-lite: 0.6.3
-
@@ -20906,11 +20819,6 @@ snapshots:
tr46: 5.1.1
webidl-conversions: 7.0.0
- dependencies:
- tr46: 0.0.3
- webidl-conversions: 3.0.1
-
@@ -20990,8 +20898,6 @@ snapshots:
dependencies:
async-limiter: 1.0.1
-