codexwire 0.1.0

A Rust client for the OpenAI Codex app-server: its JSON-RPC protocol, typed
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! One-shot `codex exec` runs on a `ChatGPT` login, each in a fresh private
//! `CODEX_HOME`.
//!
//! [`Exec::run`] makes `<tmp_root>/<run>/` (0700) with `home/` (the run's
//! `CODEX_HOME`, holding only a copy of the real `auth.json`), `work/` (its
//! working directory) and `refs/` (the input images), runs `codex exec`
//! there with the prompt on stdin and `--json` events on stdout, and copies
//! the login back when Codex refreshed it ([`crate::auth::copy_back`]'s
//! rule). A run that [`may_refresh`] the login goes alone
//! ([`crate::auth::refresh_lock`]). The [`Run`] it returns holds the run's
//! directory, removed when it is dropped (or [`Run::remove`]).
//!
//! [`failure`] reads why a run failed from its events and log;
//! [`image_prompt`] and [`Run::generated_images`] are for image generation
//! (Codex's image tool writes to `<CODEX_HOME>/generated_images/`).
//!
//! Unix only.

use core::{
    fmt,
    sync::atomic::{AtomicU64, Ordering},
    time::Duration,
};
use std::{
    path::{Path, PathBuf},
    process::Stdio,
};

use crate::auth::{Copied, may_refresh, mkdir_private, refresh_lock, write_private_atomic};

/// How to run `codex exec`.
#[derive(Clone, Debug)]
pub struct Exec {
    /// The `codex` binary.
    pub command: String,
    /// The real `CODEX_HOME`: only its `auth.json` is used (copied in, and
    /// back when refreshed).
    pub codex_home: PathBuf,
    /// Where the per-run directories go (made 0700 if missing).
    pub tmp_root: PathBuf,
    /// `--model`, if any (else Codex's default).
    pub model: Option<String>,
    /// `--sandbox` (`read-only`, `workspace-write`, `danger-full-access`).
    pub sandbox: String,
    /// Killed after this long.
    pub timeout: Duration,
    /// The child's environment on top of the parent's allowlisted variables
    /// ([`crate::client::child_env`]), e.g. `PATH`. `HOME`, `CODEX_HOME`
    /// and `TMPDIR` are always the run's; `LANG` is `C.UTF-8` unless given.
    pub env: Vec<(String, String)>,
    /// More `codex exec` arguments, before the prompt.
    pub args: Vec<String>,
}

impl Exec {
    /// `command` on the login in `codex_home`, runs under `tmp_root`: a
    /// read-only sandbox, a 5 minute timeout.
    pub fn new(
        command: impl Into<String>,
        codex_home: impl Into<PathBuf>,
        tmp_root: impl Into<PathBuf>,
    ) -> Self {
        Self {
            command: command.into(),
            codex_home: codex_home.into(),
            tmp_root: tmp_root.into(),
            model: None,
            sandbox: "read-only".into(),
            timeout: Duration::from_secs(300),
            env: Vec::new(),
            args: Vec::new(),
        }
    }
}

/// What a run is given.
#[derive(Clone, Debug, Default)]
pub struct Input {
    /// The prompt (on stdin).
    pub prompt: String,
    /// Images to attach (`--image`): a file name (e.g. `ref-1.png`; only
    /// its last component is used) and the bytes.
    pub images: Vec<(String, Vec<u8>)>,
}

/// Why a run didn't start or didn't finish setting up.
#[derive(Debug)]
pub enum ExecError {
    /// The real `auth.json` can't be read (no login, or no access).
    Login(std::io::Error),
    /// Making the run's directory or files (what, and why).
    Setup(&'static str, std::io::Error),
    /// The `codex` binary didn't start.
    Spawn(std::io::Error),
}

impl fmt::Display for ExecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Login(e) => write!(f, "Codex's auth.json isn't readable: {e}"),
            Self::Setup(what, e) => write!(f, "codex run {what}: {e}"),
            Self::Spawn(e) => write!(f, "codex didn't start: {e}"),
        }
    }
}

impl core::error::Error for ExecError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Login(e) | Self::Setup(_, e) | Self::Spawn(e) => Some(e),
        }
    }
}

/// A finished (or timed out) run. Its directory goes when it is dropped.
#[derive(Debug)]
pub struct Run {
    dir: RunDir,
    /// The exit code; `None` when it timed out and was killed (`-1` when it
    /// died of a signal).
    pub code: Option<i32>,
    /// Its `--json` events.
    pub stdout: Vec<u8>,
    /// Its log.
    pub stderr: Vec<u8>,
    /// `--output-last-message`: the final answer, if it wrote one.
    pub last_message: Vec<u8>,
    /// Whether Codex refreshed the login and it was copied back (or why
    /// copying it back failed).
    pub copied_back: std::io::Result<bool>,
}

impl Run {
    /// The run's `CODEX_HOME`.
    pub fn home(&self) -> PathBuf {
        self.dir.0.join("home")
    }

    /// Why it failed, from its events and log ([`failure`]).
    pub fn failure(&self) -> Option<Failure> {
        failure(&self.stdout, &self.stderr)
    }

    /// The images Codex made (regular files under
    /// `<CODEX_HOME>/generated_images/`, no symlinks, at most `max_bytes`
    /// each), oldest first. Not checked to be images.
    pub async fn generated_images(&self, max_bytes: u64) -> Vec<PathBuf> {
        files_oldest_first(&self.home().join("generated_images"), max_bytes).await
    }

    /// Remove the run's directory now.
    pub async fn remove(mut self) -> std::io::Result<()> {
        let p = core::mem::take(&mut self.dir.0);
        match tokio::fs::remove_dir_all(&p).await {
            Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e),
            _ => Ok(()),
        }
    }
}

/// Removes its directory when dropped (unless emptied by [`Run::remove`]).
#[derive(Debug)]
struct RunDir(PathBuf);

impl Drop for RunDir {
    fn drop(&mut self) {
        if !self.0.as_os_str().is_empty() {
            // Drop can't await; a run's directory is small.
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }
}

/// Serializes copying refreshed logins back within this process.
fn copy_back_lock() -> &'static tokio::sync::Mutex<()> {
    static L: std::sync::OnceLock<tokio::sync::Mutex<()>> = std::sync::OnceLock::new();
    L.get_or_init(tokio::sync::Mutex::default)
}

/// A name no other run of this process (or another, mostly) has.
fn run_name() -> String {
    static N: AtomicU64 = AtomicU64::new(0);
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_nanos());
    format!(
        "run-{}-{nanos}-{}",
        std::process::id(),
        N.fetch_add(1, Ordering::Relaxed)
    )
}

/// The last component of `name`, or `image-<i>` if it has none.
fn safe_name(name: &str, i: usize) -> String {
    Path::new(name)
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .filter(|n| !n.starts_with('.'))
        .unwrap_or_else(|| format!("image-{}", i + 1))
}

impl Exec {
    /// The `codex exec` arguments for a run in `dir` with `images`.
    fn argv(&self, dir: &Path, images: &[PathBuf]) -> Vec<String> {
        let s = |p: &Path| p.to_string_lossy().into_owned();
        let mut argv: Vec<String> = [
            "exec",
            "--ephemeral",
            "--ignore-user-config",
            "--ignore-rules",
            "--skip-git-repo-check",
            "--sandbox",
            &self.sandbox,
            "--color",
            "never",
            "--json",
            "--cd",
        ]
        .map(String::from)
        .into();
        argv.push(s(&dir.join("work")));
        argv.push("--output-last-message".into());
        argv.push(s(&dir.join("last.txt")));
        if let Some(m) = &self.model {
            argv.extend(["--model".into(), m.clone()]);
        }
        for p in images {
            argv.push(format!("--image={}", s(p)));
        }
        argv.extend(self.args.iter().cloned());
        argv.extend(["--".into(), "-".into()]);
        argv
    }

    /// The child's environment for a run in `dir`.
    fn child_env(&self, dir: &Path) -> Vec<(String, String)> {
        let s = |p: &Path| p.to_string_lossy().into_owned();
        let mut extra: Vec<(String, String)> = Vec::new();
        if !self.env.iter().any(|(k, _)| k == "LANG") {
            extra.push(("LANG".into(), "C.UTF-8".into()));
        }
        extra.extend(
            self.env
                .iter()
                .filter(|(k, _)| !matches!(k.as_str(), "HOME" | "CODEX_HOME" | "TMPDIR"))
                .cloned(),
        );
        extra.extend([
            ("HOME".into(), s(dir)),
            ("CODEX_HOME".into(), s(&dir.join("home"))),
            ("TMPDIR".into(), s(dir)),
        ]);
        crate::client::child_env(std::env::vars(), &extra)
    }

    /// Run `codex exec` once on `input` (see the module docs).
    pub async fn run(&self, input: &Input) -> Result<Run, ExecError> {
        let real = self.codex_home.join("auth.json");
        let read_login = || async { tokio::fs::read(&real).await.map_err(ExecError::Login) };
        // A run that may refresh the login goes alone; the next one reads
        // the login it copied back.
        let mut orig = read_login().await?;
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX));
        let _alone = if may_refresh(&orig, now) {
            let g = refresh_lock().lock().await;
            orig = read_login().await?;
            Some(g)
        } else {
            None
        };
        let setup = |what| move |e| ExecError::Setup(what, e);
        mkdir_private(&self.tmp_root)
            .await
            .map_err(setup("directory"))?;
        let dir = RunDir(self.tmp_root.join(run_name()));
        for d in ["", "home", "work", "refs"] {
            tokio::fs::DirBuilder::new()
                .mode(0o700)
                .create(dir.0.join(d))
                .await
                .map_err(setup("directory"))?;
        }
        let ours = dir.0.join("home/auth.json");
        write_private_atomic(&ours, &orig)
            .await
            .map_err(setup("login"))?;
        let mut images = Vec::with_capacity(input.images.len());
        for (i, (name, data)) in input.images.iter().enumerate() {
            let p = dir.0.join("refs").join(safe_name(name, i));
            write_private_atomic(&p, data)
                .await
                .map_err(setup("image"))?;
            images.push(p);
        }
        let out = run_capture(
            &self.command,
            &self.argv(&dir.0, &images),
            &dir.0.join("work"),
            &self.child_env(&dir.0),
            input.prompt.as_bytes(),
            self.timeout,
        )
        .await
        .map_err(ExecError::Spawn)?;
        let copied_back = {
            let _g = copy_back_lock().lock().await;
            crate::auth::copy_back(&Copied::new(real.clone(), ours, orig)).await
        };
        let last_message = tokio::fs::read(dir.0.join("last.txt"))
            .await
            .unwrap_or_default();
        Ok(Run {
            dir,
            code: out.0,
            stdout: out.1,
            stderr: out.2,
            last_message,
            copied_back,
        })
    }
}

/// Run `command argv` in `cwd` with only `env`, `input` on stdin; its exit
/// code (`None`: killed after `timeout`), stdout and stderr.
async fn run_capture(
    command: &str,
    argv: &[String],
    cwd: &Path,
    env: &[(String, String)],
    input: &[u8],
    timeout: Duration,
) -> std::io::Result<(Option<i32>, Vec<u8>, Vec<u8>)> {
    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
    let mut child = tokio::process::Command::new(command)
        .args(argv)
        .current_dir(cwd)
        .env_clear()
        .envs(env.iter().map(|(k, v)| (k, v)))
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()?;
    if let Some(mut stdin) = child.stdin.take() {
        let data = input.to_vec();
        tokio::spawn(async move {
            let _ = stdin.write_all(&data).await;
        });
    }
    let read = |r: Option<Box<dyn tokio::io::AsyncRead + Send + Unpin>>| {
        tokio::spawn(async move {
            let mut b = Vec::new();
            if let Some(mut r) = r {
                let _ = r.read_to_end(&mut b).await;
            }
            b
        })
    };
    let t_out = read(child.stdout.take().map(|o| Box::new(o) as _));
    let t_err = read(child.stderr.take().map(|e| Box::new(e) as _));
    let code = if let Ok(s) = tokio::time::timeout(timeout, child.wait()).await {
        Some(s?.code().unwrap_or(-1))
    } else {
        let _ = child.kill().await;
        None
    };
    // A grandchild may still hold the pipes open after a kill: don't wait
    // on it for long.
    let grace = if code.is_some() {
        timeout
    } else {
        Duration::from_secs(1)
    };
    let collect = |t: tokio::task::JoinHandle<Vec<u8>>| async move {
        let abort = t.abort_handle();
        tokio::time::timeout(grace, t).await.map_or_else(
            |_| {
                abort.abort();
                Vec::new()
            },
            Result::unwrap_or_default,
        )
    };
    let out = collect(t_out).await;
    let err = collect(t_err).await;
    Ok((code, out, err))
}

/// The regular files under `dir` (recursively; no symlinks) of at most
/// `max_bytes`, oldest first.
async fn files_oldest_first(dir: &Path, max_bytes: u64) -> Vec<PathBuf> {
    let mut files = Vec::new();
    let mut dirs = vec![dir.to_path_buf()];
    while let Some(d) = dirs.pop() {
        let Ok(mut rd) = tokio::fs::read_dir(&d).await else {
            continue;
        };
        while let Ok(Some(e)) = rd.next_entry().await {
            let Ok(ft) = e.file_type().await else {
                continue;
            };
            if ft.is_dir() {
                dirs.push(e.path());
            } else if ft.is_file() {
                match e.metadata().await {
                    Ok(m) if m.len() <= max_bytes => {
                        files.push((m.modified().unwrap_or(std::time::UNIX_EPOCH), e.path()));
                    }
                    _ => {}
                }
            }
        }
    }
    files.sort();
    files.into_iter().map(|(_, p)| p).collect()
}

/// Why a run failed, from its `--json` events and its log.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Failure {
    /// The `ChatGPT` login is spent or revoked (its refresh token was used
    /// up, expired or invalidated): someone must run `codex login` again.
    LoginDead,
    /// The service refused the login (a 401): what it said. Maybe a login.
    LoginRefused(String),
    /// The turn's failure, or the last error event.
    Said(String),
}

impl fmt::Display for Failure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LoginDead => f.write_str(
                "Codex's ChatGPT login is no longer valid (its refresh token was used up or \
                 revoked): run `codex login` again",
            ),
            Self::LoginRefused(m) => write!(f, "Codex's ChatGPT login was refused ({m})"),
            Self::Said(m) => f.write_str(m),
        }
    }
}

/// Why a `codex exec --json` run failed, from its events (`stdout`) and
/// its log (`stderr`) (pure): a dead login; else a 401; else the turn's
/// failure or the last error event (retry notices skipped), if any.
pub fn failure(stdout: &[u8], stderr: &[u8]) -> Option<Failure> {
    const DEAD: [&str; 6] = [
        "refresh_token_reused",
        "refresh token was already used",
        "refresh_token_expired",
        "refresh_token_invalidated",
        "Please log out and sign in again",
        "Please try signing in again",
    ];
    let err = String::from_utf8_lossy(stderr);
    let out = String::from_utf8_lossy(stdout);
    if DEAD.iter().any(|d| err.contains(d) || out.contains(d)) {
        return Some(Failure::LoginDead);
    }
    let mut said = None;
    for line in out.lines() {
        let Ok(v) = serde_json::from_str::<serde_json::Value>(line) else {
            continue;
        };
        let m = match v["type"].as_str() {
            Some("turn.failed") => v["error"]["message"].as_str(),
            Some("error") => v["message"].as_str(),
            _ => None,
        };
        if let Some(m) = m.filter(|m| !m.starts_with("Reconnecting...")) {
            said = Some(m.to_owned());
        }
    }
    said.map(|m| {
        if m.contains("401") {
            Failure::LoginRefused(m)
        } else {
            Failure::Said(m)
        }
    })
}

/// A prompt that has Codex make exactly `count` images from `brief` and
/// do nothing else; `aspect_ratio` like `16:9`; `with_images` when the
/// run attaches reference images.
pub fn image_prompt(
    brief: &str,
    count: u64,
    aspect_ratio: Option<&str>,
    with_images: bool,
) -> String {
    use fmt::Write as _;
    let mut t = format!(
        "Use your image generation tool to create exactly {count} image{} from the brief below",
        if count == 1 { "" } else { "s" }
    );
    if let Some(a) = aspect_ratio {
        let _ = write!(t, ", aspect ratio {a} (width:height)");
    }
    if with_images {
        t.push_str(", using the attached image(s) as the reference the brief refers to");
    }
    t.push_str(
        ". Generate each image separately. Do nothing else: no shell commands, no reading or \
         writing files, no web searches, no questions. When the images are made, reply with \
         only the word done.\n\nBrief:\n",
    );
    t.push_str(brief);
    t
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn image_prompt_says_what() {
        let t = image_prompt("a red apple", 2, Some("16:9"), true);
        assert!(t.contains("exactly 2 images") && t.contains("16:9") && t.contains("attached"));
        assert!(t.ends_with("Brief:\na red apple"), "{t}");
        let t = image_prompt("x", 1, None, false);
        assert!(
            t.contains("exactly 1 image ") && !t.contains("attached"),
            "{t}"
        );
    }

    #[test]
    fn failure_says_why() {
        // A spent login: stderr has the refresh error, the events only
        // "unauthorized (401)".
        let out = br#"{"type":"thread.started","thread_id":"t"}
{"type":"error","message":"Reconnecting... 2/5 (workspace routing discovery unauthorized (401))"}
{"type":"turn.failed","error":{"message":"workspace routing discovery unauthorized (401)"}}"#;
        let err = b"ERROR codex_login::auth::manager: Failed to refresh token: Your access token \
could not be refreshed because your refresh token was already used. Please log out and sign in again.";
        assert_eq!(failure(out, err), Some(Failure::LoginDead));
        assert!(Failure::LoginDead.to_string().contains("codex login"));
        assert_eq!(
            failure(out, b""),
            Some(Failure::LoginRefused(
                "workspace routing discovery unauthorized (401)".into()
            ))
        );
        let out = br#"{"type":"turn.failed","error":{"message":"You've hit your usage limit."}}"#;
        assert_eq!(
            failure(out, b""),
            Some(Failure::Said("You've hit your usage limit.".into()))
        );
        assert_eq!(failure(b"not json", b"noise"), None);
    }

    #[test]
    fn names_and_args() {
        assert_eq!(safe_name("../../etc/x.png", 0), "x.png");
        assert_eq!(safe_name("..", 1), "image-2");
        assert_eq!(safe_name(".hidden", 2), "image-3");
        let mut e = Exec::new("codex", "/h", "/t");
        e.model = Some("m".into());
        let a = e.argv(Path::new("/t/r"), &[PathBuf::from("/t/r/refs/a.png")]);
        assert_eq!(a[0], "exec");
        assert!(a.windows(2).any(|w| w == ["--sandbox", "read-only"]));
        assert!(a.windows(2).any(|w| w == ["--cd", "/t/r/work"]));
        assert!(a.windows(2).any(|w| w == ["--model", "m"]));
        assert!(a.contains(&"--image=/t/r/refs/a.png".to_owned()));
        assert!(a.ends_with(&["--".into(), "-".into()]));
        e.env = vec![
            ("PATH".into(), "/bin".into()),
            ("HOME".into(), "/nope".into()),
        ];
        let env = e.child_env(Path::new("/t/r"));
        let get = |k: &str| {
            env.iter()
                .filter(|(n, _)| n == k)
                .map(|(_, v)| v.as_str())
                .collect::<Vec<_>>()
        };
        assert_eq!(get("HOME"), ["/t/r"]);
        assert_eq!(get("CODEX_HOME"), ["/t/r/home"]);
        assert_eq!(get("PATH"), ["/bin"]);
        assert_eq!(get("LANG"), ["C.UTF-8"]);
    }

    #[tokio::test]
    async fn oldest_first_regular_files_only() {
        let d = tempfile::tempdir().unwrap();
        let g = d.path().join("t1");
        tokio::fs::create_dir_all(&g).await.unwrap();
        tokio::fs::write(g.join("a.png"), b"1").await.unwrap();
        tokio::time::sleep(Duration::from_millis(20)).await;
        tokio::fs::write(g.join("b.jpg"), b"2").await.unwrap();
        tokio::fs::write(g.join("big.png"), b"0123456789")
            .await
            .unwrap();
        std::os::unix::fs::symlink("/etc/passwd", g.join("link.png")).unwrap();
        let names: Vec<_> = files_oldest_first(d.path(), 4)
            .await
            .iter()
            .map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, ["a.png", "b.jpg"]);
        assert_eq!(
            files_oldest_first(&d.path().join("none"), 4).await,
            Vec::<PathBuf>::new()
        );
    }
}