#16743: fix: auto-reattach browser relay debugger after navigation
size: M
Cluster:
Chrome Extension Enhancements
## Summary
- Auto-reattach the Chrome extension debugger when Chrome detaches it during page navigation (cross-origin redirects, OAuth flows, SPA navigation)
- Previously, any navigation that triggered a debugger detach permanently lost the tab connection, requiring manual re-click of the extension icon
- User-initiated cancellation (clicking "Cancel" on the debugging infobar) is still respected
## Problem
When using the browser relay extension, navigating a tab through a redirect chain (e.g. OAuth/Clerk auth flow) causes Chrome to detach the debugger. The `onDebuggerDetach` handler unconditionally removed the tab from tracking, making all subsequent browser actions fail with `"tab not found"`. There was no recovery path — the user had to manually re-click the extension icon.
## Fix
In `onDebuggerDetach`, instead of immediately detaching:
1. Check if the reason is `canceled_by_user` — if so, respect it and detach as before
2. Otherwise, set the badge to "connecting" and listen for the tab to finish loading (`chrome.tabs.onUpdated` with `status === 'complete'`)
3. Re-attach the debugger via `attachTab()`, which sends a fresh `Target.attachedToTarget` event to the relay
4. Give up after 10 seconds if the tab never finishes loading
No server-side relay changes needed — `attachTab()` already sends the correct events.
## Test plan
- [ ] Navigate a relay-attached tab to a different origin — badge should briefly show "…" then return to "ON"
- [ ] OAuth redirect flow (e.g. Clerk, Google OAuth) — relay should survive the redirect chain
- [ ] Click "Cancel" on Chrome's debugging infobar — should still properly detach
- [ ] Close a relay-attached tab — should clean up without errors
- [ ] Tab that fails to load within 10 seconds — should gracefully detach
Fixes #11675
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds auto-reattach logic for the Chrome extension's browser relay debugger after transient navigation events (cross-origin redirects, OAuth flows, SPA navigation). Previously, any debugger detach permanently lost the tab connection.
- Extracts `handleDebuggerDetach` into a new `background-logic.js` module with dependency injection for testability
- On transient detach: sets badge to "connecting", waits for tab load (`status === 'complete'`), cleans up the old session, then re-attaches via `attachTab()`
- Respects permanent detach reasons (`canceled_by_user`, `replaced_with_devtools`) — these still fully detach
- Guards against rapid navigation with a `pendingReattach` map that cancels previous in-flight reattach attempts
- Cleans up listeners (`onUpdated`, `onRemoved`) and timeouts on all resolution paths (success, failure, tab close, timeout)
- Cancels all pending reattaches when the relay connection closes
- Adds `cancelAllPendingReattach` call in `detachTab` to prevent stale reattach attempts
- Comprehensive test suite (291 lines) covering guards, permanent/transient detach, rapid navigation, timeout, and error fallback paths
- Unrelated: documents Slack `assistant:write` scope requirement for typing status indicators
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge — well-tested reattach logic with proper cleanup on all paths and no server-side changes needed.
- Score of 4 reflects clean, well-structured code with comprehensive test coverage (15 test cases), proper resource cleanup, and no identified logic bugs. All previous review issues have been addressed. Slight deduction because this is browser extension code that handles complex async state transitions which are inherently tricky to verify without integration testing in a real Chrome environment.
- No files require special attention. All changes are well-tested and follow established patterns in the codebase.
<sub>Last reviewed commit: d713eb9</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19766: fix: Chrome relay extension auto-reattach after SPA navigation
by nishantkabra77 · 2026-02-18
91.4%
#17588: fix(relay): survive WS disconnects and MV3 worker restarts
by Unayung · 2026-02-15
83.4%
#10390: fix(chrome-relay): sticky attach + auto-restore after disconnects
by Mokoby · 2026-02-06
82.4%
#13942: Fix: chrome-extension: make relay reconnect reliable with one tab
by gabepsilva · 2026-02-11
80.8%
#15817: fix(chrome-relay): auto-reconnect, MV3 persistence, and keepalive
by derrickburns · 2026-02-13
80.5%
#20688: fix(browser): allow extension reconnect when stale websocket linger...
by HOYALIM · 2026-02-19
75.4%
#21314: feat: enhance browser relay with custom naming and diagnostic tools
by kelvinCB · 2026-02-19
75.2%
#16030: fix(browser): extend single-tab fallback to remote CDP profiles
by lailoo · 2026-02-14
75.1%
#22571: fix(browser): complete extension relay handshake on connect.challenge
by pandego · 2026-02-21
74.6%
#23363: Browser: fallback to managed profile and improve runtime diagnostics
by isdoho · 2026-02-22
74.0%