Skip to content

fix(hooks): emit suggest-compact via hookSpecificOutput stdout - #1

Open
richm-spp wants to merge 2 commits into
mainfrom
fix/suggest-compact-additionalContext
Open

fix(hooks): emit suggest-compact via hookSpecificOutput stdout#1
richm-spp wants to merge 2 commits into
mainfrom
fix/suggest-compact-additionalContext

Conversation

@richm-spp

Copy link
Copy Markdown
Owner

Problem

suggest-compact.js was emitting compaction nudges via console.error (stderr) only. Claude Code 2.1.x PreToolUse hooks require structured JSON on stdout via hookSpecificOutput for 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 retaining console.error for debug logging.

Tests

4 new test cases added; suite expands from 19 → 23 (all passing):

  • stdout JSON emitted at threshold
  • stdout JSON emitted at +25 interval
  • silence (no stdout) below threshold
  • stderr debug log retained alongside stdout output

richm-spp added 2 commits May 15, 2026 09:48
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).
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