Repository navigation
feat(mpc): /api/mpc/diagnose endpoint for per-slot audit - #98
Merged
Merged
Conversation
Observability endpoint that exposes the full input-output context of the most recent Optimize call, joined per slot: - inputs the DP saw: price_ore (consumer total), spot_ore (raw), confidence (1.0 day-ahead vs 0.6 forecast), pv_w, load_w, slot window - decision the DP made: battery_w, grid_w, soc_pct (end-of-slot), cost_ore (raw un-blended), reason string, ems_mode, pv_limit_w - the Params the DP was parameterized with (mode, SoC bounds, efficiency, terminal valuation, export bonus/fee) Lets operators answer "why did the planner charge at 21:00?" without shelling into the host or correlating two separate endpoint outputs. Implementation: Service keeps a reference to the slots + params that went into the most recent Optimize, alongside the resulting Plan. The Diagnose() accessor joins them by slot index. Safe-by-construction truncation handles a potential Slots/Actions length mismatch. Part of the planner overhaul roadmap (Phase 2 of 4). Subsequent PRs add PowerLimits + Loadpoint skeleton (Phase 3) and unified EV-in-DP optimization (Phase 4). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced Apr 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 2 of 4 in the planner overhaul roadmap. Builds on the observability gap that blocked root-causing Erik's 21:00 charging bug.
Adds
GET /api/mpc/diagnose— returns the full input/output context of the most recentOptimizecall, joined per slot:Per-slot inputs the DP saw:
price_ore(consumer total),spot_ore(raw),confidence(1.0 day-ahead vs 0.6 forecast),pv_w,load_w, slot windowPer-slot outputs the DP produced:
battery_w,grid_w,soc_pct,cost_ore(raw un-blended),reason,ems_mode,pv_limit_wParams the DP was parameterized with: mode, SoC bounds, efficiency, terminal valuation, export bonus/fee
Lets operators answer "why did the planner decide X at 21:00?" without correlating multiple endpoints or shelling into the host.
Implementation
Service.lastSlots []Slot+Service.lastParams Paramsfields populated alongsideService.last *Planinreplan()Service.Diagnose() *Diagnosticjoins by slot indexgo/internal/mpc/diagnose.goholds theDiagnostic/DiagnosticSlot/DiagnosticParamstypes (JSON-friendly, subset of internalParams)handleMPCDiagnoseinapi.go+ new routeSafe-by-construction: Diagnose returns nil before first replan, truncates on Slots/Actions length mismatch rather than panicking.
Example
Returns one row per horizon slot so you can spot "forecast slot got a confidence 0.6 weight that pulled the effective price toward horizon mean, hence the charge decision despite apparent 145 öre price".
Test plan
go test ./internal/mpc/...— newdiagnose_test.gocovers nil-before-replan, join shape, length-mismatch safetygo test ./...— full suite incl. e2e (27s) greencurl localhost:8080/api/mpc/diagnose | jq— inspect Erik's 21:00 decisionRoadmap context
Part of the unified-planner overhaul.
🤖 Generated with Claude Code