66#include "daemon/bootstrap.h"
77#include "foundation/compat.h"
88#include "foundation/compat_fs.h"
9+ #include "foundation/log.h"
910#include "foundation/platform.h"
1011#include "foundation/profile.h"
1112#include "mcp/index_supervisor.h"
@@ -560,7 +561,8 @@ static bool index_supervisor_test_run_probe(const char *mode, bool profiling,
560561 (void )snprintf (args , sizeof (args ), "{\"__cbm_test_worker\":\"%s\"}" , mode );
561562 cbm_profile_active = profiling ;
562563 cbm_index_worker_handle_t * handle = NULL ;
563- if (cbm_index_worker_start (args , 0 , false, NULL , NULL , & handle ) != 0 || !handle ) {
564+ const size_t budget = (size_t )1024U * 1024U * 1024U ;
565+ if (cbm_index_worker_start (args , budget , false, NULL , NULL , & handle ) != 0 || !handle ) {
564566 return false;
565567 }
566568 char log_path [INDEX_SUPERVISOR_TEST_PATH_CAP ];
@@ -588,6 +590,17 @@ static bool index_supervisor_test_run_probe(const char *mode, bool profiling,
588590 return terminal ;
589591}
590592
593+ static char g_index_supervisor_memory_log [1024 ];
594+ static int g_index_supervisor_memory_log_count ;
595+
596+ static void index_supervisor_test_memory_log_sink (const char * line ) {
597+ if (strstr (line , "index.supervisor.worker_memory" )) {
598+ g_index_supervisor_memory_log_count ++ ;
599+ (void )snprintf (g_index_supervisor_memory_log , sizeof (g_index_supervisor_memory_log ), "%s" ,
600+ line );
601+ }
602+ }
603+
591604TEST (index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling ) {
592605 char cache [INDEX_SUPERVISOR_TEST_PATH_CAP ];
593606 (void )snprintf (cache , sizeof (cache ), "%s/cbm-index-logs-XXXXXX" , cbm_tmpdir ());
@@ -597,6 +610,14 @@ TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
597610 bool saved_profile = cbm_profile_active ;
598611 (void )cbm_setenv ("CBM_CACHE_DIR" , cache , 1 );
599612
613+ CBMLogLevel saved_level = cbm_log_get_level ();
614+ CBMLogFormat saved_format = cbm_log_get_format ();
615+ cbm_log_set_level (CBM_LOG_INFO );
616+ cbm_log_set_format (CBM_LOG_FORMAT_TEXT );
617+ g_index_supervisor_memory_log [0 ] = '\0' ;
618+ g_index_supervisor_memory_log_count = 0 ;
619+ cbm_log_set_sink_ex (index_supervisor_test_memory_log_sink , CBM_LOG_SINK_TEE );
620+
600621 cbm_proc_outcome_t clean_outcome = CBM_PROC_SPAWN_FAILED ;
601622 cbm_proc_outcome_t profile_outcome = CBM_PROC_SPAWN_FAILED ;
602623 cbm_proc_outcome_t crash_outcome = CBM_PROC_SPAWN_FAILED ;
@@ -621,6 +642,9 @@ TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
621642 bool crash_classified_failure = crash_outcome == CBM_PROC_CRASH ;
622643#endif
623644
645+ cbm_log_set_sink (NULL );
646+ cbm_log_set_level (saved_level );
647+ cbm_log_set_format (saved_format );
624648 cbm_profile_active = saved_profile ;
625649 index_supervisor_test_restore_env ("CBM_CACHE_DIR" , saved_cache );
626650 (void )th_rmtree (cache );
@@ -640,6 +664,14 @@ TEST(index_supervisor_terminal_log_lifecycle_matches_outcome_and_profiling) {
640664 ASSERT_FALSE (crash_response );
641665 ASSERT_TRUE (crash_log );
642666 ASSERT_FALSE (crash_response_file );
667+ #ifdef _WIN32
668+ ASSERT_EQ (g_index_supervisor_memory_log_count , 1 ); /* crash only, never clean */
669+ ASSERT_TRUE (strstr (g_index_supervisor_memory_log , "job_limit_bytes=1610612736" ) != NULL );
670+ ASSERT_TRUE (strstr (g_index_supervisor_memory_log , "peak_job_memory_bytes=" ) != NULL );
671+ ASSERT_TRUE (strstr (g_index_supervisor_memory_log , "unavailable" ) == NULL );
672+ #else
673+ ASSERT_EQ (g_index_supervisor_memory_log_count , 0 );
674+ #endif
643675 PASS ();
644676}
645677
@@ -893,7 +925,24 @@ TEST(index_supervisor_killed_worker_log_is_never_empty_and_names_the_run) {
893925 PASS ();
894926}
895927
928+ TEST (index_supervisor_job_memory_limit_has_floor_headroom_and_no_overflow ) {
929+ const size_t mib = (size_t )1024U * 1024U ;
930+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (0 ) == 0 );
931+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (4096 ) == 0 );
932+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (8192 ) == 0 );
933+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (512U * mib - 1 ) == 0 );
934+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (512U * mib ) == 768U * mib );
935+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (512U * mib + 1 ) == 768U * mib + 1 );
936+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (1024U * mib ) == 1536U * mib );
937+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (SIZE_MAX / 2 ) ==
938+ SIZE_MAX / 2 + (SIZE_MAX / 2 ) / 2 );
939+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (SIZE_MAX - 1 ) == SIZE_MAX );
940+ ASSERT_TRUE (cbm_index_worker_job_memory_limit (SIZE_MAX ) == SIZE_MAX );
941+ PASS ();
942+ }
943+
896944SUITE (index_supervisor ) {
945+ RUN_TEST (index_supervisor_job_memory_limit_has_floor_headroom_and_no_overflow );
897946 RUN_TEST (index_supervisor_worker_argv_requires_exact_build_bound_grammar );
898947 RUN_TEST (index_supervisor_async_jobs_are_isolated_cancellable_and_terminal_cached );
899948 RUN_TEST (index_supervisor_sync_wrapper_forwards_cancel_and_drains_tree );
0 commit comments