Skip to content

bug(stats): agent-header 7d cost sparkline is permanently flat on PostgreSQL — DATE() bucket key never matches the axis #2193

Closed

Description

@trinity-ability

Symptom

On the Agent Detail header, the yellow 7-day cost sparkline renders as a flat line at zero, while every scalar beside it on the same row is correct (Today $100, ^56% vs avg, Lifetime $1,218 · 1002 runs).

Only reproduces on PostgreSQL instances. SQLite installs are unaffected, which is why it never showed up locally.

Root cause

db/schedules/stats.py::get_agent_token_stats buckets the 7-day series with a date function on a TEXT column:

SELECT DATE(started_at) as day, ... GROUP BY DATE(started_at)

started_at is an ISO-Z TEXT column (Invariant #16).

  • SQLite — DATE() returns TEXT '2026-08-14'.
  • PostgreSQL — date(x) is the type-name-as-function cast, so the column comes back as a date, which psycopg maps to datetime.date.

The gap-filled axis right below is keyed by strftime("%Y-%m-%d") strings:

raw_days = {row["day"]: row for row in day_rows}
...
d = (now_utc - timedelta(days=i)).strftime("%Y-%m-%d")
if d in raw_days:      # str vs datetime.date -> never matches on PG

A str/date dict lookup does not raise — it misses, and a missed bucket is indistinguishable from a day with no executions. So the whole series reads as legitimate zeros. The scalar cost_24h / cost_7d / lifetime_* fields come from a different query that never touches DATE(), which is why they stayed right and the failure looked cosmetic.

Verified

Same input rows, same code, both backends:

[sqlite]   daily_breakdown costs = [0.0, 0.0, 0.0, 25.0, 0.0, 40.0, 100.0]   total=165.0
[postgres] daily_breakdown costs = [0.0, 0.0, 0.0,  0.0, 0.0,  0.0,   0.0]   total=0.0
           (cost_24h=100.0  cost_7d=165.0  lifetime=165.0 on BOTH)

Why it survived

This is the last dialect-dependent date function in the backend. Every sibling series already uses the substring idiom — get_agent_analytics (#1107, substr(started_at, 1, 10)), get_fleet_execution_timeline (ent#326), the audit heatmap (Python-side bucketing) — so the #1107 Overview chart on the same page renders correctly while this one is flat. get_agent_token_stats (#250) predates the convention and was missed by the #1540 sweep, which repointed the canary collectors at the live engine but not this accessor.

Unit tests are SQLite-only unless TEST_POSTGRES_URL is set, and no workflow sets it — so a behavioural dual-backend test passes vacuously in CI.

Fix

  1. Bucket with the house idiom: substr(replace(started_at, ' ', 'T'), 1, 10) — dialect-agnostic, returns text on both backends, slices the same UTC string the row was written with. replace also covers pre-bug: schedule-triggered executions store naive started_at (no timezone suffix) — UI shows wrong relative time #1474 scheduler rows (space separator, no Z), which clear the lexicographic cutoff and are counted by the scalar query — bucketing them as 2026-08-06 would drop them from the chart alone.
  2. Normalize the join key in Python (_day_bucket_key) so a key-type mismatch can never again read as data.
  3. Guard the class, not just the instance: a static check that this accessor uses no SQL date function, so CI catches a reintroduction on the SQLite-only path.

Acceptance criteria

Activity

  1. added
    priority-p2
    type-bug
    theme-reliability
    complexity-low
    on Aug 14, 2026
  2. vybe commented on Aug 15, 2026

    @vybe
    Contributor

    /claim

  3. added
    status-in-progress
    on Aug 15, 2026
  4. github-actions commented on Aug 15, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/2193-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #2193
  5. added
    status-in-dev
    and removed
    status-in-progress
    on Aug 15, 2026
  6. removed
    status-in-dev
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p2theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions