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.
This commit is contained in:
2026-10-18 19:49:24 +02:00
parent 6fcb3c0a86
commit e5482bd092
10 changed files with 390 additions and 34 deletions
+23 -3
View File
@@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS nodes (
CREATE TABLE IF NOT EXISTS edges (
id INTEGER PRIMARY KEY, src_node INTEGER, rel TEXT, dst_value TEXT,
dst_node INTEGER, quote_id INTEGER, extraction_confidence REAL,
origin_question TEXT,
ref_date TEXT, supersedes INTEGER, preferred INTEGER DEFAULT 0,
retracted INTEGER DEFAULT 0, retraction_reason TEXT, corroboration INTEGER DEFAULT 1
);
@@ -120,7 +121,8 @@ class KnowledgeStore:
def add_edge(self, src_name: str, rel: str, dst_value: str, quote_id: int,
src_sort: str = "CONCEPT", dst_node_name: str | None = None,
dst_sort: str = "CONCEPT", confidence: float = 0.8,
ref_date: str | None = None) -> int | None:
ref_date: str | None = None,
origin_question: str | None = None) -> int | None:
"""Add an edge, applying the four contradiction cases (KNOWLEDGE.md §2).
Returns the edge id, or None when the candidate was a duplicate
@@ -144,8 +146,8 @@ class KnowledgeStore:
(src, rel)).fetchall()
cur = self.db.execute(
"INSERT INTO edges(src_node, rel, dst_value, dst_node, quote_id, "
"extraction_confidence, ref_date, preferred) VALUES(?,?,?,?,?,?,?,0)",
(src, rel, dst_value, dst_node, quote_id, confidence, ref_date))
"extraction_confidence, origin_question, ref_date, preferred) VALUES(?,?,?,?,?,?,?,?,0)",
(src, rel, dst_value, dst_node, quote_id, confidence, origin_question, ref_date))
edge_id = cur.lastrowid
for other_id, other_val, other_date in others:
case = self._classify(new_val=dst_value, new_date=ref_date,
@@ -259,5 +261,23 @@ class KnowledgeStore:
(set_name,)).fetchall()
return sorted(r[0] for r in rows)
def set_origins(self) -> list[tuple[str, str]]:
"""(set_name, origin_question) for every membership set — the gate
matches a set to its own question, never to a different one."""
rows = self.db.execute(
"SELECT DISTINCT dst_value, origin_question FROM edges "
"WHERE rel='member_of' AND retracted=0 AND origin_question IS NOT NULL"
).fetchall()
return [(r[0], r[1]) for r in rows]
def set_provenance(self, set_name: str) -> tuple[str, str]:
"""(quote, url) of the first member edge of a set — provenance for
list answers served from the KG."""
row = self.db.execute(
"SELECT q.verbatim, q.url FROM edges e JOIN quotes q ON q.id=e.quote_id "
"WHERE e.rel='member_of' AND e.retracted=0 AND lower(e.dst_value)=lower(?) "
"LIMIT 1", (set_name,)).fetchone()
return (row[0], row[1]) if row else ("", "")
def close(self) -> None:
self.db.close()