Skip to content

fix(control): size per-phase relief by the site's phase count - #808

Closed
frahlg wants to merge 1 commit into
agent/fix-deadband-runs-fuse-saverfrom
agent/fix-per-phase-relief-phase-count
Closed

fix(control): size per-phase relief by the site's phase count#808
frahlg wants to merge 1 commit into
agent/fix-deadband-runs-fuse-saverfrom
agent/fix-per-phase-relief-phase-count

Conversation

@frahlg

@frahlg frahlg commented Aug 4, 2026

Copy link
Copy Markdown
Member

Stacked on #803 (agent/fix-deadband-runs-fuse-saver) — it touches the same file. Review the top commit; the base PR carries the rest.

The bug

perPhaseOverageW(...) * 3.0 was written twice in dispatch.go — once in applyFuseGuard, once in forceFuseDischarge — while SiteFusePhases sat in the same State struct and fuseSafetyMarginW, two functions away, read it correctly and says so in its own comment: "No hardcoded 230 V / 3 phases here — both come from config."

A worst-phase overage is watts on one phase. Dispatch commands aggregate battery watts. The conversion between the two is the site's phase count: a battery spreads its action across the phases, so each aggregate watt relieves 1/N on the worst phase and an overage of N watts needs N × phases of action. Writing that as 3.0 is only right on a three-phase site.

On a 1-phase site the aggregate meter and the single phase are the same wire, so the two numbers must agree — and they didn't:

1Φ site, 16 A fuse, 0.5 A margin, meter at 5520 W (24 A) relief demanded resulting meter
correct (× 1 phase) 1955 W 3565 W import — exactly the ceiling
before (× 3) 5865 W 345 W export — through zero, wrong side of the same breaker

perPhaseOverageW had two more of the same flavour. It walked L1/L2/L3 unconditionally, so current reported on a phase a 1Φ site does not own fired the clamp on a site nowhere near its fuse. And it fell back to a hardcoded 230 for voltage — the exact thing fuseSafetyMarginW refuses to do.

The fix

One helper, perPhaseReliefW, owns the conversion, and both call sites use it.

  • Phase count comes from SiteFusePhases.
  • Voltage comes from SiteFuseVoltage, the way every other watt in this file gets it (fuseSafetyMarginW, Fuse.MaxPowerW, main.go's fuseMaxW). No invented 230 V.
  • Only the configured phases are read, in L1/L2/L3 order — the same rule sitePhaseCurrentsAt already follows in cmd/ftw/site_dispatch_safety.go.
  • A fuse described by amps alone now gets no per-phase clamp instead of one computed from numbers nobody configured. config.go defaults fuse.voltage to 230 and fuse.phases to 3 and rejects <= 0 in validation, and control_state.go copies all three fields, so no configured site loses protection — only harnesses that wire SiteFuseAmps alone.

Was the import-only gate intentional or drift?

Intentional, and it stays. The two call sites genuinely need different behaviour:

  • forceFuseDischarge has exactly one lever: command more discharge. That relieves an import-side phase and worsens an export-side one. Honouring a direction-agnostic overage there would push the over-current phase further over the breaker.
  • applyFuseGuard has both levers — shrink charge, shrink discharge — so it uses relief in both directions and attributes it by the live aggregate sign.

The evidence: the gate arrived with the PR #219 review, its comment says so, and TestForceFuseDischargeIgnoresExportSidePerPhase has pinned it since. So the answer to "one of these two is wrong" is that the duplicated * 3.0 was the wrong half, not the gate. What was wrong about the gate is that it was the only thing distinguishing the sites while the conversion was silently duplicated. The direction limit now sits at the call that needs it; the conversion is stated once.

Tests

fuse_saver_test.go pinned 3-phase behaviour only. New siblings, all four of which fail against the old code:

  • TestPerPhaseReliefMatchesSinglePhaseSiteOnImport — 1Φ site, idle battery: forced discharge equals the overage once, and the post-dispatch meter stays on the import side of zero.
  • TestPerPhaseReliefMatchesSinglePhaseSiteOnExport — the export direction forceFuseDischarge deliberately skips and applyFuseGuard owns: same law, one phase count. Under × 3 the discharge is zeroed and the site swings to 520 W of import.
  • TestPerPhaseClampReadsConfiguredPhasesOnly — a 1Φ site with L1 at 10 A ignores 24 A reported on phases it does not have.
  • TestPerPhaseClampOffWhenFuseDescriptionIncomplete — amps without a voltage or a phase count clamp nothing.
  • TestPerPhaseClampUsesConfiguredVoltage — a 240 V site converts at 240 V (2560 W, not the 2620 W a hardcoded 230 V gives).

setupPerPhase now states the site it builds (3Φ, 230 V) and delegates to setupPerPhaseSite, which takes both.

Golden corpus

Predicted before looking: zero records move. The corpus was recorded on 3-phase sites, so × SiteFusePhases is arithmetically × 3.0 there, the walk covers the same three phases, and the configured voltage is the same 230 V the fallback invented.

Verified after: TestGoldenCorpusReplay passes unchanged at the 0.01 W tolerance, all 590 records, and TestGoldenCorpusCoverage still reports per_phase=33 — the same records exercise the path, none of them silently switched off. Every record in the corpus that carries phase amps is site_fuse_voltage=230, site_fuse_phases=3; the 33 with a non-zero per-phase overage are all in that set.

make verify green (pre-commit gate).

A worst-phase overage is watts on one phase; dispatch commands
aggregate battery watts. The conversion between them is the site's
phase count, and it was written twice as a bare `* 3.0` — once in
applyFuseGuard, once in forceFuseDischarge — while SiteFusePhases sat
in the same struct and fuseSafetyMarginW read it correctly two
functions away.

On a 1-phase site the aggregate meter and the single phase are the
same wire, so relief is the overage once. Demanding three times it
overshoots the ceiling and can push the meter through zero into a
violation on the other side.

One helper now owns the conversion. perPhaseOverageW reads only the
configured phases, in L1/L2/L3 order, the way sitePhaseCurrentsAt
already does in cmd/ftw, and converts amps to watts through the
configured voltage only — no invented 230 V, the same law
fuseSafetyMarginW states in its own comment. A fuse described by amps
alone gets no per-phase clamp instead of one computed from numbers
nobody configured; config fills both fields (230 V / 3 phases by
default, validation rejects <= 0), so only harnesses that wire
SiteFuseAmps alone see that.

forceFuseDischarge keeps its import-only gate. That is deliberate,
not drift: its single lever is more discharge, which relieves import
and worsens an export-side phase trip, and applyFuseGuard's export
branch already shrinks discharge for that case. The limit now sits at
the call that needs it rather than inside a second copy of the
conversion.

Three-phase 230 V behaviour is arithmetically identical: the golden
corpus replays unmoved, with the same 33 records exercising the
per-phase path.

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

frahlg commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

Superseded by #812. GitHub closed this PR automatically when #803 merged and its base branch agent/fix-deadband-runs-fuse-saver was deleted; a reopen is blocked because the head branch was force-pushed during the rebase. #812 carries the same commit, rebased onto master, with a diff byte-identical to this one.

@frahlg
frahlg deleted the agent/fix-per-phase-relief-phase-count branch August 5, 2026 04:22
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