File size: 5,294 Bytes
3a0a69b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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 physics. 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 lgphysstratum import agents, canonical, corpus, faults, pipeline

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

_FULL_SLOTS = {"convert": {"N": "3", "U": "km", "V": "m"},
               "audit": {"F": "force*time=mass*speed"},
               "pi": {"T": "period", "S": "mass, stiffness"}}


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 _slots(kind, order):
    return {name: _FULL_SLOTS[kind][name] for name in order if name in _FULL_SLOTS[kind]}


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
    # physics 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:shift_number", "reader_a:shift_number", "coverage:rubber_stamp"])
    assert faults.has("reader_a", "shift_number")
    faults.fire("reader_a", "shift_number")
    faults.fire("reader_a", "shift_number")
    assert faults.fired("reader_a") == ["reader_a:shift_number"]
    # 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:shift_number"]
    faults.configure(())
    assert not faults.has("reader_a", "shift_number")


def test_the_reader_faults_damage_a_reading_that_carries_them():
    # A convert sentence carries a number and two units, a dimensional analysis carries a list of
    # quantities: the three faults of a reader bite on different kinds of question, and each of them
    # leaves a sentence the physics layer would happily answer. Otherwise the fault would be caught by
    # the layer refusing to parse, and the experiment would measure nothing about the readers.
    for fault in faults.CATALOG["reader_a"]:
        found = None
        for kind in canonical.KINDS:
            order = canonical.slots_for(kind)
            base = _slots(kind, order)
            faults.configure(["reader_a:" + fault])
            changed, was_changed = faults.mutate_reading("reader_a", kind, base, order)
            recorded = list(faults.fired("reader_a"))
            faults.configure(())
            if was_changed:
                found = (kind, base, changed, recorded)
                break
        assert found is not None, fault
        kind, base, changed, recorded = found
        assert changed != base, fault
        assert recorded == ["reader_a:" + fault], fault
        assert canonical.balanced(canonical.print_math(kind, changed)) is True, 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_physics_layer_that_answers_another_sentence_is_reported_as_tampering():
    tampered = _ask(ONE, ["boundary:tamper_physics"])
    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)