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
+15 -30
View File
@@ -269,18 +269,10 @@ class KnowledgeExpert:
if charge_llm:
charge_llm()
pred = self.translate(question=question, context=context)
q_raw = (pred.queries or "").strip()
queries = [q.strip() for q in
next((ln.split(":", 1)[1] for ln in q_raw.splitlines()
if ln.strip().upper().startswith("QUERIES")), q_raw).split(",")
if q.strip()][:2]
queries = [q.strip() for q in (pred.queries or []) if q.strip()][:2]
if not queries:
queries = [" ".join(kws[:3])]
e_raw = (pred.entities or "").strip()
entity_terms = [t.strip() for t in
next((ln.split(":", 1)[1] for ln in e_raw.splitlines()
if ln.strip().upper().startswith("ENTITIES")), e_raw).split(",")
if t.strip()][:4]
entity_terms = [t.strip() for t in (pred.entities or []) if t.strip()][:4]
self._log(f"translate -> queries {queries} entities {entity_terms}", think=True)
# search ALL queries first; merge + dedupe candidates; rank GLOBALLY
@@ -647,22 +639,19 @@ class KnowledgeExpert:
if charge_llm:
charge_llm()
pred = self.select_table(question=question, tables=rendered)
idx_raw = (pred.table_index or "").strip()
if not idx_raw or idx_raw.upper() in ("NONE", "N/A", "0"):
return None
try:
idx = int(re.findall(r"\d+", idx_raw)[0])
except (ValueError, IndexError):
chosen = pred.chosen
if chosen is None:
return None
idx = chosen.table_index
table = next((t for t in page.tables if t.idx == idx), None)
if table is None:
return None
page._list_table_idx = idx # the harvest skips the LIST answer's table
column = (pred.column or "").strip()
column = chosen.column.strip()
col_i = next((i for i, h in enumerate(table.header)
if column.casefold() in h.casefold()), 0)
set_name = (pred.set_name or "").strip() or question[:60]
origin_question = (pred.origin_question or "").strip() or question
set_name = chosen.set_name.strip() or question[:60]
origin_question = question
# INGEST ALL ROWS: one membership edge per row, quote = the full
# stored row line, containment against the stored table text
doc_id = self.store.add_document(
@@ -888,16 +877,12 @@ class KnowledgeExpert:
entities=", ".join(expected[:8]),
tables="\n".join(rendered))
promoted = 0
for line in (pred.decision or "").strip().splitlines():
m = re.match(r"\s*(\d+)\s*:\s*(YES|NO)\s*(.*)$", line.strip(),
flags=re.I)
if not m:
continue
idx, verdict, col = int(m.group(1)), m.group(2).upper(), m.group(3).strip()
got = lookup.get(idx)
if verdict != "YES" or got is None:
for d in (pred.decisions or []):
got = lookup.get(d.table_index)
if not d.promote or got is None:
continue
page, t = got
col = (d.column or "").strip()
ent_i = next((i for i, h in enumerate(t.header)
if h.casefold() in _ENTITY_COL), 0)
val_i = next((i for i, h in enumerate(t.header)
@@ -906,13 +891,13 @@ class KnowledgeExpert:
continue
overlap, nm = self._entity_overlap_count(t, ent_i, expected)
if expected and overlap == 0 and nm >= 2:
self._log(f"skim promote: table {idx} ({page.title}): LLM said "
self._log(f"skim promote: table {d.table_index} ({page.title}): LLM said "
f"YES but {overlap}/{nm} entity overlap — refused",
think=True)
continue
merged = self._ingest_scalar_table(page, t, ent_i, val_i)
if merged:
self._log(f"skim promote: {merged} rows of table {idx} "
self._log(f"skim promote: {merged} rows of table {d.table_index} "
f"({page.title}) ingested (LLM-proposed)", think=True)
promoted += 1
self._skim_leads = [] # consumed: bounded, never re-asked
@@ -1053,5 +1038,5 @@ class KnowledgeExpert:
"quote": pred.quote.strip(),
"url": next((r.url for r in rows if r.quote and pred.quote.strip()
and r.quote.strip() in pred.quote.strip()), rows[0].url),
"confidence": pred.confidence.strip().lower(),
"confidence": str(pred.confidence.value),
}