Add `ToolDefinition.unless_tool_kind` and `RunContext.tool_name_for_kind`, and give instructions the current step's tools

Merged
DO
1mo ago
+582-4

Part of #9398. Step 3 of the decided plan on #9410, building on the tool kinds from #9710.

Adds three things, so Memory, ToolOutputLimits and RepoContext can drop their own reader tools when FileSystem is on the agent, and name the reader without hard-coding read_file:

  • ToolDefinition.unless_tool_kind (a ToolKind or its id, read back as the id): the tool is left out of the run step when another tool in it has that tool_kind. It's resolved in ToolManager.for_run_step after get_tools (so after prepare_tools), which every model, the realtime session and the durable paths go through, so the tool left out is neither sent nor callable. It's the local-tool counterpart of unless_native, which has to live in the model adapter because it depends on what the model supports; this one only depends on the tool set.
    • Deferred (defer_loading) tools count as present, so the answer doesn't flip when one is revealed.
    • A tool is only left out in favor of a tool that stays: in a chain (A unless B's kind, B unless C's kind), C drops B and A stays. When tools would each be left out in favor of the other, the first in toolset order stays, so a mutual pair never loses both.
    • Unlike tool_kind, the kind needn't be defined: only ids are compared, so a capability can name another package's kind without importing it, and an unknown kind means there's nothing to step aside for.
  • RunContext.tool_name_for_kind(kind) -> str | None: the (possibly prefixed) name of the tool with that kind in the current step, or None. It reads RunContext.tools, so deferred tools count and superseded ones don't. With several tools of one kind it returns the first in toolset order rather than raising: two prefixed FileSystems are a legitimate setup, an accessor that raises inside instruction rendering would be surprising, and toolset order is stable from step to step. Callers that need all of them can iterate ctx.tools.
  • Instruction functions see the current step's tool manager. ToolManager.for_run_step assigned the new manager to its own copy of the context (it replaces it from the second step on to carry retries), so the RunContext the graph then rendered instructions with still held the previous step's manager. It now assigns it to the caller's context too. test_instructions_see_the_current_steps_tools fails without this.

Docs: a "Superseding a tool by kind" section in docs/tools-advanced.md next to Typed Tool Parts (with a tested example), the building-pydantic-ai-agents skill's TOOLS-ADVANCED.md, and docstrings.

Inside a Temporal activity there is no tool manager, so TemporalRunContext carries a kind-to-name snapshot taken at dispatch, like available_tool_names, and tool_name_for_kind answers from it. A custom subclass whose serialize_run_context doesn't carry the snapshot returns None there.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code