// What the Speakers lane has freed since it was last started: the "freed this // run" half of the speakers readout (controller/speakersReadout.ts). // // A file, not a counter in the runner: the lane runs for weeks across editor // restarts, and a number that resets on every restart would say "freed 0 GB" // the morning after a night that freed forty. One small JSON at the corpus // root, read-modify-written under the in-process file lock; the lane runs at // concurrency 1 and the per-channel batch shares its queue, so there is one // writer at a time anyway. // // "This run" begins when the operator presses Work through speakers on a lane // that was paused or disarmed (startSpeakersRun); Pause does not reset it, so a // paused-then-resumed night is still one run. import path from "node:path"; import type { Paths } from "./paths"; import { readJsonFile, withJsonFileLock, writeJsonAtomic, } from "./jsonFile-server"; // A dot path, like the corpus root's other runtime state (.x-runs, .x-session, // .auto-queue): state, not corpus data. export const SPEAKERS_LEDGER_FILENAME = ".speakers-run.json"; export type SpeakersLedger = { version: 1; // ISO 8601: when this run began (null before the first one). startedAt: string | null; // Audio removed by clean-after-capture this run. freedBytes: number; freedVideos: number; // ISO 8601 of the last change. updatedAt: string | null; }; export function emptySpeakersLedger(): SpeakersLedger { return { version: 1, startedAt: null, freedBytes: 0, freedVideos: 0, updatedAt: null }; } export function speakersLedgerPath(paths: Pick): string { return path.join(paths.transcriptsDir, SPEAKERS_LEDGER_FILENAME); } function coerce(value: unknown): SpeakersLedger { const v = value as Partial | null; const n = (x: unknown) => (typeof x === "number" && Number.isFinite(x) && x >= 0 ? x : 0); const s = (x: unknown) => (typeof x === "string" ? x : null); if (!v || typeof v !== "object") return emptySpeakersLedger(); return { version: 1, startedAt: s(v.startedAt), freedBytes: n(v.freedBytes), freedVideos: n(v.freedVideos), updatedAt: s(v.updatedAt), }; } export async function readSpeakersLedger( paths: Pick, ): Promise { const read = await readJsonFile(speakersLedgerPath(paths)); return read.ok ? coerce(read.value) : emptySpeakersLedger(); } async function update( paths: Pick, fn: (l: SpeakersLedger) => SpeakersLedger, ): Promise { const file = speakersLedgerPath(paths); return withJsonFileLock(file, async () => { const next = fn(await readSpeakersLedger(paths)); await writeJsonAtomic(file, next, { indent: 2 }); return next; }); } // A new run: the freed tally starts again from zero. export function startSpeakersRun( paths: Pick, now: Date = new Date(), ): Promise { const at = now.toISOString(); return update(paths, () => ({ ...emptySpeakersLedger(), startedAt: at, updatedAt: at })); } // One video's audio freed by clean-after-capture. export function recordSpeakersFreed( paths: Pick, bytes: number, now: Date = new Date(), ): Promise { const at = now.toISOString(); return update(paths, (l) => ({ ...l, startedAt: l.startedAt ?? at, freedBytes: l.freedBytes + Math.max(0, bytes), freedVideos: l.freedVideos + 1, updatedAt: at, })); }