Skip to content

Commit 205343e

Browse files
authored
Merge pull request DeusData#2694 from woodsonl/fix/opencode-augment-timeout
fix(cli): bound emitted OpenCode augment() with SIGKILL timeout and stdin guard
2 parents 8c7b3bd + 1cf6573 commit 205343e

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎src/cli/client_adapter.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎tests/test_agent_clients.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,30 @@ TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077) {
13381338
PASS();
13391339
}
13401340

1341+
/* augment() is awaited inline inside 'tool.execute.after', so a hook-augment
1342+
* that never exits would stall every grep/glob/read completion in the host
1343+
* session until the agent is restarted. The generated plugin must bound the
1344+
* wait: SIGKILL the child after AUGMENT_TIMEOUT_MS and resolve with '' (fail
1345+
* open). The promise keeps the first resolution, so the kill-triggered
1346+
* 'close' event arriving late cannot smuggle in partial output. Stream
1347+
* errors fire on child.stdin, not the child — the emitted listener and the
1348+
* guarded end() keep a failed write (EPIPE, ENOENT) from crashing the host
1349+
* as an unhandled 'error' event (reproduced under Bun 1.4 with a >64KB
1350+
* payload and a non-reading child). */
1351+
TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child) {
1352+
char *js = cbm_client_adapter_opencode("/usr/local/bin/codebase-memory-mcp");
1353+
ASSERT_NOT_NULL(js);
1354+
ASSERT_NOT_NULL(strstr(js, "const AUGMENT_TIMEOUT_MS = 10000;"));
1355+
ASSERT_NOT_NULL(strstr(js, "const timer = setTimeout("));
1356+
ASSERT_NOT_NULL(strstr(js, "child.kill('SIGKILL');"));
1357+
/* error and close must both stop the timer once they own the outcome. */
1358+
ASSERT_NOT_NULL(strstr(js, "clearTimeout(timer);"));
1359+
/* stream-level failures must not escape as unhandled 'error' events. */
1360+
ASSERT_NOT_NULL(strstr(js, "child.stdin.on('error', () => {});"));
1361+
free(js);
1362+
PASS();
1363+
}
1364+
13411365
/* Empty/NULL inputs must not produce a module at all. */
13421366
TEST(client_adapter_rejects_missing_binary_path) {
13431367
ASSERT_NULL(cbm_client_adapter_pi(NULL));
@@ -1385,5 +1409,6 @@ SUITE(agent_clients) {
13851409
RUN_TEST(client_adapter_opencode_sends_the_required_hook_event);
13861410
RUN_TEST(client_adapter_opencode_covers_lifecycle_read_and_compaction);
13871411
RUN_TEST(client_adapter_opencode_exports_the_v2_default_definition_issue2077);
1412+
RUN_TEST(client_adapter_opencode_bounds_a_hung_hook_augment_child);
13881413
RUN_TEST(client_adapter_rejects_missing_binary_path);
13891414
}

0 commit comments

Comments
 (0)