Skip to content

fix(RepeatDetection): use stdout+exit 0 to inject warning as additionalContext#1306

Open
J8MAE wants to merge 1 commit into
danielmiessler:mainfrom
J8MAE:fix/repeat-detection-userpromptsubmit-contract
Open

fix(RepeatDetection): use stdout+exit 0 to inject warning as additionalContext#1306
J8MAE wants to merge 1 commit into
danielmiessler:mainfrom
J8MAE:fix/repeat-detection-userpromptsubmit-contract

Conversation

@J8MAE
Copy link
Copy Markdown

@J8MAE J8MAE commented May 26, 2026

Summary

RepeatDetection.hook.ts (UserPromptSubmit hook) writes its warning to stderr and exits with code 2, which is the Claude Code hook block pattern — it prevents the user's prompt from being processed and surfaces the warning text to the user.

The desired behavior, per the hook's own docstring ("injects a high-priority WARNING into the model's context"), is the additionalContext pattern: write to stdout and exit with code 0. That signals Claude Code to inject the text into the model's context for the next turn, while letting the prompt proceed.

Bug observed

When the hook triggered on a repeated prompt:

  1. The user's repeated prompt was blocked (the user is the one repeating, so blocking them doesn't help).
  2. The "⚠️ REPEAT DETECTION" warning surfaced to the user instead of to the model.
  3. The model never received the signal it was supposed to use to re-read the message.

Fix

Two-line change in Releases/v5.0.0/.claude/hooks/RepeatDetection.hook.ts:

  • process.stderr.write(...)process.stdout.write(...)
  • process.exit(2)process.exit(0)

Comments updated to reflect the corrected semantics. No other behavior changed; the detection logic, threshold (0.6 jaccard similarity), and warning text are unchanged.

Reference

Claude Code UserPromptSubmit hook contract: stdout + exit 0 = additionalContext injected for the model; stderr + exit 2 = block + surface to user. The latter is intended for security/policy violations where the prompt should not proceed.

The UserPromptSubmit hook contract is that the hook can return text via
stdout + exit 0, which Claude Code injects as `additionalContext` into the
model's context. stderr + exit 2 is the BLOCK pattern — it prevents the
prompt from being processed and surfaces the message to the user instead
of the model.

Before this fix, when RepeatDetection triggered:
- The user's repeated prompt was blocked
- The "⚠️ REPEAT DETECTION" warning surfaced to the user (who already knows
  they repeated; they're the one repeating)
- The model never received the signal it was supposed to act on

After this fix:
- The user's prompt is allowed to proceed
- The warning is injected into the model's context as additionalContext
- The model receives the signal to STOP and re-read the user's message

Two-line change: stderr→stdout, exit(2)→exit(0). No other behavior changed.
@brycemagera
Copy link
Copy Markdown

Great callout! I opened #1291 for this as well.

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.

2 participants