Skip to content

feat(tracking): add parent session ID for subagent aggregation - #4

Open
karl82 wants to merge 3 commits into
feat/gain-sessionfrom
feat/parent-session-tracking
Open

feat(tracking): add parent session ID for subagent aggregation#4
karl82 wants to merge 3 commits into
feat/gain-sessionfrom
feat/parent-session-tracking

Conversation

@karl82

@karl82 karl82 commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds parent_session_id TEXT DEFAULT '' column + index to the tracking DB (migration-safe)
  • RTK's PreToolUse rewrite hook injects --parent-session=<id> into the rewritten command when CLAUDE_CODE_PARENT_SESSION_ID or RTK_PARENT_SESSION_ID is set — positioned before the subcommand so Clap consumes it without leaking into the underlying command's args
  • Global --parent-session CLI flag on Cli writes into a OnceLock override (highest priority), falling back to env vars
  • New get_by_root_session() query aggregates parent + all child sessions into one row
  • rtk gain --parent-session [<id>] shows the root-session view with per-tree savings

How it works

Parent session (CLAUDE_CODE_SESSION_ID=abc):
  rtk git status              →  session_id=abc, parent_session_id=""

Subagent (CLAUDE_CODE_SESSION_ID=xyz, CLAUDE_CODE_PARENT_SESSION_ID=abc):
  rewrite: git status → rtk --parent-session=abc git status
  rtk git status              →  session_id=xyz, parent_session_id=abc

rtk gain --parent-session
  Root abc  •  2 sessions  •  15 commands  •  12.3k saved  •  82.1%

Notes on --parent-session flag isolation

--parent-session=X is injected before the subcommand (rtk --parent-session=X git status). Clap parses it at the Cli level via global = true; it never appears in the args passed to git/cargo/etc. original_cmd and token counts are unaffected.

Stacked on

PR #3 (feat/gain-session) — per-session savings view.

🤖 Generated with Claude Code

karl82 and others added 2 commits June 21, 2026 16:29
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Adds `parent_session_id` tracking so token savings from Claude Code
subagents (spawned via the Agent tool) can be aggregated under their
parent session.

- DB: new `parent_session_id TEXT DEFAULT ''` column + index migration
- tracking: `set_parent_session_id()` + `OnceLock` override; reads
  `CLAUDE_CODE_PARENT_SESSION_ID` → `RTK_PARENT_SESSION_ID` env vars
- rewrite hook: injects `--parent-session=<id>` into rewritten RTK
  commands when running as a subagent (positions before subcommand so
  Clap consumes it before routing to git/cargo/etc.)
- CLI: global `--parent-session` flag on both the top-level `Cli`
  (consumed by tracking) and `rtk gain` (for viewing aggregated stats)
- gain: `--parent-session [<id>]` shows root-session view with
  parent + all child session savings aggregated per row
- `get_by_root_session()` query groups by effective root session

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@karl82 karl82 changed the title feat/parent session tracking feat(tracking): add parent session ID for subagent aggregation Jun 21, 2026
…tree

- `get_summary_for_root_session(prefix)`: aggregates all commands where
  `session_id GLOB prefix*` OR `parent_session_id GLOB prefix*`
- `show_root_session_detail()`: same KPI + by-command layout as
  `--session <id>`, with optional subagent session breakdown header
- `rtk gain --parent-session` (bare) still lists root sessions
- `rtk gain --parent-session <id>` now shows full detail view

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.

1 participant