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:
jif
2026-10-08 09:39:33 +00:00
committed by copyberry
parent ea27864f99
commit d2a6dac20b
12 changed files with 493 additions and 124 deletions
+26
View File
@@ -501,6 +501,32 @@ impl ThreadStateManager {
}
}
/// Removes an unsubscribed listener's entry without clearing a replacement's state.
#[expect(
clippy::await_holding_invalid_type,
reason = "the listener generation must stay fixed while removing its map entry"
)]
pub(crate) async fn remove_unsubscribed_listener(
&self,
thread_id: ThreadId,
expected: &Arc<Mutex<ThreadState>>,
generation: u64,
) -> bool {
let mut thread_state = expected.lock().await;
let mut state = self.state.lock().await;
if thread_state.listener_generation != generation
|| !state.threads.get(&thread_id).is_some_and(|entry| {
Arc::ptr_eq(&entry.state, expected) && entry.connection_ids.is_empty()
})
{
return false;
}
state.threads.remove(&thread_id);
self.unregister_listener_command_tx(thread_id);
thread_state.clear_listener();
true
}
pub(crate) async fn clear_all_listeners(&self) {
let thread_states = {
let state = self.state.lock().await;