Files
codex/codex-rs/code-mode/src/tool_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

59 lines
1.7 KiB
Rust

//! Negotiates a per-session tool catalog at cell admission. Concurrent admissions
//! can always send a self-contained request without changing the shared catalog.
use std::sync::Arc;
use codex_code_mode_protocol::ToolDefinition;
use tokio::sync::Mutex;
use tokio::sync::OwnedMutexGuard;
#[derive(Default)]
pub(crate) struct Catalog {
revision: u64,
tools: Option<Vec<Arc<ToolDefinition>>>,
}
pub(crate) struct CatalogAdmission {
cache: OwnedMutexGuard<Catalog>,
replacement: Option<Vec<Arc<ToolDefinition>>>,
}
impl CatalogAdmission {
/// Takes ownership of a new snapshot until the host acknowledges admission.
/// Clearing the old snapshot first keeps cancellation from reusing a stale revision.
pub(crate) fn prepare(cache: &Arc<Mutex<Catalog>>, tools: &[ToolDefinition]) -> Option<Self> {
if tools.is_empty() {
return None;
}
let mut cache = Arc::clone(cache).try_lock_owned().ok()?;
let replacement = if cache.tools.as_ref().is_some_and(|cached| {
cached.len() == tools.len() && cached.iter().map(Arc::as_ref).eq(tools)
}) {
None
} else {
cache.revision = cache.revision.checked_add(1)?;
cache.tools = None;
Some(ToolDefinition::shared_catalog(tools))
};
Some(Self { cache, replacement })
}
pub(crate) fn revision(&self) -> u64 {
self.cache.revision
}
pub(crate) fn reuses_catalog(&self) -> bool {
self.replacement.is_none()
}
pub(crate) fn accepted(mut self) {
if let Some(tools) = self.replacement.take() {
self.cache.tools = Some(tools);
}
}
}
#[cfg(test)]
#[path = "tool_catalog_tests.rs"]
mod tests;