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:
@@ -74,24 +74,23 @@ async function contextHook(input?: SessionStartInput): Promise<{ unformatted: st
|
|||||||
export { contextHook };
|
export { contextHook };
|
||||||
|
|
||||||
// Entry Point - handle stdin/stdout
|
// 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
|
// Running manually from terminal - show formatted output
|
||||||
contextHook(undefined).then(({ formatted }) => {
|
contextHook(undefined).then(({ formatted }) => {
|
||||||
console.log(formatted);
|
console.log(formatted);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
});
|
||||||
} else {
|
} 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 = '';
|
let input = '';
|
||||||
stdin.on('data', (chunk) => input += chunk);
|
stdin.on('data', (chunk) => input += chunk);
|
||||||
stdin.on('end', async () => {
|
stdin.on('end', async () => {
|
||||||
const parsed = input.trim() ? JSON.parse(input) : undefined;
|
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
|
// Output JSON result to stdout - no stderr output
|
||||||
process.stderr.write(formatted + '\n');
|
|
||||||
|
|
||||||
// Write unformatted version to stdout as JSON for model context
|
|
||||||
const result = {
|
const result = {
|
||||||
hookSpecificOutput: {
|
hookSpecificOutput: {
|
||||||
hookEventName: "SessionStart",
|
hookEventName: "SessionStart",
|
||||||
|
|||||||
Reference in New Issue
Block a user