-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engine.py
More file actions
242 lines (202 loc) · 8.19 KB
/
test_engine.py
File metadata and controls
242 lines (202 loc) · 8.19 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
RSI Loop validation harness — Validated edition.
Pipeline:
1. Run `detector.assess` against every scenario in benchmarks.json and
compute accuracy / precision / recall / F1.
2. If accuracy >= ACCURACY_GATE, hand control to the regulatory Auditor,
which checks the AI-tuned thresholds against the Clinical Gold
Standard. This is what prevents reward hacking.
3. Print a final RSI Loop status. Only COMPLETE when both stages pass.
Exit codes:
0 COMPLETE — Accurate AND Compliant.
1 NOT_ACCURATE — Benchmarks under the accuracy gate. Auditor not run.
2 NOT_COMPLIANT — Accuracy passed but thresholds fall outside clinical
norms; the loop is suspected of reward-hacking.
"""
from __future__ import annotations
import json
import sys
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import List, Optional
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
import auditor
import detector
BENCHMARKS_PATH = Path(__file__).parent / "benchmarks.json"
LAST_RUN_PATH = Path(__file__).parent / "last_run.json"
ACCURACY_GATE: float = 0.90 # below this we skip the audit entirely
@dataclass
class CaseResult:
scenario_id: str
expected: str
predicted: str
forward_head_metric: float
wrist_deviation_metric: float
triggered_rules: List[str]
notes: str
@property
def passed(self) -> bool:
return self.expected == self.predicted
@dataclass
class Metrics:
total: int
correct: int
accuracy: float
precision: float
recall: float
f1: float
@classmethod
def from_results(cls, results: List[CaseResult]) -> "Metrics":
total = len(results)
correct = sum(1 for r in results if r.passed)
tp = sum(1 for r in results if r.expected == "High Strain" and r.predicted == "High Strain")
fp = sum(1 for r in results if r.expected == "Safe" and r.predicted == "High Strain")
fn = sum(1 for r in results if r.expected == "High Strain" and r.predicted == "Safe")
precision = tp / (tp + fp) if (tp + fp) else 1.0
recall = tp / (tp + fn) if (tp + fn) else 1.0
f1 = (2 * precision * recall / (precision + recall)) if (precision + recall) else 0.0
return cls(
total=total,
correct=correct,
accuracy=correct / total if total else 0.0,
precision=precision,
recall=recall,
f1=f1,
)
def run() -> int:
console = Console()
benchmarks = json.loads(BENCHMARKS_PATH.read_text())
scenarios = benchmarks["scenarios"]
results: List[CaseResult] = []
for sc in scenarios:
assessment = detector.assess(sc["landmarks"])
results.append(
CaseResult(
scenario_id=sc["id"],
expected=sc["label"],
predicted=assessment.label,
forward_head_metric=assessment.forward_head_metric,
wrist_deviation_metric=assessment.wrist_deviation_metric,
triggered_rules=list(assessment.triggered_rules),
notes=sc.get("notes", ""),
)
)
metrics = Metrics.from_results(results)
_render_log(console, results, metrics)
audit_report: Optional[auditor.AuditReport] = None
accurate = metrics.accuracy >= ACCURACY_GATE
if accurate:
console.rule("[bold cyan]Stage 2 — Regulatory Audit")
audit_report = auditor.run_audit()
auditor.render_report(audit_report, console=console)
else:
console.print(
f"[bold red]Accuracy {metrics.accuracy:.0%} is below the "
f"{ACCURACY_GATE:.0%} gate — auditor skipped.[/]"
)
final_status, exit_code = _final_status(metrics, audit_report)
_render_final(console, final_status)
_persist(results, metrics, audit_report, final_status)
return exit_code
def _final_status(
metrics: Metrics, audit_report: Optional[auditor.AuditReport]
) -> tuple[str, int]:
if metrics.accuracy < ACCURACY_GATE:
return "NOT_ACCURATE", 1
assert audit_report is not None
if not audit_report.is_compliant:
return "NOT_COMPLIANT", 2
return "COMPLETE", 0
# ---------------------------------------------------------------------------
# Rendering
# ---------------------------------------------------------------------------
def _render_log(console: Console, results: List[CaseResult], metrics: Metrics) -> None:
title = Text("THE RSI LOOP — Self-Improvement Log", style="bold magenta")
console.print(Panel(title, expand=False, border_style="magenta"))
table = Table(show_header=True, header_style="bold cyan", expand=True)
table.add_column("Scenario", style="white", no_wrap=True)
table.add_column("Expected")
table.add_column("Predicted")
table.add_column("Fwd-head", justify="right")
table.add_column("Wrist-dev", justify="right")
table.add_column("Triggered", style="yellow")
table.add_column("Result", justify="center")
for r in results:
result_marker = Text("PASS", style="bold green") if r.passed else Text("FAIL", style="bold red")
predicted_style = "green" if r.passed else "red"
table.add_row(
r.scenario_id,
r.expected,
Text(r.predicted, style=predicted_style),
f"{r.forward_head_metric:+.3f}",
f"{r.wrist_deviation_metric:+.3f}",
", ".join(r.triggered_rules) or "—",
result_marker,
)
console.print(table)
summary_style = "bold green" if metrics.accuracy == 1.0 else "bold red"
summary = Text.assemble(
("Accuracy ", "white"), (f"{metrics.accuracy:.0%} ", summary_style),
("Precision ", "white"), (f"{metrics.precision:.0%} ", summary_style),
("Recall ", "white"), (f"{metrics.recall:.0%} ", summary_style),
("F1 ", "white"), (f"{metrics.f1:.2f}", summary_style),
)
console.print(Panel(summary, border_style=summary_style.split()[-1], expand=False))
failed = [r for r in results if not r.passed]
if failed:
console.rule("[bold red]Failures requiring analysis")
for r in failed:
console.print(
Panel(
Text.assemble(
(f"{r.scenario_id}\n", "bold"),
(f" expected={r.expected} predicted={r.predicted}\n", "white"),
(f" fwd-head={r.forward_head_metric:+.3f} wrist-dev={r.wrist_deviation_metric:+.3f}\n", "yellow"),
(f" notes: {r.notes}", "italic dim"),
),
border_style="red",
expand=False,
)
)
_FINAL_STYLES = {
"COMPLETE": "bold green",
"NOT_ACCURATE": "bold red",
"NOT_COMPLIANT": "bold yellow",
}
def _render_final(console: Console, status: str) -> None:
style = _FINAL_STYLES[status]
msg = {
"COMPLETE": "Accurate AND compliant — the validated RSI Loop is COMPLETE.",
"NOT_ACCURATE": "Benchmarks under the accuracy gate; improve detector and re-run.",
"NOT_COMPLIANT": "Thresholds fall outside clinical norms — possible reward hacking. Reject this iteration.",
}[status]
console.print(Panel(Text(f"RSI LOOP STATUS: {status}\n{msg}", style=style),
border_style=style.split()[-1], expand=False))
# ---------------------------------------------------------------------------
# Persistence
# ---------------------------------------------------------------------------
def _persist(
results: List[CaseResult],
metrics: Metrics,
audit_report: Optional[auditor.AuditReport],
final_status: str,
) -> None:
payload = {
"final_status": final_status,
"metrics": asdict(metrics),
"results": [asdict(r) for r in results],
"audit": (
{
"overall": audit_report.overall,
"findings": [asdict(f) for f in audit_report.findings],
}
if audit_report is not None
else None
),
}
LAST_RUN_PATH.write_text(json.dumps(payload, indent=2))
if __name__ == "__main__":
sys.exit(run())