# Specification: MiniMax API History Sequence Fix

## 1. Overview
The user is experiencing a `MiniMax Bad Request: invalid params, tool call result does not follow tool call (2013)` error. This occurs when the `_minimax_history` in `src/ai_client.py` becomes corrupted, typically when a session is interrupted after the AI makes a tool call but before the `tool` role result messages are appended. The next user message breaks the required `assistant(tool_calls) -> tool` sequence.

## 2. Functional Requirements
*   **History Repair:** Implement a `_repair_minimax_history` function (similar to `_repair_deepseek_history`) to detect dangling tool calls at the end of the history and append synthesized error `tool` responses to close the loop before adding new user messages.
*   **History Truncation:** Implement a `_trim_minimax_history` function to prevent the history array from growing indefinitely and hitting token limits. It should intelligently drop old message pairs (assistant/user) while preserving tool call sequences.
*   **Integration:** Apply these mechanisms within the `_send_minimax` execution flow.

## 3. Non-Functional Requirements
*   **Stability:** The changes must safely handle locking (`_minimax_history_lock`) and not crash if history is empty.
*   **Consistency:** The implementation should align with the established patterns used for the DeepSeek and Anthropic providers.

## 4. Acceptance Criteria
*   The `_send_minimax` function no longer crashes with error code 2013 when resuming a session after an interrupted tool call.
*   The history remains within manageable token limits over long sessions.