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

+29 -8
View File
@@ -183,10 +183,8 @@ fn handler_looks_up_namespaced_aliases_explicitly() {
let namespaced_handler = Arc::new(TestHandler {
tool_name: namespaced_name.clone(),
}) as Arc<dyn CoreToolRuntime>;
let registry = ToolRegistry::new(HashMap::from([
(plain_name.clone(), Arc::clone(&plain_handler)),
(namespaced_name.clone(), Arc::clone(&namespaced_handler)),
]));
let registry =
ToolRegistry::from_tools([Arc::clone(&plain_handler), Arc::clone(&namespaced_handler)]);
let plain = registry.tool(&plain_name);
let namespaced = registry.tool(&namespaced_name);
@@ -210,6 +208,32 @@ fn handler_looks_up_namespaced_aliases_explicitly() {
);
}
#[test]
fn registry_preserves_external_winners_and_trusted_synthetic_order() {
let handler = |tool_name| Arc::new(TestHandler { tool_name }) as Arc<dyn CoreToolRuntime>;
let [first_name, second_name, synthetic_name] =
["first", "second", "synthetic"].map(codex_tools::ToolName::plain);
let first_handler = handler(first_name.clone());
let mut registry = ToolRegistry::from_tools([Arc::clone(&first_handler)]);
assert!(!registry.register_external(handler(first_name.clone())));
assert!(registry.register_external(handler(second_name.clone())));
registry.prepend_trusted(handler(synthetic_name.clone()));
assert_eq!(
registry
.runtimes()
.map(|runtime| runtime.tool_name())
.collect::<Vec<_>>(),
vec![synthetic_name, first_name.clone(), second_name],
);
assert!(
registry
.remove(&first_name)
.is_some_and(|handler| Arc::ptr_eq(&handler, &first_handler))
);
}
#[tokio::test]
async fn readiness_selects_exact_tool_and_forwards_exposure_overrides() {
let (session, _turn) = crate::session::tests::make_session_and_context().await;
@@ -496,10 +520,7 @@ async fn dispatch_notifies_tool_lifecycle_contributors() -> anyhow::Result<()> {
tool_name: failing_tool.clone(),
result: LifecycleTestResult::Err,
}) as Arc<dyn CoreToolRuntime>;
let registry = ToolRegistry::new(HashMap::from([
(ok_tool.clone(), ok_handler),
(failing_tool.clone(), failing_handler),
]));
let registry = ToolRegistry::from_tools([ok_handler, failing_handler]);
let session = Arc::new(session);
let turn = Arc::new(turn);