From ad3b9407e4b4d004acb5a5ba9aeede3cfc57655d Mon Sep 17 00:00:00 2001 From: David Wiesen Date: Wed, 6 May 2026 10:04:51 -0700 Subject: [PATCH] Clarify sandbox git metadata denials --- codex-rs/protocol/src/error.rs | 70 +++++++++++++++++++++------- codex-rs/protocol/src/error_tests.rs | 23 +++++++++ 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/codex-rs/protocol/src/error.rs b/codex-rs/protocol/src/error.rs index 207fd94ca2..147c2d6504 100644 --- a/codex-rs/protocol/src/error.rs +++ b/codex-rs/protocol/src/error.rs @@ -596,24 +596,7 @@ impl std::fmt::Display for EnvVarError { pub fn get_error_message_ui(e: &CodexErr) -> String { let message = match e { - CodexErr::Sandbox(SandboxErr::Denied { output, .. }) => { - let aggregated = output.aggregated_output.text.trim(); - if !aggregated.is_empty() { - output.aggregated_output.text.clone() - } else { - let stderr = output.stderr.text.trim(); - let stdout = output.stdout.text.trim(); - match (stderr.is_empty(), stdout.is_empty()) { - (false, false) => format!("{stderr}\n{stdout}"), - (false, true) => output.stderr.text.clone(), - (true, false) => output.stdout.text.clone(), - (true, true) => format!( - "command failed inside sandbox with exit code {}", - output.exit_code - ), - } - } - } + CodexErr::Sandbox(SandboxErr::Denied { output, .. }) => sandbox_denied_ui_message(output), // Timeouts are not sandbox errors from a UX perspective; present them plainly. CodexErr::Sandbox(SandboxErr::Timeout { output }) => { format!( @@ -630,6 +613,57 @@ pub fn get_error_message_ui(e: &CodexErr) -> String { ) } +fn sandbox_denied_ui_message(output: &ExecToolCallOutput) -> String { + let aggregated = output.aggregated_output.text.trim(); + let mut message = if !aggregated.is_empty() { + output.aggregated_output.text.clone() + } else { + let stderr = output.stderr.text.trim(); + let stdout = output.stdout.text.trim(); + match (stderr.is_empty(), stdout.is_empty()) { + (false, false) => format!("{stderr}\n{stdout}"), + (false, true) => output.stderr.text.clone(), + (true, false) => output.stdout.text.clone(), + (true, true) => format!( + "command failed inside sandbox with exit code {}", + output.exit_code + ), + } + }; + + if is_protected_git_metadata_denial(output) + && !message.contains("Codex sandbox protects `.git` in `workspace-write` mode") + { + message.push_str( + "\n\nCodex sandbox protects `.git` in `workspace-write` mode, so Git metadata \ +writes like `FETCH_HEAD` or `index.lock` can fail. Run Git outside Codex or use a less \ +restrictive sandbox if you want Codex to manage Git state.", + ); + } + + message +} + +fn is_protected_git_metadata_denial(output: &ExecToolCallOutput) -> bool { + let output_text = [ + output.stderr.text.as_str(), + output.stdout.text.as_str(), + output.aggregated_output.text.as_str(), + ] + .join("\n") + .to_lowercase(); + + let mentions_git_metadata = output_text.contains(".git/") + || output_text.contains(".git\\") + || output_text.contains("fetch_head") + || output_text.contains("index.lock"); + let looks_like_denial = output_text.contains("permission denied") + || output_text.contains("read-only file system") + || output_text.contains("cannot open"); + + mentions_git_metadata && looks_like_denial +} + #[cfg(test)] #[path = "error_tests.rs"] mod tests; diff --git a/codex-rs/protocol/src/error_tests.rs b/codex-rs/protocol/src/error_tests.rs index aef7478607..4e7ebbef2e 100644 --- a/codex-rs/protocol/src/error_tests.rs +++ b/codex-rs/protocol/src/error_tests.rs @@ -170,6 +170,29 @@ fn sandbox_denied_reports_exit_code_when_no_output_available() { ); } +#[test] +fn sandbox_denied_appends_git_protection_hint_for_fetch_head_failures() { + let output = ExecToolCallOutput { + exit_code: 128, + stdout: StreamOutput::new(String::new()), + stderr: StreamOutput::new( + "error: cannot open '.git/FETCH_HEAD': Permission denied".to_string(), + ), + aggregated_output: StreamOutput::new(String::new()), + duration: Duration::from_millis(5), + timed_out: false, + }; + let err = CodexErr::Sandbox(SandboxErr::Denied { + output: Box::new(output), + network_policy_decision: None, + }); + + assert_eq!( + get_error_message_ui(&err), + "error: cannot open '.git/FETCH_HEAD': Permission denied\n\nCodex sandbox protects `.git` in `workspace-write` mode, so Git metadata writes like `FETCH_HEAD` or `index.lock` can fail. Run Git outside Codex or use a less restrictive sandbox if you want Codex to manage Git state." + ); +} + #[test] fn usage_limit_reached_error_formats_free_plan() { let err = UsageLimitReachedError {