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
Merge remote-tracking branch 'origin/main' into feat/index-mem-subprocess-isolation
Martin Vogel2d7f6e3a89c0
diff --cc tests/test_mcp.cindex c421ec0ab,74665efcc..09f0f549c--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@@ -4166,160 -4168,124 +4168,278 @@@ TEST(index_supervisor_gate_requires_marPASS();}+/* ══════════════════════════════════════════════════════════════════+ * #832 — background auto-index + watcher re-index must run in the+ * supervised worker SUBPROCESS (RSS isolation)+ * ══════════════════════════════════════════════════════════════════ */++/* The long-lived server ran the full index pipeline in-process on two background+ * paths (session auto-index in mcp.c, watcher re-index in main.c). Worker-thread+ * mimalloc heaps abandon pages at thread exit and mimalloc v3+ * (page_reclaim_on_free=0) does not reclaim them when the main thread later frees+ * their blocks, so RSS ratchets across re-index cycles (#832). The fix routes both+ * paths through cbm_mcp_index_run_supervised_path() — the SAME supervised worker+ * subprocess the index_repository tool uses — so the child hands 100%% of its RSS+ * back to the OS on exit.+ *+ * This guard proves the ROUTING: on a supervisor-marked host with the kill switch+ * OFF, the shared entry the watcher/auto-index now call must (a) spawn a worker+ * child (cbm_index_supervisor_spawn_count() increases) and (b) actually index the+ * fixture (the worker child writes the Function node). RED on the unfixed+ * in-process routing: it calls cbm_pipeline_run directly, so spawn_count is+ * unchanged → IDX832_NO_SPAWN. */+enum {+ IDX832_OK = 0,+ IDX832_NO_SPAWN = 51, /* spawn_count unchanged — routed in-process (RED) */+ IDX832_NULL_RESP = 52, /* supervised entry degraded to NULL */+ IDX832_NOT_INDEXED = 53, /* response/store lacks the indexed Function node */+ IDX832_SERVER_FAIL = 54,+};++static int idx832_supervised_route_check(const char *repo_dir) {+ /* Become a supervisor host with the kill switch OFF — exactly the real MCP+ * server's state. Done in the FORKED CHILD only (see the harness) so the+ * parent test-runner's process-wide host mark stays clear and the #845+ * unmarked-embedder guard is unaffected. Bound the recovery loop + worker+ * quiet-timeout so a stuck child 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);++ int spawns_before = cbm_index_supervisor_spawn_count();+ char *resp = cbm_mcp_index_run_supervised_path(repo_dir);+ int spawns_after = cbm_index_supervisor_spawn_count();++ if (spawns_after == spawns_before) {+ free(resp);+ return IDX832_NO_SPAWN; /* the discriminating assertion: RED in-process */+ }+ if (!resp) {+ return IDX832_NULL_RESP;+ }+ bool indexed = response_contains_json_fragment(resp, "\"status\":\"indexed\"");+ free(resp);+ if (!indexed) {+ return IDX832_NOT_INDEXED;+ }++ /* Store-level proof the worker child did real work: the Function node it wrote+ * must be queryable from a fresh server reading the DB the child produced. */+ char *project = cbm_project_name_from_path(repo_dir);+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);+ if (!srv) {+ free(project);+ return IDX832_SERVER_FAIL;+ }+ int code = IDX832_OK;+ if (project) {+ char q[512];+ snprintf(q, sizeof(q),+ "{\"project\":\"%s\",\"name_pattern\":\"idx832_fn\",\"label\":\"Function\"}",+ project);+ char *sr = cbm_mcp_handle_tool(srv, "search_graph", q);+ if (!sr || !strstr(sr, "idx832_fn")) {+ code = IDX832_NOT_INDEXED;+ }+ free(sr);+ }+ cbm_mcp_server_free(srv);+ free(project);+ return code;+}++TEST(index_bg_paths_route_through_supervisor_issue832) {+#ifdef _WIN32+ /* The guard marks the process as a supervisor host, which cannot be undone.+ * POSIX isolates that in a forked child; without fork we would pollute the+ * shared test-runner (breaking the #845 unmarked-embedder guard). The routing+ * logic is platform-independent and covered on POSIX CI; Windows containment+ * is covered by the end-to-end crash-containment test. */+ SKIP_PLATFORM("supervisor-host guard needs fork isolation (POSIX-only)");+#else+ char tmp_dir[256];+ snprintf(tmp_dir, sizeof(tmp_dir), "/tmp/cbm-idx832-repo-XXXXXX");+ if (!cbm_mkdtemp(tmp_dir)) {+ PASS();+ }+ char cache[256];+ snprintf(cache, sizeof(cache), "/tmp/cbm-idx832-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 idx832_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(idx832_supervised_route_check(tmp_dir));+ }+ 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 != IDX832_OK) {+ printf(" child exit code %d (51=no spawn/in-process=RED, 52=null resp, "+ "53=not indexed, 54=server fail)\n",+ code);+ }+ ASSERT_FALSE(signalled);+ ASSERT_EQ(code, IDX832_OK);+ PASS();+#endif+}++ /* ══════════════════════════════════════════════════════════════════+ * AUTO_WATCH GATE (distilled from PR #625)+ *+ * Background watcher registration on session connect is gated by the+ * `auto_watch` config key (default TRUE = existing behavior).+ * ══════════════════════════════════════════════════════════════════ */++ /* Drive the already-indexed connect path (initialize → maybe_auto_index →+ * watcher registration) and return the resulting watch count.+ * auto_watch_value: NULL leaves the key unset (exercises the default),+ * otherwise the key is set to that value before initialize.+ * Returns a negative code on fixture setup failure. */+ static int auto_watch_connect_watch_count(const char *auto_watch_value) {+ char cache[256];+ snprintf(cache, sizeof(cache), "/tmp/cbm-autowatch-cache-XXXXXX");+ if (!cbm_mkdtemp(cache)) {+ return -1;+ }++ char repodir[512];+ snprintf(repodir, sizeof(repodir), "%s/repo", cache);+ if (th_mkdir_p(repodir) != 0) {+ th_rmtree(cache);+ return -2;+ }++ /* Same derivation detect_session uses on the cwd — realpath-based, so+ * the name matches even where /tmp is a symlink (macOS). */+ char *project = cbm_project_name_from_path(repodir);+ if (!project) {+ th_rmtree(cache);+ return -3;+ }++ /* Pre-create <cache>/<project>.db so maybe_auto_index takes the+ * "already indexed" branch — the watcher-registration site under test. */+ char db_path[1024];+ snprintf(db_path, sizeof(db_path), "%s/%s.db", cache, project);+ if (th_write_file(db_path, "") != 0) {+ free(project);+ th_rmtree(cache);+ return -4;+ }+ free(project);++ const char *saved = getenv("CBM_CACHE_DIR");+ char *saved_copy = saved ? strdup(saved) : NULL;+ cbm_setenv("CBM_CACHE_DIR", cache, 1);++ char old_cwd[1024];+ if (!cbm_getcwd(old_cwd, sizeof(old_cwd)) || cbm_chdir(repodir) != 0) {+ restore_cache_dir(saved_copy);+ free(saved_copy);+ th_rmtree(cache);+ return -5;+ }++ int count = -6;+ cbm_config_t *cfg = cbm_config_open(cache);+ cbm_store_t *wstore = cbm_store_open_memory();+ cbm_watcher_t *watcher = wstore ? cbm_watcher_new(wstore, NULL, NULL) : NULL;+ if (cfg && watcher) {+ if (auto_watch_value) {+ cbm_config_set(cfg, CBM_CONFIG_AUTO_WATCH, auto_watch_value);+ }++ 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);+ count = cbm_watcher_watch_count(watcher);+ cbm_mcp_server_free(srv);+ }+ }++ if (watcher) {+ cbm_watcher_free(watcher);+ }+ if (wstore) {+ cbm_store_close(wstore);+ }+ if (cfg) {+ cbm_config_close(cfg);+ }++ (void)cbm_chdir(old_cwd);+ restore_cache_dir(saved_copy);+ free(saved_copy);+ th_rmtree(cache);+ return count;+ }++ /* Default (key unset) → watcher registered on connect. Guards the+ * no-behavior-change promise of the auto_watch gate: existing users keep+ * background auto-sync without touching config. */+ TEST(mcp_auto_watch_default_registers_watcher_on_connect) {+ int count = auto_watch_connect_watch_count(NULL);+ if (count < 0) {+ PASS(); /* fixture setup failed (tmpdir/cwd unavailable) — skip */+ }+ ASSERT_EQ(count, 1);+ PASS();+ }++ /* auto_watch=false → NO watcher registered on connect. RED on pre-gate code+ * (registration was unconditional and the key did not exist). */+ TEST(mcp_auto_watch_false_skips_watcher_on_connect) {+ int count = auto_watch_connect_watch_count("false");+ if (count < 0) {+ PASS(); /* fixture setup failed (tmpdir/cwd unavailable) — skip */+ }+ ASSERT_EQ(count, 0);+ PASS();+ }+/* ══════════════════════════════════════════════════════════════════* SUITE* ══════════════════════════════════════════════════════════════════ */