#!/usr/bin/env node /* eslint-disable */ /** * analyze-sessions.js * * Scans ~/.claude/projects/**.jsonl transcript files and reports token usage, * message counts, runtime, cache breaks, subagent and skill activity. * * Output is human-readable text by default; pass --json for machine-readable. * * Usage: * node scripts/analyze-sessions.js [--dir ] [--json] [--since ] [--top N] * * Notes on JSONL structure (discovered empirically): * - One API response is split into MULTIPLE `type:"assistant"` entries (one per * content block). They share the same `requestId` / `message.id`, and only the * LAST one carries the final `output_tokens`. We dedupe by requestId and keep * the max output_tokens to avoid 3-10x overcounting. * - `type:"user"` entries include tool_result messages, interrupt markers, * compact summaries and meta-injected text. A "human" message is one where * isSidechain/isMeta/isCompactSummary are falsy and the content is a plain * string (or text block) that isn't a tool_result or interrupt marker. * - Subagent transcripts live in //subagents/*.jsonl with a * sibling *.meta.json containing {agentType}. When meta is absent we fall back * to the filename label (`agent-a