"""Check four README claims against the committed evidence files.
A claim only passes if the number can be derived from the artifact itself."""
import json
from collections import Counter
from pathlib import Path

def load(p):
    return json.loads(Path(p).read_text(encoding='utf-8-sig'))  # two of three files carry a BOM

m5 = load('experiments/m5/f01-f11-results.json')
local = load('experiments/portfolio/local-recovery/local-410041/summary.json')
aws = load('experiments/portfolio/cloud-recovery/dur049-aws-20260923-full-0adbac0/network-isolation.json')

def report(name, ok, detail):
    print(f"[{'PASS' if ok else 'FAIL'}] {name}\n       {detail}")

# 1. 48 fault cases passed, as 16 boundaries x 3 seeds, by controller, Go test AND checker
statuses = Counter((c['status'], c['controller_status'], c['go_status'], c['checker_status']) for c in m5)
cases = {c['case_id'] for c in m5}; seeds = sorted({c['seed'] for c in m5})
checker_json = Counter(c['checker_output'] for c in m5)
report('48 fault cases passed', len(m5) == 48 and statuses == {('PASS',) * 4: 48} and len(cases) == 16 and len(seeds) == 3,
       f"entries={len(m5)} distinct_cases={len(cases)} seeds={seeds} status_tuples={dict(statuses)}")
print(f"       checker_output values: {dict(checker_json)}")

# 2 + 3. takeovers and fenced writes: the summary total must equal the sum of its arms
arms = local['arms']
t = sum(a['takeovers'] for a in arms); f = sum(a['fenced_old_owner_writes'] for a in arms)
report('60 takeovers', local['safety']['takeovers'] == 60 == t, f"safety.takeovers={local['safety']['takeovers']} sum_of_arms={t} arms={[a['fault_type'] for a in arms]}")
report('180 stale-owner writes fenced', local['safety']['fenced_old_owner_writes'] == 180 == f,
       f"safety.fenced_old_owner_writes={local['safety']['fenced_old_owner_writes']} sum_of_arms={f} false_takeovers={local['safety']['false_takeovers']}")
print(f"       recorded at commit {local['git_commit'][:12]}; limitations[0]: {local['limitations'][0]}")

# 4. late worker result rejected as STALE_ATTEMPT, and the durable rows agree with the log line
rej = aws['result']['original_worker_late_result_rejection']; fence = aws['result']['durable_invariant_checker']['fence']
report('late worker result rejected as STALE_ATTEMPT',
       rej['api_rejection'] == 'STALE_ATTEMPT' and 'status=409 code=STALE_ATTEMPT' in rej['submission_log_line']
       and fence['stale_attempt_found'] and not fence['stale_attempt_current'] and not fence['stale_result_recorded'],
       f"api_rejection={rej['api_rejection']} stale_attempt_current={fence['stale_attempt_current']} "
       f"stale_result_recorded={fence['stale_result_recorded']} superseded_epoch_transitions={fence['superseded_epoch_transitions_after_recovery_boundary']}")
