Datasets:
Modalities:
Text
Formats:
json
Size:
< 1K
Tags:
multilingual
physics
dimensional-analysis
unit-conversion
natural-language-to-formal-language
multi-agent-systems
Download lgphysstratum/decide.py from tailot/physics-stratum: direct link, hf CLI and curl.
- Browser
- Download file 8.85 kB
-
https://huggingface.co/datasets/tailot/physics-stratum/resolve/main/lgphysstratum/decide.py
- Command line
-
hf download hf://datasets/tailot/physics-stratum/lgphysstratum/decide.py
-
curl -L -o decide.py https://huggingface.co/datasets/tailot/physics-stratum/resolve/main/lgphysstratum/decide.py
8.85 kB
| """The decision: pure, and never raising anything. | |
| One function, no I/O: given what the readers understood, what the equivalence node proved, what | |
| the coverage node counted and what the physics layer answered, it states the state of the session, | |
| the assurance the user sees, and whether an answer may be shown at all. The arbiter runs it, the | |
| tools run it again on the transcript, and a test pins it, so the same rules are checked in three | |
| places -- the same shape the physics layer uses for its own ``decide``. | |
| The rule the whole module exists to enforce: | |
| assurance(shown) = min(assurance(physics), assurance(reading)) | |
| and ``assurance(reading)`` is at most two, which is also the highest level the physics layer ever | |
| reaches: no chain of this project adds a third witness on top of a physical statement, because a | |
| law of physics is not proved by a second formalizer but by the agreement of two independent | |
| routes. The stratum therefore never reports more than the layer, and a reading in a human language | |
| carries one more fallible step than a sentence the physics layer formalizes itself. A verdict is | |
| never raised by the stratum; the only direction it can move is down, and the adversary node writes | |
| the negation of that inequality as SMT2 so the referee of the orchestrator refutes it. | |
| The states, which are the eight the lexicon of every covered language names: | |
| =========== =================================================================== | |
| AGREED both readers produced the same sentence, and the physics layer solved it | |
| PASSTHROUGH the sentence was already written in the physics language: nothing was translated | |
| SINGLE one reader understood the sentence and the other abstained: the physics layer was | |
| consulted, and the assurance is capped by the missing witness | |
| DISPUTED the two readers produced different sentences, or a reading lost a number: the | |
| physics layer is not consulted at all | |
| AMBIGUOUS the sentence announces more than one kind of problem: the user has to choose | |
| NOT COVERED neither reader found anything: the language does not say this in a covered way | |
| TAMPERED the physics layer says it solved something other than the agreed sentence | |
| NONE no reading was even attempted | |
| =========== =================================================================== | |
| """ | |
| from typing import Any, Dict, List, Optional | |
| AMBIGUOUS_REASONS = ("multiple", "two_verbs") | |
| STATES = ("AGREED", "PASSTHROUGH", "SINGLE", "DISPUTED", "AMBIGUOUS", "NOT COVERED", "TAMPERED", "NONE") | |
| # The reading assurance of each state, on the scale the physics layer uses. Two is the top of this | |
| # scale, and the physics layer stands on the same rung: what raises a verdict above it would be a | |
| # third kind of witness, and this project has none for physics. | |
| CEILING = {"AGREED": 2, "PASSTHROUGH": 2, "SINGLE": 1, "DISPUTED": 0, "AMBIGUOUS": 0, | |
| "NOT COVERED": 0, "TAMPERED": 0, "NONE": 0} | |
| # The statuses that answer the question. A refusal, a guard block and an abstention are the layer | |
| # explaining why it will not answer, and they carry no answer text at the level of the stratum: | |
| # the state of the layer is reported, the sentence of the layer is not. | |
| ANSWERING = ("VERIFIED", "SINGLE_PROVER") | |
| def reader_notes(readings: List[Dict[str, Any]]) -> List[str]: | |
| """The reason keys the abstaining readers left behind, in reader order.""" | |
| notes: List[str] = [] | |
| for reading in readings: | |
| for note in reading.get("notes") or []: | |
| reason = note.get("reason") if isinstance(note, dict) else str(note) | |
| if reason and reason not in notes: | |
| notes.append(reason) | |
| return notes | |
| def state_of(readings: List[Dict[str, Any]], comparison: Optional[Dict[str, Any]], | |
| coverage: Optional[Dict[str, Any]], physics_verdict: Optional[Dict[str, Any]], | |
| direct: Optional[Dict[str, Any]] = None, echo_ok: Optional[bool] = None) -> str: | |
| """The state of a session, from the evidence the nodes collected.""" | |
| present = [reading for reading in readings if reading.get("math")] | |
| notes = reader_notes(readings) | |
| if direct is not None: | |
| return "PASSTHROUGH" | |
| if physics_verdict is not None and echo_ok is False: | |
| return "TAMPERED" | |
| if len(present) >= 2: | |
| if comparison is None: | |
| return "DISPUTED" | |
| if coverage is not None and not coverage.get("ok", False): | |
| return "DISPUTED" | |
| return "AGREED" if comparison.get("same") else "DISPUTED" | |
| if len(present) == 1: | |
| # A lone witness has to hold the numbers the person wrote: a reading that lost one is not a | |
| # weaker reading of the same problem, it is a different problem, and the ceiling of SINGLE | |
| # is not a licence to answer it. This is the case the ablation of the topology uncovered: | |
| # with one reader there is no second sentence to compare against, so the count of literals | |
| # is what stands between a shifted number and an answer the gold does not accept. | |
| if coverage is not None and not coverage.get("ok", False): | |
| return "DISPUTED" | |
| return "SINGLE" | |
| if any(reason in AMBIGUOUS_REASONS for reason in notes): | |
| return "AMBIGUOUS" | |
| return "NOT COVERED" | |
| def reasons_for(state: str, readings: List[Dict[str, Any]], comparison: Optional[Dict[str, Any]], | |
| coverage: Optional[Dict[str, Any]], physics_verdict: Optional[Dict[str, Any]], | |
| asked: bool = True) -> List[str]: | |
| """The reason keys that explain a state, in the order a reader should read them.""" | |
| out: List[str] = [] | |
| for reason in reader_notes(readings): | |
| if reason not in out: | |
| out.append(reason) | |
| if state == "DISPUTED": | |
| if coverage is not None and not coverage.get("ok", False): | |
| out.append("literal_count") | |
| if comparison is not None and not comparison.get("same"): | |
| out.append("disagree") | |
| if comparison is not None and comparison.get("why") == "unknown": | |
| out.append("physics_unknown") | |
| if comparison is not None and comparison.get("why") == "smt" and comparison.get("model"): | |
| if "disagree" not in out: | |
| out.append("disagree") | |
| if state == "TAMPERED": | |
| out.append("boundary") | |
| if physics_verdict is not None and physics_verdict.get("referee_mismatch"): | |
| out.append("referee") | |
| if asked and physics_verdict is not None and physics_verdict.get("status") == "UNKNOWN": | |
| out.append("physics_unknown") | |
| if not asked and state in ("AGREED", "SINGLE", "PASSTHROUGH"): | |
| out.append("no_math") | |
| return out | |
| def decide(dossier: Dict[str, Any]) -> Dict[str, Any]: | |
| """One verdict for one session, with the assurance the user is allowed to see.""" | |
| readings = [dossier.get("reading_a") or {}, dossier.get("reading_b") or {}] | |
| if dossier.get("reading_direct"): | |
| readings = [dossier["reading_direct"]] | |
| comparison = dossier.get("compare") | |
| coverage = dossier.get("coverage") | |
| physics_verdict = dossier.get("physics") | |
| direct = dossier.get("reading_direct") | |
| echo_ok = dossier.get("echo_ok") | |
| state = state_of(readings, comparison, coverage, physics_verdict, direct, echo_ok) | |
| ceiling = CEILING[state] | |
| assurance = 0 | |
| answer_shown = "" | |
| if state in ("AGREED", "SINGLE", "PASSTHROUGH") and physics_verdict is not None: | |
| # The physics layer answers at level two when its readers and its provers agree, at level | |
| # one when a lone prover stands, and at level zero when it abstains or says UNKNOWN. A | |
| # refusal stops the answer at zero too: the layer explained itself, it did not answer. | |
| if physics_verdict.get("status") in ANSWERING: | |
| assurance = min(ceiling, int(physics_verdict.get("assurance", 0) or 0)) | |
| else: | |
| assurance = 0 | |
| answer_shown = physics_verdict.get("answer_text") or "" | |
| if assurance == 0: | |
| answer_shown = "" | |
| reasons = reasons_for(state, readings, comparison, coverage, physics_verdict, | |
| asked=bool(str(dossier.get("sent") or ""))) | |
| return { | |
| "state": state, | |
| "assurance": assurance, | |
| "ceiling": ceiling, | |
| "physics_assurance": int((physics_verdict or {}).get("assurance", 0) or 0), | |
| "physics_status": (physics_verdict or {}).get("status"), | |
| "answer": answer_shown, | |
| "reasons": reasons, | |
| "consulted_physics": physics_verdict is not None, | |
| } | |
| def raise_assurance(final: Dict[str, Any], claimed: int) -> Dict[str, Any]: | |
| """The arbiter fault: an assurance above what the evidence supports (experiments only).""" | |
| out = dict(final) | |
| out["assurance"] = max(int(final.get("assurance", 0) or 0), int(claimed)) | |
| out["claimed_above_evidence"] = True | |
| return out | |