Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Keep a WebGPU cancel that arrives while the chat is still being routed
Claude319da002ae11
diff --git a/docs/architecture.md b/docs/architecture.mdindex 7b54f3f27..85cfec3a6 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -529,7 +529,7 @@ of passing a cut-off translation off as complete. Each call carries a job id, anthat is closed, retried or retargeted sends `WB_SELECTION_SHORTCUT_INLINE_CANCEL` so thebackground aborts the paid request it no longer needs (for the in-browser WebGPU models theprovider also sends `webgpu-chat-cancel`, and the offscreen worker interrupts the running-generation or drops a queued one; those workers report `outputTruncated` when an answer used+generation or drops a queued one, and the offscreen host remembers a cancel that arrives while the request is still being routed to a worker, so the generation never starts; those workers report `outputTruncated` when an answer usedits token budget, since they have no finish reason); the card also claims its request idbefore it waits for the language bundle, so a double click starts only one call. Selectionsover 4,000 characters skip the card and use the side-panel flow. The card draws the answer as DOMdiff --git a/src/chrome/src/offscreen/vision-inference-host.js b/src/chrome/src/offscreen/vision-inference-host.jsindex b94e3ce79..eb54b7ea4 100644--- a/src/chrome/src/offscreen/vision-inference-host.js+++ b/src/chrome/src/offscreen/vision-inference-host.js@@ -438,14 +438,35 @@ async function disposeOtherTextRuntime(keepRuntime) {}}+// A chat is not in any worker's table until the worker receives it, and getting+// it there can take a while (starting a worker, unloading the other runtime). A+// cancel that arrives in that window is remembered here and checked right before+// the request is handed over, so an abandoned card never starts a generation.+const routingChatKeys = new Set();+const cancelledChatKeys = new Set();+const CHAT_REQUEST_KEY_PATTERN = /^[a-z0-9-]{8,64}$/i;++function throwIfChatCancelled(requestKey) {+ if (!requestKey || !cancelledChatKeys.has(requestKey)) return;+ const error = new Error('The generation was cancelled.');+ error.name = 'AbortError';+ error.code = 'webgpu_chat_cancelled';+ throw error;+}+async function sendTextWorkerMessage(modelId, type, payload = {}, { exclusive = false, runtime } = {}) {+ const requestKey = typeof payload.requestKey === 'string' ? payload.requestKey : '';if (isBitgpuTextModel(modelId, runtime) || isBitgpuTextModel(payload.modelId, payload.runtime)) {if (exclusive) await disposeOtherTextRuntime('bitgpu');+ throwIfChatCancelled(requestKey);await ensureBonsaiWorker();+ throwIfChatCancelled(requestKey);return sendBonsaiWorkerMessage(type, payload);}if (exclusive) await disposeOtherTextRuntime('onnx');+ throwIfChatCancelled(requestKey);await ensureVisionWorker();+ throwIfChatCancelled(requestKey);return sendVisionWorkerMessage(type, payload);}@@ -556,11 +577,23 @@ const WEBGPU_MESSAGE_TYPES = new Set([chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {if (!WEBGPU_MESSAGE_TYPES.has(message?.type)) return false;+ // Registered before anything is awaited, so a cancel that follows can always find the chat.+ const chatKey = message.type === 'webgpu-chat' && CHAT_REQUEST_KEY_PATTERN.test(message.requestKey || '')+ ? message.requestKey+ : '';+ if (chatKey) routingChatKeys.add(chatKey);+ // A cancel is recorded synchronously for the same reason: it must not queue behind a+ // worker start-up, and a chat still being routed has no worker-side request to stop yet.+ const cancelKey = message.type === 'webgpu-chat-cancel' && typeof message.requestKey === 'string'+ ? message.requestKey+ : '';+ if (cancelKey && routingChatKeys.has(cancelKey)) cancelledChatKeys.add(cancelKey);(async () => {try {// Bonsai is GGUF/bitgpu. Never boot Transformers.js for those messages —// that runtime always fetches config.json, which the GGUF repo does not have.- if (!isBitgpuTextModel(message.model, message.runtime)) {+ // A cancel only talks to workers that already exist, so it never starts one either.+ if (message.type !== 'webgpu-chat-cancel' && !isBitgpuTextModel(message.model, message.runtime)) {await ensureVisionWorker();}if (message.type === 'webgpu-vision-preload') {@@ -759,6 +792,11 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {code: error?.code || 'webgpu_request_failed',recovery: error?.code === 'vision_inference_timeout' ? 'cancellation_requested' : undefined,});+ } finally {+ if (chatKey) {+ routingChatKeys.delete(chatKey);+ cancelledChatKeys.delete(chatKey);+ }}})();return true;diff --git a/test/run.js b/test/run.jsindex afb282827..9c8e478f4 100644--- a/test/run.js+++ b/test/run.js@@ -67048,7 +67048,7 @@ test('WebGPU worker follows local text-generation and WebBrain VL vision contrac'concurrent WebGPU model starts are not serialized around the cross-runtime transfer check');assert.match(host, /message\.type === 'webgpu-download-start'[\s\S]*?startExclusiveTextDownload\(message\)/);assert.match(host, /isBitgpuTextModel\(message\.model, message\.runtime\)/);- assert.match(host, /if \(!isBitgpuTextModel\(message\.model, message\.runtime\)\) \{\s*await ensureVisionWorker\(\);/);+ assert.match(host, /if \(message\.type !== 'webgpu-chat-cancel' && !isBitgpuTextModel\(message\.model, message\.runtime\)\) \{\s*await ensureVisionWorker\(\);/);assert.doesNotMatch(host, /sendVisionWorkerMessage\('download-text'[\s\S]*?\.catch\(\(\) => \{\}\)/);assert.match(host, /'webgpu-download-pause'/);assert.match(host, /'webgpu-download-stop'/);@@ -67422,10 +67422,16 @@ test('vision inference host enforces deadlines and recreates poisoned workers',const workers = [];const workerMessages = [];const runtimeMessages = [];+ const heldInits = [];const drain = async () => {for (let pass = 0; pass < 12; pass++) await Promise.resolve();};+ const releaseInit = async () => {+ behavior.holdInit = false;+ while (heldInits.length) heldInits.shift()();+ await drain();+ };const fakeSetTimeout = (callback, delay = 0, ...args) => {const id = nextTimerId++;timers.set(id, { at: now + Math.max(0, Number(delay) || 0), callback, args });@@ -67479,6 +67485,10 @@ test('vision inference host enforces deadlines and recreates poisoned workers',behavior.hangInitCount--;return;}+ if (behavior.holdInit) {+ heldInits.push(() => this.emit({ id, ok: true }));+ return;+ }this.emit({ id, ok: true });return;}@@ -67601,7 +67611,7 @@ test('vision inference host enforces deadlines and recreates poisoned workers',const dispatch = (message) => new Promise((resolve) => {assert.equal(listener(message, {}, resolve), true);});- return { behavior, workers, workerMessages, runtimeMessages, dispatch, advance, drain };+ return { behavior, workers, workerMessages, runtimeMessages, dispatch, advance, drain, releaseInit };}const removal = createHarness({ textRemovalDelay: 60_000 });@@ -67782,6 +67792,78 @@ test('vision inference host enforces deadlines and recreates poisoned workers',assert.ok(queuedStop.workerMessages.some(({ message }) => (message.type === 'stop-vision-download' && message.payload?.targetsQueued === true)), 'queued Stop must preserve the worker fast path after pause dequeues the preload');++ // A cancel that lands while the chat is still being routed (worker booting) must stick:+ // the worker has no request to stop yet, so the host remembers it and never hands the chat over.+ const earlyCancel = createHarness({ holdInit: true });+ const earlyChat = earlyCancel.dispatch({+ type: 'webgpu-chat',+ model: WEBGPU_COMPASS_TINY_V2_MODEL_ID,+ requestKey: 'card-routing-0001',+ messages: [{ role: 'user', content: 'Translate this' }],+ });+ await earlyCancel.drain();+ assert.equal(earlyCancel.workerMessages.some(({ message }) => message.type === 'text-chat'), false,+ 'the chat must still be waiting for the worker to initialize');+ const earlyCancelAck = await earlyCancel.dispatch({ type: 'webgpu-chat-cancel', requestKey: 'card-routing-0001' });+ assert.equal(earlyCancelAck.ok, true);+ await earlyCancel.releaseInit();+ const earlyResult = await earlyChat;+ assert.equal(earlyResult.ok, false, 'a chat cancelled while routing must fail instead of generating');+ assert.match(earlyResult.error, /cancel/i);+ assert.equal(earlyCancel.workerMessages.some(({ message }) => message.type === 'text-chat'), false,+ 'a chat cancelled while routing must never reach the worker');++ // A cancel only addresses workers that already exist; it must never boot one.+ const idleCancel = createHarness();+ const idleAck = await idleCancel.dispatch({ type: 'webgpu-chat-cancel', requestKey: 'card-idle-0001' });+ assert.equal(idleAck.ok, true);+ assert.equal(idleCancel.workers.length, 0, 'a cancel must not start a worker just to cancel nothing');++ // A cancel for one card must not affect another card routed at the same time.+ const siblings = createHarness({ holdInit: true });+ const cancelledSibling = siblings.dispatch({+ type: 'webgpu-chat',+ model: WEBGPU_COMPASS_TINY_V2_MODEL_ID,+ requestKey: 'card-routing-0002',+ messages: [{ role: 'user', content: 'one' }],+ });+ const liveSibling = siblings.dispatch({+ type: 'webgpu-chat',+ model: WEBGPU_COMPASS_TINY_V2_MODEL_ID,+ requestKey: 'card-routing-0003',+ messages: [{ role: 'user', content: 'two' }],+ });+ await siblings.drain();+ await siblings.dispatch({ type: 'webgpu-chat-cancel', requestKey: 'card-routing-0002' });+ await siblings.releaseInit();+ assert.equal((await cancelledSibling).ok, false);+ await siblings.drain();+ const posted = siblings.workerMessages.filter(({ message }) => message.type === 'text-chat');+ assert.ok(posted.every(({ message }) => message.payload.requestKey === 'card-routing-0003'),+ 'only the card that was not cancelled may reach the worker');+ assert.equal(posted.length, 1);+ assert.ok(liveSibling, 'the other card keeps its pending request');++ // Control: an uncancelled chat is dispatched with its key, and a later cancel reaches the worker.+ const control = createHarness({ holdInit: true });+ const controlChat = control.dispatch({+ type: 'webgpu-chat',+ model: WEBGPU_COMPASS_TINY_V2_MODEL_ID,+ requestKey: 'card-routing-0004',+ messages: [{ role: 'user', content: 'three' }],+ });+ await control.drain();+ await control.releaseInit();+ await control.drain();+ const controlPosted = control.workerMessages.find(({ message }) => message.type === 'text-chat');+ assert.equal(controlPosted?.message.payload.requestKey, 'card-routing-0004');+ await controlChat;+ await control.dispatch({ type: 'webgpu-chat-cancel', requestKey: 'card-routing-0004' });+ assert.ok(control.workerMessages.some(({ message }) => (+ message.type === 'cancel-text' && message.payload.requestKey === 'card-routing-0004'+ )), 'a cancel after dispatch must still be forwarded to the worker');+ assert.equal(control.workerMessages.filter(({ message }) => message.type === 'text-chat').length, 1);});test('Vision Model removal deletes only its cache entries', async () => {