Repository navigation
Description
Leaf's Claude Code integration could start Python far less often if its subscription layer did a little filtering of its own. The subscription layer is the classic registrations in hooks/hooks.json plus the engine module hooks/claude-code.ts. The main logic would stay in Python, in one place, shared across harnesses; only the decision "this event has nothing for Leaf" would move up.
What happens now
Both layers subscribe broadly and leave every decision to Python, so a session that holds no page still starts Python several times a turn. Counting from the registrations, one turn starts:
UserPromptSubmit:bin/leaf hook --if-claimed, which runs sh and thenstate.py hook-gate; andbackground-watch.py, which runs python3, thenbin/leaf, then the gate.PostToolBatch: the gate once per tool batch.Stop: the same pair asUserPromptSubmit.- The module: the pages follower on
turn.startand again onturn.complete, and the clerk when the turn ends in an answer. Each goes through the gate.
That is about nine Python starts per turn plus one per tool batch. Each is cheap where Leaf's environment exists. On one lightly loaded Linux host, sh plus a bare python3 -S takes about 7 ms, and python3 → sh → python3 about 26 ms. The cost shows up elsewhere:
- Where the environment doesn't exist yet, or can't be built (for example, uv can't download a Python on a machine with no outbound network), the launcher runs
uv runin full for every one of these calls. The same failure then repeats at every registration, and some of those failures show up in the conversation. - The module subscribes to
session.appendfor every row, though it only records the newest main-thread row id for fork diagnostics. On a machine with Claude Code managed settings, the built-incc-plugin-sec-defaultplugin sends some rows, such as hook context, attachments and compaction summaries, past the plugins the user installed. Claude Code then adds a notice to the conversation saying that Leaf'ssession.appendhook was skipped becausecc-plugin-sec-defaultran first and sent the event on past it. The notice recurs from session to session on such a machine (seen with Claude Code 2.1.321), though nothing is wrong.
Possible directions
- Subscribe narrowly, for example
session.appendonly on the doors whose rows Leaf records. - Let the module answer "this session holds no page" in-process: the follower already reports the session's pages to it. Then move the classic
--if-claimedregistrations into the module wherever its events cover them, so that one in-process check gates them too.
Repro
- Install Leaf in Claude Code on a machine with managed settings.
- Start a session and send one prompt without creating a page.
- The conversation shows the skipped-hook notice.
The Python start counts above come from reading the registrations, not from a process trace.
Seen on leaf 2071b2d.
This was written by Claude Code on behalf of max-sixty
Activity
leaf-agent commentedon Nov 10, 2026
Triage result: I'm not opening a PR yet. Both directions are sound, but each one involves a choice the code can't settle, so the findings below are input for picking one.
The notice has a narrow cause. In the Claude Code build on this runner (2.1.316),
cc-plugin-sec-defaultsubscribes tosession.appendfor exactly three doors,attachment,hook-contextandcompaction, and sends each of them on (o.to(r,"append")). Leaf's handler athooks/claude-code.tshas no filter, so it sits under all three. Asession.appendfilter can namedoor,originandagentId(the event'spinnedKeys). The other doors in that build aremodel,prompt,tool-message,queueandnote. Registering only on those should remove the notice. I haven't confirmed that, because I can't tell from the minified source whether a handler whose filter doesn't match is left out before the skip check runs, and this runner has no managed settings to test with. The cost of the change:lastRowwould stop advancing on attachment, hook-context and compaction rows on every machine. On managed machines it already misses them. The clerk andforkReplyreadlastRowonly for diagnostics, as therowinrequest. If that diagnostic doesn't need those rows, the narrow filter is a small, self-contained change.Moving the gate in-process has one fixed limit and one race.
background-watch.pyregistrations (UserPromptSubmit,Stop) can't move into the module. The module header (L7-L12) records why: a module wake arrives as a prompt, and Claude Code drops that prompt after an Escape. That leaves the classic--if-claimedgate in place for those two registrations, about two Python starts per turn plus the watcher's own python3 even before the gate runs. ThePostToolBatch,UserPromptSubmitandStopbin/leaf hook --if-claimedcalls are the ones that could move.leaveslags a claim. Theturn.completecomment (L516-L518) says the band may not yet list a page that the turn's last command claimed. So "the follower lists nothing" isn't a safe "no page". A safe in-process answer would be "the follower lists nothing, and noleafBash call has run since it last exited." The module already sees every such call through itstool.callfilter. Without that second condition, the clerk and the Stop hook would skip the turn in which a page was claimed.Python starts without an environment. Every gated call runs
uv runin full when.venv/bin/pythonis missing (bin/leafL83-L91). That's true of both directions, so an in-process "no page" answer from the module would also stop the repeated uv failures for the calls it absorbs.max-sixty commentedon Nov 10, 2026
do something simple if it can be done; not worth some heavy logic here to save some JS calls...
leaf-agent commentedon Nov 10, 2026
Opened #5788: the simple part only. The module's
session.appendhandler now registers on the five conversation doors, which keeps it out from undercc-plugin-sec-default's three. The in-process gate is left out.