lingologos-math / tests /test_agents.py
tailot's picture
Add the fourteen sentence families and the decision rule of README section 3
f74e990 verified
Raw History Blame Contribute Delete
5.74 kB
"""Behaviour of the agents, the decision rule and the redundancy claims made in the README."""
import time
import pytest
from lgmath import agents, corpus, faults, pipeline, protocol, smt
SAMPLE = [i for i in corpus.load() if i["kind"] != "unsupported"][::5]
SOLVER_FAULTS = ["solver_a:%s" % k for k in faults.CATALOG["solver_a"]] + \
["solver_b:%s" % k for k in faults.CATALOG["solver_b"]]
@pytest.fixture(autouse=True)
def _reset_faults():
faults.configure(None)
yield
faults.configure(None)
def test_protocol_round_trip_and_raw_problem_text():
dossier = {"v": 1, "problem": "evaluate 1 + 1", "fa": {"ast": "x"}}
text = protocol.encode("summary line", dossier)
assert protocol.summary_of(text) == "summary line"
assert protocol.decode(text) == dossier
assert protocol.decode(" evaluate 1 + 1 ") == {"v": 1, "problem": "evaluate 1 + 1"}
def test_fault_catalogue_names_real_agents():
assert set(faults.CATALOG) == set(agents.STEPS)
@pytest.mark.parametrize("item", SAMPLE, ids=[i["id"] for i in SAMPLE])
@pytest.mark.parametrize("fault", SOLVER_FAULTS)
def test_untrusted_solver_faults_never_produce_a_wrong_verdict(item, fault):
faults.configure([fault])
final = pipeline.run(item["text"])["final"]
assert corpus.outcome(item, final) in ("correct", "abstain", "abstain_expected")
def test_one_compromised_checker_plus_a_wrong_solver_is_still_caught():
for item in SAMPLE:
for checker in ("verifier:rubber_stamp", "adversary:rubber_stamp"):
faults.configure(["solver_a:flip_verdict", "solver_a:drop_solution", "solver_a:extra_solution", checker])
final = pipeline.run(item["text"])["final"]
assert corpus.outcome(item, final) != "wrong", (item["text"], checker)
def test_a_wrong_arbiter_is_overruled_by_the_recomputed_decision():
faults.configure(["solver_a:off_by_one", "arbiter:force_accept"])
final = pipeline.run("evaluate 1/2 + 1/3")["final"]
assert final["answer"]["value"] == "5/6"
assert "arbiter" in final["detected"]
def test_formalizer_misreading_is_detected_by_the_second_formalizer():
faults.configure(["formalizer_a:sign_flip"])
final = pipeline.run("evaluate 5 - 3")["final"]
assert final["status"] == "UNKNOWN"
assert "not provably equivalent" in " ".join(final["reasons"])
def test_a_misreading_shared_by_both_formalizers_is_the_known_limit():
"""No number of agents can detect a mistake that every formalizer makes in the same way."""
faults.configure(["formalizer_a:sign_flip", "formalizer_b:sign_flip"])
final = pipeline.run("evaluate 5 - 3")["final"]
assert final["status"] == "VERIFIED" and final["answer"]["value"] == "8" # wrong on purpose: 5 - 3 = 2
def test_without_the_second_formalizer_a_misreading_goes_unnoticed():
faults.configure(["formalizer_a:sign_flip"])
final = pipeline.run("evaluate 5 - 3", active=["formalizer_a", "solver_a", "verifier"])["final"]
assert final["status"] != "UNKNOWN" and final["answer"]["value"] == "8"
def test_a_crashing_agent_makes_the_topology_abstain(monkeypatch):
def boom(incoming, node_id="solver_b"):
raise RuntimeError("simulated failure")
monkeypatch.setitem(agents.STEPS, "solver_b", boom)
final = pipeline.run("evaluate 1 + 1")["final"]
assert final["status"] == "UNKNOWN" and "solver_b" in final["detected"]
def test_a_referee_disagreement_downgrades_the_verdict(monkeypatch):
real = smt.referee
monkeypatch.setattr(smt, "referee", lambda o, d, a: "unsat" if "(assert true)" not in a else real(o, d, a))
final = pipeline.run("evaluate 1 + 1")["final"]
assert final["status"] == "UNKNOWN" and "referee disagrees" in " ".join(final["reasons"])
def test_ontology_timeout_turns_a_hard_query_into_unknown_quickly():
hard = ["(declare-const x Int)", "(declare-const y Int)", "(declare-const z Int)",
"(assert (= (+ (* x x x) (* y y y) (* z z z)) 33))"]
started = time.time()
assert smt.referee("(set-option :timeout 300)\n", hard[:3], hard[3:]) == "unknown"
assert time.time() - started < 10
def test_assurance_levels():
assert pipeline.run("evaluate 1/2 + 1/3")["final"]["assurance"] == 3
assert pipeline.run("solve x^2 - 2 <= 0 for x")["final"]["assurance"] == 3
assert pipeline.run("prove x^2 + y^2 >= 2*x*y")["final"]["status"] == "SINGLE_PROVER"
assert pipeline.run("prove forall x in R. x^2 >= 0")["final"]["status"] == "SINGLE_PROVER"
assert pipeline.run("prove x^2 + y^2 > 2*x*y")["final"]["status"] == "VERIFIED" # rational counterexample
def test_an_inconclusive_checker_plus_a_compromised_one_cannot_pass_a_wrong_answer(monkeypatch):
"""Both present checkers must confirm: 'unknown' from one of them is not a free pass."""
real = smt.check
def verifier_cannot_decide(decls, assertions, names=(), config="nlsat", timeout_ms=8000, sort="Real"):
if config == "default":
return "unknown", {}
return real(decls, assertions, names, config, timeout_ms, sort)
monkeypatch.setattr(smt, "check", verifier_cannot_decide)
faults.configure(["solver_a:off_by_one", "adversary:rubber_stamp"])
final = pipeline.run("evaluate 1/2 + 1/3", active=["formalizer_a", "formalizer_b", "solver_a", "verifier", "adversary"])["final"]
assert final["status"] == "UNKNOWN"
assert "not confirmed by" in " ".join(final["reasons"])
def test_a_rank_claim_above_the_smaller_dimension_is_simply_false():
from lgmath import fam_linalg
assert fam_linalg.rank_claim_smt([["1.0", "1.0", "1.0"]], 2) == "false"
final = pipeline.run("nullspace [[1,1,1]]")["final"]
assert final["status"] == "VERIFIED" and final["answer"]["dim"] == 2