Repository navigation
Description
Found by the agent fixing #277, while establishing why dispatch() sends hook stderr to /dev/null. #277 is about protecting one stream; this is the other stream, already unprotected.
What happens
scripts/user-prompt-hook.sh:130-161 runs dispatch "after_user_prompt" inside a command substitution:
CTX=$( … dispatch "after_user_prompt" … )and CTX becomes additionalContext — text handed straight to the model. So anything an installed hooks.d/after_user_prompt/ hook writes to stdout lands in the prompt context verbatim.
Two consequences, and the second is the sharper one:
- Arbitrary text into the model's context. A hook that prints a stray debug line, a progress indicator, or a
set -xtrace injects it into every prompt. - It competes with the
systemMessageenvelope built below it. A hook writing something JSON-shaped is writing into the same structure the plugin uses to speak to the model in its own voice — so hook output can impersonate the plugin's own framing rather than merely appear alongside it.
Why this is worth filing rather than noting
#277 exists because a hook's stderr was discarded, so a fatal diagnostic reached nobody. This is the mirror image on stdout: a hook's output reaches the model, unbounded and unlabelled, with nothing marking where plugin text ends and hook text begins.
The ownership and world-writable guards in dispatch() mean a hook must be owned by the current user and not group- or world-writable, which is a real bound — this is not a remote-attacker path. It is a blast-radius problem: a local hook the user installed for one purpose can silently change what the model is told, and neither the user nor the model can see that it did.
Not proposing the fix
The obvious moves each carry a cost worth weighing rather than assuming:
- Discard hook stdout — kills the
after_user_promptcontract entirely; that event exists to contribute context. - Bound and label it — the honest option, and it needs the same care
dispatchdiscards a dying hook's stderr and reports it as a namelesshook failed, so a fatal hook error reaches nobody #277 needed: a cap that does not disclose its own truncation is another instance of this project's house defect. - Structure it — give hooks a declared channel (a file, a fenced block) rather than raw stdout, so plugin text and hook text are distinguishable by construction.
Which of those is right depends on what the after_user_prompt contract is meant to promise, and that is a decision rather than a repair.
Related, from the same review
- No timeout on any dispatched hook. They run sequentially in the foreground of
PostToolUse; one that hangs stalls the agent indefinitely and logs nothing, because nothing has failed. - The ownership / world-writable skips warn only to the daily log, not to
hook-errors.log— so a hook that never runs and never will is invisible to/remember:doctor, which reports OK.
Activity
test(#280): skip the hook-stdout suite on Windows, like the module it…
fdaviddpt commentedon Aug 1, 2026
Correcting my own framing on this issue, now that it is fixed: the "impersonate the systemMessage envelope" half — which is in the title — does not hold.
The agent that fixed it re-derived the
CTX=$(…)construct (correct) and then disproved the envelope claim rather than inheriting it:jq -Rs --arg msg, which JSON-encodes bothCTXand the notice. A hook cannot close a string or introduce a key.printf '%s\n' "$CTX").CTXalways begins with the plugin's own[HH:MM TZ — user]line, so the payload is never parseable as JSON regardless of what a hook prints.So there was no structural escape available, and the fix does not claim one.
What is real, and was enough on its own: impersonation one level up, in the text. A hook printing
WARNING: Context at 99%. Run /remember…produces a line byte-identical to oneuser-prompt-hook.shwrites itself — same stream, same position, no way for the reader to tell them apart.Second correction: the surface was two events, not one. I named only
user-prompt-hook.sh.session-start-hook.shinherits dispatch stdout straight into the session's opening context — that is what the documented=== TEAM ===listener is for. So the fix went intodispatch(), the shared chokepoint, and covers both.The contract that shipped, for anyone reading this later: an unprefixed line in dispatched output is the plugin speaking, and a hook cannot produce one. Every hook line is prefixed
[hook]; the block is framed by an unprefixed=== hooks.d: <event>/<script> … ===. The two options I offered — "bound and label" versus "declared channel" — collapse into one, because a fenced block is only a channel if the hook cannot print the closing marker, and it can. The boundary has to be per-line.A silent hook still produces nothing: no frame, no marker, byte-identical output on the every-prompt path.
Fixed by #284. Also folded in there: the ownership/world-writable skips now write to
hook-errors.logas well as the daily log, so a hook that never runs is no longer invisible to/remember:doctor— the two defects shared nine lines of the same function.A dispatched hook's stdout is attributed, bounded and disclosed (Digi…
test(#898): give fake plugin roots the install manifest resolve-paths…
fix(#898): release tree scrub + 5 new guards (typed-heredoc shipped a…
fix(#900): compile hooks.json-registered hooks self-contained at buil…