From 8e384b09a9105af0d4158b8ce55c61d392ad2467 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Tue, 6 Oct 2026 23:12:25 -0700 Subject: [PATCH] fix(cli): isolate language detection temporary files and sync skill hashes --- packages/cli/src/commands/transcribe.ts | 11 ++++----- packages/cli/src/whisper/language.ts | 6 ----- packages/cli/src/whisper/manager.ts | 1 - .../src/whisper/transcribe.metadata.test.ts | 23 +++++++++++++++++-- packages/cli/src/whisper/transcribe.ts | 15 +++++++++--- skills-manifest.json | 2 +- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/packages/cli/src/commands/transcribe.ts b/packages/cli/src/commands/transcribe.ts index fae1ed7e1c..1e5ec797ac 100644 --- a/packages/cli/src/commands/transcribe.ts +++ b/packages/cli/src/commands/transcribe.ts @@ -323,18 +323,17 @@ async function transcribeAudio( const { createRenderCancellationScope, stoppedByCancelSignal } = await import("../utils/renderCancellation.js"); - // Only the engine pick reads auto as no language: the whisper model choice still needs "auto" (it drops .en). - const spoken = requestedLanguage(opts.language); + const routingLanguage = requestedLanguage(opts.language); const engine = (opts.engine ?? "auto").toLowerCase(); if (engine !== "auto" && engine !== "parakeet" && engine !== "whisper") { failWith(`Unknown --engine: ${opts.engine}. Use auto, parakeet, or whisper.`, !!opts.json); } const unsupported = sherpaUnsupportedReason(); const sherpaUsable = () => !unsupported && sherpaParakeetInstalled(); - let runner = pickRunner(engine, sherpaUsable, spoken); + let runner = pickRunner(engine, sherpaUsable, routingLanguage); if (engine === "parakeet" && runner === "whisper") { failWith( - !parakeetSpeaks(spoken) + !parakeetSpeaks(routingLanguage) ? `Parakeet does not transcribe --language ${opts.language}; it covers ${PARAKEET_LANGUAGES.split(" ").join(", ")}. Use --engine whisper.` : (unsupported ?? `Parakeet is not installed. Install it with: ${PARAKEET_INSTALL_COMMAND} (or use --engine whisper)`), @@ -398,7 +397,7 @@ async function transcribeAudio( const reason = normalizeErrorMessage(err).replace(/\.+$/, ""); const parakeetError = `Parakeet failed: ${reason}. To repair it, run: ${PARAKEET_INSTALL_COMMAND}`; if (!parakeetFallsBack(engine)) throw new Error(parakeetError); - runner = pickRunner(engine, () => false, spoken); + runner = pickRunner(engine, () => false, routingLanguage); spin?.clear(); console.error(c.warn(`${parakeetError}. Using ${runner} for this run.`)); spin?.start(`Transcribing with ${label(runner)}...`); @@ -484,7 +483,7 @@ async function transcribeAudio( if (isWhisperUnavailable(err)) { trackTranscribeUnavailable({ optional: opts.optional === true }); const install = - engine === "auto" && parakeetSpeaks(spoken) && !unsupported + engine === "auto" && parakeetSpeaks(routingLanguage) && !unsupported ? PARAKEET_INSTALL_COMMAND : undefined; if (opts.json) { diff --git a/packages/cli/src/whisper/language.ts b/packages/cli/src/whisper/language.ts index 660ce50080..393f3b9500 100644 --- a/packages/cli/src/whisper/language.ts +++ b/packages/cli/src/whisper/language.ts @@ -3,13 +3,9 @@ export const WINDOW_SECONDS = 30; /** A music or noise window comes back near p = 0.5; speech in a language comes back above 0.9. */ const CONFIDENT = 0.7; -/** `--language auto`, in any case, asks for detection, as no language does. */ export const requestedLanguage = (language?: string) => language?.trim().toLowerCase() === "auto" ? undefined : language; -/** The starts of three windows spread over the clip after `onset`, so an intro alone cannot decide; they overlap - * when the speech is short. None under 30 s of speech, or under a minute when no onset was found (a loud or long - * intro hides it too): whisper's own pick stands then. */ export function detectionWindows(seconds: number, onset: number | null): number[] { const from = onset ?? 0; const span = seconds - from; @@ -25,13 +21,11 @@ export interface LanguageVote { p: number; } -/** The language at least two confident windows agree on, else null (whisper's own pick stands). */ export function pickLanguage(votes: readonly LanguageVote[]): string | null { const sure = votes.filter((vote) => vote.p >= CONFIDENT).map((vote) => vote.language); return sure.find((language, i) => sure.indexOf(language) !== i) ?? null; } -/** A detection's language when it is sure; a wrong label is worse than none. */ export const confidentLanguage = (vote: LanguageVote | null) => vote && vote.p >= CONFIDENT ? vote.language : null; diff --git a/packages/cli/src/whisper/manager.ts b/packages/cli/src/whisper/manager.ts index 31670dec59..63bfea93dc 100644 --- a/packages/cli/src/whisper/manager.ts +++ b/packages/cli/src/whisper/manager.ts @@ -7,7 +7,6 @@ import { findFFmpeg } from "../browser/ffmpeg.js"; import { downloadFile } from "../utils/download.js"; const MODELS_DIR = join(homedir(), ".cache", "hyperframes", "whisper", "models"); -// Multilingual: an English-only default turned every other language into English text. const DEFAULT_MODEL = "small"; export type WhisperSource = "env" | "system" | "brew" | "build"; diff --git a/packages/cli/src/whisper/transcribe.metadata.test.ts b/packages/cli/src/whisper/transcribe.metadata.test.ts index fd705f0674..1a9a229088 100644 --- a/packages/cli/src/whisper/transcribe.metadata.test.ts +++ b/packages/cli/src/whisper/transcribe.metadata.test.ts @@ -1,6 +1,6 @@ -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { transcribe, type TranscribeProgress } from "./transcribe.js"; import { readWav } from "./wav.js"; @@ -367,3 +367,22 @@ it("--language auto with an English-only model detects with the multilingual one expect(result).toMatchObject({ model: "small", detectedLanguage: "es" }); expect(languagePassed()).toEqual(["es"]); }); + +it("keeps detection WAVs in a private temporary directory and removes it after a failed vote", async () => { + musicThenSpanish(); + const directories = new Map(); + const previous = native.exec.getMockImplementation()!; + native.exec.mockImplementation((command: string, args: string[]) => { + if (!args.includes("--detect-language")) return previous(command, args); + const directory = dirname(args.at(-1)!); + directories.set(directory, statSync(directory).mode & 0o777); + throw new Error("detection failed"); + }); + await transcribe(join(dir, "audio.wav"), dir, { model: "small" }); + expect(directories.size).toBe(1); + for (const [directory, permissions] of directories) { + expect(directory).not.toBe(tmpdir()); + if (process.platform !== "win32") expect(permissions).toBe(0o700); + expect(existsSync(directory)).toBe(false); + } +}); diff --git a/packages/cli/src/whisper/transcribe.ts b/packages/cli/src/whisper/transcribe.ts index fb509fc93c..fe1504c38f 100644 --- a/packages/cli/src/whisper/transcribe.ts +++ b/packages/cli/src/whisper/transcribe.ts @@ -3,7 +3,15 @@ import { execFile, execFileSync } from "node:child_process"; import { once } from "node:events"; import { createInterface } from "node:readline"; import type { Readable } from "node:stream"; -import { existsSync, readFileSync, mkdirSync, rmSync, statSync, unlinkSync } from "node:fs"; +import { + existsSync, + readFileSync, + mkdirSync, + mkdtempSync, + rmSync, + statSync, + unlinkSync, +} from "node:fs"; import { join, extname } from "node:path"; import { tmpdir } from "node:os"; import { randomUUID } from "node:crypto"; @@ -643,7 +651,8 @@ async function languageFromWindows( const starts = clip.seconds === null ? [] : detectionWindows(clip.seconds, clip.onset); if (starts.length === 0) return null; clip.onProgress?.("Detecting the language..."); - const window = tempWavPath(); + const windowDir = mkdtempSync(join(tmpdir(), "hyperframes-language-")); + const window = join(windowDir, "window.wav"); const votes: LanguageVote[] = []; try { for (const start of starts) { @@ -671,7 +680,7 @@ async function languageFromWindows( } } } finally { - rmSync(window, { force: true }); + rmSync(windowDir, { recursive: true, force: true }); } return pickLanguage(votes); } diff --git a/skills-manifest.json b/skills-manifest.json index 581b3c40e8..490a479372 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -54,7 +54,7 @@ "files": 1 }, "media-use": { - "hash": "9b98931fc057f125", + "hash": "a10025845a222ac6", "files": 107 }, "motion-graphics": {