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
+62 -3
View File
@@ -2870,6 +2870,24 @@ impl Session {
}
pub(crate) async fn emit_turn_item_started(&self, turn_context: &TurnContext, item: &TurnItem) {
if matches!(
item,
TurnItem::WebSearch(_)
| TurnItem::Extension(codex_extension_items::ExtensionItem::WebSearch(_))
) && let Some(usage) = turn_context
.extension_data
.get::<crate::model_search::SearchTurnUsage>()
{
// This turn selected Search MCP. Pin native only when a fallback call starts.
crate::model_search::SearchOwner::admit(
&self.services.thread_extension_data,
codex_protocol::openai_models::WebSearchProvider::Standalone,
);
usage.record(
turn_context,
codex_protocol::openai_models::WebSearchProvider::Standalone,
);
}
let started_at_ms = turn_context
.turn_timing_state
.record_item_started(item.id(), now_unix_timestamp_ms())
@@ -3901,6 +3919,10 @@ impl Session {
// Preserve any existing tool-specific override.
let policy: codex_utils_output_truncation::TruncationPolicy =
model_info.truncation_policy.into();
crate::model_search::SearchOwner::checkpoint(
&self.services.thread_extension_data,
&mut items,
);
for envelope in &mut items {
if matches!(
envelope.item,
@@ -4231,8 +4253,6 @@ impl Session {
let extension_data =
codex_extension_api::ExtensionData::new(turn_context.sub_id.clone());
extension_data.insert_arc(Arc::clone(&settings.model_info.model));
// Standalone and hosted search must use the same turn-scoped permission mode.
extension_data.insert(turn_context.config.web_search_mode.value());
if let Some(messages) = settings
.model_info
.model_messages
@@ -4249,16 +4269,51 @@ impl Session {
if let Some(messages) = model_messages.notes() {
extension_data.insert(messages.clone());
}
let mut required_servers = required_servers.to_vec();
if settings.model_info.web_search_provider
== Some(codex_protocol::openai_models::WebSearchProvider::SearchMcp)
&& turn_context.apps_enabled()
&& !turn_context.config.requires_native_search()
&& (turn_context.config.web_search_mode.value()
!= codex_protocol::config_types::WebSearchMode::Disabled
|| turn_context
.config
.features
.enabled(codex_features::Feature::HostedSearchPolicy))
{
required_servers.push(codex_mcp::CODEX_APPS_MCP_SERVER_NAME.to_owned());
}
let (mcp, prepared_recommendations) = tokio::join!(
// MCP refresh can be large; keep it off the sampling request's stack.
Box::pin(self.mcp_runtime_for_step(
turn_context.as_ref(),
&selected_capability_roots,
required_servers,
&required_servers,
required_plugins,
)),
turn::prepare_tool_recommendations(self.as_ref(), turn_context.as_ref()),
);
let search = crate::model_search::resolve(
turn_context.as_ref(),
&settings,
&mcp,
&self.services,
)
.await;
extension_data.insert(match &search {
Some(crate::model_search::ModelSearch::Native { mode, .. }) => *mode,
_ => turn_context.config.web_search_mode.value(),
});
let mcp = if let Some(search) = search {
let binding = match &search {
crate::model_search::ModelSearch::Hosted { binding, .. } => Arc::clone(binding),
_ => mcp,
};
extension_data.insert(search);
binding
} else {
mcp
};
extension_data.insert(selected_capability_roots);
// A step keeps the plugins from the environments it captured, even if shared MCP
// moves on to another environment. Its skill tools and the model use this same copy.
@@ -4527,6 +4582,10 @@ impl Session {
CompactedHistoryInstallation::Complete(metadata) => &metadata.compaction_model_hash,
CompactedHistoryInstallation::Prefix { model_info, .. } => &model_info.comp_hash,
};
crate::model_search::SearchOwner::checkpoint(
&self.services.thread_extension_data,
&mut items,
);
let mcp_revision = self
.services
.executed_tool_calls
+1
View File
@@ -1091,6 +1091,7 @@ impl Session {
thread_extension_init,
);
thread_extension_data.insert(crate::context::GuardianReviewEvidence::default());
crate::model_search::SearchOwner::restore(&thread_extension_data, &initial_history);
// Kick off independent async setup tasks in parallel to reduce startup latency.
//
// - initialize thread persistence with new or resumed session info
+5
View File
@@ -1872,6 +1872,11 @@ async fn run_sampling_request(
if first_attempt && turn_context.config.notify.is_some() {
notify_messages = legacy_notify_input_messages(&prompt.input);
}
let _native_search = crate::model_search::SearchOwner::reserve_server_fallback(
&step_context.extension_data,
&sess.services.thread_extension_data,
&mut prompt.tools,
);
let mut draining_for_mailbox = false;
let err = match try_run_sampling_request(
tool_runtime.clone(),