From 6e0d002d05cdafaf40390d4a98ae182faf0e9762 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 2 Jun 2026 01:45:07 -0400 Subject: [PATCH] fix(minimax): Prevent dangling tool calls during history truncation - Update _trim_minimax_history to drop dangling 'tool' messages if their parent 'assistant' message is removed. - Fixes 'invalid params, tool call result does not follow tool call (2013)' error when token limit is hit. --- src/ai_client.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ai_client.py b/src/ai_client.py index 2033cf84..f73d1047 100644 --- a/src/ai_client.py +++ b/src/ai_client.py @@ -2131,6 +2131,13 @@ def _trim_minimax_history(system_blocks: list[dict[str, Any]], history: list[dic removed = history.pop(1) dropped += 1 est -= _estimate_message_tokens(removed) + + # Ensure we don't leave dangling 'tool' messages if their parent 'assistant' was dropped. + # MiniMax strictly requires 'tool' messages to immediately follow 'assistant' with tool_calls. + while len(history) > 1 and history[1].get("role") == "tool": + removed_tool = history.pop(1) + dropped += 1 + est -= _estimate_message_tokens(removed_tool) return dropped def _ensure_minimax_client() -> None: