SLOPSHOPPER

downrange

A band, pane, and /downrange table: progress, ETA, and last output for Claude Code background shells and Monitor watches, tool activity for subagents, state…

panebandguardcommandpromptprocess
v1.1.7MITupdated 2026-10-25dot-agi/downrange
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · downrange
│ ┃ downrange ✕ › fix the failing auth test and add an audit log call │ ┃ No background work yet. Jobs show here when │ ┃ Claude runs a command in the background, ● downrange: classic.Stop: no session_crons in the hook input; a canc │ ┃ starts a Monitor, a background agent, or a ● downrange: classic.Stop: no background_tasks in the hook input; rec │ ┃ workflow. ⏺ Read(src/auth.ts) │ ⎿ Read 6 lines │ ⏺ Update(src/auth.ts) │ ⎿ Added 2 lines, removed 1 line │ ⏺ Bash(bun test) │ ⎿ 3 pass, 1 fail │ │ ● Done. refresh now rejects expired claims and logs an audit event. │ │ ✻ Worked for 42s · done 4:20 PM │ │ › /downrange │ ⎿ downrange: No background work right now. │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · downrange
No background work yet. Jobs show here when Claude runs a [ command in the background, starts a Monitor, a background agent, or a workflow.
README

<img src="https://img.shields.io/badge/Claude_Code-2.1.288%2B-D97757" alt="Claude Code 2.1.288+"> <img src="https://img.shields.io/badge/Claude_Code-mod-555555" alt="Claude Code mod"> <img src="https://img.shields.io/badge/license-MIT-78997A" alt="License: MIT"> <img src="https://img.shields.io/badge/version-1.1.7-555555" alt="Version: 1.1.7">

Claude Code terminal where a line above the prompt reads: 1 running, count to 90, 21/90 (23%), about 3 minutes left.

The band above the prompt shows a background shell with its progress and ETA.

Why

Claude can run shell commands, Monitor watches, subagents, and workflows in the background while you keep working. To see how far they got, you ask Claude, and that question costs a model request that reads the whole conversation again. Users ask for progress and ETAs taken from the output itself (#39131, #82741). Downrange reads that output on your machine and shows where each job stands.

Features

  • Progress from real output. 45%, [12/40], and Step 3 of 7 lines, and the progress lines of tools such as Docker, cargo, pytest, and git, become a progress label, and a bar in the pane. Shell output and Monitor events both count. A ratio in another place, such as loss 12/40, counts only with the genericRatios option on. A shell command that sends its output to a file, such as make > build.log 2>&1, shows progress from that file.
  • ETA. From the progress rate. With no rate yet, from the usual duration of the same command in the same folder, or from a Monitor watch's deadline. In the pane, a usual-duration ETA turns yellow once the run passes 1.5 times its usual duration.
  • The background tasks of your session. Shell commands (run_in_background, ctrl+b, or a command moved at its timeout), Monitor watches, background and remote subagents, teammates, workflow runs, and the pending dynamic /loop wakeup. For background sessions, use agent view (claude agents).
  • Liveness flags. quiet 3m 00s for a shell command or subagent that stayed silent for 2 minutes or more, and 3 failed calls in a row for a struggling subagent.
  • A table that prints mid-turn. /downrange answers at once, even while Claude works. With the answerStatusPrompts option on, a prompt such as status? gets the same table.
  • In step with Claude Code. At each turn end, Downrange checks its rows against Claude Code's task list. It adds tasks it missed, and marks a job that left the list ended without a report. A shell also ends when its output file ends with Claude Code's [exited with code N] line. Running jobs stay in view across /clear, /resume, and /branch. Under managed settings or a Team or Enterprise login, Claude Code sends Downrange no task list (see Known limits).
  • Read-only by design. Downrange never stops or changes a task. Built-in /tasks stops tasks.

Pane above the prompt shows three running jobs: count to 90 with a progress bar at 15/90, a wait 40s subagent, and a Monitor watch.

The live pane lists a shell, a subagent, and a Monitor watch.

The /downrange command prints a table of a running shell at 65/90, a running Monitor watch, and a finished subagent, with ages and ETAs.

/downrange prints a status table at once, with no model request.

The usage guide explains every view, symbol, note, and message.

How it works

flowchart LR
    L["Tool calls: launches, wakeups, stops, subagent activity"] --> S[("Jobs and wakeup in memory, one list per process")]
    N["Task notifications"] --> S
    P["Stop and SubagentStop snapshots of tasks and crons"] --> S
    T["Ticker, once a second while work is live"] -->|"output file ends, agent list"| S
    N -->|"clean shell runs"| H[("Usual durations under hashed keys")]
    H -->|"usual duration at launch"| S
    S -->|"each change, and each new session"| M[("Copy of the session's view in session state")]
    M --> V["Band, pane, and /downrange table"]
  • Launches. Hooks on the Bash, Monitor, Agent, and Workflow tools record each background launch with its id and title, and a shell's output file. A ScheduleWakeup result sets the wakeup. A TaskStop result marks its job stopped.
  • Output. The ticker reads each running shell's output file every 2 seconds, or every 10 seconds above 256 KiB. It keeps the end of the file: the last 16,384 characters, or the last 16,384 bytes through tail above 4 MiB. Progress comes from the last 10 display lines of that end.
  • Redirect files. Some commands send their output to a file, such as > build.log or | tee build.log. The ticker then also reads the one file that the command's text names, its redirect file, in the same way. At each read, the row shows the file that changed last: Claude Code's output file or the redirect file. On equal times, it shows Claude Code's output file. While the row shows the redirect file, it notes output: build.log. The end and the exit code still come from Claude Code's notification, or from the [exited with code N] line at the end of Claude Code's output file. Downrange follows no redirect file on Windows, or in a command with code that may not run, such as an if or a function.
  • Ends. Task notifications end jobs and carry Monitor events. A shell also ends when a read of its output file finds Claude Code's [exited with code N] line at the end, with that exit code. The agent list updates subagent states, every second while a subagent runs and at each turn end. Stop and SubagentStop snapshots add tasks that Downrange missed, and end tasks that left the list after a 10-second grace period. A scheduled prompt that fires from 5 seconds before the wakeup's due time clears the wakeup. So does a snapshot with no one-shot schedule, after the same grace period. Managed settings or a Team or Enterprise login keep the snapshots from Downrange (see Known limits).
  • Usual durations. A clean end (exit code 0 or none) of a shell that Downrange saw start adds its run time to a history keyed by a hash of folder and command. The usual-duration ETA is the median of the last 5 runs.
  • Views and session changes. Downrange keeps the jobs and the wakeup in its own memory, which /clear, /resume, and /branch do not touch. After each change, it writes a copy of the session's view to session state, and the band, the pane, and /downrange draw from that copy. So the new session shows the running jobs and the wakeup at once, or within a second under managed settings or a Team or Enterprise login, with their ages, last lines, progress, ETAs, and file reads. Each session shows the jobs that ended after it started. Hide does not carry. The ticker stops when nothing is live.

Install

Works with Claude Code 2.1.288 and later. Tested with Claude Code 2.1.296.

Inside Claude Code:

/plugin marketplace add dot-agi/downrange
/plugin install downrange@downrange
/reload-plugins

From a shell:

claude plugin marketplace add dot-agi/downrange
claude plugin install downrange@downrange

From a clone, for one session:

git clone https://github.com/dot-agi/downrange
claude --plugin-dir ./downrange

Agents and scripts

Install from a shell with Claude Code 2.1.292 or later. The second part turns on auto-update. It runs only when the install succeeds and the marketplace entry exists:

claude plugin install downrange --marketplace dot-agi/downrange --scope user --json \
  && f="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/settings.json" \
  && jq -e --arg name downrange '.extraKnownMarketplaces[$name].source' "$f" > /dev/null \
  && tmp=$(mktemp "$f.XXXXXX") \
  && { jq --arg name downrange '.extraKnownMarketplaces[$name].autoUpdate = true' "$f" > "$tmp" && mv "$tmp" "$f" || { rm -f "$tmp"; false; }; }

Claude Code 2.1.288 to 2.1.291 has no --marketplace option. Add the marketplace first, then run the auto-update part above:

claude plugin marketplace add dot-agi/downrange --scope user --json \
  && claude plugin install downrange@downrange --scope user --json

Auto-update runs only in interactive sessions. A headless job updates at its start, before the session that uses the mod:

claude plugin update downrange@downrange --json

Check the result. The first command prints the installed version. The second prints the latest release:

claude plugin list --json | jq -r '.[] | select(.id == "downrange@downrange") | "\(.version) enabled=\(.enabled)"' \
  && git ls-remote --tags --refs https://github.com/dot-agi/downrange 'downrange--v*' | sed 's/.*--v//' | sort -V | tail -n 1

Notes:

  • On a host with no SSH key for GitHub, set CLAUDE_CODE_PLUGIN_PREFER_HTTPS=1 in the environment of every claude command and session.
  • New code loads in the next session, or after /reload-plugins. An agent cannot type /reload-plugins, so it starts a new session.
  • Running install again does not update. Use update.
  • The mv replaces the settings file, with mode 600. If settings.json is a symbolic link, edit its target instead.

Commands

Command or keyWhat it does
/downrangePrints the status table at once, mid-turn too
/downrange paneOpens or closes the live pane
j on the focused bandOpens the pane
h on the focused bandHides the band until a new job starts
c in the focused paneClears finished jobs
x in the focused paneCloses the pane

Focus the band or the pane with Ctrl+X, then Tab, or a click. That chord is Claude Code's own abovePrompt:focus binding. Release Ctrl before you press Tab, because many terminals read Ctrl+Tab as next tab. On the focused band, Tab moves between Open and Hide, Enter presses one, and Esc returns to the prompt. When the band's row has free room, it shows the dim hint ctrl+x tab to choose just before Open. In Emacs vterm, eat, or ansi-term, click the band, use /downrange pane, or rebind abovePrompt:focus with /keybindings.

Options

Set options in /config, or under pluginConfigs["downrange@downrange"].options in ~/.claude/settings.json. For a --plugin-dir load, the key is downrange@inline.

OptionDefaultEffect
bandtrueShows the band while background work is live
answerStatusPromptsfalseWhen Claude is idle and a job runs, answers an exact status prompt (status?, what's the status?, give me a status update) with the table. Send the same prompt again within 60 seconds to ask Claude
genericRatiosfalseReads progress from a ratio in any place in the output, such as loss 12/40. Off, only known formats count, such as a percentage, or a ratio in brackets, at the start of a line, after a step word such as Step, or after a progress bar. On shows progress for more jobs, and more false readings, such as a date like Released 10/11

Where it runs

WhereBandPane/downrangeLocal status answer
Terminal, fullscreenYesDocked from 110 columns, else above the promptYesWhen the option is on
Terminal, main screenYesAbove the promptYesWhen the option is on
Desktop app, Code tabYesYesYesNo
VS Code panelNoNot verified; use /downrangeYesNo
Agent SDK and stream-json sessionsNoNoYesNo
Mobile app through Remote ControlNoNot verified; the terminal on your machine shows itYesNo

How it compares

Feature or projectWhat it doesWhat Downrange adds
Built-in /tasksAn interactive dialog that lists and stops background shells, subagents, Monitors, and MCP tasksLive progress, ETA, last output, the pending /loop wakeup, and a text table that prints mid-turn and in SDK sessions. Downrange does not stop tasks
Built-in subagent panelRows of running background subagents below the prompt, with a key that stops them allShells, Monitors, wakeups, and progress
Built-in /workflowsA progress line per workflow run, and a view of its phasesEach workflow run as one row beside the other jobs. Downrange shows no phases
Built-in agent view (claude agents)Background sessions with their state, and the /loop run count and countdown. A !cmd shell job shows its last output lineThe background tasks inside one session
rabeA band and pane with subagents, workflows, background shells, Monitors, cron jobs, and /loop wakeups with the next run time. It can stop jobs and message agents, and it estimates cost per agentProgress and ETA parsed from job output, a text table, and running jobs kept across /clear, /resume, and /branch
FlotillaBash, PowerShell, and Monitor background tasks with command, run time, and end state. An agent tree with model and cost, workflow bars, a timeline, and a stuck-agent alertThe last output line, progress, and ETA for shells, the /loop wakeup, quiet flags from output silence, and the carry across /clear
claude-deckShells and agents with run time, a stop button per shell, job steps as a tree, recently ended commands, cost and usage meters, and failure toastsMonitor watches, the /loop wakeup, progress and ETA from output, and running jobs kept across /clear
claude-flightdeckA permission gate view, model vitals, and rich subagent cards and swimlanesProgress and ETA read from the output of background shells and Monitors
sproutSubagent cards with cost and a file tree. Its progress comes from a note it injects for Claude and a step tool that Claude callsProgress read from real job output, with no tool or note for Claude

Known limits

What it catches

  • Progress. Percentages, [3/7], Step 3 of 7, and the progress lines of Docker, cargo, pytest, git, and similar tools.
  • Jobs. Background shells, Monitor watches, subagents, teammates, workflow runs, and the dynamic /loop wakeup.
  • Stalls. A shell or subagent that stays silent for 2 minutes, and a subagent with 3 failed calls in a row.
  • ETAs. From the progress rate, the usual run time of the same command in the same folder, or a Monitor watch's deadline.

What it does not catch

  • Progress. Ratios outside known formats, such as loss 12/40, unless genericRatios is on. Dates such as [10/11] can still count.
  • Jobs. Fixed-interval loops such as /loop 10m, other cron schedules, jobs after /background until the next turn end (under managed settings or a Team or Enterprise login, at all), and work that goes on after its job ends. nohup alone does not end a job at once. A command that ends with & does. Downrange follows no file of a command with & or nohup.
  • Stalls. Stuck remote subagents, teammates in their own panes, and waiting agents. They show no quiet flag.
  • ETAs. Run time in the foreground before ctrl+b or a timeout move. In output silence, a rate ETA runs down to <1m and stays there.

The list below holds the limits you can hit in daily use. docs/usage.md has every limit, also the rare ones: clashes with other schedules, size caps, races, failed reads, closed teammate panes, and unusual setups or task descriptions.

  • Only dynamic /loop wakeups. Only wakeups that ScheduleWakeup creates appear. Fixed-interval loops such as /loop 10m and other cron schedules do not.
  • Progress read from output. Progress is a guess from the text. By default, a ratio counts only in a known progress format: in brackets, at the start of a line, after a step word such as Step or Epoch, after a drawn bar, or in Docker Compose's [+] Running 3/7. A ratio in such a place that is not progress still counts. A short date counts, such as [10/11] or 10/11 deploy at the start of a line, unless a two-digit clock follows it, as in 10/11 14:03. So does N of M after a step word and before a space inside a path, such as Part 1 of 2 (live)/a.flac, and a percentage that is not progress, such as CPU 45%. Real progress in another place, such as loss 12/40 or [12:34:56] 3/15 done, does not count. With the genericRatios option on, a ratio counts in any place, so Released 10/11, Disc 1 of 2 (live)/a.flac, and other ratios that are not progress count too. These change the bar and the rate ETA. In both modes, full dates such as 2026/10/11 and 10/11/2026 do not count. Neither does N of M joined to a path or a number, such as /backups/3 of 7/, version 1.2 of 7.0, or 3 of 7.5 GB. The date rule also drops real progress of 1 to 12 out of at most 31 before a clock, such as 3/15 00:12.
  • ETAs during output silence. When output stops, the bar keeps its last reading, and the rate ETA keeps counting down from the time of that reading. Once the estimate runs out, the ETA stays at <1m while the job runs. This holds after quiet shows too, so a row can show 20%, <1m, and quiet 2m 00s together. A Monitor whose events stop behaves the same way.
  • Monitor deadlines and rate ETAs. A Monitor watch shows its deadline only when it has no progress-based ETA. Once its progress gives a rate ETA, or reaches 100% and shows finishing, the deadline no longer shows. A rate ETA can run past the deadline: a 30-minute watch that goes from 1% to 2% in its first minute shows ~1h 38m, although its deadline is 29 minutes away.
  • Time in the foreground before a move to the background. A shell job's clock starts when the Bash call returns its background result. A command that first ran in the foreground, such as one moved at its timeout or with ctrl+b, loses that time. Its age, its took time, and the run it adds to the duration history count only the time after the move. For example, a command moved after 2 minutes that ends 1 minute later shows took 1m 00s and records 1 minute. Runs of one command in one folder share one history, whatever the launch mode. The next run of that command that starts in the background then shows ~1m usual, even when it needs 3 minutes, and in the pane it can turn yellow early. A moved run can get a usual-duration ETA that is too long.
  • Tasks first seen in a snapshot. Claude Code's turn-end snapshot can list a Monitor as a shell. A launch result is the result of the Bash or Monitor call that started a task. A Monitor first seen in a snapshot shows as a shell until its launch result, its first event, or its end notification arrives. Only its launch result gives it a deadline. A shell first seen in a snapshot shows no last line, progress, ETA, or quiet flag until its launch result or a notification names its output file. Only its launch result brings a usual-duration ETA. A task that started before Downrange loaded, such as one in a session moved to another process, gets no launch result. Claude Code's internal tasks also appear, under their engine labels, and a long-lived one can keep the band visible. Use Hide to dismiss the band.
  • Commands that subagents start. A command with the note ends with its agent ends when its subagent answers. It then shows ended with its agent and no exit code, unless its own notification arrives later.
  • Agents that wait. A subagent that the agent list reports as pending, waiting, or idle shows that word, beside its tool count when it shows one. It shows no quiet flag and no failed-calls flag, however long it stays in that state.
  • Agents that run elsewhere. Downrange sees the tool calls only of agents that run in this Claude Code process. A remote subagent shows remote, and a teammate in a terminal pane of its own shows own pane. Their rows show no tool count and no quiet flag, so a stuck or dead one looks the same as a busy one. A row whose launch result Downrange missed, or that an earlier version of Downrange saved, still keeps 0 tools and shows quiet from 2 minutes after launch, even while the agent works. Its notification or a snapshot still ends it.
  • A session moved to another process. /background and a hand-off to the desktop app start a fresh process that resumes the conversation. Downrange starts empty there. Running tasks return at the next turn end, where turn-end snapshots reach Downrange, with ≥ ages and no last line. A shell's notification can still name its output file, end it, and show its last line. The /loop wakeup returns only with the next ScheduleWakeup result.
  • Managed settings and Team or Enterprise logins. There, Claude Code's built-in guard keeps every classic.* hook event from Downrange, so it gets no turn-end snapshot of tasks and schedules. A shell still ends at its notification, or at the [exited with code N] line at the end of its output file. When a job's end notice carries no status, only another input ends it: a shell at that line, a subagent when the agent list reports its end, a command with the note ends with its agent when its subagent answers, and any job at a TaskStop call that stops it. Without such an input, its row keeps running until the process ends. A cancelled /loop wakeup shows until 10 minutes past due. A task that Downrange did not see start joins only through a notification: a Monitor event, or an end notice with a status. After /clear, /resume, or /branch, the new session shows the running jobs and the wakeup when Downrange writes the copy of its view, which it does at once, and no job that ended before the change, unless this process showed that session before. A reload that no slash command and no /config change causes, such as one after an edit of a settings file, before that copy lands loses the jobs and the wakeup. docs/usage.md has the details.

Privacy

Downrange watches events and changes almost none of them. It makes no network request, writes no file of its own, and adds no tool for Claude.

  • What it sends, and where. Downrange itself sends nothing off your machine. Its output goes to your screen: the band, the pane, the /downrange table, the local status answer, and downrange: diagnostic lines in the transcript and the debug log. Two of these go further through Claude Code itself:
  • The /downrange table enters the conversation, so Claude reads it with its next request, as it reads any command output. The table cuts last output lines to 80 characters and removes control chara
Source 12 files
hooks/register.tsx 1038 lines
1import { atom, read, update } from 'claude-code'
2import type { Caught, CommandRunResult, EngineInterface, HookFailure, Register, Timer } from 'claude-code'
3
4import type { Job, JobsState, ShellJob, Wakeup, WakeupState } from '../types'
5import { bandTree, failedPaneTree, paneTree } from './drawing'
6import { durationsOf, historyKeyOf, recorded, usualOf } from './history'
7import type { Durations } from './history'
8import {
9  activityEnded,
10  activityOf,
11  activityStarted,
12  agentJob,
13  agentsSynced,
14  closedRuns,
15  finalIdsOf,
16  finishedCleared,
17  finishedRuns,
18  historyAdded,
19  isLive,
20  isRunning,
21  launched,
22  monitorJob,
23  noticed,
24  noticeProblemOf,
25  observedJobs,
26  readFailed,
27  readsDue,
28  reconciled,
29  reloaded,
30  shellJob,
31  ownerAnswered,
32  shellLaunchNote,
33  stoppedJob,
34  tailRead,
35  viewedJobs,
36  wakeupAfterSnapshot,
37  wakeupAfterTick,
38  wakeupAfterTrigger,
39  wakeupOf,
40  workflowJob,
41} from './jobs'
42import type { HistoryRun, JobObservation, SnapshotTask, TailRead } from './jobs'
43import { FS_READ_MAX, MAX_SESSIONS, MIRROR_WAIT_MS, REPEAT_WINDOW_MS, TAIL_TIMEOUT_MS, TICK_MS, TICKER_STALE_MS } from './limits'
44import { noticesOf } from './notices'
45import { closingCodeOf, displayLinesOf, isOutputFileOf, jobBytesOf, jobLinesOf, oneLine, outputEndOf, outputFileOf } from './output'
46import { jobsStateOf, wakeupStateOf } from './state'
47import { isStatusPrompt, normalizePrompt } from './prompt'
48import { redirectFileOf, redirectTargetOf } from './redirect'
49import { bandHintOf, bandOf, isBandHidden, paneModelOf, tableOf } from './view'
50
51/** The slash command, without its slash; declared here so that `claude plugin validate` resolves the matcher. */
52const COMMAND = 'downrange'
53/** The pane's id, and the requestId its render hook matches. */
54const PANE_ID = 'downrange'
55/** The pane's tab title. */
56const PANE_TITLE = 'Jobs'
57
58const JOBS = atom({ plugin: 'downrange', key: 'jobs' } as const, { items: [], observed: [], removed: [], snapshotAt: null }, { shape: 'v8' })
59const WAKEUP = atom({ plugin: 'downrange', key: 'wakeup' } as const, { pending: null, updatedAt: null }, { shape: 'v4' })
60const BAND_HIDDEN_AT = atom({ plugin: 'downrange', key: 'bandHiddenAt' } as const, null)
61const LAST_ANSWERED = atom({ plugin: 'downrange', key: 'lastAnswered' } as const, null)
62
63/** The buttons of the band and the pane. */
64type Control = 'Open' | 'Hide' | 'Clear finished' | 'Close'
65/** A hook that waits for a mirror write: told when the write lands, or why it failed. */
66type Waiter = { landed: () => void; failed: (error: unknown) => void }
67/** A mirror write, under way or next in line, and the hooks that wait for it. */
68type Write = { waiters: Set<Waiter> }
69/** One hook's bound on its waits for the mirror: its timer, whether it passed, and how each wait leaves at it. */
70type Deadline = { timer: Timer | null; isPassed: boolean; leaves: Set<() => void> }
71
72const NO_JOBS: JobsState = { items: [], observed: [], removed: [], snapshotAt: null }
73const NO_WAKEUP: WakeupState = { pending: null, updatedAt: null }
74
75/** Keys of diagnostics already shown; a reload re-arms them on purpose. */
76const reported = new Set<string>()
77
78/** The ticker's timer; a reload drops it, and session.start starts a new one. */
79let ticker: Timer | null = null
80/** Guards against overlapping refreshes inside one module instance. */
81let isTicking = false
82/** When the ticker last ran a period, or started; an old time means a refused period ended it. */
83let lastTickAt = 0
84/**
85 * The process's jobs and wakeup, the source of truth. Background work belongs to the process, so every session of the
86 * process shows it, and /clear, /resume, and /branch do not touch it. Each hook changes it at once; `$.state` holds a
87 * mirror of the current session's view of it, for drawing (see mirrored).
88 */
89const memory: { jobs: JobsState; wakeup: WakeupState } = { jobs: NO_JOBS, wakeup: NO_WAKEUP }
90/** The current session's `since`: its Finished shows the jobs that ended from then on (see viewNow). */
91let since = -Infinity
92/** The `since` of each session that this process showed, by session id: the MAX_SESSIONS most recent, oldest first. */
93const sessions = new Map<string, number>()
94/** When the last /clear, /resume, or /branch ended a session. */
95let switchedAt: number | null = null
96/**
97 * The /clear, /resume, or /branch that session.end began, until the mod knows the new session: the end's reason and the
98 * session that ended. classic.SessionStart settles it; where no classic.* event reaches the mod, switched does.
99 */
100let pending: { reason: 'clear' | 'resume'; ended: string } | null = null
101/**
102 * The mirror: the read of it into memory at a load of the module (see loaded), the write under way, the one after it,
103 * which holds every change made meanwhile, whether the last write failed, and the write that carries the latest settle
104 * of a switch, until that write lands or fails (see settleLanded).
105 */
106const mirror: { load: Promise<void> | null; isLoaded: boolean; writing: Write | null; next: Write | null; isBehind: boolean; settle: Write | null } =
107  { load: null, isLoaded: false, writing: null, next: null, isBehind: false, settle: null }
108/**
109 * The last history record; each record waits for it, so two in one session never overwrite each other. Two sessions
110 * can still race, because the store has no versioned write; a lost record costs every run it held, a new key's first too.
111 */
112let historyWrites: Promise<void> = Promise.resolve()
113/** The `genericRatios` option, which register sets: any ratio in output counts as progress, not only a known format. */
114let isAnyRatio = false
115
116/** A `tail` that could not start, timed out, or exited non-zero. */
117class TailFailure extends Error {
118  constructor(message: string, readonly bytes: number) {
119    super(message)
120  }
121}
122
123function messageOf(error: unknown): string {
124  return error instanceof Error ? error.message : String(error)
125}
126
127/** A stat, read, or tail of a job's redirect file that failed, with that failure, and the code on the closing line of the output file read before it. */
128class RedirectFailure extends Error {
129  constructor(readonly failure: unknown, readonly exitCode: number | undefined) {
130    super(messageOf(failure))
131  }
132}
133
134/** A hook failure as one phrase: `hook throw: <message>`. */
135function failureText(failure: HookFailure): string {
136  return `hook ${failure.kind}: ${failure.message ?? 'no message'}`
137}
138
139/** A failed hook, named by its label, goes to the transcript the first time, then to the debug log. */
140function reportHook($: EngineInterface, hook: string, failure: HookFailure): void {
141  const key = `hook|${hook}`
142  const isFirst = !reported.has(key)
143  reported.add(key)
144  $.ui.log(`${isFirst ? 'downrange: ' : ''}${hook}: ${failureText(failure)}`, { to: isFirst ? 'transcript' : 'debug' })
145}
146
147/** Whether a hook failed by re-entry. Claude Code 2.1.309 and earlier report this kind; the 2.1.310 types no longer name it. */
148function isReentry(failure: HookFailure): boolean {
149  return (failure.kind as string) === 're-entry'
150}
151
152/** What the `.catch` of a hook that only watches does: it reports the failure under the hook's label and passes the event on unchanged. */
153function passOn<I, R>($: EngineInterface, label: string, e: I, next: ((e: I) => R) & Caught): R {
154  if (!isReentry(next.error)) reportHook($, label, next.error)
155  return next(e)
156}
157
158/** Logs the event and cause once per key; the debug sink supplies the plugin name. */
159function report($: EngineInterface, event: string, cause: string, once: string): void {
160  const key = `${event}|${once}`
161  if (reported.has(key)) return
162  reported.add(key)
163  $.ui.log(`${event}: ${cause}`, { to: 'debug' })
164}
165
166/** Starts the ticker, or starts it again (and logs once) when no period ran for TICKER_STALE_MS; a tick stops it when nothing is live. */
167async function ensureTicker($: EngineInterface): Promise<void> {
168  const now = await $.clock.now()
169  if (ticker !== null && now - lastTickAt < TICKER_STALE_MS) return
170  if (ticker !== null) {
171    ticker.cancel()
172    report($, 'clock.every', 'the ticker stopped (a period was refused); started it again', 'restart')
173  }
174  lastTickAt = now
175  ticker = $.clock.every(TICK_MS, () => {
176    void tick($)
177  })
178}
179
180/**
181 * The current session's view of the process's data: every running job, the ended jobs from its `since` on with a
182 * removal for each other one (see viewedJobs), and the wakeup.
183 */
184function viewNow(): { jobs: JobsState; wakeup: WakeupState } {
185  return { jobs: viewedJobs(memory.jobs, since), wakeup: memory.wakeup }
186}
187
188/** One write of the mirror: the view as it stands when the write begins, the jobs, then the wakeup. */
189async function writeMirror($: EngineInterface): Promise<void> {
190  const shown = viewNow()
191  await $.state.set({ plugin: 'downrange', key: 'jobs' }, { shape: 'v8', value: shown.jobs })
192  await $.state.set({ plugin: 'downrange', key: 'wakeup' }, { shape: 'v4', value: shown.wakeup })
193}
194
195/**
196 * Begins a mirror write, for the hooks that wait for it. When it ends, one listener tells each of them, and the write
197 * after it, which holds every change made meanwhile, begins with its own waiters. A failed write leaves the mirror
198 * behind until a later write lands (see refresh).
199 */
200function begun($: EngineInterface, write: Write = { waiters: new Set() }): Write {
201  mirror.writing = write
202  const ended = (failure: { error: unknown } | null): void => {
203    mirror.writing = null
204    if (mirror.settle === write) mirror.settle = null
205    mirror.isBehind = failure !== null
206    for (const waiter of write.waiters) {
207      if (failure === null) waiter.landed()
208      else waiter.failed(failure.error)
209    }
210    write.waiters.clear()
211    const next = mirror.next
212    if (next !== null) {
213      mirror.next = null
214      begun($, next)
215    }
216  }
217  writeMirror($).then(() => ended(null), (error: unknown) => ended({ error }))
218  return write
219}
220
221/** The write that holds the data as it stands now: a new write when none runs, else the one after the write under way. */
222function carrying($: EngineInterface): Write {
223  if (mirror.next !== null) return mirror.next
224  if (mirror.writing === null) return begun($)
225  mirror.next = { waiters: new Set() }
226  return mirror.next
227}
228
229/** A new bound for one hook's waits for the mirror (see waited). */
230function deadlineOf(): Deadline {
231  return { timer: null, isPassed: false, leaves: new Set() }
232}
233
234/**
235 * Waits until `write` lands, until the hook's deadline at most. The deadline starts at the hook's first wait and holds
236 * for every wait of that hook, so one hook waits MIRROR_WAIT_MS at most in all. At the deadline the hook leaves the
237 * write's waiters, so a write that does not answer keeps nothing of it; the deadline's timer ends by itself then.
238 */
239function waited($: EngineInterface, write: Write, deadline: Deadline): Promise<void> {
240  if (deadline.isPassed) return Promise.resolve()
241  return new Promise<void>((resolve, reject) => {
242    const leave = (): void => {
243      write.waiters.delete(waiter)
244      resolve()
245    }
246    const waiter: Waiter = {
247      landed: () => {
248        deadline.leaves.delete(leave)
249        resolve()
250      },
251      failed: error => {
252        deadline.leaves.delete(leave)
253        reject(error)
254      },
255    }
256    write.waiters.add(waiter)
257    deadline.leaves.add(leave)
258    deadline.timer ??= $.clock.after(MIRROR_WAIT_MS, () => {
259      deadline.isPassed = true
260      for (const left of deadline.leaves) left()
261      deadline.leaves.clear()
262    })
263  })
264}
265
266/**
267 * Writes the current session's view to the mirror after a change, and waits until a write that holds the change has
268 * landed: a reload right after the hook keeps the change. One write runs at a time, so the writes land in the order they
269 * began: a write that began before a switch lands before the write that classic.SessionStart asks for. A change while a
270 * write runs asks for one more write after it, which holds every change made meanwhile: a burst costs at most two
271 * writes, and the last write holds the current data. The hook's deadline bounds the wait (see waited): past it, the
272 * change stays in memory, and a later write or tick writes it. The write of a settle of a switch stays in
273 * `mirror.settle` until it lands or fails, past the deadline too (see settleLanded).
274 */
275async function mirrored($: EngineInterface, deadline: Deadline, isSettle = false): Promise<void> {
276  if (!mirror.isLoaded) await loaded($)
277  const write = carrying($)
278  if (isSettle) mirror.settle = write
279  await waited($, write, deadline)
280}
281
282/**
283 * Mirrors a change that memory took (see mirrored); `isSettle` marks the view that a settle of a switch gave. A failed
284 * write is no failure of the hook: memory holds the change, so what follows it in the hook still runs, the ticker
285 * writes the mirror again, and the failure reports to the transcript the first time, then to the debug log.
286 */
287async function persisted($: EngineInterface, deadline: Deadline, isSettle = false): Promise<void> {
288  try {
289    await mirrored($, deadline, isSettle)
290  } catch (error) {
291    const isFirst = !reported.has('mirror')
292    reported.add('mirror')
293    $.ui.log(`${isFirst ? 'downrange: ' : ''}state.set: cannot write the copy of the session's view: ${messageOf(error)}; the next tick writes it again`, { to: isFirst ? 'transcript' : 'debug' })
294  }
295}
296
297/**
298 * Applies an observation to the process's jobs by the ordering rule, then mirrors them; the result tells whether they
299 * changed. In a module that has not read the mirror yet, the observation waits for that read (see loaded).
300 */
301async function writeJobs($: EngineInterface, deadline: Deadline, input: JobObservation, change: (jobs: Job[]) => Job[] = jobs => jobs): Promise<boolean> {
302  if (!mirror.isLoaded) await loaded($)
303  const before = memory.jobs
304  memory.jobs = observedJobs(before, input, change)
305  const isChanged = memory.jobs !== before
306  // What follows the change runs whether or not the mirror write lands: the ticker, which reads outputs and writes a
307  // mirror that fell behind, and the rest of the hook.
308  if (isChanged) await persisted($, deadline)
309  await ensureTicker($)
310  return isChanged
311}
312
313/** The wakeup state after a write at `now`: an older write never replaces a newer one. */
314function wakeupWritten(state: WakeupState, now: number, change: (wakeup: Wakeup | null) => Wakeup | null, isReplacement: boolean): WakeupState {
315  if (state.updatedAt !== null && now < state.updatedAt) return state
316  const pending = change(state.pending)
317  return isReplacement || pending !== state.pending ? { pending, updatedAt: now } : state
318}
319
320/** Sets the process's wakeup, and preserves the latest wakeup cancellation, then mirrors it; it waits, reports, and starts the ticker as writeJobs does. */
321async function writeWakeup($: EngineInterface, deadline: Deadline, now: number, change: (wakeup: Wakeup | null) => Wakeup | null, isReplacement = false): Promise<void> {
322  if (!mirror.isLoaded) await loaded($)
323  const before = memory.wakeup
324  memory.wakeup = wakeupWritten(before, now, change, isReplacement)
325  if (memory.wakeup !== before) await persisted($, deadline)
326  await ensureTicker($)
327}
328
329/**
330 * Memory after a load of the module. A reload of the mod starts module variables empty, keeps `$.state`, and fires
331 * session.start again. So the first session.start, the first change of memory, or the first mirror write, whichever
332 * comes first, reads the mirror into memory, once. Each change waits for that read and applies on top of it, and no
333 * mirror write goes out before it: the mirror is never written from empty memory over its own data. A failed read is
334 * tried again at the next change. A later session.start of the same module keeps memory.
335 */
336function loaded($: EngineInterface): Promise<void> {
337  mirror.load ??= seeded($).catch((error: unknown) => {
338    mirror.load = null
339    throw error
340  })
341  return mirror.load
342}
343
344/**
345 * Starts memory from the mirror, which holds the current session's view: its running jobs, its ended jobs, the removals,
346 * and the wakeup (Hide stays in its own atom). No change of memory runs before it ends (see loaded). A stored shape to
347 * update, an invalid value, and rows that the current options change go back to the mirror at once; with none of
348 * them, nothing is written.
349 */
350async function seeded($: EngineInterface): Promise<void> {
351  const now = await $.clock.now()
352  const [storedJobs, storedWakeup] = await Promise.all([$.state.get({ plugin: 'downrange', key: 'jobs' }), $.state.get({ plugin: 'downrange', key: 'wakeup' })])
353  const jobs = jobsStateOf(storedJobs.value)
354  if (storedJobs.value !== undefined && jobs === undefined) report($, 'state.get', 'unsupported or invalid jobs state; reset safely', 'jobs-shape')
355  const wakeup = wakeupStateOf(storedWakeup.value)
356  if (storedWakeup.value !== undefined && wakeup === undefined) report($, 'state.get', 'unsupported or invalid wakeup state; reset safely', 'wakeup-shape')
357  memory.jobs = jobs ?? NO_JOBS
358  memory.wakeup = wakeup ?? NO_WAKEUP
359  // After a reload, as a change of genericRatios or an update causes, rows follow the current option. They keep their
360  // observation times: an input that came after the reload, before this read ended, is judged by them, not by the read.
361  const items = reloaded(memory.jobs.items, isAnyRatio, now)
362  const isReloaded = items.some((job, index) => job !== memory.jobs.items[index])
363  if (isReloaded) memory.jobs = { ...memory.jobs, items }
364  const isJobsOld = storedJobs.value !== undefined && (jobs === undefined || storedJobs.value.shape !== 'v8')
365  const isWakeupOld = storedWakeup.value !== undefined && (wakeup === undefined || storedWakeup.value.shape !== 'v4')
366  mirror.isLoaded = true
367  // The new shape goes to the mirror; the hook that waits for the read waits for its own change, not for this write.
368  if (isReloaded || isJobsOld || isWakeupOld) carrying($)
369}
370
371/**
372 * Settles a /clear, /resume, or /branch that no classic.SessionStart settled yet. Under managed settings or a Team or
373 * Enterprise login, Claude Code's built-in guard keeps every classic.* event from the mod, and session.start does not
374 * fire again. So each hook other than session.start, session.end, classic.SessionStart, and the render hooks calls this
375 * first, and so does each tick. A zero-delay timer that session.end starts calls it once at once, before the first tick
376 * (see settledAtOnce). A command.run hook for every command calls it before the command runs, and a config.set hook for
377 * every key before the row changes, so that a reload of the mod that a command, such as /reload-plugins, or a change of
378 * an option causes finds the view in `$.state`. Both then wait for the write of a settle that is under way, and write
379 * the view once more when the last write failed (see settleLanded). Once `$.session.id()` answers an id other than the
380 * ended session's, that id is the new session, once. After a /clear it starts at the switch. After a /resume or a /branch, which the mod cannot tell apart here, a session
381 * that this process showed keeps its start, and any other starts at the switch, as classic.SessionStart starts it.
382 * Until then the view is the safe one that session.end set: every running job, the wakeup, and no job that ended before
383 * the switch. Each call writes the view, because a write that landed before the engine reset `$.state` is gone. With a
384 * personal login, `$.session.id()` still answers the parent during and before classic.SessionStart on /branch, so no
385 * call settles a branch before that event does.
386 */
387async function switched($: EngineInterface, deadline: Deadline): Promise<void> {
388  const switching = pending
389  if (switching === null) return
390  const id = await $.session.id().catch(() => undefined)
391  // classic.SessionStart, another hook, or a newer switch came meanwhile.
392  if (pending !== switching) return
393  const isSettled = id !== undefined && id !== switching.ended
394  if (isSettled) {
395    pending = null
396    entered(id, (switching.reason === 'resume' ? sessions.get(id) : undefined) ?? switchedAt ?? since)
397  }
398  await persisted($, deadline, isSettled)
399  await ensureTicker($)
400}
401
402/**
403 * Waits until the write that carries the latest settle of a switch lands, until the hook's deadline at most (see
404 * waited). A settle clears `pending` before its write lands, so a call of switched that comes meanwhile, or whose read
405 * of the session id was under way, returns before that write lands. The command.run hook for every command and the
406 * config.set hook call this after switched, because a command or a change of an option can reload the mod, and the
407 * reload must find the new session's view in `$.state`. With no settle in progress, it waits for nothing and asks
408 * nothing. A failed write is no failure of the hook here. When the last mirror write failed, a settle's write or any
409 * other, both hooks then write the view once more, within the same deadline (see persisted): a reload that comes
410 * before the next tick still finds the view. With no write behind, they write nothing.
411 */
412async function settleLanded($: EngineInterface, deadline: Deadline): Promise<void> {
413  const write = mirror.settle
414  if (write === null) return
415  await waited($, write, deadline).catch(() => undefined)
416}
417
418/**
419 * The settle that session.end asks for at once, on a zero-delay timer, because the first tick can be a second away
420 * (see switched). When the engine reset `$.state` before the timer fires, a reload that no event comes before, such as
421 * one after an edit of a settings file, finds the view there. When it resets `$.state` after that, and the switch still
422 * waits, the next tick at the latest writes the view again. A failure logs once per message, as a tick's does.
423 */
424async function settledAtOnce($: EngineInterface): Promise<void> {
425  try {
426    await switched($, deadlineOf())
427  } catch (error) {
428    report($, 'clock.after', messageOf(error), `after|${messageOf(error)}`)
429  }
430}
431
432/** Makes session `id` the current one from `at` on, and keeps the MAX_SESSIONS most recent sessions. */
433function entered(id: string | undefined, at: number): void {
434  since = at
435  if (id === undefined) return
436  sessions.delete(id)
437  sessions.set(id, at)
438  for (const oldest of sessions.keys()) {
439    if (sessions.size <= MAX_SESSIONS) break
440    sessions.delete(oldest)
441  }
442}
443
444/** The stored durations; a value of the wrong shape counts as empty and logs once. */
445async function storedDurations($: EngineInterface, event: string): Promise<Durations> {
446  const durations = durationsOf(await $.store.get('durations'))
447  if (durations !== undefined) return durations
448  report($, event, 'duration history had the wrong shape; the next record replaces it', 'history-shape')
449  return {}
450}
451
452/** The history key and usual duration for a launch; a failure gives neither, logs once, and never stops the launch. */
453async function historyFor($: EngineInterface, command: string): Promise<{ historyKey: string; usualMs?: number } | undefined> {
454  try {
455    const historyKey = await historyKeyOf(await $.session.cwd(), command)
456    return { historyKey, usualMs: usualOf(await storedDurations($, 'tool.call'), historyKey) }
457  } catch (error) {
458    report($, 'tool.call', `duration history unavailable: ${messageOf(error)}`, 'history-get')
459    return undefined
460  }
461}
462
463/**
464 * The redirect file of a launch, absolute in the session's folder, or undefined. A subagent's command follows only an
465 * absolute path: a subagent can run in a folder of its own. A failure gives none, logs once, and never stops the launch.
466 */
467async function redirectFor($: EngineInterface, id: string, command: string, agentId: string | undefined): Promise<string | undefined> {
468  try {
469    const target = redirectTargetOf(command)
470    if (target === undefined || (agentId !== undefined && !target.startsWith('/'))) return undefined
471    return redirectFileOf(target, await $.session.cwd())
472  } catch (error) {
473    report($, 'tool.call', `cannot place the redirect file of task ${id}: ${messageOf(error)}`, `redirect|${id}`)
474    return undefined
475  }
476}
477
478/** Appends clean runs to the duration history, one record at a time; a failure costs the record only and logs once. */
479async function recordRuns($: EngineInterface, runs: readonly HistoryRun[], now: number, event: 'prompt.submit' | 'session.append' | 'clock.every'): Promise<void> {
480  if (runs.length === 0) return
481  const record = historyWrites.then(async () => {
482    try {
483      const durations = await storedDurations($, event)
484      await $.store.set('durations', runs.reduce((all, run) => recorded(all, run.key, run.durationMs, now), durations))
485    } catch (error) {
486      report($, event, `duration history unavailable: ${messageOf(error)}`, 'history-set')
487    }
488  })
489  historyWrites = record
490  await record
491}
492
493/** Applies every notification block in a delivered prompt, in order; history judges each block on the list the blocks before it left. */
494async function applyNotices($: EngineInterface, deadline: Deadline, text: string, noticeRow?: string, observedAt?: number): Promise<void> {
495  const event = noticeRow === undefined ? 'prompt.submit' : 'session.append'
496  const notices = noticesOf(text)
497  if (notices.length === 0) {
498    if (text.includes('<task-notification>')) report($, event, 'a task notification held no complete <task-notification> block; jobs end at the next turn end', 'no-block')
499    return
500  }
501  for (const problem of notices.map(noticeProblemOf)) {
502    if (problem !== undefined) report($, event, problem.cause, problem.once)
503  }
504  const now = observedAt ?? await $.clock.now()
505  let runs: HistoryRun[] = []
506  const changed = await writeJobs($, deadline, { at: now, ids: notices.flatMap(notice => notice.ids), kind: 'notice', finalIds: finalIdsOf(notices), noticeRow }, jobs => {
507    runs = finishedRuns(jobs, notices, now)
508    return notices.reduce((list, notice) => noticed(list, notice, now, isAnyRatio), jobs)
509  })
510  if (changed) await recordRuns($, runs, now, event)
511}
512
513/** Reconciles with a Stop or SubagentStop snapshot: the wakeup with `session_crons`, the jobs with `background_tasks`; an absent field logs once. */
514async function reconcile(
515  $: EngineInterface,
516  deadline: Deadline,
517  event: string,
518  input: { background_tasks?: readonly SnapshotTask[]; session_crons?: readonly { recurring: boolean }[] },
519): Promise<void> {
520  const now = await $.clock.now()
521  const crons = input.session_crons
522  if (crons === undefined) {
523    report($, event, 'no session_crons in the hook input; a cancelled wakeup waits for its expiry', 'no-crons')
524  } else {
525    await writeWakeup($, deadline, now, current => wakeupAfterSnapshot(current, crons, now))
526  }
527  const tasks = input.background_tasks
528  if (tasks === undefined) {
529    report($, event, 'no background_tasks in the hook input; reconciliation skipped', 'no-snapshot')
530    return
531  }
532  await writeJobs($, deadline, { at: now, kind: 'snapshot', members: tasks.map(task => task.id) }, jobs => reconciled(jobs, tasks, now))
533}
534
535/** Runs `tail -c 16384 <path>`: the last TAIL_BYTES bytes, written as text so the command reads in full; only the output file path varies. */
536async function tailOf($: EngineInterface, path: string, bytes: number): Promise<string> {
537  const ran = await $.process.run(['tail', '-c', '16384', path], { timeoutMs: TAIL_TIMEOUT_MS }).catch((error: unknown) => {
538    throw new TailFailure(messageOf(error), bytes)
539  })
540  if (ran.exitCode !== 0) throw new TailFailure(`exit ${ran.exitCode}: ${oneLine(ran.stderr)}`, bytes)
541  return ran.stdout
542}
543
544/** A file's end, by `$.fs.read` up to FS_READ_MAX and by `tail` above. */
545async function endOf($: EngineInterface, path: string, size: number): Promise<string> {
546  const isTail = size > FS_READ_MAX
547  return outputEndOf(isTail ? await tailOf($, path, size) : await $.fs.read(path), isTail)
548}
549
550/**
551 * What a job's output holds now: no new lines while its size stays, else its end, and the exit code when that end is
552 * Claude Code's closing line. A job with a redirect file also looks at that file (see redirectRead).
553 */
554async function readOutput($: EngineInterface, job: ShellJob & { outputFile: string }): Promise<TailRead> {
555  const stat = await $.fs.stat(job.outputFile)
556  const isAsked = job.failedReadBytes !== undefined || job.finalReadAskedAt !== undefined
557  const text = stat.size === job.outputBytes && !isAsked ? undefined : await endOf($, job.outputFile, stat.size)
558  const lines = text === undefined ? [] : displayLinesOf(text)
559  const output: TailRead = { bytes: stat.size, mtimeMs: stat.mtimeMs, lines, exitCode: closingCodeOf(lines) }
560  return job.redirectFile === undefined ? output : await redirectRead($, job, job.redirectFile, output, text, isAsked)
561}
562
563/**
564 * The read of a job with a redirect file, after the read of its output (`text` when that read took its end). The row
565 * takes its lines from the file that changed last, by modification time, and from the output file when the two times
566 * are equal. The output file counts only while it holds a job line. Its time is its modification time once the job wrote
567 * to it since the last read, which the engine's closing line alone is not. Until then it keeps the time of the lines that
568 * the row shows from it, and it does not count while the row shows the redirect file. When the redirect file is missing
569 * or not a regular file, the output file shows if it holds a job line. The redirect file is read again when its size or
570 * its modification time changes. A read of it carries its size and time, which tailRead keeps as redirectBytes and
571 * lastOutputAt. Every result, and a failure, carries the exit code on the closing line of the output file.
572 */
573async function redirectRead($: EngineInterface, job: ShellJob & { outputFile: string }, redirectFile: string, output: TailRead, text: string | undefined, isAsked: boolean): Promise<TailRead> {
574  const isRedirectShown = job.redirectBytes !== undefined
575  const isWritten = text !== undefined && output.bytes !== job.outputBytes && (job.outputBytes === undefined || jobBytesOf(text, output.bytes) > job.outputBytes)
576  const outputAt = isWritten ? output.mtimeMs : isRedirectShown ? undefined : job.lastOutputAt
577  /** Whether the output file holds a job line: in the lines read now, else in the line that the row shows from it. */
578  const hasJobLine = (lines: readonly string[]): boolean => (text === undefined && !isRedirectShown ? job.lastLine !== undefined : jobLinesOf(lines).length > 0)
579  let redirect: { size: number; mtimeMs: number }
580  try {
581    const stat = await $.fs.stat(redirectFile)
582    if (stat.kind !== 'file') throw new Error('not a regular file')
583    redirect = stat
584  } catch (error) {
585    // A row that shows the redirect file needs the output file's end to show it instead.
586    const lines = text === undefined && isRedirectShown ? displayLinesOf(await endOf($, job.outputFile, output.bytes)) : output.lines
587    if (hasJobLine(lines)) return { ...output, lines, exitCode: closingCodeOf(lines) }
588    throw new RedirectFailure(error, closingCodeOf(lines))
589  }
590  if (outputAt !== undefined && outputAt >= redirect.mtimeMs && hasJobLine(output.lines)) return output
591  try {
592    const isSame = isRedirectShown && redirect.size === job.redirectBytes && redirect.mtimeMs === job.lastOutputAt
593    const lines = isSame && !isAsked ? [] : displayLinesOf(await endOf($, redirectFile, redirect.size))
594    return { bytes: output.bytes, mtimeMs: redirect.mtimeMs, lines, redirectBytes: redirect.size, exitCode: output.exitCode }
595  } catch (error) {
596    throw new RedirectFailure(error, output.exitCode)
597  }
598}
599
600/** Applies a read of a job's output, or a failed read, and records the clean run that the read's closing line ends (see closedRuns). */
601async function readApplied($: EngineInterface, deadline: Deadline, id: string, now: number, exitCode: number | undefined, change: (jobs: Job[]) => Job[]): Promise<void> {
602  let runs: HistoryRun[] = []
603  const changed = await writeJobs($, deadline, { at: now, ids: [id] }, jobs => {
604    runs = closedRuns(jobs, id, exitCode, now)
605    return change(jobs)
606  })
607  if (changed) await recordRuns($, runs, now, 'clock.every')
608}
609
610/**
611 * Reads one shell job's output and applies it; only a failed stat, read, or tail sets the job's note and logs once per
612 * job and file. An output file that ends with Claude Code's closing line ends a running row with its exit code.
613 */
614async function readTail($: EngineInterface, deadline: Deadline, job: ShellJob & { outputFile: string }, now: number): Promise<void> {
615  const path = job.outputFile
616  if (!isOutputFileOf(path, job.id)) {
617    report($, 'clock.every', `refused to read ${path}: it is not the output file of task ${job.id}`, `path|${job.id}`)
618    await writeJobs($, deadline, { at: now, ids: [job.id] }, jobs => readFailed(jobs, job.id, 'output unavailable', now))
619    return
620  }
621  let found: TailRead
622  try {
623    found = await readOutput($, job)
624  } catch (error) {
625    const isRedirect = error instanceof RedirectFailure
626    const failure = isRedirect ? error.failure : error
627    const bytes = failure instanceof TailFailure ? failure.bytes : undefined
628    const verb = bytes === undefined ? 'read' : 'tail'
629    report($, 'clock.every', `cannot ${verb} ${isRedirect ? 'the redirect file' : 'the output'} of task ${job.id}: ${messageOf(failure)}`, `${isRedirect ? 'redirect ' : ''}${verb}|${job.id}`)
630    const exitCode = isRedirect ? error.exitCode : undefined
631    await readApplied($, deadline, job.id, now, exitCode, jobs => readFailed(jobs, job.id, bytes === undefined ? 'output unavailable' : 'tail failed', now, bytes, exitCode))
632    return
633  }
634  await readApplied($, deadline, job.id, now, found.exitCode, jobs => tailRead(jobs, job.id, found, now, isAnyRatio))
635}
636
637/** Applies `$.agent.list()` to agent jobs; a failed list logs once. */
638async function syncAgents($: EngineInterface, deadline: Deadline, now: number, event = 'clock.every'): Promise<void> {
639  const agents = await $.agent.list().catch((error: unknown) => {
640    report($, event, `cannot list agents: ${messageOf(error)}`, 'agents')
641    return undefined
642  })
643  if (agents !== undefined) await writeJobs($, deadline, { at: now, ids: agents.map(agent => agent.id) }, jobs => agentsSynced(jobs, agents, now))
644}
645
646/** One ticker period: it marks the ticker alive, then refreshes unless a refresh still runs; a failure logs once per message. */
647async function tick($: EngineInterface): Promise<void> {
648  try {
649    const now = await $.clock.now()
650    lastTickAt = now
651    if (isTicking) return
652    isTicking = true
653    try {
654      await refresh($, now)
655    } finally {
656      isTicking = false
657    }
658  } catch (error) {
659    report($, 'clock.every', messageOf(error), `tick|${messageOf(error)}`)
660  }
661}
662
663/**
664 * The work of one tick: settle a switch (see switched), expire the wakeup, sync agents, read outputs (a pending final
665 * read too), write a mirror that fell behind, redraw, and stop when nothing is live and no switch waits.
666 */
667async function refresh($: EngineInterface, now: number): Promise<void> {
668  const deadline = deadlineOf()
669  await switched($, deadline)
670  const seen = { ...memory }
671  const { jobs: { items: shown }, wakeup: { pending: wakeup } } = viewNow()
672  if (wakeupAfterTick(wakeup, now) !== wakeup) await writeWakeup($, deadline, now, current => wakeupAfterTick(current, now))
673  if (seen.jobs.items.some(job => job.kind === 'agent' && isRunning(job))) await syncAgents($, deadline, now)
674  for (const job of readsDue(seen.jobs.items, now)) await readTail($, deadline, job, now)
675  if (mirror.isBehind) await mirrored($, deadline)
676  // Every change replaces the jobs or the wakeup: after a change during this tick, the next tick judges liveness on fresh data.
677  if (!isLive(shown, wakeup, now) && memory.jobs === seen.jobs && memory.wakeup === seen.wakeup && mirror.writing === null && !mirror.isBehind && pending === null) {
678    ticker?.cancel()
679    ticker = null
680  }
681  $.ui.invalidate('ui.render')
682}
683
684/** Runs a button of the band or the pane; a failure, and an Open whose pane waits unplaced, log to the transcript. */
685async function pressed($: EngineInterface, control: Control): Promise<void> {
686  try {
687    if (control === 'Open') {
688      const opened = await $.ui.open({ id: PANE_ID, title: PANE_TITLE })
689      if (!opened.isPlaced) $.ui.log(`downrange: ui.press: the Jobs pane waits: ${opened.reason}`)
690    } else if (control === 'Hide') {
691      const now = await $.clock.now()
692      await update($, BAND_HIDDEN_AT, () => now)
693    } else if (control === 'Clear finished') {
694      await writeJobs($, deadlineOf(), { at: await $.clock.now() }, finishedCleared)
695    } else {
696      await $.ui.close({ id: PANE_ID })
697    }
698  } catch (error) {
699    $.ui.log(`downrange: ui.press: ${control} failed: ${messageOf(error)}`)
700  }
701}
702
703/** The time, and the jobs and the wakeup in state; in a render hook, the reads subscribe the drawing. */
704async function stateNow($: EngineInterface): Promise<{ now: number; jobs: Job[]; wakeup: WakeupState }> {
705  const [now, { items: jobs }, wakeup] = await Promise.all([$.clock.now(), read($, JOBS), read($, WAKEUP)])
706  return { now, jobs, wakeup }
707}
708
709/** The /downrange table at the command's width. */
710async function tableNow($: EngineInterface, columns: number): Promise<string> {
711  const { now, jobs, wakeup: { pending: wakeup } } = await stateNow($)
712  return tableOf(jobs, wakeup, now, columns, false)
713}
714
715/** `/downrange pane`: close an open pane, else open it; where nothing draws, the pane is not placed, or a pane call fails, the reason and the table print. */
716async function togglePane($: EngineInterface, columns: number): Promise<CommandRunResult> {
717  try {
718    if ((await $.ui.panes()).some(pane => pane.id === PANE_ID)) {
719      await $.ui.close({ id: PANE_ID })
720      return {}
721    }
722    const hasSurface = (await $.session.surfaces()).length > 0
723    const opened = hasSurface ? await $.ui.open({ id: PANE_ID, title: PANE_TITLE }) : { isPlaced: false as const, reason: 'no surface draws in this session' }
724    if (opened.isPlaced) return {}
725    if (hasSurface) await $.ui.close({ id: PANE_ID })
726    $.ui.log(`command.run: the Jobs pane was not placed: ${opened.reason}`, { to: 'debug' })
727    return { text: `The Jobs pane is not shown here: ${opened.reason}.\n\n${await tableNow($, columns)}` }
728  } catch (error) {
729    $.ui.log(`command.run: the Jobs pane could not be toggled: ${messageOf(error)}`, { to: 'debug' })
730    return { text: `The Jobs pane could not be toggled: ${messageOf(error)}.\n\n${await tableNow($, columns)}` }
731  }
732}
733
734/** The local answer: the drop reason for an exact status prompt while a job runs, or undefined to pass it on. */
735async function statusAnswer($: EngineInterface, text: string): Promise<string | undefined> {
736  const normalized = normalizePrompt(text)
737  if (!isStatusPrompt(normalized)) return undefined
738  const [{ now, jobs, wakeup: { pending: wakeup } }, answered] = await Promise.all([stateNow($), read($, LAST_ANSWERED)])
739  if (!jobs.some(isRunning)) return undefined
740  if (answered !== null && answered.text === normalized && now - answered.at <= REPEAT_WINDOW_MS) {
741    await update($, LAST_ANSWERED, () => null)
742    return undefined
743  }
744  await update($, LAST_ANSWERED, () => ({ text: normalized, at: now }))
745  const seconds = REPEAT_WINDOW_MS / 1_000
746  return `Downrange answered this (not sent to Claude). Send it again within ${seconds} s to ask Claude.\n\n${tableOf(jobs, wakeup, now)}`
747}
748
749export const register: Register = (on, options) => {
750  isAnyRatio = options.genericRatios === true
751  on('session.start', async ($, e, next) => {
752    try {
753      await $.command.register({
754        name: COMMAND,
755        description: 'Background tasks of this session (not background sessions): progress, ETA, last output. "/downrange pane" toggles the live pane.',
756        argumentHint: '[pane]',
757        immediate: true,
758      })
759    } catch (error) {
760      $.ui.log(`downrange: session.start: /downrange was not registered: ${messageOf(error)}`)
761    }
762    await loaded($)
763    // The session that this module first serves shows every ended job that memory holds; a later session.start of the
764    // module keeps the current session as it is. A session id that cannot be read keeps the session out of `sessions`.
765    const id = await $.session.id().catch(() => undefined)
766    if (id !== undefined && !sessions.has(id)) entered(id, since)
767    await ensureTicker($)
768    return next(e)
769  }).catch(($, e, next) => passOn($, 'session.start', e, next))
770
771  on('session.end', async ($, e, next) => {
772    // /clear, /resume, and /branch go on in this process: the next session that this process has not shown starts its
773    // Finished here. Memory stays as it is. Until the mod knows the new session (see switched), the view is a new
774    // session's, which shows no job that ended before the switch, and the ticker runs, so that a tick settles the
775    // switch when no other event comes. A zero-delay timer settles once at once, before the first tick.
776    if (e.reason === 'clear' || e.reason === 'resume') {
777      switchedAt = await $.clock.now()
778      pending = { reason: e.reason, ended: e.sessionId }
779      since = switchedAt
780      await ensureTicker($)
781      $.clock.after(0, () => {
782        void settledAtOnce($)
783      })
784    }
785    return next(e)
786  }).catch(($, e, next) => passOn($, 'session.end', e, next))
787
788  on('classic.SessionStart', { source: ['clear', 'resume', 'fork'] }, async ($, e, next) => {
789    // e.session_id names the session that starts; this hook asks no session id. The engine reset $.state, and the
790    // mirror write here puts the new session's view in it. One mirror write runs at a time, so a write that began
791    // before the switch lands first, and the write after it holds the new session's view (see mirrored). This event
792    // settles the switch, so a command or a /config change waits for its write (see settleLanded). A switch that a hook
793    // settled before it gets the same start here, and memory keeps every change made since (see switched).
794    pending = null
795    entered(e.session_id, sessions.get(e.session_id) ?? switchedAt ?? await $.clock.now())
796    await persisted($, deadlineOf(), true)
797    await ensureTicker($)
798    return next(e)
799  }).catch(($, e, next) => passOn($, 'classic.SessionStart{source=clear|resume|fork}', e, next))
800
801  on('command.run', { command: COMMAND }, async ($, e) => {
802    await switched($, deadlineOf())
803    const argument = e.args.trim()
804    if (argument === '') return { text: await tableNow($, e.presentation.columns) }
805    if (argument === 'pane') return togglePane($, e.presentation.columns)
806    return { text: 'Usage: /downrange [pane]' }
807  }).catch(($, e, next) => {
808    if (isReentry(next.error)) return { text: '/downrange is unavailable during a nested call.' }
809    reportHook($, 'command.run{command=downrange}', next.error)
810    return { text: `/downrange could not run: ${failureText(next.error)}.` }
811  })
812
813  on('command.run', async ($, e, next) => {
814    // Every slash command settles a pending switch before it runs, waits for the write of a settle that is under way,
815    // and writes the view once more when the last mirror write failed, then runs as typed. A command can reload the mod,
816    // such as /reload-plugins: the reload empties memory, and the engine reset $.state at the switch, so the view must be
817    // in $.state first (see switched and settleLanded). With no switch pending, no settle in progress, and no write
818    // behind, this hook asks nothing and waits for nothing.
819    const deadline = deadlineOf()
820    await switched($, deadline)
821    await settleLanded($, deadline)
822    if (mirror.isBehind) await persisted($, deadline)
823    return next(e)
824  }).catch(($, e, next) => passOn($, 'command.run', e, next))
825
826  on('config.set', async ($, e, next) => {
827    // Every /config change settles a pending switch before the row changes, waits for the write of a settle that is
828    // under way, and writes the view once more when the last mirror write failed, then changes the row as asked. A change
829    // of a Downrange option reloads the mod, and no slash command runs then, so the view must be in $.state first (see
830    // switched and settleLanded). This hook never refuses a change and never changes a value. With no switch pending, no
831    // settle in progress, and no write behind, it asks nothing and waits for nothing.
832    const deadline = deadlineOf()
833    await switched($, deadline)
834    await settleLanded($, deadline)
835    if (mirror.isBehind) await persisted($, deadline)
836    return next(e)
837  }).catch(($, e, next) => passOn($, 'config.set', e, next))
838
839  on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
840    const deadline = deadlineOf()
841    await switched($, deadline)
842    const ran = await next(e)
843    if (ran.deny !== undefined || ran.isError === true) return ran
844    const id = ran.result.backgroundTaskId
845    if (id === undefined || id === '') return ran
846    const now = await $.clock.now()
847    const outputFile = outputFileOf(ran.text ?? '', id)
848    if (outputFile === undefined) {
849      report($, 'tool.call', `no output file in the result of task ${id}; its last line is unavailable`, `path|${id}`)
850    }
851    const note = shellLaunchNote(ran.result, outputFile !== undefined)
852    const redirectFile = await redirectFor($, id, e.command, e.agentId)
853    await writeJobs($, deadline, { at: now, ids: [id] }, jobs => launched(jobs, shellJob({ id, command: e.command, description: e.description, outputFile, redirectFile, note, ownerAgentId: e.agentId, endsWithAgent: ran.result.backgroundEndsWithFinalResponse === true }, now)))
854    // The history read comes after the launch, so an end or a clear that comes meanwhile is never undone.
855    const history = await historyFor($, e.command)
856    if (history !== undefined) await writeJobs($, deadline, { at: now, ids: [id] }, jobs => historyAdded(jobs, id, history))
857    return ran
858  }).catch(($, e, next) => passOn($, 'tool.call{tool=Bash}', e, next))
859
860  on('tool.call', { tool: 'Monitor' }, async ($, e, next) => {
861    const deadline = deadlineOf()
862    await switched($, deadline)
863    const ran = await next(e)
864    if (ran.deny !== undefined || ran.isError === true) return ran
865    const now = await $.clock.now()
866    const job = monitorJob({ id: ran.result.taskId, description: e.description, timeoutMs: ran.result.timeoutMs, isPersistent: ran.result.persistent === true }, now)
867    await writeJobs($, deadline, { at: now, ids: [job.id] }, jobs => launched(jobs, job))
868    return ran
869  }).catch(($, e, next) => passOn($, 'tool.call{tool=Monitor}', e, next))
870
871  on('tool.call', { tool: 'Agent' }, async ($, e, next) => {
872    const deadline = deadlineOf()
873    await switched($, deadline)
874    const ran = await next(e)
875    if (ran.deny !== undefined || ran.isError === true) return ran
876    // A foreground call answers `completed`, and a plugin's own call an AgentCallRecord (no status) once the agent settled: nothing runs.
877    const result = ran.result
878    if (!('status' in result) || (result.status !== 'async_launched' && result.status !== 'remote_launched' && result.status !== 'teammate_spawned')) return ran
879    const now = await $.clock.now()
880    // A teammate in a terminal pane of its own runs no loop here: its id is its address in its team.
881    const isOwnPane = result.status === 'teammate_spawned' && (result.agentId ?? result.agent_id) === result.teammate_id
882    const job =
883      result.status === 'teammate_spawned'
884        ? agentJob({ id: result.agentId ?? result.agent_id, description: e.description, ...(isOwnPane ? { note: 'own pane', runsElsewhere: true } : {}) }, now)
885        : result.status === 'async_launched'
886          ? agentJob({ id: result.agentId, description: result.description }, now)
887          : agentJob({ id: result.taskId, description: result.description, note: 'remote', runsElsewhere: true }, now)
888    await writeJobs($, deadline, { at: now, ids: [job.id] }, jobs => launched(jobs, job))
889    return ran
890  }).catch(($, e, next) => passOn($, 'tool.call{tool=Agent}', e, next))
891
892  on('tool.call', { tool: 'Workflow' }, async ($, e, next) => {
893    const deadline = deadlineOf()
894    await switched($, deadline)
895    const ran = await next(e)
896    if (ran.deny !== undefined || ran.isError === true || ran.result.error !== undefined) return ran
897    const now = await $.clock.now()
898    const job = workflowJob({ id: ran.result.taskId, name: ran.result.workflowName }, now)
899    await writeJobs($, deadline, { at: now, ids: [job.id] }, jobs => launched(jobs, job))
900    return ran
901  }).catch(($, e, next) => passOn($, 'tool.call{tool=Workflow}', e, next))
902
903  on('tool.call', { tool: 'ScheduleWakeup' }, async ($, e, next) => {
904    const deadline = deadlineOf()
905    await switched($, deadline)
906    const ran = await next(e)
907    if (ran.deny !== undefined || ran.isError === true) return ran
908    const now = await $.clock.now()
909    const wakeup = ran.result.stopped === true ? null : wakeupOf(ran.result.scheduledFor, e.reason, now)
910    await writeWakeup($, deadline, now, () => wakeup, true)
911    return ran
912  }).catch(($, e, next) => passOn($, 'tool.call{tool=ScheduleWakeup}', e, next))
913
914  on('tool.call', { tool: 'TaskStop' }, async ($, e, next) => {
915    const deadline = deadlineOf()
916    await switched($, deadline)
917    const ran = await next(e)
918    if (ran.deny !== undefined || ran.isError === true) return ran
919    const now = await $.clock.now()
920    const id = ran.result.task_id
921    await writeJobs($, deadline, { at: now, ids: [id] }, jobs => stoppedJob(jobs, id, now))
922    return ran
923  }).catch(($, e, next) => passOn($, 'tool.call{tool=TaskStop}', e, next))
924
925  on('tool.call', async ($, e, next) => {
926    const deadline = deadlineOf()
927    await switched($, deadline)
928    const agentId = e.agentId
929    if (agentId === undefined) return next(e)
930    if (!mirror.isLoaded) await loaded($)
931    if (!memory.jobs.items.some(job => job.kind === 'agent' && job.id === agentId)) return next(e)
932    const started = await $.clock.now()
933    const summary = activityOf(String(e.tool), e)
934    await writeJobs($, deadline, { at: started, ids: [agentId], kind: 'activity' }, jobs => activityStarted(jobs, agentId, summary, started))
935    // A call that rejects counts as failed; the in-flight count drops whatever happens.
936    let isFailure = true
937    try {
938      const ran = await next(e)
939      isFailure = ran.deny !== undefined || ran.isError === true
940      return ran
941    } finally {
942      const settled = await $.clock.now()
943      await writeJobs($, deadline, { at: settled, ids: [agentId], kind: 'activity' }, jobs => activityEnded(jobs, agentId, isFailure, settled))
944    }
945  }).catch(($, e, next) => passOn($, 'tool.call', e, next))
946
947  on('session.append', async ($, e, next) => {
948    const deadline = deadlineOf()
949    await switched($, deadline)
950    if (e.agentId === undefined || e.origin.kind !== 'task-notification') return next(e)
951    const now = await $.clock.now()
952    const stored = await next(e)
953    if (stored.deny !== undefined || stored.uuid !== e.uuid) return stored
954    const text = stored.message.content.flatMap(block => block.type === 'text' && 'text' in block && typeof block.text === 'string' ? [block.text] : []).join('\n')
955    await applyNotices($, deadline, text, JSON.stringify([e.agentId, stored.uuid]), now)
956    return stored
957  }).catch(($, e, next) => passOn($, 'session.append', e, next))
958
959  on('turn.complete', async ($, e, next) => {
960    const deadline = deadlineOf()
961    await switched($, deadline)
962    const now = await $.clock.now()
963    const result = await next(e)
964    const agentId = e.agentId
965    if (agentId !== undefined && e.reason === 'answer' && !e.isAborted) await writeJobs($, deadline, { at: now }, jobs => ownerAnswered(jobs, agentId, now))
966    // A subagent's turn end reads the agent list. A main-loop turn end runs the agent sync of classic.Stop and
967    // classic.SubagentStop too: under managed settings or a Team or Enterprise login, those events do not reach the
968    // mod. Where they do, both syncs read the same list, and the ordering rule keeps the newer reading.
969    if (agentId !== undefined || memory.jobs.items.some(job => job.kind === 'agent')) await syncAgents($, deadline, await $.clock.now(), 'turn.complete')
970    return result
971  }).catch(($, e, next) => passOn($, 'turn.complete', e, next))
972
973  on('prompt.submit', async ($, e, next) => {
974    // The status answer reads the view, so a switch settles before it.
975    const deadline = deadlineOf()
976    await switched($, deadline)
977    if (e.origin.kind === 'task-notification') {
978      await applyNotices($, deadline, e.text)
979    } else if (e.origin.kind === 'scheduled-trigger') {
980      const now = await $.clock.now()
981      await writeWakeup($, deadline, now, wakeup => wakeupAfterTrigger(wakeup, now))
982    } else if (options.answerStatusPrompts === true && e.origin.kind === 'composer' && e.turnId === undefined && e.attachments === undefined) {
983      const answer = await statusAnswer($, e.text)
984      if (answer !== undefined) return { drop: answer }
985    }
986    return next(e)
987  }).catch(($, e, next) => passOn($, 'prompt.submit', e, next))
988
989  on('classic.Stop', async ($, e, next) => {
990    const deadline = deadlineOf()
991    await switched($, deadline)
992    await reconcile($, deadline, 'classic.Stop', e)
993    if (memory.jobs.items.some(job => job.kind === 'agent')) await syncAgents($, deadline, await $.clock.now(), 'classic.Stop')
994    return next(e)
995  }).catch(($, e, next) => passOn($, 'classic.Stop', e, next))
996
997  on('classic.SubagentStop', async ($, e, next) => {
998    const deadline = deadlineOf()
999    await switched($, deadline)
1000    await reconcile($, deadline, 'classic.SubagentStop', e)
1001    if (memory.jobs.items.some(job => job.kind === 'agent')) await syncAgents($, deadline, await $.clock.now(), 'classic.SubagentStop')
1002    return next(e)
1003  }).catch(($, e, next) => passOn($, 'classic.SubagentStop', e, next))
1004
1005  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
1006    if (options.band === false || e.props.hasSurvey) return next(e)
1007    const [{ now, jobs, wakeup: { pending: wakeup } }, hiddenAt] = await Promise.all([stateNow($), read($, BAND_HIDDEN_AT)])
1008    if (!isLive(jobs, wakeup, now) || isBandHidden(jobs, hiddenAt)) return next(e)
1009    const actions = {
1010      open: () => {
1011        void pressed($, 'Open')
1012      },
1013      hide: () => {
1014        void pressed($, 'Hide')
1015      },
1016    }
1017    const parts = bandOf(jobs, wakeup, now, e.props.bodyColumns)
1018    return bandTree($.ui.resolve(e), parts, bandHintOf(parts, e.props.bodyColumns), actions, await next(e))
1019  }).catch(($, e, next) => passOn($, 'ui.render{component=AbovePrompt}', e, next))
1020
1021  on('ui.render', { component: 'Pane', requestId: PANE_ID }, async ($, e) => {
1022    const { now, jobs, wakeup: { pending: wakeup } } = await stateNow($)
1023    const actions = {
1024      clear: () => {
1025        void pressed($, 'Clear finished')
1026      },
1027      close: () => {
1028        void pressed($, 'Close')
1029      },
1030    }
1031    return paneTree($.ui.resolve(e), paneModelOf(jobs, wakeup, now, e.props.bodyColumns), actions)
1032  }).catch(($, e, next) => {
1033    if (isReentry(next.error)) return { type: 'Text', props: {}, children: ['The Jobs pane is unavailable during a nested render.'] }
1034    reportHook($, 'ui.render{component=Pane, requestId=downrange}', next.error)
1035    return failedPaneTree($.ui.resolve(e), `The Jobs pane could not draw: ${failureText(next.error)}`)
1036  })
1037}
1038
hooks/drawing.tsx 122 lines
1import type { ElementTable, RenderElement } from 'claude-code'
2
3import type { BandPart, PaneModel, Row } from './view'
4
5type Ui = Pick<ElementTable, 'Box' | 'Text' | 'Button'>
6
7const PART_STYLES: Readonly<Record<BandPart['tone'], { color?: string; dimColor?: boolean }>> = {
8  plain: {},
9  failed: { color: 'red' },
10  done: { dimColor: true },
11}
12
13/**
14 * The band: one blank row, so the band does not look like the last line of the transcript, then one line of counts
15 * and snippets, the key hint in the free room before Open and Hide when the row holds it, and what other mods drew beneath.
16 */
17export function bandTree(ui: Ui, parts: readonly BandPart[], hint: string | undefined, actions: { open: () => void; hide: () => void }, below: RenderElement): RenderElement {
18  const { Box, Text, Button } = ui
19  return (
20    <Box flexDirection="column">
21      <Box key="band" flexDirection="row" gap={1} marginTop={1}>
22        <Box flexGrow={1}>
23          <Text wrap="truncate-end">
24            {parts.map(part => (
25              <Text {...PART_STYLES[part.tone]}>{part.text}</Text>
26            ))}
27          </Text>
28        </Box>
29        {hint === undefined ? null : (
30          <Text key="hint" dimColor wrap="truncate-end">
31            {hint}
32          </Text>
33        )}
34        <Button key="open" label="Open" hotkey="j" onPress={actions.open} />
35        <Button key="hide" label="Hide" hotkey="h" dimColor onPress={actions.hide} />
36      </Box>
37      {below}
38    </Box>
39  )
40}
41
42const TONE_STYLES: Readonly<Record<Row['tone'], { color?: string; dimColor?: boolean }>> = {
43  running: { color: 'cyan' },
44  completed: { color: 'green' },
45  failed: { color: 'red' },
46  stopped: { color: 'yellow' },
47  gone: { dimColor: true },
48}
49
50const EMPTY = 'No background work yet. Jobs show here when Claude runs a command in the background, starts a Monitor, a background agent, or a workflow.'
51
52function rowTree(ui: Ui, row: Row): RenderElement {
53  const { Box, Text } = ui
54  return (
55    <Box key={`job:${row.id}`} flexDirection="column">
56      <Box flexDirection="row" gap={1}>
57        <Text {...TONE_STYLES[row.tone]}>{row.glyph}</Text>
58        <Text bold wrap="truncate-end">
59          {row.title}
60        </Text>
61        <Text dimColor>{row.kind}</Text>
62        <Text>{row.age}</Text>
63        {row.bar === undefined ? null : <Text color="cyan">{row.bar}</Text>}
64        {row.progress === undefined ? null : <Text>{row.progress}</Text>}
65        {row.eta === undefined ? null : <Text {...(row.isSlow ? { color: 'yellow' } : {})}>{row.eta}</Text>}
66        {row.flag === undefined ? null : <Text color="yellow">{row.flag}</Text>}
67        {row.note === undefined ? null : <Text dimColor>{row.note}</Text>}
68      </Box>
69      {row.last === undefined ? null : (
70        <Text dimColor wrap="truncate-end">
71          {`  └ ${row.last}`}
72        </Text>
73      )}
74    </Box>
75  )
76}
77
78/** The pane: counts and buttons, then running and finished jobs; the empty text only when it has nothing to list. */
79export function paneTree(ui: Ui, model: PaneModel, actions: { clear: () => void; close: () => void }): RenderElement {
80  const { Box, Text, Button } = ui
81  const isEmpty = model.header === '' && model.running.length === 0 && model.ended.length === 0
82  return (
83    <Box flexDirection="column">
84      <Box key="header" flexDirection="row" gap={1}>
85        <Box flexGrow={1}>
86          {isEmpty ? (
87            <Text wrap="wrap">{EMPTY}</Text>
88          ) : (
89            <Text bold wrap="truncate-end">
90              {model.header}
91            </Text>
92          )}
93        </Box>
94        <Button key="clear" label="Clear finished" hotkey="c" dimColor onPress={actions.clear} />
95        <Button key="close" label="Close" hotkey="x" role="dismiss" onPress={actions.close} />
96      </Box>
97      {model.running.length === 0 ? null : (
98        <Text bold dimColor>
99          Running
100        </Text>
101      )}
102      {model.running.map(row => rowTree(ui, row))}
103      {model.ended.length === 0 ? null : (
104        <Text bold dimColor>
105          Finished
106        </Text>
107      )}
108      {model.ended.map(row => rowTree(ui, row))}
109    </Box>
110  )
111}
112
113/** What the pane draws when its hook failed: the cause, in red. */
114export function failedPaneTree(ui: Ui, text: string): RenderElement {
115  const { Text } = ui
116  return (
117    <Text color="red" wrap="wrap">
118      {text}
119    </Text>
120  )
121}
122
hooks/history.ts 42 lines
1import { MAX_KEYS, MAX_RUNS } from './limits'
2
3/** Past run durations by history key, as the store keeps them. */
4export type Durations = Record<string, { runs: number[]; at: number }>
5
6const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
7
8const isEntry = (value: unknown): boolean =>
9  isRecord(value) && typeof value.at === 'number' && Array.isArray(value.runs) && value.runs.every(run => typeof run === 'number')
10
11const isDurations = (value: unknown): value is Durations => isRecord(value) && Object.values(value).every(isEntry)
12
13/** The NUL character that separates folder and command in a history key, built from its code so that no string literal holds a control character. */
14const NUL = String.fromCharCode(0)
15
16/** The first 16 hex characters of SHA-256 over folder, NUL, and command: no command text is kept. */
17export async function historyKeyOf(cwd: string, command: string): Promise<string> {
18  const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(`${cwd}${NUL}${command}`)))
19  return [...digest.slice(0, 8)].map(byte => byte.toString(16).padStart(2, '0')).join('')
20}
21
22/** The stored durations: {} when nothing is stored, undefined for a value of the wrong shape. */
23export function durationsOf(value: unknown): Durations | undefined {
24  if (value === undefined) return {}
25  return isDurations(value) ? value : undefined
26}
27
28/** The median of a key's runs, or undefined when it has none. */
29export function usualOf(durations: Durations, key: string): number | undefined {
30  const runs = [...(durations[key]?.runs ?? [])].sort((a, b) => a - b)
31  if (runs.length === 0) return undefined
32  const middle = runs.slice(Math.floor((runs.length - 1) / 2), Math.floor(runs.length / 2) + 1)
33  return middle.reduce((sum, run) => sum + run, 0) / middle.length
34}
35
36/** The durations with one more run for key: MAX_RUNS runs per key, the MAX_KEYS most recent keys. */
37export function recorded(durations: Durations, key: string, durationMs: number, now: number): Durations {
38  const runs = [...(durations[key]?.runs ?? []), durationMs].slice(-MAX_RUNS)
39  const entries = Object.entries({ ...durations, [key]: { runs, at: now } })
40  return Object.fromEntries(entries.sort(([, a], [, b]) => b.at - a.at).slice(0, MAX_KEYS))
41}
42
hooks/jobs.ts 823 lines
1import type { AgentInfo, ClassicEventOf } from 'claude-code'
2
3import type { AgentJob, Job, JobsState, JobState, MonitorJob, Progress, ShellJob, Wakeup, WorkflowJob } from '../types'
4import {
5  ACTIVITY_MAX,
6  LARGE_FILE_BYTES,
7  LARGE_READ_EVERY_MS,
8  LINE_MAX,
9  MAX_BARRIERS,
10  MAX_FINISHED,
11  NOTE_MAX,
12  NOTICE_ROWS_MAX,
13  PROGRESS_LINES,
14  READ_EVERY_MS,
15  RECENT_MS,
16  SNAPSHOT_SLACK_MS,
17  STATUS_WORD_MAX,
18  TITLE_MAX,
19  TRIGGER_EARLY_MS,
20  WAKEUP_STALE_MS,
21} from './limits'
22import type { Notice } from './notices'
23import { anchorOf, clip, displayLinesOf, isOutputFileOf, jobLinesOf, oneLine, progressOf } from './output'
24
25/** The launch note of a command that its synchronous subagent ends when it answers. */
26const ENDS_WITH_AGENT = 'ends with its agent'
27/** The note of a job that left the engine's list with no notification. */
28const GONE_NOTE = 'ended without a report'
29/** The same, for a command that ended with its synchronous subagent. */
30const AGENT_GONE_NOTE = 'ended with its agent'
31/** Snapshot notes, which a later notification replaces. */
32const GONE_NOTES: ReadonlySet<string> = new Set([GONE_NOTE, AGENT_GONE_NOTE])
33
34/** Whether the job still runs. */
35export const isRunning = (job: Job): boolean => job.state === 'running'
36
37const titled = (text: string): string => clip(oneLine(text), TITLE_MAX)
38
39/** Keeps every running job and the MAX_FINISHED newest ended jobs, in their order. */
40export function trimmed(jobs: readonly Job[]): Job[] {
41  const kept = new Set(
42    jobs
43      .filter(job => !isRunning(job))
44      .sort((a, b) => (b.endedAt ?? 0) - (a.endedAt ?? 0))
45      .slice(0, MAX_FINISHED)
46      .map(job => job.id),
47  )
48  return jobs.filter(job => isRunning(job) || kept.has(job.id))
49}
50
51/** Kinds whose task id never runs again after its end; an agent can resume under its id. */
52const FINAL_KINDS: ReadonlySet<Job['kind']> = new Set(['shell', 'monitor', 'workflow'])
53
54/**
55 * Whether a notification states a final end: a known end word other than `blocked`, which a remote task can leave.
56 * A task of type `remote_agent`, such as a remote workflow, keeps the ordering rule of other inputs.
57 */
58function isFinalNotice(notice: Notice): boolean {
59  return notice.taskType !== 'remote_agent' && endStateOf(notice.status) !== undefined && notice.status?.toLowerCase() !== 'blocked'
60}
61
62/** Whether a row takes a final end whatever the mod saw since the end's stamp: a running or `gone` shell, Monitor, or workflow. */
63const takesFinalEnd = (job: Job): boolean => FINAL_KINDS.has(job.kind) && (isRunning(job) || job.state === 'gone')
64
65/** Whether this notification's end applies to the job whatever the mod saw since its stamp. */
66function isFinalEnd(job: Job, notice: Notice): boolean {
67  return takesFinalEnd(job) && isFinalNotice(notice)
68}
69
70/** The ids of a prompt's blocks that state a final end. */
71export function finalIdsOf(notices: readonly Notice[]): string[] {
72  return notices.filter(isFinalNotice).flatMap(notice => notice.ids)
73}
74
75/** Whether an end is at least as recent as the job's latest running observation and end. */
76function canEnd(job: Job, now: number): boolean {
77  return now >= Math.max(job.lastRunningAt ?? job.startedAt, job.lastEndedAt ?? job.startedAt)
78}
79
80/** A current or final end preserves the first end time, advances the latest, replaces snapshot notes, and asks for a final shell read. */
81function endedJob<T extends Job>(job: T, state: JobState, now: number, detail: { exitCode?: number; note?: string }, isFinal = false): T {
82  if (!isFinal && !canEnd(job, now)) return job
83  return {
84    ...job,
85    state,
86    omittedAt: undefined,
87    endedAt: Math.min(job.endedAt ?? now, now),
88    lastEndedAt: Math.max(job.lastEndedAt ?? now, now),
89    exitCode: detail.exitCode ?? job.exitCode,
90    note: detail.note ?? (job.note !== undefined && GONE_NOTES.has(job.note) ? undefined : job.note),
91    ...(job.kind === 'shell' ? { finalReadAskedAt: now } : {}),
92  }
93}
94
95/** The list with this job added, or replacing the one with its id. */
96function withJob(jobs: readonly Job[], job: Job): Job[] {
97  return [...jobs.filter(one => one.id !== job.id), job]
98}
99
100/** Refines an inferred shell without carrying shell-only metadata into the Monitor. */
101function asMonitor(job: ShellJob): MonitorJob {
102  const { kind, outputFile, redirectFile, redirectBytes, outputBytes, failedReadBytes, lastReadAt, successfulReadAt, finalReadAskedAt, usualMs, historyKey, ownerAgentId, endsWithAgent, ...details } = job
103  return { ...details, kind: 'monitor', eventCount: 0, note: job.note === 'no output file' || READ_NOTES.has(job.note ?? '') ? undefined : job.note }
104}
105
106/** A launch's job laid over a job of its kind that a notification, a Monitor event, or a snapshot brought first: the launch adds what only it knows, and what the mod learned since stays. */
107function withLaunch(known: Job, launch: Job): Job {
108  if (known.kind === 'shell' && launch.kind === 'monitor') known = { ...launch, ...asMonitor(known) }
109  if (known.kind === 'task' && launch.kind !== 'task') {
110    const { kind, taskType, ...details } = known
111    known = { ...launch, ...details }
112    if (known.kind === 'shell' && !isRunning(known)) known.finalReadAskedAt = known.lastEndedAt ?? known.endedAt
113  }
114  const identity = { title: launch.title, startedAt: launch.startedAt, isStartSeen: true, omittedAt: undefined, note: known.note ?? launch.note,
115    launchNote: launch.launchNote ?? known.launchNote }
116  if (known.kind === 'shell' && launch.kind === 'shell') return { ...known, ...identity, outputFile: launch.outputFile ?? known.outputFile,
117    redirectFile: launch.redirectFile ?? known.redirectFile,
118    ownerAgentId: launch.ownerAgentId ?? known.ownerAgentId, endsWithAgent: launch.endsWithAgent ?? known.endsWithAgent }
119  if (known.kind === 'agent' && launch.kind === 'agent') return { ...known, ...identity, runsElsewhere: launch.runsElsewhere ?? known.runsElsewhere }
120  return known.kind === 'monitor' && launch.kind === 'monitor' ? { ...known, ...identity, deadline: launch.deadline } : { ...known, ...identity }
121}
122
123/** The list after a launch; observed state stays and kinds can be refined. */
124export function launched(jobs: readonly Job[], launch: Job): Job[] {
125  const known = jobs.find(job => job.id === launch.id)
126  if (known === undefined) return withJob(jobs, launch)
127  const refinesShell = known.kind === 'shell' && !known.isStartSeen && launch.kind === 'monitor'
128  return known.kind === launch.kind || known.kind === 'task' || refinesShell ? withJob(jobs, withLaunch(known, launch)) : [...jobs]
129}
130
131/** A background shell command, as its launch result describes it. */
132export function shellJob(launch: { id: string; command: string; description?: string; outputFile?: string; redirectFile?: string; note?: string; ownerAgentId?: string; endsWithAgent?: boolean }, now: number): ShellJob {
133  return {
134    kind: 'shell',
135    id: launch.id,
136    title: titled(launch.description || launch.command),
137    state: 'running',
138    startedAt: now,
139    isStartSeen: true,
140    outputFile: launch.outputFile,
141    redirectFile: launch.redirectFile,
142    ownerAgentId: launch.ownerAgentId,
143    endsWithAgent: launch.endsWithAgent,
144    note: launch.note,
145    launchNote: launch.note,
146  }
147}
148
149/** The list with a launch's history key and usual duration on the shell job that holds its id; a job the list no longer holds is not added back. */
150export function historyAdded(jobs: readonly Job[], id: string, history: { historyKey: string; usualMs?: number }): Job[] {
151  return jobs.map(job => (job.kind === 'shell' && job.id === id ? { ...job, historyKey: history.historyKey, usualMs: history.usualMs } : job))
152}
153
154/** The note a shell launch shows: a missing output file first, then an end tied to a subagent, then how the command went to the background. */
155export function shellLaunchNote(
156  result: { timedOutAfterMs?: number; backgroundedByUser?: boolean; backgroundEndsWithFinalResponse?: boolean },
157  hasOutputFile: boolean,
158): string | undefined {
159  if (!hasOutputFile) return 'no output file'
160  if (result.backgroundEndsWithFinalResponse === true) return ENDS_WITH_AGENT
161  if (result.timedOutAfterMs !== undefined) return 'moved after timeout'
162  return result.backgroundedByUser === true ? 'backgrounded by you' : undefined
163}
164
165/** A Monitor watch; a non-persistent one ends at its deadline. */
166export function monitorJob(launch: { id: string; description: string; timeoutMs: number; isPersistent: boolean }, now: number): MonitorJob {
167  return {
168    kind: 'monitor',
169    id: launch.id,
170    title: titled(launch.description),
171    state: 'running',
172    startedAt: now,
173    isStartSeen: true,
174    eventCount: 0,
175    deadline: launch.timeoutMs > 0 && !launch.isPersistent ? now + launch.timeoutMs : undefined,
176  }
177}
178
179/** A background or remote subagent. */
180export function agentJob(launch: { id: string; description: string; note?: string; runsElsewhere?: boolean }, now: number): AgentJob {
181  return {
182    kind: 'agent',
183    id: launch.id,
184    title: titled(launch.description),
185    state: 'running',
186    startedAt: now,
187    isStartSeen: true,
188    runsElsewhere: launch.runsElsewhere,
189    toolCount: 0,
190    inFlight: 0,
191    errorStreak: 0,
192    note: launch.note,
193    launchNote: launch.note,
194  }
195}
196
197/** A background workflow run. */
198export function workflowJob(launch: { id: string; name?: string }, now: number): WorkflowJob {
199  return { kind: 'workflow', id: launch.id, title: titled(launch.name || 'workflow'), state: 'running', startedAt: now, isStartSeen: true }
200}
201
202/** The pending wakeup of a dynamic /loop, set at `now`. */
203export function wakeupOf(dueAt: number, reason: string | undefined, now: number): Wakeup {
204  return { dueAt, reason: clip(oneLine(reason || 'wakeup'), NOTE_MAX), setAt: now }
205}
206
207/** The list after TaskStop stopped the job with this id; an ended job stays as it is. */
208export function stoppedJob(jobs: readonly Job[], id: string, now: number): Job[] {
209  return jobs.map(job => (job.id === id && isRunning(job) ? endedJob(job, 'stopped', now, {}) : job))
210}
211
212/** One in-flight task in a Stop or SubagentStop snapshot. */
213export type SnapshotTask = NonNullable<ClassicEventOf['classic.Stop']['background_tasks']>[number]
214
215/** One finished run for the duration history. */
216export type HistoryRun = { key: string; durationMs: number }
217
218/** The end states of the engine's task status words; a remote task blocked on input has ended for this session, and the engine marks it failed. */
219const NOTICE_STATES: ReadonlyMap<string, JobState> = new Map([
220  ['completed', 'completed'],
221  ['failed', 'failed'],
222  ['blocked', 'failed'],
223  ['killed', 'stopped'],
224  ['stopped', 'stopped'],
225  ['cancelled', 'stopped'],
226])
227const SNAPSHOT_KINDS: ReadonlyMap<string, Job['kind']> = new Map([
228  ['shell', 'shell'],
229  ['subagent', 'agent'],
230  ['monitor', 'monitor'],
231  ['workflow', 'workflow'],
232])
233
234/** The state a notification status ends a job in, in any letter case, or undefined for another word or none. */
235function endStateOf(status: string | undefined): JobState | undefined {
236  return status === undefined ? undefined : NOTICE_STATES.get(status.toLowerCase())
237}
238
239/** The quote that closes a description: the engine's own words follow it, as in `"build" completed (exit code 0)` or `"watch" script failed`. */
240const DESCRIPTION_END = /" (?:completed|failed|finished|stopped|was|is|script)\b/g
241
242/**
243 * A summary split at its quoted description, `<prefix> "<description>" <engine words>`, so the description's
244 * words cannot pose as the engine's own. A matching known title decides the boundary. Otherwise a reason matching
245 * the notification status starts after the description; without one, the last quote followed by an engine verb closes it.
246 */
247function summaryParts(notice: Notice, title?: string): { description?: string; words: string } {
248  const text = notice.summary ?? ''
249  const state = endStateOf(notice.status)
250  const open = text.indexOf('"')
251  const verbs = [...text.matchAll(DESCRIPTION_END)].filter(match => match.index > open)
252  const known = title === undefined ? undefined : verbs.find(match => text.slice(open + 1, match.index) === title)
253  const reason = verbs.find(match =>
254    state === 'failed' && /^" (?:failed|is blocked): /.test(text.slice(match.index)) ||
255    state === 'stopped' && /^" was stopped: /.test(text.slice(match.index)))
256  const close = (known ?? reason ?? verbs.at(-1))?.index ?? text.lastIndexOf('"')
257  return open === -1 || close <= open ? { words: text } : { description: text.slice(open + 1, close), words: text.slice(close + 1) }
258}
259
260/** The exit code a summary's engine words state, `exit code N` or `(exit N)`; the last one wins. */
261function exitCodeOf(words: string): number | undefined {
262  const code = [...words.matchAll(/exit code (\d+)|\(exit (\d+)\)/g)].at(-1)
263  return code === undefined ? undefined : Number(code[1] ?? code[2])
264}
265
266/**
267 * The end detail a notification's summary states: a known end cause or a failure reason. A block's `<note>` is not
268 * read: the engine puts its guidance for Claude there, not an end detail.
269 */
270function endNoteOf(summary: string): string | undefined {
271  if (summary.includes('background time limit')) return 'time limit'
272  if (summary.includes('low on memory')) return 'low memory'
273  if (summary.includes('-turn limit')) return 'turn limit'
274  const reason = /(?:failed|was stopped|is blocked): ([\s\S]+)$/.exec(summary)?.[1]
275  if (reason !== undefined) return clip(oneLine(reason), NOTE_MAX)
276  return summary.includes('previous session') ? 'previous session' : undefined
277}
278
279/** A job's kind from the summary's start, as the engine words its notices: a previous session's restored tasks start `Background shell command`, `Background agent`, or `Background workflow`. */
280function kindOf(summary: string): Job['kind'] {
281  if (/^Background (?:shell )?command/.test(summary)) return 'shell'
282  if (/^(?:Background agent|Agent)/.test(summary)) return 'agent'
283  if (summary.startsWith('Monitor')) return 'monitor'
284  return /^(?:Background workflow|Dynamic workflow|Workflow)/.test(summary) ? 'workflow' : 'task'
285}
286
287/** The fields of a job the mod first sees running or ended, with no launch result. */
288function unseen(id: string, title: string, now: number) {
289  return { id, title: titled(title), state: 'running' as const, startedAt: now, isStartSeen: false }
290}
291
292function freshMonitor(id: string, title: string, now: number): MonitorJob {
293  return { kind: 'monitor', ...unseen(id, title, now), eventCount: 0 }
294}
295
296function freshJob(kind: Job['kind'], id: string, title: string, now: number): Job {
297  switch (kind) {
298    case 'shell':
299      return { kind, ...unseen(id, title, now) }
300    case 'monitor':
301      return freshMonitor(id, title, now)
302    case 'agent':
303      return { kind, ...unseen(id, title, now), toolCount: 0, inFlight: 0, errorStreak: 0 }
304    case 'workflow':
305      return { kind, ...unseen(id, title, now) }
306    case 'task':
307      return { kind, ...unseen(id, title, now), taskType: 'task' }
308  }
309}
310
311function ended(jobs: readonly Job[], notice: Notice, state: JobState, now: number): Job[] {
312  return notice.ids.reduce<Job[]>((list, id) => {
313    let known = list.find(job => job.id === id) ?? freshJob(kindOf(notice.summary ?? ''), id, summaryParts(notice).description || id, now)
314    if (known.kind === 'shell' && !known.isStartSeen && kindOf(notice.summary ?? '') === 'monitor') known = asMonitor(known)
315    const { words } = summaryParts(notice, known.title)
316    const detail = { exitCode: exitCodeOf(words), note: endNoteOf(words) }
317    return withJob(list, endedJob(known, state, now, detail, isFinalEnd(known, notice)))
318  }, [...jobs])
319}
320
321function monitorEvent(jobs: readonly Job[], notice: Notice, event: string, now: number, isAnyRatio: boolean): Job[] {
322  const lines = displayLinesOf(event)
323  const progress = progressOf(lines.slice(-PROGRESS_LINES), now, isAnyRatio)
324  const lastLine = lines.at(-1)
325  return notice.ids.reduce<Job[]>((list, id) => {
326    let known = list.find(job => job.id === id)
327    if (known?.kind === 'shell' && !known.isStartSeen) {
328      known = asMonitor(known)
329    }
330    if (known !== undefined && known.kind !== 'monitor') return list
331    const current = known ?? freshMonitor(id, summaryParts(notice).description || id, now)
332    const monitor = { ...current, eventCount: current.eventCount + 1 }
333    return withJob(list, {
334      ...monitor,
335      omittedAt: undefined,
336      lastOutputAt: now,
337      lastLine: lastLine === undefined ? monitor.lastLine : clip(lastLine, LINE_MAX),
338      progress: progress ?? monitor.progress,
339      anchor: progress === undefined ? monitor.anchor : anchorOf(monitor.anchor, progress, monitor.progress),
340    })
341  }, [...jobs])
342}
343
344/** Applies the status and output in a notification; isAnyRatio (the `genericRatios` option) lets any ratio in a Monitor event count as progress. */
345export function noticed(jobs: readonly Job[], notice: Notice, now: number, isAnyRatio = false): Job[] {
346  const state = endStateOf(notice.status)
347  const updated = state === undefined ? [...jobs] : ended(jobs, notice, state, now)
348  const refined = notice.event === undefined ? updated : monitorEvent(updated, notice, notice.event, now, isAnyRatio)
349  return refined.map(job => {
350    if (job.kind !== 'shell' || !notice.ids.includes(job.id) || notice.outputFile === undefined || !isOutputFileOf(notice.outputFile, job.id)) return job
351    return { ...job, outputFile: notice.outputFile, note: job.note === 'no output file' ? undefined : job.note,
352      launchNote: job.launchNote === 'no output file' ? undefined : job.launchNote, finalReadAskedAt: isRunning(job) ? job.finalReadAskedAt : now }
353  })
354}
355
356/**
357 * Why a block that names a task changes no job: a status word the mod does not know. A block with no task id or no
358 * status is the engine's other news (an artifact's event, a stalled command, an agent resumed), which changes no job
359 * and is no problem.
360 */
361export function noticeProblemOf(notice: Notice): { cause: string; once: string } | undefined {
362  const id = notice.ids[0]
363  if (id === undefined || notice.status === undefined || endStateOf(notice.status) !== undefined) return undefined
364  const word = clip(oneLine(notice.status), STATUS_WORD_MAX)
365  return { cause: `unknown notification status "${word}" for task ${id}`, once: `status|${word}` }
366}
367
368/**
369 * The runs one block ends cleanly: completed, exit code 0 or none, a shell job still running, seen from launch, with a
370 * history key, and an end no earlier than its start.
371 */
372function cleanRunsOf(jobs: readonly Job[], notice: Notice, now: number): HistoryRun[] {
373  if (endStateOf(notice.status) !== 'completed') return []
374  return jobs.flatMap(job =>
375    job.kind === 'shell' && isRunning(job) && (canEnd(job, now) || isFinalEnd(job, notice)) && now >= job.startedAt && job.isStartSeen && job.historyKey !== undefined && notice.ids.includes(job.id) &&
376    (exitCodeOf(summaryParts(notice, job.title).words) ?? 0) === 0
377      ? [{ key: job.historyKey, durationMs: now - job.startedAt }]
378      : [],
379  )
380}
381
382/** The runs a prompt's blocks end cleanly; each block meets the list as the blocks before it left it. */
383export function finishedRuns(jobs: readonly Job[], notices: readonly Notice[], now: number): HistoryRun[] {
384  const runs: HistoryRun[] = []
385  let list = jobs
386  for (const notice of notices) {
387    runs.push(...cleanRunsOf(list, notice, now))
388    list = noticed(list, notice, now)
389  }
390  return runs
391}
392
393function discoveredJob(task: SnapshotTask, now: number): Job {
394  const job = { ...freshJob(SNAPSHOT_KINDS.get(task.type) ?? 'task', task.id, task.name || task.description || task.command || task.id, now), lastRunningAt: now }
395  return job.kind === 'task' ? { ...job, taskType: task.type } : job
396}
397
398/**
399 * A job that a snapshot marked gone and a later snapshot lists: it runs again, without its end time, and with the launch
400 * note its snapshot note replaced. A row with no launch note, such as one that an earlier version saved, turns
401 * `ended with its agent` back into `ends with its agent`, and shows no note after another end.
402 */
403function revived(job: Job, now: number): Job {
404  return { ...job, state: 'running', lastRunningAt: now, omittedAt: undefined, endedAt: undefined,
405    note: job.launchNote ?? (job.note === AGENT_GONE_NOTE ? ENDS_WITH_AGENT : undefined) }
406}
407
408/** Reconciles running tasks, allowing a grace period before an absence ends a job. */
409export function reconciled(jobs: readonly Job[], snapshot: readonly SnapshotTask[], now: number): Job[] {
410  const listed = new Set(snapshot.map(task => task.id))
411  const known = new Set(jobs.map(job => job.id))
412  const updated = jobs.map(job => {
413    if (isRunning(job) && !listed.has(job.id) && job.startedAt <= now - SNAPSHOT_SLACK_MS && (job.lastRunningAt === undefined || job.lastRunningAt < now)) {
414      return endedJob(job, 'gone', now, { note: job.note === ENDS_WITH_AGENT ? AGENT_GONE_NOTE : GONE_NOTE })
415    }
416    if (!listed.has(job.id)) return isRunning(job) && now > (job.lastRunningAt ?? job.startedAt) && now >= (job.omittedAt ?? -Infinity) ? { ...job, omittedAt: now } : job
417    if (isRunning(job)) return { ...job, omittedAt: undefined, lastRunningAt: Math.max(job.lastRunningAt ?? job.startedAt, now) }
418    return job.state === 'gone' && job.lastEndedAt !== undefined && job.lastEndedAt < now ? revived(job, now) : job
419  })
420  const discovered = snapshot.filter(task => !known.has(task.id)).map(task => discoveredJob(task, now))
421  return [...updated, ...discovered]
422}
423
424/**
425 * What one read of a job's output found. exitCode is the code on Claude Code's closing `[exited with code N]` line,
426 * when the read took the end of the output file and that end is the closing line: the command ended.
427 */
428export type TailRead = { bytes: number; mtimeMs: number; lines: readonly string[]; redirectBytes?: number; exitCode?: number }
429
430/**
431 * A running shell that the closing line of its output file ended, at a read that began at `now`: completed for exit
432 * code 0, else failed, with that code. Claude Code writes that line when the command ends, so the end needs no
433 * notification and no turn-end snapshot. The read was the job's final read, so the end asks for no other one.
434 */
435function closed(job: ShellJob, exitCode: number | undefined, now: number): ShellJob {
436  if (exitCode === undefined || !isRunning(job)) return job
437  const end = endedJob(job, exitCode === 0 ? 'completed' : 'failed', now, { exitCode })
438  return end === job ? job : { ...end, finalReadAskedAt: job.finalReadAskedAt }
439}
440
441/**
442 * The run that a read's closing line ends cleanly, as cleanRunsOf does for a notification: exit code 0, a shell job
443 * still running, seen from launch, with a history key, and an end no earlier than its start. The notification that
444 * comes later finds the row ended, so it adds no second run.
445 */
446export function closedRuns(jobs: readonly Job[], id: string, exitCode: number | undefined, now: number): HistoryRun[] {
447  if (exitCode !== 0) return []
448  return jobs.flatMap(job =>
449    job.kind === 'shell' && job.id === id && isRunning(job) && canEnd(job, now) && now >= job.startedAt && job.isStartSeen && job.historyKey !== undefined
450      ? [{ key: job.historyKey, durationMs: now - job.startedAt }]
451      : [],
452  )
453}
454
455/** Notes a successful read clears. */
456const READ_NOTES: ReadonlySet<string> = new Set(['output unavailable', 'tail failed'])
457
458/** The states of the agent list's status words. */
459const AGENT_STATES: ReadonlyMap<string, JobState> = new Map([['pending', 'running'], ['running', 'running'], ['waiting', 'running'], ['idle', 'running'], ...NOTICE_STATES])
460
461type Detail = { field: string; shape: 'text' | 'name' | 'host' }
462
463/** The input field each tool's activity summary shows: a file name, a host, or the text itself. */
464const DETAILS: ReadonlyMap<string, Detail> = new Map([
465  ['Bash', { field: 'command', shape: 'text' }],
466  ['Monitor', { field: 'command', shape: 'text' }],
467  ['Read', { field: 'file_path', shape: 'name' }],
468  ['Edit', { field: 'file_path', shape: 'name' }],
469  ['Write', { field: 'file_path', shape: 'name' }],
470  ['NotebookEdit', { field: 'notebook_path', shape: 'name' }],
471  ['Grep', { field: 'pattern', shape: 'text' }],
472  ['Glob', { field: 'pattern', shape: 'text' }],
473  ['WebFetch', { field: 'url', shape: 'host' }],
474  ['WebSearch', { field: 'query', shape: 'text' }],
475  ['Agent', { field: 'description', shape: 'text' }],
476])
477
478function detailOf(value: string, shape: Detail['shape']): string {
479  if (shape === 'name') return value.split(/[\\/]/).pop() ?? value
480  if (shape === 'host') return URL.canParse(value) ? new URL(value).host : value
481  return value
482}
483
484/**
485 * The list after an agent list read in a tick that began at `now`: its status decides each agent job's state. A resumed
486 * agent runs again without its end time, and its launch note replaces its end note, but only when its latest end came
487 * before the tick: a later end may postdate the list.
488 */
489export function agentsSynced(jobs: readonly Job[], agents: readonly AgentInfo[], now: number): Job[] {
490  return jobs.map(job => {
491    if (job.kind !== 'agent') return job
492    const status = agents.find(agent => agent.id === job.id)?.status
493    const state = status === undefined ? undefined : AGENT_STATES.get(status)
494    if (state === undefined) return job
495    const activityStatus = status as AgentJob['activityStatus']
496    if (state === 'running' && isRunning(job)) return { ...job, activityStatus, omittedAt: undefined, lastRunningAt: Math.max(job.lastRunningAt ?? job.startedAt, now) }
497    if (state === job.state || now <= (job.lastRunningAt ?? job.startedAt)) return job
498    if (state !== 'running') return endedJob(job, state, now, {})
499    return job.lastEndedAt !== undefined && job.lastEndedAt < now ? { ...job, state, activityStatus, lastRunningAt: now, omittedAt: undefined, endedAt: undefined, note: job.launchNote } : job
500  })
501}
502
503/** `<tool>: <detail>`, or the tool alone, as one line of at most ACTIVITY_MAX characters. */
504export function activityOf(tool: string, input: object): string {
505  const detail = DETAILS.get(tool)
506  const value: unknown = detail === undefined ? undefined : Reflect.get(input, detail.field)
507  const text = detail !== undefined && typeof value === 'string' && value.trim() !== '' ? `${tool}: ${detailOf(value, detail.shape)}` : tool
508  return clip(oneLine(text), ACTIVITY_MAX)
509}
510
511/** The list after a tracked subagent started a tool call. */
512export function activityStarted(jobs: readonly Job[], agentId: string, summary: string, now: number): Job[] {
513  return jobs.map(job =>
514    job.kind === 'agent' && job.id === agentId
515      ? {
516          ...job,
517          toolCount: job.toolCount + 1,
518          inFlight: job.inFlight + 1,
519          lastLine: now >= (job.lastToolStartedAt ?? -Infinity) ? summary : job.lastLine,
520          lastToolStartedAt: Math.max(job.lastToolStartedAt ?? now, now),
521          lastOutputAt: Math.max(job.lastOutputAt ?? now, now),
522        }
523      : job,
524  )
525}
526
527/** Settled calls count in observation order. Concurrent failures retain their times until a success or no call remains. */
528export function activityEnded(jobs: readonly Job[], agentId: string, isFailure: boolean, now: number): Job[] {
529  return jobs.map(job => {
530    if (job.kind !== 'agent' || job.id !== agentId) return job
531    const inFlight = Math.max(0, job.inFlight - 1)
532    const previous = job.concurrentFailureTimes ?? []
533    const lastSuccessAt = isFailure ? job.lastSuccessAt : Math.max(job.lastSuccessAt ?? now, now)
534    const failures = isFailure
535      ? now >= (lastSuccessAt ?? -Infinity) ? [...previous, now] : previous
536      : previous.filter(at => at > lastSuccessAt!)
537    // The unrecorded part precedes these concurrent calls and every success resets it.
538    const errorStreak = (isFailure ? job.errorStreak - previous.length : 0) + failures.length
539    return {
540      ...job,
541      inFlight,
542      errorStreak,
543      concurrentFailureTimes: inFlight === 0 ? undefined : failures,
544      lastSuccessAt: inFlight === 0 ? undefined : lastSuccessAt,
545      lastOutputAt: Math.max(job.lastOutputAt ?? now, now),
546    }
547  })
548}
549
550/**
551 * Shell jobs whose output the ticker reads now: a pending final read, or a running job not read for READ_EVERY_MS
552 * (LARGE_READ_EVERY_MS once a file it reads, its output or the redirect file the row shows, passed LARGE_FILE_BYTES).
553 */
554export function readsDue(jobs: readonly Job[], now: number): (ShellJob & { outputFile: string })[] {
555  return jobs.filter(
556    (job): job is ShellJob & { outputFile: string } =>
557      job.kind === 'shell' &&
558      job.outputFile !== undefined &&
559      (job.finalReadAskedAt !== undefined ||
560        (isRunning(job) && now - (job.lastReadAt ?? 0) >= ((job.failedReadBytes ?? Math.max(job.redirectBytes ?? 0, job.outputBytes ?? 0)) > LARGE_FILE_BYTES ? LARGE_READ_EVERY_MS : READ_EVERY_MS))),
561  )
562}
563
564/** An end's final-read request that a read beginning at `readAt` leaves open: one made at or after that time. */
565const askedSince = (job: ShellJob, readAt: number): number | undefined =>
566  job.finalReadAskedAt !== undefined && job.finalReadAskedAt >= readAt ? job.finalReadAskedAt : undefined
567
568/** Whether two readings show the same value, both from a known format or both generic. A reading with no mark, which an earlier version saved, matches either. */
569const isSameReading = (a: Progress | undefined, b: Progress | undefined): boolean =>
570  a !== undefined && b !== undefined && a.label === b.label && a.percent === b.percent &&
571  (a.isGeneric === undefined || b.isGeneric === undefined || a.isGeneric === b.isGeneric)
572
573/** Whether a reading or an anchor stays with isAnyRatio off: it is absent, or marked as a known format. One with no mark came from an earlier version, which counted any ratio. */
574const isKnownOrAbsent = (value: { isGeneric?: boolean } | undefined): boolean => value === undefined || value.isGeneric === false
575
576/**
577 * The list after a reload of the mod at `now`, as a change of the `genericRatios` option or an update causes. With
578 * isAnyRatio off, a reading or an anchor that is generic or has no mark drops, and an anchor drops with its reading.
579 * Each running shell with an output file reads it once more in full, so its row follows the current option. A Monitor
580 * cannot read its old events again: its next event follows the option.
581 */
582export function reloaded(jobs: readonly Job[], isAnyRatio: boolean, now: number): Job[] {
583  return jobs.map(job => {
584    if (job.kind !== 'shell' && job.kind !== 'monitor') return job
585    const isGenericDropped = !isAnyRatio && (!isKnownOrAbsent(job.progress) || !isKnownOrAbsent(job.anchor))
586    const isReread = job.kind === 'shell' && isRunning(job) && job.outputFile !== undefined
587    if (!isGenericDropped && !isReread) return job
588    const ratios = isGenericDropped ? { progress: isKnownOrAbsent(job.progress) ? job.progress : undefined, anchor: undefined } : {}
589    return { ...job, ...ratios, ...(isReread ? { finalReadAskedAt: now } : {}) }
590  })
591}
592
593/**
594 * The list after a read that began at `now`: size, time, last line, and progress; a read-problem note clears, and a
595 * later end keeps its final read. A read of the redirect file carries that file's size and time. A read of the other
596 * file than the one the row shows replaces the row's last line, progress, and anchor with that file's. isAnyRatio (the
597 * `genericRatios` option) lets any ratio count as progress. A read whose output file ends with the closing line ends a
598 * running row (see closed).
599 */
600export function tailRead(jobs: readonly Job[], id: string, read: TailRead, now: number, isAnyRatio = false): Job[] {
601  return jobs.map(original => {
602    if (original.kind !== 'shell' || original.id !== id) return original
603    // Only Claude Code's output file ends with its closing line; a redirect file holds only what the job wrote.
604    const lines = read.redirectBytes === undefined ? jobLinesOf(read.lines) : read.lines
605    const found = progressOf(lines.slice(-PROGRESS_LINES), now, isAnyRatio)
606    const isRedirect = read.redirectBytes !== undefined
607    const job: ShellJob = isRedirect === (original.redirectBytes !== undefined)
608      ? original
609      : { ...original, redirectBytes: undefined, lastLine: undefined, progress: undefined, anchor: undefined }
610    // A full read of output that kept its size, as a reload asks for, keeps a reading it finds again, with that reading's time.
611    const isUnchanged = (isRedirect ? read.redirectBytes === job.redirectBytes : read.bytes === job.outputBytes) && job.failedReadBytes === undefined
612    const progress = isUnchanged && isSameReading(found, job.progress) ? undefined : found
613    const lastLine = lines.at(-1)
614    return closed({
615      ...job,
616      outputBytes: read.bytes,
617      ...(isRedirect ? { redirectBytes: read.redirectBytes } : {}),
618      failedReadBytes: undefined,
619      lastOutputAt: read.mtimeMs,
620      lastReadAt: now,
621      successfulReadAt: now,
622      finalReadAskedAt: askedSince(job, now),
623      note: job.note !== undefined && READ_NOTES.has(job.note) ? undefined : job.note,
624      lastLine: lastLine === undefined ? job.lastLine : clip(lastLine, LINE_MAX),
625      progress: progress ?? job.progress,
626      anchor: progress === undefined ? job.anchor : anchorOf(job.anchor, progress, job.progress),
627    }, read.exitCode, now)
628  })
629}
630
631/**
632 * The list after a read that began at `now` failed: the next try waits its interval, a failed final read is not tried
633 * again unless a later end asked, and a launch or end note wins over a read note. A failed read of the redirect file
634 * after a read of the output file that ended with the closing line still ends a running row with that `exitCode`.
635 */
636export function readFailed(jobs: readonly Job[], id: string, note: 'output unavailable' | 'tail failed', now: number, bytes?: number, exitCode?: number): Job[] {
637  return jobs.map(job =>
638    job.kind === 'shell' && job.id === id
639      ? closed({
640          ...job,
641          lastReadAt: now,
642          successfulReadAt: undefined,
643          failedReadBytes: bytes ?? job.failedReadBytes,
644          finalReadAskedAt: askedSince(job, now),
645          note: job.note === undefined || READ_NOTES.has(job.note) ? note : job.note,
646        }, exitCode, now)
647      : job,
648  )
649}
650
651/** The list with every ended job removed. */
652export function finishedCleared(jobs: readonly Job[]): Job[] {
653  return jobs.filter(isRunning)
654}
655
656/** The observation time belongs to the input, before any asynchronous work or state retry. */
657export type JobObservation = {
658  at: number
659  ids?: readonly string[]
660  kind?: 'snapshot' | 'restore' | 'activity' | 'remove' | 'notice'
661  /** Ids a notification ends for good; a running or `gone` shell, Monitor, or workflow row takes that end past a newer observation. */
662  finalIds?: readonly string[]
663  members?: readonly string[]
664  noticeRow?: string
665}
666
667/**
668 * The barriers: every barrier of a kept row, and the MAX_BARRIERS newest barriers of tasks with no row. A kept row's
669 * barrier does not count toward the cap. Only the count decides. A newer snapshot does not make the barrier of a task
670 * with no row redundant: the session that a /resume returns to can still hold that task's row, and the barrier must
671 * remove it there.
672 */
673function bounded(barriers: Iterable<[string, number]>, kept: ReadonlySet<string>): [string, number][] {
674  const entries = [...barriers]
675  const rowless = entries.filter(([id]) => !kept.has(id))
676  const excess = rowless.length - MAX_BARRIERS
677  if (excess <= 0) return entries
678  const evicted = new Set(rowless.sort(([, a], [, b]) => a - b).slice(0, excess).map(([id]) => id))
679  return entries.filter(([id]) => !evicted.has(id))
680}
681
682/** The state with both time lists bounded against its own rows. */
683function capped(state: JobsState): JobsState {
684  const kept = new Set(state.items.map(job => job.id))
685  return { ...state, observed: bounded(state.observed, kept), removed: bounded(state.removed, kept) }
686}
687
688/**
689 * One ordering rule for job inputs. Removal wins ties; other ties use write order.
690 * Snapshots also reject older inputs for absent rows. Activity counts settle every call, with their own field ordering.
691 * A final end of a running or `gone` shell, Monitor, or workflow passes newer observations, and a notification never adds back a removed row.
692 */
693export function observedJobs(state: JobsState, input: JobObservation, change: (jobs: Job[]) => Job[]): JobsState {
694  const next = ordered(state, input, change)
695  return next === state ? state : capped(next)
696}
697
698/** observedJobs before the cap. restoredJobs applies the cap once, after the whole state it lays over, so a row of it never counts. */
699function ordered(state: JobsState, input: JobObservation, change: (jobs: Job[]) => Job[]): JobsState {
700  if (input.noticeRow !== undefined && state.noticeRows?.includes(input.noticeRow)) return state
701  if (input.kind === 'snapshot' && input.at < (state.snapshotAt ?? -Infinity)) return state
702  const observed = new Map(state.observed)
703  const removed = new Map(state.removed)
704  const known = new Set(state.items.map(job => job.id))
705  const targets = input.ids === undefined ? undefined : new Set(input.ids)
706  const rows = new Map(state.items.map(job => [job.id, job]))
707  const isFinal = (id: string): boolean => {
708    const row = rows.get(id)
709    return row !== undefined && input.finalIds?.includes(id) === true && takesFinalEnd(row)
710  }
711  const accepts = (id: string): boolean =>
712    (targets === undefined || targets.has(id)) &&
713    input.at > (removed.get(id) ?? -Infinity) &&
714    // A notification never brings back a task whose row was removed.
715    !(input.kind === 'notice' && !known.has(id) && removed.has(id)) &&
716    (input.kind === 'activity' || isFinal(id) || input.at >= (observed.get(id) ?? -Infinity)) &&
717    ((input.kind !== 'restore' && known.has(id)) || input.at >= (state.snapshotAt ?? -Infinity))
718  const eligible = state.items.filter(job => accepts(job.id))
719  const changed = change(eligible).filter(job => accepts(job.id))
720  const replacements = new Map(changed.map(job => [job.id, job]))
721  const candidates = [
722    ...state.items.flatMap(job => accepts(job.id) ? replacements.get(job.id) ?? [] : job),
723    ...changed.filter(job => !known.has(job.id)),
724  ]
725  const items = trimmed(candidates)
726  const kept = new Set(items.map(job => job.id))
727  const affected = input.ids ?? [
728    ...eligible.filter(job => input.kind === 'snapshot' || replacements.get(job.id) !== job).map(job => job.id),
729    ...changed.filter(job => !known.has(job.id)).map(job => job.id),
730  ]
731  const hadRow = new Set([...eligible, ...changed].map(job => job.id))
732  for (const id of new Set(affected)) {
733    if (!accepts(id)) continue
734    observed.set(id, Math.max(observed.get(id) ?? input.at, input.at))
735    if (!kept.has(id) && (input.kind === 'remove' || hadRow.has(id))) removed.set(id, input.at)
736  }
737  // Retention is a removal too, including a finished row first discovered by this input.
738  for (const job of candidates) {
739    if (kept.has(job.id)) continue
740    const at = Math.max(input.at, observed.get(job.id) ?? input.at)
741    observed.set(job.id, at)
742    removed.set(job.id, Math.max(removed.get(job.id) ?? at, at))
743  }
744  return {
745    items,
746    observed: [...observed],
747    removed: [...removed],
748    snapshotAt: input.kind === 'snapshot' ? input.at : state.snapshotAt,
749    snapshotIds: input.kind === 'snapshot' ? input.members === undefined ? undefined : [...input.members] : state.snapshotIds,
750    noticeRows: input.noticeRow === undefined ? state.noticeRows : [...(state.noticeRows ?? []), input.noticeRow].slice(-NOTICE_ROWS_MAX),
751  }
752}
753
754/**
755 * Lays `carried` over `state` through the same ordering rule, using its original observation and removal times, then
756 * applies the cap once. The hooks no longer use it: the ordering tests keep it as the reference for the rule.
757 */
758export function restoredJobs(state: JobsState, carried: JobsState): JobsState {
759  let restored = state
760  for (const [id, at] of carried.removed) {
761    restored = ordered(restored, { at, ids: [id], kind: 'remove' }, () => [])
762  }
763  const rows = new Map(carried.items.map(job => [job.id, job]))
764  for (const [id, at] of carried.observed) {
765    const row = rows.get(id)
766    restored = ordered(restored, { at, ids: [id], kind: 'restore' }, jobs => row === undefined ? jobs : [row])
767  }
768  const saved = (carried.snapshotAt ?? -Infinity) >= (state.snapshotAt ?? -Infinity) ? carried : state
769  if (saved.snapshotAt !== null && saved.snapshotIds !== undefined) {
770    const members = new Set(saved.snapshotIds)
771    const at = saved.snapshotAt
772    restored = ordered(restored, { at, ids: restored.items.filter(job => !members.has(job.id)).map(job => job.id) }, jobs => reconciled(jobs, [], at))
773  }
774  return capped({ ...restored, snapshotAt: saved.snapshotAt, snapshotIds: saved.snapshotIds,
775    noticeRows: [...new Set([...(state.noticeRows ?? []), ...(carried.noticeRows ?? [])])].slice(-NOTICE_ROWS_MAX) })
776}
777
778/**
779 * The jobs that a session shows: every running job, and the ended jobs from `since` on. Each ended job that it does not
780 * show keeps a removal, as a cleared row does, and the cap bounds them: a reload that starts from this view still
781 * refuses the late inputs of those jobs.
782 */
783export function viewedJobs(state: JobsState, since: number): JobsState {
784  const isShown = (job: Job): boolean => isRunning(job) || (job.endedAt ?? -Infinity) >= since
785  if (state.items.every(isShown)) return state
786  const observed = new Map(state.observed)
787  const removed = new Map(state.removed)
788  for (const job of state.items) {
789    if (!isShown(job)) removed.set(job.id, Math.max(removed.get(job.id) ?? -Infinity, observed.get(job.id) ?? job.lastEndedAt ?? job.startedAt))
790  }
791  return capped({ ...state, items: state.items.filter(isShown), removed: [...removed] })
792}
793
794/** Live: a job runs, a wakeup is pending, or a job ended less than RECENT_MS ago. */
795export function isLive(jobs: readonly Job[], wakeup: Wakeup | null, now: number): boolean {
796  return wakeup !== null || jobs.some(job => isRunning(job) || now - (job.endedAt ?? 0) < RECENT_MS)
797}
798
799/** The wakeup after a scheduled trigger: cleared from TRIGGER_EARLY_MS before its due time on. */
800export function wakeupAfterTrigger(wakeup: Wakeup | null, now: number): Wakeup | null {
801  return wakeup !== null && now >= wakeup.dueAt - TRIGGER_EARLY_MS ? null : wakeup
802}
803
804/** The wakeup after a tick: dropped once it is more than WAKEUP_STALE_MS past due. */
805export function wakeupAfterTick(wakeup: Wakeup | null, now: number): Wakeup | null {
806  return wakeup !== null && now > wakeup.dueAt + WAKEUP_STALE_MS ? null : wakeup
807}
808
809/**
810 * The wakeup after the session crons of a snapshot whose hook began at `now`: a list with no one-shot entry holds no
811 * pending wakeup. The engine took the snapshot before the hook began, while hooks above it ran, so a wakeup set from
812 * SNAPSHOT_SLACK_MS before then on may be newer than the snapshot, and it stays.
813 */
814export function wakeupAfterSnapshot(wakeup: Wakeup | null, crons: readonly { recurring: boolean }[], now: number): Wakeup | null {
815  return wakeup === null || crons.some(cron => !cron.recurring) || wakeup.setAt > now - SNAPSHOT_SLACK_MS ? wakeup : null
816}
817
818/** Ends only commands whose launch tied their lifetime to this agent's final answer. */
819export function ownerAnswered(jobs: readonly Job[], agentId: string, now: number): Job[] {
820  return jobs.map(job => job.kind === 'shell' && job.ownerAgentId === agentId && job.endsWithAgent === true && isRunning(job)
821    ? endedJob(job, 'gone', now, { note: AGENT_GONE_NOTE }) : job)
822}
823
hooks/limits.ts 81 lines
1/** The ticker's period. */
2export const TICK_MS = 1_000
3/** A ticker with no period for this long has stopped (a refused period ends an interval); the next write starts it again. */
4export const TICKER_STALE_MS = 3_000
5/** The least time between two reads of one job's output. */
6export const READ_EVERY_MS = 2_000
7/** Outputs larger than this are read at most every LARGE_READ_EVERY_MS. */
8export const LARGE_FILE_BYTES = 262_144
9/** The least time between two reads of a large output. */
10export const LARGE_READ_EVERY_MS = 10_000
11/** The largest file $.fs.read reads (4 MiB); a larger output goes through `tail`. */
12export const FS_READ_MAX = 4_194_304
13/** Bytes `tail -c` returns, and characters kept from the end of a whole read. The `tail` call in register.tsx writes this number as the text '16384': change both together. */
14export const TAIL_BYTES = 16_384
15/** How long `tail` may run. */
16export const TAIL_TIMEOUT_MS = 2_000
17/** Display lines searched for progress, newest first. */
18export const PROGRESS_LINES = 10
19/** Characters before a ratio that decide whether it is a known progress counter, such as `Step` or `[builder `. */
20export const COUNTER_CONTEXT = 256
21/** The least time between anchor and sample before a rate ETA shows. */
22export const ETA_MIN_SPAN_MS = 10_000
23/** The least percentage-point gain before a rate ETA shows. */
24export const ETA_MIN_GAIN = 1
25/** Silence before a running shell or agent shows `quiet`. */
26export const QUIET_MS = 120_000
27/** Failed subagent calls in a row before the failing flag shows. */
28export const FAILING_STREAK = 3
29/** How long an ended job keeps the session live and shows in the band. */
30export const RECENT_MS = 60_000
31/** Ended jobs older than this leave the /downrange table. */
32export const TABLE_RECENT_MS = 1_800_000
33/** The most ended jobs in the /downrange table. */
34export const TABLE_FINISHED_ROWS = 5
35/** The most rows in the /downrange table. */
36export const TABLE_MAX_ROWS = 15
37/** A /downrange table narrower than this joins Kind into Job and ETA into Progress. */
38export const TABLE_NARROW_COLUMNS = 100
39/** Ended jobs kept in state. */
40export const MAX_FINISHED = 20
41/** Ordering barriers of tasks with no row kept in each of the jobs state's observed and removed lists. The oldest go first. A kept row's barriers stay and do not count. */
42export const MAX_BARRIERS = 2_000
43/** A wakeup this far past due is dropped. */
44export const WAKEUP_STALE_MS = 600_000
45/** A scheduled trigger this close before the due time clears the wakeup. */
46export const TRIGGER_EARLY_MS = 5_000
47/** A job or wakeup first seen this soon before a Stop or SubagentStop hook began may be newer than its snapshot, so absence does not end it. */
48export const SNAPSHOT_SLACK_MS = 10_000
49/** A repeated status prompt inside this window goes to Claude. */
50export const REPEAT_WINDOW_MS = 60_000
51/** Durations kept per history key. */
52export const MAX_RUNS = 5
53/** History keys kept in the store. */
54export const MAX_KEYS = 200
55/** Lengths kept in state. */
56export const TITLE_MAX = 120
57export const LINE_MAX = 200
58export const NOTE_MAX = 80
59export const ACTIVITY_MAX = 60
60/** The longest unknown status word a diagnostic quotes. */
61export const STATUS_WORD_MAX = 20
62/** Lengths in the /downrange table, wide and narrow. */
63export const CELL_TITLE_MAX = 40
64export const CELL_LINE_MAX = 80
65export const CELL_LINE_NARROW_MAX = 40
66/** Band layout: room for the two buttons, and the longest snippet title. */
67export const BAND_BUTTONS_COLUMNS = 20
68export const BAND_TITLE_MAX = 24
69/** Pane layout. */
70export const BAR_CELLS = 10
71export const BAR_MIN_COLUMNS = 60
72export const PANE_TITLE_MIN = 12
73export const PANE_FIXED_COLUMNS = 44
74
75/** Stored child rows retained for duplicate suppression. */
76export const NOTICE_ROWS_MAX = 256
77/** The longest wait of a hook for the mirror write that holds its change; past it, the hook goes on with the change in memory only. */
78export const MIRROR_WAIT_MS = 2_000
79/** Sessions of one process whose start time for Finished stays in memory, for a /resume back to them; the oldest goes first. */
80export const MAX_SESSIONS = 32
81
hooks/notices.ts 71 lines
1/** One `<task-notification>` block. */
2export type Notice = {
3  ids: string[]
4  /** The engine's task type, such as `remote_agent` for a task that runs in a cloud session. */
5  taskType?: string
6  outputFile?: string
7  status?: string
8  summary?: string
9  event?: string
10}
11
12const OPEN = '<task-notification>'
13const CLOSE = '</task-notification>'
14/** One header field, as the engine writes each one on a line of its own, before the block's body. */
15const FIELD = /\s*<(task-id|tool-use-id|task-type|output-file|status|summary)>([\s\S]*?)<\/\1>/y
16const EVENT_OPEN = '<event>'
17const EVENT_CLOSE = '</event>'
18const ID_SHAPE = /^\S{1,128}$/
19
20/** Text as it stood before the engine escaped `&`, `<`, and `>` in it. */
21const unescaped = (text: string): string => text.replaceAll('&lt;', '<').replaceAll('&gt;', '>').replaceAll('&amp;', '&')
22
23/**
24 * One block's content: its header fields (every task id of a valid shape, and the first non-empty value of each other field),
25 * then the text of the first `<event>` in its body. The body's other parts (a note, a result, usage, plain text) are
26 * not read.
27 */
28function noticeOf(block: string): Notice {
29  const ids: string[] = []
30  const first = new Map<string, string>()
31  let end = 0
32  for (;;) {
33    FIELD.lastIndex = end
34    const field = FIELD.exec(block)
35    if (field === null) break
36    end = FIELD.lastIndex
37    const [, name = '', text = ''] = field
38    const value = unescaped(text.trim())
39    if (name === 'task-id') {
40      if (ID_SHAPE.test(value)) ids.push(value)
41    } else if (value !== '' && !first.has(name)) {
42      first.set(name, value)
43    }
44  }
45  const open = block.indexOf(EVENT_OPEN, end)
46  const close = open === -1 ? -1 : block.indexOf(EVENT_CLOSE, open)
47  const event = close === -1 ? undefined : unescaped(block.slice(open + EVENT_OPEN.length, close))
48  return { ids, taskType: first.get('task-type'), outputFile: first.get('output-file'), status: first.get('status'), summary: first.get('summary'), event }
49}
50
51/**
52 * Every notification block in a delivered prompt, in order. The engine escapes every value inside a block, so a block
53 * runs to the first `</task-notification>` after its opening tag. Blocks follow one another with only white space
54 * between them. Other text after a block is that block's trailing text, which the engine does not escape (a stalled
55 * command's last output, for one), so nothing after it is read.
56 */
57export function noticesOf(text: string): Notice[] {
58  const notices: Notice[] = []
59  let at = text.indexOf(OPEN)
60  while (at !== -1) {
61    const start = at + OPEN.length
62    const close = text.indexOf(CLOSE, start)
63    if (close === -1) break
64    notices.push(noticeOf(text.slice(start, close)))
65    const end = close + CLOSE.length
66    const next = text.indexOf(OPEN, end)
67    at = next !== -1 && text.slice(end, next).trim() === '' ? next : -1
68  }
69  return notices
70}
71
hooks/output.ts 200 lines
1import type { Anchor, Progress } from '../types'
2import { COUNTER_CONTEXT, ETA_MIN_GAIN, ETA_MIN_SPAN_MS, TAIL_BYTES } from './limits'
3
4const ANSI = /\x1b\[[0-?]*[ -/]*[@-~]|\x1b\][\s\S]*?(?:\x07|\x1b\\)|\x1b[@-Z\\-_]/g
5const CONTROL = /[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]/g
6const PERCENT = /(?<![\d.])(\d{1,3}(?:\.\d+)?)\s?%/g
7const FRACTION = /(?<![\w./:-])(\d{1,7})\s?\/\s?(\d{1,7})(?![\w./-])/g
8const OF = /(?<![\w./\\])(\d{1,7}) of (\d{1,7})(?![\w/\\]|\.\w)/gi
9/** A two-digit clock right after a `done/total` match: with done 1 to 12 and total up to 31, the match is a short date, as in `10/11 14:03`. */
10const CLOCK_AFTER = /\s+\d{2}:\d{2}/y
11/** Words that name the count right after them, as in `Step 3/7`, `Epoch 3 of 10`, or pip's `Progress 12 of 40`. */
12const STEP_WORDS: ReadonlySet<string> = new Set(['step', 'stage', 'phase', 'epoch', 'iteration', 'batch', 'chunk', 'part', 'file', 'item', 'task', 'test', 'progress'])
13/** The last word before a count, with an optional colon, as in `Step 3/7` or `batch: 9/30`. */
14const WORD_BEFORE = /\W([A-Za-z]+):?\s*$/
15/** An opening bracket right before a count, after up to two labels, as in `[3/7]`, `(  3/150)`, or Docker's `[builder 3/7]`. */
16const OPENED_BEFORE = /([[(])\s*(?:[A-Za-z][\w./-]*\s+){0,2}$/
17/** A drawn bar right before a count: `[=====>   ]` (cargo), `|█████     |` (tqdm), or `━━━━━━` (rich). */
18const BAR_BEFORE = /(?:\[[=#>\-. ]{3,}\]|\|[█▉▊▋▌▍▎▏ ]{3,}\||[━╸╺█▉▊▋▌▍▎▏■▰]{3})\s*$/
19/** Docker Compose's count line, as in `[+] Running 3/7`. */
20const COMPOSE_BEFORE = /\[\+\]\s+[A-Za-z]+\s+$/
21/** The bracket that closes a count, after optional spaces. */
22const CLOSER_AFTER = /\s*([\])])/y
23/** An output file path as the engine names it: absolute, in a `tasks` folder, named `<task id>.output`. Its folders may hold spaces (a temp folder set by CLAUDE_CODE_TMPDIR). */
24const OUTPUT_PATH = /^(?:[A-Za-z]:)?[\\/][^\n]*[\\/]tasks[\\/]([A-Za-z0-9_-]+)\.output$/
25/** The engine's words before the path in a background command's result. */
26const OUTPUT_NOTICE = 'Output is being written to: '
27/** The line the engine adds to a shell's output when the command ends, with the command's exit code. */
28const EXIT_LINE = /^\[exited with code (-?\d+)\]$/
29/** That line at the end of an output's text, with the line break the engine writes before it and the one after it. */
30const EXIT_END = /\n?\[exited with code -?\d+\]\n?$/
31/** A carriage return, built from its code so that no string literal holds a control character. */
32const CR = String.fromCharCode(13)
33
34type Found = { percent: number; label: string; isGeneric: boolean }
35
36/** Cuts text to max characters, ending with an ellipsis when it cut. */
37export function clip(text: string, max: number): string {
38  return text.length <= max ? text : `${text.slice(0, max - 1)}…`
39}
40
41/** The lines a terminal would show for raw output: `\r` overwrites, escapes and controls removed, empty lines dropped. */
42export function displayLinesOf(text: string): string[] {
43  return text
44    .split('\n')
45    .map(raw => {
46      const line = raw.endsWith(CR) ? raw.slice(0, -1) : raw
47      return line
48        .slice(line.lastIndexOf(CR) + 1)
49        .replace(ANSI, '')
50        .replace(/\t/g, ' ')
51        .replace(CONTROL, '')
52        .trim()
53    })
54    .filter(line => line !== '')
55}
56
57/** Any text as one clean display line. */
58export function oneLine(text: string): string {
59  return displayLinesOf(text).join(' ').replace(/\s+/g, ' ')
60}
61
62function lastPercent(line: string): Found | undefined {
63  let found: Found | undefined
64  for (const match of line.matchAll(PERCENT)) {
65    const value = Number(match[1])
66    if (value <= 100) found = { percent: value, label: `${Math.round(value)}%`, isGeneric: false }
67  }
68  return found
69}
70
71/** Whether a `done/total` match is a short date followed by a clock. */
72function isDateBeforeClock(line: string, end: number, done: number, total: number): boolean {
73  if (done < 1 || done > 12 || total > 31) return false
74  CLOCK_AFTER.lastIndex = end
75  return CLOCK_AFTER.test(line)
76}
77
78/**
79 * Whether the count from start to end has a known progress format: it starts the line, follows a step word, sits in
80 * brackets, follows a drawn bar, or ends Docker Compose's `[+] Running`. Past the line-start test, only the
81 * COUNTER_CONTEXT characters before it decide, so a long line stays cheap.
82 */
83function isCounter(line: string, start: number, end: number): boolean {
84  if (!/\S/.test(line.slice(0, start))) return true
85  // A leading space stands for the line start, so that a word at the start has a boundary before it.
86  const before = start > COUNTER_CONTEXT ? line.slice(start - COUNTER_CONTEXT, start) : ` ${line.slice(0, start)}`
87  const word = WORD_BEFORE.exec(before)?.[1]
88  if (word !== undefined && STEP_WORDS.has(word.toLowerCase())) return true
89  const opener = OPENED_BEFORE.exec(before)?.[1]
90  CLOSER_AFTER.lastIndex = end
91  if (opener !== undefined && CLOSER_AFTER.exec(line)?.[1] === (opener === '[' ? ']' : ')')) return true
92  return BAR_BEFORE.test(before) || COMPOSE_BEFORE.test(before)
93}
94
95/** The last valid ratio in the line; unless isAnyRatio, only one in a known progress format counts. isGeneric marks one in no known format. */
96function lastRatio(line: string, pattern: RegExp, isAnyRatio: boolean): Found | undefined {
97  let found: Found | undefined
98  for (const match of line.matchAll(pattern)) {
99    const done = Number(match[1])
100    const total = Number(match[2])
101    const end = match.index + match[0].length
102    if (pattern === FRACTION && isDateBeforeClock(line, end, done, total)) continue
103    if (total < 1 || done > total) continue
104    const isKnown = isCounter(line, match.index, end)
105    if (isKnown || isAnyRatio) found = { percent: (done / total) * 100, label: `${done}/${total}`, isGeneric: !isKnown }
106  }
107  return found
108}
109
110/**
111 * The newest progress reading in the lines: percent, then `done/total`, then `N of M`. A ratio counts only in a known
112 * progress format, unless isAnyRatio (the `genericRatios` option) lets any ratio count.
113 */
114export function progressOf(lines: readonly string[], at: number, isAnyRatio: boolean): Progress | undefined {
115  for (const line of [...lines].reverse()) {
116    const found = lastPercent(line) ?? lastRatio(line, FRACTION, isAnyRatio) ?? lastRatio(line, OF, isAnyRatio)
117    if (found !== undefined) return { ...found, at }
118  }
119  return undefined
120}
121
122/** The start of the current rising run of readings, which the rate ETA measures from; a drop starts a new run. A new anchor keeps the format mark of its reading. */
123export function anchorOf(anchor: Anchor | undefined, progress: Progress, previous: Progress | undefined): Anchor {
124  return anchor === undefined || previous === undefined || progress.percent < previous.percent
125    ? { percent: progress.percent, at: progress.at, isGeneric: progress.isGeneric === true }
126    : anchor
127}
128
129/** Milliseconds left at the observed rate, or undefined before a rate exists. */
130export function remainingByRate(progress: Progress, anchor: Anchor, now: number): number | undefined {
131  const span = progress.at - anchor.at
132  const gain = progress.percent - anchor.percent
133  if (span < ETA_MIN_SPAN_MS || gain < ETA_MIN_GAIN) return undefined
134  return Math.max(0, ((100 - progress.percent) * span) / gain - (now - progress.at))
135}
136
137/** 42s, 4m 05s, 1h 05m. */
138export function durationText(ms: number): string {
139  const seconds = Math.max(0, Math.floor(ms / 1_000))
140  if (seconds < 60) return `${seconds}s`
141  const minutes = Math.floor(seconds / 60)
142  if (minutes < 60) return `${minutes}m ${String(seconds % 60).padStart(2, '0')}s`
143  return `${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, '0')}m`
144}
145
146/** <1m, ~3m, ~1h 05m: minutes rounded up. */
147export function etaText(ms: number): string {
148  if (ms < 60_000) return '<1m'
149  const minutes = Math.ceil(ms / 60_000)
150  if (minutes < 60) return `~${minutes}m`
151  return `~${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, '0')}m`
152}
153
154/** Whether a path is the output file of this task: absolute, in a `tasks` folder, and named after the task id. */
155export function isOutputFileOf(path: string, id: string): boolean {
156  return OUTPUT_PATH.exec(path)?.[1] === id
157}
158
159/**
160 * The output file named by the last valid engine notice. Its terminal filename ends the path; directories may
161 * contain the notice's words or the same filename.
162 */
163export function outputFileOf(text: string, id: string): string | undefined {
164  const name = `${id}.output`
165  const end = text.lastIndexOf(name)
166  if (end === -1) return undefined
167  if (!/^(?:\.(?:\s|$)|$)/.test(text.slice(end + name.length))) return undefined
168  let notice = text.lastIndexOf(OUTPUT_NOTICE, end)
169  while (notice !== -1) {
170    const path = text.slice(notice + OUTPUT_NOTICE.length, end + name.length)
171    if (isOutputFileOf(path, id)) return path
172    notice = notice === 0 ? -1 : text.lastIndexOf(OUTPUT_NOTICE, notice - 1)
173  }
174  return undefined
175}
176
177/** The exit code on the engine's closing `[exited with code N]` line, when the lines of an output end with it: the command ended. */
178export function closingCodeOf(lines: readonly string[]): number | undefined {
179  const code = EXIT_LINE.exec(lines.at(-1) ?? '')?.[1]
180  return code === undefined ? undefined : Number(code)
181}
182
183/** The lines of an output without the engine's closing `[exited with code N]` line: the lines the job wrote. */
184export function jobLinesOf(lines: readonly string[]): readonly string[] {
185  return EXIT_LINE.test(lines.at(-1) ?? '') ? lines.slice(0, -1) : lines
186}
187
188/** The size of an output without the engine's closing line, from its size and the text of its end: the bytes the job wrote. */
189export function jobBytesOf(text: string, size: number): number {
190  return size - (EXIT_END.exec(text)?.[0].length ?? 0)
191}
192
193/** The last TAIL_BYTES of an output, from a line start: a tail, or a read cut short, drops its partial first line; text with no line break stays whole, one line that `\r` redraws. */
194export function outputEndOf(text: string, isTail: boolean): string {
195  const end = text.slice(-TAIL_BYTES)
196  if (!isTail && end.length === text.length) return end
197  const lineStart = end.indexOf('\n') + 1
198  return lineStart === 0 ? end : end.slice(lineStart)
199}
200
hooks/state.ts 60 lines
1import type { JobsState, WakeupState } from '../types'
2
3const record = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value)
4const number = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value)
5const optionalNumbers = (value: Record<string, unknown>, keys: readonly string[]): boolean => keys.every(key => value[key] === undefined || number(value[key]))
6const optionalStrings = (value: Record<string, unknown>, keys: readonly string[]): boolean => keys.every(key => value[key] === undefined || typeof value[key] === 'string')
7const strings = (value: unknown): value is string[] => Array.isArray(value) && value.every(item => typeof item === 'string')
8const times = (value: unknown): boolean => Array.isArray(value) && value.every(item => Array.isArray(item) && item.length === 2 && typeof item[0] === 'string' && number(item[1]))
9const generic = (value: Record<string, unknown>): boolean => value.isGeneric === undefined || typeof value.isGeneric === 'boolean'
10const progress = (value: unknown): boolean => value === undefined || (record(value) && number(value.percent) && number(value.at) && typeof value.label === 'string' && generic(value))
11const anchor = (value: unknown): boolean => value === undefined || (record(value) && number(value.percent) && number(value.at) && generic(value))
12const states = new Set(['running', 'completed', 'failed', 'stopped', 'gone'])
13const activities = new Map([['pending', true], ['running', true], ['waiting', true], ['idle', true]])
14
15function job(value: unknown): boolean {
16  if (!record(value) || typeof value.id !== 'string' || typeof value.title !== 'string' || (typeof value.state !== 'string' || !states.has(value.state)) ||
17      !number(value.startedAt) || typeof value.isStartSeen !== 'boolean' ||
18      !optionalNumbers(value, ['lastRunningAt', 'omittedAt', 'endedAt', 'lastEndedAt', 'exitCode', 'lastOutputAt']) ||
19      !optionalStrings(value, ['note', 'launchNote', 'lastLine'])) return false
20  switch (value.kind) {
21    case 'shell':
22      return optionalStrings(value, ['outputFile', 'redirectFile', 'historyKey', 'ownerAgentId']) &&
23        (value.endsWithAgent === undefined || typeof value.endsWithAgent === 'boolean') &&
24        optionalNumbers(value, ['outputBytes', 'redirectBytes', 'failedReadBytes', 'lastReadAt', 'successfulReadAt', 'finalReadAskedAt', 'usualMs']) && progress(value.progress) && anchor(value.anchor)
25    case 'monitor':
26      return number(value.eventCount) && optionalNumbers(value, ['deadline']) && progress(value.progress) && anchor(value.anchor)
27    case 'agent':
28      return number(value.toolCount) && number(value.inFlight) && number(value.errorStreak) &&
29        optionalNumbers(value, ['lastToolStartedAt', 'lastSuccessAt']) &&
30        (value.activityStatus === undefined || (typeof value.activityStatus === 'string' && activities.has(value.activityStatus))) &&
31        (value.runsElsewhere === undefined || typeof value.runsElsewhere === 'boolean') &&
32        (value.concurrentFailureTimes === undefined || (Array.isArray(value.concurrentFailureTimes) && value.concurrentFailureTimes.every(number)))
33    case 'workflow': return true
34    case 'task': return typeof value.taskType === 'string'
35    default: return false
36  }
37}
38
39/** Reads the supported jobs shapes without discarding their ordering barriers. */
40export function jobsStateOf(raw: unknown): JobsState | undefined {
41  if (!record(raw) || (raw.shape !== 'v7' && raw.shape !== 'v8') || !record(raw.value)) return undefined
42  const value = raw.value
43  if (!Array.isArray(value.items) || !value.items.every(job) || !times(value.observed) || !times(value.removed) ||
44      (value.snapshotAt !== null && !number(value.snapshotAt)) ||
45      (value.snapshotIds !== undefined && !strings(value.snapshotIds)) || (value.noticeRows !== undefined && !strings(value.noticeRows))) return undefined
46  const state = value as JobsState
47  const observations = new Map(state.observed)
48  if (state.items.some(item => !observations.has(item.id))) return undefined
49  return state
50}
51
52/** Reads the previous wakeup shape, including a cancellation with no pending wakeup. */
53export function wakeupStateOf(raw: unknown): WakeupState | undefined {
54  if (!record(raw) || (raw.shape !== 'v3' && raw.shape !== 'v4') || !record(raw.value)) return undefined
55  const { pending, updatedAt } = raw.value
56  if (updatedAt !== null && !number(updatedAt)) return undefined
57  if (pending !== null && (!record(pending) || !number(pending.dueAt) || !number(pending.setAt) || typeof pending.reason !== 'string' || !number(updatedAt))) return undefined
58  return raw.value as WakeupState
59}
60
hooks/prompt.ts 14 lines
1/** A whole prompt that asks only for status, such as `status?`, `what's the eta`, or `give me a progress update`. */
2const STATUS =
3  /^(?:(?:what(?:'?s| is) (?:the )?|give me (?:the |a |an )?)?(?:latest |live |current )?(?:status|eta|progress)(?: now| update| so far)?)[?.!]*$/
4
5/** Trimmed, lowercased, single-spaced, with the typographic apostrophe made plain. */
6export function normalizePrompt(text: string): string {
7  return text.trim().toLowerCase().replace(/\s+/g, ' ').replace(/’/g, "'")
8}
9
10/** Whether a normalized prompt is an exact status question. */
11export function isStatusPrompt(normalized: string): boolean {
12  return STATUS.test(normalized)
13}
14
hooks/redirect.ts 313 lines
1/**
2 * The redirect rule: the one file that a background command's own text sends its output to, its redirect file. A
3 * small reader of shell text finds it. It gives up on text it cannot read for sure, and the command then follows no file.
4 */
5
6/** A word after quote removal; isLiteral is false when the shell can expand it: a variable, command text, a pattern, or a leading `~`. */
7type Word = { kind: 'word'; text: string; isLiteral: boolean }
8/** A redirection, with the file descriptor written right before it: digits or `{name}`, or empty. */
9type Redirect = { kind: 'redirect'; op: string; fd: string }
10/** An operator between commands. */
11type Separator = { kind: 'separator'; op: string }
12type Token = Word | Redirect | Separator
13
14/** Redirection operators, longest first. */
15const REDIRECTS = ['&>>', '&>', '>>', '>&', '>|', '>', '<<<', '<<-', '<<', '<>', '<&', '<']
16/** A here-document, whose lines this reader does not follow. */
17const HERE_DOCUMENTS: ReadonlySet<string> = new Set(['<<', '<<-'])
18/** Operators between commands, longest first. */
19const SEPARATORS = ['&&', '&', '||', '|&', '|', ';;&', ';;', ';&', ';', '(', ')', '\n']
20/** Characters that end a word outside quotes. */
21const METACHARACTERS: ReadonlySet<string> = new Set([' ', '\t', '\n', '|', '&', ';', '(', ')', '<', '>'])
22/** Unquoted characters that make a word a pattern or a brace list. */
23const PATTERN_CHARACTERS: ReadonlySet<string> = new Set(['*', '?', '[', '{'])
24/** Commands that change the folder, after which a relative path can name another file. */
25const FOLDER_CHANGES: ReadonlySet<string> = new Set(['cd', 'chdir', 'pushd', 'popd'])
26/** Words that run other shell text in this shell, or a builtin behind a prefix, which can change the folder out of sight: anywhere in the command. */
27const HIDDEN_CODE: ReadonlySet<string> = new Set(['eval', 'source', 'command', 'builtin'])
28/** The operators of a plain command list; every other operator, such as `||`, `(`, or `&`, makes the text one this reader does not follow. */
29const LIST_SEPARATORS: ReadonlySet<string> = new Set(['&&', ';', '\n', '|', '|&'])
30/** Words that, as a command, start or belong to code that may not run, or that runs later or in the background: a function, a condition, a loop, or a group. */
31const COMPOUND_WORDS: ReadonlySet<string> = new Set(['function', 'if', 'then', 'elif', 'else', 'fi', 'case', 'esac', 'while', 'until', 'for', 'select', 'do', 'done', 'repeat', 'foreach', 'coproc', '{', '}'])
32/** Words before a command that keep the next word a command: `!`, `time`, and `time -p`. */
33const PIPELINE_PREFIXES: ReadonlySet<string> = new Set(['!', 'time', '-p'])
34/** A variable assignment before a command, such as `FLAGS=-j4`. */
35const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*\+?=/
36/** The redirections that send standard output to a file, with the file descriptors each may carry: `>`, `>>`, `1>`, `1>>`, `&>`, `&>>`, and `>&`. */
37const OUTPUT_REDIRECTS: ReadonlyMap<string, ReadonlySet<string>> = new Map([
38  ['>', new Set(['', '1'])],
39  ['>>', new Set(['', '1'])],
40  ['&>', new Set([''])],
41  ['&>>', new Set([''])],
42  ['>&', new Set([''])],
43])
44/** A file descriptor written right before a redirection. */
45const FD = /^(?:\d+|\{[A-Za-z_][A-Za-z0-9_]*\})$/
46/** The target of `>&` that copies, moves (`3-`), or closes a file descriptor instead of naming a file. */
47const DUPLICATE = /^(?:\d+-?|-)$/
48/** A variable name or a special parameter after `$`. */
49const PARAMETER = /[A-Za-z_][A-Za-z0-9_]*|[0-9?$!#*@-]/y
50/** A Windows drive path, such as `C:/b.log`. */
51const DRIVE = /^[A-Za-z]:/
52
53/** The end of a `'…'` or `` `…` `` part that starts at `at`, after its closing mark; a backslash escapes the next character when isEscaped. */
54function closingMarkOf(command: string, at: number, mark: string, isEscaped: boolean): number | undefined {
55  for (let end = at + 1; end < command.length; end += 1) {
56    if (isEscaped && command[end] === '\\') end += 1
57    else if (command[end] === mark) return end + 1
58  }
59  return undefined
60}
61
62/** The end of the bracketed part that opens at `open`, after its closing bracket; quotes and expansions inside it are skipped. */
63function closingBracketOf(command: string, open: number, opener: string, closer: string): number | undefined {
64  let depth = 0
65  let at = open
66  while (at < command.length) {
67    const char = command[at]
68    if (char === '\\') {
69      at += 2
70      continue
71    }
72    if (char === "'" || char === '"' || char === '$' || char === '`') {
73      const end = char === "'" ? closingMarkOf(command, at, "'", false) : char === '"' ? doubleQuotedAt(command, at)?.end : expansionEnd(command, at)
74      if (end === undefined) return undefined
75      at = end
76      continue
77    }
78    if (char === opener) depth += 1
79    else if (char === closer && --depth === 0) return at + 1
80    at += 1
81  }
82  return undefined
83}
84
85/** The end of the expansion at `at`: `$(…)`, `$((…))`, `${…}`, `$'…'`, `$name`, a lone `$`, or `` `…` ``. */
86function expansionEnd(command: string, at: number): number | undefined {
87  if (command[at] === '`') return closingMarkOf(command, at, '`', true)
88  const next = command[at + 1]
89  if (next === '(') return closingBracketOf(command, at + 1, '(', ')')
90  if (next === '{') return closingBracketOf(command, at + 1, '{', '}')
91  if (next === "'") return closingMarkOf(command, at + 1, "'", true)
92  PARAMETER.lastIndex = at + 1
93  return PARAMETER.test(command) ? PARAMETER.lastIndex : at + 1
94}
95
96/** The `"…"` part at `at`: its text after quote removal, its end, and whether it holds no expansion. */
97function doubleQuotedAt(command: string, at: number): { text: string; end: number; isLiteral: boolean } | undefined {
98  let text = ''
99  let isLiteral = true
100  let index = at + 1
101  while (index < command.length) {
102    const char = command[index]!
103    if (char === '"') return { text, end: index + 1, isLiteral }
104    if (char === '\\' && index + 1 < command.length) {
105      const next = command[index + 1]!
106      if (next !== '\n') text += '$`"\\'.includes(next) ? next : `\\${next}`
107      index += 2
108    } else if (char === '$' || char === '`') {
109      const end = expansionEnd(command, index)
110      if (end === undefined) return undefined
111      text += command.slice(index, end)
112      isLiteral = false
113      index = end
114    } else {
115      text += char
116      index += 1
117    }
118  }
119  return undefined
120}
121
122/** The word at `start`: its text after quote removal, its end, whether it is literal, and whether any of it was quoted. */
123function wordAt(command: string, start: number): { text: string; end: number; isLiteral: boolean; isQuoted: boolean } | undefined {
124  let text = ''
125  let isLiteral = command[start] !== '~'
126  let isQuoted = false
127  let at = start
128  while (at < command.length && !METACHARACTERS.has(command[at]!)) {
129    const char = command[at]!
130    if (char === '\\') {
131      if (command[at + 1] !== '\n') text += command[at + 1] ?? ''
132      isQuoted = true
133      at += 2
134    } else if (char === "'") {
135      const end = closingMarkOf(command, at, "'", false)
136      if (end === undefined) return undefined
137      text += command.slice(at + 1, end - 1)
138      isQuoted = true
139      at = end
140    } else if (char === '"') {
141      const quoted = doubleQuotedAt(command, at)
142      if (quoted === undefined) return undefined
143      text += quoted.text
144      isLiteral &&= quoted.isLiteral
145      isQuoted = true
146      at = quoted.end
147    } else if (char === '$' || char === '`') {
148      const end = expansionEnd(command, at)
149      if (end === undefined) return undefined
150      text += command.slice(at, end)
151      isLiteral = false
152      at = end
153    } else {
154      if (PATTERN_CHARACTERS.has(char)) isLiteral = false
155      text += char
156      at += 1
157    }
158  }
159  return { text, end: at, isLiteral, isQuoted }
160}
161
162/**
163 * The tokens of a command, or undefined for text this reader does not follow: an open quote, a here-document,
164 * arithmetic in `((`, or a word right before `(`, such as a function or an array.
165 */
166function tokensOf(command: string): Token[] | undefined {
167  const tokens: Token[] = []
168  let fd = ''
169  let at = 0
170  while (at < command.length) {
171    const char = command[at]!
172    if (char === ' ' || char === '\t') {
173      at += 1
174      continue
175    }
176    if (char === '\\' && command[at + 1] === '\n') {
177      at += 2
178      continue
179    }
180    if (char === '#') {
181      const end = command.indexOf('\n', at)
182      at = end === -1 ? command.length : end
183      continue
184    }
185    if (command.startsWith('((', at)) return undefined
186    if ((char === '<' || char === '>') && command[at + 1] === '(') {
187      // Process substitution: command text in place of a path.
188      const end = closingBracketOf(command, at + 1, '(', ')')
189      if (end === undefined) return undefined
190      tokens.push({ kind: 'word', text: command.slice(at, end), isLiteral: false })
191      fd = ''
192      at = end
193      continue
194    }
195    const redirect = REDIRECTS.find(op => command.startsWith(op, at))
196    if (redirect !== undefined) {
197      if (HERE_DOCUMENTS.has(redirect)) return undefined
198      tokens.push({ kind: 'redirect', op: redirect, fd })
199      fd = ''
200      at += redirect.length
201      continue
202    }
203    const separator = SEPARATORS.find(op => command.startsWith(op, at))
204    if (separator !== undefined) {
205      tokens.push({ kind: 'separator', op: separator })
206      at += separator.length
207      continue
208    }
209    const word = wordAt(command, at)
210    if (word === undefined || command[word.end] === '(') return undefined
211    at = word.end
212    if ((command[at] === '<' || command[at] === '>') && !word.isQuoted && FD.test(word.text)) {
213      fd = word.text
214    } else {
215      tokens.push({ kind: 'word', text: word.text, isLiteral: word.isLiteral })
216    }
217  }
218  return tokens
219}
220
221/** Whether a redirection sends standard output to the file its target names. */
222function isOutputRedirect(redirect: Redirect, target: Word): boolean {
223  const fds = OUTPUT_REDIRECTS.get(redirect.op)
224  return fds !== undefined && fds.has(redirect.fd) && (redirect.op !== '>&' || !DUPLICATE.test(target.text))
225}
226
227/** The file of `tee FILE` or `tee -a FILE`; `other` for another tee, whose files are unknown; undefined when the command is no tee. */
228function teeFileOf(words: readonly Word[]): Word | 'other' | undefined {
229  if (words[0]?.text !== 'tee') return undefined
230  const files = words[1]?.text === '-a' ? words.slice(2) : words.slice(1)
231  const [file] = files
232  return files.length === 1 && file !== undefined && !file.text.startsWith('-') ? file : 'other'
233}
234
235/**
236 * The one file that a command's own text sends its output to, as the text names it: the target of `>`, `>>`, `1>`,
237 * `1>>`, `&>`, `&>>`, or `>&`, or the file of `tee FILE` or `tee -a FILE` at the end of a pipeline. Only a plain command
238 * list counts: commands joined by `;`, `&&`, a new line, or a pipe. Undefined when the text names no such file, names two
239 * different ones, names one that is not literal, holds `&` or `nohup`, holds code that may not run (a function, a
240 * condition, a loop, `||`, a group, or a subshell), or names a relative path after a folder change.
241 */
242export function redirectTargetOf(command: string): string | undefined {
243  const tokens = tokensOf(command)
244  if (tokens === undefined) return undefined
245  const targets: Word[] = []
246  let pipeline: Word[][] = [[]]
247  let isCondition = false
248  let changesFolder = false
249  /** Whether the next word is a command, where a word such as `if` or `{` starts compound code. */
250  let isCommandStart = true
251  /** Ends a pipeline: its last command's tee file counts, and an unknown tee follows nothing. */
252  const isEnded = (): boolean => {
253    const tee = pipeline.length > 1 ? teeFileOf(pipeline.at(-1) ?? []) : undefined
254    if (tee !== undefined && tee !== 'other') targets.push(tee)
255    pipeline = [[]]
256    return tee !== 'other'
257  }
258  for (let index = 0; index < tokens.length; index += 1) {
259    const token = tokens[index]!
260    if (token.kind === 'word') {
261      if (token.text === 'nohup' || (isCommandStart && (COMPOUND_WORDS.has(token.text) || token.text.startsWith('{')))) return undefined
262      if (HIDDEN_CODE.has(token.text) || (isCommandStart && token.text === '.')) changesFolder = true
263      isCommandStart &&= PIPELINE_PREFIXES.has(token.text) || ASSIGNMENT.test(token.text)
264      if (FOLDER_CHANGES.has(token.text)) changesFolder = true
265      // Inside `[[ … ]]`, `<` and `>` compare strings.
266      if (token.text === '[[') isCondition = true
267      else if (token.text === ']]') isCondition = false
268      pipeline.at(-1)?.push(token)
269    } else if (token.kind === 'redirect') {
270      if (isCondition) continue
271      const target = tokens[index + 1]
272      // zsh `>!`, `>>!`, and `>&!` force the redirection, so the word after `!` names the file: this reader follows none.
273      if (target?.kind !== 'word' || target.text.startsWith('!')) return undefined
274      index += 1
275      if (isOutputRedirect(token, target)) targets.push(target)
276    } else if (!LIST_SEPARATORS.has(token.op)) {
277      return undefined
278    } else if (token.op === '|' || token.op === '|&') {
279      isCommandStart = true
280      pipeline.push([])
281    } else if (isEnded()) {
282      isCommandStart = true
283    } else {
284      return undefined
285    }
286  }
287  if (!isEnded()) return undefined
288  const names = new Set(targets.map(target => target.text))
289  const [name] = names
290  if (names.size !== 1 || name === undefined || name === '' || targets.some(target => !target.isLiteral)) return undefined
291  return changesFolder && !name.startsWith('/') ? undefined : name
292}
293
294/** Whether a path, with `.` and `..` folded by their text, is `/dev` or under it. */
295function isDevicePath(path: string): boolean {
296  const parts: string[] = []
297  for (const part of path.split('/')) {
298    if (part === '..') parts.pop()
299    else if (part !== '' && part !== '.') parts.push(part)
300  }
301  return parts[0] === 'dev'
302}
303
304/**
305 * A redirect target as an absolute path in the session folder `cwd`. Undefined on Windows, where `cwd` is no POSIX
306 * path and the shell can map a path to another folder, and for a drive path or a path under `/dev`.
307 */
308export function redirectFileOf(target: string, cwd: string): string | undefined {
309  if (!cwd.startsWith('/') || DRIVE.test(target)) return undefined
310  const path = target.startsWith('/') ? target : `${cwd.replace(/\/+$/, '')}/${target.replace(/^(?:\.\/+)+/, '')}`
311  return isDevicePath(path) ? undefined : path
312}
313
hooks/view.ts 290 lines
1import type { AgentJob, Job, JobState, Wakeup } from '../types'
2import { isRunning } from './jobs'
3import {
4  BAND_BUTTONS_COLUMNS,
5  BAND_TITLE_MAX,
6  BAR_CELLS,
7  BAR_MIN_COLUMNS,
8  CELL_LINE_MAX,
9  CELL_LINE_NARROW_MAX,
10  CELL_TITLE_MAX,
11  FAILING_STREAK,
12  NOTE_MAX,
13  PANE_FIXED_COLUMNS,
14  PANE_TITLE_MIN,
15  QUIET_MS,
16  LARGE_READ_EVERY_MS,
17  TICK_MS,
18  RECENT_MS,
19  TABLE_FINISHED_ROWS,
20  TABLE_MAX_ROWS,
21  TABLE_NARROW_COLUMNS,
22  TABLE_RECENT_MS,
23} from './limits'
24import { clip, durationText, etaText, oneLine, remainingByRate } from './output'
25
26/** One job as every view shows it. */
27export type Row = {
28  id: string
29  glyph: string
30  tone: JobState
31  title: string
32  kind: string
33  age: string
34  progress?: string
35  percent?: number
36  bar?: string
37  eta?: string
38  isSlow: boolean
39  last?: string
40  tableLast?: string
41  flag?: string
42  note?: string
43}
44
45/** One colored piece of the band line. */
46export type BandPart = { text: string; tone: 'plain' | 'failed' | 'done' }
47
48/** What the pane draws. */
49export type PaneModel = { header: string; running: Row[]; ended: Row[] }
50
51const GLYPHS: Readonly<Record<JobState, string>> = { running: '●', completed: '✓', failed: '✗', stopped: '■', gone: '◌' }
52
53/** The words before a redirect file's name in a row's note, as in `output: build.log`. */
54const REDIRECT_NOTE = 'output: '
55
56/** Where a shell row's lines come from when that is its redirect file: `output: <file name>`. */
57function redirectNoteOf(job: Job): string | undefined {
58  if (job.kind !== 'shell' || job.redirectFile === undefined || job.redirectBytes === undefined) return undefined
59  return clip(oneLine(`${REDIRECT_NOTE}${job.redirectFile.split(/[\\/]/).pop() ?? ''}`), NOTE_MAX)
60}
61
62const endOf = (job: Job): number => job.endedAt ?? job.startedAt
63
64/** The tool of a subagent's activity summary, `<tool>: <detail>`. */
65const toolOf = (activity: string): string => activity.split(': ', 1)[0] ?? activity
66
67/** Whether none of a subagent's tool calls can reach Downrange: it runs elsewhere, and no call of it ran here. */
68const isUnseenAgent = (job: AgentJob): boolean => job.runsElsewhere === true && job.toolCount === 0
69
70function byAge(jobs: readonly Job[]): { running: Job[]; ended: Job[] } {
71  return {
72    running: jobs.filter(isRunning).sort((a, b) => a.startedAt - b.startedAt),
73    ended: jobs.filter(job => !isRunning(job)).sort((a, b) => endOf(b) - endOf(a)),
74  }
75}
76
77/** The ETA text: rate, then usual duration, then the monitor deadline. */
78function etaOf(job: Job, now: number): { text: string; isSlow: boolean } | undefined {
79  if (!isRunning(job)) return undefined
80  if ((job.kind === 'monitor' || (job.kind === 'shell' && job.successfulReadAt !== undefined)) && job.progress !== undefined) {
81    if (job.progress.percent >= 100) return { text: 'finishing', isSlow: false }
82    const remaining = job.anchor === undefined ? undefined : remainingByRate(job.progress, job.anchor, now)
83    if (remaining !== undefined) return { text: etaText(remaining), isSlow: false }
84  }
85  if (job.kind === 'shell' && job.usualMs !== undefined) {
86    const elapsed = now - job.startedAt
87    return elapsed < job.usualMs
88      ? { text: `${etaText(job.usualMs - elapsed)} usual`, isSlow: false }
89      : { text: `usual ${etaText(job.usualMs)}`, isSlow: elapsed > 1.5 * job.usualMs }
90  }
91  if (job.kind === 'monitor' && job.deadline !== undefined) {
92    return { text: job.deadline > now ? `ends in ${etaText(job.deadline - now)}` : 'ending', isSlow: false }
93  }
94  return undefined
95}
96
97/** The flags of a running job: a subagent's failed calls in a row, else silence for QUIET_MS. */
98function flagOf(job: Job, now: number): string | undefined {
99  if (!isRunning(job)) return undefined
100  if (job.omittedAt !== undefined) return 'awaiting confirmation'
101  if (job.kind === 'agent' && job.activityStatus !== undefined && job.activityStatus !== 'running') return undefined
102  if (job.kind === 'agent' && job.errorStreak >= FAILING_STREAK) return `${job.errorStreak} failed calls in a row`
103  const quiet = now - (job.lastOutputAt ?? job.startedAt)
104  const canBeQuiet = (job.kind === 'shell' && job.successfulReadAt !== undefined && now - job.successfulReadAt <= LARGE_READ_EVERY_MS + TICK_MS) || (job.kind === 'agent' && job.inFlight === 0 && !isUnseenAgent(job))
105  return canBeQuiet && quiet >= QUIET_MS ? `quiet ${durationText(quiet)}` : undefined
106}
107
108function progressCell(job: Job): { text?: string; percent?: number } {
109  if (!isRunning(job)) return { text: job.exitCode === undefined ? undefined : `exit ${job.exitCode}` }
110  if ((job.kind === 'shell' || job.kind === 'monitor') && job.progress !== undefined) {
111    const { label, percent } = job.progress
112    return { text: label.endsWith('%') ? label : `${label} (${Math.round(percent)}%)`, percent }
113  }
114  if (job.kind === 'monitor' && job.eventCount > 0) return { text: `${job.eventCount} event${job.eventCount === 1 ? '' : 's'}` }
115  if (job.kind === 'agent') {
116    const text = [job.activityStatus === 'running' ? undefined : job.activityStatus, isUnseenAgent(job) ? undefined : `${job.toolCount} tool${job.toolCount === 1 ? '' : 's'}`].filter(Boolean).join(' · ')
117    return { text: text === '' ? undefined : text }
118  }
119  return {}
120}
121
122function ageOf(job: Job, now: number): string {
123  if (isRunning(job)) return `${job.isStartSeen ? '' : '≥'}${durationText(now - job.startedAt)}`
124  return job.isStartSeen ? `took ${durationText(endOf(job) - job.startedAt)}` : 'ended'
125}
126
127function rowOf(job: Job, now: number): Row {
128  const eta = etaOf(job, now)
129  const progress = progressCell(job)
130  const redirect = redirectNoteOf(job)
131  return {
132    id: job.id,
133    glyph: GLYPHS[job.state],
134    tone: job.state,
135    title: job.title,
136    kind: job.kind === 'task' ? job.taskType : job.kind,
137    age: ageOf(job, now),
138    progress: progress.text,
139    percent: progress.percent,
140    eta: eta?.text,
141    isSlow: eta?.isSlow ?? false,
142    last: job.lastLine,
143    tableLast: job.kind === 'agent' && job.lastLine !== undefined ? toolOf(job.lastLine) : job.lastLine,
144    flag: flagOf(job, now),
145    note: redirect === undefined || job.note === undefined ? job.note ?? redirect : `${job.note} · ${redirect}`,
146  }
147}
148
149function wakeupText(wakeup: Wakeup, now: number): string {
150  return wakeup.dueAt > now ? `in ${durationText(wakeup.dueAt - now)}` : 'due now'
151}
152
153function headerOf(jobs: readonly Job[], wakeup: Wakeup | null, now: number): string {
154  const running = jobs.filter(isRunning).length
155  const failed = jobs.filter(job => job.state === 'failed' && now - endOf(job) <= TABLE_RECENT_MS).length
156  return [
157    ...(running > 0 ? [`${running} running`] : []),
158    ...(failed > 0 ? [`${failed} failed`] : []),
159    ...(wakeup === null ? [] : [`wakeup ${wakeupText(wakeup, now)} (${oneLine(wakeup.reason)})`]),
160  ].join(' · ')
161}
162
163/** A table cell: one line, cut, with table syntax escaped; backslashes before a pipe double and one more escapes the pipe, so the pipe never ends the cell. */
164const cell = (text: string, max: number): string =>
165  clip(oneLine(text), max)
166    .replace(/(\\*)\|/g, (_, slashes: string) => `${slashes}${slashes}\\|`)
167    .replace(/`/g, "'")
168
169/** Code spans keep backslashes literally. Put escaped table pipes between spans so their escapes cannot change the output. */
170const codeCell = (text: string, max: number): string =>
171  clip(oneLine(text), max)
172    .replace(/`/g, "'")
173    .split('|')
174    .map(part => part === '' ? '' : `\`${part.trim() !== '' && part.startsWith(' ') && part.endsWith(' ') ? ` ${part} ` : part}\``)
175    .join('\\|')
176
177/**
178 * One table row: the last output in backticks, then the flag and the note; a narrow table joins Kind into Job and ETA
179 * into Progress. A subagent row shows its tool alone: the table enters the conversation, and a tool's input can hold
180 * text that the main model never saw.
181 */
182function tableRow(row: Row, isNarrow: boolean): string {
183  const last =
184    [
185      ...(row.tableLast === undefined ? [] : [codeCell(row.tableLast, isNarrow ? CELL_LINE_NARROW_MAX : CELL_LINE_MAX)]),
186      ...(row.flag === undefined ? [] : [row.flag]),
187      ...(row.note === undefined ? [] : [cell(row.note, NOTE_MAX)]),
188    ].join(' · ') || '—'
189  const title = cell(row.title, CELL_TITLE_MAX)
190  const kind = cell(row.kind, CELL_TITLE_MAX)
191  if (!isNarrow) return `| ${row.glyph} | ${title} | ${kind} | ${row.age} | ${row.progress ?? '—'} | ${row.eta ?? '—'} | ${last} |`
192  const progress = [row.progress, row.eta].filter(part => part !== undefined).join(' · ') || '—'
193  return `| ${row.glyph} | ${title} (${kind}) | ${row.age} | ${progress} | ${last} |`
194}
195
196/** The /downrange table: running jobs, then up to 5 jobs that ended in the last 30 minutes; below TABLE_NARROW_COLUMNS, five columns. */
197export function tableOf(jobs: readonly Job[], wakeup: Wakeup | null, now: number, columns = TABLE_NARROW_COLUMNS, named = true): string {
198  const { running, ended } = byAge(jobs)
199  const shown = [...running, ...ended.filter(job => now - endOf(job) <= TABLE_RECENT_MS).slice(0, TABLE_FINISHED_ROWS)]
200  if (shown.length === 0 && wakeup === null) return named ? '**Downrange**: no background work right now.' : 'No background work right now.'
201  const header = [named ? '**Downrange**' : '', headerOf(jobs, wakeup, now)].filter(part => part !== '').join(' · ')
202  if (shown.length === 0) return header
203  const isNarrow = columns < TABLE_NARROW_COLUMNS
204  const more = shown.length > TABLE_MAX_ROWS ? ['', `+${shown.length - TABLE_MAX_ROWS} more: /downrange pane shows all.`] : []
205  return [
206    header,
207    '',
208    isNarrow ? '| | Job | Age | Progress | Last output |' : '| | Job | Kind | Age | Progress | ETA | Last output |',
209    isNarrow ? '| --- | --- | --- | --- | --- |' : '| --- | --- | --- | --- | --- | --- | --- |',
210    ...shown.slice(0, TABLE_MAX_ROWS).map(job => tableRow(rowOf(job, now), isNarrow)),
211    ...more,
212  ].join('\n')
213}
214
215function snippetOf(job: Job, now: number): BandPart {
216  const title = clip(job.title, BAND_TITLE_MAX)
217  if (job.state === 'failed') return { text: `✗ ${title} ${job.exitCode === undefined ? 'failed' : `exit ${job.exitCode}`}`, tone: 'failed' }
218  if (!isRunning(job)) return { text: `${GLYPHS[job.state]} ${title}`, tone: 'done' }
219  const row = rowOf(job, now)
220  const detail = [row.progress, row.eta].filter(part => part !== undefined).join(' ')
221  return { text: `${title} ${detail === '' ? row.age : detail}`, tone: 'plain' }
222}
223
224/** The band line: counts first, then as many job snippets as the width holds. */
225export function bandOf(jobs: readonly Job[], wakeup: Wakeup | null, now: number, columns: number): BandPart[] {
226  const { running, ended } = byAge(jobs)
227  const recent = ended.filter(job => now - endOf(job) < RECENT_MS)
228  const failed = recent.filter(job => job.state === 'failed')
229  const counts: BandPart[] = [
230    ...(running.length > 0 ? [{ text: `● ${running.length} running`, tone: 'plain' as const }] : []),
231    ...(failed.length > 0 ? [{ text: `✗ ${failed.length} failed`, tone: 'failed' as const }] : []),
232    ...(wakeup === null ? [] : [{ text: `◷ ${wakeupText(wakeup, now)}`, tone: 'plain' as const }]),
233  ]
234  const hasProgress = (job: Job) => (job.kind === 'shell' || job.kind === 'monitor') && job.progress !== undefined
235  const queue = [...failed, ...running.filter(hasProgress), ...running.filter(job => !hasProgress(job)), ...recent.filter(job => job.state !== 'failed')]
236  const budget = columns - BAND_BUTTONS_COLUMNS
237  const snippets: BandPart[] = []
238  let used = counts.reduce((sum, part, index) => sum + part.text.length + (index === 0 ? 0 : 2), 0)
239  for (const job of queue) {
240    const snippet = snippetOf(job, now)
241    if (used + snippet.text.length + 3 > budget) break
242    snippets.push(snippet)
243    used += snippet.text.length + 3
244  }
245  const joined = (parts: readonly BandPart[], separator: string): BandPart[] =>
246    parts.flatMap((part, index) => (index === 0 ? [part] : [{ text: separator, tone: 'plain' as const }, part]))
247  const divider: BandPart[] = counts.length > 0 && snippets.length > 0 ? [{ text: ' │ ', tone: 'plain' }] : []
248  return [...joined(counts, '  '), ...divider, ...joined(snippets, ' · ')]
249}
250
251/** The band's sign of its keyboard path, drawn dim in the free room right before its first Button. */
252const BAND_KEY_HINT = 'ctrl+x tab to choose'
253
254/** The characters that Downrange itself draws in the band: glyphs, separators, and bar parts, each one cell wide. */
255const BAND_GLYPHS: ReadonlySet<string> = new Set([...'·—…≥━│╸╺█▉▊▋▌▍▎▏■▰▱◌●◷✓✗'])
256
257/** An upper bound of the display cells of text: one for ASCII and for the band's own glyphs, two for any other character, also a combining, joining, or format character. A wide character and an emoji sequence count at least as wide as a terminal draws them, so a room check never admits text that is wider than it measured. */
258export function cellsOf(text: string): number {
259  let cells = 0
260  for (const char of text) cells += (char.codePointAt(0) ?? 0) < 0x80 || BAND_GLYPHS.has(char) ? 1 : 2
261  return cells
262}
263
264/** The key hint when the row holds it after the band line, before the Buttons, with a one-cell gap; the band line keeps all its room, in display cells. */
265export function bandHintOf(parts: readonly BandPart[], columns: number): string | undefined {
266  const line = parts.reduce((sum, part) => sum + cellsOf(part.text), 0)
267  return line + 1 + cellsOf(BAND_KEY_HINT) + BAND_BUTTONS_COLUMNS <= columns ? BAND_KEY_HINT : undefined
268}
269
270/** Hidden: the user pressed Hide, and no job started after that. */
271export function isBandHidden(jobs: readonly Job[], hiddenAt: number | null): boolean {
272  return hiddenAt !== null && jobs.every(job => job.startedAt <= hiddenAt)
273}
274
275function barOf(percent: number): string {
276  const filled = Math.min(BAR_CELLS, Math.max(0, Math.round((percent / 100) * BAR_CELLS)))
277  return '▰'.repeat(filled) + '▱'.repeat(BAR_CELLS - filled)
278}
279
280/** The pane's model: titles fit the width; bars show from BAR_MIN_COLUMNS. */
281export function paneModelOf(jobs: readonly Job[], wakeup: Wakeup | null, now: number, columns: number): PaneModel {
282  const { running, ended } = byAge(jobs)
283  const titleWidth = Math.max(PANE_TITLE_MIN, columns - PANE_FIXED_COLUMNS)
284  const shape = (job: Job): Row => {
285    const row = rowOf(job, now)
286    return { ...row, title: clip(row.title, titleWidth), bar: columns >= BAR_MIN_COLUMNS && row.percent !== undefined ? barOf(row.percent) : undefined }
287  }
288  return { header: headerOf(jobs, wakeup, now), running: running.map(shape), ended: ended.map(shape) }
289}
290
types/index.d.ts 114 lines
1export type JobState = 'running' | 'completed' | 'failed' | 'stopped' | 'gone'
2
3/**
4 * A progress reading; isGeneric is true for a ratio in no known progress format, which counts only with the
5 * `genericRatios` option on, and false for a known format. A reading that an earlier version saved has no mark.
6 */
7export type Progress = { percent: number; label: string; at: number; isGeneric?: boolean }
8
9/** The start of a rising run of readings; isGeneric is the mark of the reading that started it, absent when an earlier version saved it. */
10export type Anchor = { percent: number; at: number; isGeneric?: boolean }
11
12export type JobBase = {
13  id: string
14  title: string
15  state: JobState
16  startedAt: number
17  /** When a snapshot or agent list most recently confirmed the job running. */
18  lastRunningAt?: number
19  /** Snapshot omission protected by the grace period. */
20  omittedAt?: number
21  isStartSeen: boolean
22  /** When the job first ended. */
23  endedAt?: number
24  /** When its latest end came: a later notification can end a job that already shows ended. */
25  lastEndedAt?: number
26  exitCode?: number
27  note?: string
28  /** The note the launch result gave, which the job shows again when it runs again after an end. */
29  launchNote?: string
30  lastLine?: string
31  lastOutputAt?: number
32}
33
34export type ShellJob = JobBase & {
35  kind: 'shell'
36  ownerAgentId?: string
37  endsWithAgent?: boolean
38  successfulReadAt?: number
39  outputFile?: string
40  /** The file that the command's own text sends its output to, absolute. Each read looks at it and at the output file, and the row shows the one that changed last. */
41  redirectFile?: string
42  /** The size of redirectFile at its last successful read. While it is set, the row's lines and progress come from that file. */
43  redirectBytes?: number
44  outputBytes?: number
45  /** The size at a failed tail, used for its retry interval without marking that output as read. */
46  failedReadBytes?: number
47  lastReadAt?: number
48  /** When the latest end, or a reload of the mod, asked for one more full read of the output; a read that began after it clears it. */
49  finalReadAskedAt?: number
50  progress?: Progress
51  anchor?: Anchor
52  usualMs?: number
53  historyKey?: string
54}
55
56export type MonitorJob = JobBase & {
57  kind: 'monitor'
58  eventCount: number
59  deadline?: number
60  progress?: Progress
61  anchor?: Anchor
62}
63
64export type AgentJob = JobBase & {
65  kind: 'agent'
66  activityStatus?: 'pending' | 'running' | 'waiting' | 'idle'
67  /** Its launch said it runs outside this process, as a remote subagent or a teammate in a terminal pane of its own, so its tool calls do not reach Downrange. */
68  runsElsewhere?: boolean
69  toolCount: number
70  inFlight: number
71  errorStreak: number
72  /** When the newest tool call started, independently of completion times. */
73  lastToolStartedAt?: number
74  /** Failure times in the streak while other tool calls can still settle. */
75  concurrentFailureTimes?: number[]
76  /** Latest success among those concurrent completions; cleared when no call remains. */
77  lastSuccessAt?: number
78}
79
80export type WorkflowJob = JobBase & { kind: 'workflow' }
81
82export type TaskJob = JobBase & { kind: 'task'; taskType: string }
83
84export type Job = ShellJob | MonitorJob | AgentJob | WorkflowJob | TaskJob
85
86/** Rows and observation times survive together, including empty snapshots and removed task IDs. Each time list keeps every entry of a kept row, and the MAX_BARRIERS newest entries (hooks/limits.ts) of tasks with no row. */
87export type JobsState = {
88  items: Job[]
89  observed: [id: string, at: number][]
90  removed: [id: string, at: number][]
91  snapshotAt: number | null
92  /** Membership at snapshotAt, used only to reconcile absences. */
93  snapshotIds?: string[]
94  /** Stored child notification identities already applied. */
95  noticeRows?: string[]
96}
97
98/** The pending wakeup: when it fires, why, and when ScheduleWakeup set it. */
99export type Wakeup = { dueAt: number; reason: string; setAt: number }
100
101/** The latest wakeup change, including cancellation when no wakeup remains. */
102export type WakeupState = { pending: Wakeup | null; updatedAt: number | null }
103
104declare module 'claude-code' {
105  interface PluginState {
106    'downrange': {
107      jobs: Shaped<JobsState>
108      wakeup: Shaped<WakeupState>
109      bandHiddenAt: number | null
110      lastAnswered: { text: string; at: number } | null
111    }
112  }
113}
114