Give one-off capabilities a default `id` and a `combine` rule for repeats

Merged
DS
3mo ago
+2888-185

This pull request was posted by Codex Desktop using gpt-5.6-sol on behalf of David.

Closes #7274. Closes #7781. Supersedes #7695, #7144, and #6954, whose work is folded in here.

Why we make these changes

Capability-contributed toolsets need stable ids under durable execution, but one-off capabilities previously defaulted id to None. Separately, independently packaged capability bundles could not compose safely when both included the same one-off capability.

One-off built-ins now declare stable default ids, and AbstractCapability.combine() defines what repeated instances mean. This fixes durable leaf identity and lets independently declared configurations compose without leaving duplicate tools or unreachable capability state.

[!WARNING]
Compatibility impact: Repeated one-off capabilities that previously received order-dependent '_2' ids now merge under their class-declared default id. Scalar values use the later stated value, so two local fallbacks now keep only the later fallback; realtime session spans likewise use the effective later Instrumentation configuration.

Why this can ship in a minor release: This maintainer-approved change replaces order-dependent derived identity with the stable composition contract required by durable execution and independently packaged capability bundles. The affected duplicate configurations have the explicit compatibility path below.

Migration: Pass id=None to retain derived-and-disambiguated capability ids, or give independent instances distinct explicit ids. Native tools retain their own unique-id constraint.

New public surface

  • AbstractCapability.combine() — combines same-class capabilities sharing a class-declared default id within one application layer.
  • AbstractCapability.visit_and_replace() — rewrites a capability tree without flattening wrappers and containers.

The maintained public surface stops there; duplicate-resolution, id-policy, and validation helpers are private implementation details.

Pre-release decisions

  • Default field merging supports dataclass-declared state. A plain-class capability with a fixed default id must override combine(); the implementation must reject that unsupported default rather than silently drop subclass configuration.
  • combine() runs once for same-layer duplicates and never across the agent-to-run boundary. Realtime instrumentation must select the effective run-level configuration without calling combine() across layers.
  • Deprecated durable wrapper agents keep their existing runtime-toolset behavior. New collision and late-registration enforcement, and its documentation, apply only to the *Durability capability path.
  • The realtime Instrumentation selection change is not gated by InstrumentationSettings.version: that version controls emitted span format, not which user-supplied capability configuration wins.

User-visible behavior

Two same-class, one-off capabilities on the agent now form one configuration: collection fields union in application order, scalar fields use the later stated value, and a value stated on only one side survives. A capability passed to run() still replaces its agent-level namesake wholesale; combine() is never used across that boundary.

Capabilities representing separate identities derive distinct ids instead; MCP derives its id from the server URL.

Verification

Same-layer merge and cross-layer replacement, capability ids reaching contributed toolsets, durable registration using the capability id, and nested tree replacement pin the core behavior.

Compatibility impact

Existing duplicate one-off capabilities change behavior. They previously received order-dependent '_2' ids and coexisted; they now merge under the class-declared id. Pass id=None to retain derived-and-disambiguated ids, or distinct explicit ids when the instances represent separate identities. Native tools retain their own unique-id constraint, so distinct capability ids do not make two differently configured copies of the same native tool coexist.

For scalar fields, later wins. In particular, two local search fallbacks previously offered both tools; after this change only the later fallback remains. This is the intended meaning of a one-off capability stated twice.

Realtime session spans also move from the first Instrumentation configuration to the effective later configuration. This is intentionally not gated by InstrumentationSettings.version: the version controls the emitted span format, while this change corrects which user-supplied capability configuration is selected.

Additional implementation details and folded-in fixes
  • Every shipped capability declares whether repeated instances combine or remain distinct; the exhaustive policy test discovers classes by walking the package.
  • Merged native/local capabilities rebuild their derived tool state and re-run constructor validation, so provider-visible configuration matches the merged capability.
  • Explicit capability ids reach every contributed leaf, including callable toolsets= entries; multiple leaves use '{capability_id}_{index}'.
  • The *Durability capability path rejects runtime toolset-id collisions and post-construction toolsets without changing ordinary runs.
  • Capability-tree rewriting preserves wrapper/container structure and removal-aware rebinding.
  • Deferred MCP capabilities still require an explicit id so URL-derived material is not rendered into model-visible instructions.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy — intentionally left unchecked because the compatibility impacts above are part of the signed-off feature.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver — label, warning, and migration are present; the release draft still needs the compatibility note, and the API checker produced no finding requiring a waiver.
  • PR title is fit for the release changelog.