Files
codex/codex-rs/otel/src/agent_response.rs
T
xli-oai e6da6fd7c7 Add opt-in OTLP logging for final agent responses (#47649)
## What changed

Add `otel.log_agent_responses`, disabled by default and requiring an OTLP HTTP or gRPC log exporter. Emit `codex.agent_response` for completed `final_answer` messages from main agents and spawned task agents, with session, turn, item, and available parent/root attribution.

Strip memory citations from exported text, preserve the stored answer, and cap responses at 64 KiB on a UTF-8 character boundary. Include the original byte length and a truncation flag, and suppress duplicate item completions within each turn. Exclude commentary and non-task agents.

## Testing

Extend configuration tests for default-off and explicit opt-in behavior, the agent execution test for main/subagent response attribution, and the log-buffer test for response-event exclusion.

GitOrigin-RevId: eacda1150de2e6bac2c667d71b57214b0a086f1a
2026-09-23 20:07:03 +00:00

112 lines
4.1 KiB
Rust

//! Exports opted-in live final responses, with turn-local deduplication and bounded text.
//! Hosts supply completed raw messages and attribution; stored history is never changed.
use crate::SessionTelemetry;
use crate::events::shared::log_event;
use codex_protocol::AgentPath;
use codex_protocol::ResponseItemId;
use codex_protocol::models::ContentItem;
use codex_protocol::models::MessagePhase;
use codex_protocol::models::ResponseItem;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_utils_stream_parser::strip_citations;
use codex_utils_string::take_bytes_at_char_boundary;
use std::collections::HashSet;
use std::sync::Mutex;
const MAX_RESPONSE_BYTES: usize = 65_536;
/// Host-owned attribution for one live completed response item.
pub struct AgentResponseContext<'a> {
pub turn_id: &'a str,
pub session_source: &'a SessionSource,
pub parent_turn_id: Option<String>,
pub root_turn_id: Option<String>,
pub initiating_agent_path: Option<&'a AgentPath>,
}
/// One turn's response-log state. Create a fresh instance for each turn; never replay history.
/// Only construct when response logging is opted in and an OTLP log exporter is configured.
#[derive(Default)]
pub struct AgentResponseLogger {
logged: Mutex<HashSet<ResponseItemId>>,
}
impl AgentResponseLogger {
/// Record a live final message once, using the telemetry of its actual sampling step.
pub fn record(
&self,
telemetry: &SessionTelemetry,
item: &ResponseItem,
context: AgentResponseContext<'_>,
) {
let ResponseItem::Message {
id: Some(item_id),
role,
phase: Some(MessagePhase::FinalAnswer),
content,
..
} = item
else {
return;
};
if role != "assistant" {
return;
}
let (agent_type, parent_conversation_id) = match context.session_source {
SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
parent_thread_id, ..
}) => ("subagent", Some(parent_thread_id.to_string())),
SessionSource::Cli
| SessionSource::VSCode
| SessionSource::Exec
| SessionSource::Mcp
| SessionSource::Custom(_)
| SessionSource::Unknown => ("main", None),
SessionSource::Internal(_)
| SessionSource::SubAgent(
SubAgentSource::Review
| SubAgentSource::Compact
| SubAgentSource::MemoryConsolidation
| SubAgentSource::Other(_),
) => return,
};
let text = content
.iter()
.filter_map(|content| match content {
ContentItem::OutputText { text } => Some(text.as_str()),
ContentItem::InputText { .. }
| ContentItem::InputImage { .. }
| ContentItem::InputAudio { .. } => None,
})
.collect::<String>();
// Keep plan content, stripping memory citations only from the exported copy.
let (text, _) = strip_citations(&text);
if text.trim().is_empty()
|| !self
.logged
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.insert(item_id.clone())
{
return;
}
let response = take_bytes_at_char_boundary(&text, MAX_RESPONSE_BYTES);
log_event!(
telemetry,
event.name = "codex.agent_response",
agent.type = agent_type,
turn.id = context.turn_id,
item.id = item_id.as_str(),
parent.conversation.id = parent_conversation_id.as_deref(),
parent.turn.id = context.parent_turn_id.as_deref(),
root.turn.id = context.root_turn_id.as_deref(),
initiating.agent.path = context.initiating_agent_path.map(AgentPath::as_str),
response = response,
response_length = text.len() as u64,
response_truncated = response.len() < text.len(),
);
}
}