mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
## Why Host-owned tools need to wait for agent messages or steering input with the same behavior as native `wait_agent`, including when native multi-agent support is disabled. ## What changed - Add `CodexThread::wait_for_input_activity` and export `InputWaitOutcome` through core and core-api, sharing the wait implementation with `wait_agent`. - Distinguish notifying agent messages from steering input, with steering taking precedence when both are pending. Passive response items do not wake waits. - Observe input without consuming it. Waits remain active after the original turn ends, support independent observers, and can be cancelled by dropping the future. ## Testing Add coverage for pending and incoming input, agent-message classification, cancellation, independent observers, timeouts, and input arriving after the original turn completes or is interrupted. GitOrigin-RevId: d0c0bd776c3b6fd4ba3021603d87eaaf1d93976f
65 lines
1.9 KiB
Rust
65 lines
1.9 KiB
Rust
//! Shared, non-consuming input-activity waits for native tools and hosts.
|
|
|
|
use crate::session::InputQueueActivity;
|
|
use crate::session::session::Session;
|
|
use std::time::Duration;
|
|
use tokio::sync::watch;
|
|
|
|
/// Why a native or host input-activity wait finished.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum InputWaitOutcome {
|
|
/// Agent mail was observed, including a typed agent message from a notifying injection.
|
|
MailboxActivity,
|
|
/// User steering or another kind of notifying input was observed.
|
|
Steered,
|
|
/// The deadline elapsed or the input-activity source closed.
|
|
TimedOut,
|
|
}
|
|
|
|
pub(crate) async fn wait_for_input_activity(
|
|
session: &Session,
|
|
turn_id: &str,
|
|
timeout: Duration,
|
|
) -> InputWaitOutcome {
|
|
tokio::time::timeout(timeout, async {
|
|
let (activity, pending) = subscribe_input_activity(session, turn_id).await;
|
|
wait_for_activity(activity, pending).await
|
|
})
|
|
.await
|
|
.unwrap_or(InputWaitOutcome::TimedOut)
|
|
}
|
|
|
|
pub(crate) async fn subscribe_input_activity(
|
|
session: &Session,
|
|
turn_id: &str,
|
|
) -> (
|
|
watch::Receiver<InputQueueActivity>,
|
|
Option<InputQueueActivity>,
|
|
) {
|
|
let turn_state = session
|
|
.input_queue
|
|
.turn_state_for_sub_id(&session.active_turn, turn_id)
|
|
.await;
|
|
session
|
|
.input_queue
|
|
.subscribe_activity(turn_state.as_deref())
|
|
.await
|
|
}
|
|
|
|
pub(crate) async fn wait_for_activity(
|
|
mut activity: watch::Receiver<InputQueueActivity>,
|
|
pending: Option<InputQueueActivity>,
|
|
) -> InputWaitOutcome {
|
|
let activity = match pending {
|
|
Some(activity) => activity,
|
|
None => match activity.changed().await {
|
|
Ok(()) => *activity.borrow_and_update(),
|
|
Err(_) => return InputWaitOutcome::TimedOut,
|
|
},
|
|
};
|
|
match activity {
|
|
InputQueueActivity::Mailbox => InputWaitOutcome::MailboxActivity,
|
|
InputQueueActivity::Steer => InputWaitOutcome::Steered,
|
|
}
|
|
}
|