fix: resolve concurrent browser tab registration waits (#3752)

This commit is contained in:
Neil
2026-05-30 08:49:36 -07:00
committed by GitHub
parent c8a71b10d5
commit d899866a7e
2 changed files with 78 additions and 12 deletions
+55 -1
View File
@@ -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()
+23 -11
View File
@@ -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<string, () => 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<string, Set<() => void>>()
export function waitForTabRegistration(browserPageId: string, timeoutMs = 8_000): Promise<void> {
if (browserManager.getGuestWebContentsId(browserPageId) !== null) {
return Promise.resolve()
}
return new Promise<void>((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
}