"""What the reading kept, and what the math stratum says it solved. Two bookkeeping checks stand between the readers and the answer, and both are stated as SMT2 so the referee of the orchestrator recomputes them instead of trusting a node: * :func:`keeps` -- every number the user wrote is still in the sentence of the math language, and no number appeared there that the user did not write. A reader that merged two slots or shifted a digit changes that multiset, and a sorted element by element equality over declared reals says so; * :func:`echo_matches` -- the math stratum echoes the problem it understood, and the echo is compared with the sentence the readers agreed on. The echo is an independent channel: a node standing between the readers and the math layer can rewrite its own record, but it cannot move the echo without moving numbers that were counted upstream. Neither check decides mathematics. They count tokens, and they hand the counting to a solver so that a node claiming the numbers were kept is checked against that claim. """ from typing import Any, Dict, List, Optional from . import canonical, compare def keeps(reading: Dict[str, Any], question: str) -> Dict[str, Any]: """Whether one reading kept every number the user wrote, with the SMT claim that says so.""" return compare.keeps(reading, question) def echo_numbers(math_verdict: Dict[str, Any]) -> List[str]: """The numbers of the problem the math stratum says it solved, ordered by value.""" return compare.sorted_numbers(canonical.tidy(str(math_verdict.get("problem_math", "")))) def echo_matches(reading: Dict[str, Any], math_verdict: Dict[str, Any]) -> bool: """Did the math stratum solve the sentence the readers agreed on? A verdict that names no sentence at all -- the backend was off, or the math stratum could not be started -- leaves nothing to contradict, so it is not a mismatch: the assurance still falls to zero because there is no math assurance to take the minimum with, but nothing here may be read as tampering. Only an echo that names a sentence and names a different one is a mismatch. """ if not str(math_verdict.get("problem_math", "")).strip(): return True return compare.agrees_with_math(reading, math_verdict) def coverage_claim(question: str, readings: List[Dict[str, Any]], math_verdict: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """One SMT2 claim tying the user's numbers to the readings and, when known, to the echo. Every reading that produced a sentence has to carry the user's numbers, and once the math stratum has answered its echo has to carry them too. The assertion set is the conjunction of those element equalities: satisfiable exactly when all of them hold, which is what the referee checks against the status the node claimed. """ wrote = compare.sorted_numbers(question) readings_present = [reading for reading in readings if reading.get("math")] steps = [keeps(reading, question) for reading in readings_present] ok = all(step["ok"] for step in steps) decls: List[str] = [] asserts: List[str] = [] for step in steps: decls = decls + list(step["declarations"]) asserts = asserts + list(step["assertions"]) if math_verdict is not None: echo = echo_numbers(math_verdict) decls2, asserts2 = compare.elementwise(wrote, echo, "echo") decls = decls + decls2 asserts = asserts + asserts2 ok = ok and echo == wrote if not wrote: # Nothing was written, so there is nothing to keep: the claim is the empty conjunction, # which is satisfiable, and the node says sat. return {"ok": ok, "wrote": [], "kept": [], "declarations": [], "assertions": ["(assert (= 1 1))"], "claimed": "sat"} return { "ok": ok, "wrote": wrote, "kept": [step["kept"] for step in steps], "declarations": decls, "assertions": asserts + (["(assert (= 1 1))"] if ok else ["(assert false)"]), "claimed": "sat" if ok else "unsat", } def summary(step: Dict[str, Any]) -> Dict[str, Any]: """The part of a coverage step that belongs in the dossier and in front of the user.""" return { "ok": step.get("ok", False), "wrote": step.get("wrote", []), "missing": step.get("missing", []), "added": step.get("added", []), }