Skip to content

fix(mpc): score remaining slot time in rain-check and Energyplan budget - #1172

Merged
frahlg merged 2 commits into
masterfrom
fix/planner-remaining-time-leftovers
Sep 8, 2026
Merged

frahlg merged 2 commits into
masterfrom
fix/planner-remaining-time-leftovers

Conversation

@frahlg

@frahlg frahlg commented Sep 8, 2026

Copy link
Copy Markdown
Member

#1130 plans only the remaining time in the current price slot. Two leftovers still used the full interval.

#1144 rain-check energy

forecastLoadWh multiplied LoadW by LenMin. After trimFirstExecutionSlot, first-slot LoadW is a remaining-interval average, so rain-check integrated it as a full 15 minutes.

It now uses DurationHours(), same as ValidatePlan. Test: first slot 10 minutes in, LenMin=15 — rain-check energy is 5 minutes of load, not 15.

#1148 Energyplan wall-clock budget

energyplanTimeBudget was model-size only (500ms vs 5s). A late-in-slot fleet replan could burn the full 5s, get slot_elapsed, and retry.

Cap TimeLimitS and the process wait by remaining first-slot time minus a 50ms publish margin. If remaining time is below the small-model budget, fail fast instead of solving a result that cannot be published.

Test: now = slot end − 200ms, fleet size that would get 5s — worker is not started, budget is 0, no slot_elapsed retry.

Scope

Closes #1144
Closes #1148


Note

Medium Risk
Changes replan timing and load forecast scaling in MPC; wrong remaining-time math could skew rain-check lifts or skip/cut solves near slot boundaries.

Overview
Aligns two MPC paths with partial first-slot planning (#1130): load rain-check energy and Energyplan solve time now use remaining slot wall-clock, not the full interval.

Load rain-check: forecastLoadWh integrates LoadW with DurationHours() (matching ValidatePlan) instead of LenMin, so a trimmed first slot is not counted as a full 15 minutes when comparing/scaling the day’s forecast.

Energyplan: Adds remainingFirstSlot and refactors energyplanTimeBudget to keep the existing 500ms / 5s model-size budget but cap it by remaining first-slot time minus a 50ms publish margin; returns 0 when less than the small budget remains. Optimize fails fast without starting the worker, and tightens the process context timeout to the same remaining window so late fleet replans do not burn a full 5s solve that would hit slot_elapsed and retry.

Tests cover budget math, worker TimeLimitS/deadline, fail-fast, rain-check Wh, and a late replan integration case that keeps the previous plan.

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

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 <fredrik@sourceful-labs.com>
@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-08T09:34:44.562980Z 3ad316b PR opened
ℹ️ 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.

@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_3a768bcc-202a-40ca-baa1-03377283b658)

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

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

if len(slots) == 0 {
return 0
}
rem := slots[0].StartMs + int64(slots[0].LenMin)*60000 - slots[0].ExecutionStart()

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 Base the solve deadline on the current wall clock

When input construction or a scheduler pause takes more than the 50 ms margin after trimFirstExecutionSlot captures ExecutionStartMs, this computes the original modeled duration rather than the time remaining when the worker actually starts. Consequently, both the fail-fast check and context.WithTimeout are stale by that delay, so a late fleet solve can run past the slot boundary, be discarded by firstSlotExpired, and trigger the retry this change is intended to avoid. Subtract the current time from the slot end at admission/deadline creation rather than subtracting the captured execution start.

Useful? React with 👍 / 👎.

@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_14dbf8bc-5aa6-400b-992d-728384e50b33)

@frahlg
frahlg merged commit 8e175ab into master Sep 8, 2026
19 checks passed
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.

fix(mpc): cap Energyplan time budget by remaining slot time fix(mpc): score remaining slot time in forecastLoadWh after #1130

1 participant