Skip to content

Commit 25d27ec

Browse files
oalanicolasclaude
andcommitted
fix: add error handling for spawn and JSON serialization in precompact hook
Addresses CodeRabbit review: handle spawn 'error' event and JSON.stringify failures to prevent unhandled exceptions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 40bf63c commit 25d27ec

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

.claude/hooks/precompact-session-digest.cjs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,29 @@ async function main() {
9090
// the runner), causing the hook to block until the 9 s safety timeout.
9191
// The child receives context via AIOX_HOOK_CONTEXT env var and calls
9292
// onPreCompact() exported by the runner module.
93-
const { spawn } = require('child_process');
94-
const inlineScript = [
95-
`const ctx = JSON.parse(process.env.AIOX_HOOK_CONTEXT || '{}');`,
96-
`const { onPreCompact } = require(${JSON.stringify(runnerPath)});`,
97-
`onPreCompact(ctx).catch(() => {});`,
98-
].join('\n');
99-
const child = spawn(process.execPath, ['-e', inlineScript], {
100-
detached: true,
101-
stdio: 'ignore',
102-
env: { ...process.env, AIOX_HOOK_CONTEXT: JSON.stringify(context) },
103-
});
104-
child.unref();
93+
try {
94+
const { spawn } = require('child_process');
95+
let contextJson;
96+
try {
97+
contextJson = JSON.stringify(context);
98+
} catch (_) {
99+
contextJson = '{}';
100+
}
101+
const inlineScript = [
102+
`const ctx = JSON.parse(process.env.AIOX_HOOK_CONTEXT || '{}');`,
103+
`const { onPreCompact } = require(${JSON.stringify(runnerPath)});`,
104+
`onPreCompact(ctx).catch(() => {});`,
105+
].join('\n');
106+
const child = spawn(process.execPath, ['-e', inlineScript], {
107+
detached: true,
108+
stdio: 'ignore',
109+
env: { ...process.env, AIOX_HOOK_CONTEXT: contextJson },
110+
});
111+
child.on('error', () => {});
112+
child.unref();
113+
} catch (_) {
114+
// Silent — spawn failures must not crash the hook
115+
}
105116
}
106117

107118
/** Entry point runner — sets safety timeout and executes main(). */

0 commit comments

Comments
 (0)