Skip to main content

batty_cli/agent/
codex.rs

1//! Codex CLI adapter.
2//!
3//! Runs Codex in interactive mode by default, passing the composed task prompt
4//! as the initial user prompt argument.
5#![cfg_attr(not(test), allow(dead_code))]
6
7use std::path::Path;
8
9use anyhow::Context;
10
11use crate::agent::{AgentAdapter, SpawnConfig};
12use crate::prompt::PromptPatterns;
13
14/// Adapter for Codex CLI.
15pub struct CodexCliAdapter {
16    /// Override the codex binary name/path (default: "codex").
17    program: String,
18}
19
20impl CodexCliAdapter {
21    pub fn new(program: Option<String>) -> Self {
22        Self {
23            program: program.unwrap_or_else(|| "codex".to_string()),
24        }
25    }
26}
27
28impl AgentAdapter for CodexCliAdapter {
29    fn name(&self) -> &str {
30        "codex-cli"
31    }
32
33    fn spawn_config(&self, task_description: &str, work_dir: &Path) -> SpawnConfig {
34        SpawnConfig {
35            program: self.program.clone(),
36            args: vec![task_description.to_string()],
37            work_dir: work_dir.to_string_lossy().to_string(),
38            env: vec![],
39        }
40    }
41
42    fn prompt_patterns(&self) -> PromptPatterns {
43        PromptPatterns::codex_cli()
44    }
45
46    fn instruction_candidates(&self) -> &'static [&'static str] {
47        &["AGENTS.md", "CLAUDE.md"]
48    }
49
50    fn wrap_launch_prompt(&self, prompt: &str) -> String {
51        format!(
52            "You are running as Codex under Batty supervision.\n\
53             Treat the launch context below as authoritative session context.\n\n\
54             {prompt}"
55        )
56    }
57
58    fn format_input(&self, response: &str) -> String {
59        format!("{response}\n")
60    }
61
62    fn launch_command(
63        &self,
64        prompt: &str,
65        idle: bool,
66        resume: bool,
67        session_id: Option<&str>,
68    ) -> anyhow::Result<String> {
69        let escaped = prompt.replace('\'', "'\\''");
70        let prefix = format!(
71            "{} --dangerously-bypass-approvals-and-sandbox",
72            self.program
73        );
74        if resume {
75            let sid = session_id.context("missing Codex session ID for resume")?;
76            let fallback = if idle {
77                format!("exec {prefix}")
78            } else {
79                format!("exec {prefix} '{escaped}'")
80            };
81            Ok(format!(
82                "{program} resume '{sid}' --dangerously-bypass-approvals-and-sandbox || {fallback}",
83                program = self.program,
84            ))
85        } else if idle {
86            Ok(format!("exec {prefix}"))
87        } else {
88            Ok(format!("{prefix} '{escaped}'"))
89        }
90    }
91
92    fn supports_resume(&self) -> bool {
93        true
94    }
95
96    fn health_check(&self) -> super::BackendHealth {
97        super::check_binary_available(&self.program)
98    }
99}
100
101impl CodexCliAdapter {
102    /// Build the launch command for SDK (JSONL) mode.
103    ///
104    /// In Codex SDK mode, each message spawns a new `codex exec --json`
105    /// subprocess. The initial prompt is the system/role context; actual
106    /// task messages are sent per-turn by the runtime.
107    ///
108    /// `system_prompt`: role context passed as the initial exec prompt.
109    pub fn sdk_launch_command(&self, _system_prompt: Option<&str>) -> String {
110        // In Codex SDK mode, the shim runtime handles spawning per-message.
111        // The launch script just needs to set up the environment (PATH, CWD).
112        // We use a simple sleep loop as a placeholder process — the actual
113        // codex exec calls are made by the runtime_codex module.
114        //
115        // But we need a process that stays alive so the shim doesn't exit.
116        // Use `cat` which blocks on stdin indefinitely (stdin is /dev/null
117        // so it exits immediately — but that's fine, the Codex runtime
118        // doesn't need a persistent process).
119        //
120        // Actually, the Codex runtime handles its own subprocess spawning,
121        // so the launch command is just a sentinel that exits immediately.
122        // The runtime is designed for spawn-per-message, not persistent process.
123        "exec sleep infinity".to_string()
124    }
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[test]
132    fn default_program_is_codex() {
133        let adapter = CodexCliAdapter::new(None);
134        let config = adapter.spawn_config("test", Path::new("/tmp"));
135        assert_eq!(config.program, "codex");
136    }
137
138    #[test]
139    fn custom_program_path() {
140        let adapter = CodexCliAdapter::new(Some("/usr/local/bin/codex".to_string()));
141        let config = adapter.spawn_config("test", Path::new("/tmp"));
142        assert_eq!(config.program, "/usr/local/bin/codex");
143    }
144
145    #[test]
146    fn spawn_sets_work_dir() {
147        let adapter = CodexCliAdapter::new(None);
148        let config = adapter.spawn_config("task", Path::new("/my/worktree"));
149        assert_eq!(config.work_dir, "/my/worktree");
150    }
151
152    #[test]
153    fn prompt_patterns_detect_permission() {
154        let adapter = CodexCliAdapter::new(None);
155        let patterns = adapter.prompt_patterns();
156        let d = patterns.detect("Would you like to run the following command?");
157        assert!(d.is_some());
158        assert!(matches!(
159            d.unwrap().kind,
160            crate::prompt::PromptKind::Permission { .. }
161        ));
162    }
163
164    #[test]
165    fn format_input_appends_newline() {
166        let adapter = CodexCliAdapter::new(None);
167        assert_eq!(adapter.format_input("y"), "y\n");
168        assert_eq!(adapter.format_input("yes"), "yes\n");
169    }
170
171    #[test]
172    fn name_is_codex_cli() {
173        let adapter = CodexCliAdapter::new(None);
174        assert_eq!(adapter.name(), "codex-cli");
175    }
176
177    #[test]
178    fn codex_prefers_agents_md_instruction_order() {
179        let adapter = CodexCliAdapter::new(None);
180        assert_eq!(
181            adapter.instruction_candidates(),
182            &["AGENTS.md", "CLAUDE.md"]
183        );
184    }
185
186    #[test]
187    fn codex_wraps_launch_prompt() {
188        let adapter = CodexCliAdapter::new(None);
189        let wrapped = adapter.wrap_launch_prompt("Launch body");
190        assert!(wrapped.contains("Codex under Batty supervision"));
191        assert!(wrapped.contains("Launch body"));
192    }
193
194    // --- Backend trait method tests ---
195
196    #[test]
197    fn launch_command_active_includes_prompt() {
198        let adapter = CodexCliAdapter::new(None);
199        let cmd = adapter
200            .launch_command("do the thing", false, false, None)
201            .unwrap();
202        assert!(cmd.contains("codex --dangerously-bypass-approvals-and-sandbox"));
203        assert!(cmd.contains("'do the thing'"));
204        // Active (non-resume) should NOT use exec so the shim can detect exit
205        assert!(!cmd.starts_with("exec "));
206    }
207
208    #[test]
209    fn launch_command_idle_omits_prompt() {
210        let adapter = CodexCliAdapter::new(None);
211        let cmd = adapter
212            .launch_command("ignored", true, false, None)
213            .unwrap();
214        assert_eq!(cmd, "exec codex --dangerously-bypass-approvals-and-sandbox");
215    }
216
217    #[test]
218    fn launch_command_resume_uses_session_id() {
219        let adapter = CodexCliAdapter::new(None);
220        let cmd = adapter
221            .launch_command("ignored", false, true, Some("codex-sess-1"))
222            .unwrap();
223        assert!(cmd.contains("codex resume 'codex-sess-1'"));
224        assert!(cmd.contains("--dangerously-bypass-approvals-and-sandbox"));
225        assert!(cmd.contains("|| exec codex --dangerously-bypass-approvals-and-sandbox 'ignored'"));
226    }
227
228    #[test]
229    fn launch_command_resume_idle_falls_back_to_fresh_idle_start() {
230        let adapter = CodexCliAdapter::new(None);
231        let cmd = adapter
232            .launch_command("ignored", true, true, Some("codex-sess-1"))
233            .unwrap();
234        assert!(cmd.contains("codex resume 'codex-sess-1'"));
235        assert!(cmd.contains("|| exec codex --dangerously-bypass-approvals-and-sandbox"));
236        assert!(!cmd.contains("'ignored'"));
237    }
238
239    #[test]
240    fn launch_command_resume_without_session_id_errors() {
241        let adapter = CodexCliAdapter::new(None);
242        let result = adapter.launch_command("ignored", false, true, None);
243        assert!(result.is_err());
244    }
245
246    #[test]
247    fn new_session_id_returns_none() {
248        let adapter = CodexCliAdapter::new(None);
249        assert!(adapter.new_session_id().is_none());
250    }
251
252    #[test]
253    fn supports_resume_is_true() {
254        let adapter = CodexCliAdapter::new(None);
255        assert!(adapter.supports_resume());
256    }
257}