test: enforce prompt-cache wire-prefix stability across all recorded cassettes

Merged
DO
4mo ago
+577-3
  • Part of #6528 (slice C — the built-in invariant). Complements #6304's targeted UI-adapter round-trip tests.

What

Every VCR-recorded test now doubles as a prompt-cache prefix regression guard. An autouse conftest fixture (modeled on fail_partially_used_vcr_cassettes) checks, at playback time, that the test's recorded conversation only ever extends the serialized provider-cache prefix (tools → system → messages, per provider shape). A framework change that silently starts moving wire prefixes — the #4338 class of bug, which costs users the full input price on every turn with no error — now fails the owning test with the exact divergent block and a pointer to the exemption.

Tests that move the prefix deliberately declare it on the test itself, and the reason is mandatory (a bare marker fails):

@pytest.mark.moves_cache_prefix(reason='deliberate history compaction')

There are exactly ten such tests in the suite today: dynamic tool disclosure after ToolSearch discovery (tool-search evals, deferred-capability tests), OpenAI Responses history compaction, and one test that intentionally modifies history.

Empirical basis

A prototype pass over the full corpus (1,177 cassettes, 386 with multi-request endpoints): 490 prefix-stable consecutive pairs, and zero unexplained wire-level busts — the framework's request assembly is wire-prefix-stable today, and this locks that in. The only violating pairs were the ten deliberate movers above.

How

  • tests/cassette_utils.py: reusable helpers — canonical_prefix_blocks(body, url) (Anthropic Messages, OpenAI Chat/Responses, Google, Bedrock Converse shapes; single-block fields like plain-string system prompts and Google's systemInstruction Content dict serialize whole), classify_prefix_pair (extension / identical / shrunk / new-conversation / different-conversation / {level}-divergent), iter_cassette_prefix_violations(path), and check_cache_prefix_stability(node, path) (marker + reason enforcement + failure message; unit-testable so the failure paths are coverable).
  • Pair classification distinguishes real busts from benign cassette patterns: fresh conversations and independent conversations in one cassette (different first non-system message) are skipped; shrunk and mid-conversation divergence fail.
  • tests/conftest.py: the moves_cache_prefix(reason) marker registration and the autouse enforcement fixture. Playback-only (RecordMode.NONE): while recording, the on-disk cassette isn't final; CI runs playback, so every recorded test is enforced there.
  • tests/test_cache_prefix_invariant.py: unit tests for the helpers (synthetic violating cassette, provider shapes incl. the Google systemInstruction dict, classification edge cases, marker exemption + reason enforcement, fixture guards).
  • tests/AGENTS.md documents the invariant and the marker convention.

Deterministic tool-args serialization (the paired prevention from #6528) needed no changes — the corpus scan found zero arg-ordering drift; the invariant will catch any future regression.

Known limitation

Because enforcement runs at each test's own teardown, cassettes belonging to tests that are skipped in a given environment aren't checked there; the all-extras CI job runs effectively everything, so coverage in CI is near-complete. A standalone corpus-scan job could be added later if a stronger net is wanted.

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.

🤖 Generated with Claude Code