Add `visit_and_replace` to `AbstractCapability`
This pull request was posted by Claude Code using claude-opus-5 on behalf of David.
Draft, opened to get the design reviewed before #7248 adopts it. No issue — it came out of a bug found while working on that PR.
AbstractCapability.visit_and_replace(visitor) rewrites a capability tree in place, mirroring AbstractToolset.visit_and_replace with one deliberate divergence: the visitor may return None to remove a node.
The load-bearing invariant is that it visits exactly the nodes apply visits, in the same order. That is what makes "collect with apply, decide, remove with visit_and_replace" coherent.
The bug it exists to fix
Reproduced on main, not theoretical. WrapperCapability.apply visits self and, when it wraps a container, that container's leaves. A flat leaf_capabilities() list is therefore not a partition of the tree, and rebuilding from it double-counts:
layer = PrefixTools(CombinedCapability([Thinking(low), Capability(id='bundle')]))
run = Thinking(high)
rebuilt from the flat list:
PrefixTools id=None
Thinking id=thinking <- superseded, still reachable through the wrapper
Capability id=bundle
Capability id=bundle <- duplicated
Thinking id=thinking <- run-level
Only Wrapper(Container) breaks. Wrapper(single leaf) is fine — apply yields just the wrapper, which adopted the wrapped leaf's id.
Rewriting in place instead gives the target shape, Combined([PrefixTools(Combined([bundle])), Thinking(high)]).
What changed
| type | behavior |
|---|---|
AbstractCapability | return visitor(self) — the leaf default |
CombinedCapability | recurse children, drop Nones, replace_no_init + re-run __normalize_capabilities; self if unchanged; None if removals emptied it (an already-empty container returns self) |
WrapperCapability | offer self first — a different result, including None, takes the whole subtree; otherwise recurse into wrapped only when it registers children; rebuild via replace_no_init + __adopt_wrapped_identity |
PrefixTools, ResolvedDynamicCapability | inherit from WrapperCapability; subclass state is carried by replace_no_init |
DynamicCapability | leaf — holds no child capability, so the base implementation is correct |
def apply is defined in exactly those three places under capabilities/, so the enumeration is complete.
This also extracts WrapperCapability._registers_wrapped_children, so apply and visit_and_replace share one predicate instead of drifting. apply becomes visitor(self); if self._registers_wrapped_children: self.wrapped.apply(visitor) — behavior-identical.
Known limitation
Wrapper(leaf) never offers the leaf to the visitor. That is the existing apply contract, not a new gap: the wrapper is the registered proxy that adopted the leaf's id, so dropping it must drop the leaf. A reachability boundary, not a structure-preservation failure.
Adoption in #7248
Not ported here — _compose_run_layers only exists on that branch. The adoption replaces its flatten-and-rebuild with an occurrence-counting last-wins visitor over the composed tree.
The counter must be per-id occurrence, not flat apply index. #7248 keys supersession on (group_index, leaf_index), correct against its flat per-group lists, but that does not translate to a tree walk: flat position there removes the wrong nodes and can return None for the whole tree.
Rejected alternatives
- A public
CapabilityVisitortype alias — inlined theCallableinstead, mirroringAbstractToolset.visit_and_replaceexactly. Avoids a new public symbol on a draft. - A visitor that cannot return
None(strict toolset parity) — removal is the motivating operation, andNonealready has precedent inCapabilityFuncandDynamicToolset.toolset_func. - An emptied container returning
CombinedCapability([])— leaves a dead node and stops an enclosing wrapper from collapsing. - Offering the wrapped leaf of
Wrapper(leaf)— would diverge fromapply; see the limitation above.
Tests and docs
tests/test_capability_visit_and_replace.py, 15 tests. Its own feature-central file rather than an addition to tests/test_capabilities.py, which sits ~6 KB under the check-added-large-files pre-commit ceiling. The three modified source files are at 100% line and branch coverage.
Docstring only for docs: docs/api/capabilities.md renders the module, and toolsets' visit_and_replace carries no prose docs either.
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.
- PR title is fit for the release changelog.