isContextOverflow function
Whether message represents a context overflow.
Handles three cases, mirroring pi's isContextOverflow:
- Error-based overflow:
messagefailed with an error message matching a known overflow pattern (and no known non-overflow pattern). - Silent overflow (z.ai style): the request succeeded but the
reported input usage exceeds
contextWindow. RequirescontextWindow. - Length-stop overflow (Xiaomi MiMo style): the server truncated
oversized input to fit the context window, returning
StopReason.length with zero output and input filling the window
(>= 99% of
contextWindow). RequirescontextWindow.
Detection is reliable for providers that return a detectable error
(Anthropic, OpenAI, Google, xAI, Groq, Cerebras, Mistral, OpenRouter,
Together AI, llama.cpp, LM Studio, Kimi For Coding, DS4). z.ai and Xiaomi
MiMo need contextWindow for their silent forms; Ollama may truncate
silently in ways that cannot be detected here.
Implementation
bool isContextOverflow(AssistantMessage message, {int? contextWindow}) {
// Case 1: error message patterns.
if (_isErrorOverflow(message)) return true;
if (contextWindow == null) return false;
// Case 2: silent overflow (z.ai style) — successful but usage exceeds
// the context window.
if (_isSilentOverflow(message, contextWindow)) return true;
// Case 3: length-stop overflow (Xiaomi MiMo style) — the server truncates
// oversized input to fit the context window, leaving no room for output.
return _isLengthStopOverflow(message, contextWindow);
}