fix(hooks): emit suggest-compact via hookSpecificOutput stdout - #1
Open
richm-spp wants to merge 2 commits into
Open
fix(hooks): emit suggest-compact via hookSpecificOutput stdout#1richm-spp wants to merge 2 commits into
richm-spp wants to merge 2 commits into
Conversation
The threshold and interval suggestions in suggest-compact.js are written
to stderr via log(). Per the Claude Code hooks guide, non-blocking
PreToolUse stderr (exit code 0) is only captured in the debug log — it
does not reach the model. As shipped, the script's nudge to /compact is
silent on Claude Code 2.1.x.
Fix: alongside the existing log() call (kept for debug-log capture),
emit the same suggestion as structured JSON on stdout:
{ hookSpecificOutput: {
hookEventName: "PreToolUse",
additionalContext: msg
} }
This is the documented mechanism for a PreToolUse hook to inject
context into the next model turn without blocking the tool call.
Verified end-to-end on Claude Code 2.1.142 (VSCode native extension,
Windows 11) — the additionalContext now surfaces in the next turn as
a <system-reminder> block. Counter increment and exit code behavior
unchanged.
Tests: 4 new cases in tests/hooks/suggest-compact.test.js covering
stdout JSON at threshold, stdout JSON at +25 interval, silence below
threshold, and stderr-retention for the debug log. Suite goes from
19/19 -> 23/23 (suggest-compact) and full run-all stays clean for
the unaffected suites (the 4 pre-existing Windows broken-symlink
failures in ci/validators, lib/session-manager, and lib/utils are
unrelated to this change).
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.
Problem
suggest-compact.jswas emitting compaction nudges viaconsole.error(stderr) only. Claude Code 2.1.x PreToolUse hooks require structured JSON on stdout viahookSpecificOutputfor the nudge to surface in the UI — stderr alone is silently ignored for hook feedback.Fix
Switch to
process.stdout.write(JSON.stringify({ hookSpecificOutput: ... }))for the user-facing nudge, while retainingconsole.errorfor debug logging.Tests
4 new test cases added; suite expands from 19 → 23 (all passing):