Skip to content

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

Keep D1 loaded when Auto uses it; keep the guide's button busy until D1 starts

Claude8697c8ec7b61
diff --git a/docs/providers-and-models.md b/docs/providers-and-models.md
index 7dd46a405..0c5990384 100644
--- a/docs/providers-and-models.md
+++ b/docs/providers-and-models.md
@@ -152,10 +152,12 @@ claim. **Unload** or disabling outsourcing releases the Worker/GPU resources and
retains the cache; **Remove cached model** requires the model to be unloaded.
After **Download and load**, **Load** or the install guide's download, D1
stays loaded until it is unloaded, removed or switched off on the Providers
-page (decisions Off, or another provider saved under On; switching to Auto
-keeps it, since Auto uses it): a decision that finds it unloaded (a browser
-restart, a discarded stalled session, a lost GPU device, or a mode change that
-released the Worker) starts one reload from the verified cache in the
+page (decisions Off, or another provider saved under On). Switching to Auto
+keeps it loaded while Auto uses it; while Compass is the chat provider, Auto
+does not, so the Worker and its GPU memory are released and the Load request
+stays for later (`releaseD1OnDecisionChange` in `background.js`). A decision
+that finds D1 unloaded (a browser restart, a discarded stalled session, a lost
+GPU device, or that release) starts one reload from the verified cache in the
background and uses the fallback chain meanwhile. A reload never downloads. A decision whose caller
stops waiting (a judge timeout, Stop) is cancelled in the Worker, skipped if
it was still queued, and the loaded model stays.
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index dfd9d3d71..7886ae0c0 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -5,9 +5,25 @@ import { d1Request } from './providers/d1.js';
import { evaluateD1LocalFixture } from './providers/d1-diagnostic.js';
import { D1_CONSENT_KEY, D1_CONSENT_VERSION, D1_KEEP_LOADED_KEY } from './providers/d1-config.js';
-chrome.storage.onChanged.addListener((changes, area) => {
- if (area === 'local' && (changes.systemOneEnabled?.newValue === false || (changes.decisionMode && changes.decisionMode.newValue !== 'on') || (changes.decisionProvider && changes.decisionProvider.newValue !== 'webgpu_d1') || (changes[D1_CONSENT_KEY] && changes[D1_CONSENT_KEY].newValue !== D1_CONSENT_VERSION) || changes.strictSecretMode?.newValue === true)) void d1Request('reset').catch(() => {});
-});
+// D1's Worker and GPU memory are released when decisions are switched Off,
+// another decision provider is saved, consent changes or Strict Secret Mode
+// starts. Leaving On (for Auto) keeps D1 loaded while Auto uses it: D1 is
+// downloaded and Compass is not the chat provider (decision-availability.js).
+function releaseD1OnDecisionChange(changes, area) {
+ if (area !== 'local') return;
+ const reset = () => { void d1Request('reset').catch(() => {}); };
+ if ((changes.decisionMode && changes.decisionMode.newValue === 'off')
+ || (changes.decisionProvider && changes.decisionProvider.newValue !== 'webgpu_d1')
+ || (changes[D1_CONSENT_KEY] && changes[D1_CONSENT_KEY].newValue !== D1_CONSENT_VERSION)
+ || changes.strictSecretMode?.newValue === true) {
+ reset();
+ return;
+ }
+ if (changes.systemOneEnabled?.newValue === false || (changes.decisionMode && changes.decisionMode.newValue !== 'on')) {
+ void d1UsedForDecisions(chrome).then(used => { if (!used) reset(); }, reset);
+ }
+}
+chrome.storage.onChanged.addListener(releaseD1OnDecisionChange);
import { probeDecisionVision } from './agent/decision-vision-probe.js';
import { installSafeSocialBackground } from './safesocial/background.js';
import { createFeedbackHandoff } from './feedback-handoff.js';
diff --git a/src/chrome/src/ui/install.js b/src/chrome/src/ui/install.js
index 129ccc79a..ffe2c8a02 100644
--- a/src/chrome/src/ui/install.js
+++ b/src/chrome/src/ui/install.js
@@ -439,9 +439,13 @@ export function initializeLocalModelDownload({
} else {
render();
}
- // D1 downloads alongside, in its own worker; its line reports it.
- void d1?.start();
- await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
+ // D1 downloads alongside, in its own worker; its line reports it. The
+ // button stays busy until both downloads have started, so a Pause
+ // always finds D1's transfer to stop.
+ await Promise.all([
+ d1?.start(),
+ backgroundCommand(runtime, 'start_webgpu_download', { model: modelId }),
+ ]);
}
} catch (error) {
inFlight = false;
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index a7028310b..354b5d6fa 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -5,9 +5,25 @@ import { d1Request } from './providers/d1.js';
import { evaluateD1LocalFixture } from './providers/d1-diagnostic.js';
import { D1_CONSENT_KEY, D1_CONSENT_VERSION, D1_KEEP_LOADED_KEY } from './providers/d1-config.js';
-browser.storage.onChanged.addListener((changes, area) => {
- if (area === 'local' && (changes.systemOneEnabled?.newValue === false || (changes.decisionMode && changes.decisionMode.newValue !== 'on') || (changes.decisionProvider && changes.decisionProvider.newValue !== 'webgpu_d1') || (changes[D1_CONSENT_KEY] && changes[D1_CONSENT_KEY].newValue !== D1_CONSENT_VERSION) || changes.strictSecretMode?.newValue === true)) void d1Request('reset').catch(() => {});
-});
+// D1's Worker and GPU memory are released when decisions are switched Off,
+// another decision provider is saved, consent changes or Strict Secret Mode
+// starts. Leaving On (for Auto) keeps D1 loaded while Auto uses it: D1 is
+// downloaded and Compass is not the chat provider (decision-availability.js).
+function releaseD1OnDecisionChange(changes, area) {
+ if (area !== 'local') return;
+ const reset = () => { void d1Request('reset').catch(() => {}); };
+ if ((changes.decisionMode && changes.decisionMode.newValue === 'off')
+ || (changes.decisionProvider && changes.decisionProvider.newValue !== 'webgpu_d1')
+ || (changes[D1_CONSENT_KEY] && changes[D1_CONSENT_KEY].newValue !== D1_CONSENT_VERSION)
+ || changes.strictSecretMode?.newValue === true) {
+ reset();
+ return;
+ }
+ if (changes.systemOneEnabled?.newValue === false || (changes.decisionMode && changes.decisionMode.newValue !== 'on')) {
+ void d1UsedForDecisions(browser).then(used => { if (!used) reset(); }, reset);
+ }
+}
+browser.storage.onChanged.addListener(releaseD1OnDecisionChange);
import { probeDecisionVision } from './agent/decision-vision-probe.js';
import { installSafeSocialBackground } from './safesocial/background.js';
import { createFeedbackHandoff } from './feedback-handoff.js';
diff --git a/src/firefox/src/ui/install.js b/src/firefox/src/ui/install.js
index 129ccc79a..ffe2c8a02 100644
--- a/src/firefox/src/ui/install.js
+++ b/src/firefox/src/ui/install.js
@@ -439,9 +439,13 @@ export function initializeLocalModelDownload({
} else {
render();
}
- // D1 downloads alongside, in its own worker; its line reports it.
- void d1?.start();
- await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
+ // D1 downloads alongside, in its own worker; its line reports it. The
+ // button stays busy until both downloads have started, so a Pause
+ // always finds D1's transfer to stop.
+ await Promise.all([
+ d1?.start(),
+ backgroundCommand(runtime, 'start_webgpu_download', { model: modelId }),
+ ]);
}
} catch (error) {
inFlight = false;
diff --git a/test/d1-decision.mjs b/test/d1-decision.mjs
index 17f1db155..71d4daf24 100644
--- a/test/d1-decision.mjs
+++ b/test/d1-decision.mjs
@@ -315,6 +315,32 @@ for (const build of ['chrome', 'firefox']) {
});
}
});
+ test(`${build}: leaving On for Auto keeps D1 loaded while Auto uses it; Off and other changes release it`, async () => {
+ const background = await readFile(new URL(`../src/${build}/src/background.js`, import.meta.url), 'utf8');
+ assert.match(background, new RegExp(`${build === 'chrome' ? 'chrome' : 'browser'}\\.storage\\.onChanged\\.addListener\\(releaseD1OnDecisionChange\\);`));
+ const source = topLevelFunction(background, 'releaseD1OnDecisionChange');
+ const consent = pin.D1_CONSENT_KEY;
+ for (const [label, changes, used, expected, area = 'local'] of [
+ ['decisions Off', { decisionMode: { newValue: 'off' }, systemOneEnabled: { newValue: false } }, true, 1],
+ ['another provider saved', { decisionProvider: { newValue: 'openrouter' } }, true, 1],
+ ['consent removed', { [consent]: { newValue: undefined } }, true, 1],
+ ['Strict Secret Mode', { strictSecretMode: { newValue: true } }, true, 1],
+ ['Auto, which uses D1', { decisionMode: { newValue: 'auto' }, systemOneEnabled: { newValue: false } }, true, 0],
+ ['Auto while Compass chats', { decisionMode: { newValue: 'auto' }, systemOneEnabled: { newValue: false } }, false, 1],
+ ['On', { decisionMode: { newValue: 'on' }, systemOneEnabled: { newValue: true } }, false, 0],
+ ['consent given', { [consent]: { newValue: pin.D1_CONSENT_VERSION } }, false, 0],
+ ['an unrelated setting', { theme: { newValue: 'dark' } }, false, 0],
+ ['another storage area', { decisionMode: { newValue: 'off' } }, false, 0, 'sync'],
+ ]) {
+ const resets = [];
+ const context = vm.createContext({ D1_CONSENT_KEY: consent, D1_CONSENT_VERSION: pin.D1_CONSENT_VERSION, chrome: {}, browser: {},
+ d1Request: async command => { resets.push(command); return {}; }, d1UsedForDecisions: async () => used });
+ vm.runInContext(source, context);
+ context.releaseD1OnDecisionChange(changes, area);
+ await new Promise(resolve => setTimeout(resolve, 0));
+ assert.deepEqual(resets, expected ? ['reset'] : [], label);
+ }
+ });
test(`${build}: D1 is opt-in, endpoint/key-free, zero-cost and overrides Compass only with explicit local consent`, async () => {
const stored = { decisionProvider: 'webgpu_d1', systemOneEnabled: true };
assert.equal(config.resolveDecisionConfig(stored).enabled, false);
diff --git a/test/run.js b/test/run.js
index cdf428d29..a302f9fd4 100644
--- a/test/run.js
+++ b/test/run.js
@@ -45686,6 +45686,36 @@ test('chrome install page downloads D1 next to the chat model and says what D1 d
controller.dispose();
}
+ {
+ // The button stays busy until D1 has asked for its download too, so a
+ // Pause can never run before D1's transfer exists and leave D1 running.
+ const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'idle' } };
+ let releaseProbe;
+ const probe = new Promise((resolve) => { releaseProbe = resolve; });
+ const runtime = fakeRuntime({
+ ...bonsai(state),
+ 'd1_control:status': () => ({ success: true, result: { ...state.d1, usedForDecisions: false } }),
+ 'd1_control:probe': () => probe,
+ 'd1_control:download': () => { state.d1 = { status: 'downloading', progress: 1 }; return { success: true, result: { started: true, ...state.d1 } }; },
+ });
+ const documentLike = fakeDocument();
+ const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
+ await settle();
+ const button = documentLike.nodes['local-model-button'];
+ const clicked = button.fire('click');
+ await settle();
+ assert.ok(runtime.sent.some((message) => message.action === 'start_webgpu_download'), 'the chat model does not wait for D1');
+ assert.equal(button.disabled, true, 'busy while D1 still checks the GPU');
+ assert.equal(await Promise.race([clicked.then(() => 'done'), new Promise((resolve) => setTimeout(() => resolve('pending'), 20))]), 'pending');
+ releaseProbe({ success: true, result: { success: true } });
+ await clicked;
+ await settle();
+ assert.ok(runtime.sent.some((message) => message.action === 'd1_control' && message.command === 'download'));
+ assert.equal(button.disabled, false, 'Pause is offered once both downloads have started');
+ assert.equal(documentLike.nodes['local-model'].dataset.state, 'downloading');
+ controller.dispose();
+ }
+
{
// A GPU D1 cannot use: say so, and the chat model still downloads.
const state = { bonsai: { status: 'not-downloaded', modelId } };