// A local sweep run FOR REAL over the synthetic draft corpus — what the e2e // editor stub does with a `report-draft` sweep on `synth-kitchen` (demo epic // D3: model-log.spec.ts watches its calls arrive). The pipeline is common's // (sweep-pipeline), the corpus tools the MCP's over the test kit's synthetic // corpus, the model the fake ollama scripted with the kit's recorded run // (common/testing/draft/runs/sweep-pipeline.json), each answer held `--delay` // ms so a page can see the run while it is live. Test-only; synthetic. // // tsx e2e/fixtures/run-sweep.ts --article --n --articles --corpus [--delay 1500] // // The last line printed is `@@report-draft {article, iteration, task, status, // summary}`; the exit code is 0 when the sweep is done. import { existsSync } from "node:fs"; import { mkdir, readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { createCorpusTools } from "yt-dlp-transcript-mcp/callTool"; import { ORIGIN, cuesOf, writeDraftCorpus } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; import { startFakeOllama } from "yt-dlp-transcript-common/testing/llm/fakeOllama.mjs"; import { runTask } from "yt-dlp-transcript-common/lib/report/draft/tasks/index"; const HERE = path.dirname(/* turbopackIgnore: true */ fileURLToPath(import.meta.url)); const RUNS = path.resolve(/* turbopackIgnore: true */ HERE, "../../../common/testing/draft/runs"); const argv = process.argv.slice(2); const flag = (name: string) => { const i = argv.indexOf(`--${name}`); return i < 0 ? undefined : argv[i + 1]; }; const article = flag("article") ?? ""; const n = Number(flag("n")); const articlesDir = flag("articles") ?? ""; const corpusDir = flag("corpus") ?? ""; const delay = Number(flag("delay") ?? "1500"); if (!article || !Number.isInteger(n) || !articlesDir || !corpusDir) { console.error("usage: run-sweep.ts --article --n --articles --corpus [--delay ms]"); process.exit(2); } const run = JSON.parse(await readFile(path.join(RUNS, "sweep-pipeline.json"), "utf8")) as { turns: { message: { content: string } }[] }; // The recorded run (terms, one extract, a meaning check per kept item, the // grouping), with one more term ("oven") so a second recording is read, whose // one item is not in its transcript: code drops it (not verbatim), so the log // has a dropped quote and its reason, beside the recording's three kept ones // (a report needs three). Each answer is picked by what the request asks. const t = (content: unknown) => ({ ...run.turns[0]!, message: { ...run.turns[0]!.message, content: typeof content === "string" ? content : JSON.stringify(content) } }); const meaning = [run.turns[2]!, run.turns[3]!, run.turns[4]!]; const answerFor = (body: { messages?: { content?: string }[] }) => { const user = String(body.messages?.at(-1)?.content ?? ""); if (user.includes("Transcript excerpt:")) { return /Video: Pie stream/.test(user) ? run.turns[1]! : t({ relevant: true, items: [{ quote: "I entered the contest and I won it this year", claim: "She says she won.", on_topic: true, strength: 3 }] }); } if (user.includes("QUOTE: ")) return meaning.shift()!; if (user.includes("Findings:")) return run.turns[5]!; return t({ terms: ["contest", "judges", "oven"] }); }; const ol = await startFakeOllama(); const wait = (ms: number) => new Promise((r) => setTimeout(r, ms)); // Seven answers: terms, two extracts, three meaning checks, one grouping (a second grouping ask meets an empty script). ol.push(...Array.from({ length: 7 }, () => async (body: { messages?: { content?: string }[] }) => (await wait(delay), answerFor(body))) as never[]); const dir = existsSync(path.join(corpusDir, "corpus.json")) ? corpusDir : await writeDraftCorpus(corpusDir); const corpus = await createCorpusTools({ source: `local:${dir}` }); const exportIndexDir = path.join(articlesDir, "..", ".export-index-run-sweep"); await mkdir(exportIndexDir, { recursive: true }); let code = 1; try { const res = await runTask( { task: "sweep-pipeline", article, n, origin: ORIGIN }, { llm: { ollama: { baseUrl: ol.url } }, corpus, cuesOf, paths: { articlesDir, exportIndexDir }, log: (l) => console.log(l) }, ); console.log(`@@report-draft ${JSON.stringify({ article, iteration: n, task: "sweep", status: res.status, summary: res.summary })}`); code = res.status === "done" ? 0 : 1; } catch (e) { console.log(`@@report-draft ${JSON.stringify({ article, iteration: n, task: "sweep", status: "failed", summary: (e as Error).message })}`); } finally { await corpus.close(); await ol.close(); } process.exit(code);