Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Extract per-thread enrichment into a dedicated async helper (#47083)

Ahmed Ibrahim429afbcaa97a
diff --git a/codex-rs/app-server/src/request_processors/thread_enrichment.rs b/codex-rs/app-server/src/request_processors/thread_enrichment.rs
index eede10a28b..a005185a8f 100644
--- a/codex-rs/app-server/src/request_processors/thread_enrichment.rs
+++ b/codex-rs/app-server/src/request_processors/thread_enrichment.rs
@@ -9,6 +9,7 @@ use codex_core::ThreadManager;
use codex_protocol::ThreadId;
use codex_protocol::protocol::AgentStatus;
use codex_protocol::protocol::SubAgentSource;
+use std::collections::HashMap;
pub(super) async fn enrich_loaded_threads<T>(
thread_manager: &ThreadManager,
@@ -25,59 +26,65 @@ pub(super) async fn enrich_loaded_threads<T>(
)
.await;
- futures::future::join_all(threads.iter_mut().map(as_thread).map(|thread| {
- let statuses = &statuses;
- async move {
- let watched_status = statuses.get(&thread.id);
- if let Some(status) = watched_status {
- thread.status = status.clone();
- }
+ futures::future::join_all(
+ threads
+ .iter_mut()
+ .map(as_thread)
+ .map(|thread| enrich_loaded_thread(thread_manager, &statuses, thread)),
+ )
+ .await;
+}
- if matches!(watched_status, Some(ThreadStatus::NotLoaded)) {
- return;
- }
+async fn enrich_loaded_thread(
+ thread_manager: &ThreadManager,
+ statuses: &HashMap<String, ThreadStatus>,
+ thread: &mut Thread,
+) {
+ let watched_status = statuses.get(&thread.id);
+ if let Some(status) = watched_status {
+ thread.status = status.clone();
+ }
- let Ok(thread_id) = ThreadId::from_string(&thread.id) else {
- return;
- };
- let Ok(loaded_thread) = thread_manager.get_thread(thread_id).await else {
- return;
- };
- let config_snapshot = loaded_thread.config_snapshot().await;
- apply_live_thread_settings(thread, &config_snapshot);
- if !matches!(
- &thread.source,
- SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. })
- ) {
- return;
+ if matches!(watched_status, Some(ThreadStatus::NotLoaded)) {
+ return;
+ }
+
+ let Ok(thread_id) = ThreadId::from_string(&thread.id) else {
+ return;
+ };
+ let Ok(loaded_thread) = thread_manager.get_thread(thread_id).await else {
+ return;
+ };
+ let config_snapshot = loaded_thread.config_snapshot().await;
+ apply_live_thread_settings(thread, &config_snapshot);
+ if !matches!(
+ &thread.source,
+ SessionSource::SubAgent(SubAgentSource::ThreadSpawn { .. })
+ ) {
+ return;
+ }
+ match loaded_thread.agent_status().await {
+ AgentStatus::Running => {
+ if watched_status.is_none() {
+ thread.status =
+ resolve_thread_status(ThreadStatus::Idle, /*has_in_progress_turn*/ true);
}
- match loaded_thread.agent_status().await {
- AgentStatus::Running => {
- if watched_status.is_none() {
- thread.status = resolve_thread_status(
- ThreadStatus::Idle,
- /*has_in_progress_turn*/ true,
- );
- }
- }
- AgentStatus::PendingInit | AgentStatus::Interrupted | AgentStatus::Completed(_) => {
- if watched_status.is_none() {
- thread.status = ThreadStatus::Idle;
- }
- }
- AgentStatus::Errored(_) => {
- thread.status = ThreadStatus::SystemError;
- }
- AgentStatus::Shutdown | AgentStatus::NotFound => {
- thread.status = ThreadStatus::NotLoaded;
- return;
- }
+ }
+ AgentStatus::PendingInit | AgentStatus::Interrupted | AgentStatus::Completed(_) => {
+ if watched_status.is_none() {
+ thread.status = ThreadStatus::Idle;
}
- thread.can_accept_direct_input = Some(can_accept_direct_input(
- loaded_thread.multi_agent_version(),
- &config_snapshot.session_source,
- ));
}
- }))
- .await;
+ AgentStatus::Errored(_) => {
+ thread.status = ThreadStatus::SystemError;
+ }
+ AgentStatus::Shutdown | AgentStatus::NotFound => {
+ thread.status = ThreadStatus::NotLoaded;
+ return;
+ }
+ }
+ thread.can_accept_direct_input = Some(can_accept_direct_input(
+ loaded_thread.multi_agent_version(),
+ &config_snapshot.session_source,
+ ));
}