parse-session.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env node
  2. // Parse the newest Claude Code session log for a project + its subagent logs,
  3. // and report the tool-call breakdown (main + subagents). Works for interactive
  4. // runs (driven via itrun.sh) — Claude Code writes full transcripts to
  5. // ~/.claude/projects/<escaped-cwd>/<session>.jsonl with subagents/ alongside.
  6. import { readFileSync, readdirSync, statSync, existsSync, realpathSync } from 'fs';
  7. import { join } from 'path';
  8. import { homedir } from 'os';
  9. const projectArg = process.argv[2];
  10. if (!projectArg) { console.error('usage: parse-session.mjs <project-dir>'); process.exit(1); }
  11. // Claude Code escapes the (real) cwd by replacing every "/" with "-".
  12. const real = realpathSync(projectArg);
  13. const escaped = real.replace(/\//g, '-');
  14. const projDir = join(homedir(), '.claude', 'projects', escaped);
  15. if (!existsSync(projDir)) { console.error('no session logs at', projDir); process.exit(1); }
  16. // Newest top-level session .jsonl
  17. const sessions = readdirSync(projDir)
  18. .filter(f => f.endsWith('.jsonl'))
  19. .map(f => ({ f, m: statSync(join(projDir, f)).mtimeMs }))
  20. .sort((a, b) => b.m - a.m);
  21. if (sessions.length === 0) { console.error('no .jsonl sessions in', projDir); process.exit(1); }
  22. const sessionId = sessions[0].f.replace('.jsonl', '');
  23. function tally(file) {
  24. const counts = {};
  25. for (const line of readFileSync(file, 'utf8').split('\n')) {
  26. if (!line) continue;
  27. let ev; try { ev = JSON.parse(line); } catch { continue; }
  28. const content = ev.message?.content;
  29. if (!Array.isArray(content)) continue;
  30. for (const b of content) {
  31. if (b.type === 'tool_use') counts[b.name] = (counts[b.name] || 0) + 1;
  32. }
  33. }
  34. return counts;
  35. }
  36. // Sum token usage from a transcript. The TUI's "Done (…Xk tokens…)" line only
  37. // covers a subagent's throughput; this works for main-thread runs too and is
  38. // consistent across both paths. `gen` = output, `fresh` = uncached input
  39. // (input + cache_creation), `cached` = cache reads (≈free), `total` = all.
  40. function sumTokens(file) {
  41. const t = { gen: 0, fresh: 0, cached: 0 };
  42. for (const line of readFileSync(file, 'utf8').split('\n')) {
  43. if (!line) continue;
  44. let ev; try { ev = JSON.parse(line); } catch { continue; }
  45. const u = ev.message?.usage;
  46. if (!u) continue;
  47. t.gen += u.output_tokens || 0;
  48. t.fresh += (u.input_tokens || 0) + (u.cache_creation_input_tokens || 0);
  49. t.cached += u.cache_read_input_tokens || 0;
  50. }
  51. return t;
  52. }
  53. const mainCounts = tally(join(projDir, sessionId + '.jsonl'));
  54. // Subagent transcripts live under <session>/subagents/*.jsonl
  55. const subDir = join(projDir, sessionId, 'subagents');
  56. const subCounts = {};
  57. let subAgentFiles = 0;
  58. if (existsSync(subDir)) {
  59. for (const f of readdirSync(subDir).filter(f => f.endsWith('.jsonl'))) {
  60. subAgentFiles++;
  61. const c = tally(join(subDir, f));
  62. for (const [k, v] of Object.entries(c)) subCounts[k] = (subCounts[k] || 0) + v;
  63. }
  64. }
  65. const fmt = (counts) => Object.entries(counts).sort((a, b) => b[1] - a[1])
  66. .map(([k, v]) => ` ${String(v).padStart(3)} ${k}`).join('\n') || ' (none)';
  67. console.log(`session: ${sessionId}`);
  68. console.log(`\nMAIN thread tools:\n${fmt(mainCounts)}`);
  69. console.log(`\nSUBAGENT tools (${subAgentFiles} subagent transcript${subAgentFiles === 1 ? '' : 's'}):\n${fmt(subCounts)}`);
  70. const explore = subCounts['mcp__codegraph__codegraph_explore'] || mainCounts['mcp__codegraph__codegraph_explore'] || 0;
  71. const reads = (subCounts['Read'] || 0) + (mainCounts['Read'] || 0);
  72. const greps = (subCounts['Grep'] || 0) + (mainCounts['Grep'] || 0) + (subCounts['Bash'] || 0) + (mainCounts['Bash'] || 0);
  73. console.log(`\nVERDICT: codegraph_explore used ${explore}x | Read ${reads} | Grep/Bash ${greps}`);
  74. // Token totals (main + subagents), consistent across main-thread and subagent runs.
  75. const tok = { gen: 0, fresh: 0, cached: 0 };
  76. const addTok = (t) => { tok.gen += t.gen; tok.fresh += t.fresh; tok.cached += t.cached; };
  77. addTok(sumTokens(join(projDir, sessionId + '.jsonl')));
  78. if (existsSync(subDir)) {
  79. for (const f of readdirSync(subDir).filter(f => f.endsWith('.jsonl'))) addTok(sumTokens(join(subDir, f)));
  80. }
  81. const k = (n) => (n / 1000).toFixed(1) + 'k';
  82. console.log(`TOKENS: gen ${k(tok.gen)} | fresh-in ${k(tok.fresh)} | cached-in ${k(tok.cached)} | billable≈ ${k(tok.gen + tok.fresh)}`);