Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(cli): bound emitted OpenCode augment() with SIGKILL timeout and stdin guard
Lance Woodson6e8e785679e7
diff --git a/src/cli/client_adapter.c b/src/cli/client_adapter.cindex 68feb6102..83a404276 100644--- a/src/cli/client_adapter.c+++ b/src/cli/client_adapter.c@@ -435,29 +435,51 @@ char *cbm_client_adapter_opencode(const char *binary_path) {sb_append(&sb, "import { spawn } from 'node:child_process';\n\n");sb_append(&sb, "const BIN = '");sb_append(&sb, bin);- sb_append(&sb, "';\n\n");+ sb_append(&sb, "';\n"+ "const AUGMENT_TIMEOUT_MS = 10000;\n\n");/* hook-augment requires hook_event_name; its default dialect accepts* Grep/Glob under PreToolUse, Read under PostToolUse, and the* SessionStart lifecycle event — and emits the Claude JSON envelope, so* the plugin unwraps additionalContext instead of pasting raw JSON into- * the tool output. Every failure path resolves to '' (fail open). */+ * the tool output. Every failure path resolves to '' (fail open) —+ * including a child that never exits: augment() is awaited inline in+ * tool.execute.after, so a hung hook-augment would stall every+ * grep/glob/read completion. The timer SIGKILLs the child and resolves+ * ''; the promise keeps the first resolution, so the kill-triggered+ * 'close' event cannot override it. Stream errors (EPIPE on a write+ * still buffered when the child dies, spawn ENOENT) fire on child.stdin,+ * not on the child, and would crash the host as an unhandled 'error'+ * event — hence the stdin listener and the guarded end(). */sb_append(&sb, "function augment(payload) {\n"" return new Promise((resolve) => {\n"" const child = spawn(BIN, ['hook-augment'], {\n"" stdio: ['pipe', 'pipe', 'ignore'],\n"" env: { ...process.env, CBM_LOG_LEVEL: 'error' },\n"" });\n"+ " // Stream errors fire on the stream, not the child.\n"+ " child.stdin.on('error', () => {});\n"+ " const timer = setTimeout(() => {\n"+ " child.kill('SIGKILL');\n"+ " child.stdout.destroy();\n"+ " resolve('');\n"+ " }, AUGMENT_TIMEOUT_MS);\n"" let out = '';\n"" child.stdout.on('data', (d) => (out += d.toString()));\n"- " child.on('error', () => resolve(''));\n"+ " child.on('error', () => {\n"+ " clearTimeout(timer);\n"+ " resolve('');\n"+ " });\n"" child.on('close', () => {\n"+ " clearTimeout(timer);\n"" try {\n"" const ctx = JSON.parse(out)?.hookSpecificOutput?.additionalContext;\n"" resolve(typeof ctx === 'string' ? ctx : '');\n"" } catch { resolve(''); }\n"" });\n"- " child.stdin.end(JSON.stringify(payload));\n"+ " try {\n"+ " child.stdin.end(JSON.stringify(payload));\n"+ " } catch { resolve(''); }\n"" });\n""}\n\n");diff --git a/tests/test_agent_clients.c b/tests/test_agent_clients.cindex df8515e73..f1bc3699e 100644--- a/tests/test_agent_clients.c+++ b/tests/test_agent_clients.c@@ -1338,6 +1338,30 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {PASS();}+/* augment() is awaited inline inside 'tool.execute.after', so a hook-augment+ * that never exits would stall every grep/glob/read completion in the host+ * session until the agent is restarted. The generated plugin must bound the+ * wait: SIGKILL the child after AUGMENT_TIMEOUT_MS and resolve with '' (fail+ * open). The promise keeps the first resolution, so the kill-triggered+ * 'close' event arriving late cannot smuggle in partial output. Stream+ * errors fire on child.stdin, not the child — the emitted listener and the+ * guarded end() keep a failed write (EPIPE, ENOENT) from crashing the host+ * as an unhandled 'error' event (reproduced under Bun 1.4 with a >64KB+ * payload and a non-reading child). */+TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child) {+ char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");+ ASSERT_NOT_NULL(js);+ ASSERT_NOT_NULL(strstr(js, "const AUGMENT_TIMEOUT_MS = 10000;"));+ ASSERT_NOT_NULL(strstr(js, "const timer = setTimeout("));+ ASSERT_NOT_NULL(strstr(js, "child.kill('SIGKILL');"));+ /* error and close must both stop the timer once they own the outcome. */+ ASSERT_NOT_NULL(strstr(js, "clearTimeout(timer);"));+ /* stream-level failures must not escape as unhandled 'error' events. */+ ASSERT_NOT_NULL(strstr(js, "child.stdin.on('error', () => {});"));+ free(js);+ PASS();+}+/* Empty/NULL inputs must not produce a module at all. */TEST(client_adapter_rejects_missing_binary_path) {ASSERT_NULL(cbm_client_adapter_pi(NULL));@@ -1385,5 +1409,6 @@ SUITE(agent_clients) {RUN_TEST(client_adapter_opencode_sends_the_required_hook_event);RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);RUN_TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077);+ RUN_TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child);RUN_TEST(client_adapter_rejects_missing_binary_path);}