pub struct ModelContext { /* private fields */ }Expand description
Ephemeral, capability-limited context for exactly one model attempt.
The context intentionally is not serializable. It carries stable execution identity, a finite remaining run-budget view, a wall-clock deadline for durable evidence, the equivalent monotonic deadline for timeout enforcement, and a cooperative cancellation signal. It contains no prompt, tool argument, bearer token, credential value, provider SDK client, or mutable property bag.
Model adapters must treat cancellation and deadline enforcement as separate from provider outcome certainty. Cancellation wins when both conditions are observed together, but neither condition proves that in-flight provider work did not complete or incur usage.
Implementations§
Source§impl ModelContext
impl ModelContext
Sourcepub fn new(
tenant_id: TenantId,
run_id: RunId,
thread_id: ThreadId,
attempt_id: AttemptId,
budget: BudgetRemaining,
observed_at: Timestamp,
observed_instant: Instant,
cancellation: CancellationSignal,
) -> Result<Self, ModelContextError>
pub fn new( tenant_id: TenantId, run_id: RunId, thread_id: ThreadId, attempt_id: AttemptId, budget: BudgetRemaining, observed_at: Timestamp, observed_instant: Instant, cancellation: CancellationSignal, ) -> Result<Self, ModelContextError>
Constructs an ephemeral attempt context from one paired wall/monotonic observation.
observed_at and observed_instant must represent the same runtime
observation. The constructor converts the durable budget deadline into
a monotonic deadline so wall-clock adjustments cannot lengthen an active
provider call.
§Errors
Returns ModelContextError when the durable deadline is already
reached or the platform cannot represent the corresponding monotonic
deadline.
Sourcepub const fn tenant_id(&self) -> &TenantId
pub const fn tenant_id(&self) -> &TenantId
Returns the tenant boundary for storage, policy, and audit correlation.
Sourcepub const fn attempt_id(&self) -> AttemptId
pub const fn attempt_id(&self) -> AttemptId
Returns the exact attempt identifier required on every response and event.
Sourcepub const fn budget(&self) -> &BudgetRemaining
pub const fn budget(&self) -> &BudgetRemaining
Returns the finite remaining run capacity captured for this attempt.
Sourcepub const fn observed_at(&self) -> Timestamp
pub const fn observed_at(&self) -> Timestamp
Returns the wall-clock observation paired with the monotonic clock.
Sourcepub const fn deadline_instant(&self) -> Instant
pub const fn deadline_instant(&self) -> Instant
Returns the equivalent process-local monotonic deadline.
Sourcepub const fn cancellation(&self) -> &CancellationSignal
pub const fn cancellation(&self) -> &CancellationSignal
Returns the shared cooperative cancellation signal.
Sourcepub fn remaining_time_at(&self, observed_instant: Instant) -> Option<Duration>
pub fn remaining_time_at(&self, observed_instant: Instant) -> Option<Duration>
Returns remaining monotonic time, or None at and after the deadline.
Sourcepub fn stop_reason_at(
&self,
observed_instant: Instant,
) -> Option<ModelStopReason>
pub fn stop_reason_at( &self, observed_instant: Instant, ) -> Option<ModelStopReason>
Returns the deterministic stop reason observed at one monotonic instant.
Explicit cancellation wins if cancellation and deadline are both observable. This precedence matches the run state-machine contract for work that has not committed a result.