Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Track attach generations to preserve newer sessions on late cancellation

Emre Sokullua12cfc665b97
diff --git a/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.js
index ec1bd2537..ca574ae0e 100644
--- a/src/chrome/src/cdp/cdp-client.js
+++ b/src/chrome/src/cdp/cdp-client.js
@@ -90,6 +90,7 @@ export class CDPClient {
constructor() {
this.sessions = new Map(); // tabId -> debugger session
this.attachPromises = new Map(); // tabId -> in-flight debugger attach
+ this.attachGenerations = new Map(); // tabId -> current attach generation
this.eventHandlers = new Map(); // tabId -> { eventName -> [handlers] }
this.devDiagnostics = new Map(); // tabId -> bounded console/network buffers
this.webMcpSessions = new Map(); // tabId -> WebMCP tools + pending invocations
@@ -193,6 +194,9 @@ export class CDPClient {
this._ensureDebuggerListeners();
+ const generation = (this.attachGenerations.get(tabId) || 0) + 1;
+ this.attachGenerations.set(tabId, generation);
+
const attachPromise = new Promise((resolve, reject) => {
chrome.debugger.attach({ tabId }, '1.3', async () => {
if (chrome.runtime.lastError) {
@@ -200,8 +204,12 @@ export class CDPClient {
return;
}
- if (attachPromise.cancelled) {
- try { chrome.debugger.detach({ tabId }, () => {}); } catch {}
+ const isCurrentGeneration = this.attachGenerations.get(tabId) === generation;
+
+ if (attachPromise.cancelled || !isCurrentGeneration) {
+ if (isCurrentGeneration && !this.sessions.has(tabId)) {
+ try { chrome.debugger.detach({ tabId }, () => {}); } catch {}
+ }
reject(new Error('Debugger attachment was cancelled'));
return;
}
diff --git a/test/browser-dialogs.mjs b/test/browser-dialogs.mjs
index b65a3697c..77a1cb4d2 100644
--- a/test/browser-dialogs.mjs
+++ b/test/browser-dialogs.mjs
@@ -274,8 +274,10 @@ test('canceled attachments are evicted immediately and allow fresh retries', asy
const previousChrome = globalThis.chrome;
const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
let attachCalls = 0;
+ let detachCalls = 0;
let firstAttachCallback = null;
let secondAttachCallback = null;
+ let detachListener = null;
globalThis.chrome = {
storage: { local: area, session: area },
runtime: { getURL: value => value },
@@ -286,9 +288,13 @@ test('canceled attachments are evicted immediately and allow fresh retries', asy
if (attachCalls === 1) firstAttachCallback = callback;
else secondAttachCallback = callback;
},
- detach: (_target, callback) => { callback?.(); },
+ detach: ({ tabId }, callback) => {
+ detachCalls++;
+ callback?.();
+ detachListener?.({ tabId }, 'canceled');
+ },
onEvent: { addListener: () => {} },
- onDetach: { addListener: () => {} },
+ onDetach: { addListener: fn => { detachListener = fn; } },
},
};
const { cdpClient } = await import('../src/chrome/src/cdp/cdp-client.js');
@@ -309,11 +315,14 @@ test('canceled attachments are evicted immediately and allow fresh retries', asy
assert.equal(session.tabId, 81);
assert.equal(cdpClient.sessions.has(81), true);
- // Late callback from the first attach must not corrupt the session
+ // Late callback from the first attach must not detach or corrupt the active retry session
firstAttachCallback?.();
+ assert.equal(detachCalls, 0);
assert.equal(cdpClient.sessions.has(81), true);
await cdpClient.detach(81);
+ assert.equal(detachCalls, 1);
+ assert.equal(cdpClient.sessions.has(81), false);
} finally {
cdpClient.stopDialogHandling(81);
globalThis.chrome = previousChrome;