File size: 5,202 Bytes
bbfee07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The faults this bundle can switch on, and what each of them is required to leave behind.

The catalogue is the counterpart of the one the geometry layer runs: here the misbehaviour is in the
reading and in the reporting, never in the formal layer. A fault is worth having only if the stratum
reports it, so every experiment below ends by asking the same question -- did the assurance fall, and
did the reason name the node that fell short?
"""

import pytest

from lggeostratum import agents, canonical, corpus, faults, pipeline

ITEMS = {item["id"]: item for item in corpus.load()}
ONE = "perp-it-true"
# The readings the faults of a reader are tried on: a line drawn through two named points, and a
# point given by its coordinates. One carries the names a reader can write twice or exchange for
# another, the other the number a reader can shift.
FIXTURES = [
    ("define_line", {"E": "r", "P": "A", "Q": "B"}),
    ("define_point", {"P": "A", "V": "(0, 0)"}),
]


def _ask(item_id, fault_spec=(), active=agents.ORDER):
    item = ITEMS[item_id]
    return pipeline.ask(str(item["question"]), str(item["lang"]), active=active, fault_spec=fault_spec)


def test_the_catalogue_names_nodes_of_this_topology_only():
    assert set(faults.CATALOG) <= set(agents.ORDER)
    # The invoker is not in the catalogue: it reads nothing and proves nothing, it only asks the geometry
    # layer a question, so there is no shortcut it could take.
    assert "invoker" not in faults.CATALOG
    faults.configure(())
    for node, kinds in faults.CATALOG.items():
        assert kinds, node
        assert len(set(kinds)) == len(kinds), node
        for kind in kinds:
            assert not faults.has(node, kind), (node, kind)


def test_a_fault_configured_is_a_fault_fired_once():
    faults.configure(["reader_a:merge_slots", "reader_a:merge_slots", "coverage:rubber_stamp"])
    assert faults.has("reader_a", "merge_slots")
    faults.fire("reader_a", "merge_slots")
    faults.fire("reader_a", "merge_slots")
    assert faults.fired("reader_a") == ["reader_a:merge_slots"]
    # Configured is not the same as fired: only a fault that really changed something is recorded, so
    # the ablation can tell an activated fault from a dormant one.
    assert faults.has("coverage", "rubber_stamp")
    assert "coverage:rubber_stamp" not in faults.FIRED
    assert faults.FIRED == ["reader_a:merge_slots"]
    faults.configure(())
    assert not faults.has("reader_a", "merge_slots")


def test_the_reader_faults_damage_a_reading_that_carries_them():
    # Every fault of a reader has something to bite on in one of the two readings above. Each one
    # leaves a sentence the geometry layer would happily solve, which is what makes the experiment
    # measure the readers and not the parser of the geometry layer.
    for fault in faults.CATALOG["reader_a"]:
        bitten = []
        for kind, base in FIXTURES:
            order = canonical.slots_for(kind)
            faults.configure(["reader_a:" + fault])
            changed, was_changed = faults.mutate_reading("reader_a", kind, base, order)
            fired = list(faults.fired("reader_a"))
            faults.configure(())
            if not was_changed:
                continue
            bitten.append(kind)
            assert changed != base, (fault, kind)
            assert fired == ["reader_a:" + fault], (fault, kind, fired)
            sentence = canonical.print_geometry(kind, changed)
            assert canonical.balanced(sentence) is True, (fault, kind, sentence)
        assert bitten, fault


def test_a_reader_that_shifts_a_number_is_found_out_by_the_other_reader():
    honest = _ask(ONE)
    shifted = _ask(ONE, ["reader_a:shift_number"])
    assert honest["final"]["assurance"] > 0
    assert shifted["final"]["state"] == "DISPUTED", shifted["final"]
    assert shifted["final"]["assurance"] == 0
    assert shifted["final"]["answer"] == ""
    assert "disagree" in shifted["final"]["reasons"] or "literal_count" in shifted["final"]["reasons"]


def test_a_geometry_layer_that_answers_another_sentence_is_reported_as_tampering():
    tampered = _ask(ONE, ["boundary:tamper_math"])
    assert tampered["final"]["state"] == "TAMPERED", tampered["final"]
    assert tampered["final"]["assurance"] == 0
    assert tampered["final"]["answer"] == ""
    assert "boundary" in tampered["final"]["reasons"]


def test_an_arbiter_that_lifts_the_assurance_is_overruled_by_the_tools():
    honest = _ask(ONE)
    forced = _ask(ONE, ["arbiter:force_assurance"])
    assert any(record["claimed"] != record["referee"] for record in forced["records"]), forced["records"]
    assert forced["final"]["assurance"] <= honest["final"]["assurance"], forced["final"]
    assert forced["final"]["assurance"] <= 2
    assert "arbiter" in (forced["final"].get("detected") or [])


@pytest.mark.parametrize("node", sorted(faults.CATALOG))
def test_no_single_fault_lifts_the_assurance_above_the_honest_one(node):
    honest = _ask(ONE)
    for kind in faults.CATALOG[node]:
        outcome = _ask(ONE, ["%s:%s" % (node, kind)])
        shown = outcome["final"]
        assert shown["assurance"] <= min(2, honest["final"]["assurance"]), (node, kind, shown)