Record thread recovery candidates on managed daemon shutdown (#44299)

## What changed

- Atomically save successfully persisted, loaded root thread IDs to `app-server-daemon/loaded-threads.json` during graceful managed shutdown, excluding ephemeral threads and threads pending unload.
- Clear stale recovery state at startup and around daemon lifecycle operations, including explicit stops and fresh starts.
- Probe support for `--managed-daemon` before launching managed app servers, and clear recovery state best-effort when launching older binaries.
- Keep forced shutdown responsive while saving the snapshot and log snapshot failures without preventing shutdown.

## Testing

Add coverage for snapshots after active turns finish, forced shutdown without a snapshot, excluded threads, snapshot write failures, and recovery-state cleanup for stops, fresh starts, and legacy launches.

GitOrigin-RevId: 422dc32b85355510f7e4c57b7540597b47d9f0b2
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-09 20:39:33 +00:00
1 parent 742472c525
commit 7c88f037d9
12 files changed
+398 -41

No files matched your search

@@ -1,5 +1,5 @@
//! Persists loaded persistent root threads before managed daemon shutdown.
//! Failures are logged so other threads can still be saved.
//! Selects persistent root threads for managed daemon recovery using normal resume semantics.
//! Only threads successfully persisted through the existing thread store become candidates.
use super::ThreadRequestProcessor;
use codex_thread_store::PersistContext;
@@ -10,7 +10,8 @@ impl ThreadRequestProcessor {
clippy::await_holding_invalid_type,
reason = "snapshot selection must be serialized against pending unloads"
)]
pub(crate) async fn persist_daemon_threads(&self) {
pub(crate) async fn daemon_recovery_candidates(&self) -> Vec<String> {
let mut ids = Vec::new();
for thread_id in self.thread_manager.list_thread_ids().await {
let pending_unloads = self.pending_thread_unloads.lock().await;
if pending_unloads.contains(&thread_id) {
@@ -23,13 +24,18 @@ impl ThreadRequestProcessor {
if !config.ephemeral
&& config.parent_thread_id.is_none()
&& !config.session_source.is_non_root_agent()
&& let Err(err) = self
{
if let Err(err) = self
.thread_store
.persist_thread(thread_id, PersistContext::Standard)
.await
{
warn!(%thread_id, %err, "failed to persist thread during daemon shutdown");
{
warn!(%thread_id, %err, "skipping daemon restore for thread that could not be persisted");
continue;
}
ids.push(thread_id.to_string());
}
}
ids
}
}