Skip to content

feat(mpc): /api/mpc/diagnose endpoint for per-slot audit - #98

Merged
frahlg merged 1 commit into
masterfrom
planner-overhaul/mpc-diagnose
Apr 18, 2026
Merged

frahlg merged 1 commit into
masterfrom
planner-overhaul/mpc-diagnose

Conversation

@frahlg

@frahlg frahlg commented Apr 18, 2026

Copy link
Copy Markdown
Member

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 recent Optimize call, 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 window

Per-slot outputs the DP produced: battery_w, grid_w, soc_pct, cost_ore (raw un-blended), reason, ems_mode, pv_limit_w

Params 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 Params fields populated alongside Service.last *Plan in replan()
  • Service.Diagnose() *Diagnostic joins by slot index
  • New go/internal/mpc/diagnose.go holds the Diagnostic / DiagnosticSlot / DiagnosticParams types (JSON-friendly, subset of internal Params)
  • handleMPCDiagnose in api.go + new route

Safe-by-construction: Diagnose returns nil before first replan, truncates on Slots/Actions length mismatch rather than panicking.

Example

curl localhost:8080/api/mpc/diagnose | jq '.diagnostic.slots[] | {idx, slot_start_ms, price_ore, confidence, pv_w, battery_w, soc_pct, reason}'

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/... — new diagnose_test.go covers nil-before-replan, join shape, length-mismatch safety
  • go test ./... — full suite incl. e2e (27s) green
  • Deploy to Pi, curl localhost:8080/api/mpc/diagnose | jq — inspect Erik's 21:00 decision
  • Follow-up PR: wire a diagnose inspector into the web UI

Roadmap context

Part of the unified-planner overhaul.

🤖 Generated with Claude Code

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]>
@frahlg
frahlg merged commit e500f2b into master Apr 18, 2026
1 check passed
@frahlg
frahlg deleted the planner-overhaul/mpc-diagnose 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