diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index d13f51c436..b30a1fa89a 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -4681,7 +4681,11 @@ async fn maybe_run_previous_model_inline_compact( sess, &previous_turn_context, AutoCompactCallsite::PreTurnExcludingIncomingUserMessage, - TurnContextReinjection::Skip, + // Even though incoming turn items are excluded here, this pass can be the only + // compaction run before submission. Reinject canonical context so unchanged + // model-visible instructions remain present if no follow-up pre-turn compaction + // is needed. + TurnContextReinjection::ReinjectAboveLastRealUser, None, ) .await?; diff --git a/codex-rs/core/tests/suite/compact.rs b/codex-rs/core/tests/suite/compact.rs index d504f9ff55..5a6ad5b426 100644 --- a/codex-rs/core/tests/suite/compact.rs +++ b/codex-rs/core/tests/suite/compact.rs @@ -132,11 +132,20 @@ fn assert_pre_sampling_switch_compaction_requests( !compact_body.contains(""), "pre-sampling compact request should strip trailing model-switch update item" ); + let first_body = first.to_string(); + assert!( + body_contains_text(&first_body, ""), + "first request should include canonical environment context" + ); let follow_up_body = follow_up.to_string(); assert!( follow_up_body.contains(""), "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 preserve canonical environment context after pre-sampling compaction" + ); } async fn assert_compaction_uses_turn_lifecycle_id(codex: &std::sync::Arc) {