mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
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
This commit is contained in:
@@ -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"]);
|
||||
});
|
||||
|
||||
@@ -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<boolean> {
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user