#!/usr/bin/env bun // --------------------------------------------------------------------------- // streamline-retro / digest.ts — collapse a claude stream-json session log into // a readable timeline. // // bun digest.ts [--out timeline.md] [--full] // // Purely local file processing: no network, no DB. A 3-5 MB session.jsonl is // ~95% base64 thinking signatures and repeated tool output; the timeline is the // 1% you actually read when asking "what did the agent do, and where did it go // wrong". Errors are hoisted to the top so the failure point is the first thing // visible. Use --full to keep untruncated tool inputs/outputs. // --------------------------------------------------------------------------- import { basename } from "node:path"; const args = process.argv.slice(2); const file = args.find((a) => !a.startsWith("--")); if (!file) { console.error("usage: bun digest.ts [--out timeline.md] [--full]"); process.exit(2); } const full = args.includes("--full"); const outIdx = args.indexOf("--out"); const outFile = outIdx >= 0 ? args[outIdx + 1] : file.replace(/\.jsonl$/, "") + "-timeline.md"; const CAP_IN = full ? 100_000 : 300; // tool_use input const CAP_OUT = full ? 100_000 : 220; // tool_result output const CAP_ERR = full ? 100_000 : 1_500; // errored tool_result const CAP_TEXT = full ? 100_000 : 1_200; // assistant prose const oneLine = (s: string, cap: number) => { const t = String(s).replace(/\s+/g, " ").trim(); return t.length > cap ? t.slice(0, cap) + ` …(+${t.length - cap} chars)` : t; }; const block = (s: string, cap: number) => { const t = String(s).replace(/\r/g, "").trim(); return t.length > cap ? t.slice(0, cap) + `\n…(+${t.length - cap} chars, rerun with --full)` : t; }; /** One-line gist of a tool call — the arg that identifies *which* call it was. */ function toolGist(name: string, input: any): string { if (!input || typeof input !== "object") return ""; const pick = (...keys: string[]) => { for (const k of keys) if (typeof input[k] === "string" && input[k]) return input[k]; return ""; }; switch (name) { case "Bash": return oneLine(pick("command"), CAP_IN); case "Read": case "Write": case "NotebookEdit": return pick("file_path", "notebook_path"); case "Edit": return `${pick("file_path")} ⟨${oneLine(pick("old_string"), 60)}⟩`; case "Grep": return `${oneLine(pick("pattern"), 80)} in ${pick("path", "glob") || "."}`; case "Glob": return pick("pattern"); case "Task": case "Agent": return oneLine(pick("description", "prompt"), CAP_IN); case "TodoWrite": return oneLine(JSON.stringify(input.todos ?? input), 160); default: return oneLine(JSON.stringify(input), CAP_IN); } } const lines = (await Bun.file(file).text()).split("\n").filter((l) => l.trim()); const out: string[] = []; const errors: string[] = []; const statuses: string[] = []; const toolCounts = new Map(); const pendingTool = new Map(); // tool_use_id -> "Name gist" let sessions = 0; let events = 0; // The init event carries an empty `model` in stream mode; the first assistant // message is where the real model id shows up, so patch the header line later. let modelLineIdx = -1; let model = ""; for (const raw of lines) { events++; let ev: any; try { ev = JSON.parse(raw); } catch { out.push(`[#${events}] ⚠️ unparseable line`); continue; } const tag = `[#${events}]`; if (ev.type === "system" && ev.subtype === "init") { sessions++; model = ev.model || ""; out.push("", `## ── session ${sessions} · model=\`${model || "?"}\` · cwd=\`${ev.cwd || "?"}\` · sid=\`${ev.session_id || "?"}\``, ""); modelLineIdx = out.length - 2; continue; } if (ev.type === "assistant" && !model && ev.message?.model && modelLineIdx >= 0) { model = ev.message.model; out[modelLineIdx] = out[modelLineIdx].replace("model=`?`", `model=\`${model}\``); } if (ev.type === "result") { const dur = ev.duration_ms ? `${(ev.duration_ms / 60000).toFixed(1)}min` : "?"; const cost = ev.total_cost_usd != null ? `$${Number(ev.total_cost_usd).toFixed(2)}` : "?"; out.push("", `## ── result · subtype=\`${ev.subtype || "?"}\` · turns=${ev.num_turns ?? "?"} · ${dur} · ${cost}${ev.is_error ? " · **is_error**" : ""}`); if (ev.result) out.push("", "```", block(ev.result, full ? 100_000 : 3_000), "```"); continue; } const content = ev.message?.content; if (!Array.isArray(content)) continue; for (const c of content) { if (c.type === "thinking") { // Signatures are noise; only the (usually empty in stream mode) text matters. const t = String(c.thinking || "").trim(); if (t) out.push(`${tag} 💭 ${block(t, CAP_TEXT)}`); continue; } if (c.type === "text") { const t = String(c.text || "").trim(); if (!t) continue; out.push(`${tag} 💬 ${block(t, CAP_TEXT)}`); for (const m of t.matchAll(/STATUS:\s*([A-Z_]+)/g)) statuses.push(`${tag} STATUS: ${m[1]}`); continue; } if (c.type === "tool_use") { const gist = toolGist(c.name, c.input); toolCounts.set(c.name, (toolCounts.get(c.name) ?? 0) + 1); pendingTool.set(c.id, `${c.name}${gist ? " " + oneLine(gist, 120) : ""}`); out.push(`${tag} 🔧 **${c.name}** \`${gist.replace(/`/g, "'")}\``); continue; } if (c.type === "tool_result") { const body = typeof c.content === "string" ? c.content : Array.isArray(c.content) ? c.content.map((x: any) => x?.text ?? `[${x?.type}]`).join("\n") : ""; const who = pendingTool.get(c.tool_use_id) ?? "tool"; if (c.is_error) { const rendered = `${tag} ❌ ${who}\n\`\`\`\n${block(body, CAP_ERR)}\n\`\`\``; out.push(rendered); errors.push(rendered); } else { out.push(`${tag} ↩︎ ${oneLine(body, CAP_OUT) || "(empty)"}`); } continue; } } } const tools = [...toolCounts.entries()].sort((a, b) => b[1] - a[1]).map(([n, c]) => `${n}×${c}`).join(", "); const header = [ `# session timeline — ${basename(file)}`, "", `- events: **${events}** · sessions(rounds): **${sessions}** · errored tool calls: **${errors.length}**`, `- tools: ${tools || "(none)"}`, statuses.length ? `- status sentinels: ${statuses.join(" · ")}` : `- status sentinels: (none found)`, "", ]; if (errors.length) { header.push(`## ❌ errored tool calls (${errors.length}) — read these first`, "", ...errors.slice(0, 40), ""); if (errors.length > 40) header.push(`…(+${errors.length - 40} more, see timeline below)`, ""); } header.push("---", "", "# timeline"); await Bun.write(outFile, header.concat(out).join("\n") + "\n"); console.log(`wrote ${outFile} (${events} events, ${sessions} rounds, ${errors.length} errors, tools: ${tools || "none"})`);