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
Record the guide's Pause before stopping D1
Claude695dad9ef2ee
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex c11fe0189..546a168a1 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -5036,14 +5036,16 @@ async function handleMessage(msg, sender) {// Unload, Remove and switching D1 off in Settings end that request.await api.storage.local.set({ [D1_KEEP_LOADED_KEY]: false });}+ // The guide's Pause holds its queued chat model; it is recorded+ // before D1 stops, so no check sees the stop without it.+ if (fromInstall && msg.command === 'stop') await chatModelAfterD1.pause();const result = msg.command === 'consent' ? { consented: true } : await d1Request(msg.command, { consentVersion: D1_CONSENT_VERSION });if (!fromInstall) return { success: true, result };- // The chat model waits for D1's download, and its Pause; the guide's- // reads of D1's status start it once D1's download is over.+ // The chat model waits for D1's download; the guide's reads of D1's+ // status start it once D1's download is over.if (chatModel) await chatModelAfterD1.queue(chatModel);- else if (msg.command === 'stop') await chatModelAfterD1.pause();const chatModelQueued = msg.command === 'status'- ? await chatModelAfterD1.advance(result).catch((error) => {+ ? await chatModelAfterD1.advance().catch((error) => {console.warn('[WebBrain] The queued local chat-model download could not be checked:', error);return false;})diff --git a/src/chrome/src/providers/install-download-queue.js b/src/chrome/src/providers/install-download-queue.jsindex c73a1d08a..2d5ee0586 100644--- a/src/chrome/src/providers/install-download-queue.js+++ b/src/chrome/src/providers/install-download-queue.js@@ -34,8 +34,9 @@ export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, oreturn step;};- // Returns whether the chat model still waits for D1.- async function step(status) {+ // Returns whether the chat model still waits for D1. D1's status is read+ // here, after any Pause or Resume ahead of this step, never before it.+ async function step() {const queued = await read();if (!queued || typeof queued.model !== 'string' || !queued.model) {if (queued) await storage.remove(CHAT_AFTER_D1_KEY);@@ -44,8 +45,8 @@ export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, o}const age = now() - Number(queued.queuedAt);if (!(age >= 0 && age <= CHAT_AFTER_D1_MAX_AGE_MS)) { await clear(); return false; }- let d1 = status;- if (!d1) { try { d1 = await d1Status(); } catch { return true; } }+ let d1;+ try { d1 = await d1Status(); } catch { return true; }const d1State = String(d1?.status || '');if (d1?.loaded !== true && D1_TRANSFER_STATUSES.has(d1State)) return true;// Paused on the guide: its Resume carries on to the chat model.@@ -81,13 +82,13 @@ export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, oawait storage.set({ [CHAT_AFTER_D1_KEY]: { model, queuedAt: now(), paused: false } });try { await api.alarms?.create?.(CHAT_AFTER_D1_ALARM, { periodInMinutes: 0.5 }); } catch { /* the guide's reads still advance it */ }}),- // The guide paused D1: the chat model waits for its Resume.+ // The guide pauses D1: the chat model waits for its Resume. Called before+ // D1 is stopped, so no check sees the stop without the Pause.pause: () => serial(async () => {const queued = await read();if (queued?.model) await storage.set({ [CHAT_AFTER_D1_KEY]: { ...queued, paused: true } });}),queued: () => serial(async () => !!(await read())?.model),- // `status` is a D1 status just read; without one, D1 is asked.- advance: (status = null) => serial(() => step(status)),+ advance: () => serial(step),};}diff --git a/test/d1-decision.mjs b/test/d1-decision.mjsindex ee03ddddd..c3007dfcd 100644--- a/test/d1-decision.mjs+++ b/test/d1-decision.mjs@@ -294,8 +294,11 @@ for (const build of ['chrome', 'firefox']) {assert.match(branch, /const chatModel = fromInstall && msg\.command === 'download' && msg\.chatModel != null \? String\(msg\.chatModel\) : '';/);assert.match(branch, /if \(chatModel && !isSelectableWebgpuPreset\(chatModel\)\) throw new Error\('Unknown local chat model\.'\);/);assert.match(branch, /if \(!fromInstall\) return \{ success: true, result \};/);- assert.match(branch, /if \(chatModel\) await chatModelAfterD1\.queue\(chatModel\);\s*else if \(msg\.command === 'stop'\) await chatModelAfterD1\.pause\(\);/);- assert.match(branch, /msg\.command === 'status'\s*\? await chatModelAfterD1\.advance\(result\)/);+ assert.match(branch, /if \(chatModel\) await chatModelAfterD1\.queue\(chatModel\);/);+ // The Pause is recorded before D1 stops, so no check sees the stop alone.+ assert.ok(branch.indexOf("if (fromInstall && msg.command === 'stop') await chatModelAfterD1.pause();")+ < branch.indexOf('await d1Request(msg.command'), 'the Pause is recorded before D1 is stopped');+ assert.match(branch, /msg\.command === 'status'\s*\? await chatModelAfterD1\.advance\(\)/);assert.match(branch, /result: \{ \.\.\.result, usedForDecisions: await d1UsedForDecisions\(api\), chatModelQueued \}/);assert.match(background, /chrome\.alarms\.onAlarm\.addListener\(\(alarm\) => \{\s*if \(alarm\?\.name !== CHAT_AFTER_D1_ALARM\) return;\s*chatModelAfterD1\.advance\(\)/);assert.match(background, /onStartError: \(error\) => \{\s*console\.warn\(/, 'a failed start is reported and retried');@@ -562,12 +565,21 @@ test('chrome: the install guide queues its chat model behind D1, and the backgroassert.equal(await env.queue.queued(), false);}{- // A status the guide just read is used as it is.+ // D1's status is read inside each check, after any Pause ahead of it: a+ // Pause and a check at the same moment never start the chat model.const env = make({ d1: { status: 'downloading' } });await env.queue.queue(model);- assert.equal(await env.queue.advance({ status: 'cached', cached: true }), false);- assert.deepEqual(env.starts, [model]);- assert.equal(env.reads, 0);+ const pause = env.queue.pause();+ env.d1 = { status: 'stopped', progress: 50 };+ assert.equal(await env.queue.advance(), true, 'the Pause is seen with the stop');+ await pause;+ assert.deepEqual(env.starts, []);+ assert.ok(env.reads >= 1);+ // A status a caller read earlier (just before a Resume, say) is not used.+ env.d1 = { status: 'downloading' };+ await env.queue.queue(model);+ assert.equal(await env.queue.advance({ status: 'stopped' }), true);+ assert.deepEqual(env.starts, []);}{// A Pause on the guide holds the chat model until Resume queues it again;