Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions apps/desktop/src/main/browser-agent/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,59 @@ describe('executeTool', () => {
)
})

it('publishes main-frame load failures and retries their uncommitted URL', async () => {
const onPageState = vi.fn()
const win = new BrowserWindow()
driver.initDriver(
{
onPageState,
onTabsState: vi.fn(),
onSessionStatus: vi.fn(),
onFillAvailability: vi.fn(),
},
() => win
)
driver.activateBrowserScope('chat-test')
await driver.executeTool('chat-test', 'browser_open_tab', {})
const contents = session.requireTab().view.webContents
const eventHandlers = (contents.on as unknown as ReturnType<typeof vi.fn>).mock.calls
const failLoad = eventHandlers.find(([eventName]) => eventName === 'did-fail-load')?.[1] as
| ((...args: unknown[]) => void)
| undefined
const failedUrl = 'http://localhost:3004/login'

onPageState.mockClear()
failLoad?.({}, -102, 'ERR_CONNECTION_REFUSED', failedUrl, false)
failLoad?.({}, -3, 'ERR_ABORTED', failedUrl, true)
expect(onPageState).not.toHaveBeenCalled()

failLoad?.({}, -102, 'ERR_CONNECTION_REFUSED', failedUrl, true)

expect(onPageState).toHaveBeenLastCalledWith(
expect.objectContaining({
url: failedUrl,
issue: {
kind: 'load-error',
code: -102,
description: 'ERR_CONNECTION_REFUSED',
url: failedUrl,
},
})
)

vi.mocked(contents.loadURL).mockClear()
await driver.handlePanelAction('chat-test', { action: 'reload' })
expect(contents.loadURL).toHaveBeenCalledWith(failedUrl)

vi.mocked(contents.loadURL).mockClear()
await driver.executeTool('chat-test', 'browser_go_back', {})
expect(session.pageIssueForContents(contents)).toBeUndefined()
expect(session.canGoForward(contents)).toBe(true)

await driver.executeTool('chat-test', 'browser_go_forward', {})
expect(contents.loadURL).toHaveBeenCalledWith(failedUrl)
})

it('forces fill availability to replay on scope activation and tab switches', async () => {
const refreshAvailability = vi
.spyOn(fillCoordinator()!, 'refreshAvailability')
Expand Down
51 changes: 37 additions & 14 deletions apps/desktop/src/main/browser-agent/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,16 @@ function recordNotice(notice: string): void {
* navigations and tab switches.
*/
function pageStateFor(contents: WebContents, tabId: string): BrowserPageState {
const issue = session.pageIssueForContents(contents)
return {
scopeId: session.getBrowserScopeId(),
tabId,
url: contents.getURL(),
title: contents.getTitle(),
loading: contents.isLoadingMainFrame(),
canGoBack: contents.navigationHistory.canGoBack(),
canGoForward: contents.navigationHistory.canGoForward(),
url: issue?.url ?? contents.getURL(),
title: issue?.kind === 'load-error' ? '' : contents.getTitle(),
loading: issue ? false : contents.isLoadingMainFrame(),
canGoBack: session.canGoBack(contents),
canGoForward: session.canGoForward(contents),
...(issue ? { issue } : {}),
}
}

Expand Down Expand Up @@ -346,6 +348,18 @@ function instrumentTab(contents: WebContents): void {
pushTabsState()
})
)
contents.on(
'did-fail-load',
inScope((_event, errorCode, errorDescription, validatedURL, isMainFrame) => {
if (!isMainFrame || errorCode === 0 || errorCode === -3) return
session.recordPageLoadFailure(contents, {
kind: 'load-error',
code: errorCode,
description: errorDescription,
url: validatedURL || contents.getURL(),
})
})
)
contents.on(
'did-frame-navigate',
inScope(
Expand All @@ -364,7 +378,6 @@ function instrumentTab(contents: WebContents): void {
for (const event of [
'did-navigate-in-page',
'page-title-updated',
'did-start-loading',
'did-finish-load',
'did-stop-loading',
] as const) {
Expand All @@ -376,6 +389,14 @@ function instrumentTab(contents: WebContents): void {
})
)
}
contents.on(
'did-start-loading',
inScope(() => {
session.notePageLoadStarted(contents)
pushPageState(contents)
pushTabsState()
})
)
driverCallbacks?.onSessionStatus(true, scopeId)
}

Expand Down Expand Up @@ -435,6 +456,7 @@ export function initDriver(
// The fill affordance belongs to whichever page is in front.
void fillCoordinator()?.refreshAvailability(true)
},
onPageStateChanged: pushPageState,
onTabsChanged: pushTabsState,
onTabThemeChanged: (contents, theme) => {
void cdp.setColorScheme(contents, theme).catch((error) => {
Expand Down Expand Up @@ -1963,19 +1985,20 @@ async function executeToolInner(
case 'browser_go_forward': {
invalidateSnapshot()
const contents = session.requireAutomationTab().view.webContents
const history = contents.navigationHistory
assertCurrentExecution()
let completion: Promise<void>
if (tool === 'browser_go_back') {
if (!history.canGoBack()) throw new ToolError('Cannot go back — no earlier history entry.')
if (!session.canGoBack(contents)) {
throw new ToolError('Cannot go back — no earlier history entry.')
}
completion = waitForLoadComplete(contents, NAVIGATION_TIMEOUT_MS)
history.goBack()
session.goBack(contents)
} else {
if (!history.canGoForward()) {
if (!session.canGoForward(contents)) {
throw new ToolError('Cannot go forward — no later history entry.')
}
completion = waitForLoadComplete(contents, NAVIGATION_TIMEOUT_MS)
history.goForward()
session.goForward(contents)
}
return await navigationResult(contents, completion)
}
Expand Down Expand Up @@ -3814,13 +3837,13 @@ export async function handlePanelAction(
const contents = tab.view.webContents
switch (action.action) {
case 'reload':
contents.reload()
session.reloadPage(contents)
return
case 'back':
if (contents.navigationHistory.canGoBack()) contents.navigationHistory.goBack()
session.goBack(contents)
return
case 'forward':
if (contents.navigationHistory.canGoForward()) contents.navigationHistory.goForward()
session.goForward(contents)
return
case 'print':
contents.print({ printBackground: true })
Expand Down
Loading
Loading