Repository navigation
feat(savings): pure-Go backwards reconstruction package - #198
Conversation
Re-prices a measured day with mpc.SlotGridCostOre to compute actual cost vs no-battery counter-factual. Same cost model as live planner, so historical savings line up with what the MPC card already shows. Per-direction pricing inside a slot — a slot that imports for 30 min and exports for 30 min is scored as 0.5 kWh × import + 0.5 kWh × export revenue, not 0 kWh net. MaxGapMs (20 min) caps stale-row attribution so an outage doesn't silently inflate either side. Flow decomposition (self-consumption / arbitrage / direct-export / bat→home / bat→grid) is presentation-only and doesn't influence cost numbers. Pure functions; caller wires LoadHistory + LoadPrices in PR 2. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bbc5546330
ℹ️ 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".
| loadDeficit := load - self | ||
|
|
||
| pvToBat := minPos(pvSurplus, charged) | ||
| directExport := pvSurplus - pvToBat |
There was a problem hiding this comment.
Clamp direct-export flow at zero
directExport is derived as pvSurplus - pvToBat without a non-negative clamp, while the other channel outputs are clamped. If PVKWh is slightly negative in a slot (which can happen with meter noise or inverter standby draw), this yields a negative DirectExportKWh value, producing physically impossible breakdown data in the API/UI despite this struct representing directional kWh channels. Clamping this field like gridToBat/gridToHome/batToGrid avoids invalid negative flow rows.
Useful? React with 👍 / 👎.
Summary
PR 1 of 3 in the historical savings feature track. This drops in
a pure-Go package that re-prices a past day's measured power flows
with the same cost model the live MPC planner uses
(
mpc.SlotGridCostOre) and compares actual cost to a no-batterycounter-factual.
No DB plumbing, no API endpoints, no frontend — those land in PR 2
and PR 3 once the math is reviewed.
What's inside
ComputeDay(history, prices, params, startMs, endMs) DaySavings— pure function; caller passes
[]state.HistoryPointand[]state.PricePoint.self-consumption, direct-export, PV→battery, battery→home,
battery→grid, grid→home, grid→battery (kWh per channel).
30 min and exports for 30 min is scored as 0.5 kWh × import + 0.5
kWh × export, not 0 kWh net).
MaxGapMs(20 min) caps stale-row attribution so an outage doesn'tsilently inflate cost in either direction; coverage stays honest.
Why this design
Cost is delegated entirely to
mpc.SlotGridCostOre— the samefunction the live planner and
ComputeBaselinesalready share. Thiskeeps "savings yesterday" comparable with "vs no battery" on the
live MPC card; if we re-implemented the import/export math here we'd
inevitably drift out of sync with the planner.
The integration convention (
W × (ts − prev_ts)summed left-Riemann,clipped to slot edges) matches
state.DailyEnergyso daily Whtotals computed two different ways agree by construction.
What's NOT in this PR
/api/savings/{daily,intraday,summary}landsin PR 2 along with a small
MPC.LatestParams()getter, the dailycache, and an
e2etest exercising the full SQLite → handler path.web/savings.js+ KPI card + drill-down land inPR 3.
planner_diagnosticsJSON per slot). Slot in PR 2.historical inputs) — deferred to PR 4 once we have evidence the
no-battery baseline is the one operators read.
Test plan
go test ./internal/savings/...— 13 tests cover emptyinputs, flat-load zero savings, perfect arbitrage, perfect
self-consumption, mixed-direction slot, large-gap rejection,
no-history-but-prices, day-boundary clipping, unsorted-input
tolerance, and four decomposition cases.
go test ./...— full suite green (602 tests across 39packages); no regressions.
few real days.