knowledge: deterministic ambiguity audit for office questions (run J)

Run J answered 'What office is Pedro Sanchez associated with as
President?' with Socialist International — a literally defensible
reading, but the KG ALSO held prime minister of Spain and
secretary-general of PSOE, and the machine never learned they existed.
The fix is not prompt tuning: it is a data type the pipeline could not
express — 'this answer is one of several defensible readings'.

Layer 1 (this PR) — knowledge/ambiguity.py, zero LLM, zero network:
- GATE: role_in_text(question) — only questions naming an office-class
  role (president, prime minister, secretary general, chancellor, ...)
  enter the audit. 'second most populated country in European Union'
  and 'three longest rivers in Italy' never even run it.
- ref_entity: the question's proper-noun run, broken at lowercase words
  and role words — 'Pedro Sanchez' (not 'President'), 'France' in
  'Who is the President of France?'.
- audit_role_multiplicity: rows about the entity whose role lives in
  the relation OR the object (extraction shape varies); dates and bare
  numbers never become alternatives; same office in two shapes merges
  by token containment (quote kept, corroboration summed); the PICKED
  reading is excluded from the report. Fires only at >= 2 alternatives.
- _maybe_ambig wires the audit at the three scalar return paths of
  answer_question; a clean answer is byte-identical (gate is cheap and
  the single-reading path changes nothing).

Propagation up (flag, never choose):
- payload carries {ambiguous, axis, alternatives[{value, role, quote,
  url}]}; provenance note says the choice among readings is NOT
  verified.
- OUTPUT EXPERT line: 'Q1 -> ANSWER value=... AMBIGUOUS ([prime
  minister] Spain; [secretary-general] PSOE) quote=...'.
- machine: run notes carry the axis + other readings; the evidence
  render shows them so the ANSWER step (and later the user) can decide.

Tests: knowledge/tests/ambiguity.py — the two agreed positives (both
Pedro phrasings), the two agreed counterexamples (silent), single
office, dates-as-values, duplicate shapes, role-in-object, wiring
(expert flags + clean untouched), engine log/payload/provenance,
machine notes/render. Suite 70/70. Live smoke: the full pipeline over
a run-J-shaped page flags the 3 offices; the rivers question over the
same KG stays silent.
This commit is contained in:
2026-10-21 17:42:24 +02:00
parent 4506b4067a
commit a43d827098
5 changed files with 531 additions and 6 deletions
+19 -2
View File
@@ -130,6 +130,12 @@ class KnowledgeEngine:
src = ans.get("source", "wikipedia_miss")
value = ans.get("value", "UNKNOWN")
payload = {"value": value}
# Layer-1 ambiguity flag (run J): the answer is ONE defensible
# reading; the payload carries the others for the layers above
if ans.get("ambiguous"):
payload["ambiguous"] = True
payload["axis"] = ans.get("axis", "")
payload["alternatives"] = ans.get("alternatives", [])
if ans.get("quote"):
payload["quote"] = ans["quote"]
if ans.get("url"):
@@ -147,8 +153,11 @@ class KnowledgeEngine:
if skim_notices:
payload["skim"] = skim_notices[:3]
prov = {"expert": "knowledge", "source": src,
"note": ("source-verified: verbatim containment in trusted doc"
if ans.get("quote") else "no quote — unverified")}
"note": ("ambiguous: several office readings exist — the "
"choice among them is not verified"
if ans.get("ambiguous") else
("source-verified: verbatim containment in trusted doc"
if ans.get("quote") else "no quote — unverified"))}
# LIST rows: the answer-level check corroborates the count outside the
# source table; combined with per-source completeness it makes the
# row verified (E3-style verified semantics, first non-stub use).
@@ -174,6 +183,14 @@ class KnowledgeEngine:
f"{len(items)} members: {shown}) quote="
f"{(ans.get('quote') or '')[:60]} url="
f"{ans.get('url') or ''} [{src}]")
elif ans.get("ambiguous"):
alts = "; ".join(f"[{a.get('role')}] {a.get('value')}"
for a in (ans.get("alternatives") or [])[:3])
streams.output("EXPERT",
f"{qref} -> ANSWER value={value} "
f"AMBIGUOUS ({alts}) quote="
f"{(ans.get('quote') or '')[:60]} url="
f"{ans.get('url') or ''} [{src}]")
else:
streams.output("EXPERT",
f"{qref} -> ANSWER value={value} quote="
+16 -1
View File
@@ -451,6 +451,14 @@ class FloopRun:
# derived answers render CONTENT, not counts alone
for n in (row.payload.get("skim") or [])[:2]:
self.notes.append(f"{q.qid}: skim: {n[:150]}")
# Layer-1 ambiguity audit (run J): the expert flagged several office
# readings — the report must carry them, the machine must not choose
if row.payload.get("ambiguous"):
alts = row.payload.get("alternatives") or []
self.notes.append(
f"{q.qid}: AMBIGUOUS: {row.payload.get('axis')} — "
f"other readings: " + "; ".join(
f"{a.get('value')} ({a.get('role')})" for a in alts[:3]))
self.table.append(row)
if str(row.payload.get("value", "")).strip().upper() == "UNKNOWN":
q.status = "failed"
@@ -468,7 +476,14 @@ class FloopRun:
if isinstance(r.payload.get("list_items"), list):
return (f"list of {r.payload['list_count']} items: "
f"{', '.join(r.payload['list_items'][:8])}...")
return str(r.payload.get("value", r.payload.get("quote", "")))
val = str(r.payload.get("value", r.payload.get("quote", "")))
if r.payload.get("ambiguous"):
alts = r.payload.get("alternatives") or []
other = "; ".join(f"{a.get('value')} ({a.get('role')})"
for a in alts[:3])
return (f"{val} — AMBIGUOUS, other defensible readings: "
f"{other}")
return val
lines = []
notices = []