import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { ModelLogWriter, modelLogFilesIn, readModelLog, recordModelCall, runWithModelLog } from "../lib/llm/callLog-server"; import { iterationDir } from "../lib/report/iterations"; import { readGenerationUsage } from "./generationUsage"; import { jobModelLogDir, locateJobModelLog, modelLogged } from "./jobModelLog"; // Run with: pnpm -C common exec tsx --test controller/jobModelLog.test.ts // // A generation job's model log (demo epic D3): a job with no iteration keeps // its calls in `.generation/calls//`; a job whose calls went to an // iteration's log leaves a pointer there, and the job's page finds them // through it; either way its local calls reach the usage ledger at no cost. async function setup() { const tmp = await mkdtemp(path.join(os.tmpdir(), "job-model-log-")); const paths = { generationUsageFile: path.join(tmp, ".generation", "usage.jsonl"), articlesDir: path.join(tmp, "reports"), exportIndexDir: path.join(tmp, "idx") }; return { tmp, paths, done: () => rm(tmp, { recursive: true, force: true }) }; } const noLog = () => {}; test("a job with no iteration: its calls in its own dir, found there; its local calls in the ledger", async () => { const t = await setup(); try { const fn = modelLogged({ paths: t.paths, task: "video-assist" }, async () => { recordModelCall({ phase: "assist", model: "qwen3:8b", route: "local", prompt: "make the joins 0.15 s", answer: { ops: [] }, seconds: 2, tokensIn: 300, tokensOut: 20 }); return "ok"; }); assert.equal(await fn(noLog, new AbortController().signal, null, { jobId: "job-a" }), "ok"); const at = await locateJobModelLog(t.paths, "job-a"); assert.deepEqual(at?.where, null); assert.equal(at?.files.calls, path.join(jobModelLogDir(t.paths, "job-a"), "calls.jsonl")); const log = await readModelLog(at!.files); assert.deepEqual(log.calls.map((c) => [c.task, c.phase, c.route]), [["video-assist", "assist", "local"]]); const rows = await readGenerationUsage(t.paths.generationUsageFile); assert.deepEqual(rows.map((r) => [r.jobId, r.task, r.model, r.route, r.usd, r.calls, r.inputTokens]), [["job-a", "video-assist", "qwen3:8b", "local", 0, 1, 300]]); assert.equal(await locateJobModelLog(t.paths, "job-none"), null); } finally { await t.done(); } }); test("a job over an iteration: the iteration's log takes the calls, the job's dir points at it, the locator follows (that job's run only)", async () => { const t = await setup(); try { const dir = iterationDir(t.paths, "art", 2); const fn = modelLogged({ paths: t.paths, task: "report-draft" }, async () => { const inner = new ModelLogWriter({ files: modelLogFilesIn(dir), task: "sweep", jobId: "job-b", where: { article: "art", iteration: 2 }, scrub: {} }); await runWithModelLog(inner, async () => recordModelCall({ phase: "terms", model: "qwen3:8b", route: "local", prompt: "q", answer: { terms: [] }, seconds: 1 })); await inner.flush(); }); await fn(noLog, new AbortController().signal, null, { jobId: "job-b" }); assert.deepEqual(JSON.parse(await readFile(path.join(jobModelLogDir(t.paths, "job-b"), "where.json"), "utf8")), { v: 1, article: "art", n: 2 }); const at = await locateJobModelLog(t.paths, "job-b"); assert.deepEqual(at?.where, { article: "art", n: 2 }); assert.equal(at?.files.calls, path.join(dir, "calls.jsonl")); // The ledger names the iteration the calls were made on. const rows = await readGenerationUsage(t.paths.generationUsageFile); assert.deepEqual(rows.map((r) => [r.jobId, r.article, r.iteration, r.task, r.route]), [["job-b", "art", 2, "sweep", "local"]]); assert.throws(() => jobModelLogDir(t.paths, "../x"), /not a job id/); } finally { await t.done(); } }); test("a job id is a name, never a way up: dots alone are refused", () => { const paths = { generationUsageFile: path.join(os.tmpdir(), ".generation", "usage.jsonl") }; for (const bad of ["..", ".", "a/b", ""]) assert.throws(() => jobModelLogDir(paths, bad), /not a job id/); assert.equal(jobModelLogDir(paths, "job-1.a"), path.join(os.tmpdir(), ".generation", "calls", "job-1.a")); }); test("a pointer that names no report id leads nowhere: `..` in where.json never reaches a path", async () => { const t = await setup(); try { const { mkdir, writeFile } = await import("node:fs/promises"); for (const [job, where] of [ ["job-up", { v: 1, article: "../../etc", n: 1 }], ["job-slash", { v: 1, article: "a/b", n: 1 }], ["job-zero", { v: 1, article: "art", n: 0 }], ["job-num", { v: 1, article: 7, n: 1 }], ] as const) { await mkdir(jobModelLogDir(t.paths, job), { recursive: true }); await writeFile(path.join(jobModelLogDir(t.paths, job), "where.json"), JSON.stringify(where)); assert.equal(await locateJobModelLog(t.paths, job), null, job); } } finally { await t.done(); } });