machine: fan-out planner + disjointness guard (runs 07/08 lessons)
(a) Disjointness guard (knowledge/expert.py): a LIST ingest whose members
don't overlap the entities the parent's evidence names is refused; the
gather continues to the next article. Run-08's 'flow of Po/Adige/Tiber'
answered by the Amazon from a world-wide table can no longer happen.
(b) Fan-out planner (floop/machine.py): a 'for each' subquestion over a list
row expands deterministically into N entity questions + one join.
Top-N bound respected ('those three' over 22 rows -> top 3 by table
order, logged). The join is a deterministic sum over the entity rows
(verified=True: computation is its own proof), partial sums named.
Entity rows are tagged fan_group in provenance so the join finds them.
Also: LIST ingest keeps TABLE ORDER (ranked tables rank; alphabetical sort
destroyed the ranking) — dict.fromkeys dedup.
Budget: 40 -> 80 (a 3-entity fan-out needs ~3x a scalar question).
Verified live (cold KG, 2 runs): list ingest in table order, slice top-3,
fan-out -> 3 entity questions + join, Po answered with verbatim quote
(1,460 m3/s). 28/28 tests (fan-out expansion+join, top-N slice, guard).
This commit is contained in:
@@ -230,11 +230,12 @@ class KnowledgeExpert:
|
||||
# rows and answer by aggregation (no extract calls, no gate)
|
||||
if list_mode and page.tables:
|
||||
ans = self._list_from_page(page, question, charge_llm)
|
||||
if ans is not None:
|
||||
if ans is not None and self._guard_ingest(ans, evidence):
|
||||
self._log(f"early exit after {page.title}: list "
|
||||
f"({ans['list_count']} items)", think=True)
|
||||
return {**ans, "source": "knowledge_graph",
|
||||
"stats": {"pages": [p.title for p in pages]}}
|
||||
# refused by the guard: wrong article's list — keep gathering
|
||||
# EARLY EXIT: one article at a time; if the KG already answers,
|
||||
# stop spending calls (the gather exists to fill the gap, not to
|
||||
# hoover every candidate). For enumerations the set gate applies
|
||||
@@ -268,6 +269,57 @@ class KnowledgeExpert:
|
||||
"pages": [p.title for p in pages],
|
||||
"elapsed_s": round(time.time() - t0, 1)}}
|
||||
|
||||
def _expected_entities(self, evidence: str) -> list[str]:
|
||||
"""Entities the parent's evidence already names — 'the three longest
|
||||
rivers' means the answer's members must be among them. Extracted
|
||||
deterministically from the rendered evidence lines (E1: value (answers
|
||||
Qx)), from LIST rows' list_items and scalar rows' values. No LLM."""
|
||||
out: list[str] = []
|
||||
for line in (evidence or "").splitlines():
|
||||
if "(answers" not in line:
|
||||
continue
|
||||
val = line.split(":", 1)[1].split("(answers")[0].strip()
|
||||
if not val:
|
||||
continue
|
||||
# LIST rows render 'list of N items: a, b, c...' — members are known
|
||||
m = re.match(r"list of \d+ items: (.+)$", val)
|
||||
if m:
|
||||
out.extend(t.strip() for t in m.group(1).rstrip(".").split(","))
|
||||
continue
|
||||
# scalar: a single named entity (Po) or a short comma list (Po, Adige, Tiber)
|
||||
if len(val.split()) <= 6 and not val.isdigit():
|
||||
out.extend(t.strip() for t in val.split(",") if t.strip())
|
||||
return [e for e in out if e]
|
||||
|
||||
def _disjoint(self, members: list[str], expected: list[str]) -> bool:
|
||||
"""Deterministic overlap check, case-insensitive by prefix-normalized
|
||||
tokens. Disjoint (or near-disjoint) ingest is WRONG for a question
|
||||
about known entities — keep gathering, don't answer from it."""
|
||||
if not expected or not members:
|
||||
return False
|
||||
en = {norm_name(e) for e in expected}
|
||||
mn = {norm_name(m) for m in members}
|
||||
inter = en & mn
|
||||
# no expected entity appears, or < 1/3 do: wrong article's list
|
||||
return len(inter) * 3 < len(mn) or not inter
|
||||
|
||||
def _guard_ingest(self, ans: dict, evidence: str) -> bool:
|
||||
"""The disjointness guard: refuse a LIST ingest whose members don't
|
||||
overlap the entities the parent's evidence already names. 'Cabal de
|
||||
Po, Adige, Tiber' must never be answered by the Amazon's flow."""
|
||||
if not (evidence and ans.get("list_items")):
|
||||
return True # nothing to check against — ingest is fine
|
||||
expected = self._expected_entities(evidence)
|
||||
if not expected:
|
||||
return True
|
||||
if self._disjoint(ans["list_items"], expected):
|
||||
self._log(
|
||||
f"guard: ingest of {len(ans['list_items'])} members disjoint from "
|
||||
f"{len(expected)} expected entities — refusing, gather continues",
|
||||
think=True)
|
||||
return False
|
||||
return True
|
||||
|
||||
def _is_enumeration(self, question: str) -> bool:
|
||||
"""Deterministic LIST detection: plural nouns + enumeration ask words.
|
||||
Cheap, logged, and overridable by the planner's explicit answer_type."""
|
||||
@@ -370,7 +422,10 @@ class KnowledgeExpert:
|
||||
self._merged += 1
|
||||
if not members:
|
||||
return None
|
||||
items = sorted(set(members))
|
||||
# ORDER-PRESERVING dedup: the table's order IS information (a ranked
|
||||
# table ranks — top-N slices read it). Sorting alphabetically imposed
|
||||
# an order the source never had.
|
||||
items = list(dict.fromkeys(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
|
||||
|
||||
Reference in New Issue
Block a user