"""Judge value cleanup tests (the (K5) bug, run 085457 / 24-25/10 session). glm-5.3-flash glues row citations and glosses onto the value field because AnswerFromKG says "cite rows by id" but has no citation output field. Three live shapes, verbatim from the runs: "Socialist International (K5)" — row id "2018 (Pedro Sánchez has been Prime Minister of Spain — i.e., ...)" — parenthetical gloss "15 (list of 15 members: 17 november 2023, ...)" — LIST gloss Laws under test: 1. Trailing citation shapes are STRIPPED from the value, at the judge's output boundary (_clean_value), BEFORE the payload and the audit see it. 2. Legitimate parentheticals SURVIVE: "(PSOE)" inside a name, "(Presidente del Gobierno)" inside an office title. 3. A cleaned value lets the ablation audit RUN where the raw value killed it silently ("the judge invented it: nothing to ablate"). 4. The clean is idempotent and does not touch UNKNOWN/empty. No network. No LLM: the cleaner is fixed code; the tests call it directly plus one scripted-judge pass through _answer_from_rows. Run: .venv/bin/python knowledge/tests/value_clean.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent)) from knowledge.expert import KnowledgeExpert, _clean_value def test_row_id_suffix_stripped(): assert _clean_value("Socialist International (K5)") == "Socialist International" assert _clean_value("Rajoy (K12)") == "Rajoy" print("ok: row-id citation suffix stripped") def test_cited_from_rows_gloss_stripped(): v = ("2018 (Pedro Sánchez has been Prime Minister of Spain — i.e., " "President of the Government — since 2018; cited from rows K16 and K30)") assert _clean_value(v) == "2018", _clean_value(v) print("ok: 'cited from rows' parenthetical stripped, year kept") def test_list_gloss_stripped(): v = "15 (list of 15 members: 17 november 2023, 28 november 2023, 24 september 2024)" assert _clean_value(v) == "15", _clean_value(v) print("ok: LIST-members gloss stripped, count kept") def test_legitimate_parentheticals_survive(): assert _clean_value("Spanish Socialist Workers' Party (PSOE)") == \ "Spanish Socialist Workers' Party (PSOE)" assert _clean_value("Prime Minister of Spain (Presidente del Gobierno)") == \ "Prime Minister of Spain (Presidente del Gobierno)" # a parenthetical that merely mentions a number is NOT a citation assert _clean_value("Prime Minister of Spain (since 2018)") == \ "Prime Minister of Spain (since 2018)" print("ok: legitimate parentheticals untouched") def test_unknown_and_empty_untouched(): assert _clean_value("UNKNOWN") == "UNKNOWN" assert _clean_value("") == "" assert _clean_value(" Spain ") == "Spain" print("ok: UNKNOWN/empty/whitespace handled") def test_idempotent_and_mid_value_citations(): v = _clean_value("Socialist International (K5)") assert _clean_value(v) == v # a citation in the MIDDLE is not the live bug shape — leave it (no # aggressive surgery on the judge's sentence), the boundary law strips # only the TRAILING gloss assert _clean_value("Per (K2) the answer is Spain") == \ "Per (K2) the answer is Spain" print("ok: idempotent; mid-value text untouched (boundary law: trailing only)") def test_answer_from_rows_boundary(): """The hook: _answer_from_rows cleans pred.value before the payload is built. Scripted dspy-style Predict object; no network.""" e = KnowledgeExpert.__new__(KnowledgeExpert) e.cfg = __import__("knowledge.expert", fromlist=["KnowledgeConfig"]).KnowledgeConfig() e._log = lambda *a, **k: None class Pred: value = "Socialist International (K5)" quote = "ninth president of Socialist International since 2022" class confidence: value = "high" class Row: src = rel = dst = quote = url = "u" conflict = False e.answer = lambda question, rows: Pred() out = e._answer_from_rows("which office?", [Row()]) assert out["value"] == "Socialist International", out["value"] print("ok: _answer_from_rows boundary cleans the value") if __name__ == "__main__": test_row_id_suffix_stripped() test_cited_from_rows_gloss_stripped() test_list_gloss_stripped() test_legitimate_parentheticals_survive() test_unknown_and_empty_untouched() test_idempotent_and_mid_value_citations() test_answer_from_rows_boundary() print("\nvalue_clean: 7/7 ok")