// Triage first, umtool's side (R6): a watch's triage run read with the tick // that ran it and what it escalated to; the marks (precision, the suggested // threshold, never applied); the flags opened as a board by umtool (the one // writer of case files) — idempotent, the operator's edits kept; the boards an // Inbox read makes for escalated watch ticks. Temp stores only. // // Run with: pnpm --filter umtool exec tsx --test lib/research/triage.test.ts import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import type { Paths } from "yt-dlp-transcript-common/lib/paths"; import { createSchedule, parseCreate, recordTick } from "yt-dlp-transcript-common/controller/reportSchedule"; import { createIteration, updateIteration, writeIterationFile } from "yt-dlp-transcript-common/lib/report/iterations"; import { createTriageRun } from "yt-dlp-transcript-common/lib/report/draft/triage/store"; import { readSchedule } from "yt-dlp-transcript-common/lib/report/schedule/store"; import { DEFAULT_TRIAGE_THRESHOLD, TRIAGE_FORMAT, TRIAGE_VERSION, escalationOf, tickTriageOf, type TriageFile, type TriageFlag } from "yt-dlp-transcript-common/lib/report/draft/triage/types"; let tmp: string; let paths: Paths; let lib: { triage: typeof import("./triage"); boards: typeof import("./boards") }; const FLAGS: TriageFlag[] = [ { f: "f1", id: "v2", channel: "chan-a", title: "Weak one", date: "2026-10-12", window: { start: 0, end: 20 }, line: "she mentioned the show once", at: 10, score: 2, strength: "weak", reason: "A passing mention.", checked: "cues" }, { f: "f2", id: "v1", channel: "chan-a", title: "Strong one", date: "2026-10-13", window: { start: 0, end: 30 }, line: "I was a guest on the show last week", at: 5, score: 3, strength: "strong", reason: "She says she was a guest.", checked: "cues", url: "https://x.example/?v=chan-a%2Fv1&t=5" }, ]; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-triage-")); process.env.ARTICLES_DIR = path.join(tmp, "articles"); process.env.REPORTS_DIR = path.join(tmp, "reports"); process.env.SITES_DIR = path.join(tmp, "sites"); process.env.CHANNELS_DIR = path.join(tmp, "channels"); process.env.TRANSCRIPT_LOCAL_DIR = path.join(tmp, "no-corpus"); process.env.TRANSCRIPT_HUB_URL = ""; process.env.SETTINGS_FILE = path.join(tmp, "settings.json"); paths = { articlesDir: path.join(tmp, "articles"), exportIndexDir: path.join(tmp, "idx") } as unknown as Paths; await createSchedule(parseCreate({ shape: "watch", topic: "Guest watch", channels: ["chan-a"], cadence: "weekly mon 06:00", tz: "UTC", terms: "guest" }), { paths }); const run = await createTriageRun( (k): TriageFile => ({ format: TRIAGE_FORMAT, version: TRIAGE_VERSION, owner: "guest-watch", k, status: "done", model: "qwen3:8b", provider: "local", question: "Guest watch", scope: { channels: ["chan-a"] }, terms: "guest", regex: false, topics: [], threshold: DEFAULT_TRIAGE_THRESHOLD, since: "2026-10-07", knownCount: 9, createdAt: "2026-10-14T10:00:00.000Z", startedAt: "2026-10-14T10:00:00.000Z", tier0: { candidates: [{ id: "v1", channel: "Chan A", title: "Strong one", date: "2026-10-13", matches: 1, by: ["terms"] }, { id: "v2", channel: "Chan A", title: "Weak one", date: "2026-10-12", matches: 1, by: ["terms"] }], scanned: 11, newCount: 2, cut: [], partial: false, unreached: [], perQuery: [], newBy: "id" }, tier1: { model: "qwen3:8b", flags: FLAGS, dismissed: [], read: 2, windows: 2, chunks: 2, usd: 0 }, escalation: escalationOf(FLAGS, DEFAULT_TRIAGE_THRESHOLD), cost: { tier0: 0, tier1: 0 }, summary: "2 candidates · 2 flagged · 0 dismissed — escalate", warnings: [], }), paths, "guest-watch", ); // The escalation's sweep, done, with one kept finding. const it = await createIteration("guest-watch-2026-10-14", { kind: "sweep", model: "claude-sonnet-5-5", question: "Guest watch", scope: { channels: ["chan-a"], ids: ["v1", "v2"], triage: { owner: "guest-watch", k: 1 } } }, paths); await writeIterationFile("guest-watch-2026-10-14", it.n, "report.json", { format: "archilyzer-report", version: 1, id: "guest-watch-2026-10-14", kind: "sweep", title: "Guest watch · 2026-10-14", citations: { c1: { kind: "video", channel: "chan-a", id: "v1", start: 5, end: 8, quote: FLAGS[1]!.line } }, sections: [{ id: "s1", title: "Found", body: "She [was](cite:c1)." }] }, paths); await updateIteration("guest-watch-2026-10-14", it.n, { status: "done", usage: { "claude-sonnet-5-5": { usd: 0.42, inputTokens: 1, outputTokens: 1 } } }, paths); await recordTick("guest-watch", { at: "2026-10-14T10:00:00.000Z", trigger: "now", status: "done", jobId: "j1", article: "guest-watch-2026-10-14", iteration: it.n, usd: 0.42, triage: tickTriageOf(run, { tier2Usd: 0.42 }) }, { paths }); lib = { triage: await import("./triage"), boards: await import("./boards") }; }); after(async () => { await rm(tmp, { recursive: true, force: true }); }); test("a triage run, read: the tiers, the tick that ran it, what it escalated to, the board's id (the escalation's article)", async () => { const v = await lib.triage.readTriageRunView("guest-watch", 1); assert.deepEqual([v.k, v.runs, v.schedule.shape, v.file.tier1?.flags.length], [1, [1], "watch", 2]); assert.deepEqual(v.tick && [v.tick.status, v.tick.triage?.escalated, v.tick.triage?.ids], ["done", true, ["v2", "v1"]]); assert.deepEqual(v.tier2 && [v.tier2.article, v.tier2.n, v.tier2.status, v.tier2.usd, v.tier2.findings, v.tier2.runHref], ["guest-watch-2026-10-14", 1, "done", 0.42, 1, "/research/runs/guest-watch-2026-10-14/1"]); assert.deepEqual([v.board.id, v.board.exists], ["guest-watch-2026-10-14", false]); const t = await lib.triage.readScheduleTriage((await readSchedule("guest-watch", paths))!); assert.deepEqual(t.runs.map((r) => [r.k, r.escalated, r.flagged, r.usd.tier2, r.href]), [[1, true, 2, 0.42, "/series/guest-watch/triage/1"]]); await assert.rejects(lib.triage.readTriageRunView("guest-watch", 9), (e: { status?: number }) => e.status === 404); }); test("marks: right and wrong per flag; the precision over the schedule; the suggestion (never applied)", async () => { await lib.triage.markFlag("guest-watch", 1, "f2", "right"); const out = await lib.triage.markFlag("guest-watch", 1, "f1", "wrong"); assert.deepEqual(Object.fromEntries(Object.entries(out.marks).map(([f, m]) => [f, m.verdict])), { f1: "wrong", f2: "right" }); assert.deepEqual([out.precision.marked, out.precision.precision], [2, 0.5]); assert.equal(out.suggestion.changed, false); await assert.rejects(lib.triage.markFlag("guest-watch", 1, "f9", "right"), /no flag "f9"/); const v = await lib.triage.readTriageRunView("guest-watch", 1); assert.equal(v.marks.f2!.verdict, "right"); // The schedule's own threshold is untouched. assert.deepEqual(v.schedule.threshold, DEFAULT_TRIAGE_THRESHOLD); }); test("the flags as a board, by umtool: strong before weak, by triage, the reason as the claim; a second open adds nothing; the Inbox's ensure is idempotent", async () => { const made = await lib.triage.openTriageBoard("guest-watch", 1); assert.deepEqual([made.id, made.created, made.added, made.href], ["guest-watch-2026-10-14", true, 2, "/research/guest-watch-2026-10-14"]); const doc = await lib.boards.readCase(made.id); assert.deepEqual(doc.triage && [doc.triage.schedule, doc.triage.k], ["guest-watch", 1]); assert.deepEqual(doc.sections.map((s) => s.title), ["Strong flags", "Weak flags"]); assert.deepEqual(doc.quotes.map((q) => [q.text, q.by, q.claim, q.cite.video, q.cite.t]), [ ["I was a guest on the show last week", "triage", "She says she was a guest.", "v1", 5], ["she mentioned the show once", "triage", "A passing mention.", "v2", 10], ]); await lib.boards.patchQuote(made.id, "q1", { claim: "Ours" }); const again = await lib.triage.openTriageBoard("guest-watch", 1); assert.deepEqual([again.created, again.added, again.already], [false, 0, 2]); assert.equal((await lib.boards.readCase(made.id)).quotes[0]!.claim, "Ours"); const s = (await readSchedule("guest-watch", paths))!; const ensured = await lib.triage.ensureTriageBoards([s]); assert.deepEqual([...ensured], [["guest-watch/1", "/research/guest-watch-2026-10-14"]]); });