Add coordinated shutdown for local agent trees (#49814)

## What changed

Expose `ThreadManager::request_agent_tree_shutdown` and an `AgentTreeShutdown::wait` handle. Requests reject new starts in the shared runtime, cancel pending startup, and signal every session in that tree, including delegates outside the thread registry. Waiting covers admitted operations and session teardown, and reports cleanup or persistence writer failures. Other root trees remain unaffected; registry removal and persistence handoff remain caller responsibilities.

Retain tree membership through startup, cancelled spawn cleanup, residency eviction, and completion watchers. Release persistence resources before returning startup failures, and stop completion watchers during tree shutdown without reporting shutdown as a child result.

## Testing

Add coverage for tree isolation, children removed from the registry, delegate teardown, cancellation during required MCP initialization, aborted teardown, persistence writer failures, and successful retry after a failed resume releases its writer.

GitOrigin-RevId: a109e25adf23d20baf7595891099947050c6b6b5
This commit is contained in:
Owen Lin
2026-10-01 01:57:56 +00:00
committed by copyberry
parent d4a475adda
commit 8e44ad94b0
22 changed files with 795 additions and 51 deletions
+47 -6
View File
@@ -1,5 +1,8 @@
mod managed;
mod shared_instructions;
mod shutdown;
pub use shutdown::AgentTreeShutdown;
use crate::CodexAppsToolsCache;
use crate::agent::LocalAgentControl;
@@ -23,6 +26,8 @@ use crate::session::SessionIo;
use crate::session::SessionSpawnArgs;
use crate::session::resolve_multi_agent_version;
use crate::session::session::Session;
use crate::session::startup::SessionStartup;
use crate::session::startup::SessionStartupGuard;
use crate::tasks::InterruptedTurnHistoryMarker;
use crate::tasks::interrupted_turn_history_marker;
use crate::thread_startup_metadata::ThreadStartupMetadata;
@@ -2038,7 +2043,18 @@ impl ThreadManagerState {
}
/// Spawn a new thread with optional history and register it with the manager.
async fn spawn_thread(&self, request: ThreadSpawnRequest) -> CodexResult<NewThread> {
async fn spawn_thread(&self, mut request: ThreadSpawnRequest) -> CodexResult<NewThread> {
let runtime = request.agent_control.runtime().clone();
let membership = runtime.admit_start()?;
let owns_startup = request.startup.is_none();
let startup_state = Arc::clone(
request
.startup
.get_or_insert_with(|| Arc::new(SessionStartup::default())),
);
startup_state.hold_membership(membership);
let startup_guard =
owns_startup.then(|| SessionStartupGuard::new(Arc::clone(&startup_state)));
let ThreadSpawnRequest {
startup,
options,
@@ -2133,10 +2149,13 @@ impl ThreadManagerState {
resumed.conversation_id
)));
}
drop(threads);
let session_configured = thread
.startup_metadata()
.to_session_configured_event(initial_history.get_event_msgs());
startup_state.release_membership();
if let Some(startup_guard) = startup_guard {
startup_guard.disarm();
}
return Ok(NewThread {
thread_id: resumed.conversation_id,
session_configured,
@@ -2268,7 +2287,7 @@ impl ThreadManagerState {
};
let attachment_source =
forked_from_thread_id.filter(|_| matches!(&initial_history, InitialHistory::Forked(_)));
let (session, io) = Session::spawn(SessionSpawnArgs {
let spawn_result = Session::spawn(SessionSpawnArgs {
startup,
config,
allow_provider_model_fallback,
@@ -2329,7 +2348,16 @@ impl ThreadManagerState {
},
windows_sandbox_proxy_settings_mode,
})
.await?;
.await;
let (session, io) = match spawn_result {
Ok(spawned) => spawned,
Err(error) => {
if let Some(startup_guard) = startup_guard {
startup_guard.cleanup().await;
}
return Err(error);
}
};
if let Some(source_thread_id) = attachment_source
&& session.live_thread().is_some()
&& self.thread_store.supports_thread_attachments()
@@ -2356,9 +2384,18 @@ impl ThreadManagerState {
{
session.services.mcp_runtime.enable_full_access_form_input();
}
let new_thread = self
let finalize_result = self
.finalize_thread_spawn(session, io, tracked_session_source, initial_host_config)
.await?;
.await;
let new_thread = match finalize_result {
Ok(new_thread) => new_thread,
Err(error) => {
if let Some(startup_guard) = startup_guard {
startup_guard.cleanup().await;
}
return Err(error);
}
};
new_thread.thread.emit_thread_ready_lifecycle().await;
if source_changed_during_startup.load(Ordering::Acquire) {
new_thread.thread.session.request_mcp_runtime_refresh();
@@ -2366,6 +2403,10 @@ impl ThreadManagerState {
if is_resumed_thread {
new_thread.thread.emit_thread_resume_lifecycle().await;
}
startup_state.release_membership();
if let Some(startup_guard) = startup_guard {
startup_guard.disarm();
}
Ok(new_thread)
}