Repository navigation
A dispatched hook's stdout is attributed, bounded and disclosed (#280) - #284
Merged
Merged
Conversation
…280) dispatch()'s stdout is its caller's stdout, and two callers hand that straight to the model: user-prompt-hook.sh wraps the dispatch in CTX=$( … ) and delivers it as additionalContext on every prompt, and session-start-hook.sh prints it into the session's opening context. A listener's output was therefore never diagnostics — it was text the model reads, in the same stream and position as the plugin's own, unbounded and unattributed. The contract is now one sentence: an unprefixed line in dispatched output is the plugin speaking, and a hook cannot produce one. Every line a listener writes is prefixed "[hook] " and the block is framed by an unprefixed "=== hooks.d: <event>/<script> ===" line. Per-line attribution rather than a fence: any fence is only as good as the hook's inability to print its closing marker, and there is no outside to escape into when the boundary is on every line. Bounded at 200 lines / 2000 chars per line, and the cap says what it dropped. A silent listener produces no frame and no marker, so the every-prompt path is unchanged. Where stdout cannot be captured it is discarded and said so, never inherited unattributed. Also: the ownership and world-writable skips now report to hook-errors.log as well as the daily log, so a hook that never ran and never will is no longer reported as OK by /remember:doctor. Not remote-attacker reachable — dispatch()'s guards require the hook be owned by the invoking user and not group/world-writable. This is blast radius, not a vulnerability. Left open: no dispatched hook has a timeout. Closes #280 Co-Authored-By: Max <noreply>
… borrows from On windows-latest a bare `bash` resolves to the WSL stub in System32, which prints a UTF-16 notice and exits 1 before reading the script. tests/test_path_resolution.py carries a win32 skipif for exactly this; this file imports its helpers and needs the same marker. Four Windows legs red, eight green, and the production change was never involved. Co-Authored-By: Max <noreply>
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.
Closes #280
The premise, re-derived rather than quoted — and one correction
scripts/user-prompt-hook.shrunsCTX=$( … dispatch "after_user_prompt" )andCTXbecomesadditionalContext. Confirmed. Butafter_user_promptis not the only mouth:session-start-hook.shcallsdispatch "before_session_start"anddispatch "after_session_start"with stdout inherited straight into the session's opening context — that is exactly what the documented=== TEAM ===listener is for. So the surface is two events, not one, and the fix is indispatch()rather than inuser-prompt-hook.sh.The correction. The issue says hook output "competes with the
systemMessageenvelope" and can write "into the same structure the plugin uses". At the JSON level it cannot, and that matters for scoping. The envelope is built withjq -Rs --arg msg, which JSON-encodes bothCTXand the notice; a hook cannot close a string or add a key. On the far more common path there is no JSON at all —printf '%s\n' "$CTX"— andCTXalways begins with the plugin's own[HH:MM TZ — user]line, so the payload is never parseable as JSON no matter what a hook prints. No structural escape is available, and this PR does not claim one.What is real is impersonation one level up, in the text:
additionalContextis the plugin's voice to the model, and a hook writingproduces a line byte-identical to one
user-prompt-hook.shwrites itself, in the same stream, in the same position. Nothing in the context tells them apart. That is the defect, and it is enough of one.The judgment call
The three options in the issue, and why the middle two collapsed into one.
Discard stdout — rejected.
after_user_promptandafter_session_startexist so a listener can contribute context. A fix that deletes what a hook says is this codebase's own defect wearing the fix's clothes. What was wrong was never that hooks speak; it is that nobody could hear who was speaking.Bound and label / give hooks a declared channel — these are the same fix, and it is the one shipped. Labelling done properly is the declared channel: a fenced block is a channel only if the hook cannot print the fence's closing marker. It can. So the boundary is per-line rather than per-block.
The contract, stated for the hook author who will rely on it:
Every line a listener writes is prefixed
[hook]; the block is framed by an unprefixed=== hooks.d: <event>/<script> … ===line naming the script. A listener that prints a convincing terminator gets the terminator prefixed too — there is no outside to escape into when the boundary is on every line. Documented in README under What ahooks.d/listener may say, and in whose voice.The cap discloses itself, per #277's lesson: 200 lines, 2000 chars per line, and a trailing frame line carrying the count of what was not shown. Both numbers sit far above any honest contributor (the two shipped listeners print nothing at all) and far below a
set -xtrace.Silence stays free. A listener that prints nothing gets no frame and no marker — the every-prompt path is unchanged. Asserted, because a fix that taxed the common case in context tokens forever would be a poor trade for a rare one.
Where stdout cannot be captured, it is discarded and said so. With no writable
tmp/there is no capture file, and uncaptured stdout is inherited stdout — precisely the injection being fixed. That path runs the hook with stdout closed and prints a frame line saying the output was not shown and why. Neither quietly passed through nor quietly dropped.Scope, honestly
Not a remote-attacker path and not presented as one.
dispatch()requires a hook to be owned by the invoking user and not group- or world-writable. This is blast radius: a hook installed for one purpose could change what the model is told, and neither the user nor the model could see that it had.The two related findings — one in, one out, and I am saying which
(b) Skip warnings invisible to
/remember:doctor— IN. The ownership and world-writable skips warned only to the daily log.doctor.shreports "Recent errors" out ofhook-errors.log, so a listener that never ran and never would — onechmod 777away — showed up as OK. Both now report through the same two-destination path as a failure, naming the hook, the reason, and that it will not run on any later dispatch. It ships here rather than as a follow-up because the two defects share nine lines of the same function; splitting them would have meant touchingdispatch()twice in a week.(a) No timeout on any dispatched hook — OUT, please file it. Real, and a design rather than a repair. Killing a listener mid-flight can leave a
.git/index.lockbehind in the very backup hook this plugin ships; and a default generous enough for a network push while short enough to matter is its own judgement with its own blast radius. It deserves an issue, not a paragraph at the end of this one.TDD
Tests first,
tests/test_hook_stdout_labelling_280.py.RED — 6 failed, 1 passed. The one that passed against untouched code is
test_a_hook_that_prints_nothing_adds_nothing, which asserts absence — that is what it is for, and it is the guard against over-framing the common case.GREEN — 7 passed. Full suite: 1267 passed, 43 skipped, coverage 94.16%.
The load-bearing test hands a hook the plugin's own warning verbatim and asserts that nothing in the plugin's voice carries it — not that a marker appears somewhere. A build that printed the marker and then relayed the hook's text unlabelled anyway would satisfy "marker present" and fail this. The forgery test does the same for the frame: a hook prints a plausible closing frame and continues in the plugin's voice, and the assertion is that exactly one unprefixed frame line exists in the output.
Every test drives the real
user-prompt-hook.shthrough a full plugin copy with a real installed hook — no stubbing ofdispatch, no asserted-against-fixture output.