Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit dd40274204f6fb7cb6f2941df98bb072a51b50ac
parent aaf385c5e35f85d5e5ff1335a8749696e27d85bc
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Tue, 13 Oct 2026 13:20:25 -0400

editor: release 27 S9 — the generation reads: the day's API spend and an article's iterations

GET /api/ops/generation-usage[?day=] answers the usage ledger's total for the
UTC day and the daily cap; GET /api/ops/iterations?article=[&n=] answers an
article's iterations, or one with its directory and files on the editor's
disk. Both read only, behind the token. `pnpm ops get generation-usage` and
`get iterations <article>`; COMMANDS.md regenerated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Diffstat:
MCOMMANDS.md | 7+++++++
Aeditor/app/api/ops/_generation.test.ts | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aeditor/app/api/ops/generation-usage/route.ts | 26++++++++++++++++++++++++++
Aeditor/app/api/ops/iterations/route.ts | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/archilyzer-ops.mjs | 13+++++++++++++
Mscripts/archilyzer-ops.test.mjs | 10++++++++++
6 files changed, 205 insertions(+), 0 deletions(-)

diff --git a/COMMANDS.md b/COMMANDS.md @@ -158,6 +158,7 @@ Usage: pnpm ops <action> [--json '<body>' | --file <path>] [--wait] pnpm ops get remote-listing <slug> [--wait-timeout <seconds>] pnpm ops get transcript <videoId> [--slug <slug>] pnpm ops get coverage <slug> + pnpm ops get generation-usage [<YYYY-MM-DD>] | iterations <article> pnpm ops list --wait follows the job's log and survives a poll that fails (a busy @@ -201,6 +202,12 @@ get coverage <slug> is what a channel holds by date: held, dated (by over 30 days with nothing held. Off disk; nothing written. (The MCP's channel_coverage takes gap days, a date window and a video list.) +get generation-usage [<day>] is the day's Claude API spend on generation + (the usage ledger's total for the UTC day, default today) and the daily + cap it is checked against: {day, usd, jobs, daily}. +get iterations <article> is an article's iterations (iteration.json each) + and which is current. (The MCP's generate reads one, with its files.) + get transcript <videoId> [--slug <slug>] reads one video's cues off disk with no index: a fresh cues.json, else what build-cues would write, else the English VTT alone — {source, cuesJson, title?, ..., cues}. Nothing is diff --git a/editor/app/api/ops/_generation.test.ts b/editor/app/api/ops/_generation.test.ts @@ -0,0 +1,93 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdir, writeFile } from "node:fs/promises"; +import path from "node:path"; +import { callGet, setupOpsCorpus } from "./_testCorpus"; + +// Run with: +// pnpm -C editor exec tsx --test "app/api/ops/_generation.test.ts" +// +// The two generation reads (release 27 S9): the day's API spend +// (`generation-usage`) and an article's iterations (`iterations`), behind the +// token, read only — what the MCP's generation_policy and generate ask. + +const corpus = await setupOpsCorpus(null); +const usageFile = path.join(corpus.root, ".generation", "usage.jsonl"); +process.env.GENERATION_USAGE_FILE = usageFile; +await corpus.writeSettings({ minFreeDiskGB: 0, generation: { caps: { daily: 7 } } }); + +type Get = Parameters<typeof callGet>[0]; +const route = async (name: string) => (await import(`./${name}/route`)).GET as Get; +test.after(() => corpus.cleanup()); + +const row = (day: string, jobId: string, usd: number) => + JSON.stringify({ + at: `${day}T10:00:00.000Z`, + day, + jobId, + article: "synth-a", + iteration: 1, + task: "sweep", + model: "claude-sonnet-5-5", + inputTokens: 1, + outputTokens: 1, + cacheReadTokens: 0, + cacheWriteTokens: 0, + usd, + }); + +test("both reads are behind the token", async () => { + for (const name of ["generation-usage", "iterations"]) { + const res = await callGet(await route(name), "http://localhost/api/ops/x?article=a", {}, {}); + assert.equal(res.status, 401, name); + } +}); + +test("generation-usage answers the day's ledger total and the daily cap", async () => { + const get = await route("generation-usage"); + const none = await callGet(get, "http://localhost/api/ops/generation-usage?day=2026-10-01"); + assert.deepEqual(none.body, { ok: true, day: "2026-10-01", usd: 0, jobs: 0, daily: 7 }); + await mkdir(path.dirname(usageFile), { recursive: true }); + await writeFile(usageFile, [row("2026-10-01", "j1", 1.25), row("2026-10-01", "j2", 0.5), row("2026-10-02", "j3", 9)].join("\n") + "\n"); + const some = await callGet(get, "http://localhost/api/ops/generation-usage?day=2026-10-01"); + assert.deepEqual(some.body, { ok: true, day: "2026-10-01", usd: 1.75, jobs: 2, daily: 7 }); + const today = await callGet(get, "http://localhost/api/ops/generation-usage"); + assert.equal(today.body.day, new Date().toISOString().slice(0, 10)); + const bad = await callGet(get, "http://localhost/api/ops/generation-usage?day=yesterday"); + assert.equal(bad.status, 400); + const stray = await callGet(get, "http://localhost/api/ops/generation-usage?date=2026-10-01"); + assert.equal(stray.status, 400); +}); + +test("iterations lists an article's iterations, and one with where its files are", async () => { + const { createIteration, updateIteration, writeIterationFile } = await import( + "yt-dlp-transcript-common/lib/report/iterations" + ); + await createIteration("synth-a", { kind: "sweep", model: "claude-sonnet-5-5", question: "q?", scope: { channels: ["synth-kitchen"] } }); + await writeIterationFile("synth-a", 1, "sweep.md", "# A sweep\n"); + await updateIteration("synth-a", 1, { status: "done", ops: { applied: [{ op: "drop" }], refused: [] } }); + const get = await route("iterations"); + + const all = await callGet(get, "http://localhost/api/ops/iterations?article=synth-a"); + assert.equal(all.status, 200); + const its = all.body.iterations as { n: number; status: string }[]; + assert.deepEqual(its.map((i) => [i.n, i.status]), [[1, "done"]]); + assert.deepEqual(all.body.current, { report: null, reportFrom: null, video: null }); + + const one = await callGet(get, "http://localhost/api/ops/iterations?article=synth-a&n=1"); + assert.equal(one.status, 200); + assert.equal((one.body.iteration as { kind: string }).kind, "sweep"); + const files = one.body.files as Record<string, string | null>; + assert.equal(files["sweep.md"], path.join(String(one.body.dir), "sweep.md")); + assert.equal(files["report.json"], null); + assert.ok(String(one.body.dir).startsWith(corpus.transcripts)); + + const missing = await callGet(get, "http://localhost/api/ops/iterations?article=synth-a&n=4"); + assert.equal(missing.status, 404); + const nobody = await callGet(get, "http://localhost/api/ops/iterations?article=synth-none"); + assert.equal(nobody.status, 404); + const badId = await callGet(get, "http://localhost/api/ops/iterations?article=../x"); + assert.equal(badId.status, 400); + const badN = await callGet(get, "http://localhost/api/ops/iterations?article=synth-a&n=0"); + assert.equal(badN.status, 400); +}); diff --git a/editor/app/api/ops/generation-usage/route.ts b/editor/app/api/ops/generation-usage/route.ts @@ -0,0 +1,26 @@ +import { getPaths } from "yt-dlp-transcript-common/lib/paths"; +import { getSettings } from "yt-dlp-transcript-common/lib/settings"; +import { dayUsage, utcDay } from "yt-dlp-transcript-common/controller/generationUsage"; +import { readRoute } from "../_read"; +import { opsFail } from "../_lib"; + +export const dynamic = "force-dynamic"; + +// GET [?day=YYYY-MM-DD] +// -> { ok, day, usd, jobs, daily } +// +// The day's Claude API spend on generation (release 27 S9): the usage +// ledger's total for the UTC day (default today) — what the daily cap is +// checked against (controller/generationUsage.ts) — and the cap itself +// (`settings.generation.caps.daily`, 0 = none). The MCP's generation_policy +// and `archilyzer gen policy` show it beside the routes. Read only. +const DAY = /^\d{4}-\d{2}-\d{2}$/; + +export async function GET(request: Request) { + return readRoute(request, ["day"], async (q) => { + const day = q.get("day")?.trim() || utcDay(); + if (!DAY.test(day)) return opsFail('"day" must be a date YYYY-MM-DD (UTC)'); + const used = await dayUsage(getPaths().generationUsageFile, day); + return { ...used, daily: getSettings().generation.caps.daily }; + }); +} diff --git a/editor/app/api/ops/iterations/route.ts b/editor/app/api/ops/iterations/route.ts @@ -0,0 +1,56 @@ +import { access } from "node:fs/promises"; +import { getPaths } from "yt-dlp-transcript-common/lib/paths"; +import { + ITERATION_FILES, + iterationDir, + iterationFile, + readIteration, + readIterations, +} from "yt-dlp-transcript-common/lib/report/iterations"; +import { isReportId } from "yt-dlp-transcript-common/lib/report/schema"; +import { readRoute } from "../_read"; +import { opsFail } from "../_lib"; + +export const dynamic = "force-dynamic"; + +// GET ?article=<id>[&n=<n>] +// -> { ok, article, current, iterations: [iteration.json…], problems } +// -> with n: { ok, article, current, iteration, dir, files: {<name>: <path> | null} } +// +// An article's iterations (release 27 S9; lib/report/iterations.ts), read +// only: every one with which is current, or one with where it lives on the +// editor's disk — its directory and the path of each file it has (report.json, +// sweep.md, turns.jsonl, proposals.json; null when absent). The MCP's +// `generate` summarises a finished job's iteration from it, and names the +// files Claude Code reviews. Nothing here writes; making one current is +// umtool's Iterations tab (or the chain's `current` step). +export async function GET(request: Request) { + return readRoute(request, ["article", "n"], async (q) => { + const article = q.get("article")?.trim() ?? ""; + if (!article) return opsFail('"article" is required (an article id)'); + if (!isReportId(article)) return opsFail(`"${article}" is not an article id`); + const paths = getPaths(); + const rawN = q.get("n"); + const read = await readIterations(article, paths); + if (rawN === null) { + if (read.iterations.length === 0 && read.problems.length === 0) { + return opsFail(`"${article}" has no iterations`, 404); + } + return { article, current: read.current, iterations: read.iterations, problems: read.problems }; + } + const n = Number(rawN); + if (!Number.isInteger(n) || n < 1) return opsFail('"n" is an iteration number (1, 2, …)'); + const it = await readIteration(article, n, paths).catch((e: Error) => e); + if (it instanceof Error) return opsFail(`"${article}" i${n}: ${it.message}`, 500); + if (!it) return opsFail(`"${article}" has no iteration i${n}`, 404); + const files: Record<string, string | null> = {}; + for (const name of ITERATION_FILES) { + const file = iterationFile(paths, article, n, name); + files[name] = await access(file).then( + () => file, + () => null, + ); + } + return { article, current: read.current, iteration: it, dir: iterationDir(paths, article, n), files }; + }); +} diff --git a/scripts/archilyzer-ops.mjs b/scripts/archilyzer-ops.mjs @@ -192,6 +192,11 @@ const GETTERS = { if (jobs.slug) q.set("slug", jobs.slug); return `/api/ops/transcript?${q.toString()}`; }, + // Generation (release 27 S9): the day's Claude API spend against the daily + // cap (default today, UTC), and an article's iterations with which is current. + "generation-usage": (day) => + day ? `/api/ops/generation-usage?day=${encodeURIComponent(day)}` : "/api/ops/generation-usage", + iterations: (article) => `/api/ops/iterations?article=${encodeURIComponent(article)}`, }; // READS THAT ARE JOBS: the answer needs a request upstream, so the editor @@ -221,6 +226,7 @@ const GET_ARG_OPTIONAL = new Set([ "workers", "auto-queue", "scheduler", + "generation-usage", ]); // The flags each noun takes beyond the shared ones; any other is refused. @@ -706,6 +712,7 @@ export function usage() { " pnpm ops get remote-listing <slug> [--wait-timeout <seconds>]", " pnpm ops get transcript <videoId> [--slug <slug>]", " pnpm ops get coverage <slug>", + " pnpm ops get generation-usage [<YYYY-MM-DD>] | iterations <article>", " pnpm ops list", "", `Actions: ${ACTIONS.join(", ")}`, @@ -763,6 +770,12 @@ export function usage() { " over 30 days with nothing held. Off disk; nothing written. (The MCP's", " channel_coverage takes gap days, a date window and a video list.)", "", + "get generation-usage [<day>] is the day's Claude API spend on generation", + " (the usage ledger's total for the UTC day, default today) and the daily", + " cap it is checked against: {day, usd, jobs, daily}.", + "get iterations <article> is an article's iterations (iteration.json each)", + " and which is current. (The MCP's generate reads one, with its files.)", + "", "get transcript <videoId> [--slug <slug>] reads one video's cues off disk", " with no index: a fresh cues.json, else what build-cues would write, else", " the English VTT alone — {source, cuesJson, title?, ..., cues}. Nothing is", diff --git a/scripts/archilyzer-ops.test.mjs b/scripts/archilyzer-ops.test.mjs @@ -856,3 +856,13 @@ test("get coverage reads one channel's coverage", () => { assert.match(usage(), /get coverage <slug> is what a channel holds by date/); assert.match(usage(), /recordedDateTitlePattern/); }); + +test("get generation-usage and get iterations read the generation state (release 27 S9)", () => { + assert.equal(parseArgs(["get", "generation-usage"]).path, "/api/ops/generation-usage"); + assert.equal(parseArgs(["get", "generation-usage", "2026-10-01"]).path, "/api/ops/generation-usage?day=2026-10-01"); + const r = parseArgs(["get", "iterations", "demo-article"]); + assert.equal(r.method, "GET"); + assert.equal(r.path, "/api/ops/iterations?article=demo-article"); + assert.match(parseArgs(["get", "iterations"]).error, /needs an argument/); + assert.match(usage(), /get generation-usage \[<day>\] is the day's Claude API spend/); +});