mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Share tool output schemas and defer MCP envelope construction (#45439)
## Why MCP tool parsing eagerly cloned structured output schemas and built full call-result envelopes. Cloning tool definitions also copied their output-schema JSON, even before a consumer needed it. ## What changed - Introduce `ToolOutputSchema` with immutable `Arc` storage so tool definitions share output schemas when cloned. - Retain MCP structured output schemas and materialize the call-result envelope only when JSON is requested. - Update code-mode consumers and schema mutation sites to materialize JSON explicitly, reusing uniquely owned storage when possible. - Move structured content into the MCP envelope without an extra clone, preserving property order. ## Testing Add tests for JSON preservation, mutation isolation, equality between lazy and materialized schemas, reuse of uniquely owned storage, and equivalent code-mode definitions. GitOrigin-RevId: e98ba4c2f0efedc99f7cbc7bba206cc63a3bd8f4
This commit is contained in:
1 parent
e9633d7a02
commit
f3803587c9
22 files changed
+291
-89
No files matched your search
@@ -75,7 +75,7 @@ impl ToolExecutor<ToolInvocation> for CurrentTimeHandler {
|
||||
},
|
||||
"required": ["current_time"],
|
||||
"additionalProperties": false
|
||||
})),
|
||||
}).into()),
|
||||
})],
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub fn create_get_context_remaining_tool() -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(BTreeMap::new(), /*required*/ None, Some(false.into())),
|
||||
output_schema: Some(get_context_remaining_output_schema()),
|
||||
output_schema: Some(get_context_remaining_output_schema().into()),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ pub fn create_spawn_agent_tool_v1(options: SpawnAgentToolOptions) -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, /*required*/ None, Some(false.into())),
|
||||
output_schema: Some(spawn_agent_output_schema_v1()),
|
||||
output_schema: Some(spawn_agent_output_schema_v1().into()),
|
||||
})],
|
||||
})
|
||||
}
|
||||
@@ -134,9 +134,9 @@ pub fn create_spawn_agent_tool_v2(options: SpawnAgentToolOptions) -> ToolSpec {
|
||||
Some(vec!["task_name".to_string(), "message".to_string()]),
|
||||
Some(false.into()),
|
||||
),
|
||||
output_schema: Some(spawn_agent_output_schema_v2(
|
||||
options.hide_agent_type_model_reasoning,
|
||||
)),
|
||||
output_schema: Some(
|
||||
spawn_agent_output_schema_v2(options.hide_agent_type_model_reasoning).into(),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ pub fn create_send_input_tool_v1() -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, Some(vec!["target".to_string()]), Some(false.into())),
|
||||
output_schema: Some(send_input_output_schema()),
|
||||
output_schema: Some(send_input_output_schema().into()),
|
||||
})],
|
||||
})
|
||||
}
|
||||
@@ -256,7 +256,7 @@ pub fn create_resume_agent_tool() -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, Some(vec!["id".to_string()]), Some(false.into())),
|
||||
output_schema: Some(resume_agent_output_schema()),
|
||||
output_schema: Some(resume_agent_output_schema().into()),
|
||||
})],
|
||||
})
|
||||
}
|
||||
@@ -272,7 +272,7 @@ pub fn create_wait_agent_tool_v1(options: WaitAgentTimeoutOptions) -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: wait_agent_tool_parameters_v1(options),
|
||||
output_schema: Some(wait_output_schema_v1()),
|
||||
output_schema: Some(wait_output_schema_v1().into()),
|
||||
})],
|
||||
})
|
||||
}
|
||||
@@ -285,7 +285,7 @@ pub fn create_wait_agent_tool_v2(options: WaitAgentTimeoutOptions) -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: wait_agent_tool_parameters_v2(options),
|
||||
output_schema: Some(wait_output_schema_v2()),
|
||||
output_schema: Some(wait_output_schema_v2().into()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -306,7 +306,7 @@ pub fn create_list_agents_tool() -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, /*required*/ None, Some(false.into())),
|
||||
output_schema: Some(list_agents_output_schema()),
|
||||
output_schema: Some(list_agents_output_schema().into()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -325,9 +325,12 @@ pub fn create_close_agent_tool_v1() -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, Some(vec!["target".to_string()]), Some(false.into())),
|
||||
output_schema: Some(agent_previous_status_output_schema(
|
||||
"The agent status observed before shutdown was requested.",
|
||||
)),
|
||||
output_schema: Some(
|
||||
agent_previous_status_output_schema(
|
||||
"The agent status observed before shutdown was requested.",
|
||||
)
|
||||
.into(),
|
||||
),
|
||||
})],
|
||||
})
|
||||
}
|
||||
@@ -346,9 +349,12 @@ pub fn create_interrupt_agent_tool_v2() -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, Some(vec!["target".to_string()]), Some(false.into())),
|
||||
output_schema: Some(agent_previous_status_output_schema(
|
||||
"The agent status observed before the interrupt request was handled.",
|
||||
)),
|
||||
output_schema: Some(
|
||||
agent_previous_status_output_schema(
|
||||
"The agent status observed before the interrupt request was handled.",
|
||||
)
|
||||
.into(),
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ fn spawn_agent_tool_v2_requires_task_name_and_lists_visible_models() {
|
||||
Some(&vec!["task_name".to_string(), "message".to_string()])
|
||||
);
|
||||
assert_eq!(
|
||||
output_schema.expect("spawn_agent output schema")["required"],
|
||||
output_schema.expect("spawn_agent output schema").to_value()["required"],
|
||||
json!(["task_name", "nickname"])
|
||||
);
|
||||
}
|
||||
@@ -416,7 +416,7 @@ fn wait_agent_tool_v2_uses_timeout_only_summary_output() {
|
||||
);
|
||||
assert_eq!(parameters.required.as_ref(), None);
|
||||
assert_eq!(
|
||||
output_schema.expect("wait output schema")["properties"]["message"]["description"],
|
||||
output_schema.expect("wait output schema").to_value()["properties"]["message"]["description"],
|
||||
json!(
|
||||
"Brief wait summary without the agent's final content, including any timeout adjustment."
|
||||
)
|
||||
@@ -449,7 +449,8 @@ fn list_agents_tool_includes_path_prefix_and_agent_fields() {
|
||||
Some("Task-path prefix filter without a trailing slash. Omit to list all live agents.")
|
||||
);
|
||||
assert_eq!(
|
||||
output_schema.expect("list_agents output schema")["properties"]["agents"]["items"]["required"],
|
||||
output_schema.expect("list_agents output schema").to_value()["properties"]["agents"]["items"]
|
||||
["required"],
|
||||
json!(["agent_name", "agent_status"])
|
||||
);
|
||||
}
|
||||
@@ -462,8 +463,8 @@ fn list_agents_tool_status_schema_includes_interrupted() {
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
output_schema.expect("list_agents output schema")["properties"]["agents"]["items"]["properties"]
|
||||
["agent_status"]["allOf"][0]["oneOf"][0]["enum"],
|
||||
output_schema.expect("list_agents output schema").to_value()["properties"]["agents"]["items"]
|
||||
["properties"]["agent_status"]["allOf"][0]["oneOf"][0]["enum"],
|
||||
json!([
|
||||
"pending_init",
|
||||
"running",
|
||||
|
||||
@@ -110,7 +110,7 @@ pub(crate) fn create_exec_command_tool_with_environment_id(
|
||||
Some(vec!["cmd".to_string()]),
|
||||
Some(false.into()),
|
||||
),
|
||||
output_schema: Some(unified_exec_output_schema()),
|
||||
output_schema: Some(unified_exec_output_schema().into()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ pub fn create_write_stdin_tool() -> ToolSpec {
|
||||
Some(vec!["session_id".to_string()]),
|
||||
Some(false.into()),
|
||||
),
|
||||
output_schema: Some(unified_exec_output_schema()),
|
||||
output_schema: Some(unified_exec_output_schema().into()),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ fn exec_command_tool_matches_expected_spec() {
|
||||
Some(vec!["cmd".to_string()]),
|
||||
Some(false.into())
|
||||
),
|
||||
output_schema: Some(unified_exec_output_schema()),
|
||||
output_schema: Some(unified_exec_output_schema().into()),
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -159,7 +159,7 @@ fn write_stdin_tool_matches_expected_spec() {
|
||||
Some(vec!["session_id".to_string()]),
|
||||
Some(false.into())
|
||||
),
|
||||
output_schema: Some(unified_exec_output_schema()),
|
||||
output_schema: Some(unified_exec_output_schema().into()),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -501,14 +501,16 @@ fn one_shot_exec_command_spec(spec: ToolSpec) -> ToolSpec {
|
||||
"Maximum command runtime. Defaults to 10000 ms.".to_string(),
|
||||
)),
|
||||
);
|
||||
if let Some(output_properties) = spec
|
||||
.output_schema
|
||||
.as_mut()
|
||||
.and_then(|schema| schema.get_mut("properties"))
|
||||
.and_then(serde_json::Value::as_object_mut)
|
||||
{
|
||||
output_properties.remove("session_id");
|
||||
}
|
||||
spec.output_schema = spec.output_schema.map(|schema| {
|
||||
let mut schema = schema.into_value();
|
||||
if let Some(output_properties) = schema
|
||||
.get_mut("properties")
|
||||
.and_then(serde_json::Value::as_object_mut)
|
||||
{
|
||||
output_properties.remove("session_id");
|
||||
}
|
||||
schema.into()
|
||||
});
|
||||
ToolSpec::Function(spec)
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ pub fn create_view_image_tool(options: ViewImageToolOptions) -> ToolSpec {
|
||||
strict: false,
|
||||
defer_loading: None,
|
||||
parameters: JsonSchema::object(properties, Some(vec!["path".to_string()]), Some(false.into())),
|
||||
output_schema: Some(view_image_output_schema(options)),
|
||||
output_schema: Some(view_image_output_schema(options).into()),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user