Make tool-result telemetry limits configurable (#39779)

## What changed

- Add `otel.tool_result.max_bytes`, defaulting to 2048 bytes, to control the
  text included in `codex.tool_result` logs independently of model-visible
  output limits.
- Apply truncation centrally when emitting telemetry, preserve UTF-8
  boundaries, and report whether the logged output was truncated.
- Record typed tool names and namespaces, agent names, and process-local result
  sequence numbers while keeping tool arguments and output out of trace events.

## Testing

- Cover configuration propagation, UTF-8-safe truncation, raw tool-output
  logging, and log/trace event routing and fields.

GitOrigin-RevId: 89ca76e55f1a09b729e7770ce01f2e4d98f9a22d
This commit is contained in:
tongzhou wang authored and copyberry committed 2026-08-20 19:09:04 +00:00
1 parent 5663754f62
commit 854cbb2fd4
39 files changed
+583 -310

No files matched your search

+16 -42
View File
@@ -215,8 +215,8 @@ struct PostToolUseFeedbackOutput {
}
impl ToolOutput for PostToolUseFeedbackOutput {
fn log_preview(&self) -> String {
self.original.log_preview()
fn log_output(&self) -> String {
self.original.log_output()
}
fn success_for_logging(&self) -> bool {
@@ -482,7 +482,6 @@ impl ToolRegistry {
terminal_outcome_reached: Option<Arc<AtomicBool>>,
) -> Result<AnyToolResult, FunctionCallError> {
let tool_name = invocation.tool_name.clone();
let tool_name_flat = flat_tool_name(&tool_name);
let call_id_owned = invocation.call_id.clone();
let otel = invocation.turn.session_telemetry.clone();
let permission_profile = invocation.turn.permission_profile();
@@ -520,7 +519,7 @@ impl ToolRegistry {
let message = unsupported_tool_call_message(&invocation.payload, &tool_name);
let log_payload = tool_log_payload(&invocation.payload, &invocation.source);
otel.tool_result_with_tags(
tool_name_flat.as_ref(),
&tool_name,
&call_id_owned,
log_payload.as_ref(),
Duration::ZERO,
@@ -550,7 +549,7 @@ impl ToolRegistry {
let message = format!("tool {tool_name} invoked with incompatible payload");
let log_payload = tool_log_payload(&invocation.payload, &invocation.source);
otel.tool_result_with_tags(
tool_name_flat.as_ref(),
&tool_name,
&call_id_owned,
log_payload.as_ref(),
Duration::ZERO,
@@ -639,37 +638,26 @@ impl ToolRegistry {
tool_result_tags.push(("command_category", category));
}
let response_cell = tokio::sync::Mutex::new(None);
let invocation_for_tool = invocation.clone();
let log_payload = tool_log_payload(&invocation.payload, &invocation.source);
let result = otel
.log_tool_result_with_tags(
tool_name_flat.as_ref(),
&tool_name,
&call_id_owned,
log_payload.as_ref(),
&tool_result_tags,
&extra_trace_fields,
|| {
let tool = tool.clone();
let response_cell = &response_cell;
async move {
match handle_any_tool(tool.as_ref(), invocation_for_tool).await {
Ok(result) => {
let preview = result.result.log_preview();
let success = result.result.success_for_logging();
let mut guard = response_cell.lock().await;
*guard = Some(result);
Ok((preview, success))
}
Err(err) => Err(err),
}
}
|| handle_any_tool(tool.as_ref(), invocation.clone()),
|result| {
(
result.result.log_output(),
result.result.success_for_logging(),
)
},
)
.await;
let success = match &result {
Ok((_, success)) => *success,
Ok(result) => result.result.success_for_logging(),
Err(_) => false,
};
if let Some(analytics) = control_tool_analytics.as_mut() {
@@ -681,9 +669,9 @@ impl ToolRegistry {
}
emit_metric_for_tool_read(&invocation, success);
let post_tool_use_payload = if success {
let guard = response_cell.lock().await;
guard
result
.as_ref()
.ok()
.and_then(|result| result.post_tool_use_payload.clone())
} else {
None
@@ -715,17 +703,7 @@ impl ToolRegistry {
// A PostToolUse block rejects the result, not the already-completed tool execution.
let lifecycle_outcome = match &result {
Ok(_) => {
let guard = response_cell.lock().await;
match guard.as_ref() {
Some(result) => ToolCallOutcome::Completed {
success: result.result.success_for_logging(),
},
None => ToolCallOutcome::Failed {
handler_executed: true,
},
}
}
Ok(_) => ToolCallOutcome::Completed { success },
Err(_) => ToolCallOutcome::Failed {
handler_executed: true,
},
@@ -738,11 +716,7 @@ impl ToolRegistry {
.await;
match result {
Ok(_) => {
let mut guard = response_cell.lock().await;
let mut result = guard.take().ok_or_else(|| {
FunctionCallError::Fatal("tool produced no output".to_string())
})?;
Ok(mut result) => {
if let Some(outcome) = post_tool_use_outcome {
if outcome.should_block {
let message = outcome.feedback_message.unwrap_or_else(|| {