// THE PRIVACY GATE (plans/release-38/research-cases-and-assist.md, stage 6; "Private names are stored as hashes"). // // Every write of findings, of the summary, of an export and of a hand-off is scanned against the case's // `privateNames.sha256` (privateNames-server.ts privateNameHits: every word and 2- and 3-word phrase hashed and // compared). A hit blocks the write. What it says is COUNTS AND ROW IDS: never the term, never the phrase that // matched, never in a log -- the case's private names are in no file, and the gate's refusal must not put them in // one. A caller logs the error's message as it stands; it holds nothing else. import { NAME_WITHHELD, normalizeTerm, stripPossessive } from "./caseTypes"; import { privateNameHash, privateNameHits } from "./privateNames-server"; export type GateItem = { id: string; text: string }; export type GateResult = { blocked: boolean; hits: number; rows: { id: string; hits: number }[] }; /** Scan each item; `rows` names the items with a hit and how many, in the order given. */ export function privacyGate(items: readonly GateItem[], hashes: readonly string[]): GateResult { if (!hashes.length) return { blocked: false, hits: 0, rows: [] }; const rows: GateResult["rows"] = []; let hits = 0; for (const it of items) { const n = privateNameHits(it.text, hashes); if (!n) continue; hits += n; rows.push({ id: it.id, hits: n }); } return { blocked: rows.length > 0, hits, rows }; } /** A write the gate refused. `status` 409: the content is what has to change. */ export class PrivacyBlocked extends Error { readonly status = 409; constructor( readonly what: string, readonly result: GateResult, ) { const ids = result.rows.slice(0, 50).map((r) => r.id); super( `the privacy gate blocked ${what}: ${result.hits} private-name ${result.hits === 1 ? "hit" : "hits"} in ` + `${result.rows.length} ${result.rows.length === 1 ? "item" : "items"} (${ids.join(", ")}${result.rows.length > ids.length ? ", …" : ""}). ` + `Write [name withheld] in their place and try again.`, ); this.name = "PrivacyBlocked"; } } /** Throw PrivacyBlocked when any item hits. */ export function assertNoPrivateNames(what: string, items: readonly GateItem[], hashes: readonly string[]): void { const r = privacyGate(items, hashes); if (r.blocked) throw new PrivacyBlocked(what, r); } /** * The case's private names taken OUT of a text the case stores (lane A): every word or 2- or 3-word phrase that * hashes to one of them becomes NAME_WITHHELD (a possessive keeps its ending: "[name withheld]'s"). The windows * and batches a reader reads are corpus text, which names whom it names; withheld here, the case's files never * hold a private name, a reader never sees one, and a quote written with [name withheld] still matches its * window's words (lane B's quote check). */ export function withholdPrivateNames(text: string, hashes: readonly string[]): { text: string; replaced: number } { if (!hashes.length || !text) return { text, replaced: 0 }; const set = new Set(hashes); const tokens = [...text.matchAll(/[\p{L}\p{N}'’\-]+/gu)].map((m) => ({ start: m.index ?? 0, end: (m.index ?? 0) + m[0].length, norm: normalizeTerm(m[0]) })); let out = ""; let at = 0; let replaced = 0; for (let i = 0; i < tokens.length; ) { let n = 0; let keep = ""; for (let k = Math.min(3, tokens.length - i); k >= 1 && !n; k--) { const words = tokens.slice(i, i + k).map((t) => t.norm); if (set.has(privateNameHash(words.join(" ")))) n = k; else { const last = stripPossessive(words[k - 1]); if (last !== words[k - 1] && set.has(privateNameHash([...words.slice(0, -1), last].join(" ")))) { n = k; keep = text.slice(tokens[i + k - 1].end - 2, tokens[i + k - 1].end); } } } if (!n) { i++; continue; } out += text.slice(at, tokens[i].start) + NAME_WITHHELD + keep; at = tokens[i + n - 1].end; replaced++; i += n; } return { text: out + text.slice(at), replaced }; }