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(search): scope scans to canonical source files

Martin Vogelcc9c2f4c0f51
diff --git a/src/store/store.c b/src/store/store.c
index cbac03d70..5061fcd11 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -4465,8 +4465,17 @@ int cbm_store_list_files(cbm_store_t *s, const char *project, char ***out, int *
return CBM_STORE_ERR;
}
+ /* A node path can name a graph-only identity such as a Folder,
+ * <python-builtins>, or another synthetic source. Published indexes have
+ * one canonical File node per scannable path, so prefer that exact set.
+ * Legacy/manual stores without File nodes retain their previous node-path
+ * fallback (minus Folder directories). Missing canonical paths stay in
+ * the list so search_code still fails closed on a stale snapshot. */
const char *sql = "SELECT DISTINCT file_path FROM nodes "
- "WHERE project = ?1 AND file_path IS NOT NULL AND file_path != '' "
+ "WHERE project = ?1 AND label != 'Folder' "
+ "AND file_path IS NOT NULL AND file_path != '' "
+ "AND (label = 'File' OR NOT EXISTS ("
+ "SELECT 1 FROM nodes WHERE project = ?1 AND label = 'File')) "
"ORDER BY file_path";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
diff --git a/src/store/store.h b/src/store/store.h
index b3acc1b0f..353227f09 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -108,8 +108,8 @@ int cbm_store_find_nodes_by_qn_suffix(cbm_store_t *s, const char *project, const
/* Get CALLS degree of a node (inbound and outbound). */
void cbm_store_node_degree(cbm_store_t *s, int64_t node_id, int *in_deg, int *out_deg);
-/* Get distinct file paths for a project. Caller must free each out[i] and out itself.
- * Returns CBM_STORE_OK or CBM_STORE_ERR. */
+/* Get distinct canonical File-node paths, with a non-Folder node fallback for legacy/manual
+ * stores that have no File nodes. Caller frees each out[i] and out itself. */
int cbm_store_list_files(cbm_store_t *s, const char *project, char ***out, int *count);
/* Persisted index-format identity. Bump when a change alters the QN scheme
diff --git a/tests/test_incremental.c b/tests/test_incremental.c
index fed067f49..498d912b9 100644
--- a/tests/test_incremental.c
+++ b/tests/test_incremental.c
@@ -916,6 +916,16 @@ TEST(incr_perf_single_file_fast) {
}
delete_file_at("fastapi/incr_perf_probe.py");
+ /* Publish the cleanup too. search_code now consumes the complete scoped
+ * file list and fails closed when the graph names a source file that grep
+ * cannot read; leaving this probe indexed would make every later matching
+ * search correctly report an incomplete snapshot instead of exercising
+ * the search parameter contract this fixture is meant to cover. The
+ * cleanup reindex is deliberately outside the measured interval above. */
+ resp = index_repo();
+ ASSERT(resp != NULL);
+ ASSERT(strstr(resp, "indexed") != NULL);
+ free(resp);
PASS();
}
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index cdab338dc..0a087e314 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -8501,6 +8501,73 @@ TEST(search_code_fails_closed_when_complete_scan_is_impossible) {
PASS();
}
+TEST(search_code_scoped_scan_uses_canonical_file_nodes) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "%s/cbm_srch_folder_XXXXXX", cbm_tmpdir());
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+
+ char source_dir[512];
+ char source_path[640];
+ snprintf(source_dir, sizeof(source_dir), "%s/src", tmp);
+ snprintf(source_path, sizeof(source_path), "%s/main.c", source_dir);
+ ASSERT_EQ(cbm_mkdir(source_dir), 0);
+ ASSERT_EQ(th_write_file(source_path, "int FOLDER_SCOPE_NEEDLE = 1;\n"), 0);
+
+ 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 = "search-folder-scope";
+ cbm_mcp_server_set_project(srv, project);
+ ASSERT_EQ(cbm_store_upsert_project(store, project, tmp), CBM_STORE_OK);
+
+ /* Graph-only nodes may carry directory or synthetic file_path identities.
+ * Neither belongs in the content scanner's canonical File-node set. */
+ cbm_node_t folder = {.project = project,
+ .label = "Folder",
+ .name = "src",
+ .qualified_name = "search-folder-scope.src",
+ .file_path = "src"};
+ cbm_node_t builtin = {.project = project,
+ .label = "Function",
+ .name = "len",
+ .qualified_name = "builtins.len",
+ .file_path = "<python-builtins>",
+ .start_line = 1,
+ .end_line = 1};
+ cbm_node_t file = {.project = project,
+ .label = "File",
+ .name = "main.c",
+ .qualified_name = "search-folder-scope.src.main.c",
+ .file_path = "src/main.c"};
+ cbm_node_t symbol = {.project = project,
+ .label = "Variable",
+ .name = "FOLDER_SCOPE_NEEDLE",
+ .qualified_name = "search-folder-scope.src.FOLDER_SCOPE_NEEDLE",
+ .file_path = "src/main.c",
+ .start_line = 1,
+ .end_line = 1};
+ ASSERT_GT(cbm_store_upsert_node(store, &folder), 0);
+ ASSERT_GT(cbm_store_upsert_node(store, &builtin), 0);
+ ASSERT_GT(cbm_store_upsert_node(store, &file), 0);
+ ASSERT_GT(cbm_store_upsert_node(store, &symbol), 0);
+
+ char *response = cbm_mcp_handle_tool(
+ srv, "search_code",
+ "{\"pattern\":\"FOLDER_SCOPE_NEEDLE\",\"project\":\"search-folder-scope\","
+ "\"format\":\"json\"}");
+ ASSERT_NOT_NULL(response);
+ ASSERT_NULL(strstr(response, "\"isError\":true"));
+ ASSERT_NOT_NULL(strstr(response, "search-folder-scope.src.FOLDER_SCOPE_NEEDLE"));
+
+ free(response);
+ cbm_mcp_server_free(srv);
+ cbm_unlink(source_path);
+ cbm_rmdir(source_dir);
+ cbm_rmdir(tmp);
+ PASS();
+}
+
TEST(search_code_recursive_fallback_propagates_discovery_failures) {
#ifdef _WIN32
SKIP_PLATFORM("POSIX find/sort/xargs fallback contract");
@@ -19695,6 +19762,7 @@ SUITE(mcp) {
RUN_TEST(search_code_preserves_valid_utf8_source);
RUN_TEST(search_code_scans_complete_stream_and_ranks_globally);
RUN_TEST(search_code_fails_closed_when_complete_scan_is_impossible);
+ RUN_TEST(search_code_scoped_scan_uses_canonical_file_nodes);
RUN_TEST(search_code_recursive_fallback_propagates_discovery_failures);
RUN_TEST(search_code_default_budget_limits_raw_rows_before_graph_results);
RUN_TEST(search_code_raw_and_directory_remainders_are_independently_pageable);