// FULL AUDIO BEFORE A SALVAGE, for a transcription that would salvage // (operator ruling 2026-10-17, release 32 R8; the rule: lib/fullAudioFirst.ts). // // The editor's Transcribe (and `pnpm ops transcribe-one`) on a corrupt full // source whose record stores a trusted media URL first makes the full-audio // attempts that are still owed: each a managed download (`download`, the // caller's downloadOneManaged with `storedUrl: "only"` — the corrupt-source // refetch around R7's ranged reader, its audio check and its 20-minute gap), // until one comes back clean or FULL_AUDIO_ATTEMPTS are on record. Then the // transcription runs: in full after a clean download, as a salvage once the // attempts are exhausted. // // not-needed the rule does not apply (not a corrupt full source, no trusted // stored URL, a stored-URL mode of never): transcribe as before // clean a download came back clean: the audio is whole // exhausted every attempt made, none clean: the salvage may follow // // A refusal (rate limit) with attempts still owed stops here with an error that // says so — the next Transcribe tries again; nothing is hammered. A cancel // stops at once. import type { DownloadOutcomeRecord } from "../lib/downloadOutcome"; import { FULL_AUDIO_ATTEMPTS, fullAudioLine, type FullAudioState } from "../lib/fullAudioFirst"; import { fullAudioGate } from "../lib/fullAudioFirst-server"; import type { StoredUrlMode } from "../lib/storedMediaUrl"; export type FullAudioFirstResult = "not-needed" | "clean" | "exhausted"; export async function fullAudioFirst(o: { videoDir: string; handling?: string; mode?: StoredUrlMode; onLog: (line: string) => void; signal?: AbortSignal; download: () => Promise; // Tests: the gate's reading (default: the video dir's). gate?: (videoDir: string) => Promise; }): Promise { const gate = o.gate ?? ((dir: string) => fullAudioGate(dir, { ...(o.handling ? { handling: o.handling } : {}), ...(o.mode ? { mode: o.mode } : {}) })); let state = await gate(o.videoDir); if (!state) return "not-needed"; // At most the owed attempts: a download that records no stored-URL attempt // (it should not happen) cannot loop. for (let i = 0; i < FULL_AUDIO_ATTEMPTS && !state.exhausted; i++) { if (o.signal?.aborted) throw abortError(); o.onLog(`Full audio first (${fullAudioLine(state)}): downloading from the stored media URL before any salvage.`); const record = await o.download(); if (record.status.startsWith("ok")) { o.onLog("The stored media URL's download is clean: the audio is transcribed in full."); return "clean"; } if (o.signal?.aborted) throw abortError(); const next = await gate(o.videoDir); if (!next) return "not-needed"; state = next; if (record.failureClass === "rate_limit" && !state.exhausted) { throw new Error( `The stored media URL refused the download (rate limited; ${fullAudioLine(state)}). ` + `The salvage waits for the full-audio attempts: transcribe again later.`, ); } o.onLog(`The download ended ${record.status}${record.failureClass ? ` (${record.failureClass})` : ""}; ${fullAudioLine(state)}.`); } return state.exhausted ? "exhausted" : "not-needed"; } function abortError(): Error { const err = new Error("full-audio download aborted"); err.name = "AbortError"; return err; }