Skip to content

fix(analytics): surface active duration in top sessions (#83)#867

Closed
rodboev wants to merge 7 commits into
kenn-io:mainfrom
rodboev:pr/83-top-sessions-active-duration
Closed

fix(analytics): surface active duration in top sessions (#83)#867
rodboev wants to merge 7 commits into
kenn-io:mainfrom
rodboev:pr/83-top-sessions-active-duration

Conversation

@rodboev

@rodboev rodboev commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Top Sessions currently ranks by wall-clock duration, computed as the gap between started_at and ended_at. That overstates sessions left idle, a terminal left open overnight dominates the ranking even if actual tool work lasted only a few minutes. The underlying per-turn timing data already exists and powers the per-session timing endpoint, but the analytics aggregation path never uses it.

This change adds an explicit active_duration_min field to the Top Sessions contract, computed from the same tool-turn timing semantics the repo already uses elsewhere, and switches the duration ranking to use that active value across the SQLite, PostgreSQL, and DuckDB analytics backends. The existing duration_min field stays populated so total session time remains visible to users as secondary context, which keeps the “show both” part of the issue intact.

On the frontend, the duration view now reads as active or tool time, uses the active value as the primary metric, keeps total duration available alongside it, and routes the new tooltip text through the existing Paraglide message surface. Focused backend regression tests cover the case where wall-clock duration and active duration disagree, the SQLite DST fallback path, DuckDB parity, PostgreSQL running-session null handling, and the ranking helper, while the Top Sessions frontend test proves the new display contract.

Fixes #83

@roborev-ci

roborev-ci Bot commented Jun 25, 2026

Copy link
Copy Markdown

roborev: Combined Review (27a8c44)

Summary verdict: One medium correctness issue remains; no security issues were found.

Medium

  • Location: internal/db/analytics.go:4377, internal/db/timing.go:242, internal/duckdb/analytics_usage.go:1682
  • Problem: Active duration is rounded before ranking in the SQLite SQL path, the SQLite Go fallback, and the DuckDB path. Because the duration query orders and limits on that rounded value, sessions that differ by less than 0.05 minutes can be misordered or excluded from the top 10. This also disagrees with the PostgreSQL path, which sorts before rounding.
  • Fix: Keep active duration as a raw minute value through ordering/sorting and truncation, then round only in rankTopSessions or immediately before returning the response.

Panel: ci_default_security | Synthesis: codex, 7s | Members: codex_default (codex/default, done, 5m47s), codex_security (codex/security, done, 1m11s) | Total: 7m5s

@roborev-ci

roborev-ci Bot commented Jun 25, 2026

Copy link
Copy Markdown

roborev: Combined Review (1db5e41)

Summary verdict: No Medium, High, or Critical findings to report.

Both reviews found no security issues or correctness issues at Medium severity or above.


Panel: ci_default_security | Synthesis: codex, 4s | Members: codex_default (codex/default, done, 8m4s), codex_security (codex/security, done, 31s) | Total: 8m39s

@wesm

wesm commented Jun 25, 2026

Copy link
Copy Markdown
Member

Good catch, I was just thinking about fixing this myself

@mariusvniekerk

Copy link
Copy Markdown
Collaborator

I'm taking a look here, this feels like it will undercount heavily for sessions dominated by agent reasoning.

@mariusvniekerk

Copy link
Copy Markdown
Collaborator

Should we make this reuse the "active minutes" concept from the velocity calculation for analytics?

@rodboev

rodboev commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Do you want me to hold? I traced both paths and they are different:

  • current PR code: sums tool-use turn durations
  • velocity: sums every positive gap between consecutive messages, capped at 5 minutes per gap

That means the current branch can undercount reasoning-heavy, low-tool sessions. I was about to patch top-session duration to reuse the velocity calculation across SQLite, PostgreSQL, DuckDB, and the SQLite fallback and see how the totals net out, but I can switch things up based on your direction.

@rodboev

rodboev commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Even with this change, the top results are still very long sessions, and the new active-duration values stay close to total duration on my data. It does knock out the worst idle outliers, so it is moving in the right direction, but it still is not measuring activity the way the velocity view does. I'm reworking this to reuse the "active minutes" concept from velocity so long idle gaps are capped consistently and the Top Sessions ranking lines up better with actual active work.

@rodboev

rodboev commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Withdrawing in favor of #869.

@rodboev rodboev closed this Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Top session duration metric total vs API

3 participants