lingologos-physics / tests /test_faults.py
tailot's picture
update physics bundle
875f067 verified
Raw History Blame Contribute Delete
5.12 kB
"""Fault injection: what has to go wrong before the dataset stops claiming it is right.
Every entry of the catalog in lgphys/faults.py is a way a node can misbehave while the session is
running. Two families of behaviour are expected and both are honest:
a fault that has nothing to take advantage of stays dormant - the node has no failing check to hide,
no number to rewrite - and then the verdict must be exactly the verdict of the clean session;
a fault that does bite is caught, and the only verdict the chain is then allowed to give is one with
zero confidence, or the same refusal it had already reached on the evidence of the world.
The two documented exceptions are checked by name further down: an arbiter that is forced to report
what the evidence already reports, and a boundary node that skips its own recomputation while nobody
else is wrong either.
"""
import pytest
from lgphys import agents, faults, pipeline
CONVERT = "convert 3 km to m"
REFUSED = "convert 3 m to s"
BASELINE = {CONVERT: (agents.VERIFIED, 2), REFUSED: (agents.REFUSED, 2)}
# A node that is told to accept an answer it has no reason to doubt is not a fault against that
# answer; and a node that stops redoing a computation nobody got wrong leaves no trace to be seen.
INERT_WHEN_FIRED = {"arbiter:force_accept", "boundary:skip_recompute"}
@pytest.fixture(autouse=True)
def _clean_faults():
faults.configure("")
yield
faults.configure("")
def _run(question, spec):
faults.configure(spec)
out = pipeline.run(question)
return out["decision"], sorted(faults.FIRED)
@pytest.mark.parametrize("question", [CONVERT, REFUSED])
@pytest.mark.parametrize("entry", sorted(
"%s:%s" % (agent, fault)
for agent, kinds in faults.CATALOG.items() for fault in kinds))
def test_a_fault_is_either_dormant_or_it_costs_the_confidence(entry, question):
decision, fired = _run(question, entry)
expected_status, expected_assurance = BASELINE[question]
assert decision["status"] != agents.VERIFIED or decision["assurance"] >= 1, decision
if not fired:
assert decision["status"] == expected_status, (entry, question, decision["status"])
assert decision["assurance"] == expected_assurance, (entry, question, decision["assurance"])
return
if entry in INERT_WHEN_FIRED:
return
if decision["status"] == agents.UNKNOWN:
assert decision["assurance"] == 0, (entry, question, decision)
return
# A refusal that survives a fault was reached on a burden of the world, not on agreement.
assert decision["status"] == agents.REFUSED, (entry, question, decision)
def test_the_clean_session_is_verified_and_no_fault_fired_in_it():
decision = pipeline.run(CONVERT)["decision"]
assert decision["status"] == agents.VERIFIED and decision["assurance"] == 2
assert list(faults.FIRED) == []
def test_a_reader_that_hands_a_wrong_number_downstream_breaks_the_agreement_it_was_there_to_proof():
decision, fired = _run(CONVERT, "reader_a:corrupt_road")
assert fired == ["reader_a:corrupt_road"]
assert decision["status"] == agents.UNKNOWN and decision["assurance"] == 0
assert "equivalence_failed_agree_read_b" in decision["reasons"]
def test_a_delegate_rewritten_on_the_way_to_the_arithmetic_shows_as_a_disagreement():
decision, fired = _run(CONVERT, "coverage:corrupt_delegate")
assert fired == ["coverage:corrupt_delegate"]
assert decision["status"] == agents.UNKNOWN and decision["assurance"] == 0
assert "invoker_failed_physics_and_mathematics_agree" in decision["reasons"]
def test_an_answer_rewritten_by_the_node_that_brought_it_back_is_caught_by_recomputation():
decision, fired = _run(CONVERT, "invoker:corrupt_answer")
assert fired == ["invoker:corrupt_answer"]
assert decision["status"] == agents.UNKNOWN and decision["assurance"] == 0
assert "boundary_failed_answer_recomputed" in decision["reasons"]
def test_an_adversary_whose_claim_z3_rejects_is_not_believed_by_the_chain():
decision, fired = _run(CONVERT, "adversary:rubber_stamp")
assert fired == ["adversary:rubber_stamp"]
assert decision["status"] == agents.UNKNOWN and decision["assurance"] == 0
assert "referee_disagreed_with_adversary" in decision["reasons"]
def test_a_boundary_that_stops_checking_becomes_visible_once_somebody_else_is_wrong():
clean = pipeline.run(CONVERT)["decision"]
alone, fired_alone = _run(CONVERT, "boundary:skip_recompute")
assert fired_alone == ["boundary:skip_recompute"]
assert (alone["status"], alone["assurance"]) == (clean["status"], clean["assurance"])
together, fired_together = _run(CONVERT, "invoker:corrupt_answer,boundary:skip_recompute")
assert "boundary:skip_recompute" in fired_together
assert together["status"] == agents.UNKNOWN and together["assurance"] == 0
def test_two_faults_of_the_same_kind_are_read_as_one_specification():
_decision, fired = _run(CONVERT, "reader_a:corrupt_road,reader_b:corrupt_road")
assert fired == sorted(fired) and len(fired) == 2
assert _run(CONVERT, "reader_a:not_a_fault")[1] == []