From f8ae821240f33d615a9e91cdfeb6c026b7970782 Mon Sep 17 00:00:00 2001 From: Claude Sonnet 5 Date: Sat, 4 Jul 2026 08:50:46 +0000 Subject: feat(web,api): add Budget/Roles dashboard -- final phase of the harness redesign (Phase 9b) Two new tabs, matching Phase 9a's conventions: Budget (per-provider spend meters, escalation funnel, spend-over-time) and Roles (version history, draft activation, readable escalation-ladder view) -- the human-facing surface for the token-husbanding value proposition and the Phase 5/8 versioned role-config system. New backend (minimal, additive, matching existing endpoint conventions -- unauthenticated like /api/budget and /api/roles/*): - internal/storage/dashboard.go: QueryEscalationFunnel (executions grouped by escalation_rung + agent, count + cost) and QuerySpendTimeseries (cost per provider bucketed hourly/daily, mirroring QueryDashboardStats' existing bucketing expressions). Documented honestly: rung 0 is not exclusively "resolved locally" -- escalation_rung defaults to 0 uniformly, so non-role-typed executions (which never climb a ladder) land there too, alongside role-typed tasks genuinely resolved at tier 0. A role-only variant would need a join against tasks.config_json with no queryable role column on executions -- intentionally out of scope. - internal/api/dashboard.go: GET /api/escalation-funnel, GET /api/spend-timeseries (both ?window=5h|24h|7d|, default 24h). - internal/storage.ListRoleNames + GET /api/roles: the "which roles exist" gap -- there was no way to discover role names before this, only to list versions for a role you already knew the name of. Chart-form decisions (dataviz skill, invoked before writing chart code): horizontal stacked bar for the escalation funnel (rung order already encodes the funnel shape positionally; color only needed for per-provider segments within each rung); multi-line for spend-over-time; a fixed-order categorical palette from the skill's validated palette.md slots for provider identity (re-validated against this app's dark surface, passing); a separate status (good/warning/critical) palette for budget meters, deliberately distinct from both --state-* and the provider palette. Caught and fixed a real bug during visual QA: converging near-zero end-labels on the spend chart were overlapping (an anti-pattern the skill explicitly flags) -- fixed with a 14px minimum-gap check before direct-labeling an endpoint, leaning on the legend/tooltip otherwise. Verified with a real running server, real seeded data (65 executions across rungs 0-2 with a realistic provider mix, 2 roles with active/draft/retired role_configs versions written directly via internal/storage), and a real headless-browser session (reusing Phase 9a's Chromium/proxy scaffinding): confirmed correct rung totals/percentages, provider legends, a 3-line spend chart, live window-selector re-render, correct active-version highlighting on the role panel, and a real Activate click on a draft version -- verified via both DOM re-render and a direct backend GET that it truly persisted. go build/vet/test -race -count=1 all pass, full suite. node --test web/test/*.mjs: 291/291 passing (16 new). Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01V1moSNCJRcP6kykA4tyUSs --- internal/storage/dashboard.go | 91 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 internal/storage/dashboard.go (limited to 'internal/storage/dashboard.go') diff --git a/internal/storage/dashboard.go b/internal/storage/dashboard.go new file mode 100644 index 0000000..e432af1 --- /dev/null +++ b/internal/storage/dashboard.go @@ -0,0 +1,91 @@ +package storage + +import "time" + +// EscalationBucket is one (escalation_rung, provider) aggregate over +// executions in a window — the raw material for the budget dashboard's +// escalation funnel (GET /api/escalation-funnel), the harness's +// "how much falls through each rung" story made visible. +type EscalationBucket struct { + Rung int `json:"rung"` + Agent string `json:"agent"` + Count int `json:"count"` + CostUSD float64 `json:"cost_usd"` +} + +// QueryEscalationFunnel aggregates executions started at or after `since` by +// (escalation_rung, agent), counting executions and summing cost. Excludes +// executions still in flight (RUNNING/QUEUED/PENDING have no terminal +// outcome yet to attribute to a rung). +// +// Rung 0 is not exclusively "resolved locally": executions.escalation_rung +// defaults to 0 uniformly, so every non-role-typed execution (which never +// climbs a ladder at all) also lands there alongside role-typed tasks +// genuinely resolved at their ladder's first tier. This mirrors the +// aggregate shape suggested for this phase and keeps the query a plain +// GROUP BY with no join — a role-only variant would need to join tasks and +// inspect config_json (no queryable role column on executions), which is +// intentionally left out of scope here. +func (s *DB) QueryEscalationFunnel(since time.Time) ([]EscalationBucket, error) { + rows, err := s.db.Query(` + SELECT escalation_rung, COALESCE(agent, ''), COUNT(*), COALESCE(SUM(cost_usd), 0) + FROM executions + WHERE start_time >= ? AND status NOT IN ('RUNNING', 'QUEUED', 'PENDING') + GROUP BY escalation_rung, agent + ORDER BY escalation_rung ASC, agent ASC`, since.UTC()) + if err != nil { + return nil, err + } + defer rows.Close() + + out := []EscalationBucket{} + for rows.Next() { + var b EscalationBucket + if err := rows.Scan(&b.Rung, &b.Agent, &b.Count, &b.CostUSD); err != nil { + return nil, err + } + out = append(out, b) + } + return out, rows.Err() +} + +// SpendPoint is one (time bucket, provider) aggregate of cost — the raw +// material for the budget dashboard's spend-over-time chart +// (GET /api/spend-timeseries). +type SpendPoint struct { + Bucket string `json:"bucket"` // RFC3339 truncated to the hour, or YYYY-MM-DD + Agent string `json:"agent"` + CostUSD float64 `json:"cost_usd"` +} + +// QuerySpendTimeseries aggregates cost per provider bucketed by hour +// (hourly=true) or by calendar day (hourly=false), for executions started at +// or after since. Mirrors QueryDashboardStats' own Throughput (hourly) / +// Billing (daily) bucketing expressions, just grouped additionally by +// provider so per-provider trend lines can be drawn. +func (s *DB) QuerySpendTimeseries(since time.Time, hourly bool) ([]SpendPoint, error) { + bucketExpr := "date(start_time)" + if hourly { + bucketExpr = "strftime('%Y-%m-%dT%H:00:00Z', start_time)" + } + rows, err := s.db.Query(` + SELECT `+bucketExpr+` as bucket, COALESCE(agent, ''), COALESCE(SUM(cost_usd), 0) + FROM executions + WHERE start_time >= ? + GROUP BY bucket, agent + ORDER BY bucket ASC, agent ASC`, since.UTC()) + if err != nil { + return nil, err + } + defer rows.Close() + + out := []SpendPoint{} + for rows.Next() { + var p SpendPoint + if err := rows.Scan(&p.Bucket, &p.Agent, &p.CostUSD); err != nil { + return nil, err + } + out = append(out, p) + } + return out, rows.Err() +} -- cgit v1.2.3