diff --git a/src/main/ipc/browser.test.ts b/src/main/ipc/browser.test.ts index 7b89cc1da..7ca96e305 100644 --- a/src/main/ipc/browser.test.ts +++ b/src/main/ipc/browser.test.ts @@ -57,7 +57,11 @@ vi.mock('../browser/browser-manager', () => ({ } })) -import { registerBrowserHandlers, setAgentBrowserBridgeRef } from './browser' +import { + registerBrowserHandlers, + setAgentBrowserBridgeRef, + waitForTabRegistration +} from './browser' describe('registerBrowserHandlers', () => { beforeEach(() => { @@ -165,6 +169,56 @@ describe('registerBrowserHandlers', () => { expect(onTabChangedMock).toHaveBeenCalledWith(4242, 'wt-browser') }) + it('resolves concurrent tab registration waiters for the same page', async () => { + vi.useFakeTimers() + try { + getGuestWebContentsIdMock.mockReturnValue(null) + const first = waitForTabRegistration('page-1', 1000) + const second = waitForTabRegistration('page-1', 1000) + const settled = Promise.allSettled([first, second]) + + registerBrowserHandlers() + + const registerHandler = handleMock.mock.calls.find( + ([channel]) => channel === 'browser:registerGuest' + )?.[1] as ( + event: { sender: Electron.WebContents }, + args: { + browserPageId: string + workspaceId: string + worktreeId: string + webContentsId: number + } + ) => boolean + + const result = registerHandler( + { + sender: { + id: 91, + isDestroyed: () => false, + getType: () => 'window', + getURL: () => 'file:///renderer/index.html' + } as Electron.WebContents + }, + { + browserPageId: 'page-1', + workspaceId: 'workspace-1', + worktreeId: 'worktree-1', + webContentsId: 123 + } + ) + + expect(result).toBe(true) + await vi.advanceTimersByTimeAsync(1001) + expect(await settled).toEqual([ + { status: 'fulfilled', value: undefined }, + { status: 'fulfilled', value: undefined } + ]) + } finally { + vi.useRealTimers() + } + }) + it('validates annotation viewport bridge requests before syncing to the guest', async () => { registerBrowserHandlers() diff --git a/src/main/ipc/browser.ts b/src/main/ipc/browser.ts index c640365d7..826a2eca8 100644 --- a/src/main/ipc/browser.ts +++ b/src/main/ipc/browser.ts @@ -39,22 +39,32 @@ let agentBrowserBridgeRef: AgentBrowserBridge | null = null // Why: CLI-driven tab creation must wait until the renderer mounts the webview // and calls registerGuest, so the tab has a webContentsId and is operable by -// subsequent commands. This map holds one-shot resolvers keyed by browserPageId. -const pendingTabRegistrations = new Map void>() +// subsequent commands. Multiple commands can wait for the same page during +// startup, so keep all one-shot resolvers keyed by browserPageId. +const pendingTabRegistrations = new Map void>>() export function waitForTabRegistration(browserPageId: string, timeoutMs = 8_000): Promise { if (browserManager.getGuestWebContentsId(browserPageId) !== null) { return Promise.resolve() } return new Promise((resolve, reject) => { - const timer = setTimeout(() => { - pendingTabRegistrations.delete(browserPageId) - reject(new Error('Tab registration timed out')) - }, timeoutMs) - pendingTabRegistrations.set(browserPageId, () => { + let registrationResolvers = pendingTabRegistrations.get(browserPageId) + if (!registrationResolvers) { + registrationResolvers = new Set() + pendingTabRegistrations.set(browserPageId, registrationResolvers) + } + const resolveRegistration = (): void => { clearTimeout(timer) resolve() - }) + } + const timer = setTimeout(() => { + registrationResolvers.delete(resolveRegistration) + if (registrationResolvers.size === 0) { + pendingTabRegistrations.delete(browserPageId) + } + reject(new Error('Tab registration timed out')) + }, timeoutMs) + registrationResolvers.add(resolveRegistration) }) } @@ -128,10 +138,12 @@ export function registerBrowserHandlers(): void { if (agentBrowserBridgeRef && previousWcId !== null && previousWcId !== args.webContentsId) { agentBrowserBridgeRef.onProcessSwap(args.browserPageId, args.webContentsId, previousWcId) } - const pendingResolve = pendingTabRegistrations.get(args.browserPageId) - if (pendingResolve) { + const pendingResolves = pendingTabRegistrations.get(args.browserPageId) + if (pendingResolves) { pendingTabRegistrations.delete(args.browserPageId) - pendingResolve() + for (const pendingResolve of pendingResolves) { + pendingResolve() + } } return true }