SLOPSHOPPER

delegate

A hand_off tool: Claude gives a self-contained task to a cheaper worker model (Haiku) that stays warm between tasks, with where the change reaches from the…

panerowsguardcommandprompttool
v0.4.4MITupdated 2026-10-18HarzerHeribert/claude-modpack/delegate
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · delegate
│ ┃ delegate ✕ › fix the failing auth test and add an audit log call │ ┃ No hand-offs in this session yet. │ ⏺ 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 │ │ › /delegate │ ⎿ delegate: No hand-offs logged yet. │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · delegate
No hand-offs in this session yet.
README

<img alt="claude-modpack: eight Claude Code mods, each measured on real tasks before it shipped, built to work together. statusline, smart-compact, delegate, cells, eta, seek, draft, route." src="assets/banner-light.svg" width="880">

<h3>Install in Claude Code</h3>

/plugin install modpack --marketplace HarzerHeribert/claude-modpack

Answer y, then run /modpack:setup to set everything up step by step.<br> Or from your terminal, guided through both steps: bash <(curl -fsSL https://raw.githubusercontent.com/HarzerHeribert/claude-modpack/main/install.sh) (read it first).<br> Prefer doing it by hand? SETUP.md has every step.

Release License: MIT Claude Code 2.1.296 Quality: no loss seen 8 mods Lab notes

Claude Code with fewer wasted steps, and a clear view of what it's doing.

Eight free add-ons that work on their own. One command installs them, and there is nothing new to learn.<br> Every number on this page was measured, including where it doesn't help.

Install · What you get · Honest numbers · The site: ideas and evidence · Lab notes · Questions

<img alt="Animated: the cost per job with and without the pack, as bars that grow: in a long session $0.44 against $0.29 (−33%), bigger jobs in a fresh session $0.55 against $0.51 (−8%), quick questions in a fresh session $0.23 against $0.24 (+2%, about even); then what was measured since: −17% on 12 jobs beyond code with the pack as 0.7.0 ships it, −22% on a feature a cheaper model builds from your plan, −11% writing to a word limit in one draft, and compacting a 70k-token session in 4 ms instead of 15 s with the summary written ahead; then 2,550 graded runs, no quality loss seen." src="assets/numbers.gif" width="880">

Why it saves

Every time Claude takes a step (reads a file, makes an edit, runs the tests), it reads your whole conversation again. That re-reading is most of what a session costs, and it grows the longer you work. So the cheapest step is the one Claude never has to take.

The pack takes out steps that need no thinking. Several edits and their test run as one step. A search for a name in your code comes back with the exact answer. And when you approve a plan, a cheaper model does the building while Claude checks the result. The work comes out the same; Claude just gets there in fewer round trips.

What you get

<img alt="A real Claude Code session with the pack, six moments cut together, each with a caption: a bar and a finish time for long jobs (eta); several edits and the test run in one step (cells); an approved plan built by a cheaper model and checked (delegate); asked for at most 300 words, one draft that fits (draft); compacted at once, its summary written ahead, with that setting on (smart-compact); every mod with a switch that acts at once (/mods). Under the prompt throughout, the status line: model, context, the plan's 5-hour and weekly use." src="assets/demo-tour.gif" width="880">

Always know where your plan stands

One line under the prompt: the model, how full the conversation is, and how much of your plan's 5-hour and weekly limits you have used, with when each one resets. Long jobs get a progress bar and a finish time above the prompt. And when you step away, your session is kept ready, or tidied up before it goes cold, whichever costs less; the line tells you which.

These are the statusline, eta and smart-compact mods.

No waiting when a conversation gets long

When a conversation fills up, Claude Code summarizes it so the work can go on, and you wait while it writes: 15 seconds at 67k tokens and 102 seconds at 436k in our sessions. Turn on compacting after a turn (afterTurnPayback) and smart-compact compacts a long conversation shortly after a turn ends, when that pays back soon. Its summary is written in the background as the turn ends, so the compaction itself takes 3 to 5 ms. Against /compact from the same session, the next job cost 14% less, at full marks in 16 of 18 runs against 14 of 18 (lab note 15).

That's the smart-compact mod (compacting after a turn is off until you turn it on).

Several changes in one step, and you can still undo them

Claude makes several edits and runs the tests as one step, and you watch each part happen in a live card. Every edit still goes through Claude Code's own edit tool: you see each change, and /rewind undoes it.

That's the cells mod.

A cheaper model builds what you approved

When you approve a plan, Claude hands the building to Sonnet, a cheaper model, and checks its work before reporting back. In our tests that made a feature 22% cheaper and new tests 26% cheaper, with every result correct (corrected 16 Oct: worker costs were undercounted; it was 27% and 33%). You can watch the helper work in its card, and /delegate shows every step it took.

That's the delegate mod.

Optional: feature work goes to the cheaper model by itself

An optional mod, off until you turn it on, and the one that needs an outside classifier model: an API key for TypeSafe's Jev or ScaleDown. That classifier reads each prompt you send. Only when it is feature or test work is Claude told to hand it straight to delegate's worker: the worker reads the code and builds, delegate checks the result itself, and Claude reports after two steps of its own. On a TypeScript feature that made the job 51% cheaper and 42% faster than the pack alone, with every result correct (corrected 16 Oct: worker costs were undercounted; it was 57%); everything else stays with Claude. The classifier is an outside service (TypeSafe's Jev or ScaleDown), so this is off until you turn it on and give it a key: from then on, the start of each prompt goes to it, in the US, to be classified. In real sessions it saves less than on the benchmark: in the author's, the turns it would hand off were 13% of the spend, so at most 4 to 7% saved, nearer 3 to 6% with the hand-offs that were not feature or test work; more if most of your work is building features, and less if you already plan in plan mode, where delegate hands an approved plan to the worker without route.

That's the route mod (optional; it needs an outside classifier and its key).

Fewer, leaner steps when Claude looks around

Before Claude changes anything, it looks around your code, and everything it reads stays in the conversation, read again with every later step. Beside the first prompt you send in a session, and again as the session grows, the pack passes on your preference to look around in few, lean steps: the searches it can already name in one go, the lines that matter rather than whole files, nothing read twice. Against the pack without it, that made jobs 12% cheaper in a fresh session, 16% on a TypeScript library and 10 to 16% in long sessions, at the same quality. It runs on your machine and sends nothing anywhere.

That's the seek mod.

Writing to a word limit, in one draft

Ask for a text of at most so many words, and Claude tends to write all of it, count, find it a third too long, and write all of it again, several times. With a limit in your prompt, the pack passes on your preference to give each part its share and aim a little under, to keep every fact you asked for (and say so if they cannot all fit), and after each write it shows the word count and, when the text is well over, each section's target, so one more write lands it. On six writing tasks that made them 11% cheaper at the same quality. Without a word limit in your prompt it does nothing.

That's the draft mod.

You stay in control

/mods lists the eight and turns any of them on or off, right away, with a click or by typing, such as /mods statusline off. When something can't fully work on your computer, the pack tells you once, with the command that fixes it. When another plugin's permission or notification hook hangs (Warp's does outside the Warp terminal), the pack stops waiting for it after 10 seconds, so Claude Code can't freeze, and tells you which one it was. It keeps itself up to date (or only lets you know, if you prefer).

That's the modpack plugin, which installs the other eight.

Install

In Claude Code, type:

/plugin install modpack --marketplace HarzerHeribert/claude-modpack

Answer y to add the marketplace, then pick where to install it. That installs all eight. Then, in the project you work in:

/modpack:setup

It checks your machine, asks once what you want on (route stays off unless you say yes), sets each mod up in order, and checks that each one works. Done. /mods shows what's on.

By hand instead: SETUP.md has every step.

Each mod installs on its own the same way, for example /plugin install smart-compact --marketplace HarzerHeribert/claude-modpack. delegate and cells use a language server where you have one installed and work without it: Language servers.

Honest numbers

We measured the pack on Claude Opus 5.5 doing seven real jobs in an open-source project (click), from a one-line lookup to building a feature, on two days, 13 and 15 October. Every result was checked by hidden tests. The same jobs ran with the pack and without it, and we compared what they cost at API prices.

Cost per jobWithoutWith the pack
In a long session (Claude has already read the whole project)$0.44$0.29−33%
Bigger jobs in a long session (a feature, tests, a rename)$0.91$0.57−37%
In a fresh session$0.37$0.35−4%
Bigger jobs in a fresh session$0.55$0.51−8%, and 15% faster
Quick questions in a fresh session$0.23$0.24+2%, about even
  • Where it doesn't help: quick questions in a brand-new session. The pack's tools come along with every step, and a three-step lookup has little to save: 5% more one day, 4% less the next, about even.
  • Writing tests: the pack alone saves nothing there (−2%, 12 runs each way): every step waits on the last test run. route hands it to the worker instead (−36% to −46%, below).
  • Nothing got worse: all 224 runs behind this table produced a correct result, with the pack and without.
  • The 95% ranges: long session −39% to −27%; fresh −7% to −1%; bigger jobs fresh −12% to −3%; quick questions −1% to +6%. The long session is two runs per job each day, and the days differ: −26% on one, −40% on the other, mostly from the feature (plain needed 12 requests one day, 16 the next).
  • The same on other code: on a TypeScript library (chrono) the pack cost 7.6% less (−12% to −3%) and took fewer steps on all eight jobs. On 30 real bug reports from SWE-bench Verified it fixed 30 against 29, with 23% fewer steps; the cost, −12%, is not certain at one attempt per bug (−33% to +12%). On PocketBase, Trail-Code's own 15 tasks: −21% with 0.6.0, at one run per task as Trail-Code ran them (the average task −18%, between −27% and −8%; lab notes 3).
  • Beyond click, new in 0.7.0: on 12 tasks in a React and TypeScript app, a small service to run and fix, and documents and data to write and analyse, the pack as 0.7.0 ships it cost 17% less than plain Claude Code (−22% to −12%, 48 runs, all but four at full marks): web code −28%, operating software −8%, prose and data −5% (−10% to +1%), so prose is where it does least.
  • A plan handed to a cheaper model: −22% on a feature, −26% on tests, against Opus doing it alone with the pack (16 runs, all correct).
  • With route turned on: a further −14% on click's eight jobs (route 0.1). With 0.6.0's verified hand-offs, against the pack in the same batch on TypeScript: a feature −51% and 42% faster, writing tests −36%; Claude itself takes 2 steps instead of 6 to 11 (24 runs, all correct).
  • Corrected 16 Oct: worker costs were undercounted. A worker's transcript records its output as each response began, and our bench counted it from there, so every number with a hand-off in it came out too good. Corrected from Claude Code's own totals: a planned feature −22% (was −27%), tests −26% (was −33%), route on click −14% (was −17%), the TypeScript feature −51% (was −57%). Every saving is still a saving, a smaller one. The table above has no hand-offs and did not change. Each claim, old and new: CORRECTION-1016.md.
  • With seek (new in 0.6.0, measured against the pack rather than plain Claude Code, so not in the table above): −12% fresh on click, −16% on TypeScript, −10% to −16% in long sessions (seek, below).
  • With draft (new in 0.8.0, measured against the pack): −10.7% on six writing tasks, from −14.9% to −6.3%, at the same quality (0.987 against 0.990), and no run over its word limit (draft, below).
  • Two days, two versions: the 13 October runs used the pack as of 0.3.0 and Claude Code 2.1.296, the 15 October runs 0.4.0 and 2.1.299. What changed in the pack between them (on/off switches, setup, messages shown when a worker is refused) never reached the model in these runs: its first request was 26.7k tokens plain and 30.6k with the pack on both days. The second day's plain runs were done twice, because the first try had the pack installed by mistake: lab notes 8 tells how we caught it.

<img alt="Cost per task, plain Claude Code against the modpack, in a fresh session and in a session already holding 216k tokens." src="assets/cost-light.svg" width="880">

<img alt="Model requests per task, plain Claude Code against the modpack: 3.2 against 5.6 in a session already holding 216k tokens." src="assets/requests-light.svg" width="880">

<img alt="Cost per task, Opus alone against a plan handed to a worker: feature work 22% less, tests 26% less." src="assets/delegate-light.svg" width="880">

How we measured, every run's record, and the scripts to run it all again are in benchmarks/, starting with SUITE.md. What we tried that did not make it is in the lab notes (as Markdown), among it a 10,000-star code graph, Trail-Code, which installed into Claude Code saved nothing we could measure: alone −1.8% against plain Claude Code (−5.9% to +2.7%), beside the pack +13% (lab notes 3). Not measured: what smart-compact saves you while you're away (worked out from prices, not tested) and Windows.

Questions

Will Claude's work get worse? Not that we have seen. Every run is graded by hidden tests or fixed rules, and the same jobs run with the pack and without it. In the batches that did both, 6 of 349 runs without the pack and 8 of 407 with it fell short of full marks. Of all 2,586 graded runs that ship with this release, 96 fell short: seven on one TypeScript explanation whose answers sometimes leave out one term of its list, with the pack and without; five with Trail-Code, whose hook replaced the answer; seven with Haiku as the main model, in two experiments we dropped; one TypeScript type question; and six on PocketBase. There, two questions left out terms of their answer key in both arms, and plain Claude Code missed one term of a third. On that third question the pack scored 0: lsp-plus held a search while Go's language server was still loading, Claude Code moved it to the background, and Claude's last message answered the notice that followed instead of the question. lsp-plus 0.4.2 then gave the language server 8 seconds at most, and lsp-plus has since left the pack. Seven more are 0.7.0's: five on one prose revision whose checks are strict, in every arm, plain Claude Code too; one runbook and one rename. And 22 are 0.8.0's: nine in the writing tasks, three with draft and three without in one batch and three more with draft in the next (a removed command named as current, a follow-up missing from a brief); thirteen in the compaction benchmark, spread over every arm, ten of them one option check of one task and three a rename that kept a deprecated alias of the old name. Six are 0.9.0's, from the benchmark of the summary written ahead against /compact: five that same option check, two with the summary written ahead and three with /compact, and one rename under /compact that left the old name in one file. And 29 ship with the lab notes of 19 October: 28 from draft's first writing batches, with draft and without, graded before three of their graders were fixed (a removed command named in a history sentence, a code-span check, a stale base; lab note 13), and one with steer, a system-prompt experiment we did not ship. And six ship with 0.9.3, from the next-task benchmark of two shorter summaries, three each: the same option check three times, and the rename three times (twice a docstring note naming the old name, once the old name left in one file). Apart from the PocketBase run, the pack changes how many steps Claude takes, not whether it gets there.

Where did lsp-plus go? It left the pack in 0.7.0. Measured twice, 120 runs on renames and traces in click, a TypeScript library and a React app and on web, operating and prose work, the pack cost no more without it (−1.8% and −1.6%, both ranges across zero), and it kept a language server running per language in every session, hundreds of MB each. Its answer to a grep came from the language server, and in untyped code a server finds about half the uses of a name, where a plain word search finds them all (lab notes 12). The update removes it once and says so in one line; installed again on purpose, it stays. Its last version, 0.4.2, is in this repository's v0.6.1 tag.

Is it safe? It's open source (MIT), so every line is here to read. Code that Claude runs inside a cell runs in a sandbox with no network access. Edits go through Claude Code's own edit tool, so your usual permission prompts and /rewind still apply. The pack sends nothing anywhere except a once-a-day check on GitHub for a new release, which you can turn off, and, only if you turn route on, the start of each prompt to the classifier you chose (TypeSafe or ScaleDown).

Does the pack add anything to my prompts? Your prompt itself is never changed. Beside it, Claude can get a short note in your name: seek's setting to look around in few, lean steps (with the first prompt of a session, and again after a compaction), and, only if you turn route on, the order to hand feature or test work to the worker; delegate gives the same order when you approve a plan of 20 lines or more. A subagent Claude starts gets cells' and seek's settings at the end of its task, since your prompts never reach it. /mods seek off and /mods route off stop theirs; subagents: false in cells or seek stops theirs for subagents.

I'm on a Pro or Max plan. Does this help me? You don't pay per job, but every step counts against your 5-hour and weekly limits. We measured cost at API prices, not plan usage, so we can't promise a number. Fewer, lighter steps should leave more of your limits, and the status line shows exactly where you stand.

Does it work in the desktop app? Yes, cards, progress and the status line included. One limit: in auto mode the desktop app doesn't let an add-on start a helper model, so plans aren't handed off there (turn route off there too); in manual mode, or in the terminal, they are. Windows hasn't been tested yet.

How do I turn it off? /mods switches each part off or on, right away. /plugin removes them.

Were any of these numbers wrong? Yes, the hand-off ones. On 16 October we found that our bench had counted a worker's output only as each of its responses began, so every saving that involved a worker came out too large: a planned feature was −27% and is −22%. They are corrected here, the old and new number side by side in CORRECTION-1016.md. The pack-against-plain numbers had no workers and stand.

What does it cost? Nothing. It's free and open source.

The mods in depth

One line under the prompt, drawn in Claude Code's own hint row so the mode indicator and the status share it:

⏵⏵ auto mode on       Opus 5.5 xhigh   412k ━━━━━━──────   5h 34% ↺2h14   7d 12% ↺3d   ● ready · cold in 44m
  • Its own segments: the model and its effort, the context against the auto-compact point (a highlight sweeps along it while Claude works), and how much of the plan's 5-hour and 7-day windows is used, with when each resets: quiet under half, plain past it, amber from 80%.
  • Other mods' segments: a mod calls $.statusline.set({ id, priority, parts, short?, side? }) and lists statusline among its dependencies. A part is text, a countdown ({ until, prefix?, warnMs? }, counted by the line) or a spinner ({ spin: true }), so a mod sends a segment when it changes, not every second. $.statusline.remove({ id }) takes it off.
  • Narrow terminals drop the least important first: the model, then the windows' resets, the windows, the gauge, and another mod's countdown before its own words.
  • Published pages: a pill on the left lists the pages this session published with the Artifact tool.
  • Phone and IDE: where no line is drawn, the same line is the session's status text, in a session the phone or IDE hosts. A terminal session watched over Remote Control (/rc) from the desktop app, the web or the phone gets no mod drawing and no status text as of Claude Code 2.1.296.
OptionDefault
enabledtruefalse turns the mod off; /mods does it for all of the pack.
placementlinetail: appended dim to Claude Code's hint. band: above the prompt. off.
limitsalwayshalf: a plan window only once it is half used. off: never.
animatetrueThe highlight along the gauge.

An idle session's prompt cache expires after its TTL: 1 hour on a subscription, 5 minutes in ove

Source 4 files
hooks/register.tsx 1028 lines
1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, Timer } from 'claude-code'
3
4import {
5  DEFAULT_REVIEW,
6  WORKER_RULES,
7  REVIEW_PROMPT,
8  anchorOn,
9  capLines,
10  costUsd,
11  TEXT_SEARCH_CAP,
12  definitionPattern,
13  gitDefinitionPattern,
14  describeReach,
15  parseWorker,
16  diffStat,
17  filesOf,
18  kTokens,
19  mainAloneUsd,
20  money,
21  parseFocus,
22  parseLocations,
23  parseVerdict,
24  places,
25  reviewRoute,
26  summarize,
27  tokensOf,
28  workerPrompt,
29  parseVerify,
30  parsePlaces,
31  checkPlace,
32  readVerdict,
33  testCounts,
34  verdict,
35} from './plan'
36import type { Anchor, HandOff, Kind, Mode, Place, Reach, Usage } from './plan'
37import type { DelegateJob } from '../types'
38import { FRAME_MS, actionLabel, jobCard, pane, startingLine, usd, verb } from './view'
39import type { Elements } from './view'
40
41type Engine = EngineInterface
42
43const TOOL = 'hand_off'
44const AGENT: { [K in Kind]: string } = { read: 'reader', mechanical: 'mechanic', build: 'builder' }
45const MODE: { [K in Kind]: Mode } = { read: 'explore', mechanical: 'edit', build: 'edit' }
46// The engine's tool-name type lists only tools it knows; implement_spec is registered at run time by `framing`.
47const HAND_OFF_TOOLS = ['mcp__delegate__hand_off', 'mcp__delegate__implement_spec'] as unknown as 'mcp__delegate__hand_off'[]
48const WORKER_TOOLS: { [K in Kind]: string[] } = {
49  read: ['Read', 'Grep', 'Glob', 'LSP', 'Bash'],
50  mechanical: ['Read', 'Grep', 'Glob', 'LSP', 'Bash', 'Edit', 'Write'],
51  build: ['Read', 'Grep', 'Glob', 'LSP', 'Bash', 'Edit', 'Write'],
52}
53const MAX_FOCUS = 6
54const DIFF_LINES = 300
55/** A report that reaches the main model as a message or beside another tool's result: the diff's start, then its path. */
56const LATE_DIFF_LINES = 40
57
58const RETURNS = {
59  tool: 'It returns when the worker is done (usually within a minute or two) with its report and evidence the tool gathered itself',
60  message: "It returns at once; the worker's report arrives as a notification when it finishes, with evidence the tool gathered itself",
61}
62const describe = (wait: Wait) => `Outsources one self-contained task to a cheaper worker model. ${RETURNS[wait]}: for edit work the diff and the result of a check command it runs, where the change reaches (references, callers and doc mentions from the language server), and what the work cost.
63
64Each of your own steps re-reads your whole context; a worker's steps read only its own small one, but the hand-off itself takes you two to five steps (writing the task, reading the evidence, finishing). Measured on real tasks at the same quality: a feature from a precise spec and tests to a coverage target cost 23-35% less handed off, in a fresh session and in one holding 233k tokens; lookups, traces, explanations and small edits cost more handed off, up to 3.6 times as much in a session that already knows the code.
65
66Outsource work that is fully specified and would take you many steps in this session, where only the result matters to you: implementing a feature from a precise spec, writing tests to a target, one change applied across many files.
67The "kind" you give decides the worker: read changes no file (find, list, trace, explain); mechanical changes files and the task spells out every change (a rename, one edit applied everywhere, moving code); build changes files and something is left to work out (a feature from a spec, tests to a target).
68Keep in house: lookups, tracing, explanations and small edits, design decisions, subtle debugging, and anything that depends on what was decided in this conversation unless the task states it. The worker knows only the task text.
69
70Write the task so it stands alone: the goal, the files or symbols, constraints, and what done means. Name the symbols or file:line places it touches in focus so the tool can find where they are used. For edit work, give a check command (tests, a type check) that proves it. Don't edit the files an edit hand-off works on until its report is in.
71
72Judge what comes back by the diff and the check output, not by the worker's report. A follow-up task reaches the same worker while it is warm, its earlier reading still in its context. With background, the call returns at once and the report arrives as a notification: use it when you have other work to do meanwhile.`
73
74const CAPABILITY = 'implement_spec'
75const describeCapability = () => `Implements a precisely specified change and proves it. Give it the spec (what to build, where, the constraints, what done means) and a check command (tests, a type check); it makes the change in the working tree and returns the diff, the check's output, where the change reaches (references, callers and doc mentions from the language server), and what it cost. Use it for work that is fully specified and takes many steps: a feature from a precise spec, tests to a coverage target, one change applied in many places. Keep lookups, tracing, explanations, design decisions and debugging yourself. Judge the result by the diff and the check output. Don't edit the files it works on until it returns.`
76
77type Wait = 'tool' | 'message'
78/** The longest a hand-off holds the tool call; past it the report comes as a message. */
79const HOLD_MS = 30 * 60_000
80
81const INPUT_SCHEMA = {
82  type: 'object',
83  properties: {
84    task: { type: 'string', description: 'The whole task, standing alone: goal, files or symbols, constraints, what done means.' },
85    kind: {
86      type: 'string',
87      enum: ['read', 'mechanical', 'build'],
88      description:
89        'read: changes no file (find, list, trace, explain). mechanical: changes files, and the task spells out every change (a rename, one edit applied everywhere, moving code). build: changes files, and something is left to work out (a feature from a spec, tests to a target).',
90    },
91    focus: {
92      type: 'array',
93      items: { type: 'string' },
94      description: 'Symbols or places the task touches: "name", "path#name" or "path:line". The tool asks the language server where they are used.',
95    },
96    check: { type: 'string', description: "Edit mode: a shell command that proves the work, best the project's whole test suite (and its type check). Run by this tool after the worker finishes, with the worker's own new tests run on their own beside it." },
97    fresh: { type: 'boolean', description: 'Start a new worker instead of continuing the warm one.' },
98    background: {
99      type: 'boolean',
100      description: 'Return at once and let the report arrive as a notification, so you can work on something else meanwhile. Without it the call waits for the report, which costs you fewer requests.',
101    },
102  },
103  required: ['task', 'kind'],
104}
105
106const CAPABILITY_SCHEMA = {
107  type: 'object',
108  properties: {
109    spec: { type: 'string', description: 'The whole spec, standing alone: goal, files or symbols, constraints, what done means.' },
110    check: { type: 'string', description: 'A shell command that proves the work.' },
111    focus: INPUT_SCHEMA.properties.focus,
112    background: INPUT_SCHEMA.properties.background,
113  },
114  required: ['spec', 'check'],
115}
116
117const workers: { [K in Kind]: { model: string; effort?: string } } = { read: { model: 'haiku' }, mechanical: { model: 'haiku' }, build: { model: 'sonnet' } }
118let framing: 'hand_off' | 'capability' = 'hand_off'
119let review = 'inline'
120let reuseMs = 50 * 60_000
121let capTokens = 60_000
122let wait: Wait = 'tool'
123let afterPlan: 'hand_off' | 'off' = 'off'
124// Set once auto mode (the desktop app) has refused a worker start; reset in session.start.
125let autoModeBlocked = false
126
127export function isAutoModeBlock(deny: string): boolean {
128  return /auto mode classifier gave no verdict/i.test(deny)
129}
130
131/** For tests only. */
132export function setAutoModeBlocked(v: boolean): void {
133  autoModeBlocked = v
134}
135
136/** The tool result for a refused worker start; the first auto-mode refusal also tells the person. */
137export async function refusedStart($: Engine, deny: string): Promise<{ text: string }> {
138  if (!isAutoModeBlock(deny)) return { text: `The worker did not start: ${deny}` }
139  if (!autoModeBlocked) {
140    autoModeBlocked = true
141    await $.ui.log('delegate: auto mode in the desktop app cannot judge a worker the mod starts, so hand-offs are off here. They run in manual permission mode (no prompt) or in the terminal.')
142  }
143  return {
144    text: 'Not handed off: auto mode in this app cannot judge a worker that the delegate mod starts (the terminal can). Do this task yourself. Tell the person once, in one sentence: hand-offs run in manual permission mode, where they start without a prompt, or from the terminal.',
145  }
146}
147
148/** The order attached to an approved plan: implement it through a hand-off. */
149export function planOrder(framing: 'hand_off' | 'capability'): string {
150  const how = framing === 'capability' ? 'implement_spec' : 'the hand_off tool, kind build (kind mechanical when the plan spells out every change)'
151  return `The person approved this plan, and their setting for approved plans is: implement it through ${how}. The approved plan is the worker's spec: write it into the task so it stands alone, with the project's whole test suite as the check. The tool checks the worker's work itself; when its result says Verified and the diff is what the plan asked for, report to the person without running anything again.`
152}
153
154type Input = { task: string; kind: Kind; mode: Mode; focus: string[]; check?: string; fresh: boolean; background: boolean; worker: string; effort?: string }
155const label = (model: string, effort?: string) => (effort ? `${model} ${effort}` : model)
156type Done = { answer: string; reason: string }
157
158/** One hand-off from the tool call to the notice of its worker's end. */
159type Job = {
160  n: number
161  toolUseId: string
162  agentId: string
163  input: Input
164  started: number
165  reused: boolean
166  reach: Reach[]
167  reachText: string
168  /** Its requests so far, what they cost in tokens, and the size of the last one (the worker's context). */
169  requests: number
170  usage: Usage
171  lastContext: number
172  workerModel?: string
173  /** Each file the worker edited, as it was before its first edit (null: it did not exist). */
174  originals: Map<string, string | null>
175  /** The work tree when the hand-off began (a commit of it), and the untracked files then; absent outside git. */
176  gitBase?: string
177  untracked?: string[]
178  /** The report reached the main model; the worker's turn may end a moment later. */
179  reported: boolean
180  /** The tool call is holding the turn for this worker. */
181  holding: boolean
182  /** The report went back as the tool's result: the worker's own hand-back message is dropped. */
183  returned: boolean
184  /** The worker's report, from its hand-back message when that came first. */
185  handback?: string
186  done?: Done
187  ended?: number
188  /** eta is showing this hand-off: the status file it reads, and what has been written to it. */
189  eta?: { file: string; text: string }
190  /** Files the main session or another worker edited while this one ran: not this worker's, so not in its diff. */
191  others: Set<string>
192}
193
194type Warm = { agentId: string; at: number; requestTokens: number }
195
196/** The worker kept warm between hand-offs, one per kind. */
197const warm = new Map<string, Warm>()
198const warmKey = (input: Input) => input.kind
199/** Hand-offs whose worker is running or whose notice has not arrived yet, by the worker's agent id. */
200const jobs = new Map<string, Job>()
201/** Hand-offs reported, written to the log when the main turn that received them ends. */
202let records: HandOff[] = []
203/** Files reported edit hand-offs changed: the main model's later edits to them count as rework. */
204let touched = new Map<string, HandOff>()
205/** Workers whose end this mod reported itself (no notice came); a host notice for one is dropped. */
206const handedOver = new Set<string>()
207/** The main loop's turn while one runs. A plugin prompt sent then waits above the person's input until the turn ends. */
208let mainTurn: string | undefined
209/** Reports whose worker ended while the main turn ran: they ride on its next tool result or on the host's notice of the
210 * worker's end, whichever comes first, and only an idle session gets one as a prompt. */
211const pending = new Map<string, string>()
212/** Workers whose end delegate is reporting, while it diffs and runs the check: their report is not ready yet. */
213const preparing = new Set<string>()
214let count = 0
215let dir = ''
216/** True while this mod continues a warm worker: resuming an agent needs its type offered. */
217let resuming = false
218
219// What the card and the pane draw: each hand-off by its tool_use_id, and the one the pane has open.
220const VIEW = atom({ plugin: 'delegate', key: 'jobs' } as const, [] as DelegateJob[])
221const WATCHING = atom({ plugin: 'delegate', key: 'watching' } as const, null as string | null)
222const PANE = 'delegate'
223let frames: Timer | undefined
224
225const LOST = 'lost track: the mod reloaded or Claude Code restarted while it ran'
226
227/** The running entries whose worker this mod no longer knows (it reloaded, or Claude Code restarted). */
228export function lostEntries(view: readonly DelegateJob[], liveAgentIds: ReadonlySet<string>): DelegateJob[] {
229  return view.filter(j => j.state === 'running' && !liveAgentIds.has(j.agentId))
230}
231
232/** Ends the cards of hand-offs whose worker report can no longer be matched, and their eta rows; never throws. */
233export async function reconcile($: Engine): Promise<void> {
234  try {
235    const lost = lostEntries(await read($, VIEW), new Set(jobs.keys()))
236    if (lost.length === 0) return
237    const now = await $.clock.now()
238    const ids = new Set(lost.map(j => j.toolUseId))
239    await update($, VIEW, list => list.map(j => (ids.has(j.toolUseId) && j.state === 'running' && !jobs.has(j.agentId) ? { ...j, state: 'failed' as const, endedAt: now, note: LOST } : j)))
240    for (const j of lost) {
241      try {
242        const file = `${dir}/hand-off-${j.n}.status`
243        const text = await $.fs.read(file).catch(() => undefined)
244        if (text !== undefined) await $.fs.write(file, `${text}${text === '' || text.endsWith('\n') ? '' : '\n'}failed · lost track\n`)
245      } catch {
246        // eta is optional.
247      }
248    }
249    await animate($)
250  } catch {
251    // A stale card is better than a failing hook.
252  }
253}
254
255async function setView($: Engine, toolUseId: string, change: (j: DelegateJob) => Partial<DelegateJob>) {
256  await update($, VIEW, list => list.map(j => (j.toolUseId === toolUseId ? { ...j, ...change(j) } : j)))
257}
258
259/** Redraws four times a second while a worker runs; at rest nothing moves. */
260async function animate($: Engine) {
261  const running = (await read($, VIEW)).some(j => j.state === 'running')
262  if (running && frames === undefined) frames = $.clock.every(FRAME_MS, () => $.ui.invalidate('ui.render'))
263  else if (!running && frames !== undefined) {
264    frames.cancel()
265    frames = undefined
266    $.ui.invalidate('ui.render')
267  }
268}
269
270async function openPane($: Engine, toolUseId?: string) {
271  if (toolUseId !== undefined) await update($, WATCHING, () => toolUseId)
272  await $.ui.open({ id: PANE, title: 'Hand-offs' })
273}
274
275/** The worker's report out of its hand-back frame, unindented. */
276function reportText(text: string): string {
277  const body = /The report follows:\n([\s\S]*?)(?:<\/agent-message>|$)/.exec(text)?.[1] ?? text
278  return body
279    .split('\n')
280    .map(l => l.replace(/^  /, ''))
281    .join('\n')
282    .trim()
283}
284
285const join = (cwd: string, path: string) => (path.startsWith('/') ? path : `${cwd}/${path}`)
286const rel = (cwd: string, path: string) => (path.startsWith(cwd + '/') ? path.slice(cwd.length + 1) : path)
287
288async function git($: Engine, cwd: string, args: string[]): Promise<string | undefined> {
289  const r = await $.process.run(['git', ...args], { cwd, timeoutMs: 8000 }).catch(() => undefined)
290  return r !== undefined && (r.exitCode === 0 || r.exitCode === 1) ? r.stdout : undefined
291}
292
293/** Calls the LSP tool the way the model would; a plugin above it (lsp-plus) adds what it adds. */
294async function lsp($: Engine, operation: string, a: Anchor): Promise<{ text: string; context: string[] } | undefined> {
295  const r = await $.tool
296    .call({ tool: 'LSP', operation: operation as 'findReferences', filePath: a.path, line: a.line, character: a.character })
297    .catch(() => undefined)
298  if (r === undefined || r.deny !== undefined || r.isError) return undefined
299  const text = r.text ?? ''
300  if (/^(Error performing|No LSP server)/.test(text)) return undefined
301  return { text, context: [...(r.context ?? [])] }
302}
303
304/** Resolves one focus entry to a place in the code. */
305async function anchor($: Engine, cwd: string, raw: string): Promise<{ anchor?: Anchor; name: string }> {
306  const f = parseFocus(raw)
307  if (f.path !== undefined) {
308    const text = await $.fs.read(join(cwd, f.path)).catch(() => undefined)
309    if (text === undefined) return { name: f.name ?? raw }
310    const lines = text.split('\n')
311    let line = f.line
312    if (line === undefined && f.name !== undefined) {
313      const def = new RegExp(definitionPattern(f.name))
314      const at = lines.findIndex(l => def.test(l))
315      const any = lines.findIndex(l => new RegExp(`\\b${f.name}\\b`).test(l))
316      line = at >= 0 ? at + 1 : any >= 0 ? any + 1 : undefined
317    }
318    if (line === undefined) return { name: f.name ?? raw }
319    const on = anchorOn(lines[line - 1] ?? '', f.name)
320    return on ? { name: on.name, anchor: { name: on.name, path: f.path, line, character: on.character } } : { name: f.name ?? raw }
321  }
322  const name = f.name as string
323  const out = await git($, cwd, ['grep', '--untracked', '-n', '-I', '-w', '-E', '-e', gitDefinitionPattern(name), '--', '.', ':(exclude)*.md'])
324  const hit = out ? /^(.+?):(\d+):(.*)$/.exec(out.split('\n')[0] ?? '') : null
325  if (!hit) return { name }
326  const on = anchorOn(hit[3] as string, name)
327  return { name, anchor: on ? { name, path: hit[1] as string, line: Number(hit[2]), character: on.character } : undefined }
328}
329
330/** Where the task reaches: for each focus entry, references and callers from the language server, else a text search. */
331async function reachOf($: Engine, cwd: string, focus: string[]): Promise<Reach[]> {
332  const reach: Reach[] = []
333  for (const raw of focus.slice(0, MAX_FOCUS)) {
334    const { anchor: a, name } = await anchor($, cwd, raw)
335    if (a !== undefined) {
336      // A language server still indexing answers with the definition alone: give it a few seconds.
337      let refs = await lsp($, 'findReferences', a)
338      for (let i = 0; i < 3 && refs !== undefined && parseLocations(refs.text, cwd).length <= 1; i++) {
339        await $.process.run(['sleep', '3']).catch(() => undefined)
340        refs = await lsp($, 'findReferences', a)
341      }
342      if (refs !== undefined && parseLocations(refs.text, cwd).length > 1) {
343        const calls = await lsp($, 'incomingCalls', a)
344        reach.push({
345          anchor: a,
346          name,
347          refs: parseLocations(refs.text, cwd).filter(p => !(p.path === a.path && p.line === a.line)),
348          callers: calls ? parseLocations(calls.text, cwd) : [],
349          docs: refs.context,
350          via: 'lsp',
351        })
352        continue
353      }
354    }
355    const out = await git($, cwd, ['grep', '--untracked', '-n', '-I', '-w', '-F', '-e', name, '--', '.'])
356    const refs: Place[] = (out ?? '').split('\n').flatMap(row => {
357      const m = /^(.+?):(\d+):/.exec(row)
358      return m ? [{ path: m[1] as string, line: Number(m[2]) }] : []
359    })
360    if (refs.length > TEXT_SEARCH_CAP) reach.push({ anchor: a, name, refs: [], callers: [], docs: [], via: 'none' })
361    else reach.push({ anchor: a, name, refs, callers: [], docs: [], via: refs.length > 0 ? 'text' : 'none' })
362  }
363  return reach
364}
365
366/** Continues the warm worker of this mode and model, or starts one. */
367async function startWorker($: Engine, input: Input, prompt: string, description: string): Promise<{ agentId: string; reused: boolean; why?: string } | { deny: string }> {
368  const { kind, fresh } = input
369  const w = warm.get(warmKey(input))
370  let why: string | undefined
371  const busy = w !== undefined && jobs.get(w.agentId)?.reported === false
372  if (!fresh && w !== undefined && !busy && (await $.clock.now()) - w.at < reuseMs && w.requestTokens < capTokens) {
373    resuming = true
374    const sent = await $.session
375      .send({ to: { agentId: w.agentId }, text: prompt })
376      .catch(() => ({ isDelivered: false as const, reason: 'failed' }))
377      .finally(() => {
378        resuming = false
379      })
380    if (sent.isDelivered) return { agentId: w.agentId, reused: true }
381    warm.delete(warmKey(input))
382    why = sent.reason
383  }
384  // The session's folder, named: a worker whose shell started elsewhere once answered from another checkout on the disk.
385  const r = await $.agent.spawn({ prompt, subagentType: `delegate:${AGENT[kind]}`, description, model: input.worker, cwd: await $.session.cwd() })
386  if (r.deny !== undefined || r.agentId === undefined) return { deny: r.deny ?? 'the worker did not start' }
387  return { agentId: r.agentId, reused: false, why }
388}
389
390/** The worker's changes against the work tree it started from: what git sees, plus new untracked files. */
391async function gitDiff($: Engine, cwd: string, job: Job & { gitBase: string }): Promise<{ diff: string; files: string[]; skipped: string[] }> {
392  // From the repository's root: run from a subfolder, ls-files saw only the untracked files below it, and a worker's
393  // new folder elsewhere in the repository was missing from the evidence.
394  const top = ((await $.process.run(['git', 'rev-parse', '--show-toplevel'], { cwd, timeoutMs: 5000 }).then(r => r.stdout, () => '')) || cwd).trim()
395  const run = (args: string[]) => $.process.run(['git', ...args], { cwd: top, timeoutMs: 15_000 }).then(r => r.stdout).catch(() => '')
396  const mine = (f: string) => workersFile(join(top, f), job.others, job.originals)
397  const allNames = (await run(['diff', '--name-only', '-M', job.gitBase])).split('\n').filter(Boolean)
398  const before = new Set(job.untracked ?? [])
399  const allAdded = (await run(['ls-files', '--others', '--exclude-standard'])).split('\n').filter(f => f && !before.has(f))
400  const names = allNames.filter(mine)
401  const added = allAdded.filter(mine)
402  // Pathspecs for this worker's files only; with none, no `git diff` at all (without a path it would diff the whole tree).
403  const tracked = names.length > 0 ? await run(['diff', '--no-color', '-M', '-U2', job.gitBase, '--', ...names]) : ''
404  const fresh: string[] = []
405  for (const f of added) fresh.push(await run(['diff', '--no-index', '--no-color', '-U2', '--', '/dev/null', f]))
406  const skipped = [...allNames, ...allAdded].filter(f => !mine(f)).map(f => rel(cwd, join(top, f)))
407  return { diff: [tracked, ...fresh].join('').trimEnd(), files: [...names, ...added].map(f => rel(cwd, join(top, f))), skipped }
408}
409
410/** The worker's changes as one unified diff, paths relative to the session's folder. */
411async function diffOf($: Engine, cwd: string, job: Job): Promise<string> {
412  const parts: string[] = []
413  let i = 0
414  for (const [path, before] of job.originals) {
415    const after = await $.fs.read(path).catch(() => null)
416    if (after === before) continue
417    const old = `${dir}/before-${job.n}-${i++}`
418    if (before !== null) await $.fs.write(old, before)
419    const a = before === null ? '/dev/null' : old
420    const b = after === null ? '/dev/null' : path
421    const r = await $.process.run(['git', 'diff', '--no-index', '--no-color', '--no-prefix', '-U2', '--', a, b], { cwd, timeoutMs: 8000 }).catch(() => undefined)
422    // --no-prefix prints each absolute path without its leading slash.
423    const name = rel(cwd, path)
424    if (r !== undefined) parts.push([old, path, old.slice(1), path.slice(1)].reduce((t, p) => t.split(p).join(name), r.stdout))
425  }
426  return parts.join('').trimEnd()
427}
428
429async function runCheck($: Engine, command: string): Promise<{ exit: number; tail: string }> {
430  const r = await $.tool.call({ tool: 'Bash', command, timeout: 600_000 }).catch(err => ({ deny: String(err) }))
431  if (r.deny !== undefined) return { exit: -1, tail: `not run: ${r.deny}` }
432  const text = r.text ?? ''
433  const exit = r.isError ? Number(/Exit code (\d+)/.exec(text)?.[1] ?? 1) : 0
434  return { exit, tail: text.split('\n').slice(-25).join('\n').trim() }
435}
436
437const logFile = async ($: Engine) => `${(await $.env.get('HOME')) ?? ''}/.claude/mods/delegate/log.jsonl`
438
439async function flush($: Engine, done: HandOff[]) {
440  if (done.length === 0) return
441  const file = await logFile($)
442  await $.process.run(['mkdir', '-p', file.slice(0, file.lastIndexOf('/'))])
443  const old = await $.fs.read(file).catch(() => '')
444  await $.fs.write(file, old + done.map(r => JSON.stringify(r)).join('\n') + '\n')
445}
446
447async function readLog($: Engine): Promise<HandOff[]> {
448  const text = await $.fs.read(await logFile($)).catch(() => '')
449  return text.split('\n').flatMap(line => {
450    try {
451      return line ? [JSON.parse(line) as HandOff] : []
452    } catch {
453      return []
454    }
455  })
456}
457
458const ETA_TOOL = 'mcp__eta__track_progress'
459
460/** One line of the status file eta shows beside the hand-off: its requests, cost and latest action. */
461export function etaStatusLine(job: { requests: number; usd: number }, action?: { tool: string; label: string }): string {
462  const tail = action === undefined ? '' : ` · ${verb(action.tool)} ${action.label}`.replace(/\s+/g, ' ').trimEnd()
463  return `${job.requests} req · ${usd(job.usd)}${tail}`.slice(0, 120)
464}
465
466/** Appends a line to the hand-off's status file; never throws. */
467export async function writeEta($: Engine, job: { eta?: { file: string; text: string } }, line: string): Promise<void> {
468  const eta = job.eta
469  if (eta === undefined) return
470  try {
471    eta.text += `${line.replace(/\s*[\r\n]+\s*/g, ' ')}\n`
472    await $.fs.write(eta.file, eta.text)
473  } catch {
474    // eta is optional; the hand-off goes on without it.
475  }
476}
477
478/** Asks eta, when it is present, to show hand-off n beside the prompt. Returns what to remember on the job; never throws. */
479export async function trackInEta($: Engine, dir: string, n: number, worker: string): Promise<{ file: string; text: string } | undefined> {
480  try {
481    if (!(await $.tool.list()).some(t => t.name === ETA_TOOL)) return undefined
482    const file = `${dir}/hand-off-${n}.status`
483    const text = `starting · ${worker}\n`
484    await $.fs.write(file, text)
485    await $.tool.call({
486      tool: ETA_TOOL, task: `hand-off-${n}`, label: `hand-off ${n} · ${worker}`, file, finished_pattern: '^(done|failed) ·', status_line: true,
487    } as never).catch(() => undefined)
488    return { file, text }
489  } catch {
490    return undefined
491  }
492}
493
494/** hand_off: where the task reaches, then the worker, started or continued. */
495async function start($: Engine, toolUseId: string, input: Input): Promise<{ text: string; agentId?: string }> {
496  const cwd = await $.session.cwd()
497  const started = await $.clock.now()
498  const reach = await reachOf($, cwd, input.focus)
499  const reachText = describeReach(reach)
500  const prompt = workerPrompt({ task: input.task, mode: input.mode, reach: reachText, check: input.check, root: cwd })
501  const description = input.task.trim().split('\n')[0]?.slice(0, 60) ?? 'hand-off'
502  // The work tree as it is now, so the worker's changes can be told apart, however it makes them (Edit, sed, git mv).
503  const stash = input.mode === 'edit' ? await git($, cwd, ['stash', 'create']) : undefined
504  const gitBase = stash === undefined ? undefined : stash.trim() || (await git($, cwd, ['rev-parse', 'HEAD']))?.trim() || undefined
505  // Root-relative (--full-name, the whole repository), as gitDiff lists them.
506  const untracked = gitBase === undefined ? undefined : ((await git($, cwd, ['ls-files', '--others', '--exclude-standard', '--full-name', ':/'])) ?? '').split('\n').filter(Boolean)
507  const worker = await startWorker($, input, prompt, description)
508  if ('deny' in worker) return refusedStart($, worker.deny)
509  handedOver.delete(worker.agentId)
510  jobs.set(worker.agentId, {
511    n: ++count,
512    toolUseId,
513    agentId: worker.agentId,
514    input,
515    started,
516    reused: worker.reused,
517    reach,
518    reachText,
519    requests: 0,
520    usage: { input_tokens: 0, output_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
521    lastContext: 0,
522    originals: new Map(),
523    others: new Set(),
524    reported: false,
525    holding: !input.background,
526    returned: false,
527    gitBase,
528    untracked,
529  })
530  const refs = reach.reduce((k, r) => k + r.refs.length, 0)
531  await update($, VIEW, list => [
532    ...list.slice(-29),
533    {
534      n: count,
535      toolUseId,
536      agentId: worker.agentId,
537      mode: input.mode,
538      task: input.task.trim(),
539      model: input.worker,
540      effort: input.effort,
541      reused: worker.reused,
542      reach: refs,
543      startedAt: started,
544      background: input.background === true,
545      state: 'running',
546      requests: 0,
547      usd: 0,
548      actions: [],
549    } satisfies DelegateJob,
550  ])
551  const made = jobs.get(worker.agentId)
552  if (made !== undefined) made.eta = await trackInEta($, dir, made.n, label(input.worker, input.effort))
553  await animate($)
554  const reachLine = reach.length > 0 ? ` It was told where the task reaches: ${refs} reference${refs === 1 ? '' : 's'}.` : ''
555  const cold = worker.why === undefined ? '' : ` (the warm one could not be reached: ${worker.why})`
556  const later = `Its report arrives as a notification when it finishes, with ${input.mode === 'edit' ? `the diff${input.check ? ', the check' : ''} and ` : ''}what it cost. Carry on with other work meanwhile, or end your turn to wait.`
557  return { text: `The ${worker.reused ? 'warm' : 'new'} ${label(input.worker, input.effort)} worker (${worker.agentId}) is on it${cold}.${reachLine}${input.background ? ` ${later}` : ''}`, agentId: worker.agentId }
558}
559
560/** The report reached the main model (as the tool's result or a message): log it, keep the worker warm, update the card. */
561async function deliver($: Engine, job: Job, reportBody: string, diffLines = DIFF_LINES): Promise<string> {
562  job.reported = true
563  job.ended ??= await $.clock.now()
564  if (job.done === undefined || job.done.reason === 'answer') warm.set(warmKey(job.input), { agentId: job.agentId, at: job.ended, requestTokens: job.lastContext })
565  const made = await report($, job, reportBody, diffLines).catch(err => ({ text: `The delegate mod could not gather the evidence: ${String(err)}`, record: undefined }))
566  const rec = made.record
567  await setView($, job.toolUseId, () => ({
568    state: 'reported',
569    endedAt: job.ended,
570    report: reportBody,
571    ...(rec === undefined
572      ? {}
573      : { usd: rec.workerUsd, aloneUsd: rec.mainAloneUsd, files: rec.files, added: rec.added, removed: rec.removed, check: rec.check, verdict: rec.verdict }),
574  }))
575  await writeEta($, job, `done · ${job.requests} req · ${usd(rec?.workerUsd ?? costUsd(job.usage, job.workerModel ?? ''))}`)
576  await animate($)
577  return made.text
578}
579
580export function handOverText(n: number, agentId: string, report: string, evidence: string): string {
581  return `Hand-off ${n} (${agentId}) has ended. Its report:\n\n${report || '(none)'}\n\n${evidence}`
582}
583
584export function failedText(n: number, agentId: string, why: string): string {
585  return `Hand-off ${n} (${agentId}) failed: ${why}. Do the task yourself or hand it off again.`
586}
587
588/** Whether a changed file is this worker's: one it edited itself, or one nobody else edited while it ran. */
589export function workersFile(abs: string, others: ReadonlySet<string>, originals: ReadonlyMap<string, unknown>): boolean {
590  return originals.has(abs) || !others.has(abs)
591}
592
593/** A plugin prompt goes out only while no main turn runs: sent during one, it waits pinned above the person's input. */
594export const sendsNow = (turn: string | undefined): boolean => turn === undefined
595
596/** The pending report a host notice of a worker's end belongs to, by the worker's id in its text. */
597export function pendingFor(pendingIds: Iterable<string>, text: string): string | undefined {
598  return [...pendingIds].find(id => text.includes(id))
599}
600
601/** A host notice for a worker whose end the mod already reported, and not the mod's own prompt: drop it. */
602export function dropsNotice(handed: ReadonlySet<string>, originKind: string | undefined, text: string): boolean {
603  if (originKind === undefined || originKind === 'composer' || originKind === 'bridge' || originKind === 'sdk' || originKind === 'plugin') return false
604  return [...handed].some(id => text.includes(id))
605}
606
607/** The second copy of a report the model already has. Inside a running turn (turnId set) it rides along as one line: no yellow row, no extra request; on its own it would start a turn, so it is dropped. */
608export function secondCopy(turnId: string | undefined): { drop: string } | { text: string } {
609  if (turnId === undefined) return { drop: 'delegate: no second copy needed, the worker\'s report already reached Claude.' }
610  return { text: 'delegate: the worker\'s report already reached Claude as the hand_off result.' }
611}
612
613/** The host's notice of a worker's end while delegate still runs its check: the report is not there yet, so the notice
614 * says it follows. Inside a turn it rides along as one line; on its own it would start a turn, so it is dropped. */
615export function reportFollows(turnId: string | undefined, agentId: string): { drop: string } | { text: string } {
616  const text = `delegate: hand-off ${agentId} has ended; delegate is running its check, and the report follows.`
617  return turnId === undefined ? { drop: text } : { text }
618}
619
620/** Nobody waits on the worker (a background hand-off, or a held one past its limit) and its end brought no notice: report it from here. */
621async function handOver($: Engine, job: Job): Promise<void> {
622  if (job.reported || job.holding || job.done === undefined) return
623  handedOver.add(job.agentId)
624  const done = job.done
625  if (done.reason === 'answer') {
626    preparing.add(job.agentId)
627    try {
628      const evidence = await deliver($, job, done.answer, LATE_DIFF_LINES)
629      jobs.delete(job.agentId)
630      await send($, job.agentId, handOverText(job.n, job.agentId, done.answer, evidence))
631    } finally {
632      preparing.delete(job.agentId)
633    }
634    return
635  }
636  job.reported = true
637  await setView($, job.toolUseId, () => ({ state: 'failed', endedAt: job.ended }))
638  await writeEta($, job, `failed · ${job.requests} req · ${usd(costUsd(job.usage, job.workerModel ?? ''))}`)
639  await animate($)
640  jobs.delete(job.agentId)
641  await send($, job.agentId, failedText(job.n, job.agentId, done.reason))
642}
643
644/** A report for the main model: as a prompt while the session is idle; during a turn it waits for that turn's next tool
645 * result, the host's notice of the worker's end, or the turn's end, whichever comes first. */
646async function send($: Engine, agentId: string, text: string): Promise<void> {
647  if (sendsNow(mainTurn)) await $.prompt.submit({ text }).catch(() => undefined)
648  else pending.set(agentId, text)
649}
650
651/**
652 * Holds the tool call until the worker is done. Each wait is a one-second
653 * \`sleep\` run through the engine, during which a hook's own clock stands
654 * still, so the wait never counts against its budget. Undefined when the
655 * person interrupted it or it ran past HOLD_MS: the report then comes as a message.
656 */
657async function hold($: Engine, job: Job, signal: AbortSignal): Promise<string | undefined> {
658  const until = (await $.clock.now()) + HOLD_MS
659  while (job.done === undefined && job.handback === undefined) {
660    if (signal.aborted || (await $.clock.now()) > until) {
661      job.holding = false
662      return undefined
663    }
664    await $.process.run(['sleep', '1']).catch(() => undefined)
665  }
666  job.holding = false
667  job.returned = true
668  const body = job.handback ?? job.done?.answer ?? ''
669  const evidence = await deliver($, job, body)
670  if (job.done !== undefined && job.handback !== undefined) jobs.delete(job.agentId)
671  return `The worker's report:\n${body || '(none)'}\n\n${evidence}`
672}
673
674/** The worker's notice arrived: the evidence to read beside its report. */
675async function report($: Engine, job: Job, reportBody: string, diffLines = DIFF_LINES): Promise<{ text: string; record: HandOff }> {
676  const cwd = await $.session.cwd()
677  // The hand-back reaches the main model before the worker's turn has formally ended.
678  const done = job.done ?? { answer: '', reason: 'answer' }
679  const usage = job.usage
680  const requests = Math.max(1, job.requests)
681  const workerModel = job.workerModel ?? job.input.worker
682  const workerUsd = costUsd(usage, workerModel)
683  const mainModel = await $.session.model().catch(() => 'opus')
684  const contextTokens = (await $.session.usage().catch(() => undefined))?.context.tokens ?? 0
685  const alone = mainAloneUsd({ requests, contextTokens, worker: usage, model: mainModel })
686  const ended = job.ended ?? (await $.clock.now())
687  const seconds = Math.round((ended - job.started) / 1000)
688
689  const parts = [
690    `Hand-off ${job.n} (${job.input.mode}) ended: ${label(workerModel.replace(/^claude-/, ''), job.input.effort)} · ${requests} request${requests === 1 ? '' : 's'} · ${seconds}s · ${money(workerUsd)}; the same steps on your model: about ${money(alone)} at ${kTokens(contextTokens)} context.${job.reused ? ' A warm worker continued.' : ''}`,
691  ]
692  if (done.reason !== 'answer') parts.push(`The worker did not finish cleanly (${done.reason}).`)
693
694  const record: HandOff = {
695    at: job.started,
696    session: await $.session.id(),
697    mode: job.input.mode,
698    kind: job.input.kind,
699    model: workerModel,
700    effort: job.input.effort,
701    reused: job.reused,
702    focus: job.input.focus.length,
703    reachRefs: job.reach.reduce((k, r) => k + r.refs.length, 0),
704    requests,
705    durationMs: ended - job.started,
706    workerUsd,
707    mainAloneUsd: alone,
708    contextTokens,
709    files: [...new Set(job.reach.flatMap(r => filesOf(r.refs)))],
710    added: 0,
711    removed: 0,
712    reworkEdits: 0,
713    outcome: done.reason === 'answer' ? 'answer' : done.reason === 'refusal' ? 'refused' : done.reason === 'aborted' ? 'aborted' : 'error',
714  }
715
716  if (job.input.mode === 'edit') {
717    const fromGit = job.gitBase === undefined ? undefined : await gitDiff($, cwd, { ...job, gitBase: job.gitBase })
718    const diff = fromGit?.diff ?? (await diffOf($, cwd, job))
719    const files = fromGit?.files ?? [...job.originals.keys()].map(p => rel(cwd, p))
720    const stat = diffStat(diff)
721    Object.assign(record, { files, added: stat.added, removed: stat.removed })
722    const check = job.input.check ? await runCheck($, job.input.check) : undefined
723    if (check !== undefined && job.input.check !== undefined) record.check = { command: job.input.check, exit: check.exit }
724    const block = parseVerify(reportBody)
725    const tests = block?.tests ? await runCheck($, block.tests) : undefined
726    const checkCounts = check ? testCounts(check.tail) : undefined
727    const testsCounts = tests ? testCounts(tests.tail) : undefined
728    const v = verdict({
729      changed: files,
730      check: check ? { exit: check.exit, counts: checkCounts } : undefined,
731      tests: tests ? { exit: tests.exit, counts: testsCounts } : undefined,
732      // A worker that stopped on an error is not proven by tests that happen to pass.
733      open: [block?.open, done.reason === 'answer' ? undefined : `it did not finish cleanly (${done.reason})`].filter(Boolean).join('; ') || undefined,
734      block: block !== undefined,
735    })
736    record.verified = v.verified
737    parts.splice(1, 0, v.line)
738    const outside = job.reach.flatMap(r => [...r.refs, ...r.callers]).filter(p => !files.includes(p.path))
739    const fullPath = `${dir}/hand-off-${job.n}.diff`
740    if (diff) await $.fs.write(fullPath, diff + '\n')
741    const checkText = check ? `Check \`${job.input.check}\` (run by the tool): exit ${check.exit}\n${check.tail}` : 'No check command was given.'
742    const outsideText = outside.length > 0 ? `References outside the changed files (does the change keep them right?): ${places(outside)}` : ''
743    const testsText = tests && block?.tests ? `New tests \`${block.tests}\` (run by the tool): exit ${tests.exit}\n${tests.tail}` : ''
744    const evidence = [diff ? `Diff:\n${diff}` : 'The worker changed no file.', checkText, testsText, outsideText].filter(Boolean).join('\n\n')
745    const route = diff ? reviewRoute(review, tokensOf(evidence), contextTokens) : 'inline'
746    record.route = route
747    for (const f of files) touched.set(join(cwd, f), record)
748
749    parts.push(diff ? `Changed ${files.length} file${files.length === 1 ? '' : 's'} (+${stat.added} −${stat.removed}): ${files.join(', ')}` : 'The worker changed no file.')
750    if (fromGit !== undefined && fromGit.skipped.length > 0) parts.push(`Also changed while it ran, by you or another worker, so not in this diff: ${fromGit.skipped.join(', ')}`)
751    const fork = route === 'fork' ? await $.model.fork({ prompt: REVIEW_PROMPT(job.input.task, evidence) }) : undefined
752    if (fork !== undefined && fork.isAnswered) {
753      record.reviewUsd = costUsd(fork.usage as Usage, mainModel)
754      record.reviewUsage = fork.usage as Usage
755      record.verdict = parseVerdict(fork.text)
756      parts.push(`Review of the full diff by a fork of you (${money(record.reviewUsd)}):\n${fork.text.trim()}\nFull diff: ${fullPath}`)
757      parts.push(checkText)
758      if (testsText) parts.push(testsText)
759    } else {
760      record.route = 'inline'
761      const shown = capLines(diff, diffLines)
762      if (diff) parts.push(`${shown.text}${shown.cut > 0 ? `\n… ${shown.cut} more lines in ${fullPath}` : ''}`)
763      parts.push(checkText)
764      if (testsText) parts.push(testsText)
765      if (outsideText) parts.push(outsideText)
766    }
767    if (!v.verified) parts.push('Judge the work by the diff and the check, not by the report.')
768  } else {
769    const named = parsePlaces(reportBody)
770    const fileCache = new Map<string, string[] | undefined>()
771    for (const path of new Set((named ?? []).map(p => p.path))) {
772      fileCache.set(path, await $.fs.read(join(cwd, path)).then(t => String(t).split('\n'), () => undefined))
773    }
774    const lspFiles = job.reach.filter(r => r.via === 'lsp').flatMap(r => filesOf(r.refs))
775    const v = readVerdict({
776      places: (named ?? []).map(p => ({ ...p, result: checkPlace(fileCache.get(p.path), p.line, p.symbol) })),
777      block: named !== undefined,
778      serverFiles: lspFiles.length > 0 ? [...new Set(lspFiles)] : undefined,
779    })
780    record.verified = v.verified
781    parts.splice(1, 0, v.line)
782    if (job.reachText) parts.push(`It was told where the task reaches:\n${job.reachText}`)
783  }
784
785  records.push(record)
786  return { text: parts.join('\n\n'), record }
787}
788
789export const register: Register = (on, options) => {
790  if (options.enabled === false) {
791    // Off: a tool registered earlier may outlive the reload, so only its call is answered.
792    for (const tool of HAND_OFF_TOOLS) {
793      on('tool.call', { tool }, () => ({ result: 'delegate is turned off (/mods turns it on).' }))
794    }
795    return
796  }
797  for (const [k, key, dflt] of [['read', 'readWorker', 'haiku'], ['mechanical', 'mechanicalWorker', 'haiku'], ['build', 'buildWorker', 'sonnet']] as const) {
798    const w = parseWorker(typeof options[key] === 'string' && (options[key] as string).trim() ? (options[key] as string) : dflt)
799    workers[k] = w.model ? w : parseWorker(dflt)
800  }
801  review = options.review === 'auto' || options.review === 'fork' || options.review === 'inline' ? options.review : DEFAULT_REVIEW
802  reuseMs = (typeof options.reuseMinutes === 'number' ? options.reuseMinutes : 50) * 60_000
803  capTokens = typeof options.workerContextCap === 'number' ? options.workerContextCap : 60_000
804  wait = options.wait === 'message' ? 'message' : 'tool'
805  framing = options.framing === 'capability' ? 'capability' : 'hand_off'
806  afterPlan = options.afterPlan === 'hand_off' ? 'hand_off' : 'off'
807
808  on('session.start', async ($, e, next) => {
809    autoModeBlocked = false
810    const tmp = ((await $.env.get('TMPDIR')) ?? '/tmp/').replace(/\/$/, '')
811    dir = `${tmp}/claude-delegate/${await $.session.id()}`
812    await $.process.run(['mkdir', '-p', dir])
813    const have = new Set((await $.tool.list()).map(t => t.name))
814    for (const kind of ['read', 'mechanical', 'build'] as const) {
815      await $.agent.register({
816        name: AGENT[kind],
817        description: `The ${kind === 'read' ? 'read-only' : kind === 'mechanical' ? 'mechanical-edit' : 'building'} worker of the hand_off tool.`,
818        prompt: WORKER_RULES,
819        tools: WORKER_TOOLS[kind].filter(t => have.has(t)),
820        model: workers[kind].model,
821        ...(workers[kind].effort ? { effort: workers[kind].effort } : {}),
822      })
823    }
824    if (framing === 'capability') await $.tool.register({ name: CAPABILITY, description: describeCapability(), inputSchema: CAPABILITY_SCHEMA, isDeferred: false })
825    else await $.tool.register({ name: TOOL, description: describe(wait), inputSchema: INPUT_SCHEMA, isDeferred: false })
826    await $.command.register({ name: 'delegate', description: 'What hand-offs to the worker model cost and saved, and how often their work came back' })
827    await reconcile($)
828    return next(e)
829  })
830
831  on('turn.start', async ($, e, next) => {
832    await reconcile($)
833    const r = await next(e)
834    if (e.agentId === undefined) mainTurn = r.turnId
835    return r
836  })
837
838  // The workers are this tool's alone: the model reaches them through hand_off, never the Agent tool.
839  // Their type is offered only while this mod continues one, which resuming requires.
840  for (const kind of ['read', 'mechanical', 'build'] as const) on('agent.offer', { agent: `delegate:${AGENT[kind]}` }, () => ({ isOffered: resuming }))
841
842  on('command.run', { command: 'delegate' }, async $ => {
843    if ((await read($, VIEW)).length > 0) await openPane($).catch(() => undefined)
844    return { text: summarize([...(await readLog($)), ...records]) }
845  })
846
847  for (const tool of HAND_OFF_TOOLS) on('tool.call', { tool }, async ($, e, next) => {
848    const i = e as unknown as { task?: unknown; spec?: unknown; kind?: unknown; focus?: unknown; check?: unknown; fresh?: unknown; background?: unknown}
849    const capability = (e.tool as string) === 'mcp__delegate__implement_spec'
850    const task = capability ? i.spec : i.task
851    if (typeof task !== 'string' || !task.trim()) return { result: capability ? 'implement_spec needs a spec.' : 'hand_off needs a task.' }
852    const kind: Kind = capability ? 'build' : i.kind === 'read' || i.kind === 'mechanical' || i.kind === 'build' ? i.kind : 'build'
853    const mode = MODE[kind]
854    const focus = Array.isArray(i.focus) ? i.focus.filter((f): f is string => typeof f === 'string') : []
855    const check = typeof i.check === 'string' && i.check.trim() ? i.check : undefined
856    const background = typeof i.background === 'boolean' ? i.background : wait === 'message'
857    const { model: worker, effort } = workers[kind]
858    const started = await start($, e.tool_use_id, { task, kind, mode, effort, focus, check, fresh: !capability && i.fresh === true, background, worker })
859    const job = started.agentId === undefined ? undefined : jobs.get(started.agentId)
860    if (job === undefined || !job.holding) return { result: started.text }
861    const held = await hold($, job, next.signal)
862    return { result: held ?? `${started.text} Stopped waiting: its report arrives as a notification when it finishes.` }
863  }).catch(() => ({ result: 'The hand-off failed inside the delegate mod. Do the task yourself.' }))
864
865  /** Measured: a 50-line feature plan handed off was correct and cheap; renames (a few lines) cost more handed off. */
866  const PLAN_MIN_LINES = 20
867
868  // An approved plan: the order to implement it through a hand-off joins the tool's result. Only a plan
869  // the size of real work: a small one (a rename) costs more handed off than done in place.
870  on('tool.call', { tool: 'ExitPlanMode' }, async ($, e, next) => {
871    const r = await next(e)
872    const plan = (e as { plan?: unknown }).plan
873    const lines = typeof plan === 'string' ? plan.split('\n').filter(l => l.trim() !== '').length : 0
874    if (afterPlan !== 'hand_off' || autoModeBlocked || lines < PLAN_MIN_LINES || r.deny !== undefined || r.isError) return r
875    return { ...r, context: [...(r.context ?? []), planOrder(framing)] }
876  }).catch(($, e, next) => next(e))
877
878  // The workers' own edits (each file kept as it was before), and the main model's edits to their files afterwards.
879  on('tool.call', async ($, e, next) => {
880    const agentId = (e as { agentId?: string }).agentId
881    const path = (e as { file_path?: unknown }).file_path
882    const edits = (e.tool === 'Edit' || e.tool === 'Write') && typeof path === 'string'
883    if (agentId !== undefined) {
884      const job = jobs.get(agentId)
885      if (job === undefined) return next(e)
886      if (edits) {
887        const abs = join(await $.session.cwd(), path)
888        if (!job.originals.has(abs)) job.originals.set(abs, await $.fs.read(abs).catch(() => null))
889        for (const other of jobs.values()) if (other !== job && other.done === undefined) other.others.add(abs)
890      }
891      const startedAt = await $.clock.now()
892      const label = actionLabel(e.tool, e as unknown as { [k: string]: unknown })
893      let index = 0
894      await setView($, job.toolUseId, j => {
895        index = j.actions.length
896        return { actions: [...j.actions, { tool: e.tool, label, state: 'running', startedAt }] }
897      })
898      const r = await next(e)
899      const ms = (await $.clock.now()) - startedAt
900      const state = r.deny !== undefined || r.isError ? 'failed' : 'ok'
901      await setView($, job.toolUseId, j => ({ actions: j.actions.map((a, i) => (i === index ? { ...a, state, ms } : a)) }))
902      await writeEta($, job, etaStatusLine({ requests: job.requests, usd: costUsd(job.usage, job.workerModel ?? '') }, { tool: e.tool, label }))
903      return r
904    }
905    if (edits) for (const job of jobs.values()) if (job.done === undefined) job.others.add(join(await $.session.cwd(), path))
906    const r = await next(e)
907    if (edits && r.deny === undefined && !r.isError) {
908      const record = touched.get(join(await $.session.cwd(), path))
909      if (record !== undefined) record.reworkEdits++
910    }
911    // A refused call carries no context: the reports wait for the next result, or the turn's end.
912    if (pending.size === 0 || r.deny !== undefined) return r
913    const reports = [...pending.values()]
914    pending.clear()
915    return { ...r, context: [...(r.context ?? []), ...reports] }
916  }).catch(($, e, next) => next(e))
917
918  on('turn.step', async function* ($, e, next) {
919    const r = yield* next(e)
920    const job = e.agentId === undefined ? undefined : jobs.get(e.agentId)
921    if (job !== undefined && r.usage) {
922      const u = r.usage
923      job.requests++
924      job.workerModel = u.model
925      job.usage = {
926        input_tokens: job.usage.input_tokens + u.input_tokens,
927        output_tokens: job.usage.output_tokens + u.output_tokens,
928        cache_read_input_tokens: job.usage.cache_read_input_tokens + u.cache_read_input_tokens,
929        cache_creation_input_tokens: job.usage.cache_creation_input_tokens + u.cache_creation_input_tokens,
930      }
931      job.lastContext = u.input_tokens + u.cache_read_input_tokens + u.cache_creation_input_tokens + u.output_tokens
932      const spent = costUsd(job.usage, u.model)
933      await setView($, job.toolUseId, () => ({ requests: job.requests, usd: spent, model: u.model.replace(/^claude-/, '').replace(/-\d.*$/, '') }))
934      await writeEta($, job, etaStatusLine({ requests: job.requests, usd: spent }))
935    }
936    return r
937  })
938
939  on('turn.complete', async ($, e, next) => {
940    const r = await next(e)
941    if (e.agentId !== undefined) {
942      const job = jobs.get(e.agentId)
943      if (job !== undefined) {
944        job.done = { answer: e.answer, reason: e.reason }
945        job.ended ??= await $.clock.now()
946        if (e.reason !== 'answer' && warm.get(warmKey(job.input))?.agentId === e.agentId) warm.delete(warmKey(job.input))
947        if (job.reported && (!job.returned || job.handback !== undefined)) jobs.delete(e.agentId)
948        else await handOver($, job)
949      }
950      return r
951    }
952    // The main turn that read the reports ended: they go to the log, and edits stop counting as rework.
953    const done = records
954    records = []
955    touched = new Map()
956    await flush($, done).catch(() => undefined)
957    mainTurn = undefined
958    if (pending.size > 0) {
959      const texts = [...pending.values()]
960      pending.clear()
961      await $.prompt.submit({ text: texts.join('\n\n') }).catch(() => undefined)
962    }
963    return r
964  })
965
966  // The card in the transcript: live while the worker runs, then its outcome; its button opens the pane.
967  for (const tool of HAND_OFF_TOOLS) on('ui.render', { component: 'ToolUse', props: { tool } }, async ($, e, next) => {
968    const job = (await read($, VIEW)).find(j => j.toolUseId === e.requestId)
969    if (job === undefined) {
970      // Before the worker starts, one line instead of the call's whole input; a call that ended without a worker
971      // (refused, not handed off) draws as Claude Code draws it.
972      const line = e.props.isRunning && !e.props.isErrored ? startingLine(e.props.input, e.viewport?.columns ?? 100) : undefined
973      if (line === undefined) return next(e)
974      const { Text } = $.ui.resolve(e) as unknown as Elements
975      return <Text color="subtle">{line}</Text>
976    }
977    const now = await $.clock.now()
978    const open = () => void openPane($, job.toolUseId).catch(() => undefined)
979    return jobCard($.ui.resolve(e) as unknown as Elements, job, now, e.viewport?.columns ?? 100, open)
980  })
981
982  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
983    const now = await $.clock.now()
984    const pick = (id: string) => void update($, WATCHING, () => id).catch(() => undefined)
985    return pane($.ui.resolve(e) as unknown as Elements, await read($, VIEW), await read($, WATCHING), now, e.props.bodyColumns, pick)
986  })
987
988  // The card says it all: the tool's own result ("the worker is on it") draws nothing under it.
989  for (const tool of HAND_OFF_TOOLS) on('ui.render', { component: 'ToolResult', props: { tool } }, async ($, e, next) => {
990    if ((await read($, VIEW)).every(j => j.toolUseId !== e.requestId) || e.props.isErrored) return next(e)
991    const { Box } = $.ui.resolve(e)
992    return <Box />
993  })
994
995  // The worker's report reaching the main model (its hand-back message, or a task notice): the evidence joins it.
996  on('prompt.submit', async ($, e, next) => {
997    const kind = e.origin?.kind
998    if (kind === undefined || kind === 'composer' || kind === 'bridge' || kind === 'sdk' || kind === 'plugin') return next(e)
999    if (dropsNotice(handedOver, kind, e.text)) {
1000      const id = pendingFor(pending.keys(), e.text)
1001      if (id !== undefined) {
1002        const text = pending.get(id) as string
1003        pending.delete(id)
1004        return next({ ...e, text: `delegate: hand-off ${id} has ended; its report is attached.`, context: [...(e.context ?? []), text] })
1005      }
1006      const early = pendingFor(preparing, e.text)
1007      if (early !== undefined) {
1008        const follows = reportFollows(e.turnId, early)
1009        return 'drop' in follows ? { drop: follows.drop } : next({ ...e, text: follows.text })
1010      }
1011      const copy = secondCopy(e.turnId)
1012      return 'drop' in copy ? { drop: copy.drop } : next({ ...e, text: copy.text })
1013    }
1014    const job = [...jobs.values()].find(j => (!j.reported || (j.returned && j.handback === undefined)) && e.text.includes(j.agentId))
1015    if (job === undefined) return next(e)
1016    // Held in the tool call: the report goes back as its result, so the message itself does not enter.
1017    if (job.holding || job.returned) {
1018      job.handback = reportText(e.text)
1019      if (job.returned && job.done !== undefined) jobs.delete(job.agentId)
1020      const copy = secondCopy(e.turnId)
1021      return 'drop' in copy ? { drop: copy.drop } : next({ ...e, text: copy.text })
1022    }
1023    if (job.done !== undefined) jobs.delete(job.agentId)
1024    const evidence = await deliver($, job, reportText(e.text))
1025    return next({ ...e, context: [...(e.context ?? []), evidence] })
1026  }).catch(($, e, next) => next(e))
1027}
1028
hooks/plan.ts 466 lines
1// Pure parts of the hand-off: where a task reaches, what the worker reads,
2// what the work cost against doing it on the main model, and what goes back.
3
4export type Mode = 'explore' | 'edit'
5export type Kind = 'read' | 'mechanical' | 'build'
6export const EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max']
7
8/** "sonnet:low" -> { model: 'sonnet', effort: 'low' }; "haiku" -> { model: 'haiku' }; an unknown effort word is dropped. */
9export function parseWorker(setting: string): { model: string; effort?: string } {
10  const [m, e] = setting.trim().split(':')
11  const model = (m ?? '').trim()
12  const effort = (e ?? '').trim().toLowerCase()
13  return EFFORTS.includes(effort) ? { model, effort } : { model }
14}
15
16export type Place = { path: string; line: number }
17
18/** One thing the task touches, resolved to a place the language server can be asked about. */
19export type Anchor = { name: string; path: string; line: number; character: number }
20
21/** What the task reaches: per anchor, its references, callers and doc mentions. */
22export type Reach = {
23  anchor?: Anchor
24  name: string
25  refs: Place[]
26  callers: Place[]
27  docs: string[]
28  /** How the references were found: the language server, or a text search where none answered. */
29  via: 'lsp' | 'text' | 'none'
30}
31
32export type Focus = { path?: string; line?: number; name?: string }
33
34/** `path:line`, `path#name`, or a bare `name`. */
35export function parseFocus(raw: string): Focus {
36  const s = raw.trim()
37  const at = /^(.+?):(\d+)$/.exec(s)
38  if (at) return { path: at[1], line: Number(at[2]) }
39  const hash = /^(.+?)#([A-Za-z_$][\w$]*)$/.exec(s)
40  if (hash) return { path: hash[1], name: hash[2] }
41  if (/^[A-Za-z_$][\w$.]*$/.test(s)) return { name: s.split('.').at(-1) }
42  return { path: s }
43}
44
45const KEYWORDS = 'function|class|interface|type|enum|struct|trait|def|fn|func|const|let|var|val|record|module|namespace'
46
47/** An extended regex for `git grep -E` that finds where `name` is defined. */
48export const definitionPattern = (name: string) => `\\b(${KEYWORDS})\\s+${name}\\b`
49
50/** The same for `git grep -w -E`, whose POSIX expressions have no `\\b` or `\\s`. */
51export const gitDefinitionPattern = (name: string) => `(${KEYWORDS})[[:space:]]+${name}`
52
53/** Where on a source line to ask about a symbol: its definition's name, else `name`, else the first identifier. */
54export function anchorOn(text: string, name?: string): { name: string; character: number } | undefined {
55  const def = new RegExp(`\\b(?:${KEYWORDS})\\s+([A-Za-z_$][\\w$]*)`).exec(text)
56  if (name !== undefined) {
57    const at = new RegExp(`(^|[^\\w$])(${name.replace(/\$/g, '\\$')})($|[^\\w$])`).exec(text)
58    if (at) return { name, character: at.index + (at[1] as string).length + 1 }
59  }
60  if (def) return { name: def[1] as string, character: def.index + def[0].length - (def[1] as string).length + 1 }
61  // An import line is about what it imports, never the words `from` or `import`.
62  const imported = /\bimport\s+(?:\{\s*)?([A-Za-z_$][\w$]*)/.exec(text)
63  if (imported) return { name: imported[1] as string, character: imported.index + imported[0].length - (imported[1] as string).length + 1 }
64  for (const m of text.matchAll(/[A-Za-z_$][\w$]*/g)) {
65    if (!NOT_NAMES.has(m[0])) return { name: m[0], character: (m.index ?? 0) + 1 }
66  }
67  return undefined
68}
69
70const NOT_NAMES = new Set(
71  'export public private protected static async await pub from import as return if else for while const let var def class function fn func new await yield in of not and or self this'.split(' '),
72)
73
74/** A text search past this many hits names something too common to trace. */
75export const TEXT_SEARCH_CAP = 200
76
77const relative = (cwd: string, path: string) => (path.startsWith(cwd + '/') ? path.slice(cwd.length + 1) : path)
78
79/**
80 * Places in an LSP result, whichever way it lists them: `path:line[:col]`, or a
81 * `path:` heading over `Line n:c` rows.
82 */
83export function parseLocations(text: string, cwd: string): Place[] {
84  const found: Place[] = []
85  let file: string | undefined
86  for (const row of text.split('\n')) {
87    const heading = /^\s*((?:\/|\.{1,2}\/)?[\w@.\-/ ]+\.[\w]+):\s*$/.exec(row)
88    if (heading) {
89      file = relative(cwd, (heading[1] as string).trim())
90      continue
91    }
92    const line = /^\s*(?:[-•*]\s*)?Line\s+(\d+)(?::\d+)?/i.exec(row)
93    if (line && file !== undefined) {
94      found.push({ path: file, line: Number(line[1]) })
95      continue
96    }
97    for (const m of row.matchAll(/((?:\/|\.{1,2}\/)?[\w@.\-/]+\.[A-Za-z]\w*):(\d+)(?::\d+)?/g)) {
98      found.push({ path: relative(cwd, m[1] as string), line: Number(m[2]) })
99    }
100  }
101  const seen = new Set<string>()
102  return found.filter(p => {
103    const key = `${p.path}:${p.line}`
104    if (seen.has(key)) return false
105    seen.add(key)
106    return true
107  })
108}
109
110/** "a.ts:3,9 · b.ts:12 · … and 4 more files" */
111export function places(list: readonly Place[], maxFiles = 8): string {
112  const byFile = new Map<string, number[]>()
113  for (const p of list) byFile.set(p.path, [...(byFile.get(p.path) ?? []), p.line])
114  const files = [...byFile.entries()]
115  const shown = files.slice(0, maxFiles).map(([f, lines]) => `${f}:${lines.slice(0, 6).join(',')}${lines.length > 6 ? ',…' : ''}`)
116  const more = files.length - shown.length
117  return shown.join(' · ') + (more > 0 ? ` · and ${more} more file${more === 1 ? '' : 's'}` : '')
118}
119
120export function filesOf(list: readonly Place[]): string[] {
121  return [...new Set(list.map(p => p.path))]
122}
123
124/** The reach as the worker and the reviewer read it. */
125export function describeReach(reach: readonly Reach[]): string {
126  if (reach.length === 0) return ''
127  const rows = reach.map(r => {
128    const where = r.anchor ? ` (${r.anchor.path}:${r.anchor.line})` : ''
129    if (r.via === 'none') return `- \`${r.name}\`${where}: not found`
130    const head = `- \`${r.name}\`${where}: ${r.refs.length} reference${r.refs.length === 1 ? '' : 's'} in ${filesOf(r.refs).length} file${filesOf(r.refs).length === 1 ? '' : 's'}${r.via === 'text' ? ' (text search, not the language server)' : ''}`
131    const lines = [head]
132    if (r.refs.length > 0) lines.push(`  ${places(r.refs)}`)
133    if (r.callers.length > 0) lines.push(`  called from ${places(r.callers)}`)
134    for (const d of r.docs) lines.push(`  ${d.split('\n').join('\n  ')}`)
135    return lines.join('\n')
136  })
137  return rows.join('\n')
138}
139
140export const WORKER_RULES = `You work for another Claude, which handed you one task and will check what you did. Work fast and stay inside the task.
141
142- The task text is all you know of the conversation it came from. If it leaves a decision open that matters, do not guess: finish what you can and name what is open.
143- Before you change a function's signature or a symbol's name, ask the LSP tool for its references (findReferences) and change every one, or say which you left.
144- Do not claim you ran or verified anything you did not run. The other Claude runs the check itself and reads the diff.
145- End with a short report: what you found or changed, each with file:line, and anything you are unsure of. No preamble, no summary of the task.`
146
147const VERIFY_INSTRUCTION = `Your report must end with this block, exactly, no code fence:
148VERIFY
149tests: <one shell command, run from the project's root, that runs only the tests you added or changed; or none>
150open: <what you left undone or are unsure of, in one line; or none>`
151
152const PLACES_INSTRUCTION = `Your report must end with this block, exactly, no code fence, one line per place the answer names:
153PLACES
154<path relative to the project root>:<line> <the symbol as it appears on that line>`
155
156export function workerPrompt(args: { task: string; mode: Mode; reach: string; check?: string; root?: string }): string {
157  const mode =
158    args.mode === 'edit'
159      ? `Mode: edit. Change the files the task needs and nothing else.${args.check ? ` Afterwards this check will run: \`${args.check}\`; you may run it too.` : ''}\n\n${VERIFY_INSTRUCTION}`
160      : `Mode: explore. Read only: change no file. Find what the task asks and report it with file:line.\n\n${PLACES_INSTRUCTION}`
161  const reach = args.reach ? `\n\nWhere this reaches (from the language server; places that use what the task touches):\n${args.reach}` : ''
162  // Also for a warm worker resumed with a new task: its shell may have been left in another folder.
163  const root = args.root ? `\n\nThe project is ${args.root}: work there, never in another copy you may find on the disk.` : ''
164  return `${args.task.trim()}\n\n${mode}${root}${reach}`
165}
166
167type Counts = { passed: number; failed: number }
168
169/** The worker's VERIFY block: the last one in the report, `none` or empty read as absent. */
170export function parseVerify(report: string): { tests?: string; open?: string } | undefined {
171  const lines = report.split('\n')
172  let at = -1
173  lines.forEach((l, i) => {
174    if (l.trim() === 'VERIFY') at = i
175  })
176  if (at < 0) return undefined
177  const out: { tests?: string; open?: string } = {}
178  for (const l of lines.slice(at + 1)) {
179    const m = /^\s*(tests|open):\s*(.*)$/i.exec(l)
180    if (!m) continue
181    const v = (m[2] as string).trim()
182    if (v === '' || v.toLowerCase() === 'none') continue
183    out[(m[1] as string).toLowerCase() as 'tests' | 'open'] = v
184  }
185  return out
186}
187
188/** The pass and fail counts of the last test summary in a runner's output, or undefined where none is recognised. */
189export function testCounts(output: string): Counts | undefined {
190  const lines = output.split('\n')
191  for (let i = lines.length - 1; i >= 0; i--) {
192    const l = lines[i] as string
193    if (/no tests ran/i.test(l)) return { passed: 0, failed: 0 }
194    // pytest
195    if (/\b\d+ (passed|failed|errors?|skipped|deselected|xfailed|xpassed)\b/.test(l) && /\bin [\d.]+s/.test(l)) {
196      return { passed: num(l, /(\d+) passed/), failed: num(l, /(\d+) failed/) + num(l, /(\d+) errors?\b/) }
197    }
198    // jest
199    if (/^\s*Tests:\s/.test(l)) return { passed: num(l, /(\d+) passed/), failed: num(l, /(\d+) failed/) }
200    // vitest
201    if (/^\s*Tests\s+\d/.test(l)) return { passed: num(l, /(\d+) passed/), failed: num(l, /(\d+) failed/) }
202    // node:test
203    if (/^\s*# (pass|fail) \d+/.test(l)) {
204      const all = lines.join('\n')
205      const last = (k: string) => [...all.matchAll(new RegExp(`^\\s*# ${k} (\\d+)`, 'gm'))].at(-1)
206      const p = last('pass')
207      const f = last('fail')
208      if (p || f) return { passed: Number(p?.[1] ?? 0), failed: Number(f?.[1] ?? 0) }
209    }
210    // bun
211    if (/^\s*\d+ (pass|fail)\s*$/.test(l)) {
212      const all = lines.join('\n')
213      const last = (k: string) => [...all.matchAll(new RegExp(`^\\s*(\\d+) ${k}\\s*$`, 'gm'))].at(-1)
214      return { passed: Number(last('pass')?.[1] ?? 0), failed: Number(last('fail')?.[1] ?? 0) }
215    }
216  }
217  return undefined
218}
219
220const num = (line: string, re: RegExp) => Number(re.exec(line)?.[1] ?? 0)
221
222/** Whether a path is a test file by where it lives or how it is named. */
223export function isTestFile(path: string): boolean {
224  if (/(^|\/)(tests?|__tests__|spec)\//.test(path)) return true
225  const name = path.split('/').at(-1) ?? ''
226  return /^test_.*\.py$/.test(name) || /_test\.(py|go)$/.test(name) || /\.(test|spec)\.(js|jsx|ts|tsx|mjs)$/.test(name)
227}
228
229type Ran = { exit: number; counts?: Counts }
230
231/** The one line that tells the main model whether the worker's work is proven, and what to look at if not. */
232export function verdict(args: { changed: string[]; check?: Ran; tests?: Ran; open?: string; block: boolean }): { verified: boolean; line: string } {
233  const bad: string[] = []
234  const good: string[] = []
235  const n = (c?: Counts) => (c ? ` (${c.passed} passed)` : '')
236  if (args.changed.length === 0) bad.push('the worker changed no file')
237  const c = args.check
238  if (!c) bad.push('the check was not given')
239  else if (c.exit !== 0) bad.push(`the check failed with exit ${c.exit}`)
240  else if (c.counts && c.counts.failed > 0) bad.push(`the check had ${c.counts.failed} failing`)
241  else good.push(`the check passed${n(c.counts)}`)
242  const t = args.tests
243  if (t) {
244    if (t.exit !== 0) bad.push(`the new tests failed with exit ${t.exit}`)
245    else if (t.counts && t.counts.failed > 0) bad.push(`the new tests had ${t.counts.failed} failing`)
246    else if (t.counts && t.counts.passed === 0) bad.push('the new tests did not run (0 selected)')
247    else good.push(`the new tests passed on their own${n(t.counts)}`)
248  } else if (args.changed.some(isTestFile)) bad.push('test files changed but no command ran them on their own')
249  if (!args.block) bad.push("the worker's report had no VERIFY block")
250  if (args.open) bad.push(`the worker left open: ${args.open}`)
251  if (bad.length === 0) {
252    return { verified: true, line: `Verified: ${good.join('; ')}; nothing left open. If the diff is what you asked for, report to the person now: nothing is left to run.` }
253  }
254  return { verified: false, line: `Not verified: ${bad.join('; ')}. Look at these before you report.` }
255}
256
257type PlaceResult = 'found' | { near: number } | 'missing'
258
259/** The places in the worker's PLACES block: the last one in the report; undefined where there is none. */
260export function parsePlaces(report: string): { path: string; line: number; symbol: string }[] | undefined {
261  const lines = report.split('\n')
262  let at = -1
263  lines.forEach((l, i) => {
264    if (l.trim() === 'PLACES') at = i
265  })
266  if (at < 0) return undefined
267  const out: { path: string; line: number; symbol: string }[] = []
268  for (const l of lines.slice(at + 1)) {
269    const m = /^\s*(\S+?):(\d+)\s+(\S+)/.exec(l)
270    if (!m) break
271    out.push({ path: m[1] as string, line: Number(m[2]), symbol: (m[3] as string).replace(/`/g, '') })
272  }
273  return out
274}
275
276/** Whether the symbol is on the given 1-based line of the file, or near it, or nowhere close. */
277export function checkPlace(fileLines: string[] | undefined, line: number, symbol: string): PlaceResult {
278  if (!fileLines || symbol === '') return 'missing'
279  const esc = symbol.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
280  const re = /^\w+$/.test(symbol) ? new RegExp(`\\b${esc}\\b`) : new RegExp(esc)
281  const has = (n: number) => n >= 1 && n <= fileLines.length && re.test(fileLines[n - 1] as string)
282  if (has(line)) return 'found'
283  for (let d = 1; d <= 3; d++) {
284    if (has(line - d)) return { near: line - d }
285    if (has(line + d)) return { near: line + d }
286  }
287  return 'missing'
288}
289
290/** The one line that tells the main model whether a lookup answer is proven in the code, and what to open if not. */
291export function readVerdict(args: {
292  places: { path: string; line: number; symbol: string; result: PlaceResult }[]
293  block: boolean
294  serverFiles?: string[]
295}): { verified: boolean; line: string } {
296  const bad: string[] = []
297  const missing = args.places.filter(p => p.result === 'missing')
298  const near = args.places.filter(p => typeof p.result === 'object')
299  const named = new Set(args.places.map(p => p.path))
300  const unnamed = (args.serverFiles ?? []).filter(f => !named.has(f))
301  if (!args.block) bad.push('no PLACES block')
302  else if (args.places.length === 0) bad.push('the answer named no places')
303  if (missing.length > 0) {
304    const shown = missing.slice(0, 5).map(p => `${p.path}:${p.line} \`${p.symbol}\``).join(', ')
305    bad.push(`${missing.length} place${missing.length === 1 ? '' : 's'} not found at the line given: ${shown}${missing.length > 5 ? ', …' : ''}`)
306  }
307  // Files the server finds references in that the answer leaves out are named, not held against it: a question about
308  // src/ rightly leaves the tests out, and only the main model knows what the question covers.
309  const also = unnamed.length > 0 ? `; the language server also finds references in files the answer does not name (check whether the question covers them): ${unnamed.slice(0, 5).join(', ')}${unnamed.length > 5 ? ', …' : ''}` : ''
310  if (bad.length === 0) {
311    const off = near.length > 0 ? ` (${near.length} ${near.length === 1 ? 'was' : 'were'} a line or two off)` : ''
312    const server = args.serverFiles && args.serverFiles.length > 0 && unnamed.length === 0 ? "; the language server's references fall in the files the answer names" : also
313    return {
314      verified: true,
315      line: `Verified: all ${args.places.length} places checked in the code (the symbol is on each line named)${off}${server}. If this answers the question, answer the person from it now: nothing is left to open.`,
316    }
317  }
318  return { verified: false, line: `Not verified: ${bad.join('; ')}${also}. Open only these before you answer.` }
319}
320
321/** $/M tokens. Cache writes at the 5-minute rate (1.25x input). */
322export type Price = { input: number; output: number; read: number; write: number }
323
324export const PRICES: Record<string, Price> = {
325  haiku: { input: 0.1, output: 0.5, read: 0.01, write: 0.125 },
326  sonnet: { input: 2, output: 10, read: 0.2, write: 2.5 },
327  opus: { input: 4, output: 20, read: 0.2, write: 5 },
328  fable: { input: 10, output: 50, read: 0.25, write: 12.5 },
329}
330
331export function priceOf(model: string): Price {
332  const m = model.toLowerCase()
333  for (const k of Object.keys(PRICES)) if (m.includes(k)) return PRICES[k] as Price
334  return PRICES.opus as Price
335}
336
337export type Usage = { input_tokens: number; output_tokens: number; cache_read_input_tokens: number; cache_creation_input_tokens: number }
338
339export function costUsd(u: Usage, model: string): number {
340  const p = priceOf(model)
341  return (u.input_tokens * p.input + u.output_tokens * p.output + u.cache_read_input_tokens * p.read + u.cache_creation_input_tokens * p.write) / 1e6
342}
343
344/**
345 * What the same work would have cost on the main model: each of the worker's
346 * requests re-reads the main context from cache, what the worker read fresh is
347 * written to the main cache once, and its output is the main model's output.
348 */
349export function mainAloneUsd(args: { requests: number; contextTokens: number; worker: Usage; model: string }): number {
350  const p = priceOf(args.model)
351  const fresh = args.worker.input_tokens + args.worker.cache_creation_input_tokens
352  return (args.requests * args.contextTokens * p.read + fresh * p.write + args.worker.output_tokens * p.output) / 1e6
353}
354
355export type Route = 'inline' | 'fork'
356
357/**
358 * Who reviews the evidence. Inline: the main model reads it in its next step,
359 * so it is written to the main cache once and re-read on every later step. Fork:
360 * one extra request re-reads the whole main context and only a verdict comes
361 * back. The fork pays once the evidence costs more to carry than the context
362 * costs to read: about a 60th of the context, never under 2,000 tokens.
363 */
364export function reviewRoute(setting: string, evidenceTokens: number, contextTokens: number): Route {
365  if (setting === 'inline' || setting === 'fork') return setting
366  return evidenceTokens > Math.max(2000, contextTokens / 60) ? 'fork' : 'inline'
367}
368
369/**
370 * The default. Forks measured in the benchmark cost $0.03-0.38 each at a
371 * 40-60k context, far more than reading the evidence inline: they did not
372 * read the session's cache as assumed. Inline stays the default until they do.
373 */
374export const DEFAULT_REVIEW = 'inline'
375
376export const tokensOf = (text: string) => Math.ceil(text.length / 4)
377
378export function capLines(text: string, max: number): { text: string; cut: number } {
379  const lines = text.split('\n')
380  if (lines.length <= max) return { text, cut: 0 }
381  return { text: lines.slice(0, max).join('\n'), cut: lines.length - max }
382}
383
384/** "+40 −12" from a unified diff. */
385export function diffStat(diff: string): { added: number; removed: number } {
386  let added = 0
387  let removed = 0
388  for (const row of diff.split('\n')) {
389    if (row.startsWith('+') && !row.startsWith('+++')) added++
390    else if (row.startsWith('-') && !row.startsWith('---')) removed++
391  }
392  return { added, removed }
393}
394
395export const REVIEW_PROMPT = (task: string, evidence: string) => `A cheaper worker model just did a task you handed it. Review its work from the evidence below (the diff and the check output, not its own claims), using what you know of this conversation.
396
397The task you gave it:
398${task.trim()}
399
400${evidence}
401
402Answer in this shape and nothing else:
403VERDICT: accept | fix | redo
404Then at most five lines, each a concrete problem with file:line, or "none". "fix" means small corrections you will make yourself; "redo" means the approach is wrong.`
405
406export function parseVerdict(text: string): 'accept' | 'fix' | 'redo' | undefined {
407  const m = /VERDICT:\s*(accept|fix|redo)/i.exec(text)
408  return m ? (m[1]?.toLowerCase() as 'accept' | 'fix' | 'redo') : undefined
409}
410
411export const money = (usd: number) => (usd < 0.01 ? `$${usd.toFixed(4)}` : `$${usd.toFixed(2)}`)
412export const kTokens = (n: number) => (n >= 1000 ? `${Math.round(n / 1000)}k` : String(n))
413
414/** One hand-off as the log keeps it, for measuring what it saves and how often its work comes back. */
415export type HandOff = {
416  at: number
417  session: string
418  mode: Mode
419  kind?: Kind
420  model: string
421  effort?: string
422  reused: boolean
423  focus: number
424  reachRefs: number
425  requests: number
426  durationMs: number
427  workerUsd: number
428  mainAloneUsd: number
429  contextTokens: number
430  files: string[]
431  added: number
432  removed: number
433  check?: { command: string; exit: number }
434  route?: Route
435  reviewUsd?: number
436  /** The fork's tokens: whether it read the session's cache or paid for the whole context again. */
437  reviewUsage?: Usage
438  verdict?: string
439  /** Whether the tool's own checks passed and nothing was left open. */
440  verified?: boolean
441  /** Edits the main model made afterwards, before the person's next prompt, to files the worker changed. */
442  reworkEdits: number
443  outcome: 'answer' | 'aborted' | 'error' | 'timeout' | 'refused'
444}
445
446export function summarize(records: readonly HandOff[]): string {
447  if (records.length === 0) return 'No hand-offs logged yet.'
448  const sum = (f: (r: HandOff) => number) => records.reduce((n, r) => n + f(r), 0)
449  const worker = sum(r => r.workerUsd + (r.reviewUsd ?? 0))
450  const alone = sum(r => r.mainAloneUsd)
451  const edits = records.filter(r => r.mode === 'edit')
452  const reworked = edits.filter(r => r.reworkEdits > 0 || r.verdict === 'redo').length
453  const checked = edits.filter(r => r.check !== undefined)
454  const passed = checked.filter(r => r.check?.exit === 0).length
455  const reused = records.filter(r => r.reused).length
456  return [
457    `${records.length} hand-offs (${records.length - edits.length} explore, ${edits.length} edit), ${reused} on a warm worker.`,
458    `Worker and reviews: ${money(worker)}. The same steps on the main model: about ${money(alone)}. Saved: about ${money(alone - worker)}.`,
459    edits.length > 0 ? `Edit work that came back: ${reworked} of ${edits.length} (main model edited the same files afterwards, or the review said redo).` : '',
460    checked.length > 0 ? `Checks passed first time: ${passed} of ${checked.length}.` : '',
461    'The estimate re-reads the main context once per worker request; it cannot know how many steps the main model would really have taken.',
462  ]
463    .filter(Boolean)
464    .join('\n')
465}
466
hooks/view.tsx 238 lines
1// How a hand-off draws: in the transcript, a card that stays live while its
2// worker runs (a turning disc, a scanner, the clock, requests, cost and its
3// last actions) and then reads as an outcome; and a pane, opened from the
4// card, that lists everything each worker did and, once in, its report.
5
6import type { BoxProps, ButtonProps, ElementConstructor, RenderElement, TextProps } from 'claude-code'
7
8import type { DelegateAction, DelegateJob } from '../types'
9
10export type Elements = {
11  Box: ElementConstructor<BoxProps>
12  Text: ElementConstructor<TextProps>
13  Button: ElementConstructor<ButtonProps>
14}
15
16export const FRAME_MS = 250
17const TURN = ['◐', '◓', '◑', '◒'] as const
18const SCAN = 8
19const VERB: Record<string, string> = { Bash: 'run', Read: 'read', Edit: 'edit', Write: 'write', LSP: 'lsp', Grep: 'grep', Glob: 'glob', SubagentHandback: 'report' }
20
21export function scanner(frame: number): { before: string; head: string; after: string } {
22  const span = SCAN - 2
23  const t = frame % (2 * span)
24  const at = t <= span ? t : 2 * span - t
25  return { before: '·'.repeat(at), head: '━━', after: '·'.repeat(span - at) }
26}
27
28export function clock(ms: number): string {
29  const s = Math.max(0, Math.round(ms / 1000))
30  return s < 60 ? `${s}s` : `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`
31}
32
33export const usd = (n: number) => (n < 0.01 ? `$${n.toFixed(4)}` : `$${n.toFixed(2)}`)
34
35export function fit(text: string, room: number): string {
36  const flat = text.replace(/\s+/g, ' ').trim()
37  return flat.length > room ? `${flat.slice(0, Math.max(1, room - 1))}…` : flat
38}
39
40/** What a worker's tool call was about, in a few words. */
41export function actionLabel(tool: string, input: Record<string, unknown>): string {
42  const s = (k: string) => (typeof input[k] === 'string' ? (input[k] as string) : '')
43  const short = (p: string) => p.split('/').slice(-2).join('/')
44  if (tool === 'Bash') return s('command').split('\n')[0] ?? ''
45  if (tool === 'LSP') return `${s('operation')} ${short(s('filePath'))}:${String(input.line ?? '')}`
46  if (tool === 'Grep' || tool === 'Glob') return s('pattern')
47  return short(s('file_path') || s('path') || s('filePath'))
48}
49
50export const verb = (tool: string) => VERB[tool] ?? tool.replace(/^mcp__\w+__/, '').toLowerCase()
51
52/** The model, and the effort when one is set: "sonnet low". */
53export const workerName = (j: { model: string; effort?: string }) => (j.effort ? `${j.model} ${j.effort}` : j.model)
54
55/** The card's head: what state, which worker, how far. */
56export function headline(j: DelegateJob, now: number): string {
57  const time = clock((j.endedAt ?? now) - j.startedAt)
58  const req = `${j.requests} req`
59  const cost = usd(j.usd)
60  if (j.state === 'running') return `hand-off ${j.n} · ${j.mode} · ${workerName(j)}${j.reused ? ' (warm)' : ''} · ${time} · ${req} · ${cost}`
61  const saved = j.aloneUsd !== undefined ? ` · on your model ≈ ${usd(j.aloneUsd)}` : ''
62  return `hand-off ${j.n} · ${j.mode} · ${workerName(j)} · ${time} · ${req} · ${cost}${saved}`
63}
64
65/** The outcome line under a finished card: what changed and whether the check held. */
66export function outcome(j: DelegateJob): string | undefined {
67  if (j.state === 'running') return undefined
68  const parts: string[] = []
69  if (j.mode === 'edit') {
70    const n = j.files?.length ?? 0
71    parts.push(n === 0 ? 'no file changed' : `+${j.added ?? 0} −${j.removed ?? 0} in ${n} file${n === 1 ? '' : 's'}`)
72    if (j.check) parts.push(j.check.exit === 0 ? 'check passed' : `check failed (exit ${j.check.exit})`)
73    if (j.verdict) parts.push(`review: ${j.verdict}`)
74  } else {
75    parts.push(`${j.actions.length} step${j.actions.length === 1 ? '' : 's'} read`)
76  }
77  if (j.reach > 0) parts.push(`reach: ${j.reach} reference${j.reach === 1 ? '' : 's'}`)
78  return parts.join(' · ')
79}
80
81function actionRow({ Box, Text }: Elements, a: DelegateAction, now: number, frame: number, room: number, key: string): RenderElement {
82  const mark = a.state === 'running' ? TURN[frame % TURN.length] : a.state === 'ok' ? '✓' : '✕'
83  const tone = a.state === 'running' ? 'claude' : a.state === 'ok' ? 'success' : 'error'
84  const time = a.state === 'running' ? clock(now - a.startedAt) : a.ms !== undefined ? clock(a.ms) : ''
85  return (
86    <Box key={key} flexDirection="row" gap={1}>
87      <Text color={tone}>{mark}</Text>
88      <Text>{verb(a.tool).padEnd(5)}</Text>
89      <Text dimColor wrap="truncate-end">
90        {fit(a.label, Math.max(8, room - 16))}
91      </Text>
92      <Text dimColor>{time}</Text>
93    </Box>
94  )
95}
96
97const CODE_LINES = 6
98const SHOWN_ACTIONS = 6
99const TONE = { running: 'claude', background: 'claude', done: 'success', failed: 'warning' } as const
100
101/** Cells' time format: "0.0 s" under ten seconds, whole seconds after. */
102export function seconds(ms: number): string {
103  return ms < 10_000 ? `${(Math.max(0, ms) / 1000).toFixed(1)} s` : `${Math.round(ms / 1000)} s`
104}
105
106/** A hand-off drawn like a cell: the task dimmed, one row per worker action, a closing line that carries the state. */
107/** The one line a hand-off shows before its worker starts (delegate is still looking up where the task reaches):
108 * its kind and the task's first line, instead of the call's whole input. */
109export function startingLine(input: unknown, width: number): string | undefined {
110  const i = (input ?? {}) as { task?: unknown; spec?: unknown; kind?: unknown }
111  const task = typeof i.task === 'string' ? i.task : typeof i.spec === 'string' ? i.spec : undefined
112  if (task === undefined) return undefined
113  const kind = typeof i.kind === 'string' ? `${i.kind} · ` : ''
114  const first = task.trim().split('\n')[0]?.replace(/\s+/g, ' ') ?? ''
115  const head = `hand-off · ${kind}starting: `
116  const room = Math.max(10, width - head.length - 4)
117  return head + (first.length > room ? `${first.slice(0, room - 1)}…` : first)
118}
119
120export function jobCard(el: Elements, j: DelegateJob, now: number, width: number, onOpen: () => void): RenderElement {
121  const { Box, Text, Button } = el
122  const frame = Math.floor(now / FRAME_MS)
123  const inner = Math.max(24, width - 6)
124  const running = j.state === 'running'
125  const state = running ? (j.background ? 'background' : 'running') : j.state === 'reported' ? 'done' : 'failed'
126  const sweep = scanner(frame)
127  const program = j.task.replace(/\s+$/, '')
128  const lines = program === '' ? [] : program.split('\n')
129  const shown = lines.slice(0, CODE_LINES)
130  const hidden = Math.max(0, j.actions.length - SHOWN_ACTIONS)
131  const last = j.actions.slice(hidden)
132  const line = outcome(j)
133  const summary = [
134    `hand-off ${j.n}`,
135    workerName(j),
136    clock((j.endedAt ?? now) - j.startedAt),
137    `${j.requests} req`,
138    usd(j.usd),
139  ].join(' · ')
140  const saved = j.aloneUsd !== undefined ? ` · ≈ ${usd(j.aloneUsd)} on your model` : ''
141
142  const rows = last.map((a, i) => {
143    const mark = a.state === 'running' ? TURN[frame % TURN.length] : a.state === 'ok' ? '✓' : '✕'
144    const markTone = a.state === 'running' ? 'claude' : a.state === 'ok' ? 'success' : 'error'
145    const ms = a.state === 'running' ? now - a.startedAt : (a.ms ?? 0)
146    return (
147      <Box key={`a${hidden + i}`} flexDirection="row" justifyContent="space-between">
148        <Box flexDirection="row" gap={1} flexShrink={1}>
149          <Text color={markTone}>{mark}</Text>
150          <Text color="subtle">{verb(a.tool).padEnd(5)}</Text>
151          <Text wrap="truncate-end">{fit(a.label, inner - 16)}</Text>
152        </Box>
153        <Text color="subtle">{seconds(ms)}</Text>
154      </Box>
155    )
156  })
157
158  return (
159    <Box flexDirection="column" borderStyle="round" borderColor={running ? 'claude' : 'subtle'} paddingX={1}>
160      {lines.length === 0 ? null : (
161        <Box flexDirection="column" paddingLeft={2}>
162          {shown.map((t, i) => (
163            <Text key={`t${i}`} color="subtle" wrap="truncate-end">
164              {t.length > inner ? `${t.slice(0, inner - 1)}…` : t || ' '}
165            </Text>
166          ))}
167          {lines.length > CODE_LINES ? <Text color="inactive">{`… ${lines.length - CODE_LINES} more lines`}</Text> : null}
168        </Box>
169      )}
170      {rows.length > 0 ? (
171        <Box flexDirection="column">
172          {hidden > 0 ? <Text color="inactive">{`… ${hidden} earlier steps`}</Text> : null}
173          {rows}
174        </Box>
175      ) : null}
176      <Box flexDirection="row" gap={1}>
177        <Text bold color={TONE[state]}>
178          {state}
179        </Text>
180        {running ? (
181          <Text>
182            <Text color="inactive">{sweep.before}</Text>
183            <Text color="claude">{sweep.head}</Text>
184            <Text color="inactive">{sweep.after}</Text>
185          </Text>
186        ) : null}
187        <Text color="subtle">{`· ${summary}${saved}${line !== undefined ? ` · ${line}` : ''}`}</Text>
188        {j.note ? <Text color="warning">{`· ${j.note}`}</Text> : null}
189        <Button key={`open-${j.toolUseId}`} plain dimColor label={running ? 'watch ▸' : 'view ▸'} onPress={onOpen} />
190      </Box>
191    </Box>
192  )
193}
194
195/** The pane: the watched hand-off with all its worker did and its report, the others below; the engine scrolls it. */
196export function pane(el: Elements, jobs: readonly DelegateJob[], watching: string | null, now: number, width: number, onPick: (id: string) => void): RenderElement {
197  const { Box, Text, Button } = el
198  const frame = Math.floor(now / FRAME_MS)
199  const room = Math.max(24, width)
200  if (jobs.length === 0) return <Text dimColor>No hand-offs in this session yet.</Text>
201  const open = jobs.find(j => j.toolUseId === watching) ?? (jobs.at(-1) as DelegateJob)
202  const others = jobs.filter(j => j !== open).reverse()
203  const line = outcome(open)
204  return (
205    <Box flexDirection="column">
206      <Text bold color={open.state === 'running' ? 'claude' : open.state === 'reported' ? 'success' : 'error'}>
207        {headline(open, now)}
208      </Text>
209      <Text dimColor>{open.task}</Text>
210      {line !== undefined && <Text>{line}</Text>}
211      <Text dimColor>worker {open.agentId}</Text>
212      <Text> </Text>
213      {open.actions.length === 0 && <Text dimColor>starting…</Text>}
214      {open.actions.map((a, i) => actionRow(el, a, now, frame, room, `p${i}`))}
215      {open.report !== undefined && (
216        <Box flexDirection="column" marginTop={1}>
217          <Text bold>Report</Text>
218          <Text>{open.report}</Text>
219        </Box>
220      )}
221      {others.length > 0 && (
222        <Box flexDirection="column" marginTop={1}>
223          <Text bold>Earlier hand-offs</Text>
224          {others.map(j => (
225            <Button
226              key={`pick-${j.toolUseId}`}
227              plain
228              dimColor
229              label={fit(`${j.state === 'running' ? TURN[frame % TURN.length] : j.state === 'reported' ? '✓' : '✕'} ${headline(j, now)}`, room)}
230              onPress={() => onPick(j.toolUseId)}
231            />
232          ))}
233        </Box>
234      )}
235    </Box>
236  )
237}
238
types/index.d.ts 54 lines
1/** One tool call a worker made, as the card and the pane list it. */
2export type DelegateAction = {
3  tool: string
4  label: string
5  state: 'running' | 'ok' | 'failed'
6  startedAt: number
7  ms?: number
8}
9
10/** One hand-off as it draws: live while its worker runs, then its outcome. */
11export type DelegateJob = {
12  n: number
13  toolUseId: string
14  agentId: string
15  mode: 'explore' | 'edit'
16  task: string
17  model: string
18  /** The effort the worker runs at, when one is set. */
19  effort?: string
20  reused: boolean
21  /** References the language server (or a text search) found for the focus. */
22  reach: number
23  startedAt: number
24  endedAt?: number
25  /** The hand-off returned at once and the worker runs on. */
26  background?: boolean
27  state: 'running' | 'reported' | 'failed'
28  requests: number
29  usd: number
30  /** The same steps on the main model, estimated when the report arrives. */
31  aloneUsd?: number
32  actions: DelegateAction[]
33  files?: string[]
34  added?: number
35  removed?: number
36  check?: { command: string; exit: number }
37  verdict?: string
38  /** The worker's report, as it reached the main model. */
39  report?: string
40  /** Why the card ended as it did, when the outcome alone does not say. */
41  note?: string
42}
43
44declare module 'claude-code' {
45  interface PluginState {
46    delegate: {
47      /** This session's hand-offs, oldest first. */
48      jobs: DelegateJob[]
49      /** The hand-off the pane shows open, by its tool_use_id. */
50      watching: string | null
51    }
52  }
53}
54