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(output): align discovery defaults and help

Martin Vogel48149cac08a8
diff --git a/README.md b/README.md
index e7964ff01..b41939a8f 100644
--- a/README.md
+++ b/README.md
@@ -631,11 +631,11 @@ Commands that mutate graph data use shared OS-backed, per-project locks. This se
When stderr is an interactive terminal, the CLI automatically shows lifecycle and indexing progress. Pass `--progress` to force the same feedback when stderr is redirected or the command is run non-interactively. Pass `--quiet` to disable automatic terminal progress and ordinary diagnostics while retaining errors; it cannot be combined with `--progress` or outer `cli --verbose`. Routine informational logs are quiet by default; pass outer `cli --verbose` to include them. Progress and logs use stderr while stdout remains reserved for the command result. Read tools return a compact tree by default; pass a tool's `--format json` for machine-readable payload JSON, or outer `--json` for the full MCP envelope.
-Large compact-tree tables may start with a response-local `<section>_refs` directory and an explicit `<section>_ref_rule`. A cell such as `@0+handler.go` reconstructs to ref `0`'s prefix plus `handler.go`. References are local to that sibling `<section>` table and expansion is non-recursive: entries inside `<section>_refs` are always literal prefixes. This is limited to declared path and qualified-name columns and activates only when that table is at least 15% and 64 bytes smaller. Search and trace likewise render direct and prefix-grouped tree shapes and keep the smaller complete representation, so singleton or scattered answers do not pay directory overhead. Keys are declared once per table but never cryptically abbreviated, and `--format json` keeps stable literal strings for machine consumers. These are deterministic model-neutral byte savings that normally reduce tokens; exact token counts still depend on the caller's tokenizer.
+Large compact-tree tables may start with a response-local `<section>_refs` directory and an explicit `<section>_ref_rule`. A cell such as `@0+handler.go` reconstructs to ref `0`'s prefix plus `handler.go`. References are local to that sibling `<section>` table and expansion is non-recursive: entries inside `<section>_refs` are always literal prefixes. This is limited to declared path and qualified-name columns and activates only when the exact rendered table is at least 15% and 64 bytes smaller and a conservative model-neutral token-shape proxy also improves by at least 1%. Search and trace likewise render direct and prefix-grouped tree shapes and keep the smaller complete representation, so singleton or scattered answers do not pay directory overhead. Keys are declared once per table but never cryptically abbreviated, and `--format json` keeps stable literal strings for machine consumers. Both gates are deterministic; exact token counts still depend on the caller's tokenizer.
Lean responses truncate semantically, never by cutting arbitrary bytes from code or identifiers. Ranked graph rows are retained ahead of raw grep rows and diagnostic summaries; omitted rows/sections report totals, `has_more`, and a strictly advancing continuation offset or cursor. If even the first whole row cannot fit, CBM asks for a higher budget and emits no self-looping cursor. `max_output_tokens` is model-neutral sizing guidance: CBM enforces a deterministic ceiling of four UTF-8 bytes per requested token, so it is not a tokenizer-exact count. Detail flags such as `diagnostics`, `source_mode`, and `detail` opt into heavier fields. `search_code` pages ranked rows with `result_limit`/`result_offset` (`limit` remains a compatibility alias), raw rows with `raw_limit`/`raw_offset`, and directory summaries with `directory_limit`/`directory_offset`. Raw lines default to a UTF-8-safe match-centered preview; each row reports `content_start_byte`, returned/total byte counts, match byte bounds when known, and a content continuation offset. Pass `raw_content_offset` to page the original line without moving the raw-row cursor. `match_limit` and `source_max_lines` bound per-result details, with exact omission metadata. `detect_changes` pages changed files, impacted symbols, and module summaries independently; prefer its snapshot-bound `*_cursor` continuations, which reject changed commits, worktree bytes, graph generation, or semantic arguments instead of silently skipping or duplicating rows.
-Every response is standard UTF-8 without sacrificing POSIX byte-string identities. A value containing malformed UTF-8 is emitted reversibly as `@bytes:<lowercase hex of every original byte>`. A valid value that literally begins with the reserved `@bytes:` or `@utf8:` prefix is emitted as `@utf8:<original value>`, so decoding is unambiguous: strip one `@utf8:` prefix for literal UTF-8, or hex-decode one `@bytes:` prefix for original bytes. Ordinary valid UTF-8 is unchanged and pays no output-token overhead.
+Every response is standard UTF-8. Identifiers, paths, and raw search previews preserve POSIX byte-string identities: a preserved value containing malformed UTF-8 is emitted reversibly as `@bytes:<lowercase hex of every original byte>`. A valid preserved value that literally begins with the reserved `@bytes:` or `@utf8:` prefix is emitted as `@utf8:<original value>`, so decoding is unambiguous: strip one `@utf8:` prefix for literal UTF-8, or hex-decode one `@bytes:` prefix for original bytes. Ordinary valid UTF-8 is unchanged and pays no output-token overhead. To keep code readable, source bodies replace malformed UTF-8 with U+FFFD; use the pageable raw search preview when byte-exact source inspection is required.
Use `cli <tool> --help` to see the flags generated from that tool's input schema:
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 669af99b1..57f7d58e2 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -13235,6 +13235,18 @@ char *cbm_cli_build_args_json(const char *tool_name, int argc, char **argv, char
return result;
}
+static void cli_print_schema_value(const yyjson_val *value) {
+ if (yyjson_is_str(value)) {
+ fputs(yyjson_get_str(value), stdout);
+ return;
+ }
+ char *json = yyjson_val_write(value, 0, NULL);
+ if (json) {
+ fputs(json, stdout);
+ free(json);
+ }
+}
+
int cbm_cli_print_tool_help(const char *tool_name) {
const char *schema_str = cbm_mcp_tool_input_schema(tool_name);
if (!schema_str) {
@@ -13265,6 +13277,8 @@ int cbm_cli_print_tool_help(const char *tool_name) {
}
const char *type = "string";
const char *desc = "";
+ yyjson_val *choices = NULL;
+ yyjson_val *default_value = NULL;
if (yyjson_is_obj(pval)) {
yyjson_val *t = yyjson_obj_get(pval, "type");
if (t && yyjson_is_str(t)) {
@@ -13274,12 +13288,36 @@ int cbm_cli_print_tool_help(const char *tool_name) {
if (d && yyjson_is_str(d)) {
desc = yyjson_get_str(d);
}
+ choices = yyjson_obj_get(pval, "enum");
+ default_value = yyjson_obj_get(pval, "default");
}
char flag[CLI_BUF_256];
snprintf(flag, sizeof(flag), "%s", name);
cli_snake_to_kebab(flag);
bool req = cli_schema_required_has(required, name);
- printf(" --%s <%s>%s", flag, type, req ? " [required]" : "");
+ printf(" --%s <", flag);
+ if (choices && yyjson_is_arr(choices) && yyjson_arr_size(choices) > 0) {
+ size_t idx;
+ size_t max;
+ yyjson_val *choice;
+ yyjson_arr_foreach(choices, idx, max, choice) {
+ if (idx > 0) {
+ fputc('|', stdout);
+ }
+ cli_print_schema_value(choice);
+ }
+ } else {
+ fputs(type, stdout);
+ }
+ fputc('>', stdout);
+ if (req) {
+ fputs(" [required]", stdout);
+ }
+ if (default_value) {
+ fputs(" [default: ", stdout);
+ cli_print_schema_value(default_value);
+ fputc(']', stdout);
+ }
if (desc[0]) {
printf(" %s", desc);
}
diff --git a/src/mcp/compact_out.h b/src/mcp/compact_out.h
index fa86c24e9..677ba5884 100644
--- a/src/mcp/compact_out.h
+++ b/src/mcp/compact_out.h
@@ -1,11 +1,9 @@
-/* compact_out.h — TOON (Token-Oriented Object Notation) emission helpers.
+/* compact_out.h — compact tree emission helpers.
*
* Tool responses are consumed by LLM agents, where every byte is context
- * tokens. TOON encodes the same data as JSON but declares tabular fields
- * once in a header and streams rows line by line, cutting 40-60% of tokens
- * on homogeneous result sets at equal-or-better retrieval accuracy
- * (toonformat.dev/guide/benchmarks). Emitters here cover the subset we
- * emit: scalar key-value lines and flat tables with explicit [N] lengths.
+ * tokens. The tree format declares tabular fields once in a header and
+ * streams rows line by line, removing repeated-key overhead while retaining
+ * scalar key-value lines and explicit table lengths.
*
* Quoting: a cell/scalar is double-quoted iff it is empty, has leading or
* trailing whitespace, contains a comma, quote, newline, or CR, or would
@@ -70,8 +68,9 @@ void cbm_tree_table_rows_typed(cbm_sb_t *sb, const char *key, int nrows, const c
/* Semantic prefix-directory variant. Only columns explicitly marked in
* prefix_cols may be factored, so labels, diagnostics, and arbitrary query
- * strings are never rewritten. The directory activates only when it saves at
- * least 15% and 64 bytes. */
+ * strings are never rewritten. The directory activates only when the exact
+ * rendering saves at least 15% and 64 bytes and a conservative model-neutral
+ * token-shape proxy saves at least 1%. */
void cbm_tree_table_rows_profiled(cbm_sb_t *sb, const char *key, int nrows, const char *const *cols,
int ncols, const char *const *cells, const bool *string_cols,
const bool *prefix_cols);
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 79d9a4441..603818bf0 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -482,8 +482,8 @@ static const tool_def_t TOOLS[] = {
"},\"required\":[\"repo_path\"]}"},
{"search_graph", "Search graph",
- "Regex rows keep qn/file/lines and in/out over CALLS/USAGE/CALL_REFERENCE/INHERITS/"
- "IMPLEMENTS. Pages: offset/limit; semantic_offset/semantic_limit.",
+ "Find symbols via BM25 query, regex name/qn filters, or semantic_query. Rows keep "
+ "qn/file/lines and in/out over CALLS/USAGE/CALL_REFERENCE/INHERITS/IMPLEMENTS.",
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},"
"\"query\":{\"type\":\"string\"},"
"\"label\":{\"type\":\"string\"},\"name_pattern\":{\"type\":\"string\"},\"qn_pattern\":{"
@@ -501,9 +501,9 @@ static const tool_def_t TOOLS[] = {
"\"offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0},"
"\"max_output_tokens\":{\"type\":\"integer\",\"default\":3200,\"minimum\":128,"
"\"maximum\":1000000},"
- "\"format\":{\"type\":\"string\",\"enum\":[\"tree\",\"json\"]},"
+ "\"format\":{\"type\":\"string\",\"enum\":[\"tree\",\"json\"],\"default\":\"tree\"},"
"\"fields\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},"
- "\"detail\":{\"type\":\"string\",\"enum\":[\"ids\",\"default\"]}},"
+ "\"detail\":{\"type\":\"string\",\"enum\":[\"ids\",\"default\"],\"default\":\"default\"}},"
"\"required\":[\"project\"]}"},
{"query_graph", "Query graph",
@@ -720,7 +720,7 @@ static const tool_def_t TOOLS[] = {
{"detect_changes", "Detect changes",
"Map a Git diff to files and impact. Page with snapshot cursors.",
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"scope\":{\"type\":"
- "\"string\",\"enum\":[\"files\",\"impact\"]},"
+ "\"string\",\"enum\":[\"files\",\"impact\"],\"default\":\"impact\"},"
"\"direction\":{\"type\":\"string\",\"enum\":[\"inbound\",\"outbound\",\"both\"],\"default\":"
"\"inbound\",\"description\":\"inbound=callers; outbound=dependencies; both=union.\"},"
"\"depth\":{\"type\":\"integer\",\"default\":2},"
@@ -15143,7 +15143,8 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
int depth = cbm_mcp_get_int_arg(args, "depth", MCP_DEFAULT_BFS_DEPTH);
depth = clamp_mcp_depth(depth, "detect_changes");
- /* scope: "files" = just changed files, "symbols" = files + symbols (default) */
+ /* scope: "files" = changed files only; "impact" = files + symbols (default).
+ * Keep accepting the legacy "symbols" spelling for compatibility. */
bool want_symbols = !scope || strcmp(scope, "symbols") == 0 || strcmp(scope, "impact") == 0;
/* `since` (e.g. "HEAD~10", "v0.5.0") is the documented diff base but was
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 02cff8b76..786a3679f 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -13775,10 +13775,49 @@ TEST(cli_build_args_json_bad_positional_errors_issue680) {
PASS();
}
-/* Per-tool --help returns 0 for a known tool, -1 for an unknown one. */
+/* Capture per-tool help using the same tmpfile+dup2 idiom as config-command
+ * output above. This stays portable to the MinGW test leg. */
+static int cli_tool_help_capture(const char *tool_name, char *out, size_t cap) {
+ out[0] = '\0';
+ FILE *capture = tmpfile();
+ int saved = capture ? dup(fileno(stdout)) : -1;
+ if (!capture || saved < 0) {
+ if (capture)
+ fclose(capture);
+ if (saved >= 0)
+ close(saved);
+ return -1000;
+ }
+ fflush(stdout);
+ if (dup2(fileno(capture), fileno(stdout)) < 0) {
+ fclose(capture);
+ close(saved);
+ return -1000;
+ }
+ int rc = cbm_cli_print_tool_help(tool_name);
+ fflush(stdout);
+ (void)dup2(saved, fileno(stdout));
+ close(saved);
+ rewind(capture);
+ size_t got = fread(out, 1, cap - 1, capture);
+ out[got] = '\0';
+ fclose(capture);
+ return rc;
+}
+
+/* Per-tool --help returns 0 for a known tool, -1 for an unknown one, and
+ * surfaces schema choices/defaults rather than flattening every flag to its
+ * primitive JSON type. */
TEST(cli_print_tool_help_issue680) {
- ASSERT_EQ(cbm_cli_print_tool_help("index_repository"), 0);
- ASSERT_EQ(cbm_cli_print_tool_help("nope_not_a_tool"), -1);
+ char out[16 * 1024];
+ ASSERT_EQ(cli_tool_help_capture("search_graph", out, sizeof(out)), 0);
+ ASSERT_NOT_NULL(strstr(out, "--format <tree|json> [default: tree]"));
+ ASSERT_NOT_NULL(strstr(out, "--detail <ids|default> [default: default]"));
+ ASSERT_EQ(cli_tool_help_capture("index_repository", out, sizeof(out)), 0);
+ ASSERT_NOT_NULL(strstr(out, "--mode <full|moderate|fast|cross-repo-intelligence> [default: "
+ "full]"));
+ ASSERT_EQ(cli_tool_help_capture("nope_not_a_tool", out, sizeof(out)), -1);
+ ASSERT_STR_EQ(out, "");
PASS();
}
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index cfa8e9565..0aa4806ac 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -1251,7 +1251,9 @@ TEST(mcp_tools_list_latest_metadata) {
* relationships used for call/reference/type centrality, not every edge
* family (for example DEFINES or CONTAINS_FILE). Keep the public contract
* aligned with the store query. */
- ASSERT_NOT_NULL(strstr(json, "Regex rows keep qn/file/lines and in/out over CALLS"));
+ ASSERT_NOT_NULL(strstr(json, "Find symbols via BM25 query, regex name/qn filters, or "
+ "semantic_query"));
+ ASSERT_NOT_NULL(strstr(json, "Rows keep qn/file/lines and in/out over CALLS"));
ASSERT_NOT_NULL(strstr(json, "USAGE"));
ASSERT_NOT_NULL(strstr(json, "CALL_REFERENCE"));
ASSERT_NOT_NULL(strstr(json, "INHERITS"));
@@ -1261,6 +1263,65 @@ TEST(mcp_tools_list_latest_metadata) {
PASS();
}
+TEST(mcp_discovery_defaults_match_runtime_contract) {
+ const char *search_schema = cbm_mcp_tool_input_schema("search_graph");
+ ASSERT_NOT_NULL(search_schema);
+ yyjson_doc *doc = yyjson_read(search_schema, strlen(search_schema), 0);
+ ASSERT_NOT_NULL(doc);
+ yyjson_val *props = yyjson_obj_get(yyjson_doc_get_root(doc), "properties");
+ ASSERT_NOT_NULL(props);
+ yyjson_val *format = yyjson_obj_get(props, "format");
+ yyjson_val *detail = yyjson_obj_get(props, "detail");
+ ASSERT_NOT_NULL(format);
+ ASSERT_NOT_NULL(detail);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(format, "default")), "tree");
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(detail, "default")), "default");
+ yyjson_doc_free(doc);
+
+ const char *detect_schema = cbm_mcp_tool_input_schema("detect_changes");
+ ASSERT_NOT_NULL(detect_schema);
+ doc = yyjson_read(detect_schema, strlen(detect_schema), 0);
+ ASSERT_NOT_NULL(doc);
+ props = yyjson_obj_get(yyjson_doc_get_root(doc), "properties");
+ ASSERT_NOT_NULL(props);
+ yyjson_val *scope = yyjson_obj_get(props, "scope");
+ ASSERT_NOT_NULL(scope);
+ ASSERT_STR_EQ(yyjson_get_str(yyjson_obj_get(scope, "default")), "impact");
+ yyjson_doc_free(doc);
+
+ /* search_graph's omitted values and the newly-declared defaults must be
+ * behaviorally identical. */
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ cbm_store_t *store = cbm_mcp_server_store(srv);
+ ASSERT_NOT_NULL(store);
+ const char *project = "discovery-default-contract";
+ ASSERT_EQ(cbm_store_upsert_project(store, project, cbm_tmpdir()), CBM_STORE_OK);
+ cbm_mcp_server_set_project(srv, project);
+ cbm_node_t node = {.project = project,
+ .label = "Function",
+ .name = "default_contract",
+ .qualified_name = "fixture.default_contract",
+ .file_path = "src/default.c",
+ .start_line = 1,
+ .end_line = 2};
+ ASSERT_GT(cbm_store_upsert_node(store, &node), 0);
+ char *implicit = cbm_mcp_handle_tool(
+ srv, "search_graph",
+ "{\"project\":\"discovery-default-contract\",\"name_pattern\":\"default_contract\"}");
+ char *explicit_defaults = cbm_mcp_handle_tool(
+ srv, "search_graph",
+ "{\"project\":\"discovery-default-contract\",\"name_pattern\":\"default_contract\","
+ "\"format\":\"tree\",\"detail\":\"default\"}");
+ ASSERT_NOT_NULL(implicit);
+ ASSERT_NOT_NULL(explicit_defaults);
+ ASSERT_STR_EQ(implicit, explicit_defaults);
+ free(implicit);
+ free(explicit_defaults);
+ cbm_mcp_server_free(srv);
+ PASS();
+}
+
TEST(mcp_metadata_byte_budget) {
/* Tool metadata is paid on discovery before any graph answer is returned.
* Byte ceilings are deterministic across tokenizers and force deliberate
@@ -14094,12 +14155,19 @@ TEST(detect_changes_seeds_only_touched_symbol_issue1363) {
snprintf(dc_args, sizeof(dc_args), "{\"project\":\"%s\",\"depth\":1}", project);
char *dc_resp = cbm_mcp_handle_tool(srv, "detect_changes", dc_args);
ASSERT_NOT_NULL(dc_resp);
+ char dc_explicit_args[700];
+ snprintf(dc_explicit_args, sizeof(dc_explicit_args),
+ "{\"project\":\"%s\",\"scope\":\"impact\",\"depth\":1}", project);
+ char *dc_explicit_resp = cbm_mcp_handle_tool(srv, "detect_changes", dc_explicit_args);
+ ASSERT_NOT_NULL(dc_explicit_resp);
+ ASSERT_STR_EQ(dc_resp, dc_explicit_resp);
/* cbm_mcp_handle_tool wraps the tree text in a JSON string, so a literal
* newline in the source becomes the two-character `\n` escape sequence
* in dc_resp's actual bytes — match that, not a real newline. */
ASSERT_NOT_NULL(strstr(dc_resp, "seed_symbols: 1\\n"));
ASSERT_NULL(strstr(dc_resp, "bar"));
+ free(dc_explicit_resp);
free(dc_resp);
free(project);
cbm_mcp_server_free(srv);
@@ -19208,6 +19276,7 @@ SUITE(mcp) {
RUN_TEST(mcp_tools_list);
RUN_TEST(mcp_tools_help_list_matches_registry);
RUN_TEST(mcp_tools_list_latest_metadata);
+ RUN_TEST(mcp_discovery_defaults_match_runtime_contract);
RUN_TEST(mcp_metadata_byte_budget);
RUN_TEST(mcp_tools_have_behavior_annotations);
RUN_TEST(mcp_index_repository_declares_name_override_issue571);