File size: 7,107 Bytes
e9dac18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""The decision rule, and the ceiling that the stratum is not allowed to cross."""

import copy

from lgstratum import decide, pipeline


def _reading(sentence, kind="evaluate", slots=None, notes=()):
    return {"status": "READ", "kind": kind, "math": sentence, "slots": slots or {}, "notes": list(notes)}


def _math(status="VERIFIED", assurance=3, answer="4", sentence="evaluate 2+2", **extra):
    verdict = {"status": status, "assurance": assurance, "answer_text": answer, "problem_math": sentence}
    verdict.update(extra)
    return verdict


def _dossier(readings, comparison, coverage, math_verdict, direct=None, echo_ok=True):
    dossier = {"question": "quanto fa 2+2?", "lang": "it", "sent": "evaluate 2+2",
               "reading_a": readings[0] if len(readings) > 0 else {},
               "reading_b": readings[1] if len(readings) > 1 else {},
               "compare": comparison, "coverage": coverage, "math": math_verdict,
               "echo_ok": echo_ok}
    if direct is not None:
        dossier["reading_direct"] = direct
    return dossier


def test_no_state_carries_the_top_level_of_the_math_stratum():
    # Three belongs to the math stratum: it is what its own two formalizers and two checkers give a
    # proved sentence. A question written in a human language adds one fallible step before that, so
    # every state of this stratum is capped below three.
    assert set(decide.STATES) == set(decide.CEILING)
    assert max(decide.CEILING.values()) == 2
    for state in decide.STATES:
        assert decide.CEILING[state] < 3, state


def test_two_agreeing_readers_take_the_minimum_with_the_math_answer():
    readings = [_reading("evaluate 2+2"), _reading("evaluate 2+2")]
    for status, assurance, expected in (("VERIFIED", 3, 2), ("SINGLE_PROVER", 2, 2), ("UNKNOWN", 0, 0)):
        final = decide.decide(_dossier(readings, {"same": True}, {"ok": True},
                                       _math(status, assurance, "" if status == "UNKNOWN" else "4")))
        assert final["state"] == "AGREED"
        assert final["ceiling"] == 2
        assert final["assurance"] == min(2, assurance), (status, final)
        assert final["math_assurance"] == assurance


def test_a_missing_witness_caps_the_session_at_one():
    readings = [_reading("evaluate 2+2"), {}]
    final = decide.decide(_dossier(readings, None, None, _math()))
    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}, _math()))
    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("evaluate 2+3"), {}]
    final = decide.decide(_dossier(readings, None, {"ok": False}, _math(sentence="evaluate 2+3")))
    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_math"] is False
    assert final["answer"] == ""


def test_a_disagreement_is_never_resolved_by_the_math_stratum():
    readings = [_reading("evaluate 2+2"), _reading("evaluate 2+3")]
    final = decide.decide(_dossier(readings, {"same": False, "why": "text"}, {"ok": True}, _math()))
    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("evaluate 2+2"), _reading("evaluate 2+2")]
    final = decide.decide(_dossier(readings, {"same": True}, {"ok": False}, _math()))
    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("evaluate 2+2"), _reading("evaluate 2+2")]
    final = decide.decide(_dossier(readings, {"same": True}, {"ok": True}, _math(), echo_ok=False))
    assert final["state"] == "TAMPERED"
    assert final["assurance"] == 0
    assert "boundary" in final["reasons"]


def test_a_sentence_already_written_in_the_math_language_passes_through():
    direct = {"kind": "evaluate", "math": "evaluate 2+2", "slots": {"T": "2+2"}}
    final = decide.decide(_dossier([{}, {}], None, {"ok": True}, _math(), direct=direct))
    assert final["state"] == "PASSTHROUGH"
    assert final["ceiling"] == 2
    assert final["assurance"] == 2


def test_no_answer_is_shown_when_the_math_stratum_did_not_answer():
    readings = [_reading("evaluate 2+2"), _reading("evaluate 2+2")]
    final = decide.decide(_dossier(readings, {"same": True}, {"ok": True}, _math("UNKNOWN", 0, "")))
    assert final["assurance"] == 0
    assert final["answer"] == ""
    assert "math_unknown" in final["reasons"]


def test_the_decision_is_pure_and_leaves_the_evidence_as_it_found_it():
    dossier = _dossier([_reading("evaluate 2+2"), _reading("evaluate 2+2")], {"same": True},
                       {"ok": True}, _math())
    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("evaluate 2+2"), _reading("evaluate 2+2")],
                                    {"same": True}, {"ok": True}, _math()))
    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 [])