Skip to content

Commit cbd7d0d

Browse files
authored
chore: improve rollout session init errors (#7336)
Title: Improve rollout session initialization error messages Issue: #7283 What: add targeted mapping for rollout/session initialization errors so users get actionable messages when Codex cannot access session files. Why: session creation previously returned a generic internal error, hiding permissions/FS issues and making support harder. How: - Added rollout::error::map_session_init_error to translate the more common io::Error kinds into user-facing hints (permission, missing dir, file blocking, corruption). Others are passed through directly with `CodexErr::Fatal`. - Reused the mapper in Codex session creation to preserve root causes instead of returning InternalAgentDied.
1 parent fabdbfe commit cbd7d0d

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

codex-rs/core/src/codex.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ use crate::protocol::TurnDiffEvent;
102102
use crate::protocol::WarningEvent;
103103
use crate::rollout::RolloutRecorder;
104104
use crate::rollout::RolloutRecorderParams;
105+
use crate::rollout::map_session_init_error;
105106
use crate::shell;
106107
use crate::state::ActiveTurn;
107108
use crate::state::SessionServices;
@@ -206,7 +207,7 @@ impl Codex {
206207
.await
207208
.map_err(|e| {
208209
error!("Failed to create session: {e:#}");
209-
CodexErr::InternalAgentDied
210+
map_session_init_error(&e, &config.codex_home)
210211
})?;
211212
let conversation_id = session.conversation_id;
212213

@@ -508,7 +509,7 @@ impl Session {
508509

509510
let rollout_recorder = rollout_recorder.map_err(|e| {
510511
error!("failed to initialize rollout recorder: {e:#}");
511-
anyhow::anyhow!("failed to initialize rollout recorder: {e:#}")
512+
anyhow::Error::from(e)
512513
})?;
513514
let rollout_path = rollout_recorder.rollout_path.clone();
514515

codex-rs/core/src/rollout/error.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::io::ErrorKind;
2+
use std::path::Path;
3+
4+
use crate::error::CodexErr;
5+
use crate::rollout::SESSIONS_SUBDIR;
6+
7+
pub(crate) fn map_session_init_error(err: &anyhow::Error, codex_home: &Path) -> CodexErr {
8+
if let Some(mapped) = err
9+
.chain()
10+
.filter_map(|cause| cause.downcast_ref::<std::io::Error>())
11+
.find_map(|io_err| map_rollout_io_error(io_err, codex_home))
12+
{
13+
return mapped;
14+
}
15+
16+
CodexErr::Fatal(format!("Failed to initialize session: {err:#}"))
17+
}
18+
19+
fn map_rollout_io_error(io_err: &std::io::Error, codex_home: &Path) -> Option<CodexErr> {
20+
let sessions_dir = codex_home.join(SESSIONS_SUBDIR);
21+
let hint = match io_err.kind() {
22+
ErrorKind::PermissionDenied => format!(
23+
"Codex cannot access session files at {} (permission denied). If sessions were created using sudo, fix ownership: sudo chown -R $(whoami) {}",
24+
sessions_dir.display(),
25+
codex_home.display()
26+
),
27+
ErrorKind::NotFound => format!(
28+
"Session storage missing at {}. Create the directory or choose a different Codex home.",
29+
sessions_dir.display()
30+
),
31+
ErrorKind::AlreadyExists => format!(
32+
"Session storage path {} is blocked by an existing file. Remove or rename it so Codex can create sessions.",
33+
sessions_dir.display()
34+
),
35+
ErrorKind::InvalidData | ErrorKind::InvalidInput => format!(
36+
"Session data under {} looks corrupt or unreadable. Clearing the sessions directory may help (this will remove saved conversations).",
37+
sessions_dir.display()
38+
),
39+
ErrorKind::IsADirectory | ErrorKind::NotADirectory => format!(
40+
"Session storage path {} has an unexpected type. Ensure it is a directory Codex can use for session files.",
41+
sessions_dir.display()
42+
),
43+
_ => return None,
44+
};
45+
46+
Some(CodexErr::Fatal(format!(
47+
"{hint} (underlying error: {io_err})"
48+
)))
49+
}

codex-rs/core/src/rollout/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ pub const ARCHIVED_SESSIONS_SUBDIR: &str = "archived_sessions";
77
pub const INTERACTIVE_SESSION_SOURCES: &[SessionSource] =
88
&[SessionSource::Cli, SessionSource::VSCode];
99

10+
pub(crate) mod error;
1011
pub mod list;
1112
pub(crate) mod policy;
1213
pub mod recorder;
1314

1415
pub use codex_protocol::protocol::SessionMeta;
16+
pub(crate) use error::map_session_init_error;
1517
pub use list::find_conversation_path_by_id_str;
1618
pub use recorder::RolloutRecorder;
1719
pub use recorder::RolloutRecorderParams;

0 commit comments

Comments
 (0)