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

@@ -0,0 +1,21 @@
//! Shared on-disk candidate set for managed daemon restarts.
use std::collections::BTreeSet;
use std::io;
use std::path::Path;
use codex_core::path_utils::write_atomically;
pub fn read_candidates(path: &Path) -> io::Result<BTreeSet<String>> {
match std::fs::read(path) {
Ok(contents) => serde_json::from_slice(&contents).map_err(io::Error::other),
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(BTreeSet::new()),
Err(err) => Err(err),
}
}
pub fn write_candidates(path: &Path, candidates: &BTreeSet<String>) -> io::Result<()> {
write_atomically(
path,
&serde_json::to_string(candidates).map_err(io::Error::other)?,
)
}
+2
View File
@@ -1,3 +1,4 @@
pub mod daemon_recovery;
#[cfg(any(windows, test))]
mod daemon_shutdown;
#[cfg(windows)]
@@ -33,6 +34,7 @@ pub use transport::acquire_app_server_startup_lock;
pub use transport::app_server_control_socket_path;
pub use transport::app_server_startup_lock_path;
pub use transport::auth;
pub use transport::daemon_recovery_file_path;
pub use transport::prepare_control_socket_path;
pub use transport::start_control_socket_acceptor;
pub use transport::start_remote_control;
@@ -11,6 +11,7 @@ use codex_core::config::find_codex_home;
use codex_utils_absolute_path::AbsolutePathBuf;
use std::net::SocketAddr;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
@@ -55,6 +56,13 @@ const OVERLOADED_ERROR_CODE: i64 = -32001;
const APP_SERVER_CONTROL_SOCKET_DIR_NAME: &str = "app-server-control";
const APP_SERVER_CONTROL_SOCKET_FILE_NAME: &str = "app-server-control.sock";
const APP_SERVER_STARTUP_LOCK_FILE_NAME: &str = "app-server-startup.lock";
const DAEMON_RECOVERY_FILE_NAME: &str = "loaded-threads.json";
pub fn daemon_recovery_file_path(codex_home: &Path) -> PathBuf {
codex_home
.join("app-server-daemon")
.join(DAEMON_RECOVERY_FILE_NAME)
}
pub fn app_server_control_socket_path(codex_home: &Path) -> std::io::Result<AbsolutePathBuf> {
AbsolutePathBuf::from_absolute_path(