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.

fix(mcp): keep full width for a project's first index

Matt Van Horn26eece514fef
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index cd7b5c711..31efa9420 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -11370,6 +11370,22 @@ static bool index_root_owner_resolve(const char *repo_path, char **owner_out, ch
return false;
}
+/* Background CPU headroom is for a refresh of a graph that already serves.
+ * A project's first index keeps every core, even when it was started
+ * automatically: the caller is waiting on a graph that does not exist yet.
+ * An empty or unreadable file is not a committed index. Both sites that
+ * honour automatic indexing — handle_index_repository's _cbm_background
+ * flag and the in-process autoindex_thread — go through this so they
+ * cannot drift. */
+static bool project_has_committed_index(const char *project) {
+ if (!project || !project[0]) {
+ return false;
+ }
+ char db_path[CBM_SZ_1K];
+ project_db_path(project, db_path, sizeof(db_path));
+ return db_path[0] && project_db_is_servable(project, db_path);
+}
+
/* 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
@@ -11662,7 +11678,6 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
free(repo_path);
return cbm_mcp_text_result("failed to create pipeline", true);
}
- cbm_pipeline_set_background(p, background);
if (name_override && name_override[0] && !cbm_pipeline_set_project_name(p, name_override)) {
cbm_pipeline_free(p);
mcp_project_mutation_end(srv, mutation_project);
@@ -11672,6 +11687,10 @@ static char *handle_index_repository(cbm_mcp_server_t *srv, const char *args) {
return cbm_mcp_text_result("invalid project name", true);
}
free(name_override);
+ /* Headroom follows the final project name, decided before artifact
+ * bootstrap can create the first local copy of this index. */
+ cbm_pipeline_set_background(p, background &&
+ project_has_committed_index(cbm_pipeline_project_name(p)));
cbm_pipeline_set_persistence(p, persistence);
cbm_pipeline_set_resource_policy(p, &resource_policy);
@@ -18109,7 +18128,7 @@ static void *autoindex_thread(void *arg) {
cbm_log_warn("autoindex.err", "msg", "pipeline_create_failed");
return NULL;
}
- cbm_pipeline_set_background(p, true);
+ cbm_pipeline_set_background(p, project_has_committed_index(cbm_pipeline_project_name(p)));
/* Block until any concurrent pipeline finishes */
cbm_pipeline_lock();
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 6f6655c5f..be881942e 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -204,7 +204,7 @@ struct cbm_pipeline {
bool persistence; /* write .codebase-memory/graph.db.zst after indexing */
cbm_index_resource_policy_t resource_policy;
cbm_index_resource_violation_t resource_violation;
- bool background; /* reserve CPU headroom when no user is waiting */
+ bool background; /* reserve CPU headroom when no user is waiting */
/* Snapshot of the artifact export failure of THIS run (set only by
* export_after_publish failure, zeroed at run start, cleared on success).
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index d334c4774..f42f0dc6a 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -20006,10 +20006,43 @@ TEST(autoindex_limit_guards_git_root_issue713) {
PASS();
}
-TEST(mcp_auto_index_in_process_uses_background_worker_policy) {
+/* A servable project row is a committed index. user_version stays 0, so a
+ * later run cannot take the incremental no-op and skip recording workers. */
+static bool mcp_publish_committed_index(const char *root) {
+ char *project = root ? cbm_project_name_from_path(root) : NULL;
+ cbm_store_t *store = project ? cbm_store_open(project) : NULL;
+ bool published = store && cbm_store_upsert_project(store, project, root) == CBM_STORE_OK;
+ cbm_store_close(store);
+ free(project);
+ return published;
+}
+
+#ifdef CBM_ENABLE_TEST_SEAMS
+typedef struct {
+ const char *root;
+ bool published;
+} mcp_committed_seed_t;
+
+/* maybe_auto_index returns before the thread when <project>.db already
+ * exists. Publish the committed index from the count hook, which runs after
+ * that check and before autoindex_thread chooses a width. */
+static void mcp_autoindex_publish_committed(void *context) {
+ mcp_committed_seed_t *seed = context;
+ if (seed) {
+ seed->published = mcp_publish_committed_index(seed->root);
+ }
+}
+#endif
+
+/* First index, including one started automatically, uses every core. Headroom
+ * applies only once a servable project database already exists.
+ * automatic: in-process auto-index (initialize → autoindex_thread).
+ * Otherwise index_repository with _cbm_background, the daemon/watcher path. */
+static int mcp_assert_inprocess_worker_policy(bool committed, bool automatic) {
char cache[256];
char repo[512];
- (void)snprintf(cache, sizeof(cache), "%s/cbm-autoindex-workers-XXXXXX", cbm_tmpdir());
+ (void)snprintf(cache, sizeof(cache), "%s/cbm-worker-policy-%d%d-XXXXXX", cbm_tmpdir(),
+ committed ? 1 : 0, automatic ? 1 : 0);
bool cache_ready = cbm_mkdtemp(cache) != NULL;
(void)snprintf(repo, sizeof(repo), "%s/repo", cache);
char source[640];
@@ -20029,24 +20062,57 @@ TEST(mcp_auto_index_in_process_uses_background_worker_policy) {
environment[i].value = value ? strdup(value) : NULL;
environment_saved = environment_saved && (!value || environment[i].value);
}
- bool environment_ready =
- environment_saved && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0 &&
- cbm_unsetenv("CBM_WORKERS") == 0 && cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") == 0;
+ bool environment_ready = environment_saved && cbm_setenv("CBM_CACHE_DIR", cache, 1) == 0 &&
+ cbm_unsetenv("CBM_WORKERS") == 0 &&
+ cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") == 0;
+ bool seeded = !committed;
char old_cwd[CBM_SZ_4K] = {0};
- bool cwd_ready = repo_ready && environment_ready && cbm_getcwd(old_cwd, sizeof(old_cwd)) &&
- cbm_chdir(repo) == 0;
- cbm_config_t *config = cwd_ready ? cbm_config_open(cache) : NULL;
- bool config_ready = config && cbm_config_set(config, CBM_CONFIG_AUTO_INDEX, "true") == 0 &&
- cbm_config_set(config, CBM_CONFIG_AUTO_WATCH, "false") == 0;
- cbm_pipeline_worker_count_test_reset();
- cbm_mcp_server_t *server = config_ready ? cbm_mcp_server_new(NULL) : NULL;
+ bool cwd_ready = true;
+ cbm_config_t *config = NULL;
+ bool config_ready = true;
+ cbm_mcp_server_t *server = NULL;
char *response = NULL;
- if (server) {
- cbm_mcp_server_set_config(server, config);
- response = cbm_mcp_server_handle(
- server, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}");
- cbm_mcp_server_free(server); /* joins the automatic index thread */
+ if (automatic) {
+ cwd_ready = repo_ready && environment_ready && cbm_getcwd(old_cwd, sizeof(old_cwd)) &&
+ cbm_chdir(repo) == 0;
+ config = cwd_ready ? cbm_config_open(cache) : NULL;
+ config_ready = config && cbm_config_set(config, CBM_CONFIG_AUTO_INDEX, "true") == 0 &&
+ cbm_config_set(config, CBM_CONFIG_AUTO_WATCH, "false") == 0;
+ cbm_pipeline_worker_count_test_reset();
+ server = config_ready ? cbm_mcp_server_new(NULL) : NULL;
+ if (server) {
+ cbm_mcp_server_set_config(server, config);
+#ifdef CBM_ENABLE_TEST_SEAMS
+ mcp_committed_seed_t seed = {.root = repo, .published = !committed};
+ if (committed) {
+ cbm_mcp_server_set_auto_index_count_test_hook(
+ server, mcp_autoindex_publish_committed, &seed);
+ }
+#endif
+ response = cbm_mcp_server_handle(
+ server, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}");
+#ifdef CBM_ENABLE_TEST_SEAMS
+ seeded = seed.published;
+#endif
+ cbm_mcp_server_free(server); /* joins the automatic index thread */
+ }
+ } else {
+ if (committed && repo_ready && environment_ready) {
+ seeded = mcp_publish_committed_index(repo);
+ }
+ cbm_pipeline_worker_count_test_reset();
+ server = repo_ready && environment_ready && seeded ? cbm_mcp_server_new(NULL) : NULL;
+ if (server) {
+ char repo_json[512];
+ char args[CBM_SZ_1K];
+ (void)snprintf(repo_json, sizeof(repo_json), "%s", repo);
+ cbm_normalize_path_sep(repo_json);
+ (void)snprintf(args, sizeof(args), "{\"_cbm_background\":true,\"repo_path\":\"%s\"}",
+ repo_json);
+ response = cbm_mcp_handle_tool(server, "index_repository", args);
+ cbm_mcp_server_free(server);
+ }
}
int selected_workers = cbm_pipeline_worker_count_test_last();
bool server_ready = server != NULL;
@@ -20054,12 +20120,12 @@ TEST(mcp_auto_index_in_process_uses_background_worker_policy) {
free(response);
cbm_config_close(config);
- if (cwd_ready) {
+ if (automatic && cwd_ready) {
(void)cbm_chdir(old_cwd);
}
/* Capture while CBM_WORKERS is still unset. Restoring first makes the
- * expected count follow the lane override instead of the headroom policy. */
- int expected = cbm_default_worker_count(false);
+ * expected count follow the lane override instead of this policy. */
+ int expected = cbm_default_worker_count(!committed);
mcp_test_restore_env(environment, sizeof(environment) / sizeof(environment[0]));
bool cleaned = !cache_ready || th_rmtree(cache) == 0;
@@ -20069,6 +20135,7 @@ TEST(mcp_auto_index_in_process_uses_background_worker_policy) {
ASSERT_TRUE(environment_ready);
ASSERT_TRUE(cwd_ready);
ASSERT_TRUE(config_ready);
+ ASSERT_TRUE(seeded);
ASSERT_TRUE(server_ready);
ASSERT_TRUE(response_ready);
ASSERT_EQ(selected_workers, expected);
@@ -20076,6 +20143,22 @@ TEST(mcp_auto_index_in_process_uses_background_worker_policy) {
PASS();
}
+TEST(mcp_auto_index_in_process_fresh_project_uses_full_width) {
+ return mcp_assert_inprocess_worker_policy(false, true);
+}
+
+TEST(mcp_auto_index_in_process_existing_index_keeps_headroom) {
+ return mcp_assert_inprocess_worker_policy(true, true);
+}
+
+TEST(mcp_background_flag_fresh_project_uses_full_width) {
+ return mcp_assert_inprocess_worker_policy(false, false);
+}
+
+TEST(mcp_background_flag_existing_index_keeps_headroom) {
+ return mcp_assert_inprocess_worker_policy(true, false);
+}
+
/* ══════════════════════════════════════════════════════════════════
* #853 — auto_watch=false must ALSO gate the SUPERVISED fresh-index
* watcher registration (keystone × #849 merge interaction)
@@ -21431,7 +21514,10 @@ SUITE(mcp) {
RUN_TEST(autoindex_limit_guards_non_git_root_issue713);
RUN_TEST(autoindex_limit_admits_non_git_root_under_limit_issue713);
RUN_TEST(autoindex_limit_guards_git_root_issue713);
- RUN_TEST(mcp_auto_index_in_process_uses_background_worker_policy);
+ RUN_TEST(mcp_auto_index_in_process_fresh_project_uses_full_width);
+ RUN_TEST(mcp_auto_index_in_process_existing_index_keeps_headroom);
+ RUN_TEST(mcp_background_flag_fresh_project_uses_full_width);
+ RUN_TEST(mcp_background_flag_existing_index_keeps_headroom);
}
/* Kept separate so daemon-coordination regressions can be iterated without