Files
codex/codex-rs/code-mode-protocol/src/shared_catalog.rs
T
Charlie Marsh fef47a6769 Reuse code-mode tool catalogs across executions (#61846)
## Why

Repeated executions with the same tools resend the full catalog and duplicate tool metadata. Catalog revisions let clients reuse an acknowledged catalog within a session.

## What changed

- Negotiate catalog revision support over stdio and gRPC, retaining full requests for hosts without support.
- Send tool definitions when installing a revision, then reference that revision for matching executions. Commit client catalog state only after execution admission succeeds; concurrent admissions can send full requests.
- Keep immutable tool snapshots for each cell, reject invalid, unknown, or conflicting revisions, and share identical definitions and runtime metadata across sessions in the same process.

## Testing

Add tests for acknowledged reuse, abandoned admissions, changed definitions, revision conflicts, session isolation, snapshot lifetimes, and shared metadata.

GitOrigin-RevId: d2f29bf668bc2279715f5f1b7d24310923c57d33
2026-11-16 17:47:49 +00:00

71 lines
2.5 KiB
Rust

//! Shares immutable tool definitions across sessions in the same process. Each session
//! negotiates its own revision and owns its snapshot independently.
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::BuildHasher;
use std::hash::RandomState;
use std::sync::Arc;
use std::sync::LazyLock;
use std::sync::Mutex;
use std::sync::PoisonError;
use std::sync::Weak;
use codex_protocol::ToolName;
use crate::EnabledToolMetadata;
use crate::ToolDefinition;
type ToolIndex<T> = LazyLock<Mutex<HashMap<u64, Vec<Weak<T>>>>>;
impl ToolDefinition {
/// Shares matching definitions while preserving the client's catalog order.
pub fn shared_catalog(tools: &[Self]) -> Vec<Arc<Self>> {
static INDEX: ToolIndex<ToolDefinition> = LazyLock::new(Mutex::default);
share(&INDEX, tools.iter().map(Cow::Borrowed), |tool| {
&tool.tool_name
})
}
}
impl EnabledToolMetadata {
/// Shares matching runtime metadata while preserving the cell's callback indices.
pub fn shared_catalog(tools: Vec<Self>) -> Vec<Arc<Self>> {
static INDEX: ToolIndex<EnabledToolMetadata> = LazyLock::new(Mutex::default);
share(&INDEX, tools.into_iter().map(Cow::Owned), |tool| {
&tool.tool_name
})
}
}
fn share<'a, T: 'a + Clone + PartialEq>(
index: &ToolIndex<T>,
tools: impl Iterator<Item = Cow<'a, T>>,
name: impl Fn(&T) -> &ToolName,
) -> Vec<Arc<T>> {
static HASHER: LazyLock<RandomState> = LazyLock::new(RandomState::new);
let mut index = index.lock().unwrap_or_else(PoisonError::into_inner);
tools
.map(|tool| {
// Hash only the name. Versions of the same tool share a bucket, and
// exact equality checks the descriptions, schemas and other fields.
let hash = HASHER.hash_one(name(&tool));
if index.len() == index.capacity() && !index.contains_key(&hash) {
index.retain(|_, versions| versions.iter().any(|tool| tool.strong_count() > 0));
}
let versions = index.entry(hash).or_default();
if let Some(existing) = versions
.iter()
.filter_map(Weak::upgrade)
.find(|existing| existing.as_ref() == tool.as_ref())
{
return existing;
}
versions.retain(|tool| tool.strong_count() > 0);
let shared = Arc::new(tool.into_owned());
versions.push(Arc::downgrade(&shared));
shared
})
.collect()
}