mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Preserve agent state when unloading idle multi-agent v2 children (#52034)
## What changed - Route app-server idle unloading of multi-agent v2 children through local eviction that preserves unread mail and environment selections for reload. - Hold residency pins through input submission so newly reloaded children cannot be evicted before receiving their work. - Guard listener cleanup against replacement runtimes and subscriptions, and reject local eviction of host-controlled threads. ## Testing Add coverage for unsubscribe-driven eviction followed by `followup_task`, verifying restored conversation context and exactly-once delivery of queued mail. Extend tests to check that residency pins block eviction and that rejected host eviction preserves the thread and mailbox. GitOrigin-RevId: e36723730af683d2081e495e637f0e6d23548b8d
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
use super::*;
|
||||
use crate::extensions::send_thread_warning;
|
||||
use codex_app_server_protocol::ThreadQueueChangedNotification;
|
||||
use codex_core::ThreadEvictionOutcome;
|
||||
use codex_extension_api::ThreadIdleCause;
|
||||
use codex_protocol::config_types::MultiAgentMode;
|
||||
use codex_protocol::protocol::MultiAgentVersion;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct ListenerTaskContext {
|
||||
@@ -93,6 +96,7 @@ impl UnloadingState {
|
||||
if let Some(target) = unloading_target
|
||||
&& target <= Instant::now()
|
||||
{
|
||||
tokio::task::yield_now().await;
|
||||
return true;
|
||||
}
|
||||
let unloading_sleep = async {
|
||||
@@ -222,6 +226,10 @@ pub(super) fn log_listener_attach_result(
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "listener removal must be serialized against new subscriptions"
|
||||
)]
|
||||
pub(super) async fn ensure_listener_task_running(
|
||||
listener_task_context: ListenerTaskContext,
|
||||
conversation_id: ThreadId,
|
||||
@@ -251,6 +259,8 @@ pub(super) async fn ensure_listener_task_running(
|
||||
)
|
||||
.await;
|
||||
let config_snapshot = conversation.config_snapshot().await;
|
||||
let is_v2_child = conversation.multi_agent_version() == Some(MultiAgentVersion::V2)
|
||||
&& matches!(config_snapshot.session_source, SessionSource::SubAgent(_));
|
||||
let thread_settings_baseline = thread_settings_from_config_snapshot(&config_snapshot);
|
||||
let (mut listener_command_rx, listener_generation) = {
|
||||
let mut thread_state = thread_state.lock().await;
|
||||
@@ -392,6 +402,60 @@ pub(super) async fn ensure_listener_task_running(
|
||||
}
|
||||
pending_thread_unloads.insert(conversation_id);
|
||||
}
|
||||
if is_v2_child {
|
||||
let eviction = thread_manager.try_evict_v2_thread(Arc::clone(&conversation));
|
||||
tokio::pin!(eviction);
|
||||
let result = match tokio::time::timeout(
|
||||
Duration::from_secs(/*secs*/ 10), &mut eviction,
|
||||
).await {
|
||||
Ok(result) => result,
|
||||
Err(_) => {
|
||||
warn!(event.name = "codex.app_server.thread_shutdown_slow", "thread {conversation_id} shutdown is taking longer than expected; continuing to wait");
|
||||
eviction.await
|
||||
}
|
||||
};
|
||||
let outcome = match result {
|
||||
Ok(ThreadEvictionOutcome::Busy) => {
|
||||
pending_thread_unloads.lock().await.remove(&conversation_id);
|
||||
unloading_state.note_thread_activity_observed();
|
||||
continue;
|
||||
}
|
||||
Ok(outcome @ (ThreadEvictionOutcome::Evicted | ThreadEvictionOutcome::NotCurrent)) => outcome,
|
||||
Err(err) => {
|
||||
warn!("failed to evict thread {conversation_id}: {err}");
|
||||
pending_thread_unloads.lock().await.remove(&conversation_id);
|
||||
unloading_state.note_thread_activity_observed();
|
||||
continue;
|
||||
}
|
||||
};
|
||||
{
|
||||
let mut pending = pending_thread_unloads.lock().await;
|
||||
if !thread_state_manager.remove_unsubscribed_listener(
|
||||
conversation_id, &thread_state, listener_generation,
|
||||
).await {
|
||||
pending.remove(&conversation_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Release the status receiver before removing its watch entry.
|
||||
drop(unloading_state);
|
||||
outgoing_for_task
|
||||
.cancel_requests_for_thread(conversation_id, /*error*/ None)
|
||||
.await;
|
||||
// A runtime removed by someone else may already have a replacement.
|
||||
if outcome == ThreadEvictionOutcome::Evicted {
|
||||
thread_watch_manager
|
||||
.remove_thread(&conversation_id.to_string())
|
||||
.await;
|
||||
outgoing_for_task
|
||||
.send_server_notification(ServerNotification::ThreadClosed(
|
||||
ThreadClosedNotification { thread_id: conversation_id.to_string() },
|
||||
))
|
||||
.await;
|
||||
}
|
||||
pending_thread_unloads.lock().await.remove(&conversation_id);
|
||||
return;
|
||||
}
|
||||
unload_thread_without_subscribers(
|
||||
thread_manager.clone(),
|
||||
outgoing_for_task.clone(),
|
||||
|
||||
Reference in New Issue
Block a user