// A TRIAGE'S FLAGS, MARKED RIGHT OR WRONG (plans/umtool-redesign.md, R6). The // operator's `y` / `x` on a lit flag (the triage page) is kept per schedule -- // a watch's precision and the threshold it suggests come from them. // // REPORTS_ROOT/.umtool/triage/.json // { format: "umtool-triage-marks", version: 1, schedule, // marks: { "/": { verdict: "right" | "wrong", score, at, id, line } }, // updatedAt } // // Umtool-side state, never the corpus (the editor's triage runs are its own: // reports/.triage/). The lifecycle store's discipline (lib/lifecycle.mjs): ONE // writer, serialised in-process; a whole file to a temp name and a rename; a // file that does not parse is never overwritten (a write refuses with 409). // // Plain ESM: the unit test reads it the way the app does. import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const MARKS_FORMAT = "umtool-triage-marks"; export const MARKS_VERSION = 1; const ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; const KEY_RE = /^[1-9]\d{0,6}\/f[1-9]\d{0,5}$/; export class MarksError extends Error { constructor(message, status = 400) { super(message); this.status = status; } } export const marksFile = (reportsRoot, schedule) => { if (!ID_RE.test(schedule)) throw new MarksError(`not a schedule id: ${JSON.stringify(schedule)}`); return path.join(reportsRoot, ".umtool", "triage", `${schedule}.json`); }; /** A flag's key: its run and its flag id. */ export const markKey = (k, f) => `${k}/${f}`; function clean(raw, schedule) { const marks = {}; for (const [key, m] of Object.entries(raw?.marks && typeof raw.marks === "object" ? raw.marks : {})) { if (!KEY_RE.test(key) || !m || (m.verdict !== "right" && m.verdict !== "wrong")) continue; marks[key] = { verdict: m.verdict, score: Number.isFinite(m.score) ? m.score : 2, at: typeof m.at === "string" ? m.at.slice(0, 40) : "", ...(typeof m.id === "string" ? { id: m.id.slice(0, 128) } : {}), ...(typeof m.line === "string" ? { line: m.line.slice(0, 600) } : {}), }; } return { format: MARKS_FORMAT, version: MARKS_VERSION, schedule, marks, updatedAt: typeof raw?.updatedAt === "string" ? raw.updatedAt : "" }; } /** A schedule's marks (none: an empty set). A file that does not read is a 409. */ export async function readMarks(reportsRoot, schedule) { let text; try { text = await readFile(/* turbopackIgnore: true */ marksFile(reportsRoot, schedule), "utf8"); } catch (err) { if (err?.code === "ENOENT") return clean(null, schedule); if (err instanceof MarksError) throw err; throw new MarksError(`the marks of ${schedule} cannot be read: ${err?.message ?? err}`, 409); } let raw; try { raw = JSON.parse(text); } catch (err) { throw new MarksError(`.umtool/triage/${schedule}.json is not JSON: ${err.message}`, 409); } if (raw?.format !== MARKS_FORMAT) throw new MarksError(`.umtool/triage/${schedule}.json is not a ${MARKS_FORMAT} file`, 409); return clean(raw, schedule); } let chain = Promise.resolve(); function serialised(fn) { const run = chain.then(fn); chain = run.catch(() => {}); return run; } /** * Mark one flag right or wrong (`verdict` null: unmark). Answers the marks * as written. * * @param {string} reportsRoot * @param {string} schedule * @param {{ k: number, f: string, verdict: "right" | "wrong" | null, score?: number, id?: string, line?: string, now?: Date }} m * @returns {Promise>} */ export function setMark(reportsRoot, schedule, m) { return serialised(async () => { const key = markKey(m.k, m.f); if (!KEY_RE.test(key)) throw new MarksError(`not a flag: run ${JSON.stringify(m.k)}, flag ${JSON.stringify(m.f)}`); if (m.verdict !== null && m.verdict !== "right" && m.verdict !== "wrong") throw new MarksError('"verdict" is "right", "wrong" or null'); const doc = await readMarks(reportsRoot, schedule); const at = (m.now ?? new Date()).toISOString(); if (m.verdict === null) delete doc.marks[key]; else doc.marks[key] = { verdict: m.verdict, score: Number.isFinite(m.score) ? m.score : 2, at, ...(m.id ? { id: m.id } : {}), ...(m.line ? { line: m.line.slice(0, 600) } : {}) }; doc.updatedAt = at; const file = marksFile(reportsRoot, schedule); await mkdir(/* turbopackIgnore: true */ path.dirname(file), { recursive: true }); const tmp = tmpPathFor(file); try { await writeFile(/* turbopackIgnore: true */ tmp, `${JSON.stringify(doc, null, 2)}\n`); await rename(/* turbopackIgnore: true */ tmp, file); } catch (err) { await rm(/* turbopackIgnore: true */ tmp, { force: true }).catch(() => {}); throw err; } return doc; }); } /** The marks of one run, by flag id. */ export function marksOfRun(doc, k) { const out = {}; for (const [key, m] of Object.entries(doc.marks)) { const [run, f] = key.split("/"); if (Number(run) === k) out[f] = m; } return out; }