codex: address PR review feedback (#30618)

This commit is contained in:
Anton Panasenko
2026-06-29 21:39:42 -07:00
parent 4f20cfe962
commit 4afbe1077f
4 changed files with 95 additions and 17 deletions
+4 -2
View File
@@ -10487,7 +10487,9 @@ async fn fatal_tool_error_stops_turn_and_reports_error() {
internal_chat_message_metadata_passthrough: None,
};
let call = ToolRouter::build_tool_call(item.clone()).expect("tool call present");
let call = ToolRouter::build_tool_call(item.clone())
.expect("build tool call")
.expect("tool call present");
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
let err = router
.dispatch_tool_call_with_code_mode_result(
@@ -10824,7 +10826,7 @@ while :; do sleep 1; done"#,
call_id: "shell-cleanup-call".to_string(),
internal_chat_message_metadata_passthrough: None,
};
let call = ToolRouter::build_tool_call(item)
let call = ToolRouter::build_tool_call(item)?
.expect("shell command response item should build a tool call");
let cancellation_token = CancellationToken::new();
let cancellation_tx = cancellation_token.clone();
+78 -2
View File
@@ -12,6 +12,7 @@ use tokio_util::sync::CancellationToken;
use crate::context::ContextualUserFragment;
use crate::context::ImageGenerationInstructions;
use crate::function_tool::FunctionCallError;
use crate::parse_turn_item;
use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
@@ -22,6 +23,8 @@ use codex_memories_read::citations::thread_ids_from_memory_citation;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result;
use codex_protocol::memory_citation::MemoryCitation;
use codex_protocol::models::FunctionCallOutputBody;
use codex_protocol::models::FunctionCallOutputPayload;
use codex_protocol::models::MessagePhase;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::models::ResponseItem;
@@ -409,7 +412,7 @@ pub(crate) async fn handle_output_item_done(
match ToolRouter::build_tool_call(item.clone()) {
// The model emitted a tool call; log it, persist the item immediately, and queue the tool execution.
Some(call) => {
Ok(Some(call)) => {
ctx.sess
.input_queue
.accept_mailbox_delivery_for_current_turn(
@@ -440,7 +443,7 @@ pub(crate) async fn handle_output_item_done(
output.tool_future = Some(tool_future);
}
// No tool call: convert messages/reasoning into turn items and mark them as complete.
None => {
Ok(None) => {
let finalized_turn_item = finalize_non_tool_response_item(
ctx.sess.as_ref(),
ctx.turn_context.as_ref(),
@@ -480,6 +483,32 @@ pub(crate) async fn handle_output_item_done(
output.last_agent_message = finalized_facts.and_then(|facts| facts.last_agent_message);
}
// The tool request should be answered directly (or was denied); push that response into the transcript.
Err(FunctionCallError::RespondToModel(message)) => {
let response = ResponseInputItem::FunctionCallOutput {
call_id: String::new(),
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::Text(message),
..Default::default()
},
};
record_completed_response_item(ctx.sess.as_ref(), ctx.turn_context.as_ref(), &item)
.await;
if let Some(response_item) = response_input_to_response_item(&response) {
ctx.sess
.record_conversation_items(
&ctx.turn_context,
std::slice::from_ref(&response_item),
)
.await;
}
output.needs_follow_up = true;
}
// A fatal error occurred; surface it back into history.
Err(FunctionCallError::Fatal(message)) => {
return Err(CodexErr::Fatal(message));
}
}
Ok(output)
@@ -591,6 +620,53 @@ fn completed_item_defers_mailbox_delivery_to_next_turn(
}
}
pub(crate) fn response_input_to_response_item(input: &ResponseInputItem) -> Option<ResponseItem> {
match input {
ResponseInputItem::FunctionCallOutput { call_id, output } => {
Some(ResponseItem::FunctionCallOutput {
id: None,
call_id: call_id.clone(),
output: output.clone(),
internal_chat_message_metadata_passthrough: None,
})
}
ResponseInputItem::CustomToolCallOutput {
call_id,
name,
output,
} => Some(ResponseItem::CustomToolCallOutput {
id: None,
call_id: call_id.clone(),
name: name.clone(),
output: output.clone(),
internal_chat_message_metadata_passthrough: None,
}),
ResponseInputItem::McpToolCallOutput { call_id, output } => {
let output = output.as_function_call_output_payload();
Some(ResponseItem::FunctionCallOutput {
id: None,
call_id: call_id.clone(),
output,
internal_chat_message_metadata_passthrough: None,
})
}
ResponseInputItem::ToolSearchOutput {
call_id,
status,
execution,
tools,
} => Some(ResponseItem::ToolSearchOutput {
id: None,
call_id: Some(call_id.clone()),
status: status.clone(),
execution: execution.clone(),
tools: tools.clone(),
internal_chat_message_metadata_passthrough: None,
}),
_ => None,
}
}
#[cfg(test)]
#[path = "stream_events_utils_tests.rs"]
mod tests;
+10 -10
View File
@@ -109,8 +109,8 @@ impl ToolRouter {
.unwrap_or(false)
}
#[instrument(level = "trace", skip_all)]
pub fn build_tool_call(item: ResponseItem) -> Option<ToolCall> {
#[instrument(level = "trace", skip_all, err)]
pub fn build_tool_call(item: ResponseItem) -> Result<Option<ToolCall>, FunctionCallError> {
match item {
ResponseItem::FunctionCall {
name,
@@ -120,11 +120,11 @@ impl ToolRouter {
..
} => {
let tool_name = ToolName::new(namespace, name);
Some(ToolCall {
Ok(Some(ToolCall {
tool_name,
call_id,
payload: ToolPayload::Function { arguments },
})
}))
}
ResponseItem::ToolSearchCall {
call_id: Some(call_id),
@@ -138,25 +138,25 @@ impl ToolRouter {
query: String::new(),
limit: None,
});
Some(ToolCall {
Ok(Some(ToolCall {
tool_name: ToolName::plain("tool_search"),
call_id,
payload: ToolPayload::ToolSearch { arguments },
})
}))
}
ResponseItem::ToolSearchCall { .. } => None,
ResponseItem::ToolSearchCall { .. } => Ok(None),
ResponseItem::CustomToolCall {
name,
namespace,
input,
call_id,
..
} => Some(ToolCall {
} => Ok(Some(ToolCall {
tool_name: ToolName::new(namespace, name),
call_id,
payload: ToolPayload::Custom { input },
}),
_ => None,
})),
_ => Ok(None),
}
}
+3 -3
View File
@@ -161,7 +161,7 @@ async fn build_tool_call_uses_namespace_for_registry_name() -> anyhow::Result<()
arguments: "{}".to_string(),
call_id: "call-namespace".to_string(),
internal_chat_message_metadata_passthrough: None,
})
})?
.expect("function_call should produce a tool call");
assert_eq!(
@@ -191,7 +191,7 @@ async fn build_custom_tool_call_uses_namespace_for_registry_name() -> anyhow::Re
namespace: Some("mcp__python".to_string()),
input: "print('hello')".to_string(),
internal_chat_message_metadata_passthrough: None,
})
})?
.expect("custom_tool_call should produce a tool call");
assert_eq!(
@@ -419,7 +419,7 @@ async fn extension_tool_executors_are_model_visible_and_dispatchable() -> anyhow
arguments: json!({ "message": "hello" }).to_string(),
call_id: "call-extension".to_string(),
internal_chat_message_metadata_passthrough: None,
})
})?
.expect("function_call should produce a tool call");
let result = router
.dispatch_tool_call_with_code_mode_result(