@@ -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 = cbm_realloc(CBM_MEM_CLASS_OTHER, *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+ safe_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+ safe_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+ safe_free(derived);
11343+
11344+ if (ok && !derived_exists && owner_count == 1) {
11345+ cbm_free(CBM_MEM_CLASS_OTHER, owners);
11346+ *owner_out = owner;
11347+ return true;
11348+ }
11349+ safe_free(owner);
11350+ if (ok && (derived_exists || owner_count == 0)) {
11351+ cbm_free(CBM_MEM_CLASS_OTHER, 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+ cbm_free(CBM_MEM_CLASS_OTHER, 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
@@ -11272,8 +11392,9 @@ static bool index_call_mode_arg(const char *args, const char *key, bool *value_o
1127211392
1127311393/* The project key a status query names: an explicit name override wins (the
1127411394 * key the index call used), then repo_path resolved exactly as for indexing
11275- * (session root, canonical form, workspace boundary), then a known project
11276- * alias. NULL with *error_out set when none identifies a project. */
11395+ * (session root, canonical form, workspace boundary, and the #2134 root owner
11396+ * an unnamed start adopts), then a known project alias. NULL with *error_out
11397+ * set when none identifies a project. */
1127711398static char *index_status_project_key(cbm_mcp_server_t *srv, const char *args, char *error_out,
1127811399 size_t error_size) {
1127911400 char *name = cbm_mcp_get_string_arg(args, "name");
@@ -11307,7 +11428,21 @@ static char *index_status_project_key(cbm_mcp_server_t *srv, const char *args, c
1130711428 safe_free(repo_path);
1130811429 return NULL;
1130911430 }
11310- char *key = repo_path ? cbm_project_name_from_path(repo_path) : NULL;
11431+ /* An unnamed start of a root another project owns keys its job by that
11432+ * owner (#2134), so the poll naming the same repo_path must resolve the
11433+ * same owner, and fail on the same ambiguity, or it never finds the job. */
11434+ char *owner = NULL;
11435+ char *owner_error = NULL;
11436+ if (repo_path && repo_path[0] && !index_root_owner_resolve(repo_path, &owner, &owner_error)) {
11437+ (void)snprintf(error_out, error_size, "%s",
11438+ owner_error ? owner_error : "could not resolve index project name");
11439+ safe_free(owner_error);
11440+ safe_free(repo_path);
11441+ return NULL;
11442+ }
11443+ const char *key_source = owner ? owner : repo_path;
11444+ char *key = key_source ? cbm_project_name_from_path(key_source) : NULL;
11445+ safe_free(owner);
1131111446 safe_free(repo_path);
1131211447 if (!key) {
1131311448 (void)snprintf(error_out, error_size, "could not resolve index project name");
@@ -11325,7 +11460,7 @@ static char *handle_index_repository_status(cbm_mcp_server_t *srv, const char *a
1132511460 "this in-process server tracks no index jobs",
1132611461 true);
1132711462 }
11328- char error[CBM_SZ_1K ] = {0};
11463+ char error[CBM_SZ_4K ] = {0}; /* the #2134 ambiguity error lists every owner */
1132911464 char *project = index_status_project_key(srv, args, error, sizeof(error));
1133011465 if (!project) {
1133111466 return cbm_mcp_text_result(error[0] ? error : "could not resolve index project name", true);
@@ -11409,6 +11544,26 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1140911544 return result;
1141011545 }
1141111546
11547+ /* #2134 applies to re-indexing only. Cross-repo mode reads an existing
11548+ * source index and never writes one; adopting a root owner there could
11549+ * turn one of its own named targets into the source. */
11550+ if ((!name_override || !name_override[0]) && repo_path && repo_path[0]) {
11551+ char *owner = NULL;
11552+ char *owner_error = NULL;
11553+ if (!index_root_owner_resolve(repo_path, &owner, &owner_error)) {
11554+ index_args_free(repo_path, mode_str, name_override);
11555+ char *result = cbm_mcp_text_result(
11556+ owner_error ? owner_error : "could not resolve index project name", true);
11557+ safe_free(owner_error);
11558+ return result;
11559+ }
11560+ if (owner) {
11561+ cbm_log_info("index.root_owner_reused", "root", repo_path, "project", owner);
11562+ safe_free(name_override);
11563+ name_override = owner;
11564+ }
11565+ }
11566+
1141211567 cbm_index_resource_policy_t resource_policy;
1141311568 char policy_error[CBM_SZ_256] = {0};
1141411569 if (!load_index_policy(srv, args, &resource_policy, policy_error, sizeof(policy_error))) {
@@ -11419,7 +11574,8 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1141911574 /* A daemon session delegates the one physical write to its shared job
1142011575 * registry only after path canonicalization and workspace authorization. */
1142111576 if (srv->index_executor) {
11422- char *worker_args = index_args_with_repo_path(args, repo_path, &resource_policy);
11577+ char *worker_args =
11578+ index_args_with_repo_path(args, repo_path, name_override, &resource_policy);
1142311579 char *coordinated = worker_args ? srv->index_executor(srv->index_executor_context,
1142411580 repo_path, worker_args, async_mode)
1142511581 : NULL;
@@ -11447,7 +11603,8 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
1144711603 * installs the same guard before running the in-process pipeline. A marked
1144811604 * host fails closed if preparation or worker startup cannot complete. */
1144911605 if (cbm_index_supervisor_should_wrap()) {
11450- char *worker_args = index_args_with_repo_path(args, repo_path, &resource_policy);
11606+ char *worker_args =
11607+ index_args_with_repo_path(args, repo_path, name_override, &resource_policy);
1145111608 if (!worker_args) {
1145211609 free(mutation_project);
1145311610 index_args_free(repo_path, mode_str, name_override);
0 commit comments