mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
Add turn-start cloud plugin discovery to the MCP extension (#46572)
## What changed - Support an optional cloud provider alongside executor plugins, refreshing its thread-scoped catalog at each regular turn when plugins are enabled. - Use cloud connector metadata for app attribution without starting cloud-declared MCP servers. - Scope cached catalogs to authentication and Apps availability. Retain the previous catalog on discovery failure within the same scope, clear stale catalogs on auth changes, and reject results completed under outdated credentials. - Add a cancellable regular-task startup phase that reconciles MCP before discovery and reapplies contributor state afterward without invalidating resource caches. ## Testing Add regression coverage for per-turn refresh, empty catalogs, discovery failures, plugin disabling, auth changes during discovery, MCP reconciliation and cancellation, and cloud app attribution alongside executor MCP tools. GitOrigin-RevId: 8469eb109ea316e21cbc4122c52ad8000caeef03
This commit is contained in:
committed by
copyberry
parent
d84ebff592
commit
394dadb93d
@@ -82,6 +82,7 @@ impl<'a> McpEnvironmentScope<'a> {
|
||||
}
|
||||
|
||||
pub(crate) struct McpThreadIdentity<'a> {
|
||||
pub(crate) auth_changed: bool,
|
||||
pub(crate) session_source: &'a SessionSource,
|
||||
pub(crate) originator: &'a str,
|
||||
pub(crate) environments: McpEnvironmentScope<'a>,
|
||||
@@ -171,7 +172,8 @@ impl McpManager {
|
||||
ready_selected_capability_roots,
|
||||
executor_capability_discovery,
|
||||
)
|
||||
.with_session_source(identity.session_source),
|
||||
.with_session_source(identity.session_source)
|
||||
.with_auth_changed(identity.auth_changed),
|
||||
Some(identity.originator),
|
||||
identity.environments,
|
||||
identity.disabled_plugin_ids,
|
||||
|
||||
@@ -121,23 +121,26 @@ impl Session {
|
||||
&environments,
|
||||
)
|
||||
.await;
|
||||
let mcp_projection = self
|
||||
.services
|
||||
.mcp_manager
|
||||
.runtime_config_for_step(
|
||||
config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
session_source: &session_source,
|
||||
originator: &originator,
|
||||
disabled_plugin_ids: &disabled_plugin_ids,
|
||||
environments: McpEnvironmentScope::Selected(&environment_selections),
|
||||
},
|
||||
&ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
)
|
||||
.await;
|
||||
let mcp_projection =
|
||||
self.services
|
||||
.mcp_manager
|
||||
.runtime_config_for_step(
|
||||
config,
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self.services.mcp_runtime.current_auth_matches(
|
||||
self.services.auth_manager.auth_cached().as_ref(),
|
||||
),
|
||||
session_source: &session_source,
|
||||
originator: &originator,
|
||||
disabled_plugin_ids: &disabled_plugin_ids,
|
||||
environments: McpEnvironmentScope::Selected(&environment_selections),
|
||||
},
|
||||
&ready_selected_capability_roots,
|
||||
executor_capability_discovery.as_deref(),
|
||||
)
|
||||
.await;
|
||||
let mcp_config = self
|
||||
.project_selected_environment_mcp_servers(config, &environments, mcp_projection)
|
||||
.await
|
||||
@@ -219,6 +222,10 @@ impl Session {
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &desired.session_source,
|
||||
originator: &desired.originator,
|
||||
disabled_plugin_ids: &desired.disabled_plugin_ids,
|
||||
@@ -288,6 +295,10 @@ impl Session {
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &desired.session_source,
|
||||
originator: &desired.originator,
|
||||
disabled_plugin_ids: &desired.disabled_plugin_ids,
|
||||
@@ -688,6 +699,10 @@ impl Session {
|
||||
&self.services.mcp_thread_init,
|
||||
&self.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !self
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(desired.auth.as_ref()),
|
||||
session_source: &turn_context.session_source,
|
||||
originator: &turn_context.originator,
|
||||
disabled_plugin_ids: &disabled_plugin_ids,
|
||||
|
||||
@@ -9,6 +9,11 @@ impl Session {
|
||||
pub(crate) fn request_mcp_runtime_refresh(&self) {
|
||||
// Plugin changes can reuse connections but still change their skill resources.
|
||||
self.services.mcp_runtime.invalidate_resource_caches();
|
||||
self.request_mcp_runtime_reprojection();
|
||||
}
|
||||
|
||||
/// Reproject contributor state without invalidating cached MCP resources.
|
||||
pub(crate) fn request_mcp_runtime_reprojection(&self) {
|
||||
self.mark_mcp_runtime_dirty();
|
||||
self.schedule_mcp_prewarm();
|
||||
}
|
||||
|
||||
@@ -1125,6 +1125,7 @@ impl Session {
|
||||
mcp_thread_init_for_startup,
|
||||
thread_extension_data_for_mcp,
|
||||
McpThreadIdentity {
|
||||
auth_changed: false,
|
||||
session_source: &mcp_session_source,
|
||||
originator: &mcp_originator,
|
||||
disabled_plugin_ids: &mcp_disabled_plugin_ids,
|
||||
@@ -1818,6 +1819,10 @@ impl Session {
|
||||
&sess.services.mcp_thread_init,
|
||||
&sess.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: !sess
|
||||
.services
|
||||
.mcp_runtime
|
||||
.current_auth_matches(latest_auth.as_ref()),
|
||||
session_source: &session_configuration.session_source,
|
||||
originator: &session_configuration.originator,
|
||||
disabled_plugin_ids: &session_configuration.disabled_plugin_ids,
|
||||
|
||||
@@ -3,6 +3,9 @@ mod notification_tests;
|
||||
|
||||
use super::mcp_refresh::McpRefresh;
|
||||
use super::step_context::StepInputs;
|
||||
|
||||
#[path = "turn_start_mcp_tests.rs"]
|
||||
mod turn_start_mcp_tests;
|
||||
use super::step_settings::ResolvedStepSettings;
|
||||
use super::step_settings::StepSettings;
|
||||
use super::step_settings::StepSettingsUpdate;
|
||||
@@ -3285,7 +3288,7 @@ async fn turn_start_lifecycle_exposes_turn_metadata_and_token_baseline() {
|
||||
turn_level_id: String,
|
||||
turn_id: String,
|
||||
collaboration_mode: CollaborationMode,
|
||||
token_usage_at_turn_start: TokenUsage,
|
||||
token_usage_at_turn_start: Option<TokenUsage>,
|
||||
saw_session_store: bool,
|
||||
saw_thread_store: bool,
|
||||
}
|
||||
@@ -3309,7 +3312,7 @@ async fn turn_start_lifecycle_exposes_turn_metadata_and_token_baseline() {
|
||||
turn_level_id: input.turn_store.level_id().to_string(),
|
||||
turn_id: input.turn_id.to_string(),
|
||||
collaboration_mode: input.collaboration_mode.clone(),
|
||||
token_usage_at_turn_start: input.token_usage_at_turn_start.clone(),
|
||||
token_usage_at_turn_start: input.token_usage_at_turn_start.cloned(),
|
||||
saw_session_store: input
|
||||
.session_store
|
||||
.get::<SessionTurnStartMarker>()
|
||||
@@ -3356,7 +3359,7 @@ async fn turn_start_lifecycle_exposes_turn_metadata_and_token_baseline() {
|
||||
turn_level_id: turn_context.sub_id.clone(),
|
||||
turn_id: turn_context.sub_id.clone(),
|
||||
collaboration_mode: turn_context.collaboration_mode(),
|
||||
token_usage_at_turn_start,
|
||||
token_usage_at_turn_start: Some(token_usage_at_turn_start),
|
||||
saw_session_store: true,
|
||||
saw_thread_store: true,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
//! Turn-start MCP reconciliation and cooperative discovery cancellation regressions.
|
||||
|
||||
use super::*;
|
||||
use codex_mcp::McpResourceClient;
|
||||
use codex_mcp::McpResourceClientCacheKey;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
struct Recorder {
|
||||
client: McpResourceClient,
|
||||
requires_mcp: std::sync::atomic::AtomicBool,
|
||||
keys: std::sync::Mutex<Vec<McpResourceClientCacheKey>>,
|
||||
discovered: tokio::sync::Notify,
|
||||
}
|
||||
|
||||
impl codex_extension_api::TurnLifecycleContributor for Recorder {
|
||||
fn turn_start_phase(
|
||||
&self,
|
||||
_thread_store: &codex_extension_api::ExtensionData,
|
||||
) -> codex_extension_api::TurnStartPhase {
|
||||
codex_extension_api::TurnStartPhase::RegularTaskStart
|
||||
}
|
||||
|
||||
fn requires_mcp_runtime(&self, _thread_store: &codex_extension_api::ExtensionData) -> bool {
|
||||
self.requires_mcp.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
|
||||
fn on_turn_start<'a>(
|
||||
&'a self,
|
||||
_input: codex_extension_api::TurnStartInput<'a>,
|
||||
) -> codex_extension_api::ExtensionFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
self.keys
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.push(self.client.cache_key());
|
||||
self.discovered.notify_one();
|
||||
std::future::pending::<()>().await;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn turn_start_refreshes_dirty_mcp_reuses_clean_runtime_and_cancels_discovery() {
|
||||
let (mut session, turn_context) = make_session_and_context().await;
|
||||
let recorder = Arc::new(Recorder {
|
||||
client: McpResourceClient::new(Arc::clone(&session.services.mcp_runtime)),
|
||||
requires_mcp: Default::default(),
|
||||
keys: Default::default(),
|
||||
discovered: Default::default(),
|
||||
});
|
||||
let initial_key = recorder.client.cache_key();
|
||||
let mut builder = codex_extension_api::ExtensionRegistryBuilder::new();
|
||||
builder.turn_lifecycle_contributor(recorder.clone());
|
||||
session.services.extensions = Arc::new(builder.build());
|
||||
let session = Arc::new(session);
|
||||
let turn_context = Arc::new(turn_context);
|
||||
|
||||
for turn in 0..4 {
|
||||
recorder
|
||||
.requires_mcp
|
||||
.store(turn > 0, std::sync::atomic::Ordering::Relaxed);
|
||||
if turn == 0 {
|
||||
session.mark_mcp_runtime_dirty();
|
||||
} else if turn == 3 {
|
||||
session.services.mcp_runtime.reconnect_on_next_refresh();
|
||||
session.request_mcp_runtime_refresh();
|
||||
}
|
||||
session
|
||||
.spawn_task(
|
||||
Arc::clone(&turn_context),
|
||||
Vec::new(),
|
||||
crate::tasks::RegularTask::new(),
|
||||
)
|
||||
.await;
|
||||
tokio::time::timeout(
|
||||
std::time::Duration::from_secs(5),
|
||||
recorder.discovered.notified(),
|
||||
)
|
||||
.await
|
||||
.expect("regular-task contributor should run");
|
||||
assert_eq!(session.mcp_refresh.is_pending(), turn == 0);
|
||||
assert_eq!(
|
||||
recorder
|
||||
.keys
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.len(),
|
||||
turn + 1
|
||||
);
|
||||
if turn > 0 {
|
||||
session.refresh_mcp_if_dirty().await;
|
||||
assert!(
|
||||
recorder.client.cache_key()
|
||||
== recorder
|
||||
.keys
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)[turn],
|
||||
"a clean refresh must reuse the published MCP runtime"
|
||||
);
|
||||
}
|
||||
let (cancellation_token, done) = {
|
||||
let active_turn = session.active_turn.lock().await;
|
||||
let task = active_turn
|
||||
.as_ref()
|
||||
.and_then(|turn| turn.task.as_ref())
|
||||
.expect("discovery should run inside the registered task");
|
||||
(task.cancellation_token.clone(), Arc::clone(&task.done))
|
||||
};
|
||||
let completed = done.notified();
|
||||
tokio::pin!(completed);
|
||||
completed.as_mut().enable();
|
||||
cancellation_token.cancel();
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), completed)
|
||||
.await
|
||||
.expect("discovery cancellation must finish the task without forced abort");
|
||||
assert!(
|
||||
session.mcp_refresh.is_pending(),
|
||||
"cancelled discovery must request an MCP refresh before the task completes"
|
||||
);
|
||||
session.abort_all_tasks(TurnAbortReason::Interrupted).await;
|
||||
}
|
||||
let keys = recorder
|
||||
.keys
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
assert!(
|
||||
keys[0] == initial_key,
|
||||
"unrelated contributors must not reconcile MCP"
|
||||
);
|
||||
assert!(
|
||||
keys[1] != initial_key,
|
||||
"discovery must observe the reconciled generation"
|
||||
);
|
||||
assert!(
|
||||
keys[2] != keys[1],
|
||||
"cancelled cloud preparation must request reprojection on the next turn"
|
||||
);
|
||||
assert!(
|
||||
keys[3] != keys[2],
|
||||
"the next turn must consume a pending reconnect before discovery"
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_extension_api::ExtensionData;
|
||||
use codex_extension_api::ThreadIdleCause;
|
||||
use codex_extension_api::TurnStartPhase;
|
||||
use codex_protocol::protocol::CodexErrorInfo;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
use codex_protocol::protocol::TurnAbortReason;
|
||||
@@ -9,12 +12,21 @@ use crate::session::turn_context::TurnContext;
|
||||
|
||||
impl Session {
|
||||
pub(super) async fn emit_turn_start_lifecycle(
|
||||
&self,
|
||||
self: &Arc<Self>,
|
||||
turn_context: &TurnContext,
|
||||
token_usage_at_turn_start: &TokenUsage,
|
||||
token_usage_at_turn_start: Option<&TokenUsage>,
|
||||
phase: TurnStartPhase,
|
||||
) {
|
||||
let collaboration_mode = turn_context.collaboration_mode();
|
||||
for contributor in self.services.extensions.turn_lifecycle_contributors() {
|
||||
if contributor.turn_start_phase(&self.services.thread_extension_data) != phase {
|
||||
continue;
|
||||
}
|
||||
if phase == TurnStartPhase::RegularTaskStart
|
||||
&& contributor.requires_mcp_runtime(&self.services.thread_extension_data)
|
||||
{
|
||||
self.refresh_mcp_if_dirty().await;
|
||||
}
|
||||
contributor
|
||||
.on_turn_start(codex_extension_api::TurnStartInput {
|
||||
turn_id: turn_context.sub_id.as_str(),
|
||||
|
||||
@@ -322,8 +322,12 @@ impl Session {
|
||||
self.input_queue
|
||||
.extend_pending_input_for_turn_state(turn_state.as_ref(), pending_items)
|
||||
.await;
|
||||
self.emit_turn_start_lifecycle(turn_context.as_ref(), &token_usage_at_turn_start)
|
||||
.await;
|
||||
self.emit_turn_start_lifecycle(
|
||||
turn_context.as_ref(),
|
||||
Some(&token_usage_at_turn_start),
|
||||
codex_extension_api::TurnStartPhase::BeforeTaskRegistration,
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut active = self.active_turn.lock().await;
|
||||
let turn = active.get_or_insert_with(ActiveTurn::default);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use codex_async_utils::OrCancelExt;
|
||||
use codex_extension_api::TurnStartPhase;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::session::TurnInput;
|
||||
@@ -47,6 +49,32 @@ impl SessionTask for RegularTask {
|
||||
// not wait on startup prewarm resolution.
|
||||
let prewarmed_client_session = async {
|
||||
sess.emit_turn_started(&ctx).await;
|
||||
// Regular-start contributors run once, after the task is visible and interruptible.
|
||||
let prepares_mcp = sess
|
||||
.services
|
||||
.extensions
|
||||
.turn_lifecycle_contributors()
|
||||
.iter()
|
||||
.any(|contributor| {
|
||||
contributor.turn_start_phase(&sess.services.thread_extension_data)
|
||||
== TurnStartPhase::RegularTaskStart
|
||||
&& contributor.requires_mcp_runtime(&sess.services.thread_extension_data)
|
||||
});
|
||||
let preparation = sess
|
||||
.emit_turn_start_lifecycle(
|
||||
&ctx,
|
||||
/*token_usage_at_turn_start*/ None,
|
||||
TurnStartPhase::RegularTaskStart,
|
||||
)
|
||||
.or_cancel(&cancellation_token)
|
||||
.await;
|
||||
// Even cancelled discovery may have cleared the previous account's catalog.
|
||||
if prepares_mcp {
|
||||
sess.request_mcp_runtime_reprojection();
|
||||
}
|
||||
if preparation.is_err() {
|
||||
return SessionStartupPrewarmResolution::Cancelled;
|
||||
}
|
||||
sess.set_server_reasoning_included(/*included*/ false).await;
|
||||
sess.consume_startup_prewarm_for_regular_turn(&cancellation_token)
|
||||
.await
|
||||
|
||||
@@ -1704,6 +1704,7 @@ async fn start_thread_seeds_extension_data_for_mcp_and_lifecycle_contributors()
|
||||
&first_session.services.mcp_thread_init,
|
||||
&first_session.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: false,
|
||||
session_source: &SessionSource::Exec,
|
||||
originator: &first_originator,
|
||||
disabled_plugin_ids: &[],
|
||||
@@ -1725,6 +1726,7 @@ async fn start_thread_seeds_extension_data_for_mcp_and_lifecycle_contributors()
|
||||
&second_session.services.mcp_thread_init,
|
||||
&second_session.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: false,
|
||||
session_source: &second_session_source,
|
||||
originator: &second_originator,
|
||||
disabled_plugin_ids: &[],
|
||||
@@ -1799,6 +1801,7 @@ async fn start_thread_seeds_extension_data_for_mcp_and_lifecycle_contributors()
|
||||
&first_session.services.mcp_thread_init,
|
||||
&first_session.services.thread_extension_data,
|
||||
McpThreadIdentity {
|
||||
auth_changed: false,
|
||||
session_source: &SessionSource::Exec,
|
||||
originator: &first_originator,
|
||||
disabled_plugin_ids: &disabled_plugin_ids,
|
||||
|
||||
Reference in New Issue
Block a user