mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
## 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
94 lines
3.2 KiB
Rust
94 lines
3.2 KiB
Rust
//! Best-effort MCP prewarming.
|
|
//!
|
|
//! A bounded channel coalesces refresh requests. The worker only prepares the
|
|
//! newest thread state; exact model steps remain the correctness path.
|
|
|
|
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();
|
|
self.request_mcp_runtime_reprojection(reason);
|
|
}
|
|
|
|
/// Reproject contributor state without invalidating cached MCP resources.
|
|
pub(crate) fn request_mcp_runtime_reprojection(&self, reason: McpRefreshReason) {
|
|
self.mark_mcp_runtime_dirty(reason);
|
|
self.schedule_mcp_prewarm();
|
|
}
|
|
|
|
pub(super) fn start_mcp_prewarm_worker(
|
|
self: &Arc<Self>,
|
|
requests: async_channel::Receiver<()>,
|
|
mut auth_changes: tokio::sync::watch::Receiver<u64>,
|
|
) {
|
|
let session = Arc::downgrade(self);
|
|
let shutdown = self.mcp_prewarm_shutdown.clone();
|
|
let worker = self.services.runtime_handle.spawn(async move {
|
|
loop {
|
|
let auth_changed = tokio::select! {
|
|
biased;
|
|
_ = shutdown.cancelled() => break,
|
|
request = requests.recv() => {
|
|
if request.is_err() {
|
|
break;
|
|
}
|
|
false
|
|
},
|
|
auth_change = auth_changes.changed() => {
|
|
if auth_change.is_err() {
|
|
break;
|
|
}
|
|
true
|
|
},
|
|
};
|
|
let Some(session) = session.upgrade() else {
|
|
break;
|
|
};
|
|
if auth_changed {
|
|
session.mark_mcp_runtime_dirty(McpRefreshReason::AuthChangeNotification);
|
|
}
|
|
tokio::select! {
|
|
biased;
|
|
_ = shutdown.cancelled() => break,
|
|
_ = session.refresh_mcp_if_dirty() => {},
|
|
}
|
|
}
|
|
});
|
|
*self
|
|
.mcp_prewarm_task
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(worker);
|
|
}
|
|
|
|
pub(super) fn schedule_mcp_prewarm(&self) {
|
|
let _ = self.mcp_prewarm_tx.try_send(());
|
|
}
|
|
|
|
pub(super) async fn stop_mcp_prewarm_worker(&self) {
|
|
self.mcp_prewarm_shutdown.cancel();
|
|
let worker = self
|
|
.mcp_prewarm_task
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
.take();
|
|
if let Some(worker) = worker
|
|
&& let Err(error) = worker.await
|
|
{
|
|
warn!(%error, "MCP prewarm worker stopped unexpectedly");
|
|
}
|
|
}
|
|
}
|