Add durable user-message queue dispatch (#37204)

## What changed

- Add a storage-neutral queue extension for listing, adding, editing, reordering, deleting, and explicitly starting queued user messages.
- Dispatch queued messages in FIFO order when a thread becomes idle after a completed turn, while leaving them queued after interruptions or failures.
- Wait for user input to be persisted before removing its queue item, and report hook rejection, task termination, and persistence failures distinctly.
- Snapshot local image and audio attachments into validated data URLs before storing them, enforce input limits, and emit `ThreadQueueChanged` events after mutations.

## Testing

- Cover queue mutations, automatic and explicit dispatch, interruption and failure behavior, hook rejection, invalid records, input limits, and local attachment snapshotting.

GitOrigin-RevId: 14f39d7c8cbaccb767a6da84b82cced0edfcb4b9
This commit is contained in:
efrazer-oai
2026-08-06 03:11:42 +00:00
committed by copyberry
parent 70b4653232
commit bc8b25ea02
42 changed files with 2114 additions and 140 deletions
+80 -11
View File
@@ -6,8 +6,11 @@ use crate::session::SessionSettingsUpdate;
use crate::session::SteerInputError;
use crate::session::TurnInput;
use crate::session::session::Session;
use crate::user_message_admission::PendingUserMessageAdmissionState;
use crate::user_message_admission::UserMessageAdmission;
use crate::user_message_admission::UserMessageAdmissionError;
use codex_exec_server::SelectedCapabilityRootsStatus;
use codex_extension_api::ThreadIdleCause;
use codex_features::Feature;
use codex_otel::SessionTelemetry;
use codex_protocol::ThreadId;
@@ -100,6 +103,12 @@ pub enum TryStartTurnIfIdleRejectionReason {
/// Another turn or task is active, or the idle reservation was lost before
/// the automatic turn could start.
Busy,
/// A user-prompt hook consumed and rejected the submitted input.
RejectedByHook,
/// The automatic turn ended before its initial input was persisted.
TaskEndedBeforePersistence,
/// The initial input could not be durably written to the rollout.
PersistenceFailed,
}
/// Rejection returned when an extension asks to start automatic idle work but
@@ -233,6 +242,11 @@ impl CodexThread {
self.session.services.session_telemetry.clone()
}
/// Returns extension-owned data attached to this thread runtime.
pub fn thread_extension_data(&self) -> &codex_extension_api::ExtensionData {
&self.session.services.thread_extension_data
}
pub async fn shutdown_and_wait(&self) -> CodexResult<()> {
self.io.shutdown_and_wait().await
}
@@ -258,8 +272,8 @@ impl CodexThread {
}
}
pub async fn emit_thread_idle_lifecycle_if_idle(&self) {
self.session.emit_thread_idle_lifecycle_if_idle().await;
pub async fn emit_thread_idle_lifecycle_if_idle(&self, cause: ThreadIdleCause) {
self.session.emit_thread_idle_lifecycle_if_idle(cause).await;
}
#[doc(hidden)]
@@ -298,28 +312,79 @@ impl CodexThread {
.await
}
/// Waits until Core has actually started a turn or steered the active turn.
/// Waits until Core has started a turn or steered the active turn.
pub async fn submit_user_input_and_wait_for_admission(
&self,
op: Op,
trace: Option<W3cTraceContext>,
client_user_message_id: Option<String>,
) -> CodexResult<UserMessageAdmission> {
if !matches!(op, Op::UserInput { .. }) {
return Err(CodexErr::InvalidRequest(
"user message admission requires user input".to_string(),
self.submit_user_input_and_wait_for_admission_inner(
op,
trace,
client_user_message_id,
PendingUserMessageAdmissionState::Immediate,
)
.await
.map_err(Into::into)
}
/// Waits for durable admission and preserves its typed failure outcome.
///
/// A client user-message id is required to identify the persisted message.
pub async fn submit_user_input_and_wait_for_persisted_admission(
&self,
op: Op,
trace: Option<W3cTraceContext>,
client_user_message_id: Option<String>,
) -> Result<UserMessageAdmission, UserMessageAdmissionError> {
if client_user_message_id.is_none() {
return Err(UserMessageAdmissionError::Admission(
CodexErr::InvalidRequest(
"persisted user message admission requires a client user message id"
.to_string(),
),
));
}
self.submit_user_input_and_wait_for_admission_inner(
op,
trace,
client_user_message_id,
PendingUserMessageAdmissionState::WaitingForAdmission,
)
.await
}
async fn submit_user_input_and_wait_for_admission_inner(
&self,
op: Op,
trace: Option<W3cTraceContext>,
client_user_message_id: Option<String>,
state: PendingUserMessageAdmissionState,
) -> Result<UserMessageAdmission, UserMessageAdmissionError> {
let Op::UserInput { items, .. } = &op else {
return Err(UserMessageAdmissionError::Admission(
CodexErr::InvalidRequest("user message admission requires user input".to_string()),
));
};
if items.is_empty() {
return Err(UserMessageAdmissionError::Admission(
CodexErr::InvalidRequest(
"user message admission requires nonempty user input".to_string(),
),
));
}
self.session
.services
.agent_control
.ensure_execution_capacity_for_op(self.session.thread_id(), &op)
.await?;
.await
.map_err(UserMessageAdmissionError::Admission)?;
let submission_id = crate::session::new_submission_id();
let (_pending_admission, admission) = self
.session
.pending_user_message_admissions
.register(submission_id.clone());
.register(submission_id.clone(), client_user_message_id.clone(), state);
self.io
.submit_with_id(Submission {
id: submission_id.clone(),
@@ -328,11 +393,15 @@ impl CodexThread {
trace,
parent_turn_id: None,
})
.await?;
.await
.map_err(UserMessageAdmissionError::Admission)?;
tokio::select! {
biased;
result = admission => result.map_err(|_| CodexErr::InternalAgentDied)?,
() = self.io.session_loop_termination.clone() => Err(CodexErr::InternalAgentDied),
result = admission => result
.unwrap_or(Err(UserMessageAdmissionError::TaskEndedBeforePersistence)),
() = self.io.session_loop_termination.clone() => {
Err(UserMessageAdmissionError::TaskEndedBeforePersistence)
},
}
}