File size: 4,634 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
"""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 math stratum runs: here the misbehaviour is in the
reading and in the reporting, never in the mathematics. 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 lgstratum import agents, canonical, corpus, faults, pipeline

ITEMS = {item["id"]: item for item in corpus.load()}
ONE = "it-01"


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 math
    # stratum 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():
    # A reading of an integral carries four slots, and three of them hold a number: every fault of a
    # reader has something to bite on. Each one leaves a sentence the math stratum would happily
    # solve, which is what makes the experiment measure the readers and not the parser.
    kind = "integral"
    order = canonical.slots_for(kind)
    base = {name: "1" + name for name in order}
    for fault in faults.CATALOG["reader_a"]:
        faults.configure(["reader_a:" + fault])
        changed, was_changed = faults.mutate_reading("reader_a", kind, base, order)
        assert was_changed is True, fault
        assert changed != base, fault
        assert faults.fired("reader_a") == ["reader_a:" + fault], fault
        assert canonical.balanced(canonical.print_math(kind, changed)) is True, fault
    faults.configure(())


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_math_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)