Skip to main content

AgentContext

Struct AgentContext 

Source
pub struct AgentContext<D = ()> { /* private fields */ }
Expand description

Per-run carrier of infra context + typed operator-side dependencies. D defaults to () so deps-less agents pay no type-system tax.

AgentContext<D> flows into the Tool and validator surfaces. Layers consume ExecutionContext only, reached via Self::core; this keeps the layer ecosystem (tower::Service spine) D-free and avoids generic explosion.

Cloning a context with D: Clone is shallow: the inner ExecutionContext clones cheaply (Arc refcounts on tenant id, extensions, audit sink) and D clones with whatever semantics the operator gave it. Cloning is the canonical path for sub-agent dispatch — children share the parent’s deps and inherit a child cancellation token via Self::child.

Implementations§

Source§

impl<D> AgentContext<D>

Source

pub const fn new(core: ExecutionContext, deps: D) -> Self

Construct from an explicit ExecutionContext and D.

Source

pub const fn core(&self) -> &ExecutionContext

Borrow the wrapped ExecutionContext. Layers and the tower service spine (ModelInvocation / ToolInvocation) consume the core directly so they stay D-free.

Source

pub const fn core_mut(&mut self) -> &mut ExecutionContext

Mutable handle to the wrapped ExecutionContext. The retry middleware reaches for this to stamp an idempotency key on first entry of a retry loop; outside that path, prefer the with_* builder methods.

Source

pub const fn deps(&self) -> &D

Borrow the typed operator-side deps. Tools consume this directly; the type system enforces that the deps shape matches the agent’s D parameter.

Source

pub const fn deps_mut(&mut self) -> &mut D

Mutable handle to the typed deps. Operators threading per-run mutable state (counters, accumulators) reach for this; most deps are Arc<...> and immutable, so the shared-borrow Self::deps suffices.

Source

pub fn into_parts(self) -> (ExecutionContext, D)

Decompose into (core, deps). Useful when bridging to APIs that consume each half independently.

Source

pub fn map_deps<E, F>(self, f: F) -> AgentContext<E>
where F: FnOnce(D) -> E,

Transform D into E while preserving the infra core. Sub-agents that need a narrower deps shape than the parent project the parent’s deps through this combinator at dispatch time.

Source

pub const fn cancellation(&self) -> &CancellationToken

Borrow the cancellation token (forwarded from ExecutionContext::cancellation).

Source

pub const fn deadline(&self) -> Option<Instant>

Returns the deadline if one was attached.

Source

pub fn thread_id(&self) -> Option<&str>

Borrow the thread identifier if one was attached.

Source

pub const fn tenant_id(&self) -> &TenantId

Borrow the tenant identifier (invariant 11). Always present — defaults to the process-shared TenantId::default when not explicitly set.

Source

pub fn run_id(&self) -> Option<&str>

Borrow the per-execute correlation id, if one has been stamped.

Source

pub fn idempotency_key(&self) -> Option<&str>

Borrow the idempotency key for the current logical call, if one has been stamped (by RetryService on first entry of a retry loop, or pre-allocated by the caller).

Source

pub fn is_cancelled(&self) -> bool

Convenience: did the cancellation token fire?

Source

pub const fn extensions(&self) -> &Extensions

Borrow the typed cross-cutting carrier.

Source

pub fn extension<T>(&self) -> Option<Arc<T>>
where T: Send + Sync + 'static,

Look up the extension entry registered for T. See ExecutionContext::extension.

Source

pub fn run_budget(&self) -> Option<Arc<RunBudget>>

Borrow the RunBudget attached to the run, if any.

Source

pub fn audit_sink(&self) -> Option<Arc<AuditSinkHandle>>

Borrow the AuditSinkHandle attached to the run, if any.

Source

pub fn tool_progress_sink(&self) -> Option<Arc<ToolProgressSinkHandle>>

Borrow the ToolProgressSinkHandle attached to the run, if any.

Source

pub async fn record_phase( &self, phase: impl Into<String> + Send, status: ToolProgressStatus, )
where D: Sync,

Emit a tool-phase transition with no metadata. Silent no-op when no sink is attached or the call is outside a tool dispatch. Fire-and-forget — sink failures stay inside the sink. Forwards to ExecutionContext::record_phase.

Source

pub async fn record_phase_with( &self, phase: impl Into<String> + Send, status: ToolProgressStatus, metadata: Value, )
where D: Sync,

Emit a tool-phase transition with structured metadata. Forwards to ExecutionContext::record_phase_with.

Source

pub fn with_deadline(self, deadline: Instant) -> Self

Attach a deadline. Returns self for builder-style chaining.

Source

pub fn with_thread_id(self, thread_id: impl Into<String>) -> Self

Attach a thread_id.

Source

pub fn with_tenant_id(self, tenant_id: TenantId) -> Self

Override the tenant scope.

Source

pub fn with_run_id(self, run_id: impl Into<String>) -> Self

Attach a run_id.

Source

pub fn with_idempotency_key(self, key: impl Into<String>) -> Self

Attach an idempotency key.

Source

pub fn with_run_budget(self, budget: RunBudget) -> Self

Attach a RunBudget.

Source

pub fn with_audit_sink(self, handle: AuditSinkHandle) -> Self

Attach an AuditSinkHandle.

Source

pub fn with_tool_progress_sink(self, handle: ToolProgressSinkHandle) -> Self

Source

pub fn add_extension<T>(self, value: T) -> Self
where T: Send + Sync + 'static,

Attach a typed cross-cutting value to the Extensions slot. See ExecutionContext::add_extension for the invariant 10 boundary.

Source§

impl<D: Clone> AgentContext<D>

Source

pub fn child(&self) -> Self

Derive a scoped child. The child inherits deadline, thread_id, tenant_id, run_id, idempotency_key, and extensions, but holds a child cancellation token and a clone of deps. Cancelling the parent cascades; cancelling the child does not.

Use for scope-bounded fan-out (sub-agent dispatch, scatter branches) where a fail-fast leaf should signal still-running siblings to abort cooperatively without tearing the whole request down.

Trait Implementations§

Source§

impl<D: Clone> Clone for AgentContext<D>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D: Debug> Debug for AgentContext<D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AgentContext<()>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<ExecutionContext> for AgentContext<()>

Source§

fn from(core: ExecutionContext) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<D> Freeze for AgentContext<D>
where D: Freeze,

§

impl<D = ()> !RefUnwindSafe for AgentContext<D>

§

impl<D> Send for AgentContext<D>
where D: Send,

§

impl<D> Sync for AgentContext<D>
where D: Sync,

§

impl<D> Unpin for AgentContext<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for AgentContext<D>
where D: UnsafeUnpin,

§

impl<D = ()> !UnwindSafe for AgentContext<D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more