feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
use rand::Rng;
|
2025-10-28 18:55:53 -07:00
|
|
|
use tracing::debug;
|
2025-10-29 15:33:57 -07:00
|
|
|
use tracing::error;
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
|
2025-04-25 11:44:22 -07:00
|
|
|
const INITIAL_DELAY_MS: u64 = 200;
|
2025-08-07 15:23:31 -07:00
|
|
|
const BACKOFF_FACTOR: f64 = 2.0;
|
2025-04-25 11:44:22 -07:00
|
|
|
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
pub(crate) fn backoff(attempt: u64) -> Duration {
|
2025-04-25 11:44:22 -07:00
|
|
|
let exp = BACKOFF_FACTOR.powi(attempt.saturating_sub(1) as i32);
|
|
|
|
|
let base = (INITIAL_DELAY_MS as f64 * exp) as u64;
|
|
|
|
|
let jitter = rand::rng().random_range(0.9..1.1);
|
|
|
|
|
Duration::from_millis((base as f64 * jitter) as u64)
|
feat: initial import of Rust implementation of Codex CLI in codex-rs/ (#629)
As stated in `codex-rs/README.md`:
Today, Codex CLI is written in TypeScript and requires Node.js 22+ to
run it. For a number of users, this runtime requirement inhibits
adoption: they would be better served by a standalone executable. As
maintainers, we want Codex to run efficiently in a wide range of
environments with minimal overhead. We also want to take advantage of
operating system-specific APIs to provide better sandboxing, where
possible.
To that end, we are moving forward with a Rust implementation of Codex
CLI contained in this folder, which has the following benefits:
- The CLI compiles to small, standalone, platform-specific binaries.
- Can make direct, native calls to
[seccomp](https://man7.org/linux/man-pages/man2/seccomp.2.html) and
[landlock](https://man7.org/linux/man-pages/man7/landlock.7.html) in
order to support sandboxing on Linux.
- No runtime garbage collection, resulting in lower memory consumption
and better, more predictable performance.
Currently, the Rust implementation is materially behind the TypeScript
implementation in functionality, so continue to use the TypeScript
implmentation for the time being. We will publish native executables via
GitHub Releases as soon as we feel the Rust version is usable.
2025-04-24 13:31:40 -07:00
|
|
|
}
|
2025-10-28 18:55:53 -07:00
|
|
|
|
2025-10-29 15:33:57 -07:00
|
|
|
pub(crate) fn error_or_panic(message: String) {
|
|
|
|
|
if cfg!(debug_assertions) || env!("CARGO_PKG_VERSION").contains("alpha") {
|
|
|
|
|
panic!("{message}");
|
|
|
|
|
} else {
|
|
|
|
|
error!("{message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-28 18:55:53 -07:00
|
|
|
pub(crate) fn try_parse_error_message(text: &str) -> String {
|
|
|
|
|
debug!("Parsing server error response: {}", text);
|
|
|
|
|
let json = serde_json::from_str::<serde_json::Value>(text).unwrap_or_default();
|
|
|
|
|
if let Some(error) = json.get("error")
|
|
|
|
|
&& let Some(message) = error.get("message")
|
|
|
|
|
&& let Some(message_str) = message.as_str()
|
|
|
|
|
{
|
|
|
|
|
return message_str.to_string();
|
|
|
|
|
}
|
|
|
|
|
if text.is_empty() {
|
|
|
|
|
return "Unknown error".to_string();
|
|
|
|
|
}
|
|
|
|
|
text.to_string()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_try_parse_error_message() {
|
|
|
|
|
let text = r#"{
|
|
|
|
|
"error": {
|
|
|
|
|
"message": "Your refresh token has already been used to generate a new access token. Please try signing in again.",
|
|
|
|
|
"type": "invalid_request_error",
|
|
|
|
|
"param": null,
|
|
|
|
|
"code": "refresh_token_reused"
|
|
|
|
|
}
|
|
|
|
|
}"#;
|
|
|
|
|
let message = try_parse_error_message(text);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
message,
|
|
|
|
|
"Your refresh token has already been used to generate a new access token. Please try signing in again."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_try_parse_error_message_no_error() {
|
|
|
|
|
let text = r#"{"message": "test"}"#;
|
|
|
|
|
let message = try_parse_error_message(text);
|
|
|
|
|
assert_eq!(message, r#"{"message": "test"}"#);
|
|
|
|
|
}
|
|
|
|
|
}
|