Leo agent bridge: delivers leo daemon commands into the session and reports turn events back to the daemon.

<h1 align="center">🐈⬛ Leo</h1>
<em>Supervises Claude Code agents and schedules tasks.</em>
<a href="#install">Install</a> · <a href="#quick-start">Quick Start</a> · <a href="#what-leo-does">What it does</a> · <a href="#cli">CLI</a> · <a href="https://blackpaw-studio.github.io/leo/">Docs</a>
<a href="https://github.com/blackpaw-studio/leo/actions/workflows/ci.yml"><img src="https://github.com/blackpaw-studio/leo/actions/workflows/ci.yml/badge.svg" alt="CI"></a> <a href="https://github.com/blackpaw-studio/leo/releases/latest"><img src="https://img.shields.io/github/v/release/blackpaw-studio/leo" alt="Release"></a> <a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"></a> <a href="https://goreportcard.com/report/github.com/blackpaw-studio/leo"><img src="https://goreportcard.com/badge/github.com/blackpaw-studio/leo?style=flat" alt="Go Report Card"></a>
<img src="docs/demo/leo-demo.gif" alt="leo demo" width="860">
Leo supervises long-running Claude Code agents — spawned from templates, restarted on crash — and runs cron-driven Claude tasks (which can inject into those agents). Manage it from the CLI, a browser, or any Claude Code channel plugin (Telegram, Slack, webhook, …).
Homebrew (recommended):
brew install --cask blackpaw-studio/tap/leo
Shell installer:
curl -fsSL leo.blackpaw.studio/install | sh
Go:
go install github.com/blackpaw-studio/leo/cmd/leo@latest
Prerequisites: authenticated Claude Code CLI, tmux 3.2+ (required — Leo passes agent env via new-session -e, added in 3.2, and uses display-popup). Channel plugins (e.g. claude plugin install telegram@claude-plugins-official) are optional.
Leo runs on its own tmux socket (
-L leo) so your personaltmux lsstays clean. Inspect Leo's sessions directly withtmux -L leo ls. See tmux Config for recommended settings.
macOS Local Network privacy: third-party tools spawned by an agent can be silently denied LAN access (connections fail with "no route to host") if macOS never got the chance to attribute the local-network operation to the signed
leobinary and prompt for consent. Leo runs its tmux server in the foreground so agent processes inherit that consent grant once you've approved it. Runleo doctorto trigger the one-time Allow/Deny dialog and check the current grant state.
Upgrading: leo update replaces a tarball install in place and verifies the new release before swapping the binary. Homebrew users should run brew upgrade --cask blackpaw-studio/tap/leo && leo service restart instead — leo update detects the Homebrew install and prints these commands.
Each release publishes install.sh with a install.sh.sha256:
VER=$(curl -fsSLI -o /dev/null -w '%{url_effective}' \
https://github.com/blackpaw-studio/leo/releases/latest | awk -F/ '{print $NF}')
curl -fsSLO "https://github.com/blackpaw-studio/leo/releases/download/${VER}/install.sh"
curl -fsSLO "https://github.com/blackpaw-studio/leo/releases/download/${VER}/install.sh.sha256"
shasum -a 256 -c install.sh.sha256
sh install.sh
leo update itself verifies the release's Sigstore cosign signature against the release workflow's GitHub OIDC identity, then verifies the tarball SHA-256. Pre-signing releases can be installed with --allow-unsigned (or LEO_ALLOW_UNSIGNED_RELEASE=1); SHA-only verification with a warning. Will be removed once every supported release is signed.
Leo verifies the Fulcio keyless signature but does not consult Rekor. For transparency-log verification, run cosign manually:
VERSION=v0.3.2
curl -fsSL -O https://github.com/blackpaw-studio/leo/releases/download/$VERSION/checksums.txt
curl -fsSL -O https://github.com/blackpaw-studio/leo/releases/download/$VERSION/checksums.txt.sig
curl -fsSL -O https://github.com/blackpaw-studio/leo/releases/download/$VERSION/checksums.txt.pem
cosign verify-blob \
--certificate checksums.txt.pem \
--signature checksums.txt.sig \
--certificate-identity "https://github.com/blackpaw-studio/leo/.github/workflows/release.yml@refs/tags/$VERSION" \
--certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
checksums.txt
leo setup # interactive: profile, workspace, first agent
leo service start # start the daemon in the foreground
leo service start -d # install as a launchd/systemd service
Open the dashboard at <http://127.0.0.1:8370>. For mobile or chat access, install a channel plugin and add its ID to the agent's channels: list.
Two primitives, one daemon:
| Primitive | What it is |
|---|---|
| Agents | Spawned from reusable templates via CLI, web UI, or a channel — with or without a repo. Auto-restart with exponential backoff, own workspace/model/channels/permissions, each in its own tmux session. A long-lived assistant is just an agent that never stops: it persists in the agent store and auto-restores on daemon restart. |
| Tasks | Cron-driven non-interactive Claude runs. Prompt file + schedule. Optional retry, channel notify on failure. |
A web dashboard, a token-authed HTTP API, and a built-in MCP server (so every channel gets /clear, /compact, /stop, /tasks, /agent, /agents for free) all live in the same daemon.
Templates are reusable blueprints — spawn an agent from one with or without a repo:
templates:
assistant:
model: sonnet
channels: [plugin:telegram@claude-plugins-official]
harness_options:
remote_control: true
coding:
model: sonnet
workspace: ~/agents
harness_options:
permission_mode: auto
remote_control: true
leo agent spawn assistant # no repo — run the template as-is, agent named "assistant"
leo agent spawn coding # same, for a repo-driven template
leo agent spawn coding --repo blackpaw-studio/leo --name demo
leo agent spawn coding --repo blackpaw-studio/leo --worktree feat/cache
leo agent attach demo # full tmux attach
leo attach # no name → interactive picker
leo attach demo --cc # iTerm2 / WezTerm native tab via tmux control mode
leo agent stop feat-cache # stop — always dormant, never deletes
leo agent delete feat-cache --delete-branch # clean up the worktree + branch
A repo-less spawn (like assistant above) is how you run a long-lived, always-on assistant — it just keeps running, restarts on crash, and comes back after leo service restart.
tasks:
daily-briefing:
schedule: "0 7 * * *"
timezone: America/New_York
prompt_file: prompts/daily-briefing.md
model: opus
channels: [plugin:telegram@claude-plugins-official]
notify_on_fail: true
enabled: true
The same leo binary becomes a thin SSH client when client.hosts is set — manage agents on a remote leo host without leaving your laptop:
client:
default_host: prod
hosts:
prod: { ssh: alice@leo.example.com }
See the Remote CLI guide.
Leo doesn't ship a messaging channel. Install any Claude Code channel plugin and reference its ID in channels:. The plugin owns its own auth and routing; Leo just hands the resolved list to the spawned Claude process via --channels flags.
For Telegram slash-command autocomplete:
leo channels register-commands telegram
web:
enabled: true
port: 8370
Browser UI for agents, tasks, config, and cron previews. Binds to 127.0.0.1 by default.
Two layered controls protect the daemon:
/web/... and /api/... route. Requests must target 127.0.0.1, localhost, or [::1] on the configured port — or any hostname/IP listed in web.allowed_hosts (required when web.bind is non-loopback). Foreign Host/Origin → 403. Blocks DNS rebinding and drive-by cross-origin POSTs./api/... route. The daemon mints a 32-byte token on first start at ~/.leo/state/api.token (mode 0600). A valid token alone isn't enough — the request must also pass Host pinning.Breaking change:
/api/*previously required no auth. Channel plugins must now sendAuthorization: Bearer $(cat ~/.leo/state/api.token)or get401.
TOKEN=$(cat ~/.leo/state/api.token)
curl -sH "Authorization: Bearer $TOKEN" http://127.0.0.1:8370/api/task/list
The token file is readable by any process running as the same Unix user — intentional, so co-tenant plugins can read it directly. Rotate by deleting the file and restarting the daemon.
| Command | What it does |
|---|---|
leo setup | Interactive setup wizard |
leo status | Overall snapshot — service, agents, tasks, templates, web |
leo validate | Check config, prerequisites, workspace health |
leo doctor | Diagnose local network and daemon health (macOS Local Network privacy) |
leo service start / stop / restart / logs | Supervisor lifecycle |
leo task … | list, add, remove, enable, disable, history, logs |
leo template … | list, show, remove |
leo agent … | list, spawn, attach, stop, logs (local or over SSH) |
leo run <task> | Run a task once on demand |
leo config show / edit | Inspect (--raw, --json) or edit the effective config |
leo update | Self-update the binary |
Full reference: blackpaw-studio.github.io/leo/cli.
make build # → bin/leo
make test # go test -race -cover ./...
make lint # go vet + staticcheck
MIT
Named for my void Leo. He's a good kitty.
hooks/register.js 1330 lines1// leo-bridge: runs inside Claude Code. It streams commands from the leo daemon
2// (`leo bridge --agent <key> --launch <launch>`, one JSON command per stdout
3// line), executes them in order, and reports acks and turn events back via
4// `leo bridge report --agent <key> --launch <launch>` with the JSON report on
5// stdin. It never decides policy. <key> is $LEO_BRIDGE_AGENT, the name leo
6// routes by; <launch> is $LEO_BRIDGE_LAUNCH, the token leo minted for this
7// claude process, so the daemon can refuse a process that is no longer the
8// key's current one.
9//
10// Mods API rules this file follows: every `$.ns.method` call is spelled in
11// full, event names are string literals, and `$` is only passed to top-level
12// functions of this file. Module state resets on hot reload.
13
14import {
15 ACKED_KEY_PREFIX,
16 ackedFromStore,
17 ackReport,
18 ACTIVITY_MIN_INTERVAL_MS,
19 appendAcked,
20 BACKOFF_INITIAL_MS,
21 BACKOFF_RESET_AFTER_MS,
22 capField,
23 compactTrigger,
24 describeExit,
25 DISPATCH_KEY_PREFIX,
26 errorText,
27 eventId,
28 eventReport,
29 forkConsultAnswer,
30 helloReport,
31 inflightIds,
32 isAckedEntryStale,
33 isFinalSessionEnd,
34 nextBackoff,
35 parseCommand,
36 REPORT_RETRY_DELAYS_MS,
37 reportText,
38 splitLines,
39 REJECTED_REPORT_EXIT_CODE,
40 STALE_LAUNCH_EXIT_CODE,
41 questionSummary,
42 toolActivity,
43 touchedEntry,
44 turnTokens,
45 stopHookPending,
46 withAcked,
47 withInflight,
48} from './protocol.js'
49import {
50 AGENT_DENY_TEXT,
51 cancelRequest,
52 DELEGATION_SECTION_ID,
53 delegationNote,
54 FALLBACK_DISPATCH_FAILED,
55 FALLBACK_NO_STATE,
56 FALLBACK_STREAM_DOWN,
57 isAgentHidden,
58 isFailedCall,
59 bandChrome,
60 bandHeader,
61 isRunning,
62 lingerEnd,
63 NO_DELEGATION,
64 parseStateLine,
65 rosterRows,
66 sameDelegationText,
67 shownDispatches,
68 STATE_WAIT_MS,
69 statusLine,
70 TOLD_KEY_PREFIX,
71 toldEntry,
72 toldFromEntry,
73 trackTerminal,
74} from './roster.js'
75
76const REPORT_TIMEOUT_MS = 15_000
77const SESSION_END_WAIT_MS = 1000
78const COMPACT_ATTEMPTS = 5
79// How long a failed compact waits before its next try: briefly while the
80// mod knows no turn runs, else until the running turn ends (bounded, in
81// case the failure was something else).
82const COMPACT_RETRY_MS = 250
83const COMPACT_TURN_WAIT_MS = 60_000
84const ROSTER_TICK_MS = 1000
85const TOAST_MS = 5000
86const NOTE_FAILED = 'adding the delegation note failed: '
87const GUARD_FAILED = 'delegation guard failed, allowing: '
88// A told entry left unwritten this long is pruned; a live session's is
89// restamped at most this often so it never gets there; one session.start
90// prunes at most this many.
91const TOLD_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000
92const TOLD_RESTAMP_MS = 60 * 60 * 1000
93const TOLD_PRUNE_LIMIT = 200
94// Where the next session.start's prune window begins among the told keys,
95// so successive starts reach every entry.
96const TOLD_PRUNE_CURSOR_KEY = 'prune:told'
97
98// Ops whose engine call queues the work: the engine runs it even if this
99// module is hot-reloaded before the call returns.
100const HANDED_OFF_OPS = ['deliver', 'clear']
101
102// Bridge identity, read from the environment at session.start; null = disabled.
103let config = null
104let isStarted = false
105// Set once the daemon refuses this launch for good (a successor holds the
106// key, or no daemon adopted this session): the mod neither reconnects nor
107// reports until a reload resets it.
108let isDormant = false
109
110// The main loop's running turn id, or null when idle, and who waits for the
111// turn to end. isTurnKnown is false until this module sees a turn start or
112// end: a module loaded by a hot reload mid-turn cannot tell that turn runs.
113let runningTurn = null
114let isTurnKnown = false
115let idleWaiters = []
116
117// The effort last reported (see the turn.step hook), so only a change is sent.
118let reportedEffort = null
119
120// Whether a deliver's $.prompt.submit is in flight (it resolves once its
121// turn starts), and the interrupts waiting for the turn it starts.
122let isSubmitting = false
123let turnStartWaiters = []
124
125// The submits whose turn has not started yet, in the order they were made
126// (the order the engine runs them in). A deliver of this mod is { stamp }: the
127// command id and origin its turn is reported under. The engine raises no
128// prompt.submit hook for the plugin's own $.prompt.submit (verified on
129// 2.1.294), so every submit the hook does see is somebody else's: { origin,
130// queuedOver }, the kind of origin it came from and, for a prompt typed over
131// a running turn, that turn's id until the prompt enters the session at the
132// turn's next step (folded: no turn of its own) or the turn completes (it
133// runs as a turn of its own, next). Each entry has an id that survives the
134// entry being replaced, so what made it can forget it. See takeTurnOwner.
135let pendingSubmits = []
136let lastSubmitId = 0
137const MAX_OBSERVED_SUBMITS = 32
138
139// Serial chains: reports keep per-process order; commands run one at a time;
140// store entry rewrites never interleave (an interrupt settles off the
141// command chain).
142let reportChain = Promise.resolve()
143let commandChain = Promise.resolve()
144let storeChain = Promise.resolve()
145
146// Ids queued or executing (guards redelivery while in flight) and the
147// persisted list of ids already acked ok (null until loaded from $.store).
148let pendingIds = new Set()
149let ackedIds = null
150
151// One log line per failure streak, so a down daemon doesn't flood the transcript.
152let isStreamFailing = false
153let isReportFailing = false
154
155// The session id the last hello carried (null until the first hello).
156let helloSessionId = null
157
158// The daemon's latest state snapshot and when it arrived, or null before
159// the first; why delegation is not enforced (a FALLBACK_* reason) or null;
160// what the model was last told ({ session, delegation }, read from $.store
161// once: undefined until then, null for nothing) and the chain that keeps
162// its read-and-writes in order; the status line last set; the redraw ticker.
163let roster = null
164let stateChain = Promise.resolve()
165let fallback = null
166// Bumped when a stream opens and when it ends: a snapshot clears the
167// fallback only while the stream it came from is still the one up.
168let streamEpoch = 0
169let told = undefined
170let toldChain = Promise.resolve()
171// When this module last wrote the session's told entry ({ session, at }),
172// so restamps run at most hourly; and whether a dispatch's final end
173// deleted its entries, after which none is written again.
174let toldStamped = null
175let isToldForgotten = false
176let shownStatus = undefined
177let ticker = null
178// When each terminal dispatch was first seen terminal; null until the first
179// snapshot after a (re)load.
180let terminalSeen = null
181
182// Observe state: the home directory tool summaries abbreviate; the main
183// loop's running tool calls, oldest first ({ activity }); the activity
184// waiting to be sent (latest wins), whether a send is scheduled, and when
185// and what was last sent; the attention kind awaiting the person, or null;
186// the ids of the subagents running.
187let home = ''
188let mainCalls = []
189let pendingActivity = null
190let isActivityScheduled = false
191let activitySentAt = -Infinity
192let activitySentBody = null
193let activityChain = Promise.resolve()
194let attention = null
195let subagentIds = new Set()
196// The pending work the main loop's last Stop hook listed (see
197// stopHookPending), for its turn.complete; cleared as a turn starts.
198let stopPending = undefined
199
200function isBridging() {
201 return config !== null && !isDormant
202}
203
204function ackedKey() {
205 return ACKED_KEY_PREFIX + config.agent
206}
207
208// launch names this claude process (leo sets it per launch): the daemon
209// takes a stream or report only from the key's current launch, and what an
210// earlier load of the mod handed the engine is this process's only while
211// it matches. Null (bridge disabled) unless all three are set.
212async function readConfig($) {
213 const bin = await $.env.get('LEO_BRIDGE_BIN')
214 const agent = await $.env.get('LEO_BRIDGE_AGENT')
215 const launch = await $.env.get('LEO_BRIDGE_LAUNCH')
216 if (!bin || !agent || !launch) return null
217 return { bin, agent, launch }
218}
219
220// `leo bridge` argv naming this process's key and launch; sub is a
221// subcommand ('report') or none for the stream.
222function bridgeArgv(...sub) {
223 return [config.bin, 'bridge', ...sub, '--agent', config.agent, '--launch', config.launch]
224}
225
226// ---- reports -------------------------------------------------------------
227
228// Sends one report, retrying with backoff while the daemon does not take it
229// (down, restarting). Retrying is safe — acks are idempotent and events are
230// state, not counters — and it happens inside the report chain, so later
231// reports wait behind it and per-process order holds. The JSON goes on stdin:
232// a prompt or answer can be far larger than argv allows.
233async function sendReport($, report) {
234 const argv = bridgeArgv('report')
235 const body = JSON.stringify(report)
236 for (let attempt = 0; ; attempt++) {
237 const { why, isRejected } = await attemptReport($, argv, body)
238 if (why === null) {
239 isReportFailing = false
240 return
241 }
242 if (isRejected) {
243 // Refused for good: retrying would only hold up the reports behind it.
244 $.ui.log('report rejected, dropped: ' + why)
245 return
246 }
247 if (attempt >= REPORT_RETRY_DELAYS_MS.length) {
248 reportFailed($, why)
249 return
250 }
251 await $.clock.sleep(REPORT_RETRY_DELAYS_MS[attempt])
252 }
253}
254
255// One report attempt: null on success, else why it failed.
256async function tryReport($, argv, body) {
257 return (await attemptReport($, argv, body)).why
258}
259
260// One report attempt: why is null on success, else why it failed;
261// isRejected when the daemon refused the report for good.
262async function attemptReport($, argv, body) {
263 try {
264 const result = await $.process.run(argv, { timeoutMs: REPORT_TIMEOUT_MS, stdin: body })
265 if (result.exitCode === 0) return { why: null, isRejected: false }
266 const why = 'exit ' + result.exitCode + ': ' + result.stderr.trim()
267 return { why, isRejected: result.exitCode === REJECTED_REPORT_EXIT_CODE }
268 } catch (err) {
269 return { why: errorText(err), isRejected: false }
270 }
271}
272
273function reportFailed($, why) {
274 if (isReportFailing) return
275 isReportFailing = true
276 $.ui.log('report failed: ' + why)
277}
278
279// Queues a report behind earlier ones. `build` may be async (e.g. reads usage);
280// the returned promise settles once this report has been sent or has failed.
281function enqueueReport($, build) {
282 const sent = reportChain.then(() => buildAndSend($, build))
283 reportChain = sent
284 return sent
285}
286
287async function buildAndSend($, build) {
288 try {
289 const report = await build()
290 if (report !== null) await sendReport($, report)
291 } catch (err) {
292 reportFailed($, errorText(err))
293 }
294}
295
296// The turn id a report names its turn by; undefined when the engine gave none.
297function turnIdField(turnId) {
298 return typeof turnId === 'string' && turnId !== '' ? turnId : undefined
299}
300
301// Only keys with a value: a report never carries an explicit undefined.
302function defined(fields) {
303 return Object.fromEntries(Object.entries(fields).filter(([, v]) => v !== undefined))
304}
305
306// The final assistant message is what a dispatch returns as its result.
307// Usage is a nice-to-have: a turn.complete without it still ends the turn
308// in the daemon, which a lost turn.complete would leave stuck busy.
309// tokens is the turn's own usage (TurnUsage) as the engine reported it;
310// pending is the background work its Stop hook left in flight, if any.
311async function turnCompleteReport($, e, tokens, pending) {
312 const fields = {
313 event_id: eventId('turn.complete', e.turnId),
314 turn_id: turnIdField(e.turnId),
315 message: reportText(e.answer),
316 reason: e.isAborted === true ? 'aborted' : undefined,
317 tokens: turnTokens(tokens),
318 pending,
319 }
320 try {
321 const usage = await $.session.usage()
322 return eventReport('turn.complete', defined({ ...fields, usage }))
323 } catch (err) {
324 $.ui.log('reading session usage failed: ' + errorText(err))
325 return eventReport('turn.complete', defined(fields))
326 }
327}
328
329async function sendHello($) {
330 const sessionId = await $.session.id()
331 const version = await $.session.version()
332 helloSessionId = sessionId
333 return helloReport(sessionId, version.version, isTurnKnown ? runningTurn !== null : undefined, subagentIds.size)
334}
335
336// Re-says hello when the session id moved since the last one (a /clear or
337// resume keeps the process but starts a new session); null sends nothing.
338async function helloIfSessionChanged($) {
339 if (helloSessionId === null) return null
340 const sessionId = await $.session.id()
341 if (sessionId === helloSessionId) return null
342 return sendHello($)
343}
344
345// ---- turn state ----------------------------------------------------------
346
347function markRunning(turnId) {
348 runningTurn = turnId
349 isTurnKnown = true
350 settleTurnStartWaiters(turnId)
351}
352
353function markIdle() {
354 runningTurn = null
355 isTurnKnown = true
356 const waiters = idleWaiters
357 idleWaiters = []
358 waiters.forEach((resolve) => resolve())
359}
360
361function waitForIdle() {
362 if (runningTurn === null) return Promise.resolve()
363 return nextTurnEnd()
364}
365
366// Resolves at the next main-loop turn.complete, whatever the mod knows now.
367function nextTurnEnd() {
368 return new Promise((resolve) => {
369 idleWaiters = [...idleWaiters, resolve]
370 })
371}
372
373// Resolves with the id of the next main-loop turn to start, or null if the
374// submit in flight settles without starting one.
375function nextTurnStart() {
376 return new Promise((resolve) => {
377 turnStartWaiters = [...turnStartWaiters, resolve]
378 })
379}
380
381function settleTurnStartWaiters(turnId) {
382 const waiters = turnStartWaiters
383 turnStartWaiters = []
384 waiters.forEach((resolve) => resolve(turnId))
385}
386
387// Notes a deliver until its turn starts; the returned function forgets it
388// (its turn started, or it was dropped or failed).
389function trackSubmit(stamp) {
390 const id = ++lastSubmitId
391 pendingSubmits = [...pendingSubmits, { id, stamp }]
392 return () => forgetSubmit(id)
393}
394
395function forgetSubmit(id) {
396 pendingSubmits = pendingSubmits.filter((p) => p.id !== id)
397}
398
399// Everything pending belongs to the session that ends or starts (a /clear, a
400// resume): none of it has a turn to come in the next. That includes a deliver
401// in flight, cancelled here: its submit settling later forgets an entry that
402// is already gone, and its turn, should it still run, goes unstamped for the
403// daemon's armed-binding fallback to place.
404function forgetAllSubmits() {
405 pendingSubmits = []
406}
407
408// An origin kind the daemon accepts as a report's origin, or undefined.
409function originKind(origin) {
410 const kind = origin?.kind
411 return typeof kind === 'string' && /^[A-Za-z0-9._-]{1,32}$/.test(kind) ? kind : undefined
412}
413
414// Notes a submit someone else made; the engine raises prompt.submit before
415// that submit's turn.start. The oldest are dropped past the cap: nothing
416// starts a turn for every submit (a refusal below the hook, say).
417function noteObservedSubmit(e) {
418 const entry = { id: ++lastSubmitId, origin: originKind(e.origin), queuedOver: turnIdField(e.turnId) }
419 const observed = pendingSubmits.filter((p) => p.stamp === undefined)
420 const dropped = observed.length >= MAX_OBSERVED_SUBMITS ? observed[0] : undefined
421 pendingSubmits = [...pendingSubmits.filter((p) => p !== dropped), entry]
422 return entry.id
423}
424
425// The turn a step begins is the one prompts typed over it enter: they have
426// folded into it and start no turn of their own.
427function foldQueuedInto(turnId) {
428 pendingSubmits = pendingSubmits.filter((p) => p.queuedOver === undefined || p.queuedOver !== turnId)
429}
430
431// Prompts typed over a turn that completed without folding them run next, as
432// turns of their own.
433function releaseQueuedAfter(turnId) {
434 pendingSubmits = pendingSubmits.map((p) => (p.queuedOver !== undefined && p.queuedOver === turnId ? { ...p, queuedOver: undefined } : p))
435}
436
437// The submit this turn.start is the turn of: the oldest one that is ready to
438// run (not typed over a turn still running), or undefined for a turn nothing
439// submitted (a continuation). The engine runs submits in the order they were
440// made, so the order alone says whose a turn is, whatever words it carries:
441// a deliver's stamp is its own, and a person's or a wake's origin is theirs.
442// An unobserved turn while a deliver waits is the deliver's, since the
443// deliver is the only submit the hook never sees.
444function takeTurnOwner() {
445 const owner = pendingSubmits.find((p) => p.queuedOver === undefined)
446 if (owner) forgetSubmit(owner.id)
447 return owner
448}
449
450// ---- commands ------------------------------------------------------------
451
452async function loadAcked($) {
453 if (ackedIds !== null) return ackedIds
454 try {
455 const stored = ackedFromStore(await $.store.get(ackedKey()))
456 // A concurrent remember may have set it while we awaited; keep the newer.
457 if (ackedIds === null) ackedIds = stored
458 } catch (err) {
459 $.ui.log('reading acked ids failed: ' + errorText(err))
460 if (ackedIds === null) ackedIds = []
461 }
462 return ackedIds
463}
464
465// Rewrites this key's store entry with change(entry as stored, now), one
466// rewrite at a time. Every write reads the entry first, so a late write from
467// a module a hot reload replaced changes only what it means to.
468function updateEntry($, change) {
469 const written = storeChain.then(() => rewriteEntry($, change))
470 storeChain = written
471 return written
472}
473
474async function rewriteEntry($, change) {
475 try {
476 const key = ackedKey()
477 const now = await $.clock.now()
478 await $.store.set(key, change(await $.store.get(key), now))
479 } catch (err) {
480 $.ui.log('saving acked ids failed: ' + errorText(err))
481 }
482}
483
484// Records, before its engine call, that command id is being handed to the
485// engine by this launch.
486async function markHandedOff($, id) {
487 await updateEntry($, (entry, now) => withInflight(entry, config.launch, id, true, now))
488}
489
490// Restamps this process's entry: it is alive, whatever another's prune
491// makes of its age.
492function touchEntry($) {
493 return updateEntry($, touchedEntry)
494}
495
496// Whether an earlier load of the mod in this process handed command id to
497// the engine and never settled it: a hot reload caught it in flight.
498async function wasHandedOff($, id) {
499 try {
500 return inflightIds(await $.store.get(ackedKey()), config.launch).includes(id)
501 } catch (err) {
502 $.ui.log('reading acked ids failed: ' + errorText(err))
503 return false
504 }
505}
506
507// Records how command settled in one write: acked if ok, and no longer in
508// flight. One write, so a reload never finds it neither acked nor in flight.
509async function settleCommand($, command, ok) {
510 const isHandedOff = HANDED_OFF_OPS.includes(command.op)
511 if (ok) ackedIds = appendAcked(ackedIds ?? [], command.id)
512 if (!ok && !isHandedOff) return
513 await updateEntry($, (entry, now) => {
514 const acked = ok ? withAcked(entry, command.id, now) : entry
515 return isHandedOff ? withInflight(acked, config.launch, command.id, false, now) : acked
516 })
517}
518
519// Deletes other processes' acked entries left unwritten past
520// ACKED_MAX_AGE_MS. This process's own entry stays, however old: it is in
521// use.
522async function pruneAcked($) {
523 try {
524 const now = await $.clock.now()
525 const own = ackedKey()
526 const keys = (await $.store.keys()).filter((key) => key.startsWith(ACKED_KEY_PREFIX) && key !== own)
527 for (const key of keys) {
528 if (isAckedEntryStale(await $.store.get(key), now)) await $.store.delete(key)
529 }
530 } catch (err) {
531 $.ui.log('pruning acked ids failed: ' + errorText(err))
532 }
533}
534
535// A dispatch's claude never comes back once its session ends for good, so
536// its acked entry goes with it; an agent's is kept for its next resume. The
537// delete waits its turn on the store chain, so a rewrite still landing (the
538// last turn's restamp) cannot bring the entry back.
539function forgetDispatchAcked($, reason) {
540 if (!config.agent.startsWith(DISPATCH_KEY_PREFIX) || !isFinalSessionEnd(reason)) return Promise.resolve()
541 const deleted = storeChain.then(() => deleteEntry($))
542 storeChain = deleted
543 // Told writes run on toldChain: the cleanup queues there, behind any
544 // still landing, so none can bring an entry back; not behind a read of
545 // the baseline, whose failure must not skip it.
546 const forgotten = toldChain.then(() => forgetTold($))
547 toldChain = forgotten
548 return Promise.all([deleted, forgotten])
549}
550
551async function deleteEntry($) {
552 try {
553 await $.store.delete(ackedKey())
554 } catch (err) {
555 $.ui.log('deleting acked ids failed: ' + errorText(err))
556 }
557}
558
559async function forgetTold($) {
560 isToldForgotten = true
561 try {
562 const prefix = toldKeyPrefix()
563 for (const key of (await $.store.keys()).filter((k) => k.startsWith(prefix))) await $.store.delete(key)
564 } catch (err) {
565 $.ui.log('deleting the told delegation failed: ' + errorText(err))
566 }
567}
568
569async function runDeliver($, command) {
570 const args = command.asUser ? { text: command.text, asUser: true } : { text: command.text }
571 await markHandedOff($, command.id)
572 isSubmitting = true
573 const untrack = trackSubmit({ commandId: command.id, origin: 'plugin' })
574 try {
575 const result = await $.prompt.submit(args)
576 if (result && typeof result.drop === 'string') {
577 settleTurnStartWaiters(null)
578 return { ok: false, error: result.drop }
579 }
580 return { ok: true }
581 } catch (err) {
582 settleTurnStartWaiters(null)
583 throw err
584 } finally {
585 untrack()
586 isSubmitting = false
587 }
588}
589
590async function runCompact($, command) {
591 const args = command.instructions === undefined ? undefined : { instructions: command.instructions }
592 let lastError = null
593 for (let attempt = 0; attempt < COMPACT_ATTEMPTS; attempt++) {
594 await waitForIdle()
595 try {
596 // The engine skips the caller's own session.compact hook, so this
597 // compaction's phases are reported here.
598 const result = await observeCompact($, 'manual', () => $.session.compact(args))
599 if (result && typeof result.skip === 'string') return { ok: false, error: result.skip }
600 return { ok: true }
601 } catch (err) {
602 lastError = err
603 await waitBeforeCompactRetry($)
604 }
605 }
606 return { ok: false, error: errorText(lastError) }
607}
608
609// compact rejects while a turn runs. One may have started between the idle
610// check and the call (a just-delivered prompt), or be running unseen (this
611// module was loaded by a hot reload mid-turn): either way the retry waits
612// for that turn to end, not a fixed beat.
613async function waitBeforeCompactRetry($) {
614 const isIdle = isTurnKnown && runningTurn === null
615 await Promise.race([nextTurnEnd(), $.clock.sleep(isIdle ? COMPACT_RETRY_MS : COMPACT_TURN_WAIT_MS)])
616}
617
618// A clear took only if the session it ran in ended: the id moves on. A hook
619// may answer /clear in its place, and then nothing was cleared.
620async function runClear($, command) {
621 // Verified live on v2.1.289: a mod's command.run reaches the built-in
622 // /clear (context wiped, session.end reason 'clear', the pump survives).
623 await waitForIdle()
624 const before = await $.session.id()
625 await markHandedOff($, command.id)
626 const result = await $.command.run({ command: 'clear', args: '' })
627 if ((await $.session.id()) !== before) return { ok: true }
628 const said = result && typeof result.text === 'string' ? result.text.trim() : ''
629 return { ok: false, error: '/clear left the session as it was' + (said ? ': ' + said : '') }
630}
631
632// An interrupt aborts the running turn. One that lands while a submit is in
633// flight with no turn running yet aborts the turn that submit starts, once
634// it starts (or acks if the submit is dropped and starts none).
635async function runInterrupt($) {
636 const turnId = runningTurn ?? (isSubmitting ? await nextTurnStart() : null)
637 if (turnId === null) return { ok: true }
638 try {
639 await $.turn.abort({ turnId })
640 return { ok: true }
641 } catch (err) {
642 // The turn may have ended on its own between our read and the abort.
643 if (runningTurn !== turnId) return { ok: true }
644 return { ok: false, error: errorText(err) }
645 }
646}
647
648async function execute($, command) {
649 try {
650 if (command.op === 'deliver') return await runDeliver($, command)
651 if (command.op === 'compact') return await runCompact($, command)
652 if (command.op === 'clear') return await runClear($, command)
653 if (command.op === 'interrupt') return await runInterrupt($)
654 return { ok: false, error: 'unknown op: ' + command.op }
655 } catch (err) {
656 return { ok: false, error: errorText(err) }
657 }
658}
659
660async function handleCommand($, command) {
661 const acked = await loadAcked($)
662 if (acked.includes(command.id)) {
663 enqueueReport($, () => ackReport(command.id, true))
664 return
665 }
666 if (HANDED_OFF_OPS.includes(command.op) && (await wasHandedOff($, command.id))) {
667 // The engine still holds it and runs it: running it again would run it
668 // twice. It counts as accepted now.
669 await settleCommand($, command, true)
670 enqueueReport($, () => ackReport(command.id, true))
671 return
672 }
673 const result = await execute($, command)
674 await settleCommand($, command, result.ok)
675 enqueueReport($, () => ackReport(command.id, result.ok, result.error))
676}
677
678// deliver/compact/clear run strictly in order on commandChain; interrupt runs
679// at once so it can abort a turn that queued commands are waiting behind.
680function enqueueCommand($, command) {
681 if (pendingIds.has(command.id)) return
682 pendingIds = new Set([...pendingIds, command.id])
683 const run = () => handleCommand($, command)
684 const started = command.op === 'interrupt' ? run() : commandChain.then(run)
685 const finished = started
686 .catch((err) => $.ui.log('command ' + command.id + ' failed: ' + errorText(err)))
687 .then(() => {
688 pendingIds = new Set([...pendingIds].filter((id) => id !== command.id))
689 })
690 if (command.op !== 'interrupt') commandChain = finished
691}
692
693function receiveLine($, line, epoch) {
694 const state = parseStateLine(line)
695 if (state !== null) {
696 stateChain = stateChain.then(() => applyState($, state, epoch))
697 return
698 }
699 const parsed = parseCommand(line)
700 if (parsed.kind === 'garbage') {
701 $.ui.log('skipped a bad command line: ' + parsed.error)
702 return
703 }
704 if (parsed.kind === 'invalid') {
705 if (pendingIds.has(parsed.id)) return
706 enqueueReport($, () => ackReport(parsed.id, false, parsed.error))
707 return
708 }
709 enqueueCommand($, parsed.command)
710}
711
712// ---- the stream pump -----------------------------------------------------
713
714// Resolves with why the stream ended, and whether the daemon refused this
715// launch for good.
716async function runStream($) {
717 const argv = bridgeArgv()
718 const epoch = ++streamEpoch
719 const stream = $.process.spawn({ argv })
720 await enqueueReport($, () => sendHello($))
721 let carry = ''
722 let stderr = ''
723 for await (const chunk of stream) {
724 if (chunk.stream !== 'stdout') {
725 stderr = (stderr + chunk.text).slice(-2000)
726 continue
727 }
728 const split = splitLines(carry, chunk.text)
729 carry = split.carry
730 split.lines.forEach((line) => receiveLine($, line, epoch))
731 }
732 // A partial last line is dropped: the daemon redelivers anything unacked.
733 const result = await stream.result
734 return { why: describeExit(result, stderr), isStale: result?.code === STALE_LAUNCH_EXIT_CODE }
735}
736
737function streamEnded($, why) {
738 if (isStreamFailing) return
739 isStreamFailing = true
740 $.ui.log('bridge stream ended' + (why ? ': ' + why : '') + '; reconnecting')
741}
742
743async function pump($) {
744 let backoff = BACKOFF_INITIAL_MS
745 for (;;) {
746 const startedAt = await $.clock.now()
747 let why = ''
748 try {
749 const ended = await runStream($)
750 if (ended.isStale) return goDormant($)
751 why = ended.why
752 } catch (err) {
753 why = errorText(err)
754 }
755 const lived = (await $.clock.now()) - startedAt
756 if (lived > BACKOFF_RESET_AFTER_MS) isStreamFailing = false
757 streamEnded($, why)
758 streamDown($)
759 const plan = nextBackoff(backoff, lived)
760 await $.clock.sleep(plan.waitMs)
761 backoff = plan.next
762 }
763}
764
765function goDormant($) {
766 isDormant = true
767 streamDown($)
768 $.ui.log('this claude is no longer ' + config.agent + "'s current leo launch; the bridge stays off until the mod reloads")
769}
770
771// ---- delegation and roster -----------------------------------------------
772
773function streamDown($) {
774 streamEpoch++
775 setFallback($, FALLBACK_STREAM_DOWN)
776}
777
778// A snapshot replaces the last whole, and clears any fallback: leo answers.
779// One applied after its stream ended (it waited behind an earlier one) is
780// still the latest word on the roster, but says nothing of leo being up.
781async function applyState($, state, epoch) {
782 try {
783 const previous = roster === null ? null : roster.state.delegation
784 const now = await $.clock.now()
785 roster = { state, receivedAt: now }
786 terminalSeen = trackTerminal(terminalSeen, state.dispatches, now)
787 if (epoch === streamEpoch) fallback = null
788 if (!sameDelegationText(previous, state.delegation)) $.ui.invalidate('prompt.section')
789 await noteIfToldOtherwise($, state.delegation)
790 refreshRoster($, now)
791 } catch (err) {
792 $.ui.log('applying leo state failed: ' + errorText(err))
793 }
794}
795
796// The engine keeps the system prompt it composed first (verified live on
797// 2.1.292: neither a re-fired prompt.compose nor an invalidate reaches the
798// model), so a policy other than what the model was told goes in as a
799// note. Compared with what it was told, not the last snapshot: a reloaded
800// module has no last snapshot.
801async function noteIfToldOtherwise($, delegation) {
802 await withTold($, async (current, session) => {
803 if (current === null || sameDelegationText(current, delegation)) return
804 // Told only once the note is in: a failed one is retried next snapshot.
805 if (await addNote($, delegationNote(delegation))) await saveTold($, session, delegation)
806 })
807}
808
809// What the model was told outlives a hot reload (the engine keeps its
810// prompt and notes) and belongs to one session: a /clear starts another
811// and a resume comes back. Each lives in $.store under its own key.
812function toldKeyPrefix() {
813 return TOLD_KEY_PREFIX + config.agent + ':'
814}
815
816function toldKey(session) {
817 return toldKeyPrefix() + session
818}
819
820// Runs fn with what this session's model was last told (null: nothing
821// yet) and the session's id, after every earlier read-and-write of it.
822function withTold($, fn) {
823 const done = toldChain.then(async () => {
824 const session = await $.session.id()
825 return fn(await readTold($, session), session)
826 })
827 toldChain = done.catch((err) => $.ui.log('tracking the told delegation failed: ' + errorText(err)))
828 return toldChain
829}
830
831// The cache holds one session's; another session's is read afresh. A
832// read restamps the entry (at most hourly) so a live session's never ages.
833async function readTold($, session) {
834 if (told === undefined || told.session !== session) {
835 told = { session, delegation: toldFromEntry(await $.store.get(toldKey(session)), session) }
836 }
837 if (told.delegation !== null) await restampTold($, session, told.delegation)
838 return told.delegation
839}
840
841async function saveTold($, session, delegation) {
842 told = { session, delegation }
843 await writeTold($, session, delegation, await $.clock.now())
844}
845
846async function restampTold($, session, delegation) {
847 const now = await $.clock.now()
848 if (toldStamped !== null && toldStamped.session === session && now - toldStamped.at < TOLD_RESTAMP_MS) return
849 await writeTold($, session, delegation, now)
850}
851
852async function writeTold($, session, delegation, now) {
853 if (isToldForgotten) return
854 await $.store.set(toldKey(session), toldEntry(session, delegation, now))
855 toldStamped = { session, at: now }
856}
857
858// Deletes told entries, any agent's, left unwritten past TOLD_MAX_AGE_MS,
859// save this session's, looking at no more than TOLD_PRUNE_LIMIT of them
860// (the rest wait for a later start). A live session restamps its own.
861async function pruneTold($) {
862 try {
863 const now = await $.clock.now()
864 const own = toldKey(await $.session.id())
865 const keys = (await $.store.keys()).filter((key) => key.startsWith(TOLD_KEY_PREFIX) && key !== own)
866 const cursor = await $.store.get(TOLD_PRUNE_CURSOR_KEY)
867 const window = pruneWindow(keys, Number.isInteger(cursor) ? cursor : 0, TOLD_PRUNE_LIMIT)
868 // Fewer keys than the limit are all looked at: no window to move on.
869 if (keys.length > TOLD_PRUNE_LIMIT) await $.store.set(TOLD_PRUNE_CURSOR_KEY, window.next)
870 for (const key of window.keys) {
871 if (isToldEntryStale(await $.store.get(key), now)) await $.store.delete(key)
872 }
873 } catch (err) {
874 $.ui.log('pruning told delegations failed: ' + errorText(err))
875 }
876}
877
878// The limit keys from cursor on, wrapping round, and where the window after
879// this one begins (as counted before any of these are deleted).
880function pruneWindow(keys, cursor, limit) {
881 if (keys.length === 0) return { keys: [], next: 0 }
882 const start = cursor % keys.length
883 const window = [...keys.slice(start), ...keys.slice(0, start)].slice(0, limit)
884 return { keys: window, next: (start + window.length) % keys.length }
885}
886
887function isToldEntryStale(value, now) {
888 return typeof value?.at !== 'number' || now - value.at > TOLD_MAX_AGE_MS
889}
890
891// Whether the note went in.
892async function addNote($, text) {
893 try {
894 const appended = await $.session.append({ message: { type: 'user', content: [{ type: 'text', text }] } })
895 if (typeof appended?.deny !== 'string') return true
896 $.ui.log(NOTE_FAILED + appended.deny)
897 } catch (err) {
898 $.ui.log(NOTE_FAILED + errorText(err))
899 }
900 return false
901}
902
903function setFallback($, why) {
904 fallback = why
905 refreshStatus($)
906}
907
908function clearDispatchFallback($) {
909 if (fallback === FALLBACK_DISPATCH_FAILED) setFallback($, null)
910}
911
912function noStateYet($) {
913 if (roster === null && fallback === null) setFallback($, FALLBACK_NO_STATE)
914}
915
916function refreshStatus($) {
917 const text = statusLine(roster === null ? null : roster.state, fallback)
918 if (text === shownStatus) return
919 shownStatus = text
920 $.ui.status(text)
921}
922
923// Redraws the band now, and every second while a dispatch runs (its
924// elapsed time counts between snapshots) or an ended one lingers (it
925// leaves the band when its time is up).
926function refreshRoster($, now) {
927 refreshStatus($)
928 $.ui.invalidate('ui.render')
929 if (isBandTicking(now)) {
930 if (ticker === null) ticker = $.clock.every(ROSTER_TICK_MS, () => rosterTick($))
931 } else stopTicker()
932}
933
934async function rosterTick($) {
935 $.ui.invalidate('ui.render')
936 if (!isBandTicking(await $.clock.now())) stopTicker()
937}
938
939function isBandTicking(now) {
940 return roster !== null && (isRunning(roster.state.dispatches) || now < lingerEnd(terminalSeen))
941}
942
943function stopTicker() {
944 if (ticker === null) return
945 ticker.cancel()
946 ticker = null
947}
948
949function isFallback() {
950 return fallback !== null
951}
952
953function bandDispatches(now) {
954 return roster === null ? [] : shownDispatches(roster.state.dispatches, terminalSeen, now)
955}
956
957// A gap and a dim header set the band apart from the spinner above it, then
958// one row per dispatch, Cancel beside the live ones. Rows come first: a band
959// too short for all of it drops the gap, then the header.
960function drawRoster($, e, dispatches, now) {
961 const { Box, Text, Button } = $.ui.resolve(e)
962 const rows = rosterRows(dispatches, roster.receivedAt, now).map((row) => {
963 const parts = row.segments.map((s) => (s.color || s.dimColor ? h(Text, { color: s.color, dimColor: s.dimColor }, s.text) : s.text))
964 const text = h(Text, { wrap: 'truncate-end' }, ...parts)
965 if (!row.isLive) return h(Box, { flexDirection: 'row' }, text)
966 const d = dispatches.find((x) => x.id === row.id)
967 const cancel = h(Button, { key: 'cancel-' + d.id, label: 'Cancel', onPress: () => cancelDispatch($, d) })
968 return h(Box, { flexDirection: 'row', columnGap: 2 }, text, cancel)
969 })
970 const chrome = bandChrome(rows.length, e.props.maxRows)
971 const gap = chrome.hasGap ? [h(Text, null, ' ')] : []
972 const header = chrome.hasHeader ? [h(Text, { dimColor: true, wrap: 'truncate-end' }, bandHeader(e.props.bodyColumns))] : []
973 return h(Box, { flexDirection: 'column' }, ...gap, ...header, ...rows)
974}
975
976// One try, not the retrying report chain: the person is waiting on it.
977async function cancelDispatch($, d) {
978 const label = d.name || d.role || d.id
979 const why = isBridging() ? await tryReport($, bridgeArgv('report'), JSON.stringify(cancelRequest(d.id))) : 'leo bridge is off'
980 $.ui.toast(why === null ? 'leo: canceling ' + label : 'leo: cancel failed for ' + label + ': ' + why, { timeoutMs: TOAST_MS })
981}
982
983// The delegation guards fail open, on purpose: a broken guard lets the
984// call through, as leo being down does, instead of blocking every native
985// agent. next is replay-safe here: a guard that had called it gets that
986// answer back, nothing beneath running twice. A re-entry ran no guard and
987// may make no $ calls, so it passes without a log.
988function guardFailed($, e, next) {
989 if (next.error.kind !== 're-entry') $.ui.log(GUARD_FAILED + (next.error.message ?? next.error.kind))
990 return next(e)
991}
992
993function withDelegationSection(composed) {
994 const delegation = roster === null ? null : roster.state.delegation
995 if (delegation === null || !delegation.enabled || delegation.section === '') return composed
996 const section = { id: DELEGATION_SECTION_ID, text: delegation.section, scope: 'session' }
997 return { ...composed, sections: [...composed.sections, section] }
998}
999
1000// ---- fork consult --------------------------------------------------------
1001
1002// Asks the session's own model the consult's prompt over this conversation
1003// (same model, no tools, the transcript served from the prompt cache).
1004async function forkConsult($, e) {
1005 const prompt = typeof e.prompt === 'string' ? e.prompt : ''
1006 if (prompt.trim() === '') return { deny: 'fork consult needs a prompt' }
1007 try {
1008 const [fork, model] = await Promise.all([$.model.fork({ prompt }), $.session.model()])
1009 return forkConsultAnswer(fork, model)
1010 } catch (err) {
1011 return { deny: 'fork consult failed: ' + errorText(err) }
1012 }
1013}
1014
1015// ---- observe reports -----------------------------------------------------
1016
1017// What the main loop is doing: its most recently started tool call still
1018// running, or nothing.
1019function currentActivity() {
1020 const last = mainCalls[mainCalls.length - 1]
1021 return last === undefined ? {} : last.activity
1022}
1023
1024// Activity is latest-wins: the report waiting to be sent is replaced, at
1025// most one goes out per ACTIVITY_MIN_INTERVAL_MS, and a failed one is not
1026// retried (the next change says more than a stale retry would).
1027function reportActivity($) {
1028 if (!isBridging()) return
1029 pendingActivity = currentActivity()
1030 if (isActivityScheduled) return
1031 isActivityScheduled = true
1032 activityChain = activityChain.then(() => flushActivity($))
1033}
1034
1035async function flushActivity($) {
1036 try {
1037 const wait = activitySentAt + ACTIVITY_MIN_INTERVAL_MS - (await $.clock.now())
1038 if (wait > 0) await $.clock.sleep(wait)
1039 isActivityScheduled = false
1040 const body = JSON.stringify(eventReport('activity', pendingActivity))
1041 pendingActivity = null
1042 if (body === activitySentBody) return
1043 activitySentAt = await $.clock.now()
1044 activitySentBody = body
1045 const why = await tryReport($, bridgeArgv('report'), body)
1046 if (why === null) return
1047 activitySentBody = null
1048 reportFailed($, why)
1049 } catch (err) {
1050 isActivityScheduled = false
1051 reportFailed($, errorText(err))
1052 }
1053}
1054
1055// Observing fails open, as the delegation guards do: a broken observe
1056// hook passes the event on rather than blocking a tool call or compaction.
1057function observeFailed($, e, next) {
1058 if (next.error.kind !== 're-entry') $.ui.log('observe hook failed: ' + (next.error.message ?? next.error.kind))
1059 return next(e)
1060}
1061
1062// fields: { kind, tool?, summary? }.
1063function raiseAttention($, fields) {
1064 if (!isBridging()) return
1065 attention = fields.kind
1066 enqueueReport($, () => eventReport('attention', { state: 'needs_input', ...fields }))
1067}
1068
1069function clearAttention($) {
1070 if (attention === null || !isBridging()) return
1071 attention = null
1072 enqueueReport($, () => eventReport('attention', { state: 'cleared' }))
1073}
1074
1075// A main-loop tool settled one way or another: whatever prompt it raised
1076// is answered.
1077function onToolSettled($, e, next) {
1078 if (!e.agent_id) clearAttention($)
1079 return next(e)
1080}
1081
1082function trackSubagent($, id, isRunning) {
1083 if (!isBridging() || typeof id !== 'string' || id === '' || subagentIds.has(id) === isRunning) return
1084 subagentIds = isRunning ? new Set([...subagentIds, id]) : new Set([...subagentIds].filter((x) => x !== id))
1085 const running = subagentIds.size
1086 enqueueReport($, () => eventReport('subagents', { running }))
1087}
1088
1089function reportCompact($, phase, trigger, error) {
1090 enqueueReport($, () => eventReport('compact', defined({ phase, trigger, error: capField(error) })))
1091}
1092
1093// Runs a compaction, reporting it started and how it ended: a skip (a
1094// hook's veto) or a throw is a failure.
1095async function observeCompact($, trigger, run) {
1096 reportCompact($, 'started', trigger)
1097 try {
1098 const result = await run()
1099 if (result && typeof result.skip === 'string') reportCompact($, 'failed', trigger, result.skip)
1100 else reportCompact($, 'completed', trigger)
1101 return result
1102 } catch (err) {
1103 reportCompact($, 'failed', trigger, errorText(err))
1104 throw err
1105 }
1106}
1107
1108// ---- hooks ---------------------------------------------------------------
1109
1110async function onSessionStart($) {
1111 if (isStarted) return
1112 isStarted = true
1113 config = await readConfig($)
1114 if (config === null) {
1115 $.ui.log('LEO_BRIDGE_BIN, LEO_BRIDGE_AGENT or LEO_BRIDGE_LAUNCH is unset; bridge disabled')
1116 return
1117 }
1118 home = (await $.env.get('HOME')) ?? ''
1119 touchEntry($)
1120 $.clock.after(0, () => pruneAcked($))
1121 $.clock.after(0, () => pruneTold($))
1122 $.clock.after(0, () => pump($))
1123 $.clock.after(STATE_WAIT_MS, () => noStateYet($))
1124}
1125
1126export function register(on) {
1127 on('session.start', async ($, e, next) => {
1128 forgetAllSubmits()
1129 await onSessionStart($)
1130 return next(e)
1131 })
1132
1133 // Watches the prompts others submit (the plugin's own raise no hook here),
1134 // to tell whose turn each turn.start is.
1135 on('prompt.submit', async ($, e, next) => {
1136 const id = noteObservedSubmit(e)
1137 try {
1138 const result = await next(e)
1139 if (result && typeof result.drop === 'string') forgetSubmit(id)
1140 return result
1141 } catch (err) {
1142 forgetSubmit(id)
1143 throw err
1144 }
1145 }).catch(observeFailed)
1146
1147 on('turn.start', async ($, e, next) => {
1148 // Defensive: per the v2.1.289 typings only the main loop raises
1149 // turn.start, but a subagent's must never pass for the main loop's.
1150 if (e.agentId) return next(e)
1151 markRunning(e.turnId)
1152 stopPending = undefined
1153 const owner = takeTurnOwner()
1154 if (isBridging()) {
1155 enqueueReport($, () => helloIfSessionChanged($))
1156 const fields = {
1157 event_id: eventId('turn.start', e.turnId),
1158 turn_id: turnIdField(e.turnId),
1159 prompt: reportText(e.text),
1160 command_id: owner?.stamp?.commandId,
1161 origin: owner?.stamp?.origin ?? owner?.origin,
1162 }
1163 enqueueReport($, () => eventReport('turn.start', defined(fields)))
1164 }
1165 return next(e)
1166 })
1167
1168 on('turn.complete', async ($, e, next) => {
1169 if (e.agentId) return next(e)
1170 markIdle()
1171 releaseQueuedAfter(e.turnId)
1172 if (!isBridging()) return next(e)
1173 clearAttention($)
1174 // The report is queued now, so it keeps its place among this process's
1175 // reports, and is built once next(e) says what the turn cost.
1176 // Claude runs its Stop hooks before the turn completes, so by the time
1177 // next(e) settles the Stop's pending work has been seen.
1178 let settleUsage = () => {}
1179 const settled = new Promise((resolve) => {
1180 settleUsage = (usage) => {
1181 resolve({ usage, pending: stopPending })
1182 stopPending = undefined
1183 }
1184 })
1185 enqueueReport($, async () => {
1186 const { usage, pending } = await settled
1187 return turnCompleteReport($, e, usage, pending)
1188 })
1189 touchEntry($)
1190 // Reading what the session was told restamps its entry (at most hourly).
1191 withTold($, () => undefined)
1192 try {
1193 const result = await next(e)
1194 settleUsage(result?.usage ?? e.usage)
1195 return result
1196 } catch (err) {
1197 settleUsage(e.usage)
1198 throw err
1199 }
1200 })hooks/protocol.js 500 lines1// Pure helpers for the leo bridge protocol: framing, parsing, dedup bookkeeping,
2// report shapes, and respawn backoff. Nothing here touches the mods API.
3
4export const ACKED_CAP = 500
5// Each claude process keeps the ids it acked under ACKED_KEY_PREFIX + its
6// bridge key, restamped as it starts and after every turn. An entry left
7// untouched this long belongs to a process long gone (a dispatch killed
8// before its session ended); the next session.start of any bridged claude
9// prunes it, so the store does not grow without end.
10export const ACKED_KEY_PREFIX = 'acked:'
11export const ACKED_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000
12// Dispatch bridge keys start with this (consult.DispatchBridgeKey). A
13// dispatch's claude never resumes once its session ends, so its entry goes
14// with it.
15export const DISPATCH_KEY_PREFIX = 'dispatch.'
16// Caps the ids an acked entry records as handed to the engine; only the
17// few a reload can catch in flight matter.
18export const INFLIGHT_CAP = 50
19export const BACKOFF_INITIAL_MS = 1000
20// Kept short: a daemon restart cuts the stream, and the restarted daemon
21// waits on the mod to reconnect for anything it queued meanwhile. A failed
22// connect costs one short-lived `leo bridge` child.
23export const BACKOFF_MAX_MS = 5000
24export const BACKOFF_RESET_AFTER_MS = 60_000
25// `leo bridge` exits with this when the daemon refuses this launch for good:
26// another launch holds the key, or no daemon adopted the session. Retrying
27// cannot help, so the mod stops bridging until it reloads. Mirrored by
28// bridgemod.StaleLaunchExitCode on the Go side.
29export const STALE_LAUNCH_EXIT_CODE = 3
30// `leo bridge report` exits with this when the daemon refuses the report
31// itself for good (malformed, or an event an older daemon does not know):
32// the mod drops it. Mirrored by bridgemod.RejectedReportExitCode.
33export const REJECTED_REPORT_EXIT_CODE = 4
34// Waits before each retry of a report the daemon did not take, backing off
35// to about a minute in all: long enough to ride out a daemon restart. Acks
36// are idempotent (the mod dedups by id) and events carry ids, so a retry is
37// always safe; later reports wait behind it, so order holds.
38export const REPORT_RETRY_DELAYS_MS = [500, 1000, 2000, 4000, 8000, 15_000, 15_000, 15_000]
39// Caps a prompt or answer echoed in a report, keeping every report well
40// under the daemon's body limit.
41export const MAX_REPORT_TEXT_CHARS = 1_000_000
42
43/**
44 * A prompt or answer as a report carries it: strings capped at
45 * MAX_REPORT_TEXT_CHARS, anything else dropped.
46 * @param {unknown} value
47 * @returns {string | undefined}
48 */
49export function reportText(value) {
50 if (typeof value !== 'string') return undefined
51 return value.length > MAX_REPORT_TEXT_CHARS ? value.slice(0, MAX_REPORT_TEXT_CHARS) : value
52}
53
54const OPS = ['deliver', 'compact', 'clear', 'interrupt']
55
56/**
57 * Appends a chunk to the carried partial line and splits off complete lines.
58 * @param {string} carry text left over from earlier chunks (no newline)
59 * @param {string} text the new chunk
60 * @returns {{ lines: string[], carry: string }}
61 */
62export function splitLines(carry, text) {
63 const parts = (carry + text).split('\n')
64 const rest = parts[parts.length - 1] ?? ''
65 const lines = parts
66 .slice(0, -1)
67 .map((line) => (line.endsWith('\r') ? line.slice(0, -1) : line))
68 .filter((line) => line.trim() !== '')
69 return { lines, carry: rest }
70}
71
72/**
73 * Parses one JSONL command line.
74 * A line without a usable id cannot be acked, so it is rejected outright;
75 * a line with an id but a bad op or payload is returned for an ok:false ack.
76 * @param {string} line
77 * @returns {{ kind: 'command', command: object } | { kind: 'invalid', id: string, error: string } | { kind: 'garbage', error: string }}
78 */
79export function parseCommand(line) {
80 let value
81 try {
82 value = JSON.parse(line)
83 } catch (err) {
84 return { kind: 'garbage', error: 'invalid JSON: ' + errorText(err) }
85 }
86 if (value === null || typeof value !== 'object' || Array.isArray(value)) {
87 return { kind: 'garbage', error: 'command is not an object' }
88 }
89 const id = value.id
90 if (typeof id !== 'string' || id === '') {
91 return { kind: 'garbage', error: 'command has no id' }
92 }
93 if (!OPS.includes(value.op)) {
94 return { kind: 'invalid', id, error: 'unknown op: ' + String(value.op) }
95 }
96 if (value.op === 'deliver') {
97 if (typeof value.text !== 'string') {
98 return { kind: 'invalid', id, error: 'deliver: text must be a string' }
99 }
100 return { kind: 'command', command: { id, op: 'deliver', text: value.text, asUser: value.as_user === true } }
101 }
102 if (value.op === 'compact') {
103 if (value.instructions !== undefined && typeof value.instructions !== 'string') {
104 return { kind: 'invalid', id, error: 'compact: instructions must be a string' }
105 }
106 const instructions = value.instructions === '' ? undefined : value.instructions
107 return { kind: 'command', command: { id, op: 'compact', instructions } }
108 }
109 return { kind: 'command', command: { id, op: value.op } }
110}
111
112/**
113 * Returns a new acked-id list with id appended, keeping only the newest `cap`.
114 * @param {readonly string[]} list
115 * @param {string} id
116 * @param {number} [cap]
117 * @returns {string[]}
118 */
119export function appendAcked(list, id, cap = ACKED_CAP) {
120 const without = list.filter((x) => x !== id)
121 const next = [...without, id]
122 return next.length > cap ? next.slice(next.length - cap) : next
123}
124
125/**
126 * The acked ids in whatever the store held under an acked key: an
127 * ackedEntry, or the bare list earlier mods wrote.
128 * @param {unknown} value
129 * @returns {string[]}
130 */
131export function ackedFromStore(value) {
132 const ids = Array.isArray(value) ? value : isRecord(value) && Array.isArray(value.ids) ? value.ids : []
133 return ids.filter((x) => typeof x === 'string')
134}
135
136/**
137 * The store entry for a bridge key's acked ids, stamped with when it was
138 * written, and the ids its current launch handed the engine and has not
139 * settled yet (see withInflight).
140 * @param {readonly string[]} ids
141 * @param {number} at milliseconds since the epoch
142 * @param {{ launch: string, ids: readonly string[] } | null} [inflight]
143 * @returns {{ ids: string[], at: number, inflight?: { launch: string, ids: string[] } }}
144 */
145export function ackedEntry(ids, at, inflight = null) {
146 const entry = { ids: [...ids], at }
147 return inflight === null ? entry : { ...entry, inflight: { launch: inflight.launch, ids: [...inflight.ids] } }
148}
149
150/**
151 * The entry with id appended to its acked ids and stamped now; what it
152 * records in flight is kept.
153 * @param {unknown} value the entry as stored
154 * @param {string} id
155 * @param {number} now
156 */
157export function withAcked(value, id, now) {
158 return ackedEntry(appendAcked(ackedFromStore(value), id), now, inflightOf(value))
159}
160
161/**
162 * The entry restamped now, holding what it held: a live process touches its
163 * entry so another's prune never takes it for one long gone.
164 * @param {unknown} value the entry as stored
165 * @param {number} now
166 */
167export function touchedEntry(value, now) {
168 return ackedEntry(ackedFromStore(value), now, inflightOf(value))
169}
170
171/**
172 * The ids launch handed the engine and has not settled, per the entry;
173 * none for another launch's record, or without a launch id.
174 * @param {unknown} value the entry as stored
175 * @param {string | null} launch
176 * @returns {string[]}
177 */
178export function inflightIds(value, launch) {
179 const inflight = inflightOf(value)
180 return launch && inflight !== null && inflight.launch === launch ? inflight.ids : []
181}
182
183/**
184 * The entry with id recorded as handed to launch's engine (isHandedOff) or
185 * no longer; another launch's record is replaced, since a new process
186 * starts with an empty prompt queue.
187 * @param {unknown} value the entry as stored
188 * @param {string} launch
189 * @param {string} id
190 * @param {boolean} isHandedOff
191 * @param {number} now
192 */
193export function withInflight(value, launch, id, isHandedOff, now) {
194 const held = inflightIds(value, launch).filter((x) => x !== id)
195 const ids = isHandedOff ? appendAcked(held, id, INFLIGHT_CAP) : held
196 return ackedEntry(ackedFromStore(value), now, { launch, ids })
197}
198
199function inflightOf(value) {
200 if (!isRecord(value) || !isRecord(value.inflight)) return null
201 const { launch, ids } = value.inflight
202 if (typeof launch !== 'string' || !Array.isArray(ids)) return null
203 return { launch, ids: ids.filter((x) => typeof x === 'string') }
204}
205
206/**
207 * Whether an acked entry is past ACKED_MAX_AGE_MS at now. One without a
208 * time cannot be dated, so it counts as stale.
209 * @param {unknown} value
210 * @param {number} now
211 * @returns {boolean}
212 */
213export function isAckedEntryStale(value, now) {
214 if (!isRecord(value) || typeof value.at !== 'number') return true
215 return now - value.at > ACKED_MAX_AGE_MS
216}
217
218/**
219 * Whether a session.end leaves the process for good: not a /clear or a
220 * resume, after which the same process goes on under another session.
221 * @param {unknown} reason
222 * @returns {boolean}
223 */
224export function isFinalSessionEnd(reason) {
225 return reason !== 'clear' && reason !== 'resume'
226}
227
228function isRecord(value) {
229 return value !== null && typeof value === 'object' && !Array.isArray(value)
230}
231
232/**
233 * Decides the wait before the next spawn and the backoff after that.
234 * @param {number} current the backoff that would apply now
235 * @param {number} livedMs how long the stream that just ended lasted
236 * @returns {{ waitMs: number, next: number }}
237 */
238export function nextBackoff(current, livedMs) {
239 const waitMs = livedMs > BACKOFF_RESET_AFTER_MS ? BACKOFF_INITIAL_MS : current
240 return { waitMs, next: Math.min(waitMs * 2, BACKOFF_MAX_MS) }
241}
242
243/**
244 * @param {string} sessionId
245 * @param {string} claudeVersion
246 * @param {boolean | undefined} busy whether a main-loop turn is running right
247 * now; undefined (left out, so the daemon keeps what it knows) when the
248 * mod cannot tell
249 * @param {number} subagents the subagents this mod sees running, so a
250 * reconnect's hello keeps the daemon's count instead of resetting it
251 * @returns {{ type: 'hello', session_id: string, claude_version: string, busy?: boolean, subagents: number }}
252 */
253export function helloReport(sessionId, claudeVersion, busy, subagents) {
254 const hello = { type: 'hello', session_id: sessionId, claude_version: claudeVersion }
255 return { ...(busy === undefined ? hello : { ...hello, busy }), subagents }
256}
257
258/** @returns {{ type: 'ack', id: string, ok: boolean, error?: string }} */
259export function ackReport(id, ok, error) {
260 return ok || error === undefined ? { type: 'ack', id, ok } : { type: 'ack', id, ok, error }
261}
262
263/** @returns {{ type: 'event', name: string }} */
264export function eventReport(name, extra = {}) {
265 return { type: 'event', name, ...extra }
266}
267
268/**
269 * The id the daemon deduplicates an event by: the event name and the turn or
270 * session it belongs to, so a retried report carries the same one. Undefined
271 * when there is nothing stable to derive it from.
272 * @param {string} name
273 * @param {unknown} scope a turn id (turn events) or session id (session.end)
274 * @returns {string | undefined}
275 */
276export function eventId(name, scope) {
277 return typeof scope === 'string' && scope !== '' ? name + ':' + scope : undefined
278}
279
280/**
281 * Describes how the bridge child ended, for the reconnect log line.
282 * @param {{ code: number | null, signal: string | null } | undefined} result
283 * @param {string} stderr the tail of what the child wrote to stderr
284 * @returns {string}
285 */
286export function describeExit(result, stderr) {
287 const tail = stderr.trim()
288 if (result === undefined) return tail
289 const how = result.signal ? 'signal ' + result.signal : 'exit ' + String(result.code)
290 return tail ? how + ': ' + tail : how
291}
292
293/** @returns {string} */
294export function errorText(err) {
295 if (err instanceof Error) return err.message
296 return String(err)
297}
298
299// The MCP tool a fork consult rides on: leo's MCP server registers as
300// `leo`, so claude lists leo_consult under this name.
301export const CONSULT_TOOL = 'mcp__leo__leo_consult'
302
303/**
304 * A count the engine reported, or zero for one it left out or garbled.
305 * @param {unknown} value
306 * @returns {number}
307 */
308function tokenCount(value) {
309 return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : 0
310}
311
312/**
313 * The token counts a turn.complete report carries, from the engine's
314 * TurnUsage. The engine leaves usage out when nothing counted (an
315 * interrupt, an API error), so a turn without it counts as zero.
316 * @param {unknown} usage
317 * @returns {{ input: number, output: number, cache_read: number, cache_creation: number, model?: string }}
318 */
319export function turnTokens(usage) {
320 const u = isRecord(usage) ? usage : {}
321 const tokens = {
322 input: tokenCount(u.input_tokens),
323 output: tokenCount(u.output_tokens),
324 cache_read: tokenCount(u.cache_read_input_tokens),
325 cache_creation: tokenCount(u.cache_creation_input_tokens),
326 }
327 return typeof u.model === 'string' && u.model !== '' ? { ...tokens, model: u.model } : tokens
328}
329
330// Background-task statuses that no longer wake the session; Claude lists
331// only in-flight work, so these are defensive.
332const SETTLED_TASK_STATUSES = ['completed', 'failed', 'killed', 'stopped', 'canceled', 'cancelled']
333// Bounds the daemon holds a pending report to.
334const MAX_PENDING_TYPES = 16
335const MAX_PENDING_TYPE_LEN = 32
336const MAX_PENDING_COUNT = 1000
337
338/**
339 * The pending work a turn.complete report carries, from Claude's Stop hook
340 * input: in-flight background tasks counted by type, and the session crons
341 * that will wake the session. Undefined when nothing is pending, as when an
342 * older claude sends neither list.
343 * @param {unknown} stop the classic.Stop input
344 * @returns {{ tasks: Record<string, number>, wakeups: number } | undefined}
345 */
346export function stopHookPending(stop) {
347 const s = isRecord(stop) ? stop : {}
348 // A Map, not an object: a type may name an Object.prototype key.
349 const counts = (Array.isArray(s.background_tasks) ? s.background_tasks : [])
350 .filter((t) => isRecord(t) && !SETTLED_TASK_STATUSES.includes(String(t.status).toLowerCase()))
351 .map((t) => pendingTaskType(t.type))
352 .reduce((acc, type) => {
353 if (!acc.has(type) && acc.size >= MAX_PENDING_TYPES) return acc
354 return new Map([...acc, [type, Math.min(MAX_PENDING_COUNT, (acc.get(type) ?? 0) + 1)]])
355 }, new Map())
356 const wakeups = Math.min(MAX_PENDING_COUNT, (Array.isArray(s.session_crons) ? s.session_crons : []).filter(isRecord).length)
357 if (counts.size === 0 && wakeups === 0) return undefined
358 return { tasks: Object.fromEntries(counts), wakeups }
359}
360
361// A task type as the daemon accepts it: [a-z0-9_-], at most 32 long.
362function pendingTaskType(type) {
363 const clean = (typeof type === 'string' ? type : '').trim().toLowerCase().replace(/ /g, '_').replace(/[^a-z0-9_-]/g, '').slice(0, MAX_PENDING_TYPE_LEN)
364 return clean === '' ? 'other' : clean
365}
366
367/**
368 * How the mod answers a fork consult's tool call, from $.model.fork's
369 * result: the reply under a header naming the model and its tokens
370 * (in/out/cache-read), or a deny (an error result for the model) naming why
371 * there is none.
372 * @param {unknown} fork
373 * @param {string} model the main loop's model, which the fork ran on
374 * @returns {{ result: string } | { deny: string }}
375 */
376export function forkConsultAnswer(fork, model) {
377 if (isRecord(fork) && fork.isAnswered === true && typeof fork.text === 'string') {
378 const t = turnTokens(fork.usage)
379 return { result: `[consult · fork/${model}] tokens ${t.input}/${t.output}/${t.cache_read}\n${fork.text}` }
380 }
381 const reason = isRecord(fork) && typeof fork.reason === 'string' ? fork.reason : 'no reply'
382 const detail = isRecord(fork) && reason === 'api-error' ? ` (${fork.status ?? 'no status'} ${fork.error ?? 'unknown'})` : ''
383 return { deny: `fork consult failed: ${reason}${detail}` }
384}
385
386// ---- observe reports -----------------------------------------------------
387
388// The least time between two activity reports: activity is latest-wins, so
389// what changes faster than this is replaced before it is sent.
390export const ACTIVITY_MIN_INTERVAL_MS = 1000
391// Caps every field of an observe report; the daemon clamps again.
392export const MAX_SUMMARY_CHARS = 200
393
394const PATH_TOOLS = ['Read', 'Edit', 'Write']
395const PATTERN_TOOLS = ['Grep', 'Glob']
396/**
397 * The hostname of url, parsed as a URL so userinfo (which may itself hold
398 * '@' or ':') never leaks; undefined when url has no host or does not parse.
399 * @param {string} url
400 * @returns {string | undefined}
401 */
402function urlHost(url) {
403 try {
404 return new URL(url).hostname || undefined
405 } catch {
406 return undefined
407 }
408}
409
410/**
411 * @param {unknown} value
412 * @returns {string | undefined} the string capped at MAX_SUMMARY_CHARS, or
413 * undefined for an empty or non-string value
414 */
415export function capField(value) {
416 if (typeof value !== 'string' || value.trim() === '') return undefined
417 return value.length > MAX_SUMMARY_CHARS ? value.slice(0, MAX_SUMMARY_CHARS) : value
418}
419
420/**
421 * path with the home directory spelled `~`.
422 * @param {string} path
423 * @param {string} home
424 */
425export function abbreviateHome(path, home) {
426 const base = home.endsWith('/') ? home.slice(0, -1) : home
427 if (base === '') return path
428 if (path === base) return '~'
429 return path.startsWith(base + '/') ? '~' + path.slice(base.length) : path
430}
431
432/**
433 * The one-field summary of a tool call's input: never the input itself.
434 * @param {string} tool
435 * @param {unknown} input the tool's arguments
436 * @param {string} home
437 * @returns {string | undefined}
438 */
439function toolSummary(tool, input, home) {
440 if (!isRecord(input)) return undefined
441 if (tool === 'Bash') return typeof input.command === 'string' ? input.command.trim().split(/\s+/)[0] : undefined
442 if (PATH_TOOLS.includes(tool)) return typeof input.file_path === 'string' ? abbreviateHome(input.file_path, home) : undefined
443 if (PATTERN_TOOLS.includes(tool)) return typeof input.pattern === 'string' ? input.pattern : undefined
444 if (tool === 'WebFetch') return typeof input.url === 'string' ? urlHost(input.url) : undefined
445 return undefined
446}
447
448// What the daemon keeps of an attention summary, in code points
449// (observe.MaxActionDetail).
450// A question summary is shortened to this up front so its "(+N more)" suffix
451// is not the part the daemon's clamp cuts.
452export const MAX_ATTENTION_DETAIL_CHARS = 120
453
454/**
455 * The attention summary of an AskUserQuestion: the first question's text,
456 * whitespace-folded, plus " (+N more)" when it asks several.
457 * @param {unknown} questions the tool's `questions` argument
458 * @returns {string | undefined} undefined when no question has text
459 */
460export function questionSummary(questions) {
461 if (!Array.isArray(questions)) return undefined
462 const texts = questions
463 .map((q) => (isRecord(q) && typeof q.question === 'string' ? q.question.trim().split(/\s+/).join(' ') : ''))
464 .filter((text) => text !== '')
465 if (texts.length === 0) return undefined
466 const suffix = texts.length > 1 ? ` (+${texts.length - 1} more)` : ''
467 // Counted in code points, as the daemon counts runes, so a clip never
468 // lands inside a surrogate pair.
469 const room = MAX_ATTENTION_DETAIL_CHARS - Array.from(suffix).length
470 const chars = Array.from(texts[0])
471 const first = chars.length > room ? chars.slice(0, room - 1).join('') + '…' : texts[0]
472 return first + suffix
473}
474
475/**
476 * What an activity or attention report says of a tool call: its name and a
477 * summary of its input, each capped; keys without a value are left out.
478 * @param {string} tool
479 * @param {unknown} input
480 * @param {string} home
481 * @returns {{ tool?: string, summary?: string }}
482 */
483export function toolActivity(tool, input, home) {
484 const fields = { tool: capField(tool), summary: capField(toolSummary(tool, input, home)) }
485 return Object.fromEntries(Object.entries(fields).filter(([, v]) => v !== undefined))
486}
487
488/**
489 * A session.compact trigger as a compact report names it: a plugin's
490 * compaction (leo's own command) counts as manual; a precompute installs
491 * nothing, so it is not reported (null).
492 * @param {unknown} trigger
493 * @returns {'manual' | 'auto' | null}
494 */
495export function compactTrigger(trigger) {
496 if (trigger === 'auto') return 'auto'
497 if (trigger === 'manual' || trigger === 'plugin') return 'manual'
498 return null
499}
500hooks/roster.js 359 lines1// Pure helpers for what the daemon's state snapshots drive: delegation
2// enforcement and the roster band. Nothing here touches the mods API.
3
4// The system-prompt section the mod adds while delegation is on.
5export const DELEGATION_SECTION_ID = 'leo-bridge:leo-delegation'
6
7// What a native agent call is told while delegation is on.
8export const AGENT_DENY_TEXT =
9 'Delegation is on: use leo_dispatch(role: …) — see the leo-delegation section. Native agents are allowed only after a leo_dispatch fails.'
10
11// The leo MCP tool whose failure lets native agents through.
12export const LEO_DISPATCH_TOOL = 'mcp__leo__leo_dispatch'
13
14// How long after session.start the mod waits for a first state before it
15// stops enforcing delegation (the daemon may be down or too old).
16export const STATE_WAIT_MS = 10_000
17
18// Why native agents are let through while delegation is on.
19export const FALLBACK_NO_STATE = 'no state from leo'
20export const FALLBACK_STREAM_DOWN = 'leo bridge down'
21export const FALLBACK_DISPATCH_FAILED = 'leo_dispatch failed'
22
23const RUNNING = 'running'
24const IDLE = 'idle'
25// A dispatch whose turn stopped on background work that will wake it.
26const WAITING = 'waiting'
27const TERMINAL = ['done', 'failed', 'timeout', 'canceled', 'closed', 'released']
28
29/**
30 * Parses a state line's value: delegation policy and dispatches, every field
31 * coerced to its type. Null when it is not a state line at all.
32 * @param {unknown} value
33 */
34export function parseState(value) {
35 if (!isRecord(value) || value.op !== 'state') return null
36 const deleg = isRecord(value.delegation) ? value.delegation : {}
37 const dispatches = Array.isArray(value.dispatches) ? value.dispatches : []
38 return {
39 delegation: {
40 enabled: deleg.enabled === true,
41 section: str(deleg.section),
42 hideAgents: Array.isArray(deleg.hide_agents) ? deleg.hide_agents.filter((x) => typeof x === 'string') : [],
43 },
44 dispatches: dispatches.filter((d) => isRecord(d) && typeof d.id === 'string' && d.id !== '').map(parseDispatch),
45 }
46}
47
48/**
49 * parseState over one stream line; null for anything but a state line
50 * (commands, garbage), which the command path then handles.
51 * @param {string} line
52 */
53export function parseStateLine(line) {
54 try {
55 return parseState(JSON.parse(line))
56 } catch {
57 return null
58 }
59}
60
61// The $.store key prefix for what the model was last told about delegation.
62export const TOLD_KEY_PREFIX = 'told:'
63
64/**
65 * The stored form of what session was told: the policy as the model read
66 * it, and when it was written (at, ms) so stale entries can be pruned.
67 */
68export function toldEntry(session, delegation, at) {
69 return { session, enabled: delegation.enabled, section: delegation.section, at }
70}
71
72/**
73 * What a stored entry says session was told, or null when it holds nothing
74 * for that session (another session's, a junk value, none).
75 * @param {unknown} value
76 * @param {string} session
77 */
78export function toldFromEntry(value, session) {
79 if (!isRecord(value) || value.session !== session || typeof value.section !== 'string') return null
80 return { enabled: value.enabled === true, section: value.section }
81}
82
83// What the model was told before any state arrived: no delegation section.
84export const NO_DELEGATION = Object.freeze({ enabled: false, section: '', hideAgents: [] })
85
86function parseDispatch(d) {
87 return {
88 id: d.id,
89 name: str(d.name),
90 role: str(d.role),
91 template: str(d.template),
92 model: str(d.model),
93 effort: str(d.effort),
94 observedEffort: str(d.observed_effort),
95 status: str(d.status),
96 stalled: d.stalled === true,
97 pending: str(d.pending),
98 activeSeconds: num(d.active_seconds) ?? 0,
99 tokensIn: num(d.tokens_in),
100 tokensOut: num(d.tokens_out),
101 costUsd: num(d.cost_usd),
102 }
103}
104
105/**
106 * Whether two delegation policies read the same to the model.
107 * @param {{ enabled: boolean, section: string } | null | undefined} a
108 * @param {{ enabled: boolean, section: string } | null | undefined} b
109 */
110export function sameDelegationText(a, b) {
111 const on = (x) => x?.enabled === true && x.section !== ''
112 if (on(a) !== on(b)) return false
113 return !on(a) || a.section === b.section
114}
115
116/**
117 * The note that tells the model delegation changed after its system prompt
118 * was composed: the engine keeps the prompt it composed first, so a changed
119 * section reaches the model only this way.
120 * @param {{ enabled: boolean, section: string }} delegation
121 */
122export function delegationNote(delegation) {
123 if (delegation.enabled && delegation.section !== '') {
124 return 'leo: delegation settings changed; this replaces the leo-delegation section of your system prompt.\n\n' + delegation.section
125 }
126 return 'leo: delegation is now OFF. Ignore the leo-delegation section of your system prompt; native agents are allowed.'
127}
128
129/**
130 * The agent type without a plugin prefix (`plugin:name` → `name`).
131 * @param {string} name
132 */
133export function bareAgentName(name) {
134 const i = name.lastIndexOf(':')
135 return i < 0 ? name : name.slice(i + 1)
136}
137
138/**
139 * Whether delegation keeps the model from agent type `name`: delegation on,
140 * not fallen back, and the type listed. An Agent call also may not use the
141 * default type (`subagent_type` left out) or general-purpose.
142 * @param {{ delegation: { enabled: boolean, hideAgents: string[] } } | null | undefined} state
143 * @param {boolean} isFallback
144 * @param {string} name
145 * @param {boolean} isAgentCall
146 */
147export function isAgentHidden(state, isFallback, name, isAgentCall) {
148 if (!state?.delegation.enabled || isFallback) return false
149 const bare = bareAgentName(name)
150 if (isAgentCall && (bare === '' || bare === 'general-purpose')) return true
151 return state.delegation.hideAgents.includes(bare)
152}
153
154/** Whether a tool.call result is a failure: an error result or a refusal. */
155export function isFailedCall(result) {
156 return isRecord(result) && (result.isError === true || typeof result.deny === 'string')
157}
158
159export function isTerminal(status) {
160 return TERMINAL.includes(status)
161}
162
163export function isRunning(dispatches) {
164 return dispatches.some((d) => d.status === RUNNING)
165}
166
167/**
168 * The status line, for problems only: why delegation fell back, if it did
169 * and delegation could be on. Undefined clears the line.
170 * @param {{ delegation: { enabled: boolean } } | null | undefined} state
171 * @param {string | null} fallback
172 */
173export function statusLine(state, fallback) {
174 if (fallback === null || state?.delegation.enabled === false) return undefined
175 return 'native agents allowed: ' + fallback
176}
177
178/**
179 * Working time shown for a dispatch: the snapshot's, plus the time since it
180 * arrived while the dispatch runs.
181 */
182export function elapsedSeconds(d, receivedAt, now) {
183 const extra = d.status === RUNNING ? Math.max(0, now - receivedAt) / 1000 : 0
184 return d.activeSeconds + extra
185}
186
187/** `m:ss`, or `h:mm:ss` from an hour. */
188export function formatElapsed(seconds) {
189 const total = Math.max(0, Math.floor(seconds))
190 const s = String(total % 60).padStart(2, '0')
191 if (total < 3600) return Math.floor(total / 60) + ':' + s
192 return Math.floor(total / 3600) + ':' + String(Math.floor((total % 3600) / 60)).padStart(2, '0') + ':' + s
193}
194
195function formatTokens(n) {
196 if (n === undefined) return '–'
197 if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + 'M'
198 if (n >= 1000) return (n / 1000).toFixed(1) + 'k'
199 return String(n)
200}
201
202// How long a dispatch stays in the band after the mod first sees it end.
203export const TERMINAL_LINGER_MS = 10_000
204
205// The band's header rule width when the band's own is unknown.
206const DEFAULT_BAND_WIDTH = 40
207const BAND_TITLE = 'leo dispatches '
208const LABEL_MAX = 16
209// A model id is cut to this, so its effort label (a downgrade flag) stays in
210// view however long the id.
211const MODEL_MAX = 16
212// The band's left padding, in line with the engine's lines under the prompt;
213// rows sit a further two in, beneath the header.
214const BAND_PAD = ' '
215const INDENT = BAND_PAD + ' '
216const COLUMN_GAP = ' '
217const STALLED = 'stalled'
218const PENDING_MAX = 40
219
220/** The band's header: padding, its title, then a rule to the band's width. */
221export function bandHeader(width) {
222 const total = typeof width === 'number' && width > 0 ? width : DEFAULT_BAND_WIDTH
223 return BAND_PAD + BAND_TITLE + '─'.repeat(Math.max(0, total - BAND_PAD.length - BAND_TITLE.length))
224}
225
226/**
227 * Which of the gap and header fit beside rowCount rows in maxRows (unknown:
228 * unlimited). The rows come first: the gap goes, then the header.
229 * @param {number} rowCount
230 * @param {number | undefined} maxRows
231 */
232export function bandChrome(rowCount, maxRows) {
233 const limit = typeof maxRows === 'number' && Number.isFinite(maxRows) ? maxRows : Infinity
234 return { hasGap: rowCount + 2 <= limit, hasHeader: rowCount + 1 <= limit }
235}
236
237/**
238 * When each terminal dispatch was first seen terminal (ms), as a new map:
239 * seen is the last one, or null for the first snapshot after a (re)load,
240 * whose terminal dispatches count as long gone. Dispatches no longer
241 * terminal, or no longer in the snapshot, drop out.
242 * @param {Map<string, number> | null} seen
243 * @param {Array<{ id: string, status: string }>} dispatches
244 * @param {number} now
245 */
246export function trackTerminal(seen, dispatches, now) {
247 return new Map(
248 dispatches
249 .filter((d) => isTerminal(d.status))
250 .map((d) => [d.id, seen === null ? -Infinity : (seen.get(d.id) ?? now)]),
251 )
252}
253
254/** The dispatches the band shows: live ones, and terminal ones still lingering. */
255export function shownDispatches(dispatches, seen, now) {
256 return dispatches.filter((d) => !isTerminal(d.status) || now - (seen.get(d.id) ?? -Infinity) < TERMINAL_LINGER_MS)
257}
258
259/** When the last lingering terminal dispatch leaves the band (ms), or -Infinity. */
260export function lingerEnd(seen) {
261 return Math.max(-Infinity, ...[...seen.values()].map((at) => at + TERMINAL_LINGER_MS))
262}
263
264function glyph(status) {
265 if (status === IDLE || status === 'settling') return '⏸'
266 if (status === WAITING) return '⧗'
267 if (status === 'done' || status === 'closed') return '✓'
268 if (['failed', 'canceled', 'timeout'].includes(status)) return '✗'
269 if (status === 'queued') return '…'
270 return '⟳'
271}
272
273// The glyph's Text props: the status color.
274function glyphStyle(d) {
275 if (d.stalled) return { color: 'warning' }
276 if ([IDLE, WAITING, 'settling', 'queued'].includes(d.status)) return { dimColor: true }
277 if (d.status === 'done' || d.status === 'closed') return { color: 'success' }
278 if (['failed', 'canceled', 'timeout'].includes(d.status)) return { color: 'error' }
279 if (d.status === RUNNING) return { color: 'cyan' }
280 return {}
281}
282
283// The effort a row shows: the requested one; the observed one marked ~ when
284// none was requested (the model's default); both when they differ.
285function effortLabel(d) {
286 const observed = d.observedEffort ?? ''
287 if (d.effort !== '' && observed !== '' && observed !== d.effort) return d.effort + '→' + observed
288 if (d.effort !== '') return d.effort
289 return observed === '' ? '' : '~' + observed
290}
291
292// One row's column values, unpadded, in band order.
293function rowCells(d, receivedAt, now) {
294 return [
295 { text: Array.from(d.name || d.role || d.template || d.id).slice(0, LABEL_MAX).join(''), align: 'left', gap: COLUMN_GAP },
296 { text: [Array.from(d.model).slice(0, MODEL_MAX).join(''), effortLabel(d)].filter((x) => x !== '').join(' · '), align: 'left', gap: COLUMN_GAP, dimColor: true },
297 { text: formatElapsed(elapsedSeconds(d, receivedAt, now)), align: 'right', gap: COLUMN_GAP },
298 { text: d.stalled ? STALLED : d.status === WAITING ? Array.from(d.pending).slice(0, PENDING_MAX).join('') : '', align: 'left', gap: ' ' },
299 { text: formatTokens(d.tokensIn) + '/' + formatTokens(d.tokensOut), align: 'right', gap: COLUMN_GAP, dimColor: true },
300 { text: d.costUsd === undefined ? '' : '$' + d.costUsd.toFixed(2), align: 'right', gap: COLUMN_GAP, dimColor: true },
301 ]
302}
303
304/**
305 * The band's rows, oldest first: each the dispatch id, whether it is live
306 * (Cancel applies), and its styled segments, every column padded to the
307 * widest value in the band. A column empty in every row is left out.
308 * @param {Array<ReturnType<typeof parseDispatch>>} dispatches
309 * @param {number} receivedAt when the snapshot arrived (ms)
310 * @param {number} now (ms)
311 */
312export function rosterRows(dispatches, receivedAt, now) {
313 const cells = dispatches.map((d) => rowCells(d, receivedAt, now))
314 const widths = (cells[0] ?? []).map((_, i) => Math.max(...cells.map((row) => codePoints(row[i].text))))
315 return dispatches.map((d, r) => ({
316 id: d.id,
317 isLive: !isTerminal(d.status),
318 segments: [
319 { text: INDENT },
320 { text: glyph(d.status), ...glyphStyle(d) },
321 ...cells[r].flatMap((cell, i) => (widths[i] === 0 ? [] : [{ text: i === 0 ? ' ' : cell.gap }, padded(cell, widths[i])])),
322 ],
323 }))
324}
325
326function padded(cell, width) {
327 const pad = ' '.repeat(width - codePoints(cell.text))
328 const text = cell.align === 'right' ? pad + cell.text : cell.text + pad
329 return cell.dimColor ? { text, dimColor: true } : { text }
330}
331
332// Columns are cut and padded in code points, so a surrogate pair is never
333// split. Wide (CJK, emoji) characters still count as one column each.
334function codePoints(text) {
335 return Array.from(text).length
336}
337
338/** A row's text as drawn. */
339export function rowText(row) {
340 return row.segments.map((s) => s.text).join('')
341}
342
343/** The request report that cancels dispatch id. */
344export function cancelRequest(id) {
345 return { type: 'request', op: 'dispatch.cancel', dispatch_id: id }
346}
347
348function str(v) {
349 return typeof v === 'string' ? v : ''
350}
351
352function num(v) {
353 return typeof v === 'number' && Number.isFinite(v) ? v : undefined
354}
355
356function isRecord(value) {
357 return value !== null && typeof value === 'object' && !Array.isArray(value)
358}
359