Record interrupted turns in managed daemon recovery snapshots (#45807)

## Why

Managed daemon recovery snapshots previously saved only loaded thread IDs, without identifying active turns or preserving their turn-specific options.

## What changed

- Capture regular, uncanceled turns after their input is recorded, saving the turn ID, output schema, service tier, and cyber access program alongside persisted thread IDs.
- Store interruption metadata atomically in the existing candidate array format so older servers can still read thread candidates.
- Begin snapshotting once admitted operations drain, while turns may still be running. Run snapshot collection and thread listener attachment independently of the event loop to keep forced shutdown responsive.

## Testing

Add coverage for running, completed, canceled, and compacting turns; recovery readiness for automatic and user turns; admitted resumes during shutdown; forced shutdown with a blocked rollout writer and child listener attachment; and legacy candidate-array compatibility.

GitOrigin-RevId: ed46342c3a5c71b09c48fa9acece2f15ae748e2f
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-15 23:31:44 +00:00
1 parent 7f83d4922d
commit 4d2807023a
12 files changed
+407 -38

No files matched your search

+1 -1
View File
@@ -27,6 +27,7 @@ codex-app-server-protocol = { workspace = true }
codex-core = { workspace = true }
codex-login = { workspace = true }
codex-model-provider = { workspace = true }
codex-protocol = { workspace = true }
codex-state = { workspace = true }
codex-uds = { workspace = true }
codex-utils-absolute-path = { workspace = true }
@@ -61,7 +62,6 @@ signal-hook = { workspace = true }
[dev-dependencies]
chrono = { workspace = true }
codex-config = { workspace = true }
codex-protocol = { workspace = true }
pretty_assertions = { workspace = true }
tempfile = { workspace = true }
tokio = { workspace = true, features = ["test-util"] }
@@ -1,21 +1,79 @@
//! Shared on-disk candidate set for managed daemon restarts.
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::io;
use std::path::Path;
use codex_core::path_utils::write_atomically;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct RecoverySnapshot {
#[serde(skip)]
pub loaded: BTreeSet<String>,
pub interrupted: BTreeMap<String, InterruptedTurn>,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct InterruptedTurn {
pub turn_id: String,
pub output_schema: Option<serde_json::Value>,
pub service_tier: Option<String>,
pub cyber_access_program: Option<codex_protocol::turn_input::CyberAccessProgram>,
}
// Old servers accept the array and skip this non-thread entry during best-effort
// restoration. Keeping metadata in the same atomic file avoids stale sidecars.
const INTERRUPTION_PREFIX: &str = "codex-interrupted-v1:";
pub fn read_snapshot(path: &Path) -> io::Result<RecoverySnapshot> {
let mut loaded: 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 => return Ok(RecoverySnapshot::default()),
Err(err) => return Err(err),
};
let mut snapshot = RecoverySnapshot::default();
loaded.retain(|entry| {
if let Some(metadata) = entry.strip_prefix(INTERRUPTION_PREFIX) {
if let Ok(saved) = serde_json::from_str::<RecoverySnapshot>(metadata) {
snapshot = saved;
}
false
} else {
true
}
});
snapshot.interrupted.retain(|id, _| loaded.contains(id));
snapshot.loaded = loaded;
Ok(snapshot)
}
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),
}
Ok(read_snapshot(path)?.loaded)
}
pub fn write_candidates(path: &Path, candidates: &BTreeSet<String>) -> io::Result<()> {
write_atomically(
write_snapshot(
path,
&serde_json::to_string(candidates).map_err(io::Error::other)?,
&RecoverySnapshot {
loaded: candidates.clone(),
..Default::default()
},
)
}
pub fn write_snapshot(path: &Path, snapshot: &RecoverySnapshot) -> io::Result<()> {
let mut saved = snapshot.loaded.clone();
if !snapshot.interrupted.is_empty() {
saved.insert(format!(
"{INTERRUPTION_PREFIX}{}",
serde_json::to_string(snapshot).map_err(io::Error::other)?
));
}
write_atomically(
path,
&serde_json::to_string(&saved).map_err(io::Error::other)?,
)
}