Repository navigation
Description
Why
Since #5059 and #5051, a Leaf hook in a Claude Code session holding a page is still a leaf process of about 0.3 s, and Claude Code waits for each one: the prompt hook before a turn starts, the Stop hook at its end, PostToolBatch before each model call, and both steps of a forked reply. In the claude-code journey with forks on 2026-11-03 (macOS, load about 20):
| Step | Time |
|---|---|
| UserPromptSubmit hook, median | 0.41 s |
| Stop hook, median | 0.36 s |
Writing a fork's prompt (leaf hook --fork) |
0.26–0.37 s |
| Posting a fork's reply | 0.27–0.43 s |
| Leaf's part of a forked reply | 0.57–0.68 s |
Where one such process's time goes, cold, measured with hyperfine and in-process timers on a copy of a 54-event page:
| Part | Time |
|---|---|
The shell and the claim gate's interpreter (state.py hook-gate) |
~35 ms |
uv run's project check |
~25 ms |
| A second interpreter and Leaf's imports, among them jsonschema, turbohtml and the page model | ~100–130 ms |
| Validating the layer's registry | ~40 ms |
| Reading the page | ~40 ms |
| The hook's own work | a few ms |
A cache shared across processes could serve only the last two rows, about 80 ms. Every process pays the rest again, since it comes from starting Python and importing.
Proposal
Every Claude Code session holding a page already runs one long-lived Leaf process: the pages follower (leaf hook --pages). hooks/claude-code.ts starts it at session start, after each leaf command and at each turn's start and end, and it exits once the session holds no page. It has Leaf's modules loaded and the layer's vocabulary cached (layer.selected_vocabulary), and since #5114 it watches the session's pages natively. It could answer the hooks too.
- The follower listens on a Unix socket in the session's state directory, beside the session's other files and readable only by the user. It answers HTTP:
POST /hookcarries the hook's flags and payload, and the answer carries what the hook would have printed and its exit status. - The claim gate becomes the client.
bin/leafalready runsstate.py hook-gatebefore every--if-claimedhook, on the environment's interpreter with-S. Where the session's socket answers, the gate sends the payload there and prints the answer. Where it doesn't, because no follower is up yet, the socket is stale or the follower refuses, the gate exits as today andbin/leafruns the hook whole. No command or hook registration is added, andhooks.jsonstays as it is. A hook then costs the gate's ~30 ms and a round trip, in place of ~0.3 s. - Fork steps call the socket directly.
forkStepuses$.http.fetch("http://leaf/fork", { socketPath, method: "POST", body })in place of$.process.run, and falls back to the process when that fails. In a probe, a warm process wrote a fork's prompt over a Unix socket in a median 2.3 ms (p90 3.4 ms), against 0.26–0.37 s for the process. Fork steps then need no separate route, such as the follower writing each held thread's prompt into its listing.
Expected: the prompt, Stop and PostToolBatch hooks from medians of 0.17–0.41 s to about 40 ms each, and Leaf's part of a forked reply from about 0.6 s to about 10 ms.
Keeping updates live
Today each hook runs the delivering launcher's Python as it stands (session_runtime.py), so an update reaches a session at its next hook without a reload. A warm process keeps the modules it loaded, so the follower answers only while it is still the session's delivering Leaf:
- On each request it checks that
delivering_launcher(session)is still its own launcher. Where it isn't, it refuses, so the gate falls back, and exits, and the module starts a follower on the new Leaf. - In a checkout, where an edit changes the code in place, the follower watches its own
src/leaftree through the process's one watcher and exits on a change.
Concurrency and process state
The hook path assumes one hook per process. hooks.main and hook_transport print the answer to stdout, and the harness layer reads os.environ when a hook runs (CLAUDE_PID, CLAUDE_JOB_DIR, CLAUDE_CODE_HOST_SESSION_ID, CLAUDE_CONFIG_DIR and others). Claude Code can run several hooks at once, and forks run beside them, so requests can overlap.
- A first version can serve one request at a time behind a lock, with each hook returning its answer instead of printing it. Warm requests take milliseconds, so the lock costs little.
- Each variable the hook path reads needs one of two answers: the gate forwards it and the hook takes it as an argument, or it is shown constant for the session.
- Each cache held for a process's lifetime needs the same check: it is keyed by everything it depends on, as
selected_vocabularyis by its inputs' stamps andvalidate_registry's memo by content, or it lasts one request.
What stays a process
- The watch between turns (
hooks/scripts/background-watch.py,asyncRewake): it waits for input instead of answering. - SessionEnd's cleanup, the standalone stdlib program.
- Codex's and Pi's hooks keep the full path. The gate as client works for any harness that keeps a warm Leaf process per session, so Codex's App Server adapter could serve the same socket later.
Alternatives considered
- Handling the hooks in the hooks module instead of commands. The module's events can't do what two of the hooks do.
turn.completeonly observes a turn's end, where Leaf's Stop hook answers{"decision": "block"}to keep the turn going.prompt.submitrewrites the prompt the user sees, where UserPromptSubmit addsadditionalContextbeside it. So the command hooks stay as the entry points, and only what runs behind them changes. - A cache across processes for the validated registry and the page's reading: at most about 80 ms of the ~300.
- Trimming the process path as Claude Code hooks add hundreds of ms per tool batch in a session holding a page #4549's description lists: one interpreter where
uv's check isn't needed, and jsonschema and turbohtml kept off the import path of a hook that finds nothing. That might take a hook from ~300 ms to ~150 ms, through several separate changes.
First steps
- Audit what the
--if-claimedhook path reads from the environment and keeps for a process's lifetime. - Serve fork steps first (
POST /fork), the smallest change, and measure it withleaf-dev journey claude-code --forks. - Then make the gate a client for PostToolBatch, UserPromptSubmit and Stop, and measure with
leaf-dev journey claude-codeandleaf-dev response-times.
Part of #4549
This was written by Claude Code on behalf of max-sixty
Activity
max-sixty commentedon Nov 4, 2026
Parking this as a possible future change. For now we're keeping the hook path simple. In a session holding a page, a gated hook still runs
uv run ... python -m leaf, about 150–170 ms per hook on the maintainer's Mac, measured on 2026-11-03.On that day two cheaper ways to run the same Python hook were measured, and neither was worth its extra code. Skipping
uv runbehind Leaf's own record of the last sync saved about 16 ms on an idle machine and 25 ms under load. Continuing the hook inside the gate's interpreter saved another 16 ms or so. Both were dropped in favour ofuv run.A long-lived process, as proposed above, is still the one change that would take a claimed hook down to a few milliseconds. claude-mem does this with a resident worker and reports 8–12 ms per hook.
That payoff depends on the hooks staying in Python, since most of each hook's time goes to starting an interpreter and importing Leaf. If we build the hook path in something that starts quickly, this proposal matters much less.
Trigger: hook latency becomes a cost users notice again while the hooks are still Python.
Hold each short-lived Leaf process to the dependencies it may load, a…
max-sixty commentedon Nov 5, 2026
Closing as postponed: nothing needs this before release. Max parked this on 3 November with an explicit trigger. Since the latency fixes landed, a claimed hook takes 150-170 ms.
Reopen when: Hook latency becomes a cost users notice again while the hooks are still Python.