Stop `WarnNearLimits` from deleting user prompts that quote its `[WarnNearLimits]` marker

Merged
DO
2mo ago
+96-51

WarnNearLimits decided which parts were its own earlier warnings by looking for [WarnNearLimits] (or the pre-rename [LimitWarner]) in the text of any UserPromptPart or SystemPromptPart, then dropped them before every request. User text that quotes the marker matched as well. A prompt like Why do my logs show [WarnNearLimits] lines? was removed from every request, and when it was the only prompt the run failed outright with UserError: Processed history cannot be empty.

This marks each warning the way compaction receipts and Memory snapshots already mark their parts: the warning text goes in a TextContent with metadata='pydantic-ai-harness.warn-near-limits.v1'. Only parts with that metadata are stripped, and text is never checked. The model still sees the same text, including the [WarnNearLimits] heading.

One trade-off: a warning saved in history by an old version is plain text with no metadata, so it is no longer stripped and stays as a stale user message. On current main warnings are request-only and never reach history, so such a warning can only come from an old saved history. Recognizing those by text is exactly the bug, so I left that path out. If you'd rather keep it, it should match the whole warning shape (a request holding only that one part, in the exact format), not a substring.

How this composes with #9752

#9752 (aweis89, stacked on #9711) fixes the other half: it sends each warning as a SystemPromptPart(scope='turn') that stays in history, so the request tail no longer loses an earlier warning (the missing leading CachePoint / history-edit problem). This PR doesn't touch that. The two overlap in _warn_near_limits.py:

  • Emission: #9752 replaces the UserPromptPart append with the turn-scoped SystemPromptPart and wins that hunk. The TextContent wrapping here only applies to the user-voice warning, so it goes away there.
  • Legacy strip: after #9752, _strip_old_warnings only removes user-voice warnings left in history by older versions. With this PR that check is _is_warning_part (metadata) instead of _is_marker_part (text), so #9752's legacy strip can no longer delete user text either.
  • Turn-scoped warnings: #9752's _is_turn_scoped_marker_part still finds its own warnings by _MARKER in part.content, both to keep them in the request and to leave them out of the context estimate. Neither of those deletes anything, but a turn-scoped system prompt the app wrote itself that quotes the marker would be left out of the estimate. SystemPromptPart has no metadata slot, and application ModelRequest.metadata is dropped when consecutive requests are merged, so there is nothing besides text to key on yet. Worth deciding on #9711/#9752 whether turn-scoped parts need a provenance field.

Whichever PR merges second needs a small conflict resolution in _is_marker_part / _is_warning_part and the emission line. No change in behavior is needed on either side.

Tests

  • test_user_prompt_quoting_the_marker_survives_a_run: an Agent run with a FunctionModel and a tool call. The prompt quoting the marker reaches both requests, the second request also gets the warning after it, and history keeps the prompt. On main this fails with Processed history cannot be empty.

  • test_keeps_text_that_only_looks_like_a_warning: a user prompt quoting both markers, a system prompt starting with the marker, and a user prompt copying a full warning are all left alone. This fails on main.

  • The existing strip tests now build their "earlier warning" by running the capability, not by writing out its text by hand, and assertions read the text through TextContent. The test that only covered the text-matched SystemPromptPart branch is removed along with that branch.

  • Related: #9752, #9711

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.

šŸ¤– Generated with Claude Code