Keep a tool's `capability_id` when a prepare function rebuilds its `ToolDefinition`
No issue: found in an internal sweep of where capability content loses its <capability_content> frame.
A prepare function that returns a new ToolDefinition instead of replace()-ing the one it was given drops the definition's capability_id. That's the id CapabilityOwnedToolset stamps to say which capability contributed the tool. Without it, the tool's ToolReturn.content and anything it ctx.enqueues are no longer held to that capability's authority. For a capability at with_authority('none'), they reach the model as bare user text instead of framed data. The docs promise the frame in capabilities/custom.md.
Every prepare function that runs above the capability's toolset goes through PreparedToolset. That includes a capability's prepare_tools hook (its own or another capability's, like PrepareTools) and toolset.prepared(). PreparedToolset already refuses to add or rename tools. It now also keeps each tool's original capability_id, so the owner stays fixed for the run, as the ToolDefinition.capability_id docstring already says. Both readers that lost it get it back: the ToolReturn.content framing and ctx.enqueue.
Repro with Capability(id='plugin', tools=[fetch]).with_authority('none'), where fetch returns ToolReturn('ok', content=...) and enqueues, called natively:
| Setup | Before | After |
|---|---|---|
| no prepare hook | framed ('none') | framed |
PrepareTools(<builds new ToolDefinition>) | UserPromptPart, unframed | framed |
PrepareTools(<replace()>), .prepared(rebuild) inside the capability, Tool(prepare=rebuild), prefixed / renamed / filtered / combined toolsets, PrefixTools(cap) | framed | framed |
How this relates to #14327. #14327 keeps nested Code Mode/ToolBridge content at the inner tool's ceiling. It reads the inner tool's owner from tool_def.capability_id on the nested ToolManager. This PR doesn't touch _tool_execution.py or the harness. It makes sure the capability_id those lookups read is still there after a prepare hook, so it fixes the same bug for #14327's nested path too. The two PRs change different files and should merge cleanly in either order.
Tests: a regression test in tests/test_capability_parts.py, parametrized over the capability's own prepare_tools hook and another capability's PrepareTools. It asserts the typed CapabilityPart with CapabilitySource('plugin', authority='none'), and that the prepared request parts match a run without a prepare hook. The test fails on main. The toolset, tool availability, deferred loading, capability, and MCP prepare/prefix/filter tests pass locally.
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
Summary by cubic
Preserves a tool's capability_id when a prepare function rebuilds its ToolDefinition. Previously, a prepare hook that returned a new definition instead of replace()-ing the one it was given dropped the capability_id, so tool content from capabilities at with_authority('none') reached the model as unframed user text instead of framed data.
PreparedToolset now keeps each tool's original capability_id, so the owning capability stays fixed for the run. This affects all prepare paths: capability prepare_tools hooks, PrepareTools, and toolset.prepared(). The fix also covers the nested Code Mode/ToolBridge path that reads capability_id from the inner tool.