Skip to content

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

fix(webgpu): count a Compass VLD Tiny v5 file as downloaded only once its hash matched

Claudeff55f6c5b9ce
diff --git a/docs/webgpu-models.md b/docs/webgpu-models.md
index 038e9b136..81fc36b0e 100644
--- a/docs/webgpu-models.md
+++ b/docs/webgpu-models.md
@@ -201,11 +201,16 @@ Ask) and decisions. Its training data and evaluations are on the model card.
- **Download.** Files are read at a pinned commit (`COMPASS_VLD_REVISION`),
one at a time. Each is stored only once it arrived whole and is then
checked against its pinned size and SHA-256; a mismatch removes it and
- fails the download. A dropped connection or a server error retries that
- file twice. Inference reads only the cache. The ready marker does not name
- the revision, so readiness also checks that every pinned file is cached;
- after a new pin the model reads as not downloaded until Download fetches
- the new files, and a finished download removes the earlier revision's.
+ fails the download, and a match is recorded in a small entry beside the
+ file (`compassVldVerifiedUrl`). A file's own headers are written before its
+ body arrives, so only that record says it was checked: a file stored by a
+ download that stopped before its check is checked by the next Download
+ instead of being trusted or fetched again. A dropped connection or a server
+ error retries that file twice. Inference reads only the cache. The ready
+ marker does not name the revision, so readiness also checks that every
+ pinned file is cached and recorded as checked; after a new pin the model
+ reads as not downloaded until Download fetches the new files, and a
+ finished download removes the earlier revision's.
- **Checks on the published files** (RTX 5090, Chromium through Playwright):
- `audit_webgpu_onnx.py` passes for every graph: no `Scan` or `Loop`, and
decoder logits of shape `[batch, 1, 248094]`.
diff --git a/src/chrome/src/offscreen/compass-vld-runtime.js b/src/chrome/src/offscreen/compass-vld-runtime.js
index e27fdf0e1..333a8e6bc 100644
--- a/src/chrome/src/offscreen/compass-vld-runtime.js
+++ b/src/chrome/src/offscreen/compass-vld-runtime.js
@@ -87,20 +87,50 @@ export function compassVldFileUrl(modelId, path, revision = COMPASS_VLD_REVISION
return `https://huggingface.co/${modelId}/resolve/${encodeURIComponent(revision)}/${path}`;
}
+/**
+ * The Cache Storage key of the entry that records a file passed its SHA-256
+ * check, beside the file under the same revision (so removing the model or
+ * an earlier revision removes it too). Its body is the hash the file matched.
+ * A file's own headers are written before its body arrives, so they cannot
+ * say whether it was checked: a worker that stops between storing a file and
+ * checking it leaves the file unchecked.
+ */
+export function compassVldVerifiedUrl(modelId, path, revision = COMPASS_VLD_REVISION) {
+ return `${compassVldFileUrl(modelId, path, revision)}?webbrain-verified`;
+}
+
async function sha256Hex(buffer) {
const digest = await crypto.subtle.digest('SHA-256', buffer);
return [...new Uint8Array(digest)].map(byte => byte.toString(16).padStart(2, '0')).join('');
}
-/** Whether every file is in the cache at its pinned size (no hashing). */
+async function fileVerified(cache, modelId, file) {
+ const [stored, verified] = await Promise.all([
+ cache.match(compassVldFileUrl(modelId, file.path)),
+ cache.match(compassVldVerifiedUrl(modelId, file.path)),
+ ]);
+ return !!stored && !!verified && await verified.text() === file.sha256;
+}
+
+/** Whether every file is in the cache and passed its SHA-256 check (no hashing). */
export async function compassVldFilesCached(cache, modelId, dtype = COMPASS_VLD_DTYPE, files = compassVldFiles(dtype)) {
for (const file of files) {
- const response = await cache.match(compassVldFileUrl(modelId, file.path));
- if (!response || Number(response.headers.get('content-length')) !== file.bytes) return false;
+ if (!await fileVerified(cache, modelId, file)) return false;
}
return true;
}
+// Whether the file stored at `url` has its pinned size and SHA-256; one that
+// does not is removed.
+async function storedFileMatches(cache, url, file) {
+ const stored = await cache.match(url);
+ if (!stored) return false;
+ const bytes = await stored.arrayBuffer();
+ if (bytes.byteLength === file.bytes && await sha256Hex(bytes) === file.sha256) return true;
+ await cache.delete(url);
+ return false;
+}
+
// A dropped connection or a server error fails a file's transfer; it is tried
// again from the start, twice, before the download fails. A missing file, a
// hash mismatch, a pause or a stop is not retried.
@@ -113,7 +143,9 @@ const transient = error => error?.name !== 'AbortError' && (error instanceof Typ
* Fetch the missing files into `cache`, one at a time. A file is stored only
* once its whole body arrived (Cache.put is atomic), then read back and
* checked against its pinned SHA-256; a mismatch removes it and fails the
- * download. `onProgress` gets Transformers.js-shaped events.
+ * download, and a match is recorded (compassVldVerifiedUrl). A file an earlier
+ * download stored but never checked is checked now instead of fetched again.
+ * `onProgress` gets Transformers.js-shaped events.
*/
export async function downloadCompassVld({
cache, fetchImpl, modelId, dtype = COMPASS_VLD_DTYPE, files = compassVldFiles(dtype), onProgress = () => {}, isCancelled = () => false,
@@ -125,30 +157,32 @@ export async function downloadCompassVld({
for (const file of files) {
if (isCancelled()) throw cancelledError();
const url = compassVldFileUrl(modelId, file.path);
- const cached = await cache.match(url);
- if (cached && Number(cached.headers.get('content-length')) === file.bytes) {
+ const verifiedUrl = compassVldVerifiedUrl(modelId, file.path);
+ if (await fileVerified(cache, modelId, file)) {
onProgress({ status: 'done', file: file.path, loaded: file.bytes, total: file.bytes });
continue;
}
- for (let attempt = 0; ; attempt += 1) {
- try {
- await fetchIntoCache(cache, fetchImpl, url, file, onProgress);
- break;
- } catch (error) {
- if (isCancelled()) throw cancelledError();
- if (!transient(error) || attempt >= NETWORK_RETRIES) throw error;
- await cache.delete(url).catch(() => {});
- onProgress({ status: 'progress', file: file.path, loaded: 0, total: file.bytes });
- await sleep(RETRY_DELAY_MS * (attempt + 1));
- if (isCancelled()) throw cancelledError();
+ // Whatever is stored now has not been checked.
+ await cache.delete(verifiedUrl);
+ if (!await storedFileMatches(cache, url, file)) {
+ for (let attempt = 0; ; attempt += 1) {
+ try {
+ await fetchIntoCache(cache, fetchImpl, url, file, onProgress);
+ break;
+ } catch (error) {
+ if (isCancelled()) throw cancelledError();
+ if (!transient(error) || attempt >= NETWORK_RETRIES) throw error;
+ await cache.delete(url).catch(() => {});
+ onProgress({ status: 'progress', file: file.path, loaded: 0, total: file.bytes });
+ await sleep(RETRY_DELAY_MS * (attempt + 1));
+ if (isCancelled()) throw cancelledError();
+ }
+ }
+ if (!await storedFileMatches(cache, url, file)) {
+ throw new Error(`${file.path} failed its integrity check; download the model again.`);
}
}
- const stored = await cache.match(url);
- const bytes = stored ? await stored.arrayBuffer() : null;
- if (!bytes || bytes.byteLength !== file.bytes || await sha256Hex(bytes) !== file.sha256) {
- await cache.delete(url);
- throw new Error(`${file.path} failed its integrity check; download the model again.`);
- }
+ await cache.put(verifiedUrl, new Response(file.sha256, { headers: { 'content-type': 'text/plain' } }));
onProgress({ status: 'done', file: file.path, loaded: file.bytes, total: file.bytes });
}
await removeOtherCompassVldRevisions(cache, modelId);
@@ -181,8 +215,10 @@ async function fetchIntoCache(cache, fetchImpl, url, file, onProgress) {
controller.enqueue(chunk);
},
});
+ // No content-length: the pinned size is not yet known to be the stored one,
+ // and nothing reads a file's size from its headers.
await cache.put(url, new Response(response.body.pipeThrough(counter), {
- headers: { 'content-type': 'application/octet-stream', 'content-length': String(file.bytes) },
+ headers: { 'content-type': 'application/octet-stream' },
}));
}
diff --git a/src/chrome/src/offscreen/inference-worker.js b/src/chrome/src/offscreen/inference-worker.js
index 63db4d4cf..48ddb7033 100644
--- a/src/chrome/src/offscreen/inference-worker.js
+++ b/src/chrome/src/offscreen/inference-worker.js
@@ -661,8 +661,9 @@ async function isTextModelReady(modelId, dtype) {
const marker = await cache.match(textReadyMarkerUrl(modelId, dtype));
if (!marker) return false;
// The marker does not name Compass VLD Tiny v5's pinned revision. When its
- // files are not all cached (a newer pin), drop the marker so the model
- // reads as not downloaded and Download fetches the missing files.
+ // files are not all cached and checked (a newer pin, or a file stored but
+ // never checked), drop the marker so the model reads as not downloaded and
+ // Download fetches or checks them.
if (isCompassVldModel(modelId) && !await compassVldFilesCached(cache, modelId, dtype)) {
await cache.delete(textReadyMarkerUrl(modelId, dtype));
return false;
diff --git a/test/compass-vld-runtime.mjs b/test/compass-vld-runtime.mjs
index b99642115..603594301 100644
--- a/test/compass-vld-runtime.mjs
+++ b/test/compass-vld-runtime.mjs
@@ -80,14 +80,16 @@ test('the tokenizer is built from the cached files alone: the class AutoTokenize
}
});
-// Cache Storage and fetch, enough for the download path.
+// Cache Storage and fetch, enough for the download path. Like Cache Storage,
+// an entry keeps the headers it was stored with, whatever its body holds.
function memoryCache() {
const entries = new Map();
+ const headers = new Map();
return {
entries,
- async match(url) { const bytes = entries.get(url); return bytes ? new Response(bytes, { headers: { 'content-length': String(bytes.byteLength) } }) : undefined; },
- async put(url, response) { entries.set(url, new Uint8Array(await response.arrayBuffer())); },
- async delete(key) { return entries.delete(typeof key === 'string' ? key : key.url); },
+ async match(url) { const bytes = entries.get(url); return bytes ? new Response(bytes, { headers: headers.get(url) || {} }) : undefined; },
+ async put(url, response) { entries.set(url, new Uint8Array(await response.arrayBuffer())); headers.set(url, [...response.headers]); },
+ async delete(key) { const url = typeof key === 'string' ? key : key.url; headers.delete(url); return entries.delete(url); },
async keys() { return [...entries.keys()].map(url => new Request(url)); },
};
}
@@ -135,6 +137,51 @@ test('a download stores each file only after its size and SHA-256 match, and ski
fetchImpl: async () => { throw new Error('nothing is fetched after a cancel'); } }), error => error.name === 'AbortError');
});
+test('a file counts as downloaded only once its hash matched, also when the worker stopped before the check', async () => {
+ const files = [fileOf('config.json', '{"a":1}'), fileOf('onnx/model.onnx_data', 'weights'.repeat(1000))];
+ const [, weights] = files;
+ const url = runtime.compassVldFileUrl(MODEL, weights.path);
+ const verifiedUrl = runtime.compassVldVerifiedUrl(MODEL, weights.path);
+ assert.equal(verifiedUrl.startsWith(runtime.compassVldFileUrl(MODEL, '')), true, 'recorded beside the file, under its revision');
+ const fetched = [];
+ const fetchImpl = async requested => { fetched.push(requested); return new Response(files.find(file => requested.endsWith(`/${file.path}`)).body); };
+ // A transfer that completed with the wrong bytes, stored with the headers a
+ // download writes, by a worker that stopped before checking it.
+ const stopped = memoryCache();
+ await runtime.downloadCompassVld({ cache: stopped, fetchImpl, modelId: MODEL, files: [files[0]] });
+ await stopped.put(url, new Response(weights.body.slice(0, 100), { headers: { 'content-type': 'application/octet-stream', 'content-length': String(weights.bytes) } }));
+ assert.equal(await runtime.compassVldFilesCached(stopped, MODEL, undefined, files), false, 'an unchecked file is not downloaded');
+ fetched.length = 0;
+ await runtime.downloadCompassVld({ cache: stopped, fetchImpl, modelId: MODEL, files });
+ assert.deepEqual(fetched, [url], 'the next download checks it, removes it and fetches it again');
+ assert.deepEqual(stopped.entries.get(url), weights.body);
+ assert.equal(await runtime.compassVldFilesCached(stopped, MODEL, undefined, files), true);
+ assert.equal(new TextDecoder().decode(stopped.entries.get(verifiedUrl)), weights.sha256);
+
+ // The right bytes, stored but not checked: checked now, not fetched again.
+ const unchecked = memoryCache();
+ await unchecked.put(runtime.compassVldFileUrl(MODEL, files[0].path), new Response(files[0].body));
+ await unchecked.put(url, new Response(weights.body));
+ assert.equal(await runtime.compassVldFilesCached(unchecked, MODEL, undefined, files), false);
+ fetched.length = 0;
+ await runtime.downloadCompassVld({ cache: unchecked, fetchImpl, modelId: MODEL, files });
+ assert.deepEqual(fetched, []);
+ assert.equal(await runtime.compassVldFilesCached(unchecked, MODEL, undefined, files), true);
+
+ // A record whose file is gone, or that names another hash, does not count.
+ unchecked.entries.delete(url);
+ assert.equal(await runtime.compassVldFilesCached(unchecked, MODEL, undefined, files), false);
+ await unchecked.put(url, new Response(weights.body));
+ await unchecked.put(verifiedUrl, new Response('0'.repeat(64)));
+ assert.equal(await runtime.compassVldFilesCached(unchecked, MODEL, undefined, files), false);
+ // Removing an earlier revision removes its records too.
+ const earlier = runtime.compassVldVerifiedUrl(MODEL, weights.path, '0'.repeat(40));
+ unchecked.entries.set(earlier, new TextEncoder().encode(weights.sha256));
+ await runtime.downloadCompassVld({ cache: unchecked, fetchImpl, modelId: MODEL, files });
+ assert.equal(unchecked.entries.has(earlier), false);
+ assert.equal(await runtime.compassVldFilesCached(unchecked, MODEL, undefined, files), true);
+});
+
test('a dropped connection or a server error retries that file twice, and a stop ends the retries', async () => {
const files = [fileOf('config.json', '{"a":1}'), fileOf('onnx/model.onnx_data', 'weights'.repeat(1000))];
const waits = [];