fix(usage): attribute merged and code-mode tool specs

This commit is contained in:
Felipe Coury
2026-05-28 21:43:45 -03:00
parent 94b3e3e5eb
commit 8f460a1632
3 changed files with 88 additions and 10 deletions
@@ -5,6 +5,7 @@ use crate::tools::context::ToolPayload;
use crate::tools::context::boxed_tool_output;
use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::ToolExecutor;
use codex_protocol::protocol::UsageContributor;
use codex_tools::ToolName;
use codex_tools::ToolSpec;
@@ -16,13 +17,19 @@ use super::is_exec_tool_name;
pub struct CodeModeExecuteHandler {
spec: ToolSpec,
nested_tool_specs: Vec<ToolSpec>,
usage_contributors: Vec<UsageContributor>,
}
impl CodeModeExecuteHandler {
pub(crate) fn new(spec: ToolSpec, nested_tool_specs: Vec<ToolSpec>) -> Self {
pub(crate) fn new(
spec: ToolSpec,
nested_tool_specs: Vec<ToolSpec>,
usage_contributors: Vec<UsageContributor>,
) -> Self {
Self {
spec,
nested_tool_specs,
usage_contributors,
}
}
@@ -117,6 +124,10 @@ impl ToolExecutor<ToolInvocation> for CodeModeExecuteHandler {
}
impl CoreToolRuntime for CodeModeExecuteHandler {
fn usage_contributors(&self) -> Vec<UsageContributor> {
self.usage_contributors.clone()
}
fn matches_kind(&self, payload: &ToolPayload) -> bool {
matches!(payload, ToolPayload::Custom { .. })
}
+55 -9
View File
@@ -62,6 +62,7 @@ use codex_protocol::openai_models::ConfigShellToolType;
use codex_protocol::openai_models::InputModality;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_protocol::protocol::UsageContributor;
use codex_tools::DiscoverableTool;
use codex_tools::ResponsesApiNamespace;
use codex_tools::ResponsesApiNamespaceTool;
@@ -206,7 +207,6 @@ fn build_model_visible_specs_and_registry(
hosted_specs,
} = planned_tools;
let mut specs = Vec::new();
let mut usage_contributors = Vec::new();
let mut usage_contributors_by_tool_name = HashMap::new();
let mut seen_tool_names = HashSet::new();
for runtime in &runtimes {
@@ -226,13 +226,6 @@ fn build_model_visible_specs_and_registry(
if !namespace_tools_enabled(turn_context) && matches!(spec, ToolSpec::Namespace(_)) {
continue;
}
let estimated_tokens = crate::usage::estimate_serialized_tokens(&spec);
usage_contributors.extend(runtime_usage_contributors.into_iter().map(|contributor| {
crate::usage::UsagePromptContributor {
contributor,
source_estimated_tokens: estimated_tokens,
}
}));
specs.push(spec);
}
}
@@ -247,12 +240,14 @@ fn build_model_visible_specs_and_registry(
}
let registry = ToolRegistry::from_tools(runtimes);
let model_visible_specs = merge_into_namespaces(specs)
let model_visible_specs: Vec<ToolSpec> = merge_into_namespaces(specs)
.into_iter()
.filter(|spec| {
namespace_tools_enabled(turn_context) || !matches!(spec, ToolSpec::Namespace(_))
})
.collect();
let usage_contributors =
prompt_usage_contributors(&model_visible_specs, &usage_contributors_by_tool_name);
(
model_visible_specs,
@@ -262,6 +257,50 @@ fn build_model_visible_specs_and_registry(
)
}
fn prompt_usage_contributors(
specs: &[ToolSpec],
contributors_by_tool_name: &HashMap<ToolName, Vec<UsageContributor>>,
) -> Vec<crate::usage::UsagePromptContributor> {
let mut prompt_contributors = Vec::new();
for spec in specs {
let mut spec_contributors = Vec::new();
match spec {
ToolSpec::Namespace(namespace) => {
for tool in &namespace.tools {
let ResponsesApiNamespaceTool::Function(tool) = tool;
let tool_name = ToolName::namespaced(&namespace.name, &tool.name);
if let Some(contributors) = contributors_by_tool_name.get(&tool_name) {
for contributor in contributors {
if !spec_contributors.contains(contributor) {
spec_contributors.push(contributor.clone());
}
}
}
}
}
ToolSpec::Function(_)
| ToolSpec::ToolSearch { .. }
| ToolSpec::ImageGeneration { .. }
| ToolSpec::WebSearch { .. }
| ToolSpec::Freeform(_) => {
if let Some(contributors) =
contributors_by_tool_name.get(&ToolName::plain(spec.name()))
{
spec_contributors.extend(contributors.iter().cloned());
}
}
}
let source_estimated_tokens = crate::usage::estimate_serialized_tokens(spec);
prompt_contributors.extend(spec_contributors.into_iter().map(|contributor| {
crate::usage::UsagePromptContributor {
contributor,
source_estimated_tokens,
}
}));
}
prompt_contributors
}
fn spec_for_model_request(
turn_context: &TurnContext,
exposure: ToolExposure,
@@ -431,6 +470,7 @@ fn build_code_mode_executors(
let mut code_mode_nested_tool_specs = Vec::new();
let mut exec_prompt_tool_specs = Vec::new();
let mut exec_prompt_usage_contributors = Vec::new();
for executor in executors {
let exposure = executor.exposure();
if exposure == ToolExposure::DirectModelOnly {
@@ -444,6 +484,11 @@ fn build_code_mode_executors(
if exposure != ToolExposure::Deferred {
exec_prompt_tool_specs.push(spec.clone());
for contributor in executor.usage_contributors() {
if !exec_prompt_usage_contributors.contains(&contributor) {
exec_prompt_usage_contributors.push(contributor);
}
}
}
code_mode_nested_tool_specs.push(spec);
}
@@ -463,6 +508,7 @@ fn build_code_mode_executors(
deferred_tools_available,
),
code_mode_nested_tool_specs,
exec_prompt_usage_contributors,
)),
Arc::new(CodeModeWaitHandler),
]
@@ -525,6 +525,22 @@ async fn mcp_and_tool_search_follow_direct_and_deferred_tool_exposure() {
vec!["direct".to_string()]
);
let merged_mcp = probe_with(
|_| {},
ToolPlanInputs {
mcp_tools: Some(vec![
mcp_tool("merged", "mcp__merged", "lookup"),
mcp_tool("merged", "mcp__merged", "search"),
]),
..ToolPlanInputs::default()
},
)
.await;
assert_eq!(
merged_mcp.prompt_usage_contributor_labels,
vec!["merged".to_string()]
);
let searchable_mcp = ToolPlanInputs {
deferred_mcp_tools: Some(vec![mcp_tool("searchable", "mcp__searchable", "lookup")]),
..ToolPlanInputs::default()
@@ -758,6 +774,7 @@ async fn code_mode_only_exposes_code_executor_and_hides_nested_tools() {
set_features(turn, &[Feature::CodeMode, Feature::CodeModeOnly]);
},
ToolPlanInputs {
mcp_tools: Some(vec![mcp_tool("nested", "mcp__nested", "lookup")]),
dynamic_tools: vec![dynamic_tool(
Some("codex_app"),
"lookup",
@@ -775,6 +792,10 @@ async fn code_mode_only_exposes_code_executor_and_hides_nested_tools() {
code_mode_only.namespace_function_names("codex_app"),
Vec::<String>::new().as_slice()
);
assert_eq!(
code_mode_only.prompt_usage_contributor_labels,
vec!["nested".to_string()]
);
}
#[tokio::test]