// Night-shift triage of the open items (owner's standing rule of 2026-10-07): each open item is // settled by precedent where the owner's earlier rulings cover it, and parked for the morning list // where they do not. A first reader decides, a second reader confirms every change to the data. // bun triage_pass.ts prepare /release -> triage/in-NN.json // bun triage_pass.ts validate // bun triage_pass.ts merge -> triage/decisions.json (applied, closed, parked) // make_release_data.ts reads triage/decisions.json: applied changes are set as night-shift defaults, // closed items leave the open list, parked items stay on it with the reader's options and default. import { createHash } from "crypto"; import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "fs"; import { join } from "path"; const KINDS = [ "field ruling: ruling needs a person", "field ruling: change rejected by the second reader", "field ruling: change the first reader missed", "field ruling: record changed since the ruling was read", "new contradictions without rulings", "two readers disagree", "flag check: change rejected by the second reader", "flag check: change the first reader missed", "flag value from an unruled proposal", "label check: change turned down by two of three readers", "label check: change the first reader missed", ]; const SET_FIELDS = ["date", "victim", "location", "country", "description", "money_wanted", "coin_type"]; const FLAGS = ["detention", "violence_torture", "drugs_alcohol", "weapons", "theft"]; const CASES_PER_BATCH = 26; const J = (p: string) => JSON.parse(readFileSync(p, "utf8")); const norm = (s: unknown) => String(s ?? "").replace(/[‘’]/g, "'").replace(/[“”]/g, '"').replace(/\s+/g, " ").trim(); export const pointKey = (p: { case_id: string | number; kind: string; field?: string; detail?: string; quote?: string }) => createHash("sha1").update([p.case_id, p.kind, p.field ?? "", String(p.detail ?? "").split(" | Night shift: ")[0], p.quote ?? ""].join("\u0001")).digest("hex").slice(0, 16); const batches = () => readdirSync("triage").filter((f) => /^in-\d+\.json$/.test(f)).sort().map((f) => f.slice(3, 5)); if (import.meta.main) { const [cmd, arg] = process.argv.slice(2); if (cmd === "prepare") { if (existsSync("triage/decisions.json")) throw new Error("triage/ already holds a merged pass. Its decisions.json is what the release build reads; a new pass must keep it. Extend this script to merge passes before preparing another one."); mkdirSync("triage", { recursive: true }); const points: any[] = J(join(arg, "data/admin-points.json")); const cases = new Map(J(join(arg, "data/cases.json")).cases.map((c: any) => [c.id, c])); const byCase = new Map(); for (const p of points.filter((p) => KINDS.includes(p.kind) && cases.has(Number(p.case_id)))) byCase.set(Number(p.case_id), [...(byCase.get(Number(p.case_id)) ?? []), { key: pointKey(p), kind: p.kind, field: p.field, detail: p.detail, value_now: p.first_value, value_proposed: p.second_value, quote: p.quote }]); const items = [...byCase.entries()].sort((a, b) => a[0] - b[0]).map(([id, open]) => { const c = cases.get(id); return { id, open_items: open, record: { date: c.date, victim: c.victim, location: c.location, country: c.country, description: c.description, notes: c.notes, money_wanted: c.money_wanted, coin_type: c.coin_type, flags: c.flags, life_taken: c.life_taken_unconfirmed ? "unconfirmed" : c.life_taken, steps: c.steps, demanded: c.demanded, taken: c.taken }, summary: c.summary, dossier: c.dossier_file ? `sources/${c.dossier_file}` : "" }; }); for (let i = 0; i * CASES_PER_BATCH < items.length; i++) writeFileSync(`triage/in-${String(i + 1).padStart(2, "0")}.json`, JSON.stringify(items.slice(i * CASES_PER_BATCH, (i + 1) * CASES_PER_BATCH), null, 1)); console.log(`${items.reduce((n, c) => n + c.open_items.length, 0)} open items on ${items.length} cases in ${Math.ceil(items.length / CASES_PER_BATCH)} batches`); } else if (cmd === "validate" || cmd === "merge") { const applied: any[] = [], closed: any[] = [], parked: any[] = []; let problems = 0, missing = 0, total = 0; for (const nn of batches()) { const input: any[] = J(`triage/in-${nn}.json`); if (!existsSync(`triage/out-${nn}.json`) || statSync(`triage/out-${nn}.json`).mtimeMs <= statSync(`triage/in-${nn}.json`).mtimeMs) { missing++; console.log(`out-${nn}.json missing or older than its input`); continue; } const out: any[] = J(`triage/out-${nn}.json`); const checked = existsSync(`triage/check-${nn}.json`) && statSync(`triage/check-${nn}.json`).mtimeMs > statSync(`triage/out-${nn}.json`).mtimeMs; const verdicts: any[] = checked ? J(`triage/check-${nn}.json`) : []; for (const c of input) { const text = [c.summary, c.dossier && existsSync(c.dossier) ? readFileSync(c.dossier, "utf8") : "", ...c.open_items.flatMap((o: any) => [o.quote, o.detail, o.value_proposed])].map(norm).join(" "); const setSeen = new Set(); for (const item of c.open_items) { total++; const d = out.find((o) => o.key === item.key); const park = (reason: string, extra: any = {}) => parked.push({ key: item.key, case_id: c.id, kind: item.kind, reason, options: d?.options ?? "", default: d?.default ?? "", ...extra }); if (!d) { problems++; park("the first reader gave no decision"); continue; } if (d.action === "close") { const re = verdicts.find((x) => x.key === item.key && x.verdict === "reopen"); if (re) park(`closed by the first reader (${d.reason ?? ""}); reopened by the second: ${re.reason ?? ""}`); else closed.push({ key: item.key, case_id: c.id, reason: String(d.reason ?? ""), second_read: checked }); continue; } if (d.action !== "apply") { park(String(d.reason ?? "parked by the first reader")); continue; } // a change to the data: checked by the script, then by the second reader const ch = d.change ?? {}; const okField = ch.op === "append" ? ch.field === "notes" : SET_FIELDS.includes(ch.field) || FLAGS.includes(ch.field) || ch.field === "life_taken"; const okValue = typeof ch.value === "string" && ch.value.trim() !== "" && (FLAGS.includes(ch.field) ? ["yes", "no", "unconfirmed"].includes(ch.value) || (ch.field === "violence_torture" && ch.value === "threat only") : ch.field === "life_taken" ? /^(\d+|unconfirmed)$/.test(ch.value) : true); if (!okField || !okValue) { problems++; park("the proposed change names a field or value the structure does not take", { proposed: ch }); continue; } if (/[—–]/.test(ch.value)) { problems++; park("the proposed text contains a dash character", { proposed: ch }); continue; } if (typeof d.quote !== "string" || norm(d.quote).length < 8 || !text.includes(norm(d.quote))) { problems++; park("no supporting quote found in the ruling, the summary or the dossier", { proposed: ch }); continue; } if (ch.op !== "append" && setSeen.has(ch.field)) { problems++; park("a second change to the same field of the case", { proposed: ch }); continue; } const v = verdicts.find((x) => x.key === item.key); if (!checked || !v || v.verdict !== "confirm") { park(checked ? `second reader: ${v?.reason ?? "no verdict"}` : "not second-read", { proposed: ch }); continue; } if (ch.op !== "append") setSeen.add(ch.field); applied.push({ key: item.key, id: c.id, field: ch.field, op: ch.op === "append" ? "append" : "set", value: ch.value, quote: d.quote, reason: String(d.reason ?? ""), precedent: String(d.precedent ?? "") }); } } } if (cmd === "merge") { if (missing) throw new Error("a first-reader batch is missing; nothing merged"); writeFileSync("triage/decisions.json", JSON.stringify({ applied, closed, parked }, null, 1)); } console.log(`${total} open items: ${applied.length} applied, ${closed.length} closed, ${parked.length} parked; ${problems} mechanical rejections, ${missing} batches missing`); } else throw new Error("usage: bun triage_pass.ts prepare | validate | merge"); }