Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix: guard cloud startup and compact atomic recovery retries
Emre Sokullucb4422ce3930
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 05caa7b36..c5a066869 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -19641,18 +19641,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}const snapshot = snapshotForBudget || (maxBytes => this._conversationStorageEntry(tabId, { maxBytes }));const entry = snapshot(SESSION_CONVERSATION_BUDGET_BYTES);+ const extraEntries = retry => typeof storageEntries === 'function' ? storageEntries({ retry }) : storageEntries;const failed = error => preserveStateOnFailure? { ok: false, degraded: false, reason: isSessionQuotaError(error) ? 'quota' : 'unavailable' }: this._markPersistenceDegraded(tabId, isSessionQuotaError(error) ? 'quota' : 'unavailable', error);if (!entry) return { ok: false, degraded: false, reason: 'no_conversation' };try {- await chrome.storage.session.set({ ...storageEntries, [this._convKey(tabId)]: entry });+ await chrome.storage.session.set({ ...extraEntries(false), [this._convKey(tabId)]: entry });return { ok: true, degraded: entry.sessionSnapshotCompacted === true, reason: entry.sessionSnapshotCompacted ? 'sanitized' : null };} catch (error) {if (isSessionQuotaError(error)) {const compactEntry = snapshot(SESSION_CONVERSATION_RETRY_BUDGET_BYTES);try {- await chrome.storage.session.set({ ...storageEntries, [this._convKey(tabId)]: compactEntry });+ await chrome.storage.session.set({ ...extraEntries(true), [this._convKey(tabId)]: compactEntry });return { ok: true, degraded: true, reason: 'quota_compacted' };} catch (retryError) {return failed(retryError);diff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex b445f7e0c..f247912f7 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -2152,7 +2152,8 @@ const recoveringHistoryTabs = new Set();function assertNoActiveTabRun(tabId) {if (recoveringHistoryTabs.has(tabId)) throw new Error('Conversation recovery is in progress.');- if (agent.activeRunState(tabId)?.running || detachedRunStarts.has(tabId) || scheduler.isRunning(tabId)) {+ if (agent.activeRunState(tabId)?.running || detachedRunStarts.has(tabId)+ || scheduler.isRunning(tabId) || cloudRunController.isRunning(tabId)) {throw new Error('A run is already active for this tab.');}}diff --git a/src/chrome/src/ui/tab-chat-persistence.js b/src/chrome/src/ui/tab-chat-persistence.jsindex aea0446aa..2891c0e24 100644--- a/src/chrome/src/ui/tab-chat-persistence.js+++ b/src/chrome/src/ui/tab-chat-persistence.js@@ -415,13 +415,12 @@ export function createTabChatHandoffCoordinator(storageArea, {}if (typeof html !== 'string' || typeof commit !== 'function') throw new Error('No recovered transcript.');const generation = current.generation + 1;- const persistedHtml = compactTabChatForPersist(html);// The model snapshot, transcript, and ownership generation are one storage// transaction. A failed write leaves the previous conversation intact.- const state = await commit({- [TAB_CHAT_PREFIX + queuedTabId]: persistedHtml,+ const state = await commit(({ retry } = {}) => ({+ [TAB_CHAT_PREFIX + queuedTabId]: compactTabChatForPersist(html, retry ? TAB_CHAT_QUOTA_RETRY_BUDGET : TAB_CHAT_PERSIST_BUDGET),[TAB_CHAT_HANDOFF_PREFIX + queuedTabId]: { ownerId, generation },- });+ }));latestHtml.set(queuedTabId, html);return { ...state, html, handoffOwnerId: ownerId, handoffGeneration: generation };});diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 57c2cd504..4e750be6e 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -7834,18 +7834,19 @@ export class Agent extends LoopDetector {}const snapshot = snapshotForBudget || (maxBytes => this._conversationStorageEntry(tabId, { maxBytes }));const entry = snapshot(SESSION_CONVERSATION_BUDGET_BYTES);+ const extraEntries = retry => typeof storageEntries === 'function' ? storageEntries({ retry }) : storageEntries;const failed = error => preserveStateOnFailure? { ok: false, degraded: false, reason: isSessionQuotaError(error) ? 'quota' : 'unavailable' }: this._markPersistenceDegraded(tabId, isSessionQuotaError(error) ? 'quota' : 'unavailable', error);if (!entry) return { ok: false, degraded: false, reason: 'no_conversation' };try {- await browser.storage.session.set({ ...storageEntries, [this._convKey(tabId)]: entry });+ await browser.storage.session.set({ ...extraEntries(false), [this._convKey(tabId)]: entry });return { ok: true, degraded: entry.sessionSnapshotCompacted === true, reason: entry.sessionSnapshotCompacted ? 'sanitized' : null };} catch (error) {if (isSessionQuotaError(error)) {const compactEntry = snapshot(SESSION_CONVERSATION_RETRY_BUDGET_BYTES);try {- await browser.storage.session.set({ ...storageEntries, [this._convKey(tabId)]: compactEntry });+ await browser.storage.session.set({ ...extraEntries(true), [this._convKey(tabId)]: compactEntry });return { ok: true, degraded: true, reason: 'quota_compacted' };} catch (retryError) {return failed(retryError);diff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex 45f6c31ee..3d9357891 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -2344,7 +2344,8 @@ const recoveringHistoryTabs = new Set();function assertNoActiveTabRun(tabId) {if (recoveringHistoryTabs.has(tabId)) throw new Error('Conversation recovery is in progress.');- if (agent.activeRunState(tabId)?.running || detachedRunStarts.has(tabId) || scheduler.isRunning(tabId)) {+ if (agent.activeRunState(tabId)?.running || detachedRunStarts.has(tabId)+ || scheduler.isRunning(tabId) || cloudRunController.isRunning(tabId)) {throw new Error('A run is already active for this tab.');}}diff --git a/src/firefox/src/ui/tab-chat-persistence.js b/src/firefox/src/ui/tab-chat-persistence.jsindex a7c87c0f1..406695f36 100644--- a/src/firefox/src/ui/tab-chat-persistence.js+++ b/src/firefox/src/ui/tab-chat-persistence.js@@ -415,13 +415,12 @@ export function createTabChatHandoffCoordinator(storageArea, {}if (typeof html !== 'string' || typeof commit !== 'function') throw new Error('No recovered transcript.');const generation = current.generation + 1;- const persistedHtml = compactTabChatForPersist(html);// The model snapshot, transcript, and ownership generation are one storage// transaction. A failed write leaves the previous conversation intact.- const state = await commit({- [TAB_CHAT_PREFIX + queuedTabId]: persistedHtml,+ const state = await commit(({ retry } = {}) => ({+ [TAB_CHAT_PREFIX + queuedTabId]: compactTabChatForPersist(html, retry ? TAB_CHAT_QUOTA_RETRY_BUDGET : TAB_CHAT_PERSIST_BUDGET),[TAB_CHAT_HANDOFF_PREFIX + queuedTabId]: { ownerId, generation },- });+ }));latestHtml.set(queuedTabId, html);return { ...state, html, handoffOwnerId: ownerId, handoffGeneration: generation };});diff --git a/test/history-recovery-atomic.mjs b/test/history-recovery-atomic.mjsindex ea48133e7..aed42e67c 100644--- a/test/history-recovery-atomic.mjs+++ b/test/history-recovery-atomic.mjs@@ -1,10 +1,10 @@import assert from 'node:assert/strict';import { test } from 'node:test';-let session = {}, failWrite = false, writes = [];+let session = {}, failWrite = false, writes = [], writeBudget = Infinity;const area = {get: async key => key === null ? structuredClone(session) : { [key]: structuredClone(session[key]) },- set: async values => { writes.push(structuredClone(values)); if (failWrite) throw new Error('Storage unavailable'); Object.assign(session, structuredClone(values)); },+ set: async values => { writes.push(structuredClone(values)); if (failWrite) throw new Error('Storage unavailable'); if (JSON.stringify(values).length > writeBudget) throw new Error('QUOTA_BYTES quota exceeded'); Object.assign(session, structuredClone(values)); },remove: async key => { delete session[key]; },};globalThis.chrome = globalThis.browser = { alarms:{}, runtime:{ getURL:x=>x }, storage:{ session:area, local:{ get:async()=>({}), set:async()=>{} } } };@@ -15,13 +15,13 @@ for (const build of ['chrome','firefox']) {const archive = { id:'source', messages:[{role:'user',text:'recovered conversation',userTextSource:'verbatim'}] };async function fixture() {session = { 'tabChat:42':'previous transcript', 'tabChatHandoff:42':{ownerId:'panel',generation:1} };- failWrite = false; writes=[];+ failWrite = false; writes=[]; writeBudget=Infinity;const agent = new Agent({getActive:()=>({supportsVision:false})});agent.hydratedTabs.add(42);agent.getConversation(42,'ask').push({role:'user',content:'previous conversation'});await agent._persistNow(42);const handoff = createTabChatHandoffCoordinator(area);- const recover = (generation=1) => handoff.replace(42,'recovered transcript',{+ const recover = (generation=1, html='recovered transcript') => handoff.replace(42,html,{ownerId:'panel', handoffGeneration:generation,commit:storageEntries=>agent.recoverChatHistory(42,archive,{storageEntries}),});@@ -55,6 +55,21 @@ for (const build of ['chrome','firefox']) {assert.equal(f.agent.conversations.get(42)[1].content,'previous conversation');assert.equal((await f.handoff.load(42)).html,'previous transcript');});+ test(`${build}: quota retry compacts both transcript and model in the same write`,async()=>{+ const f=await fixture();+ const html='<div class="message assistant"><div class="message-text">'+ 'old context '.repeat(60000) +' latest result</div></div>';+ writeBudget=300*1024; writes=[];+ const result=await f.recover(1,html);+ assert.equal(writes.length,2);+ assert.ok(writes[0]['tabChat:42'].length>writeBudget);+ assert.ok(writes[1]['tabChat:42'].length<=256*1024);+ assert.match(writes[1]['tabChat:42'],/latest result/);+ assert.equal(writes[1]['agentConv:42'].conversationId,result.conversationId);+ assert.equal(writes[1]['tabChatHandoff:42'].generation,2);+ assert.equal(session['agentConv:42'].conversationId,result.conversationId);+ assert.equal(session['tabChat:42'],writes[1]['tabChat:42']);+ assert.equal((await f.handoff.load(42)).html,html,'The live cache keeps the lossless transcript');+ });test(`${build}: stale recovery owner is rejected before changing model context`,async()=>{const f=await fixture(), old=structuredClone(session);await assert.rejects(f.recover(99),/panel changed/);diff --git a/test/history-recovery-scheduler.mjs b/test/history-recovery-scheduler.mjsindex a3ba68e85..1f07223ae 100644--- a/test/history-recovery-scheduler.mjs+++ b/test/history-recovery-scheduler.mjs@@ -16,19 +16,22 @@ const deferred = () => {};function backgroundGuards(source, agent, scheduler, locks, cloudGuard = async () => {}) {+ const cloudRunController = typeof cloudGuard === 'function'+ ? { guardRunStart: cloudGuard, isRunning: () => false }+ : cloudGuard;const start = source.indexOf('agent.setRunStartGuard(async (tabId, context) => {');const end = source.indexOf('\nconst profileSync', start);assert.ok(start >= 0 && end > start, 'The production run-start guard must be exercised');let runStartGuard;new Function('agent', 'recoveringHistoryTabs', 'cloudRunController', 'teacherRunInterlock', source.slice(start, end))({ setRunStartGuard: guard => { runStartGuard = guard; } }, locks,- { guardRunStart: cloudGuard }, { guardRunStart: async () => {} },+ cloudRunController, { guardRunStart: async () => {} },);const assertStart = source.indexOf('function assertNoActiveTabRun(tabId) {');const assertEnd = source.indexOf('\nasync function activateForegroundCompatibilityTab', assertStart);assert.ok(assertStart >= 0 && assertEnd > assertStart, 'The production recovery guard must be exercised');- const assertNoActiveTabRun = new Function('agent', 'scheduler', 'recoveringHistoryTabs', 'detachedRunStarts',- `${source.slice(assertStart, assertEnd)}\nreturn assertNoActiveTabRun;`)(agent, scheduler, locks, new Map());+ const assertNoActiveTabRun = new Function('agent', 'scheduler', 'cloudRunController', 'recoveringHistoryTabs', 'detachedRunStarts',+ `${source.slice(assertStart, assertEnd)}\nreturn assertNoActiveTabRun;`)(agent, scheduler, cloudRunController, locks, new Map());return { runStartGuard, assertNoActiveTabRun };}@@ -42,10 +45,41 @@ async function createTask(h) {return result.jobId;}+function cloudFixture(createCloudRunController, { clear = async () => {}, process = async () => 'Finished' } = {}) {+ const stored = {}, counts = { clears: 0, dispatches: 0 };+ const tab = { id: 77, windowId: 1, url: 'https://example.com/' };+ const api = {+ storage: { session: {+ get: async key => ({ [key]: structuredClone(stored[key]) }),+ set: async values => Object.assign(stored, structuredClone(values)),+ } },+ tabs: {+ get: async () => tab, query: async () => [tab], create: async () => tab,+ update: async () => tab, remove: async () => {},+ },+ };+ const agent = {+ isRunning: () => false, activeRunState: () => ({ running: false }), abort: () => {},+ clearConversation: async () => { counts.clears++; await clear(); },+ processMessage: async (...args) => { counts.dispatches++; return process(...args); },+ };+ const controller = createCloudRunController({ chromeApi: api, agent, ensureOffscreen: async () => {} });+ return { agent, controller, counts };+}++async function waitForCloudIdle(controller) {+ for (let attempt = 0; attempt < 100; attempt++) {+ if (!controller.isRunning(77)) return;+ await new Promise(resolve => setImmediate(resolve));+ }+ assert.fail('Cloud run did not release its tab after the test gates settled');+}+for (const build of ['chrome', 'firefox']) {const source = await readFile(`src/${build}/src/background.js`, 'utf8');const scheduler = await import(`../src/${build}/src/agent/scheduler.js`);const { Agent } = await import(`../src/${build}/src/agent/agent.js`);+ const { createCloudRunController } = await import(`../src/${build}/src/cloud-runs.js`);test(`${build}: history recovery blocks scheduler reservation and defers the job without losing it`, async () => {let dispatches = 0, cloudChecks = 0;@@ -116,4 +150,63 @@ for (const build of ['chrome', 'firefox']) {assert.equal(agent.isRunning(77), false);assert.deepEqual([...locks], [77]);});++ test(`${build}: cloud startup reservation blocks recovery before and during conversation reset`, async () => {+ const enteredGuard = deferred(), releaseGuard = deferred(), enteredClear = deferred(), releaseClear = deferred();+ const enteredProcess = deferred(), releaseProcess = deferred(), locks = new Set();+ const h = cloudFixture(createCloudRunController, {+ clear: async () => { enteredClear.resolve(); await releaseClear.promise; },+ process: async () => { enteredProcess.resolve(); await releaseProcess.promise; return 'Finished'; },+ });+ const cloudGuard = h.controller.guardRunStart;+ h.controller.guardRunStart = async (...args) => {+ enteredGuard.resolve(); await releaseGuard.promise; return cloudGuard(...args);+ };+ const guards = backgroundGuards(source, h.agent, { isRunning: () => false }, locks, h.controller);+ h.agent.assertRunStartAllowed = (tabId, kind, runOptions) => guards.runStartGuard(tabId, { kind, runOptions });+ const starting = h.controller.startRun({ runId: 'cloud-startup', task: 'Read this page', mode: 'ask',+ conversationKey: '10000000-0000-4000-8000-000000000001' });+ try {+ await enteredGuard.promise;+ assert.equal(h.agent.isRunning(77), false);+ assert.equal(h.controller.isRunning(77), true, 'Cloud owns setup before Agent claims the run');+ assert.equal(h.counts.clears, 0);+ assert.throws(() => guards.assertNoActiveTabRun(77), /already active/);+ releaseGuard.resolve();+ await enteredClear.promise;+ assert.equal(h.agent.isRunning(77), false);+ assert.equal(h.counts.dispatches, 0);+ assert.throws(() => guards.assertNoActiveTabRun(77), /already active/);+ releaseClear.resolve();+ await enteredProcess.promise;+ assert.equal((await starting).runId, 'cloud-startup');+ assert.throws(() => guards.assertNoActiveTabRun(77), /already active/);+ } finally {+ releaseGuard.resolve(); releaseClear.resolve(); releaseProcess.resolve();+ await starting;+ await waitForCloudIdle(h.controller);+ }+ assert.equal(h.counts.clears, 1);+ assert.equal(h.counts.dispatches, 1);+ assert.doesNotThrow(() => guards.assertNoActiveTabRun(77));+ });++ test(`${build}: history recovery rejects cloud startup before conversation reset and releases the reservation`, async () => {+ const locks = new Set([77]);+ const h = cloudFixture(createCloudRunController);+ const guards = backgroundGuards(source, h.agent, { isRunning: () => false }, locks, h.controller);+ h.agent.assertRunStartAllowed = (tabId, kind, runOptions) => guards.runStartGuard(tabId, { kind, runOptions });+ const request = { runId: 'cloud-retry', task: 'Read this page', mode: 'ask',+ conversationKey: '20000000-0000-4000-8000-000000000002' };+ await assert.rejects(h.controller.startRun(request), /recovery is in progress/);+ assert.equal(h.controller.isRunning(77), false);+ assert.equal(h.controller.runs.size, 0);+ assert.deepEqual(h.counts, { clears: 0, dispatches: 0 });+ assert.deepEqual([...locks], [77]);+ locks.delete(77);+ assert.equal((await h.controller.startRun(request)).runId, 'cloud-retry');+ await waitForCloudIdle(h.controller);+ assert.deepEqual(h.counts, { clears: 1, dispatches: 1 });+ assert.doesNotThrow(() => guards.assertNoActiveTabRun(77));+ });}