Send `WarnNearLimits` warnings as turn-scoped system prompts

Closed
AW
2mo ago
+259-22

[!IMPORTANT]
Stacked on #9711 (base branch claude/turn-scoped-parts), like #9712. Review that first; the diff here is two commits on top of it.

WarnNearLimits is the other Harness capability with a per-request nudge, and it has the same problem #9712 fixes for SystemReminders. Past a threshold, before_model_request strips every earlier warning and appends a fresh UserPromptPart to the request. On this branch that list is request-only, so the warning never reaches history: request N ends with a warning that request N+1 no longer has. On Opus 5.5, Sonnet 5.5 and Fable 5.1 that deletion is a history edit that invalidates every earlier thinking block, and on any provider it moves the cached prefix on every request once the run is near a limit. Coder turns WarnNearLimits on by default (max_context_fraction=0.9), so long Coder runs hit this exactly when they are largest. The repro is in #9275 (https://github.com/pydantic/pydantic-ai/issues/9275#issuecomment-5973194740).

This sends each warning the way #9712 sends a reminder: ModelRequest([SystemPromptPart(text, scope='turn')]), appended to ctx.messages and to a new request_context.messages list.

  • Earlier turn-scoped warnings stay in the request. Core leaves them out or the provider clears them, so deleting one would be the same history edit.
  • Warnings that older versions kept in history as user prompts are still stripped, as before.
  • The context-window estimate leaves out every warning, since the model doesn't see earlier ones again.
  • Nothing is added after a suspended ModelResponse (a provider resume), and that history is left exactly as it was. A complete response at the tail still gets a warning, as before.
  • The warning is now in system voice. The old docstring said user voice was chosen because models attend to it more; #9711 only supports system voice for turn-scoped parts (open question 2 there), and the warning's text is ours, not model-written, so there's nothing to fence.

Tests: a FunctionModel run where each warning reaches its own request only and all of them stay in history as turn-scoped prompts; an earlier warning kept in the request while a new one is appended after it; legacy user-voice warnings still stripped; the estimate leaving earlier warnings out; the suspended and complete-response tails. The existing assertions on the warning's part type now expect a turn-scoped SystemPromptPart. Each behavior was checked by reverting it and seeing a test fail.

Keeping warnings in history has one knock-on effect, which applies to #9712's reminders too: SummarizingCompaction rendered every SystemPromptPart into the summary request regardless of scope, so a summary written near the limit could pick up stale "CRITICAL: 2 remaining" text and carry it forward as lasting context. The second commit leaves turn-scoped prompts out of _format_messages, and out of _extract_system_prompts when an earlier cut has left one at the front of history. Tests cover both helpers and an Agent run with WarnNearLimits and SummarizingCompaction where the summarizer never sees a warning; both fail without the change.

Related: #9275, #9408, #9711, #9712

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.