|
9 | 9 | #include "../src/foundation/log.h" |
10 | 10 | #include "test_framework.h" |
11 | 11 | #include <cli/cli.h> |
| 12 | +#include <mcp/index_supervisor.h> /* spawn-count hook — #845 in-process guard */ |
12 | 13 | #include <mcp/mcp.h> |
13 | 14 | #include <pipeline/pipeline.h> |
14 | 15 | #include <store/store.h> |
|
23 | 24 | #define cbm_chdir _chdir |
24 | 25 | #define cbm_getcwd _getcwd |
25 | 26 | #else |
| 27 | +#include <sys/wait.h> /* waitpid — #845 fork+alarm harness */ |
26 | 28 | #include <unistd.h> |
27 | 29 | #define cbm_chdir chdir |
28 | 30 | #define cbm_getcwd getcwd |
@@ -4032,6 +4034,138 @@ TEST(readonly_query_succeeds_on_readonly_fs) { |
4032 | 4034 |
|
4033 | 4035 | #undef ROQ_PROJECT |
4034 | 4036 |
|
| 4037 | +/* ══════════════════════════════════════════════════════════════════ |
| 4038 | + * #845 — supervisor gate must not wrap embedders of cbm_mcp_handle_tool |
| 4039 | + * ══════════════════════════════════════════════════════════════════ */ |
| 4040 | + |
| 4041 | +/* Child-side check: index a tiny fixture and verify it ran IN-PROCESS. |
| 4042 | + * Distinct exit codes so the parent can report the exact failure mode. */ |
| 4043 | +enum { |
| 4044 | + IDX845_OK = 0, |
| 4045 | + IDX845_SPAWNED = 41, /* a worker subprocess was spawned — the #845 bug */ |
| 4046 | + IDX845_NO_RESULT = 42, /* handle_tool returned NULL */ |
| 4047 | + IDX845_NOT_INDEXED = 43, /* response lacks status=indexed */ |
| 4048 | +}; |
| 4049 | + |
| 4050 | +static int idx845_index_inprocess_check(const char *repo_dir) { |
| 4051 | + int spawns_before = cbm_index_supervisor_spawn_count(); |
| 4052 | + |
| 4053 | + cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); |
| 4054 | + if (!srv) { |
| 4055 | + return IDX845_NO_RESULT; |
| 4056 | + } |
| 4057 | + char args[1024]; |
| 4058 | + snprintf(args, sizeof(args), "{\"repo_path\":\"%s\",\"mode\":\"fast\"}", repo_dir); |
| 4059 | + char *resp = cbm_mcp_handle_tool(srv, "index_repository", args); |
| 4060 | + |
| 4061 | + int code = IDX845_OK; |
| 4062 | + if (cbm_index_supervisor_spawn_count() != spawns_before) { |
| 4063 | + code = IDX845_SPAWNED; |
| 4064 | + } else if (!resp) { |
| 4065 | + code = IDX845_NO_RESULT; |
| 4066 | + } else if (!response_contains_json_fragment(resp, "\"status\":\"indexed\"")) { |
| 4067 | + code = IDX845_NOT_INDEXED; |
| 4068 | + } |
| 4069 | + free(resp); |
| 4070 | + cbm_mcp_server_free(srv); |
| 4071 | + return code; |
| 4072 | +} |
| 4073 | + |
| 4074 | +TEST(index_supervisor_gate_requires_marked_host_issue845) { |
| 4075 | + /* #845: index_repository via cbm_mcp_handle_tool from an EMBEDDER (this test |
| 4076 | + * binary) must index IN-PROCESS even with CBM_INDEX_SUPERVISOR unset. The |
| 4077 | + * supervisor gate may only wrap a process that called |
| 4078 | + * cbm_index_supervisor_mark_host() — i.e. the real binary's main(). Before |
| 4079 | + * the fix, should_wrap() was true for ANY embedder: the gate resolved the |
| 4080 | + * CURRENT binary (this test runner!) and spawned |
| 4081 | + * '<test-runner> cli --index-worker index_repository …', which a test binary |
| 4082 | + * interprets as suite-filter args → it re-runs test suites in the child → |
| 4083 | + * recursive spawn chains (observed 11-min hangs; kernel VM-map load during |
| 4084 | + * the 2026-07-04 host panics). |
| 4085 | + * |
| 4086 | + * POSIX: run the call in a forked child under alarm(20) so the pre-fix |
| 4087 | + * recursive behaviour cannot hang the runner; the child reports via exit |
| 4088 | + * code. Windows: no fork — run in-process (safe once the gate is fixed; the |
| 4089 | + * pre-fix redness is demonstrated on POSIX). */ |
| 4090 | + char tmp_dir[256]; |
| 4091 | + snprintf(tmp_dir, sizeof(tmp_dir), "/tmp/cbm-idx845-repo-XXXXXX"); |
| 4092 | + if (!cbm_mkdtemp(tmp_dir)) { |
| 4093 | + PASS(); |
| 4094 | + } |
| 4095 | + char cache[256]; |
| 4096 | + snprintf(cache, sizeof(cache), "/tmp/cbm-idx845-cache-XXXXXX"); |
| 4097 | + if (!cbm_mkdtemp(cache)) { |
| 4098 | + cbm_rmdir(tmp_dir); |
| 4099 | + PASS(); |
| 4100 | + } |
| 4101 | + |
| 4102 | + const char *saved_cache = getenv("CBM_CACHE_DIR"); |
| 4103 | + char *saved_cache_copy = saved_cache ? strdup(saved_cache) : NULL; |
| 4104 | + cbm_setenv("CBM_CACHE_DIR", cache, 1); |
| 4105 | + |
| 4106 | + /* The point of the guard: NO kill switch. The gate itself must keep an |
| 4107 | + * unmarked host in-process. Save + restore the ambient value. */ |
| 4108 | + const char *saved_sv = getenv("CBM_INDEX_SUPERVISOR"); |
| 4109 | + char *saved_sv_copy = saved_sv ? strdup(saved_sv) : NULL; |
| 4110 | + cbm_unsetenv("CBM_INDEX_SUPERVISOR"); |
| 4111 | + |
| 4112 | + char src_path[512]; |
| 4113 | + snprintf(src_path, sizeof(src_path), "%s/main.py", tmp_dir); |
| 4114 | + FILE *fp = fopen(src_path, "w"); |
| 4115 | + ASSERT_NOT_NULL(fp); |
| 4116 | + fputs("def main():\n return 'ok'\n", fp); |
| 4117 | + fclose(fp); |
| 4118 | + |
| 4119 | + int code = -1; |
| 4120 | + bool signalled = false; |
| 4121 | + int sig = 0; |
| 4122 | +#ifdef _WIN32 |
| 4123 | + code = idx845_index_inprocess_check(tmp_dir); |
| 4124 | +#else |
| 4125 | + fflush(NULL); |
| 4126 | + pid_t pid = fork(); |
| 4127 | + if (pid == 0) { |
| 4128 | + alarm(20); /* pre-fix spawn chain must die here, not hang the runner */ |
| 4129 | + _exit(idx845_index_inprocess_check(tmp_dir)); |
| 4130 | + } |
| 4131 | + ASSERT_TRUE(pid > 0); |
| 4132 | + int status = 0; |
| 4133 | + (void)waitpid(pid, &status, 0); |
| 4134 | + if (WIFEXITED(status)) { |
| 4135 | + code = WEXITSTATUS(status); |
| 4136 | + } else if (WIFSIGNALED(status)) { |
| 4137 | + signalled = true; |
| 4138 | + sig = WTERMSIG(status); |
| 4139 | + } |
| 4140 | +#endif |
| 4141 | + |
| 4142 | + /* Restore env BEFORE asserting so a red run doesn't leak state. */ |
| 4143 | + if (saved_sv_copy) { |
| 4144 | + cbm_setenv("CBM_INDEX_SUPERVISOR", saved_sv_copy, 1); |
| 4145 | + free(saved_sv_copy); |
| 4146 | + } else { |
| 4147 | + cbm_unsetenv("CBM_INDEX_SUPERVISOR"); |
| 4148 | + } |
| 4149 | + char *project = cbm_project_name_from_path(tmp_dir); |
| 4150 | + cleanup_project_db(cache, project); |
| 4151 | + free(project); |
| 4152 | + restore_cache_dir(saved_cache_copy); |
| 4153 | + free(saved_cache_copy); |
| 4154 | + remove(src_path); |
| 4155 | + cbm_rmdir(cache); |
| 4156 | + cbm_rmdir(tmp_dir); |
| 4157 | + |
| 4158 | + if (signalled) { |
| 4159 | + printf(" child killed by signal %d (alarm => recursive spawn chain hang)\n", sig); |
| 4160 | + } else if (code != IDX845_OK) { |
| 4161 | + printf(" child exit code %d (41=worker spawned, 42=no result, 43=not indexed)\n", |
| 4162 | + code); |
| 4163 | + } |
| 4164 | + ASSERT_FALSE(signalled); |
| 4165 | + ASSERT_EQ(code, IDX845_OK); |
| 4166 | + PASS(); |
| 4167 | +} |
| 4168 | + |
4035 | 4169 | /* ══════════════════════════════════════════════════════════════════ |
4036 | 4170 | * SUITE |
4037 | 4171 | * ══════════════════════════════════════════════════════════════════ */ |
@@ -4153,6 +4287,7 @@ SUITE(mcp) { |
4153 | 4287 | RUN_TEST(tool_manage_adr_unified_backend_issue256); |
4154 | 4288 | RUN_TEST(tool_index_repository_reports_store_backed_adr); |
4155 | 4289 | RUN_TEST(tool_index_repository_dot_uses_absolute_project_key_and_preserves_adr); |
| 4290 | + RUN_TEST(index_supervisor_gate_requires_marked_host_issue845); |
4156 | 4291 | RUN_TEST(tool_manage_adr_not_found_rich_error); |
4157 | 4292 | RUN_TEST(tool_manage_adr_get_accepts_abs_path); |
4158 | 4293 | RUN_TEST(tool_manage_adr_get_accepts_symlink_path); |
|
0 commit comments