EV-54: a refused peak cut no longer stops Solar; tried again next quarter
checks / unit (pull_request) Successful in 52m35s
infra-pin / fresh (pull_request) Successful in 1s

Refused cut tries are capped at 3 per quarter key and never set the session-wide gave_up. The next quarter tries again while a cut is needed. Once no cut stands, a refused cut value is dropped (sent_a and the plan current = the read-back, taken), never re-sent; the drop waits for a good, known read-back. A cut write that raises counts as a refused try. New notice text (Q3); the hold path's text is unchanged. Tests: test_ev54.py, test_ev54_drop.py (T1-T35); test_ev52 N8 knows the new text.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ems-agent
2026-11-30 00:24:58 +01:00
co-authored by Claude
parent bdfd3e6848
commit a698211318
11 changed files with 1654 additions and 15 deletions
+136 -6
View File
@@ -845,6 +845,16 @@ class SolarWriter(ManualWriter):
self.sent_at = None # EV-21: when `sent_a` went out
self.startup_zero = False # EV-52: the 0 A the start itself caused
self.sent_why = None # EV-52: the reason of `sent_a`'s write
# EV-54: the peak cut's refused tries, per quarter key, in memory only
# (DECISIONS/EV-54-refused-cut-retry.md).
self.cut_on = False # a cut stands (its cap), set per cycle
self.cut_blind = False # ... sized on a stale or blind meter now
self.cut_key = None # the quarter key the count is for
self.cut_fails = 0 # cut tries the charger did not take in it
self.cut_fail_at = None # ... the latest of them
self.cut_warned = False # ... and its one CHARGER_REFUSED was given
self.sent_cut = False # `sent_a` went out as a cut try
self.refused_cut = None # a cut value the charger did not take
def owe_start(self) -> None:
"""C1: the add-on starts in Off while an exit 0 A of its own was still
@@ -882,6 +892,52 @@ class SolarWriter(ManualWriter):
and self.sent_a <= TOL_A and not self.taken
and not self.hold)
@property
def cut_refused(self) -> bool:
"""EV-54: the quarter's cut tries are used up and the charger still
shows more than the cut (the amber row and the card's Why)."""
return bool(self.cut_fails >= MAX_TRIES
and self.refused_cut is not None and not self.taken)
def cut_quarter(self, key) -> None:
"""EV-54 (Q1, Q8): a new quarter key gives the cut MAX_TRIES tries
again. Only a known key counts: a cycle without a projection keeps
the count."""
if key is not None and key != self.cut_key:
self.cut_key, self.cut_fails = key, 0
def _cut_try(self, a) -> bool:
"""EV-54 (Q6): a write while a cut is due or stands, at or under its
cap, is a cut try (the cut, a retry, a 0 A of a way out or of a phase
change)."""
return self.cut_a is not None and float(a) <= self.cut_a + TOL_A
def drop_ready(self, cur, stuck) -> bool:
"""EV-54 (drop rule): a cut value the charger did not take, no cut
stands any more (lifted, or a new quarter's cut waits), and a known
read-back that is not stuck and not another writer's: Solar takes
the charger as it is instead of sending that value again."""
return bool(self.refused_cut is not None and not self.taken
and not self.cut_on and not stuck and cur is not None
and not self.hold and self.mismatch_since is None
and not self.fresh_start)
def _drop(self, cur, now) -> None:
"""EV-54: `sent_a` = the read-back, taken (an adopt: `gave_up` and the
try count cleared), and the plan's current = the read-back, timed as
a new decision. Nothing is written. The retry clock stays: no
non-zero write reaches the charger within RETRY_S of its refusal."""
if self.sent_a is not None and self.sent_a <= TOL_A:
self._owed(False) # that 0 A is no longer owed
at = self.fail_at
self.sent_a, self.sent_cut = float(cur), False
self._adopt(cur)
self.fail_at = at
if float(cur) >= CH.MIN_A:
self.plan._set(float(cur), now)
else:
self.plan.pause()
def amps_out(self, clamp) -> float:
"""The plan's current, never above the clamp; 0 when under 6 A."""
a = float(self.plan.a)
@@ -900,6 +956,7 @@ class SolarWriter(ManualWriter):
if held:
self.last_a = None
self.sent_a, self.taken = None, True
self.refused_cut = None
# ------------------------------------------------------------- writes
async def _write(self, drv, a, was, now, tracker, **kw) -> bool:
@@ -914,6 +971,10 @@ class SolarWriter(ManualWriter):
and self.boot else "Solar"
kw["why"] = why
self.sent_why = why
cut_try = self.sent_cut = self._cut_try(a)
if self.refused_cut is not None \
and abs(self.refused_cut - float(a)) > TOL_A:
self.refused_cut = None # EV-54: another value, not that one
self.sent_a, self.taken = float(a), False
if prev is None or abs(prev - float(a)) > TOL_A:
# EV-21 (PM Q1, formal C15): the 2 s grace of a lowering on its
@@ -930,6 +991,15 @@ class SolarWriter(ManualWriter):
self._owed(True) # C1: kept with the mode until back
try:
ok = await super()._write(drv, a, was, now, tracker, **kw)
except Exception as e: # noqa: BLE001
if not cut_try:
raise
# EV-54 (T15): a cut write that raises is a refused cut try, so it
# is spaced and capped like one, not tried once per cycle.
_LOG.debug("The peak cut write failed: %s", e)
self.log.append(("current", a, now, False))
self.attempted_a, self.prior_a = a, was
ok = False
finally:
if zero:
self.zero_sent_at = None
@@ -937,6 +1007,9 @@ class SolarWriter(ManualWriter):
self.taken = True
self.startup_zero = False
self.fails, self.fail_at, self.gave_up = 0, None, False
self.refused_cut = None
if cut_try:
self.cut_warned = False # EV-54: a taken cut re-arms it
if zero:
self._owed(False)
else:
@@ -949,6 +1022,25 @@ class SolarWriter(ManualWriter):
return "peak cut" if q.get("cut") else "Solar"
def _failed(self, now) -> None:
if self.sent_cut:
# EV-54 (Q1, F7): a refused cut try counts against the quarter's
# MAX_TRIES and never stops Solar; the next quarter tries again.
# `fails` counts it as before, so a way out of Off that follows
# is tried as often as before (N10).
self.fails += 1
self.fail_at = self.cut_fail_at = now
self.cut_fails += 1
self.refused_cut = self.sent_a
if self.cut_fails >= MAX_TRIES:
if not self.cut_warned:
self.cut_warned = True
_LOG.warning("The charger did not take the peak cut, so "
"the add-on tries again next quarter: "
"CHARGER_REFUSED.")
else:
_LOG.debug("The charger did not take the peak cut, %d "
"tries this quarter.", self.cut_fails)
return
self.fails += 1
self.fail_at = now
if self.fails >= MAX_TRIES and not self.gave_up:
@@ -966,24 +1058,44 @@ class SolarWriter(ManualWriter):
self.last_a = cur
self.attempted_a = self.prior_a = None
self.fails, self.fail_at, self.gave_up = 0, None, False
self.refused_cut = None
if self.sent_cut:
self.cut_warned = False # EV-54: a cut taken late re-arms it
# EV-52 (PM Q2): a write the charger took late is logged once, here.
late, self._late = self._late, None
if late is not None and cur is not None \
and abs(float(cur) - float(late[0])) <= TOL_A:
self._log_set(*late)
async def _put(self, drv, want, cur, now, tracker, stuck=False) -> None:
async def _put(self, drv, want, cur, now, tracker, stuck=False,
out=False) -> None:
"""Make the charger match `want`: write a new decision, try one the
charger did not take again after `RETRY_S`. A new 0 A gets its own
`MAX_TRIES` and never waits (0 A is the safe side)."""
`MAX_TRIES` and never waits (0 A is the safe side). EV-54: a cut try
is capped per quarter, never stopped by `gave_up`; a refused cut
value no cut needs is dropped (`_drop`), never sent again, except as
a way out's 0 A (`out`)."""
if self.hold:
return
if want > TOL_A and self.nz_block:
return # C17/C18: 0 A only, no retry or raise
# EV-54: refused cut tries count in `fails` without `gave_up`; outside
# the cut they stop a write as `gave_up` did before.
stopped = self.gave_up or self.fails >= MAX_TRIES
cut_try = self._cut_try(want)
if cut_try and self.cut_fails >= MAX_TRIES:
return # EV-54 (Q1): this quarter's tries used
if cut_try and self.cut_blind and self.refused_cut is not None \
and abs(want - self.refused_cut) <= TOL_A:
return # EV-54 (N7): no retry on a stale meter
if not out and self.refused_cut is not None and not self.taken \
and not self.cut_on and (
want > TOL_A or abs(want - self.refused_cut) <= TOL_A):
return # EV-54: the drop waits for a reading
if self.sent_a is not None and abs(self.sent_a - want) <= TOL_A:
if self.held_step:
return # C18: no retry to a charger not answering
if self.taken or self.gave_up or (
if self.taken or (stopped and not cut_try) or (
self.fail_at is not None
and now - self.fail_at < RETRY_S):
return
@@ -997,11 +1109,15 @@ class SolarWriter(ManualWriter):
return
cut = (self.cut_a is not None and want <= self.cut_a + TOL_A
and self.sent_a is not None and want < self.sent_a - TOL_A)
if cut_try and self.refused_cut is None \
and self.cut_fail_at is not None \
and now - self.cut_fail_at < RETRY_S:
return # EV-54 (F6): a new episode's try
if want <= TOL_A or cut:
# A new 0 A, or EV-21's new lower cut value: its own tries, at once.
self.fails, self.fail_at, self.gave_up = 0, None, False
elif self.gave_up or (self.fail_at is not None
and now - self.fail_at < RETRY_S):
elif (stopped and not cut_try) or (
self.fail_at is not None and now - self.fail_at < RETRY_S):
return
await self._write(drv, want, cur, now, tracker,
why="peak cut" if cut else None)
@@ -1024,7 +1140,7 @@ class SolarWriter(ManualWriter):
if not self.live and self._own_readback(cur) and not stuck:
self.taken = False # C9: a 0 A is owed
if self.live and not self.hold:
await self._put(drv, 0.0, cur, now, tracker, stuck)
await self._put(drv, 0.0, cur, now, tracker, stuck, out=True)
def _watch(self, cur, now) -> None:
was = self.hold
@@ -1183,6 +1299,10 @@ class SolarWriter(ManualWriter):
self.engaged, self.restart = True, True
self.sent_a, self.taken = None, True
self.fails, self.fail_at, self.gave_up = 0, None, False
# EV-54: nothing refused is pending; a mode change re-arms the
# notice. The quarter's try count stays (Q5).
self.refused_cut, self.sent_cut, self.cut_warned = None, False, \
False
if not self.engaged:
return
if sun is not None:
@@ -1255,6 +1375,12 @@ class SolarWriter(ManualWriter):
self.out = False
self._would_n = self._would_switch = None
self.would = None
if self.refused_cut is not None and not self.taken \
and not self.cut_on and self.sent_cut:
# EV-54 (QC): no cut stands, so a value still owed is Solar's own
# (a stuck reading's way out), not a peak cut: its reason is
# Solar's and it gets Solar's own tries.
self.sent_why, self.sent_cut, self.fails = "Solar", False, 0
fault = ("error" if (r.error or r.state in ("E", "F")) else
"stuck" if stuck else None)
if not stuck:
@@ -1311,6 +1437,10 @@ class SolarWriter(ManualWriter):
if self.fresh_start:
self.fresh_start = False
self.sent_a, self.taken = None, True # 0 A first (D2)
self.refused_cut = None
if self.drop_ready(cur, stuck):
# EV-54 (drop rule): the refused cut value is not sent again.
self._drop(cur, now)
if self.seq is None:
self._watch(cur, now)
if self.hold or self.mismatch_since is not None:
+14 -5
View File
@@ -294,7 +294,7 @@ class CarMixin:
w = self.car_solar
if not self.car_solar_mode():
self._cut = self._cut_fresh(None)
w.cut_a = None
w.cut_a, w.cut_on, w.cut_blind = None, False, False
return
key = None if real_proj is None else real_proj.block_start
st = self._cut
@@ -306,7 +306,10 @@ class CarMixin:
# N9: nothing sized from a stale projection; what stands stays.
w.cut_a = st["cap"] if st["cap"] is not None \
else self._car_wait_a(st)
w.cut_on, w.cut_blind = st["cap"] is not None, True
return
w.cut_blind = False
w.cut_quarter(key) # EV-54: the quarter's refused cut tries
ex = peakguard.excess_w(real_proj, target_w)
left = QUARTER_S - float(real_proj.elapsed_s)
r = self.car.reading
@@ -355,10 +358,11 @@ class CarMixin:
st = self._cut = self._cut_fresh(key)
st["cuts"] = cuts
w.cut_a = st["cap"]
w.cut_on = st["cap"] is not None
return
if not due:
st["since"], st["wait"] = None, None # the batteries caught it
w.cut_a = None
w.cut_a, w.cut_on = None, False
return
if st["since"] is None:
st["since"], st["car0"], st["ex"] = mono, car_w, ex
@@ -373,13 +377,18 @@ class CarMixin:
st["cap"] = 0.0 if a is None else a
st["cuts"] += 1
st["wait"], st["at"] = None, mono
w.cut_a = st["cap"]
w.cut_a, w.cut_on = st["cap"], True
return
# C4: due and waiting: no raise meanwhile. PM R1: held in watts (the
# plan's current on its phases), turned into amps with the phases at
# the write.
# the write. EV-54 (G1): a refused cut value is dropped in the wait,
# so the hold is the read-back the writer takes, not that value.
w.cut_on = False
p = w.plan
now_w = float(p.a) * carmode.VOLTS * (p.n if p.n in (1, 3) else n)
a_now = float(p.a)
if w.drop_ready(cur, self.car.stuck(mono)):
a_now = float(cur)
now_w = a_now * carmode.VOLTS * (p.n if p.n in (1, 3) else n)
st["wait"] = now_w if st["wait"] is None else min(st["wait"], now_w)
w.cut_a = self._car_wait_a(st)
+1 -1
View File
@@ -211,7 +211,7 @@ class StatusMixin:
if sw.other and self.car_mode == "solar":
out.append({"ok": False, "warn": True, "id": "charger_other",
"text": messages.row_charger_other_solar()})
if sw.gave_up:
if sw.gave_up or getattr(sw, "cut_refused", False): # EV-54
out.append({"ok": False, "warn": True, "id": "charger_refused",
"text": messages.row_charger_refused()})
if sw.phases_gave_up and self.car_mode == "solar":
+3 -2
View File
@@ -1386,11 +1386,12 @@ def _car_solar_why(c, key):
return _t("car.why.solar_peak")
if w.hold:
return _t("car.why.solar_other")
refused = w.gave_up or getattr(w, "cut_refused", False) # EV-54
if w.zero_owed:
# The way-out 0 A is not taken yet: never "paused".
return _t("car.why.solar_zero_failed" if w.gave_up
return _t("car.why.solar_zero_failed" if refused
else "car.why.solar_zeroing")
if w.gave_up:
if refused:
return _t("car.why.solar_refused")
if c.shadow:
a, n = w.would if w.would is not None else (0.0, p.n)