@@ -5245,12 +5245,157 @@ TEST(daemon_application_default_limit_admits_four_and_rejects_fifth) {
52455245 ASSERT_EQ (atomic_load (& fake .starts ), DEFAULT_CAP_RUNNING );
52465246 ASSERT_EQ (atomic_load (& fake .cancels ), DEFAULT_CAP_RUNNING );
52475247 ASSERT_EQ (atomic_load (& fake .destroys ), DEFAULT_CAP_RUNNING );
5248- size_t assigned_budget = 0 ;
5248+ /* Decision 2 (#1997 #832): each worker's slice is the aggregate divided by
5249+ * the jobs active when IT was spawned. Four requests race through
5250+ * admission, so which divisor each saw is scheduling-dependent — but every
5251+ * slice is one of aggregate/1..4 and never exceeds the aggregate. */
52495252 for (int i = 0 ; i < DEFAULT_CAP_RUNNING ; i ++ ) {
5250- ASSERT_EQ (fake .memory_budgets [i ], aggregate_budget / DEFAULT_CAP_RUNNING );
5251- assigned_budget += fake .memory_budgets [i ];
5253+ bool valid_slice = false;
5254+ for (size_t divisor = 1 ; divisor <= DEFAULT_CAP_RUNNING ; divisor ++ ) {
5255+ valid_slice = valid_slice || fake .memory_budgets [i ] == aggregate_budget / divisor ;
5256+ }
5257+ ASSERT_TRUE (valid_slice );
5258+ ASSERT_TRUE (fake .memory_budgets [i ] <= aggregate_budget );
5259+ }
5260+ PASS ();
5261+ }
5262+
5263+ /* Decision 2 (#1997 #832): the worker slice is aggregate / jobs ACTIVE at
5264+ * spawn time. Sequenced admission makes the divisor deterministic: the first
5265+ * job spawns alone and receives the whole aggregate; the second spawns while
5266+ * the first still runs and receives half. */
5267+ TEST (daemon_application_worker_slice_is_aggregate_over_active_jobs ) {
5268+ const size_t aggregate_budget = 4099 ;
5269+ app_fake_worker_context_t fake ;
5270+ app_fake_worker_context_init (& fake );
5271+ cbm_daemon_application_worker_ops_t worker_ops = {
5272+ .context = & fake ,
5273+ .start = app_fake_worker_start ,
5274+ .poll = app_fake_worker_poll ,
5275+ .cancel = app_fake_worker_cancel ,
5276+ .log_path = app_fake_worker_log_path ,
5277+ .destroy = app_fake_worker_destroy ,
5278+ };
5279+ cbm_daemon_application_config_t config = {
5280+ .worker_ops = & worker_ops ,
5281+ .aggregate_memory_budget_bytes = aggregate_budget ,
5282+ };
5283+ cbm_daemon_application_t * application = cbm_daemon_application_new (& config );
5284+ char roots [2 ][APP_TEST_PATH_CAP ];
5285+ bool roots_ok = true;
5286+ for (int i = 0 ; i < 2 ; i ++ ) {
5287+ (void )snprintf (roots [i ], sizeof (roots [i ]), "%s/cbm-app-slice-%d-XXXXXX" , cbm_tmpdir (), i );
5288+ roots_ok = roots_ok && cbm_mkdtemp (roots [i ]) != NULL ;
5289+ }
5290+ app_index_thread_t requests [2 ] = {
5291+ {.application = application , .project = "slice-first" , .root = roots [0 ], .result = -1 },
5292+ {.application = application , .project = "slice-second" , .root = roots [1 ], .result = -1 },
5293+ };
5294+ cbm_thread_t threads [2 ];
5295+ bool first_started = application && roots_ok &&
5296+ cbm_thread_create (& threads [0 ], 0 , app_index_thread , & requests [0 ]) == 0 ;
5297+ bool first_spawned = first_started && app_wait_for_atomic_int (& fake .starts , 1 );
5298+ bool second_started =
5299+ first_spawned && cbm_thread_create (& threads [1 ], 0 , app_index_thread , & requests [1 ]) == 0 ;
5300+ bool second_spawned = second_started && app_wait_for_atomic_int (& fake .starts , 2 );
5301+ atomic_store (& fake .allow_completion , true);
5302+ if (first_started ) {
5303+ (void )cbm_thread_join (& threads [0 ]);
5304+ }
5305+ if (second_started ) {
5306+ (void )cbm_thread_join (& threads [1 ]);
5307+ }
5308+ bool stopped = application && cbm_daemon_application_shutdown (application , APP_TEST_TIMEOUT_MS );
5309+ cbm_daemon_application_free (application );
5310+ for (int i = 0 ; i < 2 ; i ++ ) {
5311+ (void )cbm_rmdir (roots [i ]);
5312+ }
5313+
5314+ ASSERT_TRUE (roots_ok );
5315+ ASSERT_TRUE (first_spawned );
5316+ ASSERT_TRUE (second_spawned );
5317+ ASSERT_EQ (requests [0 ].result , 0 );
5318+ ASSERT_EQ (requests [1 ].result , 0 );
5319+ ASSERT_TRUE (stopped );
5320+ ASSERT_EQ (fake .memory_budgets [0 ], aggregate_budget );
5321+ ASSERT_EQ (fake .memory_budgets [1 ], aggregate_budget / 2 );
5322+ PASS ();
5323+ }
5324+
5325+ /* Decision A (#1997 #832): the over-budget verdict travels as a CLEAN worker
5326+ * exit carrying an error response. The daemon passes that response through
5327+ * verbatim on the first attempt — no crash/hang recovery loop, no quarantine
5328+ * of innocent files — because a healthy process reported an honest failure. */
5329+ TEST (daemon_application_over_budget_response_passes_through_without_recovery ) {
5330+ static const char over_budget_response [] =
5331+ "{\"content\":[{\"type\":\"text\",\"text\":\"{\\\"project\\\":\\\"budget\\\","
5332+ "\\\"status\\\":\\\"error\\\",\\\"reason\\\":\\\"over_memory_budget\\\","
5333+ "\\\"previous_index\\\":\\\"preserved\\\"}\"}],\"isError\":true}" ;
5334+ app_fake_worker_context_t fake ;
5335+ app_fake_worker_context_init (& fake );
5336+ atomic_store (& fake .scripted , true);
5337+ fake .outcomes [0 ] = CBM_PROC_CLEAN ;
5338+ fake .responses [0 ] = over_budget_response ;
5339+ fake .outcomes [1 ] = CBM_PROC_CLEAN ; /* a second start would be the recovery loop */
5340+ cbm_daemon_application_worker_ops_t worker_ops = {
5341+ .context = & fake ,
5342+ .start = app_fake_worker_start ,
5343+ .poll = app_fake_worker_poll ,
5344+ .cancel = app_fake_worker_cancel ,
5345+ .log_path = app_fake_worker_log_path ,
5346+ .destroy = app_fake_worker_destroy ,
5347+ };
5348+ cbm_daemon_application_config_t config = {.worker_ops = & worker_ops };
5349+ cbm_daemon_application_t * application = cbm_daemon_application_new (& config );
5350+ cbm_daemon_runtime_application_callbacks_t callbacks =
5351+ cbm_daemon_application_runtime_callbacks (application );
5352+ cbm_daemon_runtime_application_session_t * session =
5353+ application ? app_test_open (& callbacks , 71 ) : NULL ;
5354+ char root [APP_TEST_PATH_CAP ];
5355+ snprintf (root , sizeof (root ), "%s/cbm-app-over-budget-XXXXXX" , cbm_tmpdir ());
5356+ bool root_ok = cbm_mkdtemp (root ) != NULL ;
5357+ uint8_t * context = NULL ;
5358+ uint32_t context_length = 0 ;
5359+ char args [APP_TEST_PATH_CAP + 32 ];
5360+ snprintf (args , sizeof (args ), "{\"repo_path\":\"%s\"}" , root );
5361+ uint8_t * tool = NULL ;
5362+ uint32_t tool_length = 0 ;
5363+ bool setup = application && session && root_ok &&
5364+ app_test_context_request (root , root , & context , & context_length ) &&
5365+ app_test_tool_request ("index_repository" , args , & tool , & tool_length );
5366+ uint8_t * response = NULL ;
5367+ uint32_t response_length = 0 ;
5368+ if (setup ) {
5369+ setup = app_test_request (& callbacks , session , context , context_length , & response ,
5370+ & response_length ) == CBM_DAEMON_RUNTIME_APPLICATION_OK ;
5371+ free (response );
5372+ response = NULL ;
5373+ response_length = 0 ;
52525374 }
5253- ASSERT_TRUE (assigned_budget <= aggregate_budget );
5375+ cbm_daemon_runtime_application_status_t status =
5376+ setup
5377+ ? app_test_request (& callbacks , session , tool , tool_length , & response , & response_length )
5378+ : CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR ;
5379+ bool passed_through = response && response_length > 0 &&
5380+ strstr ((char * )response , "over_memory_budget" ) != NULL &&
5381+ strstr ((char * )response , "\"isError\":true" ) != NULL ;
5382+ free (response );
5383+ free (context );
5384+ free (tool );
5385+ if (session ) {
5386+ callbacks .session_cancel (callbacks .context , session );
5387+ callbacks .session_close (callbacks .context , session );
5388+ }
5389+ bool stopped = application && cbm_daemon_application_shutdown (application , APP_TEST_TIMEOUT_MS );
5390+ cbm_daemon_application_free (application );
5391+ (void )cbm_rmdir (root );
5392+
5393+ ASSERT_TRUE (setup );
5394+ ASSERT_EQ (status , CBM_DAEMON_RUNTIME_APPLICATION_OK );
5395+ ASSERT_TRUE (passed_through );
5396+ ASSERT_EQ (atomic_load (& fake .starts ), 1 );
5397+ ASSERT_EQ (atomic_load (& fake .destroys ), 1 );
5398+ ASSERT_TRUE (stopped );
52545399 PASS ();
52555400}
52565401
@@ -5458,6 +5603,8 @@ SUITE(daemon_application) {
54585603 RUN_TEST (daemon_application_thread_start_failure_rolls_back_job_reservation );
54595604 RUN_TEST (daemon_application_queues_explicit_index_behind_physical_job_limit );
54605605 RUN_TEST (daemon_application_default_limit_admits_four_and_rejects_fifth );
5606+ RUN_TEST (daemon_application_worker_slice_is_aggregate_over_active_jobs );
5607+ RUN_TEST (daemon_application_over_budget_response_passes_through_without_recovery );
54615608 RUN_TEST (daemon_application_free_reports_retained_live_ownership );
54625609 RUN_TEST (daemon_application_rejects_clean_exit_when_process_tree_is_not_contained );
54635610}
0 commit comments