Skip to content

fix(mpc): fit each worker call's time limit to its own deadline - #1968

Merged
frahlg merged 1 commit into
masterfrom
core-caution-call-time-limit
Nov 5, 2026
Merged

frahlg merged 1 commit into
masterfrom
core-caution-call-time-limit

Conversation

@frahlg

@frahlg frahlg commented Nov 5, 2026

Copy link
Copy Markdown
Member

Problem

When Core plans with forecast caution late in a slot, it makes a point call
and then a caution call to the worker. Both requests take time_limit_s from
energyplanBudget, which counts from the frozen execution start: for example
1.5 s with an active car. The caution call's own deadline comes from the time
left now (planWithForecastMargin: slot end minus the 50 ms publish margin).
After a point call that used its whole limit, the caution call can have about
950 ms before its deadline and still ask for 1.5 s. A worker that uses its full
time limit then answers after Core has given up on the call, so the call times
out and Core publishes the point plan instead of the caution plan. Under
optimizer protocol v1 a worker cannot learn Core's deadline, so Core has to
cap the limit.

Change

  • energyplanSolveTime(toDeadline) moves the existing reply margin
    (max(5 %, 30 ms)) and the existing no-call threshold (500 ms left in the
    slot, which is 450 ms before a call deadline) out of energyplanBudget.
    energyplanBudget returns the same values as before.
  • ExternalOptimizer.Optimize caps time_limit_s for a worker whose time
    Core budgets. The cap runs after request preparation, so it also covers the
    handshake and any budget reset there, and limits time_limit_s to
    energyplanSolveTime(time until the call's deadline). A call that still has
    its full budget keeps it unchanged. If less than the no-call threshold is
    left, Core does not start the worker and fails the call fast, as the
    frozen-start check already does. Core never sends a non-positive limit.

Overlap: #1966 also changes external_optimizer.go (buildRequest) and
resets time_limit_s in prepareEnergyplanRequest. The two branches merge
without conflicts, and because this cap runs after preparation it also bounds
that reset.

Tests

  • TestEnergyplanLateCautionCallLimitFitsItsDeadline: point call, then a late
    caution call through planWithForecastMargin. The point call keeps 1.5 s.
    The caution call must be ≤ remaining − margin. On master it fails with
    caution time_limit_s=1.5, want in (0, 0.9025]: the call has 950ms before its deadline.
  • TestEnergyplanLateCallLimitFollowsNoCallThreshold: with 300 ms left the
    worker is not started. With 600 ms left the limit is positive and fits the
    deadline. Both cases fail on master.
  • TestEnergyplanCallWithTimeLeftKeepsItsBudget: with no deadline, a frozen
    start near the slot end, or a 2 s caller deadline, the call keeps exactly
    the unchanged budget.
  • TestEnergyplanCapsWorkerBudgetAndWaitToRemaining now checks a range
    instead of exact equality. Its call loses a few microseconds between setting
    the deadline and sending, so the limit is now between the solve time left at
    sending and the frozen budget.

go test ./internal/mpc/... ./cmd/ftw/...: ok internal/mpc, ok cmd/ftw,
no failures. go vet ./internal/mpc/... is clean. The new tests also pass
with -race -count=5.

🤖 Generated with Claude Code

Core fixed time_limit_s from the budget at the frozen execution start.
With forecast caution, the caution call follows the point call and its
deadline counts from the time left in the slot now. After a point call
that used its whole limit, the caution call could ask for 1.5 s with
about 950 ms left before its deadline, so a worker that uses its full
time limit answered after Core had given up, and Core published the
point plan.

Each call's time_limit_s now fits the time before that call's deadline,
minus Core's reply margin, after request preparation. A call with its
full budget left keeps it. Below Core's no-call threshold the worker is
not started.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Fredrik Ahlgren <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-11-05T05:26:39.634116Z 627cadc 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.

@frahlg
frahlg merged commit 3e0a9cb into master Nov 5, 2026
23 checks passed
@frahlg frahlg mentioned this pull request Nov 6, 2026
6 tasks done
frahlg added a commit that referenced this pull request Nov 6, 2026
Bundle the worker from srcfl/energyplan 847936a (0.7.0, tag v0.7.0-beta.1)
for linux-arm64 and linux-amd64. The macOS worker is no longer bundled;
#1977 lets verify.py check such a bundle and skip execution on a Mac. The
worker lists demand_elapsed_hours, so Core (#1966) sends it the demand
hour in progress and the day's completed peak in that day's group, and
the native demand test now requires the feature.

The contract describes the feature and the demand-charge fields, each
call's time limit since #1968, the batteries' total recovery rule and
optional reply diagnostics, and it stays identical to Energyplan's copy.
The worker version pins and the README follow. Licenses and forecast
schemas are unchanged; the notices list two more microlp modifications.
Protocol v1 is unchanged.

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg deleted the core-caution-call-time-limit branch November 11, 2026 09:15
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