Restore capability composition invariants

Merged
DS
3mo ago
+298-71

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

Follow-up to #7248; no separate issue is closed.

Why we make these changes

The capability-composition contract from #7248 has edge cases that can lose configuration, permit cross-type id collisions, or corrupt a declared collection type. This restores its same-layer merge and cross-layer override semantics.

New public surface

none

User-visible behavior

agent = Agent(..., capabilities=[Thinking(id='shared')])
await agent.run('hi', capabilities=[ReinjectSystemPrompt(id='shared')])

# Before: silently replaces `Thinking`.
# After: raises `UserError`.
  • Fixed-id plain capability classes with state outside dataclass fields, including private attributes, must override combine().
  • Reusing one capability object between agent and run layers remains a wholesale run-level replacement and does not invoke combine().
  • If a declared sequence type cannot be rebuilt after a merge, the later declared value is kept without changing its runtime type.

Verification

What changes for existing users

Only configurations relying on these invalid states change; documented same-layer merging and cross-layer replacement by the same capability class are unchanged.

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.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver — none.
  • PR title is fit for the release changelog.