checks / unit (pull_request) Successful in 3m35s
FCST-03's developer reverted `self.provisional` to `here.provisional`, re-ran, and reported the mutation as a survivor. It was not one. CPython validates a cached `.pyc` on (source size, source mtime truncated to whole seconds), and that edit moved neither number - same length, same second - so the interpreter loaded the bytecode it already had. Disassembly settled it at the time: the source read `here`, the running code loaded `self`. A mutation run had graded code that was not there, with no error and exit status 0. This does not make the results already on the record wrong. It makes them UNVERIFIED, and that distinction is the whole card - so nothing here re-runs a campaign and nothing here quietly assumes the old numbers hold. REPRODUCED, THEN CLOSED. test_bytecode.py builds a real package whose two return values are the same length, imports it in a real subprocess, rewrites it inside the same second (retrying until both writes land in one whole second - milliseconds, no sleep) and reads back which value ran. Part 1 shows the interpreter running SELF off a file that says HERE. AND THE TWO OBVIOUS MITIGATIONS DO NOT WORK. Measured, not read off documentation: `python3 -B` and PYTHONDONTWRITEBYTECODE=1 are write-side only. Neither stops an EXISTING stale .pyc being read, and a mutation run always has one, because the previous run wrote it. Part 2 grades both still running the old code. The advice in test_control.py's coverage audit led with `-B`; it is corrected here - the purge is the half that works, and `-B` earns its place by keeping THIS run from leaving a cache for the NEXT edit to inherit. NO SLEEP ANYWHERE. One-second mtime resolution is the mechanism, not the enemy. Waiting it out would slow every run down and would still only cover the edit somebody remembered to slow down. THE HARNESS. tools/mutation_run.sh purges, prints a sha256/byte fingerprint of every app/*.py it is about to grade - so a mutation report can say WHICH source produced it, which is the reviewer's addition to this card - runs each suite under `python3 -B`, refuses a tree with no test_*.py (CI-01's rule, for CI-01's reason), and fails if a cache appears during the run. It takes the tree as an argument so the infra repo's replay harnesses can call it without being moved. WHERE IT IS SAID (AC 2). MUTATION.md carries the mechanism, the measurement, the reporting rules and the survey, and every one of the 16 suites now points at it from its docstring. That list is DERIVED FROM THE TREE by test_bytecode.py part 5, so a new suite that joins this directory without the pointer is red - the same failure shape CI-01's file glob exists to prevent. CI WAS ALREADY IMMUNE, AND IS DESCRIBED THAT WAY (AC 4). Verified rather than reasoned: with the checkout bind mounted `-v "$WS:/w:ro"` the interpreter writes no __pycache__ into the tree at all, and it fails SILENTLY - no error, exit status 0 - while the same probe on a writable mount produces one. Each job also rm -rf's its workspace and clones afresh, so nothing crosses between jobs on the shared runner. PYTHONDONTWRITEBYTECODE=1 is added as belt and braces only: the `:ro` is one character wide and the next person debugging a write will be tempted by it. Part 6 grades both ON THE DOCKER COMMAND LINE rather than on the file - the first draft searched the whole workflow and stayed green after the flag was deleted, because the comment explaining it still named it. THE SHIPPED ADD-ON IS NOT AFFECTED and the question is closed: goodwe_controller/Dockerfile has set PYTHONDONTWRITEBYTECODE=1 since it was written. There is no deploy-side finding. THE SURVEY (AC 3), in MUTATION.md, scoped BY THE PROPERTY. The defect is one-directional - a mutation that never took leaves the suite green, so stale bytecode manufactures SURVIVORS and cannot manufacture a red - which puts every "all red, no survivors" claim in the window out of scope, along with mutations executed in-process (SAFETY-07's B1/B2/B3, test_peakguard parts 7 and 10, where no source file is edited) and everything run after the purge discipline landed on 2026-08-31 (SAFETY-24, SAFETY-26, OPT-04, FCST-03's re-run). What is left is the recorded SURVIVORS from source-edit runs before that date. Two were re-run here under the harness and both still stand: TARIFF-01's record_block() ignoring its policy argument, and SAFETY-03's abs() on the clamp limit - 15 suites green each, on the tree as it stood. The rest are labelled still-unverified rather than assumed or re-run; each is one harness run to close, and the moment to spend it is when somebody next touches that module. OUT OF SCOPE AND RECORDED: the infra repo's sim/ replay harnesses import the add-on and write bytecode into both trees (goodwe/sim/__pycache__ exists today), so SAFETY-02's headline replay carries the same exposure. mutation_run.sh takes a tree argument for exactly that, but the change belongs in that repo and some of those harnesses want the rig. RED AND GREEN. Every new check was broken and watched fail: the purge commented out of the harness turns part 4 red (and its non-vacuity probe already proves the fixture would mis-grade without it); one docstring pointer removed turns part 5 red naming the file; `:ro` and the `-e` flag removed from the workflow turn part 6 red. Full suite green, 16 files, in the CI image on a read-only mount - the shape the runner uses. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
221 lines
9.3 KiB
Python
221 lines
9.3 KiB
Python
"""Runnable check for the control loop's cadence. `python3 test_loop.py`
|
|
|
|
SAFETY-12: the loop must evaluate on a clock as well as on a change. Before the
|
|
fix, `run_control()` ran `cycle()` only when the meter value differed from the
|
|
previous poll, so a meter that repeats a value ran NO control cycles while the
|
|
heartbeat kept re-emitting the last command - measured on the rig during TEL-05
|
|
as 70 s at a literally constant 350.0 W with 14 arrivals and zero cycles.
|
|
|
|
⚠️ THIS TEST TAKES ~95 SECONDS OF WALL CLOCK (measured 93 s), on purpose, and
|
|
that figure is the only thing standing between it and a future developer who
|
|
decides a slow test is a broken one. The thing under test is a wall-clock floor
|
|
inside a loop that sleeps 1 s; mocking the clock would leave the assertion
|
|
testing the mock. It drives the real `run_control()` and counts what actually
|
|
happened. Eight runs of the real loop is what the eight scenarios cost.
|
|
|
|
⚠️ If you shorten the windows, re-measure and update this number and the one in
|
|
HANDOVER.md. An understated runtime reads as a hang.
|
|
|
|
`cycle_floor_s` is read from options with no lower bound in code, so a huge
|
|
value reproduces the pre-fix behaviour exactly. Every claim below is therefore
|
|
an A/B on one binary, not a comparison against a remembered old one.
|
|
|
|
⚠️ MUTATING THE MODULE THIS FILE GRADES? PURGE `__pycache__` FIRST - or run
|
|
`tools/mutation_run.sh`, which does. `python3 -B` alone does NOT save you: it
|
|
stops bytecode being WRITTEN, not a stale one being READ, and a size-preserving
|
|
edit reverted inside one second is invisible to CPython's staleness test. That
|
|
is TEST-15, and it has already turned one live mutation into a false survivor.
|
|
Read MUTATION.md before you report a survivor.
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
import time
|
|
|
|
from app.main import Controller
|
|
|
|
fails = []
|
|
NEVER = 1e9 # cycle_floor_s so large the floor can never elapse: pre-fix
|
|
|
|
|
|
def check(name, cond):
|
|
print(f" {'ok ' if cond else 'FAIL'} {name}")
|
|
if not cond:
|
|
fails.append(name)
|
|
|
|
|
|
class FakeHass:
|
|
"""A scripted meter and a plant that tracks the command instantly."""
|
|
|
|
def __init__(self, meter, soc_dies_at=None):
|
|
self.meter = meter
|
|
self.soc_dies_at = soc_dies_at
|
|
self.batt = 0.0
|
|
self.writes = []
|
|
self.t0 = time.monotonic()
|
|
|
|
async def limits(self, entity):
|
|
return (-5000.0, 5000.0, 1.0)
|
|
|
|
async def number(self, entity, invert=False):
|
|
now = time.monotonic() - self.t0
|
|
if entity == "sensor.grid":
|
|
return self.meter(now)
|
|
if entity == "sensor.batt":
|
|
return self.batt
|
|
if entity == "sensor.soc":
|
|
if self.soc_dies_at is not None and now >= self.soc_dies_at:
|
|
return None
|
|
return 50.0
|
|
return None
|
|
|
|
async def set_number(self, entity, value):
|
|
self.writes.append(float(value))
|
|
self.batt = float(value)
|
|
return True
|
|
|
|
|
|
class _Store:
|
|
data = {}
|
|
|
|
def set(self, *a):
|
|
pass
|
|
|
|
|
|
class _Pub:
|
|
enabled = False
|
|
|
|
def publish(self, *a, **k):
|
|
pass
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
|
|
BASE = {"setpoint_entity": "number.sp", "meter_entity": "sensor.grid",
|
|
"soc_entity": "sensor.soc", "batt_entity": "sensor.batt",
|
|
"meter_source": "off", "max_w": 2000, "gain": 0.6, "slew_w": 1000,
|
|
"deadband_w": 15, "target_grid_w": -10, "step_w": 10,
|
|
"heartbeat_s": 10, "stale_input_s": 15}
|
|
|
|
|
|
def drive(meter, floor, seconds, soc_dies_at=None, **over):
|
|
"""Run the real control loop for `seconds`; return (cycle times, writes)."""
|
|
async def go():
|
|
hass = FakeHass(meter, soc_dies_at)
|
|
c = Controller(dict(BASE, cycle_floor_s=floor, **over), hass, _Store(),
|
|
_Pub(), stop=asyncio.Event())
|
|
c.auto = True
|
|
stamps = []
|
|
real = c.cycle
|
|
|
|
async def counted():
|
|
stamps.append(time.monotonic() - hass.t0)
|
|
await real()
|
|
|
|
c.cycle = counted
|
|
task = asyncio.create_task(c.run_control())
|
|
await asyncio.sleep(seconds)
|
|
c.stopping = True
|
|
await asyncio.wait_for(task, timeout=10)
|
|
return stamps, hass.writes, c.target, c.reason
|
|
return asyncio.run(go())
|
|
|
|
|
|
def worst_gap(stamps, seconds):
|
|
"""Longest stretch with no cycle, including the tail after the last one."""
|
|
edges = [0.0] + list(stamps) + [seconds]
|
|
return max(b - a for a, b in zip(edges, edges[1:]))
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
print("a meter that repeats the same value")
|
|
FLAT = lambda t: 350.0 # noqa: E731
|
|
FLOOR, SECONDS = 3.0, 12.0
|
|
|
|
pre_stamps, pre_writes, _, _ = drive(FLAT, NEVER, SECONDS)
|
|
# The defect itself, demonstrated rather than described: one cycle at startup
|
|
# and then nothing for the rest of the window, while writes keep happening.
|
|
check("pre-fix: a constant meter runs exactly one cycle, at startup",
|
|
len(pre_stamps) == 1)
|
|
check("...while the heartbeat keeps writing anyway, so nothing looks wrong",
|
|
len(pre_writes) >= 2)
|
|
|
|
post_stamps, post_writes, _, _ = drive(FLAT, FLOOR, SECONDS)
|
|
check("post-fix: the same constant meter runs cycles throughout",
|
|
len(post_stamps) >= int(SECONDS / FLOOR) - 1)
|
|
# The actual acceptance criterion: a BOUND on the gap, not a count.
|
|
gap = worst_gap(post_stamps, SECONDS)
|
|
check(f"...with no gap longer than the floor ({gap:.1f}s <= {FLOOR + 1.5:.1f}s)",
|
|
gap <= FLOOR + 1.5)
|
|
check("...and the loop actually corrected, instead of holding a stale command",
|
|
len(post_writes) > len(pre_writes))
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
print("a meter that changes normally - nothing may move")
|
|
# ⚠️ THE REGRESSION GUARD, and the more important half. The floor is a deadline
|
|
# on the gap between cycles, reset by every cycle whichever trigger fired - not
|
|
# a second scheduler. On a healthy meter it must never elapse, so the cycle
|
|
# count, the write count and the commanded values must be IDENTICAL either side
|
|
# of the fix. Anything else is a new write cadence, which SAFETY-12 forbids:
|
|
# the deadband, step_w and the 10 s force-write decide what reaches the wire,
|
|
# and this change is only allowed to alter when the loop thinks.
|
|
STEPS = [0.0, 300.0, 850.0, -400.0, 120.0, -900.0, 60.0]
|
|
LIVE = lambda t: STEPS[int(t // 2) % len(STEPS)] # noqa: E731
|
|
|
|
a_stamps, a_writes, _, _ = drive(LIVE, NEVER, 12.0)
|
|
b_stamps, b_writes, _, _ = drive(LIVE, FLOOR, 12.0)
|
|
check("a changing meter runs the same number of cycles as before the fix",
|
|
len(a_stamps) == len(b_stamps))
|
|
check("...commanding exactly the same sequence of values", a_writes == b_writes)
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
print("a meter parked at the target - the converged operating point")
|
|
# The case TEL-05 warned about: at the converged target the power figure
|
|
# legitimately repeats for minutes. The loop must still run, and must still
|
|
# write nothing but the heartbeat, because the deadband - not the arrival
|
|
# trigger - is what holds the command still.
|
|
AT_TARGET = lambda t: -10.0 # noqa: E731
|
|
c_stamps, c_writes, _, _ = drive(AT_TARGET, NEVER, 12.0)
|
|
d_stamps, d_writes, _, _ = drive(AT_TARGET, FLOOR, 12.0)
|
|
check("the loop evaluates at the converged point too", len(d_stamps) > len(c_stamps))
|
|
check("...and the deadband still holds the command, so writes are unchanged",
|
|
d_writes == c_writes)
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
print("an input dies while the meter repeats - the safety claim must still fire")
|
|
# ⚠️ THE STRONGEST PROPERTY IN SAFETY-12, and the one that makes it a safety fix
|
|
# rather than a latency fix. The `inputs missing -> 0 W` claim lives INSIDE
|
|
# cycle() (main.py, the stale_input_s branch and the P_SAFETY claim below it).
|
|
# Pre-fix, on a meter whose value happened to repeat, cycle() was never called
|
|
# again - so that claim could not fire AT ALL. The controller held its last
|
|
# command through an input outage of unbounded length with the status line still
|
|
# reading "loop -> N W".
|
|
#
|
|
# Layer 1 does not cover this either on a `meter_source: off` install: no P1 age
|
|
# sensor is published (see Controller.publish), so the firmware has nothing to
|
|
# veto on, and the heartbeat keeps its watchdog fed the whole time.
|
|
#
|
|
# stale_input_s is shortened to 3 s here so the outage resolves inside a test
|
|
# rather than inside a quarter of a minute. The mechanism is the default one.
|
|
c2_stamps, c2_writes, pre_target, pre_reason = drive(
|
|
FLAT, NEVER, 10.0, soc_dies_at=4.0, stale_input_s=3)
|
|
check("pre-fix: a dead input during a constant meter never reaches the safety claim",
|
|
pre_target != 0.0 and "safety" not in pre_reason)
|
|
check(f"...so it holds its last command instead ({pre_target:g} W, {pre_reason!r})",
|
|
pre_target == 220.0)
|
|
|
|
d2_stamps, d2_writes, post_target, post_reason = drive(
|
|
FLAT, FLOOR, 10.0, soc_dies_at=4.0, stale_input_s=3)
|
|
check("post-fix: the same outage commands 0 W", post_target == 0.0)
|
|
check(f"...through the safety claim, not by accident ({post_reason!r})",
|
|
"safety" in post_reason)
|
|
check("...and the 0 W actually reached the device", 0.0 in d2_writes)
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
print()
|
|
if fails:
|
|
print(f"{len(fails)} FAILED: " + "; ".join(fails))
|
|
sys.exit(1)
|
|
print("all checks passed")
|