@@ -621,3 +621,92 @@ fn insert_initial_context_before_last_real_user_or_summary_keeps_compaction_last
621621 ] ;
622622 assert_eq ! ( refreshed, expected) ;
623623}
624+
625+ fn function_call_output ( call_id : & str , text : & str ) -> ResponseItem {
626+ ResponseItem :: FunctionCallOutput {
627+ call_id : call_id. to_string ( ) ,
628+ output : codex_protocol:: models:: FunctionCallOutputPayload :: from_text ( text. to_string ( ) ) ,
629+ }
630+ }
631+
632+ fn reasoning_item ( text : & str ) -> ResponseItem {
633+ ResponseItem :: Reasoning {
634+ id : String :: new ( ) ,
635+ summary : vec ! [
636+ codex_protocol:: models:: ReasoningItemReasoningSummary :: SummaryText {
637+ text: "summary" . to_string( ) ,
638+ } ,
639+ ] ,
640+ content : Some ( vec ! [
641+ codex_protocol:: models:: ReasoningItemContent :: ReasoningText {
642+ text: text. to_string( ) ,
643+ } ,
644+ ] ) ,
645+ encrypted_content : None ,
646+ }
647+ }
648+
649+ fn function_output_text ( item : & ResponseItem ) -> Option < String > {
650+ match item {
651+ ResponseItem :: FunctionCallOutput { output, .. } => match & output. body {
652+ codex_protocol:: models:: FunctionCallOutputBody :: Text ( text) => Some ( text. clone ( ) ) ,
653+ _ => None ,
654+ } ,
655+ _ => None ,
656+ }
657+ }
658+
659+ #[ tokio:: test]
660+ async fn trim_function_call_history_truncates_outputs_behind_non_output_items ( ) {
661+ let ( _session, mut turn_context) =
662+ crate :: session:: tests:: make_session_and_context ( ) . await ;
663+ // Force a tiny context window so even modest tool outputs overflow it.
664+ turn_context. model_info . context_window = Some ( 500 ) ;
665+ turn_context. model_info . max_context_window = Some ( 500 ) ;
666+ turn_context. model_info . effective_context_window_percent = 100 ;
667+
668+ let base_instructions = codex_protocol:: models:: BaseInstructions {
669+ text : String :: new ( ) ,
670+ } ;
671+
672+ // A large tool output, then a reasoning item, then another large tool output.
673+ // The reasoning item sits between the two outputs, so the old `break`-on-first
674+ // non-truncatable-item behavior would stop after shrinking only the newest
675+ // output and leave history over the context window.
676+ let mut history = crate :: context_manager:: ContextManager :: new ( ) ;
677+ history. replace ( vec ! [
678+ function_call_output( "call-a" , & "A" . repeat( 4000 ) ) ,
679+ reasoning_item( "thinking" ) ,
680+ function_call_output( "call-b" , & "B" . repeat( 4000 ) ) ,
681+ ] ) ;
682+
683+ let before = history
684+ . estimate_token_count_with_base_instructions ( & base_instructions)
685+ . unwrap ( ) ;
686+ assert ! ( before > 500 , "precondition: history should overflow window" ) ;
687+
688+ let ( rewritten_outputs, deleted_tokens) =
689+ crate :: compact_remote:: trim_function_call_history_to_fit_context_window (
690+ & mut history,
691+ & turn_context,
692+ & base_instructions,
693+ ) ;
694+
695+ // Both outputs must be truncated, not just the newest one before the
696+ // reasoning item.
697+ assert_eq ! ( rewritten_outputs, 2 ) ;
698+ assert ! ( deleted_tokens > 0 ) ;
699+
700+ let items = history. raw_items ( ) ;
701+ let truncated = "Output exceeded the available model context and was truncated" ;
702+ assert_eq ! ( function_output_text( & items[ 0 ] ) . as_deref( ) , Some ( truncated) ) ;
703+ assert_eq ! ( function_output_text( & items[ 2 ] ) . as_deref( ) , Some ( truncated) ) ;
704+
705+ let after = history
706+ . estimate_token_count_with_base_instructions ( & base_instructions)
707+ . unwrap ( ) ;
708+ assert ! (
709+ after <= 500 ,
710+ "history should fit the context window after trimming (got {after})"
711+ ) ;
712+ }
0 commit comments