Let a capability cache derived state again, and refuse a collection the merge cannot rebuild

Merged
DO
3mo ago
+244-56

This pull request was posted by Claude Code using claude-opus-5 on behalf of Douwe.

Follow-up to #8047, which shipped in v2.39.0. No separate issue.

#8047 fixed three real defects in the capability merge. Two of its fixes overshot, and this reshapes those while keeping the diagnoses, which were right.

A capability could not cache derived state any more

#8047 was correct that the leading-underscore exemption was a hole. replace_no_init copies the last instance either way, so a capability holding configuration in a private attribute silently dropped what an earlier instance stated — the one thing the merge promises not to do. That fix is kept.

What it cost was the other use of a private attribute. Deriving state from fields and caching it was never at risk, but nothing distinguished the two, so the check took both — and an allowlist of two literal names took some of it back:

runtime_attributes = {'__orig_class__', '_pydantic_ai_durable_operation_bindings'}

The tell is that our own capabilities failed the rule. MCP._resolved_id and Hooks.on both gave up cached_property to comply, so _resolved_id now re-runs urlparse on every read — and get_toolset() reads it. A rule two shipped capabilities cannot satisfy is the wrong rule, not two wrong capabilities. The allowlist is the same observation in a different form: it is what you write when the check cannot tell configuration from cache, and it is closed to everyone outside this repo.

The class already carries the distinction. A cached_property is declared on the class, so the merge finds it by walking the MRO — nothing to maintain — and the right answer for a cached value is not to keep it but to drop it, so the next read recomputes against the merged fields:

merged = Counted.combine([Counted(names=['a']), Counted(names=['b'])])
merged.names   # ['a', 'b']
merged.count   # 2 — recomputed; the last instance had cached 1

That is the answer __post_init__ cannot give, since replace_no_init deliberately skips it, and it is exactly the "compute derived values with properties" advice the #8047 docs give — made automatic instead of manual.

_pydantic_ai_durable_operation_bindings becomes a cached_property for the same reason. It was already a lazily-rebuilt cache written by hand into __dict__; saying so on the class is what lets _merge.py stop naming a durable_exec implementation detail. _get_durable_operation_bindings goes with the hand-rolled entry it wrapped.

Hooks._ordering stays a declared field — genuinely undeclared configuration, and #8047 fixed it correctly.

One failure, three collection kinds, two answers

When a field's declared collection type cannot be rebuilt from the union, main gives different answers depending on the kind:

declared type can't be rebuilton main
mappingunion kept, as a plain dict — a type the field's annotation does not describe
setunion kept, as a plain set — same
sequenceentries dropped; only the last instance's value survives

Both halves are now one rule, and it starts by separating the two cases the old code conflated.

A record that merely satisfies Sequence — a str, a NamedTuple — is not a collection anyone is contributing entries to. Unioning Pair('a', 'b') with Pair('c', 'd') splices one record's columns into the other's. These take the later value, which is what the table already gives everything the merge cannot combine. #8047's own test docstring says this ("a NamedTuple is a record, not a collection"); this makes it the reason rather than a side effect of failing to rebuild one.

A real collection whose declared type cannot be rebuilt is refused. The union has a type the annotation does not describe and nothing downstream would catch it, since __post_init__ is skipped; taking one instance's value drops what the other stated. Neither is worth guessing:

UserError: Merging capabilities under one `id` unioned field 'members' into a list, but its
declared type Roster cannot be rebuilt from that. Keeping the union would give the field a value
Roster does not describe, and keeping one instance would drop what the other stated. Give Roster a
constructor that takes its contents, declare the field as a plain collection type, or override
`combine` to merge this field itself.

merge_field_values takes field_name as a required keyword so no caller can raise that without saying which field to look at.

Also

A loop-invariant condition is hoisted out of the wrapper-identity while in _combine_duplicate_capabilities — last_layer != duplicates[0].layer_index never changed in the body, so it is an if.

Not changed: #8047 also moved _reject_class_crossing_id out of the single-survivor guard, so two different capability classes sharing one id across layers now raise where they used to replace silently. That is a behavior change in a released version, but it is the right one — an id names one capability — and the docs cover it.

Validation

  • tests/test_capability_combine.py, test_capabilities.py, test_capability_hooks.py, test_capability_native_or_local.py, test_capability_deferred_loading.py, test_capability_spec.py, test_mcp.py: 967 passed
  • tests/durable_exec/test_capability_durable_operation.py for the bindings rename
  • Ruff lint and format clean; pyright clean

New tests: test_a_cached_property_is_recomputed_against_the_merged_fields, test_a_collection_that_cannot_be_rebuilt_is_refused, and test_a_record_that_merely_looks_like_a_sequence_takes_the_later_value (renamed from the #8047 test, whose assertion still holds).

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 — left unchecked: a capability field whose declared collection type cannot be rebuilt from its contents now raises where v2.39.0 silently kept a union of the wrong type (mapping, set) or dropped entries (sequence). Everything else here restores behavior v2.39.0 removed.
  • PR title is fit for the release changelog.