#!/usr/bin/env bun interface Options { taskId: string | null; batch: boolean; force: boolean; limit: number | null; } type ReadableModule = typeof import("../control-server/src/readable_report"); function parseArgs(argv: string[]): Options { const opts: Options = { taskId: process.env.task_id || process.env.TASK_ID || null, batch: false, force: false, limit: null, }; for (let i = 0; i < argv.length; i++) { const arg = argv[i]; if (arg === "--batch") opts.batch = true; else if (arg === "--force") opts.force = true; else if (arg === "--help" || arg === "-h") usage(0); else if (arg === "--task-id" || arg === "--task_id") opts.taskId = argv[++i] || null; else if (arg.startsWith("--task-id=")) opts.taskId = arg.slice("--task-id=".length); else if (arg.startsWith("--task_id=")) opts.taskId = arg.slice("--task_id=".length); else if (arg.startsWith("task_id=")) opts.taskId = arg.slice("task_id=".length); else if (arg === "--limit") opts.limit = Number(argv[++i] || "0") || null; else if (arg.startsWith("--limit=")) opts.limit = Number(arg.slice("--limit=".length)) || null; else { console.error(`Unknown argument: ${arg}`); usage(1); } } return opts; } function usage(code: number): never { const text = ` Usage: task_id=b3ff7fbe-b80d-4659-a600-52c4a4a87f62 bun scripts/backfill_readable_reports.ts bun scripts/backfill_readable_reports.ts --task-id b3ff7fbe-b80d-4659-a600-52c4a4a87f62 bun scripts/backfill_readable_reports.ts --batch Options: --batch Convert every historical bug task that has fix artifacts and no readable report. --force Regenerate even when readableReportHtml already exists. --limit N Process at most N tasks in batch mode. Notes: - The script calls the task's executor satellite, so the executor node must be online. - Use READABLE_REPORT_DRY_RUN=1 to test the pipeline without invoking Claude. `; console.log(text.trim()); process.exit(code); } async function convertOne(taskId: string, opts: { force: boolean }): Promise { const { getTask, getTaskReadableReport } = await import("../control-server/src/db"); const { generateReadableReportForTask } = await import("../control-server/src/readable_report"); // db.ts went async (Postgres); these were left as sync calls, so `task` was a // Promise — always truthy, and `task.meegoId` logged as undefined. const task = await getTask(taskId); if (!task) { console.error(`[missing] task not found: ${taskId}`); return false; } const existing = await getTaskReadableReport(taskId); if (!opts.force && existing?.html) { console.log(`[skip] ${taskId} already has readable report`); return true; } console.log(`[start] ${taskId} ${task.meegoId} ${task.title}`); const result = await generateReadableReportForTask(taskId, { force: opts.force, broadcastUpdate: false, }); if (result.ok) { console.log(result.skipped ? `[skip] ${taskId}` : `[ok] ${taskId}`); return true; } console.error(`[fail] ${taskId}: ${result.error || "unknown error"}`); return false; } async function main(): Promise { const opts = parseArgs(Bun.argv.slice(2)); if (!opts.taskId && !opts.batch) usage(1); if (opts.taskId && opts.batch) { console.error("Choose either --task-id/task_id or --batch, not both."); process.exit(1); } if (opts.taskId) { const ok = await convertOne(opts.taskId, { force: opts.force }); process.exit(ok ? 0 : 1); } const { readableBackfillTaskCandidates } = await import("../control-server/src/readable_report") as ReadableModule; let tasks = await readableBackfillTaskCandidates({ force: opts.force }); if (opts.limit != null) tasks = tasks.slice(0, opts.limit); console.log(`[batch] ${tasks.length} task(s) to convert${opts.force ? " (force)" : ""}`); let ok = 0; let fail = 0; for (const task of tasks) { if (await convertOne(task.id, { force: opts.force })) ok++; else fail++; } console.log(`[done] ok=${ok} fail=${fail}`); process.exit(fail ? 1 : 0); } main().catch((err) => { console.error((err as Error).stack || (err as Error).message); process.exit(1); });