diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 6639a66db6..11d58ace3a 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -3759,6 +3759,7 @@ pub(crate) async fn run_turn( &turn_context, auto_compact_limit, needs_follow_up, + true, "run_turn", ) .await; @@ -3836,6 +3837,7 @@ async fn log_post_sampling_token_usage_and_maybe_compact( turn_context: &Arc, auto_compact_limit: i64, needs_follow_up: bool, + allow_auto_compact: bool, checkpoint: &'static str, ) -> bool { let total_usage_tokens = sess.get_total_token_usage().await; @@ -3850,11 +3852,12 @@ async fn log_post_sampling_token_usage_and_maybe_compact( auto_compact_limit, token_limit_reached, needs_follow_up, + allow_auto_compact, checkpoint, "post sampling token usage" ); - if token_limit_reached && needs_follow_up { + if allow_auto_compact && token_limit_reached && needs_follow_up { run_auto_compact(sess, turn_context).await; } @@ -4500,6 +4503,7 @@ async fn drain_in_flight( &turn_context, auto_compact_limit, true, + in_flight.is_empty(), "drain_in_flight", ) .await; @@ -4653,6 +4657,7 @@ async fn try_run_sampling_request( &turn_context, auto_compact_limit, needs_follow_up, + in_flight.is_empty(), "stream_item_done_plan", ) .await; @@ -4682,6 +4687,7 @@ async fn try_run_sampling_request( &turn_context, auto_compact_limit, needs_follow_up, + in_flight.is_empty(), "stream_item_done", ) .await; diff --git a/codex-rs/core/tests/suite/compact_remote.rs b/codex-rs/core/tests/suite/compact_remote.rs index a06e46e75b..2f34290059 100644 --- a/codex-rs/core/tests/suite/compact_remote.rs +++ b/codex-rs/core/tests/suite/compact_remote.rs @@ -239,7 +239,7 @@ async fn remote_compact_runs_automatically() -> Result<()> { #[cfg_attr(target_os = "windows", ignore)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn remote_auto_compact_runs_during_multi_tool_output_drain() -> Result<()> { +async fn remote_auto_compact_runs_after_multi_tool_output_drain() -> Result<()> { skip_if_no_network!(Ok(())); let first_call_id = "first-large-call"; @@ -257,7 +257,7 @@ async fn remote_auto_compact_runs_during_multi_tool_output_drain() -> Result<()> .await?; let codex = harness.test().codex.clone(); - let responses_mock = responses::mount_sse_sequence( + responses::mount_sse_sequence( harness.server(), vec![ sse(vec![ @@ -297,16 +297,9 @@ async fn remote_auto_compact_runs_during_multi_tool_output_drain() -> Result<()> ); assert!( compact_request - .function_call_output_text(second_call_id) - .is_none(), - "expected compaction request before the second drained tool output" - ); - - assert!( - responses_mock .function_call_output_text(second_call_id) .is_some(), - "expected second tool output to be sent in follow-up sampling request" + "expected compaction request to include the second drained tool output" ); Ok(())