Skip to content

fix(control): a battery ends fuse relief only as far as the phase can take it - #1847

Merged
frahlg merged 3 commits into
masterfrom
fix/battery-relief-own-raise
Oct 25, 2026
Merged

frahlg merged 3 commits into
masterfrom
fix/battery-relief-own-raise

Conversation

@frahlg

@frahlg frahlg commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Cause

The field matrix's seven own-raise relief rows are all #1763's site: 3 × 25 A, a battery and a car capped at 8.3 kW. Each runs with Charge now or a pause, under every charger timing. They fail the same way. Traced on the easee120s pause row:

  • At 22:11:00 a 6 A kettle comes on L1 and the battery relieves at −3,105 W.
  • At 22:11:05 the meter shows L1 at 19.53 A. Core ends the relief in one step, to 0 W, while the car's order rises from 14 to 15 A.
  • L1 then reads 24.03 A against the 24 A working limit for more than 3 s. That is our own raise.

capBatteryCharge counted an ending discharge against room only to cut positive charge. It never held back the discharge itself.

Change

clampEVBatteryHeadroom / capBatteryCharge (go/internal/control/ev_battery_headroom.go):

  • Ending a discharge is our raise. The battery keeps the part that the measured room cannot take, less our raises the meter may not show yet. Those are the battery orders of the last evSettle, as positive charge already counts them, and the car's order above its lowest recent offer.
  • What the car still wants does not hold the battery's discharge. That would make the battery pay for the car's charge.
  • A battery never goes below its command bounds (fuseTargetBounds). One at its SoC floor or without a reading keeps nothing.

Deleted: the seven own-raise relief rows and the unused relief label in testdata/sitesim_field_known.txt. No code was deleted.

Tests

Check master f50b5ff this branch
TestEVBatteryEndingReliefKeepsWhatThePhaseCannotTake: L1 19.53 A, relief −3,105 W, the car 1 A up battery 0 W (fails) keeps −711 W; L1 stays at 24 A
… the phase takes the release and some charge 2,415 W same
… a battery at its SoC floor 0 W same
Field matrix 342 cases, 30 fail (idle 23, own-raise 7) 23 fail (idle 23)
TestSiteSimRandomHouse (5 seeds) passes passes, same numbers per seed
TestSiteSimScenarios passes passes

One existing test changed timing: TestFuseOverageGetsBatteryReliefUntilEVCutShows. On master the battery let go of all its relief within 3 ticks after the car's cut showed.

  • What happens now. It lets go of 3,539 W at once, as the room allows, then keeps 1,251 W for evSettle (20 s), because the first part still counts as on its way. It is idle 26 s after the step instead of about 8 s.
  • What holds. The meter stays under 16,560 W (24 A) and never exports.
  • The test. It now checks 31 s after the step and says why.
  • The trade-off. A battery that answers within a tick holds relief up to 20 s longer than it needs to. A battery that answers in seconds, as in the field rows, no longer lands two releases on top of each other. Without the battery's own unshown orders in the count, the relief rows fail again.

go vet passes, and so does go test -p 2 ./internal/control/ ./internal/loadpoint/ ./cmd/ftw/. I have not run make verify.

Independent review (Sonnet) and the fix it led to

  • Bug, fixed in 4ebaf24. Before Core's first battery order, which happens after every start, restart or update, the charge cap counts the battery as possibly discharging at its cap. The first version turned that guess into a real discharge order. In the matrix battery rows the site exported −5.6 kW against a +6 kW plan for about 20 s; the restart rows went to −10 kW. It also ignored MaxExportW. The matrix stayed green because export is not one of its invariants. Now only a discharge we ordered can be kept. TestEVBatteryWithNoOrderOfOursKeepsNoDischarge gives −2,723 W on c1e7198 and no discharge now. Plan sign-mismatch warnings in the matrix are back to master's 8.
  • The changed test is honest. The battery keeps 1,251 W of relief for 20 s and settles at 26 s instead of about 9 s. That leaves about 1.3 kW of room unused for 20 s, roughly 7 Wh. Before the cut shows, the meter now holds 16,560 W instead of overshooting to 17,134 W.
  • The car's same-tick rise into relief room could not be broken in 384 sweep cases with a car that takes the rise: riseW is counted in the release room.
  • Open, on master too: a pause records no EV demand, so clampEVBatteryHeadroom returns early. With a slow charger still drawing, a battery charge raise is then unchecked. In a sweep with the car at 10.35–11.04 kW, the pause rows still fail own-raise on cloud chargers, on master and here.

Not in this PR

  • The car's rise in the same tick spent room that only the relief made. In these rows the car was capped at 8.3 kW, so the rise drew nothing. A car that can take the rise is not covered here.
  • Relief release without a plugged car's demand. clampEVBatteryHeadroom returns early then, as on master.

Hardware

Not run on real hardware. Evidence is still needed: a site where the battery relieves a phase while the car charges.

🤖 Generated with Claude Code

… take it

Field matrix relief rows (#1763's site, 25 A, a kettle on L1): the battery relieved at -3,105 W, and once the meter showed room Core ended the relief in one step while the car's order rose a step. L1 read 24.03 A against the 24 A working limit for more than 3 s: our own raise.

Ending a discharge is our raise. The battery keeps the part of it that the measured room cannot take, less our raises the meter may not show yet (the battery's own for evSettle and the car's order above its lowest recent offer), never below its command bounds. What the car still wants does not hold the battery's discharge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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-10-25T13:59:09.452565Z c1e7198 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.

@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: c1e7198464

ℹ️ 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 on lines +145 to +146
if last := latest[target.Driver]; last < 0 {
keep[i] = math.Max(0, math.Min(0, target.TargetW)-math.Max(last, lowest[target.Driver]))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restore discharge only when a prior command actually exists

After startup, an idle battery with no command history can be commanded to discharge here: clampEVBatteryHeadroom substitutes the negative discharge cap for a missing command, and this loop treats that conservative reservation as discharge to preserve. For a 3×25 A site with an active EV, zero pending raises, a 5 kW battery discharge cap, measured export of 9 kW and a worst phase of 23 A, the new clamp changes a validated 0 W target to −4,310 W. An idle battery obeying that command raises export to 13,310 W, exceeding a configured 10 kW export ceiling; the export guard already ran earlier in applyDispatchSafetyPipeline. Keep the missing-history reservation for optional-charge budgeting, but require an actual prior discharge command before restoring negative power. This avoids issuing a clamp command without the quantified risk required by AGENTS.md.

AGENTS.md reference: AGENTS.md:L68-L68

Useful? React with 👍 / 👎.

frahlg and others added 2 commits October 25, 2026 15:22
Review of the relief fix: before Core's first battery order, after
every start, the charge cap counts the battery as possibly discharging
at its cap. keepEndingDischarge turned that pessimistic guess into a
real discharge order: in the matrix battery rows the site exported
-5.6 kW against a +6 kW plan for about 20 s, past MaxExportW. Only a
discharge we ordered can now be kept; a battery with no order of ours
keeps nothing. Plan sign-mismatch warnings in the matrix are back to
master's 8, and the relief rows still pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frahlg
frahlg merged commit e5c7483 into master Oct 25, 2026
22 checks passed
frahlg added a commit that referenced this pull request Oct 28, 2026
…1918)

* test(control): port #1816's battery relief reproductions to master

Two reproductions from #1816, adapted to master's
capBatteryCharge and clampEVBatteryHeadroom:

- Pending rise: 25 A site, 1 A margin, phases 22.3/12.3/12.3 A. The
  battery measures -2000 W and our newer -1000 W order may not show yet.
  Ending the relief must keep -827 W so L1 lands on 24 A, not 25.2 A.
- Shared relief: two batteries end -1000 W and -2000 W of relief. When
  the first cannot keep its share (SoC floor or a 300 W bound), the
  second keeps the missing 1827 W instead of the old 1218 W.

Master's keepEndingDischarge (#1847) passes both. Dropping it, ignoring
pending rises, or sharing before bounds makes them fail. Import only:
master leaves the export direction to the fuse pipeline.

Co-authored-by: segran2 <268038031+segran2@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(control): say which relief gaps master leaves open

The header said master leaves the export case to the fuse pipeline.
Nothing on master holds it. Name both no-car gaps instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: segran2 <268038031+segran2@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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