A web cockpit and a /tower pane over every Claude Code agent across your repos: see who needs you, approve and answer, send prompts and slash commands, start…

A Claude Code plugin: one cockpit for every Claude Code agent on your machine, across all your repos. See who needs you, approve and answer, send prompts and slash commands, start agents in their own git worktrees, and watch what each one changes.
Try the live demo (in your browser, on sample data) · Site · Changelog
<img src="site/images/approve-dark.webp" alt="The cockpit's Needs you cards: allowing an agent's git push and answering another agent's question">
claude plugin marketplace add alaturqua/tower-mods && claude plugin install tower@tower
On Windows, run the two commands one after the other. PowerShell 5 has no &&, and Command Prompt has no ;:
claude plugin marketplace add alaturqua/tower-mods
claude plugin install tower@tower
Install it everywhere: every session then reports to the cockpit and can be steered from it. Sessions that were already running pick it up after a restart or /reload-plugins.
Then type /cockpit in any session. It starts the cockpit in the background and opens your browser.
After an update, /cockpit swaps a cockpit of an older version for the new one by itself. /cockpit restart replaces it on demand and /cockpit stop stops it; from a shell, node plugins/tower/cockpit/server.mjs --restart or --stop.
Needs Node.js 18 or newer for the cockpit, and the GitHub CLI (gh) for pull requests. Mods draw nothing in the VS Code chat panel; for /tower and the status line, run Claude Code in a terminal (VS Code's integrated terminal works) or the desktop app's Code tab.
| Area | What it does |
|---|---|
| Usage | The header shows your 5-hour and weekly rate limits (how full, when they reset) beside what all sessions have spent. |
| Needs you | Permission requests and questions as cards, oldest first: the agent's reason, a diff for edits, a badge on risky commands (pushes, deletes, deploys). Allow, deny or answer with a click, or A / D / 1–4. Always allow on a plain, low-risk command allows it and writes that exact command as a rule to the folder's .claude/settings.local.json, so it stops asking. |
| Fleet | Every session as a table, cards or a timeline (your choice is remembered): state, what it's doing, lines changed, PR and CI, context fill, cost, and how many subagents it has running. Stuck, looping, context-full and over-budget agents are flagged, and so are two worktrees changing the same file. Click any row, card or timeline lane to open that agent. Each row's ⋯ holds Continue, Compact, Rename, Open in editor, Jump to terminal, and below a divider Clear conversation and Stop; what a session can't do stays listed, greyed, with the reason. |
| Done | A log of finished runs, newest first: what each one said last, what it changed, how long it took and what it cost, and its PR. Click one to see its whole last message and its files; one whose session still runs opens that agent. Clear log empties it. Kept in ~/.claude/tower/history.jsonl. |
| Dashboard | Runs, spend and average run time over the last 14 days, and how long you kept agents waiting in the last hour. Charts of spend per day and per repository, the most expensive runs (click one to see what it did), and the fullest context windows. |
| Repositories and workstreams | The left rail groups sessions by repo and git worktree. + Add repository opens your system's folder picker (or type a path). + New workstream starts a background agent in a fresh worktree on its own branch (claude --bg -w). Pick 2–4 agents to give the same task to several worktrees and compare them side by side; Keep this one stops the others, removes their worktrees (their branches stay) and asks the kept one for a PR. The ⋯ menus rename or remove repos and worktrees. |
| Merge queue | When a repo has two or more green PRs, Merge all in order squash-merges them oldest first, checking each again just before its merge and stopping at the first that is no longer green. |
| The selected agent | Its branch and permission mode, a model and effort switch (sent as /model and /effort, applied once it is idle), and meters for cost (against its budget) and context. Tabs for its live activity, changed files, its subagents (what each is doing and how many tool calls it made), and its pull request (squash-merge when it's green). Send it a prompt, or type / for its slash commands. Jump to its window, or stop it if it runs in the background. |
Select several agents to send them all the same prompt, or stop them. Everything follows System, Light or Dark.
plugins/tower/cockpit, no dependencies). It listens on 127.0.0.1 only. The link /cockpit opens carries a one-time token that the browser trades for a same-site cookie. The server refuses other host names and requests from other web pages..claude/settings.local.json (Always allow), remoteAnswers in ~/.claude/settings.json (the opt-in), and its own files under ~/.claude/tower/. A settings file it cannot read is left alone.claude agents --json for the live sessions, each session's status file in ~/.claude/tower/sessions/, git in each working folder, and gh for pull requests.~/.claude/tower/inbox/<session>.jsonl. The plugin in that session picks them up within a second and runs them as if typed there. The cockpit shows "Queued" until it has. A session started before installing can't receive until it restarts, and the cockpit says so.remoteAnswers to always in ~/.claude/settings.json so you can answer every session from the cockpit.The same plugin works without the browser:
/tower opens a pane beside your conversation with every session. Allow, deny, answer, send a prompt, jump, or launch a background agent. The same actions work as commands: /tower list, /tower send <session> <prompt>, /tower launch <repo path> <task>. A session that loops on a failing call or is over its budget shows ⚠ and why on its row.osascript, Linux notify-send).1 needs you · tower-mods ⎇ main* · Opus 5.5 · high · auto · ctx 42% · $1.23 / $5.00 · 5h 72%: other sessions waiting on you, then folder, branch (* when there are uncommitted changes), model, effort, permission mode, context fill, cost (and the budget, when you set one), and the 5-hour rate limit once it is half used.Change these in /config or under pluginConfigs.tower.options in ~/.claude/settings.json:
| Option | Default | Meaning |
|---|---|---|
notifications | true | Desktop notifications at all |
notifyOnDone | true | Notify when a turn ends after minTurnSeconds |
minTurnSeconds | 30 | Shorter turns end quietly |
notifyOnIdle | false | Also notify on Claude Code's "still waiting" reminders |
budgetUsd | 0 | Flag a session and notify once when its cost passes this; 0 is no budget |
limitAlertPercent | 90 | Notify once per window when the 5-hour or weekly rate limit is this full; 0 turns it off |
remoteAnswers | launched | Which sessions send permission prompts and questions to the cockpit and tower: launched, always, never |
inboxSeconds | 1 | How often a session checks for prompts and answers from the cockpit; 0 turns it off |
pollSeconds | 3 | How often /tower refreshes, once it has run in a session |
statusLine | true | The status line at all |
showBranch, showContext, showCost | true | Parts of the status line |
statusSeconds | 5 | How often branch, context and cost are re-read between turns |
The status line learns the permission mode and effort when Claude Code reports them (each prompt, tool call and turn end), so a Shift+Tab switch shows with your next prompt.
Add this working copy as a marketplace. Claude Code then reads the plugin straight from the folder:
claude plugin marketplace add D:\Projects\tower-mods
claude plugin install tower@tower
Edit, then /reload-plugins in a session, or start one with claude --plugin-dir D:\Projects\tower-mods\plugins\tower for hot reload. Run the cockpit from the folder with node plugins/tower/cockpit/server.mjs --open.
The plugin is one hooks module (hooks/register.ts) with three parts, each in its own file:
beacon.ts: status file, notifications, inbox, remote answers;pane.tsx: /tower and /cockpit;strip.ts: the status line.A part never passes $ to another file; the engine refuses that.
The cockpit's page is built from the approved design: cockpit/web/mockup.dc.html plus cockpit/web/logic.js, through node cockpit/web/build.mjs. The build writes the cockpit's page and the site's live demo, which runs the same page against site/demo/sample.js.
claude plugin validate plugins/tower
claude plugin test plugins/tower
npm i --no-save puppeteer-core # for the real-browser tests, which skip without it
node --test plugins/tower/cockpit/test/*.test.mjs
CI runs all of these on every push.
node scripts/release.mjs 0.3.0
git push --follow-tags
The script bumps the version in both manifests, regenerates CHANGELOG.md with git-cliff, commits and tags. The pushed tag publishes a GitHub release with the notes for that version.
Apache-2.0, see LICENSE.
hooks/register.ts 14 lines1import type { Register } from 'claude-code'
2
3import { registerBeacon } from './beacon'
4import { registerPane } from './pane'
5import { registerStrip } from './strip'
6
7// One plugin, three parts, each with its own switches in /config: beacon in every
8// session, the /tower pane where you steer from, and the status line.
9export const register: Register = (on, options) => {
10 registerBeacon(on, options)
11 registerPane(on, options)
12 registerStrip(on, options)
13}
14hooks/beacon.ts 477 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, SessionRateLimit } from 'claude-code'
3
4import type { BeaconPending } from '../types'
5import { currentRun } from './strip'
6
7type $ = EngineInterface
8type State = 'idle' | 'working' | 'needs-input' | 'done'
9// `at`, when the cockpit sent it; a message through the tower pane has none.
10type TowerMessage =
11 | { v: 1; kind: 'prompt'; text: string; at?: string }
12 | { v: 1; kind: 'answer'; pendingId: string; allow?: boolean; text?: string; at?: string }
13// One line of the cockpit's live activity: who did what, newest last.
14type Activity = { t: string; kind: 'you' | 'tool' | 'say' | 'wait' | 'sent' | 'fail'; text: string }
15
16// Two alerts for the same moment (AskUserQuestion's tool call and the Notification
17// hook it raises) collapse into the first.
18const DEDUPE_MS = 5000
19const QUIET_TYPES = new Set(['idle_prompt', 'auth_success'])
20const TOWER_MARK = '[tower] '
21const ACTIVITY_MAX = 30
22const LOOP_AFTER = 3
23const INBOX_MS = 1000
24
25const pending = atom({ plugin: 'tower', key: 'pending' } as const, null as BeaconPending | null)
26const approvals = atom({ plugin: 'tower', key: 'approvals' } as const, [] as string[])
27
28const WIN_TOAST = `
29$ErrorActionPreference = 'Stop'
30[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
31[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] > $null
32$t = [Security.SecurityElement]::Escape($env:BEACON_TITLE)
33$b = [Security.SecurityElement]::Escape($env:BEACON_BODY)
34$x = New-Object Windows.Data.Xml.Dom.XmlDocument
35$x.LoadXml("<toast><visual><binding template='ToastGeneric'><text>$t</text><text>$b</text></binding></visual><audio src='ms-winsoundevent:Notification.Reminder'/></toast>")
36$app = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe'
37[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app).Show([Windows.UI.Notifications.ToastNotification]::new($x))
38`
39
40let notifyOnDone = true
41let minTurnMs = 30000
42let notifyOnIdle = false
43let notifications = true
44let remoteAnswers = 'launched'
45// A cost past this (USD) flags the session and notifies once; 0 is no budget.
46let budgetUsd = 0
47let budgetAlerted = false
48// A rate-limit window used past this percent notifies once per window; 0 is never.
49let limitAlertPercent = 90
50const limitsAlerted = new Set<string>()
51const WINDOWS: Record<string, string> = { five_hour: '5-hour', seven_day: 'weekly', spend_limit: 'spend' }
52// Whether this session sends its dialogs to the tower; settled at session.start.
53let isRemote = false
54let label = ''
55let lastAlertAt = 0
56// The tower's preview of the conversation: what was last asked, and how the last answer ended.
57let lastPrompt: string | null = null
58let lastAnswer: string | null = null
59const PREVIEW_CHARS = 1500
60let activity: Activity[] = []
61let lastState: State = 'idle'
62let lastMessage: string | null = null
63// The same call failing again and again: its key, and how many times in a row.
64let failing = { key: '', count: 0, text: '' }
65// Each subagent's latest tool call and how many it made, by agentId, for the cockpit's drill-down.
66const subWork = new Map<string, { doing: string; tools: number }>()
67const SUBAGENTS_SHOWN = 20
68let inboxBusy = false
69// Inbox lines this session has taken, for the cockpit to tell "queued" from "picked up".
70let inboxSeen = 0
71let inboxOn = false
72// A message sent before this session could read it (it was not running yet, or ran an
73// older Tower) is stale by the time it arrives; the grace covers a prompt sent at launch.
74let startedAt = 0
75const STALE_GRACE_MS = 30000
76
77async function getLabel($: $) {
78 if (label) return label
79 const repo = await $.session.repo().catch(() => null)
80 const dir = repo?.root ?? (await $.session.cwd())
81 label = dir.replace(/[\\/]+$/, '').split(/[\\/]/).pop() || dir
82 return label
83}
84
85async function towerDir($: $) {
86 const home = (await $.env.get('USERPROFILE')) ?? (await $.env.get('HOME')) ?? '.'
87 return `${home}/.claude/tower`
88}
89
90async function statusPath($: $) {
91 return `${await towerDir($)}/sessions/${await $.session.id()}.json`
92}
93
94async function note($: $, kind: Activity['kind'], text: string) {
95 const t = new Date(await $.clock.now()).toISOString()
96 activity = [...activity, { t, kind, text: text.replace(/\s+/g, ' ').slice(0, 200) }].slice(-ACTIVITY_MAX)
97}
98
99// The status file the tower pane and the cockpit read for every session. A call without
100// a state keeps the last one, so a tool call can refresh the activity without changing it.
101// The session's subagents, newest last; an engine without $.agent.list() reports none.
102async function subagentsOf($: $) {
103 const list = await $.agent.list().catch(() => [])
104 return list.slice(-SUBAGENTS_SHOWN).map(a => ({
105 id: a.id, name: a.name ?? null, description: a.description, type: a.type, status: a.status, parentId: a.parentId ?? null,
106 doing: subWork.get(a.id)?.doing ?? null, tools: subWork.get(a.id)?.tools ?? 0,
107 }))
108}
109
110async function report($: $, state?: State, message?: string | null) {
111 if (state) lastState = state
112 if (message !== undefined) lastMessage = message
113 try {
114 const repo = await $.session.repo().catch(() => null)
115 const usage = await $.session.usage().catch(() => undefined)
116 const waiting = await read($, pending)
117 const run = currentRun()
118 const cost = usage?.cost?.usd ?? null
119 const limits = usage?.rateLimits ?? []
120 void watchUsage($, cost, limits).catch(err => $.ui.log(`beacon: usage alert failed: ${String(err)}`))
121 await $.fs.write(await statusPath($), JSON.stringify({
122 sessionId: await $.session.id(),
123 label: await getLabel($),
124 cwd: await $.session.cwd(),
125 remote: repo?.remote ?? null,
126 model: await $.session.model(),
127 mode: run.mode,
128 effort: run.effort,
129 contextPercent: usage?.context.percent ?? null,
130 costUsd: cost,
131 budgetUsd: budgetUsd || null,
132 rateLimits: limits,
133 state: lastState,
134 message: lastMessage,
135 pending: waiting ? { ...waiting, key: undefined } : null,
136 remoteAnswers: isRemote,
137 // The cockpit may send here only when this session reads its inbox.
138 canReceive: inboxOn,
139 inboxSeen,
140 health: failing.count >= LOOP_AFTER ? `Looping: ${failing.text} failed ${failing.count}× in a row`
141 : budgetUsd > 0 && cost !== null && cost >= budgetUsd ? `Over budget: $${cost.toFixed(2)} of $${budgetUsd.toFixed(2)}` : null,
142 lastPrompt,
143 lastAnswer,
144 activity,
145 subagents: await subagentsOf($),
146 updatedAt: new Date(await $.clock.now()).toISOString(),
147 }, null, 2))
148 } catch {
149 // A status file is best effort; never let it break the session.
150 }
151}
152
153// Budget and rate limits, from the same figures the status file gets. The budget is this
154// session's; a rate-limit window is the account's, so a mark file in the tower folder
155// keeps every other session from announcing the same window again.
156async function watchUsage($: $, cost: number | null, limits: SessionRateLimit[]) {
157 if (budgetUsd > 0 && cost !== null && cost >= budgetUsd && !budgetAlerted) {
158 budgetAlerted = true
159 if (notifications) await desktopNotify($, `Budget · ${await getLabel($)}`, `$${cost.toFixed(2)} spent, past its $${budgetUsd.toFixed(2)} budget`)
160 }
161 if (!notifications || limitAlertPercent <= 0) return
162 for (const l of limits) {
163 const key = `${l.kind}@${l.resetsAt ?? ''}`
164 if (l.percentUsed < limitAlertPercent || limitsAlerted.has(key)) continue
165 limitsAlerted.add(key)
166 const mark = `${await towerDir($)}/alerts/${l.kind.replace(/[^\w-]/g, '')}.json`
167 const seen = await $.fs.read(mark).then(text => JSON.parse(String(text))?.key).catch(() => null)
168 if (seen === key) continue
169 await $.fs.write(mark, JSON.stringify({ key }))
170 const name = WINDOWS[l.kind] ?? l.kind
171 const at = new Date(l.resetsAt ?? '')
172 const resets = Number.isNaN(at.getTime()) ? '' : `; resets at ${String(at.getHours()).padStart(2, '0')}:${String(at.getMinutes()).padStart(2, '0')}`
173 await desktopNotify($, `Usage · ${name} limit`, `${Math.round(l.percentUsed)}% of the ${name} limit used${resets}`)
174 }
175}
176
177async function desktopNotify($: $, title: string, body: string) {
178 const env = { BEACON_TITLE: title, BEACON_BODY: body.slice(0, 240) }
179 if ((await $.env.get('OS')) === 'Windows_NT') {
180 await $.process.run(['powershell.exe', '-NoProfile', '-NonInteractive', '-Command', WIN_TOAST], { env, timeoutMs: 15000 })
181 return
182 }
183 const mac = await $.process.run(
184 ['osascript', '-e', 'display notification (system attribute "BEACON_BODY") with title (system attribute "BEACON_TITLE")'],
185 { env },
186 ).catch(() => undefined)
187 if (mac?.exitCode !== 0) await $.process.run(['notify-send', title, body], { env }).catch(() => undefined)
188}
189
190// Fire and forget: a notification must never hold up the dialog it announces.
191async function alert($: $, kind: string, body: string, state: State) {
192 const now = await $.clock.now()
193 void report($, state, body)
194 if (!notifications || now - lastAlertAt < DEDUPE_MS) return
195 lastAlertAt = now
196 const title = `${kind} · ${await getLabel($)}`
197 void desktopNotify($, title, body).catch(err => $.ui.log(`beacon: notification failed: ${String(err)}`))
198}
199
200// What the tower shows for a call: the command or path, else the input itself.
201function describeInput(input: unknown) {
202 const fields = (input ?? {}) as Record<string, unknown>
203 const main = fields.command ?? fields.file_path ?? fields.path ?? fields.url ?? fields.pattern
204 return (typeof main === 'string' ? main : JSON.stringify(input) ?? '').slice(0, 300)
205}
206
207// For an edit, the change as diff lines the cockpit can draw; nothing for other tools.
208function diffOf(tool: string, input: unknown): BeaconPending['diff'] {
209 const fields = (input ?? {}) as Record<string, unknown>
210 const edits = Array.isArray(fields.edits) ? fields.edits as Record<string, unknown>[] : [fields]
211 const lines: NonNullable<BeaconPending['diff']> = []
212 for (const edit of edits) {
213 const before = typeof edit.old_string === 'string' ? edit.old_string : undefined
214 const after = typeof edit.new_string === 'string' ? edit.new_string : (tool === 'Write' && typeof fields.content === 'string' ? fields.content : undefined)
215 if (after === undefined) continue
216 lines.push([`@@ ${typeof fields.file_path === 'string' ? fields.file_path.split(/[\\/]/).pop() : ''} @@`, 'hunk'])
217 for (const line of (before ?? '').split('\n').filter(Boolean)) lines.push([`- ${line}`, 'del'])
218 for (const line of after.split('\n').filter(Boolean)) lines.push([`+ ${line}`, 'add'])
219 }
220 return lines.length ? lines.slice(0, 24) : undefined
221}
222
223// The agent's own last words before it asked: the reason the cockpit shows on the card.
224async function reasonFor($: $) {
225 const rows = await $.session.messages().catch(() => [])
226 for (let i = rows.length - 1; i >= 0; i--) {
227 const row = rows[i]
228 if (row?.role === 'assistant' && row.text?.trim()) return row.text.trim().split('\n').filter(Boolean).pop()?.slice(0, 200)
229 }
230 return undefined
231}
232
233async function waitFor($: $, item: Omit<BeaconPending, 'id' | 'since'>, alertKind: string, alertBody: string) {
234 const now = await $.clock.now()
235 const why = await reasonFor($)
236 await update($, pending, () => ({ ...item, id: `p-${now}`, since: new Date(now).toISOString(), ...(why ? { why } : {}) }))
237 await note($, 'wait', alertBody)
238 // Written before the deny returns, so the tower sees the wait as soon as the model does.
239 await report($, 'needs-input', alertBody)
240 await alert($, alertKind, `${alertBody} (answer in the tower)`, 'needs-input')
241}
242
243// A `[tower] {json}` payload anywhere in a delivery, whatever envelope wraps it.
244function parseTower(text: string): TowerMessage | undefined {
245 const at = text.indexOf(TOWER_MARK)
246 if (at < 0) return undefined
247 return parseMessage(text.slice(text.indexOf('{', at), text.lastIndexOf('}') + 1))
248}
249
250function parseMessage(body: string): TowerMessage | undefined {
251 try {
252 const msg = JSON.parse(body)
253 return msg?.v === 1 && (msg.kind === 'prompt' || msg.kind === 'answer') ? msg : undefined
254 } catch {
255 return undefined
256 }
257}
258
259// Queued as the person's own words; runs once the session is idle. Never awaited:
260// inside a running turn the call waits for that turn.
261function submit($: $, text: string) {
262 // Our own submit skips our prompt.submit hook, so the preview is set here.
263 lastPrompt = text.slice(0, 500)
264 void $.prompt.submit({ text, asUser: true }).catch(err => $.ui.log(`beacon: could not submit the tower's prompt: ${String(err)}`))
265}
266
267async function answer($: $, msg: Extract<TowerMessage, { kind: 'answer' }>) {
268 const waiting = await read($, pending)
269 if (!waiting || waiting.id !== msg.pendingId) {
270 $.ui.log(`beacon: dropped a tower answer for ${msg.pendingId}; nothing like it is pending`)
271 return
272 }
273 await update($, pending, () => null)
274 if (waiting.kind === 'permission') {
275 if (msg.allow) {
276 await update($, approvals, keys => [...keys, waiting.key ?? ''])
277 await note($, 'sent', `Approved: ${waiting.detail ?? waiting.title}`)
278 submit($, `Approved in the tower: retry ${waiting.title} ${waiting.detail ?? ''}`.trim())
279 } else {
280 await note($, 'sent', `Denied: ${waiting.detail ?? waiting.title}`)
281 submit($, `Denied in the tower: do not run ${waiting.title} ${waiting.detail ?? ''}. Continue another way or ask.`)
282 }
283 } else {
284 await note($, 'sent', `Answered: ${msg.text ?? ''}`)
285 submit($, `Answer to your question "${waiting.title}": ${msg.text ?? ''}`)
286 }
287 await report($, 'working')
288}
289
290// A slash command from the tower runs as if typed (`/compact`, `/model sonnet`); any other
291// text is a prompt. Never awaited: the command queues until the session is idle.
292function runCommand($: $, text: string) {
293 const [, command = '', args = ''] = /^\/(\S+)\s*([\s\S]*)$/.exec(text.trim()) ?? []
294 void $.command.run({ command, args } as never).catch(err => $.ui.log(`beacon: /${command} from the tower failed: ${String(err)}`))
295}
296
297// The session's slash commands, for the cockpit's send box to suggest; written once at start.
298async function publishCommands($: $) {
299 try {
300 const list = await $.command.list()
301 const commands = list.map(c => ({ name: c.name, description: c.description.slice(0, 120) }))
302 await $.fs.write(`${await towerDir($)}/commands/${await $.session.id()}.json`, JSON.stringify(commands))
303 } catch {
304 // Suggestions are a convenience; the commands still run when typed in full.
305 }
306}
307
308// One instruction from the tower, by message or by inbox line.
309async function obey($: $, msg: TowerMessage) {
310 if (msg.kind === 'prompt' && msg.text.trim().startsWith('/')) {
311 await note($, 'sent', msg.text.trim())
312 runCommand($, msg.text)
313 await report($)
314 } else if (msg.kind === 'prompt') {
315 await note($, 'sent', msg.text)
316 submit($, msg.text)
317 await report($)
318 } else {
319 await answer($, msg)
320 }
321}
322
323// The cockpit appends instructions to inbox/<session>.jsonl; the lines already obeyed
324// are counted in the store, so a reload or a restart never obeys one twice.
325async function checkInbox($: $) {
326 if (inboxBusy) return
327 inboxBusy = true
328 try {
329 const id = await $.session.id()
330 const path = `${await towerDir($)}/inbox/${id}.jsonl`
331 if (!(await $.fs.exists(path).catch(() => false))) return
332 const lines = String(await $.fs.read(path)).split('\n').filter(line => line.trim())
333 const seenKey = `inbox:${id}`
334 const seen = Number((await $.store.get(seenKey)) ?? 0)
335 inboxSeen = Math.min(seen, lines.length)
336 if (lines.length <= seen) return
337 await $.store.set(seenKey, lines.length)
338 inboxSeen = lines.length
339 for (const line of lines.slice(seen)) {
340 const msg = parseMessage(line)
341 const at = msg?.at ? Date.parse(msg.at) : NaN
342 if (msg && !(at < startedAt - STALE_GRACE_MS)) await obey($, msg)
343 }
344 } catch (err) {
345 $.ui.log(`beacon: could not read the cockpit's inbox: ${String(err)}`)
346 } finally {
347 inboxBusy = false
348 }
349}
350
351// The part in every session: status file, notifications, and the receiving end of the tower.
352export const registerBeacon: Register = (on, options) => {
353 notifyOnDone = options.notifyOnDone !== false
354 minTurnMs = Number(options.minTurnSeconds ?? 30) * 1000
355 notifyOnIdle = options.notifyOnIdle === true
356 notifications = options.notifications !== false
357 remoteAnswers = String(options.remoteAnswers ?? 'launched')
358 budgetUsd = Math.max(0, Number(options.budgetUsd ?? 0) || 0)
359 limitAlertPercent = Math.max(0, Number(options.limitAlertPercent ?? 90) || 0)
360 const inboxMs = Number(options.inboxSeconds ?? 1) * 1000
361
362 // The plugin's one unmatched session.start; the pane's and the status line's match every cwd.
363 on('session.start', async ($, e, next) => {
364 isRemote = remoteAnswers === 'always' || (remoteAnswers === 'launched'
365 && await $.fs.exists(`${await towerDir($)}/launched/${await $.session.id()}.json`).catch(() => false))
366 startedAt = await $.clock.now()
367 inboxOn = inboxMs > 0
368 void report($, 'idle')
369 void publishCommands($)
370 if (inboxOn) $.clock.every(inboxMs || INBOX_MS, () => { void checkInbox($) })
371 return next(e)
372 })
373
374 on('prompt.submit', async ($, e, next) => {
375 if (!(e.origin.kind === 'plugin' && e.origin.name === 'tower')) {
376 lastPrompt = e.text.slice(0, 500)
377 await note($, 'you', e.text)
378 }
379 return next(e)
380 })
381
382 on('turn.start', async ($, e, next) => {
383 await update($, pending, () => null)
384 void report($, 'working')
385 return next(e)
386 })
387
388 on('session.receive', async ($, e, next) => {
389 const msg = parseTower(e.text)
390 if (!msg) return next(e)
391 await obey($, msg)
392 return { consumed: 'a tower instruction' }
393 })
394
395 on('tool.check', async ($, e, next) => {
396 const verdict = await next(e)
397 if (!isRemote || verdict.decision !== 'ask' || !e.tool_use_id) return verdict
398 const key = `${e.tool}:${JSON.stringify(e.input)}`
399 const keys = await read($, approvals)
400 if (keys.includes(key)) {
401 await update($, approvals, list => list.filter(k => k !== key))
402 return { decision: 'allow', reason: 'Approved in the tower' }
403 }
404 const detail = describeInput(e.input)
405 const diff = diffOf(e.tool, e.input)
406 await waitFor($, { kind: 'permission', title: e.tool, detail, key, ...(diff ? { diff } : {}) }, 'Permission', `${e.tool}: ${detail}`)
407 return { decision: 'deny', reason: 'Waiting for approval in the tower. End your turn now; you will be told when to retry.' }
408 }).catch(($, e, next) => next(e))
409
410 on('tool.call', { tool: 'AskUserQuestion' }, async ($, e, next) => {
411 const first = e.questions?.[0]
412 if (first && isRemote) {
413 const options = first.options.map(o => o.label)
414 await waitFor($, { kind: 'question', title: first.question, options }, 'Question', first.question)
415 return { deny: 'The question was sent to the person in the tower. End your turn now; their answer arrives as the next message.' }
416 }
417 if (first) await alert($, 'Question', first.question, 'needs-input')
418 const result = await next(e)
419 void report($, 'working')
420 return result
421 }).catch(($, e, next) => next(e))
422
423 // Every main-loop tool call: one line of activity, and a count of the same call failing in a row.
424 on('tool.call', async ($, e, next) => {
425 const result = await next(e)
426 if (e.tool === 'AskUserQuestion') return result
427 // A tool.call carries the tool's arguments beside its envelope fields.
428 const { tool, tool_use_id: _id, agentId: _agent, ...args } = e as unknown as Record<string, unknown>
429 const text = `${String(tool)} ${describeInput(args)}`.trim()
430 if (e.agentId) {
431 subWork.set(e.agentId, { doing: text, tools: (subWork.get(e.agentId)?.tools ?? 0) + 1 })
432 void report($)
433 return result
434 }
435 const failed = 'isError' in result && result.isError === true
436 if (failed) {
437 const key = `${String(tool)}:${JSON.stringify(args)}`
438 failing = key === failing.key ? { key, count: failing.count + 1, text } : { key, count: 1, text }
439 } else {
440 failing = { key: '', count: 0, text: '' }
441 }
442 await note($, failed ? 'fail' : 'tool', text)
443 void report($)
444 return result
445 }).catch(($, e, next) => next(e))
446
447 on('classic.Notification', async ($, e, next) => {
448 if (!e.agent_id && (notifyOnIdle || !QUIET_TYPES.has(e.notification_type))) {
449 const kind = e.notification_type === 'permission_prompt' ? 'Permission' : 'Needs you'
450 await alert($, kind, e.message, 'needs-input')
451 }
452 return next(e)
453 }).catch(($, e, next) => next(e))
454
455 on('turn.complete', async ($, e, next) => {
456 const result = await next(e)
457 if (e.agentId) return result
458 if (e.answer.trim()) {
459 lastAnswer = e.answer.trim().slice(-PREVIEW_CHARS)
460 await note($, 'say', e.answer.trim().split('\n').filter(Boolean).pop() ?? '')
461 }
462 const waiting = await read($, pending)
463 await report($, waiting ? 'needs-input' : 'done', waiting ? waiting.title : e.answer.slice(0, 200))
464 if (!waiting && notifyOnDone && !e.isAborted && e.durationMs >= minTurnMs) {
465 const secs = Math.round(e.durationMs / 1000)
466 await alert($, 'Done', `${secs}s · ${e.answer.split('\n')[0]?.slice(0, 160) || 'Turn finished'}`, 'done')
467 }
468 return result
469 })
470
471 on('session.end', async ($, e, next) => {
472 const path = await statusPath($).catch(() => undefined)
473 if (path) await $.fs.write(path, JSON.stringify({ sessionId: await $.session.id(), state: 'ended' })).catch(() => {})
474 return next(e)
475 })
476}
477hooks/pane.tsx 425 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register } from 'claude-code'
3
4import type { TowerSession } from '../types'
5import { COLOR, MARK, ago, columns, envelope, merge, tail } from './model'
6import type { AgentRow, StatusFile, TowerMessage } from './model'
7import { setWaiting } from './strip'
8
9type $ = EngineInterface
10
11const PANE = 'tower'
12const USAGE = 'Usage: /tower · /tower list · /tower send <session> <prompt> · /tower launch <repo path> <task> · /tower add <repo path>'
13const COCKPIT_PS = `Start-Process -WindowStyle Hidden -FilePath node -ArgumentList (@('"' + $env:TOWER_SERVER + '"') + ($env:TOWER_FLAGS -split ' ')) `
14// A session the tower starts is its own, not a child of the tower's.
15const CHILD_ENV = { CLAUDECODE: '', CLAUDE_CODE_SESSION_ID: '', CLAUDE_CODE_CHILD_SESSION: '', CLAUDE_PID: '' }
16
17const sessions = atom({ plugin: 'tower', key: 'sessions' } as const, [] as TowerSession[])
18const selected = atom({ plugin: 'tower', key: 'selected' } as const, null as string | null)
19const view = atom({ plugin: 'tower', key: 'view' } as const, 'list' as 'list' | 'launch')
20const launchRepo = atom({ plugin: 'tower', key: 'launchRepo' } as const, null as string | null)
21const failure = atom({ plugin: 'tower', key: 'error' } as const, null as string | null)
22
23let pollMs = 3000
24let polling: Promise<TowerSession[]> | undefined
25// Polling starts the first time the tower is used in a session, not in every session the plugin runs in.
26let isWatching = false
27
28function folder(path: string) {
29 return path.replace(/[\\/]+$/, '').split(/[\\/]/).pop() || path
30}
31
32async function towerDir($: $) {
33 const home = (await $.env.get('USERPROFILE')) ?? (await $.env.get('HOME')) ?? '.'
34 return `${home}/.claude/tower`
35}
36
37// The running binary itself: the npm shim is a .cmd no argv call can start.
38async function claudeExe($: $) {
39 return (await $.env.get('CLAUDE_CODE_EXECPATH')) || 'claude'
40}
41
42async function readStatus($: $, dir: string, id: string): Promise<StatusFile | undefined> {
43 try {
44 return JSON.parse(String(await $.fs.read(`${dir}/sessions/${id}.json`)))
45 } catch {
46 return undefined
47 }
48}
49
50async function pollOnce($: $) {
51 try {
52 const out = await $.process.run([await claudeExe($), 'agents', '--json'], { timeoutMs: 15000 })
53 if (out.exitCode !== 0) throw new Error(out.stderr.trim() || `claude agents exited ${out.exitCode}`)
54 const agents = JSON.parse(out.stdout) as AgentRow[]
55 const dir = await towerDir($)
56 const statuses: Record<string, StatusFile> = {}
57 await Promise.all(agents.map(async a => {
58 const file = await readStatus($, dir, a.sessionId)
59 if (file) statuses[a.sessionId] = file
60 }))
61 const list = merge(agents, statuses, await $.session.id())
62 await update($, sessions, () => list)
63 await update($, failure, () => null)
64 setWaiting(list.filter(s => s.state === 'needs-input').length)
65 return list
66 } catch (err) {
67 await update($, failure, () => String(err instanceof Error ? err.message : err))
68 return read($, sessions)
69 }
70}
71
72// One poll at a time; a tick that finds one running shares its answer.
73function poll($: $) {
74 polling ??= pollOnce($).finally(() => { polling = undefined })
75 return polling
76}
77
78// A poll nobody waits for; one that outlives its module (a reload) fails quietly.
79function refresh($: $) {
80 poll($).catch(() => {})
81}
82
83function find(list: readonly TowerSession[], who: string) {
84 const w = who.toLowerCase()
85 const exact = list.find(s => s.name.toLowerCase() === w || s.id === who)
86 if (exact) return exact
87 const byLabel = list.filter(s => s.label.toLowerCase() === w)
88 if (byLabel.length) return byLabel[0]
89 return list.find(s => s.id.startsWith(who) || s.name.toLowerCase().startsWith(w))
90}
91
92async function send($: $, s: TowerSession, msg: TowerMessage) {
93 const res = await $.session.send({ to: { sessionId: s.id }, text: envelope(s.hasBeacon, msg) })
94 refresh($)
95 return res.isDelivered ? `Sent to ${s.label}.` : `Not delivered to ${s.label}: ${res.reason}`
96}
97
98// Typed text answers a pending question when the tower can; otherwise it is a new prompt.
99async function sendText($: $, s: TowerSession, text: string) {
100 if (s.pending?.kind === 'question' && s.remoteAnswers) return send($, s, { kind: 'answer', pendingId: s.pending.id, text })
101 return send($, s, { kind: 'prompt', text })
102}
103
104async function jump($: $, s: TowerSession) {
105 if (s.kind === 'background') {
106 const ran = await $.process.run(['wt.exe', '-d', s.cwd, await claudeExe($), 'attach', s.id]).catch(() => undefined)
107 return ran?.exitCode === 0 ? `Attached to ${s.label} in a new tab.` : `Couldn't open a terminal; run: claude attach ${s.id}`
108 }
109 // The window is found by process, the way the cockpit does: titles name the tab or the editor, not the session.
110 if ((await $.env.get('OS')) === 'Windows_NT' && s.pid) {
111 const script = `${$.plugin.root}/cockpit/lib/jump.ps1`
112 const ran = await $.process.run(['powershell.exe', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', script], { env: { TOWER_PID: String(s.pid) }, timeoutMs: 15000 })
113 .catch(() => undefined)
114 const app = ran?.stdout.split(' ')[0]?.trim()
115 if (ran?.exitCode === 0) return `Brought ${app || 'its window'} forward; ${s.label} is in one of its tabs.`
116 if (app) return `Windows would not bring ${app} forward; switch to it yourself. ${s.label} runs in ${s.cwd}.`
117 }
118 return `Couldn't find its window; ${s.label} runs in ${s.cwd}.`
119}
120
121// The cockpit server outlives the session: started detached, it opens the browser itself,
122// and a second start just opens the one already running.
123async function startCockpit($: $, flags: string) {
124 const server = `${$.plugin.root}/cockpit/server.mjs`
125 const isWindows = (await $.env.get('OS')) === 'Windows_NT'
126 const argv = isWindows
127 ? ['powershell.exe', '-NoProfile', '-NonInteractive', '-Command', COCKPIT_PS]
128 : ['sh', '-c', 'nohup node "$TOWER_SERVER" $TOWER_FLAGS >/dev/null 2>&1 &']
129 const ran = await $.process.run(argv, { env: { TOWER_SERVER: server, TOWER_FLAGS: flags }, timeoutMs: 15000 }).catch(err => ({ exitCode: -1, stdout: '', stderr: String(err) }))
130 if (ran.exitCode !== 0) return `Couldn't start the cockpit (is Node.js 18+ installed?): ${ran.stderr.trim().slice(0, 200)}\nRun it yourself: node "${server}" ${flags}`
131 return flags.includes('--restart') ? 'Restarting the Tower cockpit and opening it in your browser.' : 'Opening the Tower cockpit in your browser. It keeps running after this session; the link it opened works until it stops.'
132}
133
134async function stopCockpit($: $) {
135 const stopped = await $.process.run(['node', `${$.plugin.root}/cockpit/server.mjs`, '--stop'], { timeoutMs: 10000 }).catch(err => ({ exitCode: -1, stdout: '', stderr: String(err) }))
136 return stopped.exitCode === 0 ? stopped.stdout.trim() : `Couldn't stop the cockpit: ${stopped.stderr.trim().slice(0, 200)}`
137}
138
139async function knownRepos($: $) {
140 const saved = ((await $.store.get('repos')) as string[] | undefined) ?? []
141 const live = (await read($, sessions)).map(s => s.cwd)
142 const seen = new Map<string, string>()
143 for (const repo of [...saved, ...live]) seen.set(repo.toLowerCase().replace(/[\\/]+$/, ''), repo)
144 return [...seen.values()].sort((a, b) => folder(a).localeCompare(folder(b)))
145}
146
147async function remember($: $, repo: string) {
148 const saved = ((await $.store.get('repos')) as string[] | undefined) ?? []
149 if (!saved.some(r => r.toLowerCase() === repo.toLowerCase())) await $.store.set('repos', [...saved, repo])
150}
151
152async function launch($: $, repo: string, task: string) {
153 const id = crypto.randomUUID()
154 const name = `${folder(repo)}-${id.slice(0, 4)}`
155 // Written first: beacon in the new session reads it at session.start.
156 await $.fs.write(`${await towerDir($)}/launched/${id}.json`, JSON.stringify({ repo, task, launchedAt: new Date(await $.clock.now()).toISOString() }))
157 const ran = await $.process.run([await claudeExe($), '--bg', '--session-id', id, '--name', name, '--', task], { cwd: repo, env: CHILD_ENV, timeoutMs: 60000 })
158 .catch(err => ({ exitCode: -1, stdout: '', stderr: String(err) }))
159 if (ran.exitCode !== 0) return `Couldn't start a session in ${repo}: ${(ran.stderr || ran.stdout).trim().slice(0, 200)}`
160 await remember($, repo)
161 refresh($)
162 return `Started ${name} in ${repo}.`
163}
164
165function rowText(s: TowerSession, now: number, width: number) {
166 const what = s.pending ? `${s.pending.title}: ${s.pending.detail ?? ''}` : (s.message ?? '')
167 const tag = s.kind === 'background' ? ' (bg)' : ''
168 const head = `${MARK[s.state]} ${(s.label + tag).padEnd(18)} ${s.state.padEnd(11)} `
169 const tail = ` ${ago(s.updatedAt, now)}`
170 const room = Math.max(0, width - head.length - tail.length - 4)
171 return head + what.replace(/\s+/g, ' ').slice(0, room).padEnd(room) + tail
172}
173
174// `launch "D:\My Repo" the task` or `launch D:\repo the task`.
175function splitPath(rest: string): [string, string] {
176 const quoted = /^"([^"]+)"\s*(.*)$/s.exec(rest)
177 if (quoted) return [quoted[1] ?? '', quoted[2] ?? '']
178 const at = rest.search(/\s/)
179 return at < 0 ? [rest, ''] : [rest.slice(0, at), rest.slice(at).trim()]
180}
181
182async function runCommand($: $, args: string): Promise<string> {
183 const [verb = '', ...words] = args.trim().split(/\s+/)
184 const rest = args.trim().slice(verb.length).trim()
185 if (verb === 'list') {
186 const list = await poll($)
187 const now = await $.clock.now()
188 return list.length ? list.map(s => rowText(s, now, 100).trimEnd()).join('\n') : 'No other sessions are running.'
189 }
190 if (verb === 'send') {
191 const [who = '', ...text] = words
192 const list = await poll($)
193 const target = find(list, who)
194 if (!target || !text.length) return `No session "${who}". Running: ${list.map(s => s.label).join(', ') || 'none'}.`
195 return sendText($, target, rest.slice(who.length).trim())
196 }
197 if (verb === 'launch') {
198 const [repo, task] = splitPath(rest)
199 if (!repo || !task) return USAGE
200 return launch($, repo, task)
201 }
202 if (verb === 'add') {
203 if (!rest) return USAGE
204 await remember($, splitPath(rest)[0])
205 return `Remembered ${rest}.`
206 }
207 return USAGE
208}
209
210// The control pane and its /tower command.
211export const registerPane: Register = (on, options) => {
212 pollMs = Number(options.pollSeconds ?? 3) * 1000
213
214 // The /tower command. Polling waits until it is first used.
215 on('session.start', { cwd: /(?:)/ }, async ($, e, next) => {
216 await $.command.register({ name: 'tower', description: 'Watch and steer every running Claude Code session', argumentHint: '[list | send <session> <prompt> | launch <repo> <task> | add <repo>]' })
217 await $.command.register({ name: 'cockpit', description: 'Open the Tower cockpit, a web dashboard over every session, in your browser', argumentHint: '[restart | stop]' })
218 return next(e)
219 })
220
221 // The cockpit server outlives this session: started detached, it opens the browser itself,
222 // and a second start just opens the one already running.
223 // /cockpit opens it; /cockpit restart replaces it (after an update); /cockpit stop stops it.
224 on('command.run', { command: 'cockpit' }, async ($, e) => {
225 const verb = e.args.trim().toLowerCase()
226 if (verb === 'stop') return { text: await stopCockpit($) }
227 return { text: await startCockpit($, verb === 'restart' ? '--restart --open' : '--open') }
228 })
229
230 on('command.run', { command: 'tower' }, async ($, e) => {
231 if (!isWatching && pollMs > 0) {
232 isWatching = true
233 $.clock.every(pollMs, () => refresh($))
234 }
235 if (e.args.trim()) return { text: await runCommand($, e.args) }
236 await update($, view, () => 'list')
237 refresh($)
238 await $.ui.open({ id: PANE, title: 'Tower', focus: true, columns: 110, rows: 24 })
239 return { text: 'Tower opened.' }
240 })
241
242 on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
243 // Mobile has no text field or picker to steer with.
244 if (e.surface === 'mobile') {
245 const { Text } = $.ui.resolve(e)
246 return <Text dimColor>Open the tower in a terminal to steer sessions.</Text>
247 }
248 const { Box, Text, Button, Input, Select } = $.ui.resolve(e)
249 const width = (e.props as { bodyColumns?: number }).bodyColumns ?? e.viewport?.columns ?? 80
250 const toast = (text: string) => $.ui.toast(text)
251
252 if ((await read($, view)) === 'launch') {
253 const repos = await knownRepos($)
254 const repo = (await read($, launchRepo)) ?? repos[0] ?? null
255 return (
256 <Box flexDirection="column">
257 <Text bold>New session</Text>
258 {repos.length === 0
259 ? <Text dimColor>No repos known yet. Add one with /tower add <path>.</Text>
260 : <Select key="repo" label="Repo" value={repo ?? undefined} options={repos.map(r => ({ value: r, label: `${folder(r)} ${r}` }))}
261 onSelect={value => void update($, launchRepo, () => value)} />}
262 {repo && <Input key="task" label={`Task for ${folder(repo)}`} placeholder="What should it do?" autoFocus submitLabel="launch"
263 onSubmit={task => {
264 if (!task.trim()) return
265 void launch($, repo, task.trim()).then(toast)
266 void update($, view, () => 'list')
267 }} />}
268 <Box gap={1}>
269 <Button key="back" hotkey="b" onPress={() => void update($, view, () => 'list')}>Back</Button>
270 </Box>
271 </Box>
272 )
273 }
274
275 const list = await read($, sessions)
276 const error = await read($, failure)
277 const now = await $.clock.now()
278 const chosen = await read($, selected)
279 const target = list.find(s => s.id === chosen) ?? list[0]
280 const needs = list.filter(s => s.state === 'needs-input')
281 const focused = (e.props as { isFocused?: boolean }).isFocused === true
282 // Room for the session's last words: what the rows and the box leave.
283 const previewLines = Math.min(4, Math.max(1, (e.viewport?.rows ?? 30) - list.length - needs.length * 2 - 16))
284 const cols = columns(width)
285 // Docked, the pane is at least as tall as its room, so the send box sits at its bottom like a chat's.
286 const props = e.props as { placement?: string; scroll?: { bodyRows?: number } }
287 const room = props.placement === 'dock' ? Math.max(0, (props.scroll?.bodyRows ?? 0) - 1) : 0
288
289 // Two sessions in one folder share a label; the first letters of the id tell them apart.
290 const labelWidth = width >= 120 ? 30 : cols.labelWidth
291 const nameOf = (s: TowerSession, room = 200) => {
292 const tag = (list.filter(o => o.label === s.label).length > 1 ? ` #${s.id.slice(0, 4)}` : '') + (s.kind === 'background' ? ' (bg)' : '')
293 const label = s.label.length + tag.length > room ? s.label.slice(0, Math.max(4, room - tag.length - 1)) + '…' : s.label
294 return label + tag
295 }
296 const pick = (s: TowerSession) => void update($, selected, () => s.id)
297 const move = (step: number) => {
298 if (!list.length) return
299 const at = Math.max(0, list.findIndex(s => s.id === target?.id))
300 pick(list[(at + step + list.length) % list.length] as TowerSession)
301 }
302
303 // One card per session that waits on you. Only the first takes the keys: two cards on one key would clash.
304 const card = (s: TowerSession, first: boolean) => {
305 const p = s.pending
306 const answerable = s.remoteAnswers && p
307 return (
308 <Box key={`card-${s.id}`} gap={1} flexWrap="wrap">
309 <Text color="warning" bold>{p?.kind === 'question' ? '?' : '!'}</Text>
310 <Button key={`pick-card-${s.id}`} plain onPress={() => pick(s)}>{nameOf(s)}</Button>
311 <Text dimColor>{!p ? 'needs you' : p.kind === 'question' ? 'asks' : answerable ? 'needs approval' : 'waits in its terminal'}</Text>
312 <Box flexGrow={1} flexShrink={1}><Text wrap="truncate-end">{p ? `${p.title} ${p.detail ?? ''}`.trim() : (s.message ?? '')}</Text></Box>
313 {answerable && p.kind === 'permission' && (
314 <Box gap={1}>
315 <Button key={first ? 'allow' : `allow-${s.id}`} hotkey={first ? 'a' : undefined} variant="primary" onPress={() => void send($, s, { kind: 'answer', pendingId: p.id, allow: true }).then(toast)}>{first ? 'Allow (a)' : 'Allow'}</Button>
316 <Button key={first ? 'deny' : `deny-${s.id}`} hotkey={first ? 'd' : undefined} onPress={() => void send($, s, { kind: 'answer', pendingId: p.id, allow: false }).then(toast)}>{first ? 'Deny (d)' : 'Deny'}</Button>
317 </Box>
318 )}
319 {answerable && p.kind === 'question' && (
320 <Box gap={1} flexWrap="wrap">
321 {(p.options ?? []).slice(0, 4).map((label, i) => (
322 <Button key={`opt-${s.id}-${i}`} hotkey={first ? String(i + 1) : undefined} onPress={() => void send($, s, { kind: 'answer', pendingId: p.id, text: label }).then(toast)}>{first ? `${i + 1} ${label}` : label}</Button>
323 ))}
324 </Box>
325 )}
326 {!answerable && <Button key={`jump-card-${s.id}`} onPress={() => void jump($, s).then(toast)}>Jump</Button>}
327 </Box>
328 )
329 }
330
331 // One line per session: what it is doing, without picking it first.
332 const row = (s: TowerSession) => {
333 const isChosen = s.id === target?.id
334 const what = s.pending ? `${s.pending.title}: ${s.pending.detail ?? ''}` : (s.message ?? s.state)
335 return (
336 <Box key={`row-${s.id}`} gap={1}>
337 <Text color={isChosen ? 'suggestion' : undefined} bold>{isChosen ? '▶' : ' '}</Text>
338 <Text color={COLOR[s.state]} dimColor={s.state === 'idle'}>{MARK[s.state]}</Text>
339 <Button key={`pick-${s.id}`} plain onPress={() => pick(s)}>{nameOf(s, labelWidth).padEnd(labelWidth)}</Button>
340 <Box flexGrow={1} flexShrink={1}>{cols.showState && (s.health
341 ? <Text color="error" wrap="truncate-end">{`⚠ ${s.health}`}</Text>
342 : <Text dimColor={!isChosen} wrap="truncate-end">{what.replace(/\s+/g, ' ')}</Text>)}</Box>
343 <Box flexShrink={0}><Text dimColor>{ago(s.updatedAt, now)}</Text></Box>
344 </Box>
345 )
346 }
347
348 const answer = target?.lastAnswer ? tail(target.lastAnswer, previewLines).split('\n') : []
349 const title = target ? ` ${nameOf(target)} · ${target.state} · ${target.cwd} ` : ''
350
351 return (
352 <Box flexDirection="column" gap={1} minHeight={room || undefined}>
353 <Box gap={1} flexWrap="wrap">
354 <Text bold>Tower</Text>
355 <Text dimColor>· {list.length} session{list.length === 1 ? '' : 's'}</Text>
356 {needs.length > 0 && <Text color="warning" bold>· {needs.length} need{needs.length === 1 ? 's' : ''} you</Text>}
357 <Box flexGrow={1} />
358 {focused
359 ? <Text color="success">● Typing here · Esc back to your prompt</Text>
360 : <Text dimColor>Click here or press ctrl+x Tab to use it</Text>}
361 </Box>
362 {error && <Text color="warning">Stale: {error}</Text>}
363
364 {needs.length > 0 && (
365 <Box flexDirection="column" borderStyle="round" borderColor="warning" paddingX={1}>
366 <Text color="warning" bold>Needs input</Text>
367 {needs.map((s, i) => card(s, i === 0))}
368 </Box>
369 )}
370
371 <Box flexDirection="column" gap={1}>
372 {list.length === 0 && <Text dimColor>No other sessions are running. Press n to start one.</Text>}
373 {[['Working', list.filter(s => s.state === 'working')], ['Completed', list.filter(s => s.state === 'done' || s.state === 'idle')]].map(([title, members]) => (members as TowerSession[]).length > 0 && (
374 <Box key={`group-${title}`} flexDirection="column">
375 <Text dimColor bold>{title as string}</Text>
376 {(members as TowerSession[]).map(row)}
377 </Box>
378 ))}
379 </Box>
380
381 {target && (
382 <Box flexDirection="column">
383 <Text dimColor wrap="truncate-middle">{'──' + title + '─'.repeat(Math.max(2, width - title.length - 4))}</Text>
384 {!target.hasBeacon && <Text dimColor>No preview: this session runs without Tower.</Text>}
385 {target.lastPrompt && (
386 <Box gap={1}>
387 <Text bold>{'You '}</Text>
388 <Box flexGrow={1} flexShrink={1}><Text dimColor wrap="truncate-end">{target.lastPrompt.replace(/\s+/g, ' ')}</Text></Box>
389 </Box>
390 )}
391 {answer.length > 0 && (
392 <Box gap={1}>
393 <Text bold color="suggestion">Claude</Text>
394 <Box flexGrow={1} flexShrink={1} flexDirection="column">
395 {answer.map((line, i) => <Text key={`line-${i}`} wrap="truncate-end">{line}</Text>)}
396 </Box>
397 </Box>
398 )}
399 {!target.lastPrompt && answer.length === 0 && target.hasBeacon && <Text dimColor>Nothing said yet.</Text>}
400 </Box>
401 )}
402
403 <Box flexGrow={1} />
404
405 {target && (
406 <Box borderStyle="round" borderColor={focused ? 'suggestion' : undefined} borderDimColor={!focused} paddingX={1}>
407 <Input key={`say-${target.id}`} label={`To ${nameOf(target)}`}
408 placeholder={target.pending?.kind === 'question' && target.remoteAnswers ? 'Type an answer, Enter sends' : 'Type a message or /command, Enter sends'} submitLabel="send"
409 onSubmit={text => { if (text.trim()) void sendText($, target, text.trim()).then(toast) }} />
410 </Box>
411 )}
412
413 <Box gap={1} flexWrap="wrap">
414 <Button key="prev" hotkey="k" onPress={() => move(-1)}>↑ Prev (k)</Button>
415 <Button key="next" hotkey="j" onPress={() => move(1)}>↓ Next (j)</Button>
416 {target && <Button key="jump" hotkey="g" onPress={() => void jump($, target).then(toast)}>Jump (g)</Button>}
417 <Button key="new" hotkey="n" onPress={() => void update($, view, () => 'launch')}>New session (n)</Button>
418 <Button key="cockpit" hotkey="o" onPress={() => void startCockpit($, '--open').then(toast)}>Open cockpit (o)</Button>
419 <Button key="refresh" hotkey="r" onPress={() => refresh($)}>Refresh (r)</Button>
420 </Box>
421 </Box>
422 )
423 })
424}
425hooks/strip.ts 116 lines1import type { EngineInterface, Register } from 'claude-code'
2
3import { compose } from './line'
4import type { StripParts } from './line'
5
6type $ = EngineInterface
7
8let showBranch = true
9let showContext = true
10let showCost = true
11let showLine = true
12let pollMs = 5000
13let budgetUsd = 0
14// What git, the model and the usage said last; mode and effort as the engine last reported them.
15let known: StripParts = { folder: '' }
16let waiting = 0
17let shown: string | undefined
18
19function folder(path: string) {
20 return path.replace(/[\\/]+$/, '').split(/[\\/]/).pop() || path
21}
22
23async function git($: $, root: string, args: string[]) {
24 const ran = await $.process.run(['git', '-C', root, ...args], { timeoutMs: 3000 }).catch(() => undefined)
25 return ran?.exitCode === 0 ? ran.stdout : undefined
26}
27
28// Draws from what is known; no process runs, so it is cheap enough for every tool call.
29// With the line switched off, only the tower's count is left to show.
30function draw($: $) {
31 const line = showLine ? compose({ ...known, waiting }) : (compose({ folder: '', waiting }) || undefined)
32 if (line !== shown) $.ui.status((shown = line))
33}
34
35// Redraws with the session's own `$`, kept from session.start: `$` never crosses a file.
36let redraw: (() => void) | undefined
37
38// The pane's poll reports how many other sessions wait on you; one status entry carries both.
39export function setWaiting(count: number) {
40 waiting = count
41 redraw?.()
42}
43
44// Re-reads what changes on its own: the branch, the model, the context and the cost.
45async function refresh($: $) {
46 const repo = await $.session.repo().catch(() => null)
47 const root = repo?.root ?? (await $.session.cwd())
48 const next: StripParts = { ...known, folder: repo?.name ?? folder(root), branch: undefined, isDirty: undefined }
49 if (showBranch && repo) {
50 const [branch, changes] = await Promise.all([git($, root, ['rev-parse', '--abbrev-ref', 'HEAD']), git($, root, ['status', '--porcelain'])])
51 next.branch = branch?.trim() || undefined
52 next.isDirty = Boolean(changes?.trim())
53 }
54 next.model = await $.session.model().catch(() => known.model)
55 const usage = await $.session.usage().catch(() => undefined)
56 next.contextPercent = showContext ? usage?.context.percent : undefined
57 next.costUsd = showCost ? usage?.cost?.usd : undefined
58 next.budgetUsd = showCost ? budgetUsd : undefined
59 next.limitPercent = showCost ? usage?.rateLimits.find(l => l.kind === 'five_hour')?.percentUsed : undefined
60 known = next
61 draw($)
62}
63
64function background($: $) {
65 refresh($).catch(() => {})
66}
67
68// The classic hook fields that say how the session runs right now.
69// How the session runs, as last reported: beacon copies it into the status file.
70export function currentRun(): { mode: string | null; effort: string | null } {
71 return { mode: known.mode ?? null, effort: known.effort ?? null }
72}
73
74function heard($: $, e: { permission_mode?: string; effort?: { level: string } }) {
75 if (e.permission_mode) known = { ...known, mode: e.permission_mode }
76 if (e.effort?.level) known = { ...known, effort: e.effort.level }
77 draw($)
78}
79
80// The status line.
81export const registerStrip: Register = (on, options) => {
82 showBranch = options.showBranch !== false
83 showContext = options.showContext !== false
84 showCost = options.showCost !== false
85 showLine = options.statusLine !== false
86 pollMs = Number(options.statusSeconds ?? 5) * 1000
87 budgetUsd = Math.max(0, Number(options.budgetUsd ?? 0) || 0)
88
89 // The first line, then a refresh on a timer.
90 on('session.start', { cwd: /^/ }, async ($, e, next) => {
91 redraw = () => draw($)
92 if (!showLine) return next(e)
93 const effort = await $.env.get('CLAUDE_EFFORT')
94 if (effort && !known.effort) known = { ...known, effort }
95 await refresh($).catch(() => {})
96 if (pollMs > 0) $.clock.every(pollMs, () => background($))
97 return next(e)
98 })
99
100 on('classic.UserPromptSubmit', async ($, e, next) => {
101 heard($, e)
102 return next(e)
103 }).catch(($, e, next) => next(e))
104
105 on('classic.PostToolUse', async ($, e, next) => {
106 heard($, e)
107 return next(e)
108 }).catch(($, e, next) => next(e))
109
110 on('classic.Stop', async ($, e, next) => {
111 heard($, e)
112 background($)
113 return next(e)
114 }).catch(($, e, next) => next(e))
115}
116hooks/model.ts 101 lines1import type { TowerPending, TowerSession, TowerState } from '../types'
2
3// One row of `claude agents --json`.
4export type AgentRow = {
5 sessionId: string
6 pid?: number
7 cwd: string
8 kind?: string
9 startedAt?: number
10 name?: string
11 status?: string
12}
13
14// beacon's status file, as far as the tower reads it.
15export type StatusFile = {
16 sessionId?: string
17 label?: string
18 state?: string
19 message?: string | null
20 pending?: TowerPending | null
21 remoteAnswers?: boolean
22 lastPrompt?: string | null
23 lastAnswer?: string | null
24 // A problem beacon saw: the same call failing in a row, or the session past its budget.
25 health?: string | null
26 updatedAt?: string
27}
28
29export type TowerMessage =
30 | { kind: 'prompt'; text: string }
31 | { kind: 'answer'; pendingId: string; allow?: boolean; text?: string }
32
33const RANK: Record<TowerState, number> = { 'needs-input': 0, working: 1, done: 2, idle: 3 }
34const BEACON_STATES = new Set(['needs-input', 'working', 'done', 'idle'])
35
36function folder(path: string) {
37 return path.replace(/[\\/]+$/, '').split(/[\\/]/).pop() || path
38}
39
40// The engine's live list is who exists; beacon's file, when there is one, says what each is doing.
41export function merge(agents: readonly AgentRow[], statuses: Readonly<Record<string, StatusFile>>, selfId: string): TowerSession[] {
42 const list = agents.filter(a => a.sessionId !== selfId).map(a => {
43 const file = statuses[a.sessionId]
44 const beacon = file && BEACON_STATES.has(file.state ?? '') ? file : undefined
45 const isBusy = a.status === 'busy'
46 let state: TowerState = isBusy ? 'working' : 'idle'
47 if (beacon) state = beacon.state === 'working' && !isBusy ? 'idle' : (beacon.state as TowerState)
48 const updated = beacon?.updatedAt ? Date.parse(beacon.updatedAt) : NaN
49 return {
50 id: a.sessionId,
51 name: a.name ?? folder(a.cwd),
52 label: beacon?.label ?? folder(a.cwd),
53 cwd: a.cwd,
54 pid: a.pid ?? null,
55 kind: a.kind === 'background' ? 'background' : 'interactive',
56 state,
57 message: beacon?.message ?? null,
58 pending: beacon?.pending ?? null,
59 hasBeacon: Boolean(beacon),
60 remoteAnswers: beacon?.remoteAnswers === true,
61 lastPrompt: beacon?.lastPrompt ?? null,
62 lastAnswer: beacon?.lastAnswer ?? null,
63 health: beacon?.health ?? null,
64 updatedAt: Number.isNaN(updated) ? (a.startedAt ?? 0) : updated,
65 } satisfies TowerSession
66 })
67 return list.sort((x, y) => RANK[x.state] - RANK[y.state] || y.updatedAt - x.updatedAt)
68}
69
70// What the tower sends: beacon reads the JSON; a session without it reads the words.
71export function envelope(hasBeacon: boolean, msg: TowerMessage) {
72 if (!hasBeacon) return msg.kind === 'prompt' ? msg.text : (msg.text ?? (msg.allow ? 'Approved.' : 'Denied.'))
73 return `[tower] ${JSON.stringify({ v: 1, ...msg })}`
74}
75
76export function ago(then: number, now: number) {
77 const s = Math.max(0, Math.round((now - then) / 1000))
78 if (s < 60) return `${s}s`
79 if (s < 3600) return `${Math.round(s / 60)}m`
80 if (s < 86400) return `${Math.round(s / 3600)}h`
81 return `${Math.round(s / 86400)}d`
82}
83
84export const MARK: Record<TowerState, string> = { 'needs-input': '!', working: '~', done: '✓', idle: '·' }
85
86// Theme colors, so light and dark themes both read; idle is drawn dim instead.
87export const COLOR: Record<TowerState, string | undefined> = { 'needs-input': 'warning', working: 'suggestion', done: 'success', idle: undefined }
88
89// Which columns a session row has room for at this pane width.
90export function columns(width: number) {
91 return {
92 showState: width >= 60,
93 showMessage: width >= 90,
94 labelWidth: width >= 60 ? 18 : Math.max(8, width - 14),
95 }
96}
97
98export function tail(text: string, lines: number) {
99 return text.split('\n').filter(line => line.trim()).slice(-lines).join('\n')
100}
101hooks/line.ts 55 lines1// What the status line knows; any part left out is not drawn.
2export type StripParts = {
3 folder: string
4 branch?: string
5 isDirty?: boolean
6 model?: string
7 effort?: string
8 mode?: string
9 contextPercent?: number
10 costUsd?: number
11 // The budget set in /config, drawn beside the cost; 0 or unset draws none.
12 budgetUsd?: number
13 // How full the account's 5-hour rate-limit window is; drawn from LIMIT_SHOWN on.
14 limitPercent?: number
15 // Other sessions waiting on you, from the tower's poll; leads the line when any are.
16 waiting?: number
17}
18
19const MODES: Record<string, string> = {
20 default: 'default',
21 acceptEdits: 'accept edits',
22 plan: 'plan',
23 auto: 'auto',
24 bypassPermissions: 'bypass',
25 dontAsk: "don't ask",
26}
27
28// `claude-opus-5-5` reads `Opus 5.5`; a name already readable is kept.
29// Finds the name wherever it sits in the id, so a provider's prefix
30// ("eu.anthropic.claude-sonnet-5-5") needs no special case.
31export function modelName(model: string) {
32 const m = /claude-([a-z]+)-(\d+)(?:-(\d{1,2})(?!\d))?/i.exec(model)
33 if (!m) return model
34 const family = (m[1] ?? '').charAt(0).toUpperCase() + (m[1] ?? '').slice(1)
35 return `${family} ${m[2]}${m[3] ? '.' + m[3] : ''}${/\[1m\]/i.test(model) ? ' 1M' : ''}`
36}
37
38// Below this the 5-hour window is no news, and the line stays short.
39export const LIMIT_SHOWN = 50
40
41export function compose(p: StripParts) {
42 const where = p.branch ? `${p.folder} ⎇ ${p.branch}${p.isDirty ? '*' : ''}` : p.folder
43 const mode = p.mode === undefined ? undefined : (MODES[p.mode] ?? p.mode)
44 return [
45 p.waiting ? `${p.waiting} need${p.waiting === 1 ? 's' : ''} you` : undefined,
46 where,
47 p.model && modelName(p.model),
48 p.effort,
49 mode,
50 p.contextPercent === undefined ? undefined : `ctx ${Math.round(p.contextPercent)}%`,
51 p.costUsd === undefined ? undefined : `$${p.costUsd.toFixed(2)}${p.budgetUsd ? ` / $${p.budgetUsd.toFixed(2)}` : ''}`,
52 p.limitPercent !== undefined && p.limitPercent >= LIMIT_SHOWN ? `5h ${Math.round(p.limitPercent)}%` : undefined,
53 ].filter(Boolean).join(' · ')
54}
55types/index.d.ts 58 lines1export type TowerState = 'needs-input' | 'working' | 'done' | 'idle'
2
3// What beacon reports a session waits on (its status file's `pending`).
4export type TowerPending = {
5 id: string
6 kind: 'permission' | 'question'
7 title: string
8 detail?: string
9 options?: string[]
10 // The agent's last words before it asked, and for a file edit the change it wants.
11 why?: string
12 diff?: [string, 'hunk' | 'ctx' | 'del' | 'add'][]
13 since?: string
14}
15
16// What this session waits on that the tower can answer; `key` is the permission's
17// exact call, so an approval allows that call and no other.
18export type BeaconPending = TowerPending & { key?: string }
19
20// One running session as the pane draws it: the engine's live list, enriched by beacon.
21export type TowerSession = {
22 id: string
23 name: string
24 label: string
25 cwd: string
26 // The session's process, from which its window is found.
27 pid: number | null
28 kind: 'interactive' | 'background'
29 state: TowerState
30 message: string | null
31 pending: TowerPending | null
32 hasBeacon: boolean
33 remoteAnswers: boolean
34 // The conversation preview beacon keeps: the last prompt, and how the last answer ended.
35 lastPrompt: string | null
36 lastAnswer: string | null
37 // A problem beacon flagged (looping, over budget); the pane shows it instead of the activity.
38 health: string | null
39 updatedAt: number
40}
41
42declare module 'claude-code' {
43 interface PluginState {
44 tower: {
45 // The pane's view of every other session.
46 sessions: TowerSession[]
47 selected: string | null
48 view: 'list' | 'launch'
49 launchRepo: string | null
50 // When the last poll failed, why; the list then is the last good one.
51 error: string | null
52 // This session's own wait for the tower, and one-shot approvals from it.
53 pending: BeaconPending | null
54 approvals: string[]
55 }
56 }
57}
58