mirror of
https://github.com/openai/codex.git
synced 2026-03-05 21:45:28 +03:00
Add --fork session option to codex exec
This commit is contained in:
@@ -51,6 +51,7 @@ You can enable notifications by configuring a script that is run whenever the ag
|
||||
### `codex exec` to run Codex programmatically/non-interactively
|
||||
|
||||
To run Codex non-interactively, run `codex exec PROMPT` (you can also pass the prompt via `stdin`) and Codex will work on your task until it decides that it is done and exits. Output is printed to the terminal directly. You can set the `RUST_LOG` environment variable to see more about what's going on.
|
||||
Use `codex exec --fork <SESSION_ID> PROMPT` to fork an existing session without launching the interactive picker/UI.
|
||||
Use `codex exec --ephemeral ...` to run without persisting session rollout files to disk.
|
||||
|
||||
### Experimenting with the Codex Sandbox
|
||||
|
||||
@@ -1201,6 +1201,33 @@ mod tests {
|
||||
assert_eq!(args.session_id.as_deref(), Some("session-123"));
|
||||
assert_eq!(args.prompt.as_deref(), Some("re-review"));
|
||||
}
|
||||
#[test]
|
||||
fn exec_fork_accepts_prompt_positional() {
|
||||
let cli = MultitoolCli::try_parse_from([
|
||||
"codex",
|
||||
"exec",
|
||||
"--json",
|
||||
"--fork",
|
||||
"session-123",
|
||||
"2+2",
|
||||
])
|
||||
.expect("parse should succeed");
|
||||
|
||||
let Some(Subcommand::Exec(exec)) = cli.subcommand else {
|
||||
panic!("expected exec subcommand");
|
||||
};
|
||||
|
||||
assert_eq!(exec.fork_session_id.as_deref(), Some("session-123"));
|
||||
assert!(exec.command.is_none());
|
||||
assert_eq!(exec.prompt.as_deref(), Some("2+2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exec_fork_conflicts_with_resume_subcommand() {
|
||||
let parse_result =
|
||||
MultitoolCli::try_parse_from(["codex", "exec", "--fork", "session-123", "resume"]);
|
||||
assert!(parse_result.is_err());
|
||||
}
|
||||
|
||||
fn app_server_from_args(args: &[&str]) -> AppServerCommand {
|
||||
let cli = MultitoolCli::try_parse_from(args).expect("parse");
|
||||
|
||||
@@ -12,6 +12,12 @@ pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
pub command: Option<Command>,
|
||||
|
||||
/// Fork from an existing session id (or thread name) before sending the prompt.
|
||||
///
|
||||
/// This creates a new session with copied history, similar to `codex fork`.
|
||||
#[arg(long = "fork", value_name = "SESSION_ID", conflicts_with = "command")]
|
||||
pub fork_session_id: Option<String>,
|
||||
|
||||
/// Optional image(s) to attach to the initial prompt.
|
||||
#[arg(
|
||||
long = "image",
|
||||
@@ -315,4 +321,22 @@ mod tests {
|
||||
assert_eq!(args.session_id.as_deref(), Some("session-123"));
|
||||
assert_eq!(args.prompt.as_deref(), Some(PROMPT));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fork_option_parses_prompt() {
|
||||
const PROMPT: &str = "echo fork-non-interactive";
|
||||
let cli = Cli::parse_from(["codex-exec", "--fork", "session-123", "--json", PROMPT]);
|
||||
|
||||
assert_eq!(cli.fork_session_id.as_deref(), Some("session-123"));
|
||||
assert_eq!(cli.prompt.as_deref(), Some(PROMPT));
|
||||
assert!(cli.command.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fork_option_conflicts_with_subcommands() {
|
||||
let err = Cli::try_parse_from(["codex-exec", "--fork", "session-123", "resume"])
|
||||
.expect_err("fork should conflict with subcommands");
|
||||
|
||||
assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +105,7 @@ struct ExecRunArgs {
|
||||
cursor_ansi: bool,
|
||||
dangerously_bypass_approvals_and_sandbox: bool,
|
||||
exec_span: tracing::Span,
|
||||
fork_session_id: Option<String>,
|
||||
images: Vec<PathBuf>,
|
||||
json_mode: bool,
|
||||
last_message_file: Option<PathBuf>,
|
||||
@@ -132,6 +133,7 @@ pub async fn run_main(cli: Cli, arg0_paths: Arg0DispatchPaths) -> anyhow::Result
|
||||
|
||||
let Cli {
|
||||
command,
|
||||
fork_session_id,
|
||||
images,
|
||||
model: model_cli_arg,
|
||||
oss,
|
||||
@@ -388,6 +390,7 @@ pub async fn run_main(cli: Cli, arg0_paths: Arg0DispatchPaths) -> anyhow::Result
|
||||
cursor_ansi,
|
||||
dangerously_bypass_approvals_and_sandbox,
|
||||
exec_span: exec_span.clone(),
|
||||
fork_session_id,
|
||||
images,
|
||||
json_mode,
|
||||
last_message_file,
|
||||
@@ -409,6 +412,7 @@ async fn run_exec_session(args: ExecRunArgs) -> anyhow::Result<()> {
|
||||
cursor_ansi,
|
||||
dangerously_bypass_approvals_and_sandbox,
|
||||
exec_span,
|
||||
fork_session_id,
|
||||
images,
|
||||
json_mode,
|
||||
last_message_file,
|
||||
@@ -495,18 +499,28 @@ async fn run_exec_session(args: ExecRunArgs) -> anyhow::Result<()> {
|
||||
thread_id: primary_thread_id,
|
||||
thread,
|
||||
session_configured,
|
||||
} = if let Some(ExecCommand::Resume(args)) = command.as_ref() {
|
||||
let resume_path = resolve_resume_path(&config, args).await?;
|
||||
} = match command.as_ref() {
|
||||
Some(ExecCommand::Resume(args)) => {
|
||||
let resume_path = resolve_resume_path(&config, args).await?;
|
||||
|
||||
if let Some(path) = resume_path {
|
||||
thread_manager
|
||||
.resume_thread_from_rollout(config.clone(), path, auth_manager.clone())
|
||||
.await?
|
||||
} else {
|
||||
thread_manager.start_thread(config.clone()).await?
|
||||
if let Some(path) = resume_path {
|
||||
thread_manager
|
||||
.resume_thread_from_rollout(config.clone(), path, auth_manager.clone())
|
||||
.await?
|
||||
} else {
|
||||
thread_manager.start_thread(config.clone()).await?
|
||||
}
|
||||
}
|
||||
Some(ExecCommand::Review(_)) | None => {
|
||||
if let Some(session_id) = fork_session_id.as_deref() {
|
||||
let fork_path = resolve_fork_path(&config, session_id).await?;
|
||||
thread_manager
|
||||
.fork_thread(usize::MAX, config.clone(), fork_path, false)
|
||||
.await?
|
||||
} else {
|
||||
thread_manager.start_thread(config.clone()).await?
|
||||
}
|
||||
}
|
||||
} else {
|
||||
thread_manager.start_thread(config.clone()).await?
|
||||
};
|
||||
let primary_thread_id_for_span = primary_thread_id.to_string();
|
||||
exec_span.record("thread.id", primary_thread_id_for_span.as_str());
|
||||
@@ -827,18 +841,29 @@ async fn resolve_resume_path(
|
||||
}
|
||||
}
|
||||
} else if let Some(id_str) = args.session_id.as_deref() {
|
||||
if Uuid::parse_str(id_str).is_ok() {
|
||||
let path = find_thread_path_by_id_str(&config.codex_home, id_str).await?;
|
||||
Ok(path)
|
||||
} else {
|
||||
let path = find_thread_path_by_name_str(&config.codex_home, id_str).await?;
|
||||
Ok(path)
|
||||
}
|
||||
resolve_thread_path_by_id_or_name(config, id_str).await
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
async fn resolve_fork_path(config: &Config, session_id: &str) -> anyhow::Result<PathBuf> {
|
||||
resolve_thread_path_by_id_or_name(config, session_id)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("No saved session found with ID {session_id}"))
|
||||
}
|
||||
|
||||
async fn resolve_thread_path_by_id_or_name(
|
||||
config: &Config,
|
||||
id_or_name: &str,
|
||||
) -> anyhow::Result<Option<PathBuf>> {
|
||||
if Uuid::parse_str(id_or_name).is_ok() {
|
||||
find_thread_path_by_id_str(&config.codex_home, id_or_name).await
|
||||
} else {
|
||||
find_thread_path_by_name_str(&config.codex_home, id_or_name).await
|
||||
}
|
||||
}
|
||||
|
||||
fn load_output_schema(path: Option<PathBuf>) -> Option<Value> {
|
||||
let path = path?;
|
||||
|
||||
|
||||
@@ -79,4 +79,24 @@ mod tests {
|
||||
"reasoning_level=xhigh"
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn top_cli_parses_fork_option_with_root_config() {
|
||||
let cli = TopCli::parse_from([
|
||||
"codex-exec",
|
||||
"--config",
|
||||
"reasoning_level=xhigh",
|
||||
"--fork",
|
||||
"session-123",
|
||||
"echo fork",
|
||||
]);
|
||||
|
||||
assert_eq!(cli.inner.fork_session_id.as_deref(), Some("session-123"));
|
||||
assert!(cli.inner.command.is_none());
|
||||
assert_eq!(cli.inner.prompt.as_deref(), Some("echo fork"));
|
||||
assert_eq!(cli.config_overrides.raw_overrides.len(), 1);
|
||||
assert_eq!(
|
||||
cli.config_overrides.raw_overrides[0],
|
||||
"reasoning_level=xhigh"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
131
codex-rs/exec/tests/suite/fork.rs
Normal file
131
codex-rs/exec/tests/suite/fork.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
use anyhow::Context;
|
||||
use codex_utils_cargo_bin::find_resource;
|
||||
use core_test_support::test_codex_exec::test_codex_exec;
|
||||
use serde_json::Value;
|
||||
use std::string::ToString;
|
||||
use uuid::Uuid;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
/// Utility: scan the sessions dir for a rollout file that contains `marker`
|
||||
/// in any response_item.message.content entry. Returns the absolute path.
|
||||
fn find_session_file_containing_marker(
|
||||
sessions_dir: &std::path::Path,
|
||||
marker: &str,
|
||||
) -> Option<std::path::PathBuf> {
|
||||
for entry in WalkDir::new(sessions_dir) {
|
||||
let entry = match entry {
|
||||
Ok(e) => e,
|
||||
Err(_) => continue,
|
||||
};
|
||||
if !entry.file_type().is_file() {
|
||||
continue;
|
||||
}
|
||||
if !entry.file_name().to_string_lossy().ends_with(".jsonl") {
|
||||
continue;
|
||||
}
|
||||
let path = entry.path();
|
||||
let Ok(content) = std::fs::read_to_string(path) else {
|
||||
continue;
|
||||
};
|
||||
// Skip the first meta line and scan remaining JSONL entries.
|
||||
let mut lines = content.lines();
|
||||
if lines.next().is_none() {
|
||||
continue;
|
||||
}
|
||||
for line in lines {
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Ok(item): Result<Value, _> = serde_json::from_str(line) else {
|
||||
continue;
|
||||
};
|
||||
if item.get("type").and_then(|t| t.as_str()) == Some("response_item")
|
||||
&& let Some(payload) = item.get("payload")
|
||||
&& payload.get("type").and_then(|t| t.as_str()) == Some("message")
|
||||
&& payload
|
||||
.get("content")
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default()
|
||||
.contains(marker)
|
||||
{
|
||||
return Some(path.to_path_buf());
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Extract the conversation UUID from the first SessionMeta line in the rollout file.
|
||||
fn extract_conversation_id(path: &std::path::Path) -> String {
|
||||
let content = std::fs::read_to_string(path).unwrap();
|
||||
let mut lines = content.lines();
|
||||
let meta_line = lines.next().expect("missing meta line");
|
||||
let meta: Value = serde_json::from_str(meta_line).expect("invalid meta json");
|
||||
meta.get("payload")
|
||||
.and_then(|p| p.get("id"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_default()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn exec_fixture() -> anyhow::Result<std::path::PathBuf> {
|
||||
Ok(find_resource!("tests/fixtures/cli_responses_fixture.sse")?)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exec_fork_by_id_creates_new_session_with_copied_history() -> anyhow::Result<()> {
|
||||
let test = test_codex_exec();
|
||||
let fixture = exec_fixture()?;
|
||||
|
||||
let marker = format!("fork-base-{}", Uuid::new_v4());
|
||||
let prompt = format!("echo {marker}");
|
||||
|
||||
test.cmd()
|
||||
.env("CODEX_RS_SSE_FIXTURE", &fixture)
|
||||
.env("OPENAI_BASE_URL", "http://unused.local")
|
||||
.arg("--skip-git-repo-check")
|
||||
.arg(&prompt)
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
let sessions_dir = test.home_path().join("sessions");
|
||||
let original_path = find_session_file_containing_marker(&sessions_dir, &marker)
|
||||
.context("no session file found after first run")?;
|
||||
let session_id = extract_conversation_id(&original_path);
|
||||
|
||||
let marker2 = format!("fork-follow-up-{}", Uuid::new_v4());
|
||||
let prompt2 = format!("echo {marker2}");
|
||||
|
||||
test.cmd()
|
||||
.env("CODEX_RS_SSE_FIXTURE", &fixture)
|
||||
.env("OPENAI_BASE_URL", "http://unused.local")
|
||||
.arg("--skip-git-repo-check")
|
||||
.arg("--fork")
|
||||
.arg(&session_id)
|
||||
.arg(&prompt2)
|
||||
.assert()
|
||||
.success();
|
||||
|
||||
let forked_path = find_session_file_containing_marker(&sessions_dir, &marker2)
|
||||
.context("no forked session file found for second marker")?;
|
||||
|
||||
assert_ne!(
|
||||
forked_path, original_path,
|
||||
"fork should create a new session file"
|
||||
);
|
||||
|
||||
let forked_content = std::fs::read_to_string(&forked_path)?;
|
||||
assert!(forked_content.contains(&marker));
|
||||
assert!(forked_content.contains(&marker2));
|
||||
|
||||
let original_content = std::fs::read_to_string(&original_path)?;
|
||||
assert!(original_content.contains(&marker));
|
||||
assert!(
|
||||
!original_content.contains(&marker2),
|
||||
"original session should not receive the forked prompt"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -3,6 +3,7 @@ mod add_dir;
|
||||
mod apply_patch;
|
||||
mod auth_env;
|
||||
mod ephemeral;
|
||||
mod fork;
|
||||
mod mcp_required_exit;
|
||||
mod originator;
|
||||
mod output_schema;
|
||||
|
||||
Reference in New Issue
Block a user