Repository navigation
Run Claude Code's start hook only after a compaction - #5052
Merged
Merged
Conversation
Claude Code holds a resumed session's first reply, and a `/resume` switch inside a session, until its synchronous SessionStart hooks end. Leaf's start hook only reads reconnect notices, which the prompt hook repeats for every retained claim, except after a compaction, when it also tells the agent to read the leaf skill again. The registration now splits by source: `compact` stays synchronous, and `startup|resume|clear|fork` runs with `"async": true`. Claude Code delivers an async hook's JSON `additionalContext` at the next conversation turn and skips it when it repeats what the hook last delivered (hooks reference, Claude Code 2.1.316). A late or dropped notice is repeated at the first prompt. Compaction stays synchronous because an async hook's context joins only the next turn, so after an automatic compaction mid-turn the skill reminder would arrive after the replies it is meant to precede. Only `resume` reaches the full hook in practice: `startup`, `clear` and `fork` start a session id that holds no claim, which `bin/leaf`'s gate answers before uv. The resumed hook took 83 ms median at load 3 (n=15) through `bin/leaf` against a claimed page in a throwaway state home; the issue measured 468 ms median and 4.8 s at p90 on its Linux host. `test_claude_code_waits_for_the_start_hook_only_after_a_compaction` holds the split, and `_registered_hook_command` now picks the synchronous registration. Part of #4549 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Running the start hook in the background for `startup|resume|clear|fork` still handed the agent its reconnect notice twice. An async SessionStart hook's context arrives no earlier than the session's first prompt, and the prompt hook hands that same turn the same notice for every retained claim. On a live `claude --resume` (Claude Code 2.1.316, load about 40), review saw the model receive "the same text twice"; the async hook took 29 ms to hand off where the synchronous one on main held the session for 287 ms. So Claude Code now registers Leaf's SessionStart hook for `compact` alone, whose reminder to read the leaf skill again must reach the agent before it replies and which no other hook gives. A resumed session gets its reconnect notice from the prompt hook, as `test_resumed_session_is_reminded_until_its_leaf_recovers` holds. Codex's SessionStart registration is unchanged. `test_claude_code_runs_the_start_hook_only_after_a_compaction` holds the single synchronous registration, and `_registered_hook_command` goes back to skipping only the background watch. Part of #4549 Co-Authored-By: Claude Opus 5.5 <[email protected]>
max-sixty
marked this pull request as ready for review
November 2, 2026 05:19
leaf-agent
approved these changes
Nov 2, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 2, 2026
Claude Code waits for Leaf's PostToolBatch hook before every model call. In a session holding a page, that hook folded every page it held before deciding it had nothing to hand over, and nearly every boundary has nothing. The fold reads the page's vocabulary, which composes the page's whole layer, parsing every package stylesheet and scanning every widget module's imports. On this Mac that was about 350 ms of the hook's 500, spent at every tool batch. A tool boundary carries only input past a page's cursor, a status line due for its two-minute check, or a line the clerk queued. `carry_turn` now checks those first. The clerk queue and the status mark are files, and `new_input` reads each page's log under its transaction and asks `delivery.deliverable(received=False)`, the question Codex's tool hook already asks. Only when one of them holds does the hook fold its pages, exactly as before. `hook_transport` imports the page model (`activity`, `served_state.context`, `event_contracts`) in the functions that fold a page, so an empty boundary loads no jsonschema, turbohtml, tinycss2 or tree-sitter: 176 modules where it loaded 389 (`python -X importtime`). One delivery moves later. An update the cursor has passed that the session holds no usable answer reference for, as after the page changed hands (`delivery.unreferenced`), reached the next boundary. It now reaches the turn wherever a hook still folds the pages: - a boundary with new input or a clerk line beside it; - a boundary whose status check is due, every `STATUS_RENEWAL` (two minutes) and at a session's first boundary; - Stop under plain `--print`, where no watch follows the turn; - otherwise the prompt of the turn the watch wakes once this one ends. Codex's tool hook already leaves it to Stop or its adapter. Likewise, a page at an older page protocol offers no input until it is renewed, and an empty boundary no longer renews it; the next prompt or Stop hook does, the two `vendoring.renew_layer` already names. Stop and UserPromptSubmit still fold every page, and composing the page's layer is most of their cost too: in-process, `read_plans` takes 355 ms with the full composition and 44 ms when the layer composes only its registry and widget names. Composing only what a reader needs is a separate change. ## Evidence Each registered command run through `bin/leaf` against a page claimed in a throwaway state home (`.tmp/bench/bench.py` in the worktree), base and candidate interleaved, three rounds of 9 runs per arm. Median of the round medians, with the range of round medians: | Hook | Base | This change | Load | | --- | --- | --- | --- | | PostToolBatch, nothing pending | 742 ms (657–795) | 161 ms (113–227) | 12–25 | | PostToolBatch, nothing pending | 720 ms (620–842) | 173 ms (115–189) | 37–56 | | Stop | 747 / 674 ms | 758 / 817 ms | both runs | | UserPromptSubmit | 684 / 660 ms | 679 / 646 ms | both runs | At load 3, before the change, the same PostToolBatch took 502 ms (n=15). In-process on the base, `read_plans` took 350–390 ms of it and the imports up to `hook_transport` about 60 ms. After it, the empty boundary's own work is `new_input` at about 2 ms on a short log; what remains is the gate's interpreter, uv, the second interpreter, Click and Leaf's base modules. In a real Claude Code session (`uv run leaf-dev journey claude-code`, Claude Code 2.1.316), Leaf's hook durations from Claude Code's own `Hooks:` debug lines: | Hook | Before (main, load ~5) | After (load ~20–45) | | --- | --- | --- | | PostToolBatch | 565 ms median, n=26 | 212 ms median, n=26 | | Stop | 573 ms, n=13 | 767 ms, n=13 | | UserPromptSubmit | 920 ms, n=18 | 1256 ms, n=18 | Stop and UserPromptSubmit run unchanged code here, so their rise measures the busier host during the second run; PostToolBatch fell despite it. Every journey step passed on both runs, including `mid-turn`, `ending` and `boundary`, whose comments a tool boundary hands over. An independent review drove each boundary case through `cmd_hook` on base and head and found no delivery the change misses beyond the deferral above. `test_a_tool_boundary_with_nothing_new_answers_from_the_log` fails on the base (the empty boundary folded the page) and passes here; the comment that arrives next still reaches the following boundary. The `read_page` seam of `test_hook_snapshot_serializes_receipt_and_reply` moves to `served_state.context`, where `read_plans` now looks it up. `test_the_hook_transport_imports_no_page_model` (formerly `test_a_claimed_hook_imports_no_css_script_or_watch_parser`) now forbids jsonschema, turbohtml and markdown_it too, so hoisting a page-model import back into `hook_transport` fails it. ## Checks run - `uv run pytest tests/model/test_hooks.py tests/model/test_clerk.py tests/model/test_forks.py tests/model/test_hook_registration.py tests/process/test_hook_gate.py tests/model/test_session_lifecycle.py tests/model/test_thread_reading.py tests/model/test_interact_anchors.py` (210 passed) - `uv run pytest tests/model/test_codex_adapter.py tests/model/test_codex_app_server.py` (194 passed) - `uv run leaf-dev journey claude-code` before (on main) and after (this change with #5052 and current main): every step passed ## Not verified - Linux, where the issue's numbers came from. The saving there should be larger, since that host compiled stdlib bytecode on every start, but I measured only this Mac. Part of #4549 > _This was written by Claude Code on behalf of max-sixty_ --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Claude Code now runs Leaf's SessionStart hook only after a compaction (
"matcher": "compact"). On startup, resume,/clearand fork, the hook printed only reconnect notices, which the prompt hook hands the same first turn for every retained claim. Meanwhile Claude Code held a resumed session's first reply, and a/resumeswitch inside a session, until the hook ended.Why deleting is enough
cmd_hookprints onlyreconnect.notices(...), plusreload_skillwhensourceiscompact. The prompt hook passesreconnect_harnessintocarry_turn, which prints the same notices for the same retained claims. Both pass the same--if-claimedgate inbin/leaf, so nothing the start hook could reach is out of the prompt hook's reach.test_resumed_session_is_reminded_until_its_leaf_recovers[claude-code]holds the prompt hook's notice for a resumed session with a retained claim."async": true. Claude Code delivers an async hook'sadditionalContextat the next conversation turn (hooks reference), no earlier than the first prompt, which the prompt hook already reaches with the same notice. On a liveclaude --resume(Claude Code 2.1.316, load about 40), review saw the model get "the same text twice". The async hook took 29 ms to hand off, where main's synchronous one held the session for 287 ms.Codex's SessionStart registration is unchanged: its startup hook captures the native source a first claim rests on.
Evidence
bin/leafagainst a page claimed in a throwaway state home: 83 ms median at load 3 (n=15), 98–160 ms at load 12–56. The issue measured 468 ms median and 4.8 s at p90 on its Linux host. With this change a resumed session runs no start hook.uv run leaf-dev journey claude-codepassed every step with the async version of this PR. Itscompactingstep showed the compaction hook running synchronously and delivering the skill reminder ("provided additionalContext (355 chars)"), which this version keeps unchanged.Checks run
uv run pytest tests/model/test_hook_registration.py tests/process/test_hook_gate.py tests/model/test_session_lifecycle.py tests/model/test_hooks.py::test_a_compaction_sends_the_agent_back_to_the_skill(54 passed)test_claude_code_runs_the_start_hook_only_after_a_compactionholds the single synchronouscompactregistration, and_registered_hook_commandagain skips only the background watch.Not verified
Part of #4549