Lightweight coding agent that runs in your terminal
Rust
Track accepted conversation history in rollout traces (#56312)
cassirer-openai5b792948f941
diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lockindex 0ff995088b..bfc383e4d7 100644--- a/codex-rs/Cargo.lock+++ b/codex-rs/Cargo.lock@@ -2562,6 +2562,7 @@ dependencies = ["codex-utils-redacted-string","codex-websocket-auth","codex-windows-sandbox",+ "core_test_support","crossterm","flate2","futures",diff --git a/codex-rs/cli/Cargo.toml b/codex-rs/cli/Cargo.tomlindex cad79b2cb2..c812cf9887 100644--- a/codex-rs/cli/Cargo.toml+++ b/codex-rs/cli/Cargo.toml@@ -133,6 +133,7 @@ assert_matches = { workspace = true }codex-app-server-client = { workspace = true }codex-utils-cargo-bin = { workspace = true }codex-utils-pty = { workspace = true }+core_test_support = { workspace = true }flate2 = { workspace = true }insta = { workspace = true }jsonwebtoken = { workspace = true }diff --git a/codex-rs/cli/tests/rollout_trace.rs b/codex-rs/cli/tests/rollout_trace.rsnew file mode 100644index 0000000000..c9bfeb2492--- /dev/null+++ b/codex-rs/cli/tests/rollout_trace.rs@@ -0,0 +1,261 @@+//! Exercise trace acceptance through the CLI app-server and its offline reducer.++use anyhow::Result;+use app_test_support::MockResponsesConfig;+use app_test_support::TestAppServer;+use app_test_support::create_mock_responses_server_sequence;+use codex_app_server_protocol::AgentMessageDeltaNotification;+use codex_app_server_protocol::ClientRequest;+use codex_app_server_protocol::ThreadStartParams;+use codex_app_server_protocol::ThreadStartResponse;+use codex_app_server_protocol::TurnCompletedNotification;+use codex_app_server_protocol::TurnInterruptParams;+use codex_app_server_protocol::TurnInterruptResponse;+use codex_app_server_protocol::TurnStartParams;+use codex_app_server_protocol::TurnStartResponse;+use codex_app_server_protocol::TurnStatus;+use codex_app_server_protocol::UserInput;+use codex_rollout_trace::ConversationRole;+use codex_rollout_trace::ExecutionStatus;+use codex_rollout_trace::ProducerRef;+use codex_rollout_trace::RolloutTrace;+use core_test_support::responses;+use core_test_support::streaming_sse::StreamingSseChunk;+use core_test_support::streaming_sse::start_streaming_sse_server;+use pretty_assertions::assert_eq;+use serde_json::json;+use tempfile::TempDir;+use tokio::sync::oneshot;++#[tokio::test]+async fn accepted_output_survives_success_and_stream_failure() -> Result<()> {+ let codex = codex_utils_cargo_bin::cargo_bin("codex")?;+ for (source_id, terminal, status) in [+ (+ Some("msg-original"),+ "response.completed",+ ExecutionStatus::Completed,+ ),+ (None, "response.completed", ExecutionStatus::Completed),+ (+ Some("msg-original"),+ "response.failed",+ ExecutionStatus::Failed,+ ),+ (None, "response.failed", ExecutionStatus::Failed),+ ] {+ let home = TempDir::new()?;+ let traces = TempDir::new()?;+ let item = json!({+ "type": "message", "id": source_id, "role": "assistant",+ "content": [{"type": "output_text", "text": "accepted answer"}]+ });+ let mut terminal_response = json!({"id": "resp-1"});+ if status == ExecutionStatus::Failed {+ terminal_response["error"] =+ json!({"code": "server_error", "message": "stream failed"});+ }+ let response = [+ json!({"type": "response.created", "response": {"id": "resp-1"}}),+ json!({"type": "response.output_item.added", "item": item}),+ json!({"type": "response.output_item.done", "item": item}),+ json!({"type": terminal, "response": terminal_response}),+ ]+ .into_iter()+ .map(|event| format!("data: {event}\n\n"))+ .collect();+ let server = create_mock_responses_server_sequence(vec![response]).await;+ MockResponsesConfig::new(&server.uri()).write(home.path())?;+ let mut app = TestAppServer::builder()+ .with_program(&codex)+ .with_plugin_startup_tasks()+ .with_args(&["app-server"])+ .with_codex_home(home.path())+ .with_env_overrides(&[("CODEX_ROLLOUT_TRACE_ROOT", traces.path().to_str())])+ .build_initialized()+ .await?;+ let ThreadStartResponse { thread, .. } =+ app.start_thread(ThreadStartParams::default()).await?;+ let TurnStartResponse { turn } = app+ .request(|request_id| ClientRequest::TurnStart {+ request_id,+ params: TurnStartParams {+ thread_id: thread.id.clone(),+ input: vec![UserInput::Text {+ text: "answer once".into(),+ text_elements: Vec::new(),+ }],+ ..Default::default()+ },+ })+ .await?;+ let completed: TurnCompletedNotification = app.read_notification("turn/completed").await?;+ assert_eq!(+ completed.turn.status,+ if status == ExecutionStatus::Completed {+ TurnStatus::Completed+ } else {+ TurnStatus::Failed+ }+ );+ assert!(app.shutdown_gracefully().await?.success());++ // Exercise the exported artifact, not just an in-process reducer call.+ // There is no following request to prove acceptance of this final item.+ let bundle = std::fs::read_dir(traces.path())?.next().unwrap()?.path();+ let reduced = tokio::process::Command::new(&codex)+ .env("CODEX_HOME", home.path())+ .args(["debug", "trace-reduce"])+ .arg(&bundle)+ .output()+ .await?;+ assert!(+ reduced.status.success(),+ "{}",+ String::from_utf8_lossy(&reduced.stderr)+ );+ let rollout: RolloutTrace =+ serde_json::from_reader(std::fs::File::open(bundle.join("state.json"))?)?;+ let call = rollout.inference_calls.values().next().unwrap();+ let output_id = &call.response_item_ids[0];+ let output = &rollout.conversation_items[output_id];+ let mut expected = call.request_item_ids.clone();+ expected.push(output_id.clone());+ assert_eq!(rollout.threads[&thread.id].conversation_item_ids, expected);+ assert_eq!(rollout.codex_turns[&turn.id].execution.status, status);+ assert_eq!(call.execution.status, status);+ assert_eq!(+ output.produced_by,+ vec![ProducerRef::Inference {+ inference_call_id: call.inference_call_id.clone()+ }]+ );+ assert!(+ output+ .responses_item_id+ .as_ref()+ .is_some_and(|id| !id.is_empty())+ );+ if let Some(source_id) = source_id {+ assert_eq!(output.responses_item_id.as_deref(), Some(source_id));+ }+ let requests = server.received_requests().await.unwrap();+ let request = requests+ .iter()+ .find(|request| request.url.path().ends_with("/responses"))+ .unwrap();+ assert_eq!(+ request.headers["x-codex-inference-call-id"].to_str()?,+ call.inference_call_id+ );+ }+ Ok(())+}++#[tokio::test]+async fn accepted_output_survives_interruption_and_shutdown() -> Result<()> {+ let codex = codex_utils_cargo_bin::cargo_bin("codex")?;+ let home = TempDir::new()?;+ let traces = TempDir::new()?;+ let (release_response, response_gate) = oneshot::channel();+ let (server, _) = start_streaming_sse_server(vec![vec![+ StreamingSseChunk {+ gate: None,+ body: responses::sse(vec![+ responses::ev_response_created("resp-1"),+ responses::ev_assistant_message("accepted", "Keep this answer."),+ responses::ev_message_item_added("unfinished", ""),+ responses::ev_output_text_delta("Discard this unfinished text"),+ ]),+ },+ // Keep the provider stream open so only the client interrupt ends it.+ StreamingSseChunk {+ gate: Some(response_gate),+ body: String::new(),+ },+ ]])+ .await;+ MockResponsesConfig::new(server.uri()).write(home.path())?;+ let mut app = TestAppServer::builder()+ .with_program(&codex)+ .with_plugin_startup_tasks()+ .with_args(&["app-server"])+ .with_codex_home(home.path())+ .with_env_overrides(&[("CODEX_ROLLOUT_TRACE_ROOT", traces.path().to_str())])+ .build_initialized()+ .await?;+ let ThreadStartResponse { thread, .. } = app.start_thread(ThreadStartParams::default()).await?;+ let TurnStartResponse { turn } = app+ .request(|request_id| ClientRequest::TurnStart {+ request_id,+ params: TurnStartParams {+ thread_id: thread.id.clone(),+ input: vec![UserInput::Text {+ text: "answer once".into(),+ text_elements: Vec::new(),+ }],+ ..Default::default()+ },+ })+ .await?;++ // Reaching the second item proves the session processed the first one.+ let delta: AgentMessageDeltaNotification =+ app.read_notification("item/agentMessage/delta").await?;+ assert_eq!(delta.item_id, "unfinished");+ let _: TurnInterruptResponse = app+ .request(|request_id| ClientRequest::TurnInterrupt {+ request_id,+ params: TurnInterruptParams {+ thread_id: thread.id.clone(),+ turn_id: turn.id.clone(),+ },+ })+ .await?;+ let completed: TurnCompletedNotification = app.read_notification("turn/completed").await?;+ assert_eq!(completed.turn.status, TurnStatus::Interrupted);+ assert!(app.shutdown_gracefully().await?.success());+ drop(release_response);+ server.shutdown().await;++ // Reduce immediately after shutdown: the background stream recorder must+ // have preserved the accepted output even without response.completed.+ let bundle = std::fs::read_dir(traces.path())?.next().unwrap()?.path();+ let reduced = tokio::process::Command::new(&codex)+ .env("CODEX_HOME", home.path())+ .args(["debug", "trace-reduce"])+ .arg(&bundle)+ .output()+ .await?;+ assert!(+ reduced.status.success(),+ "{}",+ String::from_utf8_lossy(&reduced.stderr)+ );+ let rollout: RolloutTrace =+ serde_json::from_reader(std::fs::File::open(bundle.join("state.json"))?)?;+ assert_eq!(rollout.inference_calls.len(), 1);+ let call = rollout.inference_calls.values().next().unwrap();+ assert_eq!(call.execution.status, ExecutionStatus::Cancelled);+ assert_eq!(+ rollout.codex_turns[&turn.id].execution.status,+ ExecutionStatus::Cancelled+ );+ assert_eq!(call.response_item_ids.len(), 1);+ let output_id = &call.response_item_ids[0];+ let output = &rollout.conversation_items[output_id];+ assert_eq!(output.responses_item_id.as_deref(), Some("accepted"));+ assert_eq!(+ output.produced_by,+ vec![ProducerRef::Inference {+ inference_call_id: call.inference_call_id.clone(),+ }]+ );+ let accepted_messages: Vec<_> = rollout.threads[&thread.id]+ .conversation_item_ids+ .iter()+ .filter(|id| rollout.conversation_items[*id].role == ConversationRole::Assistant)+ .collect();+ assert_eq!(accepted_messages, vec![output_id]);+ Ok(())+}diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rsindex 152fa7145a..99550cb20c 100644--- a/codex-rs/core/src/client.rs+++ b/codex-rs/core/src/client.rs@@ -2567,6 +2567,7 @@ wherelet mut logged_error = false;let mut tx_last_response = Some(tx_last_response);let mut items_added: Vec<ResponseItem> = Vec::new();+ let mut active_item_id: Option<ResponseItemId> = None;let (request_start, mut ttft_ms) = (Instant::now(), None);let mut api_stream = api_stream;let upstream_request_id = upstream_request_id.as_deref();@@ -2589,7 +2590,17 @@ wherebreak;};match event {- Ok(ResponseEvent::OutputItemDone(item)) => {+ Ok(ResponseEvent::OutputItemDone(mut item)) => {+ // Assign IDs after interceptors, which may replace provider items.+ // Reuse the added event's ID so tracing and the harness agree.+ if item.id().is_none_or(|id| id.is_empty()) {+ item.set_id(+ active_item_id+ .take()+ .or_else(|| item.id_prefix().map(ResponseItemId::new)),+ );+ }+ active_item_id = None;items_added.push(item.clone());if tx_event.send(Ok(ResponseEvent::OutputItemDone(item)))@@ -2639,7 +2650,13 @@ wherereturn;}}- Ok(event) => {+ Ok(mut event) => {+ if let ResponseEvent::OutputItemAdded(item) = &mut event {+ if item.id().is_none_or(|id| id.is_empty()) {+ item.set_id(item.id_prefix().map(ResponseItemId::new));+ }+ active_item_id = item.id().cloned();+ }if matches!(&event, ResponseEvent::OutputItemAdded(_)) && ttft_ms.is_none() {ttft_ms = Some(i64::try_from(request_start.elapsed().as_millis()).unwrap_or(i64::MAX),diff --git a/codex-rs/core/src/client_tests.rs b/codex-rs/core/src/client_tests.rsindex f929e2c969..3e83bceadd 100644--- a/codex-rs/core/src/client_tests.rs+++ b/codex-rs/core/src/client_tests.rs@@ -1662,6 +1662,11 @@ async fn dropped_response_stream_traces_cancelled_partial_output() -> anyhow::Reassert_eq!(inference.execution.status, ExecutionStatus::Cancelled);assert_eq!(inference.response_item_ids.len(), 1);+ // Receiving an item from the transport does not establish history acceptance.+ assert_eq!(+ rollout.threads["thread-root"].conversation_item_ids,+ inference.request_item_ids+ );assert_eq!(rollout.raw_payloads.len(), 2);Ok(())diff --git a/codex-rs/rollout-trace/src/bundle.rs b/codex-rs/rollout-trace/src/bundle.rsindex 8f67da3b3b..9f0df19c5e 100644--- a/codex-rs/rollout-trace/src/bundle.rs+++ b/codex-rs/rollout-trace/src/bundle.rs@@ -10,8 +10,8 @@ pub(crate) const RAW_EVENT_LOG_FILE_NAME: &str = "trace.jsonl";pub(crate) const PAYLOADS_DIR_NAME: &str = "payloads";/// Conventional file name for a reducer-written `RolloutTrace` cache.pub const REDUCED_STATE_FILE_NAME: &str = "state.json";-pub(crate) const TRACE_MANIFEST_SCHEMA_VERSION: u32 = 1;-pub(crate) const REDUCED_TRACE_SCHEMA_VERSION: u32 = 1;+pub(crate) const TRACE_MANIFEST_SCHEMA_VERSION: u32 = 2;+pub(crate) const REDUCED_TRACE_SCHEMA_VERSION: u32 = 2;/// Manifest stored at the root of a trace bundle.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]diff --git a/codex-rs/rollout-trace/src/model/conversation.rs b/codex-rs/rollout-trace/src/model/conversation.rsindex 0cb72e85ef..fba7762d0f 100644--- a/codex-rs/rollout-trace/src/model/conversation.rs+++ b/codex-rs/rollout-trace/src/model/conversation.rs@@ -25,6 +25,10 @@ use super::session::ExecutionWindow;#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct ConversationItem {pub item_id: ConversationItemId,+ /// Original Responses item ID, scoped to this thread. Retained for joining+ /// history acceptance and exact inference without matching rendered text.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub responses_item_id: Option<String>,pub thread_id: AgentThreadId,/// Runtime activation that first introduced this item locally, when known.pub codex_turn_id: Option<CodexTurnId>,diff --git a/codex-rs/rollout-trace/src/model/session.rs b/codex-rs/rollout-trace/src/model/session.rsindex fccc386dc9..03f336582a 100644--- a/codex-rs/rollout-trace/src/model/session.rs+++ b/codex-rs/rollout-trace/src/model/session.rs@@ -45,7 +45,8 @@ pub struct AgentThread {pub execution: ExecutionWindow,/// Configured model presentation hint. Individual inference calls carry the actual upstream model.pub default_model: Option<String>,- /// Logical conversation items first observed for this thread, in transcript order.+ /// Accepted model-visible conversation in transcript order. Unaccepted+ /// response items remain in the graph, reachable through their inference call.pub conversation_item_ids: Vec<ConversationItemId>,}diff --git a/codex-rs/rollout-trace/src/protocol_event.rs b/codex-rs/rollout-trace/src/protocol_event.rsindex a72584853f..d68df83e6e 100644--- a/codex-rs/rollout-trace/src/protocol_event.rs+++ b/codex-rs/rollout-trace/src/protocol_event.rs@@ -59,7 +59,11 @@ pub(crate) fn codex_turn_trace_event(context_turn_id: codex_turn_id.clone(),payload: RawTraceEventPayload::CodexTurnEnded {codex_turn_id,- status: ExecutionStatus::Completed,+ status: if event.error.is_some() {+ ExecutionStatus::Failed+ } else {+ ExecutionStatus::Completed+ },},})}@@ -76,6 +80,12 @@ pub(crate) fn codex_turn_trace_event(},})}+ EventMsg::RawResponseItem(event) => Some(CodexTurnTraceEvent {+ context_turn_id: default_turn_id.to_string(),+ payload: RawTraceEventPayload::HistoryItemAccepted {+ item_id: event.item.id()?.to_string(),+ },+ }),_ => None,}}diff --git a/codex-rs/rollout-trace/src/raw_event.rs b/codex-rs/rollout-trace/src/raw_event.rsindex b8ec8f315e..88880d673a 100644--- a/codex-rs/rollout-trace/src/raw_event.rs+++ b/codex-rs/rollout-trace/src/raw_event.rs@@ -23,7 +23,7 @@ use serde_json::Value;pub type RawEventSeq = u64;/// Current raw event envelope schema version.-pub(crate) const RAW_TRACE_EVENT_SCHEMA_VERSION: u32 = 1;+pub(crate) const RAW_TRACE_EVENT_SCHEMA_VERSION: u32 = 2;/// One append-only raw trace event.///@@ -92,6 +92,11 @@ pub enum RawTraceEventPayload {codex_turn_id: CodexTurnId,status: ExecutionStatus,},+ /// The harness appended this Responses item to its history. This is not+ /// evidence of its rendered content, which may be truncated before sampling.+ HistoryItemAccepted {+ item_id: String,+ },InferenceStarted {inference_call_id: InferenceCallId,thread_id: AgentThreadId,@@ -245,6 +250,7 @@ impl RawTraceEventPayload {| RawTraceEventPayload::ThreadEnded { .. }| RawTraceEventPayload::CodexTurnStarted { .. }| RawTraceEventPayload::CodexTurnEnded { .. }+ | RawTraceEventPayload::HistoryItemAccepted { .. }| RawTraceEventPayload::CompactionRequestFailed { .. }| RawTraceEventPayload::CodeCellStarted { .. }| RawTraceEventPayload::McpToolCallCorrelationAssigned { .. }diff --git a/codex-rs/rollout-trace/src/reducer/acceptance_tests.rs b/codex-rs/rollout-trace/src/reducer/acceptance_tests.rsnew file mode 100644index 0000000000..01a68212f9--- /dev/null+++ b/codex-rs/rollout-trace/src/reducer/acceptance_tests.rs@@ -0,0 +1,249 @@+//! Accepted history retains original inference provenance across retries and resets.++use pretty_assertions::assert_eq;+use serde_json::json;+use tempfile::TempDir;++use super::test_support::append_completed_inference;+use super::test_support::append_inference_completion;+use super::test_support::append_inference_request;+use super::test_support::create_started_writer;+use super::test_support::message;+use super::test_support::start_thread;+use super::test_support::start_turn;+use super::test_support::start_turn_for_thread;+use super::test_support::trace_context;+use crate::ProducerRef;+use crate::RawPayloadKind;+use crate::RawTraceEventPayload;+use crate::replay_bundle;++#[test]+fn identical_text_from_a_discarded_attempt_does_not_gain_acceptance() -> anyhow::Result<()> {+ let temp = TempDir::new()?;+ let writer = create_started_writer(&temp)?;+ start_turn(&writer, "turn-1")?;+ let input = message("user", "answer");+ let mut output = message("assistant", "same text");+ output["id"] = json!("discarded");+ append_completed_inference(+ &writer,+ "thread-root",+ "turn-1",+ "first",+ vec![input.clone()],+ vec![output.clone()],+ )?;+ append_inference_request(&writer, "thread-root", "turn-1", "retry", vec![input])?;+ output["id"] = json!("accepted");+ // Acceptance can arrive before the asynchronous stream mapper's terminal record.+ writer.append_with_context(+ trace_context("turn-1"),+ RawTraceEventPayload::HistoryItemAccepted {+ item_id: "accepted".into(),+ },+ )?;+ let response = writer.write_json_payload(+ RawPayloadKind::InferenceResponse,+ &json!({"output_items": [output]}),+ )?;+ append_inference_completion(&writer, "retry", "resp-retry", response)?;++ let trace = replay_bundle(temp.path())?;+ let first = &trace.inference_calls["first"];+ let retry = &trace.inference_calls["retry"];+ assert_ne!(first.response_item_ids, retry.response_item_ids);+ let mut expected = retry.request_item_ids.clone();+ expected.extend(retry.response_item_ids.clone());+ assert_eq!(trace.threads["thread-root"].conversation_item_ids, expected);+ assert_eq!(+ trace.conversation_items[&first.response_item_ids[0]]+ .responses_item_id+ .as_deref(),+ Some("discarded")+ );+ Ok(())+}++#[test]+fn late_response_reuses_a_later_request_sighting_without_rewriting_its_snapshot()+-> anyhow::Result<()> {+ let temp = TempDir::new()?;+ let writer = create_started_writer(&temp)?;+ start_turn(&writer, "turn-1")?;+ let input = message("user", "answer");+ let mut output = message("assistant", "accepted");+ output["id"] = json!("msg-accepted");+ append_inference_request(+ &writer,+ "thread-root",+ "turn-1",+ "first",+ vec![input.clone()],+ )?;+ let later_input = vec![input, output.clone(), message("user", "continue")];+ append_inference_request(+ &writer,+ "thread-root",+ "turn-1",+ "second",+ later_input.clone(),+ )?;+ let response = writer.write_json_payload(+ RawPayloadKind::InferenceResponse,+ &json!({"output_items": [output]}),+ )?;+ append_inference_completion(&writer, "first", "resp-first", response)?;+ append_inference_request(&writer, "thread-root", "turn-1", "third", later_input)?;++ let trace = replay_bundle(temp.path())?;+ let second = &trace.inference_calls["second"];+ assert_eq!(+ trace.inference_calls["first"].response_item_ids,+ vec![second.request_item_ids[1].clone()]+ );+ assert_eq!(+ trace.inference_calls["third"].request_item_ids,+ second.request_item_ids+ );+ assert_eq!(+ trace.threads["thread-root"].conversation_item_ids,+ second.request_item_ids+ );+ assert_eq!(+ trace.conversation_items[&second.request_item_ids[1]].produced_by,+ vec![ProducerRef::Inference {+ inference_call_id: "first".into()+ }]+ );+ Ok(())+}++#[test]+fn late_response_keeps_compaction_replacement_distinct() -> anyhow::Result<()> {+ let temp = TempDir::new()?;+ let writer = create_started_writer(&temp)?;+ start_turn(&writer, "turn-1")?;+ let input = message("user", "answer");+ let mut output = message("assistant", "accepted");+ output["id"] = json!("msg-accepted");+ append_inference_request(+ &writer,+ "thread-root",+ "turn-1",+ "first",+ vec![input.clone()],+ )?;+ let checkpoint = writer.write_json_payload(+ RawPayloadKind::CompactionCheckpoint,+ &json!({+ "input_history": [input, output], "replacement_history": [output]+ }),+ )?;+ writer.append_with_context(+ trace_context("turn-1"),+ RawTraceEventPayload::CompactionInstalled {+ compaction_id: "reset".into(),+ checkpoint_payload: checkpoint,+ },+ )?;+ let response = writer.write_json_payload(+ RawPayloadKind::InferenceResponse,+ &json!({"output_items": [output]}),+ )?;+ append_inference_completion(&writer, "first", "resp-first", response)?;+ append_inference_request(&writer, "thread-root", "turn-1", "second", vec![output])?;++ let trace = replay_bundle(temp.path())?;+ let checkpoint = &trace.compactions["reset"];+ assert_eq!(+ trace.inference_calls["first"].response_item_ids,+ vec![checkpoint.input_item_ids[1].clone()]+ );+ assert_eq!(+ trace.inference_calls["second"].request_item_ids,+ checkpoint.replacement_item_ids+ );+ assert_ne!(+ checkpoint.input_item_ids[1],+ checkpoint.replacement_item_ids[0]+ );+ assert_eq!(+ trace.conversation_items[&checkpoint.replacement_item_ids[0]].produced_by,+ vec![ProducerRef::Compaction {+ compaction_id: "reset".into()+ }]+ );+ let mut expected = checkpoint.input_item_ids.clone();+ expected.push(checkpoint.marker_item_id.clone());+ expected.extend(checkpoint.replacement_item_ids.clone());+ assert_eq!(trace.threads["thread-root"].conversation_item_ids, expected);+ Ok(())+}++#[test]+fn inherited_output_is_child_prefill_without_copying_parent_sampling_provenance()+-> anyhow::Result<()> {+ let temp = TempDir::new()?;+ let writer = create_started_writer(&temp)?;+ start_turn(&writer, "turn-parent")?;+ let mut output = message("assistant", "inherited");+ output["id"] = json!("msg-parent");+ append_completed_inference(+ &writer,+ "thread-root",+ "turn-parent",+ "parent",+ vec![message("user", "answer")],+ vec![output.clone()],+ )?;+ start_thread(&writer, "child", "/root/child")?;+ start_turn_for_thread(&writer, "child", "turn-child")?;+ append_inference_request(&writer, "child", "turn-child", "child", vec![output])?;++ let trace = replay_bundle(temp.path())?;+ let parent = &trace.inference_calls["parent"];+ let child = &trace.inference_calls["child"];+ assert_ne!(child.request_item_ids, parent.response_item_ids);+ assert_eq!(+ trace.threads["child"].conversation_item_ids,+ child.request_item_ids+ );+ assert_eq!(+ trace.conversation_items[&child.request_item_ids[0]].produced_by,+ Vec::<ProducerRef>::new()+ );+ // A sighting in the child must not imply acceptance in the parent's history.+ assert_eq!(+ trace.threads["thread-root"].conversation_item_ids,+ parent.request_item_ids+ );+ Ok(())+}++#[test]+fn legacy_bundles_cannot_claim_accepted_history() -> anyhow::Result<()> {+ let temp = TempDir::new()?;+ let writer = create_started_writer(&temp)?;+ start_turn(&writer, "turn-1")?;+ append_completed_inference(+ &writer,+ "thread-root",+ "turn-1",+ "call",+ vec![message("user", "answer")],+ vec![message("assistant", "unproven")],+ )?;+ let manifest_path = temp.path().join("manifest.json");+ let mut manifest: serde_json::Value =+ serde_json::from_reader(std::fs::File::open(&manifest_path)?)?;+ manifest["schema_version"] = json!(1);+ std::fs::write(manifest_path, serde_json::to_vec(&manifest)?)?;+ assert!(+ replay_bundle(temp.path())+ .unwrap_err()+ .to_string()+ .contains("cannot establish accepted history")+ );+ Ok(())+}diff --git a/codex-rs/rollout-trace/src/reducer/conversation.rs b/codex-rs/rollout-trace/src/reducer/conversation.rsindex f105ffebd6..946bd8bdbd 100644--- a/codex-rs/rollout-trace/src/reducer/conversation.rs+++ b/codex-rs/rollout-trace/src/reducer/conversation.rs@@ -1,9 +1,8 @@//! Conversation reduction from model-facing payload snapshots.//!-//! Inference request inputs and response outputs are both part of the logical-//! conversation because they are the payloads exchanged with the model. Runtime-//! observations, such as local tool output, stay outside the transcript until a-//! later model-facing payload carries their content.+//! Replay retains all observed items, then selects accepted history using+//! request inputs, compaction checkpoints, and harness acceptance events.+//! Tool messages use the content sent to the model in subsequent requests.use anyhow::Context;use anyhow::Ok;@@ -28,9 +27,8 @@ mod normalize;impl TraceReducer {/// Reduces an inference request input snapshot into model-visible conversation items.///- /// Request snapshots are reconciled by position against the previous model-visible- /// snapshot for the thread so repeated history reuses ids while newly inserted- /// items remain distinct.+ /// Full snapshots reuse matching items even if the prompt builder reorders them.+ /// Incremental requests append to the referenced response's context.pub(super) fn reduce_inference_request(&mut self,wall_time_unix_ms: i64,@@ -153,31 +151,51 @@ impl TraceReducer {};let items = normalize::normalize_model_items(output_items, response_payload)?;- // Response output is appended immediately: it was produced by the model,- // so it is conversation even before a later request carries it forward.- let append_at = self- .thread_conversation_snapshots- .get(&thread_id)- .map_or(0, Vec::len);- let response_item_ids = self.reconcile_conversation_items(+ // A later request or compaction can be recorded before this response.+ // Only match items with source IDs, so retries with identical text stay distinct.+ let candidates = self.rollout.threads[&thread_id]+ .conversation_item_ids+ .iter()+ .filter(|id| {+ let source_id = &self.rollout.conversation_items[*id].responses_item_id;+ source_id.is_some()+ && items+ .iter()+ .any(|item| &item.responses_item_id == source_id)+ })+ .cloned()+ .collect::<Vec<_>>();+ let response_item_ids = self.reconcile_detached_conversation_items(items,- ReconcileItems {+ DetachedReconcileItems {thread_id: &thread_id,codex_turn_id: &codex_turn_id,wall_time_unix_ms,produced_by: vec![ProducerRef::Inference {inference_call_id: inference_call_id.clone(),}],- start_index: append_at,- mode: ReconcileMode::AppendOnly,- snapshot_override: None,+ candidates,},)?;self.append_thread_conversation_items(&thread_id, &response_item_ids)?;- self.thread_conversation_snapshots- .entry(thread_id)- .or_default()- .extend(response_item_ids.clone());+ let inference = &self.rollout.inference_calls[inference_call_id];+ let latest = self+ .rollout+ .inference_calls+ .values()+ .filter(|call| call.thread_id == thread_id)+ .max_by_key(|call| call.execution.started_seq);+ // A late response must not replace a newer request or compaction snapshot.+ if latest.is_some_and(|call| call.inference_call_id == *inference_call_id)+ && !self+ .pending_compaction_replacement_item_ids+ .contains_key(&thread_id)+ {+ let mut snapshot = inference.request_item_ids.clone();+ snapshot.extend(response_item_ids.clone());+ self.thread_conversation_snapshots+ .insert(thread_id, snapshot);+ }if let Some(usage) = payload.get("token_usage")@@ -319,6 +337,7 @@ impl TraceReducer {Some(codex_turn_id.to_string()),wall_time_unix_ms,NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: None,kind: ConversationItemKind::CompactionMarker,@@ -414,6 +433,7 @@ impl TraceReducer {item_id.clone(),ConversationItem {item_id: item_id.clone(),+ responses_item_id: item.responses_item_id,thread_id: thread_id.to_string(),codex_turn_id,first_seen_at_unix_ms,@@ -439,6 +459,10 @@ impl TraceReducer {bail!("conversation item {item_id} was referenced before it was created");};+ if item.responses_item_id.is_none() {+ item.responses_item_id+ .clone_from(&normalized.responses_item_id);+ }if item.kind == ConversationItemKind::Reasoning {merge_reasoning_body(&mut item.body, &normalized.body)?;}@@ -502,6 +526,12 @@ impl TraceReducer {let Some(item) = self.rollout.conversation_items.get(item_id) else {return false;};+ if let (Some(existing), Some(incoming)) =+ (&item.responses_item_id, &normalized.responses_item_id)+ && existing != incoming+ {+ return false;+ }conversation_item_matches(item, normalized)}@@ -516,10 +546,10 @@ impl TraceReducer {enum ReconcileMode {/// Full model requests are authoritative snapshots of the live context. The/// prompt builder can reorder already-observed items or replace history- /// with synthetic summary messages, so item identity is "same content,- /// reused at most once in this snapshot" rather than "same position only".+ /// with synthetic summary messages. Reuse matching content at most once+ /// per snapshot, respecting supplied item IDs without requiring the same position.FullSnapshot,- /// Incremental request deltas and response outputs append to a known prefix.+ /// Incremental request deltas append to a known prefix./// A mismatch at an occupied position means our reconstructed prefix is/// wrong and should fail replay.AppendOnly,diff --git a/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs b/codex-rs/rollout-trace/src/reducer/conversation/normalize.rsindex 8f56890de4..a2f45d2f75 100644--- a/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs+++ b/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs@@ -23,6 +23,7 @@ use crate::payload::RawPayloadRef;/// single reducer decision instead of something the parser has to know about.#[derive(Clone)]pub(super) struct NormalizedConversationItem {+ pub(super) responses_item_id: Option<String>,pub(super) role: ConversationRole,pub(super) channel: Option<ConversationChannel>,pub(super) kind: ConversationItemKind,@@ -70,11 +71,12 @@ fn normalize_model_item(raw_payload.raw_payload_id);};- match item_type {+ let mut normalized = match item_type {"message" => normalize_message_item(item, raw_payload),"agent_message" => normalize_agent_message_item(item, raw_payload),"reasoning" => normalize_reasoning_item(item, raw_payload),"function_call" => Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: Some(ConversationChannel::Commentary),kind: ConversationItemKind::FunctionCall,@@ -86,6 +88,7 @@ fn normalize_model_item(.map(ToString::to_string),}),"function_call_output" => Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Tool,channel: Some(ConversationChannel::Commentary),kind: ConversationItemKind::FunctionCallOutput,@@ -97,6 +100,7 @@ fn normalize_model_item(.map(ToString::to_string),}),"custom_tool_call" => Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: Some(ConversationChannel::Commentary),kind: ConversationItemKind::CustomToolCall,@@ -108,6 +112,7 @@ fn normalize_model_item(.map(ToString::to_string),}),"custom_tool_call_output" => Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Tool,channel: Some(ConversationChannel::Commentary),kind: ConversationItemKind::CustomToolCallOutput,@@ -120,6 +125,7 @@ fn normalize_model_item(}),"tool_search_call" | "web_search_call" | "image_generation_call" | "local_shell_call" => {Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: Some(ConversationChannel::Commentary),kind: ConversationItemKind::FunctionCall,@@ -132,6 +138,7 @@ fn normalize_model_item(})}"tool_search_output" | "mcp_tool_call_output" => Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Tool,channel: Some(ConversationChannel::Commentary),kind: ConversationItemKind::FunctionCallOutput,@@ -144,6 +151,7 @@ fn normalize_model_item(}),"compaction" | "compaction_summary" | "context_compaction" => {Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: Some(ConversationChannel::Summary),kind: ConversationItemKind::Message,@@ -156,7 +164,9 @@ fn normalize_model_item("unsupported model item type {item_type} in payload {}",raw_payload.raw_payload_id),- }+ }?;+ normalized.responses_item_id = item.get("id").and_then(Value::as_str).map(str::to_string);+ Ok(normalized)}fn normalize_message_item(@@ -176,6 +186,7 @@ fn normalize_message_item();};Ok(NormalizedConversationItem {+ responses_item_id: None,role,channel: item.get("phase")@@ -225,6 +236,7 @@ fn normalize_agent_message_item(}Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: Some(ConversationChannel::Analysis),kind: ConversationItemKind::Message,@@ -281,6 +293,7 @@ fn normalize_reasoning_item(}Ok(NormalizedConversationItem {+ responses_item_id: None,role: ConversationRole::Assistant,channel: Some(ConversationChannel::Analysis),kind: ConversationItemKind::Reasoning,diff --git a/codex-rs/rollout-trace/src/reducer/conversation_tests.rs b/codex-rs/rollout-trace/src/reducer/conversation_tests.rsindex 4a58e50304..2c5aef487b 100644--- a/codex-rs/rollout-trace/src/reducer/conversation_tests.rs+++ b/codex-rs/rollout-trace/src/reducer/conversation_tests.rs@@ -68,7 +68,7 @@ fn request_snapshots_reuse_history_without_deduping_new_identical_items() -> any}#[test]-fn response_outputs_enter_thread_conversation_on_completion() -> anyhow::Result<()> {+fn response_outputs_require_harness_acceptance() -> anyhow::Result<()> {let temp = TempDir::new()?;let writer = create_started_writer(&temp)?;start_turn(&writer, "turn-1")?;@@ -88,12 +88,14 @@ fn response_outputs_enter_thread_conversation_on_completion() -> anyhow::Result<"output_items": [{"type": "message",+ "id": "msg-observed","role": "assistant","phase": "partial_answer","content": [{"type": "output_text", "text": "first tests passed"}]},{"type": "message",+ "id": "msg-accepted","role": "assistant","phase": "final_answer","content": [{"type": "output_text", "text": "tests passed"}]@@ -103,10 +105,22 @@ fn response_outputs_enter_thread_conversation_on_completion() -> anyhow::Result<)?;append_inference_completion(&writer, "inference-1", "resp-1", response)?;+ let observed = replay_bundle(temp.path())?;+ assert_eq!(+ observed.threads["thread-root"].conversation_item_ids,+ observed.inference_calls["inference-1"].request_item_ids,+ );+ writer.append_with_context(+ trace_context("turn-1"),+ RawTraceEventPayload::HistoryItemAccepted {+ item_id: "msg-accepted".into(),+ },+ )?;+let rollout = replay_bundle(temp.path())?;let inference = &rollout.inference_calls["inference-1"];let mut expected_thread_items = inference.request_item_ids.clone();- expected_thread_items.extend(inference.response_item_ids.clone());+ expected_thread_items.push(inference.response_item_ids[1].clone());assert_eq!(inferencediff --git a/codex-rs/rollout-trace/src/reducer/inference_tests.rs b/codex-rs/rollout-trace/src/reducer/inference_tests.rsindex 77e0f39a9a..d940a9a5b5 100644--- a/codex-rs/rollout-trace/src/reducer/inference_tests.rs+++ b/codex-rs/rollout-trace/src/reducer/inference_tests.rs@@ -10,10 +10,11 @@ use crate::reducer::test_support::append_inference_start;use crate::reducer::test_support::create_started_writer;use crate::reducer::test_support::message;use crate::reducer::test_support::start_turn;+use crate::reducer::test_support::trace_context;use crate::replay_bundle;#[test]-fn cancelled_inference_reduces_partial_response_items() -> anyhow::Result<()> {+fn cancelled_inference_retains_accepted_partial_output() -> anyhow::Result<()> {let temp = TempDir::new()?;let writer = create_started_writer(&temp)?;start_turn(&writer, "turn-1")?;@@ -26,6 +27,12 @@ fn cancelled_inference_reduces_partial_response_items() -> anyhow::Result<()> {)?;append_inference_start(&writer, "inference-1", "turn-1", request)?;+ writer.append_with_context(+ trace_context("turn-1"),+ RawTraceEventPayload::HistoryItemAccepted {+ item_id: "msg-partial".into(),+ },+ )?;let partial_response = writer.write_json_payload(RawPayloadKind::InferenceResponse,&json!({@@ -33,6 +40,7 @@ fn cancelled_inference_reduces_partial_response_items() -> anyhow::Result<()> {"token_usage": null,"output_items": [{"type": "message",+ "id": "msg-partial","role": "assistant","content": [{"type": "output_text", "text": "partial"}]}]@@ -65,6 +73,12 @@ fn cancelled_inference_reduces_partial_response_items() -> anyhow::Result<()> {inference_call_id: "inference-1".to_string(),}],);+ let mut expected = inference.request_item_ids.clone();+ expected.extend(inference.response_item_ids.clone());+ assert_eq!(+ rollout.threads["thread-root"].conversation_item_ids,+ expected+ );Ok(())}diff --git a/codex-rs/rollout-trace/src/reducer/mod.rs b/codex-rs/rollout-trace/src/reducer/mod.rsindex 123c0cbff6..5487654fa2 100644--- a/codex-rs/rollout-trace/src/reducer/mod.rs+++ b/codex-rs/rollout-trace/src/reducer/mod.rs@@ -1,6 +1,7 @@//! Deterministic replay from raw trace events to `RolloutTrace`.use std::collections::BTreeMap;+use std::collections::BTreeSet;use std::fs::File;use std::io::BufRead;use std::io::BufReader;@@ -10,15 +11,18 @@ use std::path::PathBuf;use anyhow::Context;use anyhow::Result;use anyhow::bail;+use anyhow::ensure;use serde_json::Value;use crate::bundle::MANIFEST_FILE_NAME;use crate::bundle::RAW_EVENT_LOG_FILE_NAME;use crate::bundle::REDUCED_TRACE_SCHEMA_VERSION;+use crate::bundle::TRACE_MANIFEST_SCHEMA_VERSION;use crate::bundle::TraceBundleManifest;use crate::model::ExecutionStatus;use crate::model::RolloutTrace;use crate::payload::RawPayloadRef;+use crate::raw_event::RAW_TRACE_EVENT_SCHEMA_VERSION;use crate::raw_event::RawTraceEvent;use crate::raw_event::RawTraceEventPayload;@@ -31,6 +35,10 @@ pub(crate) mod test_support;mod thread;mod tool;+#[cfg(test)]+#[path = "acceptance_tests.rs"]+mod acceptance_tests;+use self::code_cell::PendingCodeCellLifecycleEvent;use self::code_cell::PendingCodeCellStart;use self::code_cell::StartedCodeCell;@@ -46,6 +54,12 @@ pub fn replay_bundle(bundle_dir: impl AsRef<Path>) -> Result<RolloutTrace> {let manifest: TraceBundleManifest =serde_json::from_reader(File::open(bundle_dir.join(MANIFEST_FILE_NAME))?).with_context(|| format!("read {}", bundle_dir.join(MANIFEST_FILE_NAME).display()))?;+ // Earlier bundles cannot distinguish accepted output from transport output.+ ensure!(+ manifest.schema_version == TRACE_MANIFEST_SCHEMA_VERSION,+ "trace schema {} cannot establish accepted history; use its matching reducer or record a new bundle",+ manifest.schema_version+ );let mut reducer = TraceReducer {rollout: RolloutTrace::new(REDUCED_TRACE_SCHEMA_VERSION,@@ -65,6 +79,7 @@ pub fn replay_bundle(bundle_dir: impl AsRef<Path>) -> Result<RolloutTrace> {pending_code_cell_starts: BTreeMap::new(),pending_code_cell_lifecycle_events: BTreeMap::new(),pending_agent_interaction_edges: Vec::new(),+ accepted_item_ids: BTreeMap::new(),};let event_log_path = bundle_dir.join(RAW_EVENT_LOG_FILE_NAME);@@ -77,6 +92,12 @@ pub fn replay_bundle(bundle_dir: impl AsRef<Path>) -> Result<RolloutTrace> {}let event: RawTraceEvent = serde_json::from_str(&line).with_context(|| format!("parse trace event line {}", line_index + 1))?;+ ensure!(+ event.schema_version == RAW_TRACE_EVENT_SCHEMA_VERSION,+ "unsupported trace event schema {} at line {}",+ event.schema_version,+ line_index + 1+ );reducer.apply_event(event)?;}// Spawn edges prefer the child task message as their target, but a child can@@ -84,24 +105,52 @@ pub fn replay_bundle(bundle_dir: impl AsRef<Path>) -> Result<RolloutTrace> {// bundle do we know which spawn deliveries need the child-thread fallback.reducer.resolve_pending_spawn_edge_fallbacks()?;+ // Acceptance and response records can arrive in either order. Select history+ // after replay, retaining discarded attempts elsewhere in the graph.+ // Request/checkpoint items include inherited context without local acceptance.+ let context_item_ids: BTreeSet<_> = reducer+ .rollout+ .inference_calls+ .values()+ .flat_map(|call| call.request_item_ids.iter())+ .chain(reducer.rollout.compactions.values().flat_map(|checkpoint| {+ checkpoint+ .input_item_ids+ .iter()+ .chain(&checkpoint.replacement_item_ids)+ .chain(std::iter::once(&checkpoint.marker_item_id))+ }))+ .collect();+ for thread in reducer.rollout.threads.values_mut() {+ let accepted_ids = reducer+ .accepted_item_ids+ .remove(&thread.thread_id)+ .unwrap_or_default();+ thread.conversation_item_ids.retain(|id| {+ let item = &reducer.rollout.conversation_items[id];+ context_item_ids.contains(id)+ || item+ .responses_item_id+ .as_ref()+ .is_some_and(|source_id| accepted_ids.contains(source_id))+ });+ }+Ok(reducer.rollout)}struct TraceReducer {rollout: RolloutTrace,bundle_dir: PathBuf,+ /// Responses IDs accepted by the harness, scoped to the receiving thread.+ accepted_item_ids: BTreeMap<String, BTreeSet<String>>,next_conversation_item_ordinal: u64,next_terminal_operation_ordinal: u64,/// Latest session for each thread-local runtime handle; each exec replaces it.terminal_ids_by_process: BTreeMap<(String, String), String>,/// Retains the begin/end consistency check when a handle is later reused.process_ids_by_terminal: BTreeMap<String, String>,- /// Last model-visible conversation snapshot per thread.- ///- /// Requests and responses both advance this sequence because both are- /// model-facing payloads. Repeated request snapshots reuse item IDs only- /// when the same normalized item appears at the same position; identical- /// content at a new position must remain a distinct conversation item.+ /// Latest request and its response items, used to reconcile the next request.thread_conversation_snapshots: BTreeMap<String, Vec<String>>,/// Replacement snapshot installed by compaction but not yet seen in a sampling request.///@@ -211,6 +260,16 @@ impl TraceReducer {status,)?;}+ RawTraceEventPayload::HistoryItemAccepted { item_id } => {+ let thread_id = event+ .thread_id+ .context("history acceptance has no thread")?;+ self.thread_mut(&thread_id)?;+ self.accepted_item_ids+ .entry(thread_id)+ .or_default()+ .insert(item_id);+ }RawTraceEventPayload::InferenceStarted {inference_call_id,thread_id,diff --git a/codex-rs/rollout-trace/src/thread.rs b/codex-rs/rollout-trace/src/thread.rsindex 4a38bacaff..5aee8637c2 100644--- a/codex-rs/rollout-trace/src/thread.rs+++ b/codex-rs/rollout-trace/src/thread.rs@@ -213,7 +213,7 @@ impl ThreadTraceContext {});}- /// Emits typed Codex turn lifecycle events from protocol lifecycle events.+ /// Records turn lifecycle and history acceptance from existing protocol events.pub fn record_codex_turn_event(&self, default_turn_id: &str, event: &EventMsg) {let ThreadTraceContextState::Enabled(context) = &self.state else {return;