Jump to content

The Principia Machina/TPC-PY/gen score corpus.py

From The Invariance Itself
Revision as of 04:32, 28 September 2026 by BFP Rauch (talk | contribs) (1 revision imported)
"""gen_score_corpus 0.1.0: builds score_corpus.json for tpc_score 0.1.0.
Deterministic (no random draw). Expectations are computed by
tpc_score.py and pinned; hand_checked cases carry a closed form checked
here against the module before pinning (the pad's Beta(21, 1) mean 21/22
and the handoff's 16.3 table to two places; a follower's step 0.2 x 0.375
x 1 x (0.8 - 0.5) = 0.0225; the Prime's 0.2 x 0.2 x 0.5 x 0.1 x 0.2 =
0.0004; the cut 0.9 x 0.75; the inquiry cost 0.01 x 2^(k-1); the state
table of D129.1 on every pending set the pause names). The params
fixture is the fleet defaults of PMP-TPM 0.12.0 (params_fixture.FLEET),
varied in cases."""
import json
import os
import sys

here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, here)
from params_fixture import FLEET, fleet  # noqa: E402
from tpc_score import run_case  # noqa: E402
from tpc_beta import canon_float  # noqa: E402

cases = []
P = FLEET


def add(name, fn, args, hand=False, check=None):
    got = run_case(fn, args)
    if check is not None:
        assert got == check, (name, got, check)
    cases.append({"name": name, "fn": fn, "args": args, "hand_checked": hand,
                  "expected": got})


def near(name, fn, args, value, field=0, tol=1e-9):
    got = run_case(fn, args)
    v = float(got.split("\n")[0].split("|")[field])
    assert abs(v - value) <= tol * max(1.0, abs(value)), (name, got, value)
    cases.append({"name": name, "fn": fn, "args": args, "hand_checked": True,
                  "expected": got})


def ev(kind, w, age, pad=None):
    e = {"kind": kind, "weight": w, "age": age}
    if pad is not None:
        e["pad"] = pad
    return e


# ---- diversity_quota (DQ1-DQ3) ---------------------------------------------
W0 = []
near("Q01 empty window: the first match admits c_max", "quota",
     {"window": W0, "counterpart": "b", "g": 1, "c_max": 0.25}, 0.25)
near("Q02 one prior match from b: q = 0.25 x 1.25 - 0.25", "quota",
     {"window": [{"counterpart": "b", "weight": 0.25}], "counterpart": "b", "g": 1,
      "c_max": 0.25}, 0.0625)
near("Q03 a pad of 20 lets a first counterpart match admit fully", "quota",
     {"window": [{"counterpart": None, "weight": 20}], "counterpart": "b", "g": 1,
      "c_max": 0.25}, 1.0)
near("Q04 four counterparts equal: q under 1", "quota",
     {"window": [{"counterpart": "b", "weight": 1}, {"counterpart": "c", "weight": 1},
                 {"counterpart": "d", "weight": 1}, {"counterpart": "e", "weight": 1}],
      "counterpart": "b", "g": 1, "c_max": 0.25}, 0.25)
near("Q05 five counterparts: full admission", "quota",
     {"window": [{"counterpart": x, "weight": 1} for x in "bcdef"], "counterpart": "b",
      "g": 1, "c_max": 0.25}, 0.5)
near("Q06 counterpart over the quota: zero", "quota",
     {"window": [{"counterpart": "b", "weight": 3}, {"counterpart": "c", "weight": 1}],
      "counterpart": "b", "g": 1, "c_max": 0.25}, 0.0)
near("Q07 g_F 2 halves the fraction", "quota",
     {"window": [{"counterpart": "c", "weight": 4}], "counterpart": "b", "g": 2,
      "c_max": 0.25}, 0.75)
near("Q08 c_max 0.5", "quota", {"window": [], "counterpart": "b", "g": 1, "c_max": 0.5}, 0.5)
near("Q09 aged weights in the window", "quota",
     {"window": [{"counterpart": "b", "weight": 0.125}, {"counterpart": "c", "weight": 0.7}],
      "counterpart": "b", "g": 1, "c_max": 0.25}, 0.33125)
near("Q10 a zero-weight prior match", "quota",
     {"window": [{"counterpart": "b", "weight": 0}], "counterpart": "b", "g": 1,
      "c_max": 0.25}, 0.25)
add("Q11 g 0 refused", "quota", {"window": [], "counterpart": "b", "g": 0, "c_max": 0.25}, True,
    "ERROR:g")
add("Q12 c_max 1 refused", "quota", {"window": [], "counterpart": "b", "g": 1, "c_max": 1}, True,
    "ERROR:c_max")
add("Q13 c_max 0 refused", "quota", {"window": [], "counterpart": "b", "g": 1, "c_max": 0}, True,
    "ERROR:c_max")
add("Q14 empty counterpart refused", "quota", {"window": [], "counterpart": "", "g": 1,
                                               "c_max": 0.25}, True, "ERROR:counterpart")
add("Q15 window not a list refused", "quota", {"window": {}, "counterpart": "b", "g": 1,
                                               "c_max": 0.25}, True, "ERROR:window")
add("Q16 negative weight refused", "quota",
    {"window": [{"counterpart": "b", "weight": -1}], "counterpart": "b", "g": 1, "c_max": 0.25},
    True, "ERROR:window")
add("Q17 boolean weight refused", "quota",
    {"window": [{"counterpart": "b", "weight": True}], "counterpart": "b", "g": 1, "c_max": 0.25},
    True, "ERROR:window")
add("Q18 extra key refused", "quota",
    {"window": [{"counterpart": "b", "weight": 1, "x": 1}], "counterpart": "b", "g": 1,
     "c_max": 0.25}, True, "ERROR:window")
add("Q19 g a string refused", "quota", {"window": [], "counterpart": "b", "g": "1",
                                        "c_max": 0.25}, True, "ERROR:g")
near("Q20 large window", "quota",
     {"window": [{"counterpart": "c%d" % i, "weight": 0.5} for i in range(40)],
      "counterpart": "c1", "g": 1, "c_max": 0.25}, 1.0)
near("Q21 g fractional 0.5", "quota",
     {"window": [{"counterpart": "c", "weight": 1}], "counterpart": "b", "g": 0.5,
      "c_max": 0.25}, 0.75)

# ---- evidence (EV1-EV4) ----------------------------------------------------
IDLE0 = {"days": 0}
near("E01 the thin file: no events", "evidence", {"events": [], "idle": IDLE0, "params": P},
     0.5, 3)
near("E02 the reference pad 20: mean 21/22", "evidence",
     {"events": [ev("FOR", 20, 0, True)], "idle": IDLE0, "params": P}, 21.0 / 22.0, 3)
add("E03 the pad's n is 0", "evidence",
    {"events": [ev("FOR", 20, 0, True)], "idle": IDLE0, "params": P}, True)
near("E04 pad 10: mean 11/12", "evidence",
     {"events": [ev("FOR", 10, 0, True)], "idle": IDLE0, "params": P}, 11.0 / 12.0, 3)
near("E05 pad 40: mean 41/42", "evidence",
     {"events": [ev("FOR", 40, 0, True)], "idle": IDLE0, "params": P}, 41.0 / 42.0, 3)
near("E06 one FOR aged a half-life: F = 0.5", "evidence",
     {"events": [ev("FOR", 1, 500)], "idle": IDLE0, "params": P}, 0.5, 0)
near("E07 ten FOR, two AGAINST, no age: E = 11/14", "evidence",
     {"events": [ev("FOR", 1, 0)] * 10 + [ev("AGAINST", 1, 0)] * 2, "idle": IDLE0,
      "params": P}, 11.0 / 14.0, 3)
add("E08 n counts FOR and AGAINST", "evidence",
    {"events": [ev("FOR", 1, 0)] * 10 + [ev("AGAINST", 1, 0)] * 2, "idle": IDLE0, "params": P},
    True)
near("E09 FOR aged two half-lives: 0.25", "evidence",
     {"events": [ev("FOR", 1, 1000)], "idle": IDLE0, "params": P}, 0.25, 0)
near("E10 h(CLEARLY HIGHER) 3 against", "evidence",
     {"events": [ev("AGAINST", 3, 0)], "idle": IDLE0, "params": P}, 3.0, 1)
near("E11 idle at T_idle exactly: factor 1", "evidence",
     {"events": [ev("FOR", 4, 0)], "idle": {"days": 90}, "params": P}, 1.0, 6)
near("E12 idle 180 days: factor 1/2", "evidence",
     {"events": [ev("FOR", 4, 0)], "idle": {"days": 180}, "params": P}, 0.5, 6)
near("E13 idle 180 days: F halves", "evidence",
     {"events": [ev("FOR", 4, 0)], "idle": {"days": 180}, "params": P}, 2.0, 0)
near("E14 idle 450 days: factor 1/5", "evidence",
     {"events": [ev("FOR", 4, 0), ev("AGAINST", 2, 0)], "idle": {"days": 450}, "params": P},
     0.2, 6)
near("E15 fractional idle days", "evidence",
     {"events": [ev("FOR", 1, 0)], "idle": {"days": 100.5}, "params": P},
     1.0 / (1.0 + 10.5 / 90.0), 6)
near("E16 HALF-LIFE varied to 100", "evidence",
     {"events": [ev("FOR", 1, 100)], "idle": IDLE0, "params": fleet({"HALF-LIFE": 100})}, 0.5, 0)
near("E17 prior a0 2, b0 3", "evidence",
     {"events": [], "idle": IDLE0, "params": fleet({"a0": 2, "b0": 3})}, 0.4, 3)
add("E18 kind unknown refused", "evidence",
    {"events": [ev("HELD", 1, 0)], "idle": IDLE0, "params": P}, True, "ERROR:events")
add("E19 negative age refused", "evidence",
    {"events": [ev("FOR", 1, -1)], "idle": IDLE0, "params": P}, True, "ERROR:events")
add("E20 pad not a boolean refused", "evidence",
    {"events": [ev("FOR", 1, 0, 1)], "idle": IDLE0, "params": P}, True, "ERROR:events")
add("E21 idle missing refused", "evidence", {"events": [], "idle": {}, "params": P}, True,
    "ERROR:idle")
add("E22 negative idle refused", "evidence", {"events": [], "idle": {"days": -1}, "params": P},
    True, "ERROR:idle")
add("E23 HALF-LIFE 0 refused", "evidence",
    {"events": [], "idle": IDLE0, "params": fleet({"HALF-LIFE": 0})}, True,
    "ERROR:params:HALF-LIFE")
add("E24 a0 missing refused", "evidence",
    {"events": [], "idle": IDLE0, "params": {k: v for k, v in P.items() if k != "a0"}}, True,
    "ERROR:params:a0")
add("E25 weight a string refused", "evidence",
    {"events": [{"kind": "FOR", "weight": "1", "age": 0}], "idle": IDLE0, "params": P}, True,
    "ERROR:events")
add("E26 mixed pads and events", "evidence",
    {"events": [ev("FOR", 20, 300, True), ev("FOR", 1, 10), ev("AGAINST", 2, 5),
                ev("FOR", 0.25, 0)], "idle": {"days": 3}, "params": P}, False)

# ---- score_interval --------------------------------------------------------
add("I01 width carried over", "interval", {"S": 0.6, "E": 0.7, "lo": 0.5, "hi": 0.9}, True,
    "0.4|0.8")
add("I02 clipped below 0", "interval", {"S": 0.1, "E": 0.5, "lo": 0.025, "hi": 0.975}, True,
    "0|0.575")
add("I03 clipped above 1", "interval", {"S": 0.9, "E": 0.5, "lo": 0.025, "hi": 0.975}, True,
    "0.425|1")
add("I04 thin file at THIN FILE START", "interval", {"S": 0.5, "E": 0.5, "lo": 0.025,
                                                    "hi": 0.975}, True, "0.025|0.975")
add("I05 degenerate interval", "interval", {"S": 0.3, "E": 0.3, "lo": 0.3, "hi": 0.3}, True,
    "0.3|0.3")
add("I06 S 0", "interval", {"S": 0, "E": 0.5, "lo": 0.4, "hi": 0.6}, True, "0|0.1")
add("I07 S 1", "interval", {"S": 1, "E": 0.5, "lo": 0.4, "hi": 0.6}, True, "0.9|1")
add("I08 integer S", "interval", {"S": 1, "E": 1, "lo": 1, "hi": 1}, True, "1|1")
add("I09 S above 1 refused", "interval", {"S": 1.1, "E": 0.5, "lo": 0.4, "hi": 0.6}, True,
    "ERROR:S")
add("I10 lo above E refused", "interval", {"S": 0.5, "E": 0.5, "lo": 0.6, "hi": 0.7}, True,
    "ERROR:interval")
add("I11 hi above 1 refused", "interval", {"S": 0.5, "E": 0.5, "lo": 0.4, "hi": 1.1}, True,
    "ERROR:interval")
add("I12 boolean S refused", "interval", {"S": True, "E": 0.5, "lo": 0.4, "hi": 0.6}, True,
    "ERROR:S")
add("I13 string E refused", "interval", {"S": 0.5, "E": "0.5", "lo": 0.4, "hi": 0.6}, True,
    "ERROR:interval")
add("I14 the reference: S 0.8, pad 20 interval", "interval",
    {"S": 0.8, "E": 0.954545454545, "lo": 0.838902384781, "hi": 0.998795116552}, False)
for i, (s, e, lo, hi) in enumerate([(0.55, 0.6, 0.45, 0.72), (0.2, 0.9, 0.85, 0.95),
                                    (0.7, 0.3, 0.1, 0.5), (0.999, 0.999, 0.998, 1),
                                    (0.001, 0.001, 0, 0.002), (0.42, 0.42, 0.1, 0.9)]):
    add("I%02d sweep" % (15 + i), "interval", {"S": s, "E": e, "lo": lo, "hi": hi}, False)

# ---- standing --------------------------------------------------------------
def iv(lo, hi, ov=None):
    d = {"lo": lo, "hi": hi}
    if ov is not None:
        d["override"] = ov
    return d


add("S01 y entirely above x: CLEARLY HIGHER", "standing", {"x": iv(0.1, 0.5), "y": iv(0.6, 0.9)},
    True, "CLEARLY HIGHER")
add("S02 y entirely below x: CLEARLY LOWER", "standing", {"x": iv(0.6, 0.9), "y": iv(0.1, 0.5)},
    True, "CLEARLY LOWER")
add("S03 overlap: SIMILAR", "standing", {"x": iv(0.1, 0.6), "y": iv(0.5, 0.9)}, True, "SIMILAR")
add("S04 touching endpoints: SIMILAR", "standing", {"x": iv(0.1, 0.5), "y": iv(0.5, 0.9)}, True,
    "SIMILAR")
add("S05 thin file against the pad: SIMILAR", "standing",
    {"x": iv(0.025, 0.975), "y": iv(0.838902384781, 0.998795116552)}, True, "SIMILAR")
add("S06 narrowed veteran below the pad: CLEARLY HIGHER", "standing",
    {"x": iv(0.5, 0.8), "y": iv(0.838902384781, 0.998795116552)}, True, "CLEARLY HIGHER")
add("S07 overridden y never CLEARLY HIGHER", "standing",
    {"x": iv(0.1, 0.5), "y": iv(0.6, 0.9, True)}, True, "SIMILAR")
add("S08 overridden y may be CLEARLY LOWER", "standing",
    {"x": iv(0.6, 0.9), "y": iv(0.1, 0.5, True)}, True, "CLEARLY LOWER")
add("S09 override false explicit", "standing", {"x": iv(0.1, 0.5), "y": iv(0.6, 0.9, False)},
    True, "CLEARLY HIGHER")
add("S10 x overridden changes nothing", "standing",
    {"x": iv(0.1, 0.5, True), "y": iv(0.6, 0.9)}, True, "CLEARLY HIGHER")
add("S11 identical intervals: SIMILAR", "standing", {"x": iv(0.3, 0.7), "y": iv(0.3, 0.7)}, True,
    "SIMILAR")
add("S12 y inside x: SIMILAR", "standing", {"x": iv(0.1, 0.9), "y": iv(0.4, 0.6)}, True,
    "SIMILAR")
add("S13 points", "standing", {"x": iv(0.5, 0.5), "y": iv(0.5000001, 0.5000001)}, True,
    "CLEARLY HIGHER")
add("S14 lo above hi refused", "standing", {"x": iv(0.6, 0.5), "y": iv(0.1, 0.5)}, True,
    "ERROR:interval")
add("S15 hi above 1 refused", "standing", {"x": iv(0.1, 0.5), "y": iv(0.6, 1.5)}, True,
    "ERROR:interval")
add("S16 override not boolean refused", "standing", {"x": iv(0.1, 0.5), "y": iv(0.6, 0.9, 1)},
    True, "ERROR:interval")
add("S17 missing hi refused", "standing", {"x": {"lo": 0.1}, "y": iv(0.6, 0.9)}, True,
    "ERROR:interval")
add("S18 extra key refused", "standing", {"x": iv(0.1, 0.5), "y": {"lo": 0.6, "hi": 0.9, "n": 1}},
    True, "ERROR:interval")
add("S19 x a list refused", "standing", {"x": [0.1, 0.5], "y": iv(0.6, 0.9)}, True,
    "ERROR:interval")
add("S20 integers", "standing", {"x": iv(0, 0), "y": iv(1, 1)}, True, "CLEARLY HIGHER")

# ---- rank ------------------------------------------------------------------
def acc(name, S, lo, hi, n=0, active=True):
    return {"account": name, "S": S, "S_lo": lo, "S_hi": hi, "n": n, "active": active}


A_REF = acc("Prime AI", 0.8, 0.838902384781, 0.998795116552, 0)
A_THIN = acc("DeltaS Zero", 0.5, 0.025, 0.975, 0)
A_VET = acc("CH7 AZ", 0.75, 0.6, 0.85, 40)
A_MID = acc("Phoenix 480", 0.6, 0.4, 0.8, 12)
add("R01 the reference outranks a thin file", "rank", {"accounts": [A_THIN, A_REF], "params": P},
    True, "rank|2|1\nrow|Prime AI|1|1|true\nrow|DeltaS Zero|2|0.5|false")
add("R02 one account: rank 1, in the tier", "rank", {"accounts": [A_THIN], "params": P}, True,
    "rank|1|1\nrow|DeltaS Zero|1|1|true")
add("R03 four accounts: one in the tier", "rank",
    {"accounts": [A_THIN, A_VET, A_REF, A_MID], "params": P}, True)
add("R04 five accounts: two in the tier (ceil)", "rank",
    {"accounts": [A_THIN, A_VET, A_REF, A_MID, acc("Alt", 0.7, 0.5, 0.9, 3)], "params": P}, True)
add("R05 ties by narrower interval", "rank",
    {"accounts": [acc("a", 0.5, 0.4, 0.9), acc("b", 0.5, 0.4, 0.7)], "params": P}, True,
    "rank|2|1\nrow|b|1|1|true\nrow|a|2|0.5|false")
add("R06 ties by larger n", "rank",
    {"accounts": [acc("a", 0.5, 0.4, 0.7, 3), acc("b", 0.5, 0.4, 0.7, 5)], "params": P}, True,
    "rank|2|1\nrow|b|1|1|true\nrow|a|2|0.5|false")
add("R07 ties by account name", "rank",
    {"accounts": [acc("b", 0.5, 0.4, 0.7, 3), acc("a", 0.5, 0.4, 0.7, 3)], "params": P}, True,
    "rank|2|1\nrow|a|1|1|true\nrow|b|2|0.5|false")
add("R08 inactive account has no position", "rank",
    {"accounts": [A_REF, acc("Gone", 0.9, 0.9, 0.95, 9, False), A_THIN], "params": P}, True,
    "rank|2|1\nrow|Prime AI|1|1|true\nrow|DeltaS Zero|2|0.5|false\nrow|Gone|-|-|false")
add("R09 no active account", "rank", {"accounts": [acc("Gone", 0.9, 0.9, 0.95, 9, False)],
                                       "params": P}, True, "rank|0|0\nrow|Gone|-|-|false")
add("R10 empty", "rank", {"accounts": [], "params": P}, True, "rank|0|0")
add("R11 TRUST TIER off: nobody in the tier", "rank",
    {"accounts": [A_THIN, A_REF], "params": fleet({"TRUST TIER": {"on": False, "fraction": 0.25}})},
    True, "rank|2|0\nrow|Prime AI|1|1|false\nrow|DeltaS Zero|2|0.5|false")
add("R12 fraction 0.5 of four: two", "rank",
    {"accounts": [A_THIN, A_VET, A_REF, A_MID],
     "params": fleet({"TRUST TIER": {"on": True, "fraction": 0.5}})}, True)
add("R13 fraction 1: everyone", "rank",
    {"accounts": [A_THIN, A_VET], "params": fleet({"TRUST TIER": {"on": True, "fraction": 1}})},
    True)
add("R14 fraction 0.1 of ten: one", "rank",
    {"accounts": [acc("a%d" % i, 0.5, 0.1 * i, 0.1 * i + 0.05, i) for i in range(10)],
     "params": fleet({"TRUST TIER": {"on": True, "fraction": 0.1}})}, True)
add("R15 fraction 0.3 of ten: three", "rank",
    {"accounts": [acc("a%d" % i, 0.5, 0.1 * i, 0.1 * i + 0.05, i) for i in range(10)],
     "params": fleet({"TRUST TIER": {"on": True, "fraction": 0.3}})}, True)
add("R16 duplicate account refused", "rank", {"accounts": [A_THIN, A_THIN], "params": P}, True,
    "ERROR:accounts")
add("R17 S_lo above S_hi refused", "rank", {"accounts": [acc("a", 0.5, 0.7, 0.6)], "params": P},
    True, "ERROR:accounts")
add("R18 active not boolean refused", "rank",
    {"accounts": [acc("a", 0.5, 0.4, 0.6, 0, 1)], "params": P}, True, "ERROR:accounts")
add("R19 fraction 0 refused", "rank",
    {"accounts": [A_THIN], "params": fleet({"TRUST TIER": {"on": True, "fraction": 0}})}, True,
    "ERROR:params:TRUST TIER")
add("R20 TRUST TIER missing refused", "rank",
    {"accounts": [A_THIN], "params": {k: v for k, v in P.items() if k != "TRUST TIER"}}, True,
    "ERROR:params:TRUST TIER")
add("R21 name with a bar escaped", "rank", {"accounts": [acc("a|b", 0.5, 0.4, 0.6)], "params": P},
    True, "rank|1|1\nrow|a\\|b|1|1|true")

# ---- prime_select ----------------------------------------------------------
add("P01 the reference is Prime", "prime", {"accounts": [A_THIN, A_REF], "override": None,
                                            "params": P}, True, "Prime AI|HIGHEST S IN TIER")
add("P02 one account", "prime", {"accounts": [A_THIN], "override": None, "params": P}, True,
    "DeltaS Zero|HIGHEST S IN TIER")
add("P03 highest S among the tier, not the highest S overall", "prime",
    {"accounts": [A_REF, A_VET, A_THIN, A_MID, acc("Lucky", 0.95, 0.2, 0.99, 1)],
     "override": None, "params": fleet({"TRUST TIER": {"on": True, "fraction": 0.5}})}, True)
add("P04 override names the Prime", "prime",
    {"accounts": [A_THIN, A_REF], "override": "DeltaS Zero", "params": P}, True,
    "DeltaS Zero|OVERRIDE")
add("P05 override of an inactive account refused", "prime",
    {"accounts": [A_REF, acc("Gone", 0.9, 0.9, 0.95, 9, False)], "override": "Gone", "params": P},
    True, "ERROR:override")
add("P06 override of an unknown account refused", "prime",
    {"accounts": [A_REF], "override": "Nobody", "params": P}, True, "ERROR:override")
add("P07 OVERTAKE on: the top rank", "prime",
    {"accounts": [A_REF, A_VET, A_THIN, acc("Lucky", 0.95, 0.2, 0.99, 1)], "override": None,
     "params": fleet({"OVERTAKE": True})}, True, "Prime AI|TOP RANK")
add("P08 no active account: NONE", "prime",
    {"accounts": [acc("Gone", 0.9, 0.9, 0.95, 9, False)], "override": None, "params": P}, True,
    "-|NONE")
add("P09 empty: NONE", "prime", {"accounts": [], "override": None, "params": P}, True, "-|NONE")
add("P10 tier off: NONE", "prime",
    {"accounts": [A_THIN, A_REF], "override": None,
     "params": fleet({"TRUST TIER": {"on": False, "fraction": 0.25}})}, True, "-|NONE")
add("P11 tie on S by narrower interval", "prime",
    {"accounts": [acc("a", 0.7, 0.6, 0.8, 1), acc("b", 0.7, 0.65, 0.75, 1)], "override": None,
     "params": fleet({"TRUST TIER": {"on": True, "fraction": 1}})}, True, "b|HIGHEST S IN TIER")
add("P12 tie by larger n", "prime",
    {"accounts": [acc("a", 0.7, 0.6, 0.8, 1), acc("b", 0.7, 0.6, 0.8, 2)], "override": None,
     "params": fleet({"TRUST TIER": {"on": True, "fraction": 1}})}, True, "b|HIGHEST S IN TIER")
add("P13 tie by name", "prime",
    {"accounts": [acc("b", 0.7, 0.6, 0.8, 1), acc("a", 0.7, 0.6, 0.8, 1)], "override": None,
     "params": fleet({"TRUST TIER": {"on": True, "fraction": 1}})}, True, "a|HIGHEST S IN TIER")
add("P14 suspended Prime replaced by the next rank", "prime",
    {"accounts": [acc("Old Prime", 0.9, 0.9, 0.95, 50, False), A_VET, A_THIN], "override": None,
     "params": P}, True, "CH7 AZ|HIGHEST S IN TIER")
add("P15 override not a name refused", "prime", {"accounts": [A_REF], "override": 5, "params": P},
    True, "ERROR:override")
add("P16 OVERTAKE missing refused", "prime",
    {"accounts": [A_REF], "override": None, "params": {k: v for k, v in P.items() if k != "OVERTAKE"}},
    True, "ERROR:params:OVERTAKE")
add("P17 accounts malformed refused", "prime", {"accounts": [{"account": "a"}], "override": None,
                                                "params": P}, True, "ERROR:accounts")
add("P18 two in tier, lower rank has the higher S", "prime",
    {"accounts": [acc("a", 0.6, 0.55, 0.65, 9), acc("b", 0.7, 0.5, 0.9, 2), A_THIN, A_MID],
     "override": None, "params": fleet({"TRUST TIER": {"on": True, "fraction": 0.5}})}, True,
    "b|HIGHEST S IN TIER")
add("P19 OVERTAKE on with override", "prime",
    {"accounts": [A_THIN, A_REF], "override": "DeltaS Zero", "params": fleet({"OVERTAKE": True})},
    True, "DeltaS Zero|OVERRIDE")
add("P20 override of the Prime itself", "prime",
    {"accounts": [A_THIN, A_REF], "override": "Prime AI", "params": P}, True, "Prime AI|OVERRIDE")

# ---- pending_level (PL1-PL3; D129.1's table) -------------------------------
def it(cp, snap, st, eta=1000, ind=None):
    d = {"counterpart": cp, "snapshot": snap, "standing": st, "eta": eta}
    if ind is not None:
        d["independent"] = ind
    return d


NOW = 500
add("L01 nothing pending: GREEN", "level", {"items": [], "now": NOW, "params": P}, True,
    "GREEN|0|0|0|0|0|0")
add("L02 one SIMILAR: YELLOW", "level", {"items": [it("a", "s1", "SIMILAR")], "now": NOW,
                                         "params": P}, True, "YELLOW|0|0|1|0|1|0")
add("L03 one CLEARLY HIGHER: YELLOW", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER")], "now": NOW, "params": P}, True,
    "YELLOW|1|1|0|0|1|0")
add("L04 two CLEARLY HIGHER, same model: ORANGE (D129.1)", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER"), it("b", "s1", "CLEARLY HIGHER")], "now": NOW,
     "params": P}, True, "ORANGE|2|1|0|0|2|0")
add("L05 two CLEARLY HIGHER, independent: SUSPENDED", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER"), it("b", "s2", "CLEARLY HIGHER")], "now": NOW,
     "params": P}, True, "SUSPENDED|2|2|0|0|2|0")
add("L06 three SIMILAR: ORANGE (WARN COUNT)", "level",
    {"items": [it("a", "s1", "SIMILAR"), it("b", "s2", "SIMILAR"), it("c", "s3", "SIMILAR")],
     "now": NOW, "params": P}, True, "ORANGE|0|0|3|0|3|0")
add("L07 two SIMILAR: YELLOW", "level",
    {"items": [it("a", "s1", "SIMILAR"), it("b", "s2", "SIMILAR")], "now": NOW, "params": P},
    True, "YELLOW|0|0|2|0|2|0")
add("L08 all CLEARLY LOWER: YELLOW", "level",
    {"items": [it("a", "s1", "CLEARLY LOWER"), it("b", "s2", "CLEARLY LOWER"),
               it("c", "s3", "CLEARLY LOWER"), it("d", "s4", "CLEARLY LOWER")], "now": NOW,
     "params": P}, True, "YELLOW|0|0|0|4|4|0")
add("L09 two CLEARLY HIGHER, one excluded by the quota: ORANGE (D129.1)", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER"), it("b", "s2", "CLEARLY HIGHER", 1000, False)],
     "now": NOW, "params": P}, True, "ORANGE|2|1|0|0|2|0")
add("L10 expired item leaves the state", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER", 400), it("b", "s2", "CLEARLY HIGHER")],
     "now": NOW, "params": P}, True, "YELLOW|1|1|0|0|1|1")
add("L11 eta equal to now is not expired", "level",
    {"items": [it("a", "s1", "SIMILAR", 500)], "now": NOW, "params": P}, True,
    "YELLOW|0|0|1|0|1|0")
add("L12 one pending per pair: duplicates collapse", "level",
    {"items": [it("a", "s1", "SIMILAR"), it("a", "s1", "CLEARLY HIGHER"),
               it("a", "s2", "CLEARLY HIGHER")], "now": NOW, "params": P}, True,
    "YELLOW|0|0|1|0|1|0")
add("L13 P_max caps the count", "level",
    {"items": [it("c%d" % i, "s%d" % i, "SIMILAR") for i in range(8)], "now": NOW,
     "params": fleet({"P_max": 2})}, True, "YELLOW|0|0|2|0|2|0")
add("L14 P_max 5 with three CLEARLY HIGHER beyond it", "level",
    {"items": [it("c%d" % i, "s%d" % i, "CLEARLY LOWER") for i in range(5)]
     + [it("h%d" % i, "t%d" % i, "CLEARLY HIGHER") for i in range(3)], "now": NOW, "params": P},
    True, "YELLOW|0|0|0|5|5|0")
add("L15 SUSPEND COUNT 3: two independent is ORANGE", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER"), it("b", "s2", "CLEARLY HIGHER")], "now": NOW,
     "params": fleet({"SUSPEND COUNT": 3})}, True, "ORANGE|2|2|0|0|2|0")
add("L16 WARN COUNT 2: two SIMILAR is ORANGE", "level",
    {"items": [it("a", "s1", "SIMILAR"), it("b", "s2", "SIMILAR")], "now": NOW,
     "params": fleet({"WARN COUNT": 2})}, True, "ORANGE|0|0|2|0|2|0")
add("L17 three independent CLEARLY HIGHER: SUSPENDED", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER"), it("b", "s2", "CLEARLY HIGHER"),
               it("c", "s3", "CLEARLY HIGHER")], "now": NOW, "params": P}, True,
    "SUSPENDED|3|3|0|0|3|0")
add("L18 mixed: one CLEARLY HIGHER and three SIMILAR: ORANGE", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER"), it("b", "s2", "SIMILAR"), it("c", "s3", "SIMILAR"),
               it("d", "s4", "SIMILAR")], "now": NOW, "params": P}, True, "ORANGE|1|1|3|0|4|0")
add("L19 standing unknown refused", "level", {"items": [it("a", "s1", "HIGHER")], "now": NOW,
                                              "params": P}, True, "ERROR:items")
add("L20 independent not boolean refused", "level",
    {"items": [it("a", "s1", "SIMILAR", 1000, 1)], "now": NOW, "params": P}, True, "ERROR:items")
add("L21 now negative refused", "level", {"items": [], "now": -1, "params": P}, True,
    "ERROR:items")
add("L22 P_max 0 refused", "level", {"items": [], "now": NOW, "params": fleet({"P_max": 0})},
    True, "ERROR:params:P_max")
add("L23 all expired: GREEN", "level",
    {"items": [it("a", "s1", "CLEARLY HIGHER", 10), it("b", "s2", "CLEARLY HIGHER", 20)],
     "now": NOW, "params": P}, True, "GREEN|0|0|0|0|0|2")

# ---- state_step (SS1-SS3) --------------------------------------------------
def sp(st, passes=0):
    return {"state": st, "passes": passes}


add("T01 GREEN to YELLOW at once", "state", {"prev": sp("GREEN"), "level": "YELLOW",
                                             "event": "LANDING", "params": P}, True, "YELLOW|0|UP")
add("T02 YELLOW stays while the item stands", "state",
    {"prev": sp("YELLOW"), "level": "YELLOW", "event": "CANARY PASS", "params": P}, True,
    "YELLOW|0|SAME")
add("T03 YELLOW, item cleared, no pass yet", "state",
    {"prev": sp("YELLOW"), "level": "GREEN", "event": "LANDING", "params": P}, True,
    "YELLOW|0|WAIT")
add("T04 YELLOW leaves at the next canary pass", "state",
    {"prev": sp("YELLOW"), "level": "GREEN", "event": "CANARY PASS", "params": P}, True,
    "GREEN|0|DOWN")
add("T05 ORANGE: first pass", "state", {"prev": sp("ORANGE"), "level": "GREEN",
                                        "event": "CANARY PASS", "params": P}, True,
    "ORANGE|1|WAIT")
add("T06 ORANGE: second pass", "state", {"prev": sp("ORANGE", 1), "level": "GREEN",
                                         "event": "CANARY PASS", "params": P}, True,
    "ORANGE|2|WAIT")
add("T07 ORANGE leaves at the third pass", "state",
    {"prev": sp("ORANGE", 2), "level": "GREEN", "event": "CANARY PASS", "params": P}, True,
    "GREEN|0|DOWN")
add("T08 ORANGE to YELLOW after three passes", "state",
    {"prev": sp("ORANGE", 2), "level": "YELLOW", "event": "CANARY PASS", "params": P}, True,
    "YELLOW|0|DOWN")
add("T09 a canary failure resets the count", "state",
    {"prev": sp("ORANGE", 2), "level": "GREEN", "event": "CANARY FAIL", "params": P}, True,
    "ORANGE|0|RESET")
add("T10 ORANGE PASSES 1", "state", {"prev": sp("ORANGE"), "level": "GREEN",
                                     "event": "CANARY PASS", "params": fleet({"ORANGE PASSES": 1})},
    True, "GREEN|0|DOWN")
add("T11 SUSPENDED holds", "state", {"prev": sp("SUSPENDED"), "level": "GREEN",
                                     "event": "CANARY PASS", "params": P}, True,
    "SUSPENDED|0|HELD")
add("T12 SUSPENDED leaves only by the audit finding", "state",
    {"prev": sp("SUSPENDED"), "level": "GREEN", "event": "AUDIT FINDING", "params": P}, True,
    "GREEN|0|AUDIT")
add("T13 audit finding to a remaining YELLOW", "state",
    {"prev": sp("SUSPENDED"), "level": "YELLOW", "event": "AUDIT FINDING", "params": P}, True,
    "YELLOW|0|AUDIT")
add("T14 escalation to SUSPENDED", "state", {"prev": sp("YELLOW"), "level": "SUSPENDED",
                                             "event": "LANDING", "params": P}, True,
    "SUSPENDED|0|UP")
add("T15 escalation YELLOW to ORANGE resets the count", "state",
    {"prev": sp("YELLOW", 0), "level": "ORANGE", "event": "LANDING", "params": P}, True,
    "ORANGE|0|UP")
add("T16 GREEN stays GREEN", "state", {"prev": sp("GREEN"), "level": "GREEN",
                                       "event": "CANARY PASS", "params": P}, True, "GREEN|0|SAME")
add("T17 ORANGE with a landing between passes keeps the count", "state",
    {"prev": sp("ORANGE", 2), "level": "GREEN", "event": "LANDING", "params": P}, True,
    "ORANGE|2|WAIT")
add("T18 unknown state refused", "state", {"prev": sp("RED"), "level": "GREEN",
                                           "event": "LANDING", "params": P}, True, "ERROR:prev")
add("T19 unknown event refused", "state", {"prev": sp("GREEN"), "level": "GREEN",
                                           "event": "PARROT PASS", "params": P}, True,
    "ERROR:event")
add("T20 ORANGE PASSES 0 refused", "state",
    {"prev": sp("ORANGE"), "level": "GREEN", "event": "CANARY PASS",
     "params": fleet({"ORANGE PASSES": 0})}, True, "ERROR:params:ORANGE PASSES")
add("T21 passes negative refused", "state", {"prev": sp("ORANGE", -1), "level": "GREEN",
                                             "event": "CANARY PASS", "params": P}, True,
    "ERROR:prev")

# ---- peer_activity ---------------------------------------------------------
def pe(cp, ind=True):
    return {"counterpart": cp, "independent": ind}


add("A01 no events: 0", "activity", {"events": [], "params": P}, True, "0|0")
add("A02 one event: 1/50", "activity", {"events": [pe("a")], "params": P}, True, "1|0.02")
add("A03 same counterpart twice counts once", "activity", {"events": [pe("a"), pe("a")],
                                                            "params": P}, True, "1|0.02")
add("A04 non-independent event does not count", "activity",
    {"events": [pe("a", False)], "params": P}, True, "0|0")
add("A05 fifty distinct: capped at 1", "activity",
    {"events": [pe("c%d" % i) for i in range(50)], "params": P}, True, "50|1")
add("A06 sixty distinct: capped at 1", "activity",
    {"events": [pe("c%d" % i) for i in range(60)], "params": P}, True, "60|1")
add("A07 RATING PERIOD 10, five distinct: 0.5", "activity",
    {"events": [pe("c%d" % i) for i in range(5)], "params": fleet({"RATING PERIOD": 10})}, True,
    "5|0.5")
add("A08 mixed independence", "activity",
    {"events": [pe("a"), pe("b", False), pe("c"), pe("b")], "params": P}, True, "3|0.06")
for i in range(12):
    add("A%02d sweep %d distinct" % (9 + i, i * 4 + 1), "activity",
        {"events": [pe("c%d" % j) for j in range(i * 4 + 1)], "params": P}, False)
add("A21 counterpart empty refused", "activity", {"events": [pe("")], "params": P}, True,
    "ERROR:events")
add("A22 independent missing refused", "activity", {"events": [{"counterpart": "a"}],
                                                    "params": P}, True, "ERROR:events")
add("A23 RATING PERIOD 0 refused", "activity",
    {"events": [], "params": fleet({"RATING PERIOD": 0})}, True, "ERROR:params:RATING PERIOD")

# ---- score_update (SU1-SU2) ------------------------------------------------
near("U01 follower below the Prime: 0.5 + 0.2 x 0.375 x 1 x 0.3", "update",
     {"role": "FOLLOWER", "S": 0.5, "E": 0.9, "S_P": 0.8, "a": 1, "params": P}, 0.5225, 0)
near("U02 the Prime's climb: k 0.2 x 0.2 x 0.5 x 0.1", "update",
     {"role": "PRIME", "S": 0.8, "E": 0.9, "S_P": 0.8, "a": 0.5, "params": P}, 0.8004, 0)
add("U03 the reference Prime lands nothing: a_P 0 holds the gauge still", "update",
    {"role": "PRIME", "S": 0.8, "E": 0.95, "S_P": 0.8, "a": 0, "params": P}, True,
    "0.8|1|0|0|false")
add("U04 follower with a_i 0 moves at k_min (the clip's floor; a period exists only when blocks land)",
    "update", {"role": "FOLLOWER", "S": 0.5, "E": 0.9, "S_P": 0.8, "a": 0, "params": P}, True,
    "0.503|0.8|0.01|0.003|false")
near("U05 follower target capped at S_P", "update",
     {"role": "FOLLOWER", "S": 0.5, "E": 0.95, "S_P": 0.8, "a": 1, "params": P}, 0.8, 1)
near("U06 OVERTAKE on: target min(E, CEILING)", "update",
     {"role": "FOLLOWER", "S": 0.5, "E": 0.95, "S_P": 0.8, "a": 1, "params": fleet({"OVERTAKE": True})},
     0.95, 1)
add("U07 step bounded by delta_max", "update",
    {"role": "FOLLOWER", "S": 0.1, "E": 0.9, "S_P": 0.9, "a": 1, "params": P}, True)
near("U08 evidence below S: the lag pulls S down", "update",
     {"role": "FOLLOWER", "S": 0.7, "E": 0.3, "S_P": 0.8, "a": 1, "params": P},
     0.7 + 0.2 * (0.1 / 0.8) * 1 * (0.3 - 0.7), 0)
add("U09 follower above the Prime: k clipped to k_min", "update",
    {"role": "FOLLOWER", "S": 0.9, "E": 0.95, "S_P": 0.8, "a": 1, "params": P}, True,
    "0.899|0.8|0.01|-0.001|false")
add("U10 k clipped to k_max", "update",
    {"role": "FOLLOWER", "S": 0.01, "E": 0.5, "S_P": 0.8, "a": 1,
     "params": fleet({"k_base": 5})}, True)
add("U11 USER follows the user Prime by the same law", "update",
    {"role": "USER", "S": 0.5, "E": 0.9, "S_P": 0.8, "a": 1, "params": P}, True,
    "0.5225|0.8|0.075|0.0225|false")
add("U12 USER PRIME climbs as the Prime", "update",
    {"role": "USER PRIME", "S": 0.8, "E": 0.9, "S_P": 0.8, "a": 0.5, "params": P}, True,
    "0.8004|1|0.002|0.0004|false")
add("U13 the Prime at the ceiling stays", "update",
    {"role": "PRIME", "S": 1, "E": 1, "S_P": 1, "a": 1, "params": P}, True, "1|1|0|0|false")
add("U14 PRIME CEILING 0.9", "update",
    {"role": "FOLLOWER", "S": 0.5, "E": 0.95, "S_P": 0.8, "a": 1,
     "params": fleet({"PRIME CEILING": 0.9, "OVERTAKE": True})}, True)
add("U15 delta_max 0.5 lets the step through", "update",
    {"role": "FOLLOWER", "S": 0.1, "E": 0.9, "S_P": 0.9, "a": 1,
     "params": fleet({"delta_max": 0.5})}, True)
add("U16 negative step bounded by delta_max", "update",
    {"role": "FOLLOWER", "S": 0.9, "E": 0.1, "S_P": 0.95, "a": 1,
     "params": fleet({"k_base": 5})}, True)
add("U17 S_P 0 refused", "update", {"role": "FOLLOWER", "S": 0.5, "E": 0.9, "S_P": 0, "a": 1,
                                    "params": P}, True, "ERROR:S_P")
add("U18 role unknown refused", "update", {"role": "LEADER", "S": 0.5, "E": 0.9, "S_P": 0.8,
                                           "a": 1, "params": P}, True, "ERROR:role")
add("U19 a above 1 refused", "update", {"role": "FOLLOWER", "S": 0.5, "E": 0.9, "S_P": 0.8,
                                        "a": 1.5, "params": P}, True, "ERROR:a")
add("U20 k_min above k_max refused", "update",
    {"role": "FOLLOWER", "S": 0.5, "E": 0.9, "S_P": 0.8, "a": 1,
     "params": fleet({"k_min": 0.6})}, True, "ERROR:params:k_min")
add("U21 E a string refused", "update", {"role": "FOLLOWER", "S": 0.5, "E": "0.9", "S_P": 0.8,
                                         "a": 1, "params": P}, True, "ERROR:E")
add("U22 a thin file's first period", "update",
    {"role": "FOLLOWER", "S": 0.5, "E": 0.5, "S_P": 0.8, "a": 0.02, "params": P}, True,
    "0.5|0.5|0.01|0|false")

# ---- violation_cut ---------------------------------------------------------
add("C01 m_1 against a SIMILAR counterpart", "cut", {"S": 0.9, "m_class": 1, "standing": "SIMILAR",
                                                     "params": P}, True, "0.675|0.25|2")
add("C02 m_2 against a CLEARLY HIGHER counterpart", "cut",
    {"S": 0.9, "m_class": 2, "standing": "CLEARLY HIGHER", "params": P}, True, "0.45|0.5|3")
add("C03 m_3 against a CLEARLY LOWER counterpart", "cut",
    {"S": 0.9, "m_class": 3, "standing": "CLEARLY LOWER", "params": P}, True, "0.765|0.15|1")
add("C04 S 0", "cut", {"S": 0, "m_class": 2, "standing": "SIMILAR", "params": P}, True,
    "0|0.5|2")
add("C05 S 1 m_2 lands at the trip level's edge", "cut",
    {"S": 0.8, "m_class": 2, "standing": "SIMILAR", "params": P}, True, "0.4|0.5|2")
add("C06 h varied", "cut",
    {"S": 0.5, "m_class": 1, "standing": "CLEARLY HIGHER",
     "params": fleet({"h": {"CLEARLY LOWER": 0.5, "SIMILAR": 1, "CLEARLY HIGHER": 4}})}, True,
    "0.375|0.25|4")
add("C07 m_1 varied to 0", "cut", {"S": 0.5, "m_class": 1, "standing": "SIMILAR",
                                   "params": fleet({"m_1": 0})}, True, "0.5|0|2")
add("C08 m_2 varied to 1", "cut", {"S": 0.5, "m_class": 2, "standing": "SIMILAR",
                                   "params": fleet({"m_2": 1})}, True, "0|1|2")
for i, s in enumerate([0.1, 0.2, 0.3, 0.55, 0.6, 0.7, 0.85, 0.95, 0.99, 1]):
    add("C%02d sweep S %s m_%d" % (9 + i, s, i % 3 + 1), "cut",
        {"S": s, "m_class": i % 3 + 1, "standing": ["SIMILAR", "CLEARLY HIGHER", "CLEARLY LOWER"][i % 3],
         "params": P}, False)
add("C19 m_class 4 refused", "cut", {"S": 0.5, "m_class": 4, "standing": "SIMILAR", "params": P},
    True, "ERROR:m_class")
add("C20 standing unknown refused", "cut", {"S": 0.5, "m_class": 1, "standing": "HIGHER",
                                            "params": P}, True, "ERROR:standing")
add("C21 h missing a standing refused", "cut",
    {"S": 0.5, "m_class": 1, "standing": "SIMILAR",
     "params": fleet({"h": {"SIMILAR": 2, "CLEARLY HIGHER": 3}})}, True, "ERROR:params:h")
add("C22 m_class boolean refused", "cut", {"S": 0.5, "m_class": True, "standing": "SIMILAR",
                                           "params": P}, True, "ERROR:m_class")

# ---- trip ------------------------------------------------------------------
add("X01 below the tier: the first violation trips", "trip",
    {"S": 0.9, "in_tier": False, "prior_ages": [], "params": P}, True, "TRIP|FIRST")
add("X02 top tier, above TRIP LEVEL, no prior: PICKUP", "trip",
    {"S": 0.45, "in_tier": True, "prior_ages": [], "params": P}, True, "PICKUP|ABOVE")
add("X03 top tier, cut leaves S below TRIP LEVEL", "trip",
    {"S": 0.39, "in_tier": True, "prior_ages": [], "params": P}, True, "TRIP|LEVEL")
add("X04 exactly TRIP LEVEL does not trip", "trip",
    {"S": 0.4, "in_tier": True, "prior_ages": [], "params": P}, True, "PICKUP|ABOVE")
add("X05 second violation inside TRIP WINDOW", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [499], "params": P}, True, "TRIP|WINDOW")
add("X06 prior at the window's edge trips", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [500], "params": P}, True, "TRIP|WINDOW")
add("X07 prior beyond the window: PICKUP", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [501], "params": P}, True, "PICKUP|ABOVE")
add("X08 level before window", "trip",
    {"S": 0.1, "in_tier": True, "prior_ages": [10], "params": P}, True, "TRIP|LEVEL")
add("X09 two old priors: PICKUP", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [900, 1200], "params": P}, True, "PICKUP|ABOVE")
add("X10 one recent among old priors", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [900, 3], "params": P}, True, "TRIP|WINDOW")
add("X11 TRIP WINDOW 100", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [150], "params": fleet({"TRIP WINDOW": 100})}, True,
    "PICKUP|ABOVE")
add("X12 TRIP LEVEL 0.7", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [], "params": fleet({"TRIP LEVEL": 0.7})}, True,
    "TRIP|LEVEL")
add("X13 below the tier with priors", "trip",
    {"S": 0.6, "in_tier": False, "prior_ages": [900], "params": P}, True, "TRIP|FIRST")
add("X14 S 0 in tier", "trip", {"S": 0, "in_tier": True, "prior_ages": [], "params": P}, True,
    "TRIP|LEVEL")
add("X15 S 1 in tier", "trip", {"S": 1, "in_tier": True, "prior_ages": [], "params": P}, True,
    "PICKUP|ABOVE")
add("X16 prior age 0 (the same block)", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [0], "params": P}, True, "TRIP|WINDOW")
add("X17 in_tier not boolean refused", "trip",
    {"S": 0.6, "in_tier": 1, "prior_ages": [], "params": P}, True, "ERROR:in_tier")
add("X18 negative prior age refused", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [-1], "params": P}, True, "ERROR:prior_ages")
add("X19 prior_ages not a list refused", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": 5, "params": P}, True, "ERROR:prior_ages")
add("X20 TRIP WINDOW 0 refused", "trip",
    {"S": 0.6, "in_tier": True, "prior_ages": [], "params": fleet({"TRIP WINDOW": 0})}, True,
    "ERROR:params:TRIP WINDOW")
add("X21 S above 1 refused", "trip", {"S": 1.5, "in_tier": True, "prior_ages": [], "params": P},
    True, "ERROR:S")

# ---- inquire ---------------------------------------------------------------
add("N01 the first inquiry costs w_I", "inquire",
    {"state": {"last": None, "k": 0}, "block": 10, "S": 0.7, "params": P}, True,
    "0.01|0.69|1|false|10|1")
add("N02 the second inside the window costs 2 w_I", "inquire",
    {"state": {"last": 10, "k": 1}, "block": 50, "S": 0.69, "params": P}, True,
    "0.02|0.67|2|true|50|2")
add("N03 the third inside the restarted window costs 4 w_I", "inquire",
    {"state": {"last": 50, "k": 2}, "block": 249, "S": 0.67, "params": P}, True,
    "0.04|0.63|3|true|249|3")
add("N04 the window closes after COOL-OFF blocks: back to w_I", "inquire",
    {"state": {"last": 50, "k": 2}, "block": 250, "S": 0.67, "params": P}, True,
    "0.01|0.66|1|false|250|1")
add("N05 the restart: 199 blocks after the third is still inside", "inquire",
    {"state": {"last": 249, "k": 3}, "block": 448, "S": 0.63, "params": P}, True,
    "0.08|0.55|4|true|448|4")
add("N06 the same block twice", "inquire",
    {"state": {"last": 10, "k": 1}, "block": 10, "S": 0.69, "params": P}, True,
    "0.02|0.67|2|true|10|2")
add("N07 the cost floors S at 0", "inquire",
    {"state": {"last": 10, "k": 7}, "block": 11, "S": 0.5, "params": P}, True,
    "1.28|0|8|true|11|8")
add("N08 k 1030: the cost past the double's range is inf", "inquire",
    {"state": {"last": 10, "k": 1030}, "block": 11, "S": 0.5, "params": P}, True,
    "inf|0|1031|true|11|1031")
add("N09 k 1023 is the last finite cost", "inquire",
    {"state": {"last": 10, "k": 1023}, "block": 11, "S": 0.5, "params": P}, True)
add("N10 w_I varied", "inquire",
    {"state": {"last": None, "k": 0}, "block": 0, "S": 0.5, "params": fleet({"w_I": 0.05})}, True,
    "0.05|0.45|1|false|0|1")
add("N11 COOL-OFF 10", "inquire",
    {"state": {"last": 0, "k": 1}, "block": 10, "S": 0.5, "params": fleet({"COOL-OFF": 10})}, True,
    "0.01|0.49|1|false|10|1")
add("N12 COOL-OFF 10 inside", "inquire",
    {"state": {"last": 0, "k": 1}, "block": 9, "S": 0.5, "params": fleet({"COOL-OFF": 10})}, True,
    "0.02|0.48|2|true|9|2")
add("N13 nothing is refused: S 0 still answers", "inquire",
    {"state": {"last": None, "k": 0}, "block": 5, "S": 0, "params": P}, True,
    "0.01|0|1|false|5|1")
for i in range(6):
    add("N%02d sweep k %d" % (14 + i, i + 3), "inquire",
        {"state": {"last": 100, "k": i + 3}, "block": 100 + 30 * i, "S": 0.9, "params": P}, False)
add("N20 block before last refused", "inquire",
    {"state": {"last": 10, "k": 1}, "block": 9, "S": 0.5, "params": P}, True, "ERROR:block")
add("N21 state missing k refused", "inquire",
    {"state": {"last": 10}, "block": 11, "S": 0.5, "params": P}, True, "ERROR:state")
add("N22 w_I above 1 refused", "inquire",
    {"state": {"last": None, "k": 0}, "block": 0, "S": 0.5, "params": fleet({"w_I": 2})}, True,
    "ERROR:params:w_I")

# ---- override_check / effective / mark -------------------------------------
FP_A = "a" * 64
FP_B = "b" * 64
FP_S = "c" * 64
NON = "0123456789abcdef0123456789abcdef"
FACTS = {"certified": [FP_A, FP_B, FP_S], "suspended": [FP_S], "nonces": ["f" * 32], "prime": FP_A}


def decl(fp=FP_B, value=0.9, nonce=NON, time=1700000000, reason="a reason"):
    return {"fingerprint": fp, "value": value, "nonce": nonce, "time": time, "reason": reason}


add("O01 a valid override replaces the Prime after the delay", "override",
    {"decl": decl(), "facts": FACTS, "params": P}, True, "OK|0.9|1700086400|500|" + FP_A)
add("O02 no Prime in force", "override",
    {"decl": decl(), "facts": dict(FACTS, prime=None), "params": P}, True,
    "OK|0.9|1700086400|500|-")
add("O03 value at THIN FILE START", "override", {"decl": decl(value=0.5), "facts": FACTS,
                                                 "params": P}, True, "OK|0.5|1700086400|500|" + FP_A)
add("O04 value at PRIME CEILING", "override", {"decl": decl(value=1), "facts": FACTS,
                                               "params": P}, True, "OK|1|1700086400|500|" + FP_A)
add("O05 value below THIN FILE START refused", "override",
    {"decl": decl(value=0.49), "facts": FACTS, "params": P}, True, "REFUSED|BOUNDS")
add("O06 value above PRIME CEILING refused", "override",
    {"decl": decl(value=1.01), "facts": FACTS, "params": P}, True, "REFUSED|BOUNDS")
add("O07 a suspended account cannot be named", "override",
    {"decl": decl(fp=FP_S), "facts": FACTS, "params": P}, True, "REFUSED|SUSPENDED")
add("O08 an uncertified account refused", "override",
    {"decl": decl(fp="d" * 64), "facts": FACTS, "params": P}, True, "REFUSED|NOT CERTIFIED")
add("O09 a reused nonce refused (replay)", "override",
    {"decl": decl(nonce="f" * 32), "facts": FACTS, "params": P}, True, "REFUSED|NONCE")
add("O10 a malformed nonce refused", "override",
    {"decl": decl(nonce="F" * 32), "facts": FACTS, "params": P}, True, "REFUSED|NONCE FORM")
add("O11 a malformed fingerprint refused", "override",
    {"decl": decl(fp="a" * 63), "facts": FACTS, "params": P}, True, "REFUSED|FINGERPRINT")
add("O12 an empty reason refused", "override",
    {"decl": decl(reason=" \t"), "facts": FACTS, "params": P}, True, "REFUSED|REASON")
add("O13 the override of the Prime itself", "override",
    {"decl": decl(fp=FP_A), "facts": FACTS, "params": P}, True, "OK|0.9|1700086400|500|" + FP_A)
add("O14 OVERRIDE DELAY 0", "override",
    {"decl": decl(), "facts": FACTS, "params": fleet({"OVERRIDE DELAY": 0})}, True,
    "OK|0.9|1700000000|500|" + FP_A)
add("O15 OVERRIDE SUNSET 1000", "override",
    {"decl": decl(), "facts": FACTS, "params": fleet({"OVERRIDE SUNSET": 1000})}, True,
    "OK|0.9|1700086400|1000|" + FP_A)
add("O16 THIN FILE START 0.6 moves the bound", "override",
    {"decl": decl(value=0.55), "facts": FACTS, "params": fleet({"THIN FILE START": 0.6})}, True,
    "REFUSED|BOUNDS")
add("O17 value not a number refused as malformed", "override",
    {"decl": decl(value="0.9"), "facts": FACTS, "params": P}, True, "ERROR:decl")
add("O18 facts prime malformed refused", "override",
    {"decl": decl(), "facts": dict(FACTS, prime="x"), "params": P}, True, "ERROR:facts")
add("O19 decl missing time refused", "override",
    {"decl": {"fingerprint": FP_B, "value": 0.9, "nonce": NON, "reason": "r"}, "facts": FACTS,
     "params": P}, True, "ERROR:decl")
add("O20 refusal order: suspended before bounds", "override",
    {"decl": decl(fp=FP_S, value=5), "facts": FACTS, "params": P}, True, "REFUSED|SUSPENDED")
add("O21 a reason with only a newline refused", "override",
    {"decl": decl(reason="\n"), "facts": FACTS, "params": P}, True, "REFUSED|REASON")

T0 = 1700000000
add("F01 pending before the delay", "effective", {"decl_time": T0, "now": T0 + 86399,
                                                  "cancelled": False, "params": P}, True, "PENDING")
add("F02 effective at the delay", "effective", {"decl_time": T0, "now": T0 + 86400,
                                                "cancelled": False, "params": P}, True,
    "EFFECTIVE")
add("F03 cancelled by a second declaration", "effective",
    {"decl_time": T0, "now": T0 + 90000, "cancelled": True, "params": P}, True, "CANCELLED")
add("F04 cancelled inside the delay", "effective",
    {"decl_time": T0, "now": T0 + 10, "cancelled": True, "params": P}, True, "CANCELLED")
add("F05 delay 0: effective at once", "effective",
    {"decl_time": T0, "now": T0, "cancelled": False, "params": fleet({"OVERRIDE DELAY": 0})}, True,
    "EFFECTIVE")
add("F06 now before the declaration", "effective",
    {"decl_time": T0, "now": T0 - 1, "cancelled": False, "params": P}, True, "PENDING")
for i in range(10):
    add("F%02d sweep %d h" % (7 + i, 3 * i), "effective",
        {"decl_time": T0, "now": T0 + 3 * 3600 * i, "cancelled": False, "params": P}, False)
add("F17 cancelled not boolean refused", "effective",
    {"decl_time": T0, "now": T0, "cancelled": 0, "params": P}, True, "ERROR:cancelled")
add("F18 negative time refused", "effective",
    {"decl_time": -1, "now": T0, "cancelled": False, "params": P}, True, "ERROR:time")
add("F19 OVERRIDE DELAY missing refused", "effective",
    {"decl_time": T0, "now": T0, "cancelled": False,
     "params": {k: v for k, v in P.items() if k != "OVERRIDE DELAY"}}, True,
    "ERROR:params:OVERRIDE DELAY")
add("F20 delay 48 h", "effective",
    {"decl_time": T0, "now": T0 + 100000, "cancelled": False,
     "params": fleet({"OVERRIDE DELAY": 172800})}, True, "PENDING")

add("M01 the mark at the grant", "mark", {"grant": 100, "now": 100, "params": P}, True,
    "true|500")
add("M02 one block before the sunset", "mark", {"grant": 100, "now": 599, "params": P}, True,
    "true|1")
add("M03 at the sunset the mark ends", "mark", {"grant": 100, "now": 600, "params": P}, True,
    "false|0")
add("M04 long after", "mark", {"grant": 100, "now": 5000, "params": P}, True, "false|0")
add("M05 sunset 1", "mark", {"grant": 0, "now": 0, "params": fleet({"OVERRIDE SUNSET": 1})},
    True, "true|1")
add("M06 sunset 1 next block", "mark", {"grant": 0, "now": 1,
                                        "params": fleet({"OVERRIDE SUNSET": 1})}, True, "false|0")
for i in range(12):
    add("M%02d sweep %d blocks" % (7 + i, 50 * i), "mark", {"grant": 1000, "now": 1000 + 50 * i,
                                                            "params": P}, False)
add("M19 now before grant refused", "mark", {"grant": 100, "now": 99, "params": P}, True,
    "ERROR:block")
add("M20 sunset 0 refused", "mark", {"grant": 100, "now": 100,
                                     "params": fleet({"OVERRIDE SUNSET": 0})}, True,
    "ERROR:params:OVERRIDE SUNSET")
add("M21 grant boolean refused", "mark", {"grant": True, "now": 100, "params": P}, True,
    "ERROR:block")

# ---- pad_check / pad_weight ------------------------------------------------
PFACTS = dict(FACTS, reference=FP_A)
add("D01 PRIME PAD 20 on the reference account: no mark", "pad",
    {"decl": decl(fp=FP_A, value=20), "facts": PFACTS, "params": P}, True,
    "OK|20|1700086400|false|-")
add("D02 a pad on a non-reference account refused by default", "pad",
    {"decl": decl(fp=FP_B, value=20), "facts": PFACTS, "params": P}, True,
    "REFUSED|NON-REFERENCE")
add("D03 PAD NON-REFERENCE on: the mark and sunset follow", "pad",
    {"decl": decl(fp=FP_B, value=20), "facts": PFACTS, "params": fleet({"PAD NON-REFERENCE": True})},
    True, "OK|20|1700086400|true|500")
add("D04 PAD MAX 40 at the edge", "pad",
    {"decl": decl(fp=FP_A, value=40), "facts": PFACTS, "params": P}, True,
    "OK|40|1700086400|false|-")
add("D05 above PAD MAX refused", "pad",
    {"decl": decl(fp=FP_A, value=41), "facts": PFACTS, "params": P}, True, "REFUSED|PAD MAX")
add("D06 P 0 refused", "pad", {"decl": decl(fp=FP_A, value=0), "facts": PFACTS, "params": P},
    True, "REFUSED|VALUE")
add("D07 P 1", "pad", {"decl": decl(fp=FP_A, value=1), "facts": PFACTS, "params": P}, True,
    "OK|1|1700086400|false|-")
add("D08 a suspended account refused", "pad",
    {"decl": decl(fp=FP_S, value=10), "facts": PFACTS, "params": P}, True, "REFUSED|SUSPENDED")
add("D09 replay refused", "pad",
    {"decl": decl(fp=FP_A, value=10, nonce="f" * 32), "facts": PFACTS, "params": P}, True,
    "REFUSED|NONCE")
add("D10 uncertified refused", "pad",
    {"decl": decl(fp="e" * 64, value=10), "facts": PFACTS, "params": P}, True,
    "REFUSED|NOT CERTIFIED")
add("D11 no reference account, non-reference off", "pad",
    {"decl": decl(fp=FP_A, value=10), "facts": dict(PFACTS, reference=None), "params": P}, True,
    "REFUSED|NON-REFERENCE")
add("D12 no reference account, non-reference on: marked", "pad",
    {"decl": decl(fp=FP_A, value=10), "facts": dict(PFACTS, reference=None),
     "params": fleet({"PAD NON-REFERENCE": True})}, True, "OK|10|1700086400|true|500")
add("D13 a float P refused as malformed", "pad",
    {"decl": decl(fp=FP_A, value=20.0), "facts": PFACTS, "params": P}, True, "ERROR:decl")
add("D14 PAD MAX 10", "pad", {"decl": decl(fp=FP_A, value=20), "facts": PFACTS,
                              "params": fleet({"PAD MAX": 10})}, True, "REFUSED|PAD MAX")
add("D15 empty reason refused", "pad",
    {"decl": decl(fp=FP_A, value=20, reason=""), "facts": PFACTS, "params": P}, True,
    "REFUSED|REASON")
add("D16 reference malformed refused", "pad",
    {"decl": decl(fp=FP_A, value=20), "facts": dict(PFACTS, reference="zz"), "params": P}, True,
    "ERROR:facts")
add("D17 facts missing reference refused", "pad",
    {"decl": decl(fp=FP_A, value=20), "facts": FACTS, "params": P}, True, "ERROR:facts")
add("D18 refusal order: PAD MAX before NON-REFERENCE", "pad",
    {"decl": decl(fp=FP_B, value=99), "facts": PFACTS, "params": P}, True, "REFUSED|PAD MAX")
add("D19 delay varied", "pad",
    {"decl": decl(fp=FP_A, value=5), "facts": PFACTS, "params": fleet({"OVERRIDE DELAY": 3600})},
    True, "OK|5|1700003600|false|-")
add("D20 negative P refused", "pad", {"decl": decl(fp=FP_A, value=-3), "facts": PFACTS,
                                      "params": P}, True, "ERROR:decl")

add("W01 pad 20 at the grant", "pad_weight", {"P": 20, "age": 0, "params": P}, True, "20")
add("W02 pad 20 after a half-life", "pad_weight", {"P": 20, "age": 500, "params": P}, True, "10")
add("W03 pad 20 after two half-lives", "pad_weight", {"P": 20, "age": 1000, "params": P}, True,
    "5")
add("W04 pad 40 after 250 blocks", "pad_weight", {"P": 40, "age": 250, "params": P}, False)
add("W05 HALF-LIFE 100", "pad_weight", {"P": 20, "age": 100, "params": fleet({"HALF-LIFE": 100})},
    True, "10")
for i in range(12):
    add("W%02d sweep age %d" % (6 + i, 37 * i), "pad_weight", {"P": 10 + i, "age": 37 * i,
                                                              "params": P}, False)
add("W18 P 0 refused", "pad_weight", {"P": 0, "age": 0, "params": P}, True, "ERROR:pad")
add("W19 age negative refused", "pad_weight", {"P": 20, "age": -1, "params": P}, True,
    "ERROR:pad")
add("W20 P a float refused", "pad_weight", {"P": 20.0, "age": 0, "params": P}, True, "ERROR:pad")
add("W21 HALF-LIFE missing refused", "pad_weight",
    {"P": 20, "age": 0, "params": {k: v for k, v in P.items() if k != "HALF-LIFE"}}, True,
    "ERROR:params:HALF-LIFE")

# ---- tier_gate_release -----------------------------------------------------
def rep(t, o, m):
    return {"in_tier": t, "override_in_force": o, "independent_matches": m}


add("G01 tier member, no mark: PASS", "gate", {"reproducer": rep(True, False, 1)}, True,
    "PASS|TIER")
add("G02 not in the tier: REFUSED", "gate", {"reproducer": rep(False, False, 1)}, True,
    "REFUSED|NOT IN TIER")
add("G03 marked, a sole match: REFUSED", "gate", {"reproducer": rep(True, True, 1)}, True,
    "REFUSED|OVERRIDE MARK")
add("G04 marked, a second independent match: PASS", "gate", {"reproducer": rep(True, True, 2)},
    True, "PASS|TIER")
add("G05 marked, three matches", "gate", {"reproducer": rep(True, True, 3)}, True, "PASS|TIER")
add("G06 marked, zero matches", "gate", {"reproducer": rep(True, True, 0)}, True,
    "REFUSED|OVERRIDE MARK")
add("G07 not in tier and marked: NOT IN TIER first", "gate", {"reproducer": rep(False, True, 0)},
    True, "REFUSED|NOT IN TIER")
add("G08 tier member, zero matches (the match itself is counted by the caller)", "gate",
    {"reproducer": rep(True, False, 0)}, True, "PASS|TIER")
for i in range(8):
    add("G%02d sweep" % (9 + i), "gate", {"reproducer": rep(i % 2 == 0, i % 4 < 2, i)}, False)
add("G17 in_tier not boolean refused", "gate", {"reproducer": rep(1, False, 1)}, True,
    "ERROR:reproducer")
add("G18 matches negative refused", "gate", {"reproducer": rep(True, False, -1)}, True,
    "ERROR:reproducer")
add("G19 missing key refused", "gate", {"reproducer": {"in_tier": True}}, True,
    "ERROR:reproducer")
add("G20 extra key refused", "gate", {"reproducer": dict(rep(True, False, 1), x=1)}, True,
    "ERROR:reproducer")

# ---- inherit ---------------------------------------------------------------
def hf(c=True, sa=True, si=True, cause="DIS"):
    return {"continuation": c, "same_account": sa, "same_identity": si, "cause": cause}


add("H01 a declared continuation after a D(IS) change inherits 0.25 F", "inherit",
    {"F_old": 40, "facts": hf(), "params": P}, True, "10|0.5|true")
add("H02 after a suite change", "inherit", {"F_old": 40, "facts": hf(cause="SUITE"),
                                            "params": P}, True, "10|0.5|true")
add("H03 no declaration: a thin file", "inherit", {"F_old": 40, "facts": hf(c=False),
                                                   "params": P}, True, "0|0.5|false")
add("H04 a change of family never continues", "inherit",
    {"F_old": 40, "facts": hf(cause="FAMILY"), "params": P}, True, "0|0.5|false")
add("H05 a PROMPT change made as a new agent never continues", "inherit",
    {"F_old": 40, "facts": hf(cause="PROMPT NEW AGENT"), "params": P}, True, "0|0.5|false")
add("H06 a different account never continues", "inherit",
    {"F_old": 40, "facts": hf(sa=False), "params": P}, True, "0|0.5|false")
add("H07 a different identity never continues", "inherit",
    {"F_old": 40, "facts": hf(si=False), "params": P}, True, "0|0.5|false")
add("H08 INHERITANCE off", "inherit",
    {"F_old": 40, "facts": hf(), "params": fleet({"INHERITANCE": {"on": False, "fraction": 0.25}})},
    True, "0|0.5|false")
add("H09 fraction 0.5", "inherit",
    {"F_old": 40, "facts": hf(), "params": fleet({"INHERITANCE": {"on": True, "fraction": 0.5}})},
    True, "20|0.5|true")
add("H10 THIN FILE START 0.4", "inherit",
    {"F_old": 40, "facts": hf(), "params": fleet({"THIN FILE START": 0.4})}, True, "10|0.4|true")
add("H11 F_old 0", "inherit", {"F_old": 0, "facts": hf(), "params": P}, True, "0|0.5|true")
add("H12 MODEL cause: no continuation", "inherit", {"F_old": 40, "facts": hf(cause="MODEL"),
                                                    "params": P}, True, "0|0.5|false")
for i in range(6):
    add("H%02d sweep F_old %s" % (13 + i, 3.5 * i), "inherit", {"F_old": 3.5 * i, "facts": hf(),
                                                             "params": P}, False)
add("H19 F_old negative refused", "inherit", {"F_old": -1, "facts": hf(), "params": P}, True,
    "ERROR:F_old")
add("H20 cause unknown refused", "inherit", {"F_old": 1, "facts": hf(cause="TIME"),
                                             "params": P}, True, "ERROR:facts")
add("H21 continuation not boolean refused", "inherit", {"F_old": 1, "facts": hf(c=1),
                                                        "params": P}, True, "ERROR:facts")

# ---- user_aggregate / user_score_evidence / sponsor_cut ----------------------
def ag(name, events, cert=True, removed=False):
    return {"account": name, "certified": cert, "removed": removed, "events": events}


def ae(kind, base, age, cp, cs):
    return {"kind": kind, "base": base, "age": age, "counterpart": cp, "counterpart_sponsor": cs}


SP = "PHX_480"
add("V01 no certified agent: NONE", "aggregate",
    {"agents": [ag("Phoenix 480", [ae("FOR", 1, 0, "Prime AI", "BFP_Rauch")], False)],
     "sponsor": SP, "params": P}, True, "aggregate|NONE")
add("V02 no agents: NONE", "aggregate", {"agents": [], "sponsor": SP, "params": P}, True,
    "aggregate|NONE")
add("V03 one match: admitted at c_max", "aggregate",
    {"agents": [ag("Phoenix 480", [ae("FOR", 1, 0, "Prime AI", "BFP_Rauch")])], "sponsor": SP,
     "params": P}, True, "aggregate|OK\nevent|FOR|0.25|0")
add("V04 an intra-sponsor match weighs zero", "aggregate",
    {"agents": [ag("Phoenix 480", [ae("FOR", 1, 0, "CH7 AZ", SP)])], "sponsor": SP, "params": P},
    True, "aggregate|OK\nevent|FOR|0|0")
add("V05 AGAINST at base", "aggregate",
    {"agents": [ag("Phoenix 480", [ae("AGAINST", 3, 0, "Prime AI", "BFP_Rauch")])], "sponsor": SP,
     "params": P}, True, "aggregate|OK\nevent|AGAINST|3|0")
add("V06 the quota jointly over two agents' matches with one counterpart", "aggregate",
    {"agents": [ag("Phoenix 480", [ae("FOR", 1, 10, "Prime AI", "BFP_Rauch")]),
                ag("CH7 AZ", [ae("FOR", 1, 0, "Prime AI", "BFP_Rauch")])], "sponsor": SP,
     "params": P}, True, "aggregate|OK\nevent|FOR|0.25|10\nevent|FOR|%s|0"
    % canon_float(0.25 * (0.25 * 2 ** (-10 / 500) + 1) - 0.25 * 2 ** (-10 / 500)))
add("V07 a removed agent's events kept", "aggregate",
    {"agents": [ag("Phoenix 480", [ae("FOR", 1, 0, "Prime AI", "BFP_Rauch")]),
                ag("Old", [ae("AGAINST", 2, 5, "Prime AI", "BFP_Rauch")], False, True)],
     "sponsor": SP, "params": P}, True, "aggregate|OK\nevent|AGAINST|2|5\nevent|FOR|0.25|0")
add("V08 landing order: oldest first, then agent order", "aggregate",
    {"agents": [ag("b", [ae("FOR", 1, 3, "x", "sx"), ae("FOR", 1, 3, "y", "sy")]),
                ag("a", [ae("FOR", 1, 3, "z", "sz"), ae("FOR", 1, 7, "w", "sw")])], "sponsor": SP,
     "params": P}, True)
add("V09 an event outside the window is not in the quota's window", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 600, "x", "sx"), ae("FOR", 1, 0, "x", "sx")])],
     "sponsor": SP, "params": P}, True, "aggregate|OK\nevent|FOR|0.25|600\nevent|FOR|0.25|0")
add("V10 an event at the window's edge is in it", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 500, "x", "sx"), ae("FOR", 1, 0, "x", "sx")])],
     "sponsor": SP, "params": P}, True)
add("V11 a counterpart-less FOR (an adopted audit contribution) at base", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 0, None, None)])], "sponsor": SP, "params": P}, True,
    "aggregate|OK\nevent|FOR|1|0")
add("V12 five counterparts in turn: admission rises toward full", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 5 - i, "c%d" % i, "s%d" % i) for i in range(5)])],
     "sponsor": SP, "params": P}, True)
add("V13 c_max 0.5", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 0, "x", "sx")])], "sponsor": SP,
     "params": fleet({"c_max": 0.5})}, True, "aggregate|OK\nevent|FOR|0.5|0")
add("V14 base 0 admitted as 0", "aggregate",
    {"agents": [ag("a", [ae("FOR", 0, 0, "x", "sx")])], "sponsor": SP, "params": P}, True,
    "aggregate|OK\nevent|FOR|0|0")
add("V15 duplicate agent refused", "aggregate",
    {"agents": [ag("a", []), ag("a", [])], "sponsor": SP, "params": P}, True, "ERROR:agents")
add("V16 kind unknown refused", "aggregate",
    {"agents": [ag("a", [ae("HELD", 1, 0, "x", "sx")])], "sponsor": SP, "params": P}, True,
    "ERROR:agents")
add("V17 counterpart sponsor not a name refused", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 0, "x", 5)])], "sponsor": SP, "params": P}, True,
    "ERROR:agents")
add("V18 sponsor empty refused", "aggregate", {"agents": [ag("a", [])], "sponsor": "",
                                               "params": P}, True, "ERROR:agents")
add("V19 c_max missing refused", "aggregate",
    {"agents": [ag("a", [])], "sponsor": SP, "params": {k: v for k, v in P.items() if k != "c_max"}},
    True, "ERROR:params:c_max")
add("V20 certified agent with no events: OK, empty", "aggregate",
    {"agents": [ag("a", [])], "sponsor": SP, "params": P}, True, "aggregate|OK")
add("V21 mixed", "aggregate",
    {"agents": [ag("a", [ae("FOR", 1, 40, "x", "sx"), ae("AGAINST", 2, 20, "x", "sx"),
                         ae("FOR", 1, 0, "y", "sy")]),
                ag("b", [ae("FOR", 1, 30, "x", "sx"), ae("FOR", 1, 30, "b2", SP)])],
     "sponsor": SP, "params": P}, False)

CE = [{"kind": "CE-F1", "resolution": "FOR", "age": 10},
      {"kind": "CE-A4", "resolution": "AGAINST", "age": 0}]
add("Y01 user evidence: pooled and conduct", "user_evidence",
    {"agents": [ag("a", [ae("FOR", 1, 0, "x", "sx")])], "conduct": CE, "sponsor": SP,
     "params": P}, True)
add("Y02 NONE with no certified agent", "user_evidence",
    {"agents": [ag("a", [ae("FOR", 1, 0, "x", "sx")], False)], "conduct": CE, "sponsor": SP,
     "params": P}, True, "NONE")
add("Y03 no events at all: the prior", "user_evidence",
    {"agents": [ag("a", [])], "conduct": [], "sponsor": SP, "params": P}, True,
    "OK|0|0|0|0.5|0.025|0.975")
add("Y04 a NEUTRAL conduct event adds nothing", "user_evidence",
    {"agents": [ag("a", [])], "conduct": [{"kind": "CE-F2", "resolution": "NEUTRAL", "age": 0}],
     "sponsor": SP, "params": P}, True, "OK|0|0|0|0.5|0.025|0.975")
near("Y05 CE-A5 weighs h_C 3: E = 1/5, lo = 1 - 0.975^(1/4)", "user_evidence",
     {"agents": [ag("a", [])], "conduct": [{"kind": "CE-A5", "resolution": "AGAINST", "age": 0}],
      "sponsor": SP, "params": P}, 1 - 0.975 ** 0.25, 5)
add("Y06 a PENDING conduct event refused", "user_evidence",
    {"agents": [ag("a", [])], "conduct": [{"kind": "CE-F1", "resolution": "PENDING", "age": 0}],
     "sponsor": SP, "params": P}, True, "ERROR:events")
add("Y07 conduct event kind unknown refused", "user_evidence",
    {"agents": [ag("a", [])], "conduct": [{"kind": "CE-X", "resolution": "FOR", "age": 0}],
     "sponsor": SP, "params": P}, True, "ERROR:events")
add("Y08 conduct age negative refused", "user_evidence",
    {"agents": [ag("a", [])], "conduct": [{"kind": "CE-F1", "resolution": "FOR", "age": -1}],
     "sponsor": SP, "params": P}, True, "ERROR:conduct")
add("Y09 g_C missing refused", "user_evidence",
    {"agents": [ag("a", [])], "conduct": [], "sponsor": SP,
     "params": {k: v for k, v in P.items() if k != "g_C"}}, True, "ERROR:params:g_C")
add("Y10 conduct not a list refused", "user_evidence",
    {"agents": [ag("a", [])], "conduct": {}, "sponsor": SP, "params": P}, True, "ERROR:conduct")
for i in range(10):
    add("Y%02d sweep" % (11 + i), "user_evidence",
        {"agents": [ag("a", [ae("FOR", 1, 10 * j, "c%d" % (j % (i + 1)), "s%d" % (j % (i + 1)))
                            for j in range(i + 2)]),
                    ag("b", [ae("AGAINST", 2, 5 * i, "c0", "s0")] if i % 2 else [])],
         "conduct": CE if i % 3 == 0 else [], "sponsor": SP, "params": P}, False)

add("Z01 CE-A5 with m_2: 0.8 x (1 - 0.5 x 0.5)", "sponsor_cut", {"S": 0.8, "m_class": 2,
                                                                  "params": P}, True, "0.6")
add("Z02 m_1", "sponsor_cut", {"S": 0.8, "m_class": 1, "params": P}, True, "0.7")
add("Z03 m_3", "sponsor_cut", {"S": 0.8, "m_class": 3, "params": P}, True, "0.74")
add("Z04 SPONSOR CUT 1", "sponsor_cut", {"S": 0.8, "m_class": 2,
                                         "params": fleet({"SPONSOR CUT": 1})}, True, "0.4")
add("Z05 SPONSOR CUT 0", "sponsor_cut", {"S": 0.8, "m_class": 2,
                                         "params": fleet({"SPONSOR CUT": 0})}, True, "0.8")
add("Z06 S 0", "sponsor_cut", {"S": 0, "m_class": 2, "params": P}, True, "0")
add("Z07 S 1", "sponsor_cut", {"S": 1, "m_class": 2, "params": P}, True, "0.75")
for i in range(10):
    add("Z%02d sweep" % (8 + i), "sponsor_cut", {"S": 0.05 + 0.09 * i, "m_class": i % 3 + 1,
                                                 "params": P}, False)
add("Z18 m_class 0 refused", "sponsor_cut", {"S": 0.8, "m_class": 0, "params": P}, True,
    "ERROR:m_class")
add("Z19 S above 1 refused", "sponsor_cut", {"S": 1.2, "m_class": 1, "params": P}, True,
    "ERROR:S")
add("Z20 SPONSOR CUT above 1 refused", "sponsor_cut",
    {"S": 0.8, "m_class": 1, "params": fleet({"SPONSOR CUT": 1.5})}, True,
    "ERROR:params:SPONSOR CUT")

names = [c["name"] for c in cases]
assert len(names) == len(set(names)), "duplicate case names"
counts = {}
for c in cases:
    counts[c["fn"]] = counts.get(c["fn"], 0) + 1
for fn, n in sorted(counts.items()):
    assert n >= 20, (fn, n)
out = {"corpus": "score_corpus", "version": "0.1.0", "module": "tpc_score 0.1.0",
       "params_fixture": "PMP-TPM 0.12.0 fleet defaults (params_fixture.FLEET)",
       "counts": counts, "cases": cases}
with open(os.path.join(here, "score_corpus.json"), "w", encoding="utf-8") as f:
    json.dump(out, f, indent=1, ensure_ascii=False)
    f.write("\n")
print("score_corpus.json", len(cases), "cases", counts)