@@ -435,29 +435,51 @@ char *cbm_client_adapter_opencode(const char *binary_path) {
435435 sb_append (& sb , "import { spawn } from 'node:child_process';\n\n" );
436436 sb_append (& sb , "const BIN = '" );
437437 sb_append (& sb , bin );
438- sb_append (& sb , "';\n\n" );
438+ sb_append (& sb , "';\n"
439+ "const AUGMENT_TIMEOUT_MS = 10000;\n\n" );
439440
440441 /* hook-augment requires hook_event_name; its default dialect accepts
441442 * Grep/Glob under PreToolUse, Read under PostToolUse, and the
442443 * SessionStart lifecycle event — and emits the Claude JSON envelope, so
443444 * the plugin unwraps additionalContext instead of pasting raw JSON into
444- * the tool output. Every failure path resolves to '' (fail open). */
445+ * the tool output. Every failure path resolves to '' (fail open) —
446+ * including a child that never exits: augment() is awaited inline in
447+ * tool.execute.after, so a hung hook-augment would stall every
448+ * grep/glob/read completion. The timer SIGKILLs the child and resolves
449+ * ''; the promise keeps the first resolution, so the kill-triggered
450+ * 'close' event cannot override it. Stream errors (EPIPE on a write
451+ * still buffered when the child dies, spawn ENOENT) fire on child.stdin,
452+ * not on the child, and would crash the host as an unhandled 'error'
453+ * event — hence the stdin listener and the guarded end(). */
445454 sb_append (& sb , "function augment(payload) {\n"
446455 " return new Promise((resolve) => {\n"
447456 " const child = spawn(BIN, ['hook-augment'], {\n"
448457 " stdio: ['pipe', 'pipe', 'ignore'],\n"
449458 " env: { ...process.env, CBM_LOG_LEVEL: 'error' },\n"
450459 " });\n"
460+ " // Stream errors fire on the stream, not the child.\n"
461+ " child.stdin.on('error', () => {});\n"
462+ " const timer = setTimeout(() => {\n"
463+ " child.kill('SIGKILL');\n"
464+ " child.stdout.destroy();\n"
465+ " resolve('');\n"
466+ " }, AUGMENT_TIMEOUT_MS);\n"
451467 " let out = '';\n"
452468 " child.stdout.on('data', (d) => (out += d.toString()));\n"
453- " child.on('error', () => resolve(''));\n"
469+ " child.on('error', () => {\n"
470+ " clearTimeout(timer);\n"
471+ " resolve('');\n"
472+ " });\n"
454473 " child.on('close', () => {\n"
474+ " clearTimeout(timer);\n"
455475 " try {\n"
456476 " const ctx = JSON.parse(out)?.hookSpecificOutput?.additionalContext;\n"
457477 " resolve(typeof ctx === 'string' ? ctx : '');\n"
458478 " } catch { resolve(''); }\n"
459479 " });\n"
460- " child.stdin.end(JSON.stringify(payload));\n"
480+ " try {\n"
481+ " child.stdin.end(JSON.stringify(payload));\n"
482+ " } catch { resolve(''); }\n"
461483 " });\n"
462484 "}\n\n" );
463485
0 commit comments