Repository navigation
fix(control): passive_arbitrage idle slot — don't absorb live PV surplus - #375
Conversation
When the DP picks idle for a planner_passive_arbitrage slot (battery_w ≈ 0) and live conditions show a baseline grid in export territory (PV exceeds load), the dispatcher now holds the battery at 0 instead of collapsing to self_consumption + grid_target=0 and reactively charging up to absorb the surplus. The DP picks idle slots deliberately — often to preserve export revenue when future PV is plentiful and future spot is lower. The old behaviour discarded that intent: a forecast miss (eg. load overestimated) would quantize the DP into a "do nothing this slot" decision, then dispatch swallowed the unexpected surplus into the battery anyway. The new gate mirrors plannerSelfExportSurplusGate but triggers on the LIVE baseline grid (`grid_meter − Σ battery_w`) rather than the plan's forecasted grid. The rationale: for the slot we're already in, live measurements override the forecast. Forecasts guide future slots. Reactive discharge on live import is unchanged — a passive_arbitrage idle slot with the meter importing still allows the battery to cover load. The gate is one-sided: block reactive *charging* when the meter shows export potential the forecast missed. Test: TestPlannerPassiveArbitrageIdleDoesNotAbsorbLiveSurplus. Found 2026-05-28 on site .40: load forecast 2782 W vs actual 504 W (phantom heating term — separate fix), planner picked idle on net-≈0 forecast, dispatch charged 2.6 kW into batteries despite high current spot (160 öre), low future spot (95 öre), and abundant forecast PV in upcoming slots. 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: 2253acb164
ℹ️ 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".
| passiveArbitrageIdleSlot = state.Mode == ModePlannerPassiveArbitrage && | ||
| dir.BatteryEnergyWh <= idleWhGate |
There was a problem hiding this comment.
Restrict the live-export gate to actual idle slots
For planner_passive_arbitrage, planned discharge slots have negative BatteryEnergyWh (the MPC tests assert this mode discharges during expensive windows), but this condition treats every negative slot as passiveArbitrageIdleSlot because it only checks <= 50. Since the new live-export charge block is keyed off that flag, a planned discharge slot that happens to have live PV/export will now be forced back to 0 W instead of allowing the reactive path to absorb surplus, even though that slot was not an idle “don’t charge” decision. Please bound this to near-zero energy (for example math.Abs(...) <= idleWhGate) so only true idle slots get the new gate.
Useful? React with 👍 / 👎.
Four findings from Codex on the dispatch / loadmodel changes shipped in v0.107.0. Two are P1 (regressions that defeat their own PR's purpose); two are P2 (defensive predicate + observability gap). P1 (PR #377): SeedHeatingCoef preserves learned value across restarts. main.go called SetHeatingCoef unconditionally at startup, clobbering the persisted online-fit value with the config seed. Every binary update wiped the adaptive learning. New SeedHeatingCoef is a no-op when the model already has samples; operator config is the cold-start prior, observation drives the value once learning has begun. SetHeatingCoef remains for explicit operator override (eg. API endpoint, manual reset). P1 (PR #378): Cover-load carve-out forces grid_target=0. The carve-out only set useEnergyPath=false; production wires PlanTarget alongside SlotDirective, so the !useEnergyPath branch called PlanTarget and then SetGridTarget(plannedImportW). A cover-load slot with 1.7 kW planned import then made PI try to import 1.7 kW — the opposite of the carve-out's purpose. Fix routes both carve-out predicates to SetGridTarget(0) and skips the legacy PlanTarget block. P2 (PR #375): passiveArbitrageIdleSlot predicate tightened. Was `dir.BatteryEnergyWh <= idleWhGate` which matched any negative-energy slot (planned discharge). Tightened to |BatteryEnergyWh| ≤ idleWhGate. The planned-discharge case folds into coverLoadDischargeSlot, which is now extended to planner_passive_arbitrage too (was arbitrage-only). The live-export gate fires on either predicate. P2 (PR #379): SlotDeliveryStats.SignMismatchCount. Planned -425 Wh vs actual +425 Wh would have scored |ratio|=1.0=on-target — the largest possible miss, invisible. New counter fires when planned and actual have opposite signs for non-idle slots; the magnitude over/under counters then only fire on same-sign cases, keeping their semantics clean. Tests: - TestSeedHeatingCoefDoesNotOverwriteLearnedValue - TestSeedHeatingCoefAppliesOnColdStart - TestPlannerArbitrageCoverLoadChasesGridZeroNotPlannedImport - TestCoverLoadDischargeDoesNotAbsorbLiveSurplus (both modes) - TestSlotMetricsDetectsSignMismatch Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Summary
planner_passive_arbitrageidle slots (DP pickedbattery_w ≈ 0) no longer absorb live PV surplus into the batterygrid_meter − Σ battery_w), not planned grid — for the slot we're already in, mätaren slår forecastBackground
Found 2026-05-28 on site .40: load forecast was 2782 W vs actual 504 W on a sunny afternoon (phantom heating term in the load model — separate fix follows). The planner picked idle on the forecast's net-≈0 balance, then the dispatcher fell through to
self_consumption + grid_target=0and charged 2.6 kW into batteries despite high current spot (160 öre), low future spot (95 öre), and abundant forecast PV in upcoming slots.The DP was right to pick idle (the math says hold off — future PV will refill cheap), but the EMS overrode the DP's intent. The gate makes the EMS respect "don't charge here" decisions even when live surplus diverges upward from forecast.
Same shape as
plannerSelfExportSurplusGate, but triggered on the live meter instead of the plan — passive_arbitrage's idle slots can quantize to "battery=0" with planned grid near zero (not export), so a plan-gated trigger would miss this case.Test plan
TestPlannerPassiveArbitrageIdleDoesNotAbsorbLiveSurplus(new failing test, now passes)go test ./...green (unit + e2e)make verify-all(cross-compile arm64/amd64/windows clean)🤖 Generated with Claude Code