tailot's picture
physics-stratum: the conversational layer over the physics topology
3a0a69b verified
Raw History Blame Contribute Delete
4.6 kB
"""What the reading kept, and what the physics layer 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 physics 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 physics layer 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 layer can rewrite its own record, but it cannot move the
echo without moving numbers that were counted upstream.
Neither check decides physics. They count tokens, they hand the counting to a solver so that a node
claiming the numbers were kept is checked against that claim, and the identity of two sentences is
settled by the parser of the bundle, which owns that language.
"""
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(physics_verdict: Dict[str, Any]) -> List[str]:
"""The numbers of the problem the physics layer says it solved, ordered by value."""
return compare.sorted_numbers(canonical.tidy(str(physics_verdict.get("problem_physics", ""))))
def echo_matches(reading: Dict[str, Any], physics_verdict: Dict[str, Any]) -> bool:
"""Did the physics layer solve the sentence the readers agreed on?
A verdict that names no sentence at all -- the backend was off, or the physics layer could not
be started -- leaves nothing to contradict, so it is not a mismatch: the assurance still falls
to zero because there is no physics 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(physics_verdict.get("problem_physics", "")).strip():
return True
return compare.agrees_with_physics(reading, physics_verdict)
def coverage_claim(question: str, readings: List[Dict[str, Any]],
physics_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 physics
layer 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 physics_verdict is not None:
echo = echo_numbers(physics_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", []),
}