Skip to content

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

Bound the Bonsai decoder-graph refresh so it cannot stall model loads

Claudec21f60af3a8e
diff --git a/src/chrome/src/offscreen/inference-worker.js b/src/chrome/src/offscreen/inference-worker.js
index 714405aad..0d47c2c43 100644
--- a/src/chrome/src/offscreen/inference-worker.js
+++ b/src/chrome/src/offscreen/inference-worker.js
@@ -58,6 +58,9 @@ const BONSAI2_DECODER_GRAPH_SHA256 = new Map([
['webbrain-one/Ternary-Bonsai-2-2B-ONNX-WebGPU-Vision', '2768d880f4fdab46a984d697b901db6e8ac54dc9b896524cb8cb769bbb339df0'],
['webbrain-one/Ternary-Bonsai-2-4B-ONNX-WebGPU-Vision', '52424fbb7094ed0a1036d8795e78c3da349fce1838ee6bd077523929b0bb7bb2'],
]);
+// A refresh must never stall model loading on a flaky network; one attempt per
+// worker lifetime, after which the working cached graph is used.
+const BONSAI2_DECODER_GRAPH_REFRESH_TIMEOUT_MS = 15_000;
const checkedBonsai2DecoderGraphs = new Set();
// The Qwen3.5 generation_config sidecar omitted by Danyal's split exports.
// Per-request decoding still controls do_sample and the output token budget.
@@ -229,12 +232,16 @@ async function sha256Hex(buffer) {
* Best effort: offline or unexpected responses keep the cached graph, which
* still produces the same answers, only with slower prefill.
*/
-export async function refreshBonsai2DecoderGraph(modelId, { expected = BONSAI2_DECODER_GRAPH_SHA256.get(modelId) } = {}) {
+export async function refreshBonsai2DecoderGraph(modelId, {
+ expected = BONSAI2_DECODER_GRAPH_SHA256.get(modelId),
+ timeoutMs = BONSAI2_DECODER_GRAPH_REFRESH_TIMEOUT_MS,
+} = {}) {
if (!expected || checkedBonsai2DecoderGraphs.has(modelId)) return 'skipped';
if (typeof caches === 'undefined' || !globalThis.crypto?.subtle) return 'unavailable';
const url = `https://huggingface.co/${modelId}/resolve/main/${BONSAI2_DECODER_GRAPH_FILE}`;
+ let cache;
try {
- const cache = await caches.open(TRANSFORMERS_CACHE_NAME);
+ cache = await caches.open(TRANSFORMERS_CACHE_NAME);
const cached = await cache.match(url);
if (!cached) {
// Not downloaded yet: the regular download fetches the current graph.
@@ -245,19 +252,27 @@ export async function refreshBonsai2DecoderGraph(modelId, { expected = BONSAI2_D
checkedBonsai2DecoderGraphs.add(modelId);
return 'current';
}
- const fetcher = typeof globalThis.fetch === 'function' ? globalThis.fetch.bind(globalThis) : nativeFetch;
- if (!fetcher) return 'unavailable';
- const response = await fetcher(url, { cache: 'no-store' });
+ } catch {
+ return 'unavailable';
+ }
+ const fetcher = typeof globalThis.fetch === 'function' ? globalThis.fetch.bind(globalThis) : nativeFetch;
+ if (!fetcher) return 'unavailable';
+ checkedBonsai2DecoderGraphs.add(modelId);
+ const controller = new AbortController();
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
+ try {
+ const response = await fetcher(url, { cache: 'no-store', signal: controller.signal });
if (!response?.ok) return 'unavailable';
const bytes = await response.arrayBuffer();
if (await sha256Hex(bytes) !== expected) return 'unexpected';
await cache.put(url, new Response(bytes, {
headers: { 'content-type': 'application/octet-stream', 'content-length': String(bytes.byteLength) },
}));
- checkedBonsai2DecoderGraphs.add(modelId);
return 'updated';
} catch {
return 'unavailable';
+ } finally {
+ clearTimeout(timer);
}
}
diff --git a/test/run.js b/test/run.js
index 360aa31fa..fa0c8c972 100644
--- a/test/run.js
+++ b/test/run.js
@@ -67590,11 +67590,29 @@ test('Bonsai 2 Vision worker replaces only a stale cached decoder graph', async
globalThis.fetch = async () => new Response('tampered graph');
assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'unexpected');
assert.equal(entries.get(otherUrl), 'scan graph', 'an unverified response must not replace the cached graph');
+ assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'skipped',
+ 'a failed refresh is attempted once per worker lifetime, not on every runtime load');
+
+ const fresh = async tag => import(`${pathToFileURL(path.join(ROOT, 'src/chrome/src/offscreen/inference-worker.js')).href}?bonsai2-graph-refresh-${tag}`);
+ const offline = await fresh('offline');
globalThis.fetch = async () => { throw new TypeError('Failed to fetch'); };
- assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'unavailable');
+ assert.equal(await offline.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'unavailable');
assert.equal(entries.get(otherUrl), 'scan graph', 'offline loads keep the working cached graph');
+
+ const stalled = await fresh('stalled');
+ let aborted = false;
+ globalThis.fetch = (_url, init) => new Promise((_resolve, reject) => {
+ init.signal.addEventListener('abort', () => { aborted = true; reject(new DOMException('Aborted', 'AbortError')); });
+ });
+ const started = Date.now();
+ assert.equal(await stalled.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph'), timeoutMs: 50 }), 'unavailable');
+ assert.equal(aborted, true, 'a stalled refresh must be aborted');
+ assert.ok(Date.now() - started < 2000, 'a stalled refresh must not hold up model loading');
+ assert.equal(entries.get(otherUrl), 'scan graph');
+
+ const current = await fresh('current');
entries.set(otherUrl, 'chunked graph');
- assert.equal(await worker.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'current');
+ assert.equal(await current.refreshBonsai2DecoderGraph(other, { expected: sha('chunked graph') }), 'current');
} finally {
if (previousSelf === undefined) delete globalThis.self;
else globalThis.self = previousSelf;