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
This commit is contained in:
xli-oai
2026-09-23 20:07:03 +00:00
committed by copyberry
parent ad26b2520a
commit e6da6fd7c7
12 changed files with 245 additions and 3 deletions
+3
View File
@@ -9900,6 +9900,7 @@ async fn metrics_exporter_defaults_to_statsig_when_missing() -> std::io::Result<
.await?;
assert_eq!(config.otel.metrics_exporter, OtelExporterKind::Statsig);
assert!(!config.otel.agent_response_logging_enabled());
Ok(())
}
@@ -9909,6 +9910,7 @@ async fn trace_exporter_defaults_to_none_when_log_exporter_is_set() -> std::io::
let mut cfg = fixture.cfg.clone();
cfg.otel = Some(OtelConfigToml {
tool_result: toml::from_str("max_bytes = 8192").expect("tool-result logging config"),
log_agent_responses: Some(true),
exporter: Some(OtelExporterKind::OtlpHttp {
endpoint: "http://localhost:14318/v1/logs".to_string(),
headers: HashMap::new(),
@@ -9930,6 +9932,7 @@ async fn trace_exporter_defaults_to_none_when_log_exporter_is_set() -> std::io::
.await?;
assert_eq!(config.otel.tool_result.max_bytes, 8192);
assert!(config.otel.agent_response_logging_enabled());
assert!(matches!(
config.otel.exporter,
OtelExporterKind::OtlpHttp { .. }
+1
View File
@@ -28,6 +28,7 @@ pub(crate) fn resolve_config(
OtelConfig {
tool_result: config.tool_result,
log_user_prompt,
log_agent_responses: config.log_agent_responses.unwrap_or(false),
environment,
exporter,
trace_exporter,
+16
View File
@@ -104,6 +104,22 @@ pub(crate) async fn record_completed_response_item_with_finalized_facts(
std::slice::from_ref(item),
)
.await;
if turn_context.config.otel.agent_response_logging_enabled() {
turn_context
.extension_data
.get_or_init(codex_otel::AgentResponseLogger::default)
.record(
&step_context.session_telemetry,
item,
codex_otel::AgentResponseContext {
turn_id: &turn_context.sub_id,
session_source: &turn_context.session_source,
parent_turn_id: turn_context.turn_metadata_state.parent_turn_id(),
root_turn_id: turn_context.turn_metadata_state.root_turn_id(),
initiating_agent_path: turn_context.turn_metadata_state.initiating_agent_path(),
},
);
}
let defers_mailbox_delivery = finalized_facts.map_or_else(
|| {
completed_item_defers_mailbox_delivery_to_next_turn(