Files
pipelines/internal/pipeline/stats.go
T
alan 3f5c11b7bf Record and expose per-run model usage (tokens, split, cost)
Carry the usage a model stage already measures for its budget
(stage.Budget.Snapshot()) onto the step as typed evidence and total it in
one pass, rather than a second harness read, a transcript parse or an
output-length estimate. A shared budget across stage revisits records only
the visit's increment, and a revisit that observes no new report records no
usage instead of a fabricated known zero.

- protocol: StageUsage/RunUsage/TotalUsage and UsageTotals.
- pipeline: step_usage table + migration, COALESCEd into the one step
  projection, FinishStep persistence, one json_extract history aggregate,
  retention removes the rows with their run.
- server: stageUsage maps the budget snapshot, stamping per-stage model and
  usage for every model stage (including failures).
- cli: inspect/stats human and JSON output.
- budget/pi: retain the reported in/out split, mark a total-only (Pi) report
  split-unknown, and keep an unreported cost unknown (never zero).

Evidence and docs updated in the packaged skill references and docs/cli.md;
the live database has no step_usage rows yet (all history predates this), so
per-run figures are reported as unavailable, not zero.
2026-10-17 20:40:56 +01:00

97 lines
4.2 KiB
Go

package pipeline
import (
"context"
"database/sql"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
const countsSQL = `COUNT(r.id),COALESCE(SUM(r.status='succeeded'),0),COALESCE(SUM(r.status='failed'),0),COALESCE(SUM(r.status='canceled'),0),COALESCE(SUM(r.status='interrupted'),0),COALESCE(SUM(r.status IN ('queued','running')),0),COALESCE(AVG(CASE WHEN r.finished_at IS NOT NULL THEN MAX(0,(julianday(r.finished_at)-julianday(r.created_at))*86400) END),0)`
func (s *Store) UsageStats(ctx context.Context) (*protocol.UsageStats, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
stats := &protocol.UsageStats{Pipelines: []protocol.PipelineUsage{}, Days: []protocol.DailyUsage{}}
c := &stats.RunCounts
if err := tx.QueryRowContext(ctx, "SELECT "+countsSQL+" FROM pipeline_runs r").Scan(&c.Total, &c.Succeeded, &c.Failed, &c.Canceled, &c.Interrupted, &c.Active, &c.AverageSeconds); err != nil {
return nil, err
}
rows, err := tx.QueryContext(ctx, "SELECT p.id,p.name,"+countsSQL+" FROM pipelines p LEFT JOIN pipeline_runs r ON r.pipeline_id=p.id GROUP BY p.id ORDER BY COUNT(r.id) DESC,p.name")
if err != nil {
return nil, err
}
for rows.Next() {
var p protocol.PipelineUsage
c := &p.RunCounts
if err := rows.Scan(&p.PipelineID, &p.Name, &c.Total, &c.Succeeded, &c.Failed, &c.Canceled, &c.Interrupted, &c.Active, &c.AverageSeconds); err != nil {
rows.Close()
return nil, err
}
stats.Pipelines = append(stats.Pipelines, p)
}
if err := rows.Err(); err != nil {
rows.Close()
return nil, err
}
rows.Close()
rows, err = tx.QueryContext(ctx, `WITH RECURSIVE days(day) AS (SELECT date('now','-13 days') UNION ALL SELECT date(day,'+1 day') FROM days WHERE day<date('now')) SELECT day,COUNT(r.id) FROM days LEFT JOIN pipeline_runs r ON r.created_at>=day AND r.created_at<date(day,'+1 day') GROUP BY day ORDER BY day`)
if err != nil {
return nil, err
}
for rows.Next() {
var d protocol.DailyUsage
if err := rows.Scan(&d.Date, &d.Runs); err != nil {
rows.Close()
return nil, err
}
stats.Days = append(stats.Days, d)
}
if err := rows.Err(); err != nil {
rows.Close()
return nil, err
}
rows.Close()
usage, err := usageTotals(ctx, tx)
if err != nil {
return nil, err
}
stats.Usage = usage
return stats, tx.Commit()
}
// usageTotals is the one extra aggregate query the history-wide usage totals add,
// over the same step_usage rows the per-run figures read. It sums reported totals
// with json_extract, counts token/cost-reporting stages and leaves a stage that
// reported no cost uncounted rather than adding a fabricated zero; an old or
// read-only database with no step_usage table yields no totals, not an error.
func usageTotals(ctx context.Context, tx *sql.Tx) (*protocol.UsageTotals, error) {
var totals protocol.UsageTotals
err := tx.QueryRowContext(ctx, `SELECT
COALESCE(COUNT(*),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') THEN json_extract(usage_json,'$.tokens_total') ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') THEN 1 ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.split_known') THEN json_extract(usage_json,'$.tokens_in') ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.split_known') THEN json_extract(usage_json,'$.tokens_out') ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.cost_known') THEN json_extract(usage_json,'$.cost_usd') ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.cost_known') THEN 1 ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') AND NOT COALESCE(json_extract(usage_json,'$.split_known'),0) THEN 1 ELSE 0 END),0)
FROM step_usage`).Scan(&totals.Stages, &totals.TokensTotal, &totals.TokensKnownStages, &totals.TokensIn, &totals.TokensOut, &totals.CostUSD, &totals.CostKnownStages, &totals.SplitUnknownStages)
if err != nil {
if strings.Contains(err.Error(), "no such table") {
return nil, nil
}
return nil, err
}
if totals.Stages == 0 {
return nil, nil
}
totals.CostUnknownStages = totals.Stages - totals.CostKnownStages
totals.SplitKnown = totals.TokensKnownStages > 0 && totals.SplitUnknownStages == 0
return &totals, nil
}