Skip to content

Claude Code: filter events in the subscription layer so fewer of them start Python #5784

Closed

Description

@max-sixty

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 then state.py hook-gate; and background-watch.py, which runs python3, then bin/leaf, then the gate.
  • PostToolBatch: the gate once per tool batch.
  • Stop: the same pair as UserPromptSubmit.
  • The module: the pages follower on turn.start and again on turn.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 run in 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.append for 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-in cc-plugin-sec-default plugin 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's session.append hook was skipped because cc-plugin-sec-default ran 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.append only 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-claimed registrations into the module wherever its events cover them, so that one in-process check gates them too.

Repro

  1. Install Leaf in Claude Code on a machine with managed settings.
  2. Start a session and send one prompt without creating a page.
  3. 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

  1. added
    enhancement
    on Nov 10, 2026
  2. leaf-agent commented on Nov 10, 2026

    @leaf-agent
    Collaborator

    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-default subscribes to session.append for exactly three doors, attachment, hook-context and compaction, and sends each of them on (o.to(r,"append")). Leaf's handler at hooks/claude-code.ts has no filter, so it sits under all three. A session.append filter can name door, origin and agentId (the event's pinnedKeys). The other doors in that build are model, prompt, tool-message, queue and note. 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: lastRow would stop advancing on attachment, hook-context and compaction rows on every machine. On managed machines it already misses them. The clerk and forkReply read lastRow only for diagnostics, as the row in request. 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.

    • The two background-watch.py registrations (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-claimed gate in place for those two registrations, about two Python starts per turn plus the watcher's own python3 even before the gate runs. The PostToolBatch, UserPromptSubmit and Stop bin/leaf hook --if-claimed calls are the ones that could move.
    • The module's leaves lags a claim. The turn.complete comment (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 no leaf Bash call has run since it last exited." The module already sees every such call through its tool.call filter. 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 run in full when .venv/bin/python is missing (bin/leaf L83-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.

  3. max-sixty commented on Nov 10, 2026

    @max-sixty
    OwnerAuthor

    do something simple if it can be done; not worth some heavy logic here to save some JS calls...

  4. leaf-agent commented on Nov 10, 2026

    @leaf-agent
    Collaborator

    Opened #5788: the simple part only. The module's session.append handler now registers on the five conversation doors, which keeps it out from under cc-plugin-sec-default's three. The in-process gate is left out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions