@@ -2706,14 +2706,38 @@ async fn ordinary_400_stays_terminal_and_triggers_no_recovery() {
27062706/// part of the assertion.
27072707#[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
27082708async fn context_recovery_budget_exhaustion_surfaces_the_error ( ) {
2709+ assert_context_recovery_budget_exhaustion ( false ) . await ;
2710+ }
2711+
2712+ /// The same real provider/ACP scenario with stderr collection held until after
2713+ /// the stdout response. The old immediate snapshot cannot observe the budget.
2714+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
2715+ async fn context_recovery_budget_exhaustion_waits_for_delayed_stderr ( ) {
2716+ assert_context_recovery_budget_exhaustion ( true ) . await ;
2717+ }
2718+
2719+ #[ tokio:: test]
2720+ #[ should_panic( expected = "timed out waiting for stderr diagnostic" ) ]
2721+ async fn stderr_diagnostic_wait_is_bounded_when_absent ( ) {
2722+ let llm = spawn_capturing_llm ( vec ! [ ] ) . await ;
2723+ let h = Harness :: spawn ( & llm. url ) . await ;
2724+ h. wait_for_stderr (
2725+ "diagnostic that is never emitted" ,
2726+ Duration :: from_millis ( 20 ) ,
2727+ )
2728+ . await ;
2729+ }
2730+
2731+ async fn assert_context_recovery_budget_exhaustion ( delay_stderr : bool ) {
2732+ let ( release_stderr, stderr_gate) = tokio:: sync:: oneshot:: channel ( ) ;
27092733 // Enough canned 400s that the queue is never the thing that stops the loop;
27102734 // the fallback response is also a 400-shaped body under this helper only if
27112735 // queued, so keep the queue generously long.
27122736 let responses: Vec < ( u16 , Value ) > = ( 0 ..40 )
27132737 . map ( |_| ( 400 , openai_context_length_error ( ) ) )
27142738 . collect ( ) ;
27152739 let llm = spawn_capturing_llm_with_status ( responses) . await ;
2716- let mut h = Harness :: spawn_with_env (
2740+ let mut h = Harness :: spawn_with_stderr_gate (
27172741 & llm. url ,
27182742 & [
27192743 ( "BUZZ_AGENT_MAX_CONTEXT_TOKENS" , "200000" ) ,
@@ -2723,6 +2747,7 @@ async fn context_recovery_budget_exhaustion_surfaces_the_error() {
27232747 ) ,
27242748 ( "BUZZ_AGENT_MAX_HANDOFFS" , "0" ) ,
27252749 ] ,
2750+ delay_stderr. then_some ( stderr_gate) ,
27262751 )
27272752 . await ;
27282753 let sid = init_session ( & mut h, json ! ( [ ] ) ) . await ;
@@ -2751,8 +2776,27 @@ async fn context_recovery_budget_exhaustion_surfaces_the_error() {
27512776 // floor produce a surfaced error, so the assertion above passes either way
27522777 // — and the floor can fire on the first rung without the budget ever being
27532778 // consumed, which would make this test silently exercise a different
2754- // mechanism than its name claims. Pin the budget explicitly.
2755- let stderr = h. stderr_text ( ) ;
2779+ // mechanism than its name claims. Pin the budget explicitly. Stdout is not
2780+ // a barrier for the independent stderr collector.
2781+ let stderr = {
2782+ let wait = h. wait_for_stderr ( "context recovery budget spent" , Duration :: from_secs ( 5 ) ) ;
2783+ tokio:: pin!( wait) ;
2784+ if delay_stderr {
2785+ assert ! (
2786+ !h. stderr_text( ) . contains( "context recovery budget spent" ) ,
2787+ "the old immediate snapshot must miss the held diagnostic"
2788+ ) ;
2789+ // Prove the actual wait stays pending before releasing the collector,
2790+ // without a sleep or depending on how quickly either task runs.
2791+ std:: future:: poll_fn ( |cx| {
2792+ assert ! ( std:: future:: Future :: poll( wait. as_mut( ) , cx) . is_pending( ) ) ;
2793+ std:: task:: Poll :: Ready ( ( ) )
2794+ } )
2795+ . await ;
2796+ release_stderr. send ( ( ) ) . expect ( "release stderr collection" ) ;
2797+ }
2798+ wait. await
2799+ } ;
27562800 assert ! (
27572801 stderr. contains( "context recovery budget spent" ) ,
27582802 "the per-run recovery BUDGET must be what stops the loop here, not the prompt floor; \
@@ -2767,6 +2811,15 @@ async fn context_recovery_budget_exhaustion_surfaces_the_error() {
27672811 "expected all 3 recovery rungs to be attempted before giving up, saw {rungs} — \
27682812 stderr={stderr}"
27692813 ) ;
2814+ assert ! (
2815+ !stderr. contains( "context recovery would shrink" ) ,
2816+ "the prompt floor must not stop this fixture: {stderr}"
2817+ ) ;
2818+ assert_eq ! (
2819+ llm. captured. lock( ) . await . len( ) ,
2820+ 4 ,
2821+ "expected the rejected completion plus exactly three failed summaries"
2822+ ) ;
27702823 h. shutdown ( ) . await ;
27712824}
27722825
@@ -2816,7 +2869,9 @@ async fn small_history_context_400_refuses_rescue_at_the_prompt_floor() {
28162869 r0. get( "error" ) . is_some( ) ,
28172870 "a context 400 with no shrinkable history must surface the error, got: {r0}"
28182871 ) ;
2819- let stderr = h. stderr_text ( ) ;
2872+ let stderr = h
2873+ . wait_for_stderr ( "context recovery would shrink" , Duration :: from_secs ( 5 ) )
2874+ . await ;
28202875 assert ! (
28212876 stderr. contains( "below the" ) && stderr. contains( "floor" ) ,
28222877 "the prompt-budget FLOOR must be what stops this, not the recovery budget; got: {stderr}"
0 commit comments