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:
@@ -129,6 +129,110 @@ def test_lm_death_aborts_cleanly():
|
||||
assert rep.notes, "notes must explain the abort"
|
||||
|
||||
|
||||
def test_fanout_expansion_and_join():
|
||||
"""The planner's fan-out: 'for each' over a list row becomes N entity
|
||||
questions + one join; the join sums the entity rows deterministically.
|
||||
Emili's run-08 case: flows of the three longest rivers of Italy."""
|
||||
import floop.machine as M
|
||||
from floop.state import AnswerType as AT, EvidenceRow
|
||||
|
||||
class FlowEngine:
|
||||
"""LIST questions get the 3-river ranked list; CONSTANT questions
|
||||
answer 1540 m3/s (every river); other kinds pass through."""
|
||||
def __init__(self, modules, charge, expert_kind):
|
||||
self.charge = charge
|
||||
def run(self, qid, question, parent_question, table, answer_type, streams=None):
|
||||
if answer_type == AT.LIST:
|
||||
payload = {"value": "3",
|
||||
"list_items": ["Po", "Adige", "Tiber"], "list_count": 3}
|
||||
else:
|
||||
payload = {"value": "1540 m3/s"}
|
||||
row = EvidenceRow(qid=qid, answer_type=answer_type, payload=payload,
|
||||
engine="stub", trace=[], provenance={}, verified=False, note="")
|
||||
if streams is not None:
|
||||
streams.output("EXPERT", f"value={payload['value']}")
|
||||
return row
|
||||
|
||||
M.LLMExpertEngine = FlowEngine
|
||||
try:
|
||||
fm = FakeModules(
|
||||
init_subs=[
|
||||
{"text": "What are the three longest rivers in Italy?",
|
||||
"deps": [], "answer_type": "LIST"},
|
||||
{"text": "What is the average annual water flow (discharge) "
|
||||
"for each of those three rivers?",
|
||||
"deps": [1], "answer_type": "CONSTANT"},
|
||||
{"text": "What is the sum of the water flows of the three rivers?",
|
||||
"deps": [2], "answer_type": "FUNCTION"},
|
||||
],
|
||||
route_fn=lambda q: "numeric",
|
||||
expert_value="1540")
|
||||
run = FloopRun("total flow of the three longest rivers of Italy?",
|
||||
budget(), modules=fm)
|
||||
rep = run.run()
|
||||
# the fan-out fired: 3 entity questions, one per river
|
||||
entity_qs = [q for q in run.stack.items if q.reason.startswith("fan-out of")]
|
||||
assert len(entity_qs) == 3, [q.text for q in run.stack.items]
|
||||
texts = {q.text for q in entity_qs}
|
||||
assert any("Po" in t for t in texts) and any("Tiber" in t for t in texts)
|
||||
# the join computed a deterministic sum: 3 x 1540 = 4620
|
||||
join_rows = [e for e in rep.evidence_rows if "logic:join" in e]
|
||||
assert join_rows and '"value": "4620"' in join_rows[0], join_rows
|
||||
# the 'for each' question is answered by the expansion, not an engine
|
||||
q2 = run.stack.items[2]
|
||||
assert q2.status == "answered" and "expanded" in q2.reason
|
||||
finally:
|
||||
M.LLMExpertEngine = _restore_llm_engine
|
||||
print("ok test_fanout_expansion_and_join")
|
||||
|
||||
|
||||
def test_fanout_respects_top_n_slice():
|
||||
"""'for each of those three rivers' over a 5-row list means top 3 by
|
||||
table order — never 5 entity questions."""
|
||||
fm = FakeModules(
|
||||
init_subs=[
|
||||
{"text": "What are the longest rivers in Italy?",
|
||||
"deps": [], "answer_type": "LIST"},
|
||||
{"text": "What is the flow for each of those three rivers?",
|
||||
"deps": [1], "answer_type": "CONSTANT"},
|
||||
],
|
||||
route_fn=lambda q: "knowledge",
|
||||
expert_value="100")
|
||||
# fake the LIST row: run's table gets a 5-item list answer for Q1
|
||||
run = FloopRun("goal?", budget(), modules=fm)
|
||||
# script Q1's answer: a route to knowledge; we inject the row manually by
|
||||
# patching the engine call — simpler: pre-build the list row via the
|
||||
# setop-free path: monkeypatch LLMExpertEngine.run to return a list row
|
||||
import floop.machine as M
|
||||
|
||||
class ListRowEngine:
|
||||
def __init__(self, modules, charge, expert_kind):
|
||||
self.charge = charge
|
||||
def run(self, qid, question, parent_question, table, answer_type, streams=None):
|
||||
from floop.state import AnswerType as AT, EvidenceRow
|
||||
row = EvidenceRow(
|
||||
qid=qid, answer_type=AT.LIST,
|
||||
payload={"value": "5", "list_items": ["Po", "Adige", "Tiber", "Reno", "Piave"],
|
||||
"list_count": 5},
|
||||
engine="knowledge", trace=[], provenance={}, verified=False, note="")
|
||||
if streams is not None:
|
||||
streams.output("EXPERT", "value=5 (list, 5 items)")
|
||||
return row
|
||||
|
||||
M.LLMExpertEngine = ListRowEngine
|
||||
try:
|
||||
rep = run.run()
|
||||
entity_qs = [q for q in run.stack.items if q.reason.startswith("fan-out of")]
|
||||
assert len(entity_qs) == 3, [q.text for q in run.stack.items]
|
||||
assert any("top 3" in n for n in rep.notes), rep.notes
|
||||
finally:
|
||||
M.LLMExpertEngine = _restore_llm_engine
|
||||
|
||||
|
||||
# keep a handle to restore the real engine after monkeypatching
|
||||
from floop.engine import LLMExpertEngine as _restore_llm_engine
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
fns = [v for k, v in sorted(globals().items()) if k.startswith("test_")]
|
||||
for fn in fns:
|
||||
|
||||
Reference in New Issue
Block a user