Refresh plugin recommendations after installed state changes (#62610)

## Why

Recommendation lookups poll the plugin service for a tooling-state marker even when the cached catalog can still be reused. Install flows and installed-plugin snapshots already give Codex signals to refresh recommendations.

## What changed

Remove tooling-state polling and refresh recommendations on the next lookup after a known install event or a change to the installed plugin inventory or enabled state. Invalidate all product SKUs for the affected plugin service and account scope. If the account changes while a remote install prompt is open, invalidate both account scopes when the user accepts.

Concurrent lookups share a catalog fetch. If a refresh fails, keep serving the last successful result when available and retry on the next lookup.

## Testing

Cover invalidation across accounts, backends, and product SKUs, retention of the last catalog after a failed refresh, and isolation from fetches that complete after invalidation. Update the remote install integration test to exercise the next turn without waiting for the poll interval.

GitOrigin-RevId: 5d2ba84d1118e9346308bf6225543e0e174f8125
This commit is contained in:
Alex Zamoshchin
2026-11-19 22:16:30 +00:00
committed by copyberry
parent 6633c6e09a
commit 6a8a81166a
8 changed files with 494 additions and 294 deletions
+109 -87
View File
@@ -164,7 +164,6 @@ const FEATURED_PLUGIN_IDS_CACHE_TTL: std::time::Duration =
std::time::Duration::from_secs(60 * 60 * 3);
const REMOTE_INSTALLED_PLUGIN_SYNC_WAIT_TIMEOUT: Duration = Duration::from_secs(120);
const LOADED_PLUGINS_CACHE_CAPACITY: usize = 8;
const TOOLING_STATE_REUSE_WINDOW: Duration = Duration::from_secs(1);
type EffectivePluginsChangedCallback = Arc<dyn Fn(EffectivePluginsChange) + Send + Sync + 'static>;
@@ -305,23 +304,34 @@ pub struct RecommendedPluginCandidatesInput<'a> {
#[derive(Clone, Hash, PartialEq, Eq)]
struct PluginServiceCacheKey {
product_sku: String,
scope: PluginServiceScope,
}
#[derive(Clone, Hash, PartialEq, Eq)]
struct PluginServiceScope {
chatgpt_base_url: String,
account_id: Option<String>,
chatgpt_user_id: Option<String>,
is_workspace_account: bool,
}
#[derive(Clone)]
struct CachedRecommendations {
mode: RecommendedPluginsMode,
tooling_changed_at: Option<String>,
checked_at: Instant,
}
#[derive(Default)]
struct RecommendationCacheEntry {
cached: Option<CachedRecommendations>,
in_flight: Option<Arc<OnceCell<RecommendedPluginsMode>>>,
fetch_result: OnceCell<Result<RecommendedPluginsMode, ()>>,
fallback: RecommendedPluginsMode,
}
impl RecommendationCacheEntry {
fn for_refresh(&self) -> Arc<Self> {
let fallback = match self.fetch_result.get() {
Some(Ok(mode)) => mode,
Some(Err(())) | None => &self.fallback,
};
Arc::new(Self {
fetch_result: OnceCell::new(),
fallback: fallback.clone(),
})
}
}
#[derive(Clone)]
@@ -354,6 +364,8 @@ impl RemoteInstalledPluginsAuthIdentity {
struct RemoteInstalledPluginsCache {
generation: u64,
auth_identity: Option<RemoteInstalledPluginsAuthIdentity>,
// The installed cache is shared across backends; compare inventories only within this scope.
snapshot_scope: Option<PluginServiceScope>,
reconciliation: ReconciliationState,
plugins: Option<Vec<RemoteInstalledPlugin>>,
pending_runtime_change: Option<uuid::Uuid>,
@@ -531,13 +543,35 @@ fn plugin_service_cache_key(
.as_deref()
.unwrap_or(CODEX_PRODUCT_SKU)
.to_owned(),
chatgpt_base_url: config.chatgpt_base_url.clone(),
scope: plugin_service_scope(&config.chatgpt_base_url, auth),
}
}
fn plugin_service_scope(chatgpt_base_url: &str, auth: Option<&CodexAuth>) -> PluginServiceScope {
PluginServiceScope {
chatgpt_base_url: chatgpt_base_url.trim_end_matches('/').to_owned(),
account_id: auth.and_then(CodexAuth::get_account_id),
chatgpt_user_id: auth.and_then(CodexAuth::get_chatgpt_user_id),
is_workspace_account: auth.is_some_and(CodexAuth::is_workspace_account),
}
}
fn installed_plugin_recommendation_state(
plugins: &[RemoteInstalledPlugin],
) -> HashSet<(&str, &str, &str, bool)> {
plugins
.iter()
.map(|plugin| {
(
plugin.marketplace_name.as_str(),
plugin.name.as_str(),
plugin.id.as_str(),
plugin.enabled,
)
})
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginInstallRequest {
pub plugin_name: String,
@@ -664,7 +698,7 @@ pub struct PluginsManager {
codex_home: PathBuf,
store: PluginStore,
featured_plugin_ids_cache: RwLock<Option<CachedFeaturedPluginIds>>,
recommended_plugins_cache: Mutex<HashMap<PluginServiceCacheKey, RecommendationCacheEntry>>,
recommended_plugins_cache: Mutex<HashMap<PluginServiceCacheKey, Arc<RecommendationCacheEntry>>>,
configured_marketplace_upgrade_state: RwLock<ConfiguredMarketplaceUpgradeState>,
non_curated_cache_refresh_lock: Semaphore,
non_curated_cache_refresh_state: RwLock<NonCuratedCacheRefreshState>,
@@ -1140,6 +1174,27 @@ impl PluginsManager {
.clear();
}
/// Reload the affected actors' SKU-specific catalogs on demand after a known tooling change.
pub fn invalidate_recommendations_for_actors<'a>(
&self,
chatgpt_base_url: &str,
actors: impl IntoIterator<Item = &'a CodexAuth>,
) {
let scopes = actors
.into_iter()
.map(|auth| plugin_service_scope(chatgpt_base_url, Some(auth)))
.collect::<Vec<_>>();
let mut cache = self
.recommended_plugins_cache
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
for (key, entry) in cache.iter_mut() {
if scopes.contains(&key.scope) {
*entry = entry.for_refresh();
}
}
}
fn clear_loaded_plugins_cache(&self) {
self.tool_suggest_metadata_cache.clear();
let mut cache = match self.loaded_plugins_cache.lock() {
@@ -1531,6 +1586,7 @@ impl PluginsManager {
generation,
plugins,
auth,
Some(&config.chatgpt_base_url),
RemoteInstalledPluginsCachePublication::Refresh(display_assets),
) else {
let auth_identity = RemoteInstalledPluginsAuthIdentity::from_auth(auth);
@@ -1577,6 +1633,7 @@ impl PluginsManager {
let cache_changed = if cache.auth_identity.as_ref() != Some(&auth_identity) {
cache.generation = cache.generation.wrapping_add(1);
cache.auth_identity = Some(auth_identity);
cache.snapshot_scope = None;
cache.reconciliation = ReconciliationState::Idle;
cache.pending_runtime_change = None;
cache.plugins.take().is_some()
@@ -1666,6 +1723,7 @@ impl PluginsManager {
generation: u64,
mut plugins: Vec<RemoteInstalledPlugin>,
auth: Option<&CodexAuth>,
chatgpt_base_url: Option<&str>,
publication: RemoteInstalledPluginsCachePublication,
) -> Option<InstalledPluginCacheUpdate> {
let auth_identity = RemoteInstalledPluginsAuthIdentity::from_auth(auth);
@@ -1713,6 +1771,14 @@ impl PluginsManager {
}
let previous = cache.plugins.as_deref();
let payload_changed = previous != Some(plugins.as_slice());
let snapshot_scope = chatgpt_base_url.map(|url| plugin_service_scope(url, auth));
let installed_state_changed = snapshot_scope.is_some()
&& (cache.snapshot_scope != snapshot_scope
|| payload_changed
&& previous.is_none_or(|old| {
installed_plugin_recommendation_state(old)
!= installed_plugin_recommendation_state(&plugins)
}));
let metadata_changed = payload_changed
&& previous.is_none_or(|old| {
!crate::remote_metadata::installed_plugin_metadata_eq(old, &plugins)
@@ -1734,7 +1800,11 @@ impl PluginsManager {
if payload_changed {
cache.plugins = Some(plugins);
}
cache.snapshot_scope = snapshot_scope;
drop(cache);
if installed_state_changed && let Some(chatgpt_base_url) = chatgpt_base_url {
self.invalidate_recommendations_for_actors(chatgpt_base_url, auth);
}
if let Some(change_id) = metadata_change_id {
tracing::info!(%change_id, ?changes, initial_snapshot, reconciliation_retry = needs_effective_plugins_refresh, is_reconcile, "installed plugin runtime metadata invalidated");
self.clear_loaded_plugins_cache();
@@ -1760,6 +1830,7 @@ impl PluginsManager {
generation,
plugins,
auth.as_ref(),
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Include),
)
.expect("test cache generation should remain current")
@@ -1774,6 +1845,7 @@ impl PluginsManager {
};
cache.generation = cache.generation.wrapping_add(1);
cache.auth_identity = None;
cache.snapshot_scope = None;
cache.reconciliation = ReconciliationState::Idle;
cache.pending_runtime_change = None;
if cache.plugins.take().is_none() {
@@ -1794,6 +1866,7 @@ impl PluginsManager {
}
cache.generation = cache.generation.wrapping_add(1);
cache.auth_identity = None;
cache.snapshot_scope = None;
cache.reconciliation = ReconciliationState::Idle;
cache.pending_runtime_change = None;
if cache.plugins.take().is_none() {
@@ -1837,6 +1910,7 @@ impl PluginsManager {
auth: Option<CodexAuth>,
on_effective_plugins_changed: Option<EffectivePluginsChangedCallback>,
) {
self.invalidate_recommendations_for_actors(&config.chatgpt_base_url, auth.iter());
self.maybe_start_remote_installed_plugins_cache_refresh_with_notify(
config,
auth,
@@ -2090,6 +2164,7 @@ impl PluginsManager {
generation,
result.installed_plugins,
auth,
Some(&config.chatgpt_base_url),
RemoteInstalledPluginsCachePublication::Reconcile,
) else {
return Err(RemoteInstalledPluginBundleSyncError::Superseded);
@@ -2233,50 +2308,37 @@ impl PluginsManager {
}
let cache_key = plugin_service_cache_key(config, auth);
let (cached, refresh) = {
let entry = {
let mut cache = self
.recommended_plugins_cache
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let entry = cache.entry(cache_key.clone()).or_default();
if let Some(cached) = &entry.cached
&& cached.checked_at.elapsed() < TOOLING_STATE_REUSE_WINDOW
{
return cached.mode.clone();
let entry = cache.entry(cache_key).or_default();
// Existing waiters share the failed attempt. The next lookup starts a new one.
if entry.fetch_result.get().is_some_and(Result::is_err) {
*entry = entry.for_refresh();
}
(
entry.cached.clone(),
Arc::clone(entry.in_flight.get_or_insert_default()),
)
Arc::clone(entry)
};
refresh
entry
.fetch_result
.get_or_init(|| async {
let updated = self.refresh_recommendations(config, auth, cached).await;
let mode = updated
.as_ref()
.map_or(RecommendedPluginsMode::Legacy, |cached| cached.mode.clone());
let mut cache = self
.recommended_plugins_cache
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
// A clear may have discarded this request while it was in flight.
if let Some(entry) = cache.get_mut(&cache_key)
&& entry
.in_flight
.as_ref()
.is_some_and(|current| Arc::ptr_eq(current, &refresh))
{
if let Some(cached) = updated {
entry.cached = Some(cached);
entry.in_flight = None;
} else {
cache.remove(&cache_key);
}
}
mode
crate::remote::fetch_recommended_plugins(
&self.remote_plugin_service_config(config),
auth,
)
.await
.map_err(|err| {
warn!(
sub_error_type = err.sub_error_type(),
"failed to load recommended plugins"
);
})
})
.await
.as_ref()
.unwrap_or(&entry.fallback)
.clone()
}
@@ -2325,47 +2387,6 @@ impl PluginsManager {
))
}
async fn refresh_recommendations(
&self,
config: &PluginsConfigInput,
auth: Option<&CodexAuth>,
mut cached: Option<CachedRecommendations>,
) -> Option<CachedRecommendations> {
let remote = self.remote_plugin_service_config(config);
// Observe the marker first so a change during the catalog read is seen on the next poll.
let marker = crate::remote::fetch_tooling_changed_at(&remote, auth)
.await
.unwrap_or_else(|err| {
tracing::debug!(
sub_error_type = err.sub_error_type(),
"failed to read recommendation tooling state"
);
None
});
let checked_at = Instant::now();
if let Some(entry) = &mut cached {
entry.checked_at = checked_at;
if marker.is_none() || marker == entry.tooling_changed_at {
return cached;
}
}
match crate::remote::fetch_recommended_plugins(&remote, auth).await {
Ok(mode) => Some(CachedRecommendations {
mode,
tooling_changed_at: marker,
checked_at,
}),
Err(err) => {
warn!(
sub_error_type = err.sub_error_type(),
"failed to load recommended plugins"
);
// Keep the previous marker so the next poll retries the changed catalog.
cached
}
}
}
pub async fn install_plugin(
&self,
config_layer_stack: &ConfigLayerStack,
@@ -3653,6 +3674,7 @@ impl PluginsManager {
request.generation,
plugins,
request.auth.as_ref(),
Some(&request.service_config.chatgpt_base_url),
RemoteInstalledPluginsCachePublication::Refresh(
InstalledPluginDisplayAssets::Include,
),
@@ -0,0 +1,246 @@
//! Regression coverage for recommendation invalidation when installed snapshots are published.
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn installed_snapshot_changes_invalidate_only_the_affected_recommendations() {
let codex_home = TempDir::new().unwrap();
write_cached_plugin(
codex_home.path(),
REMOTE_WORKSPACE_MARKETPLACE_NAME,
"linear",
);
let server = MockServer::start().await;
let mut config = load_config(codex_home.path(), codex_home.path()).await;
config.chatgpt_base_url = server.uri();
let auth_manager = test_auth_manager(Some(AuthMode::Chatgpt));
let auth = auth_manager.auth_cached().expect("test ChatGPT auth");
let other_auth = CodexAuth::from_external_chatgpt_tokens(
"header.e30.other",
"other-account",
/*chatgpt_plan_type*/ None,
)
.expect("build second ChatGPT auth");
let manager = test_plugins_manager_with_auth_manager(
codex_home.path().to_path_buf(),
Some(Product::Codex),
auth_manager,
);
let installed_state = Arc::new(std::sync::Mutex::new(Some(true)));
let response_state = Arc::clone(&installed_state);
Mock::given(method("GET"))
.and(path("/ps/plugins/installed"))
.respond_with(move |_: &wiremock::Request| {
let plugins = (*response_state.lock().unwrap())
.into_iter()
.map(|enabled| {
serde_json::json!({
"id": "plugins~Plugin_linear",
"name": "linear",
"scope": "WORKSPACE",
"discoverability": "LISTED",
"installation_policy": "AVAILABLE",
"authentication_policy": "ON_USE",
"release": {
"version": "local",
"display_name": "Linear",
"description": "Test plugin",
"interface": {},
},
"enabled": enabled,
})
})
.collect::<Vec<_>>();
ResponseTemplate::new(200).set_body_json(serde_json::json!({
"plugins": plugins,
"pagination": {"next_page_token": null},
}))
})
.expect(8)
.mount(&server)
.await;
let suggested_response = ResponseTemplate::new(200)
.set_body_json(serde_json::json!({"enabled": true, "plugins": []}));
let suggested = Mock::given(method("GET"))
.and(path("/ps/plugins/suggested/codex"))
.and(header("chatgpt-account-id", auth.get_account_id().unwrap()))
.respond_with(suggested_response.clone())
.expect(5)
.mount_as_scoped(&server)
.await;
let other_suggested = Mock::given(method("GET"))
.and(path("/ps/plugins/suggested/codex"))
.and(header(
"chatgpt-account-id",
other_auth.get_account_id().unwrap(),
))
.respond_with(suggested_response)
.expect(1)
.mount_as_scoped(&server)
.await;
let expected = RecommendedPluginsMode::Endpoint {
plugins: Vec::new(),
};
let read_recommendations = async || {
assert_eq!(
manager
.recommended_plugins_mode_for_config(&config, Some(&auth))
.await,
expected
);
};
read_recommendations().await;
assert_eq!(
manager
.recommended_plugins_mode_for_config(&config, Some(&other_auth))
.await,
expected
);
for (state, expected_reads) in [
(Some(true), 2),
(Some(true), 2),
(Some(false), 3),
(Some(false), 3),
] {
*installed_state.lock().unwrap() = state;
manager
.reconcile_remote_installed_plugins(&config, Some(&auth))
.await
.expect("reconcile installed plugins");
read_recommendations().await;
assert_eq!(suggested.received_requests().await.len(), expected_reads);
}
*installed_state.lock().unwrap() = Some(true);
manager
.build_and_cache_remote_installed_plugin_marketplaces_with_display_assets(
&config,
Some(&auth),
&[],
/*on_effective_plugins_changed*/ None,
InstalledPluginDisplayAssets::Skip,
)
.await
.expect("refresh installed metadata before reconciliation");
read_recommendations().await;
assert_eq!(suggested.received_requests().await.len(), 4);
manager
.reconcile_remote_installed_plugins(&config, Some(&auth))
.await
.expect("reconcile the already published state");
read_recommendations().await;
assert_eq!(suggested.received_requests().await.len(), 4);
for expected_reads in [5, 5] {
*installed_state.lock().unwrap() = None;
manager
.reconcile_remote_installed_plugins(&config, Some(&auth))
.await
.expect("reconcile removed plugin");
read_recommendations().await;
assert_eq!(suggested.received_requests().await.len(), expected_reads);
}
assert_eq!(
manager
.recommended_plugins_mode_for_config(&config, Some(&other_auth))
.await,
expected
);
assert_eq!(other_suggested.received_requests().await.len(), 1);
server.verify().await;
}
#[tokio::test]
async fn installed_snapshot_backend_switch_invalidates_only_the_new_backend() {
let codex_home = TempDir::new().unwrap();
let first_server = MockServer::start().await;
let second_server = MockServer::start().await;
let mut first_config = load_config(codex_home.path(), codex_home.path()).await;
first_config.chatgpt_base_url = first_server.uri();
let mut second_config = first_config.clone();
second_config.chatgpt_base_url = second_server.uri();
let auth_manager = test_auth_manager(Some(AuthMode::Chatgpt));
let auth = auth_manager.auth_cached().expect("test ChatGPT auth");
let manager = test_plugins_manager_with_auth_manager(
codex_home.path().to_path_buf(),
Some(Product::Codex),
auth_manager,
);
let installed_response = ResponseTemplate::new(200).set_body_json(serde_json::json!({
"plugins": [],
"pagination": {"next_page_token": null},
}));
for (server, calls) in [(&first_server, 1), (&second_server, 2)] {
Mock::given(method("GET"))
.and(path("/ps/plugins/installed"))
.respond_with(installed_response.clone())
.expect(calls)
.mount(server)
.await;
}
let suggested_response = ResponseTemplate::new(200)
.set_body_json(serde_json::json!({"enabled": true, "plugins": []}));
let mut suggested = Vec::new();
for (server, calls) in [(&first_server, 1), (&second_server, 2)] {
suggested.push(
Mock::given(method("GET"))
.and(path("/ps/plugins/suggested/codex"))
.respond_with(suggested_response.clone())
.expect(calls)
.mount_as_scoped(server)
.await,
);
}
let expected = RecommendedPluginsMode::Endpoint {
plugins: Vec::new(),
};
assert_eq!(
manager
.recommended_plugins_mode_for_config(&second_config, Some(&auth))
.await,
expected
);
let publish = async |config: &PluginsConfigInput| {
manager
.build_and_cache_remote_installed_plugin_marketplaces_with_display_assets(
config,
Some(&auth),
&[],
/*on_effective_plugins_changed*/ None,
InstalledPluginDisplayAssets::Skip,
)
.await
.expect("publish installed snapshot");
};
publish(&first_config).await;
assert_eq!(
manager
.recommended_plugins_mode_for_config(&first_config, Some(&auth))
.await,
expected
);
publish(&second_config).await;
for unchanged in [false, true] {
if unchanged {
publish(&second_config).await;
}
assert_eq!(
manager
.recommended_plugins_mode_for_config(&second_config, Some(&auth))
.await,
expected
);
assert_eq!(
manager
.recommended_plugins_mode_for_config(&first_config, Some(&auth))
.await,
expected
);
assert_eq!(suggested[0].received_requests().await.len(), 1);
assert_eq!(suggested[1].received_requests().await.len(), 2);
}
first_server.verify().await;
second_server.verify().await;
}
+113 -93
View File
@@ -81,6 +81,9 @@ mod curated_marketplace_policy;
#[path = "remote_metadata_cache_tests.rs"]
mod remote_metadata_cache;
#[path = "manager_recommendation_publication_tests.rs"]
mod recommendation_publication;
fn unrestricted_config_layer_stack() -> ConfigLayerStack {
ConfigLayerStack::default()
}
@@ -1982,6 +1985,7 @@ async fn unresolved_display_assets_does_not_satisfy_display_cache_reads() {
generation,
plugins.clone(),
auth.as_ref(),
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Skip),
)
.map(|update| update.metadata_change_id.is_some()),
@@ -2007,6 +2011,7 @@ async fn unresolved_display_assets_does_not_satisfy_display_cache_reads() {
generation,
plugins.clone(),
auth.as_ref(),
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(
InstalledPluginDisplayAssets::Include
),
@@ -2026,6 +2031,7 @@ async fn unresolved_display_assets_does_not_satisfy_display_cache_reads() {
generation,
plugins,
auth.as_ref(),
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Skip),
)
.map(|update| update.metadata_change_id.is_some()),
@@ -2052,6 +2058,7 @@ async fn publish_catalog_changes_without_invalidating_the_installed_runtime() {
generation,
vec![plugin.clone()],
auth.as_ref(),
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Include),
)
.unwrap();
@@ -2064,6 +2071,7 @@ async fn publish_catalog_changes_without_invalidating_the_installed_runtime() {
generation,
vec![plugin.clone()],
auth.as_ref(),
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(InstalledPluginDisplayAssets::Include),
)
.unwrap();
@@ -6354,7 +6362,8 @@ plugins = true
let cache_key = plugin_service_cache_key(&config, Some(&auth));
let cached_mode = || {
let cache = manager.recommended_plugins_cache.lock().unwrap();
Some(cache.get(&cache_key)?.cached.as_ref()?.mode.clone())
let result = cache.get(&cache_key)?.fetch_result.get()?;
result.as_ref().ok().cloned()
};
manager.maybe_start_remote_plugin_caches_refresh(
@@ -6389,31 +6398,8 @@ plugins = true
assert!(cached_mode().is_none());
}
async fn mock_recommendation_responses(
server: &MockServer,
route: &str,
responses: impl IntoIterator<Item = ResponseTemplate>,
) {
for response in responses {
Mock::given(method("GET"))
.and(path(route))
.respond_with(response)
.up_to_n_times(1)
.expect(1)
.mount(server)
.await;
}
Mock::given(method("GET"))
.and(path(route))
.respond_with(ResponseTemplate::new(500))
.with_priority(255)
.expect(0)
.mount(server)
.await;
}
#[tokio::test]
async fn recommended_plugins_stay_cached_until_a_changed_marker_refresh_succeeds() {
async fn invalidated_recommendations_keep_the_last_catalog_until_refresh_succeeds() {
let tmp = TempDir::new().unwrap();
let server = MockServer::start().await;
let mut config = load_config(tmp.path(), tmp.path()).await;
@@ -6421,83 +6407,113 @@ async fn recommended_plugins_stay_cached_until_a_changed_marker_refresh_succeeds
let manager = test_plugins_manager(tmp.path().to_path_buf());
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
let recommendations = || manager.recommended_plugins_mode_for_config(&config, Some(&auth));
let marker = |value: Option<&str>| {
ResponseTemplate::new(200).set_body_json(serde_json::json!({"tooling_changed_at": value}))
let catalog = |enabled: bool| {
ResponseTemplate::new(200)
.set_body_json(serde_json::json!({"enabled": enabled, "plugins": []}))
};
mock_recommendation_responses(
&server,
"/ps/plugins/tooling-state/turtle",
[
marker(Some("100")),
marker(None),
ResponseTemplate::new(503),
marker(Some("100")),
marker(Some("101")),
marker(Some("101")),
marker(Some("101")),
],
)
.await;
mock_recommendation_responses(
&server,
"/ps/plugins/suggested/codex",
[
ResponseTemplate::new(200).set_body_json(serde_json::json!({
"enabled": true,
"plugins": [{"id": "plugin_slack", "name": "slack", "display_name": "Slack"}]
})),
ResponseTemplate::new(503),
ResponseTemplate::new(200)
.set_body_json(serde_json::json!({"enabled": false, "plugins": []})),
],
)
.await;
let original = RecommendedPluginsMode::Endpoint {
plugins: vec![RecommendedPlugin {
config_id: "slack@openai-curated-remote".to_string(),
remote_plugin_id: "plugin_slack".to_string(),
display_name: "Slack".to_string(),
}],
};
let (left, right) = tokio::join!(recommendations(), recommendations());
assert_eq!((left, right), (original.clone(), original.clone()));
assert_eq!(recommendations().await, original, "fresh cache hit");
let key = plugin_service_cache_key(&config, Some(&auth));
let poll_cutoff = Instant::now() - TOOLING_STATE_REUSE_WINDOW;
let stale_check = poll_cutoff
.checked_sub(Duration::from_secs(24 * 60 * 60))
.unwrap_or(poll_cutoff);
let age_tooling_check = || {
let mut cache = manager.recommended_plugins_cache.lock().unwrap();
let cached = cache
.get_mut(&key)
.and_then(|entry| entry.cached.as_mut())
.expect("recommendations were cached");
cached.checked_at = stale_check;
};
for reason in ["null marker", "failed marker request", "unchanged marker"] {
age_tooling_check();
assert_eq!(recommendations().await, original, "{reason}");
for response in [
catalog(true),
ResponseTemplate::new(503),
ResponseTemplate::new(503),
catalog(false),
] {
Mock::given(method("GET"))
.and(path("/ps/plugins/suggested/codex"))
.respond_with(response)
.up_to_n_times(1)
.expect(1)
.mount(&server)
.await;
}
// Concurrent callers share the failed catalog refresh. The next poll retries the same marker.
age_tooling_check();
let enabled = RecommendedPluginsMode::Endpoint {
plugins: Vec::new(),
};
assert_eq!(recommendations().await, enabled);
manager.invalidate_recommendations_for_actors(&config.chatgpt_base_url, [&auth]);
let (left, right) = tokio::join!(recommendations(), recommendations());
assert_eq!((left, right), (original.clone(), original));
age_tooling_check();
assert_eq!(recommendations().await, RecommendedPluginsMode::Legacy);
age_tooling_check();
assert_eq!(left, enabled);
assert_eq!(right, enabled);
assert_eq!(
recommendations().await,
RecommendedPluginsMode::Legacy,
"an unchanged marker reuses the explicitly disabled response"
enabled,
"a failed retry retains the catalog"
);
assert_eq!(recommendations().await, RecommendedPluginsMode::Legacy);
assert_eq!(recommendations().await, RecommendedPluginsMode::Legacy);
assert_eq!(server.received_requests().await.unwrap().len(), 4);
}
#[tokio::test]
async fn recommended_plugins_cache_clear_rejects_stale_fetch_completion() {
async fn actor_invalidation_reloads_every_sku_only_for_the_matching_account_and_backend() {
let tmp = TempDir::new().unwrap();
let server = MockServer::start().await;
let other_server = MockServer::start().await;
let mut config = load_config(tmp.path(), tmp.path()).await;
config.chatgpt_base_url = server.uri();
let mut work = config.clone();
work.product_sku = Some("tpp".to_string());
let mut other_backend = config.clone();
other_backend.chatgpt_base_url = other_server.uri();
let manager = test_plugins_manager(tmp.path().to_path_buf());
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
let other_auth = CodexAuth::from_external_chatgpt_tokens(
"header.e30.other",
"other-account",
/*chatgpt_plan_type*/ None,
)
.expect("build second ChatGPT auth");
let catalogs = [
(&config, &auth, &server, true, 3),
(&work, &auth, &server, false, 3),
(&config, &other_auth, &server, true, 2),
(&other_backend, &auth, &other_server, true, 1),
];
for (config, auth, server, enabled, calls) in catalogs {
Mock::given(method("GET"))
.and(path("/ps/plugins/suggested/codex"))
.and(header("chatgpt-account-id", auth.get_account_id().unwrap()))
.and(header(
"OAI-Product-Sku",
config.product_sku.as_deref().unwrap_or("codex"),
))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(serde_json::json!({"enabled": enabled, "plugins": []})),
)
.expect(calls)
.mount(server)
.await;
}
let read_catalogs = async || {
for (config, auth, _, enabled, _) in catalogs {
let expected = if enabled {
RecommendedPluginsMode::Endpoint {
plugins: Vec::new(),
}
} else {
RecommendedPluginsMode::Legacy
};
assert_eq!(
manager
.recommended_plugins_mode_for_config(config, Some(auth))
.await,
expected
);
}
};
read_catalogs().await;
manager.invalidate_recommendations_for_actors(&config.chatgpt_base_url, [&auth]);
read_catalogs().await;
// A local install can span two accounts if the user switches while the prompt is open.
manager.invalidate_recommendations_for_actors(&config.chatgpt_base_url, [&auth, &other_auth]);
read_catalogs().await;
}
#[tokio::test]
async fn actor_invalidation_rejects_stale_recommendation_fetch_completion() {
let tmp = tempfile::tempdir().unwrap();
write_file(
&tmp.path().join(CONFIG_TOML_FILE),
@@ -6546,7 +6562,7 @@ async fn recommended_plugins_cache_clear_rejects_stale_fetch_completion() {
mode = old_lookup.as_mut() => panic!("old fetch finished before release: {mode:?}"),
}
manager.clear_recommended_plugins_cache();
manager.invalidate_recommendations_for_actors(&config.chatgpt_base_url, [&auth]);
release_response
.send(())
.expect("release old HTTP response");
@@ -8137,6 +8153,7 @@ fn reconciliation_fences_refresh_publication_and_recovers_after_cancellation() {
stale_refresh_generation,
Vec::new(),
/*auth*/ None,
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(
InstalledPluginDisplayAssets::Include
),
@@ -8150,6 +8167,7 @@ fn reconciliation_fences_refresh_publication_and_recovers_after_cancellation() {
reconcile_generation,
vec![remote_installed_linear_plugin()],
/*auth*/ None,
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Reconcile,
)
.map(|update| update.metadata_change_id.is_some()),
@@ -8175,6 +8193,7 @@ fn reconciliation_fences_refresh_publication_and_recovers_after_cancellation() {
refresh_generation,
vec![remote_installed_plugin("beta")],
/*auth*/ None,
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Refresh(
InstalledPluginDisplayAssets::Include
),
@@ -8212,6 +8231,7 @@ fn reconciliation_fences_refresh_publication_and_recovers_after_cancellation() {
retry_generation,
vec![remote_installed_plugin("beta")],
/*auth*/ None,
/*chatgpt_base_url*/ None,
RemoteInstalledPluginsCachePublication::Reconcile,
)
.map(|update| update.metadata_change_id.is_some()),
+5 -33
View File
@@ -117,7 +117,6 @@ const REMOTE_INSTALLED_PLUGINS_FETCH_TIMEOUT: Duration = Duration::from_secs(60)
// Allow 20,000 installed plugins without unbounded cursor history.
const MAX_REMOTE_INSTALLED_PLUGIN_PAGES: usize = 100;
const RECOMMENDED_PLUGINS_TIMEOUT: Duration = Duration::from_secs(5);
const TOOLING_STATE_TIMEOUT: Duration = Duration::from_millis(250);
const REMOTE_PLUGIN_LIST_PAGE_LIMIT: u32 = 200;
const MAX_RECOMMENDED_PLUGINS: usize = 50;
const MAX_RECOMMENDED_PLUGIN_NAME_LEN: usize = 64;
@@ -491,10 +490,13 @@ pub struct RecommendedPlugin {
pub display_name: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum RecommendedPluginsMode {
#[default]
Legacy,
Endpoint { plugins: Vec<RecommendedPlugin> },
Endpoint {
plugins: Vec<RecommendedPlugin>,
},
}
pub fn is_valid_remote_plugin_id(plugin_id: &str) -> bool {
@@ -905,11 +907,6 @@ struct RemotePluginListResponse {
pagination: RemotePluginPagination,
}
#[derive(Deserialize)]
struct PluginToolingStateResponse {
tooling_changed_at: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
struct RecommendedPluginsResponse {
enabled: bool,
@@ -1160,31 +1157,6 @@ pub fn invalidate_cached_remote_plugin_catalog_scopes(
}
}
#[instrument(
name = "plugins.recommendations.read_tooling_marker",
level = "info",
skip_all
)]
pub(crate) async fn fetch_tooling_changed_at(
config: &RemotePluginServiceConfig,
auth: Option<&CodexAuth>,
) -> Result<Option<String>, RemotePluginCatalogError> {
let auth = ensure_chatgpt_auth(auth)?;
let base_url = config.chatgpt_base_url.trim_end_matches('/');
let url = format!("{base_url}/ps/plugins/tooling-state/turtle");
let poll = async {
let request = authenticated_request(config, Method::GET, &url, auth).await?;
let response: PluginToolingStateResponse = send_and_decode(request, &url).await?;
Ok(response.tooling_changed_at)
};
tokio::time::timeout(TOOLING_STATE_TIMEOUT, poll)
.await
.unwrap_or_else(|_| {
tracing::debug!("timed out reading recommendation tooling state");
Ok(None)
})
}
#[instrument(
name = "plugins.recommendations.fetch_catalog",
level = "info",
@@ -228,50 +228,6 @@ async fn plugin_runtime_auth_shares_registration_and_strips_cross_origin_auth()
}
}
#[tokio::test]
async fn tooling_state_deadline_includes_pending_runtime_registration() {
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0))
.await
.unwrap();
let base = format!("http://{}", listener.local_addr().unwrap());
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
let runtime = Arc::new(RuntimeAuthManager::new(
AuthManager::from_auth_for_testing(auth.clone()),
AgentHostAuthorization::InstallationId {
installation_id: Uuid::nil(),
},
RuntimeRegistrationMetadata {
harness: Some("codex".into()),
harness_version: None,
role: RuntimeRole::Orchestrator,
},
format!("{base}/register").parse().unwrap(),
));
let config = RemotePluginServiceConfig::new_with_selected_runtime_auth(
format!("{base}/backend-api"),
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
/*product_sku*/ None,
(
Arc::new(std::sync::OnceLock::from(Arc::clone(&runtime))),
RuntimeAuthMode::DualHeader,
),
);
// Hold registration open before starting the marker clock.
let registration = runtime.auth();
tokio::pin!(registration);
let (_connection, _) = tokio::select! {
connection = listener.accept() => connection.unwrap(),
_ = &mut registration => panic!("registration finished before receiving a response"),
};
tokio::time::pause();
let poll = fetch_tooling_changed_at(&config, Some(&auth));
let marker = tokio::time::timeout(TOOLING_STATE_TIMEOUT * 2, poll)
.await
.expect("the marker deadline must include runtime registration")
.unwrap();
assert_eq!(marker, None);
}
#[tokio::test]
async fn custom_plugin_backend_keeps_legacy_auth_with_runtime_enabled() {
for mode in [RuntimeAuthMode::DualHeader, RuntimeAuthMode::AssertionOnly] {