aca 0.3.1

A Rust-based agentic tool that automates coding tasks using Claude Code and OpenAI Codex CLI integrations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use crate::env;
use crate::openai::rate_limiter::{OpenAIRateLimiter, RateLimiterStatus};
use crate::openai::types::{
    OpenAIConfig, OpenAIError, OpenAITaskRequest, OpenAITaskResponse, RatePermit, TokenUsage,
};
use chrono::Utc;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::fs::{self, OpenOptions};
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use tracing::{info, warn};
use uuid::Uuid;
use which::which;

/// Interface that executes the Codex CLI in headless mode.
#[derive(Debug)]
pub struct OpenAICodexInterface {
    config: OpenAIConfig,
    rate_limiter: Arc<OpenAIRateLimiter>,
}

impl OpenAICodexInterface {
    pub async fn new(config: OpenAIConfig) -> Result<Self, OpenAIError> {
        if which(&config.cli_path).is_err() {
            return Err(OpenAIError::CliUnavailable(config.cli_path.clone()));
        }

        let rate_limiter = Arc::new(OpenAIRateLimiter::new(config.rate_limits.clone()));

        Ok(Self {
            config,
            rate_limiter,
        })
    }

    pub async fn execute_task_request(
        &self,
        request: OpenAITaskRequest,
        session_dir: Option<&Path>,
    ) -> Result<OpenAITaskResponse, OpenAIError> {
        let permit = self.rate_limiter.acquire_permit(&request).await?;
        let log_paths = if self.config.logging.enable_interaction_logs {
            self.prepare_log_paths(session_dir, request.id).await
        } else {
            None
        };

        if let Some(ref paths) = log_paths {
            self.append_log(
                paths,
                &format!(
                    "[{}] Starting Codex exec | model={} | tokensā‰ˆ{}",
                    Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                    request.model,
                    request.estimated_tokens
                ),
            )
            .await;
        }

        let composed_prompt = self.compose_prompt(&request);
        let start = Instant::now();

        let mut skip_model_flag = false;
        let response = loop {
            let output = match self
                .run_codex_exec(&request, &composed_prompt, skip_model_flag)
                .await
            {
                Ok(output) => output,
                Err(err) => {
                    self.rate_limiter.record_failure().await;
                    return Err(err);
                }
            };

            let execution_time = start.elapsed();

            let parsed = match self.parse_codex_output(&output.stdout) {
                Ok(parsed) => parsed,
                Err(err) => {
                    self.rate_limiter.record_failure().await;
                    return Err(err);
                }
            };

            if let Some(ref paths) = log_paths {
                if let Err(e) = fs::write(&paths.stdout_file, &output.stdout).await {
                    warn!("Failed to persist Codex stdout: {}", e);
                }
                if !output.stderr.is_empty()
                    && let Err(e) = fs::write(&paths.stderr_file, &output.stderr).await
                {
                    warn!("Failed to persist Codex stderr: {}", e);
                }
            }

            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                let message = stderr.trim().to_string();
                if !skip_model_flag && message.contains("Unsupported model") {
                    warn!(
                        "Codex CLI reported unsupported model; retrying without explicit --model flag"
                    );
                    if let Some(ref paths) = log_paths {
                        self.append_log(
                            paths,
                            &format!(
                                "[{}] WARN: Codex reported unsupported model '{}'; retrying without explicit --model flag",
                                Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                                request.model
                            ),
                        )
                        .await;
                    }
                    skip_model_flag = true;
                    continue;
                }
                if message.contains("login") {
                    self.rate_limiter.record_failure().await;
                    return Err(OpenAIError::Authentication(message));
                }
                self.rate_limiter.record_failure().await;
                return Err(OpenAIError::CliFailed(message));
            }

            let response = self.build_response(&request, permit.clone(), parsed, execution_time)?;
            self.rate_limiter.record_success().await;
            break response;
        };

        if let Some(ref paths) = log_paths {
            let status = self.rate_limiter.get_status().await;
            self.append_log(
                paths,
                &format!(
                    "[{}] Completed in {:.2}s | preview=\"{}\" | remaining req={} tok={}",
                    Utc::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                    response.execution_time.as_secs_f64(),
                    self.preview(&response.response_text),
                    status.available_requests,
                    status.available_tokens
                ),
            )
            .await;
        }

        info!(
            "Codex CLI request {} completed in {:.2?}",
            response.task_id, response.execution_time
        );

        Ok(response)
    }

    pub async fn get_interface_status(&self) -> RateLimiterStatus {
        self.rate_limiter.get_status().await
    }

    fn compose_prompt(&self, request: &OpenAITaskRequest) -> String {
        let mut segments = Vec::new();

        if let Some(system) = request.system_message.as_ref()
            && !system.trim().is_empty()
        {
            segments.push(format!("System instructions:\n{}\n", system.trim()));
        }

        if !request.metadata.is_empty() {
            let mut ctx = String::from("Context:\n");
            for (key, value) in &request.metadata {
                ctx.push_str(&format!("• {}: {}\n", key, value.trim()));
            }
            ctx.push('\n');
            segments.push(ctx);
        }

        segments.push(request.prompt.clone());
        segments.join("\n")
    }

    async fn run_codex_exec(
        &self,
        request: &OpenAITaskRequest,
        prompt: &str,
        skip_model_flag: bool,
    ) -> Result<std::process::Output, OpenAIError> {
        let mut cmd = Command::new(&self.config.cli_path);
        cmd.arg("exec")
            .arg("--json")
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        if !skip_model_flag {
            cmd.arg("--model").arg(&request.model);
        }

        if self.config.allow_outside_git {
            cmd.arg("--skip-git-repo-check");
        }

        if let Some(profile) = &self.config.profile {
            cmd.arg("--profile").arg(profile);
        }

        if !self.config.extra_args.is_empty() {
            cmd.args(&self.config.extra_args);
        }

        cmd.arg("-")
            .stdin(Stdio::piped())
            .current_dir(&self.config.working_dir);

        let mut child = cmd
            .spawn()
            .map_err(|e| OpenAIError::CliFailed(e.to_string()))?;

        if let Some(mut stdin) = child.stdin.take() {
            let prompt_bytes = prompt.as_bytes();
            stdin
                .write_all(prompt_bytes)
                .await
                .map_err(|e| OpenAIError::CliFailed(e.to_string()))?;
        }

        let output = child
            .wait_with_output()
            .await
            .map_err(|e| OpenAIError::CliFailed(e.to_string()))?;

        Ok(output)
    }

    fn parse_codex_output(&self, stdout: &[u8]) -> Result<CodexParsedOutput, OpenAIError> {
        let text = String::from_utf8_lossy(stdout);
        let mut last_agent_message: Option<String> = None;
        let mut finish_reason: Option<String> = None;
        let mut usage = TokenUsage::default();
        let mut failure_reason: Option<String> = None;

        for line in text.lines().filter(|line| !line.trim().is_empty()) {
            let value: serde_json::Value = serde_json::from_str(line).map_err(|e| {
                OpenAIError::Serialization(format!("Failed to parse Codex JSON line: {}", e))
            })?;

            if let Some(event_type) = value.get("type").and_then(|t| t.as_str()) {
                match event_type {
                    "item.completed" => {
                        if let Some(item) = value.get("item")
                            && item
                                .get("type")
                                .and_then(|t| t.as_str())
                                .map(|t| t == "agent_message")
                                .unwrap_or(false)
                            && let Some(text) = item.get("text").and_then(|t| t.as_str())
                        {
                            last_agent_message = Some(text.to_string());
                        }
                    }
                    "turn.completed" => {
                        if let Some(usage_value) = value.get("usage") {
                            usage.prompt_tokens = usage_value
                                .get("input_tokens")
                                .and_then(|v| v.as_u64())
                                .unwrap_or(0);
                            usage.cached_prompt_tokens = usage_value
                                .get("cached_input_tokens")
                                .and_then(|v| v.as_u64())
                                .unwrap_or(0);
                            usage.completion_tokens = usage_value
                                .get("output_tokens")
                                .and_then(|v| v.as_u64())
                                .unwrap_or(0);
                            usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
                        }
                    }
                    "run.completed" => {
                        finish_reason = value
                            .get("reason")
                            .and_then(|v| v.as_str())
                            .map(|s| s.to_string())
                            .or_else(|| Some("completed".to_string()));
                    }
                    "error" => {
                        if let Some(message) = value.get("message").and_then(|v| v.as_str()) {
                            failure_reason = Some(message.to_string());
                        }
                    }
                    "turn.failed" | "run.failed" => {
                        finish_reason = Some("failed".to_string());
                        if let Some(error) = value
                            .get("error")
                            .and_then(|v| v.get("message"))
                            .and_then(|v| v.as_str())
                        {
                            failure_reason = Some(error.to_string());
                        }
                    }
                    _ => {}
                }
            }
        }

        if let Some(response_text) = last_agent_message {
            return Ok(CodexParsedOutput {
                response_text,
                finish_reason,
                usage,
            });
        }

        if let Some(reason) = failure_reason {
            return Err(OpenAIError::CliFailed(reason));
        }

        Err(OpenAIError::CliFailed(
            "Codex CLI did not return an agent message".to_string(),
        ))
    }

    fn build_response(
        &self,
        request: &OpenAITaskRequest,
        permit: RatePermit,
        parsed: CodexParsedOutput,
        elapsed: Duration,
    ) -> Result<OpenAITaskResponse, OpenAIError> {
        let mut usage = parsed.usage;
        if usage.prompt_tokens == 0 {
            usage.prompt_tokens = permit.tokens_consumed;
            usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
        }

        let response = OpenAITaskResponse {
            task_id: request.id,
            response_text: parsed.response_text,
            token_usage: usage,
            execution_time: elapsed,
            model_used: request.model.clone(),
            finish_reason: parsed.finish_reason,
        };

        Ok(response)
    }

    async fn prepare_log_paths(
        &self,
        session_dir: Option<&Path>,
        request_id: Uuid,
    ) -> Option<LogPaths> {
        let base_dir = if let Some(dir) = session_dir {
            dir.join(env::session::OPENAI_INTERACTIONS_DIR_NAME)
        } else {
            env::openai_interactions_dir_path(&self.config.working_dir, "global")
        };

        if let Err(e) = fs::create_dir_all(&base_dir).await {
            warn!(
                "Failed to create Codex interaction directory {}: {}",
                base_dir.display(),
                e
            );
            return None;
        }

        let timestamp = Utc::now().format("%Y%m%dT%H%M%S%.3f");
        let base = base_dir.join(format!("{}-{}", timestamp, request_id));

        Some(LogPaths {
            log_file: base.with_extension("log"),
            stdout_file: base.with_extension("stdout.jsonl"),
            stderr_file: base.with_extension("stderr.txt"),
        })
    }

    async fn append_log(&self, paths: &LogPaths, message: &str) {
        if let Err(e) = self.write_line(&paths.log_file, message).await {
            warn!(
                "Failed to append Codex interaction log {}: {}",
                paths.log_file.display(),
                e
            );
        }
    }

    async fn write_line(&self, file_path: &Path, content: &str) -> std::io::Result<()> {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(file_path)
            .await?;
        file.write_all(content.as_bytes()).await?;
        file.write_all(b"\n").await?;
        Ok(())
    }

    fn preview(&self, text: &str) -> String {
        let max_len = self.config.logging.max_preview_chars;
        if text.len() <= max_len {
            text.replace('\n', " ")
        } else {
            format!("{}...", text[..max_len].replace('\n', " "))
        }
    }
}

#[derive(Debug)]
struct LogPaths {
    log_file: PathBuf,
    stdout_file: PathBuf,
    stderr_file: PathBuf,
}

#[derive(Debug)]
struct CodexParsedOutput {
    response_text: String,
    finish_reason: Option<String>,
    usage: TokenUsage,
}