Files
codex/codex-rs/rollout-trace/src/raw_event.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

381 lines
14 KiB
Rust

//! Append-only raw trace events.
use crate::model::AgentThreadId;
use crate::model::CodeCellRuntimeStatus;
use crate::model::CodexTurnId;
use crate::model::CompactionId;
use crate::model::CompactionRequestId;
use crate::model::EdgeId;
use crate::model::ExecutionStatus;
use crate::model::InferenceCallId;
use crate::model::McpCallId;
use crate::model::ModelVisibleCallId;
use crate::model::RolloutStatus;
use crate::model::ToolCallId;
use crate::model::ToolCallKind;
use crate::model::ToolCallSummary;
use crate::payload::RawPayloadRef;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
/// Monotonic sequence number assigned by the raw trace writer.
pub type RawEventSeq = u64;
/// Current raw event envelope schema version.
pub(crate) const RAW_TRACE_EVENT_SCHEMA_VERSION: u32 = 3;
/// One append-only raw trace event.
///
/// Every event uses the same envelope so partial replay and corruption checks
/// can run before the reducer understands the event-specific payload.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RawTraceEvent {
pub schema_version: u32,
/// Contiguous writer-assigned order inside one rollout event log.
pub seq: RawEventSeq,
/// Unix wall-clock timestamp in milliseconds. Use for display/latency.
pub wall_time_unix_ms: i64,
pub rollout_id: String,
pub thread_id: Option<AgentThreadId>,
pub codex_turn_id: Option<CodexTurnId>,
pub payload: RawTraceEventPayload,
}
/// Writer-supplied context that appears in the raw event envelope.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RawTraceEventContext {
pub thread_id: Option<AgentThreadId>,
pub codex_turn_id: Option<CodexTurnId>,
}
impl RawTraceEventContext {
/// Scopes an observation to a model or tool turn.
pub(crate) fn turn(thread_id: &str, codex_turn_id: &str) -> Self {
Self {
thread_id: Some(thread_id.to_owned()),
codex_turn_id: Some(codex_turn_id.to_owned()),
}
}
}
/// Runtime requester as observed at the raw tool boundary.
///
/// This intentionally uses runtime-local identifiers. The reducer is the only
/// place that maps these handles to graph identities such as `CodeCellId`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum RawToolCallRequester {
Model,
CodeCell {
/// Runtime-local code-mode cell handle.
runtime_cell_id: String,
},
}
/// Typed payload for a raw trace event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum RawTraceEventPayload {
/// Final record after all producers release a writer with no recorded errors.
RecordingCompleted,
RolloutStarted {
trace_id: String,
root_thread_id: AgentThreadId,
},
RolloutEnded {
status: RolloutStatus,
},
ThreadStarted {
thread_id: AgentThreadId,
/// Stable agent path.
agent_path: String,
metadata_payload: Option<RawPayloadRef>,
},
ThreadEnded {
thread_id: AgentThreadId,
status: RolloutStatus,
},
/// An evicted runtime was reloaded for a thread already recorded in this bundle.
ThreadResumed {
thread_id: AgentThreadId,
},
CodexTurnStarted {
codex_turn_id: CodexTurnId,
thread_id: AgentThreadId,
},
CodexTurnEnded {
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,
#[serde(default, skip_serializing_if = "Option::is_none")]
source_tool_call_id: Option<ToolCallId>,
/// A delivery may never appear in a subsequent inference or compaction snapshot.
#[serde(default, skip_serializing_if = "Option::is_none")]
agent_message_payload: Option<RawPayloadRef>,
/// The accepted output has its history ID and can differ from the dispatch result.
#[serde(default, skip_serializing_if = "Option::is_none")]
tool_output_payload: Option<RawPayloadRef>,
},
InferenceStarted {
inference_call_id: InferenceCallId,
thread_id: AgentThreadId,
codex_turn_id: CodexTurnId,
model: String,
provider_name: String,
/// Actual outgoing HTTP body or WebSocket message.
request_payload: RawPayloadRef,
/// Core prompt before provider formatting.
prepared_prompt_payload: RawPayloadRef,
/// Full provider-formatted context only when the previous response was an untraced warmup.
#[serde(default, skip_serializing_if = "Option::is_none")]
warmup_context_payload: Option<RawPayloadRef>,
/// History IDs removed from tool outputs on the wire, indexed within this request input.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
stripped_tool_output_ids: Vec<(usize, String)>,
},
InferenceCompleted {
inference_call_id: InferenceCallId,
/// Responses API `response.id`; used by `previous_response_id`.
response_id: Option<String>,
/// Provider transport request id, such as `x-request-id`.
upstream_request_id: Option<String>,
response_payload: RawPayloadRef,
},
InferenceFailed {
inference_call_id: InferenceCallId,
/// Provider transport request id, such as `x-request-id`, when the
/// provider returned one before the stream failed.
upstream_request_id: Option<String>,
error: String,
/// Partial response payload, when stream events arrived before failure.
partial_response_payload: Option<RawPayloadRef>,
},
InferenceCancelled {
inference_call_id: InferenceCallId,
/// Provider transport request id, such as `x-request-id`, when observed
/// before Codex stopped consuming the stream.
upstream_request_id: Option<String>,
/// Why Codex stopped consuming the provider stream before a terminal response event.
reason: String,
/// Completed output items observed before cancellation, if any.
partial_response_payload: Option<RawPayloadRef>,
},
ToolCallStarted {
tool_call_id: ToolCallId,
/// Protocol/model call ID when this runtime call came from model output.
model_visible_call_id: Option<String>,
/// Code-mode runtime bridge ID when model-authored code issued this call.
code_mode_runtime_tool_id: Option<String>,
/// Runtime requester that caused this tool lifecycle.
requester: RawToolCallRequester,
kind: ToolCallKind,
summary: ToolCallSummary,
invocation_payload: Option<RawPayloadRef>,
},
/// Bridge correlation UUID assigned only when a tool reaches an MCP backend.
McpToolCallCorrelationAssigned {
tool_call_id: ToolCallId,
mcp_call_id: McpCallId,
},
/// Exec selected by the runtime after resolving a recyclable process handle.
StdinTargetResolved {
tool_call_id: ToolCallId,
exec_tool_call_id: ToolCallId,
},
/// Live Code Mode cell reached by a wait or terminate call.
CodeCellWaitTargetResolved {
tool_call_id: ToolCallId,
runtime_cell_id: String,
},
ToolCallRuntimeStarted {
tool_call_id: ToolCallId,
/// Executed handler, which may differ from the requested callable.
kind: ToolCallKind,
/// Runtime/protocol observation for how Codex began executing the tool.
runtime_payload: RawPayloadRef,
},
ToolCallRuntimeEnded {
tool_call_id: ToolCallId,
status: ExecutionStatus,
/// Runtime/protocol observation for how Codex finished executing the tool.
runtime_payload: RawPayloadRef,
},
ToolCallEnded {
tool_call_id: ToolCallId,
status: ExecutionStatus,
result_payload: Option<RawPayloadRef>,
},
CodeCellStarted {
/// Runtime-local handle allocated by code mode for waits and nested tools.
runtime_cell_id: String,
/// Custom tool call id on the model-visible `exec` item.
model_visible_call_id: ModelVisibleCallId,
/// JavaScript source after the public `exec` wrapper has been parsed.
source_js: String,
},
CodeCellInitialResponse {
/// Runtime-local handle, matching `CodeCellStarted`.
runtime_cell_id: String,
status: CodeCellRuntimeStatus,
response_payload: Option<RawPayloadRef>,
},
CodeCellEnded {
/// Runtime-local handle, matching `CodeCellStarted`.
runtime_cell_id: String,
status: CodeCellRuntimeStatus,
response_payload: Option<RawPayloadRef>,
},
CompactionRequestStarted {
compaction_id: CompactionId,
compaction_request_id: CompactionRequestId,
thread_id: AgentThreadId,
codex_turn_id: CodexTurnId,
model: String,
provider_name: String,
request_payload: RawPayloadRef,
},
CompactionRequestCompleted {
compaction_id: CompactionId,
compaction_request_id: CompactionRequestId,
response_payload: RawPayloadRef,
},
CompactionRequestFailed {
compaction_id: CompactionId,
compaction_request_id: CompactionRequestId,
error: String,
},
/// Checkpoint installation event for remote-compacted replacement history.
CompactionInstalled {
compaction_id: CompactionId,
/// Trace-only checkpoint payload. Do not route this through public UI protocol.
checkpoint_payload: RawPayloadRef,
},
/// Multi-agent v2 child-to-parent completion delivery.
AgentResultObserved {
edge_id: EdgeId,
child_thread_id: AgentThreadId,
child_codex_turn_id: CodexTurnId,
parent_thread_id: AgentThreadId,
message: String,
/// Raw notification payload. This is evidence for the runtime delivery,
/// not the parent-side model-visible item.
carried_payload: Option<RawPayloadRef>,
},
/// Existing UI/protocol event wrapped into trace format.
ProtocolEventObserved {
event_type: String,
event_payload: RawPayloadRef,
},
/// Structured payload for early instrumentation before a dedicated variant exists.
Other {
kind: String,
summary: String,
payloads: Vec<RawPayloadRef>,
/// Small structured metadata. Large data belongs in `payloads`.
metadata: Value,
},
}
impl RawTraceEventPayload {
/// Raw payload refs that must exist before this raw event is appended.
pub(crate) fn raw_payload_refs(&self) -> Vec<&RawPayloadRef> {
match self {
RawTraceEventPayload::RecordingCompleted
| RawTraceEventPayload::RolloutStarted { .. }
| RawTraceEventPayload::RolloutEnded { .. }
| RawTraceEventPayload::ThreadEnded { .. }
| RawTraceEventPayload::ThreadResumed { .. }
| RawTraceEventPayload::CodexTurnStarted { .. }
| RawTraceEventPayload::CodexTurnEnded { .. }
| RawTraceEventPayload::CompactionRequestFailed { .. }
| RawTraceEventPayload::CodeCellStarted { .. }
| RawTraceEventPayload::McpToolCallCorrelationAssigned { .. }
| RawTraceEventPayload::StdinTargetResolved { .. }
| RawTraceEventPayload::CodeCellWaitTargetResolved { .. }
| RawTraceEventPayload::AgentResultObserved {
carried_payload: None,
..
} => Vec::new(),
RawTraceEventPayload::HistoryItemAccepted {
agent_message_payload,
tool_output_payload,
..
} => agent_message_payload
.iter()
.chain(tool_output_payload)
.collect(),
RawTraceEventPayload::ThreadStarted {
metadata_payload, ..
} => metadata_payload.iter().collect(),
RawTraceEventPayload::InferenceStarted {
request_payload,
prepared_prompt_payload,
warmup_context_payload,
..
} => [request_payload, prepared_prompt_payload]
.into_iter()
.chain(warmup_context_payload)
.collect(),
RawTraceEventPayload::InferenceCompleted {
response_payload: request_payload,
..
}
| RawTraceEventPayload::CompactionRequestStarted {
request_payload, ..
}
| RawTraceEventPayload::CompactionRequestCompleted {
response_payload: request_payload,
..
}
| RawTraceEventPayload::CompactionInstalled {
checkpoint_payload: request_payload,
..
}
| RawTraceEventPayload::ProtocolEventObserved {
event_payload: request_payload,
..
} => vec![request_payload],
RawTraceEventPayload::InferenceFailed {
partial_response_payload,
..
}
| RawTraceEventPayload::InferenceCancelled {
partial_response_payload,
..
}
| RawTraceEventPayload::ToolCallStarted {
invocation_payload: partial_response_payload,
..
}
| RawTraceEventPayload::ToolCallEnded {
result_payload: partial_response_payload,
..
}
| RawTraceEventPayload::CodeCellInitialResponse {
response_payload: partial_response_payload,
..
}
| RawTraceEventPayload::CodeCellEnded {
response_payload: partial_response_payload,
..
} => partial_response_payload.iter().collect(),
RawTraceEventPayload::AgentResultObserved {
carried_payload: Some(carried_payload),
..
} => vec![carried_payload],
RawTraceEventPayload::ToolCallRuntimeStarted {
runtime_payload, ..
}
| RawTraceEventPayload::ToolCallRuntimeEnded {
runtime_payload, ..
} => vec![runtime_payload],
RawTraceEventPayload::Other { payloads, .. } => payloads.iter().collect(),
}
}
}