Send Harness `WarnNearLimits` warnings as request-scoped system prompts
Harness WarnNearLimits now enqueues each warning as a request-scoped system prompt, ctx.enqueue(SystemPromptPart(text, scope='request')) from before_model_request, the pattern #9711 documents in Changing what the model sees. It's the same move #9712 makes for SystemReminders.
Before, past a threshold, it appended a request-only UserPromptPart to a copy of the message list, so the warning reached the model but never the history, and the next request didn't send it. Once a run was near a limit, every request rewrote the end of the previous one:
- With message caching (
anthropic_cache_messages), the breakpoint landed on the warning block, which the next request no longer had, so nothing a request cached was ever read back. - On Claude Opus 5.5, Sonnet 5.5 and Fable 5.1, dropping a block an earlier request sent is a history edit, and it invalidates every earlier thinking block.
Coder turns WarnNearLimits on by default (max_context_fraction=0.9), so long Coder runs hit this exactly when they're largest.
As a request-scoped prompt, the warning stays in history. On Opus 5.5 it's sent as a clear_at system message, every request sends every warning unchanged, and the wire is append-only. On models that can't clear it, only the current warning is sent, as <system> text at the end of the request, and the core puts the breakpoint before it, so the next request reads what this one cached. The warning is framework voice, so a system prompt is the right channel (audit item P7). It no longer needs the [WarnNearLimits] bracket heading: the system role, or the core <system> tag on fallback models, already frames it.
[!WARNING]
Compatibility impact (Harness 0.x): three visible changes.
- Warnings now appear in
result.all_messages(), as SystemPromptPart(scope='request') in a ModelRequest of their own. Code that asserted they were absent, or counted messages, sees one more request per warning.
- The model sees the warning as a system prompt (natively, or wrapped in
<system> on models without a mid-conversation system role), not a user turn, and the text no longer starts with a [WarnNearLimits] line. Prompts or evals that looked for that heading need to look for Configured run limits are approaching.
- With several
WarnNearLimits instances, each one's warning now reaches the model. Before, each instance stripped every earlier warning, so only the last one's survived.
Why this can ship: the Harness is on 0.x, where the version policy allows behavior changes in minor releases with release-note guidance. Nothing in the API changes.
Migration: none for most users. To find warnings in a history, look for SystemPromptParts with scope == 'request', not for the [WarnNearLimits] text.
Why not #9752
#9752 made the same move on the pre-merge #9711 branch (scope='turn'), with two writes to ctx.messages and request_context.messages and its own text match to find its warnings. It was closed when #9711 merged with scope='request' and ctx.enqueue from before_model_request as the documented route. This PR uses that route, so the capability doesn't track or find its own warnings at all. The core decides what each provider is sent. Warnings that older versions left in saved histories are still recognized only by the TextContent.metadata from #10391, never by their text.
Changes
before_model_requestenqueues the warning, without the[WarnNearLimits]heading line. The legacy metadata strip is unchanged.- No warning when the request ends in a suspended
ModelResponse(a provider resume), like #9712: the core holds anything enqueued then for the next request, which gets a warning of its own, so warning here would send two. - Docstring,
docs/harness/compaction.md, and the Harness skill'sCONTEXT-MANAGEMENT.mddescribe the new behavior. The old "models pay more attention to user messages" reasoning is gone.
Tests
test_warnings_leave_the_cached_prefix_unchanged(Opus 5.5 and Sonnet 4.5, mocked Anthropic client,anthropic_cache_messages, a four-request tool loop): each request starts with everything the previous one cached, byte for byte. On Opus 5.5 the whole message list is append-only and the last request carries every warning as aclear_atsystem message. On Sonnet 4.5 only the current warning is sent, as<system>text after the breakpoint. Both cases fail onmain, where the cached block of request N is a warning that request N+1 no longer has.- Unit tests check the enqueued request-scoped part instead of an appended user prompt. A suspended-response tail gets no warning. Legacy-warning strip tests now build the old shape directly.
test_context_budget.pyfakes now recordenqueue. The fraction and fallback tests end their history with aModelRequest, as a real request does.
Follow-up (not in this PR)
SummarizingCompaction._format_messages renders every SystemPromptPart into the summary transcript whatever its scope, so a summary written near a limit can quote an old "CRITICAL: 2 remaining" warning. That's true for any request-scoped prompt, including #9712's reminders, so it belongs with the summarizing-compaction work, not here.
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