Skip to content

Commit 4015783

Browse files
committed
fix(index): gate supervised auto-index watcher registration on auto_watch (keystone×DeusData#849)
The DeusData#832 supervisor keystone added a second watcher-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 (cbm_mcp_server_set_watcher), so that guard does NOT honour the auto_watch config. PR DeusData#849 had routed ALL watcher registration through register_watcher_if_enabled() (which checks auto_watch_enabled() and logs watcher.register.skipped reason=auto_watch_off). On a supervisor-marked prod host with a fresh (unindexed) project and `config set auto_watch false`, the supervised path therefore registered a background watcher against the user's explicit opt-out — defeating DeusData#849's multi-project containment. The in-process twin ~20 lines below already used register_watcher_if_enabled() correctly. Fix: replace the ungated block in the supervised-success branch with register_watcher_if_enabled(srv), matching the in-process branch. Surrounding supervised-success logging is unchanged. Reproduce-first: mcp_auto_watch_false_skips_supervised_autoindex_issue853 drives the real public entry initialize -> maybe_auto_index -> autoindex_thread on a supervisor-marked host (kill switch off) with a FRESH project and auto_watch=false, in a fork+alarm-bounded child (like the DeusData#832/DeusData#845 gate tests). cbm_mcp_server_free() joins the autoindex thread so the registration decision has run before the watch count is read. It asserts spawn_count advanced (so the probe cannot pass vacuously by skipping the supervised branch) and watch_count == 0. RED on the unfixed code (watch_count == 1, exit 61); GREEN after the fix (register_watcher_if_enabled skips → watcher.register.skipped reason=auto_watch_off). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 2d7f6e3 commit 4015783

2 files changed

Lines changed: 183 additions & 4 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5706,10 +5706,11 @@ static void *autoindex_thread(void *arg) {
57065706
if (resp) {
57075707
free(resp);
57085708
cbm_log_info("autoindex.done", "project", srv->session_project, "mode", "supervised");
5709-
/* Register with watcher for ongoing change detection */
5710-
if (srv->watcher) {
5711-
cbm_watcher_watch(srv->watcher, srv->session_project, srv->session_root);
5712-
}
5709+
/* Register with watcher for ongoing change detection — gated on
5710+
* auto_watch (#849), same as the in-process branch below. A bare
5711+
* `if (srv->watcher)` would register even when the user set
5712+
* `config set auto_watch false`, since srv->watcher is always set. */
5713+
register_watcher_if_enabled(srv);
57135714
return NULL;
57145715
}
57155716
/* resp == NULL → spawn-failure degrade → fall through to in-process. */

‎tests/test_mcp.c‎

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4440,6 +4440,183 @@ TEST(mcp_auto_watch_false_skips_watcher_on_connect) {
44404440
PASS();
44414441
}
44424442

4443+
/* ══════════════════════════════════════════════════════════════════
4444+
* #853 — auto_watch=false must ALSO gate the SUPERVISED fresh-index
4445+
* watcher registration (keystone × #849 merge interaction)
4446+
* ══════════════════════════════════════════════════════════════════ */
4447+
4448+
/* #849 routed ALL watcher registration through register_watcher_if_enabled()
4449+
* (auto_watch gate). The #832 keystone then added a SECOND registration site in
4450+
* autoindex_thread's supervised-success branch, but wired it as a DIRECT
4451+
* cbm_watcher_watch() guarded only by `if (srv->watcher)` — srv->watcher is set
4452+
* unconditionally, so that guard does NOT honour `config set auto_watch false`.
4453+
* The above tests only cover the already-indexed on-connect path
4454+
* (register_watcher_if_enabled); this guard covers the fresh-index SUPERVISED
4455+
* autoindex_thread branch that #832 introduced.
4456+
*
4457+
* Drive the real public entry initialize → maybe_auto_index → autoindex_thread on
4458+
* a supervisor-marked host (kill switch off) with a FRESH project (no prior .db)
4459+
* and auto_watch=false. cbm_mcp_server_free() joins the autoindex thread, so the
4460+
* (buggy or gated) registration decision has run before we read the watch count.
4461+
*
4462+
* RED on the unfixed ungated block: the supervised success branch calls
4463+
* cbm_watcher_watch() unconditionally → watch_count == 1 → IDX853_WATCHER_REGISTERED.
4464+
* GREEN once it calls register_watcher_if_enabled() → auto_watch_off skip → 0.
4465+
* spawn_count is asserted to have advanced so the assertion cannot pass vacuously
4466+
* (i.e. green only because the supervised branch was never entered). */
4467+
enum {
4468+
IDX853_OK = 0, /* watch_count==0, supervised branch ran → GREEN */
4469+
IDX853_WATCHER_REGISTERED = 61, /* watch_count==1 → RED: ungated cbm_watcher_watch */
4470+
IDX853_NO_SPAWN = 62, /* spawn_count unchanged → supervised path not exercised */
4471+
IDX853_SETUP_FAIL = 63, /* config/watcher/server/cwd setup failed */
4472+
IDX853_BAD_COUNT = 64, /* unexpected watch_count (<0 or >1) */
4473+
};
4474+
4475+
static int idx853_supervised_autowatch_check(const char *repo_dir, const char *cache_dir) {
4476+
/* Become a supervisor host with the kill switch OFF — the real prod MCP
4477+
* server's state. Done in the FORKED CHILD only (see harness) so the parent
4478+
* test-runner's process-wide host mark stays clear (#845 invariant). Bound the
4479+
* worker so a stuck spawn cannot run long under the fork+alarm net. */
4480+
cbm_index_supervisor_mark_host();
4481+
cbm_unsetenv("CBM_INDEX_SUPERVISOR");
4482+
cbm_setenv("CBM_INDEX_MAX_RESTARTS", "1", 1);
4483+
cbm_setenv("CBM_INDEX_WORKER_TIMEOUT_S", "30", 1);
4484+
4485+
cbm_config_t *cfg = cbm_config_open(cache_dir);
4486+
cbm_store_t *wstore = cbm_store_open_memory();
4487+
cbm_watcher_t *watcher = wstore ? cbm_watcher_new(wstore, NULL, NULL) : NULL;
4488+
if (!cfg || !watcher) {
4489+
if (watcher) {
4490+
cbm_watcher_free(watcher);
4491+
}
4492+
if (wstore) {
4493+
cbm_store_close(wstore);
4494+
}
4495+
if (cfg) {
4496+
cbm_config_close(cfg);
4497+
}
4498+
return IDX853_SETUP_FAIL;
4499+
}
4500+
/* auto_index=true → maybe_auto_index launches autoindex_thread for the fresh
4501+
* project; auto_watch=false → the gate this guard exercises. */
4502+
cbm_config_set(cfg, CBM_CONFIG_AUTO_INDEX, "true");
4503+
cbm_config_set(cfg, CBM_CONFIG_AUTO_WATCH, "false");
4504+
4505+
/* detect_session derives session_root/session_project from the cwd. */
4506+
char old_cwd[1024];
4507+
if (!cbm_getcwd(old_cwd, sizeof(old_cwd)) || cbm_chdir(repo_dir) != 0) {
4508+
cbm_watcher_free(watcher);
4509+
cbm_store_close(wstore);
4510+
cbm_config_close(cfg);
4511+
return IDX853_SETUP_FAIL;
4512+
}
4513+
4514+
int spawns_before = cbm_index_supervisor_spawn_count();
4515+
int code = IDX853_SETUP_FAIL;
4516+
4517+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
4518+
if (srv) {
4519+
cbm_mcp_server_set_watcher(srv, watcher);
4520+
cbm_mcp_server_set_config(srv, cfg);
4521+
char *resp = cbm_mcp_server_handle(
4522+
srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}");
4523+
free(resp);
4524+
/* free() joins the autoindex thread → the supervised worker has finished
4525+
* and the registration decision (buggy or gated) has executed. */
4526+
cbm_mcp_server_free(srv);
4527+
4528+
int spawns_after = cbm_index_supervisor_spawn_count();
4529+
int watch_count = cbm_watcher_watch_count(watcher);
4530+
4531+
if (spawns_after == spawns_before) {
4532+
code = IDX853_NO_SPAWN; /* supervised branch never ran — not a valid probe */
4533+
} else if (watch_count == 1) {
4534+
code = IDX853_WATCHER_REGISTERED; /* the discriminating RED assertion */
4535+
} else if (watch_count == 0) {
4536+
code = IDX853_OK;
4537+
} else {
4538+
code = IDX853_BAD_COUNT;
4539+
}
4540+
}
4541+
4542+
(void)cbm_chdir(old_cwd);
4543+
cbm_watcher_free(watcher);
4544+
cbm_store_close(wstore);
4545+
cbm_config_close(cfg);
4546+
return code;
4547+
}
4548+
4549+
TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853) {
4550+
#ifdef _WIN32
4551+
/* Marks the process as a supervisor host (irreversible); POSIX isolates that
4552+
* in a forked child. The gate logic is platform-independent and covered on
4553+
* POSIX CI. */
4554+
SKIP_PLATFORM("supervisor-host guard needs fork isolation (POSIX-only)");
4555+
#else
4556+
char tmp_dir[256];
4557+
snprintf(tmp_dir, sizeof(tmp_dir), "/tmp/cbm-idx853-repo-XXXXXX");
4558+
if (!cbm_mkdtemp(tmp_dir)) {
4559+
PASS();
4560+
}
4561+
char cache[256];
4562+
snprintf(cache, sizeof(cache), "/tmp/cbm-idx853-cache-XXXXXX");
4563+
if (!cbm_mkdtemp(cache)) {
4564+
cbm_rmdir(tmp_dir);
4565+
PASS();
4566+
}
4567+
4568+
const char *saved_cache = getenv("CBM_CACHE_DIR");
4569+
char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL;
4570+
cbm_setenv("CBM_CACHE_DIR", cache, 1); /* inherited by the worker child */
4571+
4572+
char src_path[512];
4573+
snprintf(src_path, sizeof(src_path), "%s/main.py", tmp_dir);
4574+
FILE *fp = fopen(src_path, "w");
4575+
ASSERT_NOT_NULL(fp);
4576+
fputs("def idx853_fn():\n return 'ok'\n", fp);
4577+
fclose(fp);
4578+
4579+
int code = -1;
4580+
bool signalled = false;
4581+
int sig = 0;
4582+
fflush(NULL);
4583+
pid_t pid = fork();
4584+
if (pid == 0) {
4585+
alarm(60); /* a stuck worker dies here instead of hanging the runner */
4586+
_exit(idx853_supervised_autowatch_check(tmp_dir, cache));
4587+
}
4588+
ASSERT_TRUE(pid > 0);
4589+
int status = 0;
4590+
(void)waitpid(pid, &status, 0);
4591+
if (WIFEXITED(status)) {
4592+
code = WEXITSTATUS(status);
4593+
} else if (WIFSIGNALED(status)) {
4594+
signalled = true;
4595+
sig = WTERMSIG(status);
4596+
}
4597+
4598+
char *project = cbm_project_name_from_path(tmp_dir);
4599+
cleanup_project_db(cache, project);
4600+
free(project);
4601+
restore_cache_dir(saved_cache_copy);
4602+
free(saved_cache_copy);
4603+
remove(src_path);
4604+
cbm_rmdir(cache);
4605+
cbm_rmdir(tmp_dir);
4606+
4607+
if (signalled) {
4608+
printf(" child killed by signal %d (alarm => worker hang)\n", sig);
4609+
} else if (code != IDX853_OK) {
4610+
printf(" child exit code %d (61=watcher registered under auto_watch=false=RED, "
4611+
"62=no spawn, 63=setup fail, 64=bad count)\n",
4612+
code);
4613+
}
4614+
ASSERT_FALSE(signalled);
4615+
ASSERT_EQ(code, IDX853_OK);
4616+
PASS();
4617+
#endif
4618+
}
4619+
44434620
/* ══════════════════════════════════════════════════════════════════
44444621
* SUITE
44454622
* ══════════════════════════════════════════════════════════════════ */
@@ -4620,4 +4797,5 @@ SUITE(mcp) {
46204797
/* auto_watch gate (distilled from PR #625) */
46214798
RUN_TEST(mcp_auto_watch_default_registers_watcher_on_connect);
46224799
RUN_TEST(mcp_auto_watch_false_skips_watcher_on_connect);
4800+
RUN_TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853);
46234801
}

0 commit comments

Comments
 (0)