Repository navigation
fix(mpc): treat non-positive battery-limit overrides as config error - #93
Conversation
buildMPC used to honor any explicit batteries.max_charge_w / max_discharge_w override in config, including 0. A zero override zeroed maxChg / maxDis aggregates while leaving totalCap intact — planner started, solved the DP with no usable actions, and emitted plans where every slot said battery_w=0. From the UI that looks identical to "MPC disabled" even though the service is perfectly alive. Now: only positive overrides win; zero or negative are ignored with a warning and the 0.5C default takes over. Simpler than disabling MPC outright (which would be worse — current behavior would still produce an empty plan and mask the misconfiguration) and cheaper than a strict startup validation pass. Spotted after an operator report showed max_charge_w=0 in the "mpc: optimize params" log with a 20 kWh battery. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ddf8e3d312
ℹ️ 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".
| dis := defaultP | ||
| if b, ok := cfg.Batteries[d.Name]; ok { | ||
| if b.MaxChargeW != nil { | ||
| if b.MaxChargeW != nil && *b.MaxChargeW > 0 { |
There was a problem hiding this comment.
Preserve zero-valued battery power overrides
This > 0 gate turns an explicit max_charge_w: 0 / max_discharge_w: 0 into the 0.5C default, which re-enables a direction the operator explicitly disabled. The MPC core supports one-sided limits (MaxChargeW/MaxDischargeW are valid at zero and the action grid is -MaxDischargeW…+MaxChargeW in go/internal/mpc/mpc.go), so rejecting zero here changes behavior from “forbid charge/discharge” to “allow default power,” potentially causing unexpected battery movement in production. The guard should only reject negative values (or specifically handle the both-zero case) rather than all non-positive overrides.
Useful? React with 👍 / 👎.
PR #93 was too aggressive: it treated every non-positive override as a config error, including intentional one-sided constraints. But `max_charge_w: 0` is a legitimate "forbid charging, allow discharge" directive that mpc.Optimize's action grid (`-MaxDischargeW…+MaxChargeW`) already supports. The fix was overriding operator intent. Only the both-zero case (charge and discharge both 0) stays treated as a config error — that leaves the planner with no action space and produces the empty plan that looked like "disabled" in the UI. One-sided zero passes through untouched; negative values still fall back to default 0.5C with a warning. Also fixes a second time-sensitive flake in TestSlotDirectiveAtDischarge that was anchored to a hardcoded wall-clock date (same root cause as the one fixed in #91). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
… on Add (#95) * fix(mpc): preserve one-sided zero battery limits (Codex follow-up) PR #93 was too aggressive: it treated every non-positive override as a config error, including intentional one-sided constraints. But `max_charge_w: 0` is a legitimate "forbid charging, allow discharge" directive that mpc.Optimize's action grid (`-MaxDischargeW…+MaxChargeW`) already supports. The fix was overriding operator intent. Only the both-zero case (charge and discharge both 0) stays treated as a config error — that leaves the planner with no action space and produces the empty plan that looked like "disabled" in the UI. One-sided zero passes through untouched; negative values still fall back to default 0.5C with a warning. Also fixes a second time-sensitive flake in TestSlotDirectiveAtDischarge that was anchored to a hardcoded wall-clock date (same root cause as the one fixed in #91). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix(drivers): driver restart closes MQTT/Modbus + records health on Add Two foundational stability fixes prompted by tonight's incident on homelab-rpi where POST /api/drivers/ferroamp/restart left ferroamp in a silent `not_running: true` state until a full systemctl restart. Root cause 1: Registry.Remove tore down the runLoop + the Lua VM but never closed the driver's MQTT capability. A subsequent Add dialled a new paho client with the same clientID ("ftw-<driver>") while the old client was still connected. The broker resolved the collision by kicking the newer session on the next keepalive, and subscribe ACKs to the new client raced with the old client's disconnect — net effect was a driver whose subscriptions never activated, so extapi/data/eso messages never arrived, bat_soc stayed missing, and every tick just re-emitted the cached ehub reading. Root cause 2: driver health records were created lazily — the first RecordError (poll failure) or the first emit inserted a row in tel.AllHealth(). A freshly-added driver that was polling fine but hadn't received any MQTT data yet was therefore absent from AllHealth, and /api/status rendered it as `not_running: true, status: offline`. Same appearance as a crashed driver — operator couldn't tell them apart. Fix 1: add Close() error to the MQTTCap and ModbusCap interfaces; runLoop now calls Close() on both caps during stop-path cleanup, right after DefaultMode and Cleanup. mqtt/Capability.Close promoted from () to () error to match. Fix 2: Registry.Add eagerly calls tel.DriverHealthMut() so the record exists before runLoop runs the first poll. runLoop also now calls RecordSuccess() on every successful (non-error) poll — previously only errors and emits bumped the tick count. Tests (new registry_restart_test.go): - TestRemoveClosesMQTTCap — Close call count = 1 after Remove. - TestRemoveClosesModbusCap — same for Modbus. - TestRestartClosesOldMQTTBeforeDialingNew — three restart cycles each produce a fresh MQTT factory call; no cap leak. - TestAddCreatesHealthRecordImmediately — health visible from the instant Add returns, before runLoop has ticked. - TestRunLoopRecordsSuccessEvenWithoutEmits — TickCount > 0 after 350ms of 50ms polls even though the driver emits nothing. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
PR #93 was too aggressive: it treated every non-positive override as a config error, including intentional one-sided constraints. But `max_charge_w: 0` is a legitimate "forbid charging, allow discharge" directive that mpc.Optimize's action grid (`-MaxDischargeW…+MaxChargeW`) already supports. The fix was overriding operator intent. Only the both-zero case (charge and discharge both 0) stays treated as a config error — that leaves the planner with no action space and produces the empty plan that looked like "disabled" in the UI. One-sided zero passes through untouched; negative values still fall back to default 0.5C with a warning. Also fixes a second time-sensitive flake in TestSlotDirectiveAtDischarge that was anchored to a hardcoded wall-clock date (same root cause as the one fixed in #91). Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Operator report:
mpc: optimize params ... max_charge_w=0 max_discharge_w=0 capacity_wh=20000. Planner was alive but all slot decisions werebattery_w=0— UI looked "disabled" even though the service was running.Root:
buildMPChonored any explicitbatteries.*.max_charge_woverride, including0. Zero kills the planner's action space silently.Fix: only accept strictly-positive overrides; non-positive falls back to 0.5C default with a
slog.Warnpointing at the config mistake.🤖 Generated with Claude Code