Skip to content

feat(tracking+gain): per-session token savings tracking#2537

Open
karl82 wants to merge 4 commits into
rtk-ai:developfrom
karl82:feat/gain-session
Open

feat(tracking+gain): per-session token savings tracking#2537
karl82 wants to merge 4 commits into
rtk-ai:developfrom
karl82:feat/gain-session

Conversation

@karl82

@karl82 karl82 commented Jun 21, 2026

Copy link
Copy Markdown

What

Adds per-session token savings tracking to rtk gain. Each Claude Code session gets its own row in the tracking database so you can see exactly how much each session saved.

Changes

src/core/tracking.rs

  • New session_id TEXT DEFAULT '' column on the commands table (migration-safe ALTER TABLE)
  • Index on session_id for fast per-session queries
  • SessionStat struct: session_id, last_seen, commands, saved_tokens, avg_savings_pct
  • Tracker::get_by_session(filter) — groups commands by CLAUDE_CODE_SESSION_ID, most recent first, empty IDs excluded
  • Tracker::get_summary_for_session(prefix) — full GainSummary scoped to one session (used for detail view)
  • record() now captures CLAUDE_CODE_SESSION_ID from env on every insert

src/analytics/gain.rs

  • --session [<SESSION_ID>] flag (short -S):
    • bare --session → table of all sessions (most recent 20), sorted newest first
    • --session <prefix> → detailed gain summary scoped to that session (same layout as global rtk gain)
  • Extracted print_summary_kpis() and print_by_command_table() helpers (no behaviour change — reduces duplication between global and per-session views)

src/main.rs

  • Plumbing for the new --session flag on the Gain command

Usage

# List all sessions (most recent 20)
rtk gain --session

# Session   Last Seen     Cmds     Saved   Avg%
# ──────────────────────────────────────────────────────────────
# 2a35ea7f  3m ago          42    128.4k  83.1%
# f1c90b12  1h ago          17     45.2k  79.6%
# …
# Total: 5 sessions  •  91 commands  •  234.7k saved

# Full gain detail scoped to a session (prefix or full UUID)
rtk gain --session 2a35ea7f

Notes

  • Sessions with no CLAUDE_CODE_SESSION_ID (recorded before this change, or commands run outside Claude Code) are silently excluded from session views — they still count in the global rtk gain summary
  • get_by_session uses GLOB not LIKE for prefix matching (consistent with existing project_path filter — avoids _ and % acting as wildcards)
  • Existing databases are migrated automatically on first run; new_in_memory() (tests) uses the full schema directly

🤖 Generated with Claude Code

karl82 and others added 4 commits June 21, 2026 16:03
- Add current_session_id() helper reading CLAUDE_CODE_SESSION_ID env var
- Migrate commands table: session_id TEXT DEFAULT '' + index
- Add SessionStat struct for per-session aggregation
- Add get_by_session(filter: Option<&str>) with LIKE prefix matching
- 6 unit tests: grouping, empty-id exclusion, ordering, empty db, avg pct, prefix filter
- Backward compatible: ALTER TABLE ignored on existing column, old rows get empty default
- rtk gain --session        show all tracked sessions (most recent first)
- rtk gain --session <id>   filter by session ID prefix (e.g. '2a35ea7f')
- show_session_view(): table with short ID, relative age, cmds, saved, avg%
- Empty-state message for installs predating session tracking
- Uses clap num_args=0..=1 + default_missing_value='' for optional arg
- LIKE → GLOB for session prefix filter (matches project_path convention)
- COALESCE(SUM/AVG) guards against NULL on empty groups
- LIMIT only applies to bare --session; prefix filter returns all matches
- show_session_view: title shows prefix not first session ID
- show_session_view: chars().take(8) instead of byte-index slice
- --session <id>: show full rtk gain detail view scoped to that session
  (by-command table, efficiency meter, exec time) via get_summary_for_session()
… helpers

Eliminates the duplicated table-rendering code between run() and
show_session_detail() by factoring it into two shared private functions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Jun 21, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants