Skip to content

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

Isolate keyed Cloud conversations and reset independent task roots

Barack Sokullu9e6faa3ad3d1
diff --git a/package.json b/package.json
index 6c2474371..d7ac613dc 100644
--- a/package.json
+++ b/package.json
@@ -16,7 +16,7 @@
"test:attachment-drop": "node test/attachment-drop.mjs",
"test:workflow-editor": "node --test test/workflow-editor.mjs",
"test:security": "node test/security/injection-corpus.mjs",
- "test:cloud-bridge-approval": "node --test test/cloud-bridge-approval.mjs",
+ "test:cloud-bridge-approval": "node --test test/cloud-bridge-approval.mjs test/cloud-conversation-isolation.mjs",
"test:discord": "node --test test/discord-guard.mjs",
"test:toolbar-guard": "node test/rich-text-toolbar-guard.mjs",
"test:pdf-read": "node test/pdf-read.mjs",
diff --git a/src/chrome/src/cloud-runs.js b/src/chrome/src/cloud-runs.js
index 6be36b3bc..9f39e1525 100644
--- a/src/chrome/src/cloud-runs.js
+++ b/src/chrome/src/cloud-runs.js
@@ -583,6 +583,7 @@ function compactCloudRunForPersistence(run) {
workflowId: run?.workflowId || null,
traceRunId: run?.traceRunId || null,
parentRunId: run?.parentRunId || null,
+ conversationKey: run?.conversationKey || null,
mode: run?.mode || 'act',
captchaDiagnostics: run?.captchaDiagnostics || null,
tabId: run?.tabId,
@@ -697,6 +698,7 @@ export function createCloudRunController({
const api = chromeApi;
const runs = new Map();
const startingTabs = new Set();
+ const startingConversations = new Set();
let hydratePromise = null;
let persistQueue = Promise.resolve();
let persistTimer = null;
@@ -1008,12 +1010,36 @@ export function createCloudRunController({
}
async function startRun(msg = {}) {
+ const key = msg.conversationKey ?? msg.conversation_key;
+ if (key != null && startingConversations.has(key)) throw cloudRunError('Conversation is already starting.', 409);
+ if (key != null) startingConversations.add(key);
+ try { return await startRunReserved(msg); }
+ finally { if (key != null) startingConversations.delete(key); }
+ }
+
+ async function startRunReserved(msg = {}) {
await hydrate();
const suppliedRunId = msg.runId ?? msg.run_id;
const requestedRunId = suppliedRunId == null ? '' : String(suppliedRunId).trim();
const parentRunId = String(msg.parentRunId || msg.parent_run_id || '').trim() || null;
let parentRun = null;
let requestedTabId = msg.tabId ?? msg.tab_id;
+ const conversationKey = msg.conversationKey ?? msg.conversation_key ?? null;
+ if (conversationKey != null && (
+ typeof conversationKey !== 'string'
+ || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(conversationKey)
+ || parentRunId || requestedTabId != null || msg._workflow
+ )) throw cloudRunError('conversation_key requires a UUID and an independent run without an explicit tab.', 400);
+ if (conversationKey) {
+ // Each caller-owned task/chat gets its own tab in the same browser profile.
+ // A fresh root resets only its own conversation; child messages retain it.
+ const previous = [...runs.values()].filter(run => run.conversationKey === conversationKey)
+ .sort((a, b) => String(b.createdAt).localeCompare(String(a.createdAt)))[0];
+ let tab = previous ? await api.tabs.get(previous.tabId).catch(() => null) : null;
+ if (!isUsableCloudTab(tab)) tab = await api.tabs.create({ url: 'about:blank', active: true });
+ if (tab?.id == null) throw cloudRunError('Could not create a conversation tab.', 500);
+ requestedTabId = tab.id;
+ }
if (parentRunId) {
parentRun = runs.get(parentRunId) || null;
if (parentRun) {
@@ -1064,6 +1090,7 @@ export function createCloudRunController({
startingTabs.add(tabId);
try {
await agent.assertRunStartAllowed?.(tabId, 'cloud', { cloudRun: true });
+ if (conversationKey) await agent.clearConversation(tabId);
const targetTab = await api.tabs.get(tabId);
await activateTab(targetTab);
} catch (error) {
@@ -1073,6 +1100,7 @@ export function createCloudRunController({
const createdAt = isoNow();
const run = {
runId,
+ conversationKey: conversationKey || parentRun?.conversationKey || null,
status: 'running',
workflowId: workflow?.id || null,
traceRunId: null,
diff --git a/src/firefox/src/cloud-runs.js b/src/firefox/src/cloud-runs.js
index f4a08e83f..a76e48e82 100644
--- a/src/firefox/src/cloud-runs.js
+++ b/src/firefox/src/cloud-runs.js
@@ -583,6 +583,7 @@ function compactCloudRunForPersistence(run) {
workflowId: run?.workflowId || null,
traceRunId: run?.traceRunId || null,
parentRunId: run?.parentRunId || null,
+ conversationKey: run?.conversationKey || null,
mode: run?.mode || 'act',
captchaDiagnostics: run?.captchaDiagnostics || null,
tabId: run?.tabId,
@@ -699,6 +700,7 @@ export function createCloudRunController({
const api = chromeApi;
const runs = new Map();
const startingTabs = new Set();
+ const startingConversations = new Set();
let hydratePromise = null;
let persistQueue = Promise.resolve();
let persistTimer = null;
@@ -1010,12 +1012,36 @@ export function createCloudRunController({
}
async function startRun(msg = {}) {
+ const key = msg.conversationKey ?? msg.conversation_key;
+ if (key != null && startingConversations.has(key)) throw cloudRunError('Conversation is already starting.', 409);
+ if (key != null) startingConversations.add(key);
+ try { return await startRunReserved(msg); }
+ finally { if (key != null) startingConversations.delete(key); }
+ }
+
+ async function startRunReserved(msg = {}) {
await hydrate();
const suppliedRunId = msg.runId ?? msg.run_id;
const requestedRunId = suppliedRunId == null ? '' : String(suppliedRunId).trim();
const parentRunId = String(msg.parentRunId || msg.parent_run_id || '').trim() || null;
let parentRun = null;
let requestedTabId = msg.tabId ?? msg.tab_id;
+ const conversationKey = msg.conversationKey ?? msg.conversation_key ?? null;
+ if (conversationKey != null && (
+ typeof conversationKey !== 'string'
+ || !/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(conversationKey)
+ || parentRunId || requestedTabId != null || msg._workflow
+ )) throw cloudRunError('conversation_key requires a UUID and an independent run without an explicit tab.', 400);
+ if (conversationKey) {
+ // Each caller-owned task/chat gets its own tab in the same browser profile.
+ // A fresh root resets only its own conversation; child messages retain it.
+ const previous = [...runs.values()].filter(run => run.conversationKey === conversationKey)
+ .sort((a, b) => String(b.createdAt).localeCompare(String(a.createdAt)))[0];
+ let tab = previous ? await api.tabs.get(previous.tabId).catch(() => null) : null;
+ if (!isUsableCloudTab(tab)) tab = await api.tabs.create({ url: 'about:blank', active: true });
+ if (tab?.id == null) throw cloudRunError('Could not create a conversation tab.', 500);
+ requestedTabId = tab.id;
+ }
if (parentRunId) {
parentRun = runs.get(parentRunId) || null;
if (parentRun) {
@@ -1066,6 +1092,7 @@ export function createCloudRunController({
startingTabs.add(tabId);
try {
await agent.assertRunStartAllowed?.(tabId, 'cloud', { cloudRun: true });
+ if (conversationKey) await agent.clearConversation(tabId);
const targetTab = await api.tabs.get(tabId);
await activateTab(targetTab);
} catch (error) {
@@ -1075,6 +1102,7 @@ export function createCloudRunController({
const createdAt = isoNow();
const run = {
runId,
+ conversationKey: conversationKey || parentRun?.conversationKey || null,
status: 'running',
workflowId: workflow?.id || null,
traceRunId: null,
diff --git a/test/cloud-conversation-isolation.mjs b/test/cloud-conversation-isolation.mjs
new file mode 100644
index 000000000..360c42f34
--- /dev/null
+++ b/test/cloud-conversation-isolation.mjs
@@ -0,0 +1,56 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+
+const key1 = '00000000-0000-4000-8000-000000000001';
+const key2 = '00000000-0000-4000-8000-000000000002';
+for (const browser of ['chrome', 'firefox']) {
+ const { createCloudRunController } = await import(`../src/${browser}/src/cloud-runs.js`);
+ test(`${browser}: keyed roots isolate expired guards, reuse tabs after restart, and retain child conversations`, async () => {
+ const stored = {}, tabs = new Map([[7, { id: 7, url: 'https://example.com' }]]);
+ const guards = new Set([7]), clears = [], observed = [];
+ let tabSeq = 20, runSeq = 0, hold = null;
+ const api = {
+ storage: { session: { get: async key => ({ [key]: stored[key] }), set: async data => Object.assign(stored, data) } },
+ tabs: { get: async id => { if (!tabs.has(id)) throw new Error('Closed tab'); return tabs.get(id); },
+ query: async () => [tabs.get(7)], update: async id => tabs.get(id),
+ create: async options => { const tab = { id: tabSeq++, url: options.url }; tabs.set(tab.id, tab); return tab; } },
+ };
+ const agent = { isRunning: () => false,
+ clearConversation: async id => { clears.push(id); guards.delete(id); },
+ processMessage: async (id, task) => { observed.push({ id, task, guarded: guards.has(id) }); if (hold) await hold; return task; },
+ };
+ const make = () => createCloudRunController({ chromeApi: api, agent, ensureOffscreen: async () => {}, makeRunId: () => 'run_' + (++runSeq) });
+ let c = make();
+ async function finished(run) {
+ for (let i = 0; i < 50 && c.runs.get(run.runId).status === 'running'; i++) await new Promise(r => setTimeout(r, 5));
+ assert.equal(c.runs.get(run.runId).status, 'completed');
+ }
+ const first = await c.startRun({ conversationKey: key1, task: 'first' }); await finished(first);
+ assert.equal(first.tabId, 20); assert.ok(guards.has(7));
+ guards.add(20);
+ const second = await c.startRun({ conversationKey: key1, task: 'second' }); await finished(second);
+ assert.equal(second.tabId, 20); assert.equal(observed.at(-1).guarded, false);
+ const chat = await c.startRun({ conversationKey: key2, task: 'chat' }); await finished(chat);
+ assert.equal(chat.tabId, 21);
+ const before = clears.length;
+ const child = await c.startRun({ parentRunId: chat.runId, task: 'continue' }); await finished(child);
+ assert.equal(child.tabId, 21); assert.equal(clears.length, before);
+ c = make();
+ const restarted = await c.startRun({ conversationKey: key1, task: 'after restart' }); await finished(restarted);
+ assert.equal(restarted.tabId, 20);
+ tabs.delete(20);
+ const reopened = await c.startRun({ conversationKey: key1, task: 'closed tab' }); await finished(reopened);
+ assert.equal(reopened.tabId, 22);
+ let release;
+ hold = new Promise(r => { release = r; });
+ const racing = await Promise.allSettled([c.startRun({ conversationKey: key1, task: 'held' }), c.startRun({ conversationKey: key1, task: 'race' })]);
+ assert.equal(racing.filter(r => r.status === 'fulfilled').length, 1);
+ const clearCount = clears.length;
+ await assert.rejects(c.startRun({ conversationKey: key1, task: 'busy' }), /active|starting/);
+ assert.equal(clears.length, clearCount);
+ release(); hold = null; await finished(racing.find(r => r.status === 'fulfilled').value);
+ await assert.rejects(c.startRun({ conversationKey: key1, tabId: 7, task: 'conflict' }), /independent/);
+ await assert.rejects(c.startRun({ conversationKey: 'secret-not-a-uuid', task: 'invalid' }), /UUID/);
+ assert.ok(guards.has(7), 'unrelated conversation never reset');
+ });
+}