Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
218 lines (155 loc) · 6.94 KB
/
Copy pathtest_cli.py
File metadata and controls
218 lines (155 loc) · 6.94 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""The `pab` command line, driven through typer's CliRunner."""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from typer.testing import CliRunner
from pab.cli import app
from pab.defense import defense_names
from pab.score import Report
CORPUS_DIR = Path(__file__).resolve().parent.parent / "traces"
pytestmark = pytest.mark.skipif(
not CORPUS_DIR.is_dir() or not any(CORPUS_DIR.glob("*.json")),
reason="no harvested corpus checked in",
)
@pytest.fixture
def runner() -> CliRunner:
return CliRunner()
def run(runner: CliRunner, *args: str):
result = runner.invoke(app, list(args))
if result.exception and not isinstance(result.exception, SystemExit):
raise result.exception
return result
# ---------------------------------------------------------------------------
# pab defenses
# ---------------------------------------------------------------------------
def test_defenses_lists_everything_registered(runner):
result = run(runner, "defenses")
assert result.exit_code == 0
for name in defense_names():
assert name in result.stdout
assert "provenance-tracker" in result.stdout
assert str(len(defense_names())) in result.stdout
# ---------------------------------------------------------------------------
# pab eval
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("defense", ["passthrough", "channel-tagger", "provenance-tracker"])
def test_eval_prints_every_section(runner, defense):
result = run(runner, "eval", "--defense", defense, "--split", "smoke")
assert result.exit_code == 0
for section in (
"SAFETY AND COST",
"UAR",
"FBR",
"HRR",
"TIER ATTRIBUTION ACCURACY BY HOP COUNT",
"PER-CHANNEL ATTRIBUTION",
"CALIBRATION",
):
assert section in result.stdout, f"{defense}: missing section {section!r}"
assert f"defense={defense}" in result.stdout
assert "split=smoke" in result.stdout
def test_eval_prints_coverage_beside_accuracy(runner):
"""A high accuracy on low coverage must not be readable as a high score."""
result = run(runner, "eval", "--defense", "provenance-tracker")
assert "coverage" in result.stdout
assert "accuracy x coverage" in result.stdout
assert "of all instructions attributed to the right tier" in result.stdout
def test_eval_shows_the_hop_count_columns(runner):
result = run(runner, "eval", "--defense", "channel-tagger")
for column in ("0 hop", "1 hop", "2 hop"):
assert column in result.stdout
def test_eval_reports_chain_f1_and_calibration(runner):
result = run(runner, "eval", "--defense", "provenance-tracker")
assert "chainF1" in result.stdout
assert "spearman rho" in result.stdout
assert "monotonic" in result.stdout
def test_eval_handles_a_defense_that_attributes_nothing(runner):
result = run(runner, "eval", "--defense", "passthrough")
assert result.exit_code == 0
assert "nothing attributed" in result.stdout
assert "--" in result.stdout
def test_eval_json_emits_a_valid_report(runner):
result = run(runner, "eval", "--defense", "channel-tagger", "--json")
assert result.exit_code == 0
report = Report.from_json(result.stdout)
assert report.defense == "channel-tagger"
payload = json.loads(result.stdout)
assert "safety" in payload and "attribution" in payload
assert "overall_score" not in payload
def test_eval_accepts_a_seed(runner):
seeded = run(runner, "eval", "--defense", "provenance-tracker", "--seed", "7")
assert seeded.exit_code == 0
assert "seed=7" in seeded.stdout
def test_eval_rejects_an_unknown_defense(runner):
result = run(runner, "eval", "--defense", "nope")
assert result.exit_code != 0
assert "no defense registered" in result.output
assert "passthrough" in result.output
def test_eval_rejects_an_unknown_split(runner):
result = run(runner, "eval", "--defense", "passthrough", "--split", "nope")
assert result.exit_code != 0
assert "unknown split" in result.output
assert "smoke" in result.output
# ---------------------------------------------------------------------------
# pab compare
# ---------------------------------------------------------------------------
def test_compare_covers_every_registered_defense(runner):
result = run(runner, "compare", "--split", "smoke")
assert result.exit_code == 0
for name in defense_names():
assert name in result.stdout
for section in (
"SAFETY AND COST",
"TIER ATTRIBUTION ACCURACY BY HOP COUNT",
"DECLARED-LEVEL DETAIL AND CALIBRATION",
):
assert section in result.stdout
def test_compare_shows_the_coverage_columns(runner):
result = run(runner, "compare")
assert "cover" in result.stdout
assert "of all" in result.stdout
def test_compare_separates_safety_from_cost(runner):
"""Passthrough is maximally unsafe and free; the tagger is safe and costly."""
result = run(runner, "compare")
safety_section = result.stdout.split("TIER ATTRIBUTION ACCURACY")[0]
rows = {
line.split()[0]: line
for line in safety_section.splitlines()
if line.strip() and not line.startswith((" ", "-"))
}
# Passthrough: every unauthorized instruction acted on, nothing refused.
assert "1.00" in rows["passthrough"] and "7/7 unsafe acts" in rows["passthrough"]
# ChannelTagger: perfectly safe, and it refused most legitimate work.
assert "0.80" in rows["channel-tagger"]
assert "4/5 legit refused" in rows["channel-tagger"]
# ---------------------------------------------------------------------------
# pab floors
# ---------------------------------------------------------------------------
def test_floors_passes_on_the_checked_in_corpus(runner):
result = run(runner, "floors")
assert result.exit_code == 0
assert "ORIGIN-LEAK DETECTOR" in result.stdout
assert "floor(s) held" in result.stdout
assert "FAIL" not in result.stdout
def test_floors_prints_the_measured_numbers_per_bin(runner):
result = run(runner, "floors")
assert "measured" in result.stdout
assert "hop(s):" in result.stdout, "per-bin detail is missing"
assert "correct)" in result.stdout
assert result.stdout.count("[PASS]") == 4
def test_floors_rejects_an_unknown_split(runner):
result = run(runner, "floors", "--split", "nope")
assert result.exit_code != 0
assert "unknown split" in result.output
# ---------------------------------------------------------------------------
# Plumbing
# ---------------------------------------------------------------------------
def test_help_lists_every_subcommand(runner):
result = run(runner, "--help")
assert result.exit_code == 0
for command in ("eval", "compare", "floors", "defenses"):
assert command in result.stdout
def test_no_arguments_shows_help_rather_than_failing_obscurely(runner):
result = runner.invoke(app, [])
assert "Usage" in result.output