fix(connect): stable per-tab relay session id — re-attach auto-recovers (#17)
When a tab's chrome.debugger session was torn down and re-established (cross-process navigation, MV3 service-worker restart wiping the in-memory maps, DevTools stealing the debugger), the extension minted a brand-new monotonic `cb-tab-N` for the same tab. The daemon stays bound to the old id and the relay consumes attach/detach events without telling it to rebind, so the session was orphaned permanently → `stale sessionId / tab is gone`, and re-open never recovered. Derive the session id from the STABLE Chrome tabId (`cb-tab-<tabId>`) instead. Any re-attach of the same tab now restores the SAME session the daemon already holds, so eval/snapshot transparently follow the new page after a navigation. Extension 0.4.3 → 0.4.4. Adds a relay unit test for the detach→reattach-same- session recovery contract.
This commit is contained in:
@@ -24,7 +24,6 @@ let port = null
|
||||
/** Whether the native-messaging host (the local chrome-use CLI) is linked.
|
||||
* Read by the popup status page. */
|
||||
let hostConnected = false
|
||||
let nextSession = 1
|
||||
/** tabId -> { sessionId, targetId } */
|
||||
const tabs = new Map()
|
||||
/** sessionId -> tabId (main session per tab) */
|
||||
@@ -261,7 +260,16 @@ async function attachTab(tabId) {
|
||||
const targetInfo = info?.targetInfo
|
||||
const targetId = String(targetInfo?.targetId || '')
|
||||
if (!targetId) throw new Error('attachTab: no targetId')
|
||||
const sessionId = `cb-tab-${nextSession++}`
|
||||
// Derive the session id from the STABLE Chrome tabId, not a monotonic counter
|
||||
// (issue #17). A tab's chrome.debugger session can be torn down and
|
||||
// re-established — cross-process navigation, a service-worker restart wiping
|
||||
// these in-memory maps, DevTools stealing the debugger — and each time the tab
|
||||
// re-attaches. With a counter, re-attach minted a BRAND-NEW `cb-tab-N`, which
|
||||
// orphaned the daemon's binding (it's still pinned to the old id and the relay
|
||||
// never tells it to rebind) → permanent "stale sessionId / tab is gone". The
|
||||
// tabId is stable across all of that, so `cb-tab-<tabId>` restores the SAME
|
||||
// session the daemon already holds → eval/snapshot auto-follow the new page.
|
||||
const sessionId = `cb-tab-${tabId}`
|
||||
const entry = { sessionId, targetId }
|
||||
tabs.set(tabId, entry)
|
||||
sessionToTab.set(sessionId, tabId)
|
||||
|
||||
Reference in New Issue
Block a user