Skip to content

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

fix: await WebGPU cache removal before provider fallback

Barack Sokullu97e90f7794b6
diff --git a/src/chrome/src/offscreen/vision-inference-host.js b/src/chrome/src/offscreen/vision-inference-host.js
index 4e46332bc..380831a2e 100644
--- a/src/chrome/src/offscreen/vision-inference-host.js
+++ b/src/chrome/src/offscreen/vision-inference-host.js
@@ -451,6 +451,10 @@ async function sendTextWorkerMessage(modelId, type, payload = {}, { exclusive =
function defaultVisionWorkerTimeout(type) {
if (type === 'init') return WORKER_INITIALIZATION_TIMEOUT_MS;
if (type === 'chat') return VISION_INFERENCE_TIMEOUT_MS;
+ // Cache removal is serialized behind model work and can take longer than
+ // initialization. Keep its caller attached until the worker acknowledges
+ // completion (or fails), so provider fallback cannot miss a late deletion.
+ if (type === 'stop-text-download') return 0;
if (type === 'preload' || type === 'text-chat' || type === 'multimodal-text-chat' || type === 'download-text' || type === 'start-download-text') return 0;
return WORKER_INITIALIZATION_TIMEOUT_MS;
}
diff --git a/src/chrome/src/providers/manager.js b/src/chrome/src/providers/manager.js
index 4beed6f60..e9b794562 100644
--- a/src/chrome/src/providers/manager.js
+++ b/src/chrome/src/providers/manager.js
@@ -1808,26 +1808,27 @@ export class ProviderManager {
}
async stopWebgpuDownload(msg) {
- const result = await this._webgpuProvider().stopDownload(msg);
- // Removing the model (from Settings, Apocalypse Mode, or any other
- // surface) while WebGPU is selected would leave chats failing readiness.
- // Fall back centrally so the active selection stays usable without each
- // UI having to change the global provider (Apocalypse must not do so).
- if (this.activeProviderId === 'webgpu') {
- try {
- const currentModel = this.providers.get('webgpu')?.config?.model;
- const status = await this._webgpuProvider().downloadStatus({ model: currentModel }).catch(() => null);
- if (status && status.ready !== true) {
- this.activeProviderId = this.providers.has(WEBBRAIN_CLOUD_PROVIDER_ID)
- ? WEBBRAIN_CLOUD_PROVIDER_ID
- : [...this.providers.keys()].find((candidate) => candidate !== 'webgpu') || WEBBRAIN_CLOUD_PROVIDER_ID;
- await this.save();
+ try {
+ return await this._webgpuProvider().stopDownload(msg);
+ } finally {
+ // Revalidate after completion or failure: cleanup can remove enough
+ // files to make the model unusable before reporting an error. Keep this
+ // central so Settings and Apocalypse Mode receive the same fallback.
+ if (this.activeProviderId === 'webgpu') {
+ try {
+ const currentModel = this.providers.get('webgpu')?.config?.model;
+ const status = await this._webgpuProvider().downloadStatus({ model: currentModel }).catch(() => null);
+ if (status && status.ready !== true) {
+ this.activeProviderId = this.providers.has(WEBBRAIN_CLOUD_PROVIDER_ID)
+ ? WEBBRAIN_CLOUD_PROVIDER_ID
+ : [...this.providers.keys()].find((candidate) => candidate !== 'webgpu') || WEBBRAIN_CLOUD_PROVIDER_ID;
+ await this.save();
+ }
+ } catch {
+ // Keep the selection; chat will report the missing download.
}
- } catch {
- // Keep the selection; chat will report the missing download.
}
}
- return result;
}
/**
diff --git a/test/run.js b/test/run.js
index 01d4eebd4..68f5c4aeb 100644
--- a/test/run.js
+++ b/test/run.js
@@ -63832,6 +63832,28 @@ test('WebGPU worker follows local text-generation and WebBrain VL vision contrac
'Firefox must not package the Chromium-only bitgpu runtime');
});
+test('WebGPU removal revalidates readiness after cleanup errors without masking the error', async () => {
+ for (const ready of [true, false]) {
+ const manager = new ProviderManagerCh();
+ const removalError = new Error('Cache cleanup failed');
+ let savedActive = null;
+ manager.providers.set('webgpu', {
+ config: { model: WEBGPU_COMPASS_TINY_V2_MODEL_ID },
+ });
+ manager.providers.set('webbrain_cloud', {});
+ manager.activeProviderId = 'webgpu';
+ manager.save = async () => { savedActive = manager.activeProviderId; };
+ manager._webgpuProvider = () => ({
+ stopDownload: async () => { throw removalError; },
+ downloadStatus: async () => ({ ready }),
+ });
+ await assert.rejects(manager.stopWebgpuDownload({ model: WEBGPU_COMPASS_TINY_V2_MODEL_ID }),
+ error => error === removalError);
+ assert.equal(manager.activeProviderId, ready ? 'webgpu' : 'webbrain_cloud');
+ assert.equal(savedActive, ready ? null : 'webbrain_cloud');
+ }
+});
+
test('vision inference host enforces deadlines and recreates poisoned workers', async () => {
const source = fs.readFileSync(path.join(ROOT, 'src/chrome/src/offscreen/vision-inference-host.js'), 'utf8');
@@ -63972,6 +63994,12 @@ test('vision inference host enforces deadlines and recreates poisoned workers',
this.emit({ id, ok: true, status: 'not-downloaded', ready: false, deletedEntries: 3 });
return;
}
+ if (type === 'stop-text-download') {
+ fakeSetTimeout(() => this.emit({
+ id, ok: true, modelId: payload.modelId, status: 'not-downloaded', ready: false,
+ }), behavior.textRemovalDelay || 0);
+ return;
+ }
if (type === 'clear-cache') {
this.emit({ id, ok: true, modelId: payload.modelId, deletedEntries: 3 });
return;
@@ -64020,6 +64048,35 @@ test('vision inference host enforces deadlines and recreates poisoned workers',
return { behavior, workers, workerMessages, runtimeMessages, dispatch, advance, drain };
}
+ const removal = createHarness({ textRemovalDelay: 60_000 });
+ const manager = new ProviderManagerCh();
+ const webgpu = new WebGPUProvider({ model: WEBGPU_COMPASS_TINY_V2_MODEL_ID });
+ let removalFinished = false;
+ let savedActive = null;
+ webgpu._dispatch = async message => {
+ const response = await removal.dispatch(message);
+ removalFinished = true;
+ return response;
+ };
+ webgpu.downloadStatus = async () => ({ ready: !removalFinished });
+ manager.providers.set('webgpu', webgpu);
+ manager.providers.set('webbrain_cloud', {});
+ manager.activeProviderId = 'webgpu';
+ manager.save = async () => { savedActive = manager.activeProviderId; };
+ let stopSettled = false;
+ const slowStop = manager.stopWebgpuDownload({ model: webgpu.model }).then(
+ result => { stopSettled = true; return result; },
+ error => { stopSettled = true; throw error; },
+ );
+ await removal.drain();
+ await removal.advance(15_001);
+ assert.equal(stopSettled, false, 'slow deletion must remain attached past the initialization deadline');
+ assert.equal(removalFinished, false);
+ await removal.advance(45_000);
+ assert.equal((await slowStop).status, 'not-downloaded');
+ assert.equal(manager.activeProviderId, 'webbrain_cloud', 'late deletion must still trigger the provider fallback');
+ assert.equal(savedActive, 'webbrain_cloud', 'late deletion must persist the fallback');
+
const init = createHarness({ hangInitCount: 1 });
const hungInit = init.dispatch({ type: 'webgpu-vision-probe', model: WEBGPU_VISION_MODEL_ID });
await init.drain();