// The generation tasks end to end (release 27, S5): each task over the // synthetic draft corpus read through the MCP's own tools in process, against // the fake Anthropic and ollama servers, writing a real iterations store in a // temp dir. The model side of each run is a recorded transcript // (common/testing/draft/runs/.json). Here and not in common: common may // not import the mcp package, and the corpus tools are the MCP's. import { after, before, beforeEach, test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { CHANNELS, POSTS, ORIGIN, cuesOf, writeDraftCorpus } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; import { FAKE_KEY, startFakeAnthropic, textTurn, type FakeAnthropic } from "yt-dlp-transcript-common/testing/llm/fakeAnthropic.mjs"; import { jsonReply, reply, startFakeOllama, toolCallsReply, type FakeOllama } from "yt-dlp-transcript-common/testing/llm/fakeOllama.mjs"; import { createIteration, readIteration, readIterationFile, updateIteration, writeIterationFile, projectOf, } from "yt-dlp-transcript-common/lib/report/iterations"; import { runTask, type TaskDeps } from "yt-dlp-transcript-common/lib/report/draft/tasks/index"; import type { TaskProgress } from "yt-dlp-transcript-common/lib/report/draft/tasks/types"; import { createCorpusTools, type CorpusTools } from "./callTool"; import { modelLogFilesIn, readModelCallText, readModelLog } from "yt-dlp-transcript-common/lib/llm/callLog-server"; import { iterationDir } from "yt-dlp-transcript-common/lib/report/iterations"; const RUNS = path.join(path.dirname(new URL(import.meta.url).pathname), "../../common/testing/draft/runs"); const DRAFT = path.join(path.dirname(new URL(import.meta.url).pathname), "../../common/testing/draft"); const run = async (name: string) => JSON.parse(await readFile(path.join(RUNS, `${name}.json`), "utf8")); // A sweep's trace.jsonl (umtool R3), one event a line. const traceOf = async (article: string, n: number) => ((await readIterationFile(article, n, "trace.jsonl", paths)) ?? "") .trim() .split("\n") .filter(Boolean) .map((l) => JSON.parse(l) as Record); let tmp: string; let corpus: CorpusTools; let api: FakeAnthropic; let ol: FakeOllama; let paths: { articlesDir: string; exportIndexDir: string }; let projectsRoot: string; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "draft-tasks-")); const dir = await writeDraftCorpus(path.join(tmp, "corpus")); corpus = await createCorpusTools({ source: `local:${dir}` }); api = await startFakeAnthropic(); ol = await startFakeOllama(); }); after(async () => { await corpus.close(); await api.close(); await ol.close(); await rm(tmp, { recursive: true, force: true }); }); let caseN = 0; beforeEach(async () => { caseN++; paths = { articlesDir: path.join(tmp, `reports-${caseN}`), exportIndexDir: path.join(tmp, `idx-${caseN}`) }; projectsRoot = path.join(tmp, `projects-${caseN}`); await mkdir(paths.articlesDir, { recursive: true }); }); const postText = async (channel: string, id: string) => POSTS.find((p) => p.channelSlug === channel && p.id === id)?.text ?? null; function deps(extra: Partial = {}): TaskDeps & { lines: string[]; progressed: TaskProgress[] } { const lines: string[] = []; const progressed: TaskProgress[] = []; return { llm: { anthropic: { baseUrl: api.url, env: { ANTHROPIC_API_KEY: FAKE_KEY }, sleep: async () => {} }, ollama: { baseUrl: ol.url }, }, corpus, cuesOf, postTextOf: postText, paths, log: (l) => lines.push(l), lines, progress: (p) => progressed.push(p), progressed, ...extra, }; } // i1: a done sweep holding a fixture's sweep.md and report.json (and, given a // manifest, its video project). async function doneSweep(article: string, fixture: string, o: { manifest?: boolean; voices?: boolean } = {}) { await createIteration(article, { kind: "sweep", model: "claude-sonnet-5-5", question: "what does she say", scope: { channels: ["synth-kitchen"] } }, paths); await writeIterationFile(article, 1, "sweep.md", await readFile(path.join(DRAFT, "sweeps", `${fixture}.md`), "utf8"), paths); const report = JSON.parse(await readFile(path.join(DRAFT, "reports", `${fixture}.report.json`), "utf8")); await writeIterationFile(article, 1, "report.json", report, paths); let projectDir: string | undefined; if (o.manifest) { projectDir = projectOf(projectsRoot, article, 1); await mkdir(path.join(projectDir, "out", "narration"), { recursive: true }); await writeFile(path.join(projectDir, "video.manifest.json"), await readFile(path.join(DRAFT, "manifests", `${fixture}.video.manifest.json`), "utf8")); await writeFile(path.join(projectDir, "report.json"), JSON.stringify(report)); if (o.voices) await writeFile(path.join(projectDir, "out", "narration", "title.wav"), "RIFF fake voice"); } await updateIteration(article, 1, { status: "done", ...(projectDir ? { projectDir } : {}) }, paths); return { report, projectDir }; } test("sweep-agent: the plan, the corpus tools, extract_batch, the report file; the link backstop; one repair round; done", async () => { const r = await run("sweep-agent"); api.push(...r.turns); const it = await createIteration("synth-contest", { kind: "sweep", model: r.model, question: r.question, scope: { channels: r.channels } }, paths); const d = deps(); const out = await runTask({ task: "sweep-agent", article: "synth-contest", n: it.n, extractorModel: r.extractorModel, origin: ORIGIN, maxUsd: 5 }, d); assert.equal(out.status, "done", `${out.summary}\n${d.lines.join("\n")}`); assert.equal(api.remaining(), 0, "every scripted turn was used"); // The plan is the MCP's own sweep_plan; the agent sees no source and no writes. const first = api.requests[0]; assert.match(first.messages[0].content, /Run a \*\*corpus sweep\*\*/); const names = first.tools.map((t: { name: string }) => t.name); for (const n of ["enumerate_matches", "get_transcripts", "extract_batch", "write_report", "edit_report", "read_report"]) assert.ok(names.includes(n), n); for (const n of ["fetch_clip", "enqueue", "notes", "get_job"]) assert.ok(!names.includes(n), n); assert.ok(first.tools.some((t: { type?: string }) => t.type === "web_search_20250305"), "Claude's server web search is on"); const system = first.system[0].text as string; assert.match(system, /UNATTENDED/); assert.match(system, /A claim never carries quotation marks/); assert.ok(system.includes(`${ORIGIN}/?v=%2F