Reuse executor Discovery V2 inventories across turns (#62640)

## Why

Starting a turn discarded the discovery cache and forced MCP to rebuild
from a fresh inventory even when the executor context was unchanged.
An MCP refresh could also discover a different inventory from the one
the step had already captured.

## What changed

Keep a per-thread cache keyed by environment, executor session, working
directory, and sandbox. Explicit invalidation reloads discovery on the
next lookup and lets in-flight readers finish with their captured results.

Use the step's captured inventory for MCP reconciliation and bind it
with the current MCP policy. Apply the binding's plugin policy when
selecting skills and hooks.

## Testing

Cover cache reuse across turns, lazy invalidation during in-flight
discovery, and MCP refresh with a captured inventory after a server
has been revoked.

GitOrigin-RevId: 8d4087dffdb3d6feef2456918a08ae7724cf18e4
This commit is contained in:
Anthony Tafoya
2026-11-20 01:48:00 +00:00
committed by copyberry
parent 9224da4ce1
commit e720e6c80b
15 changed files with 841 additions and 366 deletions
-14
View File
@@ -5,14 +5,12 @@ mod regular;
mod review;
mod user_shell;
use crate::McpRefreshReason;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use codex_diagnostics::Gauge;
use codex_exec_server::ExecutorCapabilityDiscoveryV2Cache;
use codex_extension_api::ThreadIdleCause;
use futures::future::BoxFuture;
use tokio::sync::Mutex;
@@ -321,18 +319,6 @@ impl Session {
input: Vec<TurnInput>,
task: T,
) -> TurnInputSubmission {
// Replace the cache; in-flight acquisitions retain their previous Arc.
self.services
.thread_extension_data
.insert(ExecutorCapabilityDiscoveryV2Cache::default());
if turn_context
.config
.features
.enabled(Feature::ExecutorCapabilityDiscoveryV2)
{
// MCP contributors must project the new inventory even if their other inputs match.
self.request_mcp_runtime_reprojection(McpRefreshReason::TaskStartedExecutorDiscovery);
}
self.activate_plugin_selection(&turn_context).await;
// Inherited or recovered roots are applied before task start. Otherwise this
// task owns its turn, including background work. Later mail cannot change it.