Skip to content

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.

fix(cli): never read stdin for a tool that declares no arguments (#1359)

Martin Vogel9ca108fce968
diff --git a/README.md b/README.md
index a94d0e927..ca39ef418 100644
--- a/README.md
+++ b/README.md
@@ -598,7 +598,7 @@ codebase-memory-mcp cli --progress index_repository --repo-path /path/to/repo
codebase-memory-mcp cli search_graph --project my-project --label Function | jq '.results[].name'
```
-JSON arguments can also be piped on stdin. Inline JSON remains accepted for backward compatibility but is deprecated in favor of flags, `--args-file`, or stdin.
+JSON arguments can also be piped on stdin, for tools that take arguments. A tool whose input schema declares none — `list_projects` — never reads stdin, so it stays responsive when it inherits a pipe the caller never closes (the default for `child_process.spawn` and similar wrappers). Inline JSON remains accepted for backward compatibility but is deprecated in favor of flags, `--args-file`, or stdin.
## MCP Tools
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 8cd9729f5..c6c04a31a 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -12273,6 +12273,49 @@ static void cli_add_typed(yyjson_mut_doc *out, yyjson_mut_val *obj, const char *
yyjson_mut_obj_add(obj, yyjson_mut_strcpy(out, key), vv);
}
+bool cbm_cli_args_from_stdin_allowed(const char *tool_name, bool stdin_is_tty) {
+ /* An interactive run has nothing piped to read: consulting the terminal
+ * would just sit waiting for the user to type JSON. Unchanged by #1359 —
+ * this is why the hang was only ever reachable from automation. */
+ if (stdin_is_tty) {
+ return false;
+ }
+
+ /* WHY the schema gate (#1359): a non-interactive stdin used to be accepted
+ * as piped arguments unconditionally, and cli_slurp_stream reads to EOF. An
+ * ordinary non-interactive caller never sends that EOF — Node's
+ * child_process.spawn defaults to stdio:['pipe','pipe','pipe'] and the
+ * parent must call child.stdin.end() explicitly, which almost nobody does
+ * for a command they are not writing to. fd 0 then stays open with no
+ * writer and the read never returns; the reporter's shell script was still
+ * parked in fread(0) fourteen minutes later. For a tool whose input_schema
+ * declares no properties (list_projects) stdin cannot carry anything the
+ * tool would accept, so that read is pure deadlock with nothing to gain:
+ * never start it, and let the caller fall through to `{}` exactly as an
+ * interactive run already did. Tools that DO declare properties keep the
+ * documented `echo '<json>' | cli <tool>` channel untouched. */
+ const char *schema_str = cbm_mcp_tool_input_schema(tool_name);
+ if (!schema_str) {
+ /* Unknown tool: dispatch rejects it by name and no stdin content can
+ * change that verdict, so do not block for input we would discard. */
+ return false;
+ }
+
+ yyjson_doc *schema_doc = yyjson_read(schema_str, strlen(schema_str), 0);
+ if (!schema_doc) {
+ /* The schemas are compile-time constants, so a parse failure means a
+ * malformed literal rather than caller input. Keep the documented stdin
+ * channel rather than silently narrowing it on a schema we could not
+ * read — the gate must only ever fire on positive evidence that the
+ * tool takes no arguments. */
+ return true;
+ }
+ yyjson_val *props = yyjson_obj_get(yyjson_doc_get_root(schema_doc), "properties");
+ bool accepts_arguments = props && yyjson_is_obj(props) && yyjson_obj_size(props) > 0;
+ yyjson_doc_free(schema_doc);
+ return accepts_arguments;
+}
+
char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out) {
if (err_out) {
*err_out = NULL;
diff --git a/src/cli/cli.h b/src/cli/cli.h
index b6901f4b2..184c295af 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -34,6 +34,14 @@ const char *cbm_cli_get_version(void);
* must free. Caller frees the returned JSON string. */
char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char **err_out);
+/* Decide whether `cli <tool>` may take its JSON arguments from stdin, given
+ * that no --args-file, raw-JSON positional and no --flag form was supplied.
+ * stdin_is_tty is the caller's isatty(0) result. True only for a non-terminal
+ * stdin AND a tool whose input_schema declares at least one property; a
+ * zero-argument tool such as list_projects must never read stdin, because an
+ * inherited-but-never-closed pipe makes that read block forever (#1359). */
+bool cbm_cli_args_from_stdin_allowed(const char *tool_name, bool stdin_is_tty);
+
/* Print per-tool help (usage + the tool's flags with type/description/required)
* derived from its input_schema, to stdout. Returns 0 if the tool is known,
* non-zero (and prints nothing) if it is not. */
diff --git a/src/main.c b/src/main.c
index 9c9da5f29..96d753c11 100644
--- a/src/main.c
+++ b/src/main.c
@@ -756,8 +756,10 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
return SKIP_ONE;
}
args_json = heap_args;
- } else if (!cli_isatty(0)) {
- /* piped stdin (UTF-8 clean, no shell quoting): cli <tool> < args.json */
+ } else if (cbm_cli_args_from_stdin_allowed(tool_name, cli_isatty(0) != 0)) {
+ /* piped stdin (UTF-8 clean, no shell quoting): cli <tool> < args.json.
+ * Gated (#1359): a tool that declares no arguments must not read a pipe
+ * nobody is going to write to or close — see the WHY on the predicate. */
heap_args = cli_slurp_stream(stdin);
if (heap_args && heap_args[0]) {
args_json = heap_args;
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 47e4f1aa8..a17c622de 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -12110,6 +12110,63 @@ TEST(cli_print_tool_help_issue680) {
PASS();
}
+/* #1359: `cli <tool>` with no argument-bearing token used to slurp stdin to EOF
+ * for ANY non-terminal stdin. The ordinary automation caller never sends that
+ * EOF — Node's child_process.spawn defaults to stdio:['pipe','pipe','pipe'] and
+ * the parent must call child.stdin.end() explicitly — so `cli list_projects`
+ * parked in fread(0) with no writer and never returned (the reporter's script
+ * was still stuck fourteen minutes later). list_projects declares no schema
+ * properties, so stdin could never have carried anything it accepts: the read
+ * was pure deadlock. Gate the read on the tool actually declaring arguments. */
+TEST(cli_zero_argument_tool_never_reads_stdin_issue1359) {
+ /* The reported hang: zero-argument tool + non-terminal stdin. */
+ ASSERT_FALSE(cbm_cli_args_from_stdin_allowed("list_projects", false));
+
+ /* The documented `echo '<json>' | cli <tool>` channel must survive. */
+ ASSERT_TRUE(cbm_cli_args_from_stdin_allowed("index_status", false));
+ ASSERT_TRUE(cbm_cli_args_from_stdin_allowed("search_graph", false));
+ ASSERT_TRUE(cbm_cli_args_from_stdin_allowed("index_repository", false));
+
+ /* Interactive runs never read the terminal for arguments — unchanged. */
+ ASSERT_FALSE(cbm_cli_args_from_stdin_allowed("index_status", true));
+ ASSERT_FALSE(cbm_cli_args_from_stdin_allowed("list_projects", true));
+
+ /* An unknown tool is rejected by name; blocking for input first can only
+ * delay that verdict, never change it. */
+ ASSERT_FALSE(cbm_cli_args_from_stdin_allowed("nope_not_a_tool", false));
+ ASSERT_FALSE(cbm_cli_args_from_stdin_allowed(NULL, false));
+ PASS();
+}
+
+/* The gate must track the advertised input_schema for the WHOLE tool table, not
+ * carry a special case for list_projects: a future zero-argument tool would
+ * otherwise reintroduce #1359 silently. Expectation is derived independently
+ * from the schema the MCP tools/list publishes. */
+TEST(cli_stdin_args_gate_tracks_tool_schema_issue1359) {
+ int zero_argument_tools = 0;
+ for (int i = 0; i < cbm_mcp_tool_count(); i++) {
+ const char *name = cbm_mcp_tool_name(i);
+ ASSERT_NOT_NULL(name);
+ const char *schema = cbm_mcp_tool_input_schema(name);
+ ASSERT_NOT_NULL(schema);
+
+ yyjson_doc *doc = yyjson_read(schema, strlen(schema), 0);
+ ASSERT_NOT_NULL(doc);
+ yyjson_val *props = yyjson_obj_get(yyjson_doc_get_root(doc), "properties");
+ bool declares_properties = props && yyjson_is_obj(props) && yyjson_obj_size(props) > 0;
+ yyjson_doc_free(doc);
+
+ if (!declares_properties) {
+ zero_argument_tools++;
+ }
+ ASSERT_EQ(cbm_cli_args_from_stdin_allowed(name, false), declares_properties);
+ ASSERT_FALSE(cbm_cli_args_from_stdin_allowed(name, true));
+ }
+ /* list_projects at minimum; without one the sweep above proves nothing. */
+ ASSERT_GTE(zero_argument_tools, 1);
+ PASS();
+}
+
/* The self-update path verifies a downloaded archive against a published
* checksum. That check is only meaningful if the digest is actually computed —
* a broken hash command (it once invoked `shasum -a CBM_SZ_256`, an invalid
@@ -12827,4 +12884,8 @@ SUITE(cli) {
RUN_TEST(cli_build_args_json_key_equals_value_issue680);
RUN_TEST(cli_build_args_json_bad_positional_errors_issue680);
RUN_TEST(cli_print_tool_help_issue680);
+
+ /* Stdin argument gate (#1359) */
+ RUN_TEST(cli_zero_argument_tool_never_reads_stdin_issue1359);
+ RUN_TEST(cli_stdin_args_gate_tracks_tool_schema_issue1359);
}