Files
goodwe-addon/HANDOVER.md
T
glenn schrooyenandClaude Opus 5 7b4877d56d
checks / unit (pull_request) Successful in 3m35s
TEST-15: purge the bytecode cache before a mutation run, not after
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>
2026-09-01 02:07:09 +02:00

184 lines
9.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Handover — 2026-08-23
Start here. `README.md` is what the thing is; this is where it stands, what is
known to be broken, and what to do next.
---
## 1. State of the world
The add-on **is controlling a real battery right now** — the author's own house,
a GoodWe GW5000S-BP.
| | |
|---|---|
| running | `local_goodwe_controller` **0.4.0**, `boot: auto`, holding grid at zero |
| installed as | a **Local** add-on: `/addons/goodwe_controller` on HA OS at 192.168.2.158 |
| repo | Gitea `admin/goodwe-addon` (ssh://git@192.168.2.147:2222) — **private** |
| tuning | gain 0.6, `max_w` 5000, slew 1000, deadband 15, step 10, saturation 500/3, heartbeat 10 s |
| maintenance | **disabled**, and with **no completion history** — first cycle is "due" the moment it is enabled |
| e-stop | fitted at this site, Pi tap armed with `--panic` |
The thing it replaced — a working Home Assistant YAML implementation — is parked
at `/config/packages_disabled_addon_migration/` on that machine. **Rollback** is:
stop the add-on, move those four files back into `/config/packages/`, reload
automations and helpers, switch `input_boolean.goodwe_auto` on.
⚠️ **Never run both.** They write the same setpoint. Two controllers on one
actuator is the failure the whole design exists to prevent.
The research record — protocol, register map, why every tuning value is what it
is, and a long "ruled out, do not re-investigate" list — lives in the *other*
repo, `Documents/goodwe`, in `NOTES.md`. Read it before changing a constant.
---
## 2. Deploying a change
```bash
# 1. edit under goodwe_controller/
# 2. BUMP version: in goodwe_controller/config.yaml <-- not optional
scp -r goodwe_controller root@192.168.2.158:/addons/
ssh root@192.168.2.158 "ha store reload && ha addons update local_goodwe_controller"
ssh root@192.168.2.158 "ha addons logs local_goodwe_controller | tail -20"
```
⚠️ **Supervisor keys the built image by `version:`.** Without a bump it silently
reuses the old image and your change appears not to work. This cost two rounds of
"the fix doesn't work" in one evening. It applies to client updates too — a
client only gets a change when the version increments.
Run the checks before deploying. No framework needed:
```bash
cd goodwe_controller
python3 test_control.py && python3 test_maintenance.py && python3 test_arbiter.py \
&& python3 test_validate.py && python3 test_p1.py && python3 test_watchdog.py \
&& python3 test_loop.py
```
⚠️ **Mutating a module to prove a check bites? Read `goodwe_controller/
MUTATION.md` first, and run `goodwe_controller/tools/mutation_run.sh`.** A
same-size edit reverted inside one second changes neither the file size nor the
whole-second mtime, so CPython reuses the bytecode it already compiled and the
suite grades code that is not there - once already, reported as a survivor and
disproved by disassembly (TEST-15). `python3 -B` does **not** save you: it stops
bytecode being written, not a stale `.pyc` being read. The purge is the fix.
`test_watchdog.py` covers SAFETY-02's logic, its arming gate (derived from the
real arbiter, not passed in), its trip path, and the fact that the latch survives
an add-on restart. Its headline validation is not here at all: `sim/replay_sign.py`
in the infra repo drives the same module over the two captured RS485 days and must
fire on all four of the 2026-08-20 episodes and stay silent on 2026-08-23. That
needs both checkouts:
```bash
GOODWE_ADDON_REPO=/path/to/goodwe-addon python3 sim/replay_sign.py
GOODWE_ADDON_REPO=/path/to/goodwe-addon python3 sim/replay_sign.py --stub
```
The second run checks the grader still *discriminates* rather than merely still
passing — a watchdog that never trips and one that always trips must both be
rejected.
⚠️ **If you are testing the failsafe by hand, note that stopping and starting the
add-on will not clear a trip.** That is the point of it; use the *Start / stop
control* button twice, or delete `sign_tripped` from `/data/state.json`.
⚠️ `test_loop.py` takes about **95 seconds** (measured 93 s) and that is not a bug. It
drives the real control loop, which sleeps 1 s a tick, eight times over, to prove a
wall-clock property about it (SAFETY-12); mocking the clock there would leave the
assertion testing the mock. The other six suites together are ~440 checks and finish in
seconds — `test_watchdog.py` spends ~3 s of that driving a real `Controller` through a
trip. If you shorten `test_loop.py`'s windows, re-measure and correct this figure —
an understated runtime is what makes a slow test look like a hung one, and a slow test
that looks hung gets deleted.
---
## 3. Known broken / unfinished
1. **MQTT status entities are not adopted by Home Assistant.** The add-on
publishes correct discovery and live state — verified on the broker itself
(`goodwe_ctl/setpoint => 940.0`). After the entities were deleted from the
registry during development, HA would not re-adopt them, even after clearing
the retained topics and reconnecting. Affects **observability only**; control
is untouched. Try again after an HA restart, which is the cheapest next test.
2. **The repository is private**, so it cannot be used as an add-on repository
URL — HA clones anonymously. Either make it public, or keep installing
locally as at this site. Decide before the first client.
3. **No real maintenance cycle has ever run.** The mechanism is proven
end-to-end in simulated time and partially on hardware, but a genuine
low→full→hold cycle takes hours and has not happened. Do not decommission a
client's vendor controller before one has.
4. **The HA-side offline alarm is unproven.** The Pi's 30 s panic write is the
real cover; treat the HA alert as "within a few minutes".
5. **Only platform 105 is supported** — see §5.
---
## 4. What to do next, in order
1. **Peak shaving as a real strategy.** This is why the arbiter exists, and it is
now purely additive: a claim producer emitting `Claim.set("peak", P_PEAK, …)`
from the quarter-hour forecast, plus a priority. Nothing existing needs to
change — the maintenance charge-only limit already cannot bind it. Highest
value per unit of work in a capacity-tariff market.
2. **Run a full maintenance cycle** at the reference site and record what it
actually costs in energy and time.
3. **Fleet health before more features.** This system's signature failure is
*silent*: an entity-id prefix killed two safety alarms for a day, a shebang
made every call 401 while looking healthy. One site with the author watching
found those. Twenty sites will not.
4. **Decide the repository question** (§3.2).
---
## 5. The commercial question nobody has answered
**A GoodWe "-20" / G2 inverter cannot be driven by this.**
Platform comes from characters 6–8 of the serial number. Only
`ESU EMU ESA BPS BPU EMJ IJL` — platform 105, the AA55-era meter bus — are
supported. SBP G2 (`SPB`/`SPN`) and ES G2 (`ESN`/`ESC`) are platform 745, a
different protocol. The reference unit is `BPS`.
That matters more than it first sounds: **the SBP G2 is the model GoodWe
currently sells to replace a GW5000S-BP.** So today's product serves the
installed base, not the base being installed. Everything above the firmware —
control law, arbiter, maintenance, add-on, field guide — is protocol-agnostic
and would carry over; only the register map is platform-specific.
Answering it is an afternoon: put the Pi tap passively on a G2 unit's meter bus
with its vendor controller running, and capture. That either hands over the new
register map or proves the approach does not transfer. **Do this before building
more features on top of a shrinking base.**
---
## 6. Things that will bite you
Each of these was found the expensive way. They are in the code comments too.
- **`run.sh` must start `#!/usr/bin/with-contenv sh`.** s6-overlay sanitises the
environment, so a plain shebang means no `SUPERVISOR_TOKEN` and every HA call
returns 401 — while `homeassistant_api: true` makes permissions look granted.
- **A wrong entity id is not an error anywhere in HA.** It just never produces a
value, and the automation's own state still reads `on`.
- **HA composes entity ids from the device area and device name.** The same
firmware yields `sensor.goodwe_master_…` at one site and
`sensor.cellar_goodwe_master_…` at another.
- **`input_datetime` states parse timezone-naive**; subtracting `now()` raises
and kills the whole automation before it records itself as triggered. Use the
`timestamp` attribute.
- **Alpine ships paho-mqtt 1.x**, which has no `CallbackAPIVersion`, and musl has
no aiohttp wheel — install from apk, not pip.
- **A mutation run can grade code that is not there.** CPython validates a
`.pyc` on (source size, source mtime in whole seconds), so a same-size edit
reverted within one second is invisible to it and the old bytecode runs. It
manufactures *survivors*, never reds. Purge `__pycache__` -
`tools/mutation_run.sh` does - and see `goodwe_controller/MUTATION.md`.
- **The inverter holds its last command forever.** It has no meter-timeout. Every
failsafe here exists because of that one fact, and anything in doubt commands
**0 W** rather than holding.