"""EV-20 rig round 1 (rig FAIL on cc8c0dc), with EV-32 and EV-36. `python3 -B test_ev20_rig.py` 1. The stuck check's arming race (Critical, EV-32): a write waits for its read-back while the next cycle reads the charger; the check must be armed on the reading taken before the send, so a car that then stays paused is not "stuck". Tracker rows and the real Controller in the rig's interleave (`beside`), the rig's repro scenario among them. 2. EV-36 (SPIKE-05 C4): a frozen value newer than the last read is not life. 3. No 1 A ping-pong on an amp boundary (Major). 4. The 6 A hold lasts its full `ev_pause_min` from the spare's drop, 1 and 3 phases (Major); a meter as Home Assistant publishes it (`ha_signed`, on a change only) does not pause the car while it is healthy (Minors). Mutating a module this file grades? Read MUTATION.md first (purge __pycache__). """ import math import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "tools")) from datetime import datetime, timezone # noqa: E402 from app import chargers as CH # noqa: E402 import ev20_sun as S # noqa: E402 from chargersim import SimCharger # noqa: E402 from ev20_kit import (check, const, cur_writes, nevers, report, # noqa: E402 step_fn) from named_absence import nth, present # noqa: E402 UTC = datetime(2026, 11, 23, 12, 0, tzinfo=timezone.utc) DAY = "2026-11-23" TS = 1000.0 def rd(power=2300.0, allowed=10.0, e=4004.85, state="C"): return CH.ChargerReading( available=True, state=state, connected=True, charging=state in ("C", "D"), power_w=power, energy_kwh=e, current_a=(power / 230.0, 0.0, 0.0), setpoint_a=allowed, allowed_a=allowed, phases_allowed=1) # =========================================================================== # print("1. THE STUCK CHECK'S ARMING (rig Critical, EV-32)") # =========================================================================== # # The race as the rig had it: begin_write (the send), the cycle's read lands # while the write waits (it already shows the new 0 A and the paused car), # then note_write; the car stays paused (constant 0 W, 0 A) for two minutes. tr = CH.CarTracker() for i in range(-5, 1): tr.observe(rd(e=4004.85 + i / 1000.0), TS + i, UTC, DAY) tr.begin_write() tr.observe(rd(power=0.0, allowed=0.0, state="B"), TS + 1, UTC, DAY) tr.end_write(TS + 2) tr.note_write(TS, 10.0, 0.0, True) stuck_at = None for i in range(2, 130): tr.observe(rd(power=0.0, allowed=0.0, state="B"), TS + i, UTC, DAY) if stuck_at is None and tr.stuck(TS + i): stuck_at = i check(f"race (EV-32): an accepted 10 -> 0 A write whose post-write reading " f"arrived while it waited; the car then stays paused at constant " f"values: never 'stuck' (stuck at +{stuck_at})", stuck_at is None) # The same with an accepted non-zero write (EV-32's "after an accepted current # write"): a car that then draws steadily is not stuck either. tr = CH.CarTracker() for i in range(-5, 1): tr.observe(rd(power=0.0, allowed=0.0, state="B"), TS + i, UTC, DAY) tr.begin_write() tr.observe(rd(power=1840.0, allowed=8.0), TS + 1, UTC, DAY) tr.end_write(TS + 2) tr.note_write(TS, 0.0, 8.0, True) st2 = [i for i in range(2, 130) if (tr.observe(rd(power=1840.0, allowed=8.0), TS + i, UTC, DAY) or tr.stuck(TS + i))] check(f"race (EV-32): an accepted 0 -> 8 A write, the car draws a steady " f"1840 W: never 'stuck' ({st2[:1]})", not st2) # A reading that really froze before the send still is stuck at +60 s (the # EV-04 rule kept): the write was accepted, but every reading after it is the # one from before the send. tr = CH.CarTracker() for i in range(-5, 1): tr.observe(rd(), TS + i, UTC, DAY) tr.begin_write() tr.end_write(TS + 1) tr.note_write(TS, 10.0, 0.0, True) st3 = None for i in range(1, 80): tr.observe(rd(), TS + i, UTC, DAY) if st3 is None and tr.stuck(TS + i): st3 = i check(f"kept: readings frozen at the value from before the send are stuck at " f"+60 s (at +{st3})", st3 == 60) # =========================================================================== # print("\n2. EV-36: A FROZEN VALUE NEWER THAN THE LAST READ IS NOT LIFE") # =========================================================================== # # The cycle last read energy 4004.85 kWh at TS-1. The charger froze a moment # later at 4004.87 (a newer value); the refused write's polls and every later # read show that same frozen value. tr = CH.CarTracker() for i in range(-5, 0): tr.observe(rd(e=4004.85), TS + i, UTC, DAY) tr.begin_write() polls = [(TS + s, rd(e=4004.87)) for s in range(1, 11)] tr.end_write(TS + 10) tr.note_write(TS, 16.0, 10.0, False, polls) st36 = None for i in range(11, 130): tr.observe(rd(e=4004.87), TS + i, UTC, DAY) if st36 is None and tr.stuck(TS + i): st36 = i check(f"EV-36: a refused write; the first sample after the send carries a " f"newer value that then never moves: no life, so it arms at the send " f"and is stuck at +60 s (at +{st36})", st36 == 60) tr = CH.CarTracker() for i in range(-5, 0): tr.observe(rd(e=4004.85), TS + i, UTC, DAY) tr.begin_write() polls = [(TS + s, rd(e=4004.85 + s / 1000.0)) for s in range(1, 11)] tr.end_write(TS + 10) tr.note_write(TS, 16.0, 10.0, False, polls) for i in range(11, 130): tr.observe(rd(e=4004.86 + i / 1000.0), TS + i, UTC, DAY) check("EV-36 guard: a refused write on a charger whose energy keeps moving " "is not stuck (life in the second sample and after)", tr.armed_at is None and not tr.stuck(TS + 129)) # =========================================================================== # print("\n3. THE REAL CONTROLLER IN THE RIG'S INTERLEAVE (beside)") # =========================================================================== # def rig_run(**kw): sim = kw.pop("sim", None) or SimCharger(setpoint_a=kw.pop("setpoint", 0)) sim.lag_s = 1.0 return S.drive(kw.pop("house"), kw.pop("sun"), beside=True, sim=sim, **kw) # The rig's repro, EV20-solar-steady-no-false-stuck: the charger's own 10 A, # the car charging; Solar, Car first, sun 7.9 kW, house 0.9 kW (mock-up # scenario 2): 0 A first (D2), then the start rule, then charging to the end. for mc in (False, True): tag = "meter on change" if mc else "meter every second" r = rig_run(house=const(900.0), sun=const(7900.0), dur=1800, first="car", socs={"a": 71.0, "b": 100.0}, sim=SimCharger(setpoint_a=10), meter_change=mc) w = cur_writes(r) stuck = [x["t"] for x in r["trace"] if x["fault"] == "stuck"] check(f"scenario EV20-solar-steady-no-false-stuck [{tag}]: 0 A first, the " f"start within 125 s, charging to the end, never stuck ({w[:4]}, " f"stuck {stuck[:1]})", present(nth(w, 0)) and nth(w, 0)[1] == 0.0 and present(nth(w, 1)) and nth(w, 1)[1] > 0.0 and nth(w, 1)[0] - nth(w, 0)[0] <= 125 and not stuck and r["sim"].power_w > 3000.0) nevers(r, f"scenario EV20-solar-steady-no-false-stuck [{tag}]") # E2 of the scenario: a cloud pause (0 A), then the sun back: the car # restarts 120 s after the sun returns; no stuck verdict holds it at 0 A. r = rig_run(house=const(500.0), sun=step_fn((400, 900.0), (1000, 2900.0), base=2900.0), dur=1400, first="car", socs={"a": 100.0, "b": 100.0}) w = cur_writes(r) stuck = [x["t"] for x in r["trace"] if x["fault"] == "stuck"] late = [(t, a) for t, a, _ in w if t >= 1000] check(f"scenario E2: after the cloud pause the car restarts 120 s after the " f"sun returns, never held by a stuck verdict ({w}, stuck {stuck[:1]})", not stuck and present(nth(late, 0)) and nth(late, 0)[1] > 0.0 and 1119 <= nth(late, 0)[0] <= 1125) # =========================================================================== # print("\n4. NO PING-PONG ON AN AMP BOUNDARY (rig Major)") # =========================================================================== # for ph, sun_w, amps in ((1, 2900.0, 10.0), (3, 600.0 + 6900.0, 10.0)): def dither(t, base=sun_w): return base + (6.0 if int(t // 17) % 2 else -6.0) opts = {"ev_phases": "1" if ph == 1 else "3"} r = S.drive(const(500.0), dither, dur=1500, first="car", socs={"a": 100.0, "b": 100.0}, opts=opts, phases=ph) w = [(t, a) for t, a, _ in cur_writes(r) if a > 0.0] changes = len(w) - 1 check(f"[{ph} phase] the spare dithers +-6 W around the {amps:.0f} A " f"boundary for 1500 s: at most one change after the start " f"({changes} changes: {w[:6]})", present(nth(w, 0)) and changes <= 1) nevers(r, f"ping-pong [{ph} phase]") # A real rise after a step down still raises the current. r = S.drive(const(500.0), step_fn((300, 2890.0), (600, 3200.0), base=2900.0), dur=900, first="car", socs={"a": 100.0, "b": 100.0}, opts={"ev_phases": "1"}) w = [(t, a) for t, a, _ in cur_writes(r)] check(f"guard: 10 A, down to 9 A on a 10 W dip, then a real rise to 11 A " f"worth: 11 A is written ({w})", [a for _, a in w] == [10.0, 9.0, 11.0]) # =========================================================================== # print("\n5. THE HOLD LASTS ITS TIME; A HEALTHY ha_signed METER (rig Major, Minors)") # =========================================================================== # for mode, kw in (("every second", {}), ("rig interleave, meter on change", {"beside": True, "meter_change": True})): sim = SimCharger(setpoint_a=0) if kw.get("beside"): sim.lag_s = 1.0 # 1 phase: a 290 s cloud never pauses; a 400 s one pauses at 300 s. r = S.drive(const(500.0), lambda t: 900.0 if 400 <= t < 690 or 1200 <= t < 1600 else 2900.0, dur=1800, first="car", socs={"a": 100.0, "b": 100.0}, opts={"ev_phases": "1"}, sim=sim, **kw) w = [(t, a) for t, a, _ in cur_writes(r)] zeros = [t for t, a in w if a == 0.0] check(f"[1 phase, {mode}] a 290 s cloud: the 6 A hold, no pause; a 400 s " f"cloud: 0 A at 300 s from the drop, +2 ({w})", not [t for t in zeros if 400 <= t < 700] and [t for t in zeros if 1200 <= t < 1600][:1] and 1499 <= nth([t for t in zeros if 1200 <= t < 1600], 0) <= 1502) nevers(r, f"hold [1 phase, {mode}]") sim = SimCharger(setpoint_a=0) if kw.get("beside"): sim.lag_s = 1.0 # 3 phases: 7 A, then the spare under 6 A worth from 600 s: 6 A, 0 A at # 900 s (+2), not before. r = S.drive(const(500.0), step_fn((600, 600.0 + 2500.0), base=600.0 + 6000.0), dur=1100, first="car", socs={"a": 100.0, "b": 100.0}, opts={"ev_phases": "3"}, phases=3, sim=sim, **kw) w = [(t, a) for t, a, _ in cur_writes(r)] zeros = [t for t, a in w if a == 0.0 and t > 200] check(f"[3 phases, {mode}] the hold from 600 s: 6 A, then 0 A at 900 s " f"(+2), not before ({w})", present(nth(zeros, 0)) and 899 <= nth(zeros, 0) <= 902 and 6.0 in [a for t, a in w if 600 <= t < 900]) nevers(r, f"hold [3 phases, {mode}]") # A healthy meter on ha_signed: Home Assistant publishes it only on a change, # so at a flat load the value may stand 20-40 s; the car is not paused. r = S.drive(const(500.0), lambda t: 2900.0 + (15.0 if int(t // 40) % 2 else 0.0), dur=1200, first="car", socs={"a": 100.0, "b": 100.0}, opts={"ev_phases": "1"}, meter_change=True) w = cur_writes(r) unk = [x["t"] for x in r["trace"] if x["spare"] is None and x["t"] > 5] check(f"ha_signed, a healthy meter whose value stands 40 s at a time: the " f"spare stays known and the car is never paused ({w}, unknown " f"{unk[:3]})", not [x for x in w if x[1] == 0.0 and x[0] > 5] and not unk) # A silent meter on ha_signed: unknown from the source's own meter_max_age_s # (30 s), then 0 A after ev_pause_min (300 s). r = S.drive(const(500.0), const(2900.0), dur=900, first="car", socs={"a": 100.0, "b": 100.0}, opts={"ev_phases": "1"}, events={300: lambda p, c: setattr(p, "meter_on", False)}) w = cur_writes(r) z = [t for t, a, _ in w if a == 0.0] check(f"ha_signed, the meter silent from 300 s: 0 A at 30 + 300 s (+2), " f"never from a stale value ({w})", present(nth(z, 0)) and 629 <= nth(z, 0) <= 633 and [a for t, a, _ in w if 300 <= t < 629] == []) report()