mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Introduce a request type for enqueueing thread messages (#65297)
## What changed Use `EnqueueRequest` to pass the thread ID, turn input, and optional Responses API client metadata through a single `enqueue_item` method. Migrate the app server and queue service tests to this method, and deprecate `enqueue` and `enqueue_with_metadata` while keeping them as forwarding wrappers. GitOrigin-RevId: 9a4e96ca5e7f30b0b6ac7f33ee6c10420df12dba
This commit is contained in:
committed by
copyberry
parent
01192c6e19
commit
4e2490ec82
@@ -33,6 +33,7 @@ use codex_core::TurnInput;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
use codex_queue_extension::EnqueueRequest;
|
||||
use codex_queue_extension::QueueServiceError;
|
||||
use codex_queue_extension::QueuedItem;
|
||||
use codex_queue_extension::QueuedItemService;
|
||||
@@ -86,14 +87,16 @@ impl ThreadQueueRequestProcessor {
|
||||
ensure_direct_input_allowed(loaded_thread.as_deref(), &source)?;
|
||||
let queued_item = self
|
||||
.service()?
|
||||
.enqueue_with_metadata(
|
||||
thread_id,
|
||||
submission_into_turn_input(
|
||||
params.input,
|
||||
Some(params.client_user_message_id),
|
||||
&cwd,
|
||||
)?,
|
||||
params.responsesapi_client_metadata,
|
||||
.enqueue_item(
|
||||
EnqueueRequest::new(
|
||||
thread_id,
|
||||
submission_into_turn_input(
|
||||
params.input,
|
||||
Some(params.client_user_message_id),
|
||||
&cwd,
|
||||
)?,
|
||||
)
|
||||
.with_responses_metadata(params.responsesapi_client_metadata),
|
||||
)
|
||||
.await
|
||||
.map_err(queue_error)?;
|
||||
|
||||
@@ -6,6 +6,7 @@ use codex_extension_api::ExtensionRegistryBuilder;
|
||||
|
||||
mod service;
|
||||
|
||||
pub use service::EnqueueRequest;
|
||||
pub use service::QueueServiceError;
|
||||
pub use service::QueuedItem;
|
||||
pub use service::QueuedItemService;
|
||||
|
||||
@@ -37,6 +37,33 @@ use tokio::sync::OwnedMutexGuard;
|
||||
use tokio::sync::broadcast::error::TryRecvError;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Input and caller options for adding a user message to the queue.
|
||||
#[derive(Debug)]
|
||||
pub struct EnqueueRequest {
|
||||
thread_id: ThreadId,
|
||||
input: TurnInput,
|
||||
responsesapi_client_metadata: Option<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
impl EnqueueRequest {
|
||||
pub fn new(thread_id: ThreadId, input: TurnInput) -> Self {
|
||||
Self {
|
||||
thread_id,
|
||||
input,
|
||||
responsesapi_client_metadata: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Carries Responses client metadata through to the turn started by this message.
|
||||
pub fn with_responses_metadata(
|
||||
mut self,
|
||||
responsesapi_client_metadata: Option<HashMap<String, String>>,
|
||||
) -> Self {
|
||||
self.responsesapi_client_metadata = responsesapi_client_metadata;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// One user message waiting to start on its thread.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct QueuedItem {
|
||||
@@ -263,21 +290,15 @@ impl QueuedItemService {
|
||||
self.dispatch_lock(thread_id).lock_owned().await
|
||||
}
|
||||
|
||||
pub async fn enqueue(
|
||||
pub async fn enqueue_item(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
input: TurnInput,
|
||||
) -> Result<QueuedItem, QueueServiceError> {
|
||||
self.enqueue_with_metadata(thread_id, input, /*responsesapi_client_metadata*/ None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn enqueue_with_metadata(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
input: TurnInput,
|
||||
responsesapi_client_metadata: Option<HashMap<String, String>>,
|
||||
request: EnqueueRequest,
|
||||
) -> Result<QueuedItem, QueueServiceError> {
|
||||
let EnqueueRequest {
|
||||
thread_id,
|
||||
input,
|
||||
responsesapi_client_metadata,
|
||||
} = request;
|
||||
let input = prepare_queued_user_input(input).await?;
|
||||
let payload = queued_input_payload(&input, responsesapi_client_metadata.as_ref())?;
|
||||
let item = {
|
||||
@@ -290,6 +311,30 @@ impl QueuedItemService {
|
||||
Ok(item)
|
||||
}
|
||||
|
||||
#[deprecated(note = "use enqueue_item with EnqueueRequest::new")]
|
||||
pub async fn enqueue(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
input: TurnInput,
|
||||
) -> Result<QueuedItem, QueueServiceError> {
|
||||
self.enqueue_item(EnqueueRequest::new(thread_id, input))
|
||||
.await
|
||||
}
|
||||
|
||||
#[deprecated(note = "use enqueue_item with EnqueueRequest::with_responses_metadata")]
|
||||
pub async fn enqueue_with_metadata(
|
||||
&self,
|
||||
thread_id: ThreadId,
|
||||
input: TurnInput,
|
||||
responsesapi_client_metadata: Option<HashMap<String, String>>,
|
||||
) -> Result<QueuedItem, QueueServiceError> {
|
||||
self.enqueue_item(
|
||||
EnqueueRequest::new(thread_id, input)
|
||||
.with_responses_metadata(responsesapi_client_metadata),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list(&self, thread_id: ThreadId) -> Result<Vec<QueuedItem>, QueueServiceError> {
|
||||
self.list_page(thread_id, /*offset*/ 0, MAX_QUEUE_ITEMS)
|
||||
.await
|
||||
|
||||
@@ -37,6 +37,7 @@ use codex_protocol::protocol::Event;
|
||||
use codex_protocol::protocol::EventMsg;
|
||||
use codex_protocol::user_input::MAX_USER_INPUT_TEXT_CHARS;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use codex_queue_extension::EnqueueRequest;
|
||||
use codex_queue_extension::QueueServiceError;
|
||||
use codex_queue_extension::QueuedItemService;
|
||||
use codex_state::SqliteConfig;
|
||||
@@ -289,7 +290,10 @@ async fn drain_leaves_persisted_queued_message_for_a_later_start() -> anyhow::Re
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
);
|
||||
let item = staged
|
||||
.enqueue(thread_id, user_input("queued after drain"))
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
thread_id,
|
||||
user_input("queued after drain"),
|
||||
))
|
||||
.await?;
|
||||
let service = QueuedItemService::new(
|
||||
Arc::clone(&queue),
|
||||
@@ -322,13 +326,10 @@ async fn queued_input_and_unique_event_ids_round_trip() -> anyhow::Result<()> {
|
||||
let input = structured_user_input("structured message");
|
||||
|
||||
let first = service
|
||||
.enqueue_with_metadata(
|
||||
thread_id,
|
||||
input.clone(),
|
||||
Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"ios_app".to_string(),
|
||||
)])),
|
||||
.enqueue_item(
|
||||
EnqueueRequest::new(thread_id, input.clone()).with_responses_metadata(Some(
|
||||
HashMap::from([("client_type".to_string(), "ios_app".to_string())]),
|
||||
)),
|
||||
)
|
||||
.await?;
|
||||
// Old workers still read TurnInput directly from the stored payload.
|
||||
@@ -339,7 +340,9 @@ async fn queued_input_and_unique_event_ids_round_trip() -> anyhow::Result<()> {
|
||||
input,
|
||||
serde_json::from_str::<TurnInput>(&records[0].payload)?
|
||||
);
|
||||
let second = service.enqueue(thread_id, user_input("next")).await?;
|
||||
let second = service
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("next")))
|
||||
.await?;
|
||||
let TurnInput::UserInput {
|
||||
client_id: Some(generated_client_id),
|
||||
..
|
||||
@@ -380,16 +383,18 @@ async fn editing_reordering_and_deleting_preserve_queue_identity() -> anyhow::Re
|
||||
let service = QueuedItemService::new(queue, Weak::new(), Arc::new(NoopExtensionEventSink));
|
||||
let thread_id = ThreadId::new();
|
||||
let first = service
|
||||
.enqueue_with_metadata(
|
||||
thread_id,
|
||||
structured_user_input("first"),
|
||||
Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"desktop_app".to_string(),
|
||||
)])),
|
||||
.enqueue_item(
|
||||
EnqueueRequest::new(thread_id, structured_user_input("first")).with_responses_metadata(
|
||||
Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"desktop_app".to_string(),
|
||||
)])),
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
let second = service.enqueue(thread_id, user_input("second")).await?;
|
||||
let second = service
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("second")))
|
||||
.await?;
|
||||
let edited = service
|
||||
.update(thread_id, first.id.clone(), user_input("edited"))
|
||||
.await?
|
||||
@@ -441,15 +446,16 @@ async fn starting_a_selected_item_preserves_the_remaining_queue() -> anyhow::Res
|
||||
let thread_id = test.session_configured.thread_id;
|
||||
let queue = loaded_thread_queue(&test)?;
|
||||
let service = QueuedItemService::new(queue, Weak::new(), Arc::new(NoopExtensionEventSink));
|
||||
let first = service.enqueue(thread_id, user_input("first")).await?;
|
||||
let first = service
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("first")))
|
||||
.await?;
|
||||
let second = service
|
||||
.enqueue_with_metadata(
|
||||
thread_id,
|
||||
structured_user_input("second"),
|
||||
Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"ios_app".to_string(),
|
||||
)])),
|
||||
.enqueue_item(
|
||||
EnqueueRequest::new(thread_id, structured_user_input("second"))
|
||||
.with_responses_metadata(Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"ios_app".to_string(),
|
||||
)]))),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -511,7 +517,7 @@ async fn starting_a_selected_item_while_active_leaves_it_queued() -> anyhow::Res
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
);
|
||||
let queued = service
|
||||
.enqueue(thread_id, user_input("stay queued"))
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("stay queued")))
|
||||
.await?;
|
||||
|
||||
let active_turn = test
|
||||
@@ -568,7 +574,10 @@ async fn interrupted_turns_pause_queued_messages_but_failed_turns_drain_them() -
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
);
|
||||
let queued = service
|
||||
.enqueue(thread_id, user_input("continue after failure"))
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
thread_id,
|
||||
user_input("continue after failure"),
|
||||
))
|
||||
.await?;
|
||||
|
||||
emit_idle_with_cause(&service, thread_id, ThreadIdleCause::Interrupted).await;
|
||||
@@ -615,7 +624,9 @@ async fn registered_queue_lifecycle_starts_messages_in_fifo_order() -> anyhow::R
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
);
|
||||
for prompt in ["B", "C"] {
|
||||
staging.enqueue(thread_id, user_input(prompt)).await?;
|
||||
staging
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input(prompt)))
|
||||
.await?;
|
||||
}
|
||||
let queue = install_registered_queue(&test, installed.as_ref())?;
|
||||
|
||||
@@ -720,7 +731,10 @@ async fn externally_changed_queues_dispatch_independently_and_retry_failed_wakes
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(thread_id);
|
||||
let first = external_queue
|
||||
.enqueue(thread_id, user_input("written by another process"))
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
thread_id,
|
||||
user_input("written by another process"),
|
||||
))
|
||||
.await?;
|
||||
let updated = queue
|
||||
.update(
|
||||
@@ -731,10 +745,10 @@ async fn externally_changed_queues_dispatch_independently_and_retry_failed_wakes
|
||||
.await?
|
||||
.context("external queue item disappeared")?;
|
||||
external_queue
|
||||
.enqueue(
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
independent_thread.thread_id,
|
||||
user_input("independent thread"),
|
||||
)
|
||||
))
|
||||
.await?;
|
||||
|
||||
advance_queue_poll().await;
|
||||
@@ -762,7 +776,10 @@ async fn externally_changed_queues_dispatch_independently_and_retry_failed_wakes
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
test.thread_manager.remove_thread(&thread_id).await;
|
||||
external_queue
|
||||
.enqueue(thread_id, user_input("queued before ordinary resume"))
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
thread_id,
|
||||
user_input("queued before ordinary resume"),
|
||||
))
|
||||
.await?;
|
||||
advance_queue_poll().await;
|
||||
let resumed = test
|
||||
@@ -828,7 +845,9 @@ async fn rejected_queue_messages_are_consumed_without_retrying_or_blocking_follo
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
);
|
||||
for prompt in ["blocked", "C"] {
|
||||
staging.enqueue(thread_id, user_input(prompt)).await?;
|
||||
staging
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input(prompt)))
|
||||
.await?;
|
||||
}
|
||||
let queue = install_registered_queue(&test, installed.as_ref())?;
|
||||
|
||||
@@ -879,7 +898,9 @@ async fn explicitly_started_rejected_queue_messages_are_consumed() -> anyhow::Re
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
);
|
||||
|
||||
let rejected = queue.enqueue(thread_id, user_input("blocked")).await?;
|
||||
let rejected = queue
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("blocked")))
|
||||
.await?;
|
||||
let submission = tokio::time::timeout(
|
||||
Duration::from_secs(10),
|
||||
queue.start(test.codex.as_ref(), Some(rejected.id), /*trace*/ None),
|
||||
@@ -933,7 +954,9 @@ async fn local_attachments_are_snapshotted_before_enqueue_and_update() -> anyhow
|
||||
},
|
||||
);
|
||||
|
||||
let queued = service.enqueue(thread_id, queued_input).await?;
|
||||
let queued = service
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, queued_input))
|
||||
.await?;
|
||||
std::fs::remove_file(&image_path)?;
|
||||
std::fs::remove_file(&audio_path)?;
|
||||
|
||||
@@ -1003,7 +1026,9 @@ async fn invalid_local_attachments_do_not_mutate_queue() -> anyhow::Result<()> {
|
||||
let (queue, home) = test_queue().await?;
|
||||
let service = QueuedItemService::new(queue, Weak::new(), Arc::new(NoopExtensionEventSink));
|
||||
let thread_id = ThreadId::new();
|
||||
let existing = service.enqueue(thread_id, user_input("existing")).await?;
|
||||
let existing = service
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("existing")))
|
||||
.await?;
|
||||
let missing_path = home.path().join("missing.png");
|
||||
let invalid_input = user_input_with_media(
|
||||
"invalid attachments",
|
||||
@@ -1020,7 +1045,7 @@ async fn invalid_local_attachments_do_not_mutate_queue() -> anyhow::Result<()> {
|
||||
|
||||
assert!(matches!(
|
||||
service
|
||||
.enqueue(thread_id, invalid_input.clone())
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, invalid_input.clone()))
|
||||
.await,
|
||||
Err(QueueServiceError::InvalidAttachment(error))
|
||||
if error.kind() == std::io::ErrorKind::NotFound
|
||||
@@ -1046,10 +1071,10 @@ async fn non_user_input_cannot_enter_the_user_message_queue() -> anyhow::Result<
|
||||
let (queue, _home) = test_queue().await?;
|
||||
let service = QueuedItemService::new(queue, Weak::new(), Arc::new(NoopExtensionEventSink));
|
||||
let error = service
|
||||
.enqueue(
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
ThreadId::new(),
|
||||
TurnInput::ResponseItem(ResponseItem::Other),
|
||||
)
|
||||
))
|
||||
.await
|
||||
.expect_err("response item should not enter the user queue");
|
||||
assert!(matches!(error, QueueServiceError::InvalidInput));
|
||||
@@ -1061,7 +1086,9 @@ async fn queued_text_limit_is_enforced_across_all_input_items() -> anyhow::Resul
|
||||
let (queue, _home) = test_queue().await?;
|
||||
let service = QueuedItemService::new(queue, Weak::new(), Arc::new(NoopExtensionEventSink));
|
||||
let thread_id = ThreadId::new();
|
||||
let existing = service.enqueue(thread_id, user_input("existing")).await?;
|
||||
let existing = service
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, user_input("existing")))
|
||||
.await?;
|
||||
let mut oversized = user_input(&"x".repeat(MAX_USER_INPUT_TEXT_CHARS / 2));
|
||||
if let TurnInput::UserInput { content, .. } = &mut oversized {
|
||||
content.push(UserInput::Text {
|
||||
@@ -1072,7 +1099,7 @@ async fn queued_text_limit_is_enforced_across_all_input_items() -> anyhow::Resul
|
||||
|
||||
assert!(matches!(
|
||||
service
|
||||
.enqueue(thread_id, oversized.clone())
|
||||
.enqueue_item(EnqueueRequest::new(thread_id, oversized.clone()))
|
||||
.await,
|
||||
Err(QueueServiceError::InputTooLarge { actual_chars })
|
||||
if actual_chars == MAX_USER_INPUT_TEXT_CHARS + 1
|
||||
@@ -1105,7 +1132,9 @@ async fn queued_text_limit_counts_characters_and_ignores_non_text_items() -> any
|
||||
});
|
||||
}
|
||||
|
||||
let queued = service.enqueue(ThreadId::new(), input.clone()).await?;
|
||||
let queued = service
|
||||
.enqueue_item(EnqueueRequest::new(ThreadId::new(), input.clone()))
|
||||
.await?;
|
||||
assert_eq!(input, queued.input);
|
||||
|
||||
Ok(())
|
||||
@@ -1132,7 +1161,10 @@ async fn invalid_head_is_skipped_and_a_live_user_turn_is_accepted() -> anyhow::R
|
||||
);
|
||||
|
||||
service
|
||||
.enqueue(thread_id, structured_user_input("durable follow-up"))
|
||||
.enqueue_item(EnqueueRequest::new(
|
||||
thread_id,
|
||||
structured_user_input("durable follow-up"),
|
||||
))
|
||||
.await?;
|
||||
emit_idle(&service, thread_id).await;
|
||||
let client_id = wait_for_event_match(test.codex.as_ref(), |event| match event {
|
||||
@@ -1171,13 +1203,12 @@ async fn resumed_idle_dispatches_input_without_a_loaded_manager() -> anyhow::Res
|
||||
Weak::new(),
|
||||
Arc::new(NoopExtensionEventSink),
|
||||
)
|
||||
.enqueue_with_metadata(
|
||||
thread_id,
|
||||
user_input("queued while unloaded"),
|
||||
Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"ios_app".to_string(),
|
||||
)])),
|
||||
.enqueue_item(
|
||||
EnqueueRequest::new(thread_id, user_input("queued while unloaded"))
|
||||
.with_responses_metadata(Some(HashMap::from([(
|
||||
"client_type".to_string(),
|
||||
"ios_app".to_string(),
|
||||
)]))),
|
||||
)
|
||||
.await?;
|
||||
let service = QueuedItemService::new(
|
||||
|
||||
Reference in New Issue
Block a user