Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Match rollout trace image content across request snapshots (#56342)

cassirer-openaie5ea2ed50b3a
diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock
index 84057851d1..ad1b192852 100644
--- a/codex-rs/Cargo.lock
+++ b/codex-rs/Cargo.lock
@@ -4612,6 +4612,7 @@ dependencies = [
"pretty_assertions",
"serde",
"serde_json",
+ "sha2 0.10.9",
"tempfile",
"tracing",
"uuid",
diff --git a/codex-rs/rollout-trace/Cargo.toml b/codex-rs/rollout-trace/Cargo.toml
index b368c9acc5..048026f60a 100644
--- a/codex-rs/rollout-trace/Cargo.toml
+++ b/codex-rs/rollout-trace/Cargo.toml
@@ -19,6 +19,7 @@ codex-protocol = { workspace = true }
http = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
+sha2 = { workspace = true }
tracing = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
diff --git a/codex-rs/rollout-trace/src/model/conversation.rs b/codex-rs/rollout-trace/src/model/conversation.rs
index fba7762d0f..e3f977ad47 100644
--- a/codex-rs/rollout-trace/src/model/conversation.rs
+++ b/codex-rs/rollout-trace/src/model/conversation.rs
@@ -138,6 +138,9 @@ pub enum ConversationPart {
PayloadRef {
label: String,
raw_payload_id: RawPayloadId,
+ /// Content identity across snapshots, independent of the containing payload file.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ content_sha256: Option<String>,
},
}
diff --git a/codex-rs/rollout-trace/src/reducer/conversation.rs b/codex-rs/rollout-trace/src/reducer/conversation.rs
index 946bd8bdbd..95771a4e82 100644
--- a/codex-rs/rollout-trace/src/reducer/conversation.rs
+++ b/codex-rs/rollout-trace/src/reducer/conversation.rs
@@ -633,6 +633,16 @@ fn conversation_body_matches(left: &ConversationBody, right: &ConversationBody)
raw_payload_id: _,
},
) => left_summary == right_summary,
+ (
+ ConversationPart::PayloadRef {
+ content_sha256: Some(left_hash),
+ ..
+ },
+ ConversationPart::PayloadRef {
+ content_sha256: Some(right_hash),
+ ..
+ },
+ ) => left_hash == right_hash,
_ => left == right,
})
}
diff --git a/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs b/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs
index a2f45d2f75..124d19ebde 100644
--- a/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs
+++ b/codex-rs/rollout-trace/src/reducer/conversation/normalize.rs
@@ -6,6 +6,8 @@ use anyhow::bail;
use codex_protocol::models::AgentMessageInputContent;
use codex_protocol::models::ResponseItem;
use serde_json::Value;
+use sha2::Digest;
+use sha2::Sha256;
use crate::model::AgentMessageMetadata;
use crate::model::ConversationBody;
@@ -93,7 +95,7 @@ fn normalize_model_item(
channel: Some(ConversationChannel::Commentary),
kind: ConversationItemKind::FunctionCallOutput,
agent_message: None,
- body: tool_output_body(item.get("output"), raw_payload),
+ body: tool_output_body(item.get("output"), raw_payload)?,
call_id: item
.get("call_id")
.and_then(Value::as_str)
@@ -117,7 +119,7 @@ fn normalize_model_item(
channel: Some(ConversationChannel::Commentary),
kind: ConversationItemKind::CustomToolCallOutput,
agent_message: None,
- body: tool_output_body(item.get("output"), raw_payload),
+ body: tool_output_body(item.get("output"), raw_payload)?,
call_id: item
.get("call_id")
.and_then(Value::as_str)
@@ -195,7 +197,7 @@ fn normalize_message_item(
kind: ConversationItemKind::Message,
agent_message: None,
body: ConversationBody {
- parts: content_parts(item.get("content"), raw_payload),
+ parts: content_parts(item.get("content"), raw_payload)?,
},
call_id: None,
})
@@ -398,9 +400,12 @@ fn channel_from_phase(phase: &str) -> Option<ConversationChannel> {
}
}
-fn content_parts(content: Option<&Value>, raw_payload: &RawPayloadRef) -> Vec<ConversationPart> {
+fn content_parts(
+ content: Option<&Value>,
+ raw_payload: &RawPayloadRef,
+) -> Result<Vec<ConversationPart>> {
let Some(content) = content.and_then(Value::as_array) else {
- return vec![payload_ref_part("content", raw_payload)];
+ return Ok(vec![payload_ref_part("content", raw_payload)]);
};
let mut parts = Vec::new();
@@ -413,16 +418,23 @@ fn content_parts(content: Option<&Value>, raw_payload: &RawPayloadRef) -> Vec<Co
});
}
}
- Some("input_image") => parts.push(payload_ref_part("input_image", raw_payload)),
- Some(other) => parts.push(payload_ref_part(other, raw_payload)),
- None => parts.push(payload_ref_part("content", raw_payload)),
+ other => {
+ // Repeated images live in different request payloads. Compare their
+ // content without copying large image bodies into the reduced graph.
+ let mut canonical = part.clone();
+ canonical.sort_all_objects();
+ let mut digest = Sha256::new();
+ serde_json::to_writer(&mut digest, &canonical)?;
+ parts.push(ConversationPart::PayloadRef {
+ label: other.unwrap_or("content").to_string(),
+ raw_payload_id: raw_payload.raw_payload_id.clone(),
+ content_sha256: Some(format!("{:x}", digest.finalize())),
+ });
+ }
}
}
- if parts.is_empty() {
- parts.push(payload_ref_part("empty_content", raw_payload));
- }
- parts
+ Ok(parts)
}
fn custom_tool_call_body(item: &Value, raw_payload: &RawPayloadRef) -> ConversationBody {
@@ -463,19 +475,22 @@ fn raw_text_or_json_body(value: Option<&Value>, raw_payload: &RawPayloadRef) ->
}
}
-fn tool_output_body(output: Option<&Value>, raw_payload: &RawPayloadRef) -> ConversationBody {
- match output {
+fn tool_output_body(
+ output: Option<&Value>,
+ raw_payload: &RawPayloadRef,
+) -> Result<ConversationBody> {
+ Ok(match output {
Some(Value::String(text)) => ConversationBody {
parts: vec![ConversationPart::Text { text: text.clone() }],
},
Some(Value::Array(_)) => ConversationBody {
- parts: content_parts(output, raw_payload),
+ parts: content_parts(output, raw_payload)?,
},
Some(value) => json_body(value, raw_payload),
None => ConversationBody {
parts: vec![payload_ref_part("tool_output", raw_payload)],
},
- }
+ })
}
fn compaction_body(item: &Value, raw_payload: &RawPayloadRef) -> Result<ConversationBody> {
@@ -509,6 +524,7 @@ fn payload_ref_part(label: &str, raw_payload: &RawPayloadRef) -> ConversationPar
ConversationPart::PayloadRef {
label: label.to_string(),
raw_payload_id: raw_payload.raw_payload_id.clone(),
+ content_sha256: None,
}
}
diff --git a/codex-rs/rollout-trace/src/reducer/conversation_tests.rs b/codex-rs/rollout-trace/src/reducer/conversation_tests.rs
index 2c5aef487b..aad48dd3e1 100644
--- a/codex-rs/rollout-trace/src/reducer/conversation_tests.rs
+++ b/codex-rs/rollout-trace/src/reducer/conversation_tests.rs
@@ -67,6 +67,53 @@ fn request_snapshots_reuse_history_without_deduping_new_identical_items() -> any
Ok(())
}
+#[test]
+fn repeated_image_output_matches_content_instead_of_payload_location() -> anyhow::Result<()> {
+ let temp = TempDir::new()?;
+ let writer = create_started_writer(&temp)?;
+ start_turn(&writer, "turn-1")?;
+ let mut output = json!({
+ "type": "function_call_output",
+ "id": "image-output",
+ "call_id": "call-image",
+ "output": [
+ {"type": "input_text", "text": "Image:"},
+ {"type": "input_image", "image_url": "data:image/png;base64,first"}
+ ]
+ });
+ let empty_output = json!({
+ "type": "function_call_output",
+ "call_id": "call-empty",
+ "output": []
+ });
+ for inference_id in ["inference-1", "inference-2"] {
+ let request = writer.write_json_payload(
+ RawPayloadKind::InferenceRequest,
+ &json!({"input": [output.clone(), empty_output.clone()]}),
+ )?;
+ append_inference_start(&writer, inference_id, "turn-1", request)?;
+ }
+
+ let rollout = replay_bundle(temp.path())?;
+ assert_eq!(
+ rollout.inference_calls["inference-1"].request_item_ids,
+ rollout.inference_calls["inference-2"].request_item_ids,
+ );
+ assert_eq!(rollout.conversation_items.len(), 2);
+
+ // Keeping the call and item IDs must not hide a changed image.
+ output["output"][1]["image_url"] = json!("data:image/png;base64,second");
+ let request = writer.write_json_payload(
+ RawPayloadKind::InferenceRequest,
+ &json!({"input": [output]}),
+ )?;
+ append_inference_start(&writer, "inference-3", "turn-1", request)?;
+ expect_replay_error(
+ &temp,
+ "model-visible call id call-image was reused with different content",
+ )
+}
+
#[test]
fn response_outputs_require_harness_acceptance() -> anyhow::Result<()> {
let temp = TempDir::new()?;