knowledge: PR-1 skim surfaces — read articles in passing (KNOWLEDGE §7)

The Italy run (Run G) failed with the data in hand: Q1's LIST ingest
early-exited on the length table and discarded the same article's 27-row
discharge table; the Adige/Tiber discharges live in infobox templates,
invisible to both the plain extract and parse_tables; and {{cvt|50|m3/s}}
cleaned to "50" — units silently dropped everywhere.

PR-1 builds the skim's surfaces and signals (deterministic, zero LLM):

- knowledge/skim.py: skim_article() builds a per-article SkimSheet —
  section headings with term hits, table identity (caption/headers/row
  count), infobox parameter keys with resolved quantity values, link
  candidates from goal-relevant regions — from material fetch_rich
  already downloads.
- quantity-unit patterns (find_quantities): "50 m3/s" is a
  vocabulary-independent signal; the Italy discharge section's heading
  says "above 50 m3/s", never "discharge" — this finds what keyword
  search cannot.
- wiki.resolve_convert: {{convert|N|unit}} resolves to "N unit" inside
  _clean_cell BEFORE the generic template pass (which kept only the first
  argument and dropped the unit). Captions and cells keep units.
- WikiPage.wikitext: fetch_rich stores the raw source (infobox surface).
- _process_page: gated chunks are RANKED by skim signals (question + goal
  terms + quantity patterns) instead of first-N in document order.

Live smoke (real Wikipedia, no LLM): the Italy sheet shows table 2
"Rivers of Italy above 50 m3/s of average flow at the mouth", 27 rows,
quantities 50/350/239/235/187/170 m3/s; the Adige sheet shows infobox
discharge1_avg = 235 m3/s.

Tests: 6 new (knowledge/tests/skim.py), suite 35/35 green.
PR-2 (opportunistic harvest + seen-index) and PR-3 (notices to the
machine) follow.
This commit is contained in:
2026-10-20 14:52:38 +02:00
parent b85f196cbe
commit 73fe5cc8e1
4 changed files with 395 additions and 9 deletions
+37 -7
View File
@@ -23,6 +23,7 @@ import dspy
from .checks import check_candidate, quote_containment
from .chunker import chunk_page
from .skim import find_quantities, skim_article
from .extract import (AnswerFromKG, ExtractSignature, SelectTableSignature,
TranslateSignature, VerifyCountSignature, parse_facts)
from .store import KnowledgeStore, norm_name
@@ -132,6 +133,8 @@ class KnowledgeExpert:
if evidence:
context += (chr(10) * 2 if context else "") + f"Evidence:\n{evidence}"
kws = self._keywords(question)
goal_terms = self._keywords(parent_question) if parent_question else []
ev_entities = [e for e in self._expected_entities(evidence or "")][:8]
# LIST activation (moved up: the set gate needs it before the probe)
list_mode = (str(answer_type or "").upper() == "LIST"
or self._is_enumeration(question))
@@ -230,6 +233,14 @@ class KnowledgeExpert:
self._log(f"fetch failed: {title}: {e}")
continue
pages.append(page)
# SKIM (PR-1): the structural read, in passing — question AND goal
# oriented; surfaces + signals, zero LLM. The sheet drives chunk
# ranking in _process_page (PR-2 wires its harvest + notices).
sheet = skim_article(page, question_terms=kws, goal_terms=goal_terms,
evidence_entities=ev_entities)
self._log(f"skim {page.title}: {len(sheet.tables)} tables, "
f"{len(sheet.infobox_quantities)} infobox quantities, "
f"{len(sheet.link_candidates)} link candidates", think=True)
# LIST path: one SELECT TABLE call per article; on hit, ingest ALL
# rows and answer by aggregation (no extract calls, no gate)
if list_mode and page.tables:
@@ -244,7 +255,8 @@ class KnowledgeExpert:
# stop spending calls (the gather exists to fill the gap, not to
# hoover every candidate). For enumerations the set gate applies
# here too: mention rows never answer a list question.
self._process_page(page, question, kws, charge_llm)
self._process_page(page, question, kws, charge_llm,
goal_terms=goal_terms)
if list_mode:
hit = self._set_gate_hit(probe_seeds, question)
if hit is not None:
@@ -460,14 +472,32 @@ class KnowledgeExpert:
}
def _process_page(self, page: WikiPage, question: str, kws: list[str],
charge_llm) -> int:
"""Chunk -> gate -> extract -> check -> merge for ONE page. Returns
number of candidates merged this page. Counters ACCUMULATE on self."""
charge_llm, goal_terms: list[str] | None = None) -> int:
"""Chunk -> gate -> RANK (skim) -> extract -> check -> merge for ONE
page. Returns number of candidates merged this page. Counters
ACCUMULATE on self.
PR-1: chunks are RANKED by skim signals (question terms + goal terms
+ quantity-unit patterns) instead of taking the first N in document
order — the Italy run burned its extract slots on tangential chunks
while the discharge table sat later in the document. A chunk without
ANY signal is skipped for free."""
chunks = chunk_page(page.text, target=self.cfg.chunk_chars_target,
cap=self.cfg.chunk_chars_cap)
gated = [c for c in chunks if self._gate(c, kws)][: self.cfg.max_chunks_extracted]
self._log(f"{page.title}: {len(chunks)} chunks, {len(gated)} gated in")
for chunk in gated:
gated = [c for c in chunks if self._gate(c, kws)]
if goal_terms:
from .store import stem
def rank(c) -> tuple[int, int]:
low = c.text.casefold()
sig = sum(1 for t in set(kws + goal_terms)
if t and (t in low or stem(t) in low))
return (sig + 2 * len(find_quantities(c.text)), -c.idx)
gated.sort(key=rank, reverse=True)
capped = gated[: self.cfg.max_chunks_extracted]
self._log(f"{page.title}: {len(chunks)} chunks, {len(gated)} gated, "
f"{len(capped)} extracted (skim-ranked)", think=True)
for chunk in capped:
if charge_llm:
charge_llm()
raw = self.extract(question=question, heading=chunk.heading_path, text=chunk.text)