#!/usr/bin/env bun /** * One-off: drop selected notes out of a task/feature/security `reviewHistory`. * * Why this exists: a human note is the ONLY thing that increments 复核轮 R# / * humanRejects (see shared/src/types.ts — roundBreakdown). Notes that merely * asked for an E2E re-run because the device farm was flaky therefore inflate * the re-fix rate without any fix having been rejected on its merits. Removing * those notes is how the count gets corrected — there is no separate counter to * patch. * * Scope discipline: this script does NOT search for anything. It takes an * explicit selection file so the set being destroyed is reviewed by a human * first, and it matches on the note's `at` timestamp (stable, unlike array * index, and unique per note in practice). * * selection.json = [{ "kind": "feature", "id": "", "at": 1770000000000 }, ...] * * Usage (run on the control plane host, where DATABASE_URL is set): * bun scripts/purge_review_notes.ts --file selection.json # dry run * bun scripts/purge_review_notes.ts --file selection.json --apply # write * * Every run writes a backup of the affected rows' PRE-state next to the * selection file (`.backup-.json`) — including on dry runs, so the * restore payload exists before anything is touched. Restore = feed those rows' * `reviewHistory` back through the same update helpers. */ import { getTask, updateTask, getFeature, updateFeature, getSecurity, updateSecurity } from "../control-server/src/db"; // Relative, not "@bbbug/shared/types": that alias only resolves from inside a // workspace package (control-server/node_modules), not from scripts/. import { parseReviewHistory, type ReviewNote } from "../shared/src/types"; type Kind = "bug" | "feature" | "security"; interface Selection { kind: Kind; id: string; at: number } function usage(code: number): never { console.log(` purge_review_notes — remove specific review notes from the feedback lineage --file JSON array of { kind: "bug"|"feature"|"security", id, at } --apply actually write (default is a dry run) -h, --help `); process.exit(code); } const argv = process.argv.slice(2); let file: string | null = null; let apply = false; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === "--apply") apply = true; else if (a === "--file") file = argv[++i] || null; else if (a.startsWith("--file=")) file = a.slice("--file=".length); else if (a === "-h" || a === "--help") usage(0); else { console.error(`unknown arg: ${a}`); usage(1); } } if (!file) usage(1); const selections: Selection[] = JSON.parse(await Bun.file(file).text()); if (!Array.isArray(selections) || selections.length === 0) { console.error("selection file is empty — nothing to do"); process.exit(1); } /** Group by row: one UPDATE per item even when several of its notes go. */ const byRow = new Map }>(); for (const s of selections) { const key = `${s.kind}:${s.id}`; const row = byRow.get(key) ?? { kind: s.kind, id: s.id, ats: new Set() }; row.ats.add(s.at); byRow.set(key, row); } const readers = { bug: getTask, feature: getFeature, security: getSecurity, } as const; const writers = { bug: updateTask, feature: updateFeature, security: updateSecurity, } as const; const backup: unknown[] = []; let removed = 0, skipped = 0, wrote = 0; for (const row of byRow.values()) { const item = await readers[row.kind](row.id) as { reviewHistory?: unknown; title?: string } | null; if (!item) { console.warn(` ! ${row.kind}/${row.id} — not found, skipped`); skipped++; continue; } const before: ReviewNote[] = parseReviewHistory(item.reviewHistory); const after = before.filter((n) => !row.ats.has(n.at)); const gone = before.length - after.length; if (gone !== row.ats.size) { // A timestamp that matched nothing means the selection is stale (someone // edited the note since the scan). Refuse the whole row rather than // half-applying it. console.warn(` ! ${row.kind}/${row.id} — expected ${row.ats.size} notes, matched ${gone}; skipped`); skipped++; continue; } backup.push({ kind: row.kind, id: row.id, title: item.title, reviewHistory: before }); // `reviewFeedback` is the mirror of the LATEST note (see Task.reviewFeedback); // it has to follow, or the editable field would still show a purged note. const humanBefore = before.filter((n) => n.source === "human").length; const humanAfter = after.filter((n) => n.source === "human").length; // Pass the ARRAY, not JSON: db.toSetObject already stringifies this column, // so a pre-encoded string would land double-escaped. const patch = { reviewHistory: after.length ? after : null, reviewFeedback: after.length ? after[after.length - 1].text : null, }; console.log(` ${apply ? "→" : "·"} ${row.kind}/${row.id.slice(0, 8)} 「${(item.title ?? "").slice(0, 28)}」` + ` notes ${before.length}→${after.length}, 复核轮 R${humanBefore + 1}→R${humanAfter + 1}`); removed += gone; if (apply) { await writers[row.kind](row.id, patch as never); wrote++; } } const backupPath = `${file}.backup-${backup.length ? (backup[0] as { id: string }).id.slice(0, 4) : "empty"}.json`; await Bun.write(backupPath, JSON.stringify(backup, null, 1)); console.log(`\n${apply ? "APPLIED" : "DRY RUN"} — ${removed} note(s) across ${byRow.size - skipped} item(s)` + `${skipped ? `, ${skipped} skipped` : ""}${apply ? `, ${wrote} row(s) written` : ""}`); console.log(`pre-state backup: ${backupPath}`); if (!apply) console.log("re-run with --apply to write."); process.exit(0);