Skip to content

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

Keep D1 loaded when a decision times out

Claude8b51e3b9d3fa
diff --git a/docs/providers-and-models.md b/docs/providers-and-models.md
index d33eef6ed..ce3412cc2 100644
--- a/docs/providers-and-models.md
+++ b/docs/providers-and-models.md
@@ -125,6 +125,13 @@ code is not evidence that every browser/GPU is supported. CPU/WASM control and
shape operators are permitted and explicitly disclosed; it is not a pure-GPU
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** or **Load cached model**, D1 stays loaded until
+it is unloaded, removed or switched off in Settings: a decision that finds it
+unloaded (a browser restart, a discarded stalled session, a lost GPU device)
+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.
Page state and screenshots remain untrusted evidence, never instructions.
No inference input goes to Hugging Face. Existing optional provider/research
sharing settings and existing privacy boundaries are not bypassed.
diff --git a/docs/security-model.md b/docs/security-model.md
index 3bcfd54e3..7146ac28c 100644
--- a/docs/security-model.md
+++ b/docs/security-model.md
@@ -78,8 +78,11 @@ are accepted; page-selected remote image URLs cannot turn D1 into a fetch proxy.
Page text/pixels stay untrusted and existing redaction/wrapping remains in place.
Disabling outsourcing or Strict Secret Mode discards the Worker and its GPU
-sessions; cached weights remain until explicitly removed. Timeouts discard a
-stalled inference session rather than inventing an answer. CPU/WASM control/shape
+sessions; cached weights remain until explicitly removed. A caller that stops
+waiting cancels only its own request; the Worker's deadline discards a stalled
+inference session. Neither invents an answer. While the user's Settings Load
+stands, a reload after a restart reads only the verified cache and never
+downloads. CPU/WASM control/shape
fallback is disclosed, not represented as pure GPU. A positive experimental D1
judgment alone cannot establish completion: independent LLM or existing checks
still apply, including deterministic recipient/payment/download/authorization
diff --git a/src/chrome/src/agent/systemone-judge.js b/src/chrome/src/agent/systemone-judge.js
index c0855542b..a8f22cb55 100644
--- a/src/chrome/src/agent/systemone-judge.js
+++ b/src/chrome/src/agent/systemone-judge.js
@@ -66,6 +66,10 @@ export function systemOneFailureReason(error) {
JEV_INVALID_DISTRIBUTION: 'invalid_distribution',
JEV_INVALID_CHOICE: 'invalid_choice',
JEV_INVALID_SCORE: 'invalid_score',
+ // The local D1 Worker (providers/d1-worker.js) answers without its model.
+ D1_MODEL_LOADING: 'model_loading',
+ D1_MODEL_NOT_LOADED: 'model_not_loaded',
+ D1_REQUEST_CANCELLED: 'cancelled',
};
if (mapped[error?.code]) return mapped[error.code];
const status = Number(error?.status || 0);
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index 1b3f0b08f..11bc55eaf 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -3,7 +3,7 @@ import { resolveDecisionConfig, listDecisionModels } from './agent/decision-conf
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 } from './providers/d1-config.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(() => {});
@@ -4909,7 +4909,12 @@ async function handleMessage(msg, sender) {
if (!['status', 'probe', 'download', 'load', 'stop', 'dispose', 'clear', 'reset', 'consent'].includes(msg.command)) throw new Error('Invalid D1 model command.');
if (['download', 'load', 'consent'].includes(msg.command)) {
if (msg.consent !== true || msg.consentVersion !== D1_CONSENT_VERSION) throw new Error('Explicit D1 experimental-model consent is required.');
- await api.storage.local.set({ [D1_CONSENT_KEY]: D1_CONSENT_VERSION });
+ // Download and Load also ask D1 to stay loaded: after a browser
+ // restart or a discarded session the next decision reloads the cache.
+ await api.storage.local.set({ [D1_CONSENT_KEY]: D1_CONSENT_VERSION, ...(msg.command === 'consent' ? {} : { [D1_KEEP_LOADED_KEY]: true }) });
+ } else if (['reset', 'dispose', 'clear'].includes(msg.command)) {
+ // Unload, Remove and switching D1 off in Settings end that request.
+ await api.storage.local.set({ [D1_KEEP_LOADED_KEY]: false });
}
return { success: true, result: msg.command === 'consent' ? { consented: true } : await d1Request(msg.command, { consentVersion: D1_CONSENT_VERSION }) };
} catch (error) { return { success: false, error: error.message }; }
diff --git a/src/chrome/src/offscreen/d1-inference-host.js b/src/chrome/src/offscreen/d1-inference-host.js
index 39a2aa80d..a9a9bb522 100644
--- a/src/chrome/src/offscreen/d1-inference-host.js
+++ b/src/chrome/src/offscreen/d1-inference-host.js
@@ -1,9 +1,10 @@
-import { dispatchD1Worker, resetD1Worker } from '../providers/d1-host.js';
+import { cancelD1Request, dispatchD1Worker, resetD1Worker } from '../providers/d1-host.js';
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type !== 'd1-offscreen') return;
if (sender.id !== chrome.runtime.id || sender.tab || !String(sender.url || '').startsWith(chrome.runtime.getURL(''))) { sendResponse({ ok: false, error: 'D1 accepts extension-owned requests only.' }); return; }
if (message.command === 'reset') { resetD1Worker(); sendResponse({ ok: true, result: { reset: true } }); return; }
- dispatchD1Worker(message.command, message.payload, message.timeoutMs).then(result => sendResponse({ ok: true, result }), error => sendResponse({ ok: false, error: error.message }));
+ if (message.command === 'cancel') { cancelD1Request(message.payload?.requestId); sendResponse({ ok: true, result: { cancelled: true } }); return; }
+ dispatchD1Worker(message.command, message.payload, message.timeoutMs).then(result => sendResponse({ ok: true, result }), error => sendResponse({ ok: false, error: error.message, ...(typeof error.code === 'string' ? { code: error.code } : {}) }));
return true;
});
diff --git a/src/chrome/src/providers/d1-config.js b/src/chrome/src/providers/d1-config.js
index e952956b5..788248ae0 100644
--- a/src/chrome/src/providers/d1-config.js
+++ b/src/chrome/src/providers/d1-config.js
@@ -1,6 +1,11 @@
export const D1_MODEL_ID = 'webbrain-one/d1-browser-decision-fp16';
export const D1_CONSENT_VERSION = 1;
export const D1_CONSENT_KEY = 'd1WebgpuConsentVersion';
+// Set by Settings' Download and load / Load cached model, cleared by Unload,
+// Remove and switching D1 off there. While it is set, a decision that finds
+// the model unloaded (a browser restart, a discarded stalled session, a lost
+// GPU device) reloads the verified cache in the background; never downloads.
+export const D1_KEEP_LOADED_KEY = 'd1WebgpuKeepLoaded';
export const D1_CACHE_NAME = 'webbrain-d1-fp16-v1';
// Immutable release package; downloaded data is verified before local loading.
export const D1_RELEASE = Object.freeze({ revision: 'd4916f0c47d46f16e7876e425eb12a122671d3eb', manifestSha256: 'a94fb80ba47cc6c3e05cc3dbda45f9f2b99f4f1999b0770a0a09ba59e0c861ed' });
diff --git a/src/chrome/src/providers/d1-host.js b/src/chrome/src/providers/d1-host.js
index 4d31636eb..bac3518e6 100644
--- a/src/chrome/src/providers/d1-host.js
+++ b/src/chrome/src/providers/d1-host.js
@@ -7,6 +7,12 @@ export function resetD1Worker(reason = new Error('D1 worker reset.')) {
pending.clear();
}
+// The caller stopped waiting: the Worker skips or stops that evaluation and
+// keeps the model. Only a request's own deadline below discards the Worker.
+export function cancelD1Request(requestId) {
+ if (worker && typeof requestId === 'string') worker.postMessage({ id: ++nextId, type: 'cancel', payload: { requestId } });
+}
+
export function dispatchD1Worker(type, payload, timeoutMs = 90_000) {
if (!worker) {
worker = new Worker((globalThis.browser || globalThis.chrome).runtime.getURL('src/providers/d1-worker.js'), { type: 'module' });
@@ -14,7 +20,7 @@ export function dispatchD1Worker(type, payload, timeoutMs = 90_000) {
if (event.data?.state) { state = event.data.state; return; }
const request = pending.get(event.data?.id); if (!request) return;
pending.delete(event.data.id); clearTimeout(request.timer);
- event.data.ok ? request.resolve(event.data.result) : request.reject(new Error(event.data.error));
+ event.data.ok ? request.resolve(event.data.result) : request.reject(Object.assign(new Error(event.data.error), typeof event.data.code === 'string' ? { code: event.data.code } : {}));
};
worker.onerror = event => resetD1Worker(new Error('D1 worker failed: ' + event.message));
}
diff --git a/src/chrome/src/providers/d1-worker.js b/src/chrome/src/providers/d1-worker.js
index 254db1ff2..24cd127e5 100644
--- a/src/chrome/src/providers/d1-worker.js
+++ b/src/chrome/src/providers/d1-worker.js
@@ -4,8 +4,18 @@ import { createD1Runtime } from './d1-runtime.js';
let runtime = null, device = null, state = { status: 'idle', progress: 0, loaded: false }, transfer = null;
let queue = Promise.resolve();
+// One load at a time, shared by Settings' Load, a download's last step and a
+// reload. A reload starts once per Worker until a load succeeds again, so a
+// broken cache or GPU is not verified again on every decision.
+let loading = null, reloadController = null, reloadBlocked = false;
+// An evaluation whose caller stopped waiting (a judge timeout, Stop) is
+// cancelled by request id instead of discarding the loaded model: a queued
+// one is skipped, a running one stops between graph runs (d1-runtime.js).
+const requests = new Map(), cancelledEarly = new Set();
const publish = update => { state = { ...state, ...update }; self.postMessage({ state }); };
const errors = [];
+const codedError = (code, message) => Object.assign(new Error(message), { code });
+const cancelled = () => codedError('D1_REQUEST_CANCELLED', 'D1 request cancelled.');
async function probe() {
if (!navigator.gpu) throw new Error('Hardware WebGPU is unavailable in this browser.');
@@ -13,8 +23,13 @@ async function probe() {
return { adapter, info: assertD1Adapter(adapter) };
}
-async function prepare(signal) {
- if (runtime) return { ready: true, ...state };
+function prepare(signal) {
+ if (runtime) return Promise.resolve({ ready: true, ...state });
+ loading ??= load(signal).finally(() => { loading = null; });
+ return loading;
+}
+
+async function load(signal) {
const { adapter, info } = await probe();
const directory = await openD1Cache(), manifest = await readD1Manifest(directory), specs = validateD1Manifest(manifest);
const { PreTrainedTokenizer } = await import('../../vendor/d1/transformers.web.js');
@@ -40,18 +55,50 @@ async function prepare(signal) {
if (signal?.aborted) throw signal.reason;
}
runtime = createD1Runtime({ ort, tokenizer, config, ratios, sessions, device: loadingDevice, model: D1_MODEL_ID, dtype: manifest.dtype });
+ reloadBlocked = false;
publish({ status: 'ready', loaded: true, progress: 100, adapter: info, runtime: ort.env.versions, error: '', cpu_fallback_policy: 'CPU/WASM control/shape fallback allowed; not a pure GPU claim.' });
return { ready: true, ...state };
} catch (error) { for (const session of Object.values(sessions)) await session.release().catch(() => {}); loadingDevice.destroy(); if (device === loadingDevice) device = null; publish({ status: signal?.aborted ? 'stopped' : 'error', loaded: false, error: String(error.message || error) }); throw error; }
}
-async function status() {
- if (transfer || runtime || ['error', 'stopped'].includes(state.status)) return { ...state, ready: !!runtime };
+// The finished download's marker in the cache, or null.
+async function readyMarker() {
try {
const directory = await openD1Cache();
- const marker = JSON.parse(await (await (await directory.getFileHandle('ready.json')).getFile()).text());
- return { ...state, status: marker.revision === D1_RELEASE.revision && marker.manifestSha256 === D1_RELEASE.manifestSha256 ? 'cached' : 'idle', ready: false, loaded: false };
- } catch { return { ...state, ready: false }; }
+ return JSON.parse(await (await (await directory.getFileHandle('ready.json')).getFile()).text());
+ } catch { return null; }
+}
+const currentPackage = marker => marker?.revision === D1_RELEASE.revision && marker?.manifestSha256 === D1_RELEASE.manifestSha256;
+
+async function status() {
+ if (transfer || runtime || loading || ['error', 'stopped'].includes(state.status)) return { ...state, ready: !!runtime };
+ const marker = await readyMarker();
+ return marker ? { ...state, status: currentPackage(marker) ? 'cached' : 'idle', ready: false, loaded: false } : { ...state, ready: false };
+}
+
+// A decision found the model unloaded while the user's Load choice stands.
+// Only the verified cache is read; the decision itself uses the fallback.
+function reload() {
+ reloadBlocked = true;
+ const controller = new AbortController(); reloadController = controller;
+ loading = (async () => {
+ try {
+ if (!currentPackage(await readyMarker())) throw new Error('D1 is not downloaded. Download it in Decision models settings.');
+ return await load(controller.signal);
+ } catch (error) {
+ // Settings shows why D1 still answers nothing; no caller awaits a reload.
+ if (!controller.signal.aborted) publish({ status: 'error', loaded: false, error: String(error?.message || error) });
+ throw error;
+ }
+ })().finally(() => { loading = null; if (reloadController === controller) reloadController = null; });
+ loading.catch(() => {});
+}
+
+function unloaded(payload) {
+ if (!loading && !transfer && !reloadBlocked && payload?.keepLoaded === true && payload.consentVersion === D1_CONSENT_VERSION) reload();
+ return loading
+ ? codedError('D1_MODEL_LOADING', 'D1 is loading its cached model; this decision uses the existing fallback.')
+ : codedError('D1_MODEL_NOT_LOADED', 'D1 is not loaded. Download or load it explicitly in Decision models settings.');
}
function startDownload(payload) {
@@ -81,22 +128,47 @@ function startDownload(payload) {
return { started: true, ...state };
}
-async function execute(type, payload = {}) {
+async function execute(type, payload = {}, signal) {
if (type === 'probe') { const { adapter, info } = await probe(); return { success: true, adapter: info, features: [...adapter.features] }; }
if (type === 'status') return status();
if (type === 'download') return startDownload(payload);
if (type === 'stop') { transfer?.abort(new Error('D1 download stopped by user.')); return { stopped: true }; }
- if (type === 'dispose') { transfer?.abort(new Error('D1 disabled.')); await runtime?.dispose(); runtime = null; device = null; publish({ status: 'idle', loaded: false }); return { disposed: true }; }
+ if (type === 'dispose') {
+ const reason = new Error('D1 disabled.'); transfer?.abort(reason); reloadController?.abort(reason);
+ await loading?.catch(() => {});
+ await runtime?.dispose(); runtime = null; device = null; publish({ status: 'idle', loaded: false }); return { disposed: true };
+ }
if (type === 'load') { if (payload.consentVersion !== D1_CONSENT_VERSION) throw new Error('D1 consent is required.'); if (transfer) throw new Error('D1 download/load is already in progress.'); return prepare(); }
- if (type === 'clear') { if (runtime || transfer) throw new Error('Disable D1 before removing its cache.'); const directory = await openD1Cache(); for await (const [name] of directory.entries()) await directory.removeEntry(name); publish({ status: 'idle', loaded: false }); return { removed: true }; }
- if (type === 'evaluate') { if (!runtime) throw new Error('D1 is not loaded. Download or load it explicitly in Decision models settings.'); const result = await runtime.evaluate(payload); return { ...result, diagnostics: { ...result.diagnostics, adapter: state.adapter, runtime: state.runtime, model_revision: D1_RELEASE.revision, package_manifest_sha256: D1_RELEASE.manifestSha256, gpu_errors: [...errors] } }; }
+ if (type === 'clear') { if (runtime || transfer || loading) throw new Error('Disable D1 before removing its cache.'); const directory = await openD1Cache(); for await (const [name] of directory.entries()) await directory.removeEntry(name); publish({ status: 'idle', loaded: false }); return { removed: true }; }
+ if (type === 'evaluate') {
+ if (signal?.aborted) throw signal.reason;
+ if (!runtime) throw unloaded(payload);
+ const result = await runtime.evaluate({ state: payload.state, images: payload.images, questions: payload.questions, signal });
+ return { ...result, diagnostics: { ...result.diagnostics, adapter: state.adapter, runtime: state.runtime, model_revision: D1_RELEASE.revision, package_manifest_sha256: D1_RELEASE.manifestSha256, gpu_errors: [...errors] } };
+ }
throw new Error('Unknown D1 worker command.');
}
+function cancel(requestId) {
+ const controller = requests.get(requestId);
+ if (controller) { controller.abort(cancelled()); return; }
+ // The cancel overtook its request; keep a bounded record so it is skipped.
+ cancelledEarly.add(requestId);
+ if (cancelledEarly.size > 64) cancelledEarly.delete(cancelledEarly.values().next().value);
+}
+
self.onmessage = event => {
const { id, type, payload } = event.data || {};
- const run = () => execute(type, payload);
+ if (type === 'cancel') {
+ if (typeof payload?.requestId === 'string') cancel(payload.requestId);
+ self.postMessage({ id, ok: true, result: { cancelled: true } }); return;
+ }
+ const requestId = type === 'evaluate' && typeof payload?.requestId === 'string' ? payload.requestId : null;
+ const controller = requestId ? new AbortController() : null;
+ if (requestId) { if (cancelledEarly.delete(requestId)) controller.abort(cancelled()); requests.set(requestId, controller); }
+ const run = () => execute(type, payload, controller?.signal);
const immediate = ['status', 'stop', 'download', 'probe'].includes(type);
const promise = immediate ? Promise.resolve().then(run) : (queue = queue.then(run, run));
- promise.then(result => self.postMessage({ id, ok: true, result }), error => self.postMessage({ id, ok: false, error: String(error.message || error) }));
+ promise.finally(() => { if (requestId && requests.get(requestId) === controller) requests.delete(requestId); })
+ .then(result => self.postMessage({ id, ok: true, result }), error => self.postMessage({ id, ok: false, error: String(error?.message || error), ...(typeof error?.code === 'string' ? { code: error.code } : {}) }));
};
diff --git a/src/chrome/src/providers/d1.js b/src/chrome/src/providers/d1.js
index 2faac2ecf..36dc3c6b2 100644
--- a/src/chrome/src/providers/d1.js
+++ b/src/chrome/src/providers/d1.js
@@ -1,23 +1,32 @@
-import { D1_MODEL_ID, D1_CONSENT_KEY, D1_CONSENT_VERSION } from './d1-config.js';
+import { D1_MODEL_ID, D1_CONSENT_KEY, D1_CONSENT_VERSION, D1_KEEP_LOADED_KEY } from './d1-config.js';
import { ensureOffscreen } from '../offscreen/ensure.js';
-import { dispatchD1Worker, resetD1Worker } from './d1-host.js';
+import { cancelD1Request, dispatchD1Worker, resetD1Worker } from './d1-host.js';
+
+const newRequestId = () => globalThis.crypto?.randomUUID?.() || `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
+const hostError = response => Object.assign(new Error(response?.error || 'D1 offscreen host unavailable.'), typeof response?.code === 'string' ? { code: response.code } : {});
export async function d1Request(command, payload = {}, { timeoutMs = 90_000, signal } = {}) {
const api = globalThis.browser || globalThis.chrome;
- const consent = await api.storage.local.get(D1_CONSENT_KEY);
- if (['evaluate', 'load', 'download'].includes(command) && consent[D1_CONSENT_KEY] !== D1_CONSENT_VERSION) throw new Error('D1 is experimental and requires explicit local-model consent.');
+ const stored = await api.storage.local.get([D1_CONSENT_KEY, D1_KEEP_LOADED_KEY]);
+ if (['evaluate', 'load', 'download'].includes(command) && stored[D1_CONSENT_KEY] !== D1_CONSENT_VERSION) throw new Error('D1 is experimental and requires explicit local-model consent.');
if (signal?.aborted) throw signal.reason;
- const abort = () => { void d1Request('reset').catch(() => {}); };
- signal?.addEventListener('abort', abort, { once: true });
+ // A caller that stops waiting (a judge timeout, Stop) cancels only its own
+ // evaluation; the loaded model stays for the next decision. The Worker's
+ // own deadline (d1-host.js) still discards a stalled session.
+ if (command === 'evaluate') payload = { ...payload, requestId: newRequestId(), consentVersion: stored[D1_CONSENT_KEY], keepLoaded: stored[D1_KEEP_LOADED_KEY] === true };
+ const abort = () => { void d1Request('cancel', { requestId: payload.requestId }).catch(() => {}); };
+ if (command === 'evaluate') signal?.addEventListener('abort', abort, { once: true });
try {
let result;
if (api.offscreen?.createDocument) {
- if (['reset', 'dispose', 'stop'].includes(command) && !await api.offscreen.hasDocument()) return { reset: true };
+ if (['reset', 'dispose', 'stop', 'cancel'].includes(command) && !await api.offscreen.hasDocument()) return { reset: true };
await ensureOffscreen();
+ if (signal?.aborted) throw signal.reason;
const response = await api.runtime.sendMessage({ type: 'd1-offscreen', command, payload, timeoutMs });
- if (!response?.ok) throw new Error(response?.error || 'D1 offscreen host unavailable.'); result = response.result;
+ if (!response?.ok) throw hostError(response); result = response.result;
} else {
if (command === 'reset') { resetD1Worker(); return { reset: true }; }
+ if (command === 'cancel') { cancelD1Request(payload.requestId); return { cancelled: true }; }
result = await dispatchD1Worker(command, payload, timeoutMs);
}
if (signal?.aborted) throw signal.reason; return result;
diff --git a/src/firefox/src/agent/systemone-judge.js b/src/firefox/src/agent/systemone-judge.js
index a4a009f13..07515d878 100644
--- a/src/firefox/src/agent/systemone-judge.js
+++ b/src/firefox/src/agent/systemone-judge.js
@@ -66,6 +66,10 @@ export function systemOneFailureReason(error) {
JEV_INVALID_DISTRIBUTION: 'invalid_distribution',
JEV_INVALID_CHOICE: 'invalid_choice',
JEV_INVALID_SCORE: 'invalid_score',
+ // The local D1 Worker (providers/d1-worker.js) answers without its model.
+ D1_MODEL_LOADING: 'model_loading',
+ D1_MODEL_NOT_LOADED: 'model_not_loaded',
+ D1_REQUEST_CANCELLED: 'cancelled',
};
if (mapped[error?.code]) return mapped[error.code];
const status = Number(error?.status || 0);
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index d4ce24661..ccac3d340 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -3,7 +3,7 @@ import { resolveDecisionConfig, listDecisionModels } from './agent/decision-conf
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 } from './providers/d1-config.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(() => {});
@@ -4282,7 +4282,12 @@ async function handleMessage(msg, sender) {
if (!['status', 'probe', 'download', 'load', 'stop', 'dispose', 'clear', 'reset', 'consent'].includes(msg.command)) throw new Error('Invalid D1 model command.');
if (['download', 'load', 'consent'].includes(msg.command)) {
if (msg.consent !== true || msg.consentVersion !== D1_CONSENT_VERSION) throw new Error('Explicit D1 experimental-model consent is required.');
- await api.storage.local.set({ [D1_CONSENT_KEY]: D1_CONSENT_VERSION });
+ // Download and Load also ask D1 to stay loaded: after a browser
+ // restart or a discarded session the next decision reloads the cache.
+ await api.storage.local.set({ [D1_CONSENT_KEY]: D1_CONSENT_VERSION, ...(msg.command === 'consent' ? {} : { [D1_KEEP_LOADED_KEY]: true }) });
+ } else if (['reset', 'dispose', 'clear'].includes(msg.command)) {
+ // Unload, Remove and switching D1 off in Settings end that request.
+ await api.storage.local.set({ [D1_KEEP_LOADED_KEY]: false });
}
return { success: true, result: msg.command === 'consent' ? { consented: true } : await d1Request(msg.command, { consentVersion: D1_CONSENT_VERSION }) };
} catch (error) { return { success: false, error: error.message }; }
diff --git a/src/firefox/src/providers/d1-config.js b/src/firefox/src/providers/d1-config.js
index e952956b5..788248ae0 100644
--- a/src/firefox/src/providers/d1-config.js
+++ b/src/firefox/src/providers/d1-config.js
@@ -1,6 +1,11 @@
export const D1_MODEL_ID = 'webbrain-one/d1-browser-decision-fp16';
export const D1_CONSENT_VERSION = 1;
export const D1_CONSENT_KEY = 'd1WebgpuConsentVersion';
+// Set by Settings' Download and load / Load cached model, cleared by Unload,
+// Remove and switching D1 off there. While it is set, a decision that finds
+// the model unloaded (a browser restart, a discarded stalled session, a lost
+// GPU device) reloads the verified cache in the background; never downloads.
+export const D1_KEEP_LOADED_KEY = 'd1WebgpuKeepLoaded';
export const D1_CACHE_NAME = 'webbrain-d1-fp16-v1';
// Immutable release package; downloaded data is verified before local loading.
export const D1_RELEASE = Object.freeze({ revision: 'd4916f0c47d46f16e7876e425eb12a122671d3eb', manifestSha256: 'a94fb80ba47cc6c3e05cc3dbda45f9f2b99f4f1999b0770a0a09ba59e0c861ed' });
diff --git a/src/firefox/src/providers/d1-host.js b/src/firefox/src/providers/d1-host.js
index 4d31636eb..bac3518e6 100644
--- a/src/firefox/src/providers/d1-host.js
+++ b/src/firefox/src/providers/d1-host.js
@@ -7,6 +7,12 @@ export function resetD1Worker(reason = new Error('D1 worker reset.')) {
pending.clear();
}
+// The caller stopped waiting: the Worker skips or stops that evaluation and
+// keeps the model. Only a request's own deadline below discards the Worker.
+export function cancelD1Request(requestId) {
+ if (worker && typeof requestId === 'string') worker.postMessage({ id: ++nextId, type: 'cancel', payload: { requestId } });
+}
+
export function dispatchD1Worker(type, payload, timeoutMs = 90_000) {
if (!worker) {
worker = new Worker((globalThis.browser || globalThis.chrome).runtime.getURL('src/providers/d1-worker.js'), { type: 'module' });
@@ -14,7 +20,7 @@ export function dispatchD1Worker(type, payload, timeoutMs = 90_000) {
if (event.data?.state) { state = event.data.state; return; }
const request = pending.get(event.data?.id); if (!request) return;
pending.delete(event.data.id); clearTimeout(request.timer);
- event.data.ok ? request.resolve(event.data.result) : request.reject(new Error(event.data.error));
+ event.data.ok ? request.resolve(event.data.result) : request.reject(Object.assign(new Error(event.data.error), typeof event.data.code === 'string' ? { code: event.data.code } : {}));
};
worker.onerror = event => resetD1Worker(new Error('D1 worker failed: ' + event.message));
}
diff --git a/src/firefox/src/providers/d1-worker.js b/src/firefox/src/providers/d1-worker.js
index 254db1ff2..24cd127e5 100644
--- a/src/firefox/src/providers/d1-worker.js
+++ b/src/firefox/src/providers/d1-worker.js
@@ -4,8 +4,18 @@ import { createD1Runtime } from './d1-runtime.js';
let runtime = null, device = null, state = { status: 'idle', progress: 0, loaded: false }, transfer = null;
let queue = Promise.resolve();
+// One load at a time, shared by Settings' Load, a download's last step and a
+// reload. A reload starts once per Worker until a load succeeds again, so a
+// broken cache or GPU is not verified again on every decision.
+let loading = null, reloadController = null, reloadBlocked = false;
+// An evaluation whose caller stopped waiting (a judge timeout, Stop) is
+// cancelled by request id instead of discarding the loaded model: a queued
+// one is skipped, a running one stops between graph runs (d1-runtime.js).
+const requests = new Map(), cancelledEarly = new Set();
const publish = update => { state = { ...state, ...update }; self.postMessage({ state }); };
const errors = [];
+const codedError = (code, message) => Object.assign(new Error(message), { code });
+const cancelled = () => codedError('D1_REQUEST_CANCELLED', 'D1 request cancelled.');
async function probe() {
if (!navigator.gpu) throw new Error('Hardware WebGPU is unavailable in this browser.');
@@ -13,8 +23,13 @@ async function probe() {
return { adapter, info: assertD1Adapter(adapter) };
}
-async function prepare(signal) {
- if (runtime) return { ready: true, ...state };
+function prepare(signal) {
+ if (runtime) return Promise.resolve({ ready: true, ...state });
+ loading ??= load(signal).finally(() => { loading = null; });
+ return loading;
+}
+
+async function load(signal) {
const { adapter, info } = await probe();
const directory = await openD1Cache(), manifest = await readD1Manifest(directory), specs = validateD1Manifest(manifest);
const { PreTrainedTokenizer } = await import('../../vendor/d1/transformers.web.js');
@@ -40,18 +55,50 @@ async function prepare(signal) {
if (signal?.aborted) throw signal.reason;
}
runtime = createD1Runtime({ ort, tokenizer, config, ratios, sessions, device: loadingDevice, model: D1_MODEL_ID, dtype: manifest.dtype });
+ reloadBlocked = false;
publish({ status: 'ready', loaded: true, progress: 100, adapter: info, runtime: ort.env.versions, error: '', cpu_fallback_policy: 'CPU/WASM control/shape fallback allowed; not a pure GPU claim.' });
return { ready: true, ...state };
} catch (error) { for (const session of Object.values(sessions)) await session.release().catch(() => {}); loadingDevice.destroy(); if (device === loadingDevice) device = null; publish({ status: signal?.aborted ? 'stopped' : 'error', loaded: false, error: String(error.message || error) }); throw error; }
}
-async function status() {
- if (transfer || runtime || ['error', 'stopped'].includes(state.status)) return { ...state, ready: !!runtime };
+// The finished download's marker in the cache, or null.
+async function readyMarker() {
try {
const directory = await openD1Cache();
- const marker = JSON.parse(await (await (await directory.getFileHandle('ready.json')).getFile()).text());
- return { ...state, status: marker.revision === D1_RELEASE.revision && marker.manifestSha256 === D1_RELEASE.manifestSha256 ? 'cached' : 'idle', ready: false, loaded: false };
- } catch { return { ...state, ready: false }; }
+ return JSON.parse(await (await (await directory.getFileHandle('ready.json')).getFile()).text());
+ } catch { return null; }
+}
+const currentPackage = marker => marker?.revision === D1_RELEASE.revision && marker?.manifestSha256 === D1_RELEASE.manifestSha256;
+
+async function status() {
+ if (transfer || runtime || loading || ['error', 'stopped'].includes(state.status)) return { ...state, ready: !!runtime };
+ const marker = await readyMarker();
+ return marker ? { ...state, status: currentPackage(marker) ? 'cached' : 'idle', ready: false, loaded: false } : { ...state, ready: false };
+}
+
+// A decision found the model unloaded while the user's Load choice stands.
+// Only the verified cache is read; the decision itself uses the fallback.
+function reload() {
+ reloadBlocked = true;
+ const controller = new AbortController(); reloadController = controller;
+ loading = (async () => {
+ try {
+ if (!currentPackage(await readyMarker())) throw new Error('D1 is not downloaded. Download it in Decision models settings.');
+ return await load(controller.signal);
+ } catch (error) {
+ // Settings shows why D1 still answers nothing; no caller awaits a reload.
+ if (!controller.signal.aborted) publish({ status: 'error', loaded: false, error: String(error?.message || error) });
+ throw error;
+ }
+ })().finally(() => { loading = null; if (reloadController === controller) reloadController = null; });
+ loading.catch(() => {});
+}
+
+function unloaded(payload) {
+ if (!loading && !transfer && !reloadBlocked && payload?.keepLoaded === true && payload.consentVersion === D1_CONSENT_VERSION) reload();
+ return loading
+ ? codedError('D1_MODEL_LOADING', 'D1 is loading its cached model; this decision uses the existing fallback.')
+ : codedError('D1_MODEL_NOT_LOADED', 'D1 is not loaded. Download or load it explicitly in Decision models settings.');
}
function startDownload(payload) {
@@ -81,22 +128,47 @@ function startDownload(payload) {
return { started: true, ...state };
}
-async function execute(type, payload = {}) {
+async function execute(type, payload = {}, signal) {
if (type === 'probe') { const { adapter, info } = await probe(); return { success: true, adapter: info, features: [...adapter.features] }; }
if (type === 'status') return status();
if (type === 'download') return startDownload(payload);
if (type === 'stop') { transfer?.abort(new Error('D1 download stopped by user.')); return { stopped: true }; }
- if (type === 'dispose') { transfer?.abort(new Error('D1 disabled.')); await runtime?.dispose(); runtime = null; device = null; publish({ status: 'idle', loaded: false }); return { disposed: true }; }
+ if (type === 'dispose') {
+ const reason = new Error('D1 disabled.'); transfer?.abort(reason); reloadController?.abort(reason);
+ await loading?.catch(() => {});
+ await runtime?.dispose(); runtime = null; device = null; publish({ status: 'idle', loaded: false }); return { disposed: true };
+ }
if (type === 'load') { if (payload.consentVersion !== D1_CONSENT_VERSION) throw new Error('D1 consent is required.'); if (transfer) throw new Error('D1 download/load is already in progress.'); return prepare(); }
- if (type === 'clear') { if (runtime || transfer) throw new Error('Disable D1 before removing its cache.'); const directory = await openD1Cache(); for await (const [name] of directory.entries()) await directory.removeEntry(name); publish({ status: 'idle', loaded: false }); return { removed: true }; }
- if (type === 'evaluate') { if (!runtime) throw new Error('D1 is not loaded. Download or load it explicitly in Decision models settings.'); const result = await runtime.evaluate(payload); return { ...result, diagnostics: { ...result.diagnostics, adapter: state.adapter, runtime: state.runtime, model_revision: D1_RELEASE.revision, package_manifest_sha256: D1_RELEASE.manifestSha256, gpu_errors: [...errors] } }; }
+ if (type === 'clear') { if (runtime || transfer || loading) throw new Error('Disable D1 before removing its cache.'); const directory = await openD1Cache(); for await (const [name] of directory.entries()) await directory.removeEntry(name); publish({ status: 'idle', loaded: false }); return { removed: true }; }
+ if (type === 'evaluate') {
+ if (signal?.aborted) throw signal.reason;
+ if (!runtime) throw unloaded(payload);
+ const result = await runtime.evaluate({ state: payload.state, images: payload.images, questions: payload.questions, signal });
+ return { ...result, diagnostics: { ...result.diagnostics, adapter: state.adapter, runtime: state.runtime, model_revision: D1_RELEASE.revision, package_manifest_sha256: D1_RELEASE.manifestSha256, gpu_errors: [...errors] } };
+ }
throw new Error('Unknown D1 worker command.');
}
+function cancel(requestId) {
+ const controller = requests.get(requestId);
+ if (controller) { controller.abort(cancelled()); return; }
+ // The cancel overtook its request; keep a bounded record so it is skipped.
+ cancelledEarly.add(requestId);
+ if (cancelledEarly.size > 64) cancelledEarly.delete(cancelledEarly.values().next().value);
+}
+
self.onmessage = event => {
const { id, type, payload } = event.data || {};
- const run = () => execute(type, payload);
+ if (type === 'cancel') {
+ if (typeof payload?.requestId === 'string') cancel(payload.requestId);
+ self.postMessage({ id, ok: true, result: { cancelled: true } }); return;
+ }
+ const requestId = type === 'evaluate' && typeof payload?.requestId === 'string' ? payload.requestId : null;
+ const controller = requestId ? new AbortController() : null;
+ if (requestId) { if (cancelledEarly.delete(requestId)) controller.abort(cancelled()); requests.set(requestId, controller); }
+ const run = () => execute(type, payload, controller?.signal);
const immediate = ['status', 'stop', 'download', 'probe'].includes(type);
const promise = immediate ? Promise.resolve().then(run) : (queue = queue.then(run, run));
- promise.then(result => self.postMessage({ id, ok: true, result }), error => self.postMessage({ id, ok: false, error: String(error.message || error) }));
+ promise.finally(() => { if (requestId && requests.get(requestId) === controller) requests.delete(requestId); })
+ .then(result => self.postMessage({ id, ok: true, result }), error => self.postMessage({ id, ok: false, error: String(error?.message || error), ...(typeof error?.code === 'string' ? { code: error.code } : {}) }));
};
diff --git a/src/firefox/src/providers/d1.js b/src/firefox/src/providers/d1.js
index fda231d71..231c024bb 100644
--- a/src/firefox/src/providers/d1.js
+++ b/src/firefox/src/providers/d1.js
@@ -1,15 +1,22 @@
-import { D1_MODEL_ID, D1_CONSENT_KEY, D1_CONSENT_VERSION } from './d1-config.js';
-import { dispatchD1Worker, resetD1Worker } from './d1-host.js';
+import { D1_MODEL_ID, D1_CONSENT_KEY, D1_CONSENT_VERSION, D1_KEEP_LOADED_KEY } from './d1-config.js';
+import { cancelD1Request, dispatchD1Worker, resetD1Worker } from './d1-host.js';
+
+const newRequestId = () => globalThis.crypto?.randomUUID?.() || `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
export async function d1Request(command, payload = {}, { timeoutMs = 90_000, signal } = {}) {
const api = globalThis.browser || globalThis.chrome;
- const consent = await api.storage.local.get(D1_CONSENT_KEY);
- if (['evaluate', 'load', 'download'].includes(command) && consent[D1_CONSENT_KEY] !== D1_CONSENT_VERSION) throw new Error('D1 is experimental and requires explicit local-model consent.');
+ const stored = await api.storage.local.get([D1_CONSENT_KEY, D1_KEEP_LOADED_KEY]);
+ if (['evaluate', 'load', 'download'].includes(command) && stored[D1_CONSENT_KEY] !== D1_CONSENT_VERSION) throw new Error('D1 is experimental and requires explicit local-model consent.');
if (signal?.aborted) throw signal.reason;
- const abort = () => { void d1Request('reset').catch(() => {}); };
- signal?.addEventListener('abort', abort, { once: true });
+ // A caller that stops waiting (a judge timeout, Stop) cancels only its own
+ // evaluation; the loaded model stays for the next decision. The Worker's
+ // own deadline (d1-host.js) still discards a stalled session.
+ if (command === 'evaluate') payload = { ...payload, requestId: newRequestId(), consentVersion: stored[D1_CONSENT_KEY], keepLoaded: stored[D1_KEEP_LOADED_KEY] === true };
+ const abort = () => cancelD1Request(payload.requestId);
+ if (command === 'evaluate') signal?.addEventListener('abort', abort, { once: true });
try {
if (command === 'reset') { resetD1Worker(); return { reset: true }; }
+ if (command === 'cancel') { cancelD1Request(payload.requestId); return { cancelled: true }; }
const result = await dispatchD1Worker(command, payload, timeoutMs);
if (signal?.aborted) throw signal.reason; return result;
} finally { signal?.removeEventListener('abort', abort); }
diff --git a/test/d1-decision.mjs b/test/d1-decision.mjs
index b901d72e8..6a2ce2be8 100644
--- a/test/d1-decision.mjs
+++ b/test/d1-decision.mjs
@@ -108,6 +108,116 @@ for (const build of ['chrome', 'firefox']) {
globalThis.chrome = saved.chrome; globalThis.browser = saved.browser; globalThis.Worker = saved.Worker;
}
});
+ test(`${build}: a judge timeout cancels its D1 evaluation and keeps the loaded model`, async () => {
+ const saved = { chrome: globalThis.chrome, browser: globalThis.browser, Worker: globalThis.Worker };
+ const sent = [], workers = [];
+ let release = null;
+ const command = message => message.command || message.type;
+ // Chrome reaches the Worker through the offscreen host; Firefox owns it.
+ const deliver = message => { sent.push(message); if (command(message) === 'cancel') release?.(message.payload.requestId); return command(message) === 'cancel'; };
+ const cancelled = { ok: false, error: 'D1 request cancelled.', code: 'D1_REQUEST_CANCELLED' };
+ const api = { storage: { local: { get: async () => ({ [pin.D1_CONSENT_KEY]: pin.D1_CONSENT_VERSION, [pin.D1_KEEP_LOADED_KEY]: true }) } }, runtime: { getURL: path => 'chrome-extension://test/' + path } };
+ if (build === 'chrome') {
+ api.offscreen = { createDocument: async () => { throw new Error('Existing host must be reused'); }, hasDocument: async () => true };
+ api.runtime.sendMessage = async message => deliver(message) ? { ok: true, result: { cancelled: true } } : new Promise(resolve => { release = requestId => { if (requestId === message.payload.requestId) resolve(cancelled); }; });
+ }
+ globalThis.chrome = api; globalThis.browser = build === 'firefox' ? api : undefined;
+ globalThis.Worker = class {
+ constructor() { workers.push(this); this.terminated = false; }
+ postMessage(message) { if (!deliver(message)) release = requestId => { if (requestId === message.payload.requestId) queueMicrotask(() => this.onmessage({ data: { id: message.id, ...cancelled } })); }; }
+ terminate() { this.terminated = true; }
+ };
+ try {
+ const selected = config.resolveDecisionConfig({ decisionProvider: 'webgpu_d1', systemOneEnabled: true, [pin.D1_CONSENT_KEY]: pin.D1_CONSENT_VERSION });
+ const client = judge.createSystemOneJudge({ fetchImpl: () => { throw new Error('No cloud request'); }, timeoutMs: 30, maxRetries: 0 });
+ await assert.rejects(client.evaluate({ state: 'Page', questions: { q: { type: 'noul', instructions: 'Is blue visible?' } }, config: selected }), error => judge.systemOneFailureReason(error) === 'timeout');
+ await new Promise(resolve => setTimeout(resolve, 20));
+ const evaluate = sent.find(message => command(message) === 'evaluate');
+ assert.equal(typeof evaluate.payload.requestId, 'string'); assert.equal(evaluate.payload.keepLoaded, true); assert.equal(evaluate.payload.consentVersion, pin.D1_CONSENT_VERSION);
+ assert.equal(sent.find(message => command(message) === 'cancel')?.payload.requestId, evaluate.payload.requestId);
+ assert.equal(sent.some(message => command(message) === 'reset'), false, 'a timeout must not discard the loaded model');
+ assert.ok(workers.every(worker => !worker.terminated));
+ } finally {
+ const host = await import(`../src/${build}/src/providers/d1-host.js`); host.resetD1Worker();
+ globalThis.chrome = saved.chrome; globalThis.browser = saved.browser; globalThis.Worker = saved.Worker;
+ }
+ });
+ test(`${build}: the D1 host forwards Worker error codes and cancels without terminating`, async () => {
+ const saved = { chrome: globalThis.chrome, browser: globalThis.browser, Worker: globalThis.Worker };
+ const posted = [];
+ let terminated = 0;
+ globalThis.chrome = { runtime: { getURL: path => 'chrome-extension://test/' + path } }; globalThis.browser = build === 'firefox' ? globalThis.chrome : undefined;
+ globalThis.Worker = class {
+ postMessage(message) { posted.push(message); if (message.type === 'evaluate') queueMicrotask(() => this.onmessage({ data: { id: message.id, ok: false, error: 'D1 is loading its cached model.', code: 'D1_MODEL_LOADING' } })); }
+ terminate() { terminated++; }
+ };
+ const host = await import(`../src/${build}/src/providers/d1-host.js`);
+ try {
+ await assert.rejects(host.dispatchD1Worker('evaluate', { requestId: 'x' }, 1000), error => error.code === 'D1_MODEL_LOADING' && judge.systemOneFailureReason(error) === 'model_loading');
+ host.cancelD1Request('x');
+ assert.equal(posted.at(-1).type, 'cancel'); assert.equal(posted.at(-1).payload.requestId, 'x'); assert.equal(terminated, 0);
+ assert.equal(judge.systemOneFailureReason(Object.assign(new Error('x'), { code: 'D1_MODEL_NOT_LOADED' })), 'model_not_loaded');
+ assert.equal(judge.systemOneFailureReason(Object.assign(new Error('x'), { code: 'D1_REQUEST_CANCELLED' })), 'cancelled');
+ } finally {
+ host.resetD1Worker();
+ globalThis.chrome = saved.chrome; globalThis.browser = saved.browser; globalThis.Worker = saved.Worker;
+ }
+ });
+ test(`${build}: worker skips a cancelled evaluation and reloads a kept model once, answering meanwhile`, async () => {
+ const oldNavigator = Object.getOwnPropertyDescriptor(globalThis, 'navigator');
+ const oldSelf = Object.getOwnPropertyDescriptor(globalThis, 'self');
+ const messages = [];
+ let gpuReads = 0, markerReads = 0;
+ const marker = { revision: pin.D1_RELEASE.revision, manifestSha256: pin.D1_RELEASE.manifestSha256 };
+ const directory = { getFileHandle: async name => { assert.equal(name, 'ready.json'); markerReads++; return { getFile: async () => ({ text: async () => JSON.stringify(marker) }) }; } };
+ Object.defineProperty(globalThis, 'navigator', { configurable: true, value: { storage: { getDirectory: async () => ({ getDirectoryHandle: async () => directory }) }, get gpu() { gpuReads++; return undefined; } } });
+ Object.defineProperty(globalThis, 'self', { configurable: true, value: { postMessage: message => messages.push(message) } });
+ const settle = () => new Promise(resolve => setTimeout(resolve, 5));
+ const response = id => messages.find(message => message.id === id);
+ const send = data => self.onmessage({ data });
+ const evaluate = (id, extra) => send({ id, type: 'evaluate', payload: { requestId: 'r' + id, state: 'Page', questions: { q: { type: 'noul', instructions: 'Is it blue?' } }, consentVersion: pin.D1_CONSENT_VERSION, ...extra } });
+ try {
+ await import(`../src/${build}/src/providers/d1-worker.js?cancel-and-reload`);
+ // Cancelled while queued, and cancelled before its request arrived.
+ evaluate(1, { keepLoaded: true }); send({ id: 2, type: 'cancel', payload: { requestId: 'r1' } });
+ send({ id: 3, type: 'cancel', payload: { requestId: 'r4' } }); evaluate(4, { keepLoaded: true });
+ await settle();
+ assert.equal(response(1).code, 'D1_REQUEST_CANCELLED'); assert.equal(response(4).code, 'D1_REQUEST_CANCELLED'); assert.equal(response(2).ok, true);
+ assert.equal(markerReads, 0, 'a cancelled evaluation starts nothing');
+ // Without Settings' Load request (or without consent) nothing is read or loaded.
+ evaluate(5, { keepLoaded: false }); evaluate(6, { keepLoaded: true, consentVersion: 0 }); await settle();
+ assert.equal(response(5).code, 'D1_MODEL_NOT_LOADED'); assert.equal(response(6).code, 'D1_MODEL_NOT_LOADED'); assert.equal(markerReads, 0); assert.equal(gpuReads, 0);
+ // A kept model is reloaded from the verified cache; this decision falls back meanwhile.
+ evaluate(7, { keepLoaded: true }); await settle();
+ assert.equal(response(7).ok, false); assert.equal(response(7).code, 'D1_MODEL_LOADING');
+ assert.equal(markerReads, 1); assert.equal(gpuReads, 1, 'the reload reached the GPU probe');
+ assert.ok(messages.some(message => message.state?.status === 'error' && /Hardware WebGPU/.test(message.state.error)), 'Settings sees why the reload failed');
+ // A failed reload is not repeated on every decision.
+ evaluate(8, { keepLoaded: true }); await settle();
+ assert.equal(response(8).code, 'D1_MODEL_NOT_LOADED'); assert.equal(gpuReads, 1);
+ } finally {
+ if (oldNavigator) Object.defineProperty(globalThis, 'navigator', oldNavigator); else delete globalThis.navigator;
+ if (oldSelf) Object.defineProperty(globalThis, 'self', oldSelf); else delete globalThis.self;
+ }
+ });
+ test(`${build}: an aborted D1 evaluation stops between graph runs`, async () => {
+ class Tensor { constructor(type, data, dims) { Object.assign(this, { type, data, dims }); } dispose() {} }
+ const tokenizer = { bos_token_id: 1, pad_token_id: 0, convert_tokens_to_ids: () => 5, encode: text => Array.from(text, c => c.charCodeAt(0) % 200) };
+ const controller = new AbortController();
+ let runs = 0;
+ const sessions = { vision: {}, projector: {}, decision: { async run(feeds) { runs++; controller.abort(new Error('caller stopped waiting')); const batch = feeds.input_ids.dims[0]; return { probabilities: { getData: async () => Float32Array.from({ length: batch * 2 }, (_, i) => i % 2 ? .75 : .25), dispose() {} } }; } } };
+ const model = runtime.createD1Runtime({ ort: { Tensor }, tokenizer, config: { max_length: 16384, image_text_length: 896, temperatures: {} }, ratios: [], sessions, model: pin.D1_MODEL_ID });
+ const questions = Object.fromEntries(Array.from({ length: 3 }, (_, i) => [`q${i}`, { type: 'noul', instructions: `Question ${i}?` }]));
+ // Three capped rows need three batches (see the batching test above).
+ await assert.rejects(model.evaluate({ state: 'x'.repeat(9000), questions, signal: controller.signal }), /caller stopped waiting/);
+ assert.equal(runs, 1, 'the remaining batches never ran');
+ });
+ test(`${build}: Settings' Download and Load keep D1 loaded until Unload, Remove or switching it off`, async () => {
+ const background = await readFile(new URL(`../src/${build}/src/background.js`, import.meta.url), 'utf8');
+ const branch = background.slice(background.indexOf("case 'd1_control':"), background.indexOf("case 'list_decision_models':"));
+ assert.match(branch, /msg\.command === 'consent' \? \{\} : \{ \[D1_KEEP_LOADED_KEY\]: true \}/);
+ assert.match(branch, /\['reset', 'dispose', 'clear'\]\.includes\(msg\.command\)\) \{[^}]*\[D1_KEEP_LOADED_KEY\]: false/);
+ });
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);