Skip to content

feat(diagnostics): per-slot Wh delivery tracking for reactive dispatch paths - #379

Merged
frahlg merged 1 commit into
masterfrom
slot-delivery-observability
May 28, 2026
Merged

frahlg merged 1 commit into
masterfrom
slot-delivery-observability

Conversation

@frahlg

@frahlg frahlg commented May 28, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds a path-agnostic per-slot Wh accumulator that runs on every dispatch tick, regardless of execution path.
  • At slot rollover the accumulator scores actual fleet delivery against the plan's BatteryEnergyWh: ratio > 1.5 → SlotDeliveryStats.OverDeliveryCount++ + slog.Info log line; ratio < 0.5 → UnderDeliveryCount++ + log line.
  • Counters surface on GET /api/status under "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_arbitrage discharge slots with PlannedGridW > -100 W AND BatteryEnergyWh < -50 Wh fall 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 existing slotDelivered accumulator:

Without 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

  • No Wh cap is added. Pure observability. No dispatch decision reads SlotDeliveryStats; reactive paths still chase grid=0 unconstrained by plan magnitude.
  • Idle slots (|planned| ≤ 50 Wh) are intentionally skipped — over/under against ~0 is meaningless.
  • The energy-allocation bookkeeping (slotDelivered / lastTickTs / currentDirective) is untouched. The new accumulator runs alongside it on its own fields (slotActualWh, slotActualLastTs, slotActualSlotStart, slotActualPlannedWh).

Implementation notes

  • New helper updateSlotDeliveryMetrics(state, currentTotalW, now) invoked at the very top of ComputeDispatch, before the manual-hold / idle / charge / holdoff short-circuits so the tick is counted even when those paths return early.
  • dt capped at 300 s to handle long pauses (mirrors the existing energy-path guard at dispatch.go:1133).
  • Slot rollover detected by dir.SlotStart != state.slotActualSlotStart; the just-ended slot is evaluated using state.slotActualPlannedWh (cached on every tick so reactive paths that never populate state.currentDirective still have a planned magnitude to compare against).
  • First-ever tick (zero-value slotActualSlotStart) only anchors — no log emission.

Test plan

  • TestSlotMetricsAccumulatesActualWhAcrossTicks — Wh integrates across ticks at -1000 W battery.
  • TestSlotMetricsResetsOnSlotRollover — accumulator resets to 0 when SlotStart advances.
  • TestSlotMetricsLogsOverDeliveryAtSlotEnd — planned -425, actual -850 (ratio 2.0) bumps OverDeliveryCount.
  • TestSlotMetricsLogsUnderDelivery — planned -425, actual -100 (ratio 0.24) bumps UnderDeliveryCount.
  • TestSlotMetricsIgnoresIdleSlots — planned -30 Wh (below 50 Wh cutoff) never bumps counters.
  • TestSlotMetricsCounterSurvivesMultipleSlots — 3 sequential over-delivery rollovers → counter = 3.
  • make verify passes locally (vet + full test suite + build).

🤖 Generated with Claude Code

…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]>
@frahlg
frahlg merged commit c1cbda7 into master May 28, 2026
2 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

frahlg added a commit that referenced this pull request May 28, 2026
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]>
@frahlg
frahlg deleted the slot-delivery-observability branch August 7, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant