Skip to content

Commit b5f98c5

Browse files
authored
fix(desktop-browser): wait for a new tab's first page before calling it blank (#10382)
1 parent f60e98d commit b5f98c5

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎apps/desktop/src/main/browser-agent/driver.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,34 @@ describe('executeTool', () => {
10201020
}
10211021
)
10221022

1023+
it('waits for a tab still loading its first page instead of calling it blank', async () => {
1024+
await driver.executeTool('chat-test', 'browser_open_tab', {})
1025+
const contents = session.requireTab().view.webContents
1026+
vi.mocked(contents.getURL).mockReturnValue('')
1027+
vi.mocked(contents.isLoading).mockReturnValue(true)
1028+
const loadListeners = () =>
1029+
vi
1030+
.mocked(contents.on)
1031+
.mock.calls.filter(([event]) => String(event) === 'did-stop-loading')
1032+
.map(([, listener]) => listener as () => void)
1033+
const registered = loadListeners().length
1034+
const evaluated = new Promise<void>((resolve) => {
1035+
vi.mocked(contents.executeJavaScript).mockImplementation(() => {
1036+
resolve()
1037+
return new Promise<never>(() => {})
1038+
})
1039+
})
1040+
1041+
void driver.executeTool('chat-test', 'browser_snapshot', {})
1042+
await vi.waitFor(() => expect(loadListeners().length).toBeGreaterThan(registered))
1043+
expect(contents.executeJavaScript).not.toHaveBeenCalled()
1044+
1045+
vi.mocked(contents.getURL).mockReturnValue('http://127.0.0.1/popup')
1046+
vi.mocked(contents.isLoading).mockReturnValue(false)
1047+
for (const onLoadComplete of loadListeners().slice(registered)) onLoadComplete()
1048+
await evaluated
1049+
})
1050+
10231051
it('leaves no watchdog timer pending once a tool finishes', async () => {
10241052
vi.useFakeTimers()
10251053
try {

‎apps/desktop/src/main/browser-agent/driver.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,8 +1385,13 @@ async function execInPage<Args extends unknown[], Result>(
13851385
userGesture = false,
13861386
notAfter?: number
13871387
): Promise<Result> {
1388-
const url = 'getURL' in target ? target.getURL() : target.url
1389-
if ('getURL' in target && (url === '' || url === 'about:blank')) {
1388+
const isBlank = (url: string) => url === '' || url === 'about:blank'
1389+
// A tab a link or popup just opened reports no URL until its first page commits.
1390+
if ('getURL' in target && isBlank(target.getURL()) && target.isLoading()) {
1391+
const remaining = notAfter === undefined ? NAVIGATION_TIMEOUT_MS : notAfter - Date.now()
1392+
await waitForLoadComplete(target, Math.max(0, Math.min(NAVIGATION_TIMEOUT_MS, remaining)))
1393+
}
1394+
if ('getURL' in target && isBlank(target.getURL())) {
13901395
throw new ToolError(
13911396
'The active tab is blank. Call browser_navigate before using page inspection or interaction tools.'
13921397
)

0 commit comments

Comments
 (0)