[complexity] Pin the projected inputs `PrefectAgentInputs.compute_key` hashes and the full tool hook order
Merged
DS
12d ago+242+242+012345678901234567890123456789-33-33-01234567890123456789
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Why we make these changes
Part of #10983. The next complexity refactor moves code in durable_exec/, including pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_cache_policies.py, and its proof that Prefect cache keys stay identical rests on the tests below. On main, the cache-key tests only compare two keys for equal or not equal, so a projection that reads, drops or reshapes a value both keys share passes them. Tool hook order is checked by membership only.
This PR pins both:
projected_cache_inputsrecords whatPrefectAgentInputs.compute_keyhands Prefect'sINPUTSpolicy to hash, so tests assert on the projection, not its digest.test_cache_key_run_context_projection_is_exhaustivesnapshots the projection of aRunContextwith every projected field set to its own value, and pins thatworkspaceis left out when unset.test_cache_policy_projects_nested_run_context_and_toolis parametrized over a tuple, a list and a dict, and asserts a nestedRunContextis projected as a top-level one is.test_cache_policy_excludes_non_serializable_depssnapshots the projecteddepsfor a dataclass, a model, a bare lock, a list and a tuple.test_cache_policy_keeps_user_dataclass_fieldsadds a dataclass class held bydeps, which is kept rather than expanded.test_all_hook_types_on_same_capabilitynow runs a validation retry, a toolModelRetryand anon_tool_execute_errorrecovery, and snapshots the full hook log in place of its membership checks.
New public surface
None. Tests only.
Verification
tests/durable_exec/test_prefect.pyandtests/test_capability_hooks.pypass.- Each mutation of production code below fails the pin it lists. No pin fails on
main. Prefect mutations were run against the file's 36 cache-key tests (-k 'cache_policy or cache_key or cacheable'); hook mutations against the whole file.
Mutations and the pins they fail
| File | Mutation | Fails |
|---|---|---|
_cache_policies.py | 'retry': value.retry, → 'retry': value.max_retries, | test_cache_key_run_context_projection_is_exhaustive only |
_cache_policies.py | remove the deadline projection line | test_cache_key_run_context_projection_is_exhaustive only |
_cache_policies.py | **({'workspace': value.workspace.ref} if value.workspace.ref is not None else {}), → 'workspace': value.workspace.ref, | test_cache_key_run_context_projection_is_exhaustive only |
_cache_policies.py | 'agent': value.agent.name if value.agent is not None else None, → 'agent': None, | test_cache_key_run_context_projection_is_exhaustive only |
_cache_policies.py | _CACHE_EXCLUDED_FIELDS = frozenset({'timestamp', 'run_id', 'conversation_id'}) → add 'state' | test_cache_key_run_context_projection_is_exhaustive only |
_cache_policies.py | in _replace_run_context, if _is_container(value): → if _is_tuple(value): | test_cache_policy_projects_nested_run_context_and_tool[list], [dict] only |
_cache_policies.py | in _cacheable_value, return [exclude_non_serializable(item) for item in value] → return _NON_SERIALIZABLE | test_cache_policy_excludes_non_serializable_deps only |
_cache_policies.py | if is_dataclass(obj) and not isinstance(obj, type): → if is_dataclass(obj): | test_cache_policy_keeps_user_dataclass_fields only |
tool_manager.py | in _run_execution_lifecycle, tool_result = await cap.on_tool_execute_error(...) → return await cap.on_tool_execute_error(...) | test_all_hook_types_on_same_capability only |
tool_manager.py | drop ToolRetryError from the control-flow except in _run_execution_lifecycle | test_all_hook_types_on_same_capability only |
tool_manager.py | move before_tool_execute in _run_execution_lifecycle to just before after_tool_execute (a two-line move: no one-line edit reorders two hook calls) | test_all_hook_types_on_same_capability and 5 existing hook tests |
What changes for existing users
Nothing.
PR decisions
2026-11-24 · Pins PR scope and test-extension rule · iter -
- Decision: Tests-only PR: snapshot the projected Prefect cache inputs (not the hash) and one full tool-hook-order snapshot; extend or parametrize existing tests rather than add new ones. David's call, relayed by the managing session.
- Why: R23b's proof needs absolute Prefect projection pins and an ordered hook pin; limit test sprawl
- Source: https://github.com/pydantic/pydantic-ai/issues/10983
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · Projection recorded at Prefect's INPUTS boundary · iter -
- Decision: projected_cache_inputs() patches INPUTS.compute_key (the shared Prefect instance) and records the inputs PrefectAgentInputs.compute_key hands it, instead of calling the private pipeline functions
- Why: This session's pick: pins what is hashed regardless of how R23b restructures _replace_toolset_tools/_replace_run_context/_strip_cache_excluded_fields
- Source: https://github.com/pydantic/pydantic-ai/issues/10983
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · Nested RunContext pinned by equality to the top-level projection · iter -
- Decision: test_cache_policy_projects_nested_run_context_and_tool is parametrized over tuple/list/dict and asserts the nested projection equals nest(top-level projection); the absolute field values are snapshotted once in test_cache_key_run_context_projection_is_exhaustive
- Why: This session's pick: avoids three copies of a 25-line projection snapshot while still pinning container shape and projection identity
- Source: https://github.com/pydantic/pydantic-ai/issues/10983
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · Hook-order pin extends test_all_hook_types_on_same_capability · iter -
- Decision: Its membership asserts become one full cap.log snapshot over a run with a validation retry, a tool ModelRetry and an on_tool_execute_error recovery
- Why: This session's pick: the test already claimed all hook types fire on one capability; a full log pins order and the retry/recovery paths without a new test
- Source: https://github.com/pydantic/pydantic-ai/issues/10983
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -
- Decision: reserved; dispatching a fresh Haiku reviewer (light tier, effort high) via policy-base-review in a policy-base checkout
- Why: tree 86632e60331b9c72e656e9bbd31bc5220114055e; scope full branch 99c8cf8e68..98a91df95c; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/prefect-cache-hook-order-pins
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -
- Decision: no review: policy-base-review exited 1, the reviewer blocked on the folder-trust dialog for the prompt's scratchpad folder; reported to the managing session; dispatch count stays reserved
- Why: tree 86632e60331b9c72e656e9bbd31bc5220114055e; scope full branch 99c8cf8e68..98a91df95c; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/prefect-cache-hook-order-pins
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · Blocked review run does not count against the budget · iter -
- Decision: The policy-base-review run that stopped on the folder-trust dialog reached no review and does not count: 3 reviews remain. Bundle moves to the manager's review-bundles/cx-r23p-1 folder. Managing session's call.
- Why: No review content was produced
- Source: https://github.com/pydantic/pydantic-ai/issues/10983
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
- Supersedes: pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378
2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -
- Decision: reserved; relaunched the fresh Haiku reviewer (light tier, effort high) via policy-base-review, bundle in the manager's review-bundles/cx-r23p-1
- Why: tree 86632e60331b9c72e656e9bbd31bc5220114055e; scope full branch 99c8cf8e68..98a91df95c; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/prefect-cache-hook-order-pins
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -
- Decision: no findings: current at 98a91df95c27ef6d897e26553eb2d290fb09e378
- Why: tree 86632e60331b9c72e656e9bbd31bc5220114055e; scope full branch 99c8cf8e68..98a91df95c; Haiku reviewer, effort high; first review
- Source: https://github.com/pydantic/pydantic-ai/tree/prefect-cache-hook-order-pins
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
2026-11-24 · exempt delta 98a91df95c..fb2539cad5 · iter -
- Decision: No pre-push review: test code only, types the container parametrization of test_cache_policy_projects_nested_run_context_and_tool that CI's Pyright rejected (exempt row: test-harness repair)
- Why: chains from reviewed 98a91df95c27ef6d897e26553eb2d290fb09e378 (review 1 of 3); same three cases and assertions; affected test passes (3), P5 mutant still fails [list] and [dict]; local pyright 0 errors
- Source: https://github.com/pydantic/pydantic-ai/pull/14330
- Session: 8afe3b86-85df-4339-9ded-92cc810ba40f
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.