From 85f30126aad8ab706a77d023ecd33aee2eb656de Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Sat, 6 Dec 2025 22:30:14 +0000 Subject: [PATCH] fix: Remove stderr output from context hook to fix session start injection The dual stderr/stdout pattern was breaking context injection in Claude Code sessions. Claude Code expects clean JSON on stdout only. Stderr output was interfering with JSON parsing, causing context to fail loading even though the hook worked from CLI. Changes: - Remove process.stderr.write() when running as hook - Add --colors flag support (matching main branch behavior) - Output only JSON to stdout for hook execution - Keep formatted output for manual terminal runs (TTY or --colors) Co-authored-by: Alex Newman --- src/hooks/context-hook.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/hooks/context-hook.ts b/src/hooks/context-hook.ts index 7738f4b2..d82150a2 100644 --- a/src/hooks/context-hook.ts +++ b/src/hooks/context-hook.ts @@ -74,24 +74,23 @@ async function contextHook(input?: SessionStartInput): Promise<{ unformatted: st export { contextHook }; // Entry Point - handle stdin/stdout -if (stdin.isTTY) { +const forceColors = process.argv.includes('--colors'); + +if (stdin.isTTY || forceColors) { // Running manually from terminal - show formatted output contextHook(undefined).then(({ formatted }) => { console.log(formatted); process.exit(0); }); } else { - // Running from hook - formatted to stderr (user display), unformatted to stdout (model context) + // Running from hook - output only JSON to stdout (no stderr) let input = ''; stdin.on('data', (chunk) => input += chunk); stdin.on('end', async () => { const parsed = input.trim() ? JSON.parse(input) : undefined; - const { unformatted, formatted } = await contextHook(parsed); + const { unformatted } = await contextHook(parsed); - // Write formatted version to stderr for user display - process.stderr.write(formatted + '\n'); - - // Write unformatted version to stdout as JSON for model context + // Output JSON result to stdout - no stderr output const result = { hookSpecificOutput: { hookEventName: "SessionStart",