Skip to content

Run Claude Code's start hook only after a compaction - #5052

Merged
max-sixty merged 2 commits into
mainfrom
grind/issue-4549-session-start
Nov 2, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/issue-4549-session-start

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Claude Code now runs Leaf's SessionStart hook only after a compaction ("matcher": "compact"). On startup, resume, /clear and 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 /resume switch inside a session, until the hook ended.

Why deleting is enough

  • What the start hook says. At SessionStart cmd_hook prints only reconnect.notices(...), plus reload_skill when source is compact. The prompt hook passes reconnect_harness into carry_turn, which prints the same notices for the same retained claims. Both pass the same --if-claimed gate in bin/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.
  • Why not run it in the background. The first version of this PR made the non-compaction sources "async": true. Claude Code delivers an async hook's additionalContext at the next conversation turn (hooks reference), no earlier than the first prompt, which the prompt hook already reaches with the same notice. On a live claude --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.
  • Why compaction keeps it. After a compaction the hook tells an agent holding a page to read the leaf skill again before it replies, and no other hook says that. It stays synchronous, since an async hook's context would join only the next turn.

Codex's SessionStart registration is unchanged: its startup hook captures the native source a first claim rests on.

Evidence

  • Main's synchronous start hook, through bin/leaf against 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.
  • Review's live resume: async 29 ms against synchronous 287 ms at load about 40, with the notice delivered twice under async.
  • uv run leaf-dev journey claude-code passed every step with the async version of this PR. Its compacting step 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_compaction holds the single synchronous compact registration, and _registered_hook_command again skips only the background watch.

Not verified

  • The journey on this final version: it changed only by removing the async entry, which the journey's steps don't reach except at startup, where a session holds no claim.

Part of #4549

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

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 max-sixty changed the title Stop holding a resumed Claude Code session on Leaf's start hook Run Claude Code's start hook only after a compaction Nov 2, 2026
@max-sixty
max-sixty marked this pull request as ready for review November 2, 2026 05:19
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]>
@max-sixty
max-sixty merged commit 1c38e10 into main Nov 2, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/issue-4549-session-start branch November 2, 2026 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants