Repository navigation
Description
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 adate, which psycopg maps todatetime.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 PGA 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
- 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.replacealso covers pre-bug: schedule-triggered executions store naive started_at (no timezone suffix) — UI shows wrong relative time #1474 scheduler rows (space separator, noZ), which clear the lexicographic cutoff and are counted by the scalar query — bucketing them as2026-08-06would drop them from the chart alone. - Normalize the join key in Python (
_day_bucket_key) so a key-type mismatch can never again read as data. - 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
-
daily_breakdownis identical on SQLite and PostgreSQL for the same rows -
sum(daily_breakdown[].cost)reconciles withcost_7d(modulo the rolling-168h vs 7-calendar-day edge) - A pre-bug: schedule-triggered executions store naive started_at (no timezone suffix) — UI shows wrong relative time #1474 space-separator
started_atlands in its day bucket instead of vanishing - CI fails if a SQL date function returns to this accessor
Activity
vybe commentedon Aug 15, 2026
/claim
github-actions commentedon Aug 15, 2026
Claimed by @vybe. Label
status-in-progressadded.Next steps:
feature/2193-<short-slug>Fixes #2193docs(flows): token-usage-display documented the query that caused #2193
fix(stats): 7d cost sparkline was flat on PostgreSQL — DATE() bucket …