Resolve model-selected search providers with policy-aware fallback (#59036)

## Why

Search discovery must account for the active model's supported tools while honoring host search restrictions. Automatic fallback must also preserve the provider that owns existing browsing references.

## What changed

- Resolve search from model metadata, client configuration, and managed requirements, exposing the selected provider's tools.
- Discover hosted search with the model's supported tools and carry the discovered invocation context into calls.
- Allow native fallback for eligible discovery failures only when search policy permits it and hosted search has not already been used. Do not replay failed hosted invocations through native search.
- Keep native fallback references on native search across later turns, discovery recovery, and resume. Release unused fallback offers and coordinate background calls to prevent conflicting provider use.

## Testing

Add app-server and core scenario coverage for provider selection, discovery failures, search restrictions, reference continuity, resume, and concurrent background search. Include a snapshot covering discovery and call history across a model switch.

GitOrigin-RevId: 0f22605722daaa424c4fa34660654cde5a6db049
This commit is contained in:
yitaoh-oai
2026-11-03 02:08:56 +00:00
committed by copyberry
parent 3c074bb44a
commit e839b828fb
15 changed files with 1749 additions and 11 deletions
@@ -66,6 +66,17 @@ impl ToolExecutor<ToolInvocation> for ExtensionToolAdapter {
{
Box::pin(async move {
let step = &invocation.step_context;
let _native_search = if invocation.tool_name.namespace.as_deref() == Some("web")
&& invocation.tool_name.name == "run"
{
crate::model_search::SearchOwner::reserve_native_fallback(
&step.extension_data,
&invocation.session.services.thread_extension_data,
)
.map_err(|err| codex_tools::FunctionCallError::RespondToModel(err.to_string()))?
} else {
None
};
let selected: Vec<_> = match &invocation.environments {
ToolEnvironments::Arguments(environments) => {
environments.iter().map(|(_, env)| env.as_ref()).collect()
+37 -6
View File
@@ -159,6 +159,7 @@ pub(crate) fn build_tool_router(
session.services.mcp_manager.shared_definitions(),
&mut registry,
);
let search = step_store.get::<crate::model_search::ModelSearch>();
apply_mcp_tool_exposure_policy(
turn_context,
model_info,
@@ -172,12 +173,40 @@ pub(crate) fn build_tool_router(
extension_tool_executors(session, step_store),
&mut registry,
);
let native_mode = match search.as_deref() {
None => Some(turn_context.config.web_search_mode.value()),
Some(crate::model_search::ModelSearch::Native { mode, .. }) => Some(*mode),
_ => None,
};
if matches!(
search.as_deref(),
Some(
crate::model_search::ModelSearch::Native { .. }
| crate::model_search::ModelSearch::Disabled
)
) {
for tool in mcp
.tools()
.iter()
.filter(|tool| codex_mcp::is_model_search_tool(tool))
{
registry.remove(&tool.canonical_tool_name());
}
}
if native_mode.is_none() {
registry.remove(&ToolName::namespaced("web", "run"));
}
append_dynamic_tool_runtimes(dynamic_tools, &mut registry);
let hosted_specs = hosted_model_tool_specs(
turn_context,
model_info,
standalone_web_search_tool.as_slice(),
);
let hosted_specs = if let Some(mode) = native_mode {
hosted_model_tool_specs(
turn_context,
model_info,
mode,
standalone_web_search_tool.as_slice(),
)
} else {
Vec::new()
};
finalize_tool_router(
turn_context,
@@ -324,6 +353,7 @@ pub(crate) fn append_source_tools(
hosted_model_tool_specs(
turn_context,
model_info,
turn_context.config.web_search_mode.value(),
standalone_web_search_tool.as_slice(),
)
}
@@ -647,6 +677,7 @@ fn spec_for_model_request(
fn hosted_model_tool_specs(
turn_context: &TurnContext,
model_info: &ModelInfo,
mode: WebSearchMode,
registered_extension_tool_names: &[ToolName],
) -> Vec<ToolSpec> {
// Responses Lite accepts schemas for client-executed tools, not hosted Responses tools.
@@ -661,7 +692,7 @@ fn hosted_model_tool_specs(
// and the provider supports hosted search. `None` prevents emitting a hosted search tool.
let web_search_mode = (!standalone_web_search_available
&& turn_context.provider.capabilities().web_search)
.then_some(turn_context.config.web_search_mode.value());
.then_some(mode);
let web_search_config = web_search_mode
.as_ref()
.and(turn_context.config.web_search_config.as_ref());
+9 -1
View File
@@ -1142,7 +1142,12 @@ async fn environment_tools_follow_the_step_context() {
/*tool_suggest_candidates*/ None,
/*wait_for_environment_tool_config*/ None,
),
super::hosted_model_tool_specs(turn.as_ref(), turn.model_info(), &[]),
super::hosted_model_tool_specs(
turn.as_ref(),
turn.model_info(),
turn.config.web_search_mode.value(),
&[],
),
&Default::default(),
));
@@ -2273,6 +2278,7 @@ async fn tool_search_cache_rebuilds_when_deferred_sources_change() {
super::hosted_model_tool_specs(
first_step_context.turn.as_ref(),
first_step_context.turn.model_info(),
first_step_context.turn.config.web_search_mode.value(),
&[],
),
&cache,
@@ -2307,6 +2313,7 @@ async fn tool_search_cache_rebuilds_when_deferred_sources_change() {
super::hosted_model_tool_specs(
second_step_context.turn.as_ref(),
second_step_context.turn.model_info(),
second_step_context.turn.config.web_search_mode.value(),
&[],
),
&cache,
@@ -2372,6 +2379,7 @@ async fn tool_search_cache_rebuilds_when_deferred_world_state_changes() {
super::hosted_model_tool_specs(
step_context.turn.as_ref(),
step_context.turn.model_info(),
step_context.turn.config.web_search_mode.value(),
&[],
),
&cache,