EV-16: key the held Car Why to the car_over episode; add T16, T18 and N-id checks
checks / unit (pull_request) Successful in 25m43s
infra-pin / fresh (pull_request) Failing after 2s

Control bumps _car_over_ep at each start and end of car_over; the display
ignores a hold of another episode, so a read overlapping the end cannot bring
a stale text back.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude (dev)
2026-11-21 10:04:00 +01:00
parent 3b1189cdbf
commit b662d7171b
3 changed files with 115 additions and 8 deletions
+5 -2
View File
@@ -62,6 +62,7 @@ class CarMixin:
self._car_over_since = None
self._car_over_gone = None
self._car_why_hold = None # EV-16: the held Car Why (display)
self._car_over_ep = 0 # EV-16: bumped by control at each start/end of car_over; the hold is valid for one value
self._car_batt_prev = self._car_batt_prev2 = None
self._car_hx_prev = None # EV-19: last cycle's (held, car W)
self._car_need_real = self._car_need_excl = False # EV-10 latches
@@ -393,13 +394,15 @@ class CarMixin:
if self._car_over_since is None:
self._car_over_since = mono
if mono - self._car_over_since >= OVER_HOLD_S:
if not self.car_over:
self._car_over_ep += 1 # EV-16: a new episode
self.car_over = True
else:
if self._car_over_gone is None:
self._car_over_gone = mono
if mono - self._car_over_gone >= OVER_OFF_HOLD_S:
self.car_over, self._car_over_since = False, None
self._car_why_hold = None # EV-16: display only
self._car_over_ep += 1 # EV-16: ends the held Why
self._car_over_gone = None
def _car_explains_alarm(self, real_proj, guard, target_w, w) -> bool:
@@ -499,7 +502,7 @@ class CarMixin:
# The session is over: that is a real end, with no hold.
self.car_over = False
self._car_over_since = self._car_over_gone = None
self._car_why_hold = None # EV-16: display only
self._car_over_ep += 1 # EV-16: ends the held Why
if s not in (chargers.NONE_, None) and self._car_prev_state in (
chargers.NONE_, None):
self._car_session += 1
+8 -3
View File
@@ -1309,20 +1309,25 @@ WHY_HOLD_S = 180.0 # = ctl_car.OVER_OFF_HOLD_S (tested)
def _over_why_is_floor(c, pg) -> bool:
"""True: "are at {pct}"; False: "give all they can". Display only: the
state lives in `c._car_why_hold` and nothing in control reads it."""
state lives in `c._car_why_hold`, stamped with the episode `c._car_over_ep`
that control bumps; a hold of another episode is never used, so a read
overlapping the end of car_over cannot bring a stale text back."""
mono = c.car_now()
cand = (pg.floor_limited and pg.soc is not None) if pg is not None \
else None
ep = getattr(c, "_car_over_ep", 0) # owned by the control loop
h = getattr(c, "_car_why_hold", None)
if h is not None and h[2] != ep: # a hold of an ended episode
h = None
if h is None:
h = [bool(cand), None] # shown, since when cand differs
h = [bool(cand), None, ep] # shown, since when, episode
elif cand is None or cand == h[0]:
h[1] = None
else:
if h[1] is None:
h[1] = mono
if mono - h[1] >= WHY_HOLD_S:
h = [cand, None]
h = [cand, None, ep]
c._car_why_hold = h
return h[0]
+102 -3
View File
@@ -2,7 +2,9 @@
they can" while the car-over row holds and the guard's floor flag toggles.
`python3 -B test_ev16.py`
Display only: the text is held in `c._car_why_hold`; control never reads it.
Display only: the text is held in `c._car_why_hold`; control never reads it. Control owns
`c._car_over_ep` (bumped at each start and end of car_over); a hold of
another episode is never used.
Mutant: pick the text from the live `floor_limited` again (T1, T2, T7, T8, T9
go red).
Mutating a module this file grades? Read MUTATION.md first.
@@ -279,9 +281,10 @@ run(c, 100.0, lambda t: G())
c._car_prev_state = "charging"
c.state = CH.NONE_
c._now = 105.0
ep_before = c._car_over_ep
c.car_cycle(Proj(), G(), 14000.0, None)
check("T11 (cont.) the unplug clears the hold itself",
c.car_over is False and getattr(c, "_car_why_hold", "x") is None)
check("T11 (cont.) the unplug ends the episode (control bumps it)",
c.car_over is False and c._car_over_ep > ep_before)
# T12: cover off while car_over holds
c = S()
@@ -350,6 +353,102 @@ check("a held peak_guard row is not touched: the module still has no hold "
"in ctl_status", "_car_why_hold" not in open(os.path.join(
HERE, "app", "ctl_status.py")).read())
print("\n6. EV-16 review round 1: the race, T16, T18, the N-ids")
# the race: a read takes the hold, control ends the episode, the read writes
# its old hold back; the next episode must not show it
c = S()
run(c, 100.0, lambda t: G())
stale = c._car_why_hold
check("race: the read holds 'are at' before the end", kind(why(c)) == "over")
for k in range(0, 200):
c._now = 100.0 + k
c.w = None
c.car_cycle(Proj(), G(), 14000.0, None)
c._car_why_hold = stale # the late write-back
c.car_over = True
c._car_over_ep += 1 # what control does on a start
c._now = 6000.0
c.peak_guard = G(floor_limited=False, soc=60.0)
check("N4 race: a read interleaved with the clear cannot bring the old "
"'are at' into the next episode", kind(why(c)) == "full")
c = S()
run(c, 100.0, lambda t: G())
stale = c._car_why_hold
c._car_prev_state = "charging"
c.state = CH.NONE_
c._now = 105.0
c.car_cycle(Proj(), G(), 14000.0, None)
c._car_why_hold = stale # write-back after an unplug
c.state = "charging"
c.car_over = True
c.peak_guard = G(floor_limited=False, soc=60.0)
c._now = 110.0
check("N4 race (unplug): a stale write-back after the unplug is not used",
kind(why(c)) == "full")
# T16: the existing checks this one must leave alone are present and run in CI
for f in ("test_ev04.py", "test_ev10.py", "test_ev10b.py", "test_ev18.py",
"test_ev19.py", "test_home.py", "test_named_absence.py",
"test_bytecode.py"):
check(f"T16 {f} exists (run unchanged by CI's run-tests.sh)",
os.path.exists(os.path.join(HERE, f)))
t04 = open(os.path.join(HERE, "test_ev04.py")).read()
check("T16 test_ev04's Manual Why assertion 'batteries give all they can' "
"is still there", 'batteries give all they can" in cardwhy' in t04)
# T18: the mutant, in process: the live flag picks the text again
real = homeview._over_why_is_floor
homeview._over_why_is_floor = lambda c, pg: bool(
pg is not None and pg.floor_limited)
try:
m = run(S(), Q, toggle(10.0, True))
m8 = run(S(), Q, lambda t: G(soc=None))
finally:
homeview._over_why_is_floor = real
check("T18 mutant (live flag): T1 shape alternates and T8 shows 'are at', so "
"T1, T2, T7 and T8 would go red", len(changes(m)) > 20
and any(k == "over" for _, k in m8))
# the N-ids, by name
a = run(S(), Q, toggle(10.0, True))
check("N1 control: batteries, charger writes and setpoint equal base "
"(digests, section 4)", (digest(ctl1), digest(rows1), digest(flags1))
== BASE_8150608)
check("N2 `floor_limited`, `car_explains`, `car_over`, the peak_guard row "
"are not changed by the hold: the guard flag toggles as in base "
"(flags digest) and the rows digest equals base",
digest(flags1) == BASE_8150608[2] and digest(rows1) == BASE_8150608[1])
n3 = run(S(), Q, lambda t: G(soc=None)) + run(
S(), Q, lambda t: G(floor_limited=False, soc=40.0))
check("N3 never 'are at' with SoC unread or with the guard never limited",
{k for _, k in n3} == {"full"})
c = S()
run(c, 100.0, lambda t: G())
c.car_over = False
n4 = homeview._car_manual_why(c, "charging")
check("N4 no over wording once car_over is off", OVER not in n4
and FULL not in n4)
check("N5 at most one Why change in a quarter while the guard toggles",
len(changes(a)) <= 1)
try:
for g in (None, G(soc=None), G(), G(floor_limited=False, soc=1.0)):
c = S()
c.peak_guard = g
why(c)
n6 = True
except Exception: # noqa: BLE001
n6 = False
check("N6 no Traceback for a guard of None, SoC unread, limited, not "
"limited", n6)
en = open(os.path.join(HERE, "app", "ui_text", "en.json")).read()
nl = open(os.path.join(HERE, "app", "ui_text", "nl.json")).read()
check("N7 wording unchanged: the four texts are in the files verbatim",
"The batteries are at {pct}, so this charge sets the peak." in en
and "The batteries give all they can, so this charge sets the peak."
in en and "De batterijen staan op {pct}, dus deze lading bepaalt de "
"piek." in nl and "De batterijen geven alles, dus deze lading bepaalt "
"de piek." in nl)
print()
print("FAIL: " + ", ".join(fails) if fails else "PASS: every check passed")
sys.exit(1 if fails else 0)