Skip to content

fix(hooks): RepeatDetection injects warning via stdout, not stderr+exit 2#1291

Open
brycemagera wants to merge 1 commit into
danielmiessler:mainfrom
brycemagera:fix/repeat-detection-exit-code
Open

fix(hooks): RepeatDetection injects warning via stdout, not stderr+exit 2#1291
brycemagera wants to merge 1 commit into
danielmiessler:mainfrom
brycemagera:fix/repeat-detection-exit-code

Conversation

@brycemagera
Copy link
Copy Markdown

Problem

RepeatDetection.hook.ts is a UserPromptSubmit hook. On a >=60% similarity match it calls process.stderr.write(...) then process.exit(2).

For UserPromptSubmit hooks, exit code 2 blocks and erases the prompt, and routes stderr to the user only -- the model never receives the message. Two consequences:

  1. The repeat-detection warning the hook is designed to inject into the model's context never arrives there.
  2. The user's resent message is silently dropped -- the model never sees it.

The file header comment states the hook "injects a high-priority WARNING into the model's context forcing re-reading of the user's message." The implementation does the opposite of that intent.

Fix

The correct channel for UserPromptSubmit context injection is exit 0 with the text on stdout -- stdout from a UserPromptSubmit hook is appended to the model's context, and the prompt proceeds normally.

This swaps process.stderr.write -> process.stdout.write and process.exit(2) -> process.exit(0) in the trigger branch. Detection logic (the 0.6 threshold, tokenizer, and trigram similarity) is unchanged. 7 insertions, 4 deletions, one file.

Testing

  • bun build on the hook: clean.
  • Behavioral: with the fix, a >=60%-similar follow-up arrives intact with the warning prepended as context -- verified via synthetic UserPromptSubmit stdin and live in a Claude Code session. Before the fix, the same message was erased before reaching the model.

Reference

Claude Code hooks documentation, UserPromptSubmit exit-code semantics: exit 2 blocks/erases the prompt with stderr shown to the user only; exit 0 stdout is added to the model's context.

…it 2

RepeatDetection.hook.ts is a UserPromptSubmit hook. On a >=60% similarity match it called process.stderr.write() then process.exit(2).

For UserPromptSubmit hooks, exit code 2 blocks AND erases the prompt, and routes stderr to the user only -- the model never receives the message. So the repeat-detection warning the hook is meant to inject into the model's context never arrives, and the user's resent message is silently dropped.

The correct channel for UserPromptSubmit context injection is exit 0 with the text on stdout: stdout is appended to the model's context and the prompt proceeds normally.

Swaps process.stderr.write -> process.stdout.write and process.exit(2) -> process.exit(0) in the trigger branch. Detection logic (threshold, tokenizer, trigrams) is unchanged.
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