EV-16: time the Car Why hold on the control cycle, not on page reads
checks / unit (pull_request) Successful in 24m55s
infra-pin / fresh (pull_request) Failing after 1s

car_cycle ends with _car_why_tick (display only: control writes, never reads);
the Why reads the record. Adds read-gap tests (A, B, cover off/on), a long
guard-None test and a lower bound for the hold.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude (dev)
2026-11-21 11:23:13 +01:00
parent b662d7171b
commit c707eb4776
3 changed files with 134 additions and 55 deletions
+30 -6
View File
@@ -61,8 +61,8 @@ class CarMixin:
self.car_over = False # ... held: the card and row say so
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_why_rec = None # EV-16: [shown over, since, episode]
self._car_over_ep = 0 # EV-16: bumped when car_over ends
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
@@ -394,16 +394,40 @@ 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_over_ep += 1 # EV-16: ends the held Why
self._car_over_ep += 1 # EV-16: an episode ends
self._car_over_gone = None
self._car_why_tick(guard, mono)
def _car_why_tick(self, guard, mono) -> None:
"""EV-16, display only: control writes `_car_why_rec`, never reads it.
The Car Why text ("are at" / "give all they can") is held: it changes
only when the other state has stood for `OVER_OFF_HOLD_S` of THIS
cycle's clock (so reads of /status do not matter). State: floor-limited
with the SoC read; a guard of None, or unread SoC, never counts as
floor. Cleared when car_over is off or the cover is off."""
if not (self.car_over and self.car_cover):
self._car_why_rec = None
return
cand = None if guard is None else bool(
getattr(guard, "floor_limited", False)
and getattr(guard, "soc", None) is not None)
ep = self._car_over_ep
r = self._car_why_rec
if r is None or r[2] != ep:
self._car_why_rec = [bool(cand), None, ep]
elif cand is None or cand == r[0]:
r[1] = None
else:
if r[1] is None:
r[1] = mono
if mono - r[1] >= OVER_OFF_HOLD_S:
self._car_why_rec = [cand, None, ep]
def _car_explains_alarm(self, real_proj, guard, target_w, w) -> bool:
"""EV-10: is the alarm's own test true with the car and false without
@@ -502,7 +526,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_over_ep += 1 # EV-16: ends the held Why
self._car_over_ep += 1 # EV-16: an episode ends
if s not in (chargers.NONE_, None) and self._car_prev_state in (
chargers.NONE_, None):
self._car_session += 1
+10 -26
View File
@@ -1302,34 +1302,20 @@ def _car_why(key, c, good):
# EV-16: the Why of "car over the limit" follows the guard's floor flag, which
# toggles cycle by cycle at the reserve. The text is held: it changes only when
# the other state has stood for as long as the car_over row's own hold.
# toggles cycle by cycle at the reserve. The text is held, on the control
# cycle's clock (`ctl_car.CarMixin._car_why_tick`, display only: control writes
# `_car_why_rec`, never reads it). The Why only reads it.
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`, 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, 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, ep]
c._car_why_hold = h
return h[0]
"""True: "are at {pct}"; False: "give all they can". With no valid held
record (not yet ticked, or of an ended episode) the live state, written
nowhere."""
rec = getattr(c, "_car_why_rec", None)
if rec is not None and rec[2] == getattr(c, "_car_over_ep", 0):
return rec[0]
return bool(pg is not None and pg.floor_limited and pg.soc is not None)
def _car_manual_why(c, key):
@@ -1342,11 +1328,9 @@ def _car_manual_why(c, key):
if c.car_over:
pg = getattr(c, "peak_guard", None)
if not c.car_cover:
c._car_why_hold = None
return _t("car.why.manual_nocover", a=a)
return _t("car.why.manual_over" if _over_why_is_floor(c, pg)
else "car.why.manual_full", a=a, pct=pct)
c._car_why_hold = None
if c.shadow:
t, n = w.would if w.would is not None else (c.car_a, c.car_n)
return _t("car.why.manual_would", a=f"{float(t):.0f}",