SLOPSHOPPER

progress-tracker

A live progress bar for the current task list and plan, drawn in the band above the prompt.

panebandguardcommandtoastprompt
v0.3.1MITupdated 2026-10-26mjoyshuvo/claude-progress-tracker
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · progress-tracker
│ ┃ Progress ✕ › fix the failing auth test and add an audit log call │ ┃ ● Demo: ship a feature Plan 1/7 │ ┃ ⏺ Read(src/auth.ts) │ ┃ PROGRESS THIS TASK WORKED WAITED ON ⎿ Read 6 lines │ ┃ 0% 0 of 7 0s 0s none ⏺ Update(src/auth.ts) │ ┃ ⎿ Added 2 lines, removed 1 line │ ┃ [ Blocked on me 0 ] [ Found 1 ] ⏺ Bash(bun test) │ ┃ ⎿ 3 pass, 1 fail │ ┃ │ ┃ ✓ Nothing waits on you. ● Done. refresh now rejects expired claims and logs an audit event. │ ┃ Permission prompts, questions and plan │ ┃ approvals show up here. ✻ Worked for 42s · done 4:20 PM │ │ › /progress-tracker-demo │ ⎿ progress-tracker: Playing a progress-tracker demo above the prom │ │ ● Working ▟ ⠒ ⠠⢠⢤⠠⠰⡔⡢⢆⠄⣖⣐⡂⢖⢆⠔⣄⣦⢴⣐⢦⣆⢖⡶⣶▟ ✓ Done · 10 steps ▙ 0s work ▸ ☰ ✕ ▜ ⠐ ⠂⠡⠌⠉⠅⠂⠒⠄⠂⠉⠔⠗⠫⠬⠳⠚⠫⠧⠭⠋⠯⠿⠿⠿▜▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▛ ● Demo: ship a feature ▟ Plan 1/7 ▙ ╷ ╷ ╷ ╷ ╷ ▙ 0% · 0s work ▸ ☰ ✕ ▜▀▀▀▀▀▀▀▀▀▀▛ ╵ ╵ ╵ ╵ ╵ ▛ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Band
● Working ▟ ⠒ ⠠⢠⢤⠠⠰⡔⡢⢆⠄⣖⣐⡂⢖⢆⠔⣄⣦⢴⣐⢦⣆⢖⡶⣶▟ ✓ Done · 10 steps ▙ 0s work ▸ ☰ ✕ ▜ ⠐ ⠂⠡⠌⠉⠅⠂⠒⠄⠂⠉⠔⠗⠫⠬⠳⠚⠫⠧⠭⠋⠯⠿⠿⠿▜▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▛ ● Demo: ship a feature ▟ Plan 1/7 ▙ ╷ ╷ ╷ ╷ ╷ ▙ 0% · 0s work ▸ ☰ ✕ ▜▀▀▀▀▀▀▀▀▀▀▛ ╵ ╵ ╵ ╵ ╵ ▛
Pane · Progress
● Demo: ship a feature Plan 1/7 PROGRESS THIS TASK WORKED WAITED ON YOU CONTEXT 0% 0 of 7 0s 0s none 49% [ Blocked on me 0 ] [ Found 1 ] ✓ Nothing waits on you. Permission prompts, questions and plan approvals show up here.
README

progress-tracker

Unique clones

progress-tracker is a Claude Code mod made for the Code tab of the Claude desktop app. It shows what Claude is doing while it works. It draws a live progress bar in the band above the prompt, and it adds a Progress pane that lists what waits on you and what Claude found.

The mod also runs in the terminal. There, the bar is drawn in text characters, and the task list has no timeline graph.

A desktop session: a Working bar counts steps, Claude's own list takes over with a real total, a test fails twice and turns the row red, a permission prompt turns it amber, and the list finishes teal with its timeline open. The Progress pane beside the chat shows the measured numbers, the failing check's error lines, and the answered permission prompt.

Install the mod

Mods are on by default in current versions of Claude Code. The desktop app runs its own copy of Claude Code, and mods work there from version 2.1.286. In the terminal, you need version 2.1.287 or later.

Check the desktop app's version

  1. In the Code tab, start a local session.
  2. Type /status, and read the Claude Code row.
  3. If the version is lower than 2.1.286, update the app. On macOS, select Claude > Check for Updates. On Windows, select Help > Check for Updates. Then start a new session.

Install in the desktop app

The desktop app's plugin browser lists plugins only from the marketplaces you added. You add this mod's marketplace once, from a terminal. That step needs the claude command. If you don't have it, install Claude Code for the terminal first, or load a local copy instead.

  1. In a terminal, add the marketplace:
	claude plugin marketplace add mjoyshuvo/claude-progress-tracker
  1. Install the mod in the desktop app:
  2. In a local session of the Code tab, click the + button next to the prompt box.
  3. Select Plugins, then Add plugin.
  4. Select progress-tracker.
  5. For the scope, choose your user account.

You can also install it from the terminal instead:

	claude plugin install progress-tracker@mjoyshuvo-mods

The terminal and the desktop app's local sessions read the same settings files, so either way installs the mod for both.

  1. Start a new session in the Code tab. To load the mod in a session that is already open, type /reload-plugins.
  2. Type /progress-tracker-demo. A sample run plays above the prompt for about 30 seconds.

To turn the mod off or uninstall it, click +, then select Plugins > Manage plugins.

The mod runs in local sessions only. Cloud sessions and WSL sessions do not load plugins.

Install in the terminal

  1. In a Claude Code session, add the marketplace:
	/plugin marketplace add mjoyshuvo/claude-progress-tracker
  1. Install the mod:
	/plugin install progress-tracker@mjoyshuvo-mods
  1. Start a new session.
  2. Type /progress-tracker-demo.

Load a local copy

To run the mod from a folder on your computer, for example to change its code, load a local copy instead of installing it. Use one way only: install the mod, or load a local copy.

  1. Clone the repository:
	git clone https://github.com/mjoyshuvo/claude-progress-tracker.git ~/.claude/progress-tracker
  1. Open ~/.claude/settings.json, and set CLAUDE_CODE_PLUGIN_DIRS in its env block to the folder:
	{
		"env": {
			"CLAUDE_CODE_PLUGIN_DIRS": "~/.claude/progress-tracker"
		}
	}

If the file already has an env block, add the line to it. To load more than one folder, separate the paths with : on macOS and Linux, or with ; on Windows. Each path must be absolute or start with ~.

  1. Start a new session in the Code tab.

To update a local copy, run git pull in its folder, then start a new session.

Turn off other progress bar mods

Only one mod can draw in the band above the prompt. If you also use plan-progress, turn it off in + > Plugins > Manage plugins, or run this command:

claude plugin disable plan-progress@zycck-mods

Features

  • Live bar. Each task list gets a row with a moving bar. The pill names the current task and its step, for example Write tests 3/5.
  • Real totals only. A list that Claude declares shows 3/5 and a percent. Work without a list shows a step count, for example Working · 12 steps, because its total is not known.
  • Early task list. On the 3rd tool call of a turn with no list, the mod asks Claude for its task list, so most work gets a real total.
  • Waits on you. A question, a permission prompt, or a plan approval turns the row amber. A question or a permission prompt also shows a toast.
  • Failing checks. A failing test, build, lint, or typecheck turns the row red and shows the first error line. The pill counts repeated failures, for example ×3.
  • Measured numbers. The row shows the time Claude worked, the time it waited on you, and the input and output tokens that the API reported. It does not guess the time left.
  • Context warning. From 80% full, the row shows the context fill.
  • Progress pane. /progress opens a pane with the numbers as labelled tiles and two tabs: Blocked on me and Found. Press an entry to open its details.
  • Task timeline. Each row expands to list its tasks with their times. On desktop, parallel steps and sub-agents appear on a branch.
  • Sub-agent count. While sub-agents that Claude started run, the pill shows how many.
  • Plan mode. Planning gets its own row, and an approved plan becomes a row with the plan's steps.

Watch the demo

/progress-tracker-demo plays a 7-step job on its own row:

  1. The Progress pane opens, and the row expands to list its tasks.
  2. Three file reads run in parallel.
  3. A sub-agent runs, and the pill shows · 1 agent.
  4. The "Test" step fails twice. The row turns red, the pill shows ×2, and the Found tab lists the failure and a finding.
  5. The step recovers. Then the row turns amber, and Blocked on me lists Allow npm publish until it is answered.
  6. The bar finishes teal. About 12 seconds later, the row and its pane entries clear.

The demo bar moves even while Claude is idle.

Read the bar

Each task list gets one row, and the band shows at most 3 rows. A row has a label, the bar, its numbers, and buttons.

The label is the approved plan's title, the title Claude gave the list, or the first task.

The colour of a row tells you its state:

ColourStatePill text
PurpleClaude is working.Build 2/5 or Working · 12 steps
RedA check failed in the current task.✕ Verify 1/2 or ✕ Test 3/5 ×3
AmberClaude waits for you.Waiting for you or Waiting for approval
TealEvery task is done.✓ Done 5/5 or ✓ Done · 12 steps

The numbers after the bar are all measured:

NumberMeaning
60%The finished tasks of a list that Claude declared. An automatic bar has no percent.
4m 10s workThe time inside Claude's turns while the list ran, less the time it waited on you. Idle time between your messages does not count.
1m 05s waitThe time the row was amber.
1.2M in · 14k outThe tokens that each model response reported while the list ran, sub-agents included.
context 85%The context window's fill. It appears from 80%.

Input counts every prompt token the model read: uncached tokens, tokens read from the prompt cache, and tokens written to it. Each model call reads the whole conversation again, so input grows much faster than output, and most of it comes from the cache.

The buttons on a row:

ButtonAction
▸ in the terminal, ▶ Tasks on desktopLists every task with its time.
☰Opens the Progress pane.
✕Hides the row.

A toast appears when a row starts to wait for your answer or permission, and when a task list finishes, for example ✓ Fix login done in 12m 03s · 2 retries.

A finished row hides itself after a minute. New tasks after a finished list start a new row. At the next session start, the mod drops rows that are finished, hidden, or older than 12 hours.

The bar moves while Claude works on a purple or red row. It holds still while the row is amber. In the terminal, the mod draws the bar with block characters and repaints it about 15 times a second. On desktop, the bar is an SVG that animates itself, and a finished bar keeps a slow twinkle.

Use the Progress pane

To open the pane, type /progress or press ☰ on a row.

The top of the pane names the current list and shows its numbers as tiles: progress, this task's time, time worked, time waited on you, tokens in with the cached share, tokens out, model calls, and context. The tiles wrap when the pane is narrow.

The pane has two tabs. Press 1 or 2 to switch.

TabShows
Blocked on mePermission prompts, questions, and plan approvals. Open ones come first. Answered ones say how long they waited.
FoundEach failing check, once per red spell, and the findings Claude logged with found.

To open an entry's details, press its title (▸). To close them, press it again. The details depend on the kind of entry:

  • A question shows each question, its options, and the answer you gave.
  • A permission prompt shows the tool, the full request, and how the call ended: it ran, it ran and ended with an error, or it did not run.
  • A plan approval shows the plan's title, its number of steps, and whether you approved it.
  • A failing check shows the command and up to 8 output lines that name the problem.
  • A finding shows its full text.

Every entry also shows its task, how long it waited, and the time. The pane keeps the last 50 entries of each kind.

Where the tasks come from

The mod reads only the main conversation. It ignores task calls that sub-agents make.

Claude's task tools

In the terminal, Claude's TaskCreate, TaskUpdate, and TodoWrite calls fill the bar. When you approve a plan in plan mode, the plan gets its own row with the plan's title and its numbered or bulleted steps. Claude's first task list then replaces those steps.

The progress_tracker tool

The desktop app has no TaskCreate or TodoWrite, so the mod adds its own tool, mcp__progress-tracker__progress_tracker. The mod also adds a short rule to the system prompt. The rule tells Claude to use the tool for work of more than about 3 edits or commands.

FieldTypeEffect
titlestringNames the row. Use it only with tasks.
tasksstring listCreates a row with these tasks. The first task starts.
nextbooleanFinishes the current task and starts the next one.
activestringMakes the task with this name the current one. Part of the name is enough.
addstring listAdds tasks found during the work to the end of the list.
skipbooleanCloses the current task as not needed. Its part of the bar is drawn dimmer.
failedstringTurns the row red with this reason.
fixedbooleanClears the red state.
foundstringAdds a finding, such as a bug, a root cause, or a decision, to the Found tab. It works without a row.

The tool replies with the state of the row, for example 2/5, running, active "Write tests", next "Docs". If Claude names a task that does not exist, the tool refuses the call and lists the task names.

If Claude makes 12 tool calls without moving the bar, the mod adds one note to a tool result that tells Claude to update it. Only Claude sees the note.

The Working bar

Outside plan mode, the mod starts a row named "Working" on the 3rd tool call of a turn, if no other row is still open. Each tool call becomes a finished step, named after the call's description or file, for example "Read config.ts". The pill counts the steps, because the total is not known. When the turn ends, the open step becomes "Write reply", and the row turns teal.

On that 3rd tool call, Claude also gets a note that only it reads. The note asks Claude to send its task list if more steps are coming. When Claude sends a list with progress_tracker, TaskCreate, or TodoWrite, the list replaces the automatic steps, and the row shows a real total.

Plan mode

In plan mode, a row named "Planning" appears. Each tool call becomes a finished step until Claude sends its own planning steps with progress_tracker. The row turns amber while Claude asks you to approve the plan. Whether you approve or reject the plan, the Planning row turns teal. If you leave plan mode without an answer to a plan, the mod removes the Planning row.

Waits, failures, and sub-agents

  • When Claude asks you a question with AskUserQuestion, or when a permission prompt waits for you, the row turns amber until you answer.
  • When a check command fails during a task, the row turns red and shows the first error line of the output. A check is a test, build, lint, or typecheck command. The command's first words decide: pytest, tsc, ruff, make, npm test, cargo build, go test, and .venv/bin/pytest all count. Commands such as grep, go run, npm run dev, and test -f do not count, even when they exit with code 1.
  • The row turns back from red when the same check passes with any flags, or when a task changes state.
  • While sub-agents that Claude started in this conversation run, the pill shows how many, for example Search 1/3 · 2 agents. Agents from other places do not count.

Read the task timeline

On desktop, press ▶ Tasks on a row to open its timeline. A line connects the steps, and each step shows how long it took. A running task shows its time so far.

  • For an automatic step, the time is how long the tool call ran. For one of Claude's own tasks, the time runs from the task's start to its finish.
  • Steps whose run times overlap leave the line on a branch and join it again. The first of them says · 3 in parallel.
  • A sub-agent step has a robot icon and the agent type, for example · Explore agent. A sub-agent that starts during one of Claude's own tasks appears under that task, on a branch.
  • A sub-agent that runs in the background shows background instead of a time.
  • A task that failed shows how many times, for example · failed ×2.

When the timeline opens, the line draws itself from the top, and the dots appear one by one, each with one ring. Then the timeline holds still.

Known limits

  • Only steps that overlap in time count as parallel. Claude Code runs file reads at the same time, but it often runs shell commands one after another. So Bash calls that Claude sends together can still appear in a straight line.
  • Work time and tokens count from when the mod loaded. A row that was open before a reload shows only what happened after it.
  • A permission entry says how the call ended, not who decided. A hook or a settings rule can also stop a call.
  • Each timeline row is a small sandboxed frame. A long task list runs many animated frames at once.
  • The timeline colours are tuned for the dark theme.

Develop the mod

Run these commands from the mod's folder.

To start Claude Code with your local copy of the mod:

claude --plugin-dir .

To check the manifest and the hooks the way Claude Code loads them:

claude plugin validate .

To run the tests in tests/progress-tracker.test.ts:

claude plugin test .

To run the mod against a stand-in for Claude Code, without Claude Code:

node dev/simulate.mjs

To redraw docs/demo-v0.3.gif from the mod's own bar and timeline SVGs, you need Google Chrome in /Applications and Python 3 with Pillow:

node design/demo-gif.mjs

To write an animated preview of a terminal bar design to design/preview-two-row-smooth.html:

node design/harness.mjs two-row-smooth

design/variants/ holds three terminal bar designs: glow-thumb.mjs, halfblock-caps.mjs, and two-row-smooth.mjs. To switch designs, copy one over hooks/bar.mjs. design/CONTRACT.md describes what a design module must export.

Project layout

PathContents
.claude-plugin/plugin.jsonThe plugin manifest.
.claude-plugin/marketplace.jsonThe mjoyshuvo-mods marketplace entry.
hooks/progress-tracker.mjsThe hooks: task tracking, plan mode, the Working bar, the progress_tracker tool, the Progress pane, and drawing.
hooks/bar.mjsThe terminal bar (barCells).
hooks/bar-svg.mjsThe desktop bar (barSvg).
hooks/timeline-svg.mjsThe task timeline rows (timelineSvg).
types/index.d.tsThe types of the stored rows and the pane's log.
tests/progress-tracker.test.tsThe tests.
dev/simulate.mjsThe stand-in run.
design/demo-gif.mjsDraws docs/demo-v0.3.gif. design/frames-to-gif.py joins its frames.
design/Bar design previews and variants.

Why I built it

I found a Claude Code mod that shows a progress bar above the prompt, zycck/claude-mods, but I couldn't get it working on my machine. So I built my own version to learn how mods work.

License

MIT. See LICENSE.

If the mod helps you, a star on GitHub helps other people find it.

Source 5 files
hooks/progress-tracker.mjs 1935 lines
1// Progress tracker: one live bar per task list, in the band above the prompt.
2//
3// A "track" is one task list. TaskCreate / TaskUpdate / TodoWrite fill the
4// newest track; an approved plan, or new tasks after a finished list, start a
5// fresh one. Only the main loop counts: subagent calls (e.agentId) are skipped.
6
7import { barCells, ROWS as BAR_ROWS } from "./bar.mjs";
8import { barSvg, pillSvg, SVG_HEIGHT } from "./bar-svg.mjs";
9import { timelineSvg, ROW_HEIGHT, GUTTER_WIDTH } from "./timeline-svg.mjs";
10
11const MAX_ROWS = 3;
12const MIN_BAR_COLUMNS = 16;
13const FRAME_MS = 66;
14const STALE_MS = 12 * 60 * 60 * 1000;
15// A finished bar stays this long, then hides itself.
16const DONE_LINGER_MS = 60 * 1000;
17
18const DOT = { working: "#a99cf5", failed: "#ef6f5c", done: "#2bb5a5", waiting: "#f0b34a" };
19const PILL = {
20  working: { bg: "#a99cf5", fg: "#1d1a2e" },
21  failed: { bg: "#ef6f5c", fg: "#2a0a05" },
22  done: { bg: "#2bb5a5", fg: "#04201d" },
23  waiting: { bg: "#f0b34a", fg: "#2a1c05" },
24};
25
26// The running animation: one timer repaints every moving bar with $.ui.blit.
27// Timers die with a hot reload, so this holds no data worth keeping. `tickAt` is
28// the last frame: an interval whose period the host dropped (as after a long idle)
29// ends without a word, and a stale `tickAt` tells the next draw to start another.
30// `sentAt` holds each bar's blit still in flight, so frames do not pile up behind
31// a surface that is not painting.
32const animation = { timer: null, requestId: null, bars: [], t: 0, tickAt: 0, sentAt: new Map() };
33const STALL_MS = 1000;
34
35// Held by the host, so tracks survive a hot reload of this file.
36const TRACKS = { plugin: "progress-tracker", key: "tracks" };
37// The session's log for the Progress pane: what waited on the person, and what Claude
38// found. Kept apart from the tracks, so it outlives them.
39const LOG = { plugin: "progress-tracker", key: "log" };
40const LOG_CAP = 50;
41// The Progress pane and its selected tab.
42const PANE = "progress";
43const TAB = { plugin: "progress-tracker", key: "tab" };
44// The pane entry whose details are open, by id; null when none is.
45const OPEN = { plugin: "progress-tracker", key: "open" };
46const TABS = [
47  { id: "blocked", label: "Blocked on me", hotkey: "1" },
48  { id: "found", label: "Found", hotkey: "2" },
49];
50// When the main loop's running turn began, or null between turns: work time counts only inside turns.
51const TURN = { plugin: "progress-tracker", key: "turn" };
52// The progress_tracker inputs that move the bar; a call with none of them only logs a finding.
53const TOOL_OPS = new Set(["title", "tasks", "next", "active", "add", "skip", "failed", "fixed"]);
54
55// Parallel tool calls each read-modify-write the tracks; run them one by one. A job
56// whose host call never settles (its dispatch dropped while the app sat idle) holds
57// the line for QUEUE_WAIT_MS at most, so one lost call cannot freeze the bar.
58let queue = Promise.resolve();
59const QUEUE_WAIT_MS = 4000;
60
61// Outside plan mode a turn's first tool calls wait here; the AUTO_BAR_CALLS-th starts
62// a "Working" bar from them when Claude has no bar running. Reset on each turn.
63const AUTO_BAR_CALLS = 3;
64let turnSteps = [];
65let hasTurnBar = false;
66let lastMode = null;
67// Tokens the running turn's model calls used so far, so a Working bar that starts on the
68// turn's 3rd tool call still counts the calls before it.
69let turnTokens = emptyTokens();
70
71// The sub-agents the main loop started, by the Agent call that started each one.
72// The pill counts these alone; the timeline marks the call's step as a sub-agent.
73const spawned = new Map();
74
75// The mod's own tool, for sessions without TaskCreate / TodoWrite (the desktop app).
76const TOOL_NAME = "progress_tracker";
77const TOOL = `mcp__progress-tracker__${TOOL_NAME}`;
78const RULES = `# Progress bar
79Work over ~3 edits/commands, plan mode included: call ${TOOL} with {title, tasks:[3-8 short names]}, then {next:true} per finished task, {active:"name"}, {add:["found work"]}, {skip:true}, {failed:"why"}, {fixed:true}, {found:"a bug, root cause or decision worth keeping"}. Don't mention the bar.`;
80
81export function register(on) {
82  on("session.start", async ($, e, next) => {
83    const result = await next(e);
84    // Finished, dismissed and abandoned (over 12 h old) tracks go away.
85    const cutoff = Date.now() - STALE_MS;
86    await update($, (tracks) =>
87      tracks.filter((t) => !t.isDismissed && !isComplete(t) && (t.startedAt ?? Date.now()) > cutoff),
88    );
89    await $.tool.register({
90      name: TOOL_NAME,
91      description:
92        "Live progress bar above the prompt. Use it for any work over ~3 edits/commands, plan mode included: create with title + 3-8 short tasks, then call with next per finished task, active, add, skip, failed or fixed; found logs a finding. Don't mention the bar.",
93      inputSchema: {
94        type: "object",
95        properties: {
96          title: { type: "string", description: "Short name of the whole job; only when creating" },
97          tasks: { type: "array", items: { type: "string" }, description: "Every task in order; only when creating" },
98          next: { type: "boolean", description: "Current task finished, start the next one" },
99          active: { type: "string", description: "Task now in progress" },
100          add: { type: "array", items: { type: "string" }, description: "Tasks found mid-run, appended to the list" },
101          skip: { type: "boolean", description: "Current task is not needed; close it and start the next" },
102          failed: { type: "string", description: "What broke, one line" },
103          fixed: { type: "boolean", description: "The failure is resolved" },
104          found: { type: "string", description: "A bug, root cause or decision worth keeping, one line" },
105        },
106      },
107    });
108    await $.command.register({ name: "progress-tracker-demo", description: "Play a sample run of the progress bar" });
109    await $.command.register({ name: "progress", description: "Show what waits on you and what was found" });
110    return result;
111  });
112
113  on("command.run", { command: "progress" }, async ($) => {
114    await openPane($);
115    return { text: "Opened the Progress pane." };
116  });
117
118  on("command.run", { command: "progress-tracker-demo" }, async ($) => {
119    await playDemo($);
120    return { text: "Playing a progress-tracker demo above the prompt (about 30 seconds)." };
121  });
122
123  on("prompt.compose", async ($, e, next) => {
124    const result = await next(e);
125    return { sections: [...result.sections, { id: "progress-tracker:rules", text: RULES, scope: "session" }] };
126  });
127
128  // Org security mods can bypass user-tier prompt.compose, so the tool carries its own rule and stays loaded.
129  on("tool.describe", { tool: TOOL }, async ($, e, next) => ({ ...(await next(e)), isDeferred: false }));
130
131  on("tool.call", { tool: TOOL }, async ($, e) => {
132    if (typeof e.found === "string" && e.found.trim()) await logFinding($, "found", e.found.trim(), e.tool_use_id);
133    if (typeof e.found === "string" && !Object.keys(e).some((k) => TOOL_OPS.has(k))) return { result: "noted" };
134    if (Array.isArray(e.tasks) && e.tasks.length > 0) {
135      const tasks = e.tasks.map((subject, i) => ({
136        id: `step${i + 1}`,
137        subject: String(subject),
138        status: i === 0 ? "in_progress" : "pending",
139      }));
140      await update($, (tracks) => {
141        // A just-approved plan's bar, or a planning bar still guessing its steps, takes Claude's list.
142        const active = activeOf(tracks);
143        const isFreshPlan = active?.isFromPlan && !active.tasks.some((t) => t.status === "completed");
144        if (isFreshPlan || active?.isAutoSteps) {
145          const label = active.isAutoSteps && e.title ? String(e.title) : active.label;
146          return replace(tracks, { ...active, ...declared(active), label, tasks, failedCommand: null });
147        }
148        return [...tracks, { ...newTrack(tracks, e.title ? String(e.title) : null), tasks }];
149      });
150    } else {
151      await update($, (tracks) => {
152        const active = activeOf(tracks);
153        return active ? replace(tracks, applyOps(active, e)) : tracks;
154      });
155    }
156    const { value: tracks = [] } = await $.state.get(TRACKS);
157    const active = activeOf(tracks);
158    if (!active || active.tasks.length === 0) {
159      return { deny: `${TOOL_NAME}: no bar yet; create one with title and tasks.` };
160    }
161    if (typeof e.active === "string" && findTask(active.tasks, e.active) === -1) {
162      const names = active.tasks.map((t) => `"${t.subject}"`).join(", ");
163      return { deny: `${TOOL_NAME}: no task named "${e.active}"; tasks are ${names}.` };
164    }
165    const done = active.tasks.filter((t) => t.status === "completed").length;
166    const at = active.tasks.findIndex((t) => t.status === "in_progress");
167    const upcoming = active.tasks.slice(at + 1).find((t) => t.status === "pending");
168    const state = isComplete(active) ? "done" : active.failedCommand ? "failed" : "running";
169    const parts = [`${done}/${active.tasks.length}`, state];
170    if (at !== -1) parts.push(`active "${active.tasks[at].subject}"`);
171    if (at !== -1 && upcoming) parts.push(`next "${upcoming.subject}"`);
172    return { result: parts.join(", ") };
173  });
174
175  // Plan mode: a "Planning" bar from the first prompt or tool call made in plan mode.
176  // Claude's own progress_tracker list replaces its steps; until then each tool call
177  // becomes a done step, so the bar moves. The plan_mode note on every plan-mode
178  // turn is the surest sign.
179  on("prompt.attachment", { type: "plan_mode" }, async ($, e, next) => {
180    await followMode($, "plan");
181    return next(e);
182  });
183
184  // A reload asks again for every note in the transcript, old ones included, in order.
185  // The exit note that followed an old plan_mode note drops the bar that note just started.
186  for (const type of ["plan_mode_exit", "auto_mode"]) {
187    on("prompt.attachment", { type }, async ($, e, next) => {
188      await followMode($, type === "auto_mode" ? "auto" : "default");
189      return next(e);
190    });
191  }
192
193  on("agent.spawn", async ($, e, next) => {
194    if (e.parentAgentId) return next(e);
195    await followMode($, e.permissionMode);
196    const r = await next(e);
197    if (r.agentId) {
198      spawned.set(e.tool_use_id, { id: r.agentId, type: e.subagentType, isBackground: e.background === true });
199    }
200    return r;
201  });
202
203  on("classic.UserPromptSubmit", async ($, e, next) => {
204    await followMode($, e.permission_mode);
205    return next(e);
206  });
207
208  // A permission dialog waits on the person: the bar turns amber and the pane lists it.
209  on("classic.PermissionRequest", async ($, e, next) => {
210    if (!e.agent_id) {
211      const input = typeof e.tool_input === "object" && e.tool_input ? e.tool_input : {};
212      const what = e.tool_name === "Bash" && input.command ? String(input.command).trim() : stepNameOf({ ...input, tool: e.tool_name });
213      await openBlock($, "permission", `Allow ${what}`, { details: { tool: String(e.tool_name ?? ""), request: requestOf(e.tool_name, input) } });
214    }
215    return next(e);
216  });
217
218  // Where no PermissionRequest fires, the permission notification stands in for it.
219  on("classic.Notification", async ($, e, next) => {
220    if (!e.agent_id && e.notification_type === "permission_prompt") {
221      await openBlock($, "permission", String(e.message ?? "Needs your permission"), { isFallback: true });
222    }
223    return next(e);
224  });
225
226  on("classic.PreToolUse", async ($, e, next) => {
227    if (!e.agent_id) await followMode($, e.permission_mode);
228    return next(e);
229  });
230
231  on("tool.call", async ($, e, next) => {
232    const startedAt = Date.now();
233    const r = await next(e);
234    if (e.agentId) return r;
235    // The call ran or was refused: whatever it asked of the person is answered.
236    await noteBlock($, (b) => b.kind === "permission" && b.outcome == null && (b.answeredAt == null || b.toolUseId === e.tool_use_id), {
237      outcome: outcomeOf(r),
238    });
239    await closeBlocks($, e.tool_use_id);
240    if (NOT_A_STEP.has(e.tool)) return r;
241    const step = stepOf(e, startedAt);
242    await onPlanning($, (t) => (t.isAutoSteps && !t.isWaiting ? autoStep(t, step) : t));
243    const listNudge = await autoWork($, step);
244    if (step.agent) await noteAgentRun($, step);
245    const nudges = [listNudge, await countWork($)].filter(Boolean);
246    return nudges.length > 0 && r.deny === undefined ? { ...r, context: [...(r.context ?? []), ...nudges] } : r;
247  });
248
249  // Every model response reports its tokens; they count toward the list that is running.
250  // Sub-agents' responses count too: their work is part of the list's work.
251  // turn.step streams, so its hook is an async generator that forwards the stream untouched.
252  on("turn.step", async function* ($, e, next) {
253    const r = yield* next(e);
254    const tokens = tokensOfUsage(r?.usage);
255    if (!tokens) return r;
256    if (!e.agentId) turnTokens = addTokens(turnTokens, tokens);
257    await update($, (tracks) => {
258      const active = activeOf(tracks);
259      if (!active || isComplete(active) || active.isDemo) return tracks;
260      return replace(tracks, { ...active, tokens: addTokens(active.tokens, tokens) });
261    });
262    return r;
263  });
264
265  on("turn.start", async ($, e, next) => {
266    if (!e.agentId) {
267      turnSteps = [];
268      turnTokens = emptyTokens();
269      hasTurnBar = false;
270      await $.state.set(TURN, Date.now());
271    }
272    return next(e);
273  });
274
275  // The turn's "Working" bar ends with the turn and turns green; its open step becomes
276  // "Write reply", timed from the last tool call to the end of the turn.
277  on("turn.complete", async ($, e, next) => {
278    if (!e.agentId) {
279      await update($, (tracks) => tracks.map((t) => (t.isWorkBar && t.isAutoSteps ? closeWorkBar(t) : t)));
280      await closeBlocks($);
281      const { value: turnStartedAt = null } = await $.state.get(TURN);
282      await $.state.set(TURN, null);
283      if (turnStartedAt != null) await update($, (tracks) => tracks.map((t) => addTurn(t, turnStartedAt, Date.now())));
284    }
285    return next(e);
286  });
287
288  // Asking for approval turns the bar amber until the person answers. Either answer
289  // ends this round of planning (green); approved, the plan's steps get a bar of their own.
290  on("tool.call", { tool: "ExitPlanMode" }, async ($, e, next) => {
291    if (e.agentId) return next(e);
292    await onPlanning($, (t) => ({ ...toStep(t, t.tasks.length - 1), isWaiting: true }));
293    await openBlock($, "approval", "Approve the plan", { keepsBar: true, toolUseId: e.tool_use_id });
294    const r = await next(e);
295    const approved = typeof e.plan === "string" ? e.plan : r.result?.plan;
296    await noteBlock($, (b) => b.kind === "approval" && b.toolUseId === e.tool_use_id, {
297      outcome: isAnswered(r) ? "Approved" : "Not approved",
298      details: { plan: planTitleOf(approved), steps: planStepsOf(approved).length },
299    });
300    await closeBlocks($);
301    await onPlanning($, (t) => ({
302      ...toStep(t, t.tasks.length),
303      isPlanning: false,
304      isWaiting: false,
305      isAutoSteps: false,
306    }));
307    if (!isAnswered(r)) return r;
308    const plan = typeof e.plan === "string" ? e.plan : r.result?.plan;
309    await update($, (tracks) => [
310      ...tracks,
311      { ...newTrack(tracks, planTitleOf(plan)), tasks: planStepsOf(plan), isFromPlan: true },
312    ]);
313    return r;
314  });
315
316  // A question to the person turns the working bar amber until it is answered.
317  on("tool.call", { tool: "AskUserQuestion" }, async ($, e, next) => {
318    if (e.agentId) return next(e);
319    const questions = (Array.isArray(e.questions) ? e.questions : []).map((q) => ({
320      question: String(q?.question ?? ""),
321      options: (Array.isArray(q?.options) ? q.options : []).map((o) => String(o?.label ?? o)),
322    }));
323    await openBlock($, "question", questions[0]?.question || "A question for you", { toolUseId: e.tool_use_id, details: { questions } });
324    let r;
325    try {
326      r = await next(e);
327      return r;
328    } finally {
329      // The answers come back keyed by the question's text.
330      const answers = isAnswered(r ?? {}) && r.result && typeof r.result.answers === "object" ? r.result.answers : null;
331      await noteBlock($, (b) => b.kind === "question" && b.toolUseId === e.tool_use_id, {
332        outcome: answers ? "Answered" : "Not answered",
333        answers: answers ? Object.fromEntries(Object.entries(answers).map(([q, a]) => [q, String(a)])) : null,
334      });
335      await closeBlocks($);
336    }
337  });
338
339  on("tool.call", { tool: "TaskCreate" }, async ($, e, next) => {
340    const r = await next(e);
341    if (e.agentId || !isAnswered(r)) return r;
342    const id = String(r.result?.task?.id ?? e.tool_use_id);
343    const task = { id, subject: String(e.subject ?? ""), status: "pending" };
344    await update($, (tracks) => {
345      // Claude's first task after an approved plan, or on an automatic bar, replaces its steps.
346      const active = activeOf(tracks);
347      if (active?.isFromPlan || (active?.isCounter && !isComplete(active))) {
348        return replace(tracks, { ...active, ...declared(active), tasks: [task] });
349      }
350      return withActive(tracks, (t) => ({ ...t, tasks: [...t.tasks, task] }));
351    });
352    return r;
353  });
354
355  on("tool.call", { tool: "TaskUpdate" }, async ($, e, next) => {
356    const r = await next(e);
357    if (e.agentId || !isAnswered(r)) return r;
358    const id = String(e.taskId);
359    await update($, (tracks) =>
360      tracks.map((t) => {
361        if (!t.tasks.some((task) => task.id === id)) return t;
362        const tasks =
363          e.status === "deleted"
364            ? t.tasks.filter((task) => task.id !== id)
365            : t.tasks.map((task) =>
366                task.id === id
367                  ? { ...task, subject: e.subject ?? task.subject, status: e.status ?? task.status }
368                  : task,
369              );
370        const isStatusChange = e.status !== undefined;
371        return { ...t, tasks, failedCommand: isStatusChange ? null : t.failedCommand };
372      }),
373    );
374    return r;
375  });
376
377  on("tool.call", { tool: "TodoWrite" }, async ($, e, next) => {
378    const r = await next(e);
379    if (e.agentId || !isAnswered(r) || !Array.isArray(e.todos)) return r;
380    const tasks = e.todos.map((todo, i) => ({
381      id: `todo${i}`,
382      subject: String(todo.content ?? ""),
383      status: todo.status,
384    }));
385    await update($, (tracks) => withActive(tracks, (t) => ({ ...t, ...declared(t), tasks }), tasks));
386    return r;
387  });
388
389  on("tool.call", { tool: "Bash" }, async ($, e, next) => {
390    const r = await next(e);
391    if (e.agentId || r.deny !== undefined) return r;
392    const command = String(e.command ?? "");
393    await update($, (tracks) => {
394      const active = activeOf(tracks);
395      if (!active || isComplete(active)) return tracks;
396      if (r.isError && isCheck(command)) {
397        return replace(tracks, { ...active, failedCommand: command, failedReason: errorLineOf(r.result), failedLines: errorLinesOf(r.result), failedRuns: (active.failedRuns ?? 0) + 1, failedToolUseId: e.tool_use_id });
398      }
399      if (!r.isError && active.failedCommand && sameCheck(active.failedCommand, command)) {
400        return replace(tracks, { ...active, failedCommand: null, failedReason: null });
401      }
402      return tracks;
403    });
404    return r;
405  });
406
407  on("ui.render", { component: "AbovePrompt" }, async ($, e, next) => {
408    if (e.props.hasSurvey) {
409      animate($, null, []);
410      return next(e);
411    }
412    const { value: tracks = [] } = await $.state.get(TRACKS);
413    const agents = await runningAgentsOf($);
414    const shown = tracks
415      .filter((t) => !t.isDismissed && t.tasks.length > 0 && !isPastLinger(t))
416      .map((t, i, all) => (i === all.length - 1 ? { ...t, agents: t.isDemo ? (t.demoAgents ?? 0) : agents } : t));
417    const ui = $.ui.resolve(e);
418    const ctx = await contextOf($);
419    const dismiss = (id) => () =>
420      void update($, (all) => all.map((t) => (t.id === id ? { ...t, isDismissed: true } : t)));
421    const toggle = (id) => () =>
422      void update($, (all) => all.map((t) => (t.id === id ? { ...t, isExpanded: !t.isExpanded } : t)));
423
424    // Off the terminal there is no Raster: an SVG draws the bar and SMIL moves it.
425    if (e.surface !== "terminal") {
426      animate($, null, []);
427      if (shown.length === 0) return next(e);
428      return drawDesktop(ui, shown.slice(-MAX_ROWS), e.props, { dismiss, toggle, pane: () => void openPane($) }, ctx);
429    }
430
431    const rows = shown
432      .slice(-Math.max(1, Math.min(MAX_ROWS, Math.floor(e.props.maxRows / BAR_ROWS))))
433      .map((t) => rowOf(t, e.props.bodyColumns - 2, ctx));
434    animate($, e.requestId, rows.filter((r) => r.bar && isLive(r.status) && (e.props.isWorking || r.isDemo)));
435    if (rows.length === 0) return next(e);
436
437    // Expanded rows share the rows the bars leave free, in order.
438    let spare = e.props.maxRows - rows.length * BAR_ROWS;
439    const actions = { dismiss, toggle, pane: () => void openPane($) };
440    return ui.Box({
441      flexDirection: "column",
442      paddingX: 1,
443      children: rows.map((r) => {
444        const lines = r.isExpanded ? Math.max(0, spare) : 0;
445        spare -= lines;
446        return draw(ui, r, actions, lines);
447      }),
448    });
449  });
450
451  on("ui.render", { component: "Pane", requestId: PANE }, async ($, e) => {
452    const { value: log = emptyLog() } = await $.state.get(LOG);
453    const { value: saved = "blocked" } = await $.state.get(TAB);
454    const tab = TABS.some((t) => t.id === saved) ? saved : "blocked";
455    const { value: tracks = [] } = await $.state.get(TRACKS);
456    const active = activeOf(tracks.filter((t) => t.tasks.length > 0));
457    const { value: openId = null } = await $.state.get(OPEN);
458    const kit = { ...$.ui.resolve(e), openId, toggle: (id) => () => void $.state.set(OPEN, openId === id ? null : id) };
459    return drawPane(kit, { log, tab, track: active, ctx: await contextOf($) }, (id) => () => void $.state.set(TAB, id));
460  });
461}
462
463// ---- state ----
464
465function enqueue($, job) {
466  const before = queue;
467  const turn = new Promise((resolve) => {
468    let timer = null;
469    try {
470      timer = $.clock.after(QUEUE_WAIT_MS, resolve);
471    } catch {
472      // No timer: wait for the job before, however long it takes.
473    }
474    void before.then(() => {
475      timer?.cancel();
476      resolve();
477    });
478  });
479  queue = turn.then(job).catch(() => undefined);
480  return queue;
481}
482
483function update($, change) {
484  return enqueue($, async () => {
485    const { value: tracks = [] } = await $.state.get(TRACKS);
486    let changed = change(tracks);
487    if (changed === tracks) return;
488    const before = new Map(tracks.map((t) => [t.id, t]));
489    changed = changed.map((t) => stampWait(before.get(t.id), stampDone($, stampFailure(before.get(t.id), stampTasks(t)))));
490    await $.state.set(TRACKS, changed);
491    for (const t of changed) notify($, before.get(t.id), t);
492    const failures = changed.filter((t) => t.failureToLog && t.failureToLog !== before.get(t.id)?.failureToLog);
493    if (failures.length > 0) {
494      await writeLog($, (log) => ({
495        ...log,
496        findings: capped([...log.findings, ...failures.map((t, i) => ({ kind: "failed", ...t.failureToLog, id: entryId("failed", log.findings.length + i), at: Date.now(), isDemo: t.isDemo }))]),
497      }));
498    }
499  });
500}
501
502// ---- measured numbers: work, wait, tokens, context ----
503
504// A list Claude declared itself: its total is real, so the bar counts x/y again.
505function declared(track) {
506  return {
507    isFromPlan: false,
508    isAutoSteps: false,
509    isCounter: false,
510    isWorkBar: false,
511    label: track.isWorkBar ? null : track.label,
512  };
513}
514
515// Time spent waiting on the person: from the moment the bar turns amber until it turns back.
516function stampWait(old, track) {
517  const now = Date.now();
518  if (track.isWaiting && !old?.isWaiting) return { ...track, waitingSince: now };
519  if (!track.isWaiting && old?.isWaiting && track.waitingSince != null) {
520    return { ...track, waitMs: (track.waitMs ?? 0) + (now - track.waitingSince), waitingSince: null };
521  }
522  return track;
523}
524
525// A finished turn adds the part of it that overlaps the track's own life.
526function addTurn(track, turnStart, turnEnd) {
527  if (track.isDemo) return track;
528  const overlap = Math.min(turnEnd, track.doneAt ?? turnEnd) - Math.max(turnStart, track.startedAt ?? turnStart);
529  return overlap > 0 ? { ...track, turnMs: (track.turnMs ?? 0) + overlap } : track;
530}
531
532// Work: the time Claude was in a turn while the track ran, less the time it waited on the
533// person. Idle time between turns counts as neither. The demo runs outside any turn.
534export function workOf(track, now, turnStartedAt) {
535  const end = Math.min(now, track.doneAt ?? now);
536  const start = track.isDemo ? track.startedAt : turnStartedAt;
537  const live = start != null ? Math.max(0, end - Math.max(start, track.startedAt ?? start)) : 0;
538  return Math.max(0, (track.turnMs ?? 0) + live - waitOf(track, now));
539}
540
541export function waitOf(track, now) {
542  return (track.waitMs ?? 0) + (track.waitingSince != null ? now - track.waitingSince : 0);
543}
544
545// The context window's fill as the engine reports it; null where it reports none.
546async function usageOf($) {
547  try {
548    const usage = await $.session.usage();
549    return { contextPercent: usage.context?.percent ?? null };
550  } catch {
551    return { contextPercent: null };
552  }
553}
554
555async function contextOf($) {
556  const { value: turnStartedAt = null } = await $.state.get(TURN);
557  return { now: Date.now(), turnStartedAt, ...(await usageOf($)) };
558}
559
560// Tokens as the API reports them per response. Input is every prompt token the model
561// read: uncached, read from the cache, and written to it. Each call re-reads the whole
562// conversation, so input grows much faster than output, and most of it comes from the cache.
563function emptyTokens() {
564  return { input: 0, cached: 0, output: 0, calls: 0 };
565}
566
567function tokensOfUsage(usage) {
568  if (!usage) return null;
569  const cached = usage.cache_read_input_tokens ?? 0;
570  const input = (usage.input_tokens ?? 0) + cached + (usage.cache_creation_input_tokens ?? 0);
571  return { input, cached, output: usage.output_tokens ?? 0, calls: 1 };
572}
573
574function addTokens(a = emptyTokens(), b) {
575  return { input: a.input + b.input, cached: a.cached + b.cached, output: a.output + b.output, calls: a.calls + b.calls };
576}
577
578// 820, 14.2k, 142k, 1.23M.
579export function countOf(n) {
580  if (n < 1000) return String(n);
581  if (n < 100_000) return `${(n / 1000).toFixed(1)}k`;
582  if (n < 1_000_000) return `${Math.round(n / 1000)}k`;
583  return `${(n / 1_000_000).toFixed(2)}M`;
584}
585
586// Notes when each task starts and finishes. A reopened task loses its finish.
587function stampTasks(track) {
588  const now = Date.now();
589  const tasks = track.tasks.map((t) => {
590    if (t.status === "in_progress" && (t.startedAt == null || t.doneAt != null)) {
591      return { ...t, startedAt: t.startedAt ?? now, doneAt: null };
592    }
593    if (t.status === "completed" && t.doneAt == null) return { ...t, doneAt: now };
594    return t;
595  });
596  return tasks.every((t, i) => t === track.tasks[i]) ? track : { ...track, tasks };
597}
598
599// ---- the session log (Progress pane) ----
600
601function emptyLog() {
602  return { blocks: [], findings: [] };
603}
604
605function capped(list) {
606  return list.slice(-LOG_CAP);
607}
608
609// Unqueued: for use inside a queued job. Everything else goes through changeLog.
610async function writeLog($, change) {
611  const { value: log = emptyLog() } = await $.state.get(LOG);
612  const changed = change(log);
613  if (changed !== log) await $.state.set(LOG, changed);
614}
615
616function changeLog($, change) {
617  return enqueue($, () => writeLog($, change));
618}
619
620async function currentTaskOf($) {
621  const { value: tracks = [] } = await $.state.get(TRACKS);
622  const active = activeOf(tracks);
623  return active?.isAutoSteps ? null : (active?.tasks.find((t) => t.status === "in_progress")?.subject ?? null);
624}
625
626// Something waits on the person: the bar turns amber and the pane lists it. A plan
627// approval keeps its own Planning bar; a fallback adds nothing when one is already open.
628async function openBlock($, kind, text, { isFallback = false, keepsBar = false, toolUseId = null, details = null } = {}) {
629  const task = await currentTaskOf($);
630  if (!keepsBar) {
631    await update($, (tracks) => {
632      const active = activeOf(tracks);
633      return active && !isComplete(active) && !active.isWaiting ? replace(tracks, { ...active, isWaiting: true }) : tracks;
634    });
635  }
636  await changeLog($, (log) => {
637    if (isFallback && log.blocks.some((b) => b.kind === kind && b.answeredAt == null)) return log;
638    const block = { id: entryId(kind, log.blocks.length), kind, text: shorten(text, 120), task, at: Date.now(), answeredAt: null, toolUseId, details };
639    return { ...log, blocks: capped([...log.blocks, block]) };
640  });
641}
642
643// Adds what happened to the newest block that matches: the answer, how the call ended.
644function noteBlock($, isTarget, fields) {
645  return changeLog($, (log) => {
646    const at = log.blocks.findLastIndex(isTarget);
647    if (at === -1) return log;
648    return { ...log, blocks: log.blocks.map((b, i) => (i === at ? { ...b, ...fields } : b)) };
649  });
650}
651
652// How a call ended, as its result says. A refusal is not said to be the person's: a hook
653// or a rule can refuse a call too.
654function outcomeOf(r) {
655  if (r.deny !== undefined) return "It did not run";
656  return r.isError ? "It ran and ended with an error" : "It ran";
657}
658
659// What a permission prompt asked to do, in full: the command, else the file, else the input.
660function requestOf(tool, input) {
661  if (input.command) return String(input.command);
662  if (input.file_path || input.notebook_path) return String(input.file_path ?? input.notebook_path);
663  if (input.url) return String(input.url);
664  const text = JSON.stringify(input);
665  return text === "{}" ? String(tool ?? "") : shorten(text, 400);
666}
667
668// A log entry's id: stable while the entry lives, so its details stay open across renders.
669function entryId(kind, n) {
670  return `${kind}-${Date.now().toString(36)}-${n}`;
671}
672
673// A permission prompt learns which call it belonged to when that call ends.
674async function closeBlocks($, toolUseId = null) {
675  await changeLog($, (log) => {
676    if (!log.blocks.some((b) => b.answeredAt == null)) return log;
677    const now = Date.now();
678    const close = (b) => ({ ...b, answeredAt: now, toolUseId: b.toolUseId ?? toolUseId });
679    return { ...log, blocks: log.blocks.map((b) => (b.answeredAt == null ? close(b) : b)) };
680  });
681  await update($, (tracks) => {
682    const active = activeOf(tracks);
683    return active?.isWaiting && !active.isPlanning ? replace(tracks, { ...active, isWaiting: false }) : tracks;
684  });
685}
686
687async function logFinding($, kind, text, toolUseId = null) {
688  const task = await currentTaskOf($);
689  await changeLog($, (log) => ({
690    ...log,
691    findings: capped([...log.findings, { id: entryId(kind, log.findings.length), kind, text: shorten(text, 1000), task, at: Date.now(), toolUseId }]),
692  }));
693}
694
695async function openPane($) {
696  try {
697    const opened = await $.ui.open({ id: PANE, title: "Progress" });
698    if (!opened.isPlaced) $.ui.toast(`Progress pane waits: ${opened.reason ?? "this window shows no panes"}`);
699  } catch {
700    // No panes on this surface.
701  }
702}
703
704// Each new failing run counts against the task in progress; the first of a red spell
705// also goes to the Found tab.
706function stampFailure(old, track) {
707  if (!track.failedRuns || track.failedRuns === old?.failedRuns) return track;
708  const at = track.tasks.findIndex((t) => t.status === "in_progress");
709  if (at === -1) return track;
710  const tasks = track.tasks.map((t, i) => (i === at ? { ...t, failures: (t.failures ?? 0) + 1 } : t));
711  const isNewSpell = old?.failedCommand == null;
712  return {
713    ...track,
714    tasks,
715    failureToLog: isNewSpell
716      ? {
717          text: track.failedReason ?? track.failedCommand,
718          task: tasks[at].subject,
719          toolUseId: track.failedToolUseId ?? null,
720          command: track.failedCommand,
721          lines: track.failedLines ?? [],
722        }
723      : null,
724  };
725}
726
727// A toast when a bar starts waiting on the person, and when a real task list finishes.
728function notify($, old, track) {
729  try {
730    if (track.isWaiting && !old?.isWaiting && !track.isPlanning) {
731      $.ui.toast(`Waiting for you · ${track.label ?? track.tasks[0]?.subject ?? "Claude"}`);
732    }
733    const isAuto = track.isWorkBar || track.isPlanning || track.isAutoSteps;
734    if (track.doneAt != null && old?.doneAt == null && old !== undefined && !isAuto) {
735      const retries = track.tasks.reduce((sum, t) => sum + (t.failures ?? 0), 0);
736      const time = track.startedAt ? ` in ${durationOf(track.doneAt - track.startedAt)}` : "";
737      $.ui.toast(`✓ ${track.label ?? track.tasks[0]?.subject ?? "Tasks"} done${time}${retries ? ` · ${retries} ${retries === 1 ? "retry" : "retries"}` : ""}`);
738    }
739  } catch {
740    // No toasts on this surface.
741  }
742}
743
744// A finished bar past its minute. The timer stampDone sets hides it; this covers a
745// timer lost with the hooks worker, so a done bar never sticks around.
746function isPastLinger(track) {
747  return track.doneAt != null && !track.isDemo && !track.isExpanded && Date.now() - track.doneAt > DONE_LINGER_MS + 5000;
748}
749
750// Notes when a track finishes (to freeze its clock) and hides it a minute later.
751// A track that starts again (new tasks, a reopened task) loses the stamp.
752function stampDone($, track) {
753  const isDone = isComplete(track);
754  if (isDone === (track.doneAt != null)) return track;
755  if (!isDone) return { ...track, doneAt: null };
756  const doneAt = Date.now();
757  if (!track.isDemo) {
758    try {
759      $.clock.after(DONE_LINGER_MS, () =>
760        void update($, (all) =>
761          all.map((t) => (t.id === track.id && t.doneAt === doneAt && !t.isExpanded ? { ...t, isDismissed: true } : t)),
762        ),
763      );
764    } catch {
765      // No timer: the finished bar stays until ✕ or the next session.
766    }
767  }
768  return { ...track, doneAt };
769}
770
771// Ids count up from the highest one still held, so two tracks never share a Raster key.
772function newTrack(tracks, label) {
773  const highest = Math.max(0, ...tracks.map((t) => Number(t.id.replace(/^track/, "")) || 0));
774  return {
775    id: `track${highest + 1}`,
776    label,
777    tasks: [],
778    failedCommand: null,
779    isDismissed: false,
780    startedAt: Date.now(),
781    doneAt: null,
782  };
783}
784
785function activeOf(tracks) {
786  return tracks.findLast((t) => !t.isDismissed);
787}
788
789function replace(tracks, track) {
790  return tracks.map((t) => (t.id === track.id ? track : t));
791}
792
793// Applies `change` to the newest track. A finished list (or none) gets a fresh
794// track first, unless `incoming` (a TodoWrite list) still names one of its tasks.
795function withActive(tracks, change, incoming) {
796  const active = activeOf(tracks);
797  const isSameList =
798    incoming !== undefined &&
799    active !== undefined &&
800    incoming.some((task) => active.tasks.some((old) => old.subject === task.subject));
801  if (active && (!isComplete(active) || active.tasks.length === 0 || isSameList)) {
802    return replace(tracks, change(active));
803  }
804  return [...tracks, change(newTrack(tracks, null))];
805}
806
807// /progress-tracker-demo: a scripted run on its own track that shows every feature: three
808// reads in parallel, a sub-agent, a failing check that recovers, time left, and the
809// finished timeline opened on its own. The track goes away a while after it finishes.
810const DEMO_TASKS = ["Plan", "Read config.ts", "Read api.ts", "Read db.ts", "Build", "Test", "Ship"];
811// Each step: [delay, change to the demo track, change to the pane's log]. Demo log
812// entries carry isDemo and go when the demo does.
813const DEMO_STEPS = [
814  [1400, (t) => applyOps(t, { next: true })],
815  [1600, demoParallelReads],
816  [700, (t) => ({ ...t, demoAgents: 1, isExpanded: true })],
817  [1600, demoAgentDone],
818  [600, (t) => applyOps(t, { next: true })],
819  [1200, (t) => applyOps(t, { failed: "2 tests failing" })],
820  [1300, (t) => applyOps(t, { failed: "1 test failing" }), demoFinding("Retry loop in api.ts drops the auth header", "Test")],
821  [1600, (t) => applyOps(t, { fixed: true })],
822  [500, (t) => ({ ...t, isWaiting: true }), demoBlock("Allow npm publish", "Ship")],
823  [1800, (t) => ({ ...t, isWaiting: false }), demoAnswer],
824  [900, (t) => applyOps(t, { next: true })],
825  [1400, (t) => applyOps(t, { next: true })],
826];
827const DEMO_LINGER_MS = 12000;
828
829function demoFinding(text, task) {
830  return (log) => ({ ...log, findings: [...log.findings, { id: entryId("found", log.findings.length), kind: "found", text, task, at: Date.now(), isDemo: true }] });
831}
832
833function demoBlock(text, task) {
834  return (log) => ({
835    ...log,
836    blocks: [
837      ...log.blocks,
838      {
839        id: entryId("permission", log.blocks.length),
840        kind: "permission",
841        text,
842        task,
843        at: Date.now(),
844        answeredAt: null,
845        isDemo: true,
846        details: { tool: "Bash", request: text.replace(/^Allow /, "") },
847      },
848    ],
849  });
850}
851
852function demoAnswer(log) {
853  return {
854    ...log,
855    blocks: log.blocks.map((b) => (b.isDemo && b.answeredAt == null ? { ...b, answeredAt: Date.now(), outcome: "It ran" } : b)),
856  };
857}
858
859function dropDemoLog(log) {
860  const keep = (list) => list.filter((entry) => !entry.isDemo);
861  return { blocks: keep(log.blocks), findings: keep(log.findings) };
862}
863
864async function playDemo($) {
865  let id = null;
866  await update($, (tracks) => {
867    const track = newTrack(tracks, "Demo: ship a feature");
868    id = track.id;
869    const tasks = DEMO_TASKS.map((subject, i) => ({
870      id: `demo${i + 1}`,
871      subject,
872      status: i === 0 ? "in_progress" : "pending",
873    }));
874    return [...tracks, { ...track, tasks, isDemo: true }];
875  });
876  const onDemo = (change) => update($, (tracks) => tracks.map((t) => (t.id === id ? change(t) : t)));
877  await openPane($);
878  let at = 0;
879  for (const [delay, change, logChange] of DEMO_STEPS) {
880    at += delay;
881    $.clock.after(at, () => {
882      void onDemo(change);
883      if (logChange) void changeLog($, logChange);
884    });
885  }
886  $.clock.after(at + DEMO_LINGER_MS, () => {
887    void update($, (tracks) => tracks.filter((t) => t.id !== id));
888    void changeLog($, dropDemoLog);
889  });
890}
891
892// The three reads all start when Plan finishes and end at different times.
893function demoParallelReads(track) {
894  const now = Date.now();
895  const start = track.tasks[0].doneAt ?? now - 1600;
896  const lengths = [900, 1300, 1600];
897  const tasks = track.tasks.map((task, i) => {
898    if (i >= 1 && i <= 3) return { ...task, status: "completed", startedAt: start, doneAt: start + lengths[i - 1] };
899    return i === 4 ? { ...task, status: "in_progress", startedAt: start + Math.max(...lengths) } : task;
900  });
901  return { ...track, tasks };
902}
903
904function demoAgentDone(track) {
905  const now = Date.now();
906  const run = {
907    taskId: "demo5",
908    subject: "Map the routes",
909    agent: { type: "Explore", isBackground: false },
910    startedAt: now - 1600,
911    doneAt: now,
912  };
913  return { ...track, demoAgents: 0, agentRuns: [...(track.agentRuns ?? []), run] };
914}
915
916// Done and waiting-for-approval bars hold still; the rest move while Claude works.
917function isLive(status) {
918  return status === "working" || status === "failed";
919}
920
921// ---- plan mode ----
922
923// Counts work calls since the bar last moved. After NUDGE_AFTER of them Claude gets one
924// note it alone reads, so a forgotten {next:true} does not freeze the bar.
925const NUDGE_AFTER = 12;
926
927async function countWork($) {
928  let nudge = null;
929  await update($, (tracks) => {
930    const active = activeOf(tracks);
931    if (!active || active.isAutoSteps || isComplete(active)) return tracks;
932    const key = active.tasks.map((t) => t.status).join(",");
933    const calls = key === active.statusKey ? (active.callsSinceMove ?? 0) + 1 : 1;
934    if (calls >= NUDGE_AFTER) {
935      const current = active.tasks.find((t) => t.status === "in_progress");
936      const done = active.tasks.filter((t) => t.status === "completed").length;
937      nudge = `The progress bar still shows "${current?.subject ?? "nothing"}" in progress (${done}/${active.tasks.length}). If that task is done, update it now (${TOOL_NAME} next/active, or TaskUpdate). Don't mention this note.`;
938    }
939    return replace(tracks, { ...active, statusKey: key, callsSinceMove: nudge ? 0 : calls });
940  });
941  return nudge;
942}
943
944// Tools that are bookkeeping, not work: they never become a planning step.
945const NOT_A_STEP = new Set([TOOL, "ExitPlanMode", "AskUserQuestion", "ToolSearch", "TaskCreate", "TaskUpdate", "TodoWrite"]);
946
947// In plan mode a Planning bar starts. Out of it, a Planning bar that never reached
948// ExitPlanMode (the person left plan mode by hand) is dropped.
949async function followMode($, mode) {
950  if (mode) lastMode = mode;
951  if (mode === "plan") return startPlanning($);
952  if (mode) await update($, (tracks) => (tracks.some((t) => t.isPlanning) ? tracks.filter((t) => !t.isPlanning) : tracks));
953}
954
955async function startPlanning($) {
956  await update($, (tracks) => {
957    if (tracks.some((t) => t.isPlanning && !t.isDismissed)) return tracks;
958    const tasks = [
959      { id: "plan1", subject: "Explore", status: "in_progress" },
960      { id: "approval", subject: "Approval", status: "pending" },
961    ];
962    return [...tracks, { ...newTrack(tracks, "Planning"), tasks, isPlanning: true, isAutoSteps: true, isCounter: true }];
963  });
964}
965
966// The step in progress is done and named after the call; a new one starts before Approval.
967function autoStep(track, step) {
968  const at = track.tasks.findIndex((t) => t.status === "in_progress");
969  if (at === -1) return track;
970  const done = { ...track.tasks[at], ...taskOf(step), status: "completed" };
971  const subject = track.isWorkBar ? "Working" : step.isWrite ? "Write plan" : "Explore";
972  const following = { id: `plan${at + 2}`, subject, status: "in_progress" };
973  return { ...track, tasks: [...track.tasks.slice(0, at), done, following, ...track.tasks.slice(at + 1)] };
974}
975
976function closeWorkBar(track) {
977  const steps = track.tasks.filter((task) => task.status === "completed");
978  const open = track.tasks.find((task) => task.status === "in_progress");
979  const lastEnd = Math.max(0, ...steps.map((task) => task.doneAt ?? 0));
980  const reply = open
981    ? [{ ...open, subject: "Write reply", status: "completed", startedAt: lastEnd || open.startedAt, doneAt: Date.now() }]
982    : [];
983  return { ...track, tasks: [...steps, ...reply], isAutoSteps: false };
984}
985
986// One finished tool call, timed by the hook that ran it.
987function stepOf(call, startedAt) {
988  const run =
989    spawned.get(call.tool_use_id) ??
990    (call.tool === "Agent"
991      ? { type: String(call.subagent_type ?? "general-purpose"), isBackground: call.run_in_background === true }
992      : undefined);
993  const agent = run ? { type: run.type, isBackground: run.isBackground } : undefined;
994  return { subject: stepNameOf(call), startedAt, doneAt: Date.now(), isWrite: call.tool === "Write" || call.tool === "Edit", agent };
995}
996
997function taskOf(step) {
998  return { subject: step.subject, startedAt: step.startedAt, doneAt: step.doneAt, agent: step.agent };
999}
1000
1001// A sub-agent run during one of Claude's own tasks is listed under that task.
1002async function noteAgentRun($, step) {
1003  await update($, (tracks) => {
1004    const active = activeOf(tracks);
1005    const task = active?.tasks.find((t) => t.status === "in_progress");
1006    if (!task || active.isAutoSteps) return tracks;
1007    const run = { taskId: task.id, ...taskOf(step) };
1008    return replace(tracks, { ...active, agentRuns: [...(active.agentRuns ?? []), run] });
1009  });
1010}
1011
1012// The note Claude gets when a turn's work starts without a list: the bar can only count
1013// steps until Claude says how many there are.
1014const LIST_NUDGE = `This work has no task list yet, so the progress bar can only count steps. If more steps are coming, call ${TOOL} now with {title, tasks:[every step you plan]} so the bar shows a real total. Don't mention this note.`;
1015
1016// Returns the note for Claude when this call starts a Working bar.
1017async function autoWork($, step) {
1018  if (lastMode === "plan") return null;
1019  turnSteps.push(step);
1020  let nudge = null;
1021  await update($, (tracks) => {
1022    const active = activeOf(tracks);
1023    if (active?.isWorkBar && active.isAutoSteps) return replace(tracks, autoStep(active, step));
1024    if (hasTurnBar || turnSteps.length < AUTO_BAR_CALLS || (active && !isComplete(active))) return tracks;
1025    hasTurnBar = true;
1026    nudge = LIST_NUDGE;
1027    const done = turnSteps.map((s, i) => ({ id: `plan${i + 1}`, ...taskOf(s), status: "completed" }));
1028    const tasks = [...done, { id: `plan${done.length + 1}`, subject: "Working", status: "in_progress" }];
1029    const track = { ...newTrack(tracks, "Working"), startedAt: turnSteps[0].startedAt, tokens: turnTokens };
1030    return [...tracks, { ...track, tasks, isWorkBar: true, isAutoSteps: true, isCounter: true }];
1031  });
1032  return nudge;
1033}
1034
1035export function stepNameOf(call) {
1036  const file = String(call.file_path ?? call.path ?? "").split("/").filter(Boolean).at(-1);
1037  const name =
1038    call.description ??
1039    (call.tool === "Read" && file ? `Read ${file}` : null) ??
1040    (call.tool === "Write" && file ? `Write ${file}` : null) ??
1041    (call.tool === "Edit" && file ? `Edit ${file}` : null) ??
1042    (call.pattern ? `Search ${call.pattern}` : null) ??
1043    (call.tool === "Bash" ? String(call.command ?? "").trim().split(/\s+/)[0] || "Run a command" : null) ??
1044    String(call.tool ?? "Step").replace(/^mcp__.*__/, "");
1045  return shorten(String(name).replace(/\s+/g, " ").trim(), 40);
1046}
1047
1048function onPlanning($, change) {
1049  return update($, (tracks) => tracks.map((t) => (t.isPlanning && !t.isDismissed ? change(t) : t)));
1050}
1051
1052// Steps before `at` done, `at` in progress, the rest pending.
1053function toStep(track, at) {
1054  const tasks = track.tasks.map((t, i) => ({
1055    ...t,
1056    status: i < at ? "completed" : i === at ? "in_progress" : "pending",
1057  }));
1058  return { ...track, tasks };
1059}
1060
1061// A plan's numbered steps, else its bullets, as tasks: at most 8, short titles.
1062function planStepsOf(plan) {
1063  const all = typeof plan === "string" ? plan.split("\n") : [];
1064  // Steps live under the work headings; Context and Verification lists are not tasks.
1065  const SKIP = /^#{1,4}\s+.*\b(context|background|verif\w*|testing|test plan|risks?|notes?|files?|out of scope)\b/i;
1066  const WORK = /^#{1,4}\s+.*\b(steps?|implementation|phases?|changes?|approach|plan|tasks?)\b/i;
1067  const sections = [];
1068  for (const line of all) {
1069    if (/^#{1,4}\s/.test(line)) sections.push({ heading: line, lines: [] });
1070    else (sections.at(-1) ?? sections[sections.push({ heading: "", lines: [] }) - 1]).lines.push(line);
1071  }
1072  const kept = sections.filter((s) => !SKIP.test(s.heading));
1073  const work = kept.filter((s) => WORK.test(s.heading));
1074  const lines = (work.length > 0 ? work : kept).flatMap((s) => s.lines);
1075  const pick = (pattern) =>
1076    lines.map((line) => line.match(pattern)?.[1]).filter((text) => text && text.trim().length > 0);
1077  const numbered = pick(/^\s{0,3}\d+[.)]\s+(.+)/);
1078  const steps = (numbered.length > 0 ? numbered : pick(/^\s{0,1}[-*]\s+(.+)/)).slice(0, 8);
1079  const titles = steps.length > 0 ? steps : ["Carry out the plan"];
1080  return titles.map((text, i) => ({
1081    id: `step${i + 1}`,
1082    subject: shorten(text.replace(/[*_`#]/g, "").replace(/\s+/g, " ").trim(), 40),
1083    status: i === 0 ? "in_progress" : "pending",
1084  }));
1085}
1086
1087// The tool's short updates against the newest track.
1088function applyOps(track, ops) {
1089  let tasks = track.tasks;
1090  let failedCommand = track.failedCommand;
1091  if (Array.isArray(ops.add) && ops.add.length > 0) {
1092    const added = ops.add.map((subject, i) => ({ id: `add${tasks.length + i + 1}`, subject: String(subject), status: "pending" }));
1093    tasks = [...tasks, ...added];
1094    if (!tasks.some((t) => t.status === "in_progress")) {
1095      const first = tasks.findIndex((t) => t.status === "pending");
1096      tasks = tasks.map((t, i) => (i === first ? { ...t, status: "in_progress" } : t));
1097    }
1098  }
1099  if (ops.next === true || ops.skip === true) {
1100    const at = tasks.findIndex((t) => t.status === "in_progress");
1101    const upTo = at === -1 ? tasks.findIndex((t) => t.status !== "completed") : at;
1102    const closed = ops.skip === true ? { status: "completed", isSkipped: true } : { status: "completed" };
1103    tasks = tasks.map((t, i) =>
1104      i === upTo ? { ...t, ...closed } : i === upTo + 1 && t.status === "pending" ? { ...t, status: "in_progress" } : t,
1105    );
1106    failedCommand = null;
1107  }
1108  if (typeof ops.active === "string") {
1109    const at = findTask(tasks, ops.active);
1110    if (at !== -1) {
1111      tasks = tasks.map((t, i) =>
1112        i < at ? { ...t, status: "completed" } : i === at ? { ...t, status: "in_progress" } : t.status === "in_progress" ? { ...t, status: "pending" } : t,
1113      );
1114    }
1115  }
1116  let failedRuns = track.failedRuns;
1117  if (typeof ops.failed === "string") {
1118    failedCommand = ops.failed || "failed";
1119    failedRuns = (failedRuns ?? 0) + 1;
1120  }
1121  if (ops.fixed === true) failedCommand = null;
1122  const isSameFailure = failedCommand === track.failedCommand;
1123  return {
1124    ...track,
1125    tasks,
1126    failedCommand,
1127    failedRuns,
1128    failedReason: isSameFailure ? track.failedReason : null,
1129    failedLines: isSameFailure ? track.failedLines : null,
1130  };
1131}
1132
1133// Exact name first, then a prefix, then a part of the name; -1 when none fits.
1134export function findTask(tasks, name) {
1135  const want = String(name).trim().toLowerCase();
1136  const names = tasks.map((t) => t.subject.trim().toLowerCase());
1137  const exact = names.indexOf(want);
1138  if (exact !== -1 || want === "") return exact;
1139  const prefix = names.findIndex((n) => n.startsWith(want));
1140  return prefix !== -1 ? prefix : names.findIndex((n) => n.includes(want) || want.includes(n));
1141}
1142
1143function isComplete(track) {
1144  return track.tasks.length > 0 && track.tasks.every((t) => t.status === "completed");
1145}
1146
1147// Only a failing test, build, lint or typecheck turns a row red: grep, diff and
1148// test -f exit 1 as an answer, not a failure. The command's head decides, not any word in it.
1149const RUNNERS = /^(pytest|py\.test|tox|nox|jest|vitest|mocha|tsc|eslint|ruff|mypy|flake8|pylint|black|isort|pre-commit|rspec|phpunit|xcodebuild|gradle|gradlew|mvn|make|bazel|ctest)$/;
1150
1151export function isCheck(command) {
1152  return command.split(/&&|\|\||;|\|/).some((part) => {
1153    const words = part.trim().split(/\s+/).filter((w) => w && !w.startsWith("-"));
1154    while (words.length > 0 && (/^\w+=/.test(words[0]) || words[0] === "cd" || words[0] === "time")) {
1155      words.splice(0, words[0] === "cd" ? 2 : 1);
1156    }
1157    return isCheckWords(words);
1158  });
1159}
1160
1161function isCheckWords([first = "", ...rest]) {
1162  const head = first.split("/").at(-1);
1163  const [a = "", b = ""] = rest;
1164  if (RUNNERS.test(head)) return true;
1165  if (/^(npx|bunx|pnpx|uv|poetry|pipx)$/.test(head)) return isCheckWords(a === "run" || a === "exec" ? rest.slice(1) : rest);
1166  if (/^python[\d.]*$/.test(head)) return isCheckWords(rest);
1167  if (/^(npm|pnpm|yarn|bun)$/.test(head)) {
1168    return /^(test|t|build|lint|typecheck|check)$/.test(a) || (a === "run" && /^(test|build|lint|typecheck|type-check|check)(:|$)/.test(b));
1169  }
1170  if (head === "cargo") return /^(test|build|check|clippy|nextest)$/.test(a);
1171  if (head === "go") return /^(test|build|vet)$/.test(a);
1172  if (head === "dotnet" || head === "swift") return /^(test|build)$/.test(a);
1173  if (head === "claude") return a === "plugin" && /^(test|validate)$/.test(b);
1174  return false;
1175}
1176
1177const ERROR_LINE = /\b(error|errors|failed|failure|FAIL|assert\w*)\b|✕/i;
1178
1179// Up to 8 lines of a failed check's output for the pane: the lines that name a problem,
1180// else the last lines of the output.
1181function errorLinesOf(result) {
1182  const text = `${result?.stdout ?? ""}\n${result?.stderr ?? ""}`;
1183  const lines = text.split("\n").map((l) => l.trim()).filter(Boolean);
1184  const hits = lines.filter((l) => ERROR_LINE.test(l));
1185  return (hits.length > 0 ? hits.slice(0, 8) : lines.slice(-8)).map((l) => shorten(l, 200));
1186}
1187
1188// The first line of a failed check's output that names the problem.
1189function errorLineOf(result) {
1190  const text = `${result?.stdout ?? ""}\n${result?.stderr ?? ""}`;
1191  const line = text.split("\n").find((l) => /\b(error|errors|failed|failure|FAIL|assert\w*)\b|✕/i.test(l));
1192  return line ? line.trim() : null;
1193}
1194
1195// A re-run with other flags or paths still counts as the same check.
1196export function sameCheck(a, b) {
1197  const head = (c) => c.trim().split(/\s+/).filter((w) => !w.startsWith("-")).slice(0, 2).join(" ");
1198  return a === b || head(a) === head(b);
1199}
1200
hooks/bar.mjs 217 lines
1// two-row-smooth: a 2-row slider drawn on a 4-pixel-tall canvas.
2// Each cell is split into 2x2 quadrants (▀ ▄ ▌ ▐ ▖ ▗ ▘ ▝ ▙ ▛ ▜ ▟), so the track and
3// the pill are capsules with round ends at half-cell horizontal resolution. Edge
4// cells take an averaged fg/bg colour pair, which anti-aliases the curve. Inside
5// the filled part, plain cells switch to braille so the dot field has real gaps:
6// sparse and dim on the left, dense and bright at the pill, twinkling over time.
7export const ROWS = 2;
8
9const DEF = 0x01000000;
10const TERM_BG = 0x1a1a1c;
11const SPACE = 0x20;
12const TICK_TOP = 0x2577; // ╷  row 0: lower half of a line
13const TICK_BOT = 0x2575; // ╵  row 1: upper half of a line
14const H = 4; // pixel rows (2 per terminal row)
15const R = 2; // vertical capsule radius in pixels
16const RX_TRACK = 2; // horizontal cap radius in cells (circle: a half-cell pixel is ~square)
17const RX_PILL = 2;
18const LEVELS = 8; // dot brightness steps
19const COV_STEPS = 7; // anti-alias coverage steps
20
21// quadrant mask (TL=1, TR=2, BL=4, BR=8) → glyph drawn in fg
22const QUAD = [
23  0x20, 0x2598, 0x259d, 0x2580, 0x2596, 0x258c, 0x259e, 0x259b,
24  0x2597, 0x259a, 0x2590, 0x259c, 0x2584, 0x2599, 0x259f, 0x2588,
25];
26
27const PAL = {
28  working: {
29    empty: 0x29292f, fill: 0x2b2740, dotLo: 0x4a436e, dotHi: 0xe8e3ff,
30    tickFill: 0xb3a8f7, tickEmpty: 0x56555f,
31    pill: 0xaa9df6, pillMid: 0x988aea, pillLip: 0x7a6bd0, ink: 0x15112b,
32    twP: 540, shimmer: true, breathe: 0, twinkle: 0.18,
33  },
34  failed: {
35    empty: 0x29292f, fill: 0x36242a, dotLo: 0x67393b, dotHi: 0xffc4ba,
36    tickFill: 0xf59a8c, tickEmpty: 0x56555f,
37    pill: 0xef6f5c, pillMid: 0xdc5f4d, pillLip: 0xb44a3b, ink: 0x2a0a05,
38    twP: 1600, shimmer: false, breathe: 0, twinkle: 0.08,
39  },
40  done: {
41    empty: 0x29292f, fill: 0x1b2f2d, dotLo: 0x2f5a55, dotHi: 0xc8f3ec,
42    tickFill: 0x7fd6ca, tickEmpty: 0x56555f,
43    pill: 0x3cc0b0, pillMid: 0x2fae9f, pillLip: 0x228a7e, ink: 0x04201d,
44    twP: 0, shimmer: false, breathe: 1, twinkle: 0,
45  },
46  waiting: {
47    empty: 0x29292f, fill: 0x342c1f, dotLo: 0x6b5530, dotHi: 0xffe7b8,
48    tickFill: 0xf2c46d, tickEmpty: 0x56555f,
49    pill: 0xf0b34a, pillMid: 0xdca13d, pillLip: 0xb07d2a, ink: 0x2a1c05,
50    twP: 0, shimmer: false, breathe: 1, twinkle: 0,
51  },
52};
53
54const clamp01 = (v) => (v < 0 ? 0 : v > 1 ? 1 : v);
55const qcov = (v) => Math.round(clamp01(v) * (COV_STEPS - 1)) / (COV_STEPS - 1);
56function mix(a, b, k) {
57  if (k <= 0) return a;
58  if (k >= 1) return b;
59  const ch = (s) => {
60    const x = (a >> s) & 255, y = (b >> s) & 255;
61    return Math.round(x + (y - x) * k) & 255;
62  };
63  return (ch(16) << 16) | (ch(8) << 8) | ch(0);
64}
65function hash(a, b, c) {
66  let h = Math.imul(a | 0, 0x27d4eb2d) ^ Math.imul(b | 0, 0x165667b1) ^ Math.imul(c | 0, 0x9e3779b1);
67  h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
68  h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
69  h ^= h >>> 16;
70  return (h >>> 0) / 4294967296;
71}
72
73// Coverage of the box [x, x+w] x [y, y+1] by a capsule spanning x0..x1, 0..H
74// with elliptical ends (horizontal radius rx, vertical radius R).
75function capsuleCov(x, y, w, x0, x1, rx) {
76  if (x + w <= x0 || x >= x1) return 0;
77  let hit = 0;
78  for (let sy = 0; sy < 4; sy++) {
79    for (let sx = 0; sx < 4; sx++) {
80      const px = x + ((sx + 0.5) / 4) * w, py = y + (sy + 0.5) / 4;
81      if (px < x0 || px > x1) continue;
82      const cx = px < x0 + rx ? x0 + rx : px > x1 - rx ? x1 - rx : px;
83      const dx = (px - cx) / rx, dy = (py - R) / R;
84      if (dx * dx + dy * dy <= 1) hit++;
85    }
86  }
87  return hit / 16;
88}
89
90// Braille dot bits: [dotRow][dotCol]
91const BR = [[0x01, 0x08], [0x02, 0x10], [0x04, 0x20], [0x40, 0x80]];
92
93export function barCells({ width, done, total, status, pill, t, skipped = [] }) {
94  const W = width;
95  const P = PAL[status] || PAL.working;
96  const text = Array.from(String(pill ?? "")).slice(0, Math.max(1, W - 4));
97  const pillW = text.length + 4; // cap + pad + text + pad + cap
98  const frac = clamp01(total > 0 ? done / total : 0);
99  const fillEnd = status === "done" ? W : frac * W;
100  let pillX = Math.round(fillEnd - pillW / 2);
101  if (pillX > W - pillW) pillX = W - pillW;
102  if (pillX < 0) pillX = 0;
103  const pillEnd = pillX + pillW;
104  const textX = pillX + 2;
105
106  const breathe = P.breathe ? Math.round(0.5 + 0.5 * Math.sin((t / 3600) * Math.PI * 2)) : 0;
107  const span = Math.max(1, pillX);
108  const shimmerPos = P.shimmer ? ((t % 2200) / 2200) * (span + 8) - 4 : -99;
109
110  const ticks = new Set();
111  for (let i = 1; i < total; i++) ticks.add(Math.round((i / total) * W));
112
113  const out = new Array(W * 2);
114  for (let r = 0; r < 2; r++) {
115    for (let x = 0; x < W; x++) {
116      const trackCol = x < pillX + pillW / 2 ? P.fill : P.empty;
117      const inPill = x >= pillX && x < pillEnd;
118      let cell;
119      if (r === 0 && x >= textX && x < textX + text.length) {
120        cell = [text[x - textX].codePointAt(0), P.ink, P.pill];
121      } else {
122        // four quadrant samples: TL, TR, BL, BR
123        const ct = [], cp = [], pc = [];
124        for (let q = 0; q < 4; q++) {
125          const qx = x + (q & 1) * 0.5, qy = 2 * r + (q >> 1);
126          ct.push(capsuleCov(qx, qy, 0.5, 0, W, RX_TRACK));
127          cp.push(inPill ? capsuleCov(qx, qy, 0.5, pillX, pillEnd, RX_PILL) : 0);
128          pc.push(qy === 0 || qy === 1 ? P.pill : qy === 2 ? P.pillMid : P.pillLip);
129        }
130        const solidTrack = ct.every((v) => v === 1) && cp.every((v) => v === 0);
131        if (solidTrack && !inPill && ticks.has(x) && x > 2 && x < W - 3) {
132          cell = [r === 0 ? TICK_TOP : TICK_BOT, x < pillX ? P.tickFill : P.tickEmpty, trackCol];
133        } else if (solidTrack && x < pillX) {
134          cell = dotCell(x, r, pillX, span, shimmerPos, breathe, t, P);
135          // A skipped task's slice: its dots fade toward the fill.
136          if (skipped.includes(Math.floor((x / W) * total))) cell = [cell[0], mix(cell[1], P.fill, 0.65), cell[2]];
137        } else {
138          cell = shapeCell(ct, cp, pc, trackCol);
139        }
140      }
141      out[r * W + x] = cell;
142    }
143  }
144  return out;
145}
146
147// Split the 4 quadrants into inside/outside of the top-most edge, average each
148// group's coverage (quantized), and draw the matching quadrant glyph.
149function shapeCell(ct, cp, pc, trackCol) {
150  const pillEdge = cp.some((v) => v > 0);
151  const cov = pillEdge ? cp : ct;
152  let mask = 0;
153  for (let q = 0; q < 4; q++) if (cov[q] >= 0.5) mask |= 1 << q;
154  const group = (inside) => {
155    let n = 0, sct = 0, scp = 0, lip = 0;
156    for (let q = 0; q < 4; q++) {
157      if (((mask >> q) & 1) !== (inside ? 1 : 0)) continue;
158      n++; sct += ct[q]; scp += cp[q];
159      lip += q >> 1; // bottom quadrants carry the darker pill rows
160    }
161    if (!n) return null;
162    const t = qcov(sct / n), p = qcov(scp / n);
163    if (t === 0 && p === 0) return -1; // terminal background
164    let c = mix(TERM_BG, trackCol, t);
165    if (p > 0) {
166      const top = pc[0], bot = pc[2];
167      c = mix(c, lip === 0 ? top : lip === n ? bot : mix(top, bot, 0.5), p);
168    }
169    return c;
170  };
171  let fg = group(true), bg = group(false);
172  // full pill cell on the lower row: show the face/lip bevel with ▀
173  if (mask === 15 && pillEdge && pc[0] !== pc[2]) {
174    return [0x2580, pc[0], pc[2]];
175  }
176  if (fg === null) return bg === -1 ? [SPACE, TERM_BG, DEF] : [SPACE, bg, bg];
177  if (bg === null || fg === bg) return fg === -1 ? [SPACE, TERM_BG, DEF] : [SPACE, fg, fg];
178  if (fg === -1) { // draw the visible part as fg instead
179    return [QUAD[15 - mask], bg, DEF];
180  }
181  return [QUAD[mask], fg, bg === -1 ? DEF : bg];
182}
183
184function dotCell(x, r, pillX, span, shimmerPos, breathe, t, P) {
185  const u = clamp01((x + 1) / span); // 0 at the left cap, 1 next to the pill
186  const near = pillX - x; // 1 = cell touching the pill
187  const sh = shimmerPos > -50 ? Math.max(0, 1 - Math.abs(x - shimmerPos) / 3.5) : 0;
188  let density = 0.1 + 0.8 * Math.pow(u, 1.6) + 0.35 * sh;
189  if (near <= 2) density = Math.max(density, near === 1 ? 0.97 : 0.88);
190
191  let bits = 0;
192  // row 0 uses dot rows 1..3, row 1 uses dot rows 0..2 → one dot of margin top and bottom
193  for (let dr = r === 0 ? 1 : 0; dr < (r === 0 ? 4 : 3); dr++) {
194    for (let dc = 0; dc < 2; dc++) {
195      const id = (r * 4 + dr) * 2 + dc;
196      let v = hash(x, id, 1);
197      if (P.twP) {
198        // most dots keep their seat; a few re-roll each staggered epoch → twinkle
199        const epoch = Math.floor((t + hash(x, id, 7) * P.twP) / P.twP);
200        if (hash(x, id, 1000 + epoch) < P.twinkle) v = hash(x, id, 2000 + epoch);
201      }
202      if (v < density) bits |= BR[dr][dc];
203    }
204  }
205  if (!bits) return [SPACE, P.dotLo, P.fill];
206
207  let lvl = Math.round(1 + 4.6 * Math.pow(u, 1.3));
208  if (near <= 2) lvl = near === 1 ? 7 : 6;
209  if (P.twP) {
210    const e = Math.floor((t + hash(x, r, 9) * P.twP * 2) / (P.twP * 2));
211    if (hash(x, r, 3000 + e) < P.twinkle * 0.9) lvl += 1;
212  }
213  lvl += Math.round(2.2 * sh) + breathe;
214  if (lvl > LEVELS - 1) lvl = LEVELS - 1;
215  return [0x2800 + bits, mix(P.dotLo, P.dotHi, lvl / (LEVELS - 1)), P.fill];
216}
217
hooks/bar-svg.mjs 149 lines
1// The desktop bar: one SVG, like the Ultracode effort slider. A rounded track,
2// a grid of square dots that grows denser and brighter toward a rounded pill
3// riding the fill edge, and tick marks between tasks. With `isMoving`, SMIL
4// makes the dots twinkle and a soft shine sweep toward the pill; otherwise the
5// same picture holds still.
6
7export const SVG_HEIGHT = 24;
8
9const DOT = 3;
10const PITCH = 4;
11const DOT_ROWS = 4;
12const MAX_COLUMNS = 150;
13const PILL_FONT = 11;
14const PILL_CHAR = 6.7;
15
16const PALETTE = {
17  working: { track: "#2a2838", dot: "#b9adff", tick: "#e6e1ff", pill: "#a99cf5", ink: "#1d1a2e" },
18  failed: { track: "#33242a", dot: "#ff9a8a", tick: "#ffe1db", pill: "#ef6f5c", ink: "#2a0a05" },
19  waiting: { track: "#332b1f", dot: "#ffd27a", tick: "#fff0d1", pill: "#f0b34a", ink: "#2a1c05" },
20  done: { track: "#1b2f2d", dot: "#8fe0d5", tick: "#dcf5f1", pill: "#2bb5a5", ink: "#04201d" },
21};
22const EMPTY_TICK = "#55545e";
23const BAND_DARK = "#212121";
24
25export function barSvg({ width, done, total, status, pill, isMoving, skipped = [] }) {
26  const p = PALETTE[status];
27  const h = SVG_HEIGHT;
28  const r = h / 2;
29  const pillW = Math.min(width, Math.ceil(pill.length * PILL_CHAR) + 22);
30  const fillX = Math.round((done / total) * width);
31  const pillX = Math.max(0, Math.min(width - pillW, fillX - pillW / 2));
32  const fillEnd = Math.max(pillX + r, fillX);
33
34  const parts = [
35    // A moving bar is drawn in a sandboxed frame; without a matching color-scheme the browser paints
36    // an opaque white backdrop. The corners outside the track take the band's colour when dark.
37    isMoving ? `<style>:root{color-scheme:light dark}.bd{fill:none}@media (prefers-color-scheme:dark){.bd{fill:${BAND_DARK}}}</style><rect class="bd" width="${width}" height="${h}"/>` : "",
38    `<defs><clipPath id="track"><rect width="${width}" height="${h}" rx="${r}"/></clipPath>`,
39    `<clipPath id="fill"><rect width="${fillEnd}" height="${h}"/></clipPath>`,
40    `<linearGradient id="shine" x1="0" x2="1"><stop offset="0" stop-color="#fff" stop-opacity="0"/>`,
41    `<stop offset=".5" stop-color="#fff" stop-opacity=".22"/><stop offset="1" stop-color="#fff" stop-opacity="0"/></linearGradient></defs>`,
42    `<rect width="${width}" height="${h}" rx="${r}" fill="${p.track}"/>`,
43    `<g clip-path="url(#track)"><g clip-path="url(#fill)">`,
44    dots(p, fillEnd, h, status, isMoving),
45  ];
46  if (isMoving && status === "working" && fillEnd > 40) {
47    const band = Math.max(40, fillEnd * 0.25);
48    parts.push(
49      `<rect x="${-band}" width="${band}" height="${h}" fill="url(#shine)">`,
50      `<animate attributeName="x" from="${-band}" to="${fillEnd}" dur="1.1s" repeatCount="indefinite"/></rect>`,
51    );
52  }
53  // A skipped task's slice is veiled in the track colour, so its dots read dimmer.
54  for (const i of skipped) {
55    const x0 = Math.round((i / total) * width);
56    const x1 = Math.round(((i + 1) / total) * width);
57    parts.push(`<rect class="skip" x="${x0}" width="${x1 - x0}" height="${h}" fill="${p.track}" fill-opacity=".65"/>`);
58  }
59  parts.push(`</g>`);
60  for (let k = 1; k < total; k++) {
61    const x = Math.round((k / total) * width);
62    if (x > pillX - 2 && x < pillX + pillW + 2) continue;
63    const isFilled = x < fillX;
64    parts.push(
65      `<rect x="${x - 0.5}" y="${h * 0.25}" width="1" height="${h * 0.5}" rx=".5" fill="${isFilled ? p.tick : EMPTY_TICK}" fill-opacity="${isFilled ? 0.9 : 0.8}"/>`,
66    );
67  }
68  parts.push(`</g>`);
69  parts.push(
70    `<rect x="${pillX}" y="1" width="${pillW}" height="${h - 2}" rx="${(h - 2) / 2}" fill="${p.pill}"/>`,
71    `<text x="${pillX + pillW / 2}" y="${h / 2}" fill="${p.ink}" font-family="-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif" font-size="${PILL_FONT}" font-weight="600" text-anchor="middle" dominant-baseline="central">${escape(pill)}</text>`,
72  );
73  return `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${h}" viewBox="0 0 ${width} ${h}">${parts.join("")}</svg>`;
74}
75
76// Square dots on a grid: sparse and faint at the left, dense and bright at the
77// fill edge. Which dots show and how they twinkle come from a hash of their
78// place, so the same bar always draws the same field.
79//
80// A moving bar animates groups, not dots: each dot joins one of TWINKLE_GROUPS
81// groups and each group fades as one, and the glitter is a white copy of some
82// dots in SPARKLE_GROUPS groups. About a dozen animations in all, where one per
83// dot came to several hundred and made the frame stutter.
84const TWINKLE_GROUPS = 8;
85const SPARKLE_GROUPS = 5;
86
87function dots(p, fillEnd, h, status, isMoving) {
88  const top = (h - (DOT_ROWS - 1) * PITCH - DOT) / 2;
89  // At most MAX_COLUMNS dot columns, so a wide bar stays under the SVG size cap.
90  const step = Math.max(PITCH, fillEnd / MAX_COLUMNS);
91  const twinkles = Array.from({ length: TWINKLE_GROUPS }, () => []);
92  const sparkles = Array.from({ length: SPARKLE_GROUPS }, () => []);
93  for (let x = PITCH; x < fillEnd - 2; x += step) {
94    const ramp = Math.pow(x / Math.max(1, fillEnd), 1.6);
95    for (let row = 0; row < DOT_ROWS; row++) {
96      const seed = hash(x * 31 + row * 7);
97      if (seed > 0.3 + ramp * 0.7) continue;
98      const opacity = Math.min(1, (0.3 + ramp * 0.8) * (0.75 + 0.35 * hash(seed * 997)));
99      const at = `x="${Math.round(x)}" y="${top + row * PITCH}" width="${DOT}" height="${DOT}" rx=".4"`;
100      twinkles[Math.floor(hash(seed * 13) * TWINKLE_GROUPS)].push(`<rect ${at} fill-opacity="${opacity.toFixed(2)}"/>`);
101      // Glitter: some dots, more of them near the pill, flash white for a blink.
102      if (isMoving && status === "working" && hash(seed * 53) < 0.08 + ramp * 0.22) {
103        sparkles[Math.floor(hash(seed * 71) * SPARKLE_GROUPS)].push(`<rect ${at}/>`);
104      }
105    }
106  }
107  if (!isMoving) return `<g fill="${p.dot}">${twinkles.flat().join("")}</g>`;
108  const slow = status === "failed" ? 1.8 : status === "done" ? 2.2 : 1;
109  const out = twinkles.map((group, i) => {
110    if (group.length === 0) return "";
111    const dur = ((0.35 + (i / TWINKLE_GROUPS) * 0.55) * slow).toFixed(2);
112    const begin = (-hash(i * 29 + 1) * Number(dur)).toFixed(2);
113    const fade = `<animate attributeName="opacity" values="1;.15;1" dur="${dur}s" begin="${begin}s" repeatCount="indefinite"/>`;
114    return `<g fill="${p.dot}">${fade}${group.join("")}</g>`;
115  });
116  sparkles.forEach((group, i) => {
117    if (group.length === 0) return;
118    const dur = (0.9 + (i / SPARKLE_GROUPS) * 1.3).toFixed(2);
119    const begin = (-hash(i * 89 + 3) * Number(dur)).toFixed(2);
120    const flash = `<animate attributeName="opacity" values="0;1;0;0" keyTimes="0;.06;.16;1" dur="${dur}s" begin="${begin}s" repeatCount="indefinite"/>`;
121    out.push(`<g fill="#ffffff" opacity="0">${flash}${group.join("")}</g>`);
122  });
123  return out.join("");
124}
125
126function hash(n) {
127  const s = Math.sin(n * 12.9898 + 78.233) * 43758.5453;
128  return s - Math.floor(s);
129}
130
131function escape(text) {
132  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
133}
134
135// A status pill on its own, for the Progress pane's header: a rounded capsule in the
136// state's colour. Drawn as a still image; returns its source and size.
137export function pillSvg({ status, text }) {
138  const p = PALETTE[status];
139  const h = 22;
140  const w = Math.ceil(text.length * 6.9) + 22;
141  const source = [
142    `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">`,
143    `<rect width="${w}" height="${h}" rx="${h / 2}" fill="${p.pill}"/>`,
144    `<text x="${w / 2}" y="${h / 2}" fill="${p.ink}" font-family="-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif" font-size="12" font-weight="700" text-anchor="middle" dominant-baseline="central">${escape(text)}</text>`,
145    `</svg>`,
146  ].join("");
147  return { source, width: w, height: h };
148}
149
hooks/timeline-svg.mjs 98 lines
1// The gutter of a bar's task list: one small SVG per row that together draw one
2// connected timeline. Each row draws its own part of the line, edge to edge, so the rows
3// join up. Steps that ran at the same time take two lanes: the first stays on the main
4// line and the line forks from its dot; the rest run on the branch and the last one
5// joins back. A sub-agent's row hangs on the branch with a robot head instead of a dot.
6//
7// SMIL moves it once: on opening, the line draws itself down and the dots pop in row by
8// row, each with one ring. Then it holds still. Each row is its own frame with its own
9// clock, so a pulse looping across rows drifted out of step and kept every frame busy.
10
11export const ROW_HEIGHT = 22;
12export const GUTTER_WIDTH = 30;
13
14const MAIN_X = 7;
15const BRANCH_X = 21;
16const LINE = "#2f6f68";
17const PENDING = "#5d5c68";
18const ACTIVE = "#a99cf5";
19const FAILED = "#ef6f5c";
20const DONE = "#2bb5a5";
21const AGENT = "#a99cf5";
22const BAND_DARK = "#212121";
23
24const STAGGER = 0.07; // seconds between rows as the line draws in
25const DRAW = 0.18;
26
27// pos: "main" | "fork" (both on the main line; the branch leaves a fork row's dot) |
28// "start" | "middle" | "end" | "only" (rows on the branch).
29export function timelineSvg({ pos, isFirst, isLast, isAgent, isSkipped, status = "completed", failures = 0, index = 0 }) {
30  const h = ROW_HEIGHT;
31  const mid = h / 2;
32  const isBranch = pos !== "main" && pos !== "fork";
33  const x = isBranch ? BRANCH_X : MAIN_X;
34  const drawAt = index * STAGGER;
35  const parts = [
36    // Drawn in a sandboxed frame; without a matching color-scheme the browser paints an
37    // opaque white backdrop. Dark, the corners take the band's colour.
38    `<style>:root{color-scheme:light dark}.bd{fill:none}@media (prefers-color-scheme:dark){.bd{fill:${BAND_DARK}}}</style>`,
39    `<rect class="bd" width="${GUTTER_WIDTH}" height="${h}"/>`,
40  ];
41  const draw = `<animate attributeName="stroke-dashoffset" from="1" to="0" begin="${drawAt}s" dur="${DRAW}s" fill="freeze"/>`;
42  const stroke = (shape) =>
43    parts.push(`${shape} fill="none" stroke="${LINE}" stroke-width="1.5" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1">${draw}</path>`);
44  const line = (x1, y1, x2, y2) => stroke(`<path d="M${x1} ${y1} L${x2} ${y2}"`);
45  const curve = (d) => stroke(`<path d="${d}"`);
46
47  // The main line runs through every row; it starts at the first dot and ends at the last.
48  const top = isFirst && !isBranch ? mid : 0;
49  const bottom = isLast && !isBranch ? mid : h;
50  line(MAIN_X, top, MAIN_X, bottom);
51
52  if (pos === "fork") curve(`M${MAIN_X} ${mid} Q${BRANCH_X} ${mid} ${BRANCH_X} ${h}`);
53  if (pos === "start" || pos === "only") curve(`M${MAIN_X} 1 Q${BRANCH_X} 1 ${BRANCH_X} ${mid}`);
54  if (pos === "middle" || pos === "end") line(BRANCH_X, 0, BRANCH_X, mid);
55  if (pos === "start" || pos === "middle") line(BRANCH_X, mid, BRANCH_X, h);
56  if (pos === "end" || pos === "only") curve(`M${BRANCH_X} ${mid} Q${BRANCH_X} ${h - 1} ${MAIN_X} ${h - 1}`);
57
58  const popAt = drawAt + DRAW * 0.6;
59  if (isAgent) parts.push(robot(x, mid, popAt, index));
60  else parts.push(dot(x, mid, { isSkipped, status, failures }, popAt));
61  return `<svg xmlns="http://www.w3.org/2000/svg" width="${GUTTER_WIDTH}" height="${h}" viewBox="0 0 ${GUTTER_WIDTH} ${h}">${parts.join("")}</svg>`;
62}
63
64// Done: a teal dot that rings once. A step that failed on the way keeps a coral ring;
65// the running step is purple; pending and skipped steps are hollow.
66function dot(x, y, { isSkipped, status, failures }, popAt) {
67  const pop = `<animate attributeName="r" values="0;5;4" keyTimes="0;.7;1" begin="${popAt}s" dur=".25s" fill="freeze"/>`;
68  if (isSkipped || status === "pending") {
69    const edge = isSkipped ? DONE : PENDING;
70    return `<circle cx="${x}" cy="${y}" r="0" fill="${BAND_DARK}" stroke="${edge}" stroke-opacity=".7" stroke-width="1.5">${pop}</circle>`;
71  }
72  const fill = status === "in_progress" ? ACTIVE : DONE;
73  const scar = failures > 0 ? `<circle cx="${x}" cy="${y}" r="7" fill="none" stroke="${FAILED}" stroke-width="1.5" stroke-opacity=".75"/>` : "";
74  const ring = [
75    `<circle cx="${x}" cy="${y}" r="4" fill="none" stroke="${fill}" stroke-width="1.2" stroke-opacity="0">`,
76    `<animate attributeName="r" values="4;9" begin="${popAt}s" dur=".6s" fill="freeze"/>`,
77    `<animate attributeName="stroke-opacity" values=".8;0" begin="${popAt}s" dur=".6s" fill="freeze"/>`,
78    `</circle>`,
79  ].join("");
80  return `${scar}${ring}<circle cx="${x}" cy="${y}" r="0" fill="${fill}">${pop}</circle>`;
81}
82
83// A robot head: antenna with a blinking light, rounded face, two eyes that blink.
84function robot(x, y, popAt, index) {
85  const blinkAt = (index * 0.37) % 2.8;
86  const eye = (ex) =>
87    `<circle cx="${ex}" cy="${y}" r="1.4" fill="#1d1a2e"><animate attributeName="r" values="1.4;1.4;.25;1.4" keyTimes="0;.9;.95;1" dur="2.8s" begin="${blinkAt}s" repeatCount="3"/></circle>`;
88  return [
89    `<g opacity="0"><animate attributeName="opacity" from="0" to="1" begin="${popAt}s" dur=".25s" fill="freeze"/>`,
90    `<line x1="${x}" y1="${y - 8}" x2="${x}" y2="${y - 5}" stroke="${AGENT}" stroke-width="1.5"/>`,
91    `<circle cx="${x}" cy="${y - 8.5}" r="1.4" fill="${AGENT}"><animate attributeName="fill-opacity" values="1;.25;1" dur="1.2s" repeatCount="5"/></circle>`,
92    `<rect x="${x - 6}" y="${y - 5}" width="12" height="10" rx="3" fill="${AGENT}"/>`,
93    eye(x - 2.5),
94    eye(x + 2.5),
95    `</g>`,
96  ].join("");
97}
98
types/index.d.ts 81 lines
1export type ProgressTask = {
2  id: string;
3  subject: string;
4  status: "pending" | "in_progress" | "completed";
5  startedAt?: number | null;
6  doneAt?: number | null;
7  isSkipped?: boolean;
8  tokens?: number;
9  failures?: number;
10};
11
12export type ProgressTrack = {
13  id: string;
14  label: string | null;
15  tasks: ProgressTask[];
16  failedCommand: string | null;
17  failedRuns?: number;
18  isDismissed: boolean;
19  isWaiting?: boolean;
20  isExpanded?: boolean;
21  // An automatic bar (Working, Planning): it counts steps, its total unknown.
22  isCounter?: boolean;
23  // Measured time: turns that overlapped the track, and waits on the person.
24  turnMs?: number;
25  waitMs?: number;
26  waitingSince?: number | null;
27  // Tokens the API reported for the model calls made while the track ran. input counts
28  // every prompt token read (uncached, cache reads and cache writes); cached is the reads.
29  tokens?: { input: number; cached: number; output: number; calls: number };
30};
31
32// The Progress pane's log: what waited on the person, and findings.
33export type ProgressBlock = {
34  id?: string;
35  kind: "permission" | "question" | "approval";
36  text: string;
37  task: string | null;
38  at: number;
39  answeredAt: number | null;
40  toolUseId?: string | null;
41  // How it ended: the call's outcome, the plan's answer, or whether a question was answered.
42  outcome?: string;
43  // A question's answers, keyed by the question's text.
44  answers?: Record<string, string> | null;
45  details?: {
46    tool?: string;
47    request?: string;
48    questions?: { question: string; options: string[] }[];
49    plan?: string;
50    steps?: number;
51  } | null;
52  isDemo?: boolean;
53};
54
55export type ProgressFinding = {
56  id?: string;
57  kind: "found" | "failed";
58  text: string;
59  task: string | null;
60  at: number;
61  toolUseId?: string | null;
62  // A failing check: its command and the output lines that name the problem.
63  command?: string;
64  lines?: string[];
65  isDemo?: boolean;
66};
67
68export type ProgressLog = { blocks: ProgressBlock[]; findings: ProgressFinding[] };
69
70declare module "claude-code" {
71  interface PluginState {
72    "progress-tracker": {
73      tracks: ProgressTrack[];
74      log: ProgressLog;
75      tab: "blocked" | "found";
76      turn: number | null;
77      open: string | null;
78    };
79  }
80}
81