Skip to content

Commit 155d147

Browse files
committed
fix(mcp): reuse the project that owns a root on unnamed re-index (DeusData#2134)
index_repository without `name` derived the project key from the path alone. When the root had first been indexed under an explicit name, a later unnamed re-index forked a second index (same root_path, path-derived name) and left the original untouched, so the stale index kept being served while the call reported success. Resolve the key from the indexes on disk before any work starts: - the path-derived project exists -> keep it (unchanged behavior); - exactly one other project owns the canonical root -> adopt its name and update it in place, so existing indexes are never orphaned; - several projects own the root -> fail loudly, name them and ask for `name` instead of guessing or forking again; - none -> first index under the derived name, as before. The resolved name is written into the args handed to the daemon job coordinator and the supervised worker, so the project lease key and the index actually written stay the same project. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 0f52d30 commit 155d147

2 files changed

Lines changed: 248 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11157,8 +11157,12 @@ static bool resolve_session_repo_path(cbm_mcp_server_t *srv, char **repo_path) {
1115711157
}
1115811158

1115911159
/* Preserve every index option while replacing all caller-supplied repo_path
11160-
* keys with the one canonical path that was actually authorized. */
11160+
* keys with the one canonical path that was actually authorized. A non-empty
11161+
* project_name likewise replaces any caller "name" (#2134: the project key the
11162+
* parent resolved for this root travels to the daemon job / worker verbatim,
11163+
* so lease key and written index cannot diverge). */
1116111164
static char *index_args_with_repo_path(const char *args, const char *canonical_repo_path,
11165+
const char *project_name,
1116211166
const cbm_index_resource_policy_t *policy) {
1116311167
if (!args || !canonical_repo_path || !policy) {
1116411168
return NULL;
@@ -11193,7 +11197,12 @@ static char *index_args_with_repo_path(const char *args, const char *canonical_r
1119311197
(void)yyjson_mut_obj_remove_key(copy_root, call_mode_keys[i]);
1119411198
}
1119511199
}
11196-
if (!yyjson_mut_obj_add_strcpy(copy, copy_root, "repo_path", canonical_repo_path) ||
11200+
bool has_name = project_name && project_name[0];
11201+
while (has_name && yyjson_mut_obj_get(copy_root, "name")) {
11202+
(void)yyjson_mut_obj_remove_key(copy_root, "name");
11203+
}
11204+
if ((has_name && !yyjson_mut_obj_add_strcpy(copy, copy_root, "name", project_name)) ||
11205+
!yyjson_mut_obj_add_strcpy(copy, copy_root, "repo_path", canonical_repo_path) ||
1119711206
!cbm_mcp_index_policy_add_to_args(copy, copy_root, policy)) {
1119811207
yyjson_mut_doc_free(copy);
1119911208
return NULL;
@@ -11246,6 +11255,117 @@ static bool project_db_is_servable(const char *project, const char *db_path) {
1124611255
return servable;
1124711256
}
1124811257

11258+
/* #2134: an index_repository call without `name` derived the project key from
11259+
* the path alone, so re-indexing a root that was first indexed under an
11260+
* explicit name silently forked a second index (same root_path, path-derived
11261+
* name) and left the original stale while it kept being served. Resolve the
11262+
* key from the indexes already on disk instead:
11263+
* - the path-derived project exists -> keep it (unchanged path);
11264+
* - exactly one other project owns this root -> adopt its name (update it);
11265+
* - several other projects own this root -> ambiguous: fail loudly,
11266+
* naming them, never guess;
11267+
* - none -> first index, derived name.
11268+
* Roots compare as stored: both sides are the canonical repo path. Returns
11269+
* false only for the ambiguous / allocation-failure case, with *error_out set
11270+
* to a heap message; *owner_out is a heap name or NULL. */
11271+
static bool index_root_owner_append(char **list, size_t *len, size_t *cap, const char *name) {
11272+
size_t need = *len + strlen(name) + 3;
11273+
if (need > *cap) {
11274+
size_t grown_cap = need * 2;
11275+
char *grown = realloc(*list, grown_cap);
11276+
if (!grown) {
11277+
return false;
11278+
}
11279+
*list = grown;
11280+
*cap = grown_cap;
11281+
}
11282+
*len += (size_t)snprintf(*list + *len, *cap - *len, "%s%s", *len ? ", " : "", name);
11283+
return true;
11284+
}
11285+
11286+
static bool index_root_owner_resolve(const char *repo_path, char **owner_out, char **error_out) {
11287+
*owner_out = NULL;
11288+
*error_out = NULL;
11289+
char *derived = cbm_project_name_from_path(repo_path);
11290+
if (!derived) {
11291+
return true; /* the caller reports the derivation failure itself */
11292+
}
11293+
char derived_db[CBM_SZ_1K];
11294+
project_db_path(derived, derived_db, sizeof(derived_db));
11295+
if (derived_db[0] && cbm_file_exists(derived_db)) {
11296+
free(derived);
11297+
return true; /* fast path: the path-derived project is already on disk */
11298+
}
11299+
char dir_path[CBM_SZ_1K];
11300+
cache_dir(dir_path, sizeof(dir_path));
11301+
cbm_dir_t *d = cbm_opendir(dir_path);
11302+
if (!d) {
11303+
free(derived);
11304+
return true;
11305+
}
11306+
char *owner = NULL;
11307+
char *owners = NULL;
11308+
size_t owners_len = 0;
11309+
size_t owners_cap = 0;
11310+
int owner_count = 0;
11311+
bool derived_exists = false;
11312+
bool ok = true;
11313+
cbm_dirent_t *entry;
11314+
while (ok && !derived_exists && (entry = cbm_readdir(d)) != NULL) {
11315+
size_t len = strlen(entry->name);
11316+
if (!is_project_db_file(entry->name, len)) {
11317+
continue;
11318+
}
11319+
mcp_project_record_t record = {0};
11320+
project_record_status_t status =
11321+
read_project_record_identity(dir_path, entry->name, 0, &record);
11322+
if (status == PROJECT_RECORD_OOM) {
11323+
ok = false;
11324+
break;
11325+
}
11326+
if (status != PROJECT_RECORD_OK) {
11327+
continue;
11328+
}
11329+
if (strcmp(record.name, derived) == 0) {
11330+
derived_exists = true;
11331+
} else if (strcmp(record.root_path, repo_path) == 0) {
11332+
owner_count++;
11333+
ok = index_root_owner_append(&owners, &owners_len, &owners_cap, record.name);
11334+
if (owner_count == 1 && ok) {
11335+
owner = heap_strdup(record.name);
11336+
ok = owner != NULL;
11337+
}
11338+
}
11339+
project_record_clear(&record);
11340+
}
11341+
cbm_closedir(d);
11342+
free(derived);
11343+
11344+
if (ok && !derived_exists && owner_count == 1) {
11345+
free(owners);
11346+
*owner_out = owner;
11347+
return true;
11348+
}
11349+
free(owner);
11350+
if (ok && (derived_exists || owner_count == 0)) {
11351+
free(owners);
11352+
return true;
11353+
}
11354+
char msg[CBM_SZ_4K];
11355+
if (ok) {
11356+
snprintf(msg, sizeof(msg),
11357+
"several indexed projects share root_path %s: %s. Pass name=<project> to "
11358+
"choose which one to re-index (or delete_project the stale ones).",
11359+
repo_path, owners);
11360+
} else {
11361+
snprintf(msg, sizeof(msg), "out of memory while resolving the project for root_path %s",
11362+
repo_path);
11363+
}
11364+
free(owners);
11365+
*error_out = heap_strdup(msg);
11366+
return false;
11367+
}
11368+
1124911369
/* The three heap strings handle_index_repository owns from
1125011370
* cbm_mcp_get_string_arg / resolved_repo_path_from_project_arg. One release
1125111371
* point keeps the dozen early-return paths in step; free(NULL) is a no-op, so
@@ -11397,6 +11517,23 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1139711517
return cbm_mcp_text_result(boundary_err, true);
1139811518
}
1139911519

11520+
if ((!name_override || !name_override[0]) && repo_path && repo_path[0]) {
11521+
char *owner = NULL;
11522+
char *owner_error = NULL;
11523+
if (!index_root_owner_resolve(repo_path, &owner, &owner_error)) {
11524+
index_args_free(repo_path, mode_str, name_override);
11525+
char *result = cbm_mcp_text_result(
11526+
owner_error ? owner_error : "could not resolve index project name", true);
11527+
free(owner_error);
11528+
return result;
11529+
}
11530+
if (owner) {
11531+
cbm_log_info("index.root_owner_reused", "root", repo_path, "project", owner);
11532+
free(name_override);
11533+
name_override = owner;
11534+
}
11535+
}
11536+
1140011537
if (mode_str && strcmp(mode_str, "cross-repo-intelligence") == 0) {
1140111538
if (async_mode) {
1140211539
index_args_free(repo_path, mode_str, name_override);
@@ -11419,7 +11556,8 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1141911556
/* A daemon session delegates the one physical write to its shared job
1142011557
* registry only after path canonicalization and workspace authorization. */
1142111558
if (srv->index_executor) {
11422-
char *worker_args = index_args_with_repo_path(args, repo_path, &resource_policy);
11559+
char *worker_args =
11560+
index_args_with_repo_path(args, repo_path, name_override, &resource_policy);
1142311561
char *coordinated = worker_args ? srv->index_executor(srv->index_executor_context,
1142411562
repo_path, worker_args, async_mode)
1142511563
: NULL;
@@ -11447,7 +11585,8 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1144711585
* installs the same guard before running the in-process pipeline. A marked
1144811586
* host fails closed if preparation or worker startup cannot complete. */
1144911587
if (cbm_index_supervisor_should_wrap()) {
11450-
char *worker_args = index_args_with_repo_path(args, repo_path, &resource_policy);
11588+
char *worker_args =
11589+
index_args_with_repo_path(args, repo_path, name_override, &resource_policy);
1145111590
if (!worker_args) {
1145211591
free(mutation_project);
1145311592
index_args_free(repo_path, mode_str, name_override);

‎tests/test_mcp.c‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8601,6 +8601,110 @@ TEST(tool_project_arg_resolves_non_ascii_folder_issue1827) {
86018601
PASS();
86028602
}
86038603

8604+
/* #2134: re-indexing a root WITHOUT `name` must update the project that
8605+
* already owns that root_path, not fork a second index under the
8606+
* path-derived name (both then list the same root_path and the stale one
8607+
* keeps being read as fresh). Several owners of one root are ambiguous: the
8608+
* call must fail loudly and name them instead of guessing or forking. */
8609+
static int i2134_count_occurrences(const char *haystack, const char *needle) {
8610+
int count = 0;
8611+
for (const char *p = haystack ? strstr(haystack, needle) : NULL; p; p = strstr(p + 1, needle)) {
8612+
count++;
8613+
}
8614+
return count;
8615+
}
8616+
8617+
TEST(tool_index_repository_reuses_existing_project_for_root_issue2134) {
8618+
char repo[CBM_SZ_256];
8619+
char cache[CBM_SZ_256];
8620+
snprintf(repo, sizeof(repo), "/tmp/cbm-i2134r-XXXXXX");
8621+
snprintf(cache, sizeof(cache), "/tmp/cbm-i2134c-XXXXXX");
8622+
if (!cbm_mkdtemp(repo) || !cbm_mkdtemp(cache)) {
8623+
FAIL("mkdtemp failed");
8624+
}
8625+
char canonical_repo[CBM_SZ_1K];
8626+
if (!realpath(repo, canonical_repo)) {
8627+
FAIL("realpath failed");
8628+
}
8629+
const char *saved_cache = getenv("CBM_CACHE_DIR");
8630+
char *saved_cache_copy = saved_cache ? cbm_strdup(saved_cache) : NULL;
8631+
const char *saved_sup = getenv("CBM_INDEX_SUPERVISOR");
8632+
char *saved_sup_copy = saved_sup ? cbm_strdup(saved_sup) : NULL;
8633+
cbm_setenv("CBM_CACHE_DIR", cache, 1);
8634+
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
8635+
i1025_write_repo(repo, "root_owner_2134");
8636+
8637+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
8638+
ASSERT_NOT_NULL(srv);
8639+
8640+
/* 1. Index once under an explicit name. */
8641+
char args[CBM_SZ_1K];
8642+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"name\":\"named-2134\"}", repo);
8643+
char *r = cbm_mcp_handle_tool(srv, "index_repository", args);
8644+
ASSERT_NOT_NULL(r);
8645+
ASSERT_NOT_NULL(strstr(r, "named-2134"));
8646+
free(r);
8647+
8648+
/* 2. Re-index the same root without a name: must update named-2134. */
8649+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", repo);
8650+
r = cbm_mcp_handle_tool(srv, "index_repository", args);
8651+
ASSERT_NOT_NULL(r);
8652+
if (!strstr(r, "named-2134")) {
8653+
fprintf(stderr, " [2134] reindex without name forked: %.300s\n", r);
8654+
}
8655+
ASSERT_NOT_NULL(strstr(r, "named-2134"));
8656+
free(r);
8657+
8658+
/* Every listed project carries its root_path once in structuredContent,
8659+
* so the canonical root occurring once there means exactly one owner. */
8660+
r = cbm_mcp_handle_tool(srv, "list_projects", "{\"format\":\"json\"}");
8661+
ASSERT_NOT_NULL(r);
8662+
ASSERT_NOT_NULL(strstr(r, "\"structuredContent\""));
8663+
int owners = i2134_count_occurrences(strstr(r, "\"structuredContent\""), canonical_repo);
8664+
if (owners != 1) {
8665+
fprintf(stderr, " [2134] %d projects share root %s: %.400s\n", owners, canonical_repo, r);
8666+
}
8667+
ASSERT_EQ(owners, 1);
8668+
free(r);
8669+
8670+
/* 3. An explicit second name is the caller's choice; afterwards the root
8671+
* has two owners, so an unnamed re-index must refuse and list both. */
8672+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"name\":\"other-2134\"}", repo);
8673+
r = cbm_mcp_handle_tool(srv, "index_repository", args);
8674+
ASSERT_NOT_NULL(r);
8675+
ASSERT_NOT_NULL(strstr(r, "other-2134"));
8676+
free(r);
8677+
snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", repo);
8678+
r = cbm_mcp_handle_tool(srv, "index_repository", args);
8679+
ASSERT_NOT_NULL(r);
8680+
ASSERT_NOT_NULL(strstr(r, "named-2134"));
8681+
ASSERT_NOT_NULL(strstr(r, "other-2134"));
8682+
ASSERT_NOT_NULL(strstr(r, "\"isError\":true"));
8683+
free(r);
8684+
r = cbm_mcp_handle_tool(srv, "list_projects", "{\"format\":\"json\"}");
8685+
ASSERT_NOT_NULL(r);
8686+
ASSERT_NOT_NULL(strstr(r, "\"structuredContent\""));
8687+
ASSERT_EQ(i2134_count_occurrences(strstr(r, "\"structuredContent\""), canonical_repo), 2);
8688+
free(r);
8689+
8690+
cbm_mcp_server_free(srv);
8691+
if (saved_cache_copy) {
8692+
cbm_setenv("CBM_CACHE_DIR", saved_cache_copy, 1);
8693+
free(saved_cache_copy);
8694+
} else {
8695+
cbm_unsetenv("CBM_CACHE_DIR");
8696+
}
8697+
if (saved_sup_copy) {
8698+
cbm_setenv("CBM_INDEX_SUPERVISOR", saved_sup_copy, 1);
8699+
free(saved_sup_copy);
8700+
} else {
8701+
cbm_unsetenv("CBM_INDEX_SUPERVISOR");
8702+
}
8703+
th_rmtree(repo);
8704+
th_rmtree(cache);
8705+
PASS();
8706+
}
8707+
86048708
/* Regression for #604: path scopes architecture totals and content. */
86058709
TEST(tool_get_architecture_path_scoping) {
86068710
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
@@ -21022,6 +21126,7 @@ SUITE(mcp) {
2102221126
RUN_TEST(tool_search_graph_accepts_project_name_alias_issue640);
2102321127
RUN_TEST(tool_project_arg_resolves_unique_tail_issue1025);
2102421128
RUN_TEST(tool_project_arg_resolves_non_ascii_folder_issue1827);
21129+
RUN_TEST(tool_index_repository_reuses_existing_project_for_root_issue2134);
2102521130
RUN_TEST(tool_get_architecture_path_scoping);
2102621131
RUN_TEST(tool_query_graph_missing_query);
2102721132

0 commit comments

Comments
 (0)