@@ -200,15 +200,27 @@ pub(crate) fn build_compacted_history(
200200 user_messages : & [ String ] ,
201201 summary_text : & str ,
202202) -> Vec < ResponseItem > {
203- let mut history = initial_context;
203+ build_compacted_history_with_limit (
204+ initial_context,
205+ user_messages,
206+ summary_text,
207+ COMPACT_USER_MESSAGE_MAX_TOKENS * 4 ,
208+ )
209+ }
210+
211+ fn build_compacted_history_with_limit (
212+ mut history : Vec < ResponseItem > ,
213+ user_messages : & [ String ] ,
214+ summary_text : & str ,
215+ max_bytes : usize ,
216+ ) -> Vec < ResponseItem > {
204217 let mut user_messages_text = if user_messages. is_empty ( ) {
205218 "(none)" . to_string ( )
206219 } else {
207220 user_messages. join ( "\n \n " )
208221 } ;
209222 // Truncate the concatenated prior user messages so the bridge message
210223 // stays well under the context window (approx. 4 bytes/token).
211- let max_bytes = COMPACT_USER_MESSAGE_MAX_TOKENS * 4 ;
212224 if user_messages_text. len ( ) > max_bytes {
213225 user_messages_text = truncate_middle ( & user_messages_text, max_bytes) . 0 ;
214226 }
@@ -361,11 +373,16 @@ mod tests {
361373
362374 #[ test]
363375 fn build_compacted_history_truncates_overlong_user_messages ( ) {
364- // Prepare a very large prior user message so the aggregated
365- // `user_messages_text` exceeds the truncation threshold used by
366- // `build_compacted_history` (80k bytes).
367- let big = "X" . repeat ( 200_000 ) ;
368- let history = build_compacted_history ( Vec :: new ( ) , std:: slice:: from_ref ( & big) , "SUMMARY" ) ;
376+ // Use a small truncation limit so the test remains fast while still validating
377+ // that oversized user content is truncated.
378+ let max_bytes = 128 ;
379+ let big = "X" . repeat ( max_bytes + 50 ) ;
380+ let history = super :: build_compacted_history_with_limit (
381+ Vec :: new ( ) ,
382+ std:: slice:: from_ref ( & big) ,
383+ "SUMMARY" ,
384+ max_bytes,
385+ ) ;
369386
370387 // Expect exactly one bridge message added to history (plus any initial context we provided, which is none).
371388 assert_eq ! ( history. len( ) , 1 ) ;
0 commit comments