Files
codex/codex-rs/rollout-trace/src/reducer/conversation.rs
T
cassirer-openai 076ac7856b Link agent tool calls to delivered messages in rollout traces (#62709)
## Why

Matching messages by text can link a tool call to an earlier message
with the same content. A recipient can also accept a message without
ever including it in a model request.

## What changed

Record the originating tool call ID when the recipient adds a spawn,
task assignment, or message to its history. On replay, use that ID to
link the accepted message to its sender even if the recipient records
it before the sender tool finishes.

Keep the interaction edge pointed at the recipient thread until the
message appears in a model request. Track distinct conversation items
across history resets and count repeated prompt prefixes only once.

## Testing

Replay tests cover identical message text, receipt before tool
completion, and messages observed again after compaction.

GitOrigin-RevId: ccab7c33993ba5fce7fceb733dc90c9d428ec78f
2026-11-20 08:49:07 +00:00

935 lines
35 KiB
Rust

//! Conversation reduction from model-facing payload snapshots.
//!
//! 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 std::collections::HashSet;
use anyhow::Context;
use anyhow::Ok;
use anyhow::Result;
use anyhow::bail;
use serde_json::Value;
use self::normalize::NormalizedConversationItem;
use super::TraceReducer;
use crate::model::CompactionId;
use crate::model::ConversationBody;
use crate::model::ConversationItem;
use crate::model::ConversationItemKind;
use crate::model::ConversationPart;
use crate::model::ConversationRole;
use crate::model::InferenceCallId;
use crate::model::ProducerRef;
use crate::payload::RawPayloadRef;
use crate::shard::ObservedJson;
mod normalize;
impl TraceReducer {
pub(super) fn record_accepted_item(
&mut self,
thread_id: &str,
turn_id: Option<String>,
at_unix_ms: i64,
wire_id: &str,
payload: &RawPayloadRef,
) -> Result<()> {
// A snapshot can arrive first, or the recorder can repeat a notification.
let key = (thread_id.to_owned(), wire_id.to_owned());
if self.receivable_ids.contains_key(&key) {
return Ok(());
}
let ObservedJson { mut value, missing } = self.payload_reader.observed_json(payload)?;
let item = normalize::normalize_model_item(&mut value, payload, &missing)?;
if !item.is_delivery_or_tool_output() || item.responses_item_id.as_deref() != Some(wire_id)
{
bail!("accepted item payload does not match {wire_id}");
}
let is_delivery = item.agent_message.is_some();
let produced_by = if let Some(call_id) = &item.call_id
&& let Some(tool_call_id) = self.single_tool_for_model_visible_call(call_id)?
{
vec![ProducerRef::Tool { tool_call_id }]
} else {
Vec::new()
};
let id = self.create_conversation_item(thread_id, turn_id, at_unix_ms, item, produced_by);
self.thread_mut(thread_id)?
.conversation_item_ids
.push(id.clone());
if is_delivery {
self.resolve_pending_agent_edges_for_item(&id)?;
}
self.receivable_ids.insert(
key,
super::ReceivableItem {
item_id: id,
awaiting_snapshot: true,
},
);
Ok(())
}
/// Reduces an inference request input snapshot into model-visible conversation items.
///
/// 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,
inference_call_id: &InferenceCallId,
thread_id: &str,
codex_turn_id: &str,
request_payload: &RawPayloadRef,
stripped_tool_output_ids: Vec<(usize, String)>,
) -> Result<Vec<String>> {
if !request_payload.capture.is_complete() {
return Ok(Vec::new());
}
let ObservedJson {
value: mut payload,
missing,
} = self.payload_reader.observed_json(request_payload)?;
let Some(input) = payload.get_mut("input") else {
bail!(
"inference request payload {} did not contain input",
request_payload.raw_payload_id
);
};
let Some(request_items) = input.as_array_mut() else {
bail!(
"inference request payload {} had non-array input",
request_payload.raw_payload_id
);
};
// The wire payload stays intact on disk. Resolve accepted history by its native ID,
// even for two identical named outputs or a trimmed result in a websocket delta.
for (index, history_id) in stripped_tool_output_ids {
let item = request_items
.get_mut(index)
.context("stripped tool output index outside request")?;
if missing["input"][index].is_string() {
continue; // The captured request has no item to attach the history ID to.
}
if item.get("id").is_some()
|| !matches!(
item["type"].as_str(),
Some("function_call_output" | "custom_tool_call_output")
)
{
bail!(
"stripped tool output ID does not refer to an ID-less output at input {index}"
);
}
item["id"] = Value::String(history_id);
}
let items =
normalize::normalize_model_items(request_items, request_payload, &missing["input"])?;
let previous_response_id = payload.get("previous_response_id").and_then(Value::as_str);
// After compaction, the next full request is compared against the installed replacement
// history, not the pre-compaction prompt. Any repeated developer/context prefix that Codex
// reinjects must therefore become a fresh post-compaction conversation item.
let post_compaction_snapshot = if previous_response_id.is_none() {
self.pending_compaction_replacement_item_ids
.get(thread_id)
.cloned()
} else {
None
};
let request_item_ids = if let Some(previous_response_id) = previous_response_id {
// Streaming follow-up requests can send only the new input plus a
// `previous_response_id`. The trace model still exposes the full
// model-visible input, so rebuild the omitted prefix from the
// previous request and response before reducing this delta.
let previous_items = self
.rollout
.inference_calls
.values()
.find(|inference| {
inference.thread_id == thread_id
&& inference.response_id.as_deref() == Some(previous_response_id)
})
.map(|inference| {
let mut ids = inference.request_item_ids.clone();
ids.extend(inference.response_item_ids.clone());
let incomplete = std::iter::once(&inference.raw_request_payload_id)
.chain(inference.raw_response_payload_id.as_ref())
.any(|id| !self.rollout.raw_payloads[id].is_complete());
(ids, incomplete)
});
let Some((mut item_ids, incomplete)) = previous_items else {
bail!(
"incremental inference request {inference_call_id} referenced unknown previous_response_id {previous_response_id}"
);
};
let delta_item_ids = self.reconcile_conversation_items(
items,
ReconcileItems {
thread_id,
codex_turn_id,
wall_time_unix_ms,
produced_by: Vec::new(),
start_index: item_ids.len(),
mode: ReconcileMode::AppendOnly,
// After a capture gap, the number of known prefix items is not
// a position in the thread's last fully observed snapshot.
snapshot_override: incomplete.then_some(item_ids.as_slice()),
},
)?;
item_ids.extend(delta_item_ids);
item_ids
} else {
self.reconcile_conversation_items(
items,
ReconcileItems {
thread_id,
codex_turn_id,
wall_time_unix_ms,
produced_by: Vec::new(),
start_index: 0,
mode: ReconcileMode::FullSnapshot,
snapshot_override: post_compaction_snapshot.as_deref(),
},
)?
};
self.append_thread_conversation_items(thread_id, &request_item_ids)?;
if post_compaction_snapshot.is_some() {
self.pending_compaction_replacement_item_ids
.remove(thread_id);
}
self.thread_conversation_snapshots
.insert(thread_id.to_string(), request_item_ids.clone());
Ok(request_item_ids)
}
/// Reduces an inference response payload into conversation items produced by the call.
pub(super) fn reduce_inference_response(
&mut self,
wall_time_unix_ms: i64,
inference_call_id: &InferenceCallId,
response_payload: &RawPayloadRef,
) -> Result<Vec<String>> {
if !response_payload.capture.is_complete() {
return Ok(Vec::new());
}
let ObservedJson {
value: payload,
missing,
} = self.payload_reader.observed_json(response_payload)?;
let Some(output_items) = payload.get("output_items").and_then(Value::as_array) else {
bail!(
"inference response payload {} did not contain output_items",
response_payload.raw_payload_id
);
};
let Some((thread_id, codex_turn_id)) = self
.rollout
.inference_calls
.get(inference_call_id)
.map(|inference| (inference.thread_id.clone(), inference.codex_turn_id.clone()))
else {
bail!("inference response referenced unknown call {inference_call_id}");
};
let items = normalize::normalize_model_items(
output_items,
response_payload,
&missing["output_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,
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(),
}],
candidates,
},
)?;
self.append_thread_conversation_items(&thread_id, &response_item_ids)?;
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")
.and_then(normalize::token_usage_from_value)
&& let Some(inference) = self.rollout.inference_calls.get_mut(inference_call_id)
{
inference.usage = Some(usage);
}
Ok(response_item_ids)
}
fn reconcile_conversation_items(
&mut self,
items: Vec<NormalizedConversationItem>,
context: ReconcileItems<'_>,
) -> Result<Vec<String>> {
let previous_snapshot = context.snapshot_override.map_or_else(
|| {
self.thread_conversation_snapshots
.get(context.thread_id)
.cloned()
.unwrap_or_default()
},
<[_]>::to_vec,
);
let mut item_ids = Vec::with_capacity(items.len());
let mut used_item_ids = HashSet::with_capacity(items.len());
for (offset, item) in items.into_iter().enumerate() {
let index = context.start_index + offset;
let tool_link_item = item.clone();
self.ensure_call_id_consistency(context.thread_id, &item)?;
let item_id = if let Some(previous_item_id) = previous_snapshot.get(index) {
if !used_item_ids.contains(previous_item_id)
&& self.item_matches(previous_item_id, &item)
{
previous_item_id.clone()
} else if matches!(context.mode, ReconcileMode::FullSnapshot) {
self.find_matching_snapshot_item(&previous_snapshot, &used_item_ids, &item)
.unwrap_or_else(|| {
self.create_conversation_item(
context.thread_id,
Some(context.codex_turn_id.to_string()),
context.wall_time_unix_ms,
item,
context.produced_by.clone(),
)
})
} else {
let codex_turn_id = context.codex_turn_id;
let thread_id = context.thread_id;
bail!(
"model conversation mismatch while reducing turn {codex_turn_id} for \
thread {thread_id} at item index {index}: existing item \
{previous_item_id} does not match the current model payload item"
);
}
} else if matches!(context.mode, ReconcileMode::FullSnapshot) {
self.find_matching_snapshot_item(&previous_snapshot, &used_item_ids, &item)
.unwrap_or_else(|| {
self.create_conversation_item(
context.thread_id,
Some(context.codex_turn_id.to_string()),
context.wall_time_unix_ms,
item,
context.produced_by.clone(),
)
})
} else {
self.create_conversation_item(
context.thread_id,
Some(context.codex_turn_id.to_string()),
context.wall_time_unix_ms,
item,
context.produced_by.clone(),
)
};
self.update_conversation_item_from_sighting(
&item_id,
&tool_link_item,
&context.produced_by,
)?;
self.attach_model_visible_tool_item(
&item_id,
tool_link_item.call_id.as_deref(),
&tool_link_item.kind,
)?;
self.attach_model_visible_code_cell_item(
&item_id,
tool_link_item.call_id.as_deref(),
&tool_link_item.kind,
)?;
self.resolve_pending_agent_edges_for_item(&item_id)?;
used_item_ids.insert(item_id.clone());
item_ids.push(item_id);
}
Ok(item_ids)
}
/// Reduces a compaction checkpoint payload into installed replacement history.
///
/// The returned ids let the compaction reducer record both the boundary marker
/// and the snapshot that future full requests should reconcile against.
pub(super) fn reduce_compaction_checkpoint(
&mut self,
wall_time_unix_ms: i64,
thread_id: &str,
codex_turn_id: &str,
compaction_id: &CompactionId,
checkpoint_payload: &RawPayloadRef,
) -> Result<ReducedCompactionCheckpoint> {
let (input_items, replacement_items) = if checkpoint_payload.capture.is_complete() {
let ObservedJson {
value: payload,
missing,
} = self.payload_reader.observed_json(checkpoint_payload)?;
let input = required_array(&payload, "input_history", checkpoint_payload)?;
let replacement = required_array(&payload, "replacement_history", checkpoint_payload)?;
(
normalize::normalize_model_items(
input,
checkpoint_payload,
&missing["input_history"],
)?,
normalize::normalize_model_items(
replacement,
checkpoint_payload,
&missing["replacement_history"],
)?,
)
} else {
// The install event still establishes a compaction boundary.
(Vec::new(), Vec::new())
};
let input_candidates = self
.thread_conversation_snapshots
.get(thread_id)
.cloned()
.unwrap_or_default();
let input_item_ids = self.reconcile_detached_conversation_items(
input_items,
DetachedReconcileItems {
thread_id,
codex_turn_id,
wall_time_unix_ms,
produced_by: Vec::new(),
candidates: input_candidates,
},
)?;
// A compaction checkpoint has two transcript effects. First, record the structural
// boundary where old live history ended. Then append the replacement items, including
// the provider-visible summary item if the compact endpoint returned one.
let marker_item_id = self.create_conversation_item(
thread_id,
Some(codex_turn_id.to_string()),
wall_time_unix_ms,
NormalizedConversationItem {
structured_output_hash: None,
responses_item_id: None,
role: ConversationRole::Assistant,
channel: None,
kind: ConversationItemKind::CompactionMarker,
agent_message: None,
// The summary is a separate model/provider-visible item. Keep the marker body
// empty so transcript renderers cannot mistake the boundary for prompt content.
body: ConversationBody { parts: Vec::new() },
call_id: None,
},
vec![ProducerRef::Compaction {
compaction_id: compaction_id.clone(),
}],
);
let replacement_item_ids = self.reconcile_detached_conversation_items(
replacement_items,
DetachedReconcileItems {
thread_id,
codex_turn_id,
wall_time_unix_ms,
produced_by: vec![ProducerRef::Compaction {
compaction_id: compaction_id.clone(),
}],
// Replacement history is a rewrite boundary. Even if the compact endpoint emits
// text that matches old history, the installed item is a new post-compaction
// conversation item and should not reuse a pre-compaction ID.
candidates: Vec::new(),
},
)?;
self.append_thread_conversation_items(thread_id, &input_item_ids)?;
self.append_thread_conversation_items(thread_id, std::slice::from_ref(&marker_item_id))?;
self.append_thread_conversation_items(thread_id, &replacement_item_ids)?;
Ok(ReducedCompactionCheckpoint {
input_item_ids,
marker_item_id,
replacement_item_ids,
})
}
fn reconcile_detached_conversation_items(
&mut self,
items: Vec<NormalizedConversationItem>,
context: DetachedReconcileItems<'_>,
) -> Result<Vec<String>> {
let mut item_ids = Vec::with_capacity(items.len());
let mut used_item_ids = HashSet::with_capacity(items.len());
for item in items {
let tool_link_item = item.clone();
self.ensure_call_id_consistency(context.thread_id, &item)?;
let item_id = self
.find_matching_snapshot_item(&context.candidates, &used_item_ids, &item)
.unwrap_or_else(|| {
self.create_conversation_item(
context.thread_id,
Some(context.codex_turn_id.to_string()),
context.wall_time_unix_ms,
item,
context.produced_by.clone(),
)
});
self.update_conversation_item_from_sighting(
&item_id,
&tool_link_item,
&context.produced_by,
)?;
self.attach_model_visible_tool_item(
&item_id,
tool_link_item.call_id.as_deref(),
&tool_link_item.kind,
)?;
self.attach_model_visible_code_cell_item(
&item_id,
tool_link_item.call_id.as_deref(),
&tool_link_item.kind,
)?;
self.resolve_pending_agent_edges_for_item(&item_id)?;
used_item_ids.insert(item_id.clone());
item_ids.push(item_id);
}
Ok(item_ids)
}
fn create_conversation_item(
&mut self,
thread_id: &str,
codex_turn_id: Option<String>,
first_seen_at_unix_ms: i64,
item: NormalizedConversationItem,
produced_by: Vec<ProducerRef>,
) -> String {
// A request or checkpoint can contain a trimmed version of an accepted item.
// Reuse its graph ID and retain the representation actually seen by the model.
if item.is_delivery_or_tool_output()
&& let Some(wire_id) = &item.responses_item_id
{
let key = (thread_id.to_owned(), wire_id.clone());
if let Some(receipt) = self.receivable_ids.get(&key)
&& receipt.awaiting_snapshot
{
return receipt.item_id.clone();
}
}
let item_id = self.next_conversation_item_id();
if let Some(hash) = item.structured_output_hash {
self.structured_output_hashes.insert(item_id.clone(), hash);
}
self.rollout.conversation_items.insert(
item_id.clone(),
ConversationItem {
item_id: item_id.clone(),
responses_item_id: item.responses_item_id,
accepted_at_unix_ms: None,
accepted_seq: None,
thread_id: thread_id.to_string(),
codex_turn_id,
first_seen_at_unix_ms,
role: item.role,
channel: item.channel,
kind: item.kind,
agent_message: item.agent_message,
body: item.body,
call_id: item.call_id,
produced_by,
},
);
item_id
}
fn update_conversation_item_from_sighting(
&mut self,
item_id: &str,
normalized: &NormalizedConversationItem,
produced_by: &[ProducerRef],
) -> Result<()> {
let Some(item) = self.rollout.conversation_items.get_mut(item_id) else {
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)?;
}
// Replace the accepted body on its first snapshot only. Later snapshots may reference
// the same content through a different raw payload.
if normalized.is_delivery_or_tool_output()
&& let Some(wire_id) = &normalized.responses_item_id
{
let key = (item.thread_id.clone(), wire_id.clone());
let receipt = self
.receivable_ids
.entry(key)
.or_insert_with(|| super::ReceivableItem {
item_id: item_id.to_owned(),
awaiting_snapshot: false,
});
if receipt.awaiting_snapshot && receipt.item_id == item_id {
receipt.awaiting_snapshot = false;
item.body.clone_from(&normalized.body);
if let Some(hash) = normalized.structured_output_hash {
self.structured_output_hashes
.insert(item_id.to_owned(), hash);
} else {
self.structured_output_hashes.remove(item_id);
}
if normalized.agent_message.is_none() {
self.snapshot_output_anchors.insert(item_id.to_owned());
}
}
}
for producer in produced_by {
if !item.produced_by.contains(producer) {
item.produced_by.push(producer.clone());
}
}
Ok(())
}
fn append_thread_conversation_items(
&mut self,
thread_id: &str,
item_ids: &[String],
) -> Result<()> {
let history = &mut self
.rollout
.threads
.get_mut(thread_id)
.context("unknown thread")?
.conversation_item_ids;
let mut new_prefix = history.len();
for id in item_ids {
if self.snapshot_output_anchors.remove(id) {
// This output already has its acceptance position. Insert any newly exposed
// context immediately before it, preserving deliveries recorded in between.
if new_prefix < history.len() {
let prefix = history.split_off(new_prefix);
let anchor = history
.iter()
.position(|known| known == id)
.context("missing accepted output")?;
history.splice(anchor..anchor, prefix);
}
new_prefix = history.len();
} else if !history.contains(id) {
history.push(id.clone());
}
}
Ok(())
}
fn find_matching_snapshot_item(
&self,
previous_snapshot: &[String],
used_item_ids: &HashSet<String>,
normalized: &NormalizedConversationItem,
) -> Option<String> {
previous_snapshot
.iter()
.find(|item_id| {
!used_item_ids.contains(*item_id) && self.item_matches(item_id, normalized)
})
.cloned()
}
fn ensure_call_id_consistency(
&self,
thread_id: &str,
normalized: &NormalizedConversationItem,
) -> Result<()> {
// One call can emit several outputs, and history trimming can rewrite
// them. Keep distinct outputs as separate items; only the invocation
// itself must remain consistent.
if !matches!(
normalized.kind,
ConversationItemKind::FunctionCall | ConversationItemKind::CustomToolCall
) {
return Ok(());
}
let Some(call_id) = normalized.call_id.as_deref() else {
return Ok(());
};
for item in self.rollout.conversation_items.values() {
if item.thread_id == thread_id
&& item.call_id.as_deref() == Some(call_id)
&& item.kind == normalized.kind
&& !conversation_item_matches(item, normalized)
{
bail!("model-visible call id {call_id} was reused with different content");
}
}
Ok(())
}
fn item_matches(&self, item_id: &str, normalized: &NormalizedConversationItem) -> bool {
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;
}
let existing_hash = self.structured_output_hashes.get(item_id);
if existing_hash.is_some() || normalized.structured_output_hash.is_some() {
return existing_hash == normalized.structured_output_hash.as_ref();
}
conversation_item_matches(item, normalized)
}
fn next_conversation_item_id(&mut self) -> String {
let ordinal = self.next_conversation_item_ordinal;
self.next_conversation_item_ordinal += 1;
format!("conversation_item:{ordinal}")
}
}
#[derive(Clone, Copy)]
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. Reuse matching content at most once
/// per snapshot, respecting supplied item IDs without requiring the same position.
FullSnapshot,
/// 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,
}
struct ReconcileItems<'a> {
thread_id: &'a str,
codex_turn_id: &'a str,
wall_time_unix_ms: i64,
produced_by: Vec<ProducerRef>,
start_index: usize,
mode: ReconcileMode,
snapshot_override: Option<&'a [String]>,
}
struct DetachedReconcileItems<'a> {
thread_id: &'a str,
codex_turn_id: &'a str,
wall_time_unix_ms: i64,
produced_by: Vec<ProducerRef>,
candidates: Vec<String>,
}
/// Conversation ids produced when a compaction checkpoint is installed.
///
/// The marker item records the boundary, while replacement items are the live
/// history that subsequent full requests should treat as their baseline.
pub(super) struct ReducedCompactionCheckpoint {
pub(super) input_item_ids: Vec<String>,
pub(super) marker_item_id: String,
pub(super) replacement_item_ids: Vec<String>,
}
fn required_array<'a>(
payload: &'a Value,
key: &str,
raw_payload: &RawPayloadRef,
) -> Result<&'a Vec<Value>> {
payload.get(key).and_then(Value::as_array).with_context(|| {
format!(
"compaction checkpoint payload {} did not contain array {key}",
raw_payload.raw_payload_id
)
})
}
fn conversation_item_matches(
item: &ConversationItem,
normalized: &NormalizedConversationItem,
) -> bool {
let body_matches = if item.kind == ConversationItemKind::Reasoning
&& normalized.kind == ConversationItemKind::Reasoning
{
reasoning_body_matches(&item.body, &normalized.body)
} else {
conversation_body_matches(&item.body, &normalized.body)
};
item.role == normalized.role
&& item.channel == normalized.channel
&& item.kind == normalized.kind
&& item.agent_message == normalized.agent_message
&& body_matches
&& item.call_id == normalized.call_id
}
fn conversation_body_matches(left: &ConversationBody, right: &ConversationBody) -> bool {
left.parts.len() == right.parts.len()
&& left
.parts
.iter()
.zip(&right.parts)
.all(|(left, right)| match (left, right) {
(
ConversationPart::Json {
summary: left_summary,
raw_payload_id: _,
},
ConversationPart::Json {
summary: right_summary,
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,
})
}
fn reasoning_body_matches(left: &ConversationBody, right: &ConversationBody) -> bool {
if conversation_body_matches(left, right) {
return true;
}
// The Responses API may return readable reasoning on completion, but later
// request snapshots often replay only the encrypted blob. Treat the blob as
// stable model-visible identity and merge readable text as best-effort
// evidence, because request/response serialization can observe different
// readable forms for the same encrypted reasoning item.
let Some(left_encoded) = reasoning_encoded_part(left) else {
return false;
};
let Some(right_encoded) = reasoning_encoded_part(right) else {
return false;
};
left_encoded == right_encoded
}
fn merge_reasoning_body(
existing: &mut ConversationBody,
incoming: &ConversationBody,
) -> Result<()> {
if conversation_body_matches(existing, incoming) {
return Ok(());
}
if !reasoning_body_matches(existing, incoming) {
bail!("reasoning item merge attempted with different encrypted_content identity");
}
let existing_text_parts = reasoning_text_parts(existing);
let existing_summary_parts = reasoning_summary_parts(existing);
if !existing_text_parts.is_empty() && !existing_summary_parts.is_empty() {
return Ok(());
}
let incoming_text_parts = reasoning_text_parts(incoming);
let incoming_summary_parts = reasoning_summary_parts(incoming);
let text_parts = if !existing_text_parts.is_empty() {
existing_text_parts
} else {
incoming_text_parts
};
let summary_parts = if !existing_summary_parts.is_empty() {
existing_summary_parts
} else {
incoming_summary_parts
};
// We already know that the encoded part exist (and matches).
let encoded_parts = reasoning_encoded_parts(existing);
existing.parts = text_parts
.into_iter()
.cloned()
.chain(summary_parts.into_iter().cloned())
.chain(encoded_parts.into_iter().cloned())
.collect();
Ok(())
}
fn reasoning_text_parts(body: &ConversationBody) -> Vec<&ConversationPart> {
body.parts
.iter()
.filter(|part| matches!(part, ConversationPart::Text { .. }))
.collect()
}
fn reasoning_summary_parts(body: &ConversationBody) -> Vec<&ConversationPart> {
body.parts
.iter()
.filter(|part| matches!(part, ConversationPart::Summary { .. }))
.collect()
}
fn reasoning_encoded_parts(body: &ConversationBody) -> Vec<&ConversationPart> {
body.parts
.iter()
.filter(|part| matches!(part, ConversationPart::Encoded { .. }))
.collect()
}
fn reasoning_encoded_part(body: &ConversationBody) -> Option<(&str, &str)> {
body.parts.iter().find_map(|part| {
if let ConversationPart::Encoded { label, value } = part {
Some((label.as_str(), value.as_str()))
} else {
None
}
})
}
#[cfg(test)]
#[path = "conversation_tests.rs"]
mod tests;