Skip to content

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

fix(webgpu): invalidate readiness on legacy VL cleanup; probe active transfer across workers

Emre Sokulluf3fe46e93281
diff --git a/src/chrome/src/offscreen/bonsai-worker.js b/src/chrome/src/offscreen/bonsai-worker.js
index 8f4f174d9..3c593764f 100644
--- a/src/chrome/src/offscreen/bonsai-worker.js
+++ b/src/chrome/src/offscreen/bonsai-worker.js
@@ -891,6 +891,17 @@ self.addEventListener('message', async (event) => {
return;
}
if (type === 'text-download-status') {
+ const modelId = String(payload?.modelId || '').trim();
+ if (!modelId || payload?.probeActive === true) {
+ if (['starting', 'queued', 'downloading', 'paused', 'stopping'].includes(textDownloadState.status)) {
+ self.postMessage({ id, ok: true, ...textDownloadSnapshot() });
+ return;
+ }
+ if (!modelId) {
+ self.postMessage({ id, ok: true, ...textDownloadSnapshot() });
+ return;
+ }
+ }
self.postMessage({ id, ok: true, ...(await getTextDownloadStatus(payload?.modelId, payload?.dtype)) });
return;
}
diff --git a/src/chrome/src/offscreen/inference-worker.js b/src/chrome/src/offscreen/inference-worker.js
index 421dfa723..f5e6bb11a 100644
--- a/src/chrome/src/offscreen/inference-worker.js
+++ b/src/chrome/src/offscreen/inference-worker.js
@@ -895,7 +895,7 @@ async function downloadTextModel(payload, { onStarted } = {}) {
if (tracksDifferentTransfer) {
throw new Error(`Finish or stop the ${textDownloadState.modelId} download before downloading ${modelId}.`);
}
- await clearLegacyLfm25VlWrongPrecisionCache(modelId);
+ await clearLegacyLfm25VlWrongPrecisionCache(modelId, dtype);
if (await isTextModelReady(modelId, dtype)) {
if (payload?.requireTools === true) {
const runtime = await getDownloadedTextRuntime(modelId, dtype, device, { localFilesOnly: true });
@@ -977,7 +977,7 @@ async function downloadTextModel(payload, { onStarted } = {}) {
}
}
-async function clearLegacyLfm25VlWrongPrecisionCache(modelId) {
+async function clearLegacyLfm25VlWrongPrecisionCache(modelId, dtype) {
if (modelId !== WEBGPU_LFM25_VL_16B_MODEL_ID || typeof caches === 'undefined') return 0;
const modelPath = `/${modelId}/`;
const wrongPrecisionFile = /\/onnx\/(?:decoder|embed_images)\.onnx(?:_data(?:_\d+)?)?(?:[?#]|$)/;
@@ -992,6 +992,25 @@ async function clearLegacyLfm25VlWrongPrecisionCache(modelId) {
}
}
}
+ if (deletedEntries > 0) {
+ for (const key of readyTextModelKeys) {
+ if (key === modelId || key.startsWith(`${modelId}|`)) {
+ readyTextModelKeys.delete(key);
+ }
+ }
+ const markerUrl = dtype ? textReadyMarkerUrl(modelId, dtype) : '';
+ for (const name of await caches.keys()) {
+ if (!/transformers/i.test(name)) continue;
+ const cache = await caches.open(name);
+ if (markerUrl) await cache.delete(markerUrl);
+ for (const request of await cache.keys()) {
+ const url = safeDecodedUrl(request.url);
+ if (url.includes('/.well-known/webgpu-model-ready/') && (url.includes(modelId) || url.includes(encodeURIComponent(modelId)))) {
+ await cache.delete(request);
+ }
+ }
+ }
+ }
return deletedEntries;
}
@@ -1487,6 +1506,16 @@ self.addEventListener('message', async event => {
if (type === 'text-download-status') {
const modelId = String(payload?.modelId || '').trim();
const dtype = payload?.dtype || 'q4f16';
+ if (!modelId || payload?.probeActive === true) {
+ if (['starting', 'queued', 'downloading', 'paused', 'stopping'].includes(textDownloadState.status)) {
+ self.postMessage({ id, ok: true, ...textDownloadSnapshot() });
+ return;
+ }
+ if (!modelId) {
+ self.postMessage({ id, ok: true, ...textDownloadSnapshot() });
+ return;
+ }
+ }
self.postMessage({ id, ok: true, ...(await getTextDownloadStatus(modelId, dtype)) });
return;
}
diff --git a/src/chrome/src/offscreen/vision-inference-host.js b/src/chrome/src/offscreen/vision-inference-host.js
index 2f3c170d6..dd7b96f48 100644
--- a/src/chrome/src/offscreen/vision-inference-host.js
+++ b/src/chrome/src/offscreen/vision-inference-host.js
@@ -331,24 +331,24 @@ function isActiveTextTransfer(state) {
return TEXT_TRANSFER_STATUSES.has(String(state?.status || '').toLowerCase());
}
-async function probeExistingTextWorkerStatus(modelId) {
+async function probeExistingTextWorkerStatus(modelId = '', options = {}) {
try {
+ const payload = modelId ? { modelId, ...options } : { ...options };
if (isBitgpuTextModel(modelId)) {
if (!bonsaiWorker) return null;
- return await sendBonsaiWorkerMessage('text-download-status', { modelId });
+ return await sendBonsaiWorkerMessage('text-download-status', payload);
}
if (!visionWorker) return null;
- return await sendVisionWorkerMessage('text-download-status', { modelId });
+ return await sendVisionWorkerMessage('text-download-status', payload);
} catch {
return null;
}
}
async function findActiveTextTransfer(requestedModel) {
- const otherModel = isBitgpuTextModel(requestedModel)
- ? WEBGPU_LFM25_MODEL_ID
- : WEBGPU_BONSAI27_MODEL_ID;
- const other = await probeExistingTextWorkerStatus(otherModel);
+ const other = isBitgpuTextModel(requestedModel)
+ ? await probeExistingTextWorkerStatus('', { probeActive: true })
+ : await probeExistingTextWorkerStatus(WEBGPU_BONSAI27_MODEL_ID, { probeActive: true });
return isActiveTextTransfer(other) ? other : null;
}
diff --git a/test/run.js b/test/run.js
index 0acee847a..a57a339fa 100644
--- a/test/run.js
+++ b/test/run.js
@@ -63273,6 +63273,8 @@ test('WebGPU worker follows local text-generation and WebBrain VL vision contrac
'LiquidAI VL repos must use their shipped nested processor config and standalone chat template');
assert.match(worker, /clearLegacyLfm25VlWrongPrecisionCache[\s\S]*?wrongPrecisionFile/,
'a retry must remove FP32 files cached by the old VL 1.6B dtype mapping');
+ assert.match(worker, /clearLegacyLfm25VlWrongPrecisionCache[\s\S]*?readyTextModelKeys\.delete[\s\S]*?webgpu-model-ready/,
+ 'deleting legacy VL precision artifacts must invalidate in-memory and cached readiness markers');
assert.match(chromeTransformers, /async function loadImageProcessorConfig[\s\S]*?source\?\.image_processor/,
'the browser runtime must normalize nested Transformers v5 image processor metadata');
assert.match(chromeTransformers, /options\.chat_template_file[\s\S]*?getModelText/,
@@ -63468,6 +63470,10 @@ test('WebGPU worker follows local text-generation and WebBrain VL vision contrac
assert.match(host, /function findActiveTextTransfer/);
assert.match(host, /activeTransfer/);
assert.match(host, /probeExistingTextWorkerStatus/);
+ assert.match(host, /probeActive: true/,
+ 'cross-worker transfer checks must query active transfers independently of a hard-coded model');
+ assert.match(worker, /payload\?\.probeActive === true[\s\S]*?textDownloadSnapshot/,
+ 'text-download-status probes must return the active transfer when requested');
assert.match(host, /sendTextWorkerMessage\(message\.model, 'text-download-status'/);
assert.match(host, /'webgpu-dispose'/);
assert.match(host, /'webgpu-vision-dispose'/);