@@ -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). */
1116111164static 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);
0 commit comments