- .gitea/ci/affected_tests.py: which test_*.py a diff reaches (static import graph plus names in strings, transitive), a fixed safety set, SELECT ALL for anything it cannot map. .gitea/ci/run-affected.sh runs the selection in the CI image. run-tests.sh takes CI_ONLY for that; checks.yml never sets it and test_affected_tests.py fails if it does. The merge gate still runs every file. - test_modes_p4 -> p4/p4b/p4c/p4d, test_safety126 -> +b, test_safety125 -> +b: same check lines, none dropped (146, 35, 45 before and after). - run-tests.sh: 10 workers instead of 6, new longest-first order. Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Bdqr7PiaHcBnzPituu2yTC
358 lines
15 KiB
Python
358 lines
15 KiB
Python
"""Runnable check for SAFETY-126. `python3 test_safety126.py`
|
|
|
|
During the night grid charge, with both batteries under the peak reserve,
|
|
the add-on logged ERROR PEAK_GUARD_FLOOR_LIMITED and raised "Battery stopped
|
|
limiting the peak" at quarter ends, and the Batteries "Why" blinked "The peak
|
|
limit needs power from the batteries.", while every quarter landed on the aim.
|
|
The cause: the charge lands the quarter on the aim, a 1 W landing error in
|
|
the quarter's last second becomes tens of kW through `excess_w()`'s
|
|
900 / remaining, and the alarm fired on any `wanted > 0` when no battery can
|
|
give. DECISIONS/SAFETY-126-landing-error-is-not-an-overrun.md.
|
|
|
|
Drives the REAL Controller (read_inputs + cycle, the tariff task on a
|
|
day/night profile, a P1 meter on a simulated clock, OPT-15's lag), as
|
|
test_safety125.py does, with the cycle a fraction of a second off the
|
|
quarter's edge as on the rig.
|
|
|
|
1. The night grid charge under the reserve: no ERROR, no notification, no
|
|
red peak row, no "Why" peak, every quarter on the aim; the guard did want
|
|
power at the quarter's end (not vacuous); with the fix reverted the same
|
|
run alarms. The commands are the same with and without the fix.
|
|
2. A real overrun under the reserve still alarms, a step load at the same
|
|
cycle as before, and names both batteries. After it, the latch is reset:
|
|
the night's later landings are quiet again.
|
|
3. `misses_aim()` on its own.
|
|
4. The page and the "Why" on a decision within the aim.
|
|
|
|
Mutations (MUTATION.md, purged copies) are listed in the pull request.
|
|
|
|
CI-18: this file holds parts 1 and 4 (part 4 grades part 1's nights). Parts 2 and 3 are in test_safety126b.py, split out so
|
|
CI can run the pieces side by side (the one file took ~13 min). Every check is in
|
|
exactly one file; the two print exactly the check lines the one file did."""
|
|
|
|
|
|
import asyncio
|
|
import dataclasses
|
|
import io
|
|
import logging
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import types
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, HERE)
|
|
sys.path.insert(0, os.path.join(HERE, "tools"))
|
|
|
|
import opt11p3_replay as rp # noqa: E402
|
|
from named_absence import first, nth, present # noqa: E402
|
|
import main_files # noqa: E402
|
|
import app.main as m # noqa: E402
|
|
import app.p1 as p1mod # noqa: E402
|
|
from app import homeview, messages, peakguard, tariff # noqa: E402
|
|
from app import price as _price, static_tariff as _st # noqa: E402
|
|
from app.main import Controller # noqa: E402
|
|
from app.p1 import make_sample, split_signed # noqa: E402
|
|
from app.store import unit_key # noqa: E402
|
|
from app.tariff import QUARTER_S, Projection # noqa: E402
|
|
|
|
logging.disable(logging.NOTSET)
|
|
logging.getLogger().setLevel(logging.CRITICAL)
|
|
_ERRORS = io.StringIO()
|
|
_h = logging.StreamHandler(_ERRORS)
|
|
_h.setLevel(logging.ERROR)
|
|
logging.getLogger("goodwe").addHandler(_h)
|
|
logging.getLogger("goodwe").setLevel(logging.ERROR)
|
|
logging.getLogger("goodwe").propagate = False
|
|
|
|
fails = []
|
|
|
|
|
|
def check(name, cond):
|
|
print(f" {'ok ' if cond else 'FAIL'} {name}")
|
|
if not cond:
|
|
fails.append(name)
|
|
|
|
|
|
PEAK = 5413.17 # October's banked peak on the live site
|
|
AIM_CAP = 3500.0 - 50.0
|
|
AIM_PEAK = PEAK - 50.0
|
|
TOL = 10.0 # W over the aim a quarter may land (SAFETY-81)
|
|
L = timezone(timedelta(hours=1))
|
|
NIGHT = datetime(2026, 10, 30, 22, 0, tzinfo=L).astimezone(timezone.utc)
|
|
PROFILE = ('{"periods": ['
|
|
'{"name": "night", "days": "all", "from": "22:00", "to": "07:00",'
|
|
' "eur_per_kwh": 0.2333},'
|
|
'{"name": "day", "days": "all", "from": "07:00", "to": "22:00",'
|
|
' "eur_per_kwh": 0.2801}]}')
|
|
OWNER = {"a": "leftover_solar", "b": "cheap_hours"}
|
|
|
|
|
|
def opts(*, modes=True, arb=False):
|
|
o = dict(rp.options(modes, arb, 2))
|
|
o.update(cycle_cost_threshold_eur=0.03, static_profile=PROFILE,
|
|
peak_cap_w=3500, peak_margin_w=50)
|
|
o["inverters"][0].update(max_w=5000, capacity_kwh=15.36)
|
|
o["inverters"][1].update(max_w=4600, capacity_kwh=20.48)
|
|
return o
|
|
|
|
|
|
class Harder(rp.Plant):
|
|
"""Charges harder than asked, as inverter-a did live (test_safety125.py:
|
|
asked -4271 W, measured about -4750 W; asked -5000 W, about -5210 W), up
|
|
to `cap` W. So a charge sized to the aim lands a few W over it, as on the
|
|
rig (worst +7 W)."""
|
|
cap = -5216.0
|
|
|
|
def step(self, dt, floor):
|
|
c = self.cmd
|
|
if c < 0:
|
|
self.cmd = max(c * 1.11, self.cap)
|
|
try:
|
|
super().step(dt, floor)
|
|
finally:
|
|
self.cmd = c
|
|
|
|
|
|
async def _no_fetch(*a, **k):
|
|
raise AssertionError("the tariff task reached for a price feed")
|
|
|
|
|
|
def without_fix(fn):
|
|
"""release/1.0 cc9bbeb: no decision is ever within the aim."""
|
|
real = getattr(Controller, "_peak_landing", None)
|
|
if real is None:
|
|
return fn()
|
|
Controller._peak_landing = lambda self, decision: decision
|
|
try:
|
|
return fn()
|
|
finally:
|
|
Controller._peak_landing = real
|
|
|
|
|
|
def drive(o, *, modes=OWNER, socs=(10.0, 10.0), house=600.0, minutes=46,
|
|
start=NIGHT, off=0.7, peak=None):
|
|
"""The real Controller from `start` + `off` s for `minutes` at a 1 s
|
|
cycle with OPT-15's lag, so a cycle lands `1 - off` s before each
|
|
quarter's end. `house` is W or a function of t. Returns a dict: the
|
|
quarters' true grid averages, the commands, the reserve notifications,
|
|
the ERROR lines, and per cycle where the guard engaged: (t, floor
|
|
limited, within the aim, battery "Why" peak, red reserve row, red
|
|
saturated row, alarm latched)."""
|
|
store = rp._Store()
|
|
for u, mode in (modes or {}).items():
|
|
store.data[unit_key(f"inverter-{u}", "pack_use")] = True
|
|
store.data[unit_key(f"inverter-{u}", "pack_mode")] = mode
|
|
store.data[unit_key(f"inverter-{u}", "pack_sell")] = False
|
|
load = house if callable(house) else (lambda t: float(house))
|
|
t0 = start + timedelta(seconds=off)
|
|
out = {"cmds": [], "notes": [], "seen": [], "sums": {}}
|
|
_ERRORS.seek(0)
|
|
_ERRORS.truncate()
|
|
|
|
async def go():
|
|
lag = rp.Lag()
|
|
hass = rp.Hass(("a", "b"), load, {"a": socs[0], "b": socs[1]},
|
|
limits={"a": (-5000.0, 5000.0),
|
|
"b": (-4600.0, 4600.0)}, lag=lag)
|
|
for u, cap in (("a", -5216.0), ("b", -4800.0)):
|
|
hass.plants[u].__class__ = Harder
|
|
hass.plants[u].cap = cap
|
|
real_set = hass.set_number
|
|
|
|
async def set_number(e, v):
|
|
out["cmds"].append((round(hass.t, 3), e, float(v)))
|
|
return await real_set(e, v)
|
|
|
|
async def call(*a):
|
|
if a[:2] == ("persistent_notification", "create") \
|
|
and a[2]["notification_id"] == messages.ID_PEAK_RESERVE:
|
|
out["notes"].append((hass.t, a[2]["message"]))
|
|
return True
|
|
hass.set_number, hass.call = set_number, call
|
|
tmp = tempfile.mkdtemp(prefix="s126-")
|
|
db = os.path.join(tmp, "tariff.db")
|
|
if peak is not None:
|
|
tariff.PeakStore(db)._db.execute(
|
|
"INSERT INTO monthly_peak VALUES (?, ?, ?, NULL)",
|
|
(tariff.month_key(t0), peak,
|
|
"2026-10-21T20:15:00+00:00")).connection.commit()
|
|
c = Controller(dict(o), hass, store, rp._Pub(), stop=asyncio.Event(),
|
|
peak_db_path=db,
|
|
price_history_path=os.path.join(tmp, "ph.json"))
|
|
c.inverters[0].dev_min, c.inverters[0].dev_max = -5000.0, 5000.0
|
|
c.inverters[1].dev_min, c.inverters[1].dev_max = -4600.0, 4600.0
|
|
c.auto = True
|
|
real = main_files.live.datetime, homeview.datetime, p1mod.time
|
|
clock, simt = [t0], [0.0]
|
|
|
|
class _DT(real[0]):
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
return clock[0] if tz is None else clock[0].astimezone(tz)
|
|
main_files.live.datetime = homeview.datetime = _DT
|
|
p1mod.time = types.SimpleNamespace(monotonic=lambda: 1e6 + simt[0])
|
|
real_load = _price.load_prices, _st.load_prices
|
|
_price.load_prices = _st.load_prices = _no_fetch
|
|
task = (asyncio.create_task(c.run_tariff_refresh())
|
|
if o.get("battery_modes_enabled") or o.get("arbitrage_enabled")
|
|
else None)
|
|
try:
|
|
hass.advance(0.0, 10.0)
|
|
t = 0.0
|
|
while t < minutes * 60:
|
|
now = t0 + timedelta(seconds=t)
|
|
hass.t, simt[0], clock[0] = t, t, now
|
|
if t == 0.0 and task is not None:
|
|
for _ in range(500):
|
|
if c.tariff_plan is not None:
|
|
break
|
|
await asyncio.sleep(0)
|
|
q = int(now.timestamp() // QUARTER_S)
|
|
acc = out["sums"].setdefault(q, [0.0, 0])
|
|
if t >= lag.p1_s:
|
|
imp, exp = split_signed(hass._past(lag.meter_s
|
|
+ lag.p1_s)[0])
|
|
c.p1.submit(make_sample(
|
|
"ha_signed", imp, exp, phases=1,
|
|
ingest_ts=now - timedelta(seconds=lag.p1_s),
|
|
ingest_mono=1e6 + t))
|
|
await c.read_inputs(now_mono=t)
|
|
await c.cycle()
|
|
hass.advance(lag.tick, 10.0)
|
|
acc[0] += max(0.0, hass.grid())
|
|
acc[1] += 1
|
|
g = c.peak_guard
|
|
if g is not None and g.engaged and g.blind is None:
|
|
rows = {r["id"]: r for r in c.checks(now)}
|
|
row = rows.get("peak_guard", {})
|
|
red = row.get("ok") is False and not row.get("warn")
|
|
out["seen"].append((
|
|
t, g.floor_limited, getattr(g, "within_aim", False),
|
|
homeview.peak_engaged(c),
|
|
red and "banner" in row,
|
|
red and "banner" not in row,
|
|
c._peak_floor_limited))
|
|
t += lag.tick
|
|
finally:
|
|
if task is not None:
|
|
task.cancel()
|
|
try:
|
|
await task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
main_files.live.datetime, homeview.datetime, p1mod.time = real
|
|
_price.load_prices, _st.load_prices = real_load
|
|
return c
|
|
out["c"] = asyncio.run(go())
|
|
out["quarters"] = {
|
|
datetime.fromtimestamp(q * QUARTER_S, L).strftime("%H:%M"): s / k
|
|
for q, (s, k) in sorted(out["sums"].items()) if k >= QUARTER_S - 1}
|
|
out["errors"] = [x for x in _ERRORS.getvalue().splitlines()
|
|
if "PEAK_GUARD_FLOOR_LIMITED" in x]
|
|
return out
|
|
|
|
|
|
def show(tag, r):
|
|
print(f" {tag}: quarters "
|
|
f"{ {k: round(v, 1) for k, v in r['quarters'].items()} }, "
|
|
f"guard cycles {len(r['seen'])}, floor-limited "
|
|
f"{sum(1 for s in r['seen'] if s[1])}, within-aim "
|
|
f"{sum(1 for s in r['seen'] if s[2])}, Why peak "
|
|
f"{sum(1 for s in r['seen'] if s[3])}, notes {len(r['notes'])}, "
|
|
f"errors {len(r['errors'])}")
|
|
|
|
|
|
def quiet(r):
|
|
"""No reserve alarm in any form: ERROR, notification, red row, latch,
|
|
"Why" peak, and no red saturated row."""
|
|
return (not r["errors"] and not r["notes"]
|
|
and not any(s[3] or s[4] or s[5] or s[6] for s in r["seen"]))
|
|
|
|
|
|
def on_aim(r, aim):
|
|
q = r["quarters"]
|
|
return len(q) >= 3 and all(v <= aim + TOL for v in q.values())
|
|
|
|
|
|
# ============================================================================ #
|
|
print("1. the night grid charge under the peak reserve")
|
|
NIGHTS = {}
|
|
# The owner's shape at the cap's aim. At the banked peak's aim it is
|
|
# test_safety125.py's live shape (A Cheap hours, B Solar first), because
|
|
# B's 4 600 W alone stays under that aim.
|
|
LIVE = {"a": "cheap_hours", "b": "solar_first"}
|
|
for tag, kw, aim in (
|
|
("aim 3450, cycle 0.3 s before the edge", dict(off=0.7), AIM_CAP),
|
|
("aim 3450, cycle 0.01 s before the edge", dict(off=0.99), AIM_CAP),
|
|
("month peak 5413, cycle 0.01 s before the edge",
|
|
dict(off=0.99, peak=PEAK, modes=LIVE, socs=(11.0, 10.0)),
|
|
AIM_PEAK)):
|
|
head = drive(opts(), **kw)
|
|
base = without_fix(lambda: drive(opts(), **kw))
|
|
NIGHTS[tag] = (head, base)
|
|
show("head " + tag, head)
|
|
show("fix reverted", base)
|
|
check(f"⭐ {tag}: no ERROR, no notification, no red peak row and no "
|
|
f"'Why' peak", quiet(head))
|
|
check(f"{tag}: every quarter at or under the aim ({aim:.0f} W + "
|
|
f"{TOL:.0f} W)", on_aim(head, aim))
|
|
check(f"{tag}: not vacuous, the guard wanted power under the reserve "
|
|
f"at a quarter's end (floor-limited, within the aim)",
|
|
any(s[1] and s[2] for s in head["seen"]))
|
|
check(f"{tag}: with the fix reverted the same run alarms",
|
|
not quiet(base))
|
|
check(f"{tag}: the commands are the same with and without the fix",
|
|
head["cmds"] == base["cmds"] and len(head["cmds"]) > 100)
|
|
_h, _b = NIGHTS["aim 3450, cycle 0.01 s before the edge"]
|
|
check("fix reverted, at 0.01 s: the ERROR, the notification and the "
|
|
"'Why' peak all come back (the rig's three symptoms)",
|
|
_b["errors"] and _b["notes"] and any(s[3] for s in _b["seen"]))
|
|
|
|
|
|
# ============================================================================ #
|
|
print("\n4. the page and the 'Why' on a decision within the aim")
|
|
_c = NIGHTS["aim 3450, cycle 0.01 s before the edge"][0]["c"]
|
|
_dec = peakguard.PeakDecision(
|
|
engaged=True, floor_w=5000.0, ceiling_w=3500.0,
|
|
ceiling_from=peakguard.CEILING_CAP, projected_w=3451.0, excess_w=9e4,
|
|
wanted_w=9e4, target_w=AIM_CAP, saturated=True, soc=30.0, soc_floor=20.0)
|
|
|
|
|
|
def page(d):
|
|
"""(the peak row is red, the "Why" says peak) for decision `d`."""
|
|
_c.peak_guard, _c._peak_floor_limited = d, False
|
|
row = first((r for r in _c.checks(datetime.now(timezone.utc))
|
|
if r["id"] == "peak_guard"), "peak row")
|
|
if not present(row):
|
|
return {"red": None}, None
|
|
return dict(row, red=row["ok"] is False and not row.get("warn")), \
|
|
homeview.peak_engaged(_c)
|
|
|
|
|
|
_r1, _w1 = page(_dec)
|
|
_r2, _w2 = page(dataclasses.replace(_dec, within_aim=True))
|
|
check("a saturated decision: red 'Load too high' row and the 'Why' peak, "
|
|
"as before", _r1["red"] and _w1)
|
|
check("...within the aim: neither", _r2["red"] is False and _w2 is False)
|
|
_lim = dataclasses.replace(_dec, floor_w=0.0, saturated=False,
|
|
floor_limited=True, soc=10.0,
|
|
soc_floor_reason=peakguard.FLOOR_AT_SOC_FLOOR)
|
|
_r3, _w3 = page(_lim)
|
|
_r4, _w4 = page(dataclasses.replace(_lim, within_aim=True))
|
|
check("floor-limited at 0 W: the red reserve row with its banner, as "
|
|
"before", _r3["red"] and "banner" in _r3 and not _w3)
|
|
check("...within the aim: no red row",
|
|
_r4["red"] is False and _w4 is False)
|
|
|
|
|
|
# ============================================================================ #
|
|
print()
|
|
if fails:
|
|
print(f"{len(fails)} SAFETY-126 check(s) FAILED:")
|
|
for f in fails:
|
|
print(f" - {f}")
|
|
sys.exit(1)
|
|
print("all SAFETY-126 checks passed")
|