# Implementation Plan: MiniMax History Fix

## Phase 1: Implementation
- [x] Task: Implement History Repair
    - [x] Create `_repair_minimax_history(history: list[dict[str, Any]])` in `src/ai_client.py`.
    - [x] Logic: Check if the last message is `assistant` with `tool_calls`. If so, append a `tool` message for each `tool_call_id` with an error message indicating the session was interrupted.
- [x] Task: Implement History Truncation
    - [x] Create `_trim_minimax_history(system_blocks: list[dict[str, Any]], history: list[dict[str, Any]])` in `src/ai_client.py` (adapt from `_trim_anthropic_history`).
    - [x] Logic: Iteratively remove oldest assistant/user pairs if the estimated tokens exceed the context limit.
- [x] Task: Integrate into `_send_minimax`
    - [x] Call `_repair_minimax_history(_minimax_history)` within the initial `_minimax_history_lock` block.
    - [x] Call `_trim_minimax_history` at the start of the `for round_idx` loop.
    - [x] Log dropped messages to `_append_comms`.

## Phase 2: Verification
- [x] Task: Verification
    - [x] Verify that switching to MiniMax and executing a tool call, then forcibly stopping (e.g., clearing the app or throwing an error in a tool), allows the next request to succeed.
- [x] Task: Conductor - User Manual Verification 'Phase 2: Verification' (Protocol in workflow.md)