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.

Merge origin/main into feat/path-allow-broad-too-shallow-override

Martin Vogelb9df26094fa1
diff --cc src/mcp/mcp.c
index 372cd521f,f6e43b317..cf8ade005
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@@ -8708,20 -12033,267 +12033,268 @@@ static char *resolved_repo_path_from_pr
return root_path;
}
- static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
- char *repo_path = cbm_mcp_get_string_arg(args, "repo_path");
- char *mode_str = cbm_mcp_get_string_arg(args, "mode");
- char *name_override = cbm_mcp_get_string_arg(args, "name");
- cbm_normalize_path_sep(repo_path);
-
- if (!repo_path) {
- repo_path = resolved_repo_path_from_project_arg(args);
- cbm_normalize_path_sep(repo_path);
+ static bool project_db_is_servable(const char *project, const char *db_path) {
+ cbm_store_t *store = db_path && db_path[0] ? cbm_store_open_path_query(db_path) : NULL;
+ if (!store) {
+ return false;
}
-
- if (!repo_path) {
- free(mode_str);
- free(name_override);
+ cbm_project_t stored_project = {0};
+ bool servable = cbm_store_get_project(store, project, &stored_project) == CBM_STORE_OK &&
+ stored_project.root_path && stored_project.root_path[0];
+ cbm_project_free_fields(&stored_project);
+ cbm_store_close(store);
+ return servable;
+ }
+
+ /* #2134: an index_repository call without `name` derived the project key from
+ * the path alone, so re-indexing a root that was first indexed under an
+ * explicit name silently forked a second index (same root_path, path-derived
+ * name) and left the original stale while it kept being served. Resolve the
+ * key from the indexes already on disk instead:
+ * - the path-derived project exists -> keep it (unchanged path);
+ * - exactly one other project owns this root -> adopt its name (update it);
+ * - several other projects own this root -> ambiguous: fail loudly,
+ * naming them, never guess;
+ * - none -> first index, derived name.
+ * Roots compare as stored: both sides are the canonical repo path. Returns
+ * false only for the ambiguous / allocation-failure case, with *error_out set
+ * to a heap message; *owner_out is a heap name or NULL. */
+ static bool index_root_owner_append(char **list, size_t *len, size_t *cap, const char *name) {
+ size_t need = *len + strlen(name) + 3;
+ if (need > *cap) {
+ size_t grown_cap = need * 2;
+ char *grown = cbm_realloc(CBM_MEM_CLASS_OTHER, *list, grown_cap);
+ if (!grown) {
+ return false;
+ }
+ *list = grown;
+ *cap = grown_cap;
+ }
+ *len += (size_t)snprintf(*list + *len, *cap - *len, "%s%s", *len ? ", " : "", name);
+ return true;
+ }
+
+ static bool index_root_owner_resolve(const char *repo_path, char **owner_out, char **error_out) {
+ *owner_out = NULL;
+ *error_out = NULL;
+ char *derived = cbm_project_name_from_path(repo_path);
+ if (!derived) {
+ return true; /* the caller reports the derivation failure itself */
+ }
+ char derived_db[CBM_SZ_1K];
+ project_db_path(derived, derived_db, sizeof(derived_db));
+ if (derived_db[0] && cbm_file_exists(derived_db)) {
+ safe_free(derived);
+ return true; /* fast path: the path-derived project is already on disk */
+ }
+ char dir_path[CBM_SZ_1K];
+ cache_dir(dir_path, sizeof(dir_path));
+ cbm_dir_t *d = cbm_opendir(dir_path);
+ if (!d) {
+ safe_free(derived);
+ return true;
+ }
+ char *owner = NULL;
+ char *owners = NULL;
+ size_t owners_len = 0;
+ size_t owners_cap = 0;
+ int owner_count = 0;
+ bool derived_exists = false;
+ bool ok = true;
+ cbm_dirent_t *entry;
+ while (ok && !derived_exists && (entry = cbm_readdir(d)) != NULL) {
+ if (!cbm_is_project_index_db(entry->name)) {
+ continue;
+ }
+ mcp_project_record_t record = {0};
+ project_record_status_t status =
+ read_project_record_identity(dir_path, entry->name, 0, &record);
+ if (status == PROJECT_RECORD_OOM) {
+ ok = false;
+ break;
+ }
+ if (status != PROJECT_RECORD_OK) {
+ continue;
+ }
+ if (strcmp(record.name, derived) == 0) {
+ derived_exists = true;
+ } else if (strcmp(record.root_path, repo_path) == 0) {
+ owner_count++;
+ ok = index_root_owner_append(&owners, &owners_len, &owners_cap, record.name);
+ if (owner_count == 1 && ok) {
+ owner = heap_strdup(record.name);
+ ok = owner != NULL;
+ }
+ }
+ project_record_clear(&record);
+ }
+ cbm_closedir(d);
+ safe_free(derived);
+
+ if (ok && !derived_exists && owner_count == 1) {
+ cbm_free(CBM_MEM_CLASS_OTHER, owners);
+ *owner_out = owner;
+ return true;
+ }
+ safe_free(owner);
+ if (ok && (derived_exists || owner_count == 0)) {
+ cbm_free(CBM_MEM_CLASS_OTHER, owners);
+ return true;
+ }
+ char msg[CBM_SZ_4K];
+ if (ok) {
+ snprintf(msg, sizeof(msg),
+ "several indexed projects share root_path %s: %s. Pass name=<project> to "
+ "choose which one to re-index (or delete_project the stale ones).",
+ repo_path, owners);
+ } else {
+ snprintf(msg, sizeof(msg), "out of memory while resolving the project for root_path %s",
+ repo_path);
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, owners);
+ *error_out = heap_strdup(msg);
+ return false;
+ }
+
+ /* The three heap strings handle_index_repository owns from
+ * cbm_mcp_get_string_arg / resolved_repo_path_from_project_arg. One release
+ * point keeps the dozen early-return paths in step; free(NULL) is a no-op, so
+ * a path may pass NULL for an argument it never obtained. */
+ static void index_args_free(char *repo_path, char *mode_str, char *name_override) {
+ free(repo_path);
+ free(mode_str);
+ free(name_override);
+ }
+
+ /* #2144: async and status are strict booleans. A present non-boolean value is
+ * refused rather than read as false, so "async":"true" never silently blocks
+ * for the whole index. */
+ static bool index_call_mode_arg(const char *args, const char *key, bool *value_out) {
+ *value_out = false;
+ yyjson_doc *doc = args ? yyjson_read(args, strlen(args), 0) : NULL;
+ yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
+ yyjson_val *value = root && yyjson_is_obj(root) ? yyjson_obj_get(root, key) : NULL;
+ bool valid = !value || yyjson_is_bool(value);
+ *value_out = valid && value && yyjson_get_bool(value);
+ yyjson_doc_free(doc);
+ return valid;
+ }
+
+ /* The project key a status query names: an explicit name override wins (the
+ * key the index call used), then repo_path resolved exactly as for indexing
+ * (session root, canonical form, workspace boundary, and the #2134 root owner
+ * an unnamed start adopts), then a known project alias. NULL with *error_out
+ * set when none identifies a project. */
+ static char *index_status_project_key(cbm_mcp_server_t *srv, const char *args, char *error_out,
+ size_t error_size) {
+ char *name = cbm_mcp_get_string_arg(args, "name");
+ if (name && name[0]) {
+ char *key = cbm_project_name_from_path(name);
+ safe_free(name);
+ return key;
+ }
+ safe_free(name);
+ char *repo_path = cbm_mcp_get_string_arg(args, "repo_path");
+ if (!repo_path) {
+ char *project = get_project_arg(args);
+ if (!project) {
+ (void)snprintf(error_out, error_size,
+ "status needs the repo_path (or name) the index call used");
+ }
+ return project;
+ }
+ cbm_normalize_path_sep(repo_path);
+ if (!resolve_session_repo_path(srv, &repo_path)) {
+ safe_free(repo_path);
+ (void)snprintf(error_out, error_size, "failed to resolve repo_path");
+ return NULL;
+ }
+ repo_path = canonicalize_repo_path_if_exists(repo_path);
+ const char *allowed_root =
+ srv->allowed_root_policy_set ? srv->allowed_root : getenv("CBM_ALLOWED_ROOT");
+ if (repo_path && repo_path[0] &&
+ !cbm_workspace_root_allowed(repo_path, cbm_workspace_home_dir(), cbm_workspace_cache_dir(),
- allowed_root, error_out, error_size)) {
++ allowed_root, getenv("PATH_ALLOW_BROAD"), error_out,
++ error_size)) {
+ safe_free(repo_path);
+ return NULL;
+ }
+ /* An unnamed start of a root another project owns keys its job by that
+ * owner (#2134), so the poll naming the same repo_path must resolve the
+ * same owner, and fail on the same ambiguity, or it never finds the job. */
+ char *owner = NULL;
+ char *owner_error = NULL;
+ if (repo_path && repo_path[0] && !index_root_owner_resolve(repo_path, &owner, &owner_error)) {
+ (void)snprintf(error_out, error_size, "%s",
+ owner_error ? owner_error : "could not resolve index project name");
+ safe_free(owner_error);
+ safe_free(repo_path);
+ return NULL;
+ }
+ const char *key_source = owner ? owner : repo_path;
+ char *key = key_source ? cbm_project_name_from_path(key_source) : NULL;
+ safe_free(owner);
+ safe_free(repo_path);
+ if (!key) {
+ (void)snprintf(error_out, error_size, "could not resolve index project name");
+ }
+ return key;
+ }
+
+ /* index_repository(status: true): report the project's running or most recent
+ * index job without starting one. Job state lives in the daemon's job
+ * registry; a server without one (CLI index worker, embedder) has no job that
+ * could outlive a call, so it refuses instead of guessing. */
+ static char *handle_index_repository_status(cbm_mcp_server_t *srv, const char *args) {
+ if (!srv->index_status_provider) {
+ return cbm_mcp_text_result("index_repository status needs the daemon-backed MCP server; "
+ "this in-process server tracks no index jobs",
+ true);
+ }
+ char error[CBM_SZ_4K] = {0}; /* the #2134 ambiguity error lists every owner */
+ char *project = index_status_project_key(srv, args, error, sizeof(error));
+ if (!project) {
+ return cbm_mcp_text_result(error[0] ? error : "could not resolve index project name", true);
+ }
+ char *result = srv->index_status_provider(srv->index_status_context, project);
+ safe_free(project);
+ return result ? result : cbm_mcp_text_result("index job status is unavailable", true);
+ }
+
+ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
+ bool async_mode = false;
+ bool status_mode = false;
+ if (!index_call_mode_arg(args, "async", &async_mode) ||
+ !index_call_mode_arg(args, "status", &status_mode)) {
+ return cbm_mcp_text_result("async and status must be booleans", true);
+ }
+ if (async_mode && status_mode) {
+ return cbm_mcp_text_result(
+ "async and status are exclusive: start with async: true, then poll with status: true",
+ true);
+ }
+ if (status_mode) {
+ return handle_index_repository_status(srv, args);
+ }
+ if (async_mode && !srv->index_executor) {
+ return cbm_mcp_text_result("index_repository async needs the daemon-backed MCP server: "
+ "this in-process server has no process that outlives the call; "
+ "call without async",
+ true);
+ }
+
+ char *repo_path = cbm_mcp_get_string_arg(args, "repo_path");
+ char *mode_str = cbm_mcp_get_string_arg(args, "mode");
+ char *name_override = cbm_mcp_get_string_arg(args, "name");
+ cbm_normalize_path_sep(repo_path);
+
+ if (!repo_path) {
+ repo_path = resolved_repo_path_from_project_arg(args);
+ cbm_normalize_path_sep(repo_path);
+ }
+
+ if (!repo_path) {
+ index_args_free(NULL, mode_str, name_override);
return cbm_mcp_text_result("repo_path is required", true);
}
@@@ -8747,11 -12317,8 +12318,9 @@@
char boundary_err[CBM_SZ_1K];
if (repo_path && repo_path[0] &&
!cbm_workspace_root_allowed(repo_path, cbm_workspace_home_dir(), cbm_workspace_cache_dir(),
- allowed_root, boundary_err, sizeof(boundary_err))) {
+ allowed_root, getenv("PATH_ALLOW_BROAD"), boundary_err,
+ sizeof(boundary_err))) {
- free(mode_str);
- free(name_override);
- free(repo_path);
+ index_args_free(repo_path, mode_str, name_override);
return cbm_mcp_text_result(boundary_err, true);
}