Share model-visible tool specs across prompts (#37807)

## Why

Building a prompt cloned every model-visible `ToolSpec`, even though the tool
set is immutable for the lifetime of its router.

## What changed

- Store model-visible tool specs as an `Arc<[ToolSpec]>` in `ToolRouter` and
  `Prompt` so prompt construction only clones the shared pointer.
- Keep separately built routers on distinct shared slices so refreshed tool
  sets remain independent.

## Testing

Extend router tests to verify allocation sharing within a router, allocation
separation across rebuilt routers, and the existing deferred-tool filtering.

GitOrigin-RevId: 1e04cd7c4d1b3fa0b494c5c51670232d38ba8ebb
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-08-10 10:50:49 +00:00
1 parent c8e6e8555c
commit beeba1d2fc
4 files changed
+16 -8

No files matched your search

+4 -4
View File
@@ -67,7 +67,7 @@ pub(crate) fn tool_log_payload<'a>(
pub struct ToolRouter {
registry: ToolRegistry,
model_visible_specs: Vec<ToolSpec>,
model_visible_specs: Arc<[ToolSpec]>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -102,12 +102,12 @@ impl ToolRouter {
pub(crate) fn from_parts(registry: ToolRegistry, model_visible_specs: Vec<ToolSpec>) -> Self {
Self {
registry,
model_visible_specs,
model_visible_specs: model_visible_specs.into(),
}
}
pub(crate) fn model_visible_specs(&self) -> Vec<ToolSpec> {
self.model_visible_specs.clone()
pub(crate) fn model_visible_specs(&self) -> Arc<[ToolSpec]> {
Arc::clone(&self.model_visible_specs)
}
pub(crate) fn deferred_tool_namespaces(&self) -> BTreeMap<String, String> {