Skip to content

Recording hooks hand off after a tool call and at Stop, and run in one process at SessionEnd - #1677

Merged
zlareb1 merged 6 commits into
mainfrom
fix/plugin-hooks-do-not-hold-the-session
Nov 28, 2026
Merged

zlareb1 merged 6 commits into
mainfrom
fix/plugin-hooks-do-not-hold-the-session

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's recording hooks (tool observations, conversation turns, usage, the working-memory note) no longer hold the session after a tool call or at Stop: one launcher hands the event to a detached process and returns in about 70 ms whatever the server's round trip. At SessionEnd one process runs the five recording scripts where five ran, and the hook still returns only once the run's close is answered.

Why

A coding session with the plugin installed, in which the agent never called a Loom tool, still waited about 3.8 s on the plugin's hooks: a capture process after every tool call, seven processes at Stop and five at SessionEnd.

Failing cases, measured offline (method under Tests) and in that recorded run:

  • The hook that records a tool call posts without awaiting the answer, but its process stays until the answer arrives, and the host waits for the process: 458 ms per call against a server that answers after 300 ms, 158 ms against one with no delay. In a session of 55 tool calls that is about half of the hook wall time.
  • The Stop pass takes about 500 ms against a server with no delay, because the conversation hook sleeps its 350 ms settle (so the last reply reaches the transcript) inside a process the host waits for. In the recorded run the Stop pass took 0.90 s on average and the capture hook was its slowest process.
  • A session of four tool calls waits on 25 node processes.

Pattern: each of these scripts prints nothing the host reads, yet the host waits for a request or a sleep whose result the session never uses.

Hypothesis: one launcher per event runs the event's recording scripts in one process. After a tool call and at Stop it writes the payload to a private job file, starts one detached copy of itself on it, and exits. At SessionEnd it runs the scripts itself and waits for them: a host that is torn down when the client exits (a container whose entrypoint is the client, a CI runner that kills what a job leaves behind) would end a detached copy before it had sent anything, and on main the run's close is awaited there. Predicted before the run: a tool call's recording hook at 60 to 80 ms at any server latency; Stop down to its slowest gate (about 225 ms with no server delay, about unchanged at 300 ms, since a gate must wait for its answer); SessionEnd about as on main, with one process where five ran; 18 processes waited on where there were 25; the same requests at the server.

Measured: median wall time of one firing, hooks started as Claude Code starts them, before on main's plugin directory and after on this branch's.

Per session, the hook wall time the session waits for, before to after: the short shape on a quiet machine 5.06 s to 4.13 s against a server that answers after 300 ms and 2.65 s to 2.05 s against one with no delay; the long shape on a loaded machine 51.0 s to 35.5 s and 25.8 s to 18.9 s. The tables give each event.

Short shape (4 tool calls: 3 Bash, 1 Edit; 1 prompt; 1 stop), five sessions per arm, on a quiet machine (node reaches user code in 28 ms). This run was made before scripts sharing a process looked the checkout up once, which changes the SessionEnd row only:

Event (processes the host waits for, before to after) 300 ms server, before after No delay, before after
PostToolUse, Bash (1 to 1) 458 ms 71 ms 158 ms 68 ms
PostToolUse, Edit (2 to 2; the blast-radius hook awaits its answer) 490 ms 480 ms 184 ms 189 ms
Stop (7 to 4; three gates remain) 501 ms 504 ms 498 ms 177 ms
SessionEnd (5 to 1), each script looking the checkout up itself 495 ms 778 ms 505 ms 524 ms
The session's hook wall time 5.06 s 4.13 s 2.65 s 2.05 s
Node processes waited on / detached / in all 25 / 3 / 28 18 / 8 / 26 25 / 3 / 28 18 / 8 / 26

The 778 ms was the five scripts' scope lookups: 17 git calls, about 400 ms, one after the other before the run's close left the process (it arrived at 474 ms). With one lookup per process there are 5, and a test checks that the checkout is looked up once.

The final code, on a shared machine under load (node reaches user code in 37 to 43 ms, so every figure is higher than above and the two arms were run in alternation):

Short shape, eight sessions per arm 300 ms server, before after No delay, before after
PostToolUse, Bash 508 ms 123 ms 217 ms 103 ms
PostToolUse, Edit 615 ms 609 ms 296 ms 263 ms
Stop 724 ms 694 ms 681 ms 333 ms
SessionEnd 650 ms 725 ms 641 ms 550 ms
The session's hook wall time 6.29 s 5.32 s 3.66 s 3.02 s
Long shape (55 tool calls: 43 Bash, 8 Edit, 4 Read; 3 prompts; 3 stops), two sessions per arm 300 ms server, before after No delay, before after
The session's hook wall time 51.0 s 35.5 s 25.8 s 18.9 s
SessionEnd 661 ms 845 ms 587 ms 607 ms
Node processes waited on / detached / in all 154 / 5 / 159 141 / 63 / 204 154 / 5 / 159 141 / 63 / 204

SessionEnd is not a saving: it is one process where five ran, at about the cost it has on main, and under load somewhat more against a slow server. In its final form it was timed on the loaded machine only: with no server delay between 90 ms faster and 20 ms slower than main, with a 300 ms server between 75 and 195 ms slower (a third run of five sessions per arm gave 632 to 827 ms and 676 to 634 ms). One process does the scripts' synchronous work in sequence where five did it side by side, and a request needs the event loop to reach the wire, so the run's close leaves after the last script's git calls and transcript reads (it arrived about 130 ms later than on main in these runs). On a quiet machine SessionEnd should come to about 520 ms at either latency; that figure is derived from the quiet run above, not measured.

The requests that reach the server are the same in number and kind in the long pairs and in the quiet short pairs. In 2 of the 16 loaded short sessions the SessionEnd conversation script sent the last two turns a second time (see the ordering risk below). What the session still waits for is the hooks whose output the host reads: the session-start primer, the two prompt hooks and the run's opening, the code shunt before a read (about 105 ms a call), the file-context and blast-radius hooks around an edit, and the three Stop gates. At Stop a gate's own request is now the whole wait: the first Stop asks namespace.audit, and a Stop with an uncommitted change in an indexed checkout asks code.test_gate.

Scope and risk

  • The launcher. hooks/loom-record.mjs is what Claude Code runs for PostToolUse, PostToolUseFailure, Stop and SessionEnd. After a tool call and at Stop it reads the payload, writes it to a job file (loom-record-<Event>-<ms>-<pid>.json, mode 0600), starts one detached copy of itself and exits. The copy runs the event's recording scripts in one process, in the order the wiring had them, each on the payload as if the host had started it, and removes the job file when they have returned. With no credential the launcher starts nothing: there is nothing to record. In agent mode it always hands off, because resolving an agent's token can be a request.
  • SessionEnd is not handed off. The launcher runs the five scripts in its own process and exits when they have returned. By then run.complete is answered and observe.crystallize is sent, as on main; session memory, the brief and the last turns go through the flusher, as on main, and a host torn down in that instant can lose what the flusher had not sent. A test fails if the hook returns before run.complete and observe.crystallize have reached the server, or if anything is handed off.
  • What a hand-off survives. The job file is on disk before the hook returns. The copy is started detached with no inherited stdio (spawnDetached in lib/flush-job.mjs, the code the flusher already used), so it is in its own session: the hook's exit, the client's exit and a kill of the client's process group do not reach it, and a test kills the process group that ran the hook and checks the observation still arrives. It does not survive the machine: on a host torn down when the client exits, the records of a tool call or a Stop that the copy had not sent by then are lost. hooks/HOOKS.md, the README and the feature page say so.
  • A stranded hand-off. A copy that dies leaves its job file. It has a name of its own because it is not a flusher job: the client status check (bin/loom-client-status.mjs) and loom doctor --client count it beside the flusher's, and the loom-status reference names its replay, loom-record.mjs <Event> - <file>. A test lets the copy die at its start, checks that the job file is the only thing left in the temp directory (no cursor moved, nothing sent), replays it as documented and checks the Stop's four writes arrive and the file is gone.
  • One process for several scripts. runHooksTogether in lib/loom-client.mjs imports the scripts while a module flag is set: runHookMain keeps each main instead of starting it, readStdinJson answers the payload, resolveScope looks a directory up once, and scheduleExit does nothing until every main has returned, so one script finishing does not end the others. Without the single lookup the five SessionEnd scripts ran the same three git commands one after the other in one turn of the event loop (17 git calls, about 400 ms), and the first script's request left only after the last one's lookups. loom-run ended its SessionEnd with process.exit(0); it now asks scheduleExit(0, 0), which exits a script started on its own on the next timer tick and leaves a shared process to its launcher.
  • Job file names. loom-<kind>-<ms>-<pid>.json, where <ms> is the time or one past the last stamp the process took: several scripts now dispatch flusher jobs from one process, and two in one millisecond would have shared a file.
  • SessionEnd order and stdin. loom-run reads the recalled-key stash at the start of its SessionEnd branch, before the run.begin it may ask again: loom-session-memory removes the stash beside it, and a re-asked run closed without recalled_keys (a test removes the stash while the begin is awaited). The SessionEnd launcher destroys its stdin once the payload is read: on a host that never closes it the process stayed until the exit grace, 4.6 s against 0.6 s, and now exits after 1.7 s (a test runs the hook with stdin left open and a 60 s grace).
  • Not changed. The gates and the injecting hooks (session start, prompt pack, prompt recall, code shunt, file context, mentor hint, blast radius, test gate, validate gate, namespace health) run as hooks of their own, with the same input and output. The run's opening at the first prompt stays awaited: later hooks use the id it answers, and it runs beside two hooks that are awaited anyway. PreCompact starts its three scripts directly, so the working-memory note is written before the compaction's primer reads it. The Codex and generic wirings start each script themselves; the Codex wiring names the launcher and says why it is not wired there.
  • Risk: ordering. A tool call's observation and the stashes the capture script keeps (returned code paths, recalled keys, the read dedupe set) are now written a moment after the hook returns, not before. A hook that reads them runs at the next tool call or at SessionEnd, with a model request between; tool calls issued in one batch already raced on those files. When SessionEnd follows a Stop within about a second, as in a one-shot run, its conversation script can read the cursor before the Stop's flusher has moved it and send the same turns again; msg_id makes the resend idempotent. On a loaded machine this happened in 2 of 16 sessions and in none on main, where the Stop pass itself waited out the settle.
  • Risk: what is on disk. The job file holds the hook payload as the host sent it, tool input and output included and not yet redacted, until the scripts have returned; it is owner-only, like the transcript the host itself keeps. A stranded one stays until it is replayed or deleted, like a flusher job; the session-start sweep does not remove either. What the scripts send is redacted as before.
  • Risk: more processes in all. Each tool call starts two node processes (the launcher and its detached copy) where it started one, so a long session starts more processes than before although it waits on fewer. A spawn that fails loses that event's records; no cursor has moved, so the next Stop sends the conversation turns again.
  • Not measured. The offline harness times hook processes, not a live agent. Windows, and a host that is torn down at the client's exit, were not run: the SessionEnd behaviour on such a host follows from the test above (the requests are sent before the hook returns), not from a container run. The end-to-end session under the real client (tests/e2e/agent-session.mjs claude) passed while SessionEnd was still handed off and was not run again, since it needs a model call.

Compatibility, rollout and rollback

  • Wire: no op or field is added, changed or removed.
  • Operator steps: none. Clients get the change with plugin 1.2.64 (both manifests, one patch above main's 1.2.63).
  • Rollback: reverting leaves nothing behind but job files of processes that were still running.

Tests

  • The measurement: integrations/claude-code-plugin/tests/bench/hook-wall-time.mjs reads a plugin directory's hooks/hooks.json, starts every hook an event wires as node <script> <Event> with the payload on stdin, the event's hooks in parallel, and takes the time until the last one exits. It drives a scripted session (the tool calls above, a growing transcript, real edits in a git checkout with a remote) against a stub server that answers every op after a fixed delay and reports the checkout as indexed, so the gates make their requests. A preload logs each node process at start and exit, which gives the process counts; the stub's log gives the requests. Before is main's plugin directory (git archive origin/main integrations/claude-code-plugin), after is this branch's: node tests/bench/hook-wall-time.mjs --plugin <dir> --latency 300 --tools 4 --stops 1 --runs 5, and --tools 55 --stops 3 --runs 3 for the long shape.
  • node --test on tests/record-hook.test.mjs (new), tests/hook-contract.test.mjs, tests/flush-job.test.mjs, tests/hooks-json.test.mjs, tests/loom-client-status.test.mjs and tests/hook-sandbox.test.mjs under integrations/claude-code-plugin: 43 tests pass; the four other files that use the changed client functions, 34 pass. After the merge of main, these files with tests/run-id-stash.test.mjs and main's tests/test-gate.test.mjs: 101 pass, 1 skipped. The launcher's cases: a tool call's hook returns while the server has answered nothing, the observation still arrives, and the job file is gone once the scripts have returned; with no credential nothing is handed off; a copy that dies leaves its job file and no cursor, and the documented replay delivers it; one Stop hand-off carries the crystallize, usage, working-memory and conversation writes; the SessionEnd hook returns with run.complete and observe.crystallize sent and nothing handed off, and memory.set_from_messages, conversation.append and session.brief_save follow; the scripts of one process look the checkout up once; the hand-off outlives a kill of the process group that ran the hook. Also: jobs handed off in one millisecond get files of their own; the status check counts both job names; a recording script is never wired beside the launcher; the Stop gates stay wired directly. The contract test runs the launcher under every payload and server condition, among them a host that never closes stdin, and still runs each recording script started on its own.
  • Each new case fails on a scratch copy with its fix taken out: SessionEnd handed off; the job stamp taken from the clock alone; no credential check; no single scope lookup; the hand-off under the flusher's name; the status check counting the flusher's name only.
  • The sender census (tests/unit/session_content_senders.json) pins the hash of each file that holds a reviewed sender. The pins of lib/loom-client.mjs and lib/flush-job.mjs are renewed in both plugins: the census finds the same sites, and no send call or body builder changed. Its assertions, the loom doctor stranded-job case, the Codex plugin's wiring assertions and the public-copy lint pass when run as plain scripts.
  • ruff format --check, ruff check, the function-length and secrets gates, every generator with --check (the Codex copies included), Biome, Prettier, node --check on every plugin script, claude plugin validate: clean. The mypy gate reports errors only in server/llm/vertex_auth.py, a file this change does not touch, from untyped imports in the local environment.
  • Not run locally: any pytest suite, the other node test files, and claude plugin eval for the changed loom-status reference (it calls a model). CI runs the suites.

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. Checked by hand: the before and after measurement per event and per session, that the same requests reach the server, the node test files above, each new test against the fault it guards, and the trace of the SessionEnd requests and git calls.

  • 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.

🤖 Generated with Claude Code


Note

Medium Risk
Changes hook timing and SessionEnd wiring (capture cursors/races, possible duplicate conversation turns); job files may briefly hold unredacted tool I/O locally, but server wire and auth are unchanged.

Overview
Introduces hooks/loom-record.mjs as a single entry for Claude Code recording hooks (loom-capture, conversation, usage, working memory, run close, session memory/brief). PostToolUse, PostToolUseFailure, and Stop now write the payload to a private loom-record-* job file, spawn one detached child that runs the scripts in one process via runHooksTogether, and return immediately so the client does not wait on network I/O. SessionEnd collapses five wired hooks to one launcher that runs the same scripts in-process until run.complete and the other exit records are sent.

lib/flush-job.mjs generalizes detached spawning (spawnDetached, jobPath with millisecond collision avoidance); lib/loom-client.mjs adds shared-payload/stdin handling, cached resolveScope for multi-script runs, and coordinated exit scheduling. loom-run reads the recalled-key stash before SessionEnd work beside loom-session-memory.

Docs, loom-status, and loom-client-status now describe stranded loom-record-*.json replay and temp-file privacy. Plugin version 1.2.64. Codex keeps per-script wiring (launcher documented but not hooked). Adds bench and record-hook tests.

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

… wait on them

A hook that only sends data prints nothing the host reads, yet the host
waited for its process: the tool-call capture stayed until the server
answered its post, and at Stop and SessionEnd the conversation hook slept
its 350 ms settle inside a process the host waits for. Seven processes ran
at Stop and five at SessionEnd.

Claude Code now starts one launcher, hooks/loom-record.mjs, after a tool
call, at Stop and at SessionEnd. It writes the payload to a private job
file, starts one detached copy of itself on it, and exits. The copy runs
the event's recording scripts in one process, each as if the host had
started it (lib/loom-client.mjs, runHooksTogether), then removes the job
file. The job file is on disk before the hook returns and the copy has its
own session and no inherited stdio, so the hand-off outlives the hook and
the host.

The gates and the scripts that inject context are wired as before: the
host reads what they print. PreCompact, the run's opening at the first
prompt, and the Codex and generic wirings still start each script
themselves.

Measured offline against a stub server that answers after 300 ms, hooks
started as Claude Code starts them, for a session of 55 tool calls and
three stops: hook wall time 39.3 s before, 20.6 s after. A tool call's
recording hook takes 70 ms where it took 463 ms, SessionEnd 76 ms where it
took 497 ms. Stop still waits for its slowest gate (503 ms where the pass
took 528 ms; 213 ms where it took 503 ms against a server with no delay).
The same requests reach the server.

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 2:48am UTC
loom-telemetry Ready Ready Preview Nov 28, 2026 2:48am UTC

Request Review

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…s replayed under its own name

A host that is torn down when the client exits (a container whose
entrypoint is the client, a CI runner that kills what a job leaves
behind) ended the detached copy of the SessionEnd hand-off before it had
sent anything: the launcher returned with no request made, where the
run's close used to be awaited. At SessionEnd the launcher now runs the
five recording scripts in its own process and exits when they have
returned; a tool call and a Stop are still handed off. The docs say what
a hand-off survives (the hook and the client's process) and what it does
not (the machine).

Five scripts in one process each asked git for the same scope, one after
the other in one turn of the event loop, so the first script's request
left only after the last script's lookups: 17 git calls before the run's
close was sent. Scripts that share a process now look a directory up
once (5 calls).

A handed-off payload was named like a flusher job, so the status check
counted a stranded one and the documented replay, the flusher, found no
job in it and deleted it. It is now loom-record-<Event>-<ms>-<pid>.json:
the client status check and `loom doctor --client` count it, and the
status reference names its replay, `loom-record.mjs <Event> - <file>`.

Also: two jobs one process hands off in the same millisecond no longer
share a file; with no credential the launcher starts nothing after a
tool call; the launcher's order comment says when the recalled keys can
be missed; the docs say the hand-off file holds the payload as the host
sent it; the stdin reader is back in lib/loom-client.mjs, which the
launcher imports anyway (under 1 ms).

The hook wall-time figures come from
integrations/claude-code-plugin/tests/bench/hook-wall-time.mjs, now in
the tree: `node tests/bench/hook-wall-time.mjs --plugin <plugin dir>`
drives a plugin directory's hooks.json as the client does against a stub
server, and reports the time per event, the processes waited on and
detached, and the requests sent.

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

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1c35b20. Configure here.

@zlareb1 zlareb1 changed the title Recording hooks hand their work off and return, so a session does not wait on them Recording hooks hand off after a tool call and at Stop, and run in one process at SessionEnd Nov 28, 2026
zlareb1 and others added 4 commits November 28, 2026 07:49
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…ith stdin left open

loom-run read the recalled-key stash after the run.begin it asks again at
SessionEnd. loom-session-memory runs beside it and removes the stash, so
that run closed without recalled_keys. The stash is now read before any
request is awaited.

The SessionEnd launcher left stdin open after reading the payload. A host
that never closes it held the process through the exit grace, three to
eight seconds after the records were sent. The launcher now closes its
end once the payload is read.

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>
Both plugin manifests stay at 1.2.64, one patch above main's 1.2.63.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1
zlareb1 merged commit 46b441f into main Nov 28, 2026
82 of 86 checks passed
@zlareb1
zlareb1 deleted the fix/plugin-hooks-do-not-hold-the-session branch November 28, 2026 03:02
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