Share `PydanticAIDocs` toolset with its per-run copies so Temporal and Prefect accept it
Written by Claude Code on behalf of @DouweM.
Part of #14403
Why we make these changes
PydanticAIDocs.get_toolset() built a new PydanticAIDocsToolset on every call, and for_run returns a per-run copy, so the toolset a run contributes was never the one the durable engine registered when the agent was constructed. Engines that refuse runtime function toolsets (Temporal, Prefect) reject the run with UserError: PydanticAIDocsToolset cannot be added at runtime with .... Same class of bug as CompactOnRequest in #15394.
The new toolset per call held two things per run: the doc cache, which must not cross runs (workspace-local content), and local_docs_path resolved from PYDANTIC_AI_HARNESS_DOCS_PATH. Sharing one toolset can't share either of these.
PydanticAIDocsbuilds one toolset lazily and shares it with every per-run copy.for_runbuilds it on the original first, asPlanning.for_rundoes. The toolsetid(stillNone) and theread_pyai_docstool name are unchanged.- At call time the shared toolset reads the cache of the run's own copy, which it finds through
find_run_capability(the helperMemoryandPlanninguse). Concurrent runs each find their own copy, so they don't share a cache. Onlyfor_runcreates a cache, so anything that isn't a run's copy has none and caches nothing. That covers direct toolset use, a durable worker's construction-time tree, and copies made outsidefor_run, such as withwith_authority(). local_docs_pathis resolved from the capability (local_docs_path, else the env var) on each call. Before, it was resolved once per run.PydanticAIDocsToolsetgets two private hooks,_run_cache(ctx)and_run_local_docs_path(). Its public constructor and its behavior when built directly are unchanged.
User-visible behavior
Agent(model, capabilities=[LocalWorkspace(root), PydanticAIDocs(local_docs_path=Path('docs')), durability])
# before: UserError: PydanticAIDocsToolset cannot be added at runtime with <engine> ...
# after: runs; read_pyai_docs memoizes per run as before
Temporal still refuses the agent at construction because the toolset has no id. #15396 forwards the capability id. Once #15396 and this PR have both landed, remove PydanticAIDocs's DURABLE_TEMPORAL exemption (tests/capability_cases.py, including ID_LESS_TOOLSETS) and add temporal=('capability__workspace__call', 'toolset__pydantic_ai_docs__call_tool') to its case's durable_steps. I ran tests/harness/test_durable_temporal_replay.py -k PydanticAIDocs locally with #15396's id forwarding applied and the exemption removed. It records exactly those two units and replays.
What changes for existing users
- No change for non-durable agent runs: each run still memoizes its own docs, and content from one workspace doesn't reach another run.
- A toolset from
PydanticAIDocs().get_toolset()called directly outside an agent run no longer memoizes, because no run owns a cache there. This isn't a documented surface. - On a durable worker that calls the tool against the construction-time capability, every call re-reads. The docs page and README now say so. Before this change those engines refused the toolset entirely.
scripts/durable_exec_contract.py --write: no change. No public API change.
Verification
- New
TestThroughAgent.test_runs_under_a_durable_engine_that_refuses_runtime_function_toolsets[cache=True|False]: two runs under aRecordingDurabilitythat refuses runtimefunctiontoolsets. Asserts the second read in a run comes from that run's cache only. Without the fix (mainsrc, run in-process) it fails with the runtime-toolsetUserErrorabove. - New
test_concurrent_runs_keep_their_own_cache: two concurrent runs against different workspaces, with a barrier so both cache before either reads again. - New
test_run_copy_shares_the_registered_toolsetandtest_toolset_outside_a_run_copy_caches_nothing[none | construction_time | construction_time_copy]. The latter replaces the two tests that asserted the old cache-sharing internals. - Mutation check: a variant that caches on the shared capability fails the durable, concurrent and workspace-isolation tests.
- Temporal contract (
tests/harness/test_durable_temporal_replay.py -k PydanticAIDocs, single file, no-n) passes as-is, with the exemption still applying. ruff check/ruff format --checkclean.
Decisions
- Pre-push review (call 1 of 3, fresh-context, read-only): no blocking findings. Remediated three optional ones: parametrized the durable test over
cache=Falseso the branch is covered in this module, added a sentence on durable-worker caching to the docs page and README, and shortened an over-long docstring. Declined one: a private_cacheidentity assertion, which the concurrent and durable tests already cover through behavior. - Review bot finding (construction-time copies, e.g.
with_authority(), could share a cache): reproduced it and fixed it in 984b1f39af by creating the cache only infor_run. - Took the per-run cache from the run's capability copy rather than a
run_id-keyed cache in the shared toolset. That way nothing needs cleaning up at run end, and it's the patternMemoryandPlanningalready use.
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.