test: assert prompt-cache prefix byte-stability across UI-adapter round-trips

Merged
DS
5mo ago
+1801-1

Part of the cache-prefix-stability work — the verify arm. Pairs with the harness-side observe arm in pydantic/pydantic-ai-harness#327 (CacheStabilityMonitor). Related to #6300 (a fidelity gap this class of test guards against; not fixed here).

What

A UI-driven app (Vercel AI / AG-UI) holds conversation history in the protocol's own message shape and re-submits it every turn; the server turns it back into Pydantic AI messages with load_messages before sending to the provider. If that dump_messages → load_messages round-trip changes the provider request bytes, the cacheable prefix moves and the provider's prompt cache silently misses — no error, just higher cost and latency (the PR #4338 class of problem).

This adds tests/test_cache_prefix_stability.py: VCR tests that record a real conversation, round-trip the resulting history through each adapter, then re-send the same follow-up prompt with the original vs round-tripped history and compare the two recorded request bodies byte-for-byte.

Coverage (the PR #5873 wire-fidelity risks)

  • OpenAI tool-call args (arguments rides as a JSON string) — byte-stable through both adapters.
  • Anthropic thinking signatures — byte-stable through Vercel and AG-UI ≥ 0.1.13.
  • AG-UI < 0.1.13 has no reasoning carrier, so it drops ThinkingPart on dump; that test asserts the re-sent prefix changes, documenting the protocol-version limitation.

On real provider data the round-trips are byte-faithful, so these are regression guards rather than a bug reproduction. (The latent str→dict args-serialization gap in VercelAIAdapter, surfaced while building this, is tracked separately in #6300; real providers send canonical JSON so it isn't observed in practice.)

Why capture the request payload (not just structural round-trip equality)

Structural load(dump(x)) == x checks already exist. These assert the property the cache actually depends on — the serialized provider request bytes — which structural equality doesn't guarantee and existing tests don't cover.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.