Skip to content

A prompt does not wait on run.begin, and a Stop does not wait on the namespace audit - #1695

Merged
zlareb1 merged 6 commits into
mainfrom
fix/plugin-hooks-session-start-and-stop-wait
Nov 28, 2026
Merged

zlareb1 merged 6 commits into
mainfrom
fix/plugin-hooks-session-start-and-stop-wait

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 no longer holds a prompt for run.begin or a Stop for the namespace audit: a prompt starts one hook process instead of three, and the audit is asked by the first prompt's detached process and told at a Stop.

Why

Two measured stages of short headless coding sessions with the plugin installed, in which the model called no Loom tool, put the plugin's hooks at 3.2 to 4.5 s of wall time per session. The earlier hand-off of the tool-call and Stop recorders did not lower that. This change removes two waits on calls whose answer the session does not read:

  • A prompt started three node processes, one of them only to send run.begin. run.begin and the audit now go to one detached process, and the two lookups whose answer the prompt waits for (loom-prompt-pack, loom-prompt-recall) run in the launcher itself, which prints their context as one answer.
  • The first Stop waited for namespace.audit (469 ms median over 15 first passes, 193 ms of it in the server), which printed nothing in 29 sessions. The detached process of the session's first prompt now asks and stores the verdict, and the Stop hook reads it.

What this change leaves alone, and why:

  • SessionEnd still runs its five recording scripts in the hook, with timeout: 10, as on main. A host that ends with the client (a container whose entrypoint is the client, which the documented headless claude -p use allows) would end a detached process before it had sent the run's close, the last turns, session memory or the brief. An earlier revision of this pull request handed SessionEnd off and lost exactly that; the review found it, and this revision restores main's path and its tests.
  • run.begin was not what the first request waited for. In all 15 measured sessions a lookup (code.search 8 times, context.pack 7) answered after run.begin did, by a median of 169 ms. Both lookups can inject, so the prompt still waits for them.
  • SessionStart already waits only for reads that feed its primer.
  • The Stop pass is as long as its slowest gate. In 7 of 10 unblocked first passes the test gate took longer than the namespace audit.

Scope and risk

  • Touched: hooks/loom-record.mjs (hands off at a prompt and runs its two lookups; SessionEnd as on main), hooks/hooks.json (one prompt hook), hooks/loom-namespace-health.mjs and lib/namespace-health.mjs (ask at a prompt, tell at a Stop), hooks/loom-run.mjs and lib/run-hook.mjs (the launcher writes the pending begin to the run state before it starts the detached process, which asks only the begin it finds there; an answer that arrives after another writer took the state is dropped), lib/loom-client.mjs (a hook's main returns its answer; scripts in one process share the checkout-root lookup; a script run beside others reports its error under LOOM_HOOK_DEBUG=on), hooks/loom-prompt-pack.mjs and hooks/loom-prompt-recall.mjs (return their answer). Codex copies regenerated; both manifests at 1.2.66 (main is at 1.2.64 and three other open pull requests hold 1.2.65).
  • Not touched: what each hook injects or gates, the Codex and generic wirings (they start each script themselves, and loom-namespace-health still asks at their first Stop), SessionStart, SessionEnd, the test gate, the code shunt, the bench harness, the end-to-end session script.
  • Changed for the model: when both prompt lookups have context, the host receives one additionalContext (the code pointers, then the recall on the next line) where it received two.
  • Changed for the audit: the verdict is the namespace's last day as of the session's first prompt, not as of its first Stop. A Stop that comes before the audit has answered passes, and a later Stop tells the line, so a session whose only Stop precedes the answer is not told (hooks/HOOKS.md says so). A Stop that finds the ask older than twice the request bound with no verdict asks in place, as main did at the first Stop: a detached process killed between its ask and its answer does not silence the audit for the session. Twice, because the detached process stamps its ask before its checkout lookup and its request, which alone may take the bound: a live process is not asked over.
  • A session that ends before run.begin is answered. The launcher writes the begin's body to the session's run state before it starts the detached process, so the state is on disk when the prompt hook returns, as it was on main, where the prompt waited for the answer. Three orders follow, each with a test:
    • SessionEnd runs before the detached process has started (an abort right after the prompt): it finds the begin, asks it and closes the run. The detached process then finds no begin to ask and sends nothing. The server takes a begin for a closed run as opening it again, so a begin sent after the close would leave a run that nothing closes.
    • SessionEnd runs while the detached begin is unanswered: it asks again and closes the run, as it already does after a timeout, and the first answer is dropped when it arrives, because the state is no longer the attempt it belongs to (same at).
    • SessionEnd's own ask gets no answer inside its 400 ms: it stamps the state anew, so the prompt's answer is dropped when it arrives later and the state stays an attempt, which a resumed session's next prompt asks again. This is the state main leaves when the server answers neither ask. A run the server opened for an unanswered begin stays open in that case, on main and here.
    • The prompt's answer arrives during those 400 ms: it is on disk before SessionEnd's ask gives up, so SessionEnd stamps nothing over it and closes the run it opened; a refusal stays a refusal.
  • Cost of the earlier write: none in the measured session. The launcher resolves the checkout's scope for the begin's body on a session's first prompt only, and the prompt's lookups share that resolution: the prompt hook makes the same 2 git calls and 2 requests with it as without. A first prompt on which the recall does not run makes one git call more (the remote).
  • LOOM_HOOK_DEBUG=on: a script the launcher runs in the hook (the two prompt lookups, the five SessionEnd scripts), or one that fails to load, writes its error's first line to stderr, redacted, under its own name, as a script started on its own does.
  • Run ids before the answer: the prompt no longer waits for run.begin, so a first tool observation, like the SessionStart primer and the prompt's recall, can carry the derived id when the server answers another one.
  • Fail-open: every path still exits 0. A hand-off that cannot be started records nothing for that event, and a copy that dies leaves its job file for the replay.
  • SessionEnd under load, measured and not changed here. The Stop event no longer waits for the audit, so SessionEnd starts sooner, while the Stop's detached process is still at work. On a machine at 4 to 6 times its core count in load the SessionEnd hook then lived 792 ms against 646 ms on main (median of 8 alternating real-client pairs, longer in 7), while the time from the Stop to the client's exit did not differ (later in 4 pairs, earlier in 4). Claude Code 2.1.332 in -p mode ended the SessionEnd hook with SIGTERM 1.5 s after its start on main and on this branch alike (1,542 and 1,509 ms; one session each with the conversation settle stretched to 4 s, the close sent before it in both), whatever timeout the entry declares. With that budget, the longer hook life is the likely reason the client cancelled the hook in 3 of this branch's 20 ordinary sessions on the loaded machine and in none of main's 18; in 2 of the 3 the close had been sent. main's documentation says the timeout raises that budget; this change does not touch that text, and it needs its own check against the client.

Compatibility, rollout and rollback

  • Wire: none. The same ops are sent, once each, with the same bodies.
  • Operator steps: none. Clients get it with plugin 1.2.66.
  • Rollback: revert. The per-session state files the new hooks leave in the temp directory read to the old ones as a session not yet audited, or already audited.

Tests

  • node --test --test-concurrency=1 on tests/record-hook.test.mjs tests/hooks-json.test.mjs tests/namespace-health.test.mjs tests/run-id-stash.test.mjs (changed) and tests/run-hook.test.mjs tests/prompt-pack-hook.test.mjs tests/prompt-recall-hook.test.mjs tests/hooks-doc.test.mjs tests/hook-contract.test.mjs (unchanged, they cover the changed scripts): 127 of 127 pass, at a load average of 61 on 14 cores. At a load average of 120 to 190 two unchanged timing tests ("a later prompt receives changed evidence even at the same indexed span" and "a re-sent begin and the close fit Codex's one-second SessionEnd budget") have failed, about as often on origin/main as on this branch when run alone.
  • New and restored cases:
    • main's three SessionEnd and hand-off cases are back unchanged: the SessionEnd hook returns with observe.crystallize and run.complete sent and no job file left; it does not wait on a stdin its host leaves open; a tool call's hand-off outlives the process group that ran the hook. The two cases that failed on the previous head's platform runs asserted the detached SessionEnd and are gone with it.
    • A run.begin answered after SessionEnd closed the run leaves the state at {run_id}.
    • A session that ends before the prompt's detached process has started: the process is held at its start, the prompt hook returns with the begin on disk and no run. request sent, SessionEnd sends run.begin and run.complete, and the released process sends neither; the state ends at {run_id}. Without the launcher's write the hook returns with an empty run state and SessionEnd sends nothing.
    • A run.begin answered after a SessionEnd whose own ask got no answer leaves the state an attempt, not begun. Without the new stamp it ends begun with no run.complete sent.
    • A run.begin answered while SessionEnd waits on its own ask, which gets no answer: one run.complete is sent with the answered id and the state ends at {run_id}. With the stamp unguarded no run.complete is sent.
    • A script run beside others that throws, and one that does not load, each write one redacted line to stderr under LOOM_HOOK_DEBUG=on and nothing with it off. The launcher's cases run with the switch on and assert an empty stderr.
    • These four cases passed six times in a row at a load average near 55.
    • A Stop asks in place when the prompt's ask is older than twice the request bound with no verdict, once, and later Stops pass (unit and through the hook); an ask older than the bound and inside twice it passes without a call.
    • A prompt's hook prints both lookups' context as one answer while run.begin and namespace.audit are unanswered; a Stop passes while the audit is unanswered and tells the stored verdict once, with one namespace.audit in the session; a wiring with no detached process asks at the first Stop.
    • RECORDERS.UserPromptSubmit includes loom-run (in hooks-json.test.mjs, in place of a match on the launcher's source text in tests/unit/test_search_switch_ab.py), and the SessionEnd entry declares a timeout.
  • claude plugin validate integrations/claude-code-plugin --profile directory --strict and claude plugin validate .: passed.
  • Gates: ruff format --check and ruff check on the changed Python test, the function-length gate, check_no_secrets.py, agent_plugin_sync.py --check, docs_reference.py --check, op_reach_census.py --check, Biome and Prettier on the changed files, node --check on every plugin script. The mypy gate reports 5 errors, all in server/llm/vertex_auth.py, from a missing google.auth stub in the local environment.
  • Python test bodies run by snippet, not through pytest: the parameterless tests of test_search_switch_ab.py, test_codex_plugin.py, test_session_content_senders.py, test_public_copy.py, test_agent_instructions.py and test_plugin_checkout_bytes.py. The reviewed source pins in tests/unit/session_content_senders.json are renewed for the changed files and their Codex copies; no sender site changed.
  • The unit suite was not run locally.

Measurement. Before is origin/main, after is this branch. The machine was shared and at a load average of 47 to 95 on 14 cores throughout, so wall times are several times what an idle machine gives.

What the session waits for, per session. One prompt, four tool calls (three shell, one edit), one Stop and SessionEnd, driven as node tests/bench/hook-wall-time.mjs --plugin <plugin dir> --latency 50 --tools 4 --stops 1 drives them; counted in a local copy of that script with a git first on PATH and a wrapped fetch that log the calling process, over four sessions per arm in which no git call timed out (the same in all four; counted on this head):

Event Processes the session waits for git calls in them Loom requests they send
SessionStart 1 → 1 7 → 7 6 → 6
UserPromptSubmit 3 → 1 5 → 2 3 → 2
PreToolUse, 4 calls 4 → 4 6 → 6 1 → 1
PostToolUse, 4 calls 5 → 5 3 → 3 1 → 1
Stop 4 → 4 5 → 3 2 → 1
SessionEnd 1 → 1 6 → 6 2 → 2
Session 18 → 16 32 → 27 15 → 13

Detached processes go from 8 to 9, and processes in all from 26 to 25.

Wall time, node tests/bench/hook-wall-time.mjs --plugin <plugin dir> --latency 150 --tools 4 --stops 1 --runs 3, four alternating blocks per arm, the range of the blocks' medians in ms:

origin/main this branch
loom-namespace-health.mjs at Stop 1,488 to 3,832 251 to 757
UserPromptSubmit 1,131 to 2,858 1,212 to 4,444
Stop 1,879 to 4,088 1,203 to 3,505
SessionEnd 2,093 to 4,740 2,845 to 6,672
Session 13,584 to 26,658 11,008 to 31,813

These blocks ran before the launcher wrote the run's begin. The prompt hook without and with that write, six alternating blocks of three sessions at 50 ms and a load average near 55: block medians of 650 to 1,131 ms and 527 to 1,004 ms, no difference beyond the spread. Only the namespace-health hook's change is larger than the spread between blocks: the Stop event is as long as the test gate beside it. At this load the bench does not show a shorter prompt, Stop or session, and SessionEnd runs main's path. The SessionEnd hook alone, 15 alternating pairs against a stub that answers at once: 975 ms on main, 889 ms on this branch, run.complete sent before the hook returned in 15 of 15 on both.

A real client: Claude Code 2.1.332, one headless turn with one shell call, against a local fake that answers the model API and Loom at once (tests/e2e/agent-session.mjs with request times and a stub that answers run.begin with the run's id; no model provider is called), the two revisions alternating. run.complete was sent before the client exited in 18 of 18 sessions on origin/main (183 to 483 ms before the exit) and in 19 of 20 on this branch (163 to 454 ms before). In the one that missed, run while a second benchmark shared the machine, the client cancelled the SessionEnd hook before its first request. The client also reported the hook cancelled in 2 of the other 19 sessions on this branch, each after the close was sent, and in none on main. On this branch namespace.audit left with run.begin, 1.3 to 3.6 s before the exit; on main it left at the Stop, 0.7 to 1.4 s before. Those sessions ran before the launcher wrote the run's begin; one session of tests/e2e/agent-session.mjs claude on this head passed (24 requests, every hook exit 0 with nothing on stderr).

Target: under 1 s of hook time per session at 150 ms. Not met, and not reachable by handing recording off: the session still waits for four round trips one after the other (SessionStart's repository lookup and index status, the prompt's lookups, the test gate) and starts 16 node processes, 9 of them around the four tool calls.

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 recorded server log and transcripts behind each claim above, each changed script traced through one stub session before and after (processes, git calls, requests), the state transitions of the run and namespace-health files, the server's handling of a begin for a closed run (_open_run in server/tasks/app/run.py writes the row active again), real client sessions on both revisions, and the docs against the code.

  • 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
Hook orchestration and run/audit state races can affect ledger correctness and when namespace warnings appear, though behavior is heavily tested and fail-open.

Overview
Claude Code now wires a single UserPromptSubmit hook (loom-record.mjs) instead of three separate processes. The launcher runs loom-prompt-pack and loom-prompt-recall in the hook process, merges their additionalContext into one JSON line, then spawns a detached recorder for run.begin and the namespace audit so the prompt does not block on those network calls.

keepRunBegin writes the pending run.begin body to session run state before the detached job starts; loom-run and SessionEnd logic are updated so late answers do not reopen closed runs or race with SessionEnd’s own begin retry. loom-namespace-health triggers namespace.audit from the first prompt’s detached path and stores the verdict; Stop hooks mostly read that cache (with in-place audit only when the detached ask is overdue), which removes the Stop-time wait that often returned nothing.

Supporting changes include lookup hooks returning answers for orchestration, shared checkout/git caching in runHooksTogether, plugin 1.2.66, mirrored Codex sources, and docs/tests for wiring, races, and merged context.

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

…ave nothing to say

Claude Code's wiring held the session for three calls whose answer it does
not read. SessionEnd ran its five recording scripts in the hook, so the
client's exit waited for them. The first Stop waited for namespace.audit,
which prints only for a namespace with a critical finding. A prompt started
three node processes, one of them only to send run.begin.

loom-record now hands the payload to its detached copy at every event it is
wired for, SessionEnd and UserPromptSubmit included. The copy of a prompt
runs loom-run and asks namespace.audit once per session, keeping the verdict
in the session's state; loom-namespace-health at Stop tells that verdict
once and makes no call. A Stop that comes before the audit has answered
passes, and a later one tells. A wiring that starts the script at Stop only
(Codex, a hand-wired client) still asks there. The two lookups a prompt does
wait for, loom-prompt-pack and loom-prompt-recall, run in the launcher's own
process and their context goes to the host as one answer, so a prompt starts
one process.

loom-run writes the pending begin to the run state before it awaits the
answer: a SessionEnd that comes first asks again with the same body and
closes the run, as it does after a timeout. A resumed session keeps its
previous run id until the answer, as it did while the prompt waited.

A hook's main returns its answer and runHookMain prints it, so one process
can collect the answers of several scripts. Scripts that share a process
look the checkout root up once.

SessionEnd no longer declares a timeout: the hook returns after node's
start, one file write and one spawn. A host torn down when the client exits
ends the detached copy, and the session's close records with it.

The bench harness gains --shell (a session that edits and tests through the
shell alone) and each hook process's own CPU time; the end-to-end session
script expects the run's close after the client has exited.

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 11:05am UTC
loom-telemetry Ready Ready Preview Nov 28, 2026 11:05am 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.

…a late run.begin answer or a dead audit ask no longer strands session state

SessionEnd runs its five recording scripts in the hook again, with
`timeout: 10`, as on main: a host that ends with the client (a container
whose entrypoint is the client) would end a detached copy before it had
sent the run's close, the last turns, session memory or the brief. Only
the prompt's `run.begin` and the namespace audit stay handed off.

- loom-run: the prompt's begin is detached, so SessionEnd can ask again
  and close the run before the first answer arrives. That answer is now
  dropped when the run state is no longer this attempt's (same `at`);
  before, it marked the closed run begun again and a resumed session's
  next prompt opened no run.
- loom-namespace-health: a Stop that finds an ask older than the request
  bound with no verdict asks in place. A copy killed between its ask and
  its answer no longer silences the audit for the session. HOOKS.md says
  that a session whose only Stop precedes the answer is not told.
- The run-hook wiring is asserted on `RECORDERS` in hooks-json.test.mjs,
  not on the launcher's source text.
- The bench harness and the end-to-end session script are main's again:
  `--shell`, the own-CPU column and `minMs` are not part of this change.
- README: the `loom-run.mjs` row says the prompt does not wait for the
  begin's answer and that a first tool observation can precede it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1 zlareb1 changed the title A session does not wait on recording calls or on health checks that have nothing to say A prompt does not wait on run.begin, and a Stop does not wait on the namespace audit Nov 28, 2026

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

… that ends first still closes its run

The prompt's detached process was the first to record the pending
run.begin. A SessionEnd that ran before that process had started found
an empty run state and sent nothing; the process then opened the run
and marked it begun, with no close to come. The same state followed
when SessionEnd's own ask got no answer and the prompt's answer arrived
afterwards.

- loom-record writes the begin to the session's run state before it
  starts the detached process, sharing the checkout lookup with the
  prompt's lookups. The detached process asks only the begin it finds
  kept and writes nothing before its answer: after a SessionEnd has
  asked and closed the run it sends no begin, which the server would
  take as opening the run again.
- A SessionEnd whose ask gets no answer stamps the state anew, so the
  prompt's late answer is dropped and a resumed session asks again.
- A script loom-record runs in the hook, or one that fails to load,
  writes its error's first line to stderr under LOOM_HOOK_DEBUG=on, by
  script name.
- A Stop asks the namespace audit in place only when the prompt's ask
  is older than twice the request bound: the detached process stamps
  its ask before its checkout lookup and its request.
- HOOKS.md names the prompt among what a job file holds.

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 520efa6. Configure here.

zlareb1 and others added 3 commits November 28, 2026 16:11
No conflicts. main's plugin version is unchanged at 1.2.64, so both manifests stay at 1.2.66, the lowest version above it that no other open pull request uses.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…tays, and the run it opened is closed

SessionEnd asks an unanswered begin again and waits up to 400 ms. With no answer it stamped the run state anew from the copy it read before the wait, so an answer the prompt's detached process had written meanwhile was overwritten: a begun run was left with no close, and a refusal was asked again by a later prompt.

SessionEnd now stamps only a state that is still the one it read. A begun state written during the wait is read back and closed inside the same deadline.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
…r of SessionEnd's wait

The prompt's begin was answered at 900 ms, 500 ms after SessionEnd's 400 ms wait could end. An answer inside that wait is now kept and its run closed, so a SessionEnd process that started late put the test in the other ordering. Answered at 1,300 ms, still inside the prompt's 1,500 ms bound, the process again has 900 ms to start.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1
zlareb1 merged commit be63554 into main Nov 28, 2026
81 of 82 checks passed
@zlareb1
zlareb1 deleted the fix/plugin-hooks-session-start-and-stop-wait branch November 28, 2026 11:11
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