Expose current turn admission state on CodexThread (#62604)

## Why

A thread can reserve a turn slot and assign a turn ID while it still
waits for host admission. Callers need to distinguish a pending turn
from one cleared to execute.

## What changed

Add `turn_admission_state()` to report `Idle`, `Pending` with an optional
turn ID, or `Admitted` with a turn ID in a single snapshot. Host-managed
turns become admitted after a grant is installed. Native turns are
admitted synchronously, including when no execution guard is needed.
The current slot retains its admitted state through finishing.

Reject superseded submissions before they can overwrite an owned slot,
and verify that a host grant still belongs to the current turn and task
before recording admission.

## Testing

Cover the admission state while a host decision is pending, after an
interrupt, during execution after a grant, and after the slot returns
to idle. Cover native admission without a host guard.

GitOrigin-RevId: 0633dc036aef99433505559c92b0b1b26a023352
This commit is contained in:
keith thornhill
2026-11-19 21:45:13 +00:00
committed by copyberry
parent b16fddf9d0
commit 75f2d93e7c
6 changed files with 157 additions and 15 deletions
+31
View File
@@ -211,6 +211,21 @@ pub use codex_guardian_context::GuardianRootMessage;
pub use codex_guardian_reviewer::GuardianAuthorizationVersion;
pub use codex_guardian_reviewer::GuardianRootSnapshot;
/// An atomic snapshot of admission for the thread's current turn slot.
///
/// A slot may be reserved before Core assigns it a turn ID. This snapshot does
/// not include work draining after an abort removes the slot.
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
pub enum TurnAdmissionState {
/// Core does not own a current turn slot.
Idle,
/// The slot is reserved, but execution has not been admitted.
Pending { turn_id: Option<String> },
/// Execution was admitted; the slot remains admitted through finishing.
Admitted { turn_id: String },
}
pub struct CodexThread {
pub(crate) session: Arc<Session>,
pub(crate) io: SessionIo,
@@ -896,6 +911,22 @@ impl CodexThread {
self.session.active_turn.lock().await.is_some()
}
/// Returns admission and turn identity from the same current-slot snapshot.
pub async fn turn_admission_state(&self) -> TurnAdmissionState {
let active = self.session.active_turn.lock().await;
match active.as_ref() {
None => TurnAdmissionState::Idle,
Some(turn) => match &turn.turn_id {
Some(turn_id) if turn.admitted => TurnAdmissionState::Admitted {
turn_id: turn_id.clone(),
},
turn_id => TurnAdmissionState::Pending {
turn_id: turn_id.clone(),
},
},
}
}
/// Returns the current turn ID, including while its stop hook runs.
pub async fn current_turn_id(&self) -> Option<String> {
self.session
+1
View File
@@ -62,6 +62,7 @@ pub use codex_thread::GuardianAuthorizationVersion;
pub use codex_thread::GuardianRootMessage;
pub use codex_thread::GuardianRootSnapshot;
pub use codex_thread::ThreadConfigSnapshot;
pub use codex_thread::TurnAdmissionState;
pub use guardian::GuardianInferenceContext;
pub use guardian::ScoreAuthorization as GuardianScoreAuthorization;
pub use guardian::guardian_policy_model;
+3
View File
@@ -38,6 +38,8 @@ use codex_protocol::protocol::TokenUsage;
pub(crate) struct ActiveTurn {
/// The slot's owner, retained after its task detaches to emit the terminal event.
pub(crate) turn_id: Option<String>,
/// Set only after execution admission succeeds; retained through finishing.
pub(crate) admitted: bool,
pub(crate) task: Option<RunningTask>,
pub(crate) sampling_interrupt: Option<SamplingInterrupt>,
/// Present when input admission is closed; cancelled when the final batch is recorded.
@@ -70,6 +72,7 @@ impl Default for ActiveTurn {
fn default() -> Self {
Self {
turn_id: None,
admitted: false,
task: None,
sampling_interrupt: None,
finishing: None,
+11 -5
View File
@@ -56,16 +56,22 @@ pub(super) async fn admit(
// A cancelled/replaced task must never install its permit into a newer task,
// even if recovery reused the turn ID.
let mut active = session.active_turn.lock().await;
let task = active
let turn = active
.as_mut()
.filter(|turn| turn.turn_id.as_deref() == Some(context.sub_id.as_str()))
.ok_or_else(|| CodexErr::new(CodexErrorDetails::TurnAborted))?;
let task = turn
.task
.as_mut()
.and_then(|turn| turn.task.as_mut())
.filter(|task| Arc::ptr_eq(&task.turn_context, context))
.filter(|_| !cancellation.is_cancelled())
.ok_or_else(|| CodexErr::new(CodexErrorDetails::TurnAborted))?;
task._agent_execution_guard = Some(guard);
task.pending_initial_input.take().ok_or_else(|| {
let input = task.pending_initial_input.take().ok_or_else(|| {
CodexErr::Fatal("initial input missing from the task awaiting host admission".into())
})
})?;
task._agent_execution_guard = Some(guard);
turn.admitted = true;
Ok(input)
}
impl Session {
+8 -1
View File
@@ -368,8 +368,13 @@ impl Session {
};
}
let turn = active.get_or_insert_with(ActiveTurn::default);
debug_assert!(turn.task.is_none());
if turn.turn_id.is_some() || turn.task.is_some() || turn.finishing.is_some() {
return TurnInputSubmission::NotSubmitted {
reason: NotSubmittedReason::Superseded,
};
}
turn.turn_id = Some(turn_context.sub_id.clone());
turn.admitted = false;
turn.finishing = None;
Arc::clone(&turn.turn_state)
};
@@ -432,6 +437,8 @@ impl Session {
None,
),
};
// Native admission is synchronous, including unrestricted turns that have no guard.
turn.admitted = host_admission.is_none();
let done_clone = Arc::clone(&done);
let session = Arc::clone(self);
let ctx = Arc::clone(&turn_context);