Add deferred MCP reprojection for the next regular turn (#61356)

## What changed

Add `CodexThread::reproject_mcp_on_next_turn()` to request an MCP reprojection after `TurnStarted` and before tools are bound. Repeated requests coalesce, and idle reconciliation or prewarming leaves the request pending until the next regular turn.

## Testing

Add coverage for deferred requests surviving prewarm and idle reconciliation, coalescing into one reprojection, and exposing refreshed tools in the turn's first model request. Verify that discovery starts after `TurnStarted`.

GitOrigin-RevId: 260b2381013291a47eb9a7f736c129de76c5897f
This commit is contained in:
Krish Chainani
2026-11-13 18:52:09 +00:00
committed by copyberry
parent 76a8ca3914
commit ae12a9d645
7 changed files with 186 additions and 4 deletions
+6
View File
@@ -1276,6 +1276,12 @@ impl CodexThread {
self.session.hooks().list_with_environment_hooks(sources)
}
/// Rebuilds the MCP projection on the next regular turn, after turn start and before
/// tools are bound. Repeated requests coalesce without starting a background refresh.
pub fn reproject_mcp_on_next_turn(&self) {
self.session.reproject_mcp_on_next_turn();
}
/// Observes this thread's published MCP connections that match the requested config.
pub async fn mcp_connection_statuses(
&self,
+10
View File
@@ -7,6 +7,16 @@ use super::*;
use crate::McpRefreshReason;
impl Session {
pub(crate) fn reproject_mcp_on_next_turn(&self) {
self.mcp_refresh.request_for_next_turn();
}
pub(crate) fn start_deferred_mcp_reprojection(&self) {
if self.mcp_refresh.claim_for_turn_start() {
self.request_mcp_runtime_reprojection(McpRefreshReason::ExplicitInvalidation);
}
}
pub(crate) fn request_mcp_runtime_refresh(&self, reason: McpRefreshReason) {
// Plugin changes can reuse connections but still change their skill resources.
self.services.mcp_runtime.invalidate_resource_caches();
+13
View File
@@ -2,6 +2,8 @@ use crate::McpRefreshReason;
use codex_protocol::ThreadId;
use std::collections::BTreeSet;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use tokio::sync::AcquireError;
use tokio::sync::Semaphore;
use tokio::sync::SemaphorePermit;
@@ -10,6 +12,8 @@ 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>>,
// Kept separate from pending so prewarm cannot claim work reserved for a real turn.
on_next_turn: AtomicBool,
gate: Semaphore,
pub(super) apps_refreshes: TaskTracker,
}
@@ -21,11 +25,20 @@ impl McpRefresh {
apps_refreshes.close();
Self {
pending: Mutex::default(),
on_next_turn: AtomicBool::new(false),
gate: Semaphore::new(/*permits*/ 1),
apps_refreshes,
}
}
pub(super) fn request_for_next_turn(&self) {
self.on_next_turn.store(true, Ordering::Release);
}
pub(super) fn claim_for_turn_start(&self) -> bool {
self.on_next_turn.swap(false, Ordering::AcqRel)
}
pub(super) fn invalidate(&self, reason: McpRefreshReason) {
self.pending
.lock()
@@ -3,6 +3,21 @@
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn next_turn_reprojection_is_not_claimed_by_prewarm() {
let refresh = McpRefresh::new();
let thread_id = ThreadId::new();
refresh.request_for_next_turn();
refresh.request_for_next_turn();
assert!(refresh.claim(thread_id).is_none());
refresh.invalidate(McpRefreshReason::McpConfigReloaded);
let mut prewarm = refresh.claim(thread_id).expect("unrelated refresh");
prewarm.published = true;
drop(prewarm);
assert!(refresh.claim_for_turn_start());
assert!(!refresh.claim_for_turn_start());
}
#[test]
fn refresh_keeps_concurrent_invalidations_and_restores_unpublished_causes() {
let thread_id = ThreadId::new();
@@ -74,7 +74,7 @@ async fn turn_start_refreshes_dirty_mcp_reuses_clean_runtime_and_cancels_discove
.requires_mcp
.store(turn > 0, std::sync::atomic::Ordering::Relaxed);
if turn == 0 {
session.mark_mcp_runtime_dirty(McpRefreshReason::ExplicitInvalidation);
session.reproject_mcp_on_next_turn();
} else if turn == 3 {
session.services.mcp_runtime.reconnect_on_next_refresh();
session.request_mcp_runtime_refresh(McpRefreshReason::ExplicitInvalidation);
+1
View File
@@ -73,6 +73,7 @@ impl SessionTask for RegularTask {
// not wait on startup prewarm resolution.
let prewarmed_client_session = async {
sess.emit_turn_started(&ctx, TaskKind::Regular).await;
sess.start_deferred_mcp_reprojection();
// Regular-start contributors run once, after the task is visible and interruptible.
let prepares_mcp = sess
.services