From c36e821610feb7a692efd4dc075a17d2a9b02b7f Mon Sep 17 00:00:00 2001 From: Albin Cassirer Date: Tue, 21 Apr 2026 12:01:18 -0700 Subject: [PATCH] Add debug trace reduction command --- codex-rs/Cargo.lock | 1 + codex-rs/cli/Cargo.toml | 1 + codex-rs/cli/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 3f26f563f8..225d7f692a 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -2116,6 +2116,7 @@ dependencies = [ "codex-protocol", "codex-responses-api-proxy", "codex-rmcp-client", + "codex-rollout-trace", "codex-sandboxing", "codex-state", "codex-stdio-to-uds", diff --git a/codex-rs/cli/Cargo.toml b/codex-rs/cli/Cargo.toml index d318297f8f..2a9c5a6ff7 100644 --- a/codex-rs/cli/Cargo.toml +++ b/codex-rs/cli/Cargo.toml @@ -43,6 +43,7 @@ codex-model-provider = { workspace = true } codex-protocol = { workspace = true } codex-responses-api-proxy = { workspace = true } codex-rmcp-client = { workspace = true } +codex-rollout-trace = { workspace = true } codex-sandboxing = { workspace = true } codex-state = { workspace = true } codex-stdio-to-uds = { workspace = true } diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index 3dee811fac..f378afad2c 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -22,6 +22,8 @@ use codex_exec::Command as ExecCommand; use codex_exec::ReviewArgs; use codex_execpolicy::ExecPolicyCheckCommand; use codex_responses_api_proxy::Args as ResponsesApiProxyArgs; +use codex_rollout_trace::REDUCED_STATE_FILE_NAME; +use codex_rollout_trace::replay_bundle; use codex_state::StateRuntime; use codex_state::state_db_path; use codex_tui::AppExitInfo; @@ -216,6 +218,10 @@ enum DebugSubcommand { /// Render the model-visible prompt input list as JSON. PromptInput(DebugPromptInputCommand), + /// Replay a rollout trace bundle and write reduced state JSON. + #[clap(hide = true)] + TraceReduce(DebugTraceReduceCommand), + /// Internal: reset local memory state for a fresh start. #[clap(hide = true)] ClearMemories, @@ -257,6 +263,17 @@ struct DebugModelsCommand { bundled: bool, } +#[derive(Debug, Parser)] +struct DebugTraceReduceCommand { + /// Trace bundle directory containing manifest.json and trace.jsonl. + #[arg(value_name = "TRACE_BUNDLE")] + trace_bundle: PathBuf, + + /// Output path for reduced RolloutTrace JSON. Defaults to TRACE_BUNDLE/state.json. + #[arg(long = "output", short = 'o', value_name = "FILE")] + output: Option, +} + #[derive(Debug, Parser)] struct ResumeCommand { /// Conversation/session id (UUID) or thread name. UUIDs take precedence if it parses. @@ -1065,6 +1082,14 @@ async fn cli_main(arg0_paths: Arg0DispatchPaths) -> anyhow::Result<()> { ) .await?; } + DebugSubcommand::TraceReduce(cmd) => { + reject_remote_mode_for_subcommand( + root_remote.as_deref(), + root_remote_auth_token_env.as_deref(), + "debug trace-reduce", + )?; + run_debug_trace_reduce_command(cmd).await?; + } DebugSubcommand::ClearMemories => { reject_remote_mode_for_subcommand( root_remote.as_deref(), @@ -1265,6 +1290,19 @@ fn maybe_print_under_development_feature_warning( ); } +async fn run_debug_trace_reduce_command(cmd: DebugTraceReduceCommand) -> anyhow::Result<()> { + let output = cmd + .output + .unwrap_or_else(|| cmd.trace_bundle.join(REDUCED_STATE_FILE_NAME)); + + let trace = replay_bundle(&cmd.trace_bundle)?; + let reduced_json = serde_json::to_vec_pretty(&trace)?; + tokio::fs::write(&output, reduced_json).await?; + println!("{}", output.display()); + + Ok(()) +} + async fn run_debug_prompt_input_command( cmd: DebugPromptInputCommand, root_config_overrides: CliConfigOverrides,