Skip to main content

devflow_core/agents/
codex.rs

1//! OpenAI Codex agent adapter.
2//!
3//! Launches `codex exec "<prompt>"` in non-interactive mode with JSON output.
4
5use super::AgentAdapter;
6use std::path::PathBuf;
7
8pub struct CodexAgent;
9
10impl AgentAdapter for CodexAgent {
11    fn name(&self) -> &'static str {
12        "OpenAI Codex"
13    }
14
15    fn exec_command(
16        &self,
17        _phase: u32,
18        prompt: &str,
19        extra_writable_roots: &[PathBuf],
20    ) -> (&'static str, Vec<String>) {
21        let mut args: Vec<String> = vec![
22            "exec".into(),
23            "--sandbox".into(),
24            "workspace-write".into(),
25            "--json".into(),
26        ];
27        // Linked-worktree commits write git metadata outside the
28        // workspace-write sandbox (13-06 dogfood finding: Code stage
29        // implemented and tested, then could not commit). Grant every extra
30        // root in one TOML list value; escape backslashes and quotes in
31        // paths.
32        if !extra_writable_roots.is_empty() {
33            let list = extra_writable_roots
34                .iter()
35                .map(|root| {
36                    let escaped = root
37                        .display()
38                        .to_string()
39                        .replace('\\', "\\\\")
40                        .replace('"', "\\\"");
41                    format!("\"{escaped}\"")
42                })
43                .collect::<Vec<_>>()
44                .join(",");
45            args.push("-c".into());
46            args.push(format!("sandbox_workspace_write.writable_roots=[{list}]"));
47        }
48        args.push(prompt.to_string());
49        ("codex", args)
50    }
51
52    /// The sandbox has no route to the operator's signing agent, so signed
53    /// commits/tags fail headless (`ssh-keygen -Y sign` → passphrase error).
54    /// Disable signing via env, scoped to this agent's process tree only.
55    fn extra_env(&self) -> Vec<(String, String)> {
56        vec![
57            ("GIT_CONFIG_COUNT".into(), "2".into()),
58            ("GIT_CONFIG_KEY_0".into(), "commit.gpgsign".into()),
59            ("GIT_CONFIG_VALUE_0".into(), "false".into()),
60            ("GIT_CONFIG_KEY_1".into(), "tag.gpgsign".into()),
61            ("GIT_CONFIG_VALUE_1".into(), "false".into()),
62        ]
63    }
64
65    fn completion_signal_detected(&self, _output: &str) -> bool {
66        false
67    }
68}