// WHERE A TRIAGE RUN LIVES (umtool R6). Server-only (node:fs). // // reports/.triage///triage.json the run (./types.ts TriageFile) // reports/.triage///trace.jsonl what it did, as it did it // reports/.triage//seen.json the ids its last COMPLETED run saw // // A dot-directory of the article store: the registry lists only id-named // directories, so nothing reads a triage as an article. The owner is a // schedule's id (a series', a watch's, a living report's article); runs are // numbered 1, 2, … per owner. ONE writer: the editor's `report-draft // {"task":"triage"}` job (controller/reportDraft.ts creates the run under the // article store's lock; the job writes it to its end). umtool reads. import { mkdir, readdir } from "node:fs/promises"; import path from "node:path"; import type { Paths } from "../../../paths"; import { readJsonFile, writeJsonAtomic } from "../../../jsonFile-server"; import { withArticleStoreLock } from "../../registry"; import { TRIAGE_FORMAT, TRIAGE_SEEN_FORMAT, type TriageFile, type TriageSeen } from "./types"; export type TriagePaths = Pick; export const TRIAGE_DIRNAME = ".triage"; export const TRIAGE_FILE = "triage.json"; export const TRIAGE_TRACE_FILE = "trace.jsonl"; export const TRIAGE_SEEN_FILE = "seen.json"; // An owner is an article or schedule id. export const TRIAGE_OWNER_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; export const triageOwnerDir = (paths: TriagePaths, owner: string) => { if (!TRIAGE_OWNER_RE.test(owner)) throw new Error(`"${owner}" is not a triage owner (an article or schedule id)`); return path.join(paths.articlesDir, TRIAGE_DIRNAME, owner); }; export const triageRunDir = (paths: TriagePaths, owner: string, k: number) => path.join(triageOwnerDir(paths, owner), String(k)); export const triageFilePath = (paths: TriagePaths, owner: string, k: number) => path.join(triageRunDir(paths, owner, k), TRIAGE_FILE); export const triageTracePath = (paths: TriagePaths, owner: string, k: number) => path.join(triageRunDir(paths, owner, k), TRIAGE_TRACE_FILE); export const triageSeenPath = (paths: TriagePaths, owner: string) => path.join(triageOwnerDir(paths, owner), TRIAGE_SEEN_FILE); /** The runs an owner has, oldest first. */ export async function listTriageRuns(owner: string, paths: TriagePaths): Promise { let names: string[] = []; try { names = (await readdir(triageOwnerDir(paths, owner), { withFileTypes: true })).filter((e) => e.isDirectory()).map((e) => e.name); } catch { return []; } return names.filter((n) => /^[1-9]\d{0,6}$/.test(n)).map(Number).sort((a, b) => a - b); } /** One run's triage.json, or null (absent, or not a triage file). */ export async function readTriageFile(owner: string, k: number, paths: TriagePaths): Promise { let file: string; try { file = triageFilePath(paths, owner, k); } catch { return null; } const r = await readJsonFile(file); if (!r.ok) return null; const v = r.value as TriageFile; return v && v.format === TRIAGE_FORMAT && v.owner === owner && v.k === k ? v : null; } /** Write a run's triage.json whole (the job's; atomically). */ export async function writeTriageFile(f: TriageFile, paths: TriagePaths): Promise { await writeJsonAtomic(triageFilePath(paths, f.owner, f.k), f, { mkdir: true }); } /** A new run: the next number, its directory and its triage.json (queued), under the article store's lock. */ export async function createTriageRun(make: (k: number) => TriageFile, paths: TriagePaths, owner: string): Promise { return withArticleStoreLock(async () => { const runs = await listTriageRuns(owner, paths); const k = (runs.at(-1) ?? 0) + 1; await mkdir(triageRunDir(paths, owner, k), { recursive: true }); const f = make(k); await writeTriageFile(f, paths); return f; }, paths); } /** The ids the owner's last completed run saw, or null (none yet). */ export async function readTriageSeen(owner: string, paths: TriagePaths): Promise { let file: string; try { file = triageSeenPath(paths, owner); } catch { return null; } const r = await readJsonFile(file); if (!r.ok) return null; const v = r.value as TriageSeen; return v && v.format === TRIAGE_SEEN_FORMAT && Array.isArray(v.ids) ? v : null; } export async function writeTriageSeen(seen: TriageSeen, paths: TriagePaths): Promise { await writeJsonAtomic(triageSeenPath(paths, seen.owner), seen, { mkdir: true }); } /** An owner's runs, newest first (each that reads). */ export async function listTriageFiles(owner: string, paths: TriagePaths): Promise { const out: TriageFile[] = []; for (const k of (await listTriageRuns(owner, paths)).reverse()) { const f = await readTriageFile(owner, k, paths); if (f) out.push(f); } return out; }