[complexity] Pin the projected inputs `PrefectAgentInputs.compute_key` hashes and the full tool hook order

Merged
DS
12d ago
+242-33

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:

New public surface

None. Tests only.

Verification

  • tests/durable_exec/test_prefect.py and tests/test_capability_hooks.py pass.
  • 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
FileMutationFails
_cache_policies.py'retry': value.retry, → 'retry': value.max_retries,test_cache_key_run_context_projection_is_exhaustive only
_cache_policies.pyremove the deadline projection linetest_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.pyin _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.pyin _cacheable_value, return [exclude_non_serializable(item) for item in value] → return _NON_SERIALIZABLEtest_cache_policy_excludes_non_serializable_deps only
_cache_policies.pyif is_dataclass(obj) and not isinstance(obj, type): → if is_dataclass(obj):test_cache_policy_keeps_user_dataclass_fields only
tool_manager.pyin _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.pydrop ToolRetryError from the control-flow except in _run_execution_lifecycletest_all_hook_types_on_same_capability only
tool_manager.pymove 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 -

2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -

2026-11-24 · Blocked review run does not count against the budget · iter -

2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -

2026-11-24 · pre-push review 1 of 3 at 98a91df95c27ef6d897e26553eb2d290fb09e378 · iter -

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.