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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(index): gate supervised auto-index watcher registration on auto_watch (keystone×#849)
Martin Vogel40157834f897
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex c11a366c1..413a9d7aa 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -5706,10 +5706,11 @@ static void *autoindex_thread(void *arg) {if (resp) {free(resp);cbm_log_info("autoindex.done", "project", srv->session_project, "mode", "supervised");- /* Register with watcher for ongoing change detection */- if (srv->watcher) {- cbm_watcher_watch(srv->watcher, srv->session_project, srv->session_root);- }+ /* Register with watcher for ongoing change detection — gated on+ * auto_watch (#849), same as the in-process branch below. A bare+ * `if (srv->watcher)` would register even when the user set+ * `config set auto_watch false`, since srv->watcher is always set. */+ register_watcher_if_enabled(srv);return NULL;}/* resp == NULL → spawn-failure degrade → fall through to in-process. */diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex 09f0f549c..9df8b9e9b 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -4440,6 +4440,183 @@ TEST(mcp_auto_watch_false_skips_watcher_on_connect) {PASS();}+/* ══════════════════════════════════════════════════════════════════+ * #853 — auto_watch=false must ALSO gate the SUPERVISED fresh-index+ * watcher registration (keystone × #849 merge interaction)+ * ══════════════════════════════════════════════════════════════════ */++/* #849 routed ALL watcher registration through register_watcher_if_enabled()+ * (auto_watch gate). The #832 keystone then added a SECOND registration site in+ * autoindex_thread's supervised-success branch, but wired it as a DIRECT+ * cbm_watcher_watch() guarded only by `if (srv->watcher)` — srv->watcher is set+ * unconditionally, so that guard does NOT honour `config set auto_watch false`.+ * The above tests only cover the already-indexed on-connect path+ * (register_watcher_if_enabled); this guard covers the fresh-index SUPERVISED+ * autoindex_thread branch that #832 introduced.+ *+ * Drive the real public entry initialize → maybe_auto_index → autoindex_thread on+ * a supervisor-marked host (kill switch off) with a FRESH project (no prior .db)+ * and auto_watch=false. cbm_mcp_server_free() joins the autoindex thread, so the+ * (buggy or gated) registration decision has run before we read the watch count.+ *+ * RED on the unfixed ungated block: the supervised success branch calls+ * cbm_watcher_watch() unconditionally → watch_count == 1 → IDX853_WATCHER_REGISTERED.+ * GREEN once it calls register_watcher_if_enabled() → auto_watch_off skip → 0.+ * spawn_count is asserted to have advanced so the assertion cannot pass vacuously+ * (i.e. green only because the supervised branch was never entered). */+enum {+ IDX853_OK = 0, /* watch_count==0, supervised branch ran → GREEN */+ IDX853_WATCHER_REGISTERED = 61, /* watch_count==1 → RED: ungated cbm_watcher_watch */+ IDX853_NO_SPAWN = 62, /* spawn_count unchanged → supervised path not exercised */+ IDX853_SETUP_FAIL = 63, /* config/watcher/server/cwd setup failed */+ IDX853_BAD_COUNT = 64, /* unexpected watch_count (<0 or >1) */+};++static int idx853_supervised_autowatch_check(const char *repo_dir, const char *cache_dir) {+ /* Become a supervisor host with the kill switch OFF — the real prod MCP+ * server's state. Done in the FORKED CHILD only (see harness) so the parent+ * test-runner's process-wide host mark stays clear (#845 invariant). Bound the+ * worker so a stuck spawn cannot run long under the fork+alarm net. */+ cbm_index_supervisor_mark_host();+ cbm_unsetenv("CBM_INDEX_SUPERVISOR");+ cbm_setenv("CBM_INDEX_MAX_RESTARTS", "1", 1);+ cbm_setenv("CBM_INDEX_WORKER_TIMEOUT_S", "30", 1);++ cbm_config_t *cfg = cbm_config_open(cache_dir);+ cbm_store_t *wstore = cbm_store_open_memory();+ cbm_watcher_t *watcher = wstore ? cbm_watcher_new(wstore, NULL, NULL) : NULL;+ if (!cfg || !watcher) {+ if (watcher) {+ cbm_watcher_free(watcher);+ }+ if (wstore) {+ cbm_store_close(wstore);+ }+ if (cfg) {+ cbm_config_close(cfg);+ }+ return IDX853_SETUP_FAIL;+ }+ /* auto_index=true → maybe_auto_index launches autoindex_thread for the fresh+ * project; auto_watch=false → the gate this guard exercises. */+ cbm_config_set(cfg, CBM_CONFIG_AUTO_INDEX, "true");+ cbm_config_set(cfg, CBM_CONFIG_AUTO_WATCH, "false");++ /* detect_session derives session_root/session_project from the cwd. */+ char old_cwd[1024];+ if (!cbm_getcwd(old_cwd, sizeof(old_cwd)) || cbm_chdir(repo_dir) != 0) {+ cbm_watcher_free(watcher);+ cbm_store_close(wstore);+ cbm_config_close(cfg);+ return IDX853_SETUP_FAIL;+ }++ int spawns_before = cbm_index_supervisor_spawn_count();+ int code = IDX853_SETUP_FAIL;++ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ if (srv) {+ cbm_mcp_server_set_watcher(srv, watcher);+ cbm_mcp_server_set_config(srv, cfg);+ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}");+ free(resp);+ /* free() joins the autoindex thread → the supervised worker has finished+ * and the registration decision (buggy or gated) has executed. */+ cbm_mcp_server_free(srv);++ int spawns_after = cbm_index_supervisor_spawn_count();+ int watch_count = cbm_watcher_watch_count(watcher);++ if (spawns_after == spawns_before) {+ code = IDX853_NO_SPAWN; /* supervised branch never ran — not a valid probe */+ } else if (watch_count == 1) {+ code = IDX853_WATCHER_REGISTERED; /* the discriminating RED assertion */+ } else if (watch_count == 0) {+ code = IDX853_OK;+ } else {+ code = IDX853_BAD_COUNT;+ }+ }++ (void)cbm_chdir(old_cwd);+ cbm_watcher_free(watcher);+ cbm_store_close(wstore);+ cbm_config_close(cfg);+ return code;+}++TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853) {+#ifdef _WIN32+ /* Marks the process as a supervisor host (irreversible); POSIX isolates that+ * in a forked child. The gate logic is platform-independent and covered on+ * POSIX CI. */+ SKIP_PLATFORM("supervisor-host guard needs fork isolation (POSIX-only)");+#else+ char tmp_dir[256];+ snprintf(tmp_dir, sizeof(tmp_dir), "/tmp/cbm-idx853-repo-XXXXXX");+ if (!cbm_mkdtemp(tmp_dir)) {+ PASS();+ }+ char cache[256];+ snprintf(cache, sizeof(cache), "/tmp/cbm-idx853-cache-XXXXXX");+ if (!cbm_mkdtemp(cache)) {+ cbm_rmdir(tmp_dir);+ PASS();+ }++ const char *saved_cache = getenv("CBM_CACHE_DIR");+ char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;+ cbm_setenv("CBM_CACHE_DIR", cache, 1); /* inherited by the worker child */++ char src_path[512];+ snprintf(src_path, sizeof(src_path), "%s/main.py", tmp_dir);+ FILE *fp = fopen(src_path, "w");+ ASSERT_NOT_NULL(fp);+ fputs("def idx853_fn():\n return 'ok'\n", fp);+ fclose(fp);++ int code = -1;+ bool signalled = false;+ int sig = 0;+ fflush(NULL);+ pid_t pid = fork();+ if (pid == 0) {+ alarm(60); /* a stuck worker dies here instead of hanging the runner */+ _exit(idx853_supervised_autowatch_check(tmp_dir, cache));+ }+ ASSERT_TRUE(pid > 0);+ int status = 0;+ (void)waitpid(pid, &status, 0);+ if (WIFEXITED(status)) {+ code = WEXITSTATUS(status);+ } else if (WIFSIGNALED(status)) {+ signalled = true;+ sig = WTERMSIG(status);+ }++ char *project = cbm_project_name_from_path(tmp_dir);+ cleanup_project_db(cache, project);+ free(project);+ restore_cache_dir(saved_cache_copy);+ free(saved_cache_copy);+ remove(src_path);+ cbm_rmdir(cache);+ cbm_rmdir(tmp_dir);++ if (signalled) {+ printf(" child killed by signal %d (alarm => worker hang)\n", sig);+ } else if (code != IDX853_OK) {+ printf(" child exit code %d (61=watcher registered under auto_watch=false=RED, "+ "62=no spawn, 63=setup fail, 64=bad count)\n",+ code);+ }+ ASSERT_FALSE(signalled);+ ASSERT_EQ(code, IDX853_OK);+ PASS();+#endif+}+/* ══════════════════════════════════════════════════════════════════* SUITE* ══════════════════════════════════════════════════════════════════ */@@ -4620,4 +4797,5 @@ SUITE(mcp) {/* auto_watch gate (distilled from PR #625) */RUN_TEST(mcp_auto_watch_default_registers_watcher_on_connect);RUN_TEST(mcp_auto_watch_false_skips_watcher_on_connect);+ RUN_TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853);}