[voice-authority] Note text that imitates a core data frame or a capability part's frame on every untrusted channel

Draft
DO
18d ago
+11953-5202

Stacked on #11935.

Text that imitates a frame now gets a lookalike note on every untrusted channel, for every frame tag the model could mistake it for. Before this, only the system-voice tags (<system>, <context>, <context_diff>) were checked, and a capability part's body was checked only against its own tag.

What's checked

  • Tags: the system-voice tags, core's data tags (tool_result, assistant_thinking, conversation_summary, retry_feedback, validation_errors), and the frame tag of every capability part kind.
  • Channels:
    • user prompts and their text files;
    • tool returns;
    • capability part bodies;
    • retry feedback: the text it quotes, not core's fence around it;
    • compaction summaries: the summarizer's text.
  • What the note says: which frame the text imitates and who wrote it. A data-tag note reads: "contains a <x> or </x> tag written by the tool, not by the application, so it opens or closes no element the application sent". Author names go through the same identifier checks as the existing notes.

Determinism and prefix stability

  • The decision: before each model request, the agent run decides, for each request it made, which capability kinds the request's text imitates. It checks against the kinds registered at that moment.
  • Where it's stored: on the run's own history copy, in the request's reserved metadata (ModelRequest.metadata['__pydantic_ai__']['lookalike_kinds']).
  • Later renders: they read the stored decision. A process that imports a kind later, or never imports it, renders an earlier request with the same bytes, and the decision survives dump and load.
  • No-op requests: a request with no < in untrusted text needs no decision, and stores nothing.
  • Merges: when consecutive requests merge, their decisions are combined, so neither is dropped.
  • Caller's objects: a history passed in, and requests rendered directly through Model.request() / prepare_messages, are never modified. A request without a stored decision is checked against the kinds registered when it's rendered.

Limitation: UI histories. The UI adapters never send the reserved metadata to the client or accept it back, so a history that a UI client reloads every turn arrives without its stored decisions. Each render checks it again against the kinds that process has registered. That's deterministic for a given history and registry, but it can differ between processes that have imported different kinds. Follow-up: once #12326's server mode keeps history on the server, these decisions are kept there too.

Smaller change: a client-forged 'no_output' feedback is now demoted to 'model_retry' feedback, which renders as the same fenced user text. The fence stays core's, and only the client's content inside it is checked.

Tests (tests/test_lookalike_coverage.py):

  • each tag family on each channel;
  • a genuine frame renders without a note;
  • a note names who wrote the text;
  • importing a kind mid-conversation changes no earlier request, and later text is checked against the new kind;
  • the same history renders the same bytes in a process without the kind;
  • the decision round-trips through dump and load;
  • plain text stores nothing.

Pending design decision: Planning tool returns. Planning's plan tools return their own <plan_reminder> frame as tool-result text, and those results now get a note that misattributes the tag to the tool. Two tests fail on this until it's decided how a capability's own tool output carries its frame:

  • tests/harness/planning/test_planning.py::TestEndToEnd::test_tool_loop_reads_the_previous_request_from_cache_on_the_wire
  • tests/harness/system_reminders/test_system_reminders.py::TestEndToEnd::test_composes_with_planning

Known UI round-trip gaps updated. The no_output_feedback known gaps now record the forged 'no_output' feedback rendering as 'model_retry' feedback does, with the lookalike-note announcement ahead of it.

Checklist

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

Summary by cubic

Text on any untrusted channel that imitates a core data frame or a capability part kind's frame now gets a lookalike note. Previously only system-voice tags (<system>, <context>, <context_diff>) were checked, and a capability part's body was checked only against its own tag. Now core data tags (tool_result, assistant_thinking, conversation_summary, retry_feedback, validation_errors) and every registered capability kind's frame tag are checked in user prompts, text files, tool returns, capability part bodies, retry feedback quotes, and compaction summaries. Client-forged no_output feedback is demoted to model_retry, which renders as the same fenced user text.

Determinism

  • The set of capability kinds a request's text is checked against is decided once, on first render, and stored as ModelRequest.metadata['__pydantic_ai__']['lookalike_kinds'].
  • Later renders and processes that import new kinds reuse that decision, so the same history always produces the same bytes.
  • The decision is stamped only on requests the run itself made; a caller's history is left alone, and merged consecutive requests combine their decisions.
  • Requests with no < in untrusted text skip the decision; pre-upgrade histories get one on their first render.