975eb3a365
Reference models may have a smaller context window than the aggregator (e.g. kimi-k2.7-code @ 262K advising a glm-5.2 @ 1M conversation). Without context-length protection, a reference whose window is exceeded gets a hard HTTP 400 from the provider, which _run_reference's try/except silently converts to a [failed: …] note — the MoA turn silently degrades to fewer references (#60345). Redesigned implementation of #60387: - Estimate AFTER the advisory system prompt is prepended, so the request that is actually sent is what gets budgeted. - Reserve output headroom: the preset's reference_max_tokens when set, else an 8192-token constant, plus a 10% estimator-error fraction. - Trim on advisory-view boundaries (text-only user/assistant turns; no tool-result frames to orphan), preserving the system prompt, the user-first invariant after every pop (never assistant-first), and the trailing synthetic user turn. - Cache get_model_context_length per (provider, model) in a per-fan-out dict shared across the worker threads, so a turn resolves each window once instead of probing metadata sources per-reference-per-iteration (failures are cached too). Co-authored-by: webtecnica <75556242+webtecnica@users.noreply.github.com>