Centralize tool registration and protect host tools (#36127)

## Why

External MCP, extension, and dynamic tools can collide with host-owned tool
names. In particular, a dynamic `tool_search` definition must not replace the
host implementation used to discover and route deferred tools.

## What changed

- Build each turn's tools through a single ordered `ToolRegistry` that retains
  the first runtime registered for a name.
- Treat duplicate external tools as skippable collisions while preserving
  strict duplicate checks for trusted host tools.
- Reserve host-owned code-mode and `tool_search` runtimes before producing the
  model-visible tool list.
- Base hosted web-search fallback behavior on the extension runtime that
  actually won registration.

## Testing

- Cover ordered registry mutation and duplicate handling.
- Verify a client-provided `tool_search` cannot shadow the host tool.
- Verify hosted web-search fallback follows the registered browser runtime.

GitOrigin-RevId: acebe360de8ec69ad7da2f684cc2542cbf18a984
This commit is contained in:
jif authored and copyberry committed 2026-07-30 10:07:04 +00:00
1 parent 5decb399ae
commit 89a0eed93c
10 files changed
+443 -356

No files matched your search

+7 -3
View File
@@ -1,16 +1,19 @@
use crate::function_tool::FunctionCallError;
use crate::session::session::Session;
use crate::session::step_context::StepContext;
#[cfg(test)]
use crate::session::turn_context::TurnContext;
use crate::tools::context::SharedTurnDiffTracker;
use crate::tools::context::ToolInvocation;
use crate::tools::context::ToolPayload;
#[cfg(test)]
use crate::tools::handlers::ToolSearchHandlerCache;
use crate::tools::registry::AnyToolResult;
use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::ToolArgumentDiffConsumer;
use crate::tools::registry::ToolRegistry;
use crate::tools::spec_plan::build_tool_router;
#[cfg(test)]
use crate::tools::spec_plan::finalize_tool_router;
use codex_protocol::models::ResponseItem;
use codex_protocol::models::SearchToolCallParams;
use codex_tools::DiscoverableTool;
@@ -80,15 +83,16 @@ pub(crate) struct ToolSuggestCandidates {
}
impl ToolRouter {
#[cfg(test)]
pub(crate) fn from_tools(
turn_context: &TurnContext,
tool_runtimes: Vec<Arc<dyn CoreToolRuntime>>,
hosted_specs: Vec<ToolSpec>,
tool_search_handler_cache: &ToolSearchHandlerCache,
) -> Self {
build_tool_router(
finalize_tool_router(
turn_context,
tool_runtimes,
ToolRegistry::from_tools(tool_runtimes),
hosted_specs,
tool_search_handler_cache,
)