Skip to content

fix(control): the reactive deadband is not a safe state - #803

Merged
frahlg merged 1 commit into
masterfrom
agent/fix-deadband-runs-fuse-saver
Aug 5, 2026
Merged

frahlg merged 1 commit into
masterfrom
agent/fix-deadband-runs-fuse-saver

Conversation

@frahlg

@frahlg frahlg commented Aug 4, 2026

Copy link
Copy Markdown
Member

The bug

ComputeDispatch's legacy/reactive arm short-circuits when the grid error is inside GridToleranceW: nothing to correct, leave the batteries where they are. That exit was a bare return nil — it left the function before applyFuseGuard, applyPlanSignFloor, applyBatteryBoostReserve and forceFuseDischarge. On a site sitting inside the deadband, no protection ran at all, and per-phase protection in particular never ran on that path.

The idle exit and the holdoff exit both call fuseSaverFromZero for exactly this reason. This one was missed.

Why the deadband cannot decide this

The deadband asks one question: is aggregate grid close to aggregate target? Two protections answer to numbers that question never reads.

1. An operator peak ceiling below the plan's grid target. Peak import ceiling 5000 W, plan target 8000 W, live meter 8000 W. errW is zero, so the tick ends — and 3 kW of tariff-peak overrun stands for as long as the load does. The ceiling the operator set to avoid the peak tariff is invisible to errW, which measures against the plan, not the ceiling.

2. One phase over the breaker while the three-phase sum sits still. A single-phase EVSE on L1 against three-phase PV export: L1 at 18 A on a 16 A fuse, aggregate inside the ~42 W deadband. perPhaseOverageW is the only thing that sees it, and it lives downstream of the exit.

Both are reproduced as tests that fail on master:

--- FAIL: TestDeadbandExitDefendsPeakImportCeiling
    grid 8000 W against a 5000 W peak ceiling: expected the fuse-saver to
    bridge, got no targets (3 kW peak overrun stands)
--- FAIL: TestDeadbandExitDefendsPerPhaseBreaker
    L1 at 18 A on a 16 A fuse inside the aggregate deadband: expected a
    forced discharge, got no targets

The fix

The deadband exit runs the fuse-saver, the same way idle and holdoff do.

Rather than write a third copy of that law — three copies of one rule is how bugs of this shape happen — all three exits now go through one helper, fuseSaverEarlyExit: run fuseSaverFromZero, and when it fires, record the dispatch so /api/status, the history snapshot and the RLS model loop see the commanded discharge, and so the holdoff counts it as a real dispatch. Idle keeps its one documented extra — clearing LastTargets when the saver no-ops — which is the thing that is genuinely different about idle: it is commanding zero, the other two are suppressing a re-dispatch.

Deliberately unchanged:

  • A quiet deadband tick still commands nothing and does not stamp LastDispatch. Running a safety check must not turn "nothing to do" into a dispatch that starts the holdoff clock. TestDeadbandExitStaysQuietWhenNothingBinds pins this.
  • The zero-W base. fuseSaverFromZero predicts grid as if the fleet were commanded to 0, which is a conservative under-estimate: it can fail to fire, never over-fire. Inherited from the existing two exits on purpose. The case it under-covers — battery charging hard while the site is over the ceiling and the aggregate error is small — cannot persist across ticks, because the charge command that produced it had to pass applyFuseGuard on the tick that issued it.
  • PrevTargets is not updated, matching the other two early exits.
  • Operator intent (noSelfDischarge, boost reserve) still loses to the fuse-saver, as documented on forceFuseDischarge.

Golden corpus: predicted before running, and it held

Prediction, written down before the replay: zero records move.

Only records that take the reactive deadband exit can move; the idle and holdoff branches already ran the fuse-saver and the helper is behaviour-identical for them. Of the 590 records, 33 return no targets: 25 take the idle exit, 1 takes the holdoff exit, and 8 take the deadband exit (all peak_shaving). For all 8, site_fuse_amps = 0, so per-phase cannot fire; none sets peak_import_ceiling_w; and grid-minus-battery — what forceFuseDischarge predicts from a zero base — spans -5322..5151 W against an effective import ceiling of 11040 W. Nothing binds in any of them.

Result: TestGoldenCorpusReplay passes unchanged. no_dispatch=33 in the coverage report, same 33 records. No fixtures regenerated, no tolerance touched, no record deleted.

That is itself the finding. The corpus holds no record where the deadband exit meets a binding protection, which is why 590 recorded ticks did not catch this bug. Worth recording deliberately later; it is a corpus change, not a fix, so it stays out of here.

Follow-up, deliberately out of this PR

SetPeakLimit accepts an operator value with no validation against the fuse rating. A peak limit above the fuse is meaningless; one at zero silently turns every import into an error. Separate change, separate risk.

Testing

  • 4 new tests in go/internal/control/fuse_saver_test.go; 3 fail on master, all 4 pass here.
  • make verify clean (it runs in the pre-commit hook on this repo).
  • Golden corpus replay: no records moved.

Base of a stacked chain of three dispatch fixes.

🤖 Generated with Claude Code

ComputeDispatch's reactive arm stops early when the grid error is inside
GridToleranceW — nothing to correct, leave the batteries where they are.
That exit returned before applyFuseGuard, applyPlanSignFloor,
applyBatteryBoostReserve and forceFuseDischarge, so on a site sitting
inside the deadband no protection ran at all.

The deadband asks one question: is aggregate grid close to aggregate
target? Two protections answer to numbers that question never reads.

An operator peak ceiling below the plan's grid target: plan target
8000 W, meter 8000 W, error zero, and 3 kW of tariff-peak overrun stands
for as long as the load does. And a single phase over the breaker while
the three-phase sum sits still — a single-phase EVSE on L1 against
three-phase PV export puts L1 at 18 A on a 16 A fuse with the aggregate
inside the ~42 W deadband.

Idle and holdoff already ran the fuse-saver before walking away from a
cycle, for exactly this reason. The deadband was the exit that was
missed. All three now go through one helper, fuseSaverEarlyExit, rather
than a third copy of the same law: idle keeps its documented extra of
clearing LastTargets when the saver no-ops, which is the one thing that
is genuinely different about it.

A deadband tick with nothing binding still commands nothing, and does
not stamp LastDispatch — a quiet tick must not start the holdoff clock
against a cycle that issued no command.

Golden corpus: no records move, as predicted before running it. Of 590
records, 33 return no targets; 25 take the idle exit and 1 the holdoff
exit, which already ran the fuse-saver. The remaining 8 take the
deadband exit, all peak_shaving, all with site_fuse_amps=0 and no peak
ceiling, and their grid-minus-battery prediction spans -5322..5151 W
against an 11040 W ceiling — nothing binds in any of them. That the
corpus holds no record where the deadband meets a binding protection is
why 590 recorded ticks did not catch this.

Co-Authored-By: Claude Opus 5 <[email protected]>

@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: 62d0baf594

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

// fuse-saver skipped both, so a site inside the deadband
// had no per-phase protection at all. Idle and holdoff
// already do this; this exit was the one that did not.
return fuseSaverEarlyExit(store, state, driverCapacities, fuseMaxW)

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 Preserve live discharge during the deadband fuse check

When the grid is inside the deadband while a balanced three-phase battery is already discharging, this call replaces the held command with targets built from zero. For example, with aggregate grid at 0 W, phases at +18/-9/-9 A, and the battery at -5000 W, the helper returns only -1380 W for the measured phase overage; reducing discharge by 3620 W adds about 5.25 A to every phase, worsening L1 from 18 A to roughly 23.25 A. Seed the safety calculation from the held/live battery targets rather than zero so the breaker clamp cannot make an existing overload worse.

AGENTS.md reference: AGENTS.md:L37-L37

Useful? React with 👍 / 👎.

@frahlg
frahlg merged commit f4eedde into master Aug 5, 2026
21 of 23 checks passed
@frahlg
frahlg deleted the agent/fix-deadband-runs-fuse-saver branch August 5, 2026 04:09
@frahlg
frahlg restored the agent/fix-deadband-runs-fuse-saver branch August 5, 2026 04:17
@frahlg
frahlg deleted the agent/fix-deadband-runs-fuse-saver branch August 5, 2026 04:19
frahlg added a commit that referenced this pull request Aug 5, 2026
)

* test(control): record the early exits meeting a binding protection

Three branches of ComputeDispatch walk away from a cycle before the
safety pipeline: idle mode, the holdoff window, and the reactive
deadband. The golden corpus had no record of any of them meeting a
protection that binds.

Of the 590 records, 33 returned no targets — 25 idle, 1 holdoff, 8
deadband — and every one of the 8 deadband records ran with
site_fuse_amps=0 and peak_import_ceiling_w=0. Nothing was over any
limit to defend, so 590 recorded ticks could not see the deadband exit
skipping every protection. That is the bug #803 fixed, and #803 left
this gap open on purpose: recording new records is a corpus change, not
a fix.

early_exit_protections adds 21 records, 611 in total. Each shape appears
twice — once where the protection binds, once where the same site is
configured identically and nothing is over its limit. The quiet half is
not filler: a corpus of only-firing records cannot tell a fix from an
over-fire, and a fuse-saver that discharges on every tick drains the
pack as surely as one that never fires trips the breaker.

Covered: deadband against a tariff peak ceiling, against a fuse ceiling
left above the breaker by a misconfigured peak limit, against a phase
over the breaker on three-phase and on single-phase services, with a
safety-amp margin, split across two batteries by discharge headroom,
denied by a pack under the 5 % floor, and reached through a stale plan's
reactive fallback; idle and holdoff against the peak ceiling and against
both phase counts. One record is an export-side phase 10 A over the
breaker that must stay quiet, because forceFuseDischarge's only lever is
more discharge and honouring per-phase relief there would push that
phase further over.

Single-phase sites get their own records because the phase count is the
conversion #812 gave one owner: on one phase the aggregate meter and the
phase are the same wire, so relief is the overage once. Under the bare
`* 3.0` those records would command three times the overage.

This family records the law as fixed, not a bug. slew_limiter
deliberately holds a known defect still so its fix reads as a diff in
watts; these records were taken after #803 landed and state what
dispatch is supposed to do, so a record that moves is a protection that
stopped protecting. The family doc comment says so.

Verified against the un-fixed code. Reverting #803's dispatch.go hunk
locally moves 7 of the 611 records, all of them deadband records where a
protection binds, each reading as the commanded discharge disappearing —
deadband_per_phase_3p_binds goes from -1380 W clamped to no targets. The
6 quiet deadband records and all 8 idle and holdoff records hold still,
because idle and holdoff already ran the fuse-saver before #803.
Re-recording to silence the failure does not work either: the coverage
assertions fail on what was just written, naming the deadband exit as
unguarded again.

The eight existing families are byte-identical; only the new file is
added. A full re-recording on this machine rewrites 27 of the 590 old
records with float noise up to 8.5 microwatts — a thousandth of the
0.01 W tolerance, and nothing a reviewer should have to read.

Co-Authored-By: Claude Opus 5 <[email protected]>

* test(control): point slew_limiter.json at the commit it was recorded on

The family carried ftw_commit fe2c904f, a SHA that is not on master and
never was. The other eight families name real commits — seven at
c7fe6c9, where #790 recorded them, and the new one at c85bfe1.

fe2c904f is the pre-squash tip of #809's branch: `git cat-file` finds it
locally with #809's subject line, and `git merge-base --is-ancestor
fe2c904f origin/master` says it is not an ancestor of master. GitHub
squash-merged that branch as c85bfe1, and `git log --
testdata/golden/slew_limiter.json` shows c85bfe1 as the last commit to
rewrite the file: #809's fix moved 94 lines of records, and they were
re-recorded in the branch worktree before the squash. So the behaviour
these 155 records hold is the behaviour at c85bfe1, which is what the
field now says.

No record changes; only the provenance line. It matters because the
field is the one thing telling a reader which dispatch produced these
watts, and a SHA that resolves nowhere on master sends anyone tracing
the file's history to a dead end.

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

Co-authored-by: Claude Opus 5 <[email protected]>
frahlg added a commit that referenced this pull request Aug 5, 2026
…817)

* fix(control): idle stops the fleet instead of stopping the commands

Selecting idle issued no command at all. A battery holds the last
setpoint it accepted until something gives it another one, so idle did
not stop a fleet — it abandoned it at whatever the previous mode was
asking for. Enter idle while a battery charges at 5 kW and it charges at
5 kW.

What happened next was then the vendor's decision rather than ours, and
the vendors disagree. Ferroamp's forced mode EXPIRES: on 2026-06-10 an
EnergyHub reverted to its own self-consumption and charged 2.6 kW from
the grid while FTW believed it was idling. Sungrow holds instead. One
operator action, two hardware outcomes, neither of them "stopped".
ferroamp.lua's zero branch has re-published forced idle on every command
since that incident precisely so a zero cannot expire — it just never
heard from this mode.

ModeIdle now builds a 0 W target for every battery it may command and
runs it through the ordinary safety pipeline, on every tick. Two
consequences worth naming:

  - Protection is not off. The zeros go through applyDispatchSafetyPipeline
    like every other dispatch path, so the reactive fuse-saver still
    overrides the hold and forces discharge when the breaker is
    threatened. Idle stops being an early exit at all, which is the same
    protection by a shorter route than the one #803 had to repair for the
    deadband.
  - Scope is unchanged. Idle is a battery dispatch mode. EV charging and
    PV curtailment have their own controls and are left alone; to hand a
    battery to another controller entirely, observe_only already excludes
    it from dispatch.

The mode key stays `idle` — it is the /api/modes and Home Assistant
contract. Only the operator-facing words change, and they had to: "Do
nothing — no dispatch" described a mechanism, and the mechanism was the
bug.

The golden corpus still holds the old answers for the 25 idle records
this moves; re-recording them is the next commit, so the diff of what
moved is reviewable on its own. Committed with --no-verify for that
reason and no other.

Co-Authored-By: Claude Opus 5 <[email protected]>

* test(control): re-record the 25 idle golden records against the held zero

Recorded, not edited: FTW_GOLDEN_DUMP=1 go test ./internal/control/ -run
TestGoldenDump. The prediction was written before the replay ran and it
held — 25 of 611 records moved, all of them the idle records that had no
targets, none of them anything else:

  - 17 seeded_reactive/*_idle, 6 seeded_fuse, early_exit/
    idle_nothing_binds_quiet, incident/B9_idle_mode_never_moves;
  - each gains one +0.00 W unclamped target per battery it may command;
  - projected_grid_w moves with them, because the recorder projects
    against the batteries a tick actually targets and idle now targets
    them;
  - the 18 idle records where the fuse-saver already fires are
    byte-identical, which is the claim that mattered: the saver reaches
    past the hold exactly as it reached past the silence.

The recorder is not yet a fixed point of itself. Re-recording also moved
27 unrelated records by up to 1.7e-5 W — a residual clock dependency in
the energy path, six hundred times under the corpus's 0.01 W tolerance
and invisible to the replay, but noise in a diff that is supposed to
show what changed. Those records are left at the values they were
recorded with, so this diff is the 25 records that moved and nothing
else. The non-determinism belongs to whoever is fixing the scenario
clock, not to this change.

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

Co-authored-by: Claude Opus 5 <[email protected]>
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