Skip to content

Defer CSS, script and watch parser imports out of the claimed hook - #3884

Merged
max-sixty merged 2 commits into
mainfrom
grind/g-lazy-imports
Oct 27, 2026
Merged

max-sixty merged 2 commits into
mainfrom
grind/g-lazy-imports

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

A hook on a claimed page imported tinycss2 and tree_sitter (through revision_artifact, layer and styles) and watchfiles (through file_changes, reached by presence and server) 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_reader on 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. jmespath is 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_transport from python -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-code over 40 runs on a shared, noisy host: min 188 ms before, 156 ms after; medians 247 ms vs 199-229 ms. import leaf.cli and import leaf.hooks (the no-page path, already guarded by test_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, holding leaf.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.py or bin/leaf, so it merges with #3857 (hooks.main os._exit) and #3876 (launcher).

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

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]>
@max-sixty
max-sixty marked this pull request as ready for review October 27, 2026 05:17
@max-sixty
max-sixty merged commit 4cb6a6d into main Oct 27, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/g-lazy-imports branch October 27, 2026 05:34
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]>
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