Repository navigation
fix(sungrow): confirm the battery instead of trusting the label - #40
Conversation
Codex review on #39 found the guard incomplete, and it was right. When register 4999 fails or returns the documented 0/0xFFFF sentinel, detection settles on "unknown" and nothing ever classifies the device as "string". A guard testing for "string" alone falls straight through, so an SG inverter with an unreadable device-type register still got the 13049/13051 writes and still reported success. The suggested fix -- gate on a positively identified "hybrid" -- trades one outage for another. An SH inverter whose 4999 is unreadable settles on "unknown" too, but its battery block answers, so the driver reads that battery and reports its SoC every five seconds. Refusing to command a battery the driver is actively reading is its own outage, and detection giving up is common enough that DETECT_ATTEMPTS exists for it. So the rule is evidence rather than labels: take a battery command when the device named itself a hybrid, or when its battery registers have answered. Refuse otherwise. The read path may probe its way out of "unknown" because a failed read is bounded and self-correcting; a write to a register the model does not implement is neither. packages/v1/sungrow/targets/ftw.lua had the identical gap and gets the identical fix, so both control paths still refuse on one rule with one no_battery code. The v2 target had no pytest at all -- only the Lua harness, which CI does not run, which is the same way Pixii's 40288 came back. One parametrised test now holds both files to the rule, as #34 learned to do. It pins the refusal on a named string inverter and on an unidentified one that answers no battery register, and pins both ways refusing could go too far: a hybrid must still take a battery command, named or merely observed, and a string inverter must still take a curtail. Verified against the previous guard: the unidentified-string case fails for the catalog driver and the v2 target alike. Signed-off-by: Fredrik Ahlgren <[email protected]> Co-Authored-By: Claude Opus 5 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8e70b41bd1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| bat_regs = optional_read(13019, 4, "input") | ||
| end | ||
| if bat_regs then | ||
| battery_confirmed = true |
There was a problem hiding this comment.
Validate the full battery reply before confirming it
When register 4999 is unreadable and the FTW target receives a short response for the four-register battery read, optional_read accepts the table after checking only registers[1]; this line then permanently sets battery_confirmed before the subsequent nil arithmetic aborts the poll. A later battery command therefore passes the new guard and writes the EMS registers despite never receiving a complete battery block. I reproduced this with _modbus_read_short_counts[13019] = 1: every poll failed, but driver_command_v2 returned applied and performed two writes. Validate all four values before setting the flag, as the catalog copy already does.
AGENTS.md reference: AGENTS.md:L27-L30
Useful? React with 👍 / 👎.
Resolves the CHANGELOG.md conflict with #40 (sungrow 1.5.3), which merged to main after this branch forked from #36. Both Unreleased entries kept, pixii's on top as the newer change. Signed-off-by: Claude <[email protected]>
1.5.3 required the model to have named itself a hybrid or a battery register to have answered. That is right for a dispatch and wrong for zero. Zero is not a dispatch. It is the host handing the device back to itself -- forced mode off, setpoint nought -- and it arrives from the lifecycle rather than the planner, so it lands before the first poll, which is exactly when nothing has been confirmed. Refusing to write "stop" is a different risk from refusing to write "charge": a device left in a forced state stays there. On a genuine string inverter the write fails at the Modbus layer and costs nothing; the outage it prevents is a battery with no way to be told to stop. Non-zero setpoints still need evidence, so #40's hole stays shut. Both control paths move together, as #34 taught. Five new pytest cases pin the rule on a named string inverter, an unidentified one, a hybrid, and -- the case FTW's suite exercises -- before any poll at all. Also fixes the mock that made this hard to see. host_mock.lua answered a failing modbus_read with error(), but FTW's binding pushes nil plus a message and returns; it never raises. Every driver wraps the call in pcall and works either way, which is why it went unnoticed for so long. Code written correctly for the real host did not: the v2 target checks the returned error, and under the mock it threw instead, taking the whole command down rather than reporting a structured failure. The mock now returns what the host returns, and nothing broke -- which is the point. It had been lying for free. test_pilot_safety_metadata_is_explicit pinned the sungrow package version. That test is about what the package promises -- read-only, no commands, no control permissions -- and a version is not one of those. The pin made an unrelated safety test the thing that failed on every legitimate bump. Fourth hand-maintained copy of a contract to drift here. Verified against FTW: the whole Go suite passes with this driver, including TestSungrowZeroBatteryCommandForcesIdle, which was the last thing blocking the pin move. Signed-off-by: Fredrik Ahlgren <[email protected]> Co-authored-by: Claude Opus 5 <[email protected]>
1.5.4 exempted zero from the guard #40 added, on three grounds. None of them holds. "Zero hands the device back to itself: forced mode off." It does not. set_battery_idle writes 13049 = 2 -- forced mode on -- and pins the battery at 0 W under the host's control. Its own comment two lines above says so: hold at 0 W *without* handing control back. The write that returns an inverter to self-consumption is mode 0, in driver_default_mode. FTW's two tests distinguish them: default mode asserts 13049 == 0, the zero command asserts 13049 == 2. "It arrives from the lifecycle rather than the planner." Not on FTW. The only producer of {"action":"battery","power_w":0} is the dispatch loop in go/cmd/ftw/main.go -- planner targets, or the self-tune override. Every path that has to release a device without waiting for a poll goes through Registry.SendDefault to driver_default_mode: shutdown, lease expiry, the telemetry watchdog, the stale-site-meter standdown. That entrypoint is not gated. driver_init writes self-consumption as well, before the run loop exists to accept a command. "On a string inverter the write fails at the Modbus layer and costs nothing." The incident says otherwise. first_write_error returns false if any of the three writes errors, so had the SG12RT rejected them the driver would have stopped there and reported failure. It went on to the read-back and reported success instead -- which is only reachable if the writes were taken. Some firmware accepts a write to a register it does not implement. So refusing withholds nothing that is not reachable by a route which cannot refuse, and the exemption put mode 2 and a setpoint back on an SG inverter that implements neither -- with the read-back that would catch it failing on that same device. Under 1.5.4 a zero command could also put an SG inverter *into* forced mode with no battery command able to take it out again, since non-zero stays refused. Both control paths move together: sungrow 1.5.4 -> 1.5.5, FTW v2 target 1.3.4 -> 1.3.5. Four pytest cases carry the reasoning. Two of them are the load-bearing ones, and both are proven to fail when the behaviour is put back: the release is never gated, on either copy, and driver_init reaches self-consumption on its own. Those are what make refusing safe; gate either and the argument collapses. Keeps 1.5.4's host_mock fix, which was right and independent. Co-Authored-By: Claude Opus 5 <[email protected]>
1.5.4 exempted zero from the guard #40 added, on three grounds. None of them holds. "Zero hands the device back to itself: forced mode off." It does not. set_battery_idle writes 13049 = 2 -- forced mode on -- and pins the battery at 0 W under the host's control. Its own comment two lines above says so: hold at 0 W *without* handing control back. The write that returns an inverter to self-consumption is mode 0, in driver_default_mode. FTW's two tests distinguish them: default mode asserts 13049 == 0, the zero command asserts 13049 == 2. #44 states the same fact from the other side, in the reason it gives for not gating set_self_consumption. "It arrives from the lifecycle rather than the planner." Not on FTW. The only producer of {"action":"battery","power_w":0} is the dispatch loop in go/cmd/ftw/main.go -- planner targets, or the self-tune override. Every path that has to release a device without waiting for a poll goes through Registry.SendDefault to driver_default_mode: shutdown, lease expiry, the telemetry watchdog, the stale-site-meter standdown. That entrypoint is not gated. driver_init writes self-consumption as well, before the run loop exists to accept a command. "On a string inverter the write fails at the Modbus layer and costs nothing." The incident says otherwise. first_write_error returns false if any of the three writes errors, so had the SG12RT rejected them the driver would have stopped there and reported failure. It went on to the read-back and reported success instead -- which is only reachable if the writes were taken. Some firmware accepts a write to a register it does not implement. So refusing withholds nothing that is not reachable by a route which cannot refuse, and the exemption put mode 2 and a setpoint back on an SG inverter that implements neither -- with the read-back that would catch it failing on that same device. Under 1.5.4 a zero command could also put an SG inverter *into* forced mode with no battery command able to take it out again, since non-zero stays refused. Both control paths move together: sungrow 1.5.5 -> 1.5.6, FTW v2 target 1.3.4 -> 1.3.5. Four pytest cases carry the reasoning. Two are load-bearing and both are proven to fail when the behaviour is put back: the release is never gated, on either copy, and driver_init reaches self-consumption on its own. Those are what make refusing safe; gate either and the argument collapses. #44 keeps its conclusion and loses its premise. Not gating set_self_consumption is still right -- an inverter can arrive already holding mode 2, from a container that died mid-force or an installer app, and a hybrid shipped under an unrecognised device-type code lands in the "string" branch too. Its test now seeds that state instead of creating it with a zero command, and covers driver_init as well. Keeps 1.5.4's host_mock fix, which was right and independent. Co-Authored-By: Claude Opus 5 <[email protected]>
1.5.4 exempted zero from the guard #40 added, on three grounds. None of them holds. "Zero hands the device back to itself: forced mode off." It does not. set_battery_idle writes 13049 = 2 -- forced mode on -- and pins the battery at 0 W under the host's control. Its own comment two lines above says so: hold at 0 W *without* handing control back. The write that returns an inverter to self-consumption is mode 0, in driver_default_mode. FTW's two tests distinguish them: default mode asserts 13049 == 0, the zero command asserts 13049 == 2. #44 states the same fact from the other side, in the reason it gives for not gating set_self_consumption. "It arrives from the lifecycle rather than the planner." Not on FTW. The only producer of {"action":"battery","power_w":0} is the dispatch loop in go/cmd/ftw/main.go -- planner targets, or the self-tune override. Every path that has to release a device without waiting for a poll goes through Registry.SendDefault to driver_default_mode: shutdown, lease expiry, the telemetry watchdog, the stale-site-meter standdown. That entrypoint is not gated. driver_init writes self-consumption as well, before the run loop exists to accept a command. "On a string inverter the write fails at the Modbus layer and costs nothing." The incident says otherwise. first_write_error returns false if any of the three writes errors, so had the SG12RT rejected them the driver would have stopped there and reported failure. It went on to the read-back and reported success instead -- which is only reachable if the writes were taken. Some firmware accepts a write to a register it does not implement. So refusing withholds nothing that is not reachable by a route which cannot refuse, and the exemption put mode 2 and a setpoint back on an SG inverter that implements neither -- with the read-back that would catch it failing on that same device. Under 1.5.4 a zero command could also put an SG inverter *into* forced mode with no battery command able to take it out again, since non-zero stays refused. Both control paths move together: sungrow 1.5.5 -> 1.5.6, FTW v2 target 1.3.4 -> 1.3.5. Four pytest cases carry the reasoning. Two are load-bearing and both are proven to fail when the behaviour is put back: the release is never gated, on either copy, and driver_init reaches self-consumption on its own. Those are what make refusing safe; gate either and the argument collapses. #44 keeps its conclusion and loses its premise. Not gating set_self_consumption is still right -- an inverter can arrive already holding mode 2, from a container that died mid-force or an installer app, and a hybrid shipped under an unrecognised device-type code lands in the "string" branch too. Its test now seeds that state instead of creating it with a zero command, and covers driver_init as well. Keeps 1.5.4's host_mock fix, which was right and independent. Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Fredrik Ahlgren <[email protected]>
Summary
Follow-up to #39, which merged before this could be pushed to it. The gap it closes is live on
mainright now.Codex review on #39 found the 1.5.1 guard incomplete, and it was right. The guard tested
model_family == "string". But when register 4999 fails or returns the documented0/0xFFFFsentinel, detection settles on"unknown"and nothing ever classifies the device as"string"— so the guard falls straight through. An SG inverter with an unreadable device-type register still gets the 13049/13050/13051 writes and still reports success, which is the exact behaviour #39 set out to stop.Why not the tightening the review suggested
The review proposed gating on a positively identified
"hybrid". That trades one outage for another.An SH hybrid whose 4999 is unreadable settles on
"unknown"too — but its battery block answers, so the driver reads that battery and reports its SoC every five seconds. Refusing to command a battery the driver is actively reading is its own outage, and detection giving up is common enough thatDETECT_ATTEMPTSexists for it.So the rule is evidence rather than labels:
battery_confirmedis set when registers 13019-13022 actually answer. An unidentified SG is refused; an unidentified SH is not. This follows what the driver already does elsewhere — "absence has to be proved". The read path may probe its way out of"unknown"because a failed read is bounded and self-correcting. A write to a register the model does not implement is neither.Both copies, one rule
packages/v1/sungrow/targets/ftw.luahad the identical gap and gets the identical fix, so both control paths still refuse on one rule with oneno_batterycode.The v2 target had no pytest at all — only the Lua harness, which CI does not run. That is precisely how Pixii's 40288 came back. One parametrised test now holds both files to the rule, the way #34 learned to:
Verified against the previous guard: the unidentified-string case fails for the catalog driver and the v2 target alike. The last row is the one that pins why the review's tightening was not taken.
Driver evidence
sungrow(1.5.2 → 1.5.3)set_self_consumption()still writes the EMS block unconditionally fromdriver_init,deinit,driver_default_modeanddriver_cleanup. Separate safety call, tracked separatelySafety
Package or promotion evidence
manifests/sungrow.yamlin this PRcom.sourceful.driver.sungrow— unchanged at 1.3.2. The published package builds frompackages/v1/sungrow/targets/ftw-observe.lua, which this PR does not touch.targets/ftw.luais the staged control target and is not inartifact_inputsftw-coreonlyrequired:required, unchanged.targets/ftw.luastays atcontrol_enabled: falsefalseChecks
Signed-off-by.make test-driver ID=sungrowmake package-driver ID=sungrow TARGET=ftw-coremake check— 3018 passed, 1361 skipped, no generator drift🤖 Generated with Claude Code