knowledge: typed outputs for all 5 remaining signatures (block PR)

The typed-output migration completes: every knowledge signature now
declares its structure as Python types and dspy renders/parses it.

- TranslateSignature: queries/entities as list[str] — the
  'QUERIES:'/'ENTITIES:' line protocol and its prefix-scraping parser
  are gone
- AnswerFromKG: confidence is a local Confidence(str, Enum)
  (knowledge stays decoupled from floop)
- SelectTableSignature: chosen is TableChoice | None (index int,
  column, set_name) — 'NONE' string handling gone; origin_question
  dropped (it duplicated the input)
- SkimPromoteSignature: decisions as list[PromotionDecision]
  (table_index int, promote bool, column) — the 'idx: YES col' regex
  parser gone
- VerifyCountSignature: unchanged shape (quote str + NOQUOTE
  convention predates typed migration; kept — it is an answer-shaped
  field, not a format protocol)

Callers consume typed objects directly. Test fakes updated to the new
contracts (promote/store/guards).

VERIFIED:
- suite 14/14 green (84 tests)
- live E2E (ollama-cloud glm-5.3-flash, Italy rivers, fresh KG):
  correct answer 2014 m3/s (Po 1540 + Adige 235 + Tiber 239) with
  verified join; wire prompts show list[str]/TableChoice/Confidence/
  list[Fact] schemas rendered natively
This commit is contained in:
2026-10-22 15:11:33 +02:00
parent ce028b6973
commit 65d42d1942
5 changed files with 70 additions and 65 deletions
+10 -7
View File
@@ -13,6 +13,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
from knowledge.wiki import WikiPage, WikiTable, parse_tables
from knowledge.store import KnowledgeStore
from knowledge.extract import PromotionDecision
from knowledge.expert import KnowledgeExpert, KnowledgeConfig
@@ -32,15 +33,15 @@ def make_expert(store: KnowledgeStore) -> KnowledgeExpert:
class FakePromote:
"""Scripted skim_promote (dspy module shape): returns fixed decision."""
def __init__(self, decision: str):
self.decision_text = decision
"""Scripted skim_promote (dspy module shape): returns typed decisions."""
def __init__(self, decisions):
self.decisions = decisions
self.calls = 0
def __call__(self, **kw):
self.calls += 1
obj = type("P", (), {})()
obj.decision = self.decision_text
obj.decisions = self.decisions
return obj
@@ -81,7 +82,8 @@ def test_promotion_ingests_goal_table():
n = e._harvest_side_tables(lead_page(), None, "q", EVIDENCE)
assert n == 0 and len(e._skim_leads) == 1, "queued as lead, not ingested"
# bounded promotion: LLM says YES (column Mean discharge)
e.skim_promote = FakePromote("1: YES Mean discharge")
e.skim_promote = FakePromote([PromotionDecision(
table_index=1, promote=True, column="Mean discharge")])
charged = []
p = e._promote_leads(EVIDENCE, lambda: charged.append(1))
assert p == 1, "promoted table ingested"
@@ -100,7 +102,8 @@ def test_promotion_refuses_world_table():
e._current_goal_terms = ["total"] # no overlap with caption/header
n = e._harvest_side_tables(world_page(), None, "q", EVIDENCE)
assert n == 0 and len(e._skim_leads) == 1
e.skim_promote = FakePromote("1: YES Mean discharge") # LLM WRONG
e.skim_promote = FakePromote([PromotionDecision(
table_index=1, promote=True, column="Mean discharge")]) # LLM WRONG
p = e._promote_leads(EVIDENCE, lambda: None)
assert p == 0, "code disposes: zero entity overlap -> refused"
assert all(r.src != "amazon" for r in store.probe(["discharge"])), \
@@ -110,7 +113,7 @@ def test_promotion_refuses_world_table():
def test_no_leads_no_call():
e = make_expert(KnowledgeStore(":memory:"))
e.skim_promote = FakePromote("1: YES x")
e.skim_promote = FakePromote([])
charged = []
p = e._promote_leads(EVIDENCE, lambda: charged.append(1))
assert p == 0 and e.skim_promote.calls == 0 and not charged, \