From 5c4f5febbcd7e4493542d2d7465c0ff46af4b807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 25 Nov 2026 00:43:40 +0000 Subject: [PATCH] fix(cli): a cached whisper model is used only once its sha256 matches its pin (#8762) * fix(cli): a cached whisper model is used only once its sha256 matches its pin * fix(cli): the whisper model's verified stamp is read once and replaced atomically --- .../cli/src/whisper/manager.model.test.ts | 28 ++++++++++++++ packages/cli/src/whisper/manager.ts | 37 +++++++++++++++++-- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/whisper/manager.model.test.ts b/packages/cli/src/whisper/manager.model.test.ts index 08f2632305..8c6ee9ec1e 100644 --- a/packages/cli/src/whisper/manager.model.test.ts +++ b/packages/cli/src/whisper/manager.model.test.ts @@ -10,6 +10,7 @@ import { statSync, symlinkSync, truncateSync, + utimesSync, writeFileSync, } from "node:fs"; import { join } from "node:path"; @@ -122,3 +123,30 @@ it.skipIf(!process.getuid)( expect(folderModes[0]! & 0o022).toBe(0); }, ); + +// fails without the fix: a same-size file at a pinned model's name was used as the model, never checked. +it("downloads a pinned model again when the cached file has the right size but other bytes", async () => { + writeFileSync(tiny, ""); + truncateSync(tiny, 77_691_713); + vi.mocked(verifies).mockResolvedValueOnce(false); + + await expect(ensureModel("tiny")).resolves.toBe(tiny); + + expect(downloadFile).toHaveBeenCalledOnce(); + expect(readFileSync(tiny, "utf8")).toBe("fresh"); +}); + +it("hashes a cached pinned model once, then again only after it changes", async () => { + writeFileSync(tiny, ""); + truncateSync(tiny, 77_691_713); + + await ensureModel("tiny"); + await ensureModel("tiny"); + expect(verifies).toHaveBeenCalledTimes(1); + + utimesSync(tiny, new Date(2001, 0, 1), new Date(2001, 0, 1)); + await ensureModel("tiny"); + expect(verifies).toHaveBeenCalledTimes(2); + expect(downloadFile).not.toHaveBeenCalled(); + expect(readdirSync(modelsDir).sort()).toEqual(["ggml-tiny.bin", "ggml-tiny.bin.verified"]); +}); diff --git a/packages/cli/src/whisper/manager.ts b/packages/cli/src/whisper/manager.ts index 09266d456c..2dbc4c62bf 100644 --- a/packages/cli/src/whisper/manager.ts +++ b/packages/cli/src/whisper/manager.ts @@ -1,11 +1,12 @@ // fallow-ignore-file code-duplication complexity import { runsWithoutInstallPrompt } from "../utils/appleStubs.js"; import { execFileSync } from "node:child_process"; -import { existsSync, readdirSync, rmSync, statSync } from "node:fs"; +import { existsSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs"; import { platform } from "node:os"; import { dirname, join } from "node:path"; import { findFFmpeg } from "../browser/ffmpeg.js"; import { downloadFile } from "../utils/download.js"; +import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { stoppableExecFile } from "../utils/stoppableExecFile.js"; import { verifies, type ModelFile } from "./modelFile.js"; import { @@ -346,13 +347,18 @@ export async function ensureModel( const pin = PINNED_MODELS.get(model); ensurePrivateCacheDir(MODELS_DIR, hyperframesCacheDir()); const cachedBytes = statSync(modelPath, { throwIfNoEntry: false })?.size; - if (cachedBytes !== undefined && (!pin || cachedBytes === pin.bytes)) return modelPath; + if ( + cachedBytes !== undefined && + (!pin || (cachedBytes === pin.bytes && (await cachedCopyVerified(modelPath, pin)))) + ) + return modelPath; rmSync(modelPath, { force: true }); + rmSync(verifiedMarker(modelPath), { force: true }); options?.onProgress?.( cachedBytes === undefined ? `Downloading model ${model}...` - : `Model ${model} is incomplete; downloading it again...`, + : `Model ${model} does not match its pinned copy; downloading it again...`, ); await downloadFile(getModelUrl(model), modelPath, { signal: options?.signal, @@ -365,9 +371,34 @@ export async function ensureModel( } }, }); + if (pin) replaceFileAtomically(verifiedMarker(modelPath), verifiedStamp(modelPath, pin)); return modelPath; } +const verifiedMarker = (modelPath: string) => `${modelPath}.verified`; + +function verifiedStamp(modelPath: string, pin: ModelFile): string { + const { size, mtimeMs, ino } = statSync(modelPath); + return `${pin.sha256} ${size} ${mtimeMs} ${ino}`; +} + +const readStamp = (marker: string) => { + try { + return readFileSync(marker, "utf8"); + } catch { + return undefined; + } +}; + +async function cachedCopyVerified(modelPath: string, pin: ModelFile): Promise { + const marker = verifiedMarker(modelPath); + const stamp = verifiedStamp(modelPath, pin); + if (readStamp(marker) === stamp) return true; + if (!(await verifies(modelPath, pin))) return false; + replaceFileAtomically(marker, stamp); + return true; +} + export function hasFFmpeg(): boolean { return findFFmpeg() !== undefined; }