-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.ts
More file actions
489 lines (445 loc) · 21.6 KB
/
Copy pathtranscript.ts
File metadata and controls
489 lines (445 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import * as fs from 'fs';
import * as os from 'node:os';
import * as path from 'node:path';
import * as readline from 'readline';
import { createHash } from 'node:crypto';
import { getHudDir } from './config.ts';
import type { TranscriptData, ToolEntry, AgentEntry, TodoItem, WorkflowEntry } from './types.ts';
import { collectSpeed, outputTokensPerSec, inputTokensPerSec } from './speed-metrics.ts';
interface TranscriptLine {
timestamp?: string;
type?: string;
slug?: string;
customTitle?: string;
aiTitle?: string;
message?: { content?: ContentBlock[] };
// queue-operation fields (for async agent completion)
operation?: string;
content?: string;
// toolUseResult on tool_result entries
toolUseResult?: ToolUseResult;
}
interface ToolUseResult {
isAsync?: boolean;
status?: string;
agentId?: string;
agentType?: string;
description?: string;
totalDurationMs?: number;
outputFile?: string;
totalTokens?: number;
totalToolUseCount?: number;
}
interface ContentBlock {
type: string;
id?: string;
name?: string;
input?: Record<string, unknown>;
tool_use_id?: string;
is_error?: boolean;
}
interface TranscriptFileState { mtimeMs: number; size: number; }
interface SerializedToolEntry extends Omit<ToolEntry, 'startTime' | 'endTime'> { startTime: string; endTime?: string; }
interface SerializedAgentEntry extends Omit<AgentEntry, 'startTime' | 'endTime'> { startTime: string; endTime?: string; }
interface SerializedWorkflowEntry extends Omit<WorkflowEntry, 'startTime' | 'endTime'> { startTime?: string; endTime?: string; }
interface SerializedTranscriptData { tools: SerializedToolEntry[]; agents: SerializedAgentEntry[]; workflows?: SerializedWorkflowEntry[]; todos: TodoItem[]; sessionStart?: string; sessionName?: string; outputTokensPerSec?: number | null; inputTokensPerSec?: number | null; activeDurationMs?: number | null; }
interface TranscriptCacheFile { transcriptPath: string; transcriptState: TranscriptFileState; data: SerializedTranscriptData; }
function getCachePath(transcriptPath: string): string {
const hash = createHash('sha256').update(path.resolve(transcriptPath)).digest('hex');
return path.join(getHudDir(), 'transcript-cache', `${hash}.json`);
}
function readFileState(p: string): TranscriptFileState | null {
try { const s = fs.statSync(p); return s.isFile() ? { mtimeMs: s.mtimeMs, size: s.size } : null; } catch { return null; }
}
function serialize(data: TranscriptData): SerializedTranscriptData {
return {
tools: data.tools.map(t => ({ ...t, startTime: t.startTime.toISOString(), endTime: t.endTime?.toISOString() })),
agents: data.agents.map(a => ({ ...a, startTime: a.startTime.toISOString(), endTime: a.endTime?.toISOString() })),
workflows: data.workflows.map(w => ({ ...w, startTime: w.startTime?.toISOString(), endTime: w.endTime?.toISOString() })),
todos: data.todos.map(t => ({ ...t })),
sessionStart: data.sessionStart?.toISOString(),
sessionName: data.sessionName,
outputTokensPerSec: data.outputTokensPerSec ?? null,
inputTokensPerSec: data.inputTokensPerSec ?? null,
activeDurationMs: data.activeDurationMs ?? null,
};
}
function deserialize(data: SerializedTranscriptData): TranscriptData {
return {
tools: data.tools.map(t => ({ ...t, startTime: new Date(t.startTime), endTime: t.endTime ? new Date(t.endTime) : undefined })),
agents: data.agents.map(a => ({ ...a, startTime: new Date(a.startTime), endTime: a.endTime ? new Date(a.endTime) : undefined })),
workflows: (data.workflows ?? []).map(w => ({ ...w, startTime: w.startTime ? new Date(w.startTime) : undefined, endTime: w.endTime ? new Date(w.endTime) : undefined })),
todos: data.todos.map(t => ({ ...t })),
sessionStart: data.sessionStart ? new Date(data.sessionStart) : undefined,
sessionName: data.sessionName,
outputTokensPerSec: data.outputTokensPerSec ?? null,
inputTokensPerSec: data.inputTokensPerSec ?? null,
activeDurationMs: data.activeDurationMs ?? null,
};
}
function readCache(transcriptPath: string, state: TranscriptFileState): TranscriptData | null {
try {
const raw = fs.readFileSync(getCachePath(transcriptPath), 'utf8');
const parsed = JSON.parse(raw) as TranscriptCacheFile;
if (parsed.transcriptPath !== path.resolve(transcriptPath) || parsed.transcriptState?.mtimeMs !== state.mtimeMs || parsed.transcriptState?.size !== state.size) return null;
return deserialize(parsed.data);
} catch { return null; }
}
function writeCache(transcriptPath: string, state: TranscriptFileState, data: TranscriptData): void {
try {
const cachePath = getCachePath(transcriptPath);
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
fs.writeFileSync(cachePath, JSON.stringify({ transcriptPath: path.resolve(transcriptPath), transcriptState: state, data: serialize(data) }), 'utf8');
pruneCacheDir(path.dirname(cachePath));
} catch {}
}
const PRUNE_MARKER = '.last-prune';
const PRUNE_INTERVAL_MS = 24 * 60 * 60 * 1000;
const MAX_CACHE_AGE_MS = 14 * 24 * 60 * 60 * 1000;
/** Delete cache entries for long-dead sessions. Throttled via a marker file
* so the directory scan runs at most once a day across all hud invocations. */
function pruneCacheDir(dir: string): void {
try {
const marker = path.join(dir, PRUNE_MARKER);
const now = Date.now();
try {
if (now - fs.statSync(marker).mtimeMs < PRUNE_INTERVAL_MS) return;
} catch {}
fs.writeFileSync(marker, '', 'utf8');
for (const name of fs.readdirSync(dir)) {
if (!name.endsWith('.json')) continue;
const p = path.join(dir, name);
try {
if (now - fs.statSync(p).mtimeMs > MAX_CACHE_AGE_MS) fs.unlinkSync(p);
} catch {}
}
} catch {}
}
export async function parseTranscript(transcriptPath: string): Promise<TranscriptData> {
const result: TranscriptData = { tools: [], agents: [], workflows: [], todos: [] };
if (!transcriptPath || !fs.existsSync(transcriptPath)) return result;
const state = readFileState(transcriptPath);
if (!state) return result;
const cached = readCache(transcriptPath, state);
if (cached && !cached.agents.some(a => a.status === 'running') && !cached.workflows.some(w => w.status === 'running')) return cached;
const toolMap = new Map<string, ToolEntry>();
const agentMap = new Map<string, AgentEntry>();
// Maps agentId (from toolUseResult) back to tool_use_id for async agents
const asyncAgentIdToToolId = new Map<string, string>();
// Buffers completion events whose async-launch tool_result hasn't been seen yet.
// The JSONL is appended per-event, but assistant-turn entries are flushed in batches —
// a fast subagent can have its queue-operation completion written BEFORE the
// tool_use/tool_result that launched it. Without this buffer, those completions are dropped.
const pendingCompletions = new Map<string, Date>();
let latestTodos: TodoItem[] = [];
const taskIdToIndex = new Map<string, number>();
let latestSlug: string | undefined;
let aiTitle: string | undefined;
let customTitle: string | undefined;
let parsedCleanly = false;
try {
const rl = readline.createInterface({ input: fs.createReadStream(transcriptPath), crlfDelay: Infinity });
for await (const line of rl) {
if (!line.trim()) continue;
try {
const entry = JSON.parse(line) as TranscriptLine;
if (entry.type === 'custom-title' && typeof entry.customTitle === 'string') customTitle = entry.customTitle;
else if (entry.type === 'ai-title' && typeof entry.aiTitle === 'string') aiTitle = entry.aiTitle;
else if (typeof entry.slug === 'string') latestSlug = entry.slug;
processEntry(entry, toolMap, agentMap, asyncAgentIdToToolId, pendingCompletions, taskIdToIndex, latestTodos, result);
} catch {}
}
parsedCleanly = true;
} catch {}
result.tools = Array.from(toolMap.values()).slice(-20);
result.agents = Array.from(agentMap.values()).slice(-10);
result.todos = latestTodos;
result.sessionName = customTitle ?? aiTitle ?? latestSlug;
// Enrich unknown agent types from subagent meta.json files
enrichAgentTypes(transcriptPath, result.agents, asyncAgentIdToToolId);
// Collect speeds: main session + each subagent
await attachSpeeds(transcriptPath, result);
// Aggregate harness-orchestrated workflow runs (Workflow tool fleets)
result.workflows = await collectWorkflows(transcriptPath);
const hasRunning = result.agents.some(a => a.status === 'running') || result.workflows.some(w => w.status === 'running');
if (parsedCleanly && !hasRunning) writeCache(transcriptPath, state, result);
return result;
}
const MAX_WORKFLOWS_SHOWN = 2;
/**
* Workflow agents don't appear as Agent tool_use blocks in the main transcript —
* the harness orchestrates them under subagents/workflows/<runId>/. Aggregate
* each run into one entry from its journal (started/result pairs) and the
* per-agent JSONLs (timing + cost).
*/
async function collectWorkflows(transcriptPath: string): Promise<WorkflowEntry[]> {
const transcriptDir = path.dirname(transcriptPath);
const transcriptStem = path.basename(transcriptPath, '.jsonl');
const wfRoot = path.join(transcriptDir, transcriptStem, 'subagents', 'workflows');
let runDirs: Array<{ name: string; mtimeMs: number }> = [];
try {
runDirs = fs.readdirSync(wfRoot)
.map(name => { try { const st = fs.statSync(path.join(wfRoot, name)); return st.isDirectory() ? { name, mtimeMs: st.mtimeMs } : null; } catch { return null; } })
.filter((d): d is { name: string; mtimeMs: number } => d !== null)
.sort((a, b) => b.mtimeMs - a.mtimeMs)
.slice(0, MAX_WORKFLOWS_SHOWN);
} catch { return []; }
const entries: WorkflowEntry[] = [];
for (const dir of runDirs) {
const entry = await collectWorkflowRun(transcriptDir, transcriptStem, path.join(wfRoot, dir.name), dir.name);
if (entry) entries.push(entry);
}
return entries.reverse(); // oldest first, matching agent ordering
}
async function collectWorkflowRun(transcriptDir: string, transcriptStem: string, runDir: string, runId: string): Promise<WorkflowEntry | null> {
let started = 0;
let completed = 0;
try {
const journal = fs.readFileSync(path.join(runDir, 'journal.jsonl'), 'utf8');
for (const line of journal.split('\n')) {
if (!line.trim()) continue;
try {
const e = JSON.parse(line) as { type?: string };
if (e.type === 'started') started++;
else if (e.type === 'result') completed++;
} catch {}
}
} catch { return null; }
if (started === 0) return null;
// Per-agent JSONLs give wall-clock window, cost, models, and output volume
let firstMs: number | null = null;
let lastMs: number | null = null;
let costUsd = 0;
let hasCost = false;
let outputTokens = 0;
const models = new Set<string>();
try {
const agentFiles = fs.readdirSync(runDir).filter(f => f.startsWith('agent-') && f.endsWith('.jsonl'));
await Promise.all(agentFiles.map(async f => {
const m = await collectSpeed(path.join(runDir, f));
if (m.firstEventMs !== null) firstMs = firstMs === null ? m.firstEventMs : Math.min(firstMs, m.firstEventMs);
if (m.lastEventMs !== null) lastMs = lastMs === null ? m.lastEventMs : Math.max(lastMs, m.lastEventMs);
if (typeof m.costUsd === 'number') { costUsd += m.costUsd; hasCost = true; }
outputTokens += m.outputTokens;
if (m.model) models.add(m.model);
}));
} catch {}
const running = started > completed;
const elapsedMs = firstMs !== null ? (running ? Date.now() : (lastMs ?? firstMs)) - firstMs : 0;
return {
runId,
name: findWorkflowName(transcriptDir, transcriptStem, runId) ?? runId,
agentCount: started,
completedCount: completed,
status: running ? 'running' : 'completed',
startTime: firstMs !== null ? new Date(firstMs) : undefined,
endTime: !running && lastMs !== null ? new Date(lastMs) : undefined,
costUsd: hasCost ? costUsd : null,
model: models.size === 1 ? Array.from(models)[0] : (models.size > 1 ? 'mixed' : null),
outputTokens,
outputTokensPerSec: elapsedMs > 0 && outputTokens > 0 ? outputTokens / (elapsedMs / 1000) : null,
};
}
/** The script file is saved as workflows/scripts/<name>-<runId>.js next to the run. */
function findWorkflowName(transcriptDir: string, transcriptStem: string, runId: string): string | null {
try {
const scriptsDir = path.join(transcriptDir, transcriptStem, 'workflows', 'scripts');
const match = fs.readdirSync(scriptsDir).find(f => f.endsWith(`-${runId}.js`));
return match ? match.slice(0, -(`-${runId}.js`.length)) : null;
} catch { return null; }
}
async function attachSpeeds(transcriptPath: string, result: TranscriptData): Promise<void> {
// Main session speed
const mainMetrics = await collectSpeed(transcriptPath);
result.outputTokensPerSec = outputTokensPerSec(mainMetrics);
result.inputTokensPerSec = inputTokensPerSec(mainMetrics);
result.activeDurationMs = mainMetrics.activeDurationMs > 0 ? mainMetrics.activeDurationMs : null;
// Per-subagent speeds
const transcriptDir = path.dirname(transcriptPath);
const transcriptStem = path.basename(transcriptPath, '.jsonl');
const subagentsDir = path.join(transcriptDir, transcriptStem, 'subagents');
if (!fs.existsSync(subagentsDir)) return;
await Promise.all(result.agents.map(async agent => {
if (!agent.agentId) return;
const subPath = path.join(subagentsDir, `agent-${agent.agentId}.jsonl`);
if (!fs.existsSync(subPath)) return;
const m = await collectSpeed(subPath);
agent.outputTokensPerSec = outputTokensPerSec(m);
// The model the agent actually ran on beats the requested override from input.model
if (m.model) agent.model = m.model;
agent.costUsd = m.costUsd;
}));
}
function processEntry(
entry: TranscriptLine,
toolMap: Map<string, ToolEntry>,
agentMap: Map<string, AgentEntry>,
asyncAgentIdToToolId: Map<string, string>,
pendingCompletions: Map<string, Date>,
taskIdToIndex: Map<string, number>,
latestTodos: TodoItem[],
result: TranscriptData,
): void {
const timestamp = entry.timestamp ? new Date(entry.timestamp) : new Date();
if (!result.sessionStart && entry.timestamp) result.sessionStart = timestamp;
// Handle queue-operation: async agent completion notification
if (entry.type === 'queue-operation' && entry.operation === 'enqueue' && typeof entry.content === 'string') {
const taskIdMatch = entry.content.match(/<task-id>([^<]+)<\/task-id>/);
const statusMatch = entry.content.match(/<status>([^<]+)<\/status>/);
if (taskIdMatch && statusMatch?.[1] === 'completed') {
const agentId = taskIdMatch[1];
const toolId = asyncAgentIdToToolId.get(agentId);
if (toolId) {
const agent = agentMap.get(toolId);
if (agent) {
agent.status = 'completed';
agent.endTime = timestamp;
}
} else {
// Completion arrived before the tool_result that establishes the agentId↔toolId
// mapping. Buffer it; the tool_result branch below will drain pending entries.
pendingCompletions.set(agentId, timestamp);
}
}
return;
}
const content = entry.message?.content;
if (!content || !Array.isArray(content)) return;
for (const block of content) {
if (block.type === 'tool_use' && block.id && block.name) {
const toolEntry: ToolEntry = { id: block.id, name: block.name, target: extractTarget(block.name, block.input), status: 'running', startTime: timestamp };
if (block.name === 'Agent' || block.name === 'Task') {
const input = block.input as Record<string, unknown>;
agentMap.set(block.id, {
id: block.id,
type: (input?.subagent_type as string) ?? 'unknown',
model: input?.model as string,
description: input?.description as string,
status: 'running',
startTime: timestamp,
});
} else if (block.name === 'TodoWrite') {
const input = block.input as { todos?: TodoItem[] };
if (input?.todos && Array.isArray(input.todos)) { latestTodos.length = 0; taskIdToIndex.clear(); latestTodos.push(...input.todos); }
} else if (block.name === 'TaskCreate') {
const input = block.input as Record<string, unknown>;
const c = (typeof input?.subject === 'string' ? input.subject : '') || (typeof input?.description === 'string' ? input.description : '') || 'Untitled task';
const status = normalizeStatus(input?.status) ?? 'pending';
latestTodos.push({ content: c, status });
const tid = typeof input?.taskId === 'string' || typeof input?.taskId === 'number' ? String(input.taskId) : block.id;
if (tid) taskIdToIndex.set(tid, latestTodos.length - 1);
} else if (block.name === 'TaskUpdate') {
const input = block.input as Record<string, unknown>;
const index = resolveIdx(input?.taskId, taskIdToIndex, latestTodos);
if (index !== null) {
const s = normalizeStatus(input?.status); if (s) latestTodos[index].status = s;
const c = (typeof input?.subject === 'string' ? input.subject : '') || (typeof input?.description === 'string' ? input.description : '');
if (c) latestTodos[index].content = c;
}
} else {
toolMap.set(block.id, toolEntry);
}
}
if (block.type === 'tool_result' && block.tool_use_id) {
const tur = entry.toolUseResult;
// Handle agent tool_results
const agent = agentMap.get(block.tool_use_id);
if (agent && tur) {
if (tur.agentId) agent.agentId = tur.agentId;
if (tur.isAsync === true) {
// Background agent: tool_result is just the launch notification.
// Keep status as 'running'. Completion comes via queue-operation.
if (tur.agentId) {
asyncAgentIdToToolId.set(tur.agentId, block.tool_use_id);
// If the completion event was already seen (file order can put it before
// this tool_result), drain it now.
const pendingTs = pendingCompletions.get(tur.agentId);
if (pendingTs) {
agent.status = 'completed';
agent.endTime = pendingTs;
pendingCompletions.delete(tur.agentId);
}
}
} else if (tur.status === 'completed') {
// Foreground agent: tool_result means truly completed.
agent.status = 'completed';
agent.endTime = timestamp;
// Use accurate duration from toolUseResult if available
if (typeof tur.totalDurationMs === 'number') {
agent.endTime = new Date(agent.startTime.getTime() + tur.totalDurationMs);
}
// Update type from toolUseResult (more accurate than input.subagent_type)
if (tur.agentType) agent.type = tur.agentType;
if (typeof tur.totalTokens === 'number') agent.totalTokens = tur.totalTokens;
if (typeof tur.totalToolUseCount === 'number') agent.totalToolUseCount = tur.totalToolUseCount;
}
continue;
}
// Handle regular tool_results
const tool = toolMap.get(block.tool_use_id);
if (tool) { tool.status = block.is_error ? 'error' : 'completed'; tool.endTime = timestamp; }
}
}
}
function extractTarget(name: string, input?: Record<string, unknown>): string | undefined {
if (!input) return undefined;
switch (name) {
case 'Read': case 'Write': case 'Edit': return (input.file_path as string) ?? (input.path as string);
case 'NotebookEdit': return input.notebook_path as string;
case 'Glob': case 'Grep': return input.pattern as string;
case 'Skill': return input.skill as string;
case 'ToolSearch': case 'WebSearch': return input.query as string;
case 'WebFetch': return input.url as string;
case 'Bash': { const cmd = input.command as string; return cmd ? cmd.slice(0, 30) + (cmd.length > 30 ? '...' : '') : undefined; }
}
return undefined;
}
function resolveIdx(taskId: unknown, map: Map<string, number>, todos: TodoItem[]): number | null {
if (typeof taskId === 'string' || typeof taskId === 'number') {
const key = String(taskId);
const mapped = map.get(key);
if (typeof mapped === 'number') return mapped;
if (/^\d+$/.test(key)) { const i = Number.parseInt(key, 10) - 1; if (i >= 0 && i < todos.length) return i; }
}
return null;
}
/**
* For agents with type 'unknown' (typically background agents where input.subagent_type
* was not set), read the agentType from subagents/agent-{id}.meta.json.
*/
function enrichAgentTypes(
transcriptPath: string,
agents: AgentEntry[],
asyncAgentIdToToolId: Map<string, string>,
): void {
const needsEnrichment = agents.some(a => a.type === 'unknown');
if (!needsEnrichment) return;
const transcriptDir = path.dirname(transcriptPath);
const transcriptStem = path.basename(transcriptPath, '.jsonl');
const subagentsDir = path.join(transcriptDir, transcriptStem, 'subagents');
if (!fs.existsSync(subagentsDir)) return;
// Build reverse map: tool_use_id → agentId (file-system id)
const toolIdToAgentId = new Map<string, string>();
for (const [agentId, toolId] of asyncAgentIdToToolId) {
toolIdToAgentId.set(toolId, agentId);
}
for (const agent of agents) {
if (agent.type !== 'unknown') continue;
const fileAgentId = toolIdToAgentId.get(agent.id);
if (!fileAgentId) continue;
try {
const metaPath = path.join(subagentsDir, `agent-${fileAgentId}.meta.json`);
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
if (meta.agentType) agent.type = meta.agentType;
} catch {}
}
}
function normalizeStatus(s: unknown): TodoItem['status'] | null {
if (typeof s !== 'string') return null;
switch (s) {
case 'pending': case 'not_started': return 'pending';
case 'in_progress': case 'running': return 'in_progress';
case 'completed': case 'complete': case 'done': return 'completed';
default: return null;
}
}