#!/usr/bin/env bun /** * One-off: resend 待复核 cards that were lost and are too old for the periodic * sweep to reach. * * `notify_reconcile.ts` runs on a 2-hour window on purpose — a card announcing a * day-old review reads as a bug. But when the reconciliation logic itself was * wrong (it compared key EXISTENCE, so every re-fix round was invisible to it), * the backlog it should have caught is by definition already outside that window. * This is the deliberate tool for that backlog, the way `readable:backfill` is * for reports the retry sweep can no longer see. * * Dry-run by default. It reuses `reconcileReviewCards` rather than * re-implementing the decision, so what it sends is exactly what the sweep would * have sent — including the re-read that skips anything a human already resolved. * * bun scripts/backfill_review_cards.ts --hours 48 # list only * bun scripts/backfill_review_cards.ts --hours 48 --send # actually send */ interface Options { hours: number; send: boolean } function usage(code: number): never { console.log(` Usage: bun scripts/backfill_review_cards.ts [--hours N] [--send] Options: --hours N How far back to look for review entries whose card never landed. Default 48. The live sweep uses 2 and is unaffected by this. --send Actually send. Without it, this only prints what WOULD be sent. `.trim()); process.exit(code); } function parseArgs(argv: string[]): Options { const opts: Options = { hours: 48, send: false }; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === "--send") opts.send = true; else if (a === "--hours") opts.hours = Number(argv[++i] || "0") || opts.hours; else if (a.startsWith("--hours=")) opts.hours = Number(a.slice("--hours=".length)) || opts.hours; else if (a === "--help" || a === "-h") usage(0); else { console.error(`Unknown argument: ${a}`); usage(1); } } return opts; } const KINDS = ["bug", "feature", "security"] as const; async function main(): Promise { const opts = parseArgs(process.argv.slice(2)); const windowMs = opts.hours * 60 * 60 * 1000; const now = Date.now(); const { listRecentManualReviewIds, settingKeyTimes } = await import("../control-server/src/db"); const { lastEntryTimes } = await import("../control-server/src/lifecycle_db"); const { sentCardKey } = await import("../control-server/src/lark_card_media"); const { reviewCardMissing, reconcileReviewCards } = await import("../control-server/src/notify_reconcile"); const { larkEnabled } = await import("../control-server/src/lark"); if (!larkEnabled()) { console.error("LARK_APP_ID / LARK_APP_SECRET not set — nothing could be sent. Aborting."); process.exit(1); } // Same query the sweep runs, printed rather than acted on, so the operator sees // the exact set before authorising any send. console.log(`扫描最近 ${opts.hours} 小时进入 manual_review、卡片没跟上的条目:\n`); let found = 0; for (const kind of KINDS) { const rows = await listRecentManualReviewIds(kind, now - windowMs, 500); if (!rows.length) continue; const ids = rows.map((r) => r.id); const [cardAt, entryAt] = await Promise.all([ settingKeyTimes(ids.map((id) => sentCardKey(kind, id))), lastEntryTimes(kind, "manual_review", ids), ]); for (const r of rows) { const at = entryAt.get(r.id) ?? r.statusChangedAt ?? 0; const card = cardAt.get(sentCardKey(kind, r.id)); if (!reviewCardMissing(card, at)) continue; found++; const fmt = (n: number | undefined) => (n ? new Date(n).toLocaleString("sv") : "从未发过"); console.log(` ${kind.padEnd(8)} ${r.id} 进入复核 ${fmt(at)} 上次卡片 ${fmt(card)}`); } } if (!found) { console.log(" (无)"); return; } if (!opts.send) { console.log(`\n共 ${found} 条。加 --send 才会真的发。`); return; } // RESEND_BATCH caps each kind per pass, so loop until a pass sends nothing. console.log(`\n开始补发…`); let total = 0; for (let pass = 0; pass < 10; pass++) { const n = await reconcileReviewCards(Date.now(), windowMs); if (!n) break; total += n; } console.log(`补发完成: ${total} 条(仍在 manual_review 的才会发;已被人复核掉的自动跳过)。`); } await main(); process.exit(0);