Defer tool search indexing until the first search (#63440)

## Why

Tool planning built the search index even on turns that never used
`tool_search`.

## What changed

Build the index on a blocking worker when the first search over a
nonempty tool catalog needs it. Regular and Code Mode searches share
the same build within a handler and reuse the completed index. The
build continues if the search that started it is canceled.

## Testing

Add coverage for deferred indexing, concurrent first searches, and
reuse of an in-progress build after the first search is canceled.

GitOrigin-RevId: eb696e93dbc481a76a5adfe7f5506db971047bd5
This commit is contained in:
Dmitriy Kovalenko
2026-11-23 21:23:09 +00:00
committed by copyberry
parent 598083ae58
commit 8a64bc9c7e
3 changed files with 176 additions and 58 deletions
+120 -50
View File
@@ -30,7 +30,9 @@ use serde::Serialize;
use std::borrow::Cow;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::Weak;
use tokio::sync::watch;
use tracing::instrument;
#[derive(Serialize)]
@@ -40,11 +42,15 @@ struct CodeModeSearchResult {
}
pub struct ToolSearchHandler {
search_infos: Vec<ToolSearchInfo>,
search_infos: Arc<Vec<ToolSearchInfo>>,
source_listing: ToolSearchSourceListing,
spec: ToolSpec,
search_embedder: Embedder,
search_scorer: Scorer<usize>,
index: OnceLock<watch::Receiver<Option<Arc<ToolSearchIndex>>>>,
}
struct ToolSearchIndex {
embedder: Embedder,
scorer: Scorer<usize>,
}
#[derive(Default)]
@@ -163,24 +169,33 @@ impl ToolSearchHandler {
TOOL_SEARCH_DEFAULT_LIMIT,
source_listing,
);
Self {
search_infos: Arc::new(search_infos),
source_listing,
spec,
index: OnceLock::new(),
}
}
}
impl ToolSearchIndex {
#[instrument(
name = "tool_search.build_index",
level = "trace",
skip_all,
fields(search_info_count = search_infos.len())
)]
fn new(search_infos: &[ToolSearchInfo]) -> Self {
let corpus = search_infos
.iter()
.map(|search_info| search_info.entry.search_text.as_str())
.collect::<Vec<_>>();
let search_embedder =
EmbedderBuilder::with_fit_to_corpus(Language::English, &corpus).build();
let mut search_scorer = Scorer::new();
let embedder = EmbedderBuilder::with_fit_to_corpus(Language::English, &corpus).build();
let mut scorer = Scorer::new();
for (id, text) in corpus.into_iter().enumerate() {
search_scorer.upsert(&id, search_embedder.embed(text));
}
Self {
search_infos,
source_listing,
spec,
search_embedder,
search_scorer,
scorer.upsert(&id, embedder.embed(text));
}
Self { embedder, scorer }
}
}
@@ -249,27 +264,25 @@ impl ToolSearchHandler {
.map_err(|error| FunctionCallError::Fatal(error.to_string()))?;
if nested {
let tools = self.search_code_mode(
query,
limit,
&step_context.tool_router,
step_context
.turn
.config
.code_mode
.tool_input_schema_max_bytes,
&indirect_prefixes,
);
let tools = self
.search_code_mode(
query,
limit,
&step_context.tool_router,
step_context
.turn
.config
.code_mode
.tool_input_schema_max_bytes,
&indirect_prefixes,
)
.await?;
return Ok(boxed_tool_output(JsonToolOutput::new(serde_json::json!({
"tools": tools,
}))));
}
if self.search_infos.is_empty() {
return Ok(boxed_tool_output(ToolSearchOutput { tools: Vec::new() }));
}
let mut tools = self.search(query, limit);
let mut tools = self.search(query, limit).await?;
indirect_prefixes.apply_search(&mut tools);
Ok(boxed_tool_output(ToolSearchOutput { tools }))
@@ -279,17 +292,17 @@ impl ToolSearchHandler {
impl CoreToolRuntime for ToolSearchHandler {}
impl ToolSearchHandler {
fn search_code_mode(
async fn search_code_mode(
&self,
query: &str,
limit: usize,
router: &ToolRouter,
code_mode_input_schema_max_bytes: Option<usize>,
indirect_prefixes: &IndirectNamespacePrefixes<'_>,
) -> Vec<CodeModeSearchResult> {
) -> Result<Vec<CodeModeSearchResult>, FunctionCallError> {
let mut tools = Vec::new();
// Apply the exact step's nested-tool policy before limiting ranked results.
for entry in self.search_entries(query) {
for entry in self.search_entries(query).await? {
for candidate_name in entry.tool_names() {
let candidate_name = candidate_name.with_default_namespace();
let name = codex_code_mode::normalize_code_mode_identifier(
@@ -323,30 +336,70 @@ impl ToolSearchHandler {
.apply_code_mode_description(&definition.tool_name, &mut description);
tools.push(CodeModeSearchResult { name, description });
if tools.len() == limit {
return tools;
return Ok(tools);
}
}
}
tools
Ok(tools)
}
fn search(&self, query: &str, limit: usize) -> Vec<LoadableToolSpec> {
coalesce_loadable_tool_specs(
async fn search(
&self,
query: &str,
limit: usize,
) -> Result<Vec<LoadableToolSpec>, FunctionCallError> {
Ok(coalesce_loadable_tool_specs(
self.search_entries(query)
.await?
.take(limit)
.map(ToolSearchEntry::to_loadable_spec),
)
))
}
fn search_entries(&self, query: &str) -> impl Iterator<Item = &ToolSearchEntry> + '_ {
self.search_scorer
.matches(&self.search_embedder.embed(query))
async fn search_entries(
&self,
query: &str,
) -> Result<impl Iterator<Item = &ToolSearchEntry>, FunctionCallError> {
let matches = if self.search_infos.is_empty() {
Vec::new()
} else {
let mut index = self
.index
.get_or_init(|| {
// The blocking worker publishes independently of the searches waiting
// for it. The handler keeps a receiver so later searches can reuse it.
let (index_tx, index_rx) = watch::channel(None);
let search_infos = Arc::clone(&self.search_infos);
tokio::task::spawn_blocking(move || {
let index = Arc::new(ToolSearchIndex::new(&search_infos));
let _ = index_tx.send(Some(index));
});
index_rx
})
.clone();
let index = index
.wait_for(Option::is_some)
.await
.map_err(|err| {
FunctionCallError::Fatal(format!("tool search index task failed: {err}"))
})?
.clone()
.ok_or_else(|| {
FunctionCallError::Fatal("tool search index task returned no index".into())
})?;
index.scorer.matches(&index.embedder.embed(query))
};
Ok(matches
.into_iter()
.filter_map(|result| self.search_infos.get(result.id))
.map(|search_info| &search_info.entry)
.map(|search_info| &search_info.entry))
}
}
#[cfg(test)]
#[path = "tool_search_index_tests.rs"]
mod index_tests;
#[cfg(test)]
mod tests {
use super::*;
@@ -363,8 +416,8 @@ mod tests {
use rmcp::model::Tool;
use std::sync::Arc;
#[test]
fn cache_reuses_immutable_handlers_and_rebuilds_for_current_registry_changes() {
#[tokio::test]
async fn cache_reuses_immutable_handlers_and_rebuilds_for_current_registry_changes() {
let cache = ToolSearchHandlerCache::default();
let mut info = tool_info("calendar", "create_event", "Create events");
info.callable_namespace = "functions".to_string();
@@ -376,6 +429,16 @@ mod tests {
let first = cache.get_or_build(&registry, ToolSearchSourceListing::Include);
let second = cache.get_or_build(&registry, ToolSearchSourceListing::Include);
assert!(Arc::ptr_eq(&first, &second));
assert!(
first.index.get().is_none(),
"tool planning must defer indexing"
);
let (first_results, second_results) = tokio::try_join!(
first.search("Create events", /*limit*/ 1),
second.search("Create events", /*limit*/ 1),
)
.expect("both initial searches should succeed");
assert_eq!(first_results, second_results);
let without_sources = cache.get_or_build(&registry, ToolSearchSourceListing::Omit);
assert!(!Arc::ptr_eq(&first, &without_sources));
@@ -400,7 +463,7 @@ mod tests {
prefixed.search_infos[0].entry.search_text,
first.search_infos[0].entry.search_text
);
let mut expected = first.search("Create events", /*limit*/ 1);
let mut expected = first_results;
let LoadableToolSpec::Namespace(namespace) = &mut expected[0] else {
panic!("namespace")
};
@@ -408,12 +471,15 @@ mod tests {
panic!("function")
};
tool.description = format!("Guidance.\n\n{}", tool.description);
assert_eq!(prefixed.search("Create events", /*limit*/ 1), expected);
assert_eq!(
prefixed.search("Create events", /*limit*/ 1).await.unwrap(),
expected
);
registry.apply_functions_namespace_function_prefixes(/*prefixes*/ None);
let cleared = cache.get_or_build(&registry, ToolSearchSourceListing::Include);
assert_eq!(
cleared.search("Create events", /*limit*/ 1),
first.search("Create events", /*limit*/ 1)
cleared.search("Create events", /*limit*/ 1).await.unwrap(),
first.search("Create events", /*limit*/ 1).await.unwrap()
);
let mut disabled_registry = ToolRegistry::default();
@@ -580,7 +646,11 @@ mod tests {
);
}
fn tool_info(server_name: &str, tool_name: &str, description_prefix: &str) -> ToolInfo {
pub(super) fn tool_info(
server_name: &str,
tool_name: &str,
description_prefix: &str,
) -> ToolInfo {
ToolInfo {
server_key: codex_mcp::McpServerKey::unscoped(server_name),
supports_parallel_tool_calls: false,
@@ -0,0 +1,47 @@
#![cfg(test)]
use super::ToolSearchHandler;
use super::ToolSearchSourceListing;
use super::tests::tool_info;
use crate::tools::handlers::McpHandler;
use codex_tools::ToolExecutor;
use pretty_assertions::assert_eq;
use std::pin::pin;
#[test]
fn cancelling_the_first_search_preserves_the_pending_build() {
tokio::runtime::Builder::new_current_thread()
.max_blocking_threads(/*val*/ 1)
.build()
.unwrap()
.block_on(async {
let info = McpHandler::new(tool_info("calendar", "create_event", "Create events"))
.unwrap()
.search_info()
.unwrap();
let expected = vec![info.entry.to_loadable_spec()];
let handler = ToolSearchHandler::new(vec![info], ToolSearchSourceListing::Include);
// Occupy the sole blocking worker until both searches have tried to get the index.
let (release, wait) = std::sync::mpsc::channel::<()>();
let (started, ready) = tokio::sync::oneshot::channel();
let blocker = tokio::task::spawn_blocking(move || {
let _ = started.send(());
let _ = wait.recv();
});
ready.await.unwrap();
let first_build = {
let mut search = pin!(handler.search("Create events", /*limit*/ 1));
assert!(futures::poll!(&mut search).is_pending());
handler.index.get().unwrap().clone()
};
let mut search = pin!(handler.search("Create events", /*limit*/ 1));
assert!(futures::poll!(&mut search).is_pending());
assert!(first_build.same_channel(handler.index.get().unwrap()));
drop(release);
blocker.await.unwrap();
assert_eq!(search.await.unwrap(), expected);
});
}
@@ -87,7 +87,7 @@ use wiremock::matchers::path_regex;
#[derive(Clone, Default)]
struct McpCacheCounters {
binding_captures: Arc<AtomicUsize>,
search_index_builds: Arc<AtomicUsize>,
search_handler_constructions: Arc<AtomicUsize>,
}
impl<S: Subscriber> Layer<S> for McpCacheCounters {
@@ -98,7 +98,8 @@ impl<S: Subscriber> Layer<S> for McpCacheCounters {
self.binding_captures.fetch_add(1, Ordering::SeqCst);
}
("codex_core::tools::handlers::tool_search", "new") => {
self.search_index_builds.fetch_add(1, Ordering::SeqCst);
self.search_handler_constructions
.fetch_add(1, Ordering::SeqCst);
}
_ => {}
}
@@ -1766,14 +1767,14 @@ async fn deferred_tool_world_state_tracks_initial_unchanged_and_removed_namespac
test.submit_turn("inspect initially available deferred tools")
.await?;
let initial_captures = counters.binding_captures.load(Ordering::SeqCst);
let initial_index_builds = counters.search_index_builds.load(Ordering::SeqCst);
let initial_handlers = counters.search_handler_constructions.load(Ordering::SeqCst);
assert!(
initial_captures > 0,
"the initial turn must capture an MCP binding"
);
assert!(
initial_index_builds > 0,
"the initial turn must build the search index"
initial_handlers > 0,
"the initial turn must construct a tool search handler"
);
// Publish a new catalog revision with the same metadata from the ready client.
@@ -1784,9 +1785,9 @@ async fn deferred_tool_world_state_tracks_initial_unchanged_and_removed_namespac
"the follow-up must capture a new binding after the refresh"
);
assert_eq!(
counters.search_index_builds.load(Ordering::SeqCst),
initial_index_builds,
"equivalent bindings must preserve MCP handlers and reuse the search index"
counters.search_handler_constructions.load(Ordering::SeqCst),
initial_handlers,
"equivalent bindings must preserve MCP handlers and reuse the tool search handler"
);
let current_config = test.codex.config().await;