Run a nested capability's durable operation as itself when a registered capability shares its `id`
Written by Claude Code on behalf of @DouweM.
Closes #15320
Why we make these changes
A durable operation is dispatched by (capability.id, operation), and the run's dispatcher closes over the capability the run registered under that id. A capability held in another capability's field with the same id (CompactOnRequest(SummarizingCompaction()) next to a direct SummarizingCompaction(), both on the default id='summarizing_compaction') therefore ran its operation with the registered capability's config (model, prompt, settings) and recorded a spurious unit. This happened from a hook and from inside the holder's own unit, on every engine.
durable_operation_dispatch now returns the dispatcher only when the caller is the run's registered capability for that id, or the agent-level capability it came from (a for_run copy's original). Any other same-id instance runs inline as itself, the way a nested capability with a unique id already does. This is option A from the issue. No op, activity, step or task names change, binding is untouched, the check is engine-neutral, and plain agents never reach it because they have no dispatchers. Keeping the check inside durable_operation_dispatch means CodeMode's advance check (code_mode/_toolset.py) gives the same answer as the call itself.
New public surface
None. The private durable_operation_dispatch now takes the capability instead of its id; its one other caller (CodeMode) is updated.
User-visible behavior
agent = Agent(model, name='a', capabilities=[Echo('DIRECT'), Holder(Echo('NESTED')), RecordingDurability()]) # both Echo id='echo'
# Holder calls `await self.inner.describe(ctx)`
# before: 'DIRECT', with units ['a__capability__echo.describe', 'a__capability__echo.describe', ...]
# after: 'NESTED', and only the registered Echo's call records an `echo.describe` unit
Verification
The tests live in tests/durable_exec/test_capability_durable_operation.py and run on RecordingDurability:
test_an_operation_on_a_same_id_capability_in_a_field_runs_on_that_capability: the nested capability returns its own value from a hook and from inside the holder's own unit, and records no spurious unit.test_a_same_id_capability_in_a_field_runs_inline_wherever_the_registered_one_is[wrapped|deferred_group|run_capabilities]: the registered capability is reached through aWrapperCapability(CombinedCapability(...)), a loaded deferred group, or the holder passed at run time. In each case the registered capability still dispatches and the nested one runs inline.test_an_operation_on_the_agents_capability_dispatches_to_its_run_copy: a construction-time instance whosefor_runreturned a copy still dispatches. This guards the agent-leaf clause.
One test lives in tests/harness/compaction/test_compact_on_request.py:
test_durable_strategy_summarizes_with_its_own_model_beside_a_same_id_summarizer:CompactOnRequest(SummarizingCompaction(...))plus a directSummarizingCompactionsummarizes with the nested summarizer's model.
All of the above ran in-process (no pytest). With the source change reverted, every test except the run-copy guard fails. The run-copy guard fails only when the agent-leaf clause is removed. The fixture-free tests in both files still pass. scripts/durable_exec_contract.json is regenerated.
What changes for existing users
A run that recorded a spurious unit from a nested same-id caller won't replay. That happens only when the nested call came from a hook, outside any durable unit, e.g. a nested SummarizingCompaction under TieredCompaction/FallbackCompaction beside a direct one. A call from inside the holder's own unit, like CompactOnRequest's compact, already ran inline, just with the wrong config, so those runs replay cleanly. On Temporal it hits a nondeterminism error; on DBOS and journal-style engines the steps misalign. Prefect is cache-keyed and recomputes. Those runs were using the wrong capability's config anyway, so finish or restart them before upgrading. Runs without a same-id nested capability are unaffected.
PR decisions
- 2026-11-30 Option A over failing closed or keying by instance path. Failing closed would break the documented default
CompactOnRequest(SummarizingCompaction())+SummarizingCompaction()combination. Keying by path would change persisted names and require binding nested instances. Making the nested operation durable is #14874's job; that PR can restore its same-id test on top of this one. - 2026-11-30 Pre-push review 1 of 3 at
cfec53589b. Remediated in34709f6768. The check moved intodurable_operation_dispatchso CodeMode's advance check agrees with the call, which also removed the single-use helper. The parametrized test now uses aCasedataclass with an exact snapshot of each case's units. Per the task owner's instruction, only one local review was run; the remediation follows the reviewer's suggested change and was verified in-process.
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. (No API change; the in-flight-run replay note is under "What changes for existing users".)
- PR title is fit for the release changelog.
Summary by cubic
Fixes durable operation dispatch so a nested capability that shares an id with a registered capability runs its operation as itself instead of dispatching to the registered capability's config and recording a spurious unit.
durable_operation_dispatchnow takes the capability instead of itsidand returns the dispatcher only when the caller is the run's registered capability for thatid, or the agent-level capability it came from. Its other callers, CodeMode anddispatches_as_durable_operation, are updated to pass the capability.- The nested capability runs inline from hooks and from inside the holder's own durable unit, matching how nested capabilities with unique
ids already behave. - Tests cover the nested, wrapped, deferred-group, per-run, and agent-level run-copy cases, plus
CompactOnRequestsummarizing with its own model beside a same-id directSummarizingCompaction. - Documents the behavior in
docs/capabilities/custom.md.
Migration
- Runs that recorded a spurious unit from a nested same-id caller outside any durable unit (e.g. a nested
SummarizingCompactionunderTieredCompactionbeside a direct one) won't replay cleanly; finish or restart them before upgrading. Other runs are unaffected.