Progress bars above the prompt for any long task that writes ~/.local/state/agent-progress/<id>.json

A Claude Code mod that draws a progress bar above the prompt for every long task that reports one, from any thread, script or agent.
Report with bin/progress (link it onto your PATH):
progress kernel 812 4500 "Pixel 4a kernel" "compiling"
progress kernel pct 40
progress kernel done
progress kernel clear
It writes ~/.local/state/agent-progress/<id>.json (label, current, total, percent, phase, state, updatedAt, all optional), so anything that can write a file can report. The mod polls that folder every 2 s, hides a running task silent for 15 minutes and a finished one after a minute, and passes the band on to other plugins' bands.
It also shows, with no reporting needed:
bg rows with running time, green or red when it ends);/clip saves the image on the clipboard (a screenshot) to ~/.local/state/clipboard-shots/ and tells the model to read it.
hooks/register.tsx 249 lines1import { atom, read, update } from 'claude-code'
2import type { Register } from 'claude-code'
3
4import type { Activity, BackgroundTask, Task } from '../types'
5
6// Any process reports a task by writing <DIR>/<id>.json:
7// { "label": "kernel build", "current": 812, "total": 4500, "percent": 18, "phase": "3/6 compiling",
8// "state": "running" | "done" | "failed", "updatedAt": <epoch ms> } every field optional.
9const DIR = '.local/state/agent-progress'
10const CLIP_DIR = '.local/state/clipboard-shots'
11const POLL_MS = 2000
12const TICK_MS = 5000
13// A running task that has not written for this long is assumed dead and hidden.
14const STALE_MS = 15 * 60e3
15// A finished task stays on screen this long.
16const DONE_SHOWN_MS = 60e3
17// A turn with no tool call starting or ending for this long reads as possibly stuck. A guess.
18const STALL_MS = 3 * 60e3
19const BAR_CELLS = 20
20// Wider than any terminal; the divider row clips it to the band's width.
21const DIVIDER = '─'.repeat(400)
22
23const tasks = atom({ plugin: 'task-progress', key: 'tasks' } as const, [])
24const background = atom({ plugin: 'task-progress', key: 'background' } as const, [])
25const activity = atom({ plugin: 'task-progress', key: 'activity' } as const, { isTurnRunning: false, lastAt: 0, runningTool: null, toolStartedAt: null })
26const tick = atom({ plugin: 'task-progress', key: 'tick' } as const, 0)
27
28const num = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? v : null)
29const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v.trim() : null)
30
31const duration = (ms: number) => {
32 const s = Math.max(0, Math.round(ms / 1000))
33 return s >= 3600 ? `${Math.floor(s / 3600)}h${String(Math.floor((s % 3600) / 60)).padStart(2, '0')}m`
34 : s >= 60 ? `${Math.floor(s / 60)}m${String(s % 60).padStart(2, '0')}s` : `${s}s`
35}
36
37// First sighting of each task, for the ETA: percent and time.
38const firstSeen = new Map<string, { at: number; percent: number }>()
39
40async function poll($: any) {
41 const home = await $.env.get('HOME')
42 if (!home) return
43 const dir = `${home}/${DIR}`
44 const entries = await $.fs.list(dir).catch(() => [])
45 const now = await $.clock.now()
46 // Profiles share HOME, so every profile sees every file; show only this account's tasks and unstamped ones.
47 const account = await $.env.get('CLAUDE_CODE_ACCOUNT_UUID')
48 const found: Task[] = []
49 for (const entry of entries) {
50 if (entry.kind !== 'file' || !entry.name.endsWith('.json')) continue
51 const id = entry.name.slice(0, -5)
52 const raw = await $.fs.read(`${dir}/${entry.name}`).catch(() => null)
53 if (raw === null) continue
54 let data: Record<string, unknown>
55 try { data = JSON.parse(raw as string) } catch { continue }
56 if (account && str(data.account) && data.account !== account) continue
57 const current = num(data.current)
58 const total = num(data.total)
59 const percent = num(data.percent) ?? (current !== null && total ? (current / total) * 100 : null)
60 const state = data.state === 'done' || data.state === 'failed' ? data.state : 'running'
61 const updatedAt = num(data.updatedAt) ?? entry.mtimeMs
62 const age = now - updatedAt
63 if (state === 'running' ? age > STALE_MS : age > DONE_SHOWN_MS) continue
64 let etaMs: number | null = null
65 if (percent !== null && state === 'running') {
66 const first = firstSeen.get(id)
67 if (!first || percent < first.percent) firstSeen.set(id, { at: now, percent })
68 else if (percent > first.percent) etaMs = ((now - first.at) / (percent - first.percent)) * (100 - percent)
69 }
70 found.push({ id, label: str(data.label) ?? id, percent, current, total, phase: str(data.phase), state, updatedAt, etaMs })
71 }
72 found.sort((a, b) => a.id.localeCompare(b.id))
73 const before = await read($, tasks)
74 if (JSON.stringify(before) !== JSON.stringify(found)) await update($, tasks, () => found)
75 const done = (await read($, background)).filter(t => t.endedAt === null || now - t.endedAt < DONE_SHOWN_MS)
76 if (done.length !== (await read($, background)).length) await update($, background, () => done)
77}
78
79/** The task id and status a task-notification prompt names, read from its own text. */
80export function notifiedTask(text: string): { id: string; status: string } | null {
81 const id = text.match(/<task-id>([^<]+)<\/task-id>/)?.[1]?.trim()
82 if (!id) return null
83 return { id, status: text.match(/<status>([^<]+)<\/status>/)?.[1]?.trim() || 'ended' }
84}
85
86/** Ends the open rows the session no longer lists as in flight; `inFlight` absent (an older engine) ends none. */
87export function settleBackground(list: BackgroundTask[], inFlight: string[] | undefined, now: number): BackgroundTask[] {
88 if (!inFlight) return list
89 const open = new Set(inFlight)
90 return list.map(t => (t.endedAt === null && !open.has(t.id) ? { ...t, endedAt: now, status: 'ended' } : t))
91}
92
93// A row used to end only when its notification row was drawn, so on the desktop app finished rows read
94// "running" for hours (2026-10-15). It now ends on the notification prompt, a stopped task, or a turn's
95// end where the session no longer lists it.
96async function endTask($: any, id: string, status: string) {
97 const list = await read($, background)
98 if (!list.some(t => t.id === id && t.endedAt === null)) return
99 const now = await $.clock.now()
100 await update($, background, all => all.map(t => (t.id === id && t.endedAt === null ? { ...t, endedAt: now, status } : t)))
101}
102
103// Redraws elapsed times while something time-based is on screen.
104async function advance($: any) {
105 const busy = (await read($, background)).some(t => t.endedAt === null) || (await read($, activity)).isTurnRunning
106 if (busy) await update($, tick, n => n + 1)
107}
108
109async function touch($: any, tool: string | null) {
110 const now = await $.clock.now()
111 await update($, activity, a => ({ ...a, lastAt: now, runningTool: tool, toolStartedAt: tool ? now : null }))
112}
113
114async function saveClipboard($: any) {
115 const home = await $.env.get('HOME')
116 const now = await $.clock.now()
117 const path = `${home}/${CLIP_DIR}/clip-${now}.png`
118 await $.fs.write(`${home}/${CLIP_DIR}/.keep`, '')
119 const script = [
120 `set f to open for access POSIX file "${path}" with write permission`,
121 'write (the clipboard as «class PNGf») to f',
122 'close access f',
123 ]
124 const run = $.process.spawn({ argv: ['/usr/bin/osascript', ...script.flatMap(line => ['-e', line])] })
125 for await (const _ of run) { /* drain */ }
126 const { code } = await run.result
127 return code === 0 ? path : null
128}
129
130export const register: Register = on => {
131 on('session.start', async ($, e, next) => {
132 const started = await next(e)
133 await $.command.register({ name: 'clip', description: 'Attach the image on the clipboard (a screenshot) to the conversation' })
134 await poll($)
135 $.clock.every(POLL_MS, () => { void poll($) })
136 $.clock.every(TICK_MS, () => { void advance($) })
137 return started
138 })
139
140 on('command.run', { command: 'clip' }, async $ => {
141 const path = await saveClipboard($)
142 return path
143 ? { text: `Screenshot from the clipboard saved to ${path}. Read it with the Read tool before answering.` }
144 : { text: 'The clipboard holds no image.' }
145 })
146
147 on('prompt.submit', async ($, e, next) => {
148 const notified = e.origin.kind === 'task-notification' ? notifiedTask(e.text) : null
149 if (notified) await endTask($, notified.id, notified.status)
150 const now = await $.clock.now()
151 await update($, activity, () => ({ isTurnRunning: true, lastAt: now, runningTool: null, toolStartedAt: null }))
152 return next(e)
153 })
154
155 on('turn.complete', async ($, e, next) => {
156 await update($, activity, a => ({ ...a, isTurnRunning: false, runningTool: null, toolStartedAt: null }))
157 return next(e)
158 })
159
160 on('tool.call', async ($, e, next) => {
161 await touch($, e.tool)
162 const ran = await next(e)
163 await touch($, null)
164 if (e.tool === 'TaskStop' && !ran.deny && !ran.isError) {
165 const stopped = str(e.task_id) ?? str(e.shell_id)
166 if (stopped) await endTask($, stopped, 'killed')
167 }
168 const id = e.tool === 'Bash' ? (ran.result as any)?.backgroundTaskId : undefined
169 if (typeof id === 'string') {
170 const now = await $.clock.now()
171 const label = str((e as any).description) ?? String((e as any).command ?? 'background command').slice(0, 60)
172 await update($, background, list => [...list.filter(t => t.id !== id), { id, label, startedAt: now, endedAt: null, status: null }])
173 }
174 return ran
175 })
176
177 on('classic.Stop', async ($, e, next) => {
178 const result = await next(e)
179 const inFlight = (e as any).background_tasks?.map((t: { id: string }) => t.id) as string[] | undefined
180 const list = await read($, background)
181 const settled = settleBackground(list, inFlight, await $.clock.now())
182 if (settled.some((t, i) => t !== list[i])) await update($, background, () => settled)
183 return result
184 })
185
186 on('ui.render', { component: 'UserMessage', props: { origin: { kind: 'task-notification' } } }, async ($, e, next) => {
187 const task = e.props.task
188 if (task?.id) {
189 const list = await read($, background)
190 if (list.some(t => t.id === task.id && t.endedAt === null)) {
191 const now = await $.clock.now()
192 await update($, background, all => all.map(t => (t.id === task.id ? { ...t, endedAt: now, status: task.status ?? 'ended' } : t)))
193 }
194 }
195 return next(e)
196 })
197
198 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
199 const shown = await read($, tasks)
200 const bg = await read($, background)
201 const act = await read($, activity)
202 await read($, tick)
203 const below = await next(e)
204 const now = await $.clock.now()
205 const stalled = act.isTurnRunning && act.runningTool === null && act.lastAt > 0 && now - act.lastAt > STALL_MS
206 const longTool = act.isTurnRunning && act.runningTool !== null && act.toolStartedAt !== null && now - act.toolStartedAt > STALL_MS
207 if (e.props.hasSurvey || (shown.length === 0 && bg.length === 0 && !stalled && !longTool)) return below
208 const { Box, Text } = $.ui.resolve(e)
209
210 return (
211 <Box flexDirection="column">
212 {shown.map(t => {
213 const share = t.percent === null ? 0 : Math.min(1, Math.max(0, t.percent / 100))
214 const filled = Math.round(share * BAR_CELLS)
215 const color = t.state === 'failed' ? 'red' : t.state === 'done' ? 'green' : undefined
216 const count = t.current !== null ? (t.total !== null ? `${t.current}/${t.total}` : `${t.current}`) : null
217 const quiet = now - t.updatedAt > 60e3 && t.state === 'running' ? `quiet ${duration(now - t.updatedAt)}` : null
218 return (
219 <Box key={t.id} flexDirection="row" columnGap={1}>
220 <Text>{t.label}</Text>
221 <Text color={color}>{'█'.repeat(filled)}<Text dimColor>{'░'.repeat(BAR_CELLS - filled)}</Text></Text>
222 <Text color={color}>{t.state === 'done' ? 'done' : t.state === 'failed' ? 'failed' : t.percent === null ? '…' : `${Math.round(t.percent)}%`}</Text>
223 {t.phase ? <Text dimColor>{t.phase}</Text> : null}
224 {count ? <Text dimColor>{count}</Text> : null}
225 {t.etaMs !== null ? <Text dimColor>~{duration(t.etaMs)} left</Text> : null}
226 {quiet ? <Text color="yellow">{quiet}</Text> : null}
227 </Box>
228 )
229 })}
230 {bg.map(t => {
231 const failed = t.status !== null && t.status !== 'completed'
232 const color = t.endedAt === null ? undefined : failed ? 'red' : 'green'
233 return (
234 <Box key={`bg-${t.id}`} flexDirection="row" columnGap={1}>
235 <Text dimColor>bg</Text>
236 <Text>{t.label}</Text>
237 <Text color={color}>{t.endedAt === null ? `running ${duration(now - t.startedAt)}` : `${t.status} after ${duration(t.endedAt - t.startedAt)}`}</Text>
238 </Box>
239 )
240 })}
241 {stalled ? <Text color="yellow">no tool activity for {duration(now - act.lastAt)}, the agent may be stuck</Text> : null}
242 {longTool ? <Text color="yellow">{act.runningTool} running for {duration(now - act.toolStartedAt!)}</Text> : null}
243 {below ? <Box height={1} overflow="hidden"><Text dimColor>{DIVIDER}</Text></Box> : null}
244 {below}
245 </Box>
246 )
247 })
248}
249types/index.d.ts 33 lines1export type Task = {
2 id: string
3 label: string
4 percent: number | null
5 current: number | null
6 total: number | null
7 phase: string | null
8 state: 'running' | 'done' | 'failed'
9 updatedAt: number
10 etaMs: number | null
11}
12
13export type BackgroundTask = {
14 id: string
15 label: string
16 startedAt: number
17 endedAt: number | null
18 status: string | null
19}
20
21export type Activity = {
22 isTurnRunning: boolean
23 lastAt: number
24 runningTool: string | null
25 toolStartedAt: number | null
26}
27
28declare module 'claude-code' {
29 interface PluginState {
30 'task-progress': { tasks: Task[]; background: BackgroundTask[]; activity: Activity; tick: number }
31 }
32}
33