diff --git a/codex-rs/core/src/unified_exec/process_manager.rs b/codex-rs/core/src/unified_exec/process_manager.rs index fe3ec45759..4730265af5 100644 --- a/codex-rs/core/src/unified_exec/process_manager.rs +++ b/codex-rs/core/src/unified_exec/process_manager.rs @@ -1210,7 +1210,7 @@ impl UnifiedExecProcessManager { ) -> Vec { const POST_EXIT_CLOSE_WAIT_CAP: Duration = Duration::from_millis(50); - let mut collected: Vec = Vec::with_capacity(4096); + let mut collected = HeadTailBuffer::default(); let mut exit_signal_received = cancellation_token.is_cancelled(); let mut post_exit_deadline: Option = None; loop { @@ -1276,7 +1276,7 @@ impl UnifiedExecProcessManager { } for chunk in drained_chunks { - collected.extend_from_slice(&chunk); + collected.push_chunk(chunk); } exit_signal_received |= cancellation_token.is_cancelled(); @@ -1285,7 +1285,7 @@ impl UnifiedExecProcessManager { } } - collected + collected.to_bytes() } async fn extend_deadlines_while_paused( diff --git a/codex-rs/core/src/unified_exec/process_manager_tests.rs b/codex-rs/core/src/unified_exec/process_manager_tests.rs index 20a930b552..a076e50348 100644 --- a/codex-rs/core/src/unified_exec/process_manager_tests.rs +++ b/codex-rs/core/src/unified_exec/process_manager_tests.rs @@ -234,6 +234,55 @@ fn initial_exec_yield_time_has_no_platform_floor() { ); } +#[tokio::test] +async fn output_collection_stays_bounded_across_repeated_drains() { + let output_buffer = Arc::new(tokio::sync::Mutex::new(HeadTailBuffer::default())); + let output_notify = Arc::new(Notify::new()); + let output_closed = Arc::new(AtomicBool::new(false)); + let output_closed_notify = Arc::new(Notify::new()); + let cancellation_token = CancellationToken::new(); + + let collect = UnifiedExecProcessManager::collect_output_until_deadline( + &output_buffer, + &output_notify, + &output_closed, + &output_closed_notify, + &cancellation_token, + /*pause_state*/ None, + Instant::now() + Duration::from_secs(5), + ); + let produce = async { + for byte in [b'a', b'b', b'c'] { + output_buffer.lock().await.push_chunk( + vec![byte; crate::unified_exec::UNIFIED_EXEC_OUTPUT_MAX_BYTES], + ); + output_notify.notify_one(); + tokio::time::timeout(Duration::from_secs(1), async { + loop { + if output_buffer.lock().await.retained_bytes() == 0 { + break; + } + tokio::task::yield_now().await; + } + }) + .await + .expect("collector should drain each chunk"); + } + + output_closed.store(true, Ordering::Release); + cancellation_token.cancel(); + output_closed_notify.notify_waiters(); + output_notify.notify_waiters(); + }; + + let (collected, ()) = tokio::join!(collect, produce); + let head_bytes = crate::unified_exec::UNIFIED_EXEC_OUTPUT_MAX_BYTES / 2; + let tail_bytes = crate::unified_exec::UNIFIED_EXEC_OUTPUT_MAX_BYTES - head_bytes; + let mut expected = vec![b'a'; head_bytes]; + expected.resize(head_bytes + tail_bytes, b'c'); + assert_eq!(collected, expected); +} + #[tokio::test] async fn network_denial_fallback_message_names_sandbox_network_proxy() { let message = network_denial_message_for_session(/*session*/ None, /*deferred*/ None).await;