mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Respect app-server turn admission for agent mailbox wakes (#62537)
## Why Automatic mailbox wakes can start new turns while the app-server is draining or has paused turn admission. Native child results still need to reach the parent during drain so they can survive a daemon restart. ## What changed - Require a turn admission permit for ordinary automatic mailbox wakes and hold it through turn registration. Retry queued work when a temporary pause ends. Explicit follow-ups and native child results can still wake the parent during drain. - Let `thread/inject_items` queue plain-text agent messages for native child threads when native tools are enabled. Injected mail follows the ordinary wake rules. - Extract the remote app-server client, shared event and request types, and WebSocket compression into `codex-app-server-remote-client` so callers can use them without linking the app-server runtime. ## Testing Cover delivery of queued mail when admission reopens, injection into native child threads, and recovery of a child result after a daemon restart. GitOrigin-RevId: 4c5ceeca8829c2612cc2b6b74c03d0c7677a9244
This commit is contained in:
@@ -532,6 +532,7 @@ pub(super) async fn submission_loop(
|
||||
) {
|
||||
// Session shutdown and tree shutdown both use the existing teardown handler.
|
||||
let mut shutdown_received = false;
|
||||
let mut admission_updates = sess.services.extensions.turn_start_admission_updates();
|
||||
let mut catalog_updates = sess.services.models_manager.model_catalog_updates();
|
||||
if let Some(updates) = &mut catalog_updates {
|
||||
// A resumed chat must also consume a catalog fetched before it was opened.
|
||||
@@ -587,6 +588,21 @@ pub(super) async fn submission_loop(
|
||||
Ok(sub) => *sub,
|
||||
Err(_) => break,
|
||||
},
|
||||
update = async {
|
||||
match &mut admission_updates {
|
||||
Some(updates) => updates.changed().await,
|
||||
None => std::future::pending().await,
|
||||
}
|
||||
} => {
|
||||
if update.is_err() {
|
||||
admission_updates = None;
|
||||
} else {
|
||||
// Recheck the normal gate: another pause or shutdown may have
|
||||
// begun since this notification, and mail must remain queued.
|
||||
sess.maybe_start_turn_for_pending_work().await;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
update = async {
|
||||
match &mut mailbox {
|
||||
Some(updates) => updates.changed().await.map(|()| *updates.borrow_and_update()),
|
||||
|
||||
@@ -259,11 +259,19 @@ impl InputQueue {
|
||||
}
|
||||
|
||||
pub(crate) async fn has_trigger_turn_mailbox_items(&self) -> bool {
|
||||
self.has_mailbox_items_matching(|mail| mail.trigger_turn)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn has_mailbox_items_matching(
|
||||
&self,
|
||||
predicate: impl Fn(&crate::PendingAgentMessage) -> bool,
|
||||
) -> bool {
|
||||
self.mailbox_pending_mails
|
||||
.lock()
|
||||
.await
|
||||
.iter()
|
||||
.any(|mail| mail.communication.trigger_turn)
|
||||
.any(|mail| predicate(&mail.communication))
|
||||
}
|
||||
|
||||
pub(crate) async fn drain_mailbox(&self) -> Vec<PendingMailboxCommunication> {
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
//! Persistent thread settings apply on Started and Steered. Turn start
|
||||
//! options only update turn context on Started; input provenance follows each request.
|
||||
//! Host shutdown admission is checked before reserving or starting a new turn.
|
||||
//! Parent-delegated subagent input bypasses drain; automatic starts remain gated.
|
||||
//! Parent-delegated input and native child results finish admitted work during drain.
|
||||
//! Other automatic starts remain gated.
|
||||
//! Realtime drain refusals are returned to the fanout for ordered session teardown.
|
||||
//! Sender evidence is read without the active-turn lock and bound to the accepted turn.
|
||||
|
||||
@@ -64,6 +65,13 @@ mod agent;
|
||||
#[path = "turn_input_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
/// Keeps automatic wake admission alive until the reserved turn is registered.
|
||||
pub(crate) struct PendingWorkTurnReservation {
|
||||
pub(crate) turn_state: Arc<tokio::sync::Mutex<TurnState>>,
|
||||
pub(crate) previous_options: TurnStartOptions,
|
||||
pub(crate) admission: Option<Box<dyn Send>>,
|
||||
}
|
||||
|
||||
/// Why input is starting a turn; shared by admission and input delivery.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum TurnStartKind {
|
||||
@@ -890,9 +898,7 @@ impl Session {
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "The admission lock keeps the previous turn stable while capturing its attribution."
|
||||
)]
|
||||
pub(crate) async fn reserve_pending_work_turn(
|
||||
&self,
|
||||
) -> Option<(Arc<tokio::sync::Mutex<TurnState>>, TurnStartOptions)> {
|
||||
pub(crate) async fn reserve_pending_work_turn(&self) -> Option<PendingWorkTurnReservation> {
|
||||
let mut active_turn = self.active_turn.lock().await;
|
||||
if active_turn.is_some() || self.turn_starts_closed() {
|
||||
return None;
|
||||
@@ -910,13 +916,29 @@ impl Session {
|
||||
tracing::debug!(%error, "rollout budget stopped a pending-work wake");
|
||||
return None;
|
||||
}
|
||||
// Explicit follow-ups and native child results finish already-admitted work.
|
||||
// Injected mail has no terminal-result receipt and still respects drain.
|
||||
let admission = if has_trigger_turn
|
||||
|| self
|
||||
.input_queue
|
||||
.has_mailbox_items_matching(crate::PendingAgentMessage::is_terminal_result)
|
||||
.await
|
||||
{
|
||||
None
|
||||
} else {
|
||||
Some(self.services.extensions.admit_turn_start()?)
|
||||
};
|
||||
let active_turn = active_turn.get_or_insert_with(ActiveTurn::default);
|
||||
let previous_options = if has_trigger_turn {
|
||||
Default::default()
|
||||
} else {
|
||||
self.agent_message_wake_options().await
|
||||
};
|
||||
Some((Arc::clone(&active_turn.turn_state), previous_options))
|
||||
Some(PendingWorkTurnReservation {
|
||||
turn_state: Arc::clone(&active_turn.turn_state),
|
||||
previous_options,
|
||||
admission,
|
||||
})
|
||||
}
|
||||
|
||||
async fn agent_message_wake_options(&self) -> TurnStartOptions {
|
||||
|
||||
Reference in New Issue
Block a user