Repository navigation
feat(diagnostics): per-slot Wh delivery tracking for reactive dispatch paths - #379
Conversation
…h paths Adds a path-agnostic per-slot Wh accumulator that runs on EVERY dispatch tick — including the reactive paths the existing slotDelivered bookkeeping doesn't track: * planner_self (idle and participating slots) * planner_passive_arbitrage idle / non-charge slots * the planner_arbitrage cover-load carve-out from PR #378 (PlannedGridW > -100 W AND BatteryEnergyWh < -50 Wh) * manual modes, plain self_consumption At slot rollover the actual fleet delivery is scored against the plan's BatteryEnergyWh; ratios > 1.5 log an over-delivery and bump SlotDeliveryStats.OverDeliveryCount; ratios < 0.5 log under-delivery and bump UnderDeliveryCount. Idle slots (|planned| <= 50 Wh) are skipped — ratio against ~0 is meaningless. Counters surface on /api/status under "slot_delivery_stats". Pure observability: no dispatch decision reads the counters and no hard Wh cap is applied to reactive paths. The point is to measure first so we have data before deciding whether enforcement is needed. 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: d5df6d5c24
ℹ️ 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".
| // so this accumulator keeps its own slotActualPlannedWh. | ||
| plannedWh := state.slotActualPlannedWh | ||
| if math.Abs(plannedWh) > slotDeliveryIdleWhCutoff { | ||
| ratio := math.Abs(state.slotActualWh) / math.Max(math.Abs(plannedWh), 1) |
There was a problem hiding this comment.
Treat opposite-sign slot delivery as divergence
When a slot ends with the fleet moving energy in the opposite direction but a similar magnitude, this absolute-value ratio reports it as on target; for example, a planned -425 Wh discharge and actual +425 Wh charge produces ratio == 1.0, so neither counter nor log fires. Because BatteryEnergyWh and slotActualWh are site-signed, this hides one of the largest plan-vs-reality failures from /api/status; handle opposite signs explicitly before applying the magnitude thresholds.
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
BatteryEnergyWh: ratio > 1.5 →SlotDeliveryStats.OverDeliveryCount+++slog.Infolog line; ratio < 0.5 →UnderDeliveryCount+++ log line.GET /api/statusunder"slot_delivery_stats"so operators can spot systemic forecast-vs-reality drift.Why now
PR #378 (commit
adf3f86) added a cover-load carve-out:planner_arbitragedischarge slots withPlannedGridW > -100 WANDBatteryEnergyWh < -50 Whfall through from the energy-allocation path to reactive PI-on-grid=0. There are now multiple execution paths where the plan's per-slot Wh budget is not strictly tracked by the existingslotDeliveredaccumulator:planner_passive_arbitrageidle / non-charge carve-outplanner_selfpath (always reactive)self_consumptionWithout an independent accumulator we have no data on whether those paths over- or under-deliver vs the DP's plan. This PR adds that data-gathering layer so we can decide later whether a hard Wh cap is warranted.
What is NOT changing
SlotDeliveryStats; reactive paths still chase grid=0 unconstrained by plan magnitude.|planned| ≤ 50 Wh) are intentionally skipped — over/under against ~0 is meaningless.slotDelivered/lastTickTs/currentDirective) is untouched. The new accumulator runs alongside it on its own fields (slotActualWh,slotActualLastTs,slotActualSlotStart,slotActualPlannedWh).Implementation notes
updateSlotDeliveryMetrics(state, currentTotalW, now)invoked at the very top ofComputeDispatch, before the manual-hold / idle / charge / holdoff short-circuits so the tick is counted even when those paths return early.dtcapped at 300 s to handle long pauses (mirrors the existing energy-path guard atdispatch.go:1133).dir.SlotStart != state.slotActualSlotStart; the just-ended slot is evaluated usingstate.slotActualPlannedWh(cached on every tick so reactive paths that never populatestate.currentDirectivestill have a planned magnitude to compare against).slotActualSlotStart) only anchors — no log emission.Test plan
TestSlotMetricsAccumulatesActualWhAcrossTicks— Wh integrates across ticks at -1000 W battery.TestSlotMetricsResetsOnSlotRollover— accumulator resets to 0 whenSlotStartadvances.TestSlotMetricsLogsOverDeliveryAtSlotEnd— planned -425, actual -850 (ratio 2.0) bumpsOverDeliveryCount.TestSlotMetricsLogsUnderDelivery— planned -425, actual -100 (ratio 0.24) bumpsUnderDeliveryCount.TestSlotMetricsIgnoresIdleSlots— planned -30 Wh (below 50 Wh cutoff) never bumps counters.TestSlotMetricsCounterSurvivesMultipleSlots— 3 sequential over-delivery rollovers → counter = 3.make verifypasses locally (vet + full test suite + build).🤖 Generated with Claude Code