Repository navigation
Defer CSS, script and watch parser imports out of the claimed hook - #3884
Merged
Merged
Conversation
A hook on a claimed page imported tinycss2, tree-sitter and watchfiles without parsing CSS, reading a script's imports or watching a file. Each import now sits in the function that uses it; tree-sitter's parser is built once by `_javascript_reader` on the first script read. Cumulative `leaf.hook_transport` import: 102 ms before, 74-86 ms after (`python -X importtime -m leaf hook --harness claude-code`, claimed page, PostToolBatch). jsonschema stays: the claimed hook validates events. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This was referenced Oct 25, 2026
Co-Authored-By: Claude Opus 5.5 <[email protected]>
max-sixty
marked this pull request as ready for review
October 27, 2026 05:17
leaf-agent
approved these changes
Oct 27, 2026
max-sixty
added a commit
that referenced
this pull request
Oct 27, 2026
Claude Code runs Leaf's PreToolUse registration once for each `if` rule a Bash command matches, all at once (`hooks/hooks.json` has three rules). A command it cannot read, such as one with a `$VAR`, matches all three. Each run escaped the sandbox (`bridge.ensure`) and, in a session holding a page, confirmed receipts from the transcript, so the same work ran three times in parallel. Now the first run for a `tool_use_id` records it (`leases.first_hook_for_tool`, under a lock on the session's `sessions/<digest>.tools`, which keeps the last 16 ids), and the other runs return after marking that hooks ran. Claude Code still starts every run, so each duplicate still pays for the launcher, the interpreter and `hooks.py`'s imports. This PR previously also carried a standard-library gate in the launcher that answered a quiet PostToolBatch without starting Leaf, and a launcher that ran `uv sync` and the environment's interpreter directly. Both are gone: the gate was a second copy of what delivery reads, and #4141 is the launcher change that landed instead. `AGENTS.md` and every other contract the PR had rewritten are back to `main`'s wording. ### Measurement One tool call's PreToolUse runs, started as Claude Code starts them through each arm's `bin/leaf`, on a claimed, stamped scratch page under a throwaway state home, with a 1.4 MB transcript in the payload. The arms are interleaved and alternate which goes first, 25 calls per cell, at a load of 9–12 on 18 cores. Times are medians; CPU is user plus system time across all runs of the call. | Runs for the call | `main` wall | branch wall | `main` CPU | branch CPU | | --- | --: | --: | --: | --: | | 1 (a plain `leaf …` command) | 156 ms | 154 ms | 140 ms | 141 ms | | 3 (a command Claude Code cannot read) | 203 ms | 160 ms | 434 ms | 285 ms | A single run costs the same on both. When all three rules match, the call's hooks finish 43 ms sooner and spend 149 ms less CPU. Each duplicate drops from about 147 ms of CPU to about 72 ms. How often all three match: in this branch's `leaf-dev journey claude-code` (Claude Code 2.1.310), 2 of the 15 tool calls that ran the hook ran it three times, and the rest once. An earlier journey on `main` showed 1 of 15, and one on this branch's first version 4 of 17. Of the 2,619 Bash calls running `leaf` in this Mac's Claude Code transcripts from the last two days, 25% contain a `$`. That figure is an estimate. It assumes every such command matches all three rules, and Leaf's own development sessions make up much of the sample. ### Tests - `tests/model/test_hooks.py::test_one_tool_calls_hooks_do_its_work_once`: a repeated PreToolUse for one `tool_use_id` leaves the turn as it found it, and another call's hook opens it again. The test fails with the dedupe off. - Ran `tests/model/test_hooks.py`, `test_hook_registration.py` and `test_session_lifecycle.py` (93 passed), and pre-commit on the changed files. - `leaf-dev journey claude-code` passed every step. ### Overlap with #4145 and #3884 Both have since landed, and this branch carries a merge of `main` with them; the hook, claims and session tests pass on it. - #4145 (`simplify-hook-path`) merges with this branch in either order, with no conflict, and the dedupe test passes on a local merge of the two. #4145 deletes `leases.session_state_path`, so `first_hook_for_tool` names its file through `state.session_file`, which both keep. On `main`, `session_file` does not create the `sessions/` directory, but the hook's `mark_hooks` creates it before the dedupe runs; on #4145, `session_file` creates it. #4145's rename of `holds_claims` to `held_claims` touches lines this PR leaves alone. - #3884 (`grind/g-lazy-imports`) shares no files with this PR and merges cleanly. Its deferred imports make each whole hook run cheaper; this PR removes the duplicate runs' work, so the two savings add. ### Not verified - Linux. The measurement and journey ran on macOS. - Codex and Pi register no PreToolUse hook, so they never reach this code, and their journeys were not run. Part of #4084. > _This was written by Claude Code on behalf of max-sixty_ --------- Co-authored-by: Claude Opus 5.5 (1M context) <[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.
A hook on a claimed page imported
tinycss2andtree_sitter(throughrevision_artifact,layerandstyles) andwatchfiles(throughfile_changes, reached bypresenceandserver) without parsing CSS, reading a script's imports or watching a file. Those are capture, vendoring and watch work. Each import moves into the one function that needs it; tree-sitter's parser and query are built once by_javascript_readeron the first script read. No behavior changes.The overnight note's headline does not hold:
jsonschema(about 28 ms warm) is not deferrable on this path. The claimed hook reads the page and validates its stored events, and an instrumented run built three schema validators, so the import is used.jmespathis likewise loaded by registry validation (registry/layer.py) in the real hook, so it stays.Measured on a claimed scratch page with a PostToolBatch payload, warm cache. Cumulative
leaf.hook_transportfrompython -X importtime -m leaf hook --harness claude-code: 102 ms before, 74-86 ms after. Wall time of.venv/bin/python -m leaf hook --harness claude-codeover 40 runs on a shared, noisy host: min 188 ms before, 156 ms after; medians 247 ms vs 199-229 ms.import leaf.cliandimport leaf.hooks(the no-page path, already guarded bytest_a_hook_in_a_session_holding_no_page_imports_no_page_reading_or_server) are unchanged within noise.Adds
test_a_claimed_hook_imports_no_css_script_or_watch_parser, holdingleaf.hook_transport's import free of the three.Ran: tests/model/test_hook_registration.py, test_hooks.py, tests/process/test_hook_gate.py, test_interact_layer.py, test_native_watch.py, test_revision_delivery.py, test_live_shell_artifact.py. Not run: the full suite or the claude-code journey.
Only import placement changes, with no edits to
hooks.pyorbin/leaf, so it merges with #3857 (hooks.mainos._exit) and #3876 (launcher).