// What code checks of a local sweep's item once its quote is verbatim (demo // epic D2): the text around the quote in its transcript window, and whether // the item is ON the question (item 2's gate). // // D0 (11 Nov 2026): with nothing on the subject read, the extractor still // marked 100-200 items on topic, and grouping named sections for them; 39 of // the Acolyte run's 40 kept claims were off the question. The model's own // `on_topic` is advisory: an item is kept only when its quote, or the 300 // characters before it, holds one of the question's rare terms (or the topic // the model named) — what code can see. import { strictNormalize } from "../../../citations/verify"; type Win = readonly (readonly [number, string])[]; // The window's words, each with its normalized tokens (a raw word may // normalize to none, or to two: "rock-bottom"). function tokensOf(win: Win): { raw: string[]; norm: string[]; owner: number[] } { const raw: string[] = []; const norm: string[] = []; const owner: number[] = []; for (const [, text] of win) { for (const w of text.split(/\s+/)) { if (!w) continue; raw.push(w); for (const t of strictNormalize(w).split(" ")) { if (!t) continue; norm.push(t); owner.push(raw.length - 1); } } } return { raw, norm, owner }; } export type QuoteContext = { before: string; after: string }; // The transcript's own words before and after a verbatim quote in its window // (at most `before` / `after` characters, cut at a word); null when the quote // is not in it. export function quoteContext(win: Win, quote: string, o: { before?: number; after?: number } = {}): QuoteContext | null { const q = strictNormalize(quote).split(" ").filter(Boolean); if (!q.length) return null; const { raw, norm, owner } = tokensOf(win); let at = -1; for (let i = 0; i + q.length <= norm.length && at < 0; i++) { let ok = true; for (let j = 0; j < q.length && ok; j++) ok = norm[i + j] === q[j]; if (ok) at = i; } if (at < 0) return null; const first = owner[at]!; const last = owner[at + q.length - 1]!; const take = (words: string[], max: number, fromEnd: boolean) => { const out: string[] = []; let n = 0; const list = fromEnd ? [...words].reverse() : words; for (const w of list) { if (n + w.length + (out.length ? 1 : 0) > max) break; out.push(w); n += w.length + (out.length > 1 ? 1 : 0); } return (fromEnd ? out.reverse() : out).join(" "); }; return { before: take(raw.slice(0, first), o.before ?? 400, true), after: take(raw.slice(last + 1), o.after ?? 150, false) }; } export const GATE_CHARS = 300; // The question's term (or the topic) the item shows, in its quote or the // GATE_CHARS before it; null when it shows none — the item is off the question. export function onTopicBy(o: { quote: string; before: string; terms: readonly string[]; topic?: string }): string | null { const before = o.before.length > GATE_CHARS ? o.before.slice(o.before.length - GATE_CHARS).replace(/^\S*\s/, "") : o.before; const text = ` ${strictNormalize(`${before} ${o.quote}`)} `; for (const t of [...o.terms, ...(o.topic ? [o.topic] : [])]) { const k = strictNormalize(t); if (k && text.includes(` ${k} `)) return t; } return null; } // A full sweep needs this many items on the question to be a report; fewer is // "nothing usable found" (never padded). export const MIN_KEPT = 3; const HEDGES = ["allegedly", "reportedly", "claims", "claimed", "may", "might", "maybe", "if", "probably", "likely", "i think", "apparently", "supposedly", "rumor", "rumour"]; // A hedge is kept when the sentence says it in its own words: "I think" is // "he thinks"; "maybe" is "may", "might" or "perhaps". const MAYBE = ["maybe", "may", "might", "perhaps", "possibly", "could"]; const HEDGE_KEPT_BY: Record = { "i think": ["i think", "thinks?", "believes?", "feels?", "guess(?:es)?", "suspects?", "in his (?:view|opinion)"], maybe: MAYBE, may: MAYBE, might: MAYBE, probably: ["probably", "likely", "thinks?", "believes?", "expects?"], likely: ["probably", "likely", "thinks?", "believes?", "expects?"], allegedly: ["allegedly", "reportedly", "claims?", "claimed", "accused", "accusations?"], reportedly: ["reportedly", "allegedly", "reports?", "claims?", "according to"], apparently: ["apparently", "reportedly", "seems?", "appears?"], supposedly: ["supposedly", "allegedly", "reportedly", "claims?"], }; const NEGATIONS = /\b(never|not|no|nobody|nothing|none|neither|nor|cannot|without)\b|n't\b/i; // The drafted sentence against its quote: a negation or hedge lost or added, // a number not in the quote. Flags, never drops (the meaning check's fix of an // overstated sentence must pass it). export function lintClaim(claim: string, quote: string): string[] { const flags: string[] = []; const qn = quote.toLowerCase(); const cn = claim.toLowerCase(); if (NEGATIONS.test(qn) && !NEGATIONS.test(cn)) flags.push("negation: the quote has a not/never/no the claim does not"); if (!NEGATIONS.test(qn) && NEGATIONS.test(cn)) flags.push("negation: the claim has a not/never/no the quote does not"); for (const h of HEDGES) { const re = new RegExp(`\\b${h.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`); const kept = new RegExp(`\\b(${(HEDGE_KEPT_BY[h] ?? [h]).join("|")})\\b`); if (re.test(qn) && !kept.test(cn)) { flags.push(`hedge dropped: the quote says “${h}”`); break; } } for (const num of claim.match(/\d[\d,.]*/g) ?? []) { if (!quote.includes(num.replace(/[.,]+$/, ""))) { flags.push(`number ${num} is not in the quote`); break; } } return flags; }