"""The two-pass extraction (Pass 1 claims + verbatim quotes, Pass 2 typing). The single-pass EXTRACT asked slow local models for a giant JSON array of typed Facts with verbatim quotes + temporal restrictions + sibling order assertions — the output budget behind the 40-minute arithmetic. Two-pass shortens every output: Pass 1 lists the claims, each with its VERBATIM quote (extracted while the text is in front of the model); Pass 2 types ONE claim at a time — the model sees ONLY the claim and returns FactNoQuote; code composes the Fact the store already eats: Fact = FactNoQuote + verbatim (owner 1/11: the VERBATIM is obtained before, the TYPE model never sees nor rewrites it). Orders: NEVER produced here — one claim per call has no siblings to reference, and the TYPE schema (FactNoQuote) has no orders field at all. The legacy single-pass path (and table frags) keeps them. """ from __future__ import annotations from .extract import Claim, Fact def claims_from(expert, entities: str, heading: str, text: str, charge_llm=None) -> list[Claim]: """Pass 1: the chunk's claims, each with its VERBATIM quote — the quote is extracted HERE, in the same call that reads the text. The list is made honest by CODE only: exact repeats are dropped (live finding 26/10: glm sometimes lists its whole list twice, and thinking leakage leaks in as list items). Dedup is case- and whitespace-insensitive on the CLAIM text; the FIRST occurrence's (claim, verbatim) is kept. NO cap, NO semantic filter: completeness beats budget (owner ruling 26/10) — every unique claim gets its TYPE call. """ if charge_llm: charge_llm() pred = expert.claims(entities=entities, heading=heading, text=text) seen: dict[str, Claim] = {} for c in (pred.claims or []): claim = (getattr(c, "claim", "") or "").strip() verbatim = (getattr(c, "verbatim", "") or "").strip() if claim: key = " ".join(claim.lower().split()) if key not in seen: seen[key] = Claim(claim=claim, verbatim=verbatim) return list(seen.values()) def fact_from_claim(expert, claim: str, verbatim: str, charge_llm=None): """Pass 2: ONE claim -> one typed Fact (or None, honestly declined). TYPING ONLY: the model sees ONLY the claim — no text, no verbatim (owner 1/11: the VERBATIM is Pass 1's and the TYPE model never sees nor rewrites it). Code COMPOSES the store's Fact: FactNoQuote + verbatim (LLMs propose, code decides) — the quote is Pass 1's span BY CONSTRUCTION, and the store's checks validate it downstream as before (a temporal field not contained in the quote rejects the candidate; live finding 26/10: without grounding, 69/70 invented quotes were rejected). No orders: no siblings by construction, and FactNoQuote has no orders field at all. """ if charge_llm: charge_llm() pred = expert.typeclaim(claim=claim) nq = getattr(pred, "fact", None) if nq is None: return None return Fact(**nq.model_dump(), quote=verbatim)