Files
onibot e5482bd092 feat(logic): setop engine + set gate — the Europe count computed end-to-end
A) SET GATE: enumerations are answered only from membership sets, never from mention rows (the probe early-exit that served 'Armenia, Cyprus, Georgia, Turkey' to 'what countries are in Europe' is dead). Sets carry origin_question; the gate matches stem-Jaccard>=0.5 so a set is served to its own question only. B) SETOP ENGINE: logic's first real engine — a count over list rows is computed (difference/intersection/count over norm_name'd items, table markers 'Austria*' == 'Austria'), before ROUTE, zero LLM calls, verified=True because the computation IS the check. C) Catalog honesty: knowledge 'lists are ONE fetch', logic 'how-many over lists in evidence', floop 'NOT mere size'. Plus four substrate bugs the live runs exposed: LM-proxy prompt cache (instant answers are cache hits, never trust zero latency), _context rendering list rows as scalars ('E1: 50' routed 'those countries' to user), <br> concatenation in multi-value name cells, multi-line refs leaking into values ('franceincludes the 101 departments' -> France dropped from EU -> 24 not 23). Cold E2E: Q1 50 states ingested, Q2 27 EU members ingested, Q3 difference computed = 23, cited over E1/E2. 22/22 tests.
2026-10-18 19:49:24 +02:00

90 lines
3.7 KiB
Python

"""Setop tests: detection, compute, and the machine's guard — no LLM, no net.
Covers the two holes found in Emili's 2026-10-18 run:
A) the set gate: mention rows must not answer an enumeration
B) the setop engine: counts over list rows are computed, never routed
Run: uv run python tests/setops.py
"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # code/
from floop.setops import COUNT_RE, detect_setop, compute_setop, setop_row
from floop.state import AnswerType, EvidenceRow, EvidenceTable
def _list_row(eid_hint, items, qid="Q1"):
t = EvidenceTable()
row = EvidenceRow(
qid=qid, answer_type=AnswerType.LIST,
payload={"value": str(len(items)), "list_items": items,
"list_count": len(items)},
engine="knowledge", trace=[], provenance={}, verified=False)
return t.append(row)
def test_detection():
eu = _list_row("e", ["Germany", "France", "Italy"])
world = _list_row("w", ["Germany", "France", "Italy", "Norway", "Switzerland",
"United Kingdom", "Turkey"])
table = EvidenceTable()
table.rows().extend # noop, just to silence linters
for r in (eu, world):
table.append(r)
# count question + two list rows + negation -> difference
spec = detect_setop("How many countries in the European continent are not members of European Union?", table)
assert spec is not None and spec["op"] == "difference", spec
# no negation -> intersection
spec = detect_setop("How many countries are members of both?", table)
assert spec is not None and spec["op"] == "intersection", spec
# one list row -> plain count
one = EvidenceTable()
one.append(eu)
spec = detect_setop("How many countries are in the list?", one)
assert spec is not None and spec["op"] == "count", spec
# not a count question -> None (falls through to the router)
assert detect_setop("Which countries are not members?", table) is None
# count question but no list rows -> None
empty = EvidenceTable()
assert detect_setop("How many countries?", empty) is None
print("ok test_detection")
def test_compute_difference():
eu = _list_row("e", ["Germany", "France", "Italy", "Austria*"])
all_europe = _list_row("w", ["Germany", "France", "Italy", "Austria",
"Norway", "Switzerland", "United Kingdom",
"Turkey"])
# table markers ('Austria*') must not split the element from 'Austria':
# norm_name strips them, so 'Austria*' is IN the EU set -> difference
# excludes it. Expected: Norway, Switzerland, UK, Turkey = 4.
spec = {"op": "difference", "a": all_europe, "b": eu}
out = compute_setop(spec)
expected = {"norway", "switzerland", "united kingdom", "turkey"}
assert set(out["list_items"]) == expected, out
assert out["value"] == "4" and out["list_count"] == 4
print("ok test_compute_difference")
def test_setop_row_verified():
eu = _list_row("e", ["Germany", "France"])
all_e = _list_row("w", ["Germany", "France", "Norway"])
spec = {"op": "difference", "a": all_e, "b": eu}
row = setop_row(spec, "Q3", AnswerType.CONSTANT)
assert row.verified is True, "the computation IS the verification"
assert row.engine == "logic:setop"
assert row.payload["value"] == "1"
assert "setop difference" in row.trace[0]
assert row.provenance["sources"] == ["E1", "E2"] or len(row.provenance["sources"]) == 2
print("ok test_setop_row_verified")
if __name__ == "__main__":
test_detection()
test_compute_difference()
test_setop_row_verified()
print("\n3 setop tests passed")