Skip to main content

SessionSpawnBudget

Struct SessionSpawnBudget 

Source
pub struct SessionSpawnBudget(/* private fields */);
Expand description

Session-wide cumulative counter of subagent spawns.

Bounds the total number of subagents spawned over a session’s lifetime, independent of SubAgentManager::spawn’s existing max_concurrent (in-flight) and max_spawn_depth (recursion) guardrails — a shallow, low-concurrency but high-frequency sequential delegation loop trips neither of those.

§Ownership, not a shared handle

Deliberately a plain AtomicUsize newtype — no Arc, no Clone. Nothing in this crate needs a shared, cloned handle to a budget: SubAgentManager owns one instance as the origin of truth, and zeph-core’s OrchestrationState owns an independent fallback instance used only when no manager is wired. Both the manager-side spawn path and the ACP /subagent spawn chokepoint (which never touches SubAgentManager at all) reach whichever instance applies through an accessor (Agent::session_budget in zeph-core) that hands out a plain &SessionSpawnBudget reference, never a copy.

This also makes a would-be TOCTOU hazard structurally impossible rather than merely documented: such a hazard could only arise if SubAgentManager were ever shared (e.g. behind an Arc) across concurrent tasks, and a future refactor down that path would have to deliberately reintroduce Clone/Arc here — a change reviewable on its own, rather than one hiding behind an innocuous .clone() call at some unrelated call site.

§Concurrency

AtomicUsize provides interior mutability so check and record_spawn work through a shared &self reference (every access goes through &self accessors, never &mut self), satisfying NFR-001’s atomic-counter requirement. Every call path that reaches either method — SubAgentManager::spawn/resume, the orchestration scheduler, and the ACP chokepoint — is serialized behind &mut Agent on the single agent task, so Relaxed ordering suffices. The check/consume split (budget is checked at the spawn guard but only consumed at the true commit point, so a rejected or transiently retried spawn never burns budget it never used) introduces no reachable race under that serialization.

§Examples

use zeph_subagent::SessionSpawnBudget;

let budget = SessionSpawnBudget::default();
assert_eq!(budget.spawned(), 0);

budget.check(1).expect("budget not yet exhausted");
budget.record_spawn();
assert_eq!(budget.spawned(), 1);
assert!(budget.check(1).is_err(), "cap of 1 must now be exhausted");

// `0` is the unlimited sentinel: check() always succeeds regardless of count.
assert!(budget.check(0).is_ok());

Implementations§

Source§

impl SessionSpawnBudget

Source

pub fn check(&self, max: usize) -> Result<(), SubAgentError>

Check the budget without consuming it.

max == 0 is the unlimited sentinel and always succeeds, so callers never need to duplicate the sentinel check themselves (mirrors DelegationMode::permits_explicit’s anti-drift rationale for a check shared across multiple chokepoints).

§Errors

Returns SubAgentError::SessionSpawnLimit when the cumulative spawn count has already reached max.

Source

pub fn record_spawn(&self)

Record a successful spawn, incrementing the cumulative count by one.

Must be called only at a spawn’s true commit point — see the check/consume split described in the type-level concurrency note.

Source

pub fn spawned(&self) -> usize

Current cumulative spawn count.

Trait Implementations§

Source§

impl Debug for SessionSpawnBudget

Source§

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

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

impl Default for SessionSpawnBudget

Source§

fn default() -> SessionSpawnBudget

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

Auto Trait Implementations§

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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, 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