Add graceful agent-tree suspension and recovery (#61198)

## Why

Suspending a locally controlled agent tree must preserve unfinished turns and child results that arrive during teardown so they remain available after recovery.

## What changed

- Add `ThreadManager::suspend_agent_tree` to stop durable trees, close their writers, and return unfinished root and named V2 child turn IDs plus any pending root wake.
- Persist terminal child results before suspension succeeds, including results queued for evicted parents. Report failed or unconfirmed writes.
- Add `load_suspended_agent` to restore named V2 descendants through their recorded ancestors without starting model turns.
- Allow empty `continue_turn_if_idle` input to wake on saved results, guarded by the preceding turn ID. Keep mail queued until task registration so suspension can preserve it.

## Testing

Add coverage for completion and wake races, child errors, parent writer failures, evicted parents, descendant restoration, repeated suspension after caller cancellation, and ephemeral roots remaining active.

GitOrigin-RevId: 3041791f51bff2555210f6b2d2443f9f05f68330
This commit is contained in:
Bryan Ashley
2026-11-12 22:43:14 +00:00
committed by copyberry
parent cdfcc18dba
commit fae87361f4
30 changed files with 1480 additions and 320 deletions
+49 -10
View File
@@ -269,15 +269,25 @@ impl InputQueue {
pending.drain(..).collect()
}
pub(crate) async fn drain_mailbox_input_items(&self) -> (Vec<TurnInput>, TurnStartOptions) {
let pending_mails = self.drain_mailbox().await;
pub(crate) async fn pending_wake_options(&self) -> Option<TurnStartOptions> {
let mut mails = self.mailbox_pending_mails.lock().await;
self.read_mailbox(&mut mails);
let mut options = Self::trigger_turn_options(&mails)?;
if let Some(mail) = mails.iter().find(|mail| mail.communication.trigger_turn) {
options.initiating_agent_path = Some(mail.communication.author.clone());
}
Some(options)
}
fn trigger_turn_options(
pending_mails: &VecDeque<PendingMailboxCommunication>,
) -> Option<TurnStartOptions> {
// A later follow-up supersedes the earlier choice, including an omitted choice.
let mut start_options = pending_mails
.iter()
.rev()
.find(|mail| mail.communication.trigger_turn)
.map(|mail| mail.start_options.clone())
.unwrap_or_default();
.map(|mail| mail.start_options.clone())?;
start_options.parent_turn_id = pending_mails
.iter()
.filter(|mail| mail.communication.trigger_turn)
@@ -296,8 +306,15 @@ impl InputQueue {
.filter(|id| !id.trim().is_empty())
})
.map(str::to_string);
Some(start_options)
}
pub(crate) async fn drain_mailbox_input_items(&self) -> (Vec<TurnInput>, TurnStartOptions) {
let mut pending_mails = self.mailbox_pending_mails.lock().await;
self.read_mailbox(&mut pending_mails);
let start_options = Self::trigger_turn_options(&pending_mails).unwrap_or_default();
let items = pending_mails
.into_iter()
.drain(..)
.map(|mail| TurnInput::InterAgentCommunication(mail.communication))
.collect();
(items, start_options)
@@ -343,12 +360,34 @@ impl InputQueue {
/// Clear any pending waiters and input buffered for the current turn.
pub(crate) async fn clear_pending(&self, active_turn: &ActiveTurn) {
let mut turn_state = active_turn.turn_state.lock().await;
turn_state.clear_pending_waiters();
// Pending mail can own extension attachments whose destructors need the lock released.
let pending_input = std::mem::take(&mut turn_state.pending_input);
drop(turn_state);
drop(pending_input);
let pending_input = {
let mut turn_state = active_turn.turn_state.lock().await;
turn_state.clear_pending_waiters();
std::mem::take(&mut turn_state.pending_input)
};
// A terminal result still in the queue has no writer yet. Keep it available
// to the next turn or to the source writer during a graceful drain.
let results: Vec<_> = pending_input
.items
.into_iter()
.filter_map(|input| match input {
TurnInput::InterAgentCommunication(message) if message.is_terminal_result() => {
Some(message)
}
_ => None,
})
.collect();
if !results.is_empty() {
let mut pending = self.mailbox_pending_mails.lock().await;
for communication in results.into_iter().rev() {
pending.push_front(PendingMailboxCommunication {
communication,
start_options: TurnStartOptions::default(),
_diagnostics_guard: PENDING_MAILBOX_MESSAGES.track(),
});
}
}
}
pub(crate) async fn defer_mailbox_delivery_to_next_turn(