Skip to content

feat(mpc): tighter replan triggers + twin-driven replan signal - #380

Merged
frahlg merged 1 commit into
masterfrom
replanner-more-responsive
May 28, 2026
Merged

frahlg merged 1 commit into
masterfrom
replanner-more-responsive

Conversation

@frahlg

@frahlg frahlg commented May 28, 2026

Copy link
Copy Markdown
Member

Summary

  • Tighten reactive replan thresholds: PV 500 → 250 Wh, load 400 → 200 Wh,
    half-life 15 → 8 min, cooldown 60 → 30 s.
  • Add a third trigger — checkTwinDrift — that fires when the PV or
    load 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 slot
    so the twin-drift detector has a stable comparison baseline. The
    snapshot is built under the same s.mu lock as s.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

  • DP grid resolution (51 × 21 × 193) — same compute envelope.
  • Terminal-credit math (upperHalfMeanPrice, selfConsumptionTerminalPrice).
  • Plan stability protections: MaxPlanAge gate, SlotStart anchoring,
    cooldown between reactive replans (still enforced, just shorter).
  • Predictor signatures (PVPredictor, LoadPredictor) — unchanged.

Test plan

  • TestReactiveReplanFiresAtTighterLoadThreshold — exercises 1.5 kW
    sustained load drift hitting the new 200 Wh threshold within minutes.
  • TestReactiveReplanFiresAtTighterPVThreshold — symmetric PV case
    against the new 250 Wh threshold.
  • TestReactiveReplanRespectsShorterCooldown — 30 s cooldown still
    suppresses 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 gap
    does not trigger.
  • TestTwinDriftReplanSkipsWhenPredictorIsNil — no panic, no replan
    when predictors aren't wired (initial boot, unit tests).
  • make verify green (vet + unit + e2e + build).

🤖 Generated with Claude Code

@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

https://github.com/frahlg/forty-two-watts/blob/13420b50c124ba0c4fd9965c0f942a58255052a8/go/internal/mpc/service.go#L471
P2 Badge 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]>
@frahlg
frahlg force-pushed the replanner-more-responsive branch from 13420b5 to d6df009 Compare May 28, 2026 07:08
@frahlg
frahlg merged commit 2ff3d09 into master May 28, 2026
2 checks passed
@frahlg
frahlg deleted the replanner-more-responsive branch August 7, 2026 08:18
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