SLOPSHOPPER

cc-dashboard

A live agent dashboard built on Flightdeck: main model vitals (with Fable weekly usage), the architect, every permission check, subagent cards and swimlanes…

paneguardcommandstatuspromptprocess
v0.2.0MITupdated 2026-10-18kaisudo0520/cc-dashboard
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · cc-dashboard
│ ┃ Dashboard ✕ › fix the failing auth test and add an audit log call │ ┃ CC DASHBOARD · — IDLE │ ┃ ■ main ■ agents ■ gate ⏺ Read(src/auth.ts) │ ┃ ╭─────────────────────────────────────────── ⎿ Read 6 lines │ ┃ │ — · main ⏺ Update(src/auth.ts) │ ┃ │ effort ▯▯▯▯ — 0 req ⎿ Added 2 lines, removed 1 line │ ┃ │ 5h ━━━━━━━━━━─────────────────────── 31% ⏺ Bash(bun test) │ ┃ ╰─────────────────────────────────────────── ⎿ 3 pass, 1 fail │ ┃ ▣ client module ./rail.tsx │ ┃ Context ██░░░ 49% 97k/200k ● Done. refresh now rejects expired claims and logs an audit event. │ ┃ │ ┃ ▣ client module ./rail.tsx ✻ Worked for 42s · done 4:20 PM │ ┃ ╭─────────────────────────────────────────── │ ┃ │ ✓ last turn 42s · 0 agents · 3 edits · 1 e › /dashboard │ ┃ ╰─────────────────────────────────────────── ⎿ cc-dashboard: Dashboard opened and brought to the front when the │ ┃ │ ┃ ╭─────────────────────────────────────────── │ ┃ │ session log │ ┃ │ 08:53:20 main Edit → src/auth.ts │ ┃ │ 08:53:20 main Write → src/audit.ts │ ┃ │ 08:53:20 main Write → src/cache.ts │ ┃ │ 08:53:20 main Bash → bun test ✗ │ ┃ ╰─────────────────────────────────────────── │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts ⚠ cc-dashboard: ctx 49%

Draws

Pane · Dashboard
CC DASHBOARD · — IDLE ■ main ■ agents ■ gate ╭──────────────────────────────────────────────────────╮ │ — · main ○ idle │ │ effort ▯▯▯▯ — 0 req │ │ 5h ━━━━━━━━━━─────────────────────── 31% │ ╰──────────────────────────────────────────────────────╯ ▣ client module ./rail.tsx Context ██░░░ 49% 97k/200k ▣ client module ./rail.tsx ╭──────────────────────────────────────────────────────╮ │ ✓ last turn 42s · 0 agents · 3 edits · 1 error │ ╰──────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────╮ │ session log │ │ 08:53:20 main Edit → src/auth.ts │ │ 08:53:20 main Write → src/audit.ts │ │ 08:53:20 main Write → src/cache.ts │ │ 08:53:20 main Bash → bun test ✗ │ ╰──────────────────────────────────────────────────────╯
README

cc-dashboard

English · 繁體中文

A Claude Code mod that draws a live mission-control pane beside the transcript (or above the prompt). It shows only real session events and readings: no made-up numbers.

The docked pane while three subagents run (two done, one running): main and Codex usage side by side, the context line, branch and gate side by side, the architect, the agent cards with their flow lines, the turn receipt

Built on Flightdeck (Stephen Casella, MIT). The layouts, panels, interactions and English labels come from Flightdeck. cc-dashboard adds:

  • A Codex usage panel: 5-hour and weekly usage. It normally reads local files only; the server is asked for fresh figures only when you press ↻.
  • A branch panel: branch, sync state, and how many files are unreviewed and how many changed.
  • Fable weekly usage in the main box.
  • A red "waiting on you" line when another Claude Code session needs you (this session also writes its own wait mark).
  • codex exec runs shown as subagent cards (no Codex session id).
  • An orange flow line under the main box: it runs while the main session works, matching the subagents' line.
  • Paired layout, a context line of its own, and a dock width that follows the terminal.

Panels

| Panel | Shows | From | | :- | :- | :- | | Waiting on you (red line under the header) | Only when another running Claude Code session is waiting on you: ! waiting on you: <session name> <permission/question> <time, e.g. 3m ago>, with · +<n> more when several (longest wait first). Absent, and taking no row, when nobody waits | See "Waiting on you" below | | Main | Model, effort, permission mode, request count. Claude's first two usage windows (5h, 7d) and Fable weekly usage get a row each: a stretched ━─ bar, percent used, and the time to reset (⏱). Past its reset a window counts as restarted and reads ~0% and stale until a new reading arrives; a failed Fable refresh labels it Fable~. Bars in one box share a width and line up. Right under the box is an orange flow line as wide as the box: packets run while the main loop works, a still dim line otherwise; two boxes to a row put it under the main box's side only; motion: off keeps it still. The Context meter (█░ bar, percent used, 116k/200k, compactions ⟲) is not in the box: it is a frameless line of its own under the main box (or the pair) and the orange line, about half the column wide and left-aligned (the left column in the wide layout, the whole pane otherwise), with a blank row below it. The filled bar and the percent share one colour: green under 50%, yellow from 50%, red from 80% (following the chosen palette); the track is faint. Not drawn when panels has no main, nor while no context reading exists | turn.step, session.measure, session.compact, $.session.usage(); Fable below | | Codex | Codex · <model> · <effort>, then 5h and weekly windows, a row each: a percent-used bar and the time to reset. A window past its reset counts as restarted (Codex only opens a new window at its first request after the reset) and reads ~0% with stale until a new reading arrives. When no figures can be read, the reason shows (e.g. no usage recorded yet). The ↻ button at the right of the title (hotkey r) asks the server for fresh figures only when pressed, and says how long ago it last did (↻ 3m ago) | See "Codex usage" below | | Branch | 🔀 <branch>, ↔ <upstream> ↑ahead ↓behind (no upstream without one), and unreviewed N files · changed M files. All reviewed reads ✓ unreviewed 0; when it cannot be known it reads unreviewed ?, never 0. With unreviewed files, u unfolds the list (up to 8 rows, the rest counted as … N more) | See "Branch" below | | Architect | A timeline of consults, whether one is running, how long the last took; the inferred moment of each (before a plan / error repeats / before done, marked (inferred)); the first line of a subagent architect's advice | An agent type that matches the pattern, or a matching server tool in the assistant's rows | | Gate | One cell per permission check: green allowed by a rule, blue decided by the classifier or by you and then run, amber pending, red ✗ denied; checks made inside a subagent are dimmer. Totals, and a drill-down per tool family (f file / s shell / o other) of the last 5 checks, credentials masked | tool.check, settled by the tool.call around it | | Agents | Cards side by side while they fit: task, type, model (left out when it matches the main loop's) and effort (high; a numeric effort as the number; none, none shown), ctx and output tokens, steps, a running clock, max_tokens in red. Beyond that, swimlanes on one time axis. 1–6 expand: the full task, model and effort, the last 3 tool calls, the start of the answer. A codex exec is a card too: type Codex, the model and effort the Codex panel read from its config, the task its purpose (Codex plan draft / Codex review / Codex resume / Codex, with the call's description), counted as running; no Codex session id is shown | agent.spawn, turn.step, tool.call, turn.complete; Codex below | | Other loops | Model loops that match no card: workflow agents, compactions, memory forks | turn.step ids no card claims | | Receipt | The running turn, or the last one: duration, agents, edits, errors | turn.start, turn.complete | | Log | Prompts, spawns, completions, consults, edits, errors and denials; filtered to one agent while you view its transcript | All of the above |

A panel with nothing to show takes no room: no subagents, no agents panel; outside a git repository, no branch panel; no readable Codex, no Codex panel.

Layouts.

  • Wide (docked, 110 columns or more): the left column holds main, Codex, architect and gate; the right column holds branch, agents, other loops and receipt; the log runs full width at the bottom.
  • Single column with the layout on auto (a pane under 110 columns): from 70 columns (about a 180-column terminal at a 40% dock) two pairs share a row: main | Codex, then branch | gate (the gate moves above the architect). After them come architect, agents, other loops, receipt, log. The two boxes of a pair are the same height; the shorter one stretches. If one of the two has nothing to show, or panels leaves them apart, the other fills its row as before. compact never pairs, and below 70 columns the stacked layout is unchanged. Inside a pair the wording narrows: the main box's effort, mode and request count fold into ▮▮▮▯ high · auto · 12 req; the Codex box drops quota · used; the branch box puts the sync state and each count on a row of their own; the gate's summary and buttons wrap, so ✗ N denied is never cut.
  • Inline above the prompt (not fullscreen): a summary of at most 8 rows. The first row is the model, the context and Claude's 5h 2% · 7d 3% (from the API response, available after the session's first reply); the red line is on the second; Fable, Codex and the branch share one row: Fable 31% Codex 5h 13% · weekly 40% 🔀 main unreviewed 2 files · changed 5 files. While those rows show, fewer agent rows are listed so the total stays within 8.

Dock (fullscreen)

After /tui fullscreen (or "tui": "fullscreen" in settings.json, effective at the next start) the pane docks beside the transcript at dockPercent% of the terminal's width (default 40%, less the frame, at least 40 columns) and follows the terminal within about 2 seconds of a resize, but only while the terminal is at least 144 columns wide (narrower, it keeps its width). The terminal's width is known only from a drawing of the terminal's own pane or from running /dashboard, so at first the dock keeps the engine's default width and takes its share after the first drawing; a Desktop or phone attached to the session does not affect it. A width you dragged the dock to wins (an engine rule). The fullscreen layout docks from 110 columns; opening on start (when nobody asked) needs 144, and below that /dashboard opens it. /dashboard asks to bring the pane to the front and focus it (the engine grants that while the prompt has the keys and the composer is empty; Esc returns to the prompt).

Use

Install. Clone the repository to a fixed folder of your choice:

git clone https://github.com/kaisudo0520/cc-dashboard.git <folder>

To try it in one session: claude --plugin-dir <folder>. To load it in every session, put the folder in CLAUDE_CODE_PLUGIN_DIRS under env in ~/.claude/settings.json:

{ "env": { "CLAUDE_CODE_PLUGIN_DIRS": "<folder>" } }

Update. Run git pull in that folder; new sessions use the new version.

Developing the mod. Keep the working copy you edit apart from the copy your everyday sessions load. Claude Code reloads a mod in every running session that loads its folder whenever the folder's files change, so while you edit, the pane in those sessions can go blank or end up behind another pane's tab mid-edit; /dashboard or restarting the session brings it back.

| Command | Does | | :- | :- | | /dashboard | open the pane | | /dashboard close | close it | | /dashboard reset | clear agents, checks, consults, log and the turn (usage, compactions, Codex and branch stay) | | /dashboard refresh | ask the server for Codex usage (same as ↻), and read the branch and the other sessions' waits again now (whether or not the pane shows; Fable keeps its own schedule) | | /dashboard layout auto\|compact\|wide\|mini | override the layout for this session |

With the pane focused (ctrl+x tab, or /dashboard): 1–6 expand an agent card or lane, f s o open the gate's drill-down, u unfolds the unreviewed files, r asks the server for Codex usage. /clear resets the pane along with the conversation (Codex and branch are untouched).

Codex usage

Read-only, display-only. It needs USERPROFILE and SystemRoot (Windows); without either nothing is read and the Codex panel does not appear.

  • Model and effort: the model is the codex key of ~/.claude/model-choice.json (the rules' default, gpt-6-sol, without one); the effort is the first line of ~/.codex/config.toml that begins model_reasoning_effort = "…" (double quotes, unindented; wherever it sits in the file, so possibly inside a profile; a single-quoted value gives none).
  • Windows, the server only when you press ↻: pressing ↻ on the Codex box's title (hotkey r with the pane focused, or /dashboard refresh), if ~/.codex/auth.json holds a ChatGPT login (tokens.access_token; tokens.account_id is sent as ChatGPT-Account-Id only when it is present and valid), sends GET https://chatgpt.com/backend-api/wham/usage. That is the call Codex's own /status makes, with the headers Codex sends (Authorization: Bearer …, ChatGPT-Account-Id, User-Agent: codex-cli). It answers the account's server-side 5-hour and weekly percent used and reset times and spends none of the quota; usage from another computer or Codex on the web counts, so the figures are current even if Codex was not called. The token is used for that one request only: never kept in state, never drawn, never written back to auth.json (when it has expired it waits for Codex's next run to refresh it, and until then the call is refused and the rollouts are read instead). Only the characters a JWT or an id holds are accepted, so nothing extra can be smuggled into a header. This is an undocumented private API: an answer of another shape, a refusal, no network, or an API-key login (no ChatGPT token) all fall back to the rollouts below. A press (or /dashboard refresh) while a Codex read is already under way is ignored.
  • Windows, the rollouts normally (the schedule reads local files only and sends no request; also used when you press ↻ and the server does not answer): from the newest 14 day folders of ~/.codex/sessions/YYYY/MM/DD (no older folder once 200 files are listed; a folder that cannot be listed counts as empty), each candidate is ranked by its own last-modified time (asked of the file system one by one, not the listing's time, which can be stale; where that fails the listing's time stands). The 2 most recently modified rollouts are read (a long-lived older session keeps writing to its original file; the next 2, up to 4 in all, only when neither holds a codex reading), and the reading whose event timestamp is latest wins (a tie goes to the more recently modified; a reading with no parseable time counts as oldest). One command per rollout (usually 2 per refresh, at most 4), through %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe (a full path, not PATH), takes the lines containing "limit_id":"codex" from the last 2000 lines of the file and keeps the last 5 of them. The rollout's path rides in the environment variable ROLLOUT, never in the command text. The 5-hour and weekly windows are told apart by their length.
  • The newer reading is shown. A server reading is dated by the ask, a rollout by its event time (a reading with no time counts as oldest), so after ↻ an older rollout does not overwrite it, and once you use Codex here the newer rollout takes over. The ↻ button says how long ago the server last answered (↻ 3m ago). A new reading that lacks a window keeps the old one. The rollouts are read every 5 minutes, only while the pane shows.

Fable weekly usage

GET https://api.anthropic.com/api/oauth/usage, authorised by the engine's own handle (the credential never reaches this plugin; only a bearer login can use the endpoint, and without one nothing is asked or shown). It takes the limits[] entry whose kind is weekly_scoped and whose model name contains Fable. Every 90 seconds; after a failure the wait doubles up to 10 minutes (a longer retry-after is followed too, but never past 10 minutes, and only in seconds, not as a date), and the last figure stays with the label Fable~. Once the window's reset time has passed it reads ~0%.

Waiting on you

  • Reading (other sessions): the local session registry ~/.claude/sessions/<pid>.json (only the newest 32 plain files with numeric names, 64 KB or smaller), plus the process table, %SystemRoot%\System32\tasklist.exe /FO CSV /NH (a full path, 15-second timeout); only sessions whose pid still runs as claude or node count. Then their mark files, ~/.claude/mods-state/status-sidebar/waiting/<pid>.json, whose sessionId must equal the registry's (a stale file from a reused pid does not count). Marks are read about every 5 seconds, the registry and tasklist about every 30. Whenever a step cannot be read, nothing is shown: nothing is guessed.
  • Writing (this session's own): the same folder and the same format as status-sidebar, {"sessionId":…,"reason":"permission"|"question"|null,"sinceMs":…}, so sessions running either mod see each other. classic.PermissionRequest writes permission; AskUserQuestion / ExitPlanMode write question when they start. It clears when that call returns, when you type a new prompt, when the main turn ends, and when the session ends. Driven by events, so it is written whether or not the pane shows.
  • A permission wait is known to start and not to end until its call returns, hence "may be waiting on you". The session name is cut to 80 characters, and control, format, separator and tag characters in it become spaces, before it is drawn.

Codex runs (as subagents)

codex exec / codex exec resume that the main conversation runs through Bash or PowerShell (a subagent's calls do not count). It counts only in command position: a codex exec inside quotes, a comment, a heredoc body or a PowerShell here-string is not a run (masking and matching are a single pass over the first 64 KB). The purpose comes from the command: resume is Codex resume, --sandbox read-only is Codex plan draft, workspace-write is Codex review, anything else is Codex; the card's model is the one the Codex panel read.

  • A foreground run ends when its call returns; an isError or a refusal is a failure.
  • A run moved to the background (the result carries backgroundTaskId) stays running until its task-notification ends it (task-id, tool-use-id and status are read from before <summary>; a failed / killed / error status, or a non-zero (exit code N) in the <summary> text, is a failure). A notification never makes this mod open a file.
  • At each turn's classic.Stop it is checked against the engine's own list: a foreground run still open is inferred failed, a background run the list no longer holds is inferred done.

Branch

Read-only git, through $.process.run (15-second timeout). Every call carries --no-optional-locks -c core.quotePath=false -c core.fsmonitor=false -c protocol.ext.allow=never and the environment variable GIT_NO_LAZY_FETCH=1: rev-parse --show-toplevel; config (lists the filter drivers, then switches every one off for status and hash-object; a partial clone is not read at all; if they cannot be listed nothing further is read); status --ignore-submodules=all --porcelain=v2 --branch -z -uall; and hash-object --no-filters --stdin-paths (no -w, so no object is written). A symbolic link, or a path that lands outside the repository, is not hashed and always counts as unreviewed. Output the engine cut short is a failure. Every 30 seconds, and once right after a main turn ends.

  • Changed = files that differ from HEAD (untracked included).
  • Unreviewed = changed files whose content differs from what the last review saw.
  • The review record (one of the plugin's two writes; the other is the wait mark under "Waiting on you"): its own $.store, key git-reviewed:<repository root>, value "path → content hash"; a second key, git-reviewed-index, keeps the order of the 8 most recently written repositories, the only ones kept, 2000 paths each (past that it stores too-many-paths and the panel reads unreviewed ?; with more than 500 untracked files no hashes are made, so the panel reads unreviewed ? and no record is written). It is written only when: (1) a Skill call whose skill name contains review; (2) an Agent call whose subagent_type contains review (for both, the tree is read as the call begins and kept only if the call is not refused and does not fail); (3) a /…review slash command that you type yourself (composer / bridge / sdk origin), the tree read and recorded as it is sent.

Fable, Codex, git and the other sessions' waits are polled only while the pane shows (a pump looks every 2 seconds at which is due). A closed pane, or one in a background tab, makes no scheduled request and runs no scheduled process. These run whatever the pane's state: to learn the file name of its own mark, the session reads the registry and tasklist once at start (or when a mark is to be written and the pid is not known yet); a review trigger (the Skill / Agent call or the typed /…review above) reads the git tree; /dashboard refresh asks and reads now; and the wait mark is written on events (see "Waiting on you").

Inferred, not measured

  • The architect's moments: before a plan = no edit yet this turn, error repeats = 2 or more main-loop errors in a row, before done = edits were made. Marked (inferred); moments: false turns them off.
  • A server-side architect (such as advisor) is encrypted: it can be counted and timed, not read.
  • An agent's ctx is its latest request's whole input, not a cost.

Configure

In /config, or under pluginConfigs["cc-dashboard"].options in settings.json:

| Option | Default | Meaning | | :- | :- | :- | | architectPattern | advisor\|architect | case-insensitive regex for agent types and server tools that count as the architect | | matchDescriptions | false | also match agent descriptions | | architectLabel | ARCHITECT | the architect's name in the pane | | gateLabel | GATE | the permission panel's name | | panels | main,codex,git,architect,gate,agents,loops,receipt,log | which panels show, in order | | layout | auto | mini, compact, wide, or auto | | dockPercent | 40 | share of the terminal's width the docked pane takes in fullscreen (20–80) | | maxCards | 3 | agent cards side by side before swimlanes (1–6) | | motion | while-active | off keeps the connectors and the main line still | | moments | true | show the inferred consult moments | | palette | theme | pastel uses fixed colours tuned for dark terminals | | openOnStart | true | open the pane when a session starts | | statusLine | true | context, agents, consults and denials in the status line |

Structure

| File | Holds | | :- | :- | | hooks/register.tsx | the event hooks, state access, the Codex and git I/O and polling, and the drawing of every panel (everything that touches $ is here) | | hooks/core.ts | every reducer, formatter and layout rule as pure functions, including the Codex windows and the branch text | | hooks/rail.tsx, hooks/elapsed.tsx | surface modules: animated connectors (the subagents', and the main model's orange line) and live clocks | | hooks/lib/codex-usage.ts | the ChatGPT login in Codex's auth.json, the usage call's headers and its answer's parsing | | hooks/lib/codex-quota.ts | parsing and walking Codex's rollouts, config and model choice (takes its reads as functions, never touches $) | | hooks/lib/git.ts, git-tree.ts, git-filters.ts | git status parsing, the read-only git commands, switching filters off, the unreviewed count | | hooks/lib/review-store.ts | reading and writing the review record, and its caps | | hooks/lib/fable.ts | parsing Fable's answer and the back-off | | hooks/lib/sessions.ts, waiting.ts, serial.ts | registry / tasklist parsing, the wait mark's format and the banner's text, a queue that runs writes one after another | | hooks/lib/codex.ts, shell-mask.ts, notify.ts | recognising codex exec and its purpose, masking what is not code in a shell command, background-task notifications | | hooks/lib/plain.ts, system.ts | making outside strings plain and "how long ago" (3m ago), full paths of tasklist / PowerShell | | types/index.d.ts | the state's types | | tests/ | Drawing and hooks: dashboard.test.ts (Flightdeck's own panels, the main flow line), dashboard-extras.test.ts (Codex usage and ↻, branch, review, Fable, waiting on you, Codex cards, the summary rows, polling), pairs-pane.test.ts / pairs.test.ts (the paired layout), dock.test.ts (dock width), context-line.test.ts, palette.test.ts; the rest are unit tests of each lib (codex*, git*, review-store, fable, notify, sessions, waiting, shell-mask, serial, plain, system) |

Test

claude plugin test
claude plugin validate .
npx -p typescript tsc -p .   # after a first interactive load with --plugin-dir, which writes .claude-plugin/types/

License

MIT, see LICENSE. Flightdeck © Stephen Casella.

Source 20 files
hooks/register.tsx 1831 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, Timer } from 'claude-code'
3
4import type { AgentCard, Architect, Bucket, Check, CodexQuota, Gate, GitSnap, Layout, LogLine, Loop, Main, Roster, Turn, Usage, View, WaitReason, WaitingRow } from '../types'
5import {
6  DEFAULT_ARCHITECT,
7  DEFAULT_GATE,
8  DEFAULT_MAIN,
9  DEFAULT_ROSTER,
10  DEFAULT_TURN,
11  DEFAULT_USAGE,
12  DEFAULT_VIEW,
13  EDIT_TOOLS,
14  SCHEMA_VERSION,
15  afterCall,
16  applyStep,
17  cardKind,
18  bucketOf,
19  cardTitle,
20  titleLines,
21  consultTimeline,
22  describeInput,
23  endConsult,
24  fitLegend,
25  fmtClock,
26  fmtDuration,
27  fmtTimer,
28  plural,
29  windowShort,
30  windowMeter,
31  meterPercent,
32  meterWidth,
33  lineMeterWidth,
34  padCells,
35  LINE_GLYPHS,
36  BLOCK_GLYPHS,
37  CTX_LINE_SHARE,
38  ctxTone,
39  limitWindow,
40  claudeWindows,
41  DOCK_MIN_COLUMNS,
42  DOCK_UNASKED_MIN_COLUMNS,
43  dockColumns,
44  dockShareMoved,
45  terminalWidth,
46  GIT_PATH_ROWS,
47  unreviewedLabel,
48  changedLabel,
49  syncLabel,
50  canUnfold,
51  cells,
52  pathLines,
53  pathRows,
54  codexCard,
55  codexCardFor,
56  endCard,
57  stopCodex,
58  gateSummary,
59  gauge,
60  isAdvising,
61  isLoopActive,
62  kTokens,
63  lanes,
64  limitLabel,
65  limitOf,
66  listOf,
67  logRows,
68  momentOf,
69  narrowEffort,
70  normalize,
71  normalizeCard,
72  uniqueById,
73  normalizeGate,
74  normalizeLog,
75  noteTool,
76  PALETTES,
77  PANEL_FRAME,
78  pairRows,
79  pairWidths,
80  withCtx,
81  SVG_COLORS,
82  parseConfig,
83  prettyModel,
84  promptLine,
85  handbackOf,
86  adviceLine,
87  receiptOf,
88  recordCheck,
89  settleCheck,
90  shorten,
91  startConsult,
92  stepLoop,
93  wrapRows,
94} from './core'
95import type { ColumnRow, Config, Meter, Panel, PairRow } from './core'
96import { describeCodex } from './lib/codex'
97import { ROLLOUT_QUERY, mergeCodex, readCodexQuota } from './lib/codex-quota'
98import { FABLE_POLL_MS, fableFromUsage, nextFableDelay } from './lib/fable'
99import { backgroundIdOf, parseNotifications } from './lib/notify'
100import { serial } from './lib/serial'
101import { readLiveSessions } from './lib/sessions'
102import { bannerText, isQuestionTool, markPath, markText, readWaits, sameWaits } from './lib/waiting'
103import { isPersonPrompt, isReviewCall, isReviewPrompt, snapOf } from './lib/git'
104import { GIT_TIMEOUT_MS, readTree } from './lib/git-tree'
105import type { GitRun, Inspect, Tree } from './lib/git-tree'
106import { ago, plain } from './lib/plain'
107import { readReviewed, recordReviewed } from './lib/review-store'
108import type { ReviewStore } from './lib/review-store'
109import { powershellExe, tasklistExe } from './lib/system'
110
111const PANE = 'dashboard'
112const TITLE = 'Dashboard'
113
114
115// ---------------------------------------------------------------- state
116
117const meta = atom({ plugin: 'cc-dashboard', key: 'meta' } as const, { schemaVersion: SCHEMA_VERSION })
118const main = atom({ plugin: 'cc-dashboard', key: 'main' } as const, DEFAULT_MAIN)
119const usage = atom({ plugin: 'cc-dashboard', key: 'usage' } as const, DEFAULT_USAGE)
120const architect = atom({ plugin: 'cc-dashboard', key: 'architect' } as const, DEFAULT_ARCHITECT)
121const gate = atom({ plugin: 'cc-dashboard', key: 'gate' } as const, DEFAULT_GATE)
122const agents = atom({ plugin: 'cc-dashboard', key: 'agents' } as const, [])
123const loops = atom({ plugin: 'cc-dashboard', key: 'loops' } as const, [])
124const log = atom({ plugin: 'cc-dashboard', key: 'log' } as const, [])
125const turn = atom({ plugin: 'cc-dashboard', key: 'turn' } as const, DEFAULT_TURN)
126const receipt = atom({ plugin: 'cc-dashboard', key: 'receipt' } as const, null)
127const view = atom({ plugin: 'cc-dashboard', key: 'view' } as const, DEFAULT_VIEW)
128const roster = atom({ plugin: 'cc-dashboard', key: 'roster' } as const, DEFAULT_ROSTER)
129const codex = atom({ plugin: 'cc-dashboard', key: 'codex' } as const, null)
130const git = atom({ plugin: 'cc-dashboard', key: 'git' } as const, null)
131const fable = atom({ plugin: 'cc-dashboard', key: 'fable' } as const, null)
132const waiting = atom({ plugin: 'cc-dashboard', key: 'waiting' } as const, null)
133const sessions = atom({ plugin: 'cc-dashboard', key: 'sessions' } as const, [])
134const waitingOthers = atom({ plugin: 'cc-dashboard', key: 'waitingOthers' } as const, [])
135
136const swallow = (): void => undefined
137
138type ServerBlock = { type: string; id?: string; name?: string; tool_use_id?: string }
139
140// Every read goes through these, so a value saved under an older shape still reads.
141async function getMain($: EngineInterface): Promise<Main> {
142  return normalize(DEFAULT_MAIN, await read($, main))
143}
144async function getUsage($: EngineInterface): Promise<Usage> {
145  return normalize(DEFAULT_USAGE, await read($, usage))
146}
147async function getArchitect($: EngineInterface): Promise<Architect> {
148  const a = normalize(DEFAULT_ARCHITECT, await read($, architect))
149  return { ...a, consults: listOf(a.consults), ids: listOf(a.ids), seen: listOf(a.seen) }
150}
151async function getGate($: EngineInterface): Promise<Gate> {
152  return normalizeGate(await read($, gate))
153}
154async function getCards($: EngineInterface): Promise<AgentCard[]> {
155  return uniqueById(listOf<unknown>(await read($, agents)).map(normalizeCard))
156}
157async function getLoops($: EngineInterface): Promise<Loop[]> {
158  return listOf<Loop>(await read($, loops))
159}
160async function getLog($: EngineInterface): Promise<LogLine[]> {
161  return normalizeLog(await read($, log))
162}
163async function getTurn($: EngineInterface): Promise<Turn> {
164  return normalize(DEFAULT_TURN, await read($, turn))
165}
166async function getView($: EngineInterface): Promise<View> {
167  return normalize(DEFAULT_VIEW, await read($, view))
168}
169async function getRoster($: EngineInterface): Promise<Roster> {
170  const r = normalize(DEFAULT_ROSTER, await read($, roster))
171  return { architectTypes: listOf(r.architectTypes) }
172}
173
174/** A stored shape older than this build's: drop what cannot be read, keep the rest. */
175async function migrate($: EngineInterface) {
176  const m = await read($, meta)
177  if ((m?.schemaVersion ?? 0) >= SCHEMA_VERSION) return
178  await update($, log, list => normalizeLog(list))
179  await update($, agents, list => listOf<unknown>(list).map(normalizeCard))
180  await update($, gate, g => normalizeGate(g))
181  await update($, meta, () => ({ schemaVersion: SCHEMA_VERSION }))
182}
183
184async function say($: EngineInterface, who: string, text: string, kind: LogLine['kind'] = 'info', agentId: string | null = null) {
185  const line: LogLine = { at: await $.clock.now(), who, text, kind, agentId }
186  await update($, log, list => [...normalizeLog(list), line].slice(-60))
187}
188
189async function refreshStatus($: EngineInterface, cfg: Config) {
190  if (!cfg.statusLine) return $.ui.status(undefined)
191  const [u, a, g, cards] = await Promise.all([getUsage($), getArchitect($), getGate($), getCards($)])
192  const running = cards.filter(c => c.status === 'running').length
193  const s = gateSummary(g)
194  const parts = [
195    u.pct !== null ? `ctx ${Math.round(u.pct)}%` : null,
196    cards.length > 0 ? `agents ${running}/${cards.length}` : null,
197    a.consults.length > 0 || a.ids.length > 0 ? `${cfg.architectLabel.toLowerCase()} ${isAdvising(a) ? 'advising' : a.consults.length}` : null,
198    s.deny > 0 ? `denied ${s.deny}` : null,
199  ]
200  // Only fields with something to say; with none, no status entry at all.
201  const shown = parts.filter(Boolean)
202  $.ui.status(shown.length > 0 ? shown.join(' · ') : undefined)
203}
204
205async function whoIs($: EngineInterface, agentId: string | undefined) {
206  if (!agentId) return 'main'
207  const card = (await getCards($)).find(c => c.id === agentId)
208  return card ? shorten(cardTitle(card), 14) : 'agent'
209}
210
211async function consultStarted($: EngineInterface, cfg: Config, id: string, via: string) {
212  const t = await getTurn($)
213  const moment = momentOf(t)
214  const at = await $.clock.now()
215  await update($, architect, a => startConsult(normalize(DEFAULT_ARCHITECT, a), { id, at, moment, via }))
216  if (moment === 'before done') await update($, turn, x => ({ ...normalize(DEFAULT_TURN, x), isReviewing: true }))
217  await say($, cfg.architectLabel.toLowerCase(), cfg.moments ? `${moment} · ${via}` : `consulted · ${via}`, 'consult')
218  await refreshStatus($, cfg)
219}
220
221async function consultEnded($: EngineInterface, cfg: Config, advice: string | null, id?: string) {
222  const at = await $.clock.now()
223  const first = advice?.split('\n').find(l => l.trim()) ?? null
224  const text = first ? shorten(first.replace(/^[#>*\s-]+/, ''), 160) : null
225  await update($, architect, a => endConsult(normalize(DEFAULT_ARCHITECT, a), at, text, id))
226  await update($, turn, t => ({ ...normalize(DEFAULT_TURN, t), isReviewing: false }))
227  await say($, cfg.architectLabel.toLowerCase(), text ? `advice: ${shorten(text, 60)}` : 'advice returned', 'consult')
228  await refreshStatus($, cfg)
229}
230
231async function noteAdvice($: EngineInterface, cfg: Config, advice: string) {
232  await update($, architect, x => ({ ...normalize(DEFAULT_ARCHITECT, x), lastAdvice: advice }))
233  await say($, cfg.architectLabel.toLowerCase(), `advice: ${shorten(advice, 60)}`, 'consult')
234}
235
236async function isArchitectType($: EngineInterface, cfg: Config, type: string) {
237  return cfg.architect.test(type) || (await getRoster($)).architectTypes.includes(type)
238}
239
240// The dock's width is a share of the terminal's, and the terminal's width is known only from a
241// drawing or a command's presentation. A docked drawing gives the transcript's columns, so the
242// dock's body and grip are added back. A drawing may not act, so it notes the width here and the
243// pump asks the dock again (see syncDock). What a person dragged the dock to still wins.
244let terminalColumns: number | undefined
245let lastPlacement: 'dock' | 'inline' | undefined
246let requestedColumns: number | undefined
247
248async function openPane($: EngineInterface, dockPercent: number, { focus = false } = {}) {
249  // columns apply when docked beside the transcript, rows when seated inline above the prompt.
250  // Before any width is known the dock keeps the engine's share; the pump asks once it is.
251  // Only an open the person asked for may focus: it raises the pane over another pane's tab.
252  requestedColumns = terminalColumns === undefined ? undefined : dockColumns(terminalColumns, dockPercent)
253  try {
254    const args = {
255      id: PANE,
256      title: TITLE,
257      rows: 8,
258      ...(requestedColumns === undefined ? {} : { columns: requestedColumns }),
259      ...(focus ? { focus: true as const } : {}),
260    }
261    return await $.ui.open(args)
262  } catch (error) {
263    requestedColumns = undefined // a refused open is asked again on the next tick
264    throw error
265  }
266}
267
268// A docked pane asks again when the terminal's width moved its share; inline, or hidden, it is left alone.
269// Under the unasked floor the engine may leave this open undrawn, so a narrow terminal is not asked; the
270// width last asked for is forgotten there, so the first reading back above the floor asks for its share.
271async function syncDock($: EngineInterface, dockPercent: number) {
272  if (lastPlacement !== 'dock' || terminalColumns === undefined) return
273  if (terminalColumns < DOCK_UNASKED_MIN_COLUMNS) {
274    requestedColumns = undefined
275    return
276  }
277  if (!dockShareMoved(terminalColumns, dockPercent, requestedColumns)) return
278  if (await isPaneShown($)) await openPane($, dockPercent)
279}
280
281async function resetAll($: EngineInterface) {
282  await update($, main, m => ({ ...DEFAULT_MAIN, model: normalize(DEFAULT_MAIN, m).model, mode: normalize(DEFAULT_MAIN, m).mode }))
283  await update($, architect, () => DEFAULT_ARCHITECT)
284  await update($, gate, () => DEFAULT_GATE)
285  await update($, agents, () => [])
286  await update($, loops, () => [])
287  await update($, log, () => [])
288  await update($, turn, () => DEFAULT_TURN)
289  await update($, receipt, () => null)
290  await update($, view, () => DEFAULT_VIEW)
291  // The context gauge waits for the next measurement rather than showing the pre-clear fill.
292  await update($, usage, x => ({ ...normalize(DEFAULT_USAGE, x), pct: null, tokens: null }))
293}
294
295async function noteMode($: EngineInterface, mode: string | undefined) {
296  if (mode) await update($, main, m => (normalize(DEFAULT_MAIN, m).mode === mode ? normalize(DEFAULT_MAIN, m) : { ...normalize(DEFAULT_MAIN, m), mode }))
297}
298
299// ---------------------------------------------------------------- Codex usage
300// Codex's figures are only displayed (see ./lib/codex-quota): its model from the choice file, its
301// effort from its config, and its 5-hour and weekly windows from the ChatGPT backend's usage call
302// (signed with Codex's own ChatGPT login; spends none of the quota), else from the most recently modified rollouts
303// (the latest event time among the top two wins; the next two are read only when those hold none),
304// read through Windows PowerShell by full path. The rollout's path rides in the environment, never in
305// the command text. Without USERPROFILE or SystemRoot nothing is read and the panel stays hidden.
306
307async function readCodex($: EngineInterface, askServer: boolean): Promise<CodexQuota | null> {
308  const home = await $.env.get('USERPROFILE')
309  const root = await $.env.get('SystemRoot')
310  if (home === undefined || root === undefined) return null
311  const nowMs = await $.clock.now()
312  return readCodexQuota(home, {
313    readText: path => $.fs.read(path).catch(() => ''),
314    list: dir => $.fs.list(dir),
315    stat: path => $.fs.stat(path).then(info => info.mtimeMs),
316    queryRollout: path =>
317      $.process
318        .run([powershellExe(root), '-NoProfile', '-NonInteractive', '-Command', ROLLOUT_QUERY], { env: { ROLLOUT: path }, timeoutMs: 20_000 })
319        .then(done => done.stdout, () => ''),
320    fetch: (url, headers) => $.http.fetch(url, { headers }),
321  }, { askServer, nowMs })
322}
323
324// The schedule reads the rollouts only, with no request; the server is asked only when the person
325// presses ↻ (or runs /dashboard refresh). The newer of the two readings is shown (see mergeCodex).
326async function refreshCodex($: EngineInterface, askServer = false) {
327  const reading = await readCodex($, askServer)
328  await update($, codex, old => (reading === null ? null : mergeCodex(old, reading)))
329}
330
331// ↻ on the Codex box: asks the server now, unless a read is already under way; the schedule starts over.
332async function askCodex($: EngineInterface) {
333  if (busy.has('codex')) return
334  busy.add('codex')
335  try {
336    await refreshCodex($, true).catch(swallow)
337    dueAt.codex = (await $.clock.now()) + 300_000
338  } finally {
339    busy.delete('codex')
340  }
341}
342
343// ---------------------------------------------------------------- branch (git)
344// The session's repository, read with read-only git (the commands are spelled in ./lib/git-tree).
345// A changed file is unreviewed when the last review did not see its contents as they are now. What
346// a review saw is one of the two things this plugin writes (the other is the wait mark): under `git-reviewed:<root>` in its own `$.store`.
347
348const gitRun = ($: EngineInterface): GitRun => (argv, init) => $.process.run(argv, { timeoutMs: GIT_TIMEOUT_MS, ...init })
349
350const reviewStore = ($: EngineInterface): ReviewStore => ({
351  get: key => $.store.get(key),
352  set: (key, value) => $.store.set(key, value),
353  delete: key => $.store.delete(key),
354  keys: () => $.store.keys(),
355})
356
357// Whether a path is a link, and where it lands (a junction lands elsewhere without being a link); one that cannot be checked counts as a link.
358const inspect = ($: EngineInterface): Inspect => path =>
359  $.fs.stat(path, { resolve: true }).then(
360    st => ({ isLink: st.isLink, ...(st.realPath === undefined ? {} : { realPath: st.realPath }) }),
361    () => ({ isLink: true }),
362  )
363
364const readGitTree = ($: EngineInterface): Promise<Tree | undefined> => readTree(gitRun($), inspect($))
365const readTreeQuietly = ($: EngineInterface): Promise<Tree | undefined> => readGitTree($).catch(() => undefined)
366
367// Outside a repository, or with a git that cannot run, the panel empties: old figures are not kept.
368async function refreshGit($: EngineInterface) {
369  const tree = await readGitTree($).catch(() => undefined)
370  const snap: GitSnap | null = tree === undefined ? null : snapOf(tree.status, tree.hashes, await readReviewed(reviewStore($), tree.root))
371  await update($, git, () => snap)
372}
373
374// The tree read as a review started is what that review saw: once it is under way (not refused,
375// not failed) those contents count as reviewed. This runs whether or not the pane is showing.
376async function recordReview($: EngineInterface, tree: Tree | undefined) {
377  if (tree?.hashes === undefined) return
378  const kept = await recordReviewed(reviewStore($), tree.root, tree.hashes)
379  await update($, git, () => snapOf(tree.status, tree.hashes, kept))
380}
381
382// ---------------------------------------------------------------- Fable
383// Fable's weekly window is not in the session's rate limits: it comes from /api/oauth/usage. The
384// request is authorised by the engine's own handle (the credential never reaches this plugin), and
385// only a bearer login can use that endpoint. A failed answer keeps the last figure and marks it
386// stale. Answers how long to wait before asking again: 90 seconds, doubled after a failure up to
387// ten minutes, or as long as the server asks in `retry-after`.
388
389const USAGE_URL = 'https://api.anthropic.com/api/oauth/usage'
390let fableDelayMs = FABLE_POLL_MS
391
392async function refreshFable($: EngineInterface): Promise<number> {
393  const auth = await $.session.authorize().catch(() => null)
394  const response =
395    auth === null || auth.kind !== 'bearer'
396      ? undefined
397      : await $.http
398          .fetch(USAGE_URL, { auth: auth.handle, headers: { 'anthropic-beta': 'oauth-2025-04-20', 'Content-Type': 'application/json' } })
399          .catch(() => undefined)
400  const window = response?.ok === true ? fableFromUsage(response.text) : undefined
401  await update($, fable, old =>
402    window !== undefined ? { window, isStale: false } : old?.window === undefined ? { isStale: false } : { window: old.window, isStale: true },
403  )
404  fableDelayMs = nextFableDelay(fableDelayMs, response?.ok === true, response?.headers['retry-after'])
405  return fableDelayMs
406}
407
408// ---------------------------------------------------------------- waits on the person
409// A session that waits on the person (a permission dialog, a question, a plan to approve) says so
410// in a small mark file named by its pid, in the folder and format status-sidebar uses, so sessions
411// running either mod see each other. Writing this session's own mark follows its events, whether
412// or not the pane shows; reading the others' (and the registry and process table that say which
413// marks to trust) only serves the banner, so it waits for the pane to show.
414
415let herePid: number | undefined
416let pidLookup: Promise<void> | undefined
417let sessionsAt = 0
418// Calls whose permission is put to the decider, so a dialog's wait ends with its own call.
419const asked = new Set<string>()
420
421// The live sessions: the registry in ~/.claude/sessions, kept to the pids still running as Claude
422// Code. A registry or process table that cannot be read leaves no sessions, and so no banner.
423async function refreshSessions($: EngineInterface) {
424  try {
425    const home = await $.env.get('USERPROFILE')
426    if (home === undefined) throw new Error('USERPROFILE is not set')
427    const root = await $.env.get('SystemRoot')
428    if (root === undefined) throw new Error('SystemRoot is not set')
429    const rows = await readLiveSessions(home, await $.session.id(), {
430      list: dir => $.fs.list(dir),
431      read: path => $.fs.read(path).catch(() => ''),
432      tasklist: () => $.process.run([tasklistExe(root), '/FO', 'CSV', '/NH'], { timeoutMs: 15_000 }),
433    })
434    herePid = rows.find(row => row.isHere)?.pid ?? herePid
435    await update($, sessions, () => rows)
436  } catch (error) {
437    await update($, sessions, () => [])
438    throw error
439  }
440}
441
442function learnPid($: EngineInterface): Promise<void> {
443  pidLookup ??= refreshSessions($)
444    .catch(swallow)
445    .finally(() => {
446      pidLookup = undefined
447    })
448  return pidLookup
449}
450
451// What the mark says is the wait as it stands when the write begins. Until the pid is known nothing
452// waits for it, so a dialog is never held up on it.
453async function putMark($: EngineInterface) {
454  if (herePid === undefined) {
455    void learnPid($)
456      .then(() => (herePid === undefined ? undefined : writeMark($)))
457      .catch(swallow)
458    return
459  }
460  const home = await $.env.get('USERPROFILE')
461  if (home === undefined) return
462  const value = await read($, waiting)
463  await $.fs.write(markPath(home, herePid), markText(await $.session.id(), value?.reason ?? null, value?.sinceMs ?? 0))
464}
465
466// The writes go one after another, so a slow one never lands after a newer one.
467const inMarkOrder = serial()
468function writeMark($: EngineInterface): Promise<void> {
469  return inMarkOrder(() => putMark($))
470}
471
472async function setWaiting($: EngineInterface, reason: WaitReason | null) {
473  if (((await read($, waiting))?.reason ?? null) === reason) return
474  const value = reason === null ? null : { reason, sinceMs: await $.clock.now() }
475  await update($, waiting, () => value)
476  await writeMark($)
477}
478
479// A new prompt, the turn ending, or the session ending means whatever was asked is over.
480async function endWaiting($: EngineInterface) {
481  asked.clear()
482  await setWaiting($, null)
483}
484
485// A call that returned has had its question answered or its dialog closed. A permission wait ends
486// once no call put to the decider is still pending.
487async function settleWait($: EngineInterface, tool: string, toolUseId: string | undefined) {
488  if (toolUseId !== undefined) asked.delete(toolUseId)
489  const now = (await read($, waiting))?.reason
490  if ((now === 'question' && isQuestionTool(tool)) || (now === 'permission' && asked.size === 0)) await setWaiting($, null)
491}
492
493// The same waits as the last read change nothing: no write, no redraw.
494async function noteOthers($: EngineInterface, rows: WaitingRow[]) {
495  if (sameWaits(await read($, waitingOthers), rows)) return
496  await update($, waitingOthers, () => rows)
497}
498
499// The other live sessions that wait on the person: each one's mark, if the session id inside it is
500// the one the registry names. Whatever cannot be read leaves no banner: nothing is guessed.
501async function readOthers($: EngineInterface): Promise<number> {
502  try {
503    const now = await $.clock.now()
504    if (now - sessionsAt >= 30_000) {
505      sessionsAt = now
506      await refreshSessions($)
507    }
508    const home = await $.env.get('USERPROFILE')
509    await noteOthers($, home === undefined ? [] : await readWaits(home, await read($, sessions), path => $.fs.read(path).catch(() => '')))
510  } catch {
511    await noteOthers($, [])
512  }
513  return 5_000
514}
515
516// ---------------------------------------------------------------- Codex runs as subagents
517// A `codex exec` the main session starts through Bash or PowerShell is drawn as a subagent card:
518// running from the call's start, ended by its result, by a background task's notification, or
519// (inferred) by the turn's stop. No Codex session id is read.
520
521async function codexStarted($: EngineInterface, id: string, call: { purpose: string; note?: string }) {
522  const codexNow = await read($, codex)
523  const card = codexCard(id, call, codexNow?.model ?? '', await $.clock.now(), codexNow?.effort ?? '')
524  await update($, agents, list => [...listOf<unknown>(list).map(normalizeCard), card].slice(-24))
525  await say($, shorten(cardTitle(card), 12), 'spawned · Codex', 'info', id)
526}
527
528async function codexEnded($: EngineInterface, id: string, ok: boolean) {
529  const card = (await getCards($)).find(c => c.id === id)
530  if (card?.codex === undefined || card.status !== 'running') return
531  const now = await $.clock.now()
532  await update($, agents, list => listOf<unknown>(list).map(normalizeCard).map(c => (c.id === id ? endCard(c, ok, now) : c)))
533  await say($, await whoIs($, id), ok ? `done · ${fmtDuration(now - card.spawnedAt)}` : 'failed', ok ? 'done' : 'error', id)
534}
535
536// The call's result: the run moved to the background (kept running, under its task id), or is over.
537async function codexSettled($: EngineInterface, id: string, ran: { deny?: string; isError?: boolean; result?: unknown }) {
538  const taskId = ran.deny === undefined ? backgroundIdOf(ran.result) : undefined
539  if (taskId === undefined) return codexEnded($, id, ran.deny === undefined && ran.isError !== true)
540  await update($, agents, list => listOf<unknown>(list).map(normalizeCard).map(c => (c.id === id && c.codex ? { ...c, codex: { taskId } } : c)))
541}
542
543// ---------------------------------------------------------------- polling
544// Fable, Codex, git and the other sessions' waits are read on their own schedules, and only while
545// the pane is showing: a pane nobody sees costs no request and no process. One pump looks every 2
546// seconds and starts whichever is due and not already running.
547
548type Source = 'fable' | 'codex' | 'git' | 'others'
549const SOURCES: readonly Source[] = ['fable', 'codex', 'git', 'others']
550const dueAt: Record<Source, number> = { fable: 0, codex: 0, git: 0, others: 0 }
551const busy = new Set<Source>()
552let pump: Timer | undefined
553
554async function isPaneShown($: EngineInterface) {
555  return (await $.ui.panes()).some(p => p.id === PANE && p.isShown)
556}
557
558// Each source reads, then answers how long to wait before it is read again.
559async function readSource($: EngineInterface, source: Source): Promise<number> {
560  switch (source) {
561    case 'fable':
562      return refreshFable($)
563    case 'codex':
564      await refreshCodex($)
565      return 300_000
566    case 'git':
567      await refreshGit($)
568      return 30_000
569    case 'others':
570      return readOthers($)
571  }
572}
573
574// A read that throws is tried again after the usual wait, or for Fable after its next backoff.
575function failureWait(source: Source) {
576  if (source === 'fable') return (fableDelayMs = nextFableDelay(fableDelayMs, false, undefined))
577  return source === 'codex' ? 300_000 : source === 'git' ? 30_000 : 5_000
578}
579
580async function runSource($: EngineInterface, source: Source) {
581  busy.add(source)
582  try {
583    const wait = await readSource($, source).catch(() => failureWait(source))
584    dueAt[source] = (await $.clock.now()) + wait
585  } finally {
586    busy.delete(source)
587  }
588}
589
590async function pumpPolls($: EngineInterface) {
591  if (!(await isPaneShown($).catch(() => false))) return
592  const now = await $.clock.now()
593  for (const source of SOURCES) {
594    if (!busy.has(source) && dueAt[source] <= now) void runSource($, source).catch(swallow)
595  }
596}
597
598// One pump per plugin instance: a second start (a hot reload) replaces the first.
599function startPolling($: EngineInterface, dockPercent: number) {
600  pump?.cancel()
601  pump = $.clock.every(2_000, () => {
602    void pumpPolls($).catch(swallow)
603    void syncDock($, dockPercent).catch(swallow)
604  })
605}
606
607// ---------------------------------------------------------------- hooks
608
609export const register: Register = (on, options) => {
610  const cfg = parseConfig(options)
611  const C = PALETTES[cfg.palette]
612  // tool.check carries no loop id; the tool.call around it does, keyed by the call's id.
613  const callLoop = new Map<string, string | null>()
614
615  on('session.start', async ($, e, next) => {
616    await $.command.register({
617      name: 'dashboard',
618      description: 'CC Dashboard, the live agent dashboard: open, close, reset, refresh, or set the layout',
619      argumentHint: '[open|close|reset|refresh|layout auto|compact|wide|mini]',
620    })
621    await migrate($)
622    // A host without usage (headless, an SDK host, a session not yet bound) just starts without it.
623    const u = await $.session.usage().catch(() => null)
624    if (u) {
625      await update($, usage, x => ({
626        ...normalize(DEFAULT_USAGE, x),
627        pct: u.context.percent ?? null,
628        tokens: u.context.tokens ?? null,
629        window: u.context.window,
630        limits: u.rateLimits.map(limitOf),
631      }))
632    }
633    if (cfg.openOnStart) void openPane($, cfg.dockPercent).catch(() => undefined)
634    await refreshStatus($, cfg)
635    startPolling($, cfg.dockPercent)
636    void pumpPolls($).catch(swallow)
637    // A fresh load writes the wait as it stands: none on a start, the open one when a reload lands while a dialog is up.
638    void writeMark($).catch(swallow)
639    return next(e)
640  })
641
642  on('session.end', async ($, e, next) => {
643    if (e.reason === 'clear') {
644      await resetAll($)
645      await refreshStatus($, cfg)
646    }
647    await endWaiting($).catch(swallow)
648    return next(e)
649  })
650
651  on('command.run', { command: 'dashboard' }, async ($, e) => {
652    const [verb = 'open', arg = ''] = e.args.trim().split(/\s+/)
653    if (e.presentation.isFullscreen) terminalColumns = e.presentation.columns
654    if (verb === 'close') {
655      await $.ui.close({ id: PANE })
656      return { text: 'Dashboard closed.' }
657    }
658    if (verb === 'reset') {
659      await resetAll($)
660      await refreshStatus($, cfg)
661      return { text: 'Dashboard reset.' }
662    }
663    if (verb === 'refresh') {
664      // Read now, whether or not the pane shows; the schedules start over from here. Fable keeps its own.
665      sessionsAt = 0
666      await Promise.all([askCodex($), ...(['git', 'others'] as const).filter(src => !busy.has(src)).map(src => runSource($, src))])
667      return { text: 'Asked the server for Codex usage, and read the branch and the other sessions waiting on you again.' }
668    }
669    if (verb === 'layout') {
670      const layout: Layout | null = arg === 'compact' || arg === 'wide' || arg === 'auto' || arg === 'mini' ? arg : null
671      if (!layout) return { text: 'Usage: /dashboard layout auto|compact|wide|mini' }
672      await update($, view, v => ({ ...normalize(DEFAULT_VIEW, v), layout }))
673      const opened = await openPane($, cfg.dockPercent)
674      return { text: opened.isPlaced ? `Dashboard layout: ${layout}.` : `Layout set to ${layout}; the pane is not shown yet: ${opened.reason}` }
675    }
676    const opened = await openPane($, cfg.dockPercent, { focus: true })
677    if (!opened.isPlaced) return { text: `Dashboard is not shown yet: ${opened.reason}` }
678    void pumpPolls($).catch(swallow)
679    return { text: 'Dashboard opened and brought to the front when the prompt is free (Esc returns to the prompt; ctrl+x tab focuses it). 1-6 expand cards, f/s/o open the gate rows, u lists the unreviewed files, r asks the server for Codex usage.' }
680  })
681
682  on('classic.UserPromptSubmit', async ($, e, next) => {
683    await noteMode($, e.permission_mode)
684    return next(e)
685  })
686
687  on('agent.offer', async ($, e, next) => {
688    const offered = await next(e)
689    if (cfg.architect.test(e.agent) || (cfg.matchDescriptions && cfg.architect.test(e.description))) {
690      await update($, roster, r => {
691        const x = normalize(DEFAULT_ROSTER, r)
692        return x.architectTypes.includes(e.agent) ? x : { architectTypes: [...listOf<string>(x.architectTypes), e.agent].slice(-20) }
693      })
694    }
695    return offered
696  })
697
698  on('turn.start', async ($, e, next) => {
699    const now = await $.clock.now()
700    await update($, turn, () => ({ ...DEFAULT_TURN, startedAt: now }))
701    await update($, main, m => ({ ...normalize(DEFAULT_MAIN, m), isRunning: true }))
702    // A background architect's report reaches the main loop as the text opening this turn. The
703    // SubagentHandback tool call (in tool.call) normally carries it first; this is the fallback.
704    const back = e.text ? handbackOf(e.text) : null
705    const a = back ? await getArchitect($) : null
706    if (back && a && a.ids.includes(back.from)) {
707      const advice = adviceLine(back.body)
708      if (advice && advice !== a.lastAdvice) await noteAdvice($, cfg, advice)
709    } else if (e.text) {
710      const p = promptLine(e.text)
711      await say($, p.who, p.text)
712    }
713    return next(e)
714  })
715
716  on('turn.step', async function* ($, e, next) {
717    // The main loop's model is known when its request starts; a long first request shouldn't read "—".
718    if (!e.agentId) {
719      await update($, main, m => {
720        const x = normalize(DEFAULT_MAIN, m)
721        return { ...x, model: e.model, effort: String(e.effort ?? x.effort), steps: x.steps + 1 }
722      })
723      return yield* next(e)
724    }
725    const result = yield* next(e)
726    const id = e.agentId
727    const [cards, a] = await Promise.all([getCards($), getArchitect($)])
728    if (cards.some(c => c.id === id)) {
729      const step = { model: e.model, effort: e.effort, usage: result.usage, stopReason: result.stopReason }
730      await update($, agents, list => listOf<unknown>(list).map(normalizeCard).map(c => (c.id === id ? applyStep(c, step) : c)))
731      if (result.stopReason === 'max_tokens') await say($, await whoIs($, id), 'hit max_tokens', 'error', id)
732    } else if (!a.ids.includes(id)) {
733      const now = await $.clock.now()
734      await update($, loops, l => stepLoop(listOf<Loop>(l), id, now))
735    }
736    return result
737  })
738
739  on('session.measure', async ($, e, next) => {
740    await update($, usage, x => ({
741      ...normalize(DEFAULT_USAGE, x),
742      pct: e.context.percent ?? null,
743      tokens: e.context.tokens ?? null,
744      window: e.context.window,
745      limits: e.rateLimits.map(limitOf),
746    }))
747    if (e.changed.includes('context')) await refreshStatus($, cfg)
748    return next(e)
749  })
750
751  on('session.compact', async ($, e, next) => {
752    const done = await next(e)
753    if (!e.agentId && e.trigger !== 'precompute') {
754      const now = await $.clock.now()
755      await update($, usage, x => {
756        const u = normalize(DEFAULT_USAGE, x)
757        return { ...u, compactions: u.compactions + 1, lastCompactAt: now }
758      })
759      await say($, 'main', `context compacted (${e.trigger})`)
760    }
761    return done
762  })
763
764  on('tool.check', async ($, e, next) => {
765    const verdict = await next(e)
766    if (verdict.decision === 'ask' && e.tool_use_id) asked.add(e.tool_use_id)
767    if (e.tool_use_id) {
768      const check: Check = {
769        id: e.tool_use_id,
770        tool: e.tool,
771        bucket: bucketOf(e.tool),
772        verdict: verdict.decision === 'allow' ? 'rule' : verdict.decision,
773        inSubagent: Boolean(callLoop.get(e.tool_use_id)),
774        detail: shorten(describeInput(e.tool, e.input), 90),
775        at: await $.clock.now(),
776      }
777      await update($, gate, g => recordCheck(normalizeGate(g), check))
778      // The status line updates when the call settles; only a refusal ends here.
779      if (verdict.decision === 'deny') {
780        await say($, 'gate', `denied by rule · ${check.detail}`, 'error')
781        await refreshStatus($, cfg)
782      }
783    }
784    return verdict
785  })
786
787  // A permission dialog opens: this session waits on the person until the call returns.
788  on('classic.PermissionRequest', async ($, e, next) => {
789    await setWaiting($, 'permission').catch(swallow)
790    return next(e)
791  }).catch(async (_$, e, next) => next(e))
792
793  // At the end of a turn the engine's own list of background tasks says which Codex runs still live.
794  on('classic.Stop', async ($, e, next) => {
795    const liveIds = e.background_tasks?.map(task => task.id)
796    for (const end of stopCodex(await getCards($), liveIds)) await codexEnded($, end.id, end.ok)
797    await refreshStatus($, cfg)
798    return next(e)
799  }).catch(async (_$, e, next) => next(e))
800
801  on('tool.call', async ($, e, next) => {
802    const tool = String(e.tool)
803    callLoop.set(e.tool_use_id, e.agentId ?? null)
804    // A review is noted whoever starts it: the tree is read as the call begins, and kept only if
805    // the call is not refused and does not fail.
806    const review = isReviewCall(tool, e) ? readTreeQuietly($) : undefined
807    // A question put to the person is a wait from the call's start to its return, whoever asks.
808    if (isQuestionTool(tool)) await setWaiting($, 'question').catch(swallow)
809    // A `codex exec` the main session runs is a subagent card from here to its end.
810    const shell = !e.agentId && (tool === 'Bash' || tool === 'PowerShell') ? (e as unknown as { command?: unknown; description?: unknown }) : null
811    const run =
812      shell && typeof shell.command === 'string'
813        ? describeCodex(shell.command, typeof shell.description === 'string' ? shell.description : undefined, tool === 'PowerShell' ? 'powershell' : 'bash')
814        : undefined
815    if (run) {
816      await codexStarted($, e.tool_use_id, run).catch(swallow)
817      await refreshStatus($, cfg)
818    }
819    const ran = await next(e)
820      .catch(async error => {
821        if (run) await codexEnded($, e.tool_use_id, false).catch(swallow)
822        throw error
823      })
824      .finally(() => {
825        callLoop.delete(e.tool_use_id)
826        void settleWait($, tool, e.tool_use_id).catch(swallow)
827      })
828    if (run) await codexSettled($, e.tool_use_id, ran).catch(swallow)
829    const didRun = ran.deny === undefined
830    if (review !== undefined && didRun && ran.isError !== true) void review.then(tree => recordReview($, tree)).catch(swallow)
831    // Settle this call's pending ask, if it had one; skip the write (and the redraw) otherwise.
832    const g0 = await getGate($)
833    const isSettled = settleCheck(g0, e.tool_use_id, didRun) !== g0
834    if (isSettled) await update($, gate, g => settleCheck(normalizeGate(g), e.tool_use_id, didRun))
835    // A background agent hands its report back through this tool; an architect's report is its advice.
836    if (String(e.tool) === 'SubagentHandback') {
837      const message = (e as unknown as { message?: unknown }).message
838      const a = e.agentId ? await getArchitect($) : null
839      if (a && e.agentId && a.ids.includes(e.agentId) && typeof message === 'string') {
840        const advice = adviceLine(message)
841        if (advice && advice !== a.lastAdvice) await noteAdvice($, cfg, advice)
842      }
843      return ran
844    }
845    if (e.tool === 'Agent') {
846      if (isSettled) await refreshStatus($, cfg)
847      return ran
848    }
849    const hasFailed = didRun && ran.isError === true
850    const isEdit = !hasFailed && didRun && EDIT_TOOLS.has(e.tool)
851    const t0 = await getTurn($)
852    if (isEdit || hasFailed || (!e.agentId && t0.errorStreak > 0)) {
853      await update($, turn, t => afterCall(normalize(DEFAULT_TURN, t), { inSubagent: Boolean(e.agentId), hasFailed, isEdit }))
854    }
855    const text = shorten(describeInput(e.tool, e), 64)
856    if (e.agentId) {
857      const id = e.agentId
858      await update($, agents, list =>
859        listOf<unknown>(list)
860          .map(normalizeCard)
861          .map(c => (c.id === id ? noteTool(c, { tool: e.tool, text, isError: hasFailed || ran.deny !== undefined }) : c)),
862      )
863    }
864    // The log keeps what is worth a glance: refusals, errors and edits; the rest is on the cards.
865    if (ran.deny !== undefined) await say($, await whoIs($, e.agentId), `${text}  denied`, 'error', e.agentId ?? null)
866    else if (hasFailed) await say($, await whoIs($, e.agentId), `${text}  ✗`, 'error', e.agentId ?? null)
867    else if (isEdit) await say($, await whoIs($, e.agentId), text, 'info', e.agentId ?? null)
868    if (isSettled || !didRun || run) await refreshStatus($, cfg)
869    return ran
870  })
871
872  // A typed /…review has no result to wait for: it counts as it is sent, if the person typed it.
873  // A background task's notification arrives as a prompt of its own origin: it ends a Codex run.
874  // What the person types answers whatever was asked.
875  on('prompt.submit', async ($, e, next) => {
876    if (e.origin.kind === 'task-notification') {
877      for (const n of parseNotifications(e.text)) {
878        const card = codexCardFor(await getCards($), n.id, n.toolUseId)
879        if (card) await codexEnded($, card.id, n.ok).catch(swallow)
880      }
881      await refreshStatus($, cfg)
882    }
883    const result = await next(e)
884    if (result.drop === undefined && isPersonPrompt(e.origin.kind)) {
885      if (isReviewPrompt(e.text)) {
886        void readTreeQuietly($)
887          .then(tree => recordReview($, tree))
888          .catch(swallow)
889      }
890      await endWaiting($).catch(swallow)
891    }
892    return result
893  }).catch(async (_$, e, next) => next(e))
894
895  // A server-side review tool never reaches tool.call: it shows only in the assistant's rows.
896  on('session.append', async ($, e, next) => {
897    if (!e.agentId && e.message.type === 'assistant') {
898      const a = await getArchitect($)
899      // Consults this row opened: their result may be in the same row, after the stale read above.
900      const opened = new Set<string>()
901      for (const block of e.message.content as unknown as readonly ServerBlock[]) {
902        if (block.type === 'server_tool_use' && block.name && block.id && cfg.architect.test(block.name)) {
903          if (a.seen.includes(block.id)) continue
904          const id = block.id
905          await update($, architect, x => {
906            const y = normalize(DEFAULT_ARCHITECT, x)
907            return { ...y, seen: [...listOf<string>(y.seen), id].slice(-60) }
908          })
909          opened.add(id)
910          await consultStarted($, cfg, id, `${block.name} tool`)
911        } else if (block.type.endsWith('_tool_result') && block.tool_use_id) {
912          const id = block.tool_use_id
913          const isOpen = opened.has(id) || (await getArchitect($)).consults.some(c => c.id === id && c.endAt === null)
914          if (isOpen) await consultEnded($, cfg, null, id)
915        }
916      }
917    }
918    return next(e)
919  })
920
921  on('agent.spawn', async ($, e, next) => {
922    const started = await next(e)
923    if (!e.parentAgentId) await noteMode($, e.permissionMode)
924    if (started.deny !== undefined || !started.agentId) return started
925    const id = started.agentId
926    if (await isArchitectType($, cfg, e.subagentType)) {
927      await update($, architect, a => {
928        const x = normalize(DEFAULT_ARCHITECT, a)
929        return { ...x, ids: [...listOf<string>(x.ids), id].slice(-40) }
930      })
931      await update($, loops, l => listOf<Loop>(l).filter(x => x.id !== id))
932      await consultStarted($, cfg, id, e.subagentType.split(':').pop() ?? 'agent')
933      return started
934    }
935    const card: AgentCard = {
936      ...normalizeCard({}),
937      id,
938      type: e.name ?? e.subagentType,
939      model: started.model,
940      description: e.description,
941      spawnedAt: await $.clock.now(),
942    }
943    // A resumed subagent comes back with its old id: its fresh card replaces the old one
944    await update($, agents, list => uniqueById([...listOf<unknown>(list).map(normalizeCard), card]).slice(-24))
945    await update($, loops, l => listOf<Loop>(l).filter(x => x.id !== id))
946    await say($, shorten(cardTitle(card), 12), `spawned · ${card.type}`, 'info', id)
947    await refreshStatus($, cfg)
948    return started
949  })
950
951  on('turn.complete', async ($, e, next) => {
952    const done = await next(e)
953    const id = e.agentId
954    const now = await $.clock.now()
955    if (!id) {
956      const [t, cards] = await Promise.all([getTurn($), getCards($)])
957      const r = receiptOf(t, {
958        durationMs: e.durationMs,
959        agentsSince: cards.filter(c => c.spawnedAt >= t.startedAt).length,
960        reason: e.reason,
961      })
962      await update($, receipt, () => r)
963      await update($, main, m => ({ ...normalize(DEFAULT_MAIN, m), isRunning: false }))
964      // The turn may have changed files: the next pump reads git without waiting out its 30 seconds.
965      dueAt.git = 0
966      await endWaiting($).catch(swallow)
967      await refreshStatus($, cfg)
968      return done
969    }
970    if ((await getArchitect($)).ids.includes(id)) {
971      await consultEnded($, cfg, e.answer, id)
972      return done
973    }
974    const cards = await getCards($)
975    if (cards.some(c => c.id === id)) {
976      const status = e.reason === 'answer' ? 'done' : e.reason === 'aborted' ? 'stopped' : 'failed'
977      await update($, agents, list =>
978        listOf<unknown>(list)
979          .map(normalizeCard)
980          .map(c => (c.id === id ? { ...c, status, endedAt: now, answer: shorten(e.answer, 400) } : c)),
981      )
982      const card = cards.find(c => c.id === id)
983      const took = card ? fmtDuration(now - card.spawnedAt) : ''
984      await say($, await whoIs($, id), status === 'done' ? `done · ${took}` : status, status === 'done' ? 'done' : 'error', id)
985    } else {
986      await update($, loops, l => listOf<Loop>(l).map(x => (x.id === id ? { ...x, isDone: true, lastAt: now } : x)))
987    }
988    await refreshStatus($, cfg)
989    return done
990  })
991
992  // ---------------------------------------------------------------- drawing
993
994  const receiptText = (x: { durationMs: number; agents: number; edits: number; errors: number }) =>
995    `last turn ${fmtDuration(x.durationMs)} · ${plural(x.agents, 'agent')} · ${plural(x.edits, 'edit')} · ${plural(x.errors, 'error')}`
996
997  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
998    // Noted for the pump, which sizes the dock: a drawing itself only reads. Only the terminal's
999    // drawing counts; a desktop or phone attached to the session measures a pane of its own.
1000    if (e.surface === 'terminal') {
1001      lastPlacement = e.props.placement
1002      // Docked, the viewport is the transcript beside the dock, so the dock is added back.
1003      if (e.viewport?.isFullscreen === true) {
1004        terminalColumns = e.props.placement === 'dock' ? terminalWidth(e.viewport.columns, e.props.bodyColumns) : e.viewport.columns
1005      }
1006    }
1007    const els = $.ui.resolve(e)
1008    const { Box, Text, Button } = els
1009    const hasClient = 'Client' in els
1010    const [m, u, a, g, cards, lp, lines, t, r, v, now, cx, gs, fb, others] = await Promise.all([
1011      getMain($),
1012      getUsage($),
1013      getArchitect($),
1014      getGate($),
1015      getCards($),
1016      getLoops($),
1017      getLog($),
1018      getTurn($),
1019      read($, receipt),
1020      getView($),
1021      $.clock.now(),
1022      read($, codex),
1023      read($, git),
1024      read($, fable),
1025      read($, waitingOthers),
1026    ])
1027    // Another live session waiting on the person: one red line, absent while nobody waits.
1028    const banner = bannerText(others, now)
1029    const fableWindow = fb?.window
1030    const W = Math.max(DOCK_MIN_COLUMNS, e.props.bodyColumns)
1031    const layout = v.layout ?? cfg.layout
1032    const isWide = layout === 'wide' || (layout === 'auto' && W >= 110)
1033    const colW = isWide ? Math.floor((W - 2) / 2) : W
1034    const modelName = prettyModel(m.model)
1035    const viewed = e.props.view?.agentId ?? null
1036    const advising = isAdvising(a)
1037    const running = cards.filter(c => c.status === 'running')
1038    const showArchitect = a.consults.length > 0 || a.ids.length > 0
1039    const motion = cfg.motion && hasClient
1040    // A panel with nothing to show yet takes no room: most sessions never spawn an agent.
1041    const isEmpty: Record<Panel, boolean> = {
1042      main: false,
1043      codex: cx === null,
1044      git: gs === null,
1045      architect: !showArchitect,
1046      gate: g.recent.length === 0 && gateSummary(g).total === 0,
1047      agents: cards.length === 0,
1048      loops: lp.length === 0,
1049      receipt: !m.isRunning && !r,
1050      log: false,
1051    }
1052    const panels = cfg.panels.filter(p => !isEmpty[p])
1053    // The single column seats two panels to a row when the pane is wide enough and the person left the layout on auto.
1054    const pairing = layout === 'auto' && !isWide ? pairWidths(W) : null
1055    // The context is a line of its own under the main box (under the Codex box when that comes next), once it is read.
1056    const ctxMeter: Meter | null =
1057      u.pct !== null
1058        ? { label: 'Context', pct: Math.round(u.pct), isStale: false, detail: u.tokens !== null ? `${kTokens(u.tokens)}/${kTokens(u.window)}` : '' }
1059        : null
1060    const ctxNote = u.compactions > 0 ? `  ⟲${u.compactions}` : ''
1061    const withContext = (list: readonly PairRow[]): ColumnRow[] => (ctxMeter ? withCtx(list) : [...list])
1062    const rows = withContext(pairing ? pairRows(panels) : panels)
1063    const isPaired = (a: Panel, b: Panel) => rows.some(r => typeof r !== 'string' && r[0] === a && r[1] === b)
1064    const decider = m.mode === 'auto' ? 'classifier' : 'you'
1065
1066    // A connector between panels: animated while its flow is live, a dim line otherwise.
1067    const rail = (key: string, active: boolean, color: string, width: number, marks: number[] = [], isMerge = false) =>
1068      motion ? (
1069        <els.Client
1070          key={key}
1071          module="./rail.tsx"
1072          width={width}
1073          height={1}
1074          props={{ active, width, color, dim: C.faint, marks, isMerge }}
1075        />
1076      ) : (
1077        <Text color={C.faint}>{'─'.repeat(Math.max(1, width))}</Text>
1078      )
1079
1080    // A start time of 0 is unknown (state saved before it was recorded): no clock, not decades.
1081    const clock = (key: string, since: number, endAt: number | null, color: string) =>
1082      since <= 0 ? (
1083        <Text color={color}>—</Text>
1084      ) : hasClient ? (
1085        <els.Client key={key} module="./elapsed.tsx" props={{ since, now, endAt, color }} />
1086      ) : (
1087        <Text color={color}>{fmtTimer((endAt ?? now) - since)}</Text>
1088      )
1089
1090    // A usage row as the first version drew its quota header: the label, a bar across the line, the
1091    // percent in a column of its own, then the time to the reset. The rows
1092    // of one panel share a label column and a bar width (see meterLayout), so their bars line up.
1093    // Past its reset a window reads 0%, marked `~` and `stale`; a window not read yet is a dash.
1094    const meterRow = (key: string, row: Meter, layout: { labelW: number; barW: number }, color: string) => {
1095      const label = padCells(row.label, layout.labelW)
1096      if (row.pct === null) return <Text key={key} dimColor wrap="truncate">{`${label} —`}</Text>
1097      const bar = gauge(row.pct, layout.barW, LINE_GLYPHS)
1098      const tone = row.isStale ? C.dim : row.pct >= 80 ? C.warn : color
1099      return (
1100        <Text key={key} wrap="truncate">
1101          <Text dimColor>{`${label} `}</Text>
1102          <Text color={tone}>{bar.on}</Text>
1103          <Text color={C.faint}>{bar.off}</Text>
1104          <Text color={tone} bold={!row.isStale}>{` ${meterPercent(row)}`}</Text>
1105          {row.detail ? <Text dimColor>{` ${row.detail}`}</Text> : null}
1106        </Text>
1107      )
1108    }
1109    // The label column and bar width a panel's rows share, the bar sized by the widest detail after a percent.
1110    const meterLayout = (rows: Meter[], w: number) => {
1111      const labelW = Math.max(0, ...rows.map(r => cells(r.label)))
1112      return { labelW, barW: meterWidth(w, labelW, rows.map(r => r.detail)) }
1113    }
1114
1115    // ---- main
1116    const effortN = { low: 1, medium: 2, high: 3, xhigh: 4, max: 4 }[m.effort] ?? 0
1117    // Claude's windows and Fable, a row each.
1118    const usageMeters = [
1119      ...u.limits.slice(0, 2).map(l => windowMeter(limitLabel(l.kind), limitWindow(l), now)),
1120      ...(fableWindow ? [windowMeter(fb?.isStale ? 'Fable~' : 'Fable', fableWindow, now)] : []),
1121    ]
1122    const mainPanel = (w: number, isNarrow = false) => {
1123      const layout = meterLayout(usageMeters, w)
1124      const effortBar = '▮'.repeat(effortN) + '▯'.repeat(4 - effortN)
1125      const narrowLine = narrowEffort(m.effort, m.mode, m.steps, w - PANEL_FRAME)
1126      return (
1127        <Box flexDirection="column" borderStyle="round" borderColor={C.main} paddingX={1} width={w}>
1128          <Box justifyContent="space-between">
1129            <Text color={C.main} bold>
1130              {modelName} · main
1131            </Text>
1132            <Text color={m.isRunning ? C.main : C.dim}>{m.isRunning ? '● working' : '○ idle'}</Text>
1133          </Box>
1134          {isNarrow ? (
1135            <Text wrap="truncate">
1136              <Text color={C.main}>{effortBar} </Text>
1137              <Text color={C.main} bold>
1138                {narrowLine.effort}
1139              </Text>
1140              <Text dimColor>{narrowLine.tail}</Text>
1141            </Text>
1142          ) : (
1143            <Text wrap="truncate">
1144              <Text dimColor>effort </Text>
1145              <Text color={C.main}>{effortBar} </Text>
1146              <Text color={C.main} bold>
1147                {m.effort || '—'}
1148              </Text>
1149              {m.mode ? <Text dimColor>{`   mode ${m.mode}`}</Text> : null}
1150              <Text dimColor>{`   ${m.steps} req`}</Text>
1151            </Text>
1152          )}
1153          {usageMeters.map((row, i) => meterRow(`usage-${i}`, row, layout, C.main))}
1154        </Box>
1155      )
1156    }
1157    // The context: one line about half the pane wide, no box, left-aligned in its column with a blank row below; its bar
1158    // is sized by its own detail and note, and the filled cells and the percent share one colour by how full it is.
1159    const ctxLine = (w: number) => {
1160      if (!ctxMeter || ctxMeter.pct === null) return null
1161      const barW = lineMeterWidth(Math.round(w * CTX_LINE_SHARE), cells(ctxMeter.label), ctxMeter.detail, ctxNote)
1162      const bar = gauge(ctxMeter.pct, barW, BLOCK_GLYPHS)
1163      const tone = C[ctxTone(ctxMeter.pct)]
1164      return (
1165        <Box key="context" width={w} marginBottom={1}>
1166          <Text wrap="truncate">
1167            <Text dimColor>{`${ctxMeter.label} `}</Text>
1168            <Text color={tone}>{bar.on}</Text>
1169            <Text color={C.faint}>{bar.off}</Text>
1170            <Text color={tone} bold>{` ${meterPercent(ctxMeter)}`}</Text>
1171            {ctxMeter.detail ? <Text dimColor>{` ${ctxMeter.detail}`}</Text> : null}
1172            {ctxNote ? <Text color={C.amber}>{ctxNote}</Text> : null}
1173          </Text>
1174        </Box>
1175      )
1176    }
1177
1178    // ---- architect
1179    const lastConsult = a.consults[a.consults.length - 1]
1180    const architectPanel = (w: number) => {
1181      const tl = consultTimeline(a, now, Math.max(8, w - 4))
1182      return (
1183        <Box flexDirection="column" borderStyle="round" borderColor={C.arch} paddingX={1} width={w}>
1184          <Box justifyContent="space-between">
1185            <Text color={C.arch} bold>
1186              {cfg.architectLabel} · {advising ? 'advising' : 'on call'}
1187            </Text>
1188            <Text>
1189              <Text dimColor>consults </Text>
1190              <Text color={C.arch} bold>
1191                {a.consults.length}
1192              </Text>
1193            </Text>
1194          </Box>
1195          <Text color={C.arch}>{tl}</Text>
1196          {lastConsult ? (
1197            <Text dimColor wrap="truncate">
1198              {advising
1199                ? `consulting since ${fmtClock(lastConsult.at)}`
1200                : `last ${fmtDuration(now - (lastConsult.endAt ?? lastConsult.at))} ago · took ${fmtDuration((lastConsult.endAt ?? now) - lastConsult.at)}`}
hooks/core.ts 811 lines
1// Pure data: defaults, reducers, formatting and layout math. Nothing here touches `$`, so every
2// behaviour is testable directly (the test kit cannot raise a subagent's tool call or a
3// permission check inside a call; these functions are what the hooks apply).
4import type {
5  AgentCard,
6  Architect,
7  Bucket,
8  Check,
9  Gate,
10  Layout,
11  LogLine,
12  Loop,
13  Main,
14  Moment,
15  Receipt,
16  Roster,
17  Tally,
18  ToolNote,
19  Turn,
20  Usage,
21  UsageWindow,
22  GitSnap,
23  View,
24} from '../types'
25
26export const SCHEMA_VERSION = 3
27
28// ---------------------------------------------------------------- defaults
29
30export const DEFAULT_MAIN: Main = { model: '', effort: '', mode: '', steps: 0, isRunning: false }
31export const DEFAULT_USAGE: Usage = {
32  pct: null,
33  tokens: null,
34  window: 0,
35  limits: [],
36  compactions: 0,
37  lastCompactAt: null,
38}
39export const DEFAULT_ARCHITECT: Architect = { consults: [], ids: [], seen: [], lastAdvice: '' }
40const ZERO: Tally = { rule: 0, ask: 0, cleared: 0, deny: 0 }
41export const DEFAULT_GATE: Gate = { recent: [], totals: { file: ZERO, shell: ZERO, other: ZERO } }
42export const DEFAULT_TURN: Turn = { edits: 0, errorStreak: 0, errors: 0, isReviewing: false, startedAt: 0 }
43export const DEFAULT_VIEW: View = { expanded: null, gateOpen: null, layout: null, gitOpen: false }
44export const DEFAULT_ROSTER: Roster = { architectTypes: [] }
45
46const isObject = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v)
47
48/** A stored object merged over its defaults, so a value saved under an older shape still reads. */
49export const normalize = <T extends object>(def: T, stored: unknown): T =>
50  isObject(stored) ? ({ ...def, ...stored } as T) : def
51
52/** A stored list, or empty when what is stored is not a list. */
53export const listOf = <T>(stored: unknown): T[] => (Array.isArray(stored) ? (stored as T[]) : [])
54
55export const normalizeGate = (stored: unknown): Gate => {
56  const g = normalize(DEFAULT_GATE, stored)
57  const totals = normalize(DEFAULT_GATE.totals, g.totals)
58  return {
59    recent: listOf<Check>(g.recent),
60    totals: { file: normalize(ZERO, totals.file), shell: normalize(ZERO, totals.shell), other: normalize(ZERO, totals.other) },
61  }
62}
63
64export const normalizeCard = (stored: unknown): AgentCard =>
65  normalize<AgentCard>(
66    {
67      id: '',
68      type: 'agent',
69      model: '',
70      effort: '',
71      description: '',
72      status: 'running',
73      spawnedAt: 0,
74      endedAt: null,
75      ctx: 0,
76      out: 0,
77      steps: 0,
78      lastStop: null,
79      tools: [],
80      answer: '',
81    },
82    stored,
83  )
84
85/** Cards with unique ids: the last of any repeated id wins, the rest keep their order. */
86export const uniqueById = (cards: AgentCard[]): AgentCard[] => {
87  const ids = cards.map(c => c.id)
88  return cards.filter((c, i) => ids.lastIndexOf(c.id) === i)
89}
90
91export const normalizeLog = (stored: unknown): LogLine[] =>
92  listOf<Record<string, unknown>>(stored).map(l => ({
93    at: typeof l.at === 'number' ? l.at : 0,
94    who: String(l.who ?? ''),
95    text: String(l.text ?? ''),
96    agentId: typeof l.agentId === 'string' ? l.agentId : null,
97    kind: l.kind === 'error' || l.kind === 'consult' || l.kind === 'done' ? l.kind : 'info',
98  }))
99
100// ---------------------------------------------------------------- config
101
102export type Panel = 'main' | 'codex' | 'git' | 'architect' | 'gate' | 'agents' | 'loops' | 'receipt' | 'log'
103const PANELS: readonly Panel[] = ['main', 'codex', 'git', 'architect', 'gate', 'agents', 'loops', 'receipt', 'log']
104
105export type Config = {
106  architect: RegExp
107  architectLabel: string
108  gateLabel: string
109  panels: Panel[]
110  motion: boolean
111  moments: boolean
112  matchDescriptions: boolean
113  maxCards: number
114  layout: Layout
115  palette: Palette
116  openOnStart: boolean
117  statusLine: boolean
118  /** The share of the terminal's width the dock asks for in the fullscreen layout, in percent. */
119  dockPercent: number
120}
121
122const safeRegExp = (source: string, fallback: string) => {
123  try {
124    return new RegExp(source || fallback, 'i')
125  } catch {
126    return new RegExp(fallback, 'i')
127  }
128}
129
130/** The plugin's `/config` values, read leniently: anything malformed falls back to the default. */
131export const parseConfig = (o: Readonly<Record<string, unknown>>): Config => {
132  const str = (k: string, d: string) => (typeof o[k] === 'string' && o[k] !== '' ? (o[k] as string) : d)
133  const bool = (k: string, d: boolean) => (typeof o[k] === 'boolean' ? (o[k] as boolean) : d)
134  const panels = str('panels', PANELS.join(','))
135    .split(',')
136    .map(s => s.trim())
137    .filter((p): p is Panel => (PANELS as readonly string[]).includes(p))
138  const layout = str('layout', 'auto')
139  const max = typeof o.maxCards === 'number' ? Math.round(o.maxCards) : 3
140  const dock = typeof o.dockPercent === 'number' && Number.isFinite(o.dockPercent) ? Math.round(o.dockPercent) : DOCK_PERCENT
141  return {
142    architect: safeRegExp(str('architectPattern', ''), 'advisor|architect'),
143    architectLabel: str('architectLabel', 'ARCHITECT'),
144    gateLabel: str('gateLabel', 'GATE'),
145    panels: panels.length > 0 ? [...new Set(panels)] : [...PANELS],
146    motion: str('motion', 'while-active') !== 'off',
147    moments: bool('moments', true),
148    matchDescriptions: bool('matchDescriptions', false),
149    maxCards: Math.min(6, Math.max(1, max)),
150    layout: layout === 'compact' || layout === 'wide' || layout === 'mini' ? layout : 'auto',
151    palette: str('palette', 'theme') === 'pastel' ? 'pastel' : 'theme',
152    openOnStart: bool('openOnStart', true),
153    statusLine: bool('statusLine', true),
154    dockPercent: Math.min(DOCK_PERCENT_MAX, Math.max(DOCK_PERCENT_MIN, dock)),
155  }
156}
157
158// ---------------------------------------------------------------- dock
159
160const DOCK_PERCENT = 40
161const DOCK_PERCENT_MIN = 20
162const DOCK_PERCENT_MAX = 80
163/** The cells the dock's frame takes beside its body: the divider and the edge. */
164const DOCK_FRAME_COLUMNS = 2
165/** The narrowest body the pane lays out for; the drawing never goes below it either. */
166export const DOCK_MIN_COLUMNS = 40
167
168/** The body columns to ask the dock for: `percent` of the terminal's width, less the frame. */
169export const dockColumns = (terminalColumns: number, percent: number) =>
170  Math.max(DOCK_MIN_COLUMNS, Math.round((terminalColumns * percent) / 100) - DOCK_FRAME_COLUMNS)
171
172/**
173 * The terminal width from which the engine draws an open that did not answer the person's input
174 * (a timer, session.start); narrower, such an open may leave the pane undrawn, so it is not made.
175 * The engine's own floor is 110 for an id the person already opened; 144 is kept as the conservative
176 * floor because the plugin cannot know that.
177 */
178export const DOCK_UNASKED_MIN_COLUMNS = 144
179
180/** Whether the dock's share moved since it was last asked: it differs from the width requested then. */
181export const dockShareMoved = (terminalColumns: number, percent: number, requestedColumns: number | undefined) =>
182  dockColumns(terminalColumns, percent) !== requestedColumns
183
184/** The cell the engine draws beside a docked pane's body, for the person to drag it by. */
185const DOCK_GRIP_COLUMNS = 1
186
187/**
188 * The terminal's width from a docked drawing: on the terminal `viewport.columns` is the transcript's
189 * columns only, so the dock (its body and its grip) is added back.
190 */
191export const terminalWidth = (viewportColumns: number, bodyColumns: number) =>
192  viewportColumns + bodyColumns + DOCK_GRIP_COLUMNS
193
194// ---------------------------------------------------------------- palette
195
196export type Palette = 'theme' | 'pastel'
197export type Colors = Record<'main' | 'agent' | 'gate' | 'cleared' | 'arch' | 'codex' | 'branch' | 'ok' | 'amber' | 'warn' | 'dim' | 'faint' | 'text', string>
198
199/**
200 * `theme` names the person's own theme colours (they follow light, dark and colour-blind themes);
201 * `pastel` is fixed hex tuned for dark terminals.
202 */
203export const PALETTES: Record<Palette, Colors> = {
204  theme: {
205    main: 'claude',
206    agent: 'suggestion',
207    gate: 'success',
208    cleared: 'permission',
209    arch: 'merged',
210    codex: 'ide',
211    branch: 'promptBorder',
212    ok: 'success',
213    amber: 'warning',
214    warn: 'error',
215    dim: 'inactive',
216    faint: 'subtle',
217    text: 'text',
218  },
219  pastel: {
220    main: '#7dd3fc',
221    agent: '#93c5fd',
222    gate: '#86efac',
223    cleared: '#5eead4',
224    arch: '#c4b5fd',
225    codex: '#60a5fa',
226    branch: '#d0d7de',
227    ok: '#86efac',
228    amber: '#fcd34d',
229    warn: '#fca5a5',
230    dim: '#6b7280',
231    faint: '#3f4654',
232    text: '#e5e7eb',
233  },
234}
235
236/** SVG cannot name theme keys: mid-tone colours that read on light and dark backgrounds. */
237export const SVG_COLORS = { running: '#3b82f6', done: '#16a34a', failed: '#dc2626', other: '#8b5cf6', label: '#6b7280' }
238
239// ---------------------------------------------------------------- formatting
240
241/** `claude-opus-5-5` → `Opus 5.5`; anything else is shown as given. */
242/**
243 * A model id as people say it, from any provider's spelling: `claude-opus-5-5[1m]` → `Opus 5.5 1M`,
244 * `us.anthropic.claude-sonnet-4-5-20250929-v1:0` → `Sonnet 4.5`, `claude-3-5-haiku-20241022` →
245 * `Haiku 3.5`. Anything else is shown as given, cut to 22 characters.
246 */
247export const prettyModel = (id: string) => {
248  if (!id) return '—'
249  const cap = (f: string) => f.charAt(0).toUpperCase() + f.slice(1)
250  const big = /\[1m\]|-1m\b/i.test(id) ? ' 1M' : ''
251  const now = /claude-([a-z]+)-(\d+)(?:-(\d{1,2}))?(?:-\d{8})?(?![\d])/i.exec(id)
252  if (now?.[1] && !/^\d/.test(now[1])) return `${cap(now[1].toLowerCase())} ${now[2]}${now[3] ? `.${now[3]}` : ''}${big}`
253  const old = /claude-(\d+)(?:-(\d))?-([a-z]+)/i.exec(id)
254  if (old?.[3]) return `${cap(old[3].toLowerCase())} ${old[1]}${old[2] ? `.${old[2]}` : ''}${big}`
255  return shorten(id, 22)
256}
257
258export const shorten = (s: string, n: number) => {
259  const one = s.replace(/\s+/g, ' ').trim()
260  return n <= 0 ? '' : one.length > n ? `${one.slice(0, Math.max(0, n - 1)).trimEnd()}…` : one
261}
262
263export const kTokens = (n: number) =>
264  n >= 1_000_000 ? `${(n / 1_000_000).toFixed(1)}M` : n >= 1000 ? `${Math.round(n / 1000)}k` : String(n)
265
266export const fmtDuration = (ms: number) => {
267  const s = Math.max(0, Math.round(ms / 1000))
268  if (s < 60) return `${s}s`
269  const m = Math.floor(s / 60)
270  return m < 60 ? `${m}m${String(s % 60).padStart(2, '0')}s` : `${Math.floor(m / 60)}h${String(m % 60).padStart(2, '0')}m`
271}
272
273/** A running clock as the live cards draw it: m:ss, or XhYY past an hour. */
274export const fmtTimer = (ms: number) => {
275  const s = Math.max(0, Math.floor(ms / 1000))
276  const m = Math.floor(s / 60)
277  return m < 60 ? `${m}:${String(s % 60).padStart(2, '0')}` : `${Math.floor(m / 60)}h${String(m % 60).padStart(2, '0')}`
278}
279
280export const fmtClock = (ms: number) => (ms > 0 ? new Date(ms).toTimeString().slice(0, 8) : '--:--:--')
281
282/** `1 error`, `2 errors`. */
283export const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? '' : 's'}`
284
285/** A gauge of `width` cells: ▰ filled, ▱ empty, or in the glyphs given. */
286export const gauge = (pct: number, width: number, [filled, empty]: readonly [string, string] = ['▰', '▱']) => {
287  const full = Math.max(0, Math.min(width, Math.round((pct / 100) * width)))
288  return { on: filled.repeat(full), off: empty.repeat(Math.max(0, width - full)) }
289}
290
291/** The usage rows' glyphs, as the first version drew its quota bars: ━ used, ─ the rest. */
292export const LINE_GLYPHS = ['━', '─'] as const
293
294/** The context bar's glyphs, as the first version drew it: █ used, ░ the rest. Swap them here. */
295export const BLOCK_GLYPHS = ['█', '░'] as const
296
297/** The colour of the context bar and its percent: green under 50, yellow from 50, red from 80. */
298export const ctxTone = (pct: number): 'ok' | 'amber' | 'warn' => (pct >= 80 ? 'warn' : pct >= 50 ? 'amber' : 'ok')
299
300/** A rate-limit reading as the pane keeps it, its ISO reset time turned into ms since the epoch. */
301export const limitOf = (r: { kind: string; percentUsed: number; resetsAt?: string }) => {
302  const at = r.resetsAt ? Date.parse(r.resetsAt) : NaN
303  return { kind: r.kind, pct: r.percentUsed, resetsAt: Number.isFinite(at) ? at : null }
304}
305
306/** Time left until a window resets, its two largest units: `25m`, `3h10m`, `4d2h`; empty when unknown or past. */
307export const fmtResetIn = (resetsAt: number | null | undefined, now: number) => {
308  if (resetsAt === null || resetsAt === undefined || resetsAt <= now) return ''
309  const m = Math.max(1, Math.floor((resetsAt - now) / 60_000))
310  const d = Math.floor(m / 1440)
311  const h = Math.floor((m % 1440) / 60)
312  if (d > 0) return `${d}d${h}h`
313  return h > 0 ? `${h}h${String(m % 60).padStart(2, '0')}m` : `${m}m`
314}
315
316/** A rate-limit window's short name: `five_hour` → `5h`, `seven_day_opus` → `7d opus`. */
317export const limitLabel = (kind: string) =>
318  kind
319    .replace(/five[_ -]?hours?/i, '5h')
320    .replace(/seven[_ -]?days?/i, '7d')
321    .replace(/[_-]+/g, ' ')
322    .trim()
323
324// ---------------------------------------------------------------- redaction
325
326const SECRETS: [RegExp, string][] = [
327  [/(authorization\s*[:=]\s*)(bearer\s+|basic\s+)?\S+/gi, '$1$2•••'],
328  [/\b(bearer)\s+[A-Za-z0-9._~+/-]{8,}=*/gi, '$1 •••'],
329  [/\b(sk|pk|rk|ghp|gho|ghs|github_pat|xox[abprs])[-_][A-Za-z0-9_-]{8,}/g, '•••'],
330  [/((?:api[_-]?key|access[_-]?token|token|secret|password|passwd|pwd)\s*[=:]\s*)("[^"]*"|'[^']*'|\S+)/gi, '$1•••'],
331  [/(--(?:token|password|api-key|secret)[= ])\S+/gi, '$1•••'],
332  [/(\b[A-Z][A-Z0-9_]*(?:KEY|TOKEN|SECRET|PASSWORD)=)\S+/g, '$1•••'],
333  [/(\b[a-z][a-z0-9+.-]*:\/\/[^\s/:@]+:)[^\s@]+@/gi, '$1•••@'],
334]
335
336/** What the gate drill-down may store: credentials masked before anything is written to state. */
337export const redact = (s: string) => SECRETS.reduce((t, [re, to]) => t.replace(re, to), s)
338
339// ---------------------------------------------------------------- tools and gate
340
341const FILE_TOOLS = new Set(['Read', 'Edit', 'Write', 'NotebookEdit', 'Glob', 'Grep'])
342const SHELL_TOOLS = new Set(['Bash', 'PowerShell'])
343export const EDIT_TOOLS = new Set(['Edit', 'Write', 'NotebookEdit'])
344
345export const bucketOf = (tool: string): Bucket =>
346  FILE_TOOLS.has(tool) ? 'file' : SHELL_TOOLS.has(tool) ? 'shell' : 'other'
347
348/** One line saying what a call was about: its command, path or pattern; redacted. */
349export const describeInput = (tool: string, input: unknown) => {
350  const i = isObject(input) ? input : {}
351  const path = typeof i.file_path === 'string' ? i.file_path.split(/[\\/]/).slice(-2).join('/') : ''
352  const what =
353    typeof i.command === 'string'
354      ? i.command
355      : path || (typeof i.pattern === 'string' ? i.pattern : typeof i.url === 'string' ? i.url : typeof i.description === 'string' ? i.description : '')
356  return redact(what ? `${tool} → ${what}` : tool)
357}
358
359/** Keeps the last `max` checks, but never drops a pending ask: its settle must still find it. */
360export const trimRecent = (list: Check[], max: number): Check[] => {
361  let extra = list.length - max
362  if (extra <= 0) return list
363  const out: Check[] = []
364  for (const c of list) {
365    if (extra > 0 && c.verdict !== 'ask') {
366      extra -= 1
367      continue
368    }
369    out.push(c)
370  }
371  return out.slice(-max * 2)
372}
373
374export const recordCheck = (g: Gate, c: Check): Gate => {
375  const t = g.totals[c.bucket]
376  return {
377    recent: trimRecent([...g.recent, c], 80),
378    totals: { ...g.totals, [c.bucket]: { ...t, [c.verdict]: t[c.verdict] + 1 } },
379  }
380}
381
382/** An `ask` settled by the call that followed it: it ran (cleared) or was refused (deny). */
383export const settleCheck = (g: Gate, id: string, didRun: boolean): Gate => {
384  const c = g.recent.find(r => r.id === id && r.verdict === 'ask')
385  if (!c) return g
386  const verdict = didRun ? 'cleared' : 'deny'
387  const t = g.totals[c.bucket]
388  return {
389    recent: g.recent.map(r => (r === c ? { ...r, verdict } : r)),
390    totals: { ...g.totals, [c.bucket]: { ...t, ask: Math.max(0, t.ask - 1), [verdict]: t[verdict] + 1 } },
391  }
392}
393
394export const gateSummary = (g: Gate) => {
395  const all = (['file', 'shell', 'other'] as const).reduce(
396    (s, k) => ({
397      rule: s.rule + g.totals[k].rule,
398      ask: s.ask + g.totals[k].ask,
399      cleared: s.cleared + g.totals[k].cleared,
400      deny: s.deny + g.totals[k].deny,
401    }),
402    { ...ZERO },
403  )
404  return { ...all, total: all.rule + all.ask + all.cleared + all.deny }
405}
406
407// ---------------------------------------------------------------- turn and architect
408
409/**
410 * The turn after one tool call. Errors count in the main loop only (a subagent's failure is its
411 * own); edits count from every loop, so delegated work still reaches "before done".
412 */
413export const afterCall = (t: Turn, c: { inSubagent: boolean; hasFailed: boolean; isEdit: boolean }): Turn => ({
414  ...t,
415  errorStreak: c.inSubagent ? t.errorStreak : c.hasFailed ? t.errorStreak + 1 : 0,
416  errors: t.errors + (!c.inSubagent && c.hasFailed ? 1 : 0),
417  edits: t.edits + (c.isEdit ? 1 : 0),
418})
419
420/** Which of the architect's three moments a consult falls at: an inference over this turn so far. */
421export const momentOf = (t: Pick<Turn, 'edits' | 'errorStreak'>): Moment =>
422  t.errorStreak >= 2 ? 'error repeats' : t.edits === 0 ? 'before a plan' : 'before done'
423
424export const startConsult = (a: Architect, c: { id: string; at: number; moment: Moment; via: string }): Architect =>
425  a.consults.some(x => x.id === c.id) ? a : { ...a, consults: [...a.consults, { ...c, endAt: null }].slice(-40) }
426
427/** Ends the open consult (the latest without an end), or the one named. */
428export const endConsult = (a: Architect, at: number, advice: string | null, id?: string): Architect => {
429  const open = [...a.consults].reverse().find(c => c.endAt === null && (id === undefined || c.id === id))
430  return {
431    ...a,
432    consults: a.consults.map(c => (c === open ? { ...c, endAt: at } : c)),
433    lastAdvice: advice ?? a.lastAdvice,
434  }
435}
436
437export const isAdvising = (a: Architect) => a.consults.some(c => c.endAt === null)
438
439/** A one-row timeline of consults across `width` cells: ◆ a consult, ━ while it ran. */
440export const consultTimeline = (a: Architect, now: number, width: number) => {
441  if (a.consults.length === 0 || width < 4) return '─'.repeat(Math.max(0, width))
442  const first = a.consults[0]?.at ?? now
443  const span = Math.max(1, now - first)
444  const cells = Array.from({ length: width }, () => '─')
445  for (const c of a.consults) {
446    const from = Math.min(width - 1, Math.floor(((c.at - first) / span) * (width - 1)))
447    const to = Math.min(width - 1, Math.floor((((c.endAt ?? now) - first) / span) * (width - 1)))
448    for (let i = from + 1; i <= to; i += 1) cells[i] = '━'
449    cells[from] = '◆'
450  }
451  return cells.join('')
452}
453
454export const receiptOf = (t: Turn, o: { durationMs: number; agentsSince: number; reason: string }): Receipt => ({
455  durationMs: o.durationMs,
456  agents: o.agentsSince,
457  edits: t.edits,
458  errors: t.errors,
459  reason: o.reason,
460})
461
462// ---------------------------------------------------------------- agents and loops
463
464type StepUsage = { input_tokens?: number; cache_read_input_tokens?: number; cache_creation_input_tokens?: number; output_tokens?: number } | null
465
466/**
467 * A card after one of its model requests: its context is the latest step's whole input; output adds up.
468 * Its model is first-wins (the spawn's, else the first step's) but its effort is latest-wins: the latest
469 * step that carried one, and a step without one keeps the previous.
470 */
471export const applyStep = (c: AgentCard, s: { model: string; effort?: string | number; usage: StepUsage; stopReason: string | null }): AgentCard => {
472  const u = s.usage ?? {}
473  const ctx = (u.input_tokens ?? 0) + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0)
474  return {
475    ...c,
476    model: c.model || s.model,
477    effort: s.effort === undefined ? c.effort : String(s.effort),
478    steps: c.steps + 1,
479    ctx: ctx > 0 ? ctx : c.ctx,
480    out: c.out + (u.output_tokens ?? 0),
481    lastStop: s.stopReason,
482  }
483}
484
485/**
486 * A card's kind line: its type, its model when that is not the main loop's (`mainModel`, as prettyModel
487 * spells it), and its effort when it has one, as the main box says it.
488 */
489export const cardKind = (c: AgentCard, mainModel: string) =>
490  [c.type, ...(!c.model || prettyModel(c.model) === mainModel ? [] : [prettyModel(c.model)]), ...(c.effort ? [c.effort] : [])].join(' · ')
491
492export const noteTool = (c: AgentCard, n: ToolNote): AgentCard => ({ ...c, tools: [...c.tools, n].slice(-3) })
493
494export const stepLoop = (loops: Loop[], id: string, at: number): Loop[] => {
495  const found = loops.find(l => l.id === id)
496  const next = found
497    ? loops.map(l => (l === found ? { ...l, steps: l.steps + 1, lastAt: at } : l))
498    : [...loops, { id, steps: 1, firstAt: at, lastAt: at, isDone: false }]
499  return next.slice(-60)
500}
501
502export const LOOP_ACTIVE_MS = 15_000
503export const isLoopActive = (l: Loop, now: number) => !l.isDone && now - l.lastAt < LOOP_ACTIVE_MS
504
505/** Swimlane geometry: each agent's bar on one shared axis from the first spawn to now. */
506export const lanes = (cards: AgentCard[], now: number, width: number) => {
507  const start = Math.min(...cards.map(c => c.spawnedAt).filter(n => n > 0), now)
508  const span = Math.max(1, now - start)
509  return cards.map(c => {
510    const from = Math.floor(((Math.max(c.spawnedAt, start) - start) / span) * width)
511    const to = Math.max(from + 1, Math.ceil((((c.endedAt ?? now) - start) / span) * width))
512    return { id: c.id, before: Math.min(from, width), bar: Math.min(to, width) - Math.min(from, width), after: width - Math.min(to, width) }
513  })
514}
515
516// ---------------------------------------------------------------- layout
517
518/** How many log lines fit: what the other panels leave, never fewer than 4 nor more than 8. */
519export const logRows = (bodyRows: number, used: number) => Math.max(4, Math.min(8, bodyRows - used - 3))
520
521/** Terminal cells a string takes: CJK, full-width characters and emoji (U+1F300 to U+1FAFF, the branch marker among them) take two. */
522export const cells = (s: string) =>
523  [...s].reduce((n, ch) => n + (/[\u1100-\u115f\u2e80-\ua4cf\uac00-\ud7a3\uf900-\ufaff\ufe30-\ufe4f\uff00-\uff60\uffe0-\uffe6\u{1f300}-\u{1faff}]/u.test(ch) ? 2 : 1), 0)
524
525/** Legend items that fit on one row of `width` cells, in order; the rest are dropped. */
526export const fitLegend = <T extends { label: string }>(items: T[], width: number) => {
527  const out: T[] = []
528  let used = 0
529  for (const it of items) {
530    const w = cells(it.label) + 4
531    if (used + w > width) break
532    out.push(it)
533    used += w
534  }
535  return out
536}
537
538/** A card's title row: the task in the agent's own words, the type only when there is none. */
539export const cardTitle = (c: AgentCard) => c.description || c.type
540
541/** A title split over two rows at a word boundary: `first` cells on row one, `rest` on row two. */
542export const titleLines = (title: string, first: number, rest: number): [string, string] => {
543  const t = title.replace(/\s+/g, ' ').trim()
544  if (t.length <= first) return [t, '']
545  const cut = t.lastIndexOf(' ', first)
546  const at = cut > 0 ? cut : first
547  return [t.slice(0, at).trim(), shorten(t.slice(at), rest)]
548}
549
550/**
551 * What a turn's opening text was, for the log: the person's words, or for a turn the engine
552 * opened with a tagged message (a subagent's hand-back, a task notification), that message's kind.
553 */
554export const promptLine = (text: string): { who: string; text: string } => {
555  const tag = /^\s*<([a-z][\w-]*)/i.exec(text)?.[1]
556  if (!tag) return { who: 'you', text: shorten(text, 70) }
557  const from = /\bfrom="([^"]+)"/.exec(text)?.[1]
558  return { who: 'engine', text: shorten(`${tag.replace(/[-_]/g, ' ')}${from ? ` from ${from.slice(0, 8)}` : ''}`, 70) }
559}
560
561/**
562 * A subagent's hand-back message: who sent it and the first line of what it said. The report
563 * follows a framing header in the message; without one, the first line after the opening tag.
564 */
565export const handbackOf = (text: string): { from: string; body: string } | null => {
566  const from = /^\s*<agent-message\s+from="([^"]+)"/.exec(text)?.[1]
567  if (!from) return null
568  const afterHeader = text.split(/The report follows:\s*\n/)[1]
569  const rest = afterHeader ?? text.replace(/^\s*<agent-message[^>]*>/, '')
570  const body =
571    rest
572      .split('\n')
573      .map(l => l.trim())
574      .find(l => l && !l.startsWith('[') && !l.startsWith('<') && !l.startsWith('</')) ?? ''
575  return body ? { from, body } : null
576}
577
578/** The advice line a pane shows for a report: its first real line, markdown markers stripped, cut to 160. */
579export const adviceLine = (report: string) => {
580  const first = report.split('\n').map(l => l.trim()).find(l => l && !l.startsWith('[') && !l.startsWith('<')) ?? ''
581  return shorten(first.replace(/\*\*|__/g, '').replace(/^[#>*\s-]+/, ''), 160)
582}
583
584export const elapsedOf = (c: AgentCard, now: number) => (c.endedAt ?? now) - c.spawnedAt
585
586// ---------------------------------------------------------------- Codex usage
587
588/**
589 * A usage window as the pane draws it: percent used, whether the reading is past its reset, and the
590 * time left to the reset. A window past its reset has restarted (the next request opens a new one),
591 * so it reads 0% used, still marked until a fresh reading arrives. Null when the window is unknown.
592 */
593export const windowView = (w: UsageWindow | undefined, now: number) => {
594  if (w === undefined || !Number.isFinite(w.usedPercent)) return null
595  const isStale = w.resetsAtMs !== undefined && w.resetsAtMs <= now
596  const pct = isStale ? 0 : Math.max(0, Math.min(100, Math.round(w.usedPercent)))
597  return { pct, isStale, resetIn: isStale ? '' : fmtResetIn(w.resetsAtMs ?? null, now) }
598}
599
600/** `5h 13%`, `~0%` past its reset, `—` unknown: the window in a few cells. */
601export const windowShort = (label: string, w: UsageWindow | undefined, now: number) => {
602  const v = windowView(w, now)
603  return `${label} ${v === null ? '—' : `${v.isStale ? '~' : ''}${v.pct}%`}`
604}
605
606const CLAUDE_WINDOWS = ['five_hour', 'seven_day']
607
608/** A rate-limit reading as a usage window, its reset kept when the API gave one. */
609export const limitWindow = (l: { pct: number; resetsAt?: number | null }): UsageWindow =>
610  l.resetsAt == null ? { usedPercent: l.pct } : { usedPercent: l.pct, resetsAtMs: l.resetsAt }
611
612/** Claude's 5-hour and 7-day windows in a few cells, `5h 19% · 7d 3%`; null before any reading. */
613export const claudeWindows = (limits: readonly { kind: string; pct: number; resetsAt?: number | null }[], now: number) => {
614  const shown = CLAUDE_WINDOWS.flatMap(kind => limits.filter(l => l.kind === kind).slice(0, 1))
615  return shown.length === 0 ? null : shown.map(l => windowShort(limitLabel(l.kind), limitWindow(l), now)).join(' · ')
616}
617
618// ---------------------------------------------------------------- usage rows
619
620/** The cells a round-bordered panel with one cell of padding on each side takes from its width. */
621export const PANEL_FRAME = 4
622const METER_MIN = 4
623const METER_MAX = 40
624/** The percent column, right-aligned after a space: `100%`, or `~100%` past its reset. */
625const METER_PCT_W = 5
626/** The detail column of a boxed row: `⏱  10d23h` (two spaces after the clock) or `stale`. */
627const METER_DETAIL_W = 9
628
629/** One usage row as the pane draws it: `pct` null is a window not read yet. */
630export type Meter = { label: string; pct: number | null; isStale: boolean; detail: string }
631
632/** A usage window as a row: the time to its reset, or `stale` once past it, at 0% used. */
633export const windowMeter = (label: string, w: UsageWindow | undefined, now: number): Meter => {
634  const v = windowView(w, now)
635  if (v === null) return { label, pct: null, isStale: false, detail: '' }
636  return { label, pct: v.pct, isStale: v.isStale, detail: v.isStale ? 'stale' : v.resetIn ? `⏱  ${v.resetIn}` : '' }
637}
638
639/** `  58%`, ` ~58%` past its reset: right-aligned in the percent column. */
640export const meterPercent = (m: Meter) => `${m.isStale ? '~' : ''}${m.pct ?? 0}%`.padStart(METER_PCT_W)
641
642/**
643 * The bar's cells for the rows of a panel `panelW` wide: the label column, a space, the bar, a space,
644 * the percent, a space, then the widest of the rows' details. One width for every row of the panel,
645 * so the bars start and end together whatever each row's detail is.
646 */
647export const meterWidth = (panelW: number, labelW: number, details: readonly string[]) =>
648  Math.min(METER_MAX, Math.max(METER_MIN, panelW - PANEL_FRAME - labelW - METER_PCT_W - 3 - Math.max(METER_DETAIL_W, ...details.map(cells))))
649
650/**
651 * The bar of a line drawn without a box, alone in its row (the context): whatever the label, the
652 * percent, its own detail and its note leave of `lineW`, with no upper cap. The detail has a space
653 * before it only when there is one; the note follows as written.
654 */
655export const lineMeterWidth = (lineW: number, labelW: number, detail: string, note = '') =>
656  Math.max(METER_MIN, lineW - labelW - METER_PCT_W - 2 - (detail ? 1 + cells(detail) : 0) - cells(note))
657
658/** The share of the pane the context line takes. */
659export const CTX_LINE_SHARE = 0.5
660
661/** `s` padded with spaces to `width` cells, a wide character counting two. */
662export const padCells = (s: string, width: number) => s + ' '.repeat(Math.max(0, width - cells(s)))
663
664// ---------------------------------------------------------------- branch
665
666/** How many unreviewed paths the unfolded list draws before counting the rest. */
667export const GIT_PATH_ROWS = 8
668
669/** `unreviewed 3 files`; `✓ unreviewed 0` when every change was seen; `unreviewed ?` when that cannot be known, never 0. */
670export const unreviewedLabel = (s: GitSnap) =>
671  s.unreviewed === undefined ? 'unreviewed ?' : s.unreviewed === 0 ? '✓ unreviewed 0' : `unreviewed ${plural(s.unreviewed, 'file')}`
672
673/** `changed 5 files`: files that differ from HEAD, untracked ones included. */
674export const changedLabel = (s: GitSnap) => `changed ${plural(s.changed, 'file')}`
675
676/** `↔ origin/main ↑2 ↓1`, or `no upstream`. */
677export const syncLabel = (s: GitSnap) => (s.upstream === undefined ? 'no upstream' : `↔ ${s.upstream} ↑${s.ahead ?? 0} ↓${s.behind ?? 0}`)
678
679/** The list of unreviewed paths is only worth unfolding when there are some. */
680export const canUnfold = (s: GitSnap) => (s.unreviewed ?? 0) > 0
681
682/**
683 * The paths to draw in `slots` rows, and how many unreviewed files are left out. Room for all of
684 * them draws every path; less room gives the last row to the count of those not drawn.
685 */
686export const pathLines = (s: GitSnap, slots: number): { paths: string[]; hidden: number } => {
687  const listed = s.unreviewedPaths
688  const total = s.unreviewed ?? 0
689  const more = total > listed.length ? 1 : 0
690  if (slots <= 0 || total === 0) return { paths: [], hidden: 0 }
691  if (slots >= listed.length + more) return { paths: listed, hidden: total - listed.length }
692  return { paths: listed.slice(0, slots - 1), hidden: total - (slots - 1) }
693}
694
695/** The rows the unfolded list takes: its paths and a closing count, within GIT_PATH_ROWS. */
696export const pathRows = (s: GitSnap, isOpen: boolean) => {
697  if (!isOpen) return 0
698  const { paths, hidden } = pathLines(s, GIT_PATH_ROWS)
699  return paths.length + (hidden > 0 ? 1 : 0)
700}
701
702// ---------------------------------------------------------------- Codex runs as cards
703
704/** A `codex exec` run the main session started, as a running card: no steps or tokens to tell. */
705export const codexCard = (id: string, call: { purpose: string; note?: string }, model: string, at: number, effort = ''): AgentCard => ({
706  ...normalizeCard({}),
707  id,
708  type: 'Codex',
709  model,
710  effort,
711  description: call.note ? `${call.purpose}: ${call.note}` : call.purpose,
712  spawnedAt: at,
713  codex: {},
714})
715
716/** A card ended: done when it went well, failed otherwise. A card already ended stays as it ended. */
717export const endCard = (c: AgentCard, ok: boolean, at: number): AgentCard =>
718  c.status !== 'running' ? c : { ...c, status: ok ? 'done' : 'failed', endedAt: at }
719
720/**
721 * The Codex runs a turn's stop settles: a foreground run still open was abandoned (failed); a
722 * background run the engine no longer lists has finished (done). Unknown lists settle nothing
723 * in the background. Answers the ids it ended, with how.
724 */
725export const stopCodex = (cards: AgentCard[], liveIds: readonly string[] | undefined): { id: string; ok: boolean }[] =>
726  cards.flatMap((c): { id: string; ok: boolean }[] => {
727    if (c.codex === undefined || c.status !== 'running') return []
728    if (c.codex.taskId === undefined) return [{ id: c.id, ok: false }]
729    return liveIds !== undefined && !liveIds.includes(c.codex.taskId) ? [{ id: c.id, ok: true }] : []
730  })
731
732/** The card a notification ends: by its background task id, else by the shell call that started it. */
733export const codexCardFor = (cards: AgentCard[], taskId: string, toolUseId: string | undefined) =>
734  cards.find(c => c.codex !== undefined && (c.codex.taskId === taskId || (toolUseId !== undefined && c.id === toolUseId)))
735
736// ---------------------------------------------------------------- panels side by side
737
738/** The narrowest pane that seats two panels in a row. */
739const PAIR_MIN_COLUMNS = 70
740
741/** The two panels' widths in a pane `w` wide, a cell between them, the left one taking the odd cell; null when too narrow. */
742export const pairWidths = (w: number): [number, number] | null =>
743  w < PAIR_MIN_COLUMNS ? null : [Math.ceil((w - 1) / 2), Math.floor((w - 1) / 2)]
744
745/** A row of the single column: one panel, or two side by side (the left one first). */
746export type PairRow = Panel | readonly [Panel, Panel]
747
748const PAIRS: readonly (readonly [Panel, Panel])[] = [
749  ['main', 'codex'],
750  ['git', 'gate'],
751]
752
753/**
754 * The single column's rows when panels may share one. The gate rises over the architect to sit
755 * beside the branch; a pair forms only for panels listed next to each other (in either order),
756 * so a person's own `panels` order is kept and a panel without its partner has a row to itself.
757 */
758export const pairRows = (panels: readonly Panel[]): PairRow[] => {
759  const g = panels.indexOf('git')
760  const k = panels.indexOf('gate')
761  const lifted =
762    g >= 0 && k > g && panels.slice(g + 1, k).every(p => p === 'architect')
763      ? [...panels.slice(0, g + 1), 'gate' as const, ...panels.slice(g + 1, k), ...panels.slice(k + 1)]
764      : [...panels]
765  const rows: PairRow[] = []
766  for (let i = 0; i < lifted.length; i++) {
767    const [a, b] = [lifted[i], lifted[i + 1]]
768    const pair = PAIRS.find(([x, y]) => (a === x && b === y) || (a === y && b === x))
769    if (pair) i++
770    rows.push(pair ?? (a as Panel))
771  }
772  return rows
773}
774
775/** A row of the single column: a panel, two side by side, or the context line under the main box. */
776export type ColumnRow = PairRow | 'ctx'
777
778/**
779 * The column's rows with the context line put in: right after the row that holds the main box, or
780 * after the Codex box when that is the next row. The context belongs to the main box, so a column
781 * without the main box has no context line.
782 */
783export const withCtx = (rows: readonly PairRow[]): ColumnRow[] => {
784  const main = rows.findIndex(r => r === 'main' || (typeof r !== 'string' && r.includes('main')))
785  if (main < 0) return [...rows]
786  const at = rows[main] === 'main' && rows[main + 1] === 'codex' ? main + 2 : main + 1
787  return [...rows.slice(0, at), 'ctx', ...rows.slice(at)]
788}
789
790/** The cells the effort bar and its space take ahead of the narrow effort line's effort. */
791const EFFORT_LEAD = 5
792
793/**
794 * The main box's effort line when two boxes share a row: `▮▮▮▯ high · auto · 12 req`. The bar is
795 * drawn by the caller; the mode is cut (or left out) so the line fits `inner` cells.
796 */
797export const narrowEffort = (effort: string, mode: string, steps: number, inner: number) => {
798  const label = effort || '—'
799  const count = ` · ${steps} req`
800  const room = inner - EFFORT_LEAD - cells(label) - cells(count) - 3
801  const shown = mode && room >= 2 ? shorten(mode, room) : ''
802  return { effort: label, tail: `${shown ? ` · ${shown}` : ''}${count}` }
803}
804
805/** The rows that items `widths` wide take in a wrapping row of `inner` cells with `gap` cells between them. */
806export const wrapRows = (widths: readonly number[], inner: number, gap: number) =>
807  widths.reduce(
808    (s, w) => (s.rows > 0 && s.used + gap + w <= inner ? { rows: s.rows, used: s.used + gap + w } : { rows: s.rows + 1, used: w }),
809    { rows: 0, used: 0 },
810  ).rows
811
hooks/lib/codex.ts 76 lines
1// Reading a `codex exec` call: what the shell command says it is for. The pane
2// shows each run as a subagent card. Plain text in, plain data out.
3
4import { argumentsEnd, commandAfter, maskShell } from './shell-mask'
5import type { Shell } from './shell-mask'
6
7/** What a shell command that runs `codex exec` tells about the run. */
8export type CodexCall = { purpose: string; note?: string }
9
10// `codex exec` counts only in command position (see ./shell-mask): in the code of
11// the command, with quoted strings, comments and heredoc bodies taken out, so
12// `echo note: codex exec`, `echo "codex exec"` and a script written by
13// `cat <<'EOF'` are not runs. Only the first 64 KB of a call is read (the masking
14// is one pass: 100 KB take about 20 ms); a string still open at that cut is masked on.
15const MAX_CHARS = 65_536
16// The flag is found in the code, where a quoted prompt cannot hold one; its value is read from the text as written.
17const SANDBOX_FLAG = /(?<![\w-])(?:--sandbox|-s)(?:=|[ \t])/
18const FLAG_VALUE = /^(?:[ \t]|\\\r?\n)*(?:"([^"]*)"|'([^']*)'|(\S+))/
19
20/** `codex exec` flags that take the next word as their value. */
21const VALUE_FLAGS = new Set([
22  '-m', '--model', '-s', '--sandbox', '-c', '--config', '-C', '--cd', '-p', '--profile',
23  '-o', '--output-last-message', '-i', '--image', '--color', '--output-schema',
24])
25
26const SANDBOX_PURPOSE: Readonly<Record<string, string>> = {
27  'read-only': 'Codex plan draft',
28  'workspace-write': 'Codex review',
29}
30const RESUME_PURPOSE = 'Codex resume'
31const PLAIN_PURPOSE = 'Codex'
32
33// The sandbox the run asks for: `--sandbox=X`, `--sandbox X` or `-s X`, the flag as the code has it, the value as written.
34const sandboxOf = (maskedArgs: string, args: string): string | undefined => {
35  const flag = SANDBOX_FLAG.exec(maskedArgs)
36  if (flag === null) return undefined
37
38  const value = FLAG_VALUE.exec(args.slice(flag.index + flag[0].length))
39
40  return value?.[1] ?? value?.[2] ?? value?.[3]
41}
42
43// The words after `resume` when it is the subcommand: the first word that is
44// not a flag (or a flag's value). A `resume` further on is part of a prompt.
45const resumeArgs = (args: string): string | undefined => {
46  const words = args.trim().split(/\s+/)
47  let i = 0
48
49  while (words[i]?.startsWith('-') === true) i += VALUE_FLAGS.has(words[i] ?? '') ? 2 : 1
50
51  return words[i] === 'resume' ? words.slice(i + 1).join(' ') : undefined
52}
53
54/**
55 * The call as Codex run, or undefined when the command does not run `codex exec`.
56 * The flags read are those of that command alone, up to the next `;`, `|` or `&`.
57 */
58export const describeCodex = (call: string, description?: string, shell: Shell = 'bash'): CodexCall | undefined => {
59  const command = call.slice(0, MAX_CHARS)
60  if (!/codex/i.test(command)) return undefined
61
62  const code = maskShell(command, shell, call.length > MAX_CHARS)
63  const execEnd = commandAfter(code)
64  if (execEnd < 0) return undefined
65
66  const argsEnd = argumentsEnd(code, execEnd)
67  const args = command.slice(execEnd, argsEnd)
68  const note = description === undefined || description === '' ? {} : { note: description }
69  const resume = resumeArgs(args)
70  if (resume !== undefined) return { purpose: RESUME_PURPOSE, ...note }
71
72  const sandbox = sandboxOf(code.slice(execEnd, argsEnd), args)
73
74  return { purpose: SANDBOX_PURPOSE[sandbox ?? ''] ?? PLAIN_PURPOSE, ...note }
75}
76
hooks/lib/codex-quota.ts 297 lines
1import type { CodexQuota, UsageWindow } from '../../types'
2import { CODEX_USAGE_URL, parseCodexAuth, parseCodexUsage, usageHeaders } from './codex-usage'
3
4// Codex's usage windows, model and effort, read from what Codex itself writes.
5// One `token_count` event in a Codex rollout carries
6//   "rate_limits":{"limit_id":"codex","primary":{"used_percent":13.0,
7//   "window_minutes":300,"resets_at":1791132451},"secondary":{...}|null,...}
8// Lines are huge (tool output rides along), so the figures are cut out with a
9// regex instead of parsing the whole line. Windows are told apart by their
10// length, not by position (which of primary/secondary is the weekly one is the
11// plan's choice), and a plan with a single window just has one.
12const WINDOW = /\{"used_percent":([\d.]+),"window_minutes":(\d+),"resets_at":(\d+|null)\}/g
13
14const FIVE_HOUR_MAX_MINUTES = 360
15const CODEX_MARK = '"limit_id":"codex"'
16
17const descending = (names: readonly string[]): string[] => [...names].sort().reverse()
18
19// Names are what Codex itself writes; anything else in these folders is not read.
20const YEAR = /^\d{4}$/
21const TWO_DIGITS = /^\d{2}$/
22const ROLLOUT = /^rollout-[\w.-]+\.jsonl$/
23
24/** A directory listing, as `$.fs.list` gives it; the caller hands it in so this stays free of `$`. */
25export type ListDir = (path: string) => Promise<readonly { name: string; kind: string; mtimeMs: number }[]>
26
27/** A file's own last-modified time, as `$.fs.stat` gives it; rejects when it cannot be read. */
28export type StatMtime = (path: string) => Promise<number>
29
30// A rollout is written to for as long as its session lives, so the one Codex is working in is the one
31// last modified, not the one started last (the folders and names say when it started). Looking for it
32// stays bounded: the newest ROLLOUT_DAYS_WALKED day folders (Codex makes a folder only for a day it
33// ran, so two weeks of use), and no further folder, month or year is listed once ROLLOUT_FILES_MAX
34// files are in hand (the day folder that crosses the mark is still taken whole).
35export const ROLLOUT_DAYS_WALKED = 14
36export const ROLLOUT_FILES_MAX = 200
37
38type Candidate = { path: string; mtimeMs: number }
39
40/**
41 * The rollout files under `root`/YYYY/MM/DD in the newest day folders, the most recently modified
42 * first (equal times: the later name, i.e. the later-started). Only plain files with the names Codex
43 * writes; a link, a folder or anything else is not listed. A folder whose listing fails counts as empty.
44 *
45 * A directory listing can show a file that is still held open for appending at an older time than it
46 * has (NTFS updates the directory entry late), so with `stat` each candidate's time is read from the
47 * file itself: one in-process call per candidate. Where that fails the listing's time stands.
48 */
49export const recentRollouts = async (list: ListDir, root: string, stat?: StatMtime): Promise<string[]> => {
50  const found: Candidate[] = []
51  let days = 0
52  const full = (): boolean => days >= ROLLOUT_DAYS_WALKED || found.length >= ROLLOUT_FILES_MAX
53  const entriesOf = (path: string) => list(path).catch(() => [])
54  const subdirs = async (path: string, shape: RegExp): Promise<string[]> =>
55    descending((await entriesOf(path)).filter(entry => entry.kind === 'dir' && shape.test(entry.name)).map(entry => entry.name))
56
57  walk: for (const year of await subdirs(root, YEAR)) {
58    if (full()) break
59    for (const month of await subdirs(`${root}/${year}`, TWO_DIGITS)) {
60      if (full()) break walk
61      for (const day of await subdirs(`${root}/${year}/${month}`, TWO_DIGITS)) {
62        if (full()) break walk
63
64        const dir = `${root}/${year}/${month}/${day}`
65        days += 1
66        for (const entry of await entriesOf(dir)) {
67          if (entry.kind === 'file' && ROLLOUT.test(entry.name)) found.push({ path: `${dir}/${entry.name}`, mtimeMs: entry.mtimeMs })
68        }
69      }
70    }
71  }
72
73  return byRecency(stat === undefined ? found : await Promise.all(found.map(file => withOwnTime(file, stat))))
74}
75
76const withOwnTime = async (file: Candidate, stat: StatMtime): Promise<Candidate> => {
77  try {
78    const mtimeMs = await stat(file.path)
79
80    return Number.isFinite(mtimeMs) ? { ...file, mtimeMs } : file
81  } catch {
82    return file
83  }
84}
85
86const byRecency = (files: readonly Candidate[]): string[] =>
87  [...files].sort((a, b) => b.mtimeMs - a.mtimeMs || (a.path < b.path ? 1 : a.path > b.path ? -1 : 0)).map(file => file.path)
88
89/** What the rules fall back to when no choice has been made. */
90export const DEFAULT_CODEX_MODEL = 'gpt-6-sol'
91
92type Raw = { usedPercent: number; minutes: number; resetsAtMs: number | undefined }
93
94const toWindow = ({ usedPercent, resetsAtMs }: Raw): UsageWindow =>
95  resetsAtMs === undefined ? { usedPercent } : { usedPercent, resetsAtMs }
96
97// A percent that is not a number (`1.2.3`) says nothing: that window is left out.
98const windowsOf = (line: string): Raw[] =>
99  [...line.slice(line.indexOf(CODEX_MARK)).matchAll(WINDOW)]
100    .slice(0, 2)
101    .map(match => ({
102      usedPercent: Number(match[1]),
103      minutes: Number(match[2]),
104      resetsAtMs: match[3] === 'null' ? undefined : Number(match[3]) * 1000,
105    }))
106    .filter(window => Number.isFinite(window.usedPercent))
107
108// The event's own time is the line's first key. A line without a parseable one is a reading of
109// unknown age: -Infinity, older than any reading that has a time.
110const EVENT_TIME = /^\{\s*"timestamp"\s*:\s*"([^"]+)"/
111const UNKNOWN_TIME = Number.NEGATIVE_INFINITY
112
113const eventTimeOf = (line: string): number => {
114  const stamp = EVENT_TIME.exec(line)?.[1]
115  const ms = stamp === undefined ? Number.NaN : Date.parse(stamp)
116
117  return Number.isNaN(ms) ? UNKNOWN_TIME : ms
118}
119
120/** A Codex rate-limit reading and the time of the event that carried it. */
121export type CodexReading = { atMs: number; limits: Pick<CodexQuota, 'fiveHour' | 'weekly'> }
122
123/** The newest Codex rate-limit reading in `text` (a rollout's tail), if any. */
124export const parseCodexReading = (text: string): CodexReading | undefined => {
125  const lines = text.split('\n')
126
127  for (let i = lines.length - 1; i >= 0; i -= 1) {
128    const line = lines[i] ?? ''
129    if (!line.includes(CODEX_MARK)) continue
130
131    const windows = windowsOf(line)
132    if (windows.length === 0) continue
133
134    const five = windows.find(w => w.minutes <= FIVE_HOUR_MAX_MINUTES)
135    const week = windows.find(w => w.minutes > FIVE_HOUR_MAX_MINUTES)
136
137    return {
138      atMs: eventTimeOf(line),
139      limits: {
140        fiveHour: five === undefined ? undefined : toWindow(five),
141        weekly: week === undefined ? undefined : toWindow(week),
142      },
143    }
144  }
145
146  return undefined
147}
148
149/** The newest Codex rate-limit windows in `text` (a rollout's tail), if any. */
150export const parseCodexLimits = (text: string): Pick<CodexQuota, 'fiveHour' | 'weekly'> | undefined =>
151  parseCodexReading(text)?.limits
152
153// The choice file is read by the rules and the slug ends up in `codex exec -m
154// <slug>`, so only what a real model name looks like is accepted. The config's
155// effort is drawn in cards and panels, so it has to look like a slug too.
156const SLUG = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/
157
158/** The reasoning effort `~/.codex/config.toml` selects; a value that is not slug-like (control characters, escapes, bidi marks) is no effort. */
159export const parseCodexEffort = (toml: string): string | undefined => {
160  const value = /^model_reasoning_effort\s*=\s*"([^"]+)"/m.exec(toml)?.[1]
161
162  return value !== undefined && SLUG.test(value) ? value : undefined
163}
164
165/** The model `~/.claude/model-choice.json` pins Codex calls to: its `codex` key. Read only. */
166export const parseChoice = (json: string): string | undefined => {
167  try {
168    const value: unknown = JSON.parse(json)
169    const choice = typeof value === 'object' && value !== null ? (value as { codex?: unknown }).codex : undefined
170
171    return typeof choice === 'string' && SLUG.test(choice) ? choice : undefined
172  } catch {
173    return undefined
174  }
175}
176
177const ROLLOUT_TAIL_LINES = 2000
178// Rollouts are queried ROLLOUTS_PER_ROUND at a time, the most recently modified first; a second round
179// runs only when the first found no codex reading (a rollout of an exec that never reached a token_count),
180// and nothing beyond ROLLOUTS_TRIED is read. So a refresh runs 2 PowerShell queries, at most 4.
181const ROLLOUTS_PER_ROUND = 2
182export const ROLLOUTS_TRIED = 4
183const CHOICE_FILE = '.claude/model-choice.json'
184
185// The newest codex reading in a rollout's tail. The path rides in the
186// environment, never in the command text, and only the matching lines come
187// back, so a big rollout cannot push the figures out of the output limit.
188export const ROLLOUT_QUERY =
189  `Get-Content -LiteralPath $env:ROLLOUT -Tail ${ROLLOUT_TAIL_LINES} | ` +
190  `Select-String -SimpleMatch '${CODEX_MARK}' | Select-Object -Last 5 | ForEach-Object { $_.Line }`
191
192/** The reads Codex's figures need; the caller hands them in so this stays free of `$`. */
193export type CodexIo = {
194  /** A file's text; empty when it cannot be read. */
195  readText: (path: string) => Promise<string>
196  list: ListDir
197  /** A file's own modified time (it rejects when it cannot be read); the listing's time can be stale for an open file. */
198  stat: StatMtime
199  /** What the rollout query prints for the rollout at `path`; empty when it fails. */
200  queryRollout: (path: string) => Promise<string>
201  /** A GET of `url` with `headers`; it may reject. Used only when the server is asked (see readCodexQuota). */
202  fetch?: (url: string, headers: Record<string, string>) => Promise<{ ok: boolean; text: string }>
203}
204
205// The account's windows as the ChatGPT backend reports them, signed with the ChatGPT login Codex
206// keeps in auth.json (see ./codex-usage). The token is used for this one request and goes nowhere
207// else. No login, an expired token (Codex refreshes it when it next runs; this never writes it), a
208// refusal or an answer of another shape: undefined, and the rollouts are read instead.
209const serverWindows = async (root: string, io: CodexIo): Promise<Pick<CodexQuota, 'fiveHour' | 'weekly'> | undefined> => {
210  if (io.fetch === undefined) return undefined
211  const auth = parseCodexAuth(await io.readText(`${root}/auth.json`))
212  if (auth === undefined) return undefined
213  try {
214    const answer = await io.fetch(CODEX_USAGE_URL, usageHeaders(auth))
215
216    return answer.ok ? parseCodexUsage(answer.text) : undefined
217  } catch {
218    return undefined
219  }
220}
221
222/**
223 * Codex's model is the one the choice file pins its calls to (the rules' default
224 * without one) and its reasoning effort comes from Codex's config: both are
225 * only displayed. Its 5-hour and weekly windows come, when `askServer` is set (the person pressed
226 * refresh) and the login allows, from the ChatGPT backend (current even when Codex has not run here,
227 * dated `nowMs`); otherwise, or when that fails, from the newest
228 * token_count event in a rollout, the only place the CLI records them. Of the most recently modified
229 * rollouts the reading with the latest event time is shown, not the one from the rollout started last.
230 */
231export const readCodexQuota = async (
232  home: string,
233  io: CodexIo,
234  options: { askServer?: boolean; nowMs?: number } = {},
235): Promise<CodexQuota> => {
236  const root = `${home}/.codex`
237  const model = parseChoice(await io.readText(`${home}/${CHOICE_FILE}`)) ?? DEFAULT_CODEX_MODEL
238  const effort = parseCodexEffort(await io.readText(`${root}/config.toml`))
239  const base = { model, ...(effort === undefined ? {} : { effort }) }
240
241  const server = options.askServer === true ? await serverWindows(root, io) : undefined
242  if (server !== undefined) {
243    const at = options.nowMs
244    return { ...base, ...server, ...(at === undefined ? {} : { readAtMs: at, serverAtMs: at }) }
245  }
246
247  const paths = (await recentRollouts(io.list, `${root}/sessions`, io.stat).catch(() => [])).slice(0, ROLLOUTS_TRIED)
248  for (let from = 0; from < paths.length; from += ROLLOUTS_PER_ROUND) {
249    const latest = latestOf(await Promise.all(paths.slice(from, from + ROLLOUTS_PER_ROUND).map(path => readingOf(io, path))))
250    if (latest !== undefined) return { ...base, ...latest.limits, ...(Number.isFinite(latest.atMs) ? { readAtMs: latest.atMs } : {}) }
251  }
252
253  return { ...base, note: 'no usage recorded yet' }
254}
255
256// A query that fails is a rollout with no reading: it must not take the others' readings with it.
257const readingOf = async (io: CodexIo, path: string): Promise<CodexReading | undefined> => {
258  try {
259    return parseCodexReading(await io.queryRollout(path))
260  } catch {
261    return undefined
262  }
263}
264
265// The latest event wins. On a tie the earlier one in `readings` stays: they come most recently
266// modified first, so that is the rollout modified last.
267const latestOf = (readings: readonly (CodexReading | undefined)[]): CodexReading | undefined =>
268  readings.reduce<CodexReading | undefined>(
269    (best, reading) => (reading !== undefined && (best === undefined || reading.atMs > best.atMs) ? reading : best),
270    undefined,
271  )
272
273/**
274 * The panel's figures after a new reading: the windows of whichever reading is newer (a reading of
275 * unknown time is the oldest), a window the newer one lacks kept from the other. So a server reading
276 * the person asked for is not undone by an older rollout at the next poll, and Codex used here later
277 * shows through. The model and effort are the new reading's; the time of the last server answer stays.
278 */
279export const mergeCodex = (old: CodexQuota | null, fresh: CodexQuota): CodexQuota => {
280  if (old === null) return fresh
281  const isNewer = (fresh.readAtMs ?? Number.NEGATIVE_INFINITY) >= (old.readAtMs ?? Number.NEGATIVE_INFINITY)
282  const [first, other] = isNewer ? [fresh, old] : [old, fresh]
283  const fiveHour = first.fiveHour ?? other.fiveHour
284  const weekly = first.weekly ?? other.weekly
285  const readAtMs = isNewer ? fresh.readAtMs : old.readAtMs
286  const serverAtMs = fresh.serverAtMs ?? old.serverAtMs
287  const { note, readAtMs: _r, serverAtMs: _s, fiveHour: _f, weekly: _w, ...rest } = fresh
288  return {
289    ...rest,
290    ...(fiveHour === undefined ? {} : { fiveHour }),
291    ...(weekly === undefined ? {} : { weekly }),
292    ...(fiveHour === undefined && weekly === undefined && note !== undefined ? { note } : {}),
293    ...(readAtMs === undefined ? {} : { readAtMs }),
294    ...(serverAtMs === undefined ? {} : { serverAtMs }),
295  }
296}
297
hooks/lib/fable.ts 59 lines
1import type { UsageWindow } from '../../types'
2
3// The Fable weekly window: not in the session's rate limits, so it is asked of
4// /api/oauth/usage. Plain data in and out; the hooks do the calling.
5
6const clampPercent = (value: number): number => Math.min(100, Math.max(0, value))
7
8const windowOf = (usedPercent: number, resetsAtMs: number): UsageWindow =>
9  Number.isNaN(resetsAtMs) ? { usedPercent } : { usedPercent, resetsAtMs }
10
11// Per-model weekly windows (Fable) are not in the session's rate limits. The
12// server reports them in /api/oauth/usage as `limits[]` entries:
13//   {"kind":"weekly_scoped","scope":{"model":{"display_name":"Fable"}},
14//    "percent":12,"resets_at":"2026-10-11T08:00:00+00:00"}
15const isRecord = (value: unknown): value is Record<string, unknown> =>
16  typeof value === 'object' && value !== null && !Array.isArray(value)
17
18const isFableLimit = (limit: Record<string, unknown>): boolean => {
19  const model = isRecord(limit.scope) && isRecord(limit.scope.model) ? limit.scope.model : {}
20
21  return (
22    limit.kind === 'weekly_scoped' &&
23    /fable/i.test(String(model.display_name)) &&
24    typeof limit.percent === 'number' &&
25    Number.isFinite(limit.percent)
26  )
27}
28
29/** The Fable weekly window out of an /api/oauth/usage body, if the account has one. */
30export const fableFromUsage = (json: string): UsageWindow | undefined => {
31  try {
32    const data: unknown = JSON.parse(json)
33    const limits = isRecord(data) && Array.isArray(data.limits) ? data.limits : []
34    const hit = limits.filter(isRecord).find(isFableLimit)
35    if (hit === undefined || typeof hit.percent !== 'number') return undefined
36
37    return windowOf(clampPercent(hit.percent), typeof hit.resets_at === 'string' ? Date.parse(hit.resets_at) : NaN)
38  } catch {
39    return undefined
40  }
41}
42
43export const FABLE_POLL_MS = 90_000
44const FABLE_BACKOFF_MAX_MS = 600_000
45
46/**
47 * How long to wait before asking again: the usual interval after an answer,
48 * and after a failure double the last wait up to ten minutes, or as long as
49 * the server asked for in `retry-after` (seconds) when that is longer.
50 */
51export const nextFableDelay = (previousMs: number, isOk: boolean, retryAfter: string | undefined): number => {
52  if (isOk) return FABLE_POLL_MS
53
54  const asked = Number(retryAfter) * 1000
55  const backoff = Math.min(FABLE_BACKOFF_MAX_MS, Math.max(FABLE_POLL_MS, previousMs) * 2)
56
57  return Number.isFinite(asked) && asked > 0 ? Math.min(FABLE_BACKOFF_MAX_MS, Math.max(asked, backoff)) : backoff
58}
59
hooks/lib/notify.ts 53 lines
1// Reading a background task's notification and a shell result's hand-off to
2// the background. Plain text and plain data in and out. Only which task ended
3// and how is read: nothing a notification names is opened.
4
5/** One finished background task, as its notification reports it. */
6export type Notification = { id: string; toolUseId?: string; ok: boolean }
7
8const BLOCK = /<task-notification>([\s\S]*?)<\/task-notification>/g
9const FAILED_STATUSES = new Set(['failed', 'killed', 'error'])
10
11const tag = (block: string, name: string): string | undefined =>
12  new RegExp(`<${name}>([^<]*)</${name}>`).exec(block)?.[1]?.trim() || undefined
13
14const SUMMARY = '<summary>'
15
16const parseBlock = (block: string): Notification | undefined => {
17  // The task's own tags come before the summary. What follows is text a task wrote (a summary, a
18  // result) and may hold tags of its own: none of it is the task's.
19  const at = block.indexOf(SUMMARY)
20  const head = at < 0 ? block : block.slice(0, at)
21  const id = tag(head, 'task-id')
22  if (id === undefined) return undefined
23
24  const toolUseId = tag(head, 'tool-use-id')
25  // The exit code is the summary's own; a result may quote any number.
26  const code = /\(exit code (\d+)\)/.exec(at < 0 ? '' : (tag(block.slice(at), 'summary') ?? ''))?.[1]
27  const failed = FAILED_STATUSES.has(tag(head, 'status') ?? '') || (code !== undefined && Number(code) !== 0)
28
29  return { id, ...(toolUseId === undefined ? {} : { toolUseId }), ok: !failed }
30}
31
32/** The notifications in a text; one text may merge several. A task is reported once, by the first block that names it. */
33export const parseNotifications = (text: string): Notification[] => {
34  const seen = new Set<string>()
35  const found: Notification[] = []
36
37  for (const match of text.matchAll(BLOCK)) {
38    const note = parseBlock(match[1] ?? '')
39    if (note === undefined || seen.has(note.id)) continue
40    seen.add(note.id)
41    found.push(note)
42  }
43
44  return found
45}
46
47/** The background task id of a shell result that moved to the background. */
48export const backgroundIdOf = (result: unknown): string | undefined => {
49  const id = typeof result === 'object' && result !== null ? (result as { backgroundTaskId?: unknown }).backgroundTaskId : undefined
50
51  return typeof id === 'string' && id !== '' ? id : undefined
52}
53
hooks/lib/serial.ts 16 lines
1/**
2 * A queue that runs the jobs it is given one after another, each starting when
3 * the one before has finished, whether that one succeeded or not. Every job
4 * gets its own result (or its own rejection).
5 */
6export const serial = (): (<T>(job: () => Promise<T>) => Promise<T>) => {
7  let tail: Promise<unknown> = Promise.resolve()
8
9  return job => {
10    const run = tail.then(job)
11    tail = run.catch(() => undefined)
12
13    return run
14  }
15}
16
hooks/lib/sessions.ts 90 lines
1import type { LiveSession } from '../../types'
2
3// Every live Claude Code process keeps a registry file, ~/.claude/sessions/<pid>.json:
4//   {"pid":31600,"sessionId":"…","name":"jev-router-41","entrypoint":"cli",…}
5// A crashed process can leave its file behind, so a row counts only while its
6// pid is still running as a Claude Code process (pids are reused). Only what
7// the wait banner needs is read: which session, which process, what to call it.
8
9export type SessionFile = { pid: number; sessionId: string; name: string }
10
11const MAX_NAME = 80
12
13export const parseSessionFile = (json: string): SessionFile | undefined => {
14  try {
15    const value: Record<string, unknown> | null = JSON.parse(json)
16    const { pid, sessionId, name } = value ?? {}
17
18    // The pid names a file in the marks folder, so it must be an ordinary process number.
19    if (typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0 || typeof sessionId !== 'string') return undefined
20
21    return { pid, sessionId, name: typeof name === 'string' ? name.slice(0, MAX_NAME) : sessionId.slice(0, 8) }
22  } catch {
23    return undefined
24  }
25}
26
27/** The registry files that parse; the rest are skipped. */
28export const parseSessionFiles = (texts: readonly string[]): SessionFile[] =>
29  texts.flatMap(text => {
30    const file = parseSessionFile(text)
31
32    return file === undefined ? [] : [file]
33  })
34
35/** The running processes in `tasklist /FO CSV /NH` output, pid to image name: `"claude.exe","31600",…`. */
36export const parseTasklist = (csv: string): Map<number, string> =>
37  new Map(
38    csv.split('\n').flatMap(line => {
39      const match = /^"([^"]*)","(\d+)"/.exec(line.trim())
40
41      return match === null ? [] : [[Number(match[2]), match[1] ?? ''] as [number, string]]
42    }),
43  )
44
45const CLAUDE_IMAGE = /^(claude|node)(\.exe)?$/i
46
47/** The live sessions: those whose pid still runs as Claude Code. A session id appears once. */
48export const liveSessions = (
49  files: readonly SessionFile[],
50  processes: ReadonlyMap<number, string>,
51  hereId: string,
52): LiveSession[] => {
53  const isAlive = (file: SessionFile): boolean => CLAUDE_IMAGE.test(processes.get(file.pid) ?? '')
54  const rows = files
55    .filter(isAlive)
56    .map<LiveSession>(file => ({ id: file.sessionId, pid: file.pid, name: file.name, isHere: file.sessionId === hereId }))
57
58  return [...new Map(rows.map(row => [row.id, row])).values()]
59}
60
61const REGISTRY_DIR = '.claude/sessions'
62
63/** The reads the registry needs; the caller hands them in so this stays free of `$`. */
64export type SessionIo = {
65  list: (dir: string) => Promise<readonly { name: string; size: number; mtimeMs: number; kind: string; isLink: boolean }[]>
66  /** A file's text; empty when it cannot be read. */
67  read: (path: string) => Promise<string>
68  /** The process table, as `tasklist /FO CSV /NH` prints it. */
69  tasklist: () => Promise<{ exitCode: number; stdout: string }>
70}
71
72/** At most this many registry files are read, the newest; and none over this many bytes (a real one is a few hundred). */
73const MAX_FILES = 32
74const MAX_FILE_BYTES = 65_536
75
76/** The live sessions out of the registry and the process table; a table that fails rejects. */
77export const readLiveSessions = async (home: string, hereId: string, io: SessionIo): Promise<LiveSession[]> => {
78  const dir = `${home}/${REGISTRY_DIR}`
79  const names = (await io.list(dir))
80    .filter(entry => entry.kind === 'file' && !entry.isLink && /^\d+\.json$/.test(entry.name) && entry.size <= MAX_FILE_BYTES)
81    .sort((a, b) => b.mtimeMs - a.mtimeMs)
82    .slice(0, MAX_FILES)
83    .map(entry => entry.name)
84  const files = parseSessionFiles(await Promise.all(names.map(name => io.read(`${dir}/${name}`))))
85  const table = await io.tasklist()
86  if (table.exitCode !== 0) throw new Error(`tasklist exited ${table.exitCode}`)
87
88  return liveSessions(files, parseTasklist(table.stdout), hereId)
89}
90
hooks/lib/waiting.ts 79 lines
1import type { LiveSession, WaitReason, WaitingRow } from '../../types'
2import { ago, plain } from './plain'
3
4// Each session keeps one small mark file, named by its pid, saying whether it
5// is waiting on the person right now and since when. The session id inside it
6// tells a live session's mark from one a reused pid left behind. The format is
7// the one status-sidebar writes and reads, so sessions running either see each other.
8
9const MARK_DIR = '.claude/mods-state/status-sidebar/waiting'
10
11/** The mark file of the session running as `pid`. */
12export const markPath = (home: string, pid: number): string => `${home}/${MARK_DIR}/${pid}.json`
13
14export const markText = (sessionId: string, reason: WaitReason | null, sinceMs: number): string =>
15  JSON.stringify({ sessionId, reason, sinceMs })
16
17const REASONS: readonly string[] = ['permission', 'question']
18
19/** The wait a mark records for `sessionId`; undefined when cleared, broken or another session's. */
20export const parseMark = (text: string, sessionId: string): { reason: WaitReason; sinceMs: number } | undefined => {
21  try {
22    const value: Record<string, unknown> | null = JSON.parse(text)
23    const { reason, sinceMs } = value ?? {}
24
25    if (value?.sessionId !== sessionId || typeof reason !== 'string' || !REASONS.includes(reason)) return undefined
26
27    return { reason: reason as WaitReason, sinceMs: typeof sinceMs === 'number' ? sinceMs : 0 }
28  } catch {
29    return undefined
30  }
31}
32
33/** The other live sessions waiting on the person, the longest wait first. */
34export const waitingRows = (rows: readonly LiveSession[], markOf: (pid: number) => string): WaitingRow[] =>
35  rows
36    .filter(row => !row.isHere)
37    .flatMap(row => {
38      const mark = parseMark(markOf(row.pid), row.id)
39
40      return mark === undefined ? [] : [{ id: row.id, name: row.name, ...mark }]
41    })
42    .sort((a, b) => a.sinceMs - b.sinceMs)
43
44/** The other live sessions waiting on the person: each one's mark file is read (`readFile`; empty when it cannot be). */
45export const readWaits = async (
46  home: string,
47  rows: readonly LiveSession[],
48  readFile: (path: string) => Promise<string>,
49): Promise<WaitingRow[]> => {
50  const marks = new Map(
51    await Promise.all(rows.filter(row => !row.isHere).map(async row => [row.pid, await readFile(markPath(home, row.pid))] as const)),
52  )
53
54  return waitingRows(rows, pid => marks.get(pid) ?? '')
55}
56
57/** True when two reads found the same waits, in the same order. */
58export const sameWaits = (a: readonly WaitingRow[], b: readonly WaitingRow[]): boolean =>
59  JSON.stringify(a) === JSON.stringify(b)
60
61/** A tool that stops to ask the person: a question, or a plan to approve. */
62export const isQuestionTool = (tool: string): boolean => tool === 'AskUserQuestion' || tool === 'ExitPlanMode'
63
64const WAIT_LABEL: Record<WaitReason, string> = { permission: 'permission', question: 'question' }
65
66/**
67 * The banner line: the session that has waited longest, why, and since when,
68 * with how many more wait. Undefined while nobody does. Permission waits are
69 * known to start and not to end until their call returns, hence "waiting on you".
70 */
71export const bannerText = (rows: readonly WaitingRow[], nowMs: number): string | undefined => {
72  const [first] = rows
73  if (first === undefined) return undefined
74
75  const more = rows.length > 1 ? ` · +${rows.length - 1} more` : ''
76
77  return `! waiting on you: ${plain(first.name)} ${WAIT_LABEL[first.reason]} ${ago(first.sinceMs, nowMs)}${more}`
78}
79
hooks/lib/git.ts 159 lines
1import type { GitSnap } from '../../types'
2import { plain } from './plain'
3
4/** How many unreviewed paths the branch panel can unfold. */
5export const UNREVIEWED_SHOWN = 20
6
7// The work tree as `git status --porcelain=v2 --branch -z -uall` reports it:
8// NUL-separated records, `# branch.*` headers first, a rename (`2`) followed
9// by one more record holding its old path. Paths are relative to the root.
10
11/**
12 * One changed path. A symbolic link is not hashed (`hash-object` would follow
13 * it to whatever it points at): `isLink` when status says the work tree holds
14 * one. Status does not say that of an untracked path, so `isUntracked` tells
15 * the caller which to look at itself.
16 */
17export type GitEntry = { path: string; isMissing: boolean; isUntracked?: true; isLink?: true }
18
19/** Stands for the content of a changed path that was not hashed. A review never stores it, so the path stays unreviewed. */
20export const UNHASHED = 'unhashed'
21
22export type GitStatus = { branch: string; upstream?: string; ahead?: number; behind?: number; entries: GitEntry[] }
23
24// How many space-separated fields come before the path, per record kind.
25const PATH_AFTER: Record<string, number> = { '1': 8, '2': 9, u: 10, '?': 1 }
26
27const pathOf = (record: string, fields: number): string => record.split(' ').slice(fields).join(' ')
28
29// Gone from the work tree: deleted there, or deleted from the index and not put back.
30const isMissing = (xy: string): boolean => xy[1] === 'D' || (xy[0] === 'D' && xy[1] === '.')
31
32// The work-tree mode sits after the head and index modes: `1`/`2` records read `<kind> <XY> <sub> <mH> <mI> <mW> …`,
33// an unmerged one `u <XY> <sub> <m1> <m2> <m3> <mW> …`. 120000 is a symbolic link.
34const MODE_AT: Record<string, number> = { '1': 5, '2': 5, u: 6 }
35const SYMLINK = '120000'
36
37const linkFlag = (kind: string, fields: readonly string[]): { isLink?: true } =>
38  fields[MODE_AT[kind] ?? -1] === SYMLINK ? { isLink: true } : {}
39
40export const parseGitStatus = (output: string): GitStatus | undefined => {
41  const records = output.split('\0')
42  let branch: string | undefined
43  let upstream: string | undefined
44  let ahead: number | undefined
45  let behind: number | undefined
46  const entries: GitEntry[] = []
47
48  for (let i = 0; i < records.length; i++) {
49    const record = records[i] ?? ''
50    const kind = record.split(' ', 1)[0] ?? ''
51
52    if (record.startsWith('# branch.head ')) branch = record.slice('# branch.head '.length)
53    if (record.startsWith('# branch.upstream ')) upstream = record.slice('# branch.upstream '.length)
54    const ab = /^# branch\.ab \+(\d+) -(\d+)$/.exec(record)
55    if (ab !== null) {
56      ahead = Number(ab[1])
57      behind = Number(ab[2])
58    }
59
60    const fields = PATH_AFTER[kind]
61    if (fields === undefined) continue
62    const xy = kind === '?' ? '??' : (record.split(' ')[1] ?? '')
63    // A conflicted file is on disk in some form unless both sides deleted it.
64    entries.push({
65      path: pathOf(record, fields),
66      isMissing: kind === 'u' ? xy === 'DD' : isMissing(xy),
67      ...(kind === '?' ? { isUntracked: true as const } : linkFlag(kind, record.split(' '))),
68    })
69    if (kind === '2') i++
70  }
71
72  return branch === undefined ? undefined : { branch, upstream, ahead, behind, entries }
73}
74
75/** The paths `git hash-object --stdin-paths` is given: the files still on disk (not a deleted path, not a link). */
76export const hashTargets = (entries: readonly GitEntry[]): string[] =>
77  entries.filter(entry => !entry.isMissing && entry.isLink !== true).map(entry => entry.path)
78
79/**
80 * Each changed path with its content hash (`deleted` for a missing one,
81 * `unhashed` for a link), from the hash-object output for
82 * `hashTargets(entries)`; undefined when the two do not line up.
83 */
84export const withHashes = (entries: readonly GitEntry[], output: string): Record<string, string> | undefined => {
85  const hashes = output.split('\n').map(line => line.trim()).filter(line => line !== '')
86  if (hashes.length !== hashTargets(entries).length) return undefined
87
88  let next = 0
89
90  const valueOf = (entry: GitEntry): string => {
91    if (entry.isMissing) return 'deleted'
92
93    return entry.isLink === true ? UNHASHED : (hashes[next++] ?? '')
94  }
95
96  return Object.fromEntries(entries.map(entry => [entry.path, valueOf(entry)]))
97}
98
99/** What a review stores of the hashes it saw: not the paths that were not hashed, which must stay unreviewed. */
100export const storable = (hashes: Record<string, string>): Record<string, string> =>
101  Object.fromEntries(Object.entries(hashes).filter(([, hash]) => hash !== UNHASHED))
102
103/** A stored path-to-hash map as read back from the store; anything else is no record. */
104export const asHashes = (value: unknown): Record<string, string> =>
105  value === null || typeof value !== 'object' || Array.isArray(value)
106    ? {}
107    : Object.fromEntries(Object.entries(value).filter((pair): pair is [string, string] => typeof pair[1] === 'string'))
108
109/** Changed files whose content the last review did not see as it is now. */
110export const unreviewedPaths = (current: Record<string, string>, reviewed: Record<string, string>): string[] =>
111  Object.entries(current)
112    .filter(([path, hash]) => reviewed[path] !== hash)
113    .map(([path]) => path)
114
115const REVIEW = /review/i
116
117const field = (input: unknown, key: string): string => {
118  const value = (input as Record<string, unknown> | null)?.[key]
119
120  return typeof value === 'string' ? value : ''
121}
122
123/** A tool call that starts a review: a reviewing skill, or a reviewer subagent. */
124export const isReviewCall = (tool: string, input: unknown): boolean =>
125  (tool === 'Skill' && REVIEW.test(field(input, 'skill'))) ||
126  (tool === 'Agent' && REVIEW.test(field(input, 'subagent_type')))
127
128/** A typed slash command that starts a review (`/code-review high`). */
129export const isReviewPrompt = (text: string): boolean => /^\/[\w:-]*review(?=\s|$)/i.test(text.trim())
130
131/** Only a person's own prompt can be a typed review: a notification or a peer message is not one. */
132export const isPersonPrompt = (originKind: string): boolean =>
133  originKind === 'composer' || originKind === 'bridge' || originKind === 'sdk'
134
135/**
136 * The branch panel's figures from a status, its hashes and what the last review
137 * saw. When either the hashes or the review record could not be had, how many
138 * files are unreviewed is unknown (absent), never zero.
139 */
140export const snapOf = (
141  status: GitStatus,
142  hashes: Record<string, string> | undefined,
143  reviewed: Record<string, string> | undefined,
144): GitSnap => {
145  // What the repository names (branch, upstream, paths) is drawn as text, so it is made plain first.
146  const base = {
147    branch: plain(status.branch),
148    upstream: status.upstream === undefined ? undefined : plain(status.upstream),
149    ahead: status.ahead,
150    behind: status.behind,
151    changed: status.entries.length,
152  }
153  if (hashes === undefined || reviewed === undefined) return { ...base, unreviewedPaths: [] }
154
155  const unseen = unreviewedPaths(hashes, reviewed)
156
157  return { ...base, unreviewed: unseen.length, unreviewedPaths: unseen.slice(0, UNREVIEWED_SHOWN).map(plain) }
158}
159
hooks/lib/git-tree.ts 155 lines
1import { hashTargets, parseGitStatus, withHashes } from './git'
2import type { GitEntry, GitStatus } from './git'
3import { CONFIG_QUERY, filterDrivers, filterOverrides } from './git-filters'
4
5// Reading the session's work tree through git. Nothing here reaches `$`: the
6// caller hands in a runner, so the exact commands are spelled in one place and
7// a test can see every one of them. Only read-only git runs: `rev-parse`,
8// `config` (to list), `status` and `hash-object` without `-w`;
9// `--no-optional-locks` keeps even `status` from refreshing the index, so a poll
10// never writes into the repository or takes a lock a person's own git command
11// could trip over.
12//
13// The repository's own configuration is not trusted either, since a poll would
14// otherwise run whatever a hostile one names:
15//   - `core.fsmonitor=false` stops `status` from running a hook program;
16//   - `protocol.ext.allow=never` refuses the `ext::` transport, which runs a command;
17//   - `GIT_NO_LAZY_FETCH=1` keeps a partial clone from contacting its promisor remote
18//     (git 2.44 and later; older git ignores it, so a repository that is a partial
19//     clone at all is not read, see ./git-filters);
20//   - `status --ignore-submodules=all` leaves submodules (and their filters) out;
21//   - each `filter.<driver>` is listed first and switched off for `status` and
22//     `hash-object` (see ./git-filters), and `hash-object --no-filters` skips filters anyway.
23// Review time and poll time hash the same way, so their hashes still compare.
24// If the filters cannot be listed, nothing more is read. Output the engine cut
25// short (it keeps the first 4 MiB) is never taken for a whole answer: a listing
26// that was cut could hide a driver, and a cut status would give partial counts.
27
28export type GitRun = (
29  argv: readonly string[],
30  init?: { cwd?: string; stdin?: string; env?: Record<string, string> },
31) => Promise<{ exitCode: number; stdout: string; isStdoutTruncated?: boolean }>
32
33/** What is known of a path (absolute): whether it is itself a symbolic link, and where it really lands (absent: nowhere). */
34export type Inspect = (path: string) => Promise<{ isLink: boolean; realPath?: string }>
35
36/** The repository root, what differs from HEAD and each changed file's content hash. */
37export type Tree = {
38  root: string
39  status: GitStatus
40  /** Undefined when the hashes could not be had: nothing is then counted as unreviewed. */
41  hashes?: Record<string, string>
42}
43
44export const GIT_TIMEOUT_MS = 15_000
45
46/** Set on every git call. */
47export const GIT_ENV: Readonly<Record<string, string>> = { GIT_NO_LAZY_FETCH: '1' }
48
49/** More untracked files than this are not each looked at for being a link: the hashes are then unknown. */
50export const MAX_UNTRACKED = 500
51
52const GIT = [
53  'git',
54  '--no-optional-locks',
55  '-c',
56  'core.quotePath=false',
57  '-c',
58  'core.fsmonitor=false',
59  '-c',
60  'protocol.ext.allow=never',
61] as const
62
63/** What the last review saw is kept per repository root. */
64export const reviewedKey = (root: string): string => `git-reviewed:${root}`
65
66const call = (run: GitRun, argv: readonly string[], init: { cwd?: string; stdin?: string } = {}) =>
67  run(argv, { ...init, env: { ...GIT_ENV } })
68
69// The `-c` options that switch off every configured filter driver. `git config --get-regexp`
70// exits 1 and prints nothing when nothing matches: that is no filters, not a failure. Any
71// other failure, a listing the engine cut short, or a name that cannot be passed safely
72// (or a partial clone) is undefined: read nothing further.
73const filterArgs = async (run: GitRun, root: string): Promise<string[] | undefined> => {
74  const listed = await call(run, [...GIT, 'config', '-z', '--name-only', '--get-regexp', CONFIG_QUERY], { cwd: root })
75  if (listed.isStdoutTruncated === true) return undefined
76  if (listed.exitCode === 1) return listed.stdout === '' ? [] : undefined
77  if (listed.exitCode !== 0) return undefined
78
79  const drivers = filterDrivers(listed.stdout)
80
81  return drivers === undefined ? undefined : filterOverrides(drivers)
82}
83
84// Paths are compared in one spelling: forward slashes, no trailing one, lower case (Windows paths).
85const spelled = (path: string): string => path.replaceAll('\\', '/').replace(/\/+$/, '').toLowerCase()
86
87// A symbolic link or a junction (which lands elsewhere) has to be told by the file system for the
88// paths status does not describe (untracked ones). One that is a link, lands outside the
89// repository (under its root as spelled, or under the root's own real path) or leads nowhere,
90// counts as a link. Undefined when there are too many to check.
91const markLinks = async (entries: readonly GitEntry[], root: string, inspect: Inspect): Promise<GitEntry[] | undefined> => {
92  const untracked = entries.filter(entry => entry.isUntracked === true && !entry.isMissing)
93  if (untracked.length > MAX_UNTRACKED) return undefined
94  if (untracked.length === 0) return [...entries]
95
96  const rootReal = (await inspect(root)).realPath
97  const inside = [root, ...(rootReal === undefined ? [] : [rootReal])].map(spelled)
98  const isOutside = ({ isLink, realPath }: { isLink: boolean; realPath?: string }): boolean => {
99    if (isLink || realPath === undefined) return true
100    const real = spelled(realPath)
101
102    return !inside.some(base => real === base || real.startsWith(`${base}/`))
103  }
104  const links = new Set(
105    (await Promise.all(untracked.map(async entry => [entry.path, isOutside(await inspect(`${root}/${entry.path}`))] as const)))
106      .filter(([, outside]) => outside)
107      .map(([path]) => path),
108  )
109
110  return entries.map(entry => (links.has(entry.path) ? { ...entry, isLink: true as const } : entry))
111}
112
113const hashesOf = async (
114  run: GitRun,
115  root: string,
116  entries: readonly GitEntry[],
117  off: readonly string[],
118): Promise<Record<string, string> | undefined> => {
119  const targets = hashTargets(entries)
120  if (targets.length === 0) return withHashes(entries, '')
121
122  const hashed = await call(run, [...GIT, ...off, 'hash-object', '--no-filters', '--stdin-paths'], {
123    cwd: root,
124    stdin: `${targets.join('\n')}\n`,
125  })
126
127  return hashed.exitCode === 0 && hashed.isStdoutTruncated !== true ? withHashes(entries, hashed.stdout) : undefined
128}
129
130/**
131 * The session's repository, or undefined outside one (or when `status` fails,
132 * or the filters could not be listed). A runner that throws (git cannot start,
133 * the call times out) is passed on.
134 */
135export const readTree = async (run: GitRun, inspect: Inspect): Promise<Tree | undefined> => {
136  const top = await call(run, [...GIT, 'rev-parse', '--show-toplevel'])
137  if (top.exitCode !== 0 || top.isStdoutTruncated === true) return undefined
138
139  const root = top.stdout.trim()
140  const off = await filterArgs(run, root)
141  if (off === undefined) return undefined
142
143  const listed = await call(run, [...GIT, ...off, 'status', '--ignore-submodules=all', '--porcelain=v2', '--branch', '-z', '-uall'], {
144    cwd: root,
145  })
146  // A status the engine cut short would give partial counts: it is no status.
147  const status = listed.exitCode === 0 && listed.isStdoutTruncated !== true ? parseGitStatus(listed.stdout) : undefined
148  if (status === undefined) return undefined
149
150  const entries = await markLinks(status.entries, root, inspect)
151  if (entries === undefined) return { root, status }
152
153  return { root, status: { ...status, entries }, hashes: await hashesOf(run, root, entries, off) }
154}
155
hooks/lib/plain.ts 18 lines
1// Text from outside (a branch name, a path, a model slug) drawn as it is could
2// carry control or format characters, and the engine refuses a Text holding
3// them (the whole tree is then not drawn). Every such string is made plain first.
4
5/** Control (Cc), format (Cf), line and paragraph separators and tag characters become spaces. */
6export const plain = (text: string): string => text.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\u{e0000}-\u{e007f}]/gu, ' ')
7
8/** How long ago `thenMs` was: `just now`, `3m ago`, `2h ago`; `—` when it never happened. */
9export const ago = (thenMs: number | undefined, nowMs: number): string => {
10  if (thenMs === undefined) return '—'
11
12  const minutes = Math.max(0, Math.floor((nowMs - thenMs) / 60000))
13  if (minutes < 1) return 'just now'
14  if (minutes < 60) return `${minutes}m ago`
15
16  return `${Math.floor(minutes / 60)}h ago`
17}
18