Expand description
Adaptive effect handlers (§10.8).
This module provides the Rust-level infrastructure for runtime
strategy selection on effect failure. The surface follows the
spec’s RecoveryStrategy[E, T] trait, its RecoveryContext
record, and the five built-in combinators (retry, use_cached,
degrade, circuit_break, escalate).
Runtime plumbing (full Cancel ambient effect, interpreter wiring
of handling (... with adaptive(...)) blocks) is deferred to
Phase 5/6 per the 2026-04-22 changelog. What lands here in Phase D:
RecoveryContextmatching §10.8 exactly.- A
RecoveryStrategytrait whoseattemptreturnsStrategyOutcome<T, E>— the Rust spelling ofResult[T, E] | Cancelled— and a default-no-opon_cancel. - Five built-in combinators with cancel-awareness at their internal await points.
AdaptiveHandler: theEffect.adaptive()combinator. Given a list of strategies and a provider, it selects viaAiProvider::selectin development/sketch, or looks up a pin in the runtime decision manifest in production.AdaptivePinKey: the(error_signature, operation)pair that Q6 of the 2026-04-20 amendment specifies for pin granularity.
The module is not yet connected to the interpreter’s handling
block — that wiring happens in Phase 5/6 when the effect handler
runtime is extended to support the adaptive path. What this module
delivers is the stable API surface Phase 5/6 will wire up, with
unit-level coverage that exercises it end to end.
Structs§
- Adaptive
Handler - Handler constructed by
Effect.adaptive(strategies, context_aware). - Adaptive
Handler Builder - Builder for
AdaptiveHandler. Returned byadaptive/ [Effect::adaptive]-style callers. - Adaptive
PinKey - Pin key =
(error_signature, operation)per Q6 of the 2026-04-20 spec amendment. - Annotations
- Snapshot of
@context,@performance,@domain,@securityannotations reaching the recovery site. - Cancel
Checkpoint - Cooperative cancellation flag used by built-in combinators at their internal await points.
- Cancelled
- Phase D stub for the
Cancelledvalue that will cross the adaptive boundary in Phase 5/6 when the fullCancelambient effect lands. - Circuit
Breaker Strategy circuit_break(threshold, reset_after)combinator.- Degrade
Strategy degrade(fallback)combinator. Immediately returns a fallback value of the operation’s type.- Error
Occurrence - A prior error observed by this handler. Bounded to 10 most recent
entries in
RecoveryContext. - Escalate
Strategy escalate()combinator. Propagates the error without recovery.- Recovery
Context - Shape defined in §10.8 (Q5 of the 2026-04-20 amendment).
- Recovery
Result - Outcome of an adaptive recovery call. Wraps
StrategyOutcomewith theSelectionRecordthat was consulted so callers can inspect what happened. - Retry
Strategy retry(max, backoff)combinator. Re-invokes the operation up tomaxadditional times, waitingbackoff.delay(attempt)between attempts. Checks cancellation before each retry.- Selection
Record - Description of the selection that the handler applied. Useful for tests and the manifest layer.
- Simple
Error - A simple owned error value suitable for tests and library callers that don’t yet have a first-class error representation.
- UseCached
Strategy use_cached(ttl)combinator. Returns the cached value if present and within TTL; otherwise forwards the original error.
Enums§
- Adaptive
Error - Error type produced by
AdaptiveHandler::recoverwhen the handler cannot make a selection (e.g., unknown pattern in production mode). - Backoff
- Backoff function between retry attempts.
- Selection
Source - Explains why the handler picked a strategy.
- Strategy
Outcome - The Rust spelling of the spec’s
Result[T, E] | Cancelledsum.
Traits§
- Error
Value - Minimal view of a Bock error that the adaptive handler needs.
- Recovery
Strategy - Bock spec:
Functions§
- adaptive
Effect.adaptive(...)factory. Creates anAdaptiveHandlerBuilderwith the developer-preferred default (context_aware = true, development strictness, no provider, no manifest).- adaptive_
scope - Convenience: register the runtime scope for a decision so callers building a manifest entry by hand don’t have to duplicate the check.
- circuit_
break - Constructs a
CircuitBreakerStrategy. - degrade
- Constructs a
DegradeStrategyreturningfallbackon the first invocation. - escalate
- Constructs an
EscalateStrategy. - retry
- Constructs a
RetryStrategy. See spec §10.8. - use_
cached - Constructs a
UseCachedStrategywired tolookup.ttlis recorded in the description but enforcement is the lookup’s responsibility.
Type Aliases§
- Boxed
Strategy - Heap-allocated strategy pointer used everywhere the adaptive handler stores strategies.
- Cache
Lookup - A cached lookup function. Used by
use_cached. - PinTable
- Thread-safe lookup table for pinned selections. In production strictness the handler consults this before anything else.
- Recovery
Operation - The operation the adaptive handler will re-run on each attempt.