This commit is contained in:
jif-oai
2025-10-13 17:56:44 +01:00
parent 26f7c46856
commit 8245a4f53b
17 changed files with 3334 additions and 21 deletions
+450
View File
@@ -0,0 +1,450 @@
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::bail;
use chrono::SecondsFormat;
use chrono::Utc;
use clap::Parser;
use clap::Subcommand;
use codex_common::CliConfigOverrides;
use codex_core::CodexAuth;
use codex_core::auth::read_codex_api_key_from_env;
use codex_core::auth::read_openai_api_key_from_env;
use codex_core::config::Config;
use codex_core::config::ConfigOverrides;
use codex_infty::InftyOrchestrator;
use codex_infty::ResumeParams;
use codex_infty::RoleConfig;
use codex_infty::RunExecutionOptions;
use codex_infty::RunParams;
use codex_infty::RunStore;
use serde::Serialize;
const DEFAULT_TIMEOUT_SECS: u64 = 60;
#[derive(Debug, Parser)]
pub struct InftyCli {
#[clap(flatten)]
pub config_overrides: CliConfigOverrides,
/// Override the default runs root (`~/.codex/infty`).
#[arg(long = "runs-root", value_name = "DIR")]
pub runs_root: Option<PathBuf>,
#[command(subcommand)]
command: InftyCommand,
}
#[derive(Debug, Subcommand)]
enum InftyCommand {
/// Create a new run store and spawn solver/director sessions.
Create(CreateArgs),
/// List stored runs.
List(ListArgs),
/// Show metadata for a stored run.
Show(ShowArgs),
/// Send a message to a role within a run and print the first reply.
Drive(DriveArgs),
}
#[derive(Debug, Parser)]
struct CreateArgs {
/// Explicit run id. If omitted, a timestamp-based id is generated.
#[arg(long = "run-id", value_name = "RUN_ID")]
run_id: Option<String>,
/// Optional objective to send to the solver immediately after creation.
#[arg(long)]
objective: Option<String>,
/// Timeout in seconds when waiting for the solver reply to --objective.
#[arg(long = "timeout-secs", default_value_t = DEFAULT_TIMEOUT_SECS)]
timeout_secs: u64,
}
#[derive(Debug, Parser)]
struct ListArgs {
/// Emit JSON describing the stored runs.
#[arg(long)]
json: bool,
}
#[derive(Debug, Parser)]
struct ShowArgs {
/// Run id to display.
run_id: String,
/// Emit JSON metadata instead of human-readable text.
#[arg(long)]
json: bool,
}
#[derive(Debug, Parser)]
struct DriveArgs {
/// Run id to resume.
run_id: String,
/// Role to address (e.g. solver, director).
role: String,
/// Message to send to the role.
message: String,
/// Timeout in seconds to await the first assistant message.
#[arg(long = "timeout-secs", default_value_t = DEFAULT_TIMEOUT_SECS)]
timeout_secs: u64,
}
#[derive(Debug, Serialize)]
struct RunSummary {
run_id: String,
path: String,
created_at: String,
updated_at: String,
roles: Vec<String>,
}
impl InftyCli {
pub async fn run(self) -> Result<()> {
let InftyCli {
config_overrides,
runs_root,
command,
} = self;
match command {
InftyCommand::Create(args) => {
run_create(config_overrides, runs_root, args).await?;
}
InftyCommand::List(args) => run_list(runs_root, args)?,
InftyCommand::Show(args) => run_show(runs_root, args)?,
InftyCommand::Drive(args) => {
run_drive(config_overrides, runs_root, args).await?;
}
}
Ok(())
}
}
async fn run_create(
config_overrides: CliConfigOverrides,
runs_root_override: Option<PathBuf>,
args: CreateArgs,
) -> Result<()> {
let config = load_config(config_overrides).await?;
let auth = load_auth(&config)?;
let runs_root = resolve_runs_root(runs_root_override)?;
let mut run_id = if let Some(id) = args.run_id {
id
} else {
generate_run_id()
};
run_id = run_id.trim().to_string();
validate_run_id(&run_id)?;
let run_path = runs_root.join(&run_id);
if run_path.exists() {
bail!("run {run_id} already exists at {}", run_path.display());
}
let orchestrator = InftyOrchestrator::with_runs_root(auth, runs_root.clone());
let run_params = RunParams {
run_id: run_id.clone(),
run_root: Some(run_path.clone()),
solver: RoleConfig::new("solver", config.clone()),
director: RoleConfig::new("director", config.clone()),
verifiers: Vec::new(),
};
if let Some(objective) = args.objective {
let mut options = RunExecutionOptions::default();
options.objective = Some(objective);
let timeout = Duration::from_secs(args.timeout_secs);
options.director_timeout = timeout;
options.verifier_timeout = timeout;
let outcome = orchestrator
.execute_new_run(run_params, options)
.await
.with_context(|| format!("failed to execute run {run_id}"))?;
println!(
"Run {run_id} completed. Deliverable: {}",
outcome.deliverable_path.display()
);
if let Some(summary) = outcome.summary {
println!("Summary: {summary}");
}
} else {
let sessions = orchestrator
.spawn_run(run_params)
.await
.with_context(|| format!("failed to create run {run_id}"))?;
println!(
"Created run {run_id} at {}",
sessions.store.path().display()
);
}
Ok(())
}
fn run_list(runs_root_override: Option<PathBuf>, args: ListArgs) -> Result<()> {
let runs_root = resolve_runs_root(runs_root_override)?;
let listings = collect_run_summaries(&runs_root)?;
if args.json {
println!("{}", serde_json::to_string_pretty(&listings)?);
return Ok(());
}
if listings.is_empty() {
println!("No runs found under {}", runs_root.display());
return Ok(());
}
println!("Runs in {}", runs_root.display());
for summary in listings {
println!(
"{}\t{}\t{}",
summary.run_id, summary.updated_at, summary.path
);
}
Ok(())
}
fn run_show(runs_root_override: Option<PathBuf>, args: ShowArgs) -> Result<()> {
validate_run_id(&args.run_id)?;
let runs_root = resolve_runs_root(runs_root_override)?;
let run_path = runs_root.join(&args.run_id);
let store =
RunStore::load(&run_path).with_context(|| format!("failed to load run {}", args.run_id))?;
let metadata = store.metadata();
let summary = RunSummary {
run_id: metadata.run_id.clone(),
path: run_path.display().to_string(),
created_at: metadata
.created_at
.to_rfc3339_opts(SecondsFormat::Secs, true),
updated_at: metadata
.updated_at
.to_rfc3339_opts(SecondsFormat::Secs, true),
roles: metadata
.roles
.iter()
.map(|role| role.role.clone())
.collect(),
};
if args.json {
println!("{}", serde_json::to_string_pretty(&summary)?);
return Ok(());
}
println!("Run: {}", summary.run_id);
println!("Path: {}", summary.path);
println!("Created: {}", summary.created_at);
println!("Updated: {}", summary.updated_at);
println!("Roles: {}", summary.roles.join(", "));
Ok(())
}
async fn run_drive(
config_overrides: CliConfigOverrides,
runs_root_override: Option<PathBuf>,
args: DriveArgs,
) -> Result<()> {
validate_run_id(&args.run_id)?;
let config = load_config(config_overrides).await?;
let auth = load_auth(&config)?;
let runs_root = resolve_runs_root(runs_root_override)?;
let run_path = runs_root.join(&args.run_id);
let store =
RunStore::load(&run_path).with_context(|| format!("failed to load run {}", args.run_id))?;
let solver_role = store
.role_metadata("solver")
.ok_or_else(|| anyhow!("run {} is missing solver role", args.run_id))?;
let director_role = store
.role_metadata("director")
.ok_or_else(|| anyhow!("run {} is missing director role", args.run_id))?;
let verifiers: Vec<_> = store
.metadata()
.roles
.iter()
.filter(|role| role.role != solver_role.role && role.role != director_role.role)
.map(|role| RoleConfig::new(role.role.clone(), config.clone()))
.collect();
let orchestrator = InftyOrchestrator::with_runs_root(auth, runs_root);
let sessions = orchestrator
.resume_run(ResumeParams {
run_path: run_path.clone(),
solver: RoleConfig::new(solver_role.role.clone(), config.clone()),
director: RoleConfig::new(director_role.role.clone(), config.clone()),
verifiers,
})
.await
.with_context(|| format!("failed to resume run {}", args.run_id))?;
let timeout = Duration::from_secs(args.timeout_secs);
let reply = orchestrator
.call_role(&sessions.run_id, &args.role, args.message, timeout, None)
.await
.with_context(|| {
format!(
"failed to deliver message to role {} in run {}",
args.role, sessions.run_id
)
})?;
println!("{}", reply.message.message);
Ok(())
}
fn validate_run_id(run_id: &str) -> Result<()> {
if run_id.is_empty() {
bail!("run id must not be empty");
}
if run_id.starts_with('.') || run_id.ends_with('.') {
bail!("run id must not begin or end with '.'");
}
if run_id
.chars()
.any(|c| !(c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')))
{
bail!("run id may only contain ASCII alphanumerics, '-', '_', or '.'");
}
Ok(())
}
fn generate_run_id() -> String {
let timestamp = Utc::now().format("run-%Y%m%d-%H%M%S");
format!("{timestamp}")
}
async fn load_config(cli_overrides: CliConfigOverrides) -> Result<Config> {
let overrides = cli_overrides
.parse_overrides()
.map_err(|err| anyhow!("failed to parse -c overrides: {err}"))?;
Config::load_with_cli_overrides(overrides, ConfigOverrides::default())
.await
.context("failed to load Codex configuration")
}
fn load_auth(config: &Config) -> Result<CodexAuth> {
if let Some(auth) =
CodexAuth::from_codex_home(&config.codex_home).context("failed to read auth.json")?
{
return Ok(auth);
}
if let Some(api_key) = read_codex_api_key_from_env() {
return Ok(CodexAuth::from_api_key(&api_key));
}
if let Some(api_key) = read_openai_api_key_from_env() {
return Ok(CodexAuth::from_api_key(&api_key));
}
bail!("no Codex authentication found. Run `codex login` or set OPENAI_API_KEY.");
}
fn resolve_runs_root(override_path: Option<PathBuf>) -> Result<PathBuf> {
if let Some(path) = override_path {
return Ok(path);
}
Ok(codex_infty::default_runs_root()?)
}
fn collect_run_summaries(root: &Path) -> Result<Vec<RunSummary>> {
let mut summaries = Vec::new();
let iter = match fs::read_dir(root) {
Ok(read_dir) => read_dir,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(summaries),
Err(err) => {
return Err(
anyhow!(err).context(format!("failed to read runs root {}", root.display()))
);
}
};
for entry in iter {
let entry = entry?;
if !entry.file_type()?.is_dir() {
continue;
}
let run_path = entry.path();
let store = match RunStore::load(&run_path) {
Ok(store) => store,
Err(err) => {
eprintln!(
"Skipping {}: failed to load run metadata: {err}",
run_path.display()
);
continue;
}
};
let metadata = store.metadata();
summaries.push(RunSummary {
run_id: metadata.run_id.clone(),
path: run_path.display().to_string(),
created_at: metadata
.created_at
.to_rfc3339_opts(SecondsFormat::Secs, true),
updated_at: metadata
.updated_at
.to_rfc3339_opts(SecondsFormat::Secs, true),
roles: metadata
.roles
.iter()
.map(|role| role.role.clone())
.collect(),
});
}
summaries.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
Ok(summaries)
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn validates_run_ids() {
assert!(validate_run_id("run-20241030-123000").is_ok());
assert!(validate_run_id("run.alpha").is_ok());
assert!(validate_run_id("").is_err());
assert!(validate_run_id("..bad").is_err());
assert!(validate_run_id("bad/value").is_err());
}
#[test]
fn generates_timestamped_run_id() {
let run_id = generate_run_id();
assert!(run_id.starts_with("run-"));
assert_eq!(run_id.len(), "run-YYYYMMDD-HHMMSS".len());
}
#[test]
fn collect_summaries_returns_empty_for_missing_root() {
let temp = TempDir::new().expect("temp dir");
let missing = temp.path().join("not-present");
let summaries = collect_run_summaries(&missing).expect("collect");
assert!(summaries.is_empty());
}
}
+13
View File
@@ -23,8 +23,10 @@ use owo_colors::OwoColorize;
use std::path::PathBuf;
use supports_color::Stream;
mod infty;
mod mcp_cmd;
use crate::infty::InftyCli;
use crate::mcp_cmd::McpCli;
/// Codex CLI
@@ -97,6 +99,10 @@ enum Subcommand {
/// Internal: run the responses API proxy.
#[clap(hide = true)]
ResponsesApiProxy(ResponsesApiProxyArgs),
/// [experimental] Manage Codex Infty long-running task runs.
#[clap(name = "infty")]
Infty(InftyCli),
}
#[derive(Debug, Parser)]
@@ -343,6 +349,13 @@ async fn cli_main(codex_linux_sandbox_exe: Option<PathBuf>) -> anyhow::Result<()
);
codex_cloud_tasks::run_main(cloud_cli, codex_linux_sandbox_exe).await?;
}
Some(Subcommand::Infty(mut infty_cli)) => {
prepend_config_flags(
&mut infty_cli.config_overrides,
root_config_overrides.clone(),
);
infty_cli.run().await?;
}
Some(Subcommand::Sandbox(sandbox_args)) => match sandbox_args.cmd {
SandboxCommand::Macos(mut seatbelt_cli) => {
prepend_config_flags(