File size: 11,657 Bytes
a755486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
"""
case_study/run_case_study.py — reproduce every table in the paper.

    python case_study/run_case_study.py

Reads the anonymised datasets in data/ and writes:
    case_study/results/CASE_STUDY.md
    case_study/results/tables.json

The data are real. They were recorded by an automated self-assessment job and
a safety gate on a long-running autonomous agent, then anonymised by
tools/anonymise.py. Nothing here is synthetic, and no value was edited: only
field *names* were neutralised before publication.

Scope note. The production implementations of the three instruments under test
are not part of this repository. Conclusions below are drawn from the recorded
ledgers alone, which is the stronger form of evidence anyway: a ledger is what
the instrument actually emitted, whereas re-running a reimplementation would
only show what a reimplementation does.
"""

from __future__ import annotations

import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
sys.path.insert(0, ROOT)

from greencheck import audit_gate, audit_gate_counts, audit_ledger, get_path, load_jsonl, __version__  # noqa: E402
from greencheck.report import render_markdown  # noqa: E402

DATA = os.path.join(ROOT, "data")
OUT = os.path.join(HERE, "results")


def _last_zero_firing_day():
    """The last day that closed with zero firings, before the first day that fired.

    Straight out of gate_daily.jsonl, which is the file a reader has.
    """
    last = None
    for row in load_jsonl(os.path.join(DATA, "gate_daily.jsonl")):
        if row.get("firings", 0) > 0:
            break
        last = row["date"]
    return last


def audit_ledger_paths(field, subject, count_field=None, fresh_field=None):
    rows = load_jsonl(os.path.join(DATA, "ledger_metric_samples.jsonl"))
    projected = []
    for r in rows:
        item = {"value": get_path(r, field)}
        if count_field:
            item["_count"] = get_path(r, count_field)
        if fresh_field:
            item["_fresh"] = get_path(r, fresh_field)
        projected.append(item)
    return audit_ledger(
        projected,
        field="value",
        subject=subject,
        count_field="_count" if count_field else None,
        fresh_field="_fresh" if fresh_field else None,
    )


def main() -> int:
    os.makedirs(OUT, exist_ok=True)

    ledger = load_jsonl(os.path.join(DATA, "ledger_metric_samples.jsonl"))
    gate_daily = load_jsonl(os.path.join(DATA, "gate_daily.jsonl"))
    gate_summary = json.load(open(os.path.join(DATA, "gate_summary.json"), encoding="utf-8"))

    # --- the three instruments declared by the self-assessment job ---------
    results = [
        audit_ledger_paths(
            "identity.identity_score",
            "self_assessment.identity_score",
            count_field="identity.anchors_total",
        ),
        audit_ledger_paths(
            "memory_recall.recall_rate",
            "self_assessment.memory_recall.recall_rate",
            count_field="memory_recall.sampled",
        ),
        audit_ledger_paths(
            "reflection.reflection_score",
            "self_assessment.reflection_score",
            fresh_field="reflection.fresh",
        ),
        audit_ledger_paths(
            "overall",
            "self_assessment.overall (composite)",
            fresh_field="reflection.fresh",
        ),
    ]

    # --- the guard ---------------------------------------------------------
    # Two windows, because the interesting fact is not the total firing count
    # but the fact that the count was zero for the entire period before anyone
    # constructed an input the guard was known to have to block.
    #
    # The numbers here are the whole-day ones, not the timestamp-precise ones.
    # `events_before_first_fire` needs event-level data, and only aggregates are
    # published, so it cannot be recomputed by a reader: the published daily rows
    # sum to a smaller figure, because the day of the first firing was partly
    # silent. A headline nobody can check is the exact defect this package
    # reports, so the headline is the sum over whole days that ended with zero
    # firings. Same claim, one day coarser, checkable with a for-loop.
    pre_total = gate_summary["events_in_zero_firing_days"]
    pre_health = gate_summary["event_totals_in_zero_firing_days"].get("self_change", 0)
    last_silent_day = _last_zero_firing_day()
    gate_dead = audit_gate_counts(
        total_events=pre_total,
        firings=0,
        health_events=pre_health,
        subject="guard denial path — 13 full days with zero firings",
        window=[gate_summary["window_start_utc"], last_silent_day],
        silent_days_before_first_fire=gate_summary["days_with_zero_firings_before_first_fire"],
    )
    gate_all = audit_gate_counts(
        total_events=gate_summary["total_events"],
        firings=gate_summary["total_firings"],
        health_events=gate_summary["event_totals"].get("self_change", 0),
        subject="guard denial path — full window",
        first_fire=gate_summary["first_fire_utc"],
        note=(
            f"The path has fired {gate_summary['total_firings']} times, the first at "
            f"{gate_summary['first_fire_utc']}. That first firing was produced by a positive "
            "control written specifically to exercise the path, not by ordinary traffic."
        ),
    )
    gate_results = [gate_dead, gate_all]
    all_results = results + gate_results

    # --- composite picture -------------------------------------------------
    n_samples = len(ledger)
    identity_vals = [get_path(r, "identity.identity_score") for r in ledger]
    recall_vals = [get_path(r, "memory_recall.recall_rate") for r in ledger]
    sampled_vals = [get_path(r, "memory_recall.sampled") for r in ledger]
    overall_vals = [get_path(r, "overall") for r in ledger]

    headline = {
        "ledger_samples": n_samples,
        "ledger_window": [ledger[0].get("ts"), ledger[-1].get("ts")] if ledger else [],
        "identity_score_distinct": len(set(map(str, identity_vals))),
        "identity_score_value": sorted(set(map(str, identity_vals))),
        "recall_rate_distinct": len(set(map(str, recall_vals))),
        "samples_examining_nothing": sum(1 for v in sampled_vals if v == 0),
        "overall_distinct": len(set(map(str, overall_vals))),
        "gate_events": gate_summary["total_events"],
        "gate_firings": gate_summary["total_firings"],
        "gate_days_covered": gate_summary["days_covered"],
        "gate_silent_days_before_first_fire": gate_summary["days_with_zero_firings_before_first_fire"],
        "gate_first_fire_utc": gate_summary["first_fire_utc"],
    }

    # --- render ------------------------------------------------------------
    lines = []
    lines.append("# Case study: self-reported instruments on a long-running autonomous agent")
    lines.append("")
    lines.append(f"Generated by `case_study/run_case_study.py` (greencheck {__version__}).")
    lines.append("")
    lines.append("## Data")
    lines.append("")
    lines.append(f"- **Metric ledger** — {n_samples} samples recorded in production, "
                 f"{headline['ledger_window'][0]} → {headline['ledger_window'][1]}.")
    lines.append(f"- **Guard log** — {gate_summary['total_events']} events over "
                 f"{gate_summary['days_covered']} days, aggregated to daily counts before release.")
    lines.append("")
    lines.append("Every instrument below was written and deployed by the agent itself, to check")
    lines.append("itself. No external observer chose them, their inputs, or their thresholds.")
    lines.append("That is the condition under test.")
    lines.append("")
    lines.append("## Result summary")
    lines.append("")
    lines.append("| instrument | n | verdict |")
    lines.append("|---|---:|---|")
    for r in all_results:
        v = "PASS" if r.ok else "; ".join(r.verdicts)
        lines.append(f"| `{r.subject}` | {r.n_samples:,} | **{v}** |")
    lines.append("")
    lines.append(render_markdown(all_results, title="Findings in detail", level=2))
    lines.append("")
    lines.append("## What the failures have in common")
    lines.append("")
    lines.append("Each instrument was correct code. None crashed. None returned nonsense.")
    lines.append("Each produced a number on schedule, and each number was read.")
    lines.append("What none of them did was vary with the thing it was named after:")
    lines.append("")
    lines.append("- `identity_score` reported 1.0 in every one of the 84 samples. A constant")
    lines.append("  cannot carry information about anything.")
    lines.append(f"- `recall_rate` reported 0.0 in every sample. {headline['samples_examining_nothing']} of")
    lines.append("  those samples examined nothing at all, and reported the same value as samples")
    lines.append("  that examined eight items and matched none.")
    lines.append("- `overall` and `reflection_score` each took two values across the ledger, and")
    lines.append("  the split between them is exactly the split on one boolean. Neither is an")
    lines.append("  independent finding: they are the same bit, reported twice.")
    lines.append(f"- The guard logged {pre_health:,} healthy events over")
    lines.append(f"  {gate_summary['days_with_zero_firings_before_first_fire']} days and fired 0 times.")
    lines.append("")
    lines.append("## Falsifiers")
    lines.append("")
    lines.append("These results are stated so that they can be wrong:")
    lines.append("")
    lines.append("1. If any instrument above is shown to separate inputs within the dimension it")
    lines.append("   claims to measure, the corresponding finding is false.")
    lines.append("2. If the guard is shown to have fired before "
                 f"{gate_summary['first_fire_utc']} on an input that should have been")
    lines.append("   blocked, the DEAD_GATE finding is false.")
    lines.append("3. If the anonymisation changed any value (as opposed to a field name), every")
    lines.append("   number here is void. `tools/anonymise.py` is the only transform applied;")
    lines.append("   it rewrites keys and aggregates the guard log, and its leak check is mandatory.")
    lines.append("")

    md = "\n".join(lines)
    with open(os.path.join(OUT, "CASE_STUDY.md"), "w", encoding="utf-8", newline="\n") as fh:
        fh.write(md + "\n")

    payload = {
        "mdt_version": __version__,
        "headline": headline,
        "metric_results": [r.to_dict() for r in results],
        "gate_results": [g.to_dict() for g in gate_results],
    }
    with open(os.path.join(OUT, "tables.json"), "w", encoding="utf-8", newline="\n") as fh:
        json.dump(payload, fh, indent=2, ensure_ascii=False)
        fh.write("\n")

    print(f"wrote {os.path.join(OUT, 'CASE_STUDY.md')}")
    print(f"wrote {os.path.join(OUT, 'tables.json')}")
    print()
    print(f"ledger samples        : {n_samples}")
    print(f"identity_score values : {headline['identity_score_value']} ({headline['identity_score_distinct']} distinct)")
    print(f"recall_rate distinct  : {headline['recall_rate_distinct']}")
    print(f"overall distinct      : {headline['overall_distinct']}")
    print(f"gate events / firings : {headline['gate_events']} / {headline['gate_firings']}")
    print(f"gate silent days      : {headline['gate_silent_days_before_first_fire']}")
    flagged = sum(1 for r in all_results if not r.ok)
    print(f"instruments flagged   : {flagged}/{len(all_results)}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())