Repository navigation
feat(mpc): tighter replan triggers + twin-driven replan signal - #380
Conversation
There was a problem hiding this comment.
💡 Codex Review
https://github.com/frahlg/forty-two-watts/blob/13420b50c124ba0c4fd9965c0f942a58255052a8/go/internal/mpc/service.go#L471
Start reactive ticker for twin-only replans
When both integral divergence thresholds are set to 0 to disable the legacy PV/load energy triggers, this condition leaves reactiveTick nil, so the select loop never reaches checkTwinDrift even if TwinDriftPVW or TwinDriftLoadW is configured. That makes the new exported twin-drift trigger silently inactive in the documented “0 disables” configuration for the old triggers; include the twin-drift thresholds in the ticker-enable condition.
ℹ️ 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".
Lower the reactive replan thresholds and add a third trigger so the planner reacts to twin drift well before the integral-of-error path crosses its window. Why: - PR #378's cover-load reactive carve-out pulled more decisions out of the plan and into the dispatch layer. The plan goes stale during those windows even though the immediate gap is covered. The fix is to replan more readily, not to hold the plan longer. - The PV / load twins already self-correct every cycle via RLS, but the planner only consumed their output every 15 min. Twin RMSE is a cleaner staleness signal than the energy integral — the integral fades briefly to zero when the forecast and reality both swing, while the twin's prediction has by then moved permanently. What changes: - LoadDivergenceWh default 400 → 200 - PVDivergenceWh default 500 → 250 - Reactive integral half-life 15 min → 8 min (480 s) - MinReplanGap default 60 s → 30 s - New TwinDriftPVW (250 W RMSE), TwinDriftLoadW (200 W RMSE), TwinDriftHorizonSlots (16 ≈ 4 h @15-min) — replan when the twin's next-N-slot prediction has RMSE'd past the threshold from the snapshot the active plan was built on. Subject to the same MinReplanGap cooldown. No-op when predictors aren't wired. - replan() now snapshots the planned PV + load predictions per slot so the twin-drift detector has a stable comparison baseline. Why replanning more freely is safe: a full DP solve is ~100 ms on a Pi 4 (51 SoC × 21 action × 193 slots, sub-1 % CPU). Being stingy was the wrong default — the cost is in the noise. What does NOT change: DP grid resolution, terminal-credit math, plan- stability protections (SlotStart anchoring, MaxPlanAge gate, cooldown between reactive replans). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
13420b5 to
d6df009
Compare
Summary
half-life 15 → 8 min, cooldown 60 → 30 s.
checkTwinDrift— that fires when the PV orload twin's CURRENT prediction has RMSE'd past 250 W (PV) / 200 W
(load) from the prediction the active plan was built on, across the
next 16 slots (~4 h @ 15-min).
replan()now snapshots the planned PV + load predictions per slotso the twin-drift detector has a stable comparison baseline. The
snapshot is built under the same
s.mulock ass.last.Motivation
PR #378 added a cover-load reactive carve-out in the dispatch layer. That
moved more decisions out of the plan and into the reactive paths, which
exposed how lazily the plan itself was refreshing — the integral-of-error
path can sit just under the threshold while the twins (RLS, self-correcting
every cycle) have already moved their forecast meaningfully.
The new twin-drift signal closes that gap: it's a direct read on "the
inputs we built the plan from have changed", not a derived signal that
fades back toward zero when reality swings symmetrically.
Why being aggressive about replanning is safe: a full DP solve on a Pi 4
is ~100 ms (51 SoC × 21 action × 193 slots, sub-1 % CPU). Sitting on a
stale plan for kWh-scale arbitrage value to recover the ~100 ms is the
wrong trade.
What does NOT change
upperHalfMeanPrice,selfConsumptionTerminalPrice).MaxPlanAgegate,SlotStartanchoring,cooldown between reactive replans (still enforced, just shorter).
PVPredictor,LoadPredictor) — unchanged.Test plan
TestReactiveReplanFiresAtTighterLoadThreshold— exercises 1.5 kWsustained load drift hitting the new 200 Wh threshold within minutes.
TestReactiveReplanFiresAtTighterPVThreshold— symmetric PV caseagainst the new 250 Wh threshold.
TestReactiveReplanRespectsShorterCooldown— 30 s cooldown stillsuppresses back-to-back replans.
TestTwinDriftReplanFiresOnLargePVShift— RMSE 500 W → twin-drift-pv.TestTwinDriftReplanFiresOnLargeLoadShift— RMSE 300 W → twin-drift-load.TestTwinDriftReplanIgnoresTinyShift— RMSE 100 W stays quiet.TestTwinDriftReplanRespectsCooldown— large shift inside 30 s gapdoes not trigger.
TestTwinDriftReplanSkipsWhenPredictorIsNil— no panic, no replanwhen predictors aren't wired (initial boot, unit tests).
make verifygreen (vet + unit + e2e + build).🤖 Generated with Claude Code