Skip to content

Commit a8abd97

Browse files
committed
fix(mcp): discover projects with underscore-prefixed names
Use the shared exact-name database predicate for all five MCP cache scans, including reindex root ownership. Preserve exclusions for the two internal stores and cover listing, lookup, hints, and root reuse. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 96c3f41 commit a8abd97

2 files changed

Lines changed: 192 additions & 35 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ enum {
1919
MCP_COL_7 = 7,
2020
MCP_COL_10 = 10,
2121
MCP_COL_16 = 16,
22-
MCP_DB_EXT = 3, /* strlen(".db") */
23-
MCP_MIN_DB_NAME = 4, /* min length for "x.db" */
24-
MCP_SEPARATOR = 2, /* space for separator chars */
22+
MCP_DB_EXT = 3, /* strlen(".db") */
23+
MCP_SEPARATOR = 2, /* space for separator chars */
2524
MCP_DEFAULT_DEPTH = 3,
2625
MCP_DEFAULT_BFS_DEPTH = 2,
2726
MCP_DEFAULT_LIMIT = 10,
@@ -1594,7 +1593,6 @@ static char *normalize_project_arg(char *project) {
15941593

15951594
/* Forward decls — defined below alongside store resolution. */
15961595
static const char *cache_dir(char *buf, size_t bufsz);
1597-
static bool is_project_db_file(const char *name, size_t len);
15981596

15991597
/* #1025: agents naturally pass the repo FOLDER name ("codebase-memory-mcp"),
16001598
* but indexed project names derive from the full path
@@ -1626,7 +1624,7 @@ static char *resolve_project_tail(char *project) {
16261624
while ((entry = cbm_readdir(d)) != NULL) {
16271625
const char *n = entry->name;
16281626
size_t len = strlen(n);
1629-
if (!is_project_db_file(n, len)) {
1627+
if (!cbm_is_project_index_db(n)) {
16301628
continue;
16311629
}
16321630
size_t stem_len = len - MCP_DB_EXT; /* strip ".db" */
@@ -2158,14 +2156,14 @@ static const char *project_db_path(const char *project, char *buf, size_t bufsz)
21582156
* /corrupt dbs (0-byte file, missing `projects` table, or >1 row). On success
21592157
* the internal name is copied into name_out; if out_store is non-NULL the open
21602158
* handle is transferred to the caller (who must cbm_store_close it). On failure
2161-
* the store is always closed. Defined after is_project_db_file below. */
2159+
* the store is always closed. Defined below. */
21622160
static bool db_internal_project_name(const char *full_path, char *name_out, size_t name_sz,
21632161
cbm_store_t **out_store);
21642162

21652163
/* #704 fallback: scan the cache dir for the db whose sole internal project name
21662164
* equals `project`, returning an open store handle (caller owns it) or NULL.
21672165
* Used only when <project>.db is absent or its internal name differs from the
2168-
* passed name (drifted filename). Defined after is_project_db_file below. */
2166+
* passed name (drifted filename). Defined below. */
21692167
static cbm_store_t *resolve_store_fallback_scan(const char *project);
21702168

21712169
static bool reserve_unique_corrupt_pending(const char *path, char *pending, size_t pending_size,
@@ -2544,9 +2542,6 @@ static cbm_store_t *resolve_store(cbm_mcp_server_t *srv, const char *project) {
25442542
return resolve_store_internal(srv, project, false, false, NULL, false);
25452543
}
25462544

2547-
/* Forward decl — definition lives below alongside list_projects. */
2548-
static bool is_project_db_file(const char *name, size_t len);
2549-
25502545
/* Forward decl — definition lives below in handle_trace_call_path's helpers. */
25512546
static void free_node_contents(cbm_node_t *n);
25522547

@@ -2562,8 +2557,7 @@ static int collect_db_project_names(const char *dir_path, char *out, size_t out_
25622557
cbm_dirent_t *entry;
25632558
while ((entry = cbm_readdir(d)) != NULL) {
25642559
const char *n = entry->name;
2565-
size_t len = strlen(n);
2566-
if (!is_project_db_file(n, len)) {
2560+
if (!cbm_is_project_index_db(n)) {
25672561
continue;
25682562
}
25692563
/* #704: advertise the db's INTERNAL project name, not its filename, and
@@ -2718,23 +2712,6 @@ static bool project_has_adr(cbm_store_t *store, const char *project, const char
27182712

27192713
/* ── Tool handler implementations ─────────────────────────────── */
27202714

2721-
/* Return true if filename is a valid project .db file (not temp/internal).
2722-
*
2723-
* Project names derived from /tmp/... source roots legitimately begin with
2724-
* "tmp-" (cbm_project_name_from_path: "/tmp/bench/..." → "tmp-bench-...";
2725-
* see tests/test_pipeline.c fixtures), so the prefix must NOT be excluded.
2726-
* The "_" prefix is reserved for internal/hidden DBs, and ":memory:" is the
2727-
* SQLite in-memory marker (defensive — never appears as a real file). */
2728-
static bool is_project_db_file(const char *name, size_t len) {
2729-
if (len < MCP_MIN_DB_NAME || strcmp(name + len - MCP_DB_EXT, ".db") != 0) {
2730-
return false;
2731-
}
2732-
if (strncmp(name, "_", SLEN("_")) == 0 || strncmp(name, ":memory:", SLEN(":memory:")) == 0) {
2733-
return false;
2734-
}
2735-
return true;
2736-
}
2737-
27382715
/* db_internal_project_name — see forward declaration above resolve_store. */
27392716
static bool db_internal_project_name(const char *full_path, char *name_out, size_t name_sz,
27402717
cbm_store_t **out_store) {
@@ -2787,8 +2764,7 @@ static cbm_store_t *resolve_store_fallback_scan(const char *project) {
27872764
cbm_dirent_t *entry;
27882765
while ((entry = cbm_readdir(d)) != NULL) {
27892766
const char *n = entry->name;
2790-
size_t len = strlen(n);
2791-
if (!is_project_db_file(n, len)) {
2767+
if (!cbm_is_project_index_db(n)) {
27922768
continue;
27932769
}
27942770
char full_path[CBM_SZ_2K];
@@ -3002,8 +2978,7 @@ static char *handle_list_projects(cbm_mcp_server_t *srv, const char *args) {
30022978
cbm_dirent_t *entry;
30032979
while ((entry = cbm_readdir(d)) != NULL) {
30042980
const char *name = entry->name;
3005-
size_t len = strlen(name);
3006-
if (!is_project_db_file(name, len)) {
2981+
if (!cbm_is_project_index_db(name)) {
30072982
continue;
30082983
}
30092984
char full_path[CBM_SZ_2K];
@@ -11315,8 +11290,7 @@ static bool index_root_owner_resolve(const char *repo_path, char **owner_out, ch
1131511290
bool ok = true;
1131611291
cbm_dirent_t *entry;
1131711292
while (ok && !derived_exists && (entry = cbm_readdir(d)) != NULL) {
11318-
size_t len = strlen(entry->name);
11319-
if (!is_project_db_file(entry->name, len)) {
11293+
if (!cbm_is_project_index_db(entry->name)) {
1132011294
continue;
1132111295
}
1132211296
mcp_project_record_t record = {0};

‎tests/test_mcp.c‎

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../src/foundation/log.h"
1111
#include "../src/foundation/platform.h" /* cbm_file_size */
1212
#include "../src/foundation/mem.h" /* cbm_mem_set_budget_for_tests — over-budget seam */
13+
#include "../src/foundation/str_util.h"
1314
#include "../src/foundation/subprocess.h"
1415
#include "../src/foundation/workspace.h"
1516
#include "../src/mcp/compact_out.h"
@@ -17734,6 +17735,186 @@ static bool issue704_make_db(const char *dir, const char *filename, const char *
1773417735
return ok;
1773517736
}
1773617737

17738+
/* Cache entry classification must use exact internal names. CI workspace
17739+
* paths can produce real project names beginning with one or more underscores. */
17740+
TEST(tool_underscore_projects_visible_in_cache_scans) {
17741+
char cache[CBM_SZ_512];
17742+
int path_len = snprintf(cache, sizeof(cache), "%s/cbm-uscore-cache-XXXXXX", cbm_tmpdir());
17743+
ASSERT_TRUE(path_len > 0 && (size_t)path_len < sizeof(cache));
17744+
ASSERT_NOT_NULL(cbm_mkdtemp(cache));
17745+
const char *saved = getenv("CBM_CACHE_DIR");
17746+
char *saved_copy = saved ? cbm_strdup(saved) : NULL;
17747+
bool setup = (!saved || saved_copy) && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0;
17748+
bool seeded = setup &&
17749+
issue704_make_db(cache, "__w-org-uscorerepo.db", "__w-org-uscorerepo",
17750+
"uscore_target_fn") &&
17751+
issue704_make_db(cache, "_renamed.db", "uscore-drift", "uscore_drift_fn") &&
17752+
issue704_make_db(cache, "_config-extra.db", "_config-extra", "uscore_extra_fn") &&
17753+
issue704_make_db(cache, CBM_CONFIG_DB_FILENAME, "hidden-config", "hidden_fn") &&
17754+
issue704_make_db(cache, CBM_CROSS_REPO_DB_FILENAME, "hidden-cross", "hidden_fn");
17755+
cbm_mcp_server_t *srv = seeded ? cbm_mcp_server_new(NULL) : NULL;
17756+
bool server_created = srv != NULL;
17757+
bool listed = false;
17758+
bool queried[3] = {false};
17759+
bool hinted = false;
17760+
bool hidden = false;
17761+
if (srv) {
17762+
char *response = cbm_mcp_handle_tool(srv, "list_projects", "{\"format\":\"json\"}");
17763+
char *inner = response ? extract_text_content(response) : NULL;
17764+
yyjson_doc *doc = inner ? yyjson_read(inner, strlen(inner), 0) : NULL;
17765+
if (doc) {
17766+
yyjson_val *root = yyjson_doc_get_root(doc);
17767+
yyjson_val *projects = yyjson_obj_get(root, "projects");
17768+
bool found[3] = {false};
17769+
const char *expected[] = {"__w-org-uscorerepo", "uscore-drift", "_config-extra"};
17770+
for (size_t i = 0; i < yyjson_arr_size(projects); i++) {
17771+
const char *name =
17772+
yyjson_get_str(yyjson_obj_get(yyjson_arr_get(projects, i), "name"));
17773+
for (size_t j = 0; j < 3; j++) {
17774+
found[j] = found[j] || (name && strcmp(name, expected[j]) == 0);
17775+
}
17776+
}
17777+
listed = yyjson_arr_size(projects) == 3 &&
17778+
yyjson_get_int(yyjson_obj_get(root, "total")) == 3 && found[0] && found[1] &&
17779+
found[2];
17780+
yyjson_doc_free(doc);
17781+
}
17782+
free(inner);
17783+
free(response);
17784+
const char *args[] = {
17785+
"{\"project\":\"__w-org-uscorerepo\",\"name_pattern\":\"uscore_target_fn\"}",
17786+
"{\"project\":\"uscorerepo\",\"name_pattern\":\"uscore_target_fn\"}",
17787+
"{\"project\":\"uscore-drift\",\"name_pattern\":\"uscore_drift_fn\"}"};
17788+
const char *targets[] = {"uscore_target_fn", "uscore_target_fn", "uscore_drift_fn"};
17789+
for (size_t i = 0; i < 3; i++) {
17790+
response = cbm_mcp_handle_tool(srv, "search_graph", args[i]);
17791+
queried[i] = response && !strstr(response, "\"isError\":true") &&
17792+
!strstr(response, "project not found") && strstr(response, targets[i]);
17793+
free(response);
17794+
}
17795+
response =
17796+
cbm_mcp_handle_tool(srv, "search_graph",
17797+
"{\"project\":\"unknown-uscore-project\",\"name_pattern\":\".*\"}");
17798+
hinted = response && strstr(response, "project not found") &&
17799+
strstr(response, "__w-org-uscorerepo") && strstr(response, "uscore-drift") &&
17800+
strstr(response, "_config-extra") && !strstr(response, "hidden-config") &&
17801+
!strstr(response, "hidden-cross");
17802+
free(response);
17803+
response = cbm_mcp_handle_tool(
17804+
srv, "search_graph", "{\"project\":\"hidden-config\",\"name_pattern\":\"hidden_fn\"}");
17805+
hidden = response && strstr(response, "project not found");
17806+
free(response);
17807+
}
17808+
cbm_mcp_server_free(srv);
17809+
bool restored = !setup || (saved_copy ? cbm_setenv("CBM_CACHE_DIR", saved_copy, 1)
17810+
: cbm_unsetenv("CBM_CACHE_DIR")) == 0;
17811+
free(saved_copy);
17812+
bool removed = th_rmtree(cache) == 0;
17813+
ASSERT_TRUE(setup && seeded && server_created);
17814+
ASSERT_TRUE(restored && removed);
17815+
ASSERT_TRUE(listed);
17816+
ASSERT_TRUE(queried[0] && queried[1] && queried[2]);
17817+
ASSERT_TRUE(hinted && hidden);
17818+
PASS();
17819+
}
17820+
17821+
/* The fifth cache scan adopts an existing root owner before reindexing.
17822+
* Ignoring its underscore name would create a second path-derived project. */
17823+
TEST(tool_reindex_reuses_underscore_root_owner) {
17824+
char repo[CBM_SZ_4K];
17825+
char cache[CBM_SZ_4K];
17826+
int repo_len = snprintf(repo, sizeof(repo), "%s/cbm-uscore-repo-XXXXXX", cbm_tmpdir());
17827+
int cache_len = snprintf(cache, sizeof(cache), "%s/cbm-uscore-root-cache-XXXXXX", cbm_tmpdir());
17828+
ASSERT_TRUE(repo_len > 0 && (size_t)repo_len < sizeof(repo));
17829+
ASSERT_TRUE(cache_len > 0 && (size_t)cache_len < sizeof(cache));
17830+
bool repo_created = cbm_mkdtemp(repo) != NULL;
17831+
bool cache_created = repo_created && cbm_mkdtemp(cache) != NULL;
17832+
char canonical[CBM_SZ_4K];
17833+
bool setup = cache_created && cbm_canonical_path(repo, canonical, sizeof(canonical));
17834+
if (setup) {
17835+
cbm_normalize_path_sep(canonical);
17836+
}
17837+
const char *keys[] = {"CBM_CACHE_DIR", "CBM_INDEX_SUPERVISOR"};
17838+
char *saved[2] = {NULL};
17839+
bool present[2] = {false};
17840+
for (size_t i = 0; i < 2; i++) {
17841+
const char *value = getenv(keys[i]);
17842+
present[i] = value != NULL;
17843+
saved[i] = value ? cbm_strdup(value) : NULL;
17844+
setup = setup && (!value || saved[i]);
17845+
}
17846+
bool env_changed = setup;
17847+
if (setup) {
17848+
setup = cbm_setenv(keys[0], cache, 1) == 0 && cbm_setenv(keys[1], "0", 1) == 0;
17849+
}
17850+
char path[CBM_SZ_4K];
17851+
int length = setup ? snprintf(path, sizeof(path), "%s/mod.py", repo) : -1;
17852+
FILE *source = length > 0 && (size_t)length < sizeof(path) ? cbm_fopen(path, "w") : NULL;
17853+
bool written = source && fputs("def underscore_root_target():\n return 1\n", source) >= 0;
17854+
if (source) {
17855+
written = fclose(source) == 0 && written;
17856+
}
17857+
length = setup ? snprintf(path, sizeof(path), "%s/_root-owner.db", cache) : -1;
17858+
cbm_store_t *store =
17859+
written && length > 0 && (size_t)length < sizeof(path) ? cbm_store_open_path(path) : NULL;
17860+
bool seeded =
17861+
store && cbm_store_upsert_project(store, "_root-owner", canonical) == CBM_STORE_OK;
17862+
if (store) {
17863+
cbm_store_close(store);
17864+
}
17865+
cbm_mcp_server_t *srv = seeded ? cbm_mcp_server_new(NULL) : NULL;
17866+
bool server_created = srv != NULL;
17867+
bool session_bound = srv && cbm_mcp_server_set_session_context(srv, canonical, canonical);
17868+
bool indexed = false;
17869+
bool resolved = false;
17870+
bool no_duplicate = false;
17871+
if (session_bound) {
17872+
yyjson_mut_doc *args = yyjson_mut_doc_new(NULL);
17873+
yyjson_mut_val *object = args ? yyjson_mut_obj(args) : NULL;
17874+
char *serialized = NULL;
17875+
if (object) {
17876+
yyjson_mut_doc_set_root(args, object);
17877+
if (yyjson_mut_obj_add_str(args, object, "repo_path", canonical)) {
17878+
serialized = yyjson_mut_write(args, 0, NULL);
17879+
}
17880+
}
17881+
char *response =
17882+
serialized ? cbm_mcp_handle_tool(srv, "index_repository", serialized) : NULL;
17883+
indexed =
17884+
response && !strstr(response, "\"isError\":true") && strstr(response, "_root-owner");
17885+
free(response);
17886+
free(serialized);
17887+
yyjson_mut_doc_free(args);
17888+
response = cbm_mcp_handle_tool(
17889+
srv, "search_graph",
17890+
"{\"project\":\"_root-owner\",\"name_pattern\":\"underscore_root_target\"}");
17891+
resolved = response && !strstr(response, "\"isError\":true") &&
17892+
strstr(response, "underscore_root_target") &&
17893+
!strstr(response, "project not found");
17894+
free(response);
17895+
char *derived = cbm_project_name_from_path(canonical);
17896+
length = derived ? snprintf(path, sizeof(path), "%s/%s.db", cache, derived) : -1;
17897+
no_duplicate = derived && strcmp(derived, "_root-owner") != 0 && length > 0 &&
17898+
(size_t)length < sizeof(path) && !cbm_file_exists(path);
17899+
free(derived);
17900+
}
17901+
cbm_mcp_server_free(srv);
17902+
bool restored = true;
17903+
for (size_t i = 0; i < 2; i++) {
17904+
if (env_changed) {
17905+
int rc = present[i] ? cbm_setenv(keys[i], saved[i], 1) : cbm_unsetenv(keys[i]);
17906+
restored = rc == 0 && restored;
17907+
}
17908+
free(saved[i]);
17909+
}
17910+
bool repo_removed = !repo_created || th_rmtree(repo) == 0;
17911+
bool cache_removed = !cache_created || th_rmtree(cache) == 0;
17912+
ASSERT_TRUE(setup && written && seeded && server_created && session_bound);
17913+
ASSERT_TRUE(restored && repo_removed && cache_removed);
17914+
ASSERT_TRUE(indexed && resolved && no_duplicate);
17915+
PASS();
17916+
}
17917+
1773717918
TEST(tool_resolve_store_by_internal_name_issue704) {
1773817919
char cache[256];
1773917920
snprintf(cache, sizeof(cache), "/tmp/cbm-issue704-XXXXXX");
@@ -21350,6 +21531,8 @@ SUITE(mcp) {
2135021531
RUN_TEST(snippet_source_invalid_utf8);
2135121532
RUN_TEST(tool_bad_project_name_no_overflow_issue235);
2135221533
RUN_TEST(tool_bad_project_error_valid_json_issue235);
21534+
RUN_TEST(tool_underscore_projects_visible_in_cache_scans);
21535+
RUN_TEST(tool_reindex_reuses_underscore_root_owner);
2135321536
RUN_TEST(tool_resolve_store_by_internal_name_issue704);
2135421537
RUN_TEST(tool_list_projects_ignores_missed_shadow_issue1044);
2135521538

0 commit comments

Comments
 (0)