04336308f5
The stage completion cap (e.g. 24_576) is a ceiling, not a promise. When the rendered prompt + tool schemas fill most of the model window, sending the raw cap makes llama.cpp truncate the prompt from the left — the traced Gemma4 32_767-token blow-up. Compute effectiveMaxTokens = min(cap, contextSize - promptTokens - toolSchemas - templateOverhead - reserve), counting prompt/tool tokens with the model's own tokenizer (char/4 fallback). Live-path only; deterministic replay never calls infer, so no event recording needed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>