Check the prompt-cache prefix of `FunctionModel` and `TestModel` conversations
The cassette check (fail_cache_prefix_violations, from #6535) only sees requests that were recorded over the wire. Most framework behavior tests drive the agent with FunctionModel or TestModel, so the check never sees them: the Harness suite has 9 cassettes across 226 test files, and code mode, the dynamic catalog, tool search, deferred capabilities, Memory and SystemReminders have none.
This adds a second autouse check (tests/function_model_prefix.py). It records every request FunctionModel and TestModel receive, renders each one offline into the body AnthropicModel would send (a stand-in client hands the body back instead of sending it), and runs the same classify_prefix_pair and deferred-tool checks as the cassette check on consecutive requests of one conversation. Tests that move the prefix on purpose use the existing @pytest.mark.moves_cache_prefix(reason=...).
How a request is rendered and grouped:
-
Anthropic renders the output mode the test model resolved for its own profile; the rest of the preparation is left to
AnthropicModel, so instructions aren't rendered twice.cache_controlis stripped and messages are compared per content block, the granularity Anthropic caches at. Requests Anthropic can't render (a setting or output mode it rejects) aren't compared. -
A request follows the latest earlier request of the same run on the same model, so concurrent runs are each compared with themselves however they were scheduled. A run's first request follows the latest earlier request of its conversation (same model, same first message) with fewer messages, the one whose history it continues; one with as many or more is a branch (a run from a shorter
message_history) and isn't compared. -
No network and no event loop: the body is built by stepping
AnthropicModel.requestby hand until the stand-in client raises it. (Running it on a loop of our own shut down DBOS's thread pool, which DBOS sets as the test loop's default executor, and failed every later DBOS test in the worker.) A render that awaits something fails loudly rather than skipping the request. -
Benchmarks (
@pytest.mark.benchmark) aren't recorded, since recording copies each request inside the code they measure.tests/function_model_prefix.pyis listed as a benchmark input in.github/workflows/benchmark.yml, astests/conftest.pyimports it. -
Cost:
tests/test_agent.pyplustests/harness/compaction, single process, took 33.2s and 33.4s with the check and 32.8s and 33.6s without it. The Anthropic adapter's coverage now partly comes from the render as well; pausing coverage around it slowed acoverage runoftests/test_tools.pyfrom 7.5s to 161s, so it isn't paused.
Marked tests
Every test the check flagged on the current main (108 test IDs) is marked, with 81 markers (on top of the 3 this PR's first commit added). Most use one of a few shared reasons in tests/function_model_prefix.py:
| Reason | Markers |
|---|---|
TOOLS_CHANGE_MID_RUN: a prepare, filter, selector or dynamic toolset changes the tools between steps | 25 |
COMPACTION_REWRITES_HISTORY | 12 |
REQUEST_SCOPED_PROMPT_DROPPED: a request-scoped prompt is sent with its own request only | 11 |
INSTRUCTIONS_CHANGE_MID_RUN | 9 |
HOOK_REWRITES_REQUEST: a hook or history processor rewrites an earlier request | 8 |
FALLBACK_REWINDS: a failed fallback continuation rewinds the history | 8 |
A reason of its own: an enqueued ModelRequest with its own instructions, hand-built histories that branch or resume, the CodeMode default, the clai2 plugin snapshot, the handoff and wrap-up docs examples | 8 |
Where only some parameters of a test move the prefix, only those parameters are marked. Docs examples are marked by title in tests/test_examples.py.
Real busts found
None confirmed. Two flagged behaviors are deliberate but worth a decision, and are marked with a specific reason for now:
CodeMode's defaultdynamic_catalog=Falserewritesrun_code's description when a capability loads or a tool is discovered, which busts the whole cached prefix once per discovery (TestCodeMode.test_loaded_capability_tool_folds_into_run_code, clai2test_speculation_toggled_mid_turn_...). This is documented indocs/harness/code-mode.md, anddynamic_catalog=Trueavoids it.- Request-scoped prompts (
SystemReminders, the planning reminder,ctx.enqueue(..., scope='request')) are dropped from the previous request's tail, which costs that one block per request. Existing cassette tests already mark this as deliberate.
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.
š¤ Generated with Claude Code