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.

Report excluded subtrees in the index_repository result (#411)

Martin Vogelba52d6ba5657
diff --git a/src/discover/discover.c b/src/discover/discover.c
index 0601cf52e..314c00c53 100644
--- a/src/discover/discover.c
+++ b/src/discover/discover.c
@@ -208,8 +208,34 @@ typedef struct {
cbm_file_info_t *files;
int count;
int capacity;
+ /* Directories skipped during the walk (rel paths), so callers can surface
+ * which subtrees were dropped (#411). strdup'd; freed by the caller via
+ * cbm_discover_free_excluded or internally when not requested. */
+ char **excluded;
+ int excluded_count;
+ int excluded_cap;
} file_list_t;
+static void file_list_add_excluded(file_list_t *fl, const char *rel_path) {
+ if (!rel_path || rel_path[0] == '\0') {
+ return;
+ }
+ if (fl->excluded_count >= fl->excluded_cap) {
+ int new_cap = fl->excluded_cap ? fl->excluded_cap * PAIR_LEN : CBM_SZ_64;
+ char **grown = realloc(fl->excluded, new_cap * sizeof(char *));
+ if (!grown) {
+ return;
+ }
+ fl->excluded = grown;
+ fl->excluded_cap = new_cap;
+ }
+ char *copy = strdup(rel_path);
+ if (!copy) {
+ return;
+ }
+ fl->excluded[fl->excluded_count++] = copy;
+}
+
static void fl_add(file_list_t *fl, const char *abs_path, const char *rel_path, CBMLanguage lang,
int64_t size) {
if (fl->count >= fl->capacity) {
@@ -429,6 +455,9 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram
if (!should_skip_directory(entry->name, rel_path, opts, gitignore, cbmignore,
frame->local_gi, frame->local_gi_prefix)) {
walk_push_subdir(stack, top, abs_path, rel_path, frame);
+ } else {
+ /* Record the excluded subtree root so callers can report it (#411). */
+ file_list_add_excluded(out, rel_path);
}
} else if (S_ISREG(st.st_mode)) {
walk_dir_process_file(abs_path, rel_path, entry->name, opts, gitignore, cbmignore,
@@ -488,6 +517,17 @@ static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_dis
int cbm_discover(const char *repo_path, const cbm_discover_opts_t *opts, cbm_file_info_t **out,
int *count) {
+ return cbm_discover_ex(repo_path, opts, out, count, NULL, NULL);
+}
+
+int cbm_discover_ex(const char *repo_path, const cbm_discover_opts_t *opts, cbm_file_info_t **out,
+ int *count, char ***excluded_out, int *excluded_count_out) {
+ if (excluded_out) {
+ *excluded_out = NULL;
+ }
+ if (excluded_count_out) {
+ *excluded_count_out = 0;
+ }
if (!repo_path || !out || !count) {
return CBM_NOT_FOUND;
}
@@ -530,6 +570,16 @@ int cbm_discover(const char *repo_path, const cbm_discover_opts_t *opts, cbm_fil
*out = fl.files;
*count = fl.count;
+
+ /* Hand the excluded-dir list to the caller, or free it if not requested. */
+ if (excluded_out) {
+ *excluded_out = fl.excluded;
+ if (excluded_count_out) {
+ *excluded_count_out = fl.excluded_count;
+ }
+ } else {
+ cbm_discover_free_excluded(fl.excluded, fl.excluded_count);
+ }
return 0;
}
@@ -543,3 +593,13 @@ void cbm_discover_free(cbm_file_info_t *files, int count) {
}
free(files);
}
+
+void cbm_discover_free_excluded(char **excluded, int count) {
+ if (!excluded) {
+ return;
+ }
+ for (int i = 0; i < count; i++) {
+ free(excluded[i]);
+ }
+ free(excluded);
+}
diff --git a/src/discover/discover.h b/src/discover/discover.h
index c604e6f83..09d08b279 100644
--- a/src/discover/discover.h
+++ b/src/discover/discover.h
@@ -110,7 +110,22 @@ typedef struct {
int cbm_discover(const char *repo_path, const cbm_discover_opts_t *opts, cbm_file_info_t **out,
int *count);
+/* Like cbm_discover(), but also reports the directory subtrees that were
+ * skipped during the walk (hardcoded ALWAYS_SKIP/FAST_SKIP dirs + gitignore
+ * matches), so callers can surface which subtrees were dropped (#411).
+ * On success, *excluded_out receives a heap-allocated array of strdup'd
+ * relative directory paths and *excluded_count_out its length; the caller
+ * owns it and must free via cbm_discover_free_excluded(). Pass NULL for
+ * excluded_out (and/or excluded_count_out) to discard the list — the internal
+ * accumulator is freed in that case (no leak).
+ * Returns 0 on success, -1 on error. */
+int cbm_discover_ex(const char *repo_path, const cbm_discover_opts_t *opts, cbm_file_info_t **out,
+ int *count, char ***excluded_out, int *excluded_count_out);
+
/* Free an array of file info results. NULL-safe. */
void cbm_discover_free(cbm_file_info_t *files, int count);
+/* Free the excluded-directory list returned by cbm_discover_ex(). NULL-safe. */
+void cbm_discover_free_excluded(char **excluded, int count);
+
#endif /* CBM_DISCOVER_H */
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 86fef0dfd..0c939d5d6 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -2495,10 +2495,40 @@ static void try_artifact_bootstrap(const char *project_name, const char *repo_pa
}
}
+/* Cap on excluded dir paths listed in the response — keep it compact on large
+ * repos (node_modules / vendor / etc. can produce many skip points). The full
+ * count is still reported via "count" + "truncated". */
+enum { INDEX_EXCLUDED_DIR_CAP = 25 };
+
+/* Attach a compact summary of directory subtrees skipped during discovery (#411).
+ * Shape: "excluded": {"dirs": [up to 25 rel-paths], "count": <total>, "truncated": <bool>}.
+ * No-op when nothing was excluded. excluded_dirs[] is borrowed (copied into doc). */
+static void add_excluded_summary(yyjson_mut_doc *doc, yyjson_mut_val *root, char **excluded_dirs,
+ int excluded_count) {
+ if (!excluded_dirs || excluded_count <= 0) {
+ return;
+ }
+ yyjson_mut_val *excluded = yyjson_mut_obj(doc);
+ yyjson_mut_val *dirs = yyjson_mut_arr(doc);
+ int shown = excluded_count < INDEX_EXCLUDED_DIR_CAP ? excluded_count : INDEX_EXCLUDED_DIR_CAP;
+ for (int i = 0; i < shown; i++) {
+ if (excluded_dirs[i]) {
+ yyjson_mut_arr_add_strcpy(doc, dirs, excluded_dirs[i]);
+ }
+ }
+ yyjson_mut_obj_add_val(doc, excluded, "dirs", dirs);
+ yyjson_mut_obj_add_int(doc, excluded, "count", excluded_count);
+ yyjson_mut_obj_add_bool(doc, excluded, "truncated", excluded_count > shown);
+ yyjson_mut_obj_add_val(doc, root, "excluded", excluded);
+}
+
/* Build the success portion of the index_repository response. */
static void build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *doc,
yyjson_mut_val *root, const char *project_name,
- const char *repo_path, bool persistence) {
+ const char *repo_path, bool persistence,
+ char **excluded_dirs, int excluded_count) {
+ add_excluded_summary(doc, root, excluded_dirs, excluded_count);
+
cbm_store_t *store = resolve_store(srv, project_name);
if (!store) {
return;
@@ -2586,7 +2616,13 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
srv->active_pipeline = NULL;
cbm_pipeline_unlock();
- cbm_pipeline_free(p);
+ /* Capture the excluded-subtree list (#411) while the pipeline (which owns
+ * the strings) is still alive — the response builder copies them into the
+ * JSON doc, so they need only outlive that call, not cbm_pipeline_free. */
+ char **excluded_dirs = NULL;
+ int excluded_count = 0;
+ cbm_pipeline_get_excluded(p, &excluded_dirs, &excluded_count);
+
cbm_mem_collect(); /* return mimalloc pages to OS after large indexing */
/* Invalidate cached store so next query reopens the fresh database */
@@ -2611,11 +2647,14 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
}
if (rc == 0) {
- build_index_success_response(srv, doc, root, project_name, repo_path, persistence);
+ build_index_success_response(srv, doc, root, project_name, repo_path, persistence,
+ excluded_dirs, excluded_count);
}
char *json = yy_doc_to_str(doc);
yyjson_mut_doc_free(doc);
+ /* Free the pipeline only after the response doc copied the excluded list. */
+ cbm_pipeline_free(p);
free(project_name);
free(repo_path);
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 95db2931e..4b02de9cd 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -82,6 +82,12 @@ struct cbm_pipeline {
cbm_gbuf_t *gbuf;
cbm_registry_t *registry;
+ /* Directory subtrees skipped during discovery (rel paths). Captured from
+ * cbm_discover_ex so the MCP layer can report excluded subtrees (#411).
+ * Owned by the pipeline; freed in cbm_pipeline_free. */
+ char **excluded_dirs;
+ int excluded_count;
+
/* User-defined extension overrides (loaded once per run) */
cbm_userconfig_t *userconfig;
};
@@ -161,6 +167,9 @@ void cbm_pipeline_free(cbm_pipeline_t *p) {
free(p->repo_path);
free(p->db_path);
free(p->project_name);
+ cbm_discover_free_excluded(p->excluded_dirs, p->excluded_count);
+ p->excluded_dirs = NULL;
+ p->excluded_count = 0;
/* gbuf, store, registry freed during/after run */
/* Defensively free userconfig in case run() was never called or panicked */
if (p->userconfig) {
@@ -193,6 +202,15 @@ int cbm_pipeline_get_mode(const cbm_pipeline_t *p) {
return p ? (int)p->mode : 0;
}
+void cbm_pipeline_get_excluded(const cbm_pipeline_t *p, char ***out, int *count) {
+ if (out) {
+ *out = p ? p->excluded_dirs : NULL;
+ }
+ if (count) {
+ *count = p ? p->excluded_count : 0;
+ }
+}
+
/* Resolve the DB path for this pipeline. Caller must free(). */
static char *resolve_db_path(const cbm_pipeline_t *p) {
char *path = malloc(CBM_SZ_1K);
@@ -958,7 +976,14 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
};
cbm_file_info_t *files = NULL;
int file_count = 0;
- int rc = cbm_discover(p->repo_path, &opts, &files, &file_count);
+ /* Capture skipped subtrees on the pipeline so the MCP layer can report
+ * which directories were excluded (#411). Replace any prior list (e.g. a
+ * re-run on the same pipeline) to avoid leaking the previous one. */
+ cbm_discover_free_excluded(p->excluded_dirs, p->excluded_count);
+ p->excluded_dirs = NULL;
+ p->excluded_count = 0;
+ int rc = cbm_discover_ex(p->repo_path, &opts, &files, &file_count, &p->excluded_dirs,
+ &p->excluded_count);
if (rc != 0) {
cbm_log_error("pipeline.err", "phase", "discover", "rc", itoa_buf(rc));
}
diff --git a/src/pipeline/pipeline.h b/src/pipeline/pipeline.h
index d48857554..1aa5ab5f7 100644
--- a/src/pipeline/pipeline.h
+++ b/src/pipeline/pipeline.h
@@ -66,6 +66,12 @@ const char *cbm_pipeline_project_name(const cbm_pipeline_t *p);
/* Get the index mode (CBM_MODE_FULL, CBM_MODE_MODERATE, CBM_MODE_FAST). */
int cbm_pipeline_get_mode(const cbm_pipeline_t *p);
+/* Get the list of directory subtrees skipped during discovery (#411).
+ * *out receives a borrowed array of rel-path strings (owned by the pipeline,
+ * valid until cbm_pipeline_free()); *count receives its length. Both are set
+ * to NULL/0 when p is NULL or nothing was excluded. Do not free. */
+void cbm_pipeline_get_excluded(const cbm_pipeline_t *p, char ***out, int *count);
+
/* ── Index lock (prevents concurrent pipeline runs on same DB) ──── */
/* Try to acquire the global index lock. Returns true if acquired,
diff --git a/tests/test_integration.c b/tests/test_integration.c
index 3fdd24f67..de0d22008 100644
--- a/tests/test_integration.c
+++ b/tests/test_integration.c
@@ -524,11 +524,59 @@ TEST(integ_store_bfs_traversal) {
PASS();
}
+/* #411: index_repository silently drops entire subtrees with no record.
+ * Moderate/fast mode applies FAST_SKIP_DIRS (tools/scripts/bin/docs/...) and ALL
+ * modes apply ALWAYS_SKIP_DIRS (node_modules/...), so files are excluded from the
+ * graph — but the response only reports nodes/edges/status, giving the user no
+ * way to know which subtrees were dropped (the reporter lost a 47-file tools/).
+ * Desired (maintainer): a COMPACT per-directory summary of excluded files (dir +
+ * count, not a verbose per-file list) surfaced in the index result for any mode.
+ * RED until the index response reports excluded subtrees. Self-contained. */
+TEST(index_reports_excluded_subtrees_issue411) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_excl_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+
+ char path[512];
+ /* one real source file ... */
+ snprintf(path, sizeof(path), "%s/app.py", tmp);
+ FILE *f = fopen(path, "wb");
+ ASSERT_NOT_NULL(f);
+ fputs("def app():\n return 1\n", f);
+ fclose(f);
+ /* ... and a node_modules subtree that is excluded in EVERY mode. */
+ snprintf(path, sizeof(path), "%s/node_modules", tmp);
+ cbm_mkdir_p(path, 0755);
+ snprintf(path, sizeof(path), "%s/node_modules/dep.js", tmp);
+ f = fopen(path, "wb");
+ ASSERT_NOT_NULL(f);
+ fputs("export function dep() { return 2; }\n", f);
+ fclose(f);
+
+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
+ ASSERT_NOT_NULL(srv);
+ char args[600];
+ snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", tmp);
+ char *resp = cbm_mcp_handle_tool(srv, "index_repository", args);
+ ASSERT_NOT_NULL(resp);
+
+ /* The dropped node_modules/dep.js must be reported somewhere compact in the
+ * response so the user knows it wasn't indexed. Today the response carries no
+ * excluded/skipped summary at all → this fails (reproduces the silent drop). */
+ bool reports_excluded = strstr(resp, "excluded") != NULL || strstr(resp, "skipped") != NULL;
+ free(resp);
+ cbm_mcp_server_free(srv);
+ th_rmtree(tmp);
+ ASSERT_TRUE(reports_excluded);
+ PASS();
+}
+
/* ══════════════════════════════════════════════════════════════════
* SUITE
* ══════════════════════════════════════════════════════════════════ */
SUITE(integration) {
+ RUN_TEST(index_reports_excluded_subtrees_issue411);
/* Set up: create temp project and index it */
if (integration_setup() != 0) {
/* A suite that cannot establish its preconditions has FAILED, not