A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: resolve GitHub Copilot "Retry Connection" failing with "Agent service not initialized" (#8925) (#8976)
Myles Scolnick7c3cc608afad
diff --git a/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts b/frontend/src/core/codemirror/copilot/__tests__/transport.test.tsindex 46cd45e90c..7034e50bcc 100644--- a/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts+++ b/frontend/src/core/codemirror/copilot/__tests__/transport.test.ts@@ -365,7 +365,7 @@ describe("LazyWebsocketTransport", () => {expect(delegate.connect).toHaveBeenCalled();});- it("should clamp timeout to maxTimeoutMs", async () => {+ it("should use maxTimeoutMs as default when no timeout is provided", async () => {const transport = new LazyWebsocketTransport({getWsUrl: mockGetWsUrl,waitForReady: mockWaitForReady,@@ -376,12 +376,29 @@ describe("LazyWebsocketTransport", () => {await transport.connect();const data: any = { method: "test", params: [] };- await transport.sendData(data, 10_000);+ 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) {@@ -407,6 +424,115 @@ describe("LazyWebsocketTransport", () => {});});+ 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);++ 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({diff --git a/frontend/src/core/codemirror/copilot/client.ts b/frontend/src/core/codemirror/copilot/client.tsindex 57f2888840..c59cfc2d37 100644--- a/frontend/src/core/codemirror/copilot/client.ts+++ b/frontend/src/core/codemirror/copilot/client.ts@@ -37,13 +37,20 @@ export const createWSTransport = once(() => {export const getCopilotClient = once(() => {const userConfig = store.get(resolvedMarimoConfigAtom);const copilotSettings = userConfig.ai?.github?.copilot_settings ?? {};+ const transport = createWSTransport();- return new CopilotLanguageServerClient({+ const client = new CopilotLanguageServerClient({rootUri: FILE_URI,workspaceFolders: null,- transport: createWSTransport(),+ transport,copilotSettings,});++ // Re-run the LSP initialize handshake when the transport reconnects+ // after a close or connection failure.+ transport.onReconnect = () => client.reInitialize();++ return client;});export function copilotServer() {diff --git a/frontend/src/core/codemirror/copilot/language-server.ts b/frontend/src/core/codemirror/copilot/language-server.tsindex d2f50497a5..40c56b6fc9 100644--- a/frontend/src/core/codemirror/copilot/language-server.ts+++ b/frontend/src/core/codemirror/copilot/language-server.ts@@ -86,6 +86,17 @@ export class CopilotLanguageServerClient extends LanguageServerClient {});}+ /**+ * Re-run the LSP initialize handshake and send configuration.+ * Called by the transport's onReconnect callback after reconnecting.+ */+ async reInitialize(): Promise<void> {+ logger.log("#reInitialize: Re-initializing LSP connection");+ this.initializePromise = this.initialize();+ await this.initializePromise;+ await this.sendConfiguration();+ }+private async sendConfiguration() {const settings = this.copilotSettings;// Skip if no settings are provideddiff --git a/frontend/src/core/codemirror/copilot/transport.ts b/frontend/src/core/codemirror/copilot/transport.tsindex 8453fda1be..0d0694ea7a 100644--- a/frontend/src/core/codemirror/copilot/transport.ts+++ b/frontend/src/core/codemirror/copilot/transport.ts@@ -36,7 +36,8 @@ export interface LazyWebsocketTransportOptions {retryDelayMs?: number;/**- * Maximum timeout for sendData operations in milliseconds.+ * Default timeout for sendData operations in milliseconds.+ * Used when the caller does not provide an explicit timeout.* @default 5000*/maxTimeoutMs?: number;@@ -60,6 +61,13 @@ 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>;constructor(options: LazyWebsocketTransportOptions) {super();@@ -157,6 +165,7 @@ export class LazyWebsocketTransport extends Transport {);if (attempt === this.options.retries) {this.delegate = undefined;+ this.needsReInitialization = true;// Show error toast on final retrythis.options.showError("GitHub Copilot Connection Error",@@ -183,6 +192,7 @@ export class LazyWebsocketTransport extends Transport {override close(): void {this.delegate?.close();this.delegate = undefined;+ this.needsReInitialization = true;}override async sendData(@@ -202,6 +212,25 @@ export class LazyWebsocketTransport extends Transport {"Unable to connect to GitHub Copilot. Please check your settings and try again.",);}++ // 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@@ -211,11 +240,8 @@ export class LazyWebsocketTransport extends Transport {);}- // Clamp timeout to maxTimeoutMs- timeout = Math.min(- timeout ?? this.options.maxTimeoutMs,- this.options.maxTimeoutMs,- );+ // 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/websocket/useWebSocket.tsx b/frontend/src/core/websocket/useWebSocket.tsxindex a9254a9757..ccdaee3d37 100644--- a/frontend/src/core/websocket/useWebSocket.tsx+++ b/frontend/src/core/websocket/useWebSocket.tsx@@ -30,6 +30,8 @@ function createConnectionTransport(// Create a connection transport using the ReconnectingWebSocket from partysocket// This handles reconnecting when the connection is lost.const urlProvider = options.url; // We don't call the URL provider now since it may change (i.e. if the runtime redirects)+ // Cast needed: ReconnectingWebSocket types readyState as `number`+ // but IConnectionTransport expects `0 | 1 | 2 | 3`return new ReconnectingWebSocket(urlProvider, undefined, {// We don't want Infinity retriesmaxRetries: 10,@@ -38,7 +40,7 @@ function createConnectionTransport(// long timeout -- the server can become slow when many notebooks// are open.connectionTimeout: 10_000,- });+ }) as unknown as IConnectionTransport;}/**diff --git a/frontend/src/utils/json/base64.ts b/frontend/src/utils/json/base64.tsindex fae9a31103..2bf55e213b 100644--- a/frontend/src/utils/json/base64.ts+++ b/frontend/src/utils/json/base64.ts@@ -71,7 +71,6 @@ function base64ToUint8ArrayFallback(bytes: string): Uint8Array {* Uses native Uint8Array.fromBase64 if available, otherwise falls back to manual implementation.*/export const base64ToUint8Array: (bytes: Base64String) => Uint8Array =- // @ts-expect-error - Uint8Array.fromBase64 types coming in TypeScript 5.10+Uint8Array.fromBase64 ?? base64ToUint8ArrayFallback;/**@@ -100,10 +99,8 @@ function uint8ArrayToBase64Fallback(binary: Uint8Array): Base64String {* Uses native Uint8Array.prototype.toBase64 if available, otherwise falls back to manual implementation.*/export const uint8ArrayToBase64: (binary: Uint8Array) => Base64String =- // @ts-expect-error - Uint8Array.prototype.toBase64 types coming in TypeScript 5.10+- Uint8Array.prototype.toBase64- ? // @ts-expect-error - Uint8Array.prototype.toBase64 types coming in TypeScript 5.10+- (binary) => binary.toBase64()+ "toBase64" in Uint8Array.prototype+ ? (binary) => binary.toBase64() as Base64String: uint8ArrayToBase64Fallback;/**