Skip to content

Commit 37c00d1

Browse files
authored
Merge pull request DeusData#851 from DeusData/fix/845-supervisor-host-gate
fix(supervisor): wrap only marked host binaries; watchdog in worker mode
2 parents 54efcef + 4fff22f commit 37c00d1

8 files changed

Lines changed: 357 additions & 2 deletions

File tree

‎scripts/test.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ echo "=== Step 5: parent-death watchdog regression (#406/#407) ==="
7575
make -j"$NPROC" -f Makefile.cbm cbm $MAKE_ARGS
7676
bash "$ROOT/tests/test_parent_watchdog.sh"
7777

78+
# Step 5b: worker-mode parent-death watchdog (#845). A supervised index worker
79+
# (`cli --index-worker …`) whose supervisor dies must self-exit instead of
80+
# indexing on as an orphan. Reuses the prod binary built in Step 5.
81+
echo "=== Step 5b: worker-mode watchdog regression (#845) ==="
82+
bash "$ROOT/tests/test_worker_watchdog.sh"
83+
7884
# Step 6: security-strings URL allow-list regression. The MSYS2 CLANG64 toolchain
7985
# bakes its package-tracker URL into the static Windows .exe; the binary string
8086
# audit must allow-list it (Windows-only — Linux smoke never saw it).

‎src/main.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,29 @@ static int run_cli(int argc, char **argv) {
328328
const char *response_out = cli_strip_flag_value(&argc, argv, "--response-out");
329329
cbm_index_set_worker_role(index_worker, response_out);
330330

331+
#ifndef _WIN32
332+
/* #845: a supervised worker must not outlive its supervisor. If the parent
333+
* dies without reaping us (agent killed, supervisor crashed), an orphaned
334+
* worker would index on unsupervised — observed contributing to memory
335+
* pressure during the 2026-07-04 host panics. Reuse the parent-death
336+
* watchdog (safe outside server mode: on ppid change it only writes to
337+
* stderr and _exit(0)s — no cleanup dependencies). Detached: the worker
338+
* exits by returning from run_cli; exit() tears the thread down. Failure
339+
* to start is non-fatal, same policy as the MCP-server watchdog. */
340+
if (index_worker) {
341+
static pid_t worker_initial_ppid; /* static: outlives run_cli for the thread */
342+
worker_initial_ppid = getppid();
343+
cbm_thread_t worker_watchdog_tid;
344+
if (cbm_thread_create(&worker_watchdog_tid, PARENT_WATCHDOG_STACK_SIZE,
345+
parent_watchdog_thread, &worker_initial_ppid) == 0) {
346+
(void)cbm_thread_detach(&worker_watchdog_tid);
347+
cbm_log_info("worker.watchdog.start");
348+
} else {
349+
cbm_log_warn("worker.watchdog.unavailable", "reason", "thread_create_failed");
350+
}
351+
}
352+
#endif
353+
331354
if (argc < MAIN_MIN_ARGC) {
332355
(void)fprintf(stderr, CLI_USAGE);
333356
return SKIP_ONE;
@@ -563,6 +586,13 @@ int main(int argc, char **argv) {
563586
* below opens sqlite early), else sqlite3_config returns SQLITE_MISUSE and
564587
* the bind is silently ignored. No-op in the test build. */
565588
cbm_alloc_init();
589+
/* #845: mark this process as the REAL binary so the index supervisor may
590+
* wrap index_repository in a worker subprocess. Must run before any
591+
* subcommand dispatch so MCP-server, CLI, and HTTP paths are all covered.
592+
* Embedders of cbm_mcp_handle_tool (test binaries) never mark themselves,
593+
* so they index in-process instead of re-invoking themselves as
594+
* `<self> cli --index-worker …` (recursive suite re-runs / spawn chains). */
595+
cbm_index_supervisor_mark_host();
566596
cbm_cli_set_version(CBM_VERSION);
567597
cbm_profile_init(); /* reads CBM_PROFILE env var, gates all prof macros */
568598
/* CBM_LOG_LEVEL support — distilled from #414 (closes #413). Apply before

‎src/mcp/index_supervisor.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,27 @@ const char *cbm_index_worker_response_out(void) {
4343
return g_worker_response_out[0] ? g_worker_response_out : NULL;
4444
}
4545

46+
/* Test hook (#845): counts spawn ATTEMPTS (entry to cbm_index_spawn_worker),
47+
* including ones that fail to resolve the self binary — an embedder must never
48+
* even try to spawn. */
49+
static int g_spawn_count = 0;
50+
51+
int cbm_index_supervisor_spawn_count(void) {
52+
return g_spawn_count;
53+
}
54+
55+
/* #845: opt-in host mark — see the header. Set once from the real binary's
56+
* main(); embedders never set it, so should_wrap() stays false for them. */
57+
static bool g_host_marked = false;
58+
59+
void cbm_index_supervisor_mark_host(void) {
60+
g_host_marked = true;
61+
}
62+
4663
bool cbm_index_supervisor_should_wrap(void) {
64+
if (!g_host_marked) {
65+
return false; /* embedder (#845): never spawn `<self> cli --index-worker` */
66+
}
4767
if (g_worker_active) {
4868
return false; /* I am the worker — run in-process, never re-supervise */
4969
}
@@ -115,6 +135,7 @@ static void worker_tmp_path(char *out, size_t out_sz, int pid, const char *suffi
115135

116136
int cbm_index_spawn_worker(const char *args_json, bool single_thread, const char *marker_file,
117137
const char *quarantine_file, cbm_index_worker_result_t *result) {
138+
g_spawn_count++; /* test hook (#845) — see cbm_index_supervisor_spawn_count */
118139
result->outcome = CBM_PROC_SPAWN_FAILED;
119140
result->exit_code = -1;
120141
result->term_signal = 0;

‎src/mcp/index_supervisor.h‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,28 @@ void cbm_index_set_worker_role(bool is_worker, const char *response_out);
3232
bool cbm_index_worker_active(void);
3333
const char *cbm_index_worker_response_out(void);
3434

35+
/* Host marking (#845): the supervisor gate is OPT-IN per process. Only the real
36+
* codebase-memory-mcp binary calls this (first thing in main(), before any
37+
* subcommand dispatch, so MCP server + CLI + HTTP paths are all covered).
38+
* EMBEDDERS of cbm_mcp_handle_tool (test binaries, future library users) never
39+
* call it, so they index in-process by default. Without this gate the supervisor
40+
* resolved the CURRENT executable and re-invoked it as
41+
* `<self> cli --index-worker …` — a test binary ignores those args and re-runs
42+
* its suites instead, producing recursive spawn chains (11-min hangs; kernel
43+
* VM-map pressure during the 2026-07-04 host panics). */
44+
void cbm_index_supervisor_mark_host(void);
45+
3546
/* True when handle_index_repository should wrap the run in a supervised child:
36-
* this process is not itself a worker AND the kill switch (CBM_INDEX_SUPERVISOR=0)
37-
* is not set. */
47+
* this process called cbm_index_supervisor_mark_host() (i.e. it IS the real
48+
* binary, not an embedder), is not itself a worker, AND the kill switch
49+
* (CBM_INDEX_SUPERVISOR=0) is not set. */
3850
bool cbm_index_supervisor_should_wrap(void);
3951

52+
/* TEST HOOK (#845): process-wide count of worker-spawn attempts, incremented on
53+
* entry to cbm_index_spawn_worker. Embedder tests assert the count is unchanged
54+
* across an index_repository call to prove indexing ran IN-PROCESS. */
55+
int cbm_index_supervisor_spawn_count(void);
56+
4057
typedef struct {
4158
cbm_proc_outcome_t outcome; /* how the worker ended */
4259
int exit_code; /* worker exit code (-1 if signalled) */

‎tests/repro/repro_main.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int tf_fail_count = 0;
2626
int tf_skip_count = 0;
2727

2828
#include "test_framework.h"
29+
#include "foundation/compat.h" /* cbm_setenv — #845 supervisor kill switch */
2930

3031
/* Per-suite summary + filter. RUN_SUITE prints a one-line
3132
* "[SUITE] <name> P passed, F failed" report (greppable for which suites still
@@ -112,6 +113,13 @@ extern void suite_repro_lsp_java_cs(void);
112113
extern void suite_repro_lsp_kt_php_rust(void);
113114

114115
int main(void) {
116+
/* #845 belt-and-suspenders: this binary EMBEDS cbm_mcp_handle_tool and its
117+
* main() IGNORES argv — spawned as `<self> cli --index-worker …` it would
118+
* re-run EVERY repro suite recursively (the observed 11-min hangs). The
119+
* supervisor gate already ignores unmarked hosts; pin the kill switch too.
120+
* A test that exercises the supervisor must explicitly re-enable it. */
121+
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
122+
115123
/* Unbuffered: a reproduction may crash/_exit (or a sanitizer may _exit on a
116124
* leak) before stdio flushes — keep every printed line so the summary and the
117125
* RED rows always reach the board even on an abnormal exit. */

‎tests/test_main.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ int tf_fail_count = 0;
99
int tf_skip_count = 0;
1010

1111
#include "test_framework.h"
12+
#include "foundation/compat.h" /* cbm_setenv — #845 supervisor kill switch */
1213
#include <sqlite3.h>
1314
#include <stdbool.h>
1415
#include <string.h>
@@ -134,6 +135,13 @@ extern void suite_dump_verify_io(void);
134135
extern void cbm_kind_in_set_free_cache(void);
135136

136137
int main(int argc, char **argv) {
138+
/* #845 belt-and-suspenders: this binary EMBEDS cbm_mcp_handle_tool. The
139+
* supervisor gate already ignores unmarked hosts, but pin the kill switch
140+
* too so even a future supervisor-marked test host can never resolve THIS
141+
* binary as `<self> cli --index-worker …` and recursively re-run suites.
142+
* A test that exercises the supervisor must explicitly re-enable it. */
143+
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
144+
137145
g_suite_argc = argc;
138146
g_suite_argv = argv;
139147
printf("\n codebase-memory-mcp C test suite\n");

‎tests/test_mcp.c‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "test_framework.h"
1111
#include "test_helpers.h"
1212
#include <cli/cli.h>
13+
#include <mcp/index_supervisor.h> /* spawn-count hook — #845 in-process guard */
1314
#include <mcp/mcp.h>
1415
#include <pipeline/pipeline.h>
1516
#include <store/store.h>
@@ -25,6 +26,7 @@
2526
#define cbm_chdir _chdir
2627
#define cbm_getcwd _getcwd
2728
#else
29+
#include <sys/wait.h> /* waitpid — #845 fork+alarm harness */
2830
#include <unistd.h>
2931
#define cbm_chdir chdir
3032
#define cbm_getcwd getcwd
@@ -4034,6 +4036,138 @@ TEST(readonly_query_succeeds_on_readonly_fs) {
40344036

40354037
#undef ROQ_PROJECT
40364038

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

0 commit comments

Comments
 (0)