diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts index 8a3d0bfbba3..24c6f1d73e7 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.test.ts @@ -240,7 +240,7 @@ describe('createAgentEventSource', () => { }) describe('createAgentEventSource reconnect backoff', () => { - it('doubles the delay per refused connect and resets once a socket delivers a frame', async () => { + it('doubles the delay per refused connect', async () => { const source = createSource() source.subscribe(vi.fn()) @@ -252,12 +252,52 @@ describe('createAgentEventSource reconnect backoff', () => { expect(sockets()).toHaveLength(2) await vi.advanceTimersByTimeAsync(1) expect(sockets()).toHaveLength(3) + }) + + it('keeps backing off when the server accepts, sends one frame and closes', async () => { + const source = createSource() + + source.subscribe(vi.fn()) + await connected() + for (const wait of [1000, 2000, 4000]) { + lastSocket().open() + lastSocket().receive({ type: 'agent_message_delta', data: {} }) + lastSocket().close() + const opened = sockets().length + await vi.advanceTimersByTimeAsync(wait - 1) + expect(sockets()).toHaveLength(opened) + await vi.advanceTimersByTimeAsync(1) + expect(sockets()).toHaveLength(opened + 1) + } + }) + + it('schedules a reconnect when the socket constructor throws', async () => { + let throwOnConstruct = true + vi.stubGlobal( + 'WebSocket', + Object.assign( + function WebSocketStub(url: string) { + if (!throwOnConstruct) return new FakeSocket(url) + throwOnConstruct = false + throw new DOMException('insecure socket', 'SecurityError') + }, + { + CONNECTING: FakeSocket.CONNECTING, + OPEN: FakeSocket.OPEN, + CLOSED: FakeSocket.CLOSED + } + ) + ) + const source = createSource() + const status = vi.fn() + source.onStatus(status) + + source.subscribe(vi.fn()) + await connected() + expect(sockets()).toHaveLength(0) - sockets()[2].open() - sockets()[2].receive({ type: 'agent_message_delta', data: {} }) - sockets()[2].close() await vi.advanceTimersByTimeAsync(1000) - expect(sockets()).toHaveLength(4) + expect(sockets()).toHaveLength(1) }) it('keeps backing off when the server accepts the upgrade and then closes', async () => { @@ -435,6 +475,47 @@ describe('createAgentEventSource token', () => { expect(sockets()).toHaveLength(0) }) + it('opens the socket once a later read supplies the missing credential', async () => { + const reads: (() => Promise)[] = [ + async () => null, + async () => tokenParams('signed-in') + ] + const source = createSource({ + getAuthParams: () => reads.shift()?.() ?? Promise.resolve(null) + }) + + source.subscribe(vi.fn()) + await connected() + expect(sockets()).toHaveLength(0) + + await vi.advanceTimersByTimeAsync(1000) + + expect( + sockets().map((socket) => new URL(socket.url).searchParams.get('token')) + ).toEqual(['signed-in']) + }) + + it('reconnects after an auth read that never settles', async () => { + const reads: (() => Promise)[] = [ + () => new Promise(() => {}), + async () => tokenParams('recovered') + ] + const source = createSource({ + getAuthParams: () => reads.shift()?.() ?? Promise.resolve(null) + }) + + source.subscribe(vi.fn()) + await connected() + expect(sockets()).toHaveLength(0) + + source.reconnect() + await connected() + + expect( + sockets().map((socket) => new URL(socket.url).searchParams.get('token')) + ).toEqual(['recovered']) + }) + it('carries every auth param onto the socket URL', async () => { const source = createAgentEventSource({ endpoint: '/api/agent/events', @@ -488,4 +569,63 @@ describe('createAgentEventSource send', () => { expect(source.send('{"type":"doc_subscribe"}')).toBe(true) expect(lastSocket().send).toHaveBeenCalledWith('{"type":"doc_subscribe"}') }) + it('does not open a second socket when a stale auth read settles mid-reconnect', async () => { + // The stale read's `finally` used to clear `connecting` unconditionally, releasing + // the latch held by the newer generation's in-flight read. A subscribe arriving in + // that window started a third read, and both survivors called open(), so the second + // overwrote `socket` and the first stayed connected forever with every handler + // bailing on `socket !== current`. + const pending: Array<(params: URLSearchParams) => void> = [] + const source = createSource({ + getAuthParams: () => + new Promise((resolve) => pending.push(resolve)) + }) + + source.subscribe(() => {}) + await connected() + expect(pending).toHaveLength(1) + + source.reconnect() + await connected() + expect(pending).toHaveLength(2) + + pending[0](tokenParams('stale')) + await connected() + + source.subscribe(() => {}) + await connected() + expect(pending).toHaveLength(2) + + pending[1](tokenParams('current')) + await connected() + pending.at(-1)?.(tokenParams('extra')) + await connected() + + expect(sockets()).toHaveLength(1) + expect(lastSocket().url).toContain('token=current') + }) + it('does not open a second socket when a subscribe lands as the auth read settles', async () => { + // The window this closes is one microtask wide: with the latch released inside + // readAuth, it was already false by the time connect() resumed, so a subscribe() + // arriving in between started a second read in the SAME generation. Both reached + // open() and the second overwrote `socket`, orphaning the first. + const pending: Array<(params: URLSearchParams) => void> = [] + const source = createSource({ + getAuthParams: () => + new Promise((resolve) => pending.push(resolve)) + }) + + source.subscribe(() => {}) + await connected() + expect(pending).toHaveLength(1) + + pending[0](tokenParams('first')) + await Promise.resolve() + source.subscribe(() => {}) + await connected() + pending.at(-1)?.(tokenParams('second')) + await connected() + + expect(sockets()).toHaveLength(1) + }) }) diff --git a/src/workbench/extensions/agent/services/agent/agentEventSource.ts b/src/workbench/extensions/agent/services/agent/agentEventSource.ts index f4cc1546fdc..84d1e38c886 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventSource.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventSource.ts @@ -62,34 +62,49 @@ export function createAgentEventSource({ reconnectTimer.start() } + async function readAuth(): Promise { + try { + return { ok: true, params: await getAuthParams() } + } catch { + return { ok: false } + } + } + async function connect(): Promise { if (listeners.size === 0 || socket !== null || connecting) return connecting = true const generation = connectGeneration - let auth: AuthRead - try { - auth = { ok: true, params: await getAuthParams() } - } catch { - auth = { ok: false } - } finally { - connecting = false - } - if (generation !== connectGeneration) { - void connect() - return - } + const auth = await readAuth() + // A stale read must leave the latch alone: `disconnect()` already cleared it and + // bumped the generation, so by now it belongs to a newer `connect()`. + if (generation !== connectGeneration) return + // Released here rather than in `readAuth`, because everything from this point to + // `open()` is synchronous. Clearing it before this continuation ran left a window + // in which a `subscribe()` could start a second read in the same generation; both + // would reach `open()`, and the second would overwrite `socket` without closing + // the first, leaving an orphan whose handlers all bail on `socket !== current`. + connecting = false if (listeners.size === 0) return - if (!auth.ok) { + // A credential that cannot be read right now — a rejected refresh, or a + // token the provider has not minted yet — is retried on the backoff rather + // than leaving the source dead until something else calls `reconnect()`. + if (!auth.ok || auth.params === null) { notifyStatus(false) scheduleReconnect() return } - if (auth.params === null) return open(eventUrl(auth.params)) } function open(url: string): void { - const current = new WebSocket(url) + let current: WebSocket + try { + current = new WebSocket(url) + } catch { + notifyStatus(false) + scheduleReconnect() + return + } socket = current let openedAt: number | null = null current.addEventListener('open', () => { @@ -99,7 +114,6 @@ export function createAgentEventSource({ }) current.addEventListener('message', (event) => { if (socket !== current) return - failedConnects = 0 let frame: unknown = event.data if (typeof event.data === 'string') { try { @@ -115,6 +129,9 @@ export function createAgentEventSource({ }) current.addEventListener('close', () => { if (socket !== current) return + // Only a connection that lasted clears the backoff. A server that + // accepts the upgrade, writes a frame and hangs up must not reset it, or + // the panel reconnects in a tight loop that never widens. if (openedAt !== null && Date.now() - openedAt >= maxReconnectDelayMs) failedConnects = 0 socket = null @@ -125,6 +142,9 @@ export function createAgentEventSource({ function disconnect(): void { connectGeneration++ + // An auth read that never settles would otherwise latch `connecting` and + // make every later `connect()` — `reconnect()` included — a no-op. + connecting = false reconnectTimer.stop() const current = socket socket = null