Skip to content

bug(stats): agent-header cost strip disagrees with itself — 168h window vs 7-day axis, legacy error rows dropped, trend baseline includes today #2203

Open

Description

@trinity-ability

Three pre-existing defects in db/schedules/stats.py::get_agent_token_stats (#250), surfaced by the code review on #2194 (the #2193 fix). All three make the agent-header cost strip contradict either itself or the rest of the platform. None was introduced by #2193 — they were deliberately left out of that PR to keep a bug fix reviewable, and #2193 makes the first one visible on PostgreSQL for the first time (the sparkline was all zeros there before).

1. Rolling 168h window vs a 7-calendar-day axis

The per-day query filters started_at > iso_cutoff(168) — a rolling 168 hours — while the gap-fill axis emits 7 calendar days (i in range(6, -1, -1)). The partial 8th day is computed and then discarded.

now = 2026-08-14 18:00Z → cutoff_7d = 2026-08-07 18:00Z. A row at 2026-08-07 19:00Z costing $40 passes the WHERE, buckets to 2026-08-07, and is summed into cost_7d — but the axis only emits 2026-08-08..2026-08-14, so that bucket is never read. The header renders 7d $165 above a sparkline totalling $125.

Same "chart contradicts the number above it" class as #2193, by a third route. Either window can be authoritative; they must be the same one.

2. Legacy error rows are dropped here and counted everywhere else

This accessor filters status IN ('success', 'failed'). Every sibling in the same file uses status IN ('failed', 'error') (lines ~395 / ~463 / ~596), and analytics.py documents error as the legacy failed status.

So for any agent carrying legacy error rows, the header's Lifetime, cost_24h, cost_7d and the sparkline all under-count, while the Executions page, /api/executions/stats, the ent#326 timeline tile and the #1107 Overview chart on the same page include them.

Note this changes displayed spend figures, so it wants its own review rather than riding along in a fix.

3. avg_daily_cost includes today, while its comment says it excludes it

# Trend: today vs 7d daily average (excluding today to avoid comparison bias)
avg_daily_cost = cost_7d / 7.0 if cost_7d > 0 else 0.0

cost_7d is the rolling 168h total, which contains cost_24h. So today is in the baseline it is compared against, and the ^N% vs avg chip is systematically damped — the bias growing with the spike size, i.e. worst exactly in the case the chip exists to surface. Agent spends $0 for six days then $700 today: the intended baseline is $0, the actual baseline is $100.

Either the comment or the arithmetic is wrong. Decide which, then make them agree.

Acceptance criteria

  • sum(daily_breakdown[].cost) == cost_7d for any row set (one window, not two)
  • Terminal-status filtering matches the sibling accessors in the same file
  • The trend baseline and its comment describe the same computation
  • Guarded by tests that fail against today's behaviour

Found by: /code-review on #2194.

Activity

  1. added
    priority-p3
    type-bug
    theme-reliability
    complexity-low
    on Aug 15, 2026
  2. added
    status-ready
    on Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    complexity-lowpriority-p3status-readytheme-reliabilitytype-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions