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 <thedotmack@users.noreply.github.com>
This commit is contained in:
claude[bot]
2025-12-06 22:30:14 +00:00
parent 2e67821445
commit 85f30126aa
+6 -7
View File
@@ -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",