Keep effective tool exposure in the registry (#36367)

## Why

`ToolExecutor::exposure()` describes a tool's preferred exposure, while the host
may apply step-specific policy when building a tool plan.

## What changed

- Store each runtime together with its effective exposure in `ToolRegistry`.
- Apply MCP, namespace, and collaboration-tool exposure overrides directly to
  registry entries instead of wrapping runtimes.
- Use the registry-owned exposure when building model-visible specs, deferred
  tool search, code-mode executors, and parallel-call support.

## Testing

- Cover that a hidden MCP tool remains routable but is not eligible for parallel
  tool calls.

GitOrigin-RevId: 1bc2760add8d993f477954269fa13681bfcc2444
This commit is contained in:
jif authored and copyberry committed 2026-07-31 17:24:33 +00:00
1 parent 287e1020ae
commit 775fb21d2a
11 files changed
+235 -269

No files matched your search

+38 -98
View File
@@ -35,7 +35,6 @@ use codex_protocol::protocol::EventMsg;
use codex_rollout::state_db;
use codex_shell_command::parse_command::parse_shell_script;
use codex_tools::ToolName;
use codex_tools::ToolSearchInfo;
use codex_tools::ToolSpec;
use futures::future::BoxFuture;
use indexmap::IndexMap;
@@ -243,94 +242,15 @@ pub(crate) struct PostToolUsePayload {
pub(crate) tool_response: Value,
}
pub(crate) fn override_tool_exposure(
handler: Arc<dyn CoreToolRuntime>,
exposure: ToolExposure,
) -> Arc<dyn CoreToolRuntime> {
if handler.exposure() == exposure {
return handler;
}
Arc::new(ExposureOverride { handler, exposure })
}
struct ExposureOverride {
handler: Arc<dyn CoreToolRuntime>,
exposure: ToolExposure,
}
impl ToolExecutor<ToolInvocation> for ExposureOverride {
fn tool_name(&self) -> ToolName {
self.handler.tool_name()
}
fn spec(&self) -> ToolSpec {
self.handler.spec()
}
fn exposure(&self) -> ToolExposure {
self.exposure
}
fn supports_parallel_tool_calls(&self) -> bool {
self.exposure != ToolExposure::Hidden && self.handler.supports_parallel_tool_calls()
}
fn search_info(&self) -> Option<ToolSearchInfo> {
self.handler.search_info()
}
fn handle(&self, invocation: ToolInvocation) -> codex_tools::ToolExecutorFuture<'_> {
self.handler.handle(invocation)
}
}
impl CoreToolRuntime for ExposureOverride {
fn wait_until_ready<'a>(&'a self, session: &'a Arc<Session>) -> Option<BoxFuture<'a, ()>> {
self.handler.wait_until_ready(session)
}
fn matches_kind(&self, payload: &ToolPayload) -> bool {
self.handler.matches_kind(payload)
}
fn waits_for_runtime_cancellation(&self) -> bool {
self.handler.waits_for_runtime_cancellation()
}
fn pre_tool_use_payload(&self, invocation: &ToolInvocation) -> Option<PreToolUsePayload> {
self.handler.pre_tool_use_payload(invocation)
}
fn post_tool_use_payload(
&self,
invocation: &ToolInvocation,
result: &dyn ToolOutput,
) -> Option<PostToolUsePayload> {
self.handler.post_tool_use_payload(invocation, result)
}
fn with_updated_hook_input(
&self,
invocation: ToolInvocation,
updated_input: Value,
) -> Result<ToolInvocation, FunctionCallError> {
self.handler
.with_updated_hook_input(invocation, updated_input)
}
fn telemetry_tags(&self, invocation: &ToolInvocation) -> ToolTelemetryTags {
self.handler.telemetry_tags(invocation)
}
fn create_diff_consumer(&self) -> Option<Box<dyn ToolArgumentDiffConsumer>> {
self.handler.create_diff_consumer()
}
/// A tool runtime together with its effective exposure for the current step.
pub(crate) struct RegisteredTool {
pub(crate) runtime: Arc<dyn CoreToolRuntime>,
pub(crate) exposure: ToolExposure,
}
#[derive(Default)]
pub struct ToolRegistry {
tools: IndexMap<ToolName, Arc<dyn CoreToolRuntime>>,
tools: IndexMap<ToolName, RegisteredTool>,
}
impl ToolRegistry {
@@ -358,13 +278,22 @@ impl ToolRegistry {
where
T: CoreToolRuntime + 'static,
{
self.register_trusted(override_tool_exposure(Arc::new(handler), exposure));
self.register_trusted_with_exposure(Arc::new(handler), exposure);
}
pub(crate) fn register_trusted(&mut self, runtime: Arc<dyn CoreToolRuntime>) {
let exposure = runtime.exposure();
self.register_trusted_with_exposure(runtime, exposure);
}
pub(crate) fn register_trusted_with_exposure(
&mut self,
runtime: Arc<dyn CoreToolRuntime>,
exposure: ToolExposure,
) {
match self.tools.entry(runtime.tool_name()) {
Entry::Vacant(entry) => {
entry.insert(runtime);
entry.insert(RegisteredTool { runtime, exposure });
}
Entry::Occupied(entry) => {
let tool_name = entry.key();
@@ -380,10 +309,21 @@ impl ToolRegistry {
return;
}
self.tools.shift_insert(0, tool_name, runtime);
let exposure = runtime.exposure();
self.tools
.shift_insert(0, tool_name, RegisteredTool { runtime, exposure });
}
pub(crate) fn register_external(&mut self, runtime: Arc<dyn CoreToolRuntime>) -> bool {
let exposure = runtime.exposure();
self.register_external_with_exposure(runtime, exposure)
}
pub(crate) fn register_external_with_exposure(
&mut self,
runtime: Arc<dyn CoreToolRuntime>,
exposure: ToolExposure,
) -> bool {
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");
@@ -392,7 +332,7 @@ impl ToolRegistry {
match self.tools.entry(tool_name) {
Entry::Vacant(entry) => {
entry.insert(runtime);
entry.insert(RegisteredTool { runtime, exposure });
true
}
Entry::Occupied(entry) => {
@@ -406,21 +346,21 @@ impl ToolRegistry {
}
pub(crate) fn remove(&mut self, tool_name: &ToolName) -> Option<Arc<dyn CoreToolRuntime>> {
self.tools.shift_remove(tool_name)
self.tools.shift_remove(tool_name).map(|tool| tool.runtime)
}
pub(crate) fn runtimes(&self) -> impl Iterator<Item = &Arc<dyn CoreToolRuntime>> {
pub(crate) fn entries(&self) -> impl Iterator<Item = &RegisteredTool> {
self.tools.values()
}
pub(crate) fn runtimes_mut(&mut self) -> impl Iterator<Item = &mut Arc<dyn CoreToolRuntime>> {
pub(crate) fn entries_mut(&mut self) -> impl Iterator<Item = &mut RegisteredTool> {
self.tools.values_mut()
}
pub(crate) fn deferred_tool_namespaces(&self) -> BTreeMap<String, String> {
let mut namespaces = BTreeMap::<String, String>::new();
for (name, tool) in &self.tools {
if tool.exposure() != ToolExposure::Deferred {
if tool.exposure != ToolExposure::Deferred {
continue;
}
let Some(namespace) = &name.namespace else {
@@ -430,7 +370,7 @@ impl ToolRegistry {
if !existing_description.trim().is_empty() {
continue;
}
let description = match tool.spec() {
let description = match tool.runtime.spec() {
ToolSpec::Namespace(namespace) => namespace.description,
ToolSpec::Function(_)
| ToolSpec::Freeform(_)
@@ -458,7 +398,7 @@ impl ToolRegistry {
}
pub(crate) fn tool(&self, name: &ToolName) -> Option<Arc<dyn CoreToolRuntime>> {
self.tools.get(name).map(Arc::clone)
self.tools.get(name).map(|tool| Arc::clone(&tool.runtime))
}
#[cfg(test)]
@@ -470,7 +410,7 @@ impl ToolRegistry {
#[cfg(test)]
pub(crate) fn tool_exposure(&self, name: &ToolName) -> Option<ToolExposure> {
self.tools.get(name).map(|tool| tool.exposure())
self.tools.get(name).map(|tool| tool.exposure)
}
pub(crate) fn create_diff_consumer(
@@ -481,8 +421,8 @@ impl ToolRegistry {
}
pub(crate) fn supports_parallel_tool_calls(&self, name: &ToolName) -> Option<bool> {
let tool = self.tool(name)?;
Some(tool.supports_parallel_tool_calls())
let tool = self.tools.get(name)?;
Some(tool.exposure != ToolExposure::Hidden && tool.runtime.supports_parallel_tool_calls())
}
pub(crate) fn waits_for_runtime_cancellation(&self, name: &ToolName) -> Option<bool> {