Draws the current turn as a timeline in a pane (/timeline): model requests and tool calls per loop, where the time went, and the slowest steps

A pane that draws the current turn as a timeline, one lane per loop.
<img src="../docs/turn-timeline.png" alt="The turn-timeline pane with a timeline, time breakdown and slowest steps" width="520">
claude plugin marketplace add arasovic/claude-code-mods
claude plugin install turn-timeline@claude-code-mods
Restart Claude Code and type /timeline. It follows the running turn and keeps the last one after it ends.
claude plugin validate .
claude plugin test .
../typecheck.sh turn-timelinehooks/register.tsx 378 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { TimelineSpan, TimelineTurn } from '../types'
5
6const PANE = 'timeline'
7const LABEL_W = 10
8// Spans kept per turn; a long agentic turn keeps its latest ones.
9const KEEP = 400
10
11const turn = atom({ plugin: 'turn-timeline', key: 'turn' } as const, null as TimelineTurn | null)
12
13// Subagent ids never change type, so one lookup per id is enough.
14const agentTypes = new Map<string, string>()
15
16export const clock = (ms: number) => {
17 const s = Math.max(0, ms / 1000)
18 if (s < 10) return `${s.toFixed(1)}s`
19 if (s < 60) return `${Math.round(s)}s`
20 const r = Math.round(s)
21 return r % 60 ? `${Math.floor(r / 60)}m${r % 60}s` : `${r / 60}m`
22}
23
24// What a tool call is about, in a few words: the command, the file's name, the pattern, the URL.
25export const target = (input: Record<string, unknown>) => {
26 for (const key of ['command', 'file_path', 'path', 'pattern', 'url', 'description', 'query', 'prompt', 'skill']) {
27 const value = input[key]
28 if (typeof value !== 'string' || !value) continue
29 const text = key === 'file_path' || key === 'path' ? (value.split('/').pop() ?? value) : value
30 return text.replace(/\s+/g, ' ').trim()
31 }
32 return ''
33}
34
35const live = (s: TimelineSpan, now: number) => s.end ?? now
36
37// One lane's cells: a tool call wins a cell over a model request, and any overlap counts, so a short call still shows.
38export const laneCells = (spans: readonly TimelineSpan[], start: number, end: number, cols: number, now: number) => {
39 const step = Math.max(1, end - start) / cols
40 return Array.from({ length: cols }, (_, i) => {
41 const t0 = start + i * step
42 const t1 = t0 + step
43 const hits = spans.filter(s => s.start < t1 && live(s, now) > t0)
44 const tool = hits.find(s => s.kind === 'tool')
45 if (tool) return tool.ok === false ? 'failed' : 'tool'
46 return hits.length ? 'model' : 'idle'
47 })
48}
49
50// Where the main loop's wall-clock went, sampled: at each instant the oldest running tool, else a model request, else idle.
51export const breakdown = (spans: readonly TimelineSpan[], start: number, end: number, now: number, samples = 600) => {
52 const totals = new Map<string, number>()
53 const step = Math.max(1, end - start) / samples
54 for (let i = 0; i < samples; i++) {
55 const t = start + (i + 0.5) * step
56 const running = spans.filter(s => s.start <= t && live(s, now) > t)
57 const tool = running.filter(s => s.kind === 'tool').sort((a, b) => a.start - b.start)[0]
58 const key = tool ? tool.name : running.length ? 'model' : 'idle'
59 totals.set(key, (totals.get(key) ?? 0) + step)
60 }
61 return [...totals].map(([name, ms]) => ({ name, ms, pct: (ms / Math.max(1, end - start)) * 100 })).sort((a, b) => b.ms - a.ms)
62}
63
64// An axis row with a few round labels, each left-aligned at its column and never overlapping the last.
65export const axis = (duration: number, cols: number) => {
66 const STEPS = [1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 1800, 3600].map(s => s * 1000)
67 const step = STEPS.find(s => duration / s <= 3) ?? 3_600_000
68 let row = ''
69 for (let t = 0; t <= duration; t += step) {
70 const col = Math.round((t / Math.max(1, duration)) * (cols - 1))
71 const label = t === 0 ? '0' : clock(t)
72 if (col < row.length + 1 && row.length) continue
73 if (col + label.length > cols) break
74 row = row.padEnd(col) + label
75 }
76 return row
77}
78
79const agentLabel = async ($: EngineInterface, id: string) => {
80 // The engine's own forks (compaction, memory) carry ids no list names, so they are never cached.
81 const type = agentTypes.get(id) ?? (await $.agent.list()).find(a => a.id === id)?.type
82 if (type) agentTypes.set(id, type)
83 return type ?? 'fork'
84}
85
86// Opens a span on its loop's lane, adding the lane on first sight; returns the span id to close it with.
87const open = async ($: EngineInterface, agentId: string | undefined, kind: TimelineSpan['kind'], name: string, detail: string) => {
88 const at = await $.clock.now()
89 const lane = agentId ?? 'main'
90 const label = agentId ? await agentLabel($, agentId) : 'main'
91 const id = `${lane}:${kind}:${at}:${Math.random().toString(36).slice(2, 6)}`
92 await update($, turn, t => {
93 const base = t ?? { start: at, lanes: [{ id: 'main', label: 'main' }], spans: [] }
94 const same = base.lanes.filter(l => l.label === label || l.label.startsWith(`${label} `)).length
95 const lanes = base.lanes.some(l => l.id === lane) ? base.lanes : [...base.lanes, { id: lane, label: same ? `${label} ${same + 1}` : label }]
96 return { ...base, lanes, spans: [...base.spans, { id, lane, kind, name, detail, start: at }].slice(-KEEP) }
97 })
98 return id
99}
100
101const close = async ($: EngineInterface, id: string, ok?: boolean) => {
102 const end = await $.clock.now()
103 await update($, turn, t => (t ? { ...t, spans: t.spans.map(s => (s.id === id ? { ...s, end, ok } : s)) } : t))
104}
105
106// Rows are runs of styled text. The terminal draws them on a grid of cells, so frames pad and cut them exactly.
107// Other surfaces draw text in a proportional font, where spaces line nothing up: there a row is a flex line.
108// `fill` stretches, `w` is a column that many characters wide (`right` aligns it), `keep` never gives way
109// when the line runs short, and a Bar draws `parts` as real bars in place of its `cells`.
110type Seg = { t: string; c?: string; d?: boolean; b?: boolean; fill?: boolean; w?: number; right?: boolean; keep?: boolean }
111type Bar = { cells: Seg[]; parts: { n: number; c?: string }[] }
112type Row = (Seg | Bar)[]
113
114// The empty part of a bar where the surface draws real bars.
115const TRACK = 'userMessageBackgroundHover'
116
117const segs = (row: Row): Seg[] => row.flatMap(s => ('t' in s ? [s] : s.cells))
118
119const width = (row: Row) => segs(row).reduce((n, s) => n + s.t.length, 0)
120
121// Consecutive cells of one colour as one part of a real bar.
122const runs = (cells: readonly Seg[]) =>
123 cells.reduce<{ n: number; c?: string }[]>((out, s) => {
124 const last = out.at(-1)
125 if (last && last.c === s.c) last.n += s.t.length
126 else out.push({ n: s.t.length, c: s.c })
127 return out
128 }, [])
129
130const fit = (row: Row, w: number): Seg[] => {
131 const out: Seg[] = []
132 let n = 0
133 for (const s of segs(row)) {
134 const room = w - n
135 if (room <= 0) break
136 if (s.t.length <= room) {
137 out.push(s)
138 n += s.t.length
139 } else {
140 out.push({ ...s, t: `${s.t.slice(0, room - 1)}…` })
141 n = w
142 }
143 }
144 if (n < w) out.push({ t: ' '.repeat(w - n) })
145 return out
146}
147
148// The left half is cut first, so a long target never pushes the right half (a duration) off the edge.
149const spread = (left: Row, right: Row, w: number): Row => {
150 const room = Math.max(0, w - width(right) - 1)
151 const kept = width(left) > room ? fit(left, room) : left
152 return [...kept, { t: ' '.repeat(Math.max(1, w - width(kept) - width(right))), fill: true }, ...right]
153}
154
155// A name column that keeps a blank cell before what follows: "general-purpose" reads "general-…",
156// and a numbered lane keeps its number: "general-purpose 2" reads "genera… 2".
157const column = (text: string, w: number) => {
158 if (text.length < w) return text.padEnd(w)
159 const n = / \d+$/.exec(text)?.[0] ?? ''
160 return `${text.slice(0, w - 2 - n.length)}…${n} `
161}
162
163const CELL: Record<ReturnType<typeof laneCells>[number], Seg> = {
164 tool: { t: '█', c: 'success' },
165 failed: { t: '█', c: 'error' },
166 model: { t: '▒', c: 'suggestion' },
167 idle: { t: '·', d: true },
168}
169
170export const register: Register = on => {
171 on('session.start', async ($, e, next) => {
172 await $.command.register({ name: 'timeline', description: 'Show the current turn as a timeline of model requests and tool calls' })
173 return next(e)
174 })
175
176 on('command.run', { command: 'timeline' }, async $ => {
177 const opened = await $.ui.open({ id: PANE, title: 'Timeline' })
178 return { text: opened.isPlaced ? 'Timeline pane opened.' : `Timeline pane is waiting: ${opened.reason}` }
179 })
180
181 on('turn.start', async ($, e, next) => {
182 const at = await $.clock.now()
183 await update($, turn, () => ({ start: at, lanes: [{ id: 'main', label: 'main' }], spans: [] }))
184 return next(e)
185 })
186
187 on('turn.complete', async ($, e, next) => {
188 if (e.agentId === undefined) {
189 const at = await $.clock.now()
190 // Nothing of the main loop's own runs past its end: a step whose close never landed ends here.
191 await update($, turn, t => (t ? { ...t, end: at, spans: t.spans.map(s => (s.lane === 'main' && s.end === undefined ? { ...s, end: at } : s)) } : t))
192 }
193 return next(e)
194 })
195
196 on('tool.call', async ($, e, next) => {
197 const id = await open($, e.agentId, 'tool', e.tool.startsWith('mcp__') ? (e.tool.split('__').at(-1) ?? e.tool) : e.tool, target(e as unknown as Record<string, unknown>))
198 let ok = false
199 try {
200 const result = await next(e)
201 ok = result.deny === undefined && !result.isError
202 return result
203 } finally {
204 await close($, id, ok)
205 }
206 })
207
208 on('turn.step', async function* ($, e, next) {
209 let id: string | undefined
210 try {
211 id = await open($, e.agentId, 'model', 'model', '')
212 } catch {
213 // The timeline is a convenience: a failed write must never block the model's request.
214 }
215 try {
216 return yield* next(e)
217 } finally {
218 if (id) await close($, id).catch(() => undefined)
219 }
220 })
221
222 on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
223 const { Box, Text } = $.ui.resolve(e)
224 const t = await read($, turn)
225 const now = await $.clock.now()
226 const w = Math.max(30, e.props.bodyColumns - 1)
227 const inner = w - 6
228 const isGrid = e.surface === 'terminal'
229 if (!t) return <Text dimColor>The timeline starts with the next turn.</Text>
230
231 const draw = (row: Row, width: number) =>
232 fit(row, width).map((s, j) => <Text key={String(j)} color={s.c} dimColor={s.d} bold={s.b}>{s.t}</Text>)
233 // A row as a flex line, for surfaces that draw text in a proportional font.
234 const items = (row: Row) =>
235 row.map((s, j) => {
236 if ('cells' in s) {
237 const total = Math.max(1, s.parts.reduce((n, p) => n + p.n, 0))
238 return <Box flexGrow={1} height={0.5}>{s.parts.map(p => <Box flexGrow={(p.n / total) * 1000} backgroundColor={p.c ?? TRACK} />)}</Box>
239 }
240 if (s.fill) return <Box flexGrow={1} />
241 const text = <Text key={String(j)} color={s.c} dimColor={s.d} bold={s.b} wrap="truncate-end">{!s.w ? s.t : s.right ? s.t.trim() : s.t.trimEnd()}</Text>
242 return s.w || s.keep ? <Box width={s.w} flexShrink={0} justifyContent={s.right ? 'flex-end' : 'flex-start'}>{text}</Box> : text
243 })
244 const line = (row: Row) => (row.length ? <Box alignItems="center">{items(row)}</Box> : <Box height={0.5} />)
245 const frame = (title: string, tone: string, rows: Row[], right = '') => {
246 if (!isGrid)
247 return (
248 <Box flexDirection="column" marginTop={2} borderStyle="round" borderColor={tone} borderDimColor>
249 <Box marginBottom={1}>
250 <Box flexShrink={0}>
251 <Text color={tone} bold>{title}</Text>
252 </Box>
253 <Box flexGrow={1} />
254 {right ? <Text dimColor wrap="truncate-end">{right}</Text> : null}
255 </Box>
256 {rows.map(line)}
257 </Box>
258 )
259 const rule = Math.max(0, w - 6 - title.length - (right ? right.length + 2 : 0))
260 return (
261 <Box key={title} flexDirection="column" marginTop={1}>
262 <Text>
263 <Text color={tone} dimColor>╭─ </Text>
264 <Text color={tone} bold>{title}</Text>
265 <Text color={tone} dimColor> {'─'.repeat(rule)}</Text>
266 {right ? <Text dimColor> {right} </Text> : null}
267 <Text color={tone} dimColor>─╮</Text>
268 </Text>
269 {rows.map((row, i) => (
270 <Text key={String(i)}>
271 <Text color={tone} dimColor>│ </Text>
272 {draw(row, inner)}
273 <Text color={tone} dimColor> │</Text>
274 </Text>
275 ))}
276 <Text color={tone} dimColor>╰{'─'.repeat(w - 2)}╯</Text>
277 </Box>
278 )
279 }
280
281 // A background subagent outlives the main turn, so the timeline runs until its last step ends too.
282 const running = t.end === undefined || t.spans.some(s => s.end === undefined)
283 const end = running ? now : Math.max(t.end ?? now, ...t.spans.map(s => s.end ?? 0))
284 const duration = end - t.start
285 const cols = inner - LABEL_W
286 const main = t.spans.filter(s => s.lane === 'main')
287 const parts = breakdown(main, t.start, end, now)
288 const pct = (name: string) => Math.round(parts.find(p => p.name === name)?.pct ?? 0)
289 const toolPct = Math.max(0, 100 - pct('model') - pct('idle'))
290
291 const glance: Row = [
292 { t: running ? 'Running ' : 'Last turn ', d: true },
293 { t: clock(duration), b: true, keep: true },
294 { t: ' model ', d: true },
295 { t: `${pct('model')}%`, c: 'suggestion', b: true, keep: true },
296 { t: ' tools ', d: true },
297 { t: `${toolPct}%`, c: 'success', b: true, keep: true },
298 { t: ' idle ', d: true },
299 { t: `${pct('idle')}%`, b: true, keep: true },
300 ]
301
302 // One row per loop, the busiest first after main; lanes past the room left fold into a count.
303 const maxLanes = Math.max(1, Math.floor((e.props.scroll.bodyRows - 20) / 2))
304 const lanes = t.lanes.slice(0, maxLanes)
305 const laneRows: Row[] = lanes.flatMap((lane, i) => {
306 const cells = laneCells(
307 t.spans.filter(s => s.lane === lane.id),
308 t.start,
309 end,
310 cols,
311 now,
312 ).map(k => CELL[k])
313 const row: Row = [{ t: column(lane.label, LABEL_W), b: lane.id === 'main', d: lane.id !== 'main', w: LABEL_W }, { cells, parts: runs(cells) }]
314 return i ? [[], row] : [row]
315 })
316 const hidden = t.lanes.length - lanes.length
317 const timelineRows: Row[] = [
318 ...laneRows,
319 ...(hidden > 0 ? [[{ t: `+${hidden} more loops`, d: true }]] : []),
320 isGrid ? [{ t: ' '.repeat(LABEL_W) }, { t: axis(duration, cols), d: true }] : [{ t: '', w: LABEL_W }, { t: '0', d: true }, { t: '', fill: true }, { t: clock(duration), d: true }],
321 [],
322 isGrid
323 ? [{ t: '█', c: 'success' }, { t: ' tool ', d: true }, { t: '▒', c: 'suggestion' }, { t: ' model ', d: true }, { t: '█', c: 'error' }, { t: ' failed ', d: true }, { t: '·', d: true }, { t: ' idle', d: true }]
324 : [{ t: '■', c: 'success' }, { t: ' tool ', d: true }, { t: '■', c: 'suggestion' }, { t: ' model ', d: true }, { t: '■', c: 'error' }, { t: ' failed ', d: true }, { t: '□', d: true }, { t: ' idle', d: true }],
325 ]
326
327 // Each share as a bar against the turn's length, model and idle named, tools by name.
328 const barW = inner - 25
329 const shareRows: Row[] = parts.slice(0, 6).map(p => {
330 const filled = Math.round((p.pct / 100) * barW)
331 const tone = p.name === 'model' ? 'suggestion' : p.name === 'idle' ? undefined : 'success'
332 const left: Row = [
333 { t: column(p.name, LABEL_W), d: p.name === 'idle', w: LABEL_W },
334 {
335 cells: [{ t: '█'.repeat(filled), c: tone, d: p.name === 'idle' }, { t: '·'.repeat(Math.max(0, barW - filled)), d: true }],
336 parts: [{ n: filled, c: tone ?? 'promptBorder' }, { n: Math.max(0, barW - filled) }],
337 },
338 ]
339 const right: Row = [
340 { t: clock(p.ms).padStart(6), d: true, w: 6, right: true },
341 { t: `${Math.round(p.pct)}%`.padStart(5), b: true, w: 5, right: true },
342 ]
343 // A real bar stretches by itself; a spacer beside it would take half the room.
344 return isGrid ? spread(left, right, inner) : [...left, ...right]
345 })
346
347 const slowest = t.spans
348 .filter(s => s.end !== undefined)
349 .sort((a, b) => (b.end ?? 0) - b.start - ((a.end ?? 0) - a.start))
350 .slice(0, 3)
351 // A model request has no target of its own, so it is named by its place in its loop: "request 3 of 5".
352 const requestOf = (s: TimelineSpan) => {
353 const mine = t.spans.filter(x => x.lane === s.lane && x.kind === 'model')
354 return `request ${mine.indexOf(s) + 1} of ${mine.length}`
355 }
356 const slowRows: Row[] = slowest.length
357 ? slowest.map(s => {
358 const left: Row = [
359 { t: s.kind === 'model' ? 'model' : s.name, c: s.kind === 'model' ? 'suggestion' : s.ok === false ? 'error' : 'success', b: true, keep: true },
360 { t: ` ${s.lane === 'main' ? '' : `${t.lanes.find(l => l.id === s.lane)?.label ?? ''} `}${s.kind === 'model' ? requestOf(s) : s.detail}`, d: true },
361 ]
362 const took = clock((s.end ?? now) - s.start)
363 // Off the grid only the target gives way; the name and the duration stay whole.
364 return isGrid ? spread(left, [{ t: took, b: true }], inner) : [...left, { t: '', fill: true }, { t: ` ${took}`, b: true, keep: true }]
365 })
366 : [[{ t: 'Nothing has finished yet.', d: true }]]
367
368 return (
369 <Box flexDirection="column" paddingTop={1}>
370 {isGrid ? <Text>{draw(glance, w - 3)}</Text> : line(glance)}
371 {frame('Timeline', 'suggestion', timelineRows, `${t.lanes.length} ${t.lanes.length === 1 ? 'loop' : 'loops'}`)}
372 {frame('Where time went', 'success', shareRows, 'main loop')}
373 {frame('Slowest', 'warning', slowRows)}
374 </Box>
375 )
376 })
377}
378types/index.d.ts 12 lines1export type TimelineSpan = { id: string; lane: string; kind: 'model' | 'tool'; name: string; detail: string; start: number; end?: number; ok?: boolean }
2export type TimelineLane = { id: string; label: string }
3export type TimelineTurn = { start: number; end?: number; lanes: TimelineLane[]; spans: TimelineSpan[] }
4
5declare module 'claude-code' {
6 interface PluginState {
7 'turn-timeline': {
8 turn: TimelineTurn | null
9 }
10 }
11}
12