Skip to content

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

In-browser decisions: cancel worker work on abort, 15 s default budget, restore Settings fields

Claudee6c472c7905e
diff --git a/scripts/check-webgpu-decisions.mjs b/scripts/check-webgpu-decisions.mjs
index d68ff385a..1d2b91670 100644
--- a/scripts/check-webgpu-decisions.mjs
+++ b/scripts/check-webgpu-decisions.mjs
@@ -21,6 +21,8 @@
// --decoder-graph F install a replacement decoder graph in the cache first
// --refresh download a cached model again first, the Settings path
// that replaces a stale decoder graph by hash
+// --cancel-check after the run, cancel decisions in the offscreen worker
+// (mid-run and before dispatch) and time the next one
// --return-hidden with --prompt verify, record each option's hidden state
import fs from 'node:fs/promises';
import path from 'node:path';
@@ -182,6 +184,28 @@ try {
const promptVariant = option('--prompt', 'probe');
const returnHidden = args.includes('--return-hidden');
await run('bonsai', fixture => message('webgpu_decide_fixture', { fixture: { ...fixture, temperature, promptVariant, returnHidden } }));
+ if (args.includes('--cancel-check')) {
+ // Cancellation in the offscreen host and worker: a decision cancelled
+ // while it runs should end early and free the queue for the next one.
+ const request = requests.at(-1);
+ const base = { model: bonsaiModel, runtime: 'onnx-vl', device: 'webgpu', dtype: preset.dtype, state: request.state, images: await Promise.all(request.images.map(dataUrl)), questions: questionsOf(request), promptVariant };
+ result.engines.bonsai.cancelCheck = await harness.evaluate(async ({ base, delayMs }) => {
+ const send = message => new Promise(resolve => chrome.runtime.sendMessage(message, resolve));
+ const timed = async run => { const started = performance.now(); const reply = await run(); return { ok: reply?.ok !== false && !reply?.error, error: reply?.error || null, ms: Math.round(performance.now() - started) }; };
+ const full = await timed(() => send({ ...base, type: 'webgpu-decide', decisionId: 'cancel-check-full' }));
+ const cancelled = await timed(async () => {
+ const reply = send({ ...base, type: 'webgpu-decide', decisionId: 'cancel-check-mid' });
+ await new Promise(resolve => setTimeout(resolve, delayMs));
+ send({ type: 'webgpu-decide-cancel', decisionId: 'cancel-check-mid' });
+ return reply;
+ });
+ const next = await timed(() => send({ ...base, type: 'webgpu-decide', decisionId: 'cancel-check-next' }));
+ await send({ type: 'webgpu-decide-cancel', decisionId: 'cancel-check-early' });
+ const early = await timed(() => send({ ...base, type: 'webgpu-decide', decisionId: 'cancel-check-early' }));
+ return { delayMs, full, cancelled, next, early };
+ }, { base, delayMs: 300 });
+ console.log('cancel check', JSON.stringify(result.engines.bonsai.cancelCheck));
+ }
if (promptVariant === 'probe') {
// The decision provider path Settings uses: judge -> provider -> worker.
const started = Date.now();
diff --git a/src/chrome/src/agent/systemone-judge.js b/src/chrome/src/agent/systemone-judge.js
index a66a0d4c7..a4a009f13 100644
--- a/src/chrome/src/agent/systemone-judge.js
+++ b/src/chrome/src/agent/systemone-judge.js
@@ -18,6 +18,9 @@ export const SYSTEM_ONE_COMPLETION_THRESHOLD_KEY = 'systemOneCompletionThreshold
export const SYSTEM_ONE_RETRY_BASE_MS = 250;
export const SYSTEM_ONE_MAX_RETRIES = 2;
export const SYSTEM_ONE_TIMEOUT_MS = 5000;
+// The in-browser chat model prefills its screenshot on this GPU (about a second
+// on a desktop card) and loads on first use after a browser restart.
+export const SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS = 15000;
export const SYSTEM_ONE_MIN_THRESHOLD = 0.5;
export const SYSTEM_ONE_MAX_THRESHOLD = 0.95;
export const SYSTEM_ONE_DEFAULT_THRESHOLD = 0.7;
@@ -267,7 +270,8 @@ export function shouldDowngradeSuccess(answers, { threshold = SYSTEM_ONE_DEFAULT
export function createSystemOneJudge({
fetchImpl = globalThis.fetch,
sleep = defaultSleep,
- timeoutMs = SYSTEM_ONE_TIMEOUT_MS,
+ // Unset: each provider's own limit. A caller's budget is capped by it.
+ timeoutMs = null,
maxRetries = SYSTEM_ONE_MAX_RETRIES,
now = () => Date.now(),
} = {}) {
@@ -280,9 +284,7 @@ export function createSystemOneJudge({
validateState(state, config?.supportsVision === true);
validateQuestions(questions);
const started = now();
- // The in-browser model prefills its screenshot on this GPU (about a second
- // on a desktop card) and loads on first use after a browser restart.
- const cap = config?.provider === 'webgpu_chat' ? 15000 : 5000;
+ const cap = config?.provider === 'webgpu_chat' ? SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS : SYSTEM_ONE_TIMEOUT_MS;
const request = requestSignal(signal, Math.max(1, Math.min(cap, Number(timeoutMs) || cap)));
const retries = Math.max(0, Math.min(2, Math.floor(Number(maxRetries) || 0)));
try {
diff --git a/src/chrome/src/offscreen/inference-worker.js b/src/chrome/src/offscreen/inference-worker.js
index b403f1eb4..2db3fdb09 100644
--- a/src/chrome/src/offscreen/inference-worker.js
+++ b/src/chrome/src/offscreen/inference-worker.js
@@ -330,8 +330,9 @@ function presentToPastName(name) {
}
// Prefill the first `count` prompt tokens in slices of `sliceTokens` (one
-// pass when 0) and return the recurrent/conv/KV cache.
-async function prefillBonsai2Cache(runtime, inputs, sliceTokens, count) {
+// pass when 0) and return the recurrent/conv/KV cache. `beforeSlice` runs
+// before each slice and may throw to stop (a cancelled decision).
+async function prefillBonsai2Cache(runtime, inputs, sliceTokens, count, beforeSlice = null) {
const { model, library } = runtime;
let embeds = await model.encode_text({ input_ids: inputs.input_ids });
let attentionMask = inputs.attention_mask;
@@ -357,6 +358,7 @@ async function prefillBonsai2Cache(runtime, inputs, sliceTokens, count) {
let cache = null;
try {
for (let start = 0; start < count; start += step) {
+ beforeSlice?.();
const end = Math.min(start + step, count);
const outputs = await model.forward({
inputs_embeds: embeds.slice(null, [start, end], null),
@@ -1829,7 +1831,19 @@ function encodeFloat16Base64(values) {
// use the chunked decoder whose cache the continuations share.
const DECISION_QUESTION_MARKER = '\u2063WEBBRAIN_DECISION_QUESTION\u2063';
-async function runMultimodalDecision(payload) {
+function decisionCancelledError() {
+ const error = new Error('The in-browser decision was cancelled.');
+ error.name = 'AbortError';
+ error.code = 'webgpu_decision_cancelled';
+ return error;
+}
+
+// `isCancelled` reports a `cancel` for this request (a judge timeout or Stop).
+// It is checked before loading, between prefill slices and before every
+// continuation, so a timed-out decision frees the model queue promptly.
+async function runMultimodalDecision(payload, { isCancelled = () => false } = {}) {
+ const stopIfCancelled = () => { if (isCancelled()) throw decisionCancelledError(); };
+ stopIfCancelled();
const modelId = assertOnnxTextModel(payload?.modelId);
if (!WEBGPU_BONSAI2_VISION_MODEL_IDS.has(modelId)) {
throw new Error(`${modelId} does not support in-browser decisions.`);
@@ -1844,6 +1858,7 @@ async function runMultimodalDecision(payload) {
owner: 'text',
readiness: 'text',
});
+ stopIfCancelled();
const temperature = Number(payload?.temperature) > 0 ? Number(payload.temperature) : 1;
const messages = [
{ role: 'system', content: [{ type: 'text', text: decisionSystemPrompt(payload?.promptVariant) }] },
@@ -1885,7 +1900,7 @@ async function runMultimodalDecision(payload) {
const questionMs = [];
let suffixTokens = 0;
try {
- cache = await prefillBonsai2Cache(runtime, prefix, bonsai2PrefillSliceTokens(payload?.prefillSliceTokens), prefixLength);
+ cache = await prefillBonsai2Cache(runtime, prefix, bonsai2PrefillSliceTokens(payload?.prefillSliceTokens), prefixLength, stopIfCancelled);
const prefillMs = performance.now() - started;
// `probe` (the default) scores each option's verify continuation with the
// trained probe; `verify` returns the raw Yes margins; `readout` and
@@ -1907,6 +1922,7 @@ async function runMultimodalDecision(payload) {
for (let index = 0; index < count; index++) {
const suffix = tokenizer(`${decisionVerifyText(question, index)}${tailText}`, { add_special_tokens: false });
suffixTokens += suffix.input_ids.dims.at(-1);
+ stopIfCancelled();
const output = await bonsai2ContinuationLogits(runtime, prefix, cache, suffix.input_ids, { withHidden: true });
margins.push(output.logits[yesId] - output.logits[noId]);
if (variant === 'probe' && !output.hidden) {
@@ -1924,6 +1940,7 @@ async function runMultimodalDecision(payload) {
}
const suffix = tokenizer(`${decisionQuestionText(question)}${tailText}`, { add_special_tokens: false });
suffixTokens += suffix.input_ids.dims.at(-1);
+ stopIfCancelled();
const logits = await bonsai2ContinuationLogits(runtime, prefix, cache, suffix.input_ids);
const ids = decisionOptions(question).codes.map(codeId);
let top = -Infinity;
@@ -2234,8 +2251,22 @@ self.addEventListener('message', async event => {
return;
}
if (type === 'multimodal-decide') {
- const result = await enqueueModelOperation(() => runMultimodalDecision(payload));
- self.postMessage({ id, ok: true, ...result });
+ // Cancellable through `cancel` like vision generations, while queued or running.
+ queuedVisionGenerations.add(id);
+ try {
+ const result = await enqueueModelOperation(async () => {
+ activeVisionGenerations.set(id, {});
+ try {
+ return await runMultimodalDecision(payload, { isCancelled: () => cancelledVisionGenerations.has(id) });
+ } finally {
+ activeVisionGenerations.delete(id);
+ }
+ });
+ self.postMessage({ id, ok: true, ...result });
+ } finally {
+ queuedVisionGenerations.delete(id);
+ cancelledVisionGenerations.delete(id);
+ }
return;
}
if (type === 'multimodal-text-chat') {
diff --git a/src/chrome/src/offscreen/vision-inference-host.js b/src/chrome/src/offscreen/vision-inference-host.js
index c743bce3c..55b4ab06a 100644
--- a/src/chrome/src/offscreen/vision-inference-host.js
+++ b/src/chrome/src/offscreen/vision-inference-host.js
@@ -438,7 +438,7 @@ async function disposeOtherTextRuntime(keepRuntime) {
}
}
-async function sendTextWorkerMessage(modelId, type, payload = {}, { exclusive = false, runtime } = {}) {
+async function sendTextWorkerMessage(modelId, type, payload = {}, { exclusive = false, runtime, onDispatch } = {}) {
if (isBitgpuTextModel(modelId, runtime) || isBitgpuTextModel(payload.modelId, payload.runtime)) {
if (exclusive) await disposeOtherTextRuntime('bitgpu');
await ensureBonsaiWorker();
@@ -446,7 +446,7 @@ async function sendTextWorkerMessage(modelId, type, payload = {}, { exclusive =
}
if (exclusive) await disposeOtherTextRuntime('onnx');
await ensureVisionWorker();
- return sendVisionWorkerMessage(type, payload);
+ return sendVisionWorkerMessage(type, payload, { onDispatch });
}
function defaultVisionWorkerTimeout(type) {
@@ -460,7 +460,7 @@ function defaultVisionWorkerTimeout(type) {
return WORKER_INITIALIZATION_TIMEOUT_MS;
}
-function sendVisionWorkerMessage(type, payload = {}, { timeoutMs = defaultVisionWorkerTimeout(type) } = {}) {
+function sendVisionWorkerMessage(type, payload = {}, { timeoutMs = defaultVisionWorkerTimeout(type), onDispatch } = {}) {
const id = nextVisionRequestId++;
const worker = visionWorker;
if (!worker) return Promise.reject(new Error('Vision worker is unavailable.'));
@@ -483,6 +483,7 @@ function sendVisionWorkerMessage(type, payload = {}, { timeoutMs = defaultVision
pendingVisionRequests.set(id, { resolve, reject, timeoutId, type, worker });
try {
worker.postMessage({ id, type, payload });
+ onDispatch?.(id, worker);
} catch (error) {
if (timeoutId) clearTimeout(timeoutId);
pendingVisionRequests.delete(id);
@@ -536,9 +537,35 @@ async function ensureVisionWorker() {
}
}
+// In-browser decisions by the provider's decisionId. A judge timeout or Stop
+// sends webgpu-decide-cancel, which cancels the worker operation instead of
+// leaving it on the serialized model queue ahead of the next request.
+const activeWebgpuDecisions = new Map();
+// Cancels that arrived before their decision; dropped after a minute.
+const cancelledWebgpuDecisions = new Set();
+
+function cancelWebgpuDecisionWork(decision) {
+ if (!decision.worker || decision.worker !== visionWorker) return;
+ try {
+ decision.worker.postMessage({ id: nextVisionRequestId++, type: 'cancel', payload: { requestId: decision.requestId } });
+ } catch {}
+}
+
+function cancelWebgpuDecision(decisionId) {
+ const decision = activeWebgpuDecisions.get(decisionId);
+ if (decision) {
+ decision.cancelled = true;
+ cancelWebgpuDecisionWork(decision);
+ return;
+ }
+ cancelledWebgpuDecisions.add(decisionId);
+ setTimeout(() => cancelledWebgpuDecisions.delete(decisionId), 60_000);
+}
+
const WEBGPU_MESSAGE_TYPES = new Set([
'webgpu-chat',
'webgpu-decide',
+ 'webgpu-decide-cancel',
'webgpu-download-start',
'webgpu-download-pause',
'webgpu-download-stop',
@@ -556,6 +583,11 @@ const WEBGPU_MESSAGE_TYPES = new Set([
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (!WEBGPU_MESSAGE_TYPES.has(message?.type)) return false;
+ if (message.type === 'webgpu-decide-cancel') {
+ if (typeof message.decisionId === 'string' && message.decisionId) cancelWebgpuDecision(message.decisionId);
+ sendResponse({ ok: true });
+ return false;
+ }
(async () => {
try {
// Bonsai is GGUF/bitgpu. Never boot Transformers.js for those messages —
@@ -718,18 +750,37 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
return;
}
if (message.type === 'webgpu-decide') {
- sendResponse(await sendTextWorkerMessage(message.model, 'multimodal-decide', {
- modelId: message.model,
- device: message.device,
- dtype: message.dtype,
- state: message.state,
- images: message.images || [],
- questions: message.questions,
- temperature: message.temperature,
- promptVariant: message.promptVariant,
- returnHidden: message.returnHidden === true,
- prefillSliceTokens: message.prefillSliceTokens,
- }, { exclusive: true, runtime: message.runtime }));
+ const decisionId = typeof message.decisionId === 'string' ? message.decisionId : '';
+ if (decisionId && cancelledWebgpuDecisions.delete(decisionId)) {
+ sendResponse({ ok: false, error: 'The in-browser decision was cancelled.', code: 'webgpu_decision_cancelled' });
+ return;
+ }
+ const decision = { requestId: null, worker: null, cancelled: false };
+ if (decisionId) activeWebgpuDecisions.set(decisionId, decision);
+ try {
+ sendResponse(await sendTextWorkerMessage(message.model, 'multimodal-decide', {
+ modelId: message.model,
+ device: message.device,
+ dtype: message.dtype,
+ state: message.state,
+ images: message.images || [],
+ questions: message.questions,
+ temperature: message.temperature,
+ promptVariant: message.promptVariant,
+ returnHidden: message.returnHidden === true,
+ prefillSliceTokens: message.prefillSliceTokens,
+ }, {
+ exclusive: true,
+ runtime: message.runtime,
+ onDispatch: (requestId, worker) => {
+ decision.requestId = requestId;
+ decision.worker = worker;
+ if (decision.cancelled) cancelWebgpuDecisionWork(decision);
+ },
+ }));
+ } finally {
+ if (decisionId) activeWebgpuDecisions.delete(decisionId);
+ }
return;
}
if (message.type === 'webgpu-chat') {
diff --git a/src/chrome/src/providers/webgpu-decision.js b/src/chrome/src/providers/webgpu-decision.js
index dbf7a15d0..d8a772526 100644
--- a/src/chrome/src/providers/webgpu-decision.js
+++ b/src/chrome/src/providers/webgpu-decision.js
@@ -11,7 +11,7 @@ export async function evaluateWebgpuDecision({ state, images, questions, config,
// D1 does, never as base64 text in the prompt.
const input = splitD1State(state, images);
const provider = new WebGPUProvider({ model: WEBGPU_DECISION_MODEL_ID });
- const result = await provider.decide({ state: input.state, images: input.images, questions });
+ const result = await provider.decide({ state: input.state, images: input.images, questions, signal });
if (signal?.aborted) throw signal.reason;
return { model: result.model, provider: 'webgpu_chat', answers: result.answers, usage: result.usage };
}
diff --git a/src/chrome/src/providers/webgpu.js b/src/chrome/src/providers/webgpu.js
index d89aa7d36..4a87cbd48 100644
--- a/src/chrome/src/providers/webgpu.js
+++ b/src/chrome/src/providers/webgpu.js
@@ -477,29 +477,44 @@ export class WebGPUProvider extends WebGPUOffscreenProvider {
* Decision readout on the in-browser model: option probabilities from one
* forward pass per question, in the d1 answer shape. Bonsai 2 Vision only.
*/
- async decide({ state = '', images = [], questions, temperature, promptVariant, returnHidden = false } = {}) {
+ async decide({ state = '', images = [], questions, temperature, promptVariant, returnHidden = false, signal } = {}) {
if (webgpuModelRuntime(this.model) !== WEBGPU_RUNTIME_ONNX_VL || !WEBGPU_DECISION_MODEL_IDS.has(this.model)) {
throw new Error(`${webgpuModelDisplayName(this.model)} does not support in-browser decisions.`);
}
+ const stopIfAborted = () => { if (signal?.aborted) throw signal.reason ?? new DOMException('The in-browser decision was cancelled.', 'AbortError'); };
+ stopIfAborted();
const download = await this.downloadStatus();
if (!download.ready) {
throw new Error(`${webgpuModelDisplayName(this.model)} is not downloaded. Open Settings > Providers > WebGPU or Apocalypse Mode > WebGPU to download it first.`);
}
const prefillSliceTokens = await this._prefillSliceTokens();
- const response = await this._dispatch({
- type: 'webgpu-decide',
- model: this.model,
- runtime: WEBGPU_RUNTIME_ONNX_VL,
- device: this.device,
- dtype: this.dtype,
- state,
- images,
- questions,
- ...(Number(temperature) > 0 ? { temperature: Number(temperature) } : {}),
- ...(['readout', 'strict', 'verify', 'probe'].includes(promptVariant) ? { promptVariant } : {}),
- ...(returnHidden === true ? { returnHidden: true } : {}),
- ...(prefillSliceTokens == null ? {} : { prefillSliceTokens }),
- });
+ stopIfAborted();
+ // An abort (judge timeout, Stop) cancels the worker operation, so it does
+ // not keep the model queue busy ahead of the next request.
+ const decisionId = globalThis.crypto?.randomUUID?.() || `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
+ const cancel = () => { this._dispatch({ type: 'webgpu-decide-cancel', decisionId }).catch(() => {}); };
+ signal?.addEventListener?.('abort', cancel, { once: true });
+ let response;
+ try {
+ response = await this._dispatch({
+ type: 'webgpu-decide',
+ decisionId,
+ model: this.model,
+ runtime: WEBGPU_RUNTIME_ONNX_VL,
+ device: this.device,
+ dtype: this.dtype,
+ state,
+ images,
+ questions,
+ ...(Number(temperature) > 0 ? { temperature: Number(temperature) } : {}),
+ ...(['readout', 'strict', 'verify', 'probe'].includes(promptVariant) ? { promptVariant } : {}),
+ ...(returnHidden === true ? { returnHidden: true } : {}),
+ ...(prefillSliceTokens == null ? {} : { prefillSliceTokens }),
+ });
+ } finally {
+ signal?.removeEventListener?.('abort', cancel);
+ }
+ stopIfAborted();
if (!response || response.ok === false || response.error) {
throw new Error(`In-browser WebGPU: ${response?.error || 'no response from the inference worker'}`);
}
diff --git a/src/chrome/src/ui/settings.js b/src/chrome/src/ui/settings.js
index 2c2f812b7..3200871ea 100644
--- a/src/chrome/src/ui/settings.js
+++ b/src/chrome/src/ui/settings.js
@@ -246,11 +246,10 @@ function syncWebgpuDecisionProvider() {
const selected = decisionProviderInput?.value === 'webgpu_chat';
const note = document.getElementById('webgpu-decision-note');
if (note) note.hidden = !selected;
- if (selected) {
- decisionModelInput.value = WEBGPU_CHAT_DECISION_MODEL;
- decisionModelInput.readOnly = true;
- }
- if (systemOneApiKeyInput && selected) systemOneApiKeyInput.disabled = true;
+ if (selected) decisionModelInput.value = WEBGPU_CHAT_DECISION_MODEL;
+ // Restore both controls when another provider is chosen.
+ if (decisionModelInput) decisionModelInput.readOnly = selected;
+ if (systemOneApiKeyInput) systemOneApiKeyInput.disabled = selected;
}
const systemOneApiKeyInput = document.getElementById('system-one-api-key');
diff --git a/src/firefox/src/agent/systemone-judge.js b/src/firefox/src/agent/systemone-judge.js
index a66a0d4c7..a4a009f13 100644
--- a/src/firefox/src/agent/systemone-judge.js
+++ b/src/firefox/src/agent/systemone-judge.js
@@ -18,6 +18,9 @@ export const SYSTEM_ONE_COMPLETION_THRESHOLD_KEY = 'systemOneCompletionThreshold
export const SYSTEM_ONE_RETRY_BASE_MS = 250;
export const SYSTEM_ONE_MAX_RETRIES = 2;
export const SYSTEM_ONE_TIMEOUT_MS = 5000;
+// The in-browser chat model prefills its screenshot on this GPU (about a second
+// on a desktop card) and loads on first use after a browser restart.
+export const SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS = 15000;
export const SYSTEM_ONE_MIN_THRESHOLD = 0.5;
export const SYSTEM_ONE_MAX_THRESHOLD = 0.95;
export const SYSTEM_ONE_DEFAULT_THRESHOLD = 0.7;
@@ -267,7 +270,8 @@ export function shouldDowngradeSuccess(answers, { threshold = SYSTEM_ONE_DEFAULT
export function createSystemOneJudge({
fetchImpl = globalThis.fetch,
sleep = defaultSleep,
- timeoutMs = SYSTEM_ONE_TIMEOUT_MS,
+ // Unset: each provider's own limit. A caller's budget is capped by it.
+ timeoutMs = null,
maxRetries = SYSTEM_ONE_MAX_RETRIES,
now = () => Date.now(),
} = {}) {
@@ -280,9 +284,7 @@ export function createSystemOneJudge({
validateState(state, config?.supportsVision === true);
validateQuestions(questions);
const started = now();
- // The in-browser model prefills its screenshot on this GPU (about a second
- // on a desktop card) and loads on first use after a browser restart.
- const cap = config?.provider === 'webgpu_chat' ? 15000 : 5000;
+ const cap = config?.provider === 'webgpu_chat' ? SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS : SYSTEM_ONE_TIMEOUT_MS;
const request = requestSignal(signal, Math.max(1, Math.min(cap, Number(timeoutMs) || cap)));
const retries = Math.max(0, Math.min(2, Math.floor(Number(maxRetries) || 0)));
try {
diff --git a/test/webgpu-decisions.mjs b/test/webgpu-decisions.mjs
index 46a7c16f6..6c054894c 100644
--- a/test/webgpu-decisions.mjs
+++ b/test/webgpu-decisions.mjs
@@ -9,7 +9,9 @@ import {
normalizeDecisionRequest,
} from '../src/chrome/src/offscreen/webgpu-decisions.js';
// The worker module registers its message listener on import.
-globalThis.self ??= { addEventListener() {}, postMessage() {} };
+const workerListeners = [];
+const workerMessages = [];
+globalThis.self ??= { addEventListener(type, listener) { if (type === 'message') workerListeners.push(listener); }, postMessage(message) { workerMessages.push(message); } };
const { bonsai2ContinuationLogits } = await import('../src/chrome/src/offscreen/inference-worker.js');
const choice = { type: 'choice', instructions: 'Is the task done?', criteria: { completed: 'A final acknowledgement is visible.', not_completed: 'An error is visible.', unknown: '' } };
@@ -201,3 +203,111 @@ test('Settings offers the chat-model decision provider in Chrome only', async ()
assert.ok(decisionEnglish['st.decision.webgpu_chat'] && decisionEnglish['st.decision.webgpu_chat_note']);
assert.ok(decisionTranslations.tr['st.decision.webgpu_chat']);
});
+
+test('a cancelled decision stops in the worker before using the model', async () => {
+ assert.equal(workerListeners.length, 1);
+ const post = data => workerListeners[0]({ data });
+ const reply = id => workerMessages.find(message => message.id === id);
+ const settle = async id => { for (let i = 0; i < 50 && !reply(id); i++) await new Promise(resolve => setTimeout(resolve, 1)); return reply(id); };
+ const payload = { modelId: 'no-such/model', state: 'Page', questions: { q: { type: 'noul', instructions: 'Done?' } } };
+ // Without a cancel the request reaches the model checks.
+ const control = post({ id: 9001, type: 'multimodal-decide', payload });
+ assert.doesNotMatch((await settle(9001)).error, /cancelled/);
+ await control;
+ // A cancel that arrives while the decision is queued stops it first.
+ const decide = post({ id: 9002, type: 'multimodal-decide', payload });
+ await post({ id: 9003, type: 'cancel', payload: { requestId: 9002 } });
+ assert.equal(reply(9003).cancelled, true);
+ await decide;
+ assert.deepEqual(await settle(9002), { id: 9002, ok: false, error: 'The in-browser decision was cancelled.' });
+ const worker = await readFile(new URL('../src/chrome/src/offscreen/inference-worker.js', import.meta.url), 'utf8');
+ const run = worker.slice(worker.indexOf('async function runMultimodalDecision'), worker.indexOf('async function runText'));
+ assert.match(run, /prefixLength, stopIfCancelled\)/, 'prefill slices check for a cancel');
+ assert.equal((run.match(/stopIfCancelled\(\);\n\s*const (?:output|logits) = await bonsai2ContinuationLogits/g) || []).length, 2,
+ 'every continuation checks for a cancel');
+});
+
+test('an aborted judge request cancels the in-browser decision in the offscreen worker', async () => {
+ const saved = { chrome: globalThis.chrome };
+ const sent = [];
+ let finishDecide;
+ globalThis.chrome = {
+ offscreen: { hasDocument: async () => true, createDocument: async () => {} },
+ runtime: {
+ lastError: null,
+ getURL: path => `chrome-extension://test/${path}`,
+ sendMessage(message, callback) {
+ sent.push(message);
+ if (message.type === 'webgpu-decide') { finishDecide = () => callback({ ok: false, error: 'The in-browser decision was cancelled.' }); return; }
+ callback(message.type === 'webgpu-download-status' ? { ready: true } : { ok: true });
+ },
+ },
+ storage: { local: { get: async () => ({}) } },
+ };
+ try {
+ const { resolveDecisionConfig } = await import('../src/chrome/src/agent/decision-config.js');
+ const { createSystemOneJudge } = await import('../src/chrome/src/agent/systemone-judge.js');
+ const config = resolveDecisionConfig({ decisionProvider: 'webgpu_chat', systemOneEnabled: true });
+ const controller = new AbortController();
+ const pending = createSystemOneJudge({ fetchImpl: () => { throw new Error('no cloud request'); }, maxRetries: 0 })
+ .evaluate({ state: 'Page', questions: { q: { type: 'noul', instructions: 'Is it done?' } }, config, signal: controller.signal });
+ for (let i = 0; i < 50 && !finishDecide; i++) await new Promise(resolve => setTimeout(resolve, 1));
+ const decide = sent.find(message => message.type === 'webgpu-decide');
+ assert.ok(decide?.decisionId, 'each decision carries an id');
+ controller.abort(new Error('Stopped'));
+ await assert.rejects(pending);
+ for (let i = 0; i < 50 && !sent.some(message => message.type === 'webgpu-decide-cancel'); i++) await new Promise(resolve => setTimeout(resolve, 1));
+ const cancel = sent.find(message => message.type === 'webgpu-decide-cancel');
+ assert.equal(cancel?.decisionId, decide.decisionId, 'the abort cancels that decision');
+ finishDecide();
+ const host = await readFile(new URL('../src/chrome/src/offscreen/vision-inference-host.js', import.meta.url), 'utf8');
+ assert.match(host, /'webgpu-decide-cancel',/);
+ assert.match(host, /cancelledWebgpuDecisions\.delete\(decisionId\)/, 'a cancel before dispatch skips the decision');
+ assert.match(host, /type: 'cancel', payload: \{ requestId: decision\.requestId \}/, 'a dispatched decision is cancelled in its worker');
+ } finally {
+ if (saved.chrome === undefined) delete globalThis.chrome; else globalThis.chrome = saved.chrome;
+ }
+});
+
+test('in-browser decisions get their own timeout unless a caller sets a shorter one', async () => {
+ const saved = { chrome: globalThis.chrome, setTimeout: globalThis.setTimeout };
+ const delays = [];
+ // Record request deadlines without arming them.
+ globalThis.setTimeout = (fn, ms, ...rest) => { if (ms >= 1000) { delays.push(ms); return saved.setTimeout(() => {}, 0); } return saved.setTimeout(fn, ms, ...rest); };
+ globalThis.chrome = {
+ offscreen: { hasDocument: async () => true, createDocument: async () => {} },
+ runtime: {
+ lastError: null,
+ getURL: path => `chrome-extension://test/${path}`,
+ sendMessage(message, callback) {
+ callback(message.type === 'webgpu-download-status' ? { ready: true }
+ : message.type === 'webgpu-decide' ? { ok: true, model: message.model, answers: { q: { type: 'noul', noul: 0.8 } }, usage: { input_tokens: 900, output_tokens: 0 } } : { ok: true });
+ },
+ },
+ storage: { local: { get: async () => ({}) } },
+ };
+ try {
+ const { resolveDecisionConfig } = await import('../src/chrome/src/agent/decision-config.js');
+ const { createSystemOneJudge, SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS } = await import('../src/chrome/src/agent/systemone-judge.js');
+ const config = resolveDecisionConfig({ decisionProvider: 'webgpu_chat', systemOneEnabled: true });
+ const ask = options => createSystemOneJudge({ fetchImpl: () => { throw new Error('no cloud request'); }, maxRetries: 0, ...options })
+ .evaluate({ state: 'Page', questions: { q: { type: 'noul', instructions: 'Is it done?' } }, config });
+ for (const [options, expected] of [[{}, SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS], [{ timeoutMs: 1000 }, 1000], [{ timeoutMs: 60_000 }, SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS]]) {
+ delays.length = 0;
+ await ask(options);
+ assert.ok(delays.includes(expected), `${JSON.stringify(options)} -> ${expected}, got ${delays}`);
+ }
+ assert.equal(SYSTEM_ONE_WEBGPU_CHAT_TIMEOUT_MS, 15000);
+ } finally {
+ globalThis.setTimeout = saved.setTimeout;
+ if (saved.chrome === undefined) delete globalThis.chrome; else globalThis.chrome = saved.chrome;
+ }
+});
+
+test('Settings restores the model and key fields after leaving the chat-model provider', async () => {
+ const settings = await readFile(new URL('../src/chrome/src/ui/settings.js', import.meta.url), 'utf8');
+ const sync = settings.slice(settings.indexOf('function syncWebgpuDecisionProvider'), settings.indexOf('const systemOneApiKeyInput'));
+ assert.match(sync, /decisionModelInput\.readOnly = selected;/);
+ assert.match(sync, /systemOneApiKeyInput\.disabled = selected;/);
+ assert.doesNotMatch(sync, /= true;/, 'nothing is locked only one way');
+});