diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index b68cd44960..6fec45e1dc 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -4670,7 +4670,7 @@ async fn maybe_run_previous_model_inline_compact( sess, // We use previous turn context here because we compact with the previous model &previous_turn_context, - AutoCompactCallsite::PreTurnExcludingIncomingUserMessage, + AutoCompactCallsite::PreSamplingModelSwitch, None, ) .await diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs index f3a1237106..52ee0e436d 100644 --- a/codex-rs/core/src/compact.rs +++ b/codex-rs/core/src/compact.rs @@ -44,6 +44,9 @@ pub(crate) enum AutoCompactCallsite { /// default pre-turn flow and retained for future model-specific strategies. #[allow(dead_code)] PreTurnExcludingIncomingUserMessage, + /// Pre-sampling compaction triggered by model switch to a smaller context window. + /// This compacts prior-turn history only and should reinsert previous-turn canonical context. + PreSamplingModelSwitch, /// Mid-turn compaction between assistant responses in a follow-up loop. MidTurnContinuation, } @@ -284,7 +287,9 @@ async fn run_compact_task_inner( let mut new_history = process_compacted_history(compacted_history); if matches!( auto_compact_callsite, - Some(AutoCompactCallsite::MidTurnContinuation) + Some( + AutoCompactCallsite::MidTurnContinuation | AutoCompactCallsite::PreSamplingModelSwitch + ) ) { let initial_context = sess.build_initial_context(turn_context.as_ref()).await; insert_initial_context_before_last_real_user(&mut new_history, initial_context); diff --git a/codex-rs/core/src/compact_remote.rs b/codex-rs/core/src/compact_remote.rs index 66b896cd78..8695ea7582 100644 --- a/codex-rs/core/src/compact_remote.rs +++ b/codex-rs/core/src/compact_remote.rs @@ -168,7 +168,10 @@ async fn run_remote_compact_task_inner_impl( }) .await?; new_history = process_compacted_history(new_history); - if auto_compact_callsite == AutoCompactCallsite::MidTurnContinuation { + if matches!( + auto_compact_callsite, + AutoCompactCallsite::MidTurnContinuation | AutoCompactCallsite::PreSamplingModelSwitch + ) { let initial_context = sess.build_initial_context(turn_context.as_ref()).await; insert_initial_context_before_last_real_user(&mut new_history, initial_context); } diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs index 6690609a62..49d3252647 100644 --- a/codex-rs/core/tests/suite/compact.rs +++ b/codex-rs/core/tests/suite/compact.rs @@ -142,8 +142,8 @@ fn assert_pre_sampling_switch_compaction_requests( "follow-up request after successful model-switch compaction should include model-switch update item" ); assert!( - !body_contains_text(&follow_up_body, ""), - "follow-up request should not reinsert canonical environment context after pre-sampling compaction" + body_contains_text(&follow_up_body, ""), + "follow-up request should include canonical environment context from previous-turn context reinjection" ); } diff --git a/codex-rs/core/tests/suite/snapshots/all__suite__compact__pre_sampling_model_switch_compaction_shapes.snap b/codex-rs/core/tests/suite/snapshots/all__suite__compact__pre_sampling_model_switch_compaction_shapes.snap index da2fc5ce71..16ca877075 100644 --- a/codex-rs/core/tests/suite/snapshots/all__suite__compact__pre_sampling_model_switch_compaction_shapes.snap +++ b/codex-rs/core/tests/suite/snapshots/all__suite__compact__pre_sampling_model_switch_compaction_shapes.snap @@ -1,6 +1,5 @@ --- source: core/tests/suite/compact.rs -assertion_line: 1460 expression: "format_labeled_requests_snapshot(\"Pre-sampling compaction on model switch to a smaller context window: current behavior compacts using prior-turn history only (incoming user message excluded), and the follow-up request carries compacted history plus the new user message.\",\n&[(\"Initial Request (Previous Model)\", &requests[0]),\n(\"Pre-sampling Compaction Request\", &requests[1]),\n(\"Post-Compaction Follow-up Request (Next Model)\", &requests[2]),])" --- Scenario: Pre-sampling compaction on model switch to a smaller context window: current behavior compacts using prior-turn history only (incoming user message excluded), and the follow-up request carries compacted history plus the new user message. @@ -22,7 +21,10 @@ Scenario: Pre-sampling compaction on model switch to a smaller context window: c 06:message/user: ## Post-Compaction Follow-up Request (Next Model) -00:message/user:before switch -01:message/user:\nPRE_SAMPLING_SUMMARY -02:message/developer:\nThe user was previously using a different model.... -03:message/user:after switch +00:message/developer: +01:message/user: +02:message/user:> +03:message/user:before switch +04:message/user:\nPRE_SAMPLING_SUMMARY +05:message/developer:\nThe user was previously using a different model.... +06:message/user:after switch