When conversation history exceeds max_history, the blind slice in trim_messages could cut between an assistant(tool_calls) message and its tool result messages. DeepSeek/OpenAI requires every role:"tool" message to follow a preceding assistant message with matching tool_calls. The fix detects when the slice boundary lands on a tool message, then scans backwards to include the parent assistant(tool_calls) message(s). If the parent cannot be found (e.g. cut at a user boundary), the orphaned tool messages are removed. This fixes the "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'" error from DeepSeek. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
23 KiB
23 KiB