diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs index d54b43ff4b..e997d154d6 100644 --- a/codex-rs/core/src/session/turn.rs +++ b/codex-rs/core/src/session/turn.rs @@ -114,7 +114,7 @@ use tracing::trace; use tracing::trace_span; use tracing::warn; -const MAX_AUTO_COMPACTIONS_PER_TURN: usize = 3; +const MAX_CONSECUTIVE_NO_PROGRESS_AUTO_COMPACTIONS: usize = 3; pub(crate) enum RunTurnResult { Continue(Option), @@ -686,27 +686,31 @@ struct AutoCompactTokenStatus { #[derive(Default)] pub(crate) struct AutoCompactTurnLimiter { - attempts: usize, + consecutive_no_progress: usize, } impl AutoCompactTurnLimiter { fn reset_after_user_input(&mut self) { - self.attempts = 0; + self.consecutive_no_progress = 0; } - async fn acquire( + async fn ensure_can_dispatch( &mut self, sess: &Session, turn_context: &TurnContext, reason: CompactionReason, phase: CompactionPhase, ) -> CodexResult<()> { - if self.attempts >= MAX_AUTO_COMPACTIONS_PER_TURN { + if self.consecutive_no_progress >= MAX_CONSECUTIVE_NO_PROGRESS_AUTO_COMPACTIONS { let token_status = auto_compact_token_status(sess, turn_context).await; + if !token_status.token_limit_reached { + self.consecutive_no_progress = 0; + return Ok(()); + } warn!( turn_id = %turn_context.sub_id, - auto_compactions_attempted = self.attempts, - auto_compaction_limit = MAX_AUTO_COMPACTIONS_PER_TURN, + consecutive_no_progress_auto_compactions = self.consecutive_no_progress, + no_progress_auto_compaction_limit = MAX_CONSECUTIVE_NO_PROGRESS_AUTO_COMPACTIONS, reason = ?reason, phase = ?phase, active_context_tokens = token_status.active_context_tokens, @@ -720,7 +724,7 @@ impl AutoCompactTurnLimiter { turn_context, EventMsg::Error(ErrorEvent { message: format!( - "Codex stopped after {MAX_AUTO_COMPACTIONS_PER_TURN} automatic compactions in this turn because the context remained too large. Start a new thread or reduce earlier history before retrying." + "Codex stopped after {MAX_CONSECUTIVE_NO_PROGRESS_AUTO_COMPACTIONS} consecutive automatic compactions because the context remained too large. Start a new thread or reduce earlier history before retrying." ), codex_error_info: Some(CodexErrorInfo::ContextWindowExceeded), }), @@ -729,9 +733,16 @@ impl AutoCompactTurnLimiter { return Err(CodexErr::ContextWindowExceeded); } - self.attempts += 1; Ok(()) } + + fn record_successful_compaction(&mut self, token_status: &AutoCompactTokenStatus) { + if token_status.token_limit_reached { + self.consecutive_no_progress += 1; + } else { + self.consecutive_no_progress = 0; + } + } } async fn auto_compact_token_status( @@ -891,10 +902,10 @@ async fn run_auto_compact( phase: CompactionPhase, ) -> CodexResult { auto_compact_limiter - .acquire(sess.as_ref(), turn_context.as_ref(), reason, phase) + .ensure_can_dispatch(sess.as_ref(), turn_context.as_ref(), reason, phase) .await?; - if should_use_remote_compact_task(turn_context.provider.info()) { + let reset_client_session = if should_use_remote_compact_task(turn_context.provider.info()) { if turn_context.features.enabled(Feature::RemoteCompactionV2) { run_inline_remote_auto_compact_task_v2( Arc::clone(sess), @@ -905,16 +916,18 @@ async fn run_auto_compact( phase, ) .await?; - return Ok(false); + false + } else { + run_inline_remote_auto_compact_task( + Arc::clone(sess), + Arc::clone(turn_context), + initial_context_injection, + reason, + phase, + ) + .await?; + true } - run_inline_remote_auto_compact_task( - Arc::clone(sess), - Arc::clone(turn_context), - initial_context_injection, - reason, - phase, - ) - .await?; } else { run_inline_auto_compact_task( Arc::clone(sess), @@ -924,8 +937,13 @@ async fn run_auto_compact( phase, ) .await?; - } - Ok(true) + true + }; + + let token_status = auto_compact_token_status(sess.as_ref(), turn_context.as_ref()).await; + auto_compact_limiter.record_successful_compaction(&token_status); + + Ok(reset_client_session) } pub(super) fn collect_explicit_app_ids_from_skill_items( diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs index 938046e30b..42ebe60528 100644 --- a/codex-rs/core/tests/suite/compact.rs +++ b/codex-rs/core/tests/suite/compact.rs @@ -1494,7 +1494,7 @@ async fn multiple_auto_compact_per_task_runs_after_token_limit_hit() { // Windows CI only: bump to 4 workers to prevent SSE/event starvation and test timeouts. #[cfg_attr(windows, tokio::test(flavor = "multi_thread", worker_threads = 4))] #[cfg_attr(not(windows), tokio::test(flavor = "multi_thread", worker_threads = 2))] -async fn auto_compact_stops_after_per_turn_limit() { +async fn auto_compact_stops_after_consecutive_no_progress_compactions() { skip_if_no_network!(); let server = start_mock_server().await; @@ -1548,7 +1548,9 @@ async fn auto_compact_stops_after_per_turn_limit() { .with_config(move |config| { config.model_provider = model_provider; set_test_compact_prompt(config); - config.model_auto_compact_token_limit = Some(200_000); + // Force compaction to remain above the configured trigger after each + // successful compaction, independent of the token estimator. + config.model_auto_compact_token_limit = Some(1); }) .build(&server) .await @@ -1573,7 +1575,7 @@ async fn auto_compact_stops_after_per_turn_limit() { EventMsg::Error(err) if err .message - .contains("Codex stopped after 3 automatic compactions in this turn") => + .contains("Codex stopped after 3 consecutive automatic compactions") => { Some(err.clone()) } @@ -1595,7 +1597,7 @@ async fn auto_compact_stops_after_per_turn_limit() { .count(); assert_eq!( compaction_requests, 3, - "fourth automatic compaction should be blocked before dispatch" + "fourth no-progress automatic compaction should be blocked before dispatch" ); assert_eq!( requests.len(), diff --git a/codex-rs/core/tests/suite/pending_input.rs b/codex-rs/core/tests/suite/pending_input.rs index 81979117e3..54553e3c0a 100644 --- a/codex-rs/core/tests/suite/pending_input.rs +++ b/codex-rs/core/tests/suite/pending_input.rs @@ -801,7 +801,7 @@ async fn steered_user_input_resets_auto_compact_limit() { async fn steered_user_input_is_discarded_when_auto_compact_guard_fires() { let (gate_fourth_completed_tx, gate_fourth_completed_rx) = oneshot::channel(); let token_count_used = 270_000; - let token_count_used_after_compaction = 80_000; + let token_count_used_after_compaction = 260_000; let tool_sampling_chunks = |idx: usize| { vec![