Repository navigation
Recording hooks hand off after a tool call and at Stop, and run in one process at SessionEnd - #1677
Merged
Merged
Conversation
… 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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…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>
There was a problem hiding this comment.
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).
❌ 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.
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>
9 of 10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Changelog category (leave one):
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:
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
mainthe 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 onmain, 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:
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):
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 thanmain, 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 onmainin 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 askscode.test_gate.Scope and risk
hooks/loom-record.mjsis 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.run.completeis answered andobserve.crystallizeis sent, as onmain; session memory, the brief and the last turns go through the flusher, as onmain, and a host torn down in that instant can lose what the flusher had not sent. A test fails if the hook returns beforerun.completeandobserve.crystallizehave reached the server, or if anything is handed off.spawnDetachedinlib/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.bin/loom-client-status.mjs) andloom doctor --clientcount it beside the flusher's, and theloom-statusreference 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.runHooksTogetherinlib/loom-client.mjsimports the scripts while a module flag is set:runHookMainkeeps each main instead of starting it,readStdinJsonanswers the payload,resolveScopelooks a directory up once, andscheduleExitdoes 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-runended its SessionEnd withprocess.exit(0); it now asksscheduleExit(0, 0), which exits a script started on its own on the next timer tick and leaves a shared process to its launcher.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.loom-runreads the recalled-key stash at the start of its SessionEnd branch, before therun.beginit may ask again:loom-session-memoryremoves the stash beside it, and a re-asked run closed withoutrecalled_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).msg_idmakes the resend idempotent. On a loaded machine this happened in 2 of 16 sessions and in none onmain, where the Stop pass itself waited out the settle.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
main's 1.2.63).Tests
integrations/claude-code-plugin/tests/bench/hook-wall-time.mjsreads a plugin directory'shooks/hooks.json, starts every hook an event wires asnode <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 ismain'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 3for the long shape.node --testontests/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.mjsandtests/hook-sandbox.test.mjsunderintegrations/claude-code-plugin: 43 tests pass; the four other files that use the changed client functions, 34 pass. After the merge ofmain, these files withtests/run-id-stash.test.mjsandmain'stests/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 withrun.completeandobserve.crystallizesent and nothing handed off, andmemory.set_from_messages,conversation.appendandsession.brief_savefollow; 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.tests/unit/session_content_senders.json) pins the hash of each file that holds a reviewed sender. The pins oflib/loom-client.mjsandlib/flush-job.mjsare renewed in both plugins: the census finds the same sites, and no send call or body builder changed. Its assertions, theloom doctorstranded-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 --checkon every plugin script,claude plugin validate: clean. The mypy gate reports errors only inserver/llm/vertex_auth.py, a file this change does not touch, from untyped imports in the local environment.claude plugin evalfor the changedloom-statusreference (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
make test-unitand applicable integration results are reported by CI; a full local run is not required before pushing.docs/reference/configuration.mdand thedocs/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).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).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.mjsas 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 privateloom-record-*job file, spawn one detached child that runs the scripts in one process viarunHooksTogether, 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 untilrun.completeand the other exit records are sent.lib/flush-job.mjsgeneralizes detached spawning (spawnDetached,jobPathwith millisecond collision avoidance);lib/loom-client.mjsadds shared-payload/stdin handling, cachedresolveScopefor multi-script runs, and coordinated exit scheduling.loom-runreads the recalled-key stash before SessionEnd work besideloom-session-memory.Docs,
loom-status, andloom-client-statusnow describe strandedloom-record-*.jsonreplay 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.