Files
pipelines/internal/pipeline/stats.go
T
alan b3c9575fde Runs park on usage limits; failure classes, harness error policy, run budgets
- A stage refused for a Claude usage limit no longer fails its run: the
  runner tries the chats' Claude account auto-switch (per-home setting) and
  reruns the visit, else parks the run in the new waiting_quota status
  (persisted, with reset/resume time in run summaries, details and events)
  and resumes the same visit at reset + 1 min, also after a restart.
  resume_run / `pipelines resume` resumes now; cancel works on parked runs.
  The rerun visit gets a "# Previous attempt" interruption note. Subscription
  limits no longer say "top up the provider account".
- run/retry into an account known to be limited are deferred (quota_policy
  defer/refuse/now; CLI refuses under --json/--yes unless --defer or --now).
- failure_class on runs (quota, loop_exhausted, timeout, permission_denied,
  harness_error, blocked, environment, canceled, interrupted, budget,
  check_failed, other) in RunSummary/RunDetail/events, CLI inspect/runs/wait/
  stats and the app failure line.
- Claude permission denials (permission_denials / refused tool results) fail
  a blocked visit with permission_denied naming the tool use.
- Harness failures are classified transient vs deterministic; deterministic
  ones (and a repeated empty readback) skip error self-loops; transient
  self-loops back off 15 s, 1 min, 4 min.
- Whole-run USD budget: run --budget, server default (run-budget /
  get/set_run_settings); 80% warning notification, stop with class budget.
- App: waiting_quota status/filter with resume time, Resume now and Cancel,
  failure class wording. Docs and skill references updated.

Merges main (check stage, delivery gate, Claude stage restart survival,
schedule changes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:34:53 +00:00

120 lines
6.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
if stats.FailureClasses, err = failureClassCounts(ctx, tx); err != nil {
return nil, err
}
if err := tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM pipeline_runs WHERE status=?`, protocol.RunStatusWaitingQuota).Scan(&stats.WaitingQuota); err != nil {
return nil, err
}
return stats, tx.Commit()
}
// usageSumColumns sums step_usage rows into the RunUsage/UsageTotals shape:
// stages, reported total, token-reporting stages, split in/out, cost,
// cost-reporting stages, token stages without a split, then reasoning, cache
// read and cache write sums and the stages that recorded them, in that order. It is
// the one SQL definition of the sum, shared by the history-wide totals and the
// per-run list aggregate, and mirrors protocol.TotalUsage over the same rows.
const usageSumColumns = `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),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') AND json_extract(usage_json,'$.cache_known') THEN COALESCE(json_extract(usage_json,'$.tokens_reasoning'),0) ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') AND json_extract(usage_json,'$.cache_known') THEN COALESCE(json_extract(usage_json,'$.cache_read'),0) ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') AND json_extract(usage_json,'$.cache_known') THEN COALESCE(json_extract(usage_json,'$.cache_write'),0) ELSE 0 END),0),
COALESCE(SUM(CASE WHEN json_extract(usage_json,'$.tokens_known') AND json_extract(usage_json,'$.cache_known') THEN 1 ELSE 0 END),0)`
// usageFrom is the step_usage rows that belong to a recorded step. A usage row
// whose step (or run) no longer exists, such as one left behind by an old
// history wipe, is never counted, and is not deleted here either. It is the
// same row set the run detail reads through its step loop.
const usageFrom = `FROM step_usage u JOIN run_steps s ON s.run_id=u.run_id AND s.sequence=u.sequence`
// 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 `+usageSumColumns+` `+usageFrom).Scan(&totals.Stages, &totals.TokensTotal, &totals.TokensKnownStages, &totals.TokensIn, &totals.TokensOut, &totals.CostUSD, &totals.CostKnownStages, &totals.SplitUnknownStages, &totals.TokensReasoning, &totals.CacheRead, &totals.CacheWrite, &totals.CacheKnownStages)
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
totals.CacheKnown = totals.TokensKnownStages > 0 && totals.CacheKnownStages == totals.TokensKnownStages
return &totals, nil
}