Skip to content

fix(planner): use remaining slot time and score active decisions - #1130

Merged
frahlg merged 3 commits into
masterfrom
fix/planner-partial-slot
Sep 8, 2026
Merged

frahlg merged 3 commits into
masterfrom
fix/planner-partial-slot

Conversation

@frahlg

@frahlg frahlg commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

A replan at 04:57:28.939 used live battery SoC but credited a full 04:45–05:00 interval. Its 1514 W charge represented 379 Wh in the model even though only 151 seconds remained, enough for 64 Wh at that power. Slot delivery metrics also compared the full measured quarter with the last replan, which could report a sign mismatch after an ordinary change of direction.

Add execution_start_ms inside the first price interval. Energyplan 0.3.1, Core DP, independent validation, baselines, shadows, diagnostic restore, battery/EV directives and the app protocol now use the same remaining time. Price and forecast identity remain tied to the original interval. Core rejects a plan whose first interval ends before publication and queues one fresh retry. Existing catch-up, SoC, fuse, stale-meter and direction protections remain in the control path; partial intervals retain the full interval's intent threshold.

Delivery accounting now combines the decisions observed during each part of the price interval. A replan changes only the remaining target. Logs identify this basis and count replans; gaps without measurements or a plan do not add energy to either side. Support reports compare measured energy with PlannedSoFarWh over those same intervals, so a future direction change cannot trigger a false historical shortfall. They describe elapsed-time and replan credits as budget accounting, separate from measured energy.

Paired private worker PR: https://github.com/srcfl/energyplan/pull/8, source 0ef352a5842c2827421a50f83ca446397527cae7. FTW contains only its compiled Linux ARM64/AMD64 and macOS ARM64 workers and public integration metadata.

Execution starts at the read of the latest battery state, before solving. Sensor age and physical changes during solving remain model latency; an expired first interval cannot publish.

Validation:

  • Energyplan make verify, including charge/discharge, multiple batteries and EV, invalid intervals and 0/7.5/12.5 minutes elapsed, the observed 151.061-second case and one second remaining.
  • FTW make verify and the real compiled-worker contract suite.
  • go test -race ./internal/mpc ./internal/control ./internal/appproto, plus the control/API race suites after the report fix.
  • Focused regressions cover the observed 04:57 energy/SoC values, fallback and both DP comparisons, diagnostic round trip, a slot ending during solve, weighted delivery across a direction change, short-slot charge intent and the protected 05:00 transition. A 30-second publication delay adds no measured energy; six subsequent seconds at 900 W add 1.5 Wh. Report tests cover future direction changes, a net-zero future budget and unobserved publication time.
  • CI exposed a race in two existing updater tests: terminal state precedes the sidecar callback, so waiting for done could read the callback count too soon. These behavior tests now finish runJob before checking the result. The updater race suite and 100 repeated ordering tests pass; updater production code is unchanged.

Physical verification for the combined beta, owned by the release coordinator:

  1. Confirm the installed Core commit/image and Energyplan 0.3.1 worker digest, current plan freshness and selected engine on the Pi.
  2. Capture accepted plan versions, execution starts, targets and measured battery power across at least three replans and price boundaries, including one late replan.
  3. Check first-slot energy and predicted SoC against remaining seconds, per-asset limits and live starting energy. Check that no expired candidate publishes and no late catch-up spike appears.
  4. Integrate actual and planned power over the same observed decision intervals. A direction change must not rewrite the earlier target. Verify normal dispatch recovery at the next boundary.

No merge, box installation or release performed by this task. Hardware validation remains pending.


Note

High Risk
Changes core planner publication, optimizer contract, and dispatch energy accounting—errors could mis-size battery commands or slot-delivery diagnostics on live sites.

Overview
Late replans were modeling and dispatching against a full price interval even when only seconds remained, which inflated energy/SoC and could spike catch-up power. This PR introduces execution_start_ms on the first slot so Core DP, Energyplan 0.3.1, validation, shadows, EV budgets, directives, and app plan projection all use the same remaining execution window while price slot identity stays on the original interval.

Publication rejects a solve whose first interval ends before install and triggers a slot_elapsed retry from live state. Dispatch seeds the execution budget from elapsed-time credit without counting that credit as measured energy; idle/charge/discharge intent still uses the full price-interval threshold via intentEnergyWh.

Slot delivery metrics and support reports now compare measured Wh to PlannedSoFarWh over the intervals each accepted decision actually covered, so a later direction change cannot rewrite earlier expectations or infer “reactive path” from budget accounting alone.

Updater self-replace tests wait for runJob to finish before asserting sidecar replacement, fixing a race where done could be read before the callback ran.

Reviewed by Cursor Bugbot for commit 918cac5. Bugbot is set up for automated code reviews on this repo. Configure here.

@frahlg
frahlg marked this pull request as ready for review September 8, 2026 07:18
@frahlg
frahlg merged commit 6eadec3 into master Sep 8, 2026
14 checks passed
@cursor

cursor Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_61125591-78a5-4155-8e61-752f9cd2fb1d)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-08T07:25:22.279236Z 918cac5 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

frahlg added a commit that referenced this pull request Sep 8, 2026
…et (#1172)

The load rain-check still integrated the first price slot as LenMin after
#1130 trimmed it to remaining time. Energyplan's worker budget was
model-size only, so a late-in-slot fleet replan could spend 5s on a
result that firstSlotExpired then discarded.

Use DurationHours() for rain-check energy. Cap TimeLimitS and the
process wait by remaining first-slot time minus a short publish margin,
and fail fast when remaining time is below the small-model budget.

Closes #1144
Closes #1148

Signed-off-by: Fredrik Ahlgren <[email protected]>
@frahlg
frahlg deleted the fix/planner-partial-slot branch November 11, 2026 09:21
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