physics-stratum / tests /test_decide.py
tailot's picture
physics-stratum: the conversational layer over the physics topology
3a0a69b verified
Raw History Blame Contribute Delete
8.87 kB
"""The decision rule, and the ceiling that the stratum is not allowed to cross."""
import copy
from lgphysstratum import decide, pipeline
def _reading(sentence, kind="convert", slots=None, notes=()):
return {"status": "READ", "kind": kind, "math": sentence, "slots": slots or {}, "notes": list(notes)}
def _physics(status="VERIFIED", assurance=2, answer="3 km = 3000 m (L)",
sentence="convert 3 km to m", **extra):
verdict = {"status": status, "assurance": assurance, "answer_text": answer,
"problem_physics": sentence}
verdict.update(extra)
return verdict
def _dossier(readings, comparison, coverage, physics_verdict, direct=None, echo_ok=True):
dossier = {"question": "converti 3 chilometri in metri", "lang": "it", "sent": "convert 3 km to m",
"reading_a": readings[0] if len(readings) > 0 else {},
"reading_b": readings[1] if len(readings) > 1 else {},
"compare": comparison, "coverage": coverage, "physics": physics_verdict,
"echo_ok": echo_ok}
if direct is not None:
dossier["reading_direct"] = direct
return dossier
def test_no_state_sits_above_the_top_of_the_physics_layer():
# The physics layer never shows more than two: its own two provers and its referee stop there,
# there is no third level to reach. So the ceiling of a reading is not one level below the layer,
# as it is for a layer that can reach three -- it is the layer itself, and a reading can only ever
# lower the confidence, never lift it.
assert set(decide.STATES) == set(decide.CEILING)
assert max(decide.CEILING.values()) == 2
for state in decide.STATES:
assert decide.CEILING[state] <= 2, state
assert max(decide.CEILING[state] for state in ("AGREED", "PASSTHROUGH")) == 2
for state in ("DISPUTED", "AMBIGUOUS", "NOT COVERED", "TAMPERED", "NONE"):
assert decide.CEILING[state] == 0, state
def test_two_agreeing_readers_take_the_minimum_with_the_physics_answer():
readings = [_reading("convert 3 km to m"), _reading("convert 3 km to m")]
# The physics layer stands at two when both of its roads produced the number and at one when a
# lone prover stands; a stratum that reads the sentence with two witnesses cannot add to that.
for status, assurance, expected in (("VERIFIED", 2, 2), ("SINGLE_PROVER", 1, 1),
("ABSTAINED", 0, 0), ("UNKNOWN", 0, 0)):
final = decide.decide(_dossier(readings, {"same": True}, {"ok": True},
_physics(status, assurance,
"" if assurance == 0 else "3 km = 3000 m (L)")))
assert final["state"] == "AGREED"
assert final["ceiling"] == 2
assert final["assurance"] == min(2, assurance), (status, final)
assert final["physics_assurance"] == assurance
def test_a_refusal_of_the_layer_is_never_shown_as_an_answer():
# A dimensionally false statement is a verdict too, and the layer states it: the stratum reports
# the refusal, and the reading that was sure of its sentence does not get to show an answer. The
# reason line is the explanation the layer itself wrote, so no reason key of the stratum is added
# here -- the one below, for a layer that stayed silent, is a different case.
readings = [_reading("convert 3 kg to m"), _reading("convert 3 kg to m")]
verdict = _physics("REFUSED", 0, "", sentence="convert 3 kg to m")
final = decide.decide(_dossier(readings, {"same": True}, {"ok": True}, verdict))
assert final["state"] == "AGREED"
assert final["assurance"] == 0
assert final["answer"] == ""
assert final["physics_status"] == "REFUSED"
def test_a_missing_witness_caps_the_session_at_one():
readings = [_reading("convert 3 km to m"), {}]
final = decide.decide(_dossier(readings, None, None, _physics()))
assert final["state"] == "SINGLE"
assert final["assurance"] == 1
# The cap is not the only thing that holds with one reader: the count of literals still does, so a
# witness that stands alone does not thereby get to answer another problem.
kept = decide.decide(_dossier(readings, None, {"ok": True}, _physics()))
assert kept["state"] == "SINGLE"
assert kept["assurance"] == 1
def test_a_lone_reading_that_lost_a_number_answers_a_different_problem():
# What the ablation of the topology found: with the second reader switched out, a number shifted
# by the first one used to reach the person as a single witness answer. The ceiling said one, but
# the answer was of another problem, so the count of the literals refuses it instead.
readings = [_reading("convert 4 km to m"), {}]
final = decide.decide(_dossier(readings, None, {"ok": False}, _physics(sentence="convert 4 km to m")))
assert final["state"] == "DISPUTED"
assert final["assurance"] == 0
assert final["answer"] == ""
assert "literal_count" in final["reasons"]
def test_a_question_that_announces_two_kinds_of_problem_is_left_to_the_user():
readings = [{"status": "AMBIGUOUS", "kind": None, "math": "", "slots": {},
"notes": [{"reason": "two_verbs"}]}, {}]
final = decide.decide(_dossier(readings, None, None, None))
assert final["state"] == "AMBIGUOUS"
assert final["ceiling"] == 0
assert final["assurance"] == 0
assert "two_verbs" in final["reasons"]
def test_a_question_this_language_does_not_write_is_not_covered():
final = decide.decide(_dossier([{}, {}], None, None, None))
assert final["state"] == "NOT COVERED"
assert final["assurance"] == 0
assert final["consulted_physics"] is False
assert final["answer"] == ""
def test_a_disagreement_is_never_resolved_by_the_physics_layer():
readings = [_reading("convert 3 km to m"), _reading("convert 3 km to h")]
final = decide.decide(_dossier(readings, {"same": False, "why": "text"}, {"ok": True}, _physics()))
assert final["state"] == "DISPUTED"
assert final["assurance"] == 0
assert final["answer"] == ""
assert "disagree" in final["reasons"]
def test_a_reading_that_lost_a_number_is_disputed_even_when_the_readers_agree():
readings = [_reading("convert 3 km to m"), _reading("convert 3 km to m")]
final = decide.decide(_dossier(readings, {"same": True}, {"ok": False}, _physics()))
assert final["state"] == "DISPUTED"
assert "literal_count" in final["reasons"]
assert final["assurance"] == 0
def test_an_echo_that_names_another_sentence_is_reported_as_tampering():
readings = [_reading("convert 3 km to m"), _reading("convert 3 km to m")]
final = decide.decide(_dossier(readings, {"same": True}, {"ok": True}, _physics(), echo_ok=False))
assert final["state"] == "TAMPERED"
assert final["assurance"] == 0
assert "boundary" in final["reasons"]
def test_a_sentence_already_written_in_the_physics_language_passes_through():
direct = {"kind": "convert", "math": "convert 3 km to m", "slots": {}, "mode": "direct"}
final = decide.decide(_dossier([{}, {}], None, {"ok": True}, _physics(), direct=direct))
assert final["state"] == "PASSTHROUGH"
assert final["ceiling"] == 2
assert final["assurance"] == 2
def test_no_answer_is_shown_when_the_physics_layer_did_not_answer():
readings = [_reading("convert 3 km to m"), _reading("convert 3 km to m")]
final = decide.decide(_dossier(readings, {"same": True}, {"ok": True},
_physics("UNKNOWN", 0, "")))
assert final["assurance"] == 0
assert final["answer"] == ""
assert "physics_unknown" in final["reasons"]
def test_the_decision_is_pure_and_leaves_the_evidence_as_it_found_it():
dossier = _dossier([_reading("convert 3 km to m"), _reading("convert 3 km to m")], {"same": True},
{"ok": True}, _physics())
before = copy.deepcopy(dossier)
first = decide.decide(dossier)
second = decide.decide(dossier)
assert first == second
assert dossier == before
def test_a_claimed_assurance_is_marked_and_the_tools_lower_it_again():
honest = decide.decide(_dossier([_reading("convert 3 km to m"), _reading("convert 3 km to m")],
{"same": True}, {"ok": True}, _physics()))
forced = decide.raise_assurance(honest, 3)
assert forced["assurance"] == 3
assert forced["claimed_above_evidence"] is True
# The tools do not show what the arbiter wrote: they re-run the decision on the transcript, and
# what stands is the more cautious of the two.
checked = pipeline.referee_adjusted(forced,
[{"sender": "arbiter", "claimed": "unsat", "referee": "sat"}])
assert checked["assurance"] <= min(2, honest["assurance"])
assert "arbiter" in (checked.get("detected") or [])