Skip to content

fix: discover subagent files by directory scan instead of JSONL references#245

Open
neoeny152 wants to merge 1 commit intosirmalloc:mainfrom
neoeny152:fix/subagent-token-discovery
Open

fix: discover subagent files by directory scan instead of JSONL references#245
neoeny152 wants to merge 1 commit intosirmalloc:mainfrom
neoeny152:fix/subagent-token-discovery

Conversation

@neoeny152
Copy link
Copy Markdown

Summary

  • Bug: getSubagentTranscriptPaths() relies on getReferencedSubagentIds() finding agentId fields in the main session JSONL. However, the main JSONL contains zero entries with agentId — all agent IDs exist only in the separate subagent files under {sessionDir}/subagents/. This causes getReferencedSubagentIds() to return an empty set, making getSubagentTranscriptPaths() short-circuit and return [], so no subagent files are ever read.
  • Impact: Both speed metrics (tok/s) and token counts miss all subagent tokens. In sessions with heavy agent usage (16+ parallel agents via GSD, team agents, etc.), 90%+ of tokens are invisible — e.g. showing ~40 tok/s when actual throughput is ~3,333 tok/s.
  • Fix: Scan subagents/ directories for all agent-*.jsonl files directly instead of requiring pre-discovered IDs. Also add subagent token aggregation to getTokenMetrics().

Changes

  1. getSubagentTranscriptPaths() — Remove referencedAgentIds parameter and the early-return gate. Scan candidate directories for all agent-*.jsonl files directly. This is safe because the subagents/ directory only contains files for the current session.

  2. getTokenMetrics() — After reading the main JSONL, also discover and read all subagent files, summing their token usage (input, output, cached).

  3. getSpeedMetricsCollection() — Remove the broken getReferencedSubagentIds(mainLines) call and use the simplified getSubagentTranscriptPaths(transcriptPath) directly.

Verified

Tested against real Claude Code sessions on Windows. Before patch: main JSONL had 0 agent IDs, 0 subagent files discovered. After patch: all subagent files found, ~48% more tokens counted in a 2-agent session. For 16+ agent sessions the difference is ~90%+.

Test plan

  • Verify speed widgets show combined main+agent tok/s during sessions with subagents
  • Verify token count widgets include subagent tokens
  • Verify backward compatibility when no agents are running (subagents/ dir doesn't exist)
  • Verify no double-counting (main JSONL has isSidechain: false only, subagent files have the agent data)

🤖 Generated with Claude Code

… of relying on main JSONL references

The subagent discovery mechanism in getSubagentTranscriptPaths() relied on
getReferencedSubagentIds() finding agentId fields in the main session JSONL.
However, the main JSONL contains zero entries with agentId — all agent IDs
exist only in the separate subagent files under {sessionDir}/subagents/.
This caused getReferencedSubagentIds() to return an empty set, making
getSubagentTranscriptPaths() short-circuit and return [], so no subagent
files were ever read.

This affected both speed metrics (tok/s showing only main-chain throughput)
and token counts (missing all subagent tokens). In sessions with heavy agent
usage (e.g. 16+ parallel agents), 90%+ of tokens were invisible.

Changes:
- Remove referencedAgentIds parameter from getSubagentTranscriptPaths() and
  scan subagents/ directories for all agent-*.jsonl files directly
- Add subagent token aggregation to getTokenMetrics() so total token counts
  include agent usage
- Simplify getSpeedMetricsCollection() to use directory-based discovery

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sirmalloc
Copy link
Copy Markdown
Owner

@neoeny152 I'm not sure this is necessary. After reviewing my session transcript JSONL files, I've found a 1:1 match of unique agentIds in the JSONL files and the subagent transcripts. Can you expand on your assertion that the main JSONL has no agentId fields in it, and provide a recent example with the latest Claude Code version?

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