Persist loaded threads before managed daemon shutdown (#44283)

## Why

Loaded threads, including idle threads whose rollout files are still deferred, need to survive a managed daemon restart. Shutdown must also remain forceable when rollout I/O is blocked.

## What changed

- Add a hidden `--managed-daemon` option for app-server Unix socket transports. After active turns and admitted requests drain, save loaded persistent root threads before exiting.
- Skip ephemeral threads, non-root agent threads, and threads pending unload. Log persistence failures and continue saving other threads.
- Apply shutdown admission checks to thread and turn settings updates, thread deletion, and archiving.
- Keep force signals and daemon shutdown requests responsive during persistence. Return `AppServerExit::Forced` so executables can exit without waiting for runtime teardown.

## Testing

Add integration coverage for resuming active and idle threads after restart, forcing shutdown during active work, and forcing shutdown with a blocked rollout writer. Extend shutdown rejection coverage to deletion and settings updates.

GitOrigin-RevId: 4344e97d39f9f80c5d84c41c17300fd6aa99b4cc
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-09 19:22:22 +00:00
1 parent fcd90d8f07
commit c1840dc55e
12 files changed
+465 -16

No files matched your search

@@ -0,0 +1,35 @@
//! Persists loaded persistent root threads before managed daemon shutdown.
//! Failures are logged so other threads can still be saved.
use super::ThreadRequestProcessor;
use codex_thread_store::PersistContext;
use tracing::warn;
impl ThreadRequestProcessor {
#[expect(
clippy::await_holding_invalid_type,
reason = "snapshot selection must be serialized against pending unloads"
)]
pub(crate) async fn persist_daemon_threads(&self) {
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) {
continue;
}
let Ok(thread) = self.thread_manager.get_thread(thread_id).await else {
continue;
};
let config = thread.config_snapshot().await;
if !config.ephemeral
&& config.parent_thread_id.is_none()
&& !config.session_source.is_non_root_agent()
&& let Err(err) = self
.thread_store
.persist_thread(thread_id, PersistContext::Standard)
.await
{
warn!(%thread_id, %err, "failed to persist thread during daemon shutdown");
}
}
}
}
@@ -1,3 +1,6 @@
#[path = "daemon_snapshot.rs"]
mod daemon_snapshot;
use super::persisted_resume_settings::PersistedResumeSettings;
use super::persisted_resume_settings::latest_persisted_resume_settings;
use super::thread_enrichment::enrich_loaded_threads;