knowledge: answer-level count corroboration (DESIGN §12) — LIST rows verified via prose

- VerifyCountSignature: LLM finds ONE verbatim sentence in the article prose
  (tables excluded by construction) stating the set's count
- _verify_count: code checks containment + digit equality — no LLM compares
  numbers; NOQUOTE -> count_corroborated=false (honest, one call, never loops)
- row verified <=> complete_per_source AND count_corroborated
- DESIGN §12 documents now vs deferred (extension, criteria, cross-article)
- live: EU question cold — E1 (50) + E2 (27) both corroborated with quotes,
  23 computed; 25/25 tests
This commit is contained in:
2026-10-19 14:47:18 +02:00
parent b14915079e
commit 852db78c99
6 changed files with 235 additions and 10 deletions
+50 -7
View File
@@ -21,10 +21,10 @@ from dataclasses import dataclass
import dspy
from .checks import check_candidate
from .checks import check_candidate, quote_containment
from .chunker import chunk_page
from .extract import (AnswerFromKG, ExtractSignature, SelectTableSignature,
TranslateSignature, parse_facts)
TranslateSignature, VerifyCountSignature, parse_facts)
from .store import KnowledgeStore, norm_name
from .wiki import WikipediaClient, WikiPage
@@ -56,6 +56,7 @@ class KnowledgeExpert:
self.answer = dspy.Predict(AnswerFromKG)
self.translate = dspy.Predict(TranslateSignature)
self.select_table = dspy.Predict(SelectTableSignature)
self.verify_count = dspy.Predict(VerifyCountSignature)
self._checked = 0
self._merged = 0
@@ -277,6 +278,38 @@ class KnowledgeExpert:
and w not in ("states?", "does", "was", "has")]
return bool(words & ask) and len(plurals) >= 1
def _verify_count(self, set_name: str, count: int, prose: str,
charge_llm) -> dict | None:
"""Answer-level CHECK: corroborate the list COUNT from article prose
that is NOT the source table.
Law split: the LLM finds language (one verbatim sentence stating the
count); fixed code decides validity — the sentence must be contained
in the prose AND state the same count as `count` digits. No LLM ever
compares numbers. Returns a verification record or None (no
corroborating sentence found — honest, not an error)."""
if not prose or count <= 0:
return None
if charge_llm:
charge_llm()
pred = self.verify_count(set_name=set_name, count=str(count),
text=prose[: self.cfg.chunk_chars_cap])
q = (pred.quote or "").strip()
if not q or q.upper() in ("NOQUOTE", "NONE", "N/A"):
return None
if not quote_containment(q, prose):
self._log(f"verify: quote not contained in prose — rejected")
return None
# deterministic number comparison: the digits the sentence states
# must equal the count we computed from the rows
nums = {int(n.replace(",", "")) for n in re.findall(r"[\d,]{1,7}", q)
if n.replace(",", "").isdigit()}
if count in nums:
return {"count_corroborated": True, "quote": q}
self._log(f"verify: corroborating sentence states {nums}, not {count} "
f"— numbers disagree")
return None
def _list_from_page(self, page: WikiPage, question: str, charge_llm) -> dict | None:
"""The LIST path: SELECT TABLE (LLM picks table+column from metadata,
never the rows) -> ingest ALL rows as membership edges -> aggregate.
@@ -340,17 +373,27 @@ class KnowledgeExpert:
items = sorted(set(members))
self._log(f"LIST ingest: {len(items)} members of '{set_name}' from "
f"table {idx} ({table.total_rows} rows)", think=True)
# ANSWER-LEVEL CHECK: corroborate the count from prose OUTSIDE the
# table (the extract text is the article prose; tables are separate).
# One bounded call; failure = honest 'no corroboration', never a loop.
verify = self._verify_count(set_name, len(items), page.text, charge_llm)
coverage = {"source": page.title, "table": idx,
"rows_total": table.total_rows,
"rows_ingested": len(members),
"complete_per_source": len(members) == table.total_rows}
if verify:
coverage["count_corroborated"] = True
coverage["corroboration_quote"] = verify["quote"]
else:
coverage["count_corroborated"] = False
return {
"value": str(len(items)),
"list_items": items,
"list_count": len(items),
"quote": table.rows[0] and " | ".join(table.rows[0]),
"url": page.url,
"confidence": "high",
"coverage": {"source": page.title, "table": idx,
"rows_total": table.total_rows,
"rows_ingested": len(members),
"complete_per_source": len(members) == table.total_rows},
"confidence": "high" if verify else "medium",
"coverage": coverage,
}
def _process_page(self, page: WikiPage, question: str, kws: list[str],