mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Deduplicate MCP text fallbacks in Code Mode output (#63540)
## Why MCP tools can return structured data alongside a JSON text fallback for clients that need it. Printing the whole result in Code Mode can show the same data twice. ## What changed Enable `features.code_mode_mcp_result_dedup` to let `text()` omit `content` when it prints an original, successful MCP result whose only content block is a plain text fallback that parses to the same JSON as `structuredContent`. The JavaScript result retains both fields. Carry the result format through gRPC so the host can distinguish MCP results from ordinary JSON. The feature is off by default. ## Testing Add coverage for both feature flag settings, for printing original MCP results and copies through gRPC, and for comparing equivalent JSON. GitOrigin-RevId: a8b0b42f5375d9744c235a0e663090dd5982e37e
This commit is contained in:
@@ -5,6 +5,7 @@ use std::time::Duration;
|
||||
|
||||
use codex_code_mode_protocol::CellId;
|
||||
use codex_code_mode_protocol::CodeModeSessionDelegate;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::grpc;
|
||||
use codex_protocol::protocol::W3cTraceContext;
|
||||
use futures::FutureExt;
|
||||
@@ -206,7 +207,7 @@ impl SessionInner {
|
||||
&self,
|
||||
invocation_id: String,
|
||||
cancellation: CancellationToken,
|
||||
result: Result<serde_json::Value, String>,
|
||||
result: Result<CodeModeToolResult, String>,
|
||||
) {
|
||||
let request = completion::request(&self.id, &invocation_id, result);
|
||||
tokio::select! {
|
||||
|
||||
@@ -7,6 +7,7 @@ use std::sync::atomic::AtomicBool;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_code_mode_protocol::CodeModeOutputTruncation;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::grpc;
|
||||
use http_body_util::Full;
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -42,7 +43,7 @@ async fn missing_invocation_completion_racing_cancellation_preserves_session() {
|
||||
.complete_tool_call(
|
||||
"invocation".to_string(),
|
||||
cancellation,
|
||||
Ok(serde_json::Value::Null),
|
||||
Ok(CodeModeToolResult::from(serde_json::Value::Null)),
|
||||
)
|
||||
.await;
|
||||
|
||||
@@ -104,7 +105,7 @@ async fn cancelled_completion_does_not_wait_for_delivery_capacity() {
|
||||
let completion = session.complete_tool_call(
|
||||
"queued".to_string(),
|
||||
cancellation.clone(),
|
||||
Ok(serde_json::Value::Null),
|
||||
Ok(CodeModeToolResult::from(serde_json::Value::Null)),
|
||||
);
|
||||
tokio::pin!(completion);
|
||||
assert!(futures::poll!(completion.as_mut()).is_pending());
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::grpc;
|
||||
use codex_code_mode_protocol::host::MAX_FRAME_BYTES;
|
||||
use prost::Message;
|
||||
@@ -5,7 +6,7 @@ use prost::Message;
|
||||
pub(super) fn request(
|
||||
session_id: &str,
|
||||
invocation_id: &str,
|
||||
result: Result<serde_json::Value, String>,
|
||||
result: Result<CodeModeToolResult, String>,
|
||||
) -> grpc::CompleteToolCallRequest {
|
||||
request_with_maximum(session_id, invocation_id, result, MAX_FRAME_BYTES)
|
||||
}
|
||||
@@ -13,20 +14,25 @@ pub(super) fn request(
|
||||
fn request_with_maximum(
|
||||
session_id: &str,
|
||||
invocation_id: &str,
|
||||
result: Result<serde_json::Value, String>,
|
||||
result: Result<CodeModeToolResult, String>,
|
||||
maximum_message_bytes: usize,
|
||||
) -> grpc::CompleteToolCallRequest {
|
||||
let outcome = match result {
|
||||
Ok(value) => match serde_json::to_vec(&value) {
|
||||
Ok(output_json) => {
|
||||
grpc::complete_tool_call_request::Outcome::Succeeded(grpc::ToolCallSucceeded {
|
||||
output_json,
|
||||
})
|
||||
let reply = result.and_then(|result| {
|
||||
let (value, format) = match result {
|
||||
CodeModeToolResult::Json(value) => (value, grpc::ToolResultFormat::Json),
|
||||
CodeModeToolResult::McpCallToolResult(value) => {
|
||||
(value, grpc::ToolResultFormat::McpCallToolResult)
|
||||
}
|
||||
Err(error) => grpc::complete_tool_call_request::Outcome::Failed(grpc::ToolCallFailed {
|
||||
message: format!("failed to encode code-mode tool result: {error}"),
|
||||
}),
|
||||
},
|
||||
};
|
||||
let output_json = serde_json::to_vec(&value)
|
||||
.map_err(|error| format!("failed to encode code-mode tool result: {error}"))?;
|
||||
Ok(grpc::ToolCallSucceeded {
|
||||
output_json,
|
||||
result_format: format as i32,
|
||||
})
|
||||
});
|
||||
let outcome = match reply {
|
||||
Ok(reply) => grpc::complete_tool_call_request::Outcome::Succeeded(reply),
|
||||
Err(message) => {
|
||||
grpc::complete_tool_call_request::Outcome::Failed(grpc::ToolCallFailed { message })
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::grpc;
|
||||
use pretty_assertions::assert_eq;
|
||||
use prost::Message;
|
||||
@@ -9,7 +10,12 @@ use super::request_with_maximum;
|
||||
fn completion_size_includes_the_protobuf_envelope() {
|
||||
let output = serde_json::Value::String("a".repeat(100));
|
||||
let raw_json_bytes = serde_json::to_vec(&output).expect("valid JSON").len();
|
||||
let completion = request_with_maximum("session", "invocation", Ok(output), raw_json_bytes);
|
||||
let completion = request_with_maximum(
|
||||
"session",
|
||||
"invocation",
|
||||
Ok(CodeModeToolResult::from(output)),
|
||||
raw_json_bytes,
|
||||
);
|
||||
|
||||
assert!(matches!(
|
||||
completion.outcome,
|
||||
@@ -33,7 +39,7 @@ fn delegate_errors_larger_than_64_kib_are_preserved() {
|
||||
|
||||
#[test]
|
||||
fn completion_at_exact_message_limit_is_accepted() {
|
||||
let value = serde_json::json!({ "ok": true });
|
||||
let value = CodeModeToolResult::McpCallToolResult(serde_json::json!({ "content": [] }));
|
||||
let expected = request("session", "invocation", Ok(value.clone()));
|
||||
let actual = request_with_maximum("session", "invocation", Ok(value), expected.encoded_len());
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use codex_code_mode_protocol::CellId;
|
||||
use codex_code_mode_protocol::CodeModeNestedToolCall;
|
||||
use codex_code_mode_protocol::CodeModeSessionDelegate;
|
||||
use codex_code_mode_protocol::CodeModeToolKind;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::FunctionCallOutputContentItem;
|
||||
use codex_code_mode_protocol::NotificationFuture;
|
||||
use codex_code_mode_protocol::RuntimeResponse;
|
||||
@@ -40,7 +41,7 @@ impl CodeModeSessionDelegate for RecordingDelegate {
|
||||
_cancellation_token: CancellationToken,
|
||||
) -> ToolInvocationFuture<'a> {
|
||||
self.calls.lock().expect("calls lock").push(invocation);
|
||||
Box::pin(async { Ok(json!({ "ok": true })) })
|
||||
Box::pin(async { Ok(CodeModeToolResult::from(json!({ "ok": true }))) })
|
||||
}
|
||||
|
||||
fn notify<'a>(
|
||||
@@ -128,7 +129,7 @@ async fn reconnect_maps_every_delegate_callback_to_its_generation() {
|
||||
delegate
|
||||
.invoke_tool(invocation.clone(), CancellationToken::new())
|
||||
.await,
|
||||
Ok(json!({ "ok": true }))
|
||||
Ok(CodeModeToolResult::from(json!({ "ok": true })))
|
||||
);
|
||||
assert_eq!(
|
||||
delegate
|
||||
|
||||
Reference in New Issue
Block a user