Skip to main content

sanitize_tool_sequence

Function sanitize_tool_sequence 

Source
pub fn sanitize_tool_sequence(messages: &mut Vec<Message>)
Expand description

Sanitize tool_call → tool_result ordering in a message sequence.

This runs provider-agnostically before messages are sent to any LLM provider, ensuring that:

  1. Every tool_result has a matching tool_call earlier in the sequence.
  2. No non-tool messages (system hints, summaries) appear between an assistant’s tool_call message and its corresponding user/tool tool_result message.
  3. Orphaned tool_results (no matching tool_call) are removed.

This fixes issues caused by compression moving pinned tool_results out of order, system routing hints persisting between call/result, and summary injection.