From 36bbb41d6c4a25f043edac0fe28d710bf909741b Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sun, 23 Aug 2026 17:29:37 -0700 Subject: [PATCH] fix(desktop): single-flight tolerates sync resume runners; delegate tests assert owner routing CI sibling-test blast radius from the cluster branch: - singleFlightSessionResume crashed on run() doubles that return non-promises (Cannot read 'finally'); wrap via Promise.resolve().then(run). - Three use-session-tile-delegate tests pinned the pre-#92961 ambient dispatch for default-profile sessions; the routing-authority change intentionally routes every known owner through the profile router, so the tests now assert requestGatewayForProfile('default', ...) instead. --- .../hooks/use-session-tile-delegate.test.ts | 60 ++++++++++++------- .../single-flight-resume.ts | 15 +++-- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts index 6d8ed3be1c..8bf3ffb50e 100644 --- a/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts +++ b/apps/desktop/src/app/contrib/hooks/use-session-tile-delegate.test.ts @@ -107,19 +107,29 @@ describe('useSessionTileDelegate resumeTile', () => { it('resolves and carries a default-profile session explicitly', async () => { setSessions([row({ id: 'stored-y', profile: 'default' })]) - const requestGateway = vi.fn(async (method: string) => - method === 'session.resume' ? ({ session_id: 'runtime-2' } as never) : ({} as never) - ) + const requestGateway = vi.fn(async () => ({}) as never) + + // #92961: a known owner is ALWAYS routed through the profile router — + // even 'default' — never dispatched on the ambient socket. + vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-2' } as never) renderTile(requestGateway) - await sessionTileDelegate()!.resumeTile('stored-y') + const runtimeId = await sessionTileDelegate()!.resumeTile('stored-y') - expect(requestGateway).toHaveBeenCalledWith('session.resume', { - session_id: 'stored-y', - cols: 96, - profile: 'default', - omit_messages: true - }) + expect(runtimeId).toBe('runtime-2') + expect(requestGatewayForProfile).toHaveBeenCalledWith( + 'default', + 'session.resume', + { + session_id: 'stored-y', + cols: 96, + profile: 'default', + omit_messages: true + }, + undefined, + undefined + ) + expect(requestGateway).not.toHaveBeenCalled() }) it('routes a Bot tile prefetch and resume through its exact connection owner', async () => { @@ -174,20 +184,26 @@ describe('useSessionTileDelegate resumeTile', () => { const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-b', 'runtime-dead']]) } const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', staleState]]) } - const requestGateway = vi.fn(async (method: string) => - method === 'session.resume' ? ({ session_id: 'runtime-fresh' } as never) : ({} as never) - ) + const requestGateway = vi.fn(async () => ({}) as never) + + vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-fresh' } as never) renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef }) const runtimeId = await sessionTileDelegate()!.resumeTile('stored-b') expect(runtimeId).toBe('runtime-fresh') - expect(requestGateway).toHaveBeenCalledWith('session.resume', { - session_id: 'stored-b', - cols: 96, - profile: 'default', - omit_messages: true - }) + expect(requestGatewayForProfile).toHaveBeenCalledWith( + 'default', + 'session.resume', + { + session_id: 'stored-b', + cols: 96, + profile: 'default', + omit_messages: true + }, + undefined, + undefined + ) }) it('invalidateRuntimeBindings clears the stored→runtime map so tiles re-resume after reconnect', async () => { @@ -197,9 +213,9 @@ describe('useSessionTileDelegate resumeTile', () => { const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-c', 'runtime-dead']]) } const sessionStateByRuntimeIdRef = { current: new Map([['runtime-dead', liveState]]) } - const requestGateway = vi.fn(async (method: string) => - method === 'session.resume' ? ({ session_id: 'runtime-fresh' } as never) : ({} as never) - ) + const requestGateway = vi.fn(async () => ({}) as never) + + vi.mocked(requestGatewayForProfile).mockResolvedValueOnce({ session_id: 'runtime-fresh' } as never) renderTile(requestGateway, { runtimeIdByStoredSessionIdRef, sessionStateByRuntimeIdRef }) diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts index a2ce9f3a36..96184ba35d 100644 --- a/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts +++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/single-flight-resume.ts @@ -22,11 +22,16 @@ export function singleFlightSessionResume(storedSessionId: string, run: () => return existing as Promise } - const flight = run().finally(() => { - if (_inFlightResumeByStoredSessionId.get(storedSessionId) === flight) { - _inFlightResumeByStoredSessionId.delete(storedSessionId) - } - }) + // Promise.resolve().then(run) tolerates run() being synchronous, returning a + // bare value, or throwing synchronously (test doubles and legacy callers do + // all three) — a raw run().finally() would crash on a non-promise return. + const flight = Promise.resolve() + .then(run) + .finally(() => { + if (_inFlightResumeByStoredSessionId.get(storedSessionId) === flight) { + _inFlightResumeByStoredSessionId.delete(storedSessionId) + } + }) _inFlightResumeByStoredSessionId.set(storedSessionId, flight)