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
@@ -187,16 +187,22 @@ impl RequestPluginInstallHandler {
"{argument_name} must match one of {source}"
))
})?;
let install_auth = match &tool {
DiscoverableTool::Plugin(plugin) if plugin.remote_plugin_id.is_some() => {
session.services.auth_manager.auth().await
}
DiscoverableTool::Plugin(_) | DiscoverableTool::Connector(_) => None,
};
let install_auth = install_auth.as_ref();
let tool = if self.presentation == ToolSuggestPresentation::RecommendationContext {
let plugin_id = tool.id().to_string();
let auth = session.services.auth_manager.auth().await;
let plugins_config = turn.config.plugins_config_input();
match codex_core_plugins::hydrate_selected_recommended_plugin_install_metadata(
&session
.services
.plugins_manager
.remote_plugin_service_config(&plugins_config),
auth.as_ref(),
install_auth,
tool,
)
.await
@@ -267,10 +273,8 @@ impl RequestPluginInstallHandler {
.as_ref()
.is_some_and(|response| response.action == ElicitationAction::Accept);
let auth = session.services.auth_manager.auth().await;
let completed = if user_confirmed {
verify_request_plugin_install_completed(&session, &turn, mcp, &tool, auth.as_ref())
.await
verify_request_plugin_install_completed(&session, &turn, mcp, &tool, install_auth).await
} else {
false
};
@@ -409,8 +413,10 @@ async fn verify_request_plugin_install_completed(
turn: &crate::session::turn_context::TurnContext,
mcp: &codex_mcp::McpBinding,
tool: &DiscoverableTool,
auth: Option<&codex_login::CodexAuth>,
install_auth: Option<&codex_login::CodexAuth>,
) -> bool {
let current_auth = session.services.auth_manager.auth().await;
let auth = current_auth.as_ref();
match tool {
DiscoverableTool::Connector(connector) => refresh_missing_requested_connectors(
session,
@@ -425,7 +431,15 @@ async fn verify_request_plugin_install_completed(
verified_connector_install_completed(connector.id.as_str(), &accessible_connectors)
}),
DiscoverableTool::Plugin(plugin) => {
if is_remote_plugin_install_suggestion(&plugin.id) {
if plugin.remote_plugin_id.is_some() {
// The client can switch accounts while the install prompt is open.
session
.services
.plugins_manager
.invalidate_recommendations_for_actors(
&turn.config.chatgpt_base_url,
install_auth.into_iter().chain(auth),
);
let (_, accessible_connectors) = tokio::join!(
refresh_remote_installed_plugins_cache_after_install(
session,
@@ -494,12 +508,6 @@ async fn refresh_remote_installed_plugins_cache_after_install(
}
}
fn is_remote_plugin_install_suggestion(plugin_id: &str) -> bool {
plugin_id
.rsplit_once('@')
.is_some_and(|(_, marketplace_name)| marketplace_name == REMOTE_GLOBAL_MARKETPLACE_NAME)
}
async fn refresh_missing_requested_connectors(
session: &Arc<crate::session::session::Session>,
turn: &crate::session::turn_context::TurnContext,
@@ -126,17 +126,6 @@ async fn verified_plugin_install_completed_requires_installed_plugin() {
));
}
#[test]
fn remote_plugin_install_suggestions_skip_core_installed_verification() {
assert!(is_remote_plugin_install_suggestion(
"snowflake@openai-curated-remote"
));
assert!(!is_remote_plugin_install_suggestion(
"snowflake@openai-curated"
));
assert!(!is_remote_plugin_install_suggestion("Plugin_123"));
}
#[test]
fn recommended_plugin_install_args_accept_legacy_tool_id() {
let current: RecommendedPluginInstallArgs = serde_json::from_value(json!({
@@ -389,17 +389,6 @@ async fn mount_remote_calendar_recommendation(
server: &wiremock::MockServer,
installed: Arc<AtomicBool>,
) {
let installed_for_marker = Arc::clone(&installed);
Mock::given(method("GET"))
.and(path("/ps/plugins/tooling-state/turtle"))
.respond_with(move |_: &wiremock::Request| {
let marker = installed_for_marker
.load(Ordering::SeqCst)
.then_some("1700000000000");
ResponseTemplate::new(200).set_body_json(json!({ "tooling_changed_at": marker }))
})
.mount(server)
.await;
Mock::given(method("GET"))
.and(path("/ps/plugins/suggested/codex"))
.and(query_param("scope", "GLOBAL"))
@@ -1664,8 +1653,6 @@ async fn run_remote_plugin_install_refresh_case(refreshed_tools: RefreshedAppsTo
.codex
.refresh_runtime_config(current_config, test.config.clone())
.await;
// Let the one-second marker poll window expire before the next turn.
tokio::time::sleep(Duration::from_secs(1)).await;
test.submit_turn("check whether Calendar is still installed")
.await?;
let requests = mock.requests();