Skip to content

Answer Claude Code's hooks and fork steps from the session's pages follower #5246

Closed as not planned

Description

@max-sixty

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 /hook carries 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/leaf already runs state.py hook-gate before every --if-claimed hook, 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 and bin/leaf runs the hook whole. No command or hook registration is added, and hooks.json stays 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. forkStep uses $.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/leaf tree 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_vocabulary is by its inputs' stamps and validate_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.complete only observes a turn's end, where Leaf's Stop hook answers {"decision": "block"} to keep the turn going. prompt.submit rewrites the prompt the user sees, where UserPromptSubmit adds additionalContext beside 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

  1. Audit what the --if-claimed hook path reads from the environment and keeps for a process's lifetime.
  2. Serve fork steps first (POST /fork), the smallest change, and measure it with leaf-dev journey claude-code --forks.
  3. Then make the gate a client for PostToolBatch, UserPromptSubmit and Stop, and measure with leaf-dev journey claude-code and leaf-dev response-times.

Part of #4549

This was written by Claude Code on behalf of max-sixty

Activity

  1. added
    backlog
    on Nov 3, 2026
  2. max-sixty commented on Nov 4, 2026

    @max-sixty
    OwnerAuthor

    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 run behind 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 of uv 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.

    This was written by Claude Code on behalf of max-sixty

  3. added
    etc
    on Nov 4, 2026
  4. max-sixty commented on Nov 5, 2026

    @max-sixty
    OwnerAuthor

    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.

    This was written by Claude Code on behalf of max-sixty

  5. added
    postponed
    and removed
    etc
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogpostponed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions