Repository navigation
fix(browser): harden Chrome relay stale-target recovery for snapshot and navigate - #39144
mas-jarvis wants to merge 1 commit into
Conversation
…and navigate - Tighten isChromeStaleTargetError: restore && logic for 404+tab-not-found, add explicit stale-targetid match, exclude 'no attached' errors which are not recoverable by dropping the targetId - Wrap snapshot retry in try/catch; on failure call shared throwWithTabContext helper that checks attached tabs and surfaces actionable relay guidance - Same treatment for navigate retry in browser-tool.ts - Extract throwWithTabContext helper in browser-tool.actions.ts to deduplicate the tab-check fallback previously inlined in executeActAction - Add browserTabs import to browser-tool.ts - 8 new tests: explicit stale-targetid signal, no-retry on no-attached-tabs error, relay guidance on retry failure (no-tabs and tabs-present variants), non-chrome passthrough
Greptile SummaryThis PR hardens stale Chrome relay target recovery for the Key improvements verified:
Maintainability note: The navigate handler in Confidence Score: 4/5
Last reviewed commit: 2c7af59 |
| } catch { | ||
| // Retry failed — surface actionable relay guidance. | ||
| const tabs = proxyRequest | ||
| ? (( | ||
| (await proxyRequest({ | ||
| method: "GET", | ||
| path: "/tabs", | ||
| profile, | ||
| })) as { tabs?: unknown[] } | ||
| ).tabs ?? []) | ||
| : await browserTabs(baseUrl, { profile }).catch(() => []); | ||
| if (!tabs.length) { | ||
| throw new Error( | ||
| "No Chrome tabs are attached via the OpenClaw Browser Relay extension. Click the toolbar icon on the tab you want to control (badge ON), then retry.", | ||
| { cause: err }, | ||
| ); | ||
| } | ||
| throw new Error( | ||
| `Chrome tab not found (stale targetId?). Run action=tabs profile="chrome" and use one of the returned targetIds.`, | ||
| { cause: err }, | ||
| ); |
There was a problem hiding this comment.
throwWithTabContext is not exported — navigate handler duplicates the helper
The PR description states that throwWithTabContext was extracted as a shared helper to replace inline duplicates, but the helper is not exported from browser-tool.actions.ts. As a result, this navigate retry catch block is a verbatim copy of the same tab-fetching + error-throwing logic already live inside throwWithTabContext.
If the relay guidance messages ever need updating (e.g. wording changes, new URL, extra context), they'll need to be changed in two places — which is exactly the problem the extraction was meant to solve.
The fix is to export throwWithTabContext from browser-tool.actions.ts and call it here instead:
| } catch { | |
| // Retry failed — surface actionable relay guidance. | |
| const tabs = proxyRequest | |
| ? (( | |
| (await proxyRequest({ | |
| method: "GET", | |
| path: "/tabs", | |
| profile, | |
| })) as { tabs?: unknown[] } | |
| ).tabs ?? []) | |
| : await browserTabs(baseUrl, { profile }).catch(() => []); | |
| if (!tabs.length) { | |
| throw new Error( | |
| "No Chrome tabs are attached via the OpenClaw Browser Relay extension. Click the toolbar icon on the tab you want to control (badge ON), then retry.", | |
| { cause: err }, | |
| ); | |
| } | |
| throw new Error( | |
| `Chrome tab not found (stale targetId?). Run action=tabs profile="chrome" and use one of the returned targetIds.`, | |
| { cause: err }, | |
| ); | |
| } catch { | |
| // Retry failed — surface actionable relay guidance. | |
| await throwWithTabContext(err, profile, baseUrl, proxyRequest); | |
| } |
This also removes the need for the browserTabs import that was added to browser-tool.ts.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/tools/browser-tool.ts
Line: 549-569
Comment:
`throwWithTabContext` is not exported — navigate handler duplicates the helper
The PR description states that `throwWithTabContext` was extracted as a *shared* helper to replace inline duplicates, but the helper is not exported from `browser-tool.actions.ts`. As a result, this navigate retry catch block is a verbatim copy of the same tab-fetching + error-throwing logic already live inside `throwWithTabContext`.
If the relay guidance messages ever need updating (e.g. wording changes, new URL, extra context), they'll need to be changed in two places — which is exactly the problem the extraction was meant to solve.
The fix is to export `throwWithTabContext` from `browser-tool.actions.ts` and call it here instead:
```suggestion
} catch {
// Retry failed — surface actionable relay guidance.
await throwWithTabContext(err, profile, baseUrl, proxyRequest);
}
```
This also removes the need for the `browserTabs` import that was added to `browser-tool.ts`.
How can I resolve this? If you propose a fix, please make it concise.
Problem
When the Chrome MV3 extension relay briefly disconnects or a targetId goes stale, the browser tool would silently fail or surface confusing errors for
snapshotandnavigateactions.Two specific bugs:
1.
isChromeStaleTargetErrorwas too broadThe
||refactor meant"tab not found"alone matched"no attached Chrome tabs"errors (relay has no tabs at all). Retrying withouttargetIdin that case is useless and masks the real problem.2.
snapshotandnavigateretries had no fallback guidanceOn retry failure, both rethrew a raw error with no actionable context. The
actaction already surfaced"No Chrome tabs attached — click the toolbar icon"or"stale targetId — run action=tabs".snapshotandnavigatewere missing this entirely.Changes
browser-tool.actions.tsisChromeStaleTargetError: restore&&logic for404: tab not found, add explicitstale targetidmatch, addno attachedearly-exit guardthrowWithTabContexthelper (replaces inline duplicate inexecuteActAction)try/catch— failure callsthrowWithTabContextbrowser-tool.tstry/catch— failure surfaces same tab-check guidancebrowserTabsimportbrowser-tool.test.tsTest results
Notes
waitUntil: "load"navigate timeout issue is a separate follow-up