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
fix(windows): add headroom and diagnostics to worker memory limits
Subgurim2b8bd0c065c1
diff --cc src/foundation/subprocess.cindex a12bd19c4,196165cea..475fe6741--- a/src/foundation/subprocess.c+++ b/src/foundation/subprocess.c@@@ -791,6 -785,6 +791,17 @@@ static bool cbm_win_job_active(cbm_subpreturn accounting.ActiveProcesses != 0;}++static void cbm_win_capture_job_memory(cbm_subprocess_t *process) {++ process->result.job_memory_limit_bytes = process->memory_limit_bytes;++ JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits;++ ZeroMemory(&limits, sizeof(limits));++ if (QueryInformationJobObject(process->job, JobObjectExtendedLimitInformation, &limits,++ sizeof(limits), NULL)) {++ process->result.peak_job_memory_bytes = (size_t)limits.PeakJobMemoryUsed;++ process->result.job_memory_available = true;++ }++}++static void cbm_win_begin_termination(cbm_subprocess_t *process, uint64_t now) {if (process->termination_started) {return;@@@ -879,9 -873,9 +890,11 @@@ static cbm_proc_poll_t cbm_subprocess_pif (process->force_started_ms != 0 &&now - process->force_started_ms >= CBM_SUBPROCESS_FORCE_SETTLE_MS &&(!job_known || job_active || !process->root_reaped)) {++ cbm_win_capture_job_memory(process);return cbm_subprocess_finish_failed(process, out);}if (process->root_reaped && !job_active) {++ cbm_win_capture_job_memory(process);return cbm_subprocess_finish(process, out);}return CBM_PROC_POLL_RUNNING;diff --cc src/foundation/subprocess.hindex df3fba844,62592c592..52267272c--- a/src/foundation/subprocess.h+++ b/src/foundation/subprocess.h@@@ -42,6 -42,6 +42,9 @@@ typedef structbool forced; /* force was needed after grace expiry or to reap descendants after root exit */bool tree_quiesced; /* the owned process tree has no surviving processes */bool supervision_failed; /* the bounded containment deadline expired; tree_quiesced is false */++ size_t job_memory_limit_bytes; /* Windows Job Object commit cap; zero on POSIX or uncapped */++ size_t peak_job_memory_bytes; /* Windows peak commit, not RSS or proof of a limit hit */++ bool job_memory_available; /* terminal Job Object memory query succeeded */} cbm_proc_result_t;/* Called synchronously for each newly-completed log chunk while the child runs.diff --cc src/mcp/index_supervisor.cindex 000bdafb0,eed78f418..4f4f3ac51--- a/src/mcp/index_supervisor.c+++ b/src/mcp/index_supervisor.c@@@ -588,6 -598,6 +598,20 @@@ static void worker_terminal_log(cbm_ind(void)snprintf(exit_text, sizeof(exit_text), "%d", handle->result.exit_code);cbm_log_info("index.supervisor.reap", "outcome", cbm_proc_outcome_str(handle->result.outcome),"exit_code", exit_text, "signal", signal_text);++ if (!worker_result_succeeded(&handle->result) &&++ handle->process_result.job_memory_limit_bytes > 0) {++ char limit_text[CBM_SZ_32];++ char peak_text[CBM_SZ_32];++ (void)snprintf(limit_text, sizeof(limit_text), "%zu",++ handle->process_result.job_memory_limit_bytes);++ (void)snprintf(peak_text, sizeof(peak_text), "%zu",++ handle->process_result.peak_job_memory_bytes);++ /* A rejected allocation can leave peak commit BELOW the cap. Report++ * evidence, not an inferred OOM classification or a file to quarantine. */++ cbm_log_warn("index.supervisor.worker_memory", "job_limit_bytes", limit_text,++ "peak_job_memory_bytes",++ handle->process_result.job_memory_available ? peak_text : "unavailable");++ }if (handle->result.response_rejected) {cbm_log_error("index.supervisor.response_rejected", "reason", "payload_too_large", "log",handle->log_path);@@@ -608,6 -618,6 +632,23 @@@}}++size_t cbm_index_worker_job_memory_limit(size_t memory_budget_bytes) {++ /* Tiny token budgets must still reach main (image/CRT/stack startup already++ * needs memory). Keep their argv value but leave the OS cap disabled below++ * 512 MiB. KILL_ON_JOB_CLOSE remains in force regardless of the cap.++ *++ * The cooperative RSS budget, including its fail-whole over-budget gate,++ * gets first chance to recover/abort. Windows charges job-wide COMMIT, not++ * RSS, so this 1.5x limit is a looser last-resort backstop for descendants++ * too, not an exact enforcement of the worker's accounting budget. */++ const size_t minimum_budget = (size_t)512U * 1024U * 1024U;++ if (memory_budget_bytes < minimum_budget) {++ return 0;++ }++ size_t headroom = memory_budget_bytes / 2;++ return memory_budget_bytes > SIZE_MAX - headroom ? SIZE_MAX : memory_budget_bytes + headroom;++}++int cbm_index_worker_start_with_log(const char *args_json, size_t memory_budget_bytes,bool single_thread, const char *marker_file,const char *quarantine_file, cbm_proc_log_cb log_callback,@@@ -695,7 -705,6 +736,7 @@@options.on_log_line = NULL;options.log_ud = NULL;options.quiet_timeout_ms = worker_quiet_timeout_ms();- options.memory_limit_bytes = memory_budget_bytes;++ options.memory_limit_bytes = cbm_index_worker_job_memory_limit(memory_budget_bytes);options.delete_log_on_exit = false;if (cbm_subprocess_spawn(&options, &handle->process) != 0) {(void)cbm_unlink(handle->response_path);diff --cc src/mcp/index_supervisor.hindex 31259df90,31259df90..1acad4cf7--- a/src/mcp/index_supervisor.h+++ b/src/mcp/index_supervisor.h@@@ -151,6 -151,6 +151,10 @@@ typedef enumCBM_INDEX_WORKER_POLL_TERMINAL = 1,} cbm_index_worker_poll_t;++/* Pure Windows backstop policy, available on all platforms for regression tests.++ * Small/zero budgets disable the OS cap, not the cooperative budget or containment. */++size_t cbm_index_worker_job_memory_limit(size_t memory_budget_bytes);++/* Start returns after process creation. All string arguments are copied by the* contained subprocess layer. Recovery values are encoded only as hidden argv* and applied after exec by cbm_index_set_worker_role_options(). */diff --cc tests/test_index_supervisor.cindex c67f61fc4,8a65a4c03..d69354d76--- a/tests/test_index_supervisor.c+++ b/tests/test_index_supervisor.c@@@ -6,6 -6,6 +6,7 @@@#include "daemon/bootstrap.h"#include "foundation/compat.h"#include "foundation/compat_fs.h"++#include "foundation/log.h"#include "foundation/platform.h"#include "foundation/profile.h"#include "mcp/index_supervisor.h"@@@ -560,7 -560,7 +561,8 @@@ static bool index_supervisor_test_run_p(void)snprintf(args, sizeof(args), "{\"__cbm_test_worker\":\"%s\"}", mode);cbm_profile_active = profiling;cbm_index_worker_handle_t *handle = NULL;-- if (cbm_index_worker_start(args, 0, false, NULL, NULL, &handle) != 0 || !handle) {++ const size_t budget = (size_t)1024U * 1024U * 1024U;++ if (cbm_index_worker_start(args, budget, false, NULL, NULL, &handle) != 0 || !handle) {return false;}char log_path[INDEX_SUPERVISOR_TEST_PATH_CAP];@@@ -588,6 -588,6 +590,17 @@@return terminal;}++static char g_index_supervisor_memory_log[1024];++static int g_index_supervisor_memory_log_count;++++static void index_supervisor_test_memory_log_sink(const char *line) {++ if (strstr(line, "index.supervisor.worker_memory")) {++ g_index_supervisor_memory_log_count++;++ (void)snprintf(g_index_supervisor_memory_log, sizeof(g_index_supervisor_memory_log), "%s",++ line);++ }++}++TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {char cache[INDEX_SUPERVISOR_TEST_PATH_CAP];(void)snprintf(cache, sizeof(cache), "%s/cbm-index-logs-XXXXXX", cbm_tmpdir());@@@ -597,6 -597,6 +610,14 @@@bool saved_profile = cbm_profile_active;(void)cbm_setenv("CBM_CACHE_DIR", cache, 1);++ CBMLogLevel saved_level = cbm_log_get_level();++ CBMLogFormat saved_format = cbm_log_get_format();++ cbm_log_set_level(CBM_LOG_INFO);++ cbm_log_set_format(CBM_LOG_FORMAT_TEXT);++ g_index_supervisor_memory_log[0] = '\0';++ g_index_supervisor_memory_log_count = 0;++ cbm_log_set_sink_ex(index_supervisor_test_memory_log_sink, CBM_LOG_SINK_TEE);++cbm_proc_outcome_t clean_outcome = CBM_PROC_SPAWN_FAILED;cbm_proc_outcome_t profile_outcome = CBM_PROC_SPAWN_FAILED;cbm_proc_outcome_t crash_outcome = CBM_PROC_SPAWN_FAILED;@@@ -621,6 -621,6 +642,9 @@@bool crash_classified_failure = crash_outcome == CBM_PROC_CRASH;#endif++ cbm_log_set_sink(NULL);++ cbm_log_set_level(saved_level);++ cbm_log_set_format(saved_format);cbm_profile_active = saved_profile;index_supervisor_test_restore_env("CBM_CACHE_DIR", saved_cache);(void)th_rmtree(cache);@@@ -640,6 -640,50 +664,58 @@@ASSERT_FALSE(crash_response);ASSERT_TRUE(crash_log);ASSERT_FALSE(crash_response_file);++#ifdef _WIN32++ ASSERT_EQ(g_index_supervisor_memory_log_count, 1); /* crash only, never clean */++ ASSERT_TRUE(strstr(g_index_supervisor_memory_log, "job_limit_bytes=1610612736") != NULL);++ ASSERT_TRUE(strstr(g_index_supervisor_memory_log, "peak_job_memory_bytes=") != NULL);++ ASSERT_TRUE(strstr(g_index_supervisor_memory_log, "unavailable") == NULL);++#else++ ASSERT_EQ(g_index_supervisor_memory_log_count, 0);++#endif+ PASS();+ }++ TEST(index_supervisor_worker_keeps_default_info_liveness_heartbeat) {+ char cache[INDEX_SUPERVISOR_TEST_PATH_CAP];+ (void)snprintf(cache, sizeof(cache), "%s/cbm-index-heartbeat-XXXXXX", cbm_tmpdir());+ ASSERT_NOT_NULL(cbm_mkdtemp(cache));+ const char *old_cache = getenv("CBM_CACHE_DIR");+ const char *old_log_level = getenv("CBM_LOG_LEVEL");+ char *saved_cache = old_cache ? cbm_strdup(old_cache) : NULL;+ char *saved_log_level = old_log_level ? cbm_strdup(old_log_level) : NULL;+ (void)cbm_setenv("CBM_CACHE_DIR", cache, 1);+ (void)cbm_unsetenv("CBM_LOG_LEVEL");++ cbm_index_worker_handle_t *handle = NULL;+ int start_rc = cbm_index_worker_start("{\"__cbm_test_worker\":\"heartbeat\"}", 0, false,+ NULL, NULL, &handle);+ char log_path[INDEX_SUPERVISOR_TEST_PATH_CAP] = {0};+ if (handle) {+ (void)snprintf(log_path, sizeof(log_path), "%s", cbm_index_worker_log_path(handle));+ }+ bool ready = log_path[0] && index_supervisor_test_wait_file_text(+ log_path, "async worker heartbeat probe ready",+ INDEX_SUPERVISOR_TEST_READY_MS);+ bool heartbeat = ready && index_supervisor_test_wait_file_text(+ log_path, "msg=pipeline.discover", 1000);+ const cbm_index_worker_result_t *result = NULL;+ bool terminal = handle && index_supervisor_test_poll_terminal(+ handle, INDEX_SUPERVISOR_TEST_TERMINAL_MS, &result);+ bool clean = terminal && result && result->outcome == CBM_PROC_CLEAN && result->response;+ if (terminal) {+ cbm_index_worker_destroy(handle);+ } else {+ index_supervisor_test_dump("heartbeat worker log", log_path);+ index_supervisor_test_cleanup_handle(handle);+ }+ index_supervisor_test_restore_env("CBM_LOG_LEVEL", saved_log_level);+ index_supervisor_test_restore_env("CBM_CACHE_DIR", saved_cache);+ (void)th_rmtree(cache);++ ASSERT_EQ(start_rc, 0);+ ASSERT_TRUE(ready);+ ASSERT_TRUE(heartbeat);+ ASSERT_TRUE(clean);PASS();}@@@ -849,7 -893,7 +925,24 @@@ TEST(index_supervisor_killed_worker_logPASS();}++TEST(index_supervisor_job_memory_limit_has_floor_headroom_and_no_overflow) {++ const size_t mib = (size_t)1024U * 1024U;++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(0) == 0);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(4096) == 0);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(8192) == 0);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(512U * mib - 1) == 0);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(512U * mib) == 768U * mib);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(512U * mib + 1) == 768U * mib + 1);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(1024U * mib) == 1536U * mib);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(SIZE_MAX / 2) ==++ SIZE_MAX / 2 + (SIZE_MAX / 2) / 2);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(SIZE_MAX - 1) == SIZE_MAX);++ ASSERT_TRUE(cbm_index_worker_job_memory_limit(SIZE_MAX) == SIZE_MAX);++ PASS();++}++SUITE(index_supervisor) {++ RUN_TEST(index_supervisor_job_memory_limit_has_floor_headroom_and_no_overflow);RUN_TEST(index_supervisor_worker_argv_requires_exact_build_bound_grammar);RUN_TEST(index_supervisor_async_jobs_are_isolated_cancellable_and_terminal_cached);RUN_TEST(index_supervisor_sync_wrapper_forwards_cancel_and_drains_tree);diff --cc tests/test_main.cindex d4e7f5899,72d45be10..42848f991--- a/tests/test_main.c+++ b/tests/test_main.c@@@ -51,29 -51,6 +52,29 @@@ int tf_skip_count = 0* unwind after either production containment or the test's verified backstop. */#define TF_BLOCKING_GIT_MARKER_ENV "CBM_TEST_RUNTIME_BLOCKING_GIT_PID_FILE"+/* Native child for subprocess_windows_job_object_enforces_memory_limit. The+ * fixed-size commit keeps the RED path bounded: without a Job memory limit it+ * succeeds and exits 0; with the limit it is denied and exits with the sentinel+ * code expected by the parent test. */+static int tf_maybe_run_windows_memory_limit_probe(int argc, char **argv) {+#ifdef _WIN32+ if (argc == 2 && argv && strcmp(argv[1], "__cbm_windows_memory_limit_probe") == 0) {- const SIZE_T allocation_size = (SIZE_T)512U * 1024U * 1024U;++ const SIZE_T allocation_size = (SIZE_T)2U * 1024U * 1024U * 1024U;+ void *allocation =+ VirtualAlloc(NULL, allocation_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);+ if (!allocation) {+ return 73;+ }+ (void)VirtualFree(allocation, 0, MEM_RELEASE);+ return 0;+ }+#else+ (void)argc;+ (void)argv;+#endif+ return -1;+}+#ifdef _WIN32static bool tf_invoked_as_windows_git_module(void) {/* CreateProcessW authenticates the executable with lpApplicationName, butdiff --cc tests/test_subprocess.cindex 43d06daa8,1554821c6..79d456376--- a/tests/test_subprocess.c+++ b/tests/test_subprocess.c@@@ -25,6 -25,6 +25,7 @@@#include <unistd.h>#else#include <windows.h>++#include "../src/foundation/win_utf8.h"#endif/* ── Layer 1: pure classifier (all platforms) ─────────────────────────────── */@@@ -744,30 -744,6 +745,35 @@@ TEST(subprocess_windows_job_object_canc#endif}+TEST(subprocess_windows_job_object_enforces_memory_limit) {+#ifndef _WIN32+ SKIP_PLATFORM("native Windows Job Object memory-limit probe");+#else- char self_path[32768];- DWORD self_length = GetModuleFileNameA(NULL, self_path, (DWORD)sizeof(self_path));- ASSERT_TRUE(self_length > 0 && self_length < sizeof(self_path));++ char *self_path = cbm_module_path_utf8();++ ASSERT_TRUE(self_path != NULL);+ const char *argv[] = {self_path, "__cbm_windows_memory_limit_probe", NULL};+ cbm_proc_opts_t opts = {0};+ opts.bin = self_path;+ opts.argv = argv;- opts.memory_limit_bytes = (size_t)512U * 1024U * 1024U;++ opts.memory_limit_bytes = (size_t)1024U * 1024U * 1024U;+ opts.quiet_timeout_ms = 5000;++ cbm_proc_result_t result = {0};- ASSERT_EQ(cbm_subprocess_run(&opts, &result), 0);++ int run_rc = cbm_subprocess_run(&opts, &result);++ free(self_path);++ ASSERT_EQ(run_rc, 0);+ ASSERT_EQ(result.outcome, CBM_PROC_EXIT_NONZERO);+ ASSERT_EQ(result.exit_code, 73);+ ASSERT_TRUE(result.tree_quiesced);+ ASSERT_FALSE(result.supervision_failed);++ ASSERT_TRUE(result.job_memory_available);++ ASSERT_TRUE(result.job_memory_limit_bytes == opts.memory_limit_bytes);++ ASSERT_TRUE(result.peak_job_memory_bytes > 0);++ ASSERT_TRUE(result.peak_job_memory_bytes <= result.job_memory_limit_bytes);+ PASS();+#endif+}+TEST(subprocess_cancel_grace_is_hard_capped) {#ifdef _WIN32SKIP_PLATFORM("POSIX process-group grace cap probe; native Windows coverage pending");