Skip to content

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

Queue the guide's chat model behind D1 in the background

Claude227a278f8d15
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 99c07e2a9..41130beab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,7 @@ This changelog was generated from the repository Git history and release tags. V
- New tab page: WebBrain now provides a new tab with a clock, quick links, bundled backgrounds and a WebBrain box (Ask, Act or Search). Ask and Act both run in the side panel straight away (Act shows a warning that it starts working immediately; Plan before Act still applies). Based on Mue (BSD-3-Clause), with all of Mue's network calls and telemetry removed. Adds the `search` permission. See `docs/newtab.md`. The new tab is opt-in: the last onboarding step has a "Use WebBrain as my new tab page" checkbox (ticked by default, next to Help Improve WebBrain) and Settings β†’ General has the same switch; when it is off, new tabs go to the browser's own new tab page. On Firefox the new tab stays on and has no switch in WebBrain, because Firefox cannot hand a tab to its own new tab page; change it in Firefox's settings.
### Changed
-- The first-install guide's **Download local models** button now downloads D1 first and Ternary Bonsai 2 2B Vision after it, and the progress bar follows whichever is downloading. If this GPU can't run D1, or D1's download stops, Bonsai starts anyway. Once D1 is downloaded, Auto uses it to check whether tasks and scheduled runs are finished even while WebBrain Cloud is your chat provider, so the guide no longer asks you to choose it in Settings. WebBrain Cloud's decision model still answers what a request asks for, and, as with any D1 answer, a "finished" from D1 is confirmed by the chat model. Chrome; the Auto change applies to Firefox too.
+- The first-install guide's **Download local models** button now downloads D1 first and Ternary Bonsai 2 2B Vision after it, and the progress bar follows whichever is downloading. Bonsai follows even if you close the guide while D1 downloads, and if this GPU can't run D1, or D1's download stops, Bonsai starts anyway. Once D1 is downloaded, Auto uses it to check whether tasks and scheduled runs are finished even while WebBrain Cloud is your chat provider, so the guide no longer asks you to choose it in Settings. WebBrain Cloud's decision model still answers what a request asks for, and, as with any D1 answer, a "finished" from D1 is confirmed by the chat model. Chrome; the Auto change applies to Firefox too.
- Settings is reorganized. Its tabs are now General, Budgets, Memory, Skills, CAPTCHA, Bridge and Advanced. The providers and the assistive models moved to a new Providers page with LLM, Vision, Speech, Generative, Decision and SafeSocial tabs; open it from **More…** in the side panel's model menu or from the Providers link at the top of Settings. Site permissions moved to their own page, opened with **Manage site permissions** in the ⚠ menu next to the microphone. Advanced is now a tab instead of a drawer inside General, and the CAPTCHA solvers have a tab of their own. The new Budgets tab holds the cloud cost allowances, the question timeout and what happens when it runs out, Max Agent Steps, the LLM request timeout and the image budget. Screenshot redaction moved to Advanced. Language and UI zoom are chosen in the side panel only. The search box looks through General, Budgets, CAPTCHA and Advanced at once. Old links to the moved tabs open the new pages.
- Auto-screenshot now defaults to On navigation instead of On state change. A value already saved is kept, including one restored from an older configuration export, which records the old default.
- Record traces and Record full request details are now one switch: traces are recorded with their full request details, or not at all. An install that recorded traces without full details starts recording them after the update.
diff --git a/docs/providers-and-models.md b/docs/providers-and-models.md
index 4fc55f2f3..1110b9c16 100644
--- a/docs/providers-and-models.md
+++ b/docs/providers-and-models.md
@@ -126,11 +126,16 @@ the [Hugging Face model repository](https://huggingface.co/webbrain-one/d1-brows
into OPFS. In Chrome the first-install guide offers the same download: its
optional **Download local models** button, whose note names D1 and its size,
is that consent. It downloads D1 first, in D1's own Worker, and loads it;
-Ternary Bonsai 2 2B Vision follows once D1 is ready, or at once when this GPU
-cannot run D1 or D1's download stops with an error. The guide reads D1's
-status, checks the GPU and starts or stops the download (the only
-`d1_control` commands it may send); it writes no decision settings, so Auto,
-the default, then uses D1 as described above, and the guide says so. The package stores FP16 weights and computes in FP32; it is the
+Ternary Bonsai 2 2B Vision follows once D1's download is over (ready or
+saved, failed, or stopped other than by the guide's Pause), and at once when
+this GPU cannot run D1. The guide's download queues Bonsai in the background
+(`providers/install-download-queue.js`, `installChatModelAfterD1` in storage),
+so it follows even after the guide is closed: the guide's reads of D1's
+status and an alarm move it along, and a queue older than D1's own
+thirty-minute limit allows is dropped. The guide reads D1's status, checks the
+GPU and starts or stops the download (the only `d1_control` commands it may
+send); it writes no decision settings, so Auto, the default, then uses D1 as
+described above, and the guide says so. The package stores FP16 weights and computes in FP32; it is the
[FP32 release](https://huggingface.co/webbrain-one/d1-browser-decision-fp32)
with its weights rounded once, and answers the same on its held-out test. A
cached FP32 package from an earlier version is removed on the next download. The repository name is not a production-readiness claim; this provider
diff --git a/docs/security-model.md b/docs/security-model.md
index 646de4d4e..3a29fa1e0 100644
--- a/docs/security-model.md
+++ b/docs/security-model.md
@@ -72,8 +72,9 @@ Providers page, or the Chrome install guide's optional **Download local
models** button, whose note names D1, its size and its source. The control
handler accepts only the extension's own Providers page and, for status, the
GPU check, download and stop only, its own install guide page; load, unload,
-removal and consent stay on the Providers page. The guide writes no decision
-settings. Auto uses D1 only after that consent, while the download is complete
+removal and consent stay on the Providers page. The guide's download may name
+the chat model to download after D1, and only a shipped, selectable WebGPU
+preset is queued. The guide writes no decision settings. Auto uses D1 only after that consent, while the download is complete
and its Load request stands, and never in place of a saved choice; with Compass
as the chat provider, D1 takes only the completion and scheduled checks, and
Compass's managed route keeps semantic decisions. Chrome uses its shared offscreen
diff --git a/docs/webgpu-models.md b/docs/webgpu-models.md
index f8792493a..cc28986da 100644
--- a/docs/webgpu-models.md
+++ b/docs/webgpu-models.md
@@ -257,9 +257,10 @@ Use the tools in `scripts/models/bonsai/`:
Mode, and the first-install page (Chrome), which offers Ternary Bonsai 2 2B
Vision and D1, the decision model, behind its optional "Download local
models" button. D1 downloads first, in its own worker, and Bonsai follows
- once D1 is ready (or at once when D1 cannot download here); while D1's
- download runs, the button pauses or resumes it and the progress bar
- follows it, and D1's progress has a line of its own. Once the chat model is
+ once D1's download is over (or at once when D1 cannot download here),
+ queued in the background so it follows even after the page is closed;
+ while D1's download runs, the button pauses or resumes it and the progress
+ bar follows it, and D1's progress has a line of its own. Once the chat model is
ready, the same button starts, pauses, resumes or retries D1 alone. The
install page checks for
hardware WebGPU before fetching anything (D1 runs its own adapter check, and
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index fc7a07a37..f9d30027f 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -4,6 +4,7 @@ import { resolveCurrentDecisionConfig } from './agent/decision-availability.js';
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';
+import { CHAT_AFTER_D1_ALARM, createChatModelAfterD1 } from './providers/install-download-queue.js';
// D1's Worker and GPU memory are released when decisions are switched Off,
// another decision provider is saved, consent changes or Strict Secret Mode
@@ -39,6 +40,7 @@ import {
WEBGPU_VISION_DOWNLOAD_STATE_KEY,
WEBGPU_VISION_DOWNLOAD_STATE_MESSAGE,
WEBGPU_VISION_MODEL_ID,
+ isSelectableWebgpuPreset,
isShippedWebgpuPreset,
webgpuModelDisplayName,
} from './providers/webgpu.js';
@@ -184,6 +186,26 @@ installSafeSocialBackground(chrome, async (command, payload = {}) => {
});
const providerManager = new ProviderManager();
+// The install guide's chat model, queued behind D1's download; the alarm moves
+// it along while the guide is closed (providers/install-download-queue.js).
+const chatModelAfterD1 = createChatModelAfterD1({
+ api: chrome,
+ d1Status: () => d1Request('status'),
+ chatStatus: async (model) => {
+ if (providerManager.providers.size === 0) await providerManager.load();
+ return providerManager.getWebgpuDownloadStatus({ model });
+ },
+ startChat: async (model) => {
+ if (providerManager.providers.size === 0) await providerManager.load();
+ return providerManager.startWebgpuDownload({ model });
+ },
+});
+chrome.alarms.onAlarm.addListener((alarm) => {
+ if (alarm?.name !== CHAT_AFTER_D1_ALARM) return;
+ chatModelAfterD1.advance().catch((error) => {
+ console.warn('[WebBrain] The queued local chat-model download could not start:', error);
+ });
+});
const apocalypseController = createApocalypseController(chrome);
const VISION_OFFSCREEN_URL = chrome.runtime.getURL('src/offscreen/offscreen.html');
// The stale-run repair scan waits a beat after wake so a run resuming from
@@ -4989,13 +5011,16 @@ async function handleMessage(msg, sender) {
// The D1 controls are on the Decision tab of the Providers page.
const api = globalThis.browser || globalThis.chrome;
const page = sender.id === api.runtime.id ? String(sender.url || '').split(/[?#]/)[0] : '';
- // The install guide downloads D1 next to its local chat model: it reads
+ // The install guide downloads D1 before its local chat model: it reads
// D1's status, checks the GPU, and starts or stops that download. Every
// other control stays on the Providers page.
const fromInstall = page === api.runtime.getURL('src/ui/install.html')
&& ['status', 'probe', 'download', 'stop'].includes(msg.command);
if (page !== api.runtime.getURL('src/ui/providers.html') && !fromInstall) return { success: false, error: 'D1 model controls are available only from the extension Providers page.' };
try {
+ // The guide's download names the chat model to download after D1.
+ const chatModel = fromInstall && msg.command === 'download' && msg.chatModel != null ? String(msg.chatModel) : '';
+ if (chatModel && !isSelectableWebgpuPreset(chatModel)) throw new Error('Unknown local chat model.');
await strictSecretModeReady;
if (agent.strictSecretMode && !['dispose', 'reset', 'stop', 'clear', 'status'].includes(msg.command)) throw new Error('D1 is disabled in Strict Secret Mode.');
if (!['status', 'probe', 'download', 'load', 'stop', 'dispose', 'clear', 'reset', 'consent'].includes(msg.command)) throw new Error('Invalid D1 model command.');
@@ -5009,8 +5034,19 @@ async function handleMessage(msg, sender) {
await api.storage.local.set({ [D1_KEEP_LOADED_KEY]: false });
}
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.
+ 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) => {
+ console.warn('[WebBrain] The queued local chat-model download could not start:', error);
+ return false;
+ })
+ : await chatModelAfterD1.queued();
// The guide says whether D1 answers this profile's decisions.
- return { success: true, result: fromInstall ? { ...result, usedForDecisions: await d1UsedForDecisions(api) } : result };
+ return { success: true, result: { ...result, usedForDecisions: await d1UsedForDecisions(api), chatModelQueued } };
} catch (error) { return { success: false, error: error.message }; }
}
case 'list_decision_models': {
diff --git a/src/chrome/src/providers/install-download-queue.js b/src/chrome/src/providers/install-download-queue.js
new file mode 100644
index 000000000..fa613d618
--- /dev/null
+++ b/src/chrome/src/providers/install-download-queue.js
@@ -0,0 +1,84 @@
+// The install guide downloads D1 first and the in-browser chat model after it.
+// The guide can be closed while D1 downloads, so the chat model's download is
+// queued in storage and the background starts it once D1's download is over:
+// when the guide reads D1's status, and on an alarm while the guide is gone.
+
+export const CHAT_AFTER_D1_KEY = 'installChatModelAfterD1';
+export const CHAT_AFTER_D1_ALARM = 'install-chat-model-after-d1';
+// D1's download and load stop after thirty minutes (d1-worker.js), so a queue
+// this old was left behind by a browser restart or an abandoned guide.
+export const CHAT_AFTER_D1_MAX_AGE_MS = 45 * 60_000;
+
+// D1 is still checking the GPU, downloading or loading.
+const D1_TRANSFER_STATUSES = new Set(['checking', 'downloading', 'loading']);
+// A chat-model download that has started, or a transfer holding the WebGPU
+// download slot (install.js LOCAL_MODEL_TRANSFER_STATUSES).
+const CHAT_TRANSFER_STATUSES = new Set(['checking', 'starting', 'queued', 'downloading', 'paused', 'stopping', 'loading']);
+
+/**
+ * `d1Status()` reads D1's status, `chatStatus(model)` the chat model's
+ * download status and `startChat(model)` starts that download.
+ */
+export function createChatModelAfterD1({ api, d1Status, chatStatus, startChat, now = () => Date.now() }) {
+ const storage = api.storage.local;
+ const read = async () => (await storage.get(CHAT_AFTER_D1_KEY))?.[CHAT_AFTER_D1_KEY] || null;
+ const clearAlarm = async () => { try { await api.alarms?.clear?.(CHAT_AFTER_D1_ALARM); } catch { /* best effort */ } };
+ const clear = async () => { await storage.remove(CHAT_AFTER_D1_KEY); await clearAlarm(); };
+ // One step at a time: the guide's status reads and the alarm must not both
+ // start the download.
+ let steps = Promise.resolve();
+ const serial = (fn) => {
+ const step = steps.then(fn);
+ steps = step.catch(() => {});
+ return step;
+ };
+
+ // Returns whether the chat model still waits for D1.
+ async function step(status) {
+ const queued = await read();
+ if (!queued || typeof queued.model !== 'string' || !queued.model) {
+ if (queued) await storage.remove(CHAT_AFTER_D1_KEY);
+ await clearAlarm();
+ return false;
+ }
+ 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; } }
+ 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.
+ if (d1State === 'stopped' && queued.paused === true && d1?.cached !== true) return true;
+ // D1's download is over: ready or saved, failed, or stopped some other way.
+ let chat;
+ try { chat = await chatStatus(queued.model); } catch { return true; }
+ const chatState = String(chat?.status || '');
+ const other = chat?.activeTransfer;
+ const otherModel = String(other?.modelId || other?.model || '').trim();
+ if (chat?.ready !== true && !CHAT_TRANSFER_STATUSES.has(chatState)
+ && otherModel && otherModel !== queued.model && CHAT_TRANSFER_STATUSES.has(String(other?.status || ''))) {
+ // Another model holds the WebGPU download slot: start when it is free.
+ return true;
+ }
+ await clear();
+ if (chat?.ready === true || CHAT_TRANSFER_STATUSES.has(chatState)) return false;
+ await startChat(queued.model);
+ return false;
+ }
+
+ return {
+ // The guide asked for both models: the chat model follows D1.
+ queue: (model) => serial(async () => {
+ await 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.
+ 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)),
+ };
+}
diff --git a/src/chrome/src/ui/install.js b/src/chrome/src/ui/install.js
index f9697d893..020bc1062 100644
--- a/src/chrome/src/ui/install.js
+++ b/src/chrome/src/ui/install.js
@@ -225,16 +225,18 @@ export async function pointWebgpuProviderAtModel(modelId, { runtime = globalThis
// ── D1, the on-device decision model ─────────────────────────────────────
// The same button downloads D1, WebBrain's experimental decision model, and
-// then the chat model: D1 first, in its own worker, and the chat model once D1
-// is ready or saved, or cannot be downloaded here. D1 loads when its files are
-// in. The click is the consent the note asks for. The guide writes no decision
-// settings: once D1 is downloaded, Auto, the default mode, uses it for its
-// checks, whatever the chat provider (decision-availability.js).
+// then the chat model: D1 first, in its own worker, and the chat model once
+// D1's download is over. The background queues the chat model with D1's
+// download, so it follows even after this page is closed
+// (providers/install-download-queue.js); when D1 cannot download here, the
+// chat model starts at once. D1 loads when its files are in. The click is the
+// consent the note asks for. The guide writes no decision settings: once D1 is
+// downloaded, Auto, the default mode, uses it for its checks, whatever the
+// chat provider (decision-availability.js).
const D1_POLL_MS = 1500;
const D1_POLLING_STATUSES = new Set(['checking', 'downloading']);
-// D1's download is under way: checking the GPU, transferring or paused. The
-// chat model waits for it.
+// D1's download is under way: checking the GPU, transferring or paused.
const D1_TRANSFER_STATUSES = new Set(['checking', 'downloading', 'paused']);
// Chat model states in which a D1 transfer has the button: the chat model is
// not downloading, so D1 goes first.
@@ -244,14 +246,17 @@ export function normalizeD1Snapshot(snapshot = {}) {
const raw = String(snapshot?.status || '');
const progress = Math.max(0, Math.min(100, Math.round(Number(snapshot?.progress) || 0)));
const usedForDecisions = snapshot?.usedForDecisions === true;
- if (snapshot?.loaded === true || raw === 'ready') return { status: 'ready', progress: 100, error: '', usedForDecisions };
+ // The background still holds the chat model's download for after D1's.
+ const chatModelQueued = snapshot?.chatModelQueued === true;
+ const shared = { usedForDecisions, chatModelQueued };
+ if (snapshot?.loaded === true || raw === 'ready') return { status: 'ready', progress: 100, error: '', ...shared };
// Downloaded and verified but not loaded: a decision loads it again.
- if (raw === 'cached' || snapshot?.cached === true) return { status: 'saved', progress: 100, error: '', usedForDecisions };
- if (raw === 'checking') return { status: 'checking', progress: 0, error: '', usedForDecisions };
- if (['downloading', 'loading'].includes(raw)) return { status: 'downloading', progress, error: '', usedForDecisions };
- if (raw === 'stopped') return { status: 'paused', progress, error: '', usedForDecisions };
- if (raw === 'error') return { status: 'error', progress: 0, error: String(snapshot?.error || ''), usedForDecisions };
- return { status: 'idle', progress: 0, error: '', usedForDecisions };
+ if (raw === 'cached' || snapshot?.cached === true) return { status: 'saved', progress: 100, error: '', ...shared };
+ if (raw === 'checking') return { status: 'checking', progress: 0, error: '', ...shared };
+ if (['downloading', 'loading'].includes(raw)) return { status: 'downloading', progress, error: '', ...shared };
+ if (raw === 'stopped') return { status: 'paused', progress, error: '', ...shared };
+ if (raw === 'error') return { status: 'error', progress: 0, error: String(snapshot?.error || ''), ...shared };
+ return { status: 'idle', progress: 0, error: '', ...shared };
}
/** The D1 line for one normalized state; nothing while D1 was never started. */
@@ -310,7 +315,7 @@ export function initializeD1Download({
} = {}) {
const line = documentLike?.getElementById?.('local-model-d1');
if (!line || !runtime?.sendMessage) return null;
- let state = { status: 'idle', progress: 0, error: '', usedForDecisions: false };
+ let state = { status: 'idle', progress: 0, error: '', usedForDecisions: false, chatModelQueued: false };
let timer = null;
let disposed = false;
let starting = false;
@@ -328,7 +333,7 @@ export function initializeD1Download({
if (!disposed && D1_POLLING_STATUSES.has(state.status)) timer = setTimeout(() => { void refresh(); }, pollMs);
};
const setState = (next) => {
- state = { progress: 0, error: '', usedForDecisions: false, ...next };
+ state = { progress: 0, error: '', usedForDecisions: false, chatModelQueued: false, ...next };
render();
schedule();
onChange?.();
@@ -344,7 +349,8 @@ export function initializeD1Download({
}
}
- async function start() {
+ // `chatModel`: the chat model to download once D1's download is over.
+ async function start({ chatModel = '' } = {}) {
if (starting || ['checking', 'downloading', 'ready', 'saved'].includes(state.status)) return;
starting = true;
try {
@@ -357,7 +363,7 @@ export function initializeD1Download({
return;
}
try {
- const response = await command('download', { consent: true, consentVersion: D1_CONSENT_VERSION });
+ const response = await command('download', { consent: true, consentVersion: D1_CONSENT_VERSION, ...(chatModel ? { chatModel } : {}) });
setState(normalizeD1Snapshot({ status: 'checking', ...response?.result }));
} catch (error) {
setState({ status: 'error', error: error?.message || String(error) });
@@ -407,21 +413,18 @@ export function initializeLocalModelDownload({
let pollTimer = null;
let disposed = false;
let rendered = false;
- // Set by the click that downloads both models: the chat model starts once
- // D1's download is over (ready or saved, unsupported, or stopped with an
- // error). A Pause of D1 keeps it set, so Resume carries on to the chat model.
- let chatAfterD1 = false;
- // The person paused D1 here. A D1 transfer stopped any other way (no bytes
- // for two minutes, its time limit, a Stop on the Providers page) does not
- // hold the chat model back.
- let d1PausedHere = false;
- // D1's own changes redraw the button once this controller has drawn it,
- // and the end of D1's download starts the chat model.
+ // Whether the background held the chat model's download for after D1's.
+ let chatModelQueued = false;
+ // D1's own changes redraw the button once this controller has drawn it. When
+ // the background lets go of the queued chat model, it has started it (or
+ // found it already downloading or ready), so its state is read again.
const d1 = initializeD1Download({
documentLike, runtime, translate, pollMs,
onChange: () => {
if (rendered) render();
- continueAfterD1();
+ const queued = d1?.getState().chatModelQueued === true;
+ if (chatModelQueued && !queued && !inFlight) void refresh();
+ chatModelQueued = queued;
},
});
const buttonView = () => localModelButtonView(localModelView(state), state.status, d1?.getState());
@@ -461,7 +464,6 @@ export function initializeLocalModelDownload({
state = { progress: 0, error: '', ...next };
render();
schedulePoll();
- continueAfterD1();
};
async function refresh({ quiet = false } = {}) {
@@ -475,27 +477,12 @@ export function initializeLocalModelDownload({
}
}
- // The second download: the chat model, once D1's is over.
- function continueAfterD1() {
- if (!chatAfterD1 || inFlight || disposed) return;
- const d1Status = d1?.getState().status;
- if (['checking', 'downloading'].includes(d1Status) || (d1Status === 'paused' && d1PausedHere)) return;
- // Another model holds the WebGPU download slot: start when it is free.
- if (['busy', 'checking'].includes(state.status)) return;
- chatAfterD1 = false;
- // A chat model already downloading, paused elsewhere or ready is left as it is.
- if (['not-downloaded', 'error'].includes(state.status)) void startChatModel();
- }
-
+ // The chat model now: D1 has nothing to download here (no D1, already
+ // saved, or a GPU it cannot use, or its download did not start).
async function startChatModel() {
inFlight = true;
render();
try {
- try {
- await pointWebgpuProviderAtModel(modelId, { runtime });
- } catch {
- // Only cosmetic for Settings; the download itself does not need it.
- }
await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
} catch (error) {
inFlight = false;
@@ -512,22 +499,21 @@ export function initializeLocalModelDownload({
inFlight = true;
if (view.action === 'd1-start' || view.action === 'd1-pause') {
// Only D1 is downloaded, paused or resumed: before the chat model while
- // D1 goes first, or after it once the chat model is ready.
- d1PausedHere = view.action === 'd1-pause';
+ // D1 goes first, or after it once the chat model is ready. Resuming a D1
+ // the chat model was queued behind keeps it queued.
render();
try {
- await (view.action === 'd1-start' ? d1.start() : d1.pause());
+ if (view.action === 'd1-pause') await d1.pause();
+ else await d1.start(d1.getState().chatModelQueued && state.status !== 'ready' ? { chatModel: modelId } : {});
} finally {
inFlight = false;
render();
}
- continueAfterD1();
return;
}
try {
if (view.action === 'pause') {
render();
- if (['checking', 'downloading'].includes(d1?.getState().status)) d1PausedHere = true;
void d1?.pause();
await backgroundCommand(runtime, 'pause_webgpu_download');
} else if (view.action === 'resume') {
@@ -544,20 +530,21 @@ export function initializeLocalModelDownload({
setState({ status: 'unsupported', error: String(probe.error || '') });
return;
}
- // D1 downloads first; its line reports it, and the chat model follows
- // (continueAfterD1). The button stays busy until D1's download has
- // started, so a Pause always finds D1's transfer to stop.
- chatAfterD1 = true;
- d1PausedHere = false;
+ try {
+ await pointWebgpuProviderAtModel(modelId, { runtime });
+ } catch {
+ // Only cosmetic for Settings; the download itself does not need it.
+ }
+ // D1 downloads first; its line reports it, and the background starts
+ // the chat model once D1's download is over. The button stays busy
+ // until D1's download has started, so a Pause always finds D1's
+ // transfer to stop.
if (d1) {
state = { status: 'not-downloaded', progress: 0, error: '' };
render();
- await d1.start();
+ await d1.start({ chatModel: modelId });
}
- // Nothing of D1's to wait for (no D1 here, already saved, or a GPU it
- // cannot use): the chat model goes now.
- if (!D1_TRANSFER_STATUSES.has(d1?.getState().status)) {
- chatAfterD1 = false;
+ if (d1?.getState().chatModelQueued !== true) {
inFlight = false;
await startChatModel();
return;
diff --git a/src/firefox/src/ui/install.js b/src/firefox/src/ui/install.js
index f9697d893..020bc1062 100644
--- a/src/firefox/src/ui/install.js
+++ b/src/firefox/src/ui/install.js
@@ -225,16 +225,18 @@ export async function pointWebgpuProviderAtModel(modelId, { runtime = globalThis
// ── D1, the on-device decision model ─────────────────────────────────────
// The same button downloads D1, WebBrain's experimental decision model, and
-// then the chat model: D1 first, in its own worker, and the chat model once D1
-// is ready or saved, or cannot be downloaded here. D1 loads when its files are
-// in. The click is the consent the note asks for. The guide writes no decision
-// settings: once D1 is downloaded, Auto, the default mode, uses it for its
-// checks, whatever the chat provider (decision-availability.js).
+// then the chat model: D1 first, in its own worker, and the chat model once
+// D1's download is over. The background queues the chat model with D1's
+// download, so it follows even after this page is closed
+// (providers/install-download-queue.js); when D1 cannot download here, the
+// chat model starts at once. D1 loads when its files are in. The click is the
+// consent the note asks for. The guide writes no decision settings: once D1 is
+// downloaded, Auto, the default mode, uses it for its checks, whatever the
+// chat provider (decision-availability.js).
const D1_POLL_MS = 1500;
const D1_POLLING_STATUSES = new Set(['checking', 'downloading']);
-// D1's download is under way: checking the GPU, transferring or paused. The
-// chat model waits for it.
+// D1's download is under way: checking the GPU, transferring or paused.
const D1_TRANSFER_STATUSES = new Set(['checking', 'downloading', 'paused']);
// Chat model states in which a D1 transfer has the button: the chat model is
// not downloading, so D1 goes first.
@@ -244,14 +246,17 @@ export function normalizeD1Snapshot(snapshot = {}) {
const raw = String(snapshot?.status || '');
const progress = Math.max(0, Math.min(100, Math.round(Number(snapshot?.progress) || 0)));
const usedForDecisions = snapshot?.usedForDecisions === true;
- if (snapshot?.loaded === true || raw === 'ready') return { status: 'ready', progress: 100, error: '', usedForDecisions };
+ // The background still holds the chat model's download for after D1's.
+ const chatModelQueued = snapshot?.chatModelQueued === true;
+ const shared = { usedForDecisions, chatModelQueued };
+ if (snapshot?.loaded === true || raw === 'ready') return { status: 'ready', progress: 100, error: '', ...shared };
// Downloaded and verified but not loaded: a decision loads it again.
- if (raw === 'cached' || snapshot?.cached === true) return { status: 'saved', progress: 100, error: '', usedForDecisions };
- if (raw === 'checking') return { status: 'checking', progress: 0, error: '', usedForDecisions };
- if (['downloading', 'loading'].includes(raw)) return { status: 'downloading', progress, error: '', usedForDecisions };
- if (raw === 'stopped') return { status: 'paused', progress, error: '', usedForDecisions };
- if (raw === 'error') return { status: 'error', progress: 0, error: String(snapshot?.error || ''), usedForDecisions };
- return { status: 'idle', progress: 0, error: '', usedForDecisions };
+ if (raw === 'cached' || snapshot?.cached === true) return { status: 'saved', progress: 100, error: '', ...shared };
+ if (raw === 'checking') return { status: 'checking', progress: 0, error: '', ...shared };
+ if (['downloading', 'loading'].includes(raw)) return { status: 'downloading', progress, error: '', ...shared };
+ if (raw === 'stopped') return { status: 'paused', progress, error: '', ...shared };
+ if (raw === 'error') return { status: 'error', progress: 0, error: String(snapshot?.error || ''), ...shared };
+ return { status: 'idle', progress: 0, error: '', ...shared };
}
/** The D1 line for one normalized state; nothing while D1 was never started. */
@@ -310,7 +315,7 @@ export function initializeD1Download({
} = {}) {
const line = documentLike?.getElementById?.('local-model-d1');
if (!line || !runtime?.sendMessage) return null;
- let state = { status: 'idle', progress: 0, error: '', usedForDecisions: false };
+ let state = { status: 'idle', progress: 0, error: '', usedForDecisions: false, chatModelQueued: false };
let timer = null;
let disposed = false;
let starting = false;
@@ -328,7 +333,7 @@ export function initializeD1Download({
if (!disposed && D1_POLLING_STATUSES.has(state.status)) timer = setTimeout(() => { void refresh(); }, pollMs);
};
const setState = (next) => {
- state = { progress: 0, error: '', usedForDecisions: false, ...next };
+ state = { progress: 0, error: '', usedForDecisions: false, chatModelQueued: false, ...next };
render();
schedule();
onChange?.();
@@ -344,7 +349,8 @@ export function initializeD1Download({
}
}
- async function start() {
+ // `chatModel`: the chat model to download once D1's download is over.
+ async function start({ chatModel = '' } = {}) {
if (starting || ['checking', 'downloading', 'ready', 'saved'].includes(state.status)) return;
starting = true;
try {
@@ -357,7 +363,7 @@ export function initializeD1Download({
return;
}
try {
- const response = await command('download', { consent: true, consentVersion: D1_CONSENT_VERSION });
+ const response = await command('download', { consent: true, consentVersion: D1_CONSENT_VERSION, ...(chatModel ? { chatModel } : {}) });
setState(normalizeD1Snapshot({ status: 'checking', ...response?.result }));
} catch (error) {
setState({ status: 'error', error: error?.message || String(error) });
@@ -407,21 +413,18 @@ export function initializeLocalModelDownload({
let pollTimer = null;
let disposed = false;
let rendered = false;
- // Set by the click that downloads both models: the chat model starts once
- // D1's download is over (ready or saved, unsupported, or stopped with an
- // error). A Pause of D1 keeps it set, so Resume carries on to the chat model.
- let chatAfterD1 = false;
- // The person paused D1 here. A D1 transfer stopped any other way (no bytes
- // for two minutes, its time limit, a Stop on the Providers page) does not
- // hold the chat model back.
- let d1PausedHere = false;
- // D1's own changes redraw the button once this controller has drawn it,
- // and the end of D1's download starts the chat model.
+ // Whether the background held the chat model's download for after D1's.
+ let chatModelQueued = false;
+ // D1's own changes redraw the button once this controller has drawn it. When
+ // the background lets go of the queued chat model, it has started it (or
+ // found it already downloading or ready), so its state is read again.
const d1 = initializeD1Download({
documentLike, runtime, translate, pollMs,
onChange: () => {
if (rendered) render();
- continueAfterD1();
+ const queued = d1?.getState().chatModelQueued === true;
+ if (chatModelQueued && !queued && !inFlight) void refresh();
+ chatModelQueued = queued;
},
});
const buttonView = () => localModelButtonView(localModelView(state), state.status, d1?.getState());
@@ -461,7 +464,6 @@ export function initializeLocalModelDownload({
state = { progress: 0, error: '', ...next };
render();
schedulePoll();
- continueAfterD1();
};
async function refresh({ quiet = false } = {}) {
@@ -475,27 +477,12 @@ export function initializeLocalModelDownload({
}
}
- // The second download: the chat model, once D1's is over.
- function continueAfterD1() {
- if (!chatAfterD1 || inFlight || disposed) return;
- const d1Status = d1?.getState().status;
- if (['checking', 'downloading'].includes(d1Status) || (d1Status === 'paused' && d1PausedHere)) return;
- // Another model holds the WebGPU download slot: start when it is free.
- if (['busy', 'checking'].includes(state.status)) return;
- chatAfterD1 = false;
- // A chat model already downloading, paused elsewhere or ready is left as it is.
- if (['not-downloaded', 'error'].includes(state.status)) void startChatModel();
- }
-
+ // The chat model now: D1 has nothing to download here (no D1, already
+ // saved, or a GPU it cannot use, or its download did not start).
async function startChatModel() {
inFlight = true;
render();
try {
- try {
- await pointWebgpuProviderAtModel(modelId, { runtime });
- } catch {
- // Only cosmetic for Settings; the download itself does not need it.
- }
await backgroundCommand(runtime, 'start_webgpu_download', { model: modelId });
} catch (error) {
inFlight = false;
@@ -512,22 +499,21 @@ export function initializeLocalModelDownload({
inFlight = true;
if (view.action === 'd1-start' || view.action === 'd1-pause') {
// Only D1 is downloaded, paused or resumed: before the chat model while
- // D1 goes first, or after it once the chat model is ready.
- d1PausedHere = view.action === 'd1-pause';
+ // D1 goes first, or after it once the chat model is ready. Resuming a D1
+ // the chat model was queued behind keeps it queued.
render();
try {
- await (view.action === 'd1-start' ? d1.start() : d1.pause());
+ if (view.action === 'd1-pause') await d1.pause();
+ else await d1.start(d1.getState().chatModelQueued && state.status !== 'ready' ? { chatModel: modelId } : {});
} finally {
inFlight = false;
render();
}
- continueAfterD1();
return;
}
try {
if (view.action === 'pause') {
render();
- if (['checking', 'downloading'].includes(d1?.getState().status)) d1PausedHere = true;
void d1?.pause();
await backgroundCommand(runtime, 'pause_webgpu_download');
} else if (view.action === 'resume') {
@@ -544,20 +530,21 @@ export function initializeLocalModelDownload({
setState({ status: 'unsupported', error: String(probe.error || '') });
return;
}
- // D1 downloads first; its line reports it, and the chat model follows
- // (continueAfterD1). The button stays busy until D1's download has
- // started, so a Pause always finds D1's transfer to stop.
- chatAfterD1 = true;
- d1PausedHere = false;
+ try {
+ await pointWebgpuProviderAtModel(modelId, { runtime });
+ } catch {
+ // Only cosmetic for Settings; the download itself does not need it.
+ }
+ // D1 downloads first; its line reports it, and the background starts
+ // the chat model once D1's download is over. The button stays busy
+ // until D1's download has started, so a Pause always finds D1's
+ // transfer to stop.
if (d1) {
state = { status: 'not-downloaded', progress: 0, error: '' };
render();
- await d1.start();
+ await d1.start({ chatModel: modelId });
}
- // Nothing of D1's to wait for (no D1 here, already saved, or a GPU it
- // cannot use): the chat model goes now.
- if (!D1_TRANSFER_STATUSES.has(d1?.getState().status)) {
- chatAfterD1 = false;
+ if (d1?.getState().chatModelQueued !== true) {
inFlight = false;
await startChatModel();
return;
diff --git a/test/d1-decision.mjs b/test/d1-decision.mjs
index cace6b323..f4cd40172 100644
--- a/test/d1-decision.mjs
+++ b/test/d1-decision.mjs
@@ -286,7 +286,22 @@ for (const build of ['chrome', 'firefox']) {
assert.match(branch, /const page = sender\.id === api\.runtime\.id \? String\(sender\.url \|\| ''\)\.split\(\/\[\?#\]\/\)\[0\] : '';/);
assert.match(branch, /if \(page !== api\.runtime\.getURL\('src\/ui\/providers\.html'\) && !fromInstall\) return \{ success: false/);
assert.match(branch, /if \(msg\.consent !== true \|\| msg\.consentVersion !== D1_CONSENT_VERSION\) throw/);
- assert.match(branch, /result: fromInstall \? \{ \.\.\.result, usedForDecisions: await d1UsedForDecisions\(api\) \} : result/);
+ if (build === 'chrome') {
+ // The guide's download queues its chat model behind D1 in the
+ // background (install-download-queue.js): only a shipped, selectable
+ // WebGPU chat model, only from the guide's download; its Pause holds
+ // the queue and its status reads move it along.
+ 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, /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\(\)/);
+ } else {
+ // Firefox has no in-browser chat model to queue.
+ assert.match(branch, /result: fromInstall \? \{ \.\.\.result, usedForDecisions: await d1UsedForDecisions\(api\) \} : result/);
+ }
assert.doesNotMatch(background, /adoptForDecisions|adoptD1ForDecisions/, 'the guide never writes decision settings');
// The guide says whether D1 answers this profile's decisions now.
const source = topLevelFunction(background, 'd1UsedForDecisions');
@@ -493,6 +508,151 @@ for (const build of ['chrome', 'firefox']) {
});
}
+test('chrome: the install guide queues its chat model behind D1, and the background starts it once D1 is done', async () => {
+ const queue = await import('../src/chrome/src/providers/install-download-queue.js');
+ const model = 'webbrain-one/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision';
+ const make = ({ d1 = { status: 'downloading' }, chat = { status: 'not-downloaded' } } = {}) => {
+ const env = { d1, chat, now: 1_000_000, area: {}, alarms: [], cleared: [], starts: [], reads: 0 };
+ const api = {
+ storage: { local: {
+ get: async (key) => (key in env.area ? { [key]: structuredClone(env.area[key]) } : {}),
+ set: async (values) => { Object.assign(env.area, structuredClone(values)); },
+ remove: async (key) => { delete env.area[key]; },
+ } },
+ alarms: { create: async (name, info) => { env.alarms.push([name, info]); }, clear: async (name) => { env.cleared.push(name); return true; } },
+ };
+ env.queue = queue.createChatModelAfterD1({
+ api,
+ d1Status: async () => { env.reads += 1; if (env.d1 instanceof Error) throw env.d1; return env.d1; },
+ chatStatus: async () => env.chat,
+ startChat: async (name) => { env.starts.push(name); env.chat = { status: 'downloading' }; },
+ now: () => env.now,
+ });
+ return env;
+ };
+
+ {
+ // Nothing queued: nothing starts, and a left-over alarm is cleared.
+ const env = make({ d1: { status: 'ready', loaded: true } });
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, []);
+ assert.deepEqual(env.cleared, [queue.CHAT_AFTER_D1_ALARM]);
+ }
+ {
+ // Queued while D1 downloads: the alarm checks again; D1 first.
+ const env = make();
+ await env.queue.queue(model);
+ assert.deepEqual(env.area[queue.CHAT_AFTER_D1_KEY], { model, queuedAt: env.now, paused: false });
+ assert.deepEqual(env.alarms, [[queue.CHAT_AFTER_D1_ALARM, { periodInMinutes: 0.5 }]]);
+ assert.equal(await env.queue.queued(), true);
+ for (const status of ['checking', 'downloading', 'loading']) {
+ env.d1 = { status };
+ assert.equal(await env.queue.advance(), true, status);
+ }
+ assert.deepEqual(env.starts, []);
+ // D1 is ready: the chat model starts once, and the queue is gone.
+ env.d1 = { status: 'ready', loaded: true };
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, [model]);
+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY], undefined);
+ assert.ok(env.cleared.includes(queue.CHAT_AFTER_D1_ALARM));
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, [model], 'started once');
+ assert.equal(await env.queue.queued(), false);
+ }
+ {
+ // A status the guide just read is used as it is.
+ 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);
+ }
+ {
+ // A Pause on the guide holds the chat model until Resume queues it again;
+ // a D1 transfer stopped any other way does not.
+ const env = make();
+ await env.queue.queue(model);
+ await env.queue.pause();
+ env.d1 = { status: 'stopped', progress: 40 };
+ assert.equal(await env.queue.advance(), true, 'paused on the guide');
+ assert.deepEqual(env.starts, []);
+ await env.queue.queue(model);
+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY].paused, false, 'Resume');
+ assert.equal(await env.queue.advance(), false, 'stopped by a stall, its time limit or the Providers page');
+ assert.deepEqual(env.starts, [model]);
+ }
+ {
+ // Paused on the guide, but the package was already saved: nothing to wait for.
+ const env = make();
+ await env.queue.queue(model);
+ await env.queue.pause();
+ env.d1 = { status: 'stopped', cached: true };
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, [model]);
+ }
+ for (const d1 of [{ status: 'error', error: 'no bytes' }, { status: 'idle' }, { status: 'cached' }]) {
+ // Failed, reset or saved: the chat model does not wait any longer.
+ const env = make();
+ await env.queue.queue(model);
+ env.d1 = d1;
+ assert.equal(await env.queue.advance(), false, d1.status);
+ assert.deepEqual(env.starts, [model], d1.status);
+ }
+ for (const chat of [{ status: 'downloading' }, { status: 'paused' }, { status: 'ready', ready: true }, { status: 'loading' }]) {
+ // A chat model already under way, paused elsewhere or ready is left alone.
+ const env = make({ chat });
+ await env.queue.queue(model);
+ env.d1 = { status: 'ready', loaded: true };
+ assert.equal(await env.queue.advance(), false, chat.status);
+ assert.deepEqual(env.starts, [], chat.status);
+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY], undefined, chat.status);
+ }
+ {
+ // Another model holds the WebGPU download slot: start when it is free.
+ const env = make({ chat: { status: 'not-downloaded', activeTransfer: { modelId: 'other/model', status: 'downloading' } } });
+ await env.queue.queue(model);
+ env.d1 = { status: 'ready', loaded: true };
+ assert.equal(await env.queue.advance(), true);
+ assert.deepEqual(env.starts, []);
+ env.chat = { status: 'not-downloaded' };
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, [model]);
+ }
+ {
+ // D1's status cannot be read: try again later.
+ const env = make({ d1: new Error('offscreen document closed') });
+ await env.queue.queue(model);
+ assert.equal(await env.queue.advance(), true);
+ assert.deepEqual(env.starts, []);
+ }
+ {
+ // A queue older than D1's own time limit allows is dropped, unstarted.
+ const env = make();
+ await env.queue.queue(model);
+ env.now += queue.CHAT_AFTER_D1_MAX_AGE_MS + 1;
+ env.d1 = { status: 'ready', loaded: true };
+ assert.equal(await env.queue.advance(), false);
+ assert.deepEqual(env.starts, []);
+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY], undefined);
+ }
+ {
+ // The guide's read and the alarm at the same moment start it once.
+ const env = make();
+ await env.queue.queue(model);
+ env.d1 = { status: 'ready', loaded: true };
+ assert.deepEqual(await Promise.all([env.queue.advance(), env.queue.advance(), env.queue.advance()]), [false, false, false]);
+ assert.deepEqual(env.starts, [model]);
+ }
+ {
+ // A stored entry without a model is removed.
+ const env = make();
+ env.area[queue.CHAT_AFTER_D1_KEY] = { queuedAt: env.now };
+ assert.equal(await env.queue.advance(), false);
+ assert.equal(env.area[queue.CHAT_AFTER_D1_KEY], undefined);
+ }
+});
+
test('new shared decision runtime modules are byte-identical across Chrome and Firefox', async () => {
for (const name of ['d1-config.js', 'd1-runtime.js', 'd1-preprocess.js', 'd1-cache.js', 'd1-worker.js', 'd1-host.js', 'd1-diagnostic.js']) assert.deepEqual(await readFile(new URL(`../src/chrome/src/providers/${name}`, import.meta.url)), await readFile(new URL(`../src/firefox/src/providers/${name}`, import.meta.url)), name);
});
diff --git a/test/run.js b/test/run.js
index c7bb1388e..64849ec01 100644
--- a/test/run.js
+++ b/test/run.js
@@ -45576,13 +45576,14 @@ test('chrome install page downloads D1 first, then the chat model, and says what
const modelId = webgpu.WEBGPU_BONSAI2_VL_2B_MODEL_ID;
const normalize = install.normalizeD1Snapshot;
- assert.deepEqual(normalize({ status: 'ready', loaded: true, usedForDecisions: true }), { status: 'ready', progress: 100, error: '', usedForDecisions: true });
+ assert.deepEqual(normalize({ status: 'ready', loaded: true, usedForDecisions: true }), { status: 'ready', progress: 100, error: '', usedForDecisions: true, chatModelQueued: false });
+ assert.equal(normalize({ status: 'downloading', progress: 3, chatModelQueued: true }).chatModelQueued, true, 'the background still holds the chat model');
assert.equal(normalize({ status: 'cached' }).status, 'saved');
assert.equal(normalize({ status: 'error', cached: true, error: 'WebGPU device lost' }).status, 'saved', 'a verified cache is still saved');
- assert.deepEqual(normalize({ status: 'downloading', progress: 41.6 }), { status: 'downloading', progress: 42, error: '', usedForDecisions: false });
+ assert.deepEqual(normalize({ status: 'downloading', progress: 41.6 }), { status: 'downloading', progress: 42, error: '', usedForDecisions: false, chatModelQueued: false });
assert.equal(normalize({ status: 'loading', progress: 100 }).status, 'downloading');
assert.equal(normalize({ status: 'stopped', progress: 30 }).status, 'paused');
- assert.deepEqual(normalize({ status: 'error', error: 'no bytes' }), { status: 'error', progress: 0, error: 'no bytes', usedForDecisions: false });
+ assert.deepEqual(normalize({ status: 'error', error: 'no bytes' }), { status: 'error', progress: 0, error: 'no bytes', usedForDecisions: false, chatModelQueued: false });
assert.equal(normalize({ status: 'idle' }).status, 'idle');
assert.equal(normalize(undefined).status, 'idle');
const view = install.d1View;
@@ -45653,19 +45654,41 @@ test('chrome install page downloads D1 first, then the chat model, and says what
});
const d1Commands = (runtime) => runtime.sent.filter((message) => message.action === 'd1_control' && message.command !== 'status').map((message) => message.command);
+ // Chat-model downloads the page asked for itself.
const chatStarts = (runtime) => runtime.sent.filter((message) => message.action === 'start_webgpu_download').length;
+ const d1Downloads = (runtime) => runtime.sent.filter((message) => message.action === 'd1_control' && message.command === 'download');
+ // The background's side (providers/install-download-queue.js, tested on its
+ // own in test/d1-decision.mjs): a guide download that names the chat model
+ // queues it, a Pause here holds it, and the next read of D1's status once
+ // D1's download is over starts it.
+ const queueBackground = (state) => ({
+ 'd1_control:status': () => {
+ const raw = state.d1.status;
+ if (state.queued && !['checking', 'downloading', 'loading'].includes(raw) && !(raw === 'stopped' && state.pausedHere)) {
+ state.queued = false;
+ state.backgroundStarts = (state.backgroundStarts || 0) + 1;
+ state.bonsai = { status: 'downloading', modelId, loaded: 100, total: 1000 };
+ }
+ return { success: true, result: { ...state.d1, usedForDecisions: raw === 'ready', chatModelQueued: !!state.queued } };
+ },
+ 'd1_control:probe': { success: true, result: { success: true } },
+ 'd1_control:download': (message) => {
+ state.d1 = { status: 'downloading', progress: state.d1.progress || 5 };
+ if (message.chatModel) { state.queued = true; state.pausedHere = false; }
+ return { success: true, result: { started: true, ...state.d1, chatModelQueued: !!state.queued } };
+ },
+ 'd1_control:stop': () => {
+ state.d1 = { status: 'stopped', progress: 30 };
+ if (state.queued) state.pausedHere = true;
+ return { success: true, result: { stopped: true, chatModelQueued: !!state.queued } };
+ },
+ });
{
// A new install: one click downloads D1 first, then the chat model; Auto
// then uses D1 for its checks.
const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'idle' } };
- const runtime = fakeRuntime({
- ...bonsai(state),
- 'd1_control:status': () => ({ success: true, result: { ...state.d1, usedForDecisions: state.d1.status === 'ready' } }),
- 'd1_control:probe': { success: true, result: { success: true } },
- 'd1_control:download': () => { state.d1 = { status: 'downloading', progress: 5 }; return { success: true, result: { started: true, ...state.d1 } }; },
- 'd1_control:stop': () => { state.d1 = { status: 'stopped', progress: 30 }; return { success: true, result: { stopped: true } }; },
- });
+ const runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state) });
const documentLike = fakeDocument();
const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
await settle();
@@ -45679,8 +45702,10 @@ test('chrome install page downloads D1 first, then the chat model, and says what
const d1Messages = runtime.sent.filter((message) => message.action === 'd1_control' && message.command !== 'status');
assert.deepEqual(d1Messages.map((message) => message.command), ['probe', 'download'], 'D1 checks the GPU, then downloads');
assert.deepEqual(d1Messages[1], { target: 'background', action: 'd1_control', command: 'download', consent: true,
- consentVersion: d1Config.D1_CONSENT_VERSION }, 'the click is the consent the note asks for, and nothing else');
+ consentVersion: d1Config.D1_CONSENT_VERSION, chatModel: modelId },
+ 'the click is the consent the note asks for, and the chat model is queued behind D1, so it follows even if the guide is closed');
assert.equal(chatStarts(runtime), 0, 'the chat model waits for D1');
+ assert.equal(state.backgroundStarts || 0, 0);
// While D1 downloads, the button and the bar follow it.
assert.equal(nodes['local-model'].dataset.state, 'downloading');
assert.equal(nodes['local-model-label'].textContent, 'install.local_model.pause');
@@ -45691,7 +45716,7 @@ test('chrome install page downloads D1 first, then the chat model, and says what
assert.equal(nodes['local-model-d1'].hidden, false);
assert.equal(nodes['local-model-d1'].textContent, 'install.local_model.d1_downloading|5');
- // Pause stops D1, and Resume carries on with it; the chat model still waits.
+ // Pause stops D1 and holds the chat model; Resume carries on with both.
await nodes['local-model-button'].fire('click');
await settle();
assert.deepEqual(d1Commands(runtime), ['probe', 'download', 'stop']);
@@ -45699,19 +45724,20 @@ test('chrome install page downloads D1 first, then the chat model, and says what
assert.equal(nodes['local-model-d1'].textContent, 'install.local_model.d1_paused|30');
assert.equal(nodes['local-model-label'].textContent, 'install.local_model.resume');
assert.equal(nodes['local-model'].dataset.state, 'paused');
- assert.equal(chatStarts(runtime), 0);
+ assert.equal(state.backgroundStarts || 0, 0, 'a Pause here holds the chat model');
await nodes['local-model-button'].fire('click');
await settle();
- assert.equal(runtime.sent.filter((message) => message.action === 'd1_control' && message.command === 'download').length, 2,
- 'resume downloads what D1 still lacks');
+ assert.equal(d1Downloads(runtime).length, 2, 'resume downloads what D1 still lacks');
+ assert.equal(d1Downloads(runtime)[1].chatModel, modelId, 'and keeps the chat model queued behind it');
assert.equal(chatStarts(runtime), 0);
- // D1 is ready: now the chat model downloads.
+ // D1 is ready: the background starts the chat model, and the page shows it.
state.d1 = { status: 'ready', loaded: true };
await controller.d1.refresh();
await settle();
assert.equal(nodes['local-model-d1'].textContent, 'install.local_model.d1_ready');
- assert.equal(chatStarts(runtime), 1, 'the chat model follows D1');
+ assert.equal(state.backgroundStarts, 1, 'the chat model follows D1');
+ assert.equal(chatStarts(runtime), 0, 'the page does not start it a second time');
assert.equal(nodes['local-model'].dataset.state, 'downloading');
assert.equal(nodes['local-model-status'].textContent, 'install.local_model.downloading|10');
assert.equal(nodes['local-model-progress'].getAttribute('aria-valuenow'), '10', 'the bar follows the chat model now');
@@ -45729,12 +45755,7 @@ test('chrome install page downloads D1 first, then the chat model, and says what
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 runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state), 'd1_control:probe': () => probe });
const documentLike = fakeDocument();
const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
await settle();
@@ -45748,7 +45769,7 @@ test('chrome install page downloads D1 first, then the chat model, and says what
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(d1Downloads(runtime).length, 1);
assert.equal(button.disabled, false, 'Pause is offered once D1 is downloading');
assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause');
assert.equal(documentLike.nodes['local-model'].dataset.state, 'downloading');
@@ -45812,6 +45833,7 @@ test('chrome install page downloads D1 first, then the chat model, and says what
await nodes['local-model-button'].fire('click');
await settle();
assert.deepEqual(d1Commands(runtime), ['probe', 'download']);
+ assert.equal(d1Downloads(runtime)[0].chatModel, undefined, 'no chat model to queue');
assert.ok(!runtime.sent.some((message) => ['test_provider', 'start_webgpu_download'].includes(message.action)), 'the ready chat model is left alone');
assert.equal(nodes['local-model-label'].textContent, 'install.local_model.pause');
assert.equal(nodes['local-model-d1'].textContent, 'install.local_model.d1_downloading|20');
@@ -45822,7 +45844,8 @@ test('chrome install page downloads D1 first, then the chat model, and says what
assert.equal(nodes['local-model-label'].textContent, 'install.local_model.resume');
await nodes['local-model-button'].fire('click');
await settle();
- assert.equal(runtime.sent.filter((message) => message.action === 'd1_control' && message.command === 'download').length, 2);
+ assert.equal(d1Downloads(runtime).length, 2);
+ assert.equal(d1Downloads(runtime)[1].chatModel, undefined);
state.d1 = { status: 'ready', loaded: true };
await controller.d1.refresh();
assert.equal(nodes['local-model-label'].textContent, 'install.local_model.ready');
@@ -45833,10 +45856,9 @@ test('chrome install page downloads D1 first, then the chat model, and says what
{
// A GPU D1 cannot use: say so, and the chat model downloads at once.
- const state = { bonsai: { status: 'not-downloaded', modelId } };
+ const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'idle' } };
const runtime = fakeRuntime({
- ...bonsai(state),
- 'd1_control:status': { success: true, result: { status: 'idle', usedForDecisions: false } },
+ ...bonsai(state), ...queueBackground(state),
'd1_control:probe': { success: false, error: 'D1 requires hardware WebGPU, not a software adapter.' },
});
const documentLike = fakeDocument();
@@ -45844,70 +45866,106 @@ test('chrome install page downloads D1 first, then the chat model, and says what
await settle();
await documentLike.nodes['local-model-button'].fire('click');
await settle();
- assert.ok(!runtime.sent.some((message) => message.action === 'd1_control' && message.command === 'download'), 'nothing downloads that cannot run');
- assert.equal(chatStarts(runtime), 1);
+ assert.equal(d1Downloads(runtime).length, 0, 'nothing downloads that cannot run');
+ assert.equal(chatStarts(runtime), 1, 'the page starts the chat model itself');
assert.equal(documentLike.nodes['local-model'].dataset.state, 'downloading');
assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_unsupported');
controller.dispose();
}
{
- // D1's download stops with an error: the chat model still follows, and the
- // button offers to retry D1 once the chat model is ready.
+ // D1's download cannot start: the page starts the chat model itself.
const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'idle' } };
const runtime = fakeRuntime({
- ...bonsai(state),
- 'd1_control:status': () => ({ success: true, result: { ...state.d1, usedForDecisions: false } }),
- 'd1_control:probe': { success: true, result: { success: true } },
- 'd1_control:download': () => { state.d1 = { status: 'downloading', progress: 50 }; return { success: true, result: { started: true, ...state.d1 } }; },
+ ...bonsai(state), ...queueBackground(state),
+ 'd1_control:download': { success: false, error: 'D1 is disabled in Strict Secret Mode.' },
});
const documentLike = fakeDocument();
const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
await settle();
await documentLike.nodes['local-model-button'].fire('click');
await settle();
- assert.equal(chatStarts(runtime), 0);
- state.d1 = { status: 'error', error: 'D1 download made no byte progress for two minutes.' };
- await controller.d1.refresh();
- await settle();
- assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_error|D1 download made no byte progress for two minutes.');
- assert.equal(chatStarts(runtime), 1, 'a failed D1 does not hold the chat model back');
- state.bonsai = { status: 'ready', ready: true, modelId };
- await controller.refresh();
- assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.retry');
+ assert.equal(chatStarts(runtime), 1);
+ assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_error|D1 is disabled in Strict Secret Mode.');
controller.dispose();
}
{
- // A D1 transfer that stops without a Pause here (no bytes for two minutes,
- // say) does not hold the chat model back; a Pause here does.
- const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'idle' } };
- const runtime = fakeRuntime({
- ...bonsai(state),
- 'd1_control:status': () => ({ success: true, result: { ...state.d1, usedForDecisions: false } }),
- 'd1_control:probe': { success: true, result: { success: true } },
- 'd1_control:download': () => { state.d1 = { status: 'downloading', progress: 25 }; return { success: true, result: { started: true, ...state.d1 } }; },
- 'd1_control:stop': () => { state.d1 = { status: 'stopped', progress: 25 }; return { success: true, result: { stopped: true } }; },
- });
+ // D1's download stops with an error, or stalls without a Pause here: the
+ // background still starts the chat model, and once that is ready the
+ // button offers to retry D1.
+ for (const stopped of [{ status: 'error', error: 'D1 download made no byte progress for two minutes.' },
+ { status: 'stopped', progress: 60, error: 'D1 download made no byte progress for two minutes.' }]) {
+ const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'idle' } };
+ const runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state) });
+ const documentLike = fakeDocument();
+ const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
+ await settle();
+ await documentLike.nodes['local-model-button'].fire('click');
+ await settle();
+ assert.equal(state.backgroundStarts || 0, 0);
+ state.d1 = stopped;
+ await controller.d1.refresh();
+ await settle();
+ assert.equal(state.backgroundStarts, 1, `${stopped.status}: a stopped D1 does not hold the chat model back`);
+ assert.equal(chatStarts(runtime), 0);
+ assert.equal(documentLike.nodes['local-model-status'].textContent, 'install.local_model.downloading|10', 'the page shows the chat download');
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause', 'the chat download has the button');
+ state.bonsai = { status: 'ready', ready: true, modelId };
+ await controller.refresh();
+ assert.equal(documentLike.nodes['local-model-label'].textContent,
+ stopped.status === 'error' ? 'install.local_model.retry' : 'install.local_model.resume');
+ controller.dispose();
+ }
+ }
+
+ {
+ // A guide reopened while D1 downloads with the chat model queued: its
+ // Pause and Resume keep the chat model queued behind D1.
+ const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'downloading', progress: 70 }, queued: true };
+ const runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state) });
const documentLike = fakeDocument();
const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
await settle();
const button = documentLike.nodes['local-model-button'];
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause');
await button.fire('click');
await settle();
+ assert.equal(state.pausedHere, true);
await button.fire('click');
await settle();
- assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_paused|25');
- assert.equal(chatStarts(runtime), 0, 'paused here: Resume carries on with D1 first');
- await button.fire('click');
+ assert.equal(d1Downloads(runtime).length, 1);
+ assert.equal(d1Downloads(runtime)[0].chatModel, modelId, 'Resume keeps the chat model queued');
+ state.d1 = { status: 'ready', loaded: true };
+ await controller.d1.refresh();
await settle();
- assert.equal(chatStarts(runtime), 0);
- state.d1 = { status: 'stopped', progress: 60, error: 'D1 download made no byte progress for two minutes.' };
+ assert.equal(state.backgroundStarts, 1);
+ assert.equal(documentLike.nodes['local-model-status'].textContent, 'install.local_model.downloading|10');
+ controller.dispose();
+ }
+
+ {
+ // A guide opened while D1 downloads with nothing queued (started on the
+ // Providers page, say) follows that download, but only a click here
+ // downloads the chat model.
+ const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'downloading', progress: 70 } };
+ const runtime = fakeRuntime({ ...bonsai(state), ...queueBackground(state) });
+ const documentLike = fakeDocument();
+ const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
+ await settle();
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause');
+ assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_downloading|70');
+ await documentLike.nodes['local-model-button'].fire('click');
+ await settle();
+ await documentLike.nodes['local-model-button'].fire('click');
+ await settle();
+ assert.equal(d1Downloads(runtime)[0].chatModel, undefined, 'resuming a download started elsewhere queues nothing');
+ state.d1 = { status: 'ready', loaded: true };
await controller.d1.refresh();
await settle();
- assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_paused|60');
- assert.equal(chatStarts(runtime), 1, 'a stalled D1 does not hold the chat model back');
- assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause', 'the chat download has the button');
+ assert.equal(state.backgroundStarts || 0, 0, 'no click, no chat model download');
+ assert.equal(chatStarts(runtime), 0);
+ assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.download');
controller.dispose();
}
@@ -45931,27 +45989,6 @@ test('chrome install page downloads D1 first, then the chat model, and says what
controller.dispose();
}
- {
- // A guide opened while D1 downloads (started on the Providers page, say)
- // follows that download, but only a click here downloads the chat model.
- const state = { bonsai: { status: 'not-downloaded', modelId }, d1: { status: 'downloading', progress: 70 } };
- const runtime = fakeRuntime({
- ...bonsai(state),
- 'd1_control:status': () => ({ success: true, result: { ...state.d1, usedForDecisions: state.d1.status === 'ready' } }),
- });
- const documentLike = fakeDocument();
- const controller = install.initializeLocalModelDownload({ documentLike, runtime, translate, pollMs: 60_000 });
- await settle();
- assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.pause');
- assert.equal(documentLike.nodes['local-model-d1'].textContent, 'install.local_model.d1_downloading|70');
- state.d1 = { status: 'ready', loaded: true };
- await controller.d1.refresh();
- await settle();
- assert.equal(chatStarts(runtime), 0, 'no click, no chat model download');
- assert.equal(documentLike.nodes['local-model-label'].textContent, 'install.local_model.download');
- controller.dispose();
- }
-
{
// No hardware WebGPU at all: D1 is not even checked.
const runtime = fakeRuntime({