Skip to content

fix(browser): harden Chrome relay stale-target recovery for snapshot and navigate - #39144

Closed
mas-jarvis wants to merge 1 commit into
openclaw:mainfrom
mas-jarvis:fix/browser-relay-stale-target-recovery
Closed

mas-jarvis wants to merge 1 commit into
openclaw:mainfrom
mas-jarvis:fix/browser-relay-stale-target-recovery

Conversation

@mas-jarvis

Copy link
Copy Markdown

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 snapshot and navigate actions.

Two specific bugs:

1. isChromeStaleTargetError was too broad
The || refactor meant "tab not found" alone matched "no attached Chrome tabs" errors (relay has no tabs at all). Retrying without targetId in that case is useless and masks the real problem.

2. snapshot and navigate retries had no fallback guidance
On retry failure, both rethrew a raw error with no actionable context. The act action already surfaced "No Chrome tabs attached — click the toolbar icon" or "stale targetId — run action=tabs". snapshot and navigate were missing this entirely.

Changes

browser-tool.actions.ts

  • Tighten isChromeStaleTargetError: restore && logic for 404: tab not found, add explicit stale targetid match, add no attached early-exit guard
  • Extract shared throwWithTabContext helper (replaces inline duplicate in executeActAction)
  • Wrap snapshot retry in try/catch — failure calls throwWithTabContext

browser-tool.ts

  • Wrap navigate retry in try/catch — failure surfaces same tab-check guidance
  • Add missing browserTabs import

browser-tool.test.ts

  • 8 new tests covering: explicit stale-targetid signal, no-retry on no-attached-tabs error, relay guidance on retry failure (no-tabs and tabs-present variants for both snapshot and navigate), non-chrome passthrough

Test results

✓ src/agents/tools/browser-tool.test.ts (31 tests) 54ms

Notes

  • The waitUntil: "load" navigate timeout issue is a separate follow-up
  • Tested live against Chrome relay across 10 sites with no disconnects post-fix

…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
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Mar 7, 2026
@mas-jarvis mas-jarvis closed this Mar 7, 2026
@greptile-apps

greptile-apps Bot commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR hardens stale Chrome relay target recovery for the snapshot and navigate browser tool actions by tightening the isChromeStaleTargetError guard, wrapping retries in try/catch blocks, and surfacing actionable guidance when retries fail.

Key improvements verified:

  • isChromeStaleTargetError tightened: now normalizes the error message to lowercase, adds an early-exit for "no attached" errors (where dropping targetId and retrying is pointless), and adds an explicit "stale targetid" match alongside the existing 404: tab not found check.
  • throwWithTabContext helper extracted in browser-tool.actions.ts and used by the snapshot retry path to emit "No Chrome tabs are attached..." or "stale targetId?" guidance on retry failure.
  • Test coverage is solid: 8 new tests cover all key paths including the no-retry guard, retry-success, retry-failure (tabs absent / present), and non-chrome passthrough.

Maintainability note: The navigate handler in browser-tool.ts duplicates the throwWithTabContext logic due to the helper not being exported. While functionally correct, exporting the helper and reusing it would prevent divergence and improve maintainability.

Confidence Score: 4/5

  • Safe to merge — all logic is correct and well-tested; the only concern is code duplication in the navigate handler that doesn't affect correctness.
  • The PR achieves its stated goal of hardening stale-target recovery with sound error detection improvements and solid test coverage. The logic is correct across all three files. Score is 4 rather than 5 because the throwWithTabContext helper is not exported, leaving the navigate handler with a maintenance concern: identical error guidance logic is duplicated in two places, creating a risk that future updates to error messages could diverge or be missed.
  • src/agents/tools/browser-tool.ts — The navigate retry catch block should be refactored to use the exported throwWithTabContext helper instead of duplicating the logic.

Last reviewed commit: 2c7af59

Comment on lines +549 to +569
} 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 },
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
} 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant