Share Apps tool catalogs without retaining unused snapshots (#45440)

## Why

Idle Apps clients and cached MCP bindings can retain replaced tool definitions. Shared catalog updates can also invalidate prepared calls even when the current definitions match the captured catalog, including after tools are restored while a call awaits approval.

## What changed

- Share immutable tool arrays across equivalent live discovery contexts and reuse storage for equal results. Let unused providers and cached bindings expire.
- Include requested capabilities and initialization results in the sharing scope, and detach servers that disable catalog caching from live sharing.
- Capture catalog snapshots for prepared calls. Accept equivalent shared catalogs regardless of tool-list order, while rejecting changed definitions and calls captured before an explicit refresh on that client.

## Testing

Add regression coverage for shared storage and scope isolation, release of replaced tools and unused bindings, equivalent catalog restoration, explicit refresh invalidation, and an Apps call completing after catalog restoration while awaiting approval.

GitOrigin-RevId: a0516186e4286d0ff13405fafbab6fcf3f1f3773
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-09-14 15:06:28 +00:00
1 parent f3803587c9
commit f8bed26f7b
12 files changed
+556 -132

No files matched your search

@@ -6,12 +6,16 @@
//! owned by the connector metadata store, not by this module.
//! Live catalog subscriptions publish only successful fetches from a matching
//! discovery scope, never disk snapshots or another scope's discovery winner.
//! Equivalent live contexts share one current catalog. Each fetch still contacts
//! the server; equal definitions retain their established storage and ordering.
//! Providers expire with their last context.
use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::Weak;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::time::Duration;
@@ -34,15 +38,21 @@ use self::persistence::tools_cache_path;
const MCP_TOOLS_CACHE_PUBLISH_DURATION_METRIC: &str = "codex.mcp.tools.cache_publish.duration_ms";
type LiveCatalog<T> = watch::Sender<Option<Arc<ConnectorRuntimeSnapshot<T>>>>;
/// The current immutable tools for matching discovery inputs.
struct CatalogProvider<T> {
scope: Vec<String>,
updates: watch::Sender<Option<Arc<ConnectorRuntimeSnapshot<T>>>>,
}
/// Values stored in the connector runtime's persisted tool snapshot.
///
/// The runtime uses the connector-owned Codex Apps cache layout for every
/// serializable, cloneable payload.
pub trait ConnectorRuntimePayload: Clone + Serialize + DeserializeOwned {}
/// serializable, cloneable payload. Equality determines whether fresh results can
/// retain the previous storage and tool version, so it must include all metadata
/// that affects readers or prepared calls.
pub trait ConnectorRuntimePayload: Clone + PartialEq + Serialize + DeserializeOwned {}
impl<T> ConnectorRuntimePayload for T where T: Clone + Serialize + DeserializeOwned {}
impl<T> ConnectorRuntimePayload for T where T: Clone + PartialEq + Serialize + DeserializeOwned {}
/// The account and workspace identity of a connector runtime catalog.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
@@ -96,9 +106,10 @@ pub fn connector_runtime_cache_path(codex_home: &Path, auth: Option<&CodexAuth>)
/// intentionally applied by readers rather than persisted in this snapshot.
#[derive(Debug, Clone)]
pub struct ConnectorRuntimeSnapshot<T> {
tools: Vec<T>,
tools: Arc<[T]>,
refreshed_at: SystemTime,
generation: u64,
tools_version: u64,
}
impl<T> ConnectorRuntimeSnapshot<T> {
@@ -106,6 +117,15 @@ impl<T> ConnectorRuntimeSnapshot<T> {
&self.tools
}
pub fn shared_tools(&self) -> Arc<[T]> {
Arc::clone(&self.tools)
}
/// Advances only when tool definitions change, so equal refreshes preserve prepared calls.
pub fn tools_version(&self) -> u64 {
self.tools_version
}
pub fn refreshed_at(&self) -> SystemTime {
self.refreshed_at
}
@@ -182,7 +202,7 @@ impl<T: ConnectorRuntimePayload> ConnectorRuntimeManager<T> {
/// Handle to one shared account/workspace connector runtime.
pub struct ConnectorRuntimeContext<T: ConnectorRuntimePayload> {
entry: Arc<ConnectorRuntimeEntry<T>>,
live_catalog: Option<LiveCatalog<T>>,
live_catalog: Option<Arc<CatalogProvider<T>>>,
}
impl<T: ConnectorRuntimePayload> Clone for ConnectorRuntimeContext<T> {
@@ -196,21 +216,42 @@ impl<T: ConnectorRuntimePayload> Clone for ConnectorRuntimeContext<T> {
impl<T: ConnectorRuntimePayload> ConnectorRuntimeContext<T> {
/// Groups executable catalogs by equivalent discovery inputs within this account/home.
/// The caller must include the endpoint, auth configuration and listing protocol in `scope`.
/// Include transport/auth, requested capabilities, and initialization results.
/// Calling this again refines the existing scope; it does not replace it.
/// Account-wide discovery reads are unaffected.
pub fn with_live_scope(mut self, scope: String) -> Self {
self.live_catalog = Some(
lock_unpoisoned(&self.entry.live_catalogs)
.entry(scope)
.or_insert_with(|| watch::channel(None).0)
.clone(),
);
let mut scope_parts = self
.live_catalog
.as_ref()
.map(|catalog| catalog.scope.clone())
.unwrap_or_default();
scope_parts.push(scope);
let mut catalogs = lock_unpoisoned(&self.entry.live_catalogs);
catalogs.retain(|_, catalog| catalog.strong_count() > 0);
let catalog = catalogs.entry(scope_parts.clone()).or_default();
self.live_catalog = Some(catalog.upgrade().unwrap_or_else(|| {
let provider = Arc::new(CatalogProvider {
scope: scope_parts,
updates: watch::channel(None).0,
});
*catalog = Arc::downgrade(&provider);
provider
}));
drop(catalogs);
self
}
/// Detaches a server that opts out of live catalog sharing, including publication.
pub fn without_live_scope(mut self) -> Self {
self.live_catalog = None;
self
}
/// Subscribes to accepted live tools without refetching or waiting for other clients.
pub fn subscribe(&self) -> Option<watch::Receiver<Option<Arc<ConnectorRuntimeSnapshot<T>>>>> {
self.live_catalog.as_ref().map(watch::Sender::subscribe)
self.live_catalog
.as_ref()
.map(|catalog| catalog.updates.subscribe())
}
pub fn current_snapshot(&self) -> Option<Arc<ConnectorRuntimeSnapshot<T>>> {
@@ -249,7 +290,7 @@ impl<T: ConnectorRuntimePayload> ConnectorRuntimeContext<T> {
pub fn current_tools(&self) -> Option<Vec<T>> {
self.current_snapshot()
.map(|snapshot| snapshot.tools.clone())
.map(|snapshot| snapshot.tools.to_vec())
}
pub fn publish_runtime_if_newest_accepted(
@@ -283,13 +324,26 @@ impl<T: ConnectorRuntimePayload> ConnectorRuntimeContext<T> {
) -> Arc<ConnectorRuntimeSnapshot<T>> {
let publish_start = Instant::now();
let mut last_accepted_generation = lock_unpoisoned(&self.entry.last_accepted_generation);
let prior = self
.live_catalog
.as_ref()
.and_then(|catalog| catalog.updates.borrow().clone());
let (tools, tools_version) = match prior {
Some(prior)
if prior.generation < ticket.generation && prior.tools() == tools.as_slice() =>
{
(prior.shared_tools(), prior.tools_version)
}
_ => (tools.into(), ticket.generation),
};
let snapshot = Arc::new(ConnectorRuntimeSnapshot {
tools,
tools_version,
refreshed_at: SystemTime::now(),
generation: ticket.generation,
});
if let Some(live_catalog) = &self.live_catalog {
live_catalog.send_if_modified(|current| {
live_catalog.updates.send_if_modified(|current| {
if current
.as_ref()
.is_some_and(|current| current.generation >= ticket.generation)
@@ -336,7 +390,7 @@ impl<T: ConnectorRuntimePayload> ConnectorRuntimeContext<T> {
) -> Vec<T> {
self.publish_runtime_if_newest_accepted(ticket, server_info, tools)
.tools
.clone()
.to_vec()
}
}
@@ -367,7 +421,7 @@ struct ConnectorRuntimeEntry<T: ConnectorRuntimePayload> {
current_snapshot: ArcSwapOption<ConnectorRuntimeSnapshot<T>>,
next_fetch_generation: AtomicU64,
last_accepted_generation: Mutex<u64>,
live_catalogs: Mutex<HashMap<String, LiveCatalog<T>>>,
live_catalogs: Mutex<HashMap<Vec<String>, Weak<CatalogProvider<T>>>>,
}
impl<T: ConnectorRuntimePayload> ConnectorRuntimeEntry<T> {
@@ -5,7 +5,6 @@ use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
#[cfg(test)]
use std::sync::Arc;
use std::time::Instant;
use std::time::SystemTime;
@@ -65,6 +64,7 @@ pub(crate) fn load_cached_connector_runtime_for_identity<T: ConnectorRuntimePayl
tools: cache.tools,
refreshed_at: modified_at,
generation: 0,
tools_version: 0,
},
)
}
@@ -207,7 +207,7 @@ fn write_codex_apps_cache_file(
#[derive(Debug, Clone, Serialize, Deserialize)]
struct CodexAppsToolsDiskCache<T> {
schema_version: u8,
tools: Vec<T>,
tools: Arc<[T]>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -232,9 +232,10 @@ pub(crate) fn write_cached_codex_apps_tools_for_test<T>(
T: ConnectorRuntimePayload,
{
let snapshot = ConnectorRuntimeSnapshot {
tools: tools.to_vec(),
tools: tools.into(),
refreshed_at: SystemTime::now(),
generation: 0,
tools_version: 0,
};
cache_context
.entry
@@ -251,7 +252,7 @@ where
T: ConnectorRuntimePayload,
{
load_cached_connector_runtime_for_identity(&cache_context.entry.identity)
.map(|snapshot| snapshot.tools)
.map(|snapshot| snapshot.tools.to_vec())
}
#[cfg(test)]
@@ -263,9 +264,10 @@ where
T: ConnectorRuntimePayload,
{
let snapshot = ConnectorRuntimeSnapshot {
tools: tools.to_vec(),
tools: tools.into(),
refreshed_at: SystemTime::now(),
generation: 0,
tools_version: 0,
};
write_cached_connector_runtime(cache_context, &snapshot)
}
@@ -630,6 +630,56 @@ fn contexts_for_different_identities_keep_isolated_snapshots() {
);
}
#[test]
fn live_provider_reuses_equal_tools_only_within_its_scope() {
let manager = ConnectorRuntimeManager::<TestTool>::new_without_cache();
let context = manager.context(
PathBuf::from("unused"),
ConnectorRuntimeContextKey::personal(
/*account_id*/ None, /*chatgpt_user_id*/ None,
),
);
let first = context
.clone()
.with_live_scope("endpoint".into())
.with_live_scope("capabilities".into());
let second = context
.clone()
.with_live_scope("endpoint".into())
.with_live_scope("capabilities".into());
let other = context
.clone()
.with_live_scope("other-endpoint".into())
.with_live_scope("capabilities".into());
let tools = vec![create_test_tool(CODEX_APPS_MCP_SERVER_NAME, "search")];
let publish = |context: &ConnectorRuntimeContext<TestTool>| {
context.publish_runtime_if_newest_accepted(
context.begin_fetch(ConnectorRuntimeFetchSource::Startup),
&create_test_server_info("Apps"),
tools.clone(),
)
};
let original = publish(&first);
let repeated = publish(&second);
let separate = publish(&other);
assert!(Arc::ptr_eq(
&original.shared_tools(),
&repeated.shared_tools()
));
assert_eq!(original.tools_version(), repeated.tools_version());
assert!(!Arc::ptr_eq(
&original.shared_tools(),
&separate.shared_tools()
));
drop(first);
drop(second);
let restarted = context
.with_live_scope("endpoint".into())
.with_live_scope("capabilities".into());
assert!(restarted.current_snapshot().is_some());
assert!(restarted.subscribe().unwrap().borrow().is_none());
}
#[test]
fn oversized_tools_cache_is_ignored_during_initial_load() {
let codex_home = tempdir().expect("tempdir");