Enforce strict tool name collision errors (#37020)

## What changed

- Track the first duplicate effective tool name while assembling the tool registry, including collisions introduced by external tools, code mode, and tool search.
- When `[features.tool_registry].error_on_tool_collisions` is enabled, fail the turn with a `duplicate tool: <namespace>.<name>` error before sending a model request.
- Continue allowing identical tool names in different namespaces, and preserve the existing collision behavior when strict checking is disabled.

## Testing

- Cover registry collision tracking, strict tool planning across tool sources, namespace isolation, failure before sampling, and pre-sampling compaction.

GitOrigin-RevId: 4cf83538fb513cec0b9c8b567780caaaadd3243d
This commit is contained in:
rka-oai authored and copyberry committed 2026-08-05 04:00:50 +00:00
1 parent 5d89ab65dc
commit 1e489adad0
13 files changed
+374 -17

No files matched your search

+15 -3
View File
@@ -251,14 +251,13 @@ pub(crate) struct RegisteredTool {
#[derive(Default)]
pub struct ToolRegistry {
tools: IndexMap<ToolName, RegisteredTool>,
first_collision: Option<ToolName>,
}
impl ToolRegistry {
#[cfg(test)]
pub(crate) fn from_tools(tools: impl IntoIterator<Item = Arc<dyn CoreToolRuntime>>) -> Self {
let mut registry = Self {
tools: IndexMap::new(),
};
let mut registry = Self::default();
for runtime in tools {
registry.register_trusted(runtime);
@@ -327,6 +326,9 @@ impl ToolRegistry {
let tool_name = runtime.tool_name();
if tool_name.namespace.is_none() && tool_name.name == "shell_command" {
tracing::warn!(tool_name = %tool_name, "skipping external tool with reserved name");
if self.tools.contains_key(&tool_name) {
self.record_collision(tool_name);
}
return false;
}
@@ -340,11 +342,21 @@ impl ToolRegistry {
tool_name = %entry.key(),
"skipping duplicate external tool that is already registered"
);
self.first_collision
.get_or_insert_with(|| entry.key().clone());
false
}
}
}
pub(crate) fn record_collision(&mut self, tool_name: ToolName) {
self.first_collision.get_or_insert(tool_name);
}
pub(crate) fn first_collision(&self) -> Option<&ToolName> {
self.first_collision.as_ref()
}
pub(crate) fn remove(&mut self, tool_name: &ToolName) -> Option<Arc<dyn CoreToolRuntime>> {
self.tools.shift_remove(tool_name).map(|tool| tool.runtime)
}