// Transparent runs (R3): a trace projected into steps (scope, turns with // their enumerations and batches, the findings, the quote checks kept, // refused and dropped with why, the end), a turns-only run projected the same // way with what it never recorded as a gap, several run starts as separate // runs never summed, and a run's findings opened as a board. // // Run with: pnpm --filter umtool exec tsx --test lib/research/runs.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 { DROPPED, KEPT_KEEPER, REPAIRED, RUN_ARTICLE, RUN_REPORT, TURNS_ARTICLE, finishedTrace, jsonLines, liveTraceHead, liveTraceNext, twoStartTurns, } from "../../e2e/fixtures/run-trace-fixture.mjs"; import { prettyModel, runView, sourceLabelOf, summarizeCalls, turnRuns, type RunIteration } from "./runs-view"; import { runBoardId, runHref } from "./runs-hrefs"; const IT: RunIteration = { kind: "sweep", status: "done", model: "claude-sonnet-5-5", question: "q", scope: { channels: ["harbor-radio"] }, usage: { "claude-sonnet-5-5": { usd: 0.17 }, "claude-haiku-4-5": { usd: 0.01 } }, checks: [{ name: "convert", ok: true }], }; test("a trace: scope, the turns with their enumeration and batch, findings, checks, the end; spend cumulative", () => { const v = runView({ article: RUN_ARTICLE, n: 1, iteration: IT, trace: jsonLines(finishedTrace()), turns: null, report: RUN_REPORT, board: null }); assert.equal(v.from, "trace"); assert.equal(v.live, false); assert.deepEqual(v.runs.map((r) => r.index), [1]); assert.deepEqual( v.steps.map((s) => [s.id, s.label]), [ ["scope", "scope"], ["t0", "enumerate"], ["t1", "extract"], ["t2", "write_report"], ["r0", "edit_report"], ["findings", "findings"], ["checks", "quote checks"], ["end", "ended · done"], ], ); const t0 = v.steps[1]!; assert.equal(t0.detail.enumerations[0]!.n, 3); assert.match(t0.sub!, /3 matched/); const t1 = v.steps[2]!; assert.deepEqual(t1.detail.batches[0]!.ids, ["hr-2022a", "nd-2023", "hr-2021a"]); assert.ok(t1.detail.batches[0]!.lines!.some((l) => l.includes(REPAIRED.now)), "the extractor's own lines"); assert.equal(t1.usd, 0.1, "the turn's total takes in its batch"); assert.equal(Math.round(t1.stepUsd! * 100) / 100, 0.05, "the turn and its extractor"); assert.deepEqual(v.steps.map((s) => s.usd), [null, 0.05, 0.1, 0.16, 0.18, null, null, 0.18]); const checks = v.steps.find((s) => s.kind === "checks")!; assert.equal(checks.sub, "2 kept · 1 dropped · 3 refused"); const drop = checks.detail.checks.find((c) => c.dropped)!; assert.deepEqual([drop.quote, drop.reason, drop.claim], [DROPPED.quote, DROPPED.reason, DROPPED.claim]); assert.equal(v.steps.find((s) => s.kind === "findings")!.detail.findings[1]!.cites[0]!.quote, KEPT_KEEPER); assert.deepEqual([v.header.kept, v.header.dropped, v.header.usd, v.header.iterationUsd], [2, 1, 0.18, 0.18]); assert.deepEqual(v.header.coverage, { matched: 3, read: 3, partial: false, unreached: [] }); assert.equal(v.header.extractor, "claude-haiku-4-5"); assert.equal(v.findings, 2, "two quotes to open as a board"); }); test("a live trace: no end, live; the next turn lands as a step", () => { const head = jsonLines(liveTraceHead()); const live = runView({ article: "l", n: 1, iteration: { ...IT, status: "running" }, trace: head, turns: null, report: null, board: null }); assert.equal(live.live, true); assert.deepEqual(live.steps.map((s) => s.id), ["scope", "t0"]); const more = runView({ article: "l", n: 1, iteration: { ...IT, status: "running" }, trace: head + jsonLines(liveTraceNext()) + '{"t":"cost","to', turns: null, report: null, board: null }); assert.deepEqual(more.steps.map((s) => s.id), ["scope", "t0", "t1"], "a torn last line (still being written) is skipped"); assert.equal(more.steps.at(-1)!.usd, 0.08); }); test("a trace with two runs: the last by default, either on asking, never summed", () => { const two = jsonLines(finishedTrace()) + jsonLines(liveTraceHead().map((e) => ({ ...e, run: 2 }))); const v = runView({ article: "x", n: 1, iteration: IT, trace: two, turns: null, report: RUN_REPORT, board: null }); assert.equal(v.run, 2); assert.deepEqual(v.runs.map((r) => [r.index, r.usd]), [[1, 0.18], [2, 0.03]]); assert.equal(v.header.usd, 0.03); assert.equal(v.findings, 2, "the report is the last run's"); const first = runView({ article: "x", n: 1, iteration: IT, trace: two, turns: null, report: RUN_REPORT, board: null, want: 1 }); assert.equal(first.header.usd, 0.18); assert.equal(first.findings, 0, "an earlier run does not open the report as its board"); }); test("turns only, two run starts: two runs, the stopgap's running totals as each step's spend, the checks a gap", () => { const turns = jsonLines(twoStartTurns()); const v = runView({ article: TURNS_ARTICLE, n: 1, iteration: { ...IT, imported: { extractor: "claude-haiku-4-5" } }, trace: null, turns, report: null, board: null }); assert.equal(v.from, "turns"); assert.deepEqual(v.runs.map((r) => r.index), [1, 2]); assert.equal(v.run, 2); assert.deepEqual(v.steps.map((s) => s.id), ["scope", "t0", "t1", "t2", "t3", "checks"]); assert.deepEqual(v.steps.map((s) => s.usd), [null, 0.04, 0.1, 0.15, 0.16, null]); const gap = v.steps.at(-1)!; assert.equal(gap.gap, true); assert.equal(gap.sub, "kept / dropped not recorded"); assert.ok(v.notes.some((n) => /2 run starts/.test(n))); const first = runView({ article: TURNS_ARTICLE, n: 1, iteration: IT, trace: null, turns, report: null, board: null, want: 1 }); assert.deepEqual(first.steps.map((s) => s.id), ["scope", "t0", "t1", "t2", "checks"]); assert.equal(first.header.usd, 0.09, "the first run's own total, not the file's"); const t1 = first.steps[2]!; assert.deepEqual(t1.detail.batches[0]!.ids, ["hr-2022a", "hr-2021a"]); assert.equal(t1.detail.batches[0]!.lines, null, "the extractor's lines were never kept"); assert.equal(first.steps[0]!.detail.facts.find(([k]) => k === "extractor")![1], "claude-haiku-4-5"); }); test("turnRuns: a file with no start notes splits where turn 0 comes back, but not at its repair round", () => { const r = (turn: number) => ({ kind: "response", turn, usd: 0.01 }); const runs = turnRuns([r(0), r(1), { kind: "note", text: "repair round" }, r(0), r(1), r(0), r(1)]); assert.deepEqual(runs.map((x) => x.length), [5, 2]); assert.deepEqual(turnRuns([]).length, 1); }); test("the small words: models, sources, calls, the board's id and the run's href", () => { assert.equal(prettyModel("claude-sonnet-5-5"), "Sonnet 5.5"); assert.equal(prettyModel("claude-haiku-4-5-20251001"), "Haiku 4.5"); assert.equal(prettyModel("qwen3:8b"), "qwen3:8b"); assert.equal(sourceLabelOf("local:/x/export/.export-builds/jasolyzer-private/out"), "jasolyzer-private build"); assert.equal(sourceLabelOf("local:/repo/export/public"), "local corpus"); assert.equal(sourceLabelOf("remote:https://anilyzer.pages.dev"), "anilyzer.pages.dev"); assert.equal(summarizeCalls(["enumerate_matches", "enumerate_matches", "get_post"]), "enumerate ×2 · get_post"); assert.equal(runBoardId("piratesoftware-ai-friends", 1), "piratesoftware-ai-friends-run1"); assert.equal(runBoardId("a".repeat(64), 12).length, 64); assert.equal(runHref("a-b", 3), "/research/runs/a-b/3"); assert.equal(runHref("a-b", 3, { run: 2 }), "/research/runs/a-b/3?run=2"); }); // ── a run's findings as a board (the store in a temp REPORTS_ROOT) ── let tmp: string; let lib: { board: typeof import("./runs-board"); boards: typeof import("./boards") }; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "umtool-runs-")); const fixture = await import("../../e2e/fixtures/run-trace-fixture.mjs"); const research = await import("../../e2e/fixtures/research-fixture.mjs"); const pub = research.makeResearchFixture({ dest: tmp, channels: path.join(tmp, "channels") }).pub; fixture.makeRunTraceFixture({ articles: path.join(tmp, "articles"), source: `local:${pub}` }); process.env.TRANSCRIPT_LOCAL_DIR = pub; process.env.TRANSCRIPT_HUB_URL = ""; process.env.CHANNELS_DIR = path.join(tmp, "channels"); process.env.SITES_DIR = path.join(tmp, "sites"); process.env.ARTICLES_DIR = path.join(tmp, "articles"); process.env.REPORTS_DIR = path.join(tmp, "reports"); process.env.UMTOOL_CACHE_DIR = path.join(tmp, "cache"); process.env.SETTINGS_FILE = path.join(tmp, "settings.json"); lib = { board: await import("./runs-board"), boards: await import("./boards") }; }); after(async () => { await rm(tmp, { recursive: true, force: true }); }); test("open as a board: the kept findings become quotes by the agent, in their sections; a second open adds nothing twice", async () => { const out = await lib.board.openRunBoard(RUN_ARTICLE, 1); assert.deepEqual([out.id, out.created, out.added, out.already, out.href], ["runfix-run1", true, 2, 0, "/research/runfix-run1"]); const doc = await lib.boards.readCase(out.id); assert.equal(doc.title, "The lighthouse budget"); assert.deepEqual(doc.run && [doc.run.article, doc.run.n], [RUN_ARTICLE, 1]); assert.deepEqual(doc.sections.map((s) => s.title), ["The vote", "The keeper"]); assert.deepEqual(doc.quotes.map((q) => [q.text, q.by, q.claim, q.section]), [ [REPAIRED.now, "agent", "The council voted the budget down", "s1"], [KEPT_KEEPER, "agent", "The keeper says he never saw it", "s2"], ]); const c = doc.quotes[0]!.cite; assert.deepEqual([c.kind, c.channel, c.video, c.t, c.end, c.date, c.clock], ["video", "harbor-radio", "hr-2022a", 2044, 2048, "20220211", "34:04"]); assert.equal(c.url, "https://archive.example/?v=harbor-radio%2Fhr-2022a&t=2044"); assert.match(c.source, /^local:/); // The operator's edit survives a second open, which adds nothing twice. await lib.boards.patchQuote(out.id, "q1", { claim: "Ours now" }); const again = await lib.board.openRunBoard(RUN_ARTICLE, 1); assert.deepEqual([again.created, again.added, again.already], [false, 0, 2]); assert.equal((await lib.boards.readCase(out.id)).quotes[0]!.claim, "Ours now"); // A run with no report keeps nothing to open. await assert.rejects(lib.board.openRunBoard(TURNS_ARTICLE, 1), /kept no findings yet/); // The board's quotes re-verify against the corpus like any pin. const verified = await lib.boards.verifyCase(out.id); assert.deepEqual(verified.results.map((r) => r.verify.status), ["ok", "ok"]); });