// Recheck of two fields on every case, ordered by the owner on 2026-10-07: // outcome the new value "fought off" // who_was_attacked every person attacked, not only the holder // bun recheck_pass.ts prepare /release/data/cases.json -> recheck/in-NN.json // bun recheck_pass.ts validate // bun recheck_pass.ts merge -> recheck/merged.json (second-reader-confirmed changes) and recheck/not-applied.json // A change is applied only when its quote is found in the case text and the second reader confirmed it. import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "fs"; const LABELS = JSON.parse(readFileSync("step-labels.json", "utf8")); const BATCH = 40; const J = (p: string) => JSON.parse(readFileSync(p, "utf8")); const norm = (s: unknown) => String(s ?? "").replace(/[‘’]/g, "'").replace(/[“”]/g, '"').replace(/\s+/g, " ").trim(); const same = (a: unknown, b: unknown) => JSON.stringify(Array.isArray(a) ? [...a].sort() : a) === JSON.stringify(Array.isArray(b) ? [...b].sort() : b); const batches = () => readdirSync("recheck").filter((f) => /^in-\d+\.json$/.test(f)).sort().map((f) => f.slice(3, 5)); const [cmd, arg] = process.argv.slice(2); if (cmd === "prepare") { const cases: any[] = J(arg).cases; const items = cases.map((c) => ({ id: c.id, victim_field: c.victim, current: { who_was_attacked: c.steps.who_was_attacked, outcome: c.steps.outcome }, attack: c.steps.attack, detention: c.flags.detention, summary: c.summary, })); for (let i = 0; i * BATCH < items.length; i++) writeFileSync(`recheck/in-${String(i + 1).padStart(2, "0")}.json`, JSON.stringify(items.slice(i * BATCH, (i + 1) * BATCH), null, 1)); console.log(`${items.length} cases in ${Math.ceil(items.length / BATCH)} batches`); } else if (cmd === "validate" || cmd === "merge") { const merged: any[] = []; const notApplied: any[] = []; let problems = 0, applied = 0, missing = 0, read = 0; const say = (nn: string, id: unknown, msg: string) => (problems++, cmd === "validate" && console.log(`out-${nn} case ${id}: ${msg}`)); for (const nn of batches()) { const input: any[] = J(`recheck/in-${nn}.json`); if (!existsSync(`recheck/out-${nn}.json`)) { missing++; console.log(`out-${nn}.json missing`); continue; } const out: any[] = J(`recheck/out-${nn}.json`); const checked = existsSync(`recheck/check-${nn}.json`) && statSync(`recheck/check-${nn}.json`).mtimeMs > statSync(`recheck/out-${nn}.json`).mtimeMs; const verdicts: any[] = checked ? J(`recheck/check-${nn}.json`) : []; const byId = new Map(out.map((o) => [o.id, o])); for (const c of input) { const o = byId.get(c.id); if (!o) { say(nn, c.id, "missing"); notApplied.push({ id: c.id, kind: "recheck: case not read", detail: "missing from the first reader's output" }); continue; } read++; const text = norm(c.summary); const changes: any[] = []; for (const [field, evidence] of [["who_was_attacked", "who_evidence"], ["outcome", "outcome_evidence"]] as const) { const value = o[field]; const spec = LABELS[field]; const values: unknown[] = spec.several ? value : [value]; if (spec.several ? !Array.isArray(value) || !value.length : typeof value !== "string") { say(nn, c.id, `${field}: wrong shape`); notApplied.push({ id: c.id, kind: "recheck: change rejected by the script", field, detail: "value has the wrong shape" }); continue; } if (values.some((x) => typeof x !== "string" || !Object.hasOwn(spec.values, x))) { say(nn, c.id, `${field}: label not in the list`); notApplied.push({ id: c.id, kind: "recheck: change rejected by the script", field, proposed: value, detail: "label not in the list" }); continue; } if (same(value, c.current[field])) continue; const quote = o[evidence]; const reject = (why: string) => { say(nn, c.id, `${field}: ${why}`); notApplied.push({ id: c.id, kind: "recheck: change rejected by the script", field, proposed: value, detail: why, quote }); }; if (typeof quote !== "string" || norm(quote).length < 8) { reject("no quote for the change"); continue; } if (!text.includes(norm(quote))) { reject("quote not found in the case text"); continue; } const v = verdicts.find((x) => x.id === c.id && x.field === field); if (!checked) { notApplied.push({ id: c.id, kind: "recheck: change not second-read", field, proposed: value, quote }); continue; } if (!v || v.verdict !== "confirm") { notApplied.push({ id: c.id, kind: "recheck: change rejected by the second reader", field, proposed: value, quote, detail: v?.reason ?? "no verdict given" }); continue; } changes.push({ field, old: c.current[field], new: value, quote }); applied++; } for (const v of verdicts.filter((x) => x.id === c.id && x.verdict === "missed")) notApplied.push({ id: c.id, kind: "recheck: change the first reader missed", field: v.field, proposed: v.proposed, quote: v.quote, detail: v.reason }); if (changes.length) merged.push({ id: c.id, changes }); } } if (cmd === "merge") { if (missing) throw new Error("a first-reader batch is missing; nothing merged"); writeFileSync("recheck/merged.json", JSON.stringify(merged, null, 1)); writeFileSync("recheck/not-applied.json", JSON.stringify(notApplied, null, 1)); } console.log(`${read} cases read, ${applied} changes ${cmd === "merge" ? "applied" : "would apply"} on ${merged.length} cases, ${notApplied.length} not applied, ${problems} mechanical rejections, ${missing} batches missing`); } else throw new Error("usage: bun recheck_pass.ts prepare | validate | merge");