Skip to content

A plugin event starts one hook process that loads only its mode's scripts, and the instructions ask for a first call only to a loaded tool - #1720

Open
zlareb1 wants to merge 8 commits into
mainfrom
fix/plugin-code-mode-idle-cost
Open

zlareb1 wants to merge 8 commits into
mainfrom
fix/plugin-code-mode-idle-cost

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

The Claude Code plugin starts one hook process after a tool call, at Stop and before a compaction, where it started two to four, and in code or memory mode that process loads only that plane's scripts. A shell command with no read or search in it costs the code shunt no git call, a prompt waits for its lookups no longer than LOOM_PROMPT_PACK_TIMEOUT_MS, and the plugin's MCP instructions ask for a first call only to a code tool whose schema is loaded up front.

Why

A benchmark ran one bug-fix task in 29 sessions with the plugin installed. The agent called no Loom tool in any of them, so what the plugin added was overhead, and the hook processes were the part of it that costs time. hooks.json is fixed per event, not per plane, so an install of one plane still started the other plane's scripts:

  • A shell call started one process before it and three after it in code mode: the file context, which is memory's and exits unused, the test gate and the recorder. A Stop started four, a compaction four.
  • The instructions asked for a call that costs a request. The plugin's initialize paragraph said "Use Loom first" and named loom_code_search, whose schema is deferred, so following it costs the client a tool-search request before the call.
  • A prompt could wait two or three bounds. The code pointers gave their repository lookup and their answer 2.5 s each, and the memory recall beside them had the request bound, 3 to 8 s.

What is true now:

  • One process per event. loom-record.mjs, which already ran an event's recording scripts and a prompt's two lookups, also runs the scripts the session waits for after a tool call (file context, mentor hint, blast radius, tests line, each for the tools it was wired for) and at Stop (the three gates), with runHooksTogether, and prints their answers as one. It loads a script only for its tools and only where the mode runs its plane. Before a compaction it runs the four scripts in its own process, as at SessionEnd.
  • A shell command with nothing to judge costs the shunt nothing. The shunt parses the command before it looks the checkout up, so a command with no read, no file search and no issue search starts no git and asks for no repository.
  • One bound per prompt. The pointers' repository lookup and answer share LOOM_PROMPT_PACK_TIMEOUT_MS, and the recall's request has the same bound. A lookup that has not answered by then is skipped.
  • The instruction sentence follows the listing. It names the code tools the listing loads up front (loom_code_edit_context and loom_code_grep by default) and, where a deployment loads none up front, asks for no first call.

Measured on one shared machine, base and branch back to back, the order alternating (tests/bench/hook-wall-time.mjs, stub server at 20 ms, 50 tool calls and one stop):

main this branch
Processes the session waits for, code mode 196 104
Session hook wall time, code mode, median of 10 rounds 44.5 s 39.4 s
Per firing, code mode: before a shell call 352 ms 255 ms
Per firing, code mode: after a shell call 414 ms 344 ms
Session hook wall time, mode both, 3 rounds 48.3, 45.7, 129.5 s 37.9, 33.0, 29.5 s

The machine was under a load average of 22 to 60 for all of it, and an empty node process took 84 to 303 ms to start and exit, so 104 of them alone took 12 to 20 s. The absolute times say little about an idle machine; the ratios are the result. The paired ratio in code mode ranged from 0.77 to 1.50, median 0.92.

What this does not fix:

  • The skill listing does not follow the mode. The client loads each skill's description as fixed text when it starts and substitutes an install option only into a skill's body, so the three listed skills cost about 2,300 bytes of the first request in every mode. The code mode first request is 5,964 bytes with the plugin's defaults (5,960 on main; the sentence is four bytes longer). With LOOM_MCP_ALWAYS_LOAD_TOOLS empty it is 3,337 bytes and the paragraph asks for no first call.
  • A tool call still costs two process starts, one before and one after. A hook entry cannot depend on an install option, so nothing in the plugin removes them.

Scope and risk

  • Touches: hooks/hooks.json (PostToolUse, PreCompact and Stop each wire the launcher alone), hooks/loom-record.mjs, six scripts that now return their answer through runHookMain instead of writing it (loom-file-context, loom-mentor-hint, loom-blast-radius, loom-test-gate, loom-validate-gate, loom-namespace-health), loom-code-shunt, the two prompt lookups, and on the server the plugin paragraph in server/transport/mcp_primer.py.
  • Leaves alone: the PreToolUse wiring, which already started one script per tool; the Codex and hand-wired wirings, which start each script as a hook of its own, where each still prints its own answer; the detached recording process; which schemas load up front.
  • Two scripts answering one event reach the model as one message. After a shell command that both read a file with memory and changed a file with tests, the file's memory and the tests line are one additionalContext, joined by a newline, where the client showed two. Two gates holding the same stop are one block with a reason on each line. Each script's text is unchanged.
  • A slow recall is skipped. The recall's request had the request bound and has 2.5 s now.
  • The six scripts report a thrown error under LOOM_HOOK_DEBUG=on, as every script that ends in runHookMain does. They were silent.
  • Hooks in a session: a script that throws or does not load has no answer and the others keep theirs; every path still exits 0.

Compatibility, rollout and rollback

  • Wire: no op or field changes. The text of the initialize instructions changes for a plugin connection (schema_profile=plugin), in every mode. LOOM_PROMPT_PACK_TIMEOUT_MS (2,500 ms) now also bounds the memory recall's request.
  • Operator steps: none. A server without this change keeps sending the earlier sentence.
  • Rollback: reverting leaves nothing behind. Job file names, state files and cursors are unchanged.
  • Both plugins are 1.2.78.

Tests

Ran; each passes except where a line says otherwise:

  • node --test, one file at a time, on the four plugin test files this changes (tests/hooks-json.test.mjs 8, tests/record-hook.test.mjs 24, tests/hook-contract.test.mjs 7, tests/code-shunt-hook.test.mjs 27) and on tests/plugin-modes.test.mjs (4, unchanged: it runs a scripted session per mode through the wiring). Run as one invocation while a replay shared the machine, three tests failed on timing in one run and three others in the next.
  • Python test bodies through a snippet, not through pytest, so parametrized tests and tests with other fixtures did not run: tests/unit/test_mcp_primer_order.py (the plugin paragraph, with the new test), test_codex_plugin.py, test_mcp_coding_toolset.py, test_mcp_listing_budget.py, test_search_switch_ab.py, test_public_copy.py, test_agent_instructions.py, test_published_links.py. In test_session_content_senders.py the reviewed pins of the eight scripts this edits are renewed (their send sites and bodies are unchanged); the test still fails on the pin of sdk/python/loom/memory.py, which does not match on main either.
  • ruff format --check, ruff check, the function-length gate, agent_plugin_sync.py --check, docs_reference.py --check, op_reach_census.py --check, settings_reference.py --check, Biome and Prettier on the changed files, node --check on every changed plugin file, claude plugin validate integrations/claude-code-plugin --profile directory --strict. The mypy gate reports only server/llm/vertex_auth.py, where this machine has no stubs for google-auth.
  • node integrations/claude-code-plugin/tests/e2e/agent-session.mjs claude: one session of the installed plugin on the current client, against a local fake for the model API and Loom. Every event fired, every hook exited 0 with nothing on stderr, and each event's Loom call arrived (PASS claude on darwin-arm64: 24 requests).

Replay: the hook inputs of the 29 recorded sessions (344 events) went through the hooks of main and of this branch, started the way the client starts them, against a stub server whose answers make the hooks inject. The recorded shell commands were not run; a call that writes a file of the checkout appends a line to it.

  • Mode both, replayed twice: every event's output is byte-identical, 69,417 bytes over 139 events with output, and both arms sent the same requests. The sessions waited for 660 processes on main and 344 here. The hooks took 264.0 s on main and 247.2 s here in the first replay, and 402.7 s and 406.5 s in the second, which shared the machine with the test runs.
  • No recorded event had two scripts answer at once, so the joined answer is covered by the tests in tests/record-hook.test.mjs, not by the replay.
  • Mode code: every event's output is byte-identical, 10,848 bytes over 75 events. In one session one arm sent one code.test_gate request fewer during a load spike; replayed alone, that session sent the same requests in both arms.

Not run: the unit suite and the integration suite (CI), the other plugin test files, the plugin skill evals, a session on Codex.

The client's hook schema was read in two versions. The wiring uses the fields main uses (type, command, args, matcher, timeout) and adds none. A client older than the documented minimum, 2.1.139, drops args and runs no plugin hook, on main as here.

Not verified:

  • How the current client shows one hook's context that holds two scripts' text. An earlier client's source shows one reminder per hook with its lines joined by newlines; the current client is not readable.
  • Hook times on an idle machine.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code wrote the diff and ran the measurements and tests listed above. A person has not read it yet.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.
  • I signed the Contributor License Agreement (CLA) if CLA assistant requested a signature.

🤖 Generated with Claude Code


Note

Medium Risk
Consolidating multiple hooks into one launcher changes timing, merging, and mode gating of injected context and stop gates; replay/tests claim byte-identical outputs in both mode but paired hook timing varied under load.

Overview
Hook wiring no longer starts separate processes for post-tool context, gates, and PreCompact housekeeping. hooks.json wires loom-record.mjs alone for PostToolUse, PreCompact, and Stop; the launcher runs mode-filtered “waited” scripts (loom-file-context, mentor hint, blast radius, tests line, stop gates) in-process via runHooksTogether, merges their outputs into one additionalContext or block reason, and still spawns a detached process for recording-only scripts.

Individual hook scripts return JSON answers through runHookMain instead of writing stdout, so they can run standalone (Codex) or under the launcher (Claude Code).

Prompt latency is capped by a single shared bound: LOOM_PROMPT_PACK_TIMEOUT_MS (default 2.5s) applies to the code pack’s repository lookup plus search answer and to prompt recall; slow lookups are skipped.

Code shunt exits early on Bash commands with no file read, grep, or issue search, avoiding git and repository resolution.

PreCompact runs capture, working memory, session brief, and file-context reset inside loom-record synchronously so the working-memory note exists before the post-compaction primer.

Documentation and plugin version 1.2.78 (Claude Code and Codex manifests) align with the new event model and MCP startup guidance (first call only to tools loaded up front).

Reviewed by Cursor Bugbot for commit a052d09. Bugbot is set up for automated code reviews on this repo. Configure here.

zlareb1 and others added 8 commits November 28, 2026 23:26
… a first call only to a tool loaded up front

Work in progress on this branch; the pull request is squash-merged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…ripts

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…iewed sender pins follow the edited scripts

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 28, 2026 7:37pm UTC
loom-telemetry Ready Ready Preview Nov 28, 2026 7:37pm UTC

Request Review

This branch has not been deployed

No deployments
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.

1 participant