import { readFile } from "node:fs/promises"; import path from "node:path"; import { readDefinition, withFileLock as caseFileLock, writeAtomic } from "yt-dlp-transcript-common/lib/research/caseStore-server"; import { REPORTS_ROOT } from "../paths.mjs"; import { CaseError, caseDir, isCaseId, readCase, storedPath } from "./cases.mjs"; import { changePrivateNames } from "./pipeline"; // --------------------------------------------------------------------------- // LANE C's STAGE FILES (release 38: verify, leads, evidence), on lane A's // seams. The case's directory is the board's; its pipeline files are lane A's // store's (common/lib/research/caseStore-server.ts): the same cross-process // lock files (`.lock`, O_EXCL) and whole-file writes (temp + rename). // What lane C writes: // // findings.jsonl verify's changes, read and rewritten whole under the // findings lock and the privacy gate (case-findings-store.ts) // leads.json CaseLead[] (case-leads.ts) // evidence.json CaseEvidence[] (case-evidence.ts) // pipeline.json ONE thing: a never-archive lead's name as its hash, through // lane A's changePrivateNames (which also withholds it from // the candidates and batches already stored) // // A file that does not parse is never overwritten: a write refuses with 409. // --------------------------------------------------------------------------- export { CaseError }; /** The case's directory, once the case is known to exist (its case.json reads). */ export async function stageDir(caseId: string): Promise { if (!isCaseId(caseId)) throw new CaseError(`not a case id: ${JSON.stringify(caseId)}`, 400); await readCase(REPORTS_ROOT, caseId); return caseDir(REPORTS_ROOT, caseId); } export const stageFile = (dir: string, name: string) => path.join(/* turbopackIgnore: true */ dir, name); /** Run `fn` holding `file`'s lock (lane A's lock file beside it, so the CLI and the page never write at once). */ export const withFileLock = (file: string, fn: () => Promise): Promise => caseFileLock(`${file}.lock`, fn); /** Write `text` to `file` whole: a temp name, then a rename. */ export const writeWhole = (file: string, text: string) => writeAtomic(file, text); /** A JSON file's value; `fallback` when there is no file; a 409 when it does not parse. */ export async function readJsonFile(file: string, fallback: T, label: string): Promise { let text: string; try { text = await readFile(/* turbopackIgnore: true */ file, "utf8"); } catch (e) { if ((e as NodeJS.ErrnoException).code === "ENOENT") return fallback; throw new CaseError(`${label} cannot be read: ${(e as Error).message}`, 409); } try { return JSON.parse(text) as T; } catch (e) { throw new CaseError(`${label} is not JSON (${(e as Error).message}); it is never overwritten until it is fixed`, 409); } } export const labelOf = (caseId: string, name: string) => `${storedPath(caseId)}${name}`; /** The case's private-name hashes (none when it has no definition). A definition that does not read refuses. */ export async function privateHashes(caseId: string): Promise { return (await readDefinition(await stageDir(caseId), caseId))?.privateNames.sha256 ?? []; } /** The scope's channels, as the definition names them (none when it has none). */ export async function scopeChannels(caseId: string): Promise { return (await readDefinition(await stageDir(caseId), caseId))?.scope.channels ?? []; } /** Add `hash` to the case's private names (a never-archive lead), through lane A's writer. Answers whether it was new. */ export async function feedPrivateNameHash(caseId: string, hash: string): Promise { if (!/^[0-9a-f]{64}$/.test(hash)) throw new CaseError("a private name is fed as its sha256, lowercase hex", 400); return (await changePrivateNames(caseId, { add: [hash] })).changed > 0; }