Skip to content

fix(control): reset energy-path bookkeeping inside reactive carve-outs - #386

Merged
frahlg merged 1 commit into
masterfrom
harden-carve-out-bookkeeping-reset
May 28, 2026
Merged

frahlg merged 1 commit into
masterfrom
harden-carve-out-bookkeeping-reset

Conversation

@frahlg

@frahlg frahlg commented May 28, 2026

Copy link
Copy Markdown
Member

Summary

Defence-in-depth follow-up to #383 / #378. The carve-out gate added in #383 calls SetGridTarget(0) but does not call resetEnergyDispatchBookkeeping. preparePlannerSelf does both (dispatch.go:797-804) for the same reason: when the slot was previously on the energy-allocation path — directly or via an operator mode-hop earlier in the same 15-min window — slotDelivered / lastTickTs / currentDirective hold stale values. A subsequent transition back to the energy path within the same slot would then read those and miscompute remainingWh.

Symmetric with the comment at dispatch.go:798-803:

"Reset the energy-allocation bookkeeping so a future switch to planner_cheap / planner_arbitrage within the same 15-minute slot can't read stale slotDelivered accumulated before the operator hopped through planner_self."

Adding the reset to the carve-out branch gives planner_arbitrage cover-load and planner_passive_arbitrage idle the same guarantee.

What does NOT change

  • Steady-state cover-load reactive behaviour is unchanged.
  • No PI.Reset call — the existing pattern in preparePlannerSelf doesn't reset the PI either, and resetting per tick would prevent the integral from doing its job.

Test plan

  • TestPlannerArbitrageCoverLoadResetsEnergyPathBookkeeping — RED before fix (slotDelivered=-200 / non-zero ts / non-zero directive), GREEN after.
  • TestPlannerPassiveArbitrageIdleResetsEnergyPathBookkeeping — same for passive idle.
  • Existing carve-out tests still pass.
  • `make verify` clean.

🤖 Generated with Claude Code

Mirror what preparePlannerSelf already does (dispatch.go:797-804): when
the planner_arbitrage cover-load or planner_passive_arbitrage idle carve-
out fires, call resetEnergyDispatchBookkeeping alongside the existing
SetGridTarget(0). Otherwise slotDelivered / lastTickTs / currentDirective
keep values left over from a previous energy-path tick in the same slot,
and a future mid-slot transition back to the energy path (plan refine,
mode hop) miscomputes remainingWh.

Pure defence-in-depth — no change in steady-state cover-load reactive
behaviour. Same forward-transition risk preparePlannerSelf has guarded
against since PR #131.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@frahlg
frahlg force-pushed the harden-carve-out-bookkeeping-reset branch from 3ba074e to 8e0ec44 Compare May 28, 2026 07:41

@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: 3ba074e04c

ℹ️ 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".

Comment thread go/internal/control/dispatch.go Outdated
// within the same slot would then read those and
// miscompute remainingWh. Reset on every carve-out
// tick (idempotent).
resetEnergyDispatchBookkeeping(state)

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 Preserve delivered Wh when leaving carve-outs

When a carve-out fires after this slot has already run on the energy path, this unconditional reset discards the valid slotDelivered accumulated earlier in the same SlotStart. If a later mid-slot replan flips the same slot back to the energy path, the rollover check at currentDirective.SlotStart treats it as a brand-new slot and remainingWh is computed from the full directive budget, ignoring energy already delivered before the carve-out; that can over-discharge/over-charge and bypass the existing same-slot rebase protections for plan changes.

Useful? React with 👍 / 👎.

@frahlg
frahlg merged commit 0779ff2 into master May 28, 2026
2 checks passed
@frahlg
frahlg deleted the harden-carve-out-bookkeeping-reset branch May 28, 2026 07:43
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