helper used only by the POSIX fork harness below */
| 9783 | |
| 9784 | #ifndef _WIN32 /* helper used only by the POSIX fork harness below */ |
| 9785 | static int idx853_supervised_autowatch_check(const char *repo_dir, const char *cache_dir) { |
| 9786 | /* Become a supervisor host with the kill switch OFF — the real prod MCP |
| 9787 | * server's state. Done in the FORKED CHILD only (see harness) so the parent |
| 9788 | * test-runner's process-wide host mark stays clear (#845 invariant). Bound the |
| 9789 | * worker so a stuck spawn cannot run long under the fork+alarm net. */ |
| 9790 | cbm_index_supervisor_mark_host(); |
| 9791 | cbm_unsetenv("CBM_INDEX_SUPERVISOR"); |
| 9792 | cbm_setenv("CBM_INDEX_MAX_RESTARTS", "1", 1); |
| 9793 | cbm_setenv("CBM_INDEX_WORKER_TIMEOUT_S", "30", 1); |
| 9794 | |
| 9795 | cbm_config_t *cfg = cbm_config_open(cache_dir); |
| 9796 | cbm_store_t *wstore = cbm_store_open_memory(); |
| 9797 | cbm_watcher_t *watcher = wstore ? cbm_watcher_new(wstore, NULL, NULL) : NULL; |
| 9798 | if (!cfg || !watcher) { |
| 9799 | if (watcher) { |
| 9800 | cbm_watcher_free(watcher); |
| 9801 | } |
| 9802 | if (wstore) { |
| 9803 | cbm_store_close(wstore); |
| 9804 | } |
| 9805 | if (cfg) { |
| 9806 | cbm_config_close(cfg); |
| 9807 | } |
| 9808 | return IDX853_SETUP_FAIL; |
| 9809 | } |
| 9810 | /* auto_index=true → maybe_auto_index launches autoindex_thread for the fresh |
| 9811 | * project; auto_watch=false → the gate this guard exercises. */ |
| 9812 | cbm_config_set(cfg, CBM_CONFIG_AUTO_INDEX, "true"); |
| 9813 | cbm_config_set(cfg, CBM_CONFIG_AUTO_WATCH, "false"); |
| 9814 | |
| 9815 | /* detect_session derives session_root/session_project from the cwd. */ |
| 9816 | char old_cwd[1024]; |
| 9817 | if (!cbm_getcwd(old_cwd, sizeof(old_cwd)) || cbm_chdir(repo_dir) != 0) { |
| 9818 | cbm_watcher_free(watcher); |
| 9819 | cbm_store_close(wstore); |
| 9820 | cbm_config_close(cfg); |
| 9821 | return IDX853_SETUP_FAIL; |
| 9822 | } |
| 9823 | |
| 9824 | int spawns_before = cbm_index_supervisor_spawn_count(); |
| 9825 | int code = IDX853_SETUP_FAIL; |
| 9826 | |
| 9827 | cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); |
| 9828 | if (srv) { |
| 9829 | cbm_mcp_server_set_watcher(srv, watcher); |
| 9830 | cbm_mcp_server_set_config(srv, cfg); |
| 9831 | char *resp = cbm_mcp_server_handle( |
| 9832 | srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}"); |
| 9833 | free(resp); |
| 9834 | /* free() joins the autoindex thread → the supervised worker has finished |
| 9835 | * and the registration decision (buggy or gated) has executed. */ |
| 9836 | cbm_mcp_server_free(srv); |
| 9837 | |
| 9838 | int spawns_after = cbm_index_supervisor_spawn_count(); |
| 9839 | int watch_count = cbm_watcher_watch_count(watcher); |
| 9840 | |
| 9841 | if (spawns_after == spawns_before) { |
| 9842 | code = IDX853_NO_SPAWN; /* supervised branch never ran — not a valid probe */ |
no test coverage detected