Refresh Apps tools outside the MCP runtime gate (#60639)

## Why

Holding the session MCP gate during `tools/list` blocks other runtime access. Releasing it requires preventing stale refresh results from publishing after configuration or authentication changes.

## What changed

- Capture the Apps client under the session gate, then release the gate before fetching tools.
- Validate runtime and authentication state before publishing the shared cache and client catalog; retry a superseded refresh once.
- Wait for in-flight Apps refreshes before capturing model tools, preparing Apps calls, or shutting down the runtime.

## Testing

Add regression coverage for stale refreshes after runtime invalidation or logout, publication validation after call preparation drains, and a model step that waits for refreshed Apps tools while MCP status queries remain available.

GitOrigin-RevId: 42d4be8c4a9218933f8e3d07fc7f32f2da9cbd9d
This commit is contained in:
Dmitriy Kovalenko
2026-11-10 18:33:49 +00:00
committed by copyberry
parent 057d9140a8
commit 3f797555cd
13 changed files with 405 additions and 42 deletions
+2 -1
View File
@@ -358,8 +358,9 @@ pub(super) async fn shutdown_session_runtime(sess: &Arc<Session>) {
{
let _refresh = sess.mcp_refresh.acquire().await;
sess.mcp_refresh.close();
sess.services.mcp_runtime.shutdown().await;
}
sess.wait_for_apps_tool_refresh().await;
sess.services.mcp_runtime.shutdown().await;
sess.drain_code_mode_messages().await;
+47 -6
View File
@@ -3,6 +3,7 @@ use crate::McpRefreshReason;
use crate::environment_selection::TurnEnvironmentSnapshot;
use crate::environment_selection::combine_selected_capability_roots;
use crate::mcp_tool_call::McpToolApprovalContext;
use anyhow::Context;
use codex_exec_server::DiscoverV2CapabilitiesRequest;
use codex_exec_server::ExecutorCapabilityDiscoveryCache;
use codex_exec_server::ExecutorCapabilityDiscoverySnapshot;
@@ -38,6 +39,7 @@ use codex_protocol::openai_models::GuardianScope;
use codex_rmcp_client::Elicitation;
use rmcp::model::ElicitationAction;
use serde_json::Map;
use tokio::sync::SemaphorePermit;
const TOOL_SUGGESTION_ACTION_INSTALL: &str = "install";
const TOOL_SUGGESTION_ACTION_KEY: &str = "suggest_type";
@@ -194,6 +196,11 @@ impl Session {
error!("MCP runtime refresh semaphore closed");
return;
};
self.reconcile_mcp_runtime().await;
}
/// Reconcile pending state under the session permit.
async fn reconcile_mcp_runtime(self: &Arc<Self>) {
loop {
// Compare and rebuild from current environments, not choices saved for a future turn.
let environments = self.services.turn_environments.snapshot().await;
@@ -280,15 +287,47 @@ impl Session {
pub(crate) async fn refresh_codex_apps_tools(
self: &Arc<Self>,
) -> anyhow::Result<codex_mcp::CodexAppsToolSnapshot> {
// Reconcile unchanged config so failed or closed clients can be replaced.
self.mark_mcp_runtime_dirty(McpRefreshReason::CodexAppsToolRefresh);
self.refresh_mcp_if_dirty().await;
let _refresh = self
let permit = self
.mcp_refresh
.acquire()
.await
.map_err(|_| anyhow::anyhow!("MCP runtime refresh semaphore closed"))?;
self.services.mcp_runtime.refresh_codex_apps_tools().await
.context("MCP runtime refresh semaphore closed")?;
let _in_flight = self.mcp_refresh.apps_refreshes.token();
// Reconcile once per batch so a failed or closed client can be replaced.
if self.mcp_refresh.apps_refreshes.len() == 1 {
self.mark_mcp_runtime_dirty(McpRefreshReason::CodexAppsToolRefresh);
}
if let Some(snapshot) = self.fetch_apps_catalog(permit).await? {
return Ok(snapshot);
}
let permit = self
.mcp_refresh
.acquire()
.await
.context("MCP runtime refresh semaphore closed")?;
self.fetch_apps_catalog(permit)
.await?
.context("MCP runtime changed during both Apps tool refresh attempts")
}
async fn fetch_apps_catalog(
self: &Arc<Self>,
permit: SemaphorePermit<'_>,
) -> anyhow::Result<Option<codex_mcp::CodexAppsToolSnapshot>> {
self.reconcile_mcp_runtime().await;
let refresh = self.services.mcp_runtime.capture_apps_tool_refresh();
// An invalidation may arrive after reconciliation but before capture.
if self.mcp_refresh.is_pending() {
return Ok(None);
}
drop(permit);
refresh.await
}
/// Wait outside the session permit so a superseded refresh can retry.
#[tracing::instrument(name = "mcp.runtime.apps_catalog_wait", skip_all)]
pub(crate) async fn wait_for_apps_tool_refresh(&self) {
self.mcp_refresh.apps_refreshes.wait().await;
}
/// Reconnects the runtime so refreshed Apps tools belong to their new exact client.
@@ -379,6 +418,7 @@ impl Session {
pub(super) fn mark_mcp_runtime_dirty(&self, reason: McpRefreshReason) {
self.mcp_refresh.invalidate(reason);
self.services.mcp_runtime.invalidate_apps_tool_refresh();
}
#[tracing::instrument(
@@ -457,6 +497,7 @@ impl Session {
.chain(&recovered_oauth_servers)
.cloned()
.collect::<Vec<_>>();
self.wait_for_apps_tool_refresh().await;
if let Some(binding) = self
.services
.mcp_runtime
+6 -1
View File
@@ -5,18 +5,24 @@ use std::sync::Mutex;
use tokio::sync::AcquireError;
use tokio::sync::Semaphore;
use tokio::sync::SemaphorePermit;
use tokio_util::task::TaskTracker;
/// Owns MCP invalidation and the single gate used to publish runtime updates.
pub(super) struct McpRefresh {
pending: Mutex<BTreeSet<McpRefreshReason>>,
gate: Semaphore,
pub(super) apps_refreshes: TaskTracker,
}
impl McpRefresh {
pub(super) fn new() -> Self {
let apps_refreshes = TaskTracker::new();
// Closed trackers still accept tokens and wake waiters when empty.
apps_refreshes.close();
Self {
pending: Mutex::default(),
gate: Semaphore::new(/*permits*/ 1),
apps_refreshes,
}
}
@@ -27,7 +33,6 @@ impl McpRefresh {
.insert(reason);
}
#[cfg(test)]
pub(super) fn is_pending(&self) -> bool {
!self
.pending
+5
View File
@@ -62,6 +62,11 @@ impl Session {
advertised_tool: &ToolInfo,
) -> anyhow::Result<PreparedMcpCall> {
self.refresh_mcp_if_dirty().await;
// Apps refreshes fetch tools outside the MCP runtime lock. Wait for any
// in-flight refresh before capturing the catalog for this call.
if advertised_tool.server_name == CODEX_APPS_MCP_SERVER_NAME {
self.wait_for_apps_tool_refresh().await;
}
self.services
.mcp_runtime
.prepare_call(advertised_tool)