@@ -32,10 +32,14 @@ fn safe_drain_boundary(messages: &[Message], mut boundary: usize) -> usize {
3232 // is in the last drained message (boundary - 1). Pull boundary back by 1.
3333 if messages[ boundary] . role == Role :: User {
3434 if let MessageContent :: Blocks ( blocks) = & messages[ boundary] . content {
35- let has_tool_result = blocks. iter ( ) . any ( |b| matches ! ( b, ContentBlock :: ToolResult { .. } ) ) ;
35+ let has_tool_result = blocks
36+ . iter ( )
37+ . any ( |b| matches ! ( b, ContentBlock :: ToolResult { .. } ) ) ;
3638 if has_tool_result && boundary > 0 && messages[ boundary - 1 ] . role == Role :: Assistant {
3739 if let MessageContent :: Blocks ( asst_blocks) = & messages[ boundary - 1 ] . content {
38- let has_tool_use = asst_blocks. iter ( ) . any ( |b| matches ! ( b, ContentBlock :: ToolUse { .. } ) ) ;
40+ let has_tool_use = asst_blocks
41+ . iter ( )
42+ . any ( |b| matches ! ( b, ContentBlock :: ToolUse { .. } ) ) ;
3943 if has_tool_use {
4044 boundary -= 1 ;
4145 debug ! (
@@ -135,7 +139,8 @@ pub fn recover_from_overflow(
135139 debug ! (
136140 estimated_tokens = estimated,
137141 removing = remove,
138- "Stage 1: moderate trim to last {} messages" , messages. len( ) - remove
142+ "Stage 1: moderate trim to last {} messages" ,
143+ messages. len( ) - remove
139144 ) ;
140145 messages. drain ( ..remove) ;
141146 // Re-check after trim
@@ -156,7 +161,8 @@ pub fn recover_from_overflow(
156161 warn ! (
157162 estimated_tokens = estimate_tokens( messages, system_prompt, tools) ,
158163 removing = remove,
159- "Stage 2: aggressive overflow compaction to last {} messages" , messages. len( ) - remove
164+ "Stage 2: aggressive overflow compaction to last {} messages" ,
165+ messages. len( ) - remove
160166 ) ;
161167 let summary = Message :: user ( format ! (
162168 "[System: {} earlier messages were removed due to context overflow. \
@@ -373,7 +379,10 @@ mod tests {
373379 ] ;
374380 // Boundary 2 would cut between the assistant(ToolUse) at [1] and user(ToolResult) at [2].
375381 let adjusted = safe_drain_boundary ( & msgs, 2 ) ;
376- assert_eq ! ( adjusted, 1 , "Should pull boundary back to keep the ToolUse/ToolResult pair together" ) ;
382+ assert_eq ! (
383+ adjusted, 1 ,
384+ "Should pull boundary back to keep the ToolUse/ToolResult pair together"
385+ ) ;
377386 }
378387
379388 #[ test]
@@ -385,7 +394,10 @@ mod tests {
385394 Message :: assistant( "d" ) ,
386395 ] ;
387396 let adjusted = safe_drain_boundary ( & msgs, 2 ) ;
388- assert_eq ! ( adjusted, 2 , "Should not change boundary for plain text messages" ) ;
397+ assert_eq ! (
398+ adjusted, 2 ,
399+ "Should not change boundary for plain text messages"
400+ ) ;
389401 }
390402
391403 #[ test]
0 commit comments