Summarize harness-added user-channel content as `<harness_data>` instead of user messages in `SummarizingCompaction`
SummarizingCompaction rendered every UserPromptPart as <message role="user"> in the summarizer transcript, and keep_user_messages kept every one of them as a user turn. Harness capabilities also put their own content on the user channel and mark it with an origin in TextContent.metadata, for example a BackgroundTools result (pydantic-ai-harness.background_tools.result.v1). That content was summarized as something the user said and could be carried forward as the user's intent, even though it is tool or reviewer output.
This PR treats a user part whose first item is a TextContent with a metadata['origin'] starting with pydantic-ai-harness. as labeled data, for the whole part. That matches the BackgroundTools history contract: a result's tagged text comes first, followed by any files and extra content.
- It is rendered as
<harness_data origin="...">ā¦</harness_data>, neverrole="user". The body is escaped like every other transcript element:harness_datais added to the transcript's own neutralized tags. It is capped attool_return_max_chars, like tool output. - It is excluded from what compaction carries over verbatim from the summarized prefix (the recent unsummarized suffix is kept as is):
keep_user_messages, and the defaultpreserve_first_user_message, which now keeps the first request with the user's own words and only those words. A turn an earlier compaction already kept loses its harness-data parts on the next compaction: they are summarized instead, and its genuine user parts stay kept. - When the transcript holds such content, the summary request gets one more line: the content comes from the application, and the summarizer should record it where relevant but not treat it as the user's requests or standing constraints. Summary requests without harness data are byte-identical to before.
A tag that only shows up later in a part, after what the user wrote, doesn't relabel it. Matching the whole pydantic-ai-harness. origin prefix is deliberate and temporary. It's an interim generic rule, so every harness capability that tags its user-channel content is covered without per-capability code, until core provenance (CapabilityPart) lands. The check then moves to that, and a code comment and a TODO say so. The rule is generic: background results (on main) are covered now, and judge feedback (#10545), task output (#10550) and reminders (#9712) are covered as soon as they land, with no per-capability code.
[!WARNING]
Compatibility impact: Summaries of conversations that contain BackgroundTools results (or other harness-tagged user-channel content) may change. That content now reaches the summarizer as <harness_data> instead of as a user message. keep_user_messages=True no longer retains it, including where an earlier compaction already kept it, and summary requests that contain it get one more line.
Why this can ship in a minor release: Pydantic AI Harness is on 0.x, and treating tool output as the user's words was a defect, not documented behavior.
Migration: None needed.
- Follow-up from the H18 work on #10545. No issue.
Tests:
- A real
BackgroundToolsrun's history is compacted withkeep_user_messages=True. The result reaches the summarizer as<harness_data origin="pydantic-ai-harness.background_tools.result.v1">together with the guidance line, and is not retained, while the user's own prompt is still rendered as a user message and kept. - A synthetic
pydantic-ai-harness.origin is labeled and can't close its element; a non-harness origin, and a tag after the user's own text, stay user messages (snapshot). - Harness data is capped by
tool_return_max_chars. - With
keep_user_messages=False,preserve_first_user_messagekeeps the user's first prompt and not harness data, whether the first request is only harness data or mixes it with the prompt. This test fails without the filter. - A summary request without harness data matches the default prompt's rendering exactly. With harness data, it ends with the guidance line.
- Compacting twice from a turn kept before this change: the first compaction summarizes the background result and keeps the real ask, and the second never brings the result back. This test fails without the re-filter.
- A real
BackgroundToolsresult that returns a file and extra content is summarized as one<harness_data>element and is not retained. This test fails with the single-item check.
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.
- Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
š¤ Generated with Claude Code