Skip to content

feat(analytics): add Codex session stats to rtk gain and rtk session#1893

Open
yaco16 wants to merge 2 commits into
rtk-ai:developfrom
yaco16:feat/codex-session-stats
Open

feat(analytics): add Codex session stats to rtk gain and rtk session#1893
yaco16 wants to merge 2 commits into
rtk-ai:developfrom
yaco16:feat/codex-session-stats

Conversation

@yaco16
Copy link
Copy Markdown

@yaco16 yaco16 commented May 15, 2026

Summary

Adds first-class Codex analytics to `rtk gain` and `rtk session`, using the same session-parsing architecture as the existing Claude Code provider.

New: `src/analytics/codex.rs`

Parses `~/.codex/sessions/**/*.jsonl` to extract per-session statistics:

Field Source
Session ID `session_meta.payload.id` (first 8 chars)
Tool calls `response_item` where `payload.type == "function_call" && payload.name == "exec_command"`
RTK adoption each `exec_command` argument is split via `split_command_chain` and classified via `classify_command` (same logic as Claude Code sessions)
Tokens last `event_msg { payload.type: "token_count" }` entry — counts are cumulative, the final snapshot is authoritative

Updated: `src/analytics/gain.rs`

Default summary (`rtk gain`) now appends a Codex Activity (last 30d) block:

```
Codex Activity (last 30d)
─────────────────────────────────────────────
Sessions 27
RTK adoption 329/1 342 cmds (25%)
Total tokens 104M (in: 98M, out: 5M, cached: 0)
```

Updated: `src/analytics/session_cmd.rs`

`rtk session` appends a Codex RTK Adoption table (last 10 sessions):

```
Codex RTK Adoption (last 10)

Session Date Cmds RTK Adoption Tokens

019e2704 Today 48 12 25% █░░░░ 104.2M
Average adoption: 24%
```

Codex JSONL format

Sessions live at `~/.codex/sessions/YYYY/MM/DD/.jsonl`:

Entry type Relevant fields
`session_meta` `payload.id` — session UUID
`response_item` `payload.type`, `payload.name`, `payload.arguments` (JSON-encoded `{"cmd":"..."}`)
`event_msg` `payload.type == "token_count"` → `payload.info.total_token_usage.{input,output,cached_input,total}_tokens`

Test plan

  • `cargo test analytics::codex` — 11 unit tests: exec_command counting, explicit `rtk ` prefix adoption, hook-rewritten command adoption, chained commands (`&&`), cumulative token parsing (last-entry wins), ID shortening, empty files, malformed lines, missing sessions directory
  • `cargo test` — full suite passes (pre-existing env-dependent failure unrelated to this PR)
  • `rtk gain` on a machine with Codex history → "Codex Activity" block appears
  • `rtk session` on a machine with Codex history → Codex adoption table appended
  • Both commands degrade silently when `~/.codex/sessions` is absent

🤖 Generated with Claude Code

Parse ~/.codex/sessions/**/*.jsonl to count exec_command tool calls
and accumulate token usage (input/output/cached) per session.

- src/analytics/codex.rs: new module — discover_sessions() walks the
  Codex sessions dir; parse_session() extracts exec_command call counts
  and the final cumulative total_token_usage from token_count events.
- src/analytics/session_cmd.rs: rtk session now appends a Codex table
  (last 10 sessions, columns: Session / Date / Calls / Tokens).
- src/analytics/gain.rs: rtk gain default summary now shows a compact
  Codex Activity block (sessions, CLI calls, token breakdown).
- src/analytics/mod.rs: expose pub mod codex.

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

CLAassistant commented May 15, 2026

CLA assistant check
All committers have signed the CLA.

Extract exec_command.arguments.cmd from Codex JSONL and apply the same
classify_command + split_command_chain logic used for Claude Code sessions.

- CodexSessionSummary: replace tool_calls with total_cmds/rtk_cmds + adoption_pct()
- parse_session: parse inner arguments JSON, classify each command part
- rtk session: Codex table now mirrors Claude Code (Cmds / RTK / Adoption% / bar / Tokens)
- rtk gain: Codex Activity block shows RTK adoption ratio alongside token breakdown
- 4 new tests: explicit rtk prefix, hook-rewritten commands, chained commands, empty args

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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