Enforce required environment skills before model inference (#51157)

## What changed

- Accept per-environment `skills.required` lists through `environment/add` and `environments.toml`.
- Before inference, require each named skill to be enabled and supplied by the selected environment. Missing skills fail the turn before a model request, including when environment setup fails.
- Stop applying an environment's requirements after it is deselected. Exempt isolated Guardian reviewers, which have no skill catalog.

## Testing

Add tests for available, missing, disabled, and wrong-environment skills, failed environment setup, recovery after deselection, and propagation of TOML requirements to the correct environment.

GitOrigin-RevId: 302ced4774697d2f414f745fe0535e71f747eda2
This commit is contained in:
Adam Perry @ OpenAI
2026-10-05 18:54:17 +00:00
committed by copyberry
parent c9253c4977
commit 42312d4ff4
33 changed files with 615 additions and 78 deletions
@@ -494,7 +494,10 @@ fn environment_states(snapshot: &TurnEnvironmentSnapshot) -> BTreeMap<String, En
const MAX_TOTAL_ERROR_BYTES: usize = 512;
let mut remaining_error_bytes = MAX_TOTAL_ERROR_BYTES;
for environment in &snapshot.environments {
if let TurnEnvironmentState::Failed { selection, error } = environment {
if let TurnEnvironmentState::Failed {
selection, error, ..
} = environment
{
let detail = error
[..error.floor_char_boundary(remaining_error_bytes.min(MAX_ERROR_BYTES))]
.to_string();
@@ -424,6 +424,7 @@ fn failure_context_limits_total_detail_bytes_at_utf8_boundaries() {
let snapshot = TurnEnvironmentSnapshot {
environments: (0..4)
.map(|index| TurnEnvironmentState::Failed {
required_skills: Vec::new(),
selection: TurnEnvironmentSelection {
environment_id: format!("remote-{index}"),
cwd: PathUri::parse("file:///workspace").unwrap(),
@@ -27,6 +27,7 @@ use codex_protocol::protocol::EnvironmentConnectionEvent;
use codex_protocol::protocol::Event;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::TurnEnvironmentSelection;
use codex_skills_extension::EnvironmentSkillRequirements;
use codex_utils_absolute_path::AbsolutePathBuf;
use codex_utils_path_uri::PathUri;
use futures::FutureExt;
@@ -982,6 +983,7 @@ pub(crate) enum TurnEnvironmentState {
Failed {
// Keep the input form so connection failure doesn't hide whether config comes from the thread.
selection: TurnEnvironmentSelection,
required_skills: Vec<String>,
error: String,
},
}
@@ -991,9 +993,11 @@ impl TurnEnvironmentState {
starting: StartingTurnEnvironment,
resolved: Option<TurnEnvironmentResult>,
) -> Self {
let required_skills = starting.environment.required_skills().to_vec();
if let EnvironmentConfigState::Failed(error) = &starting.selection.config {
let error = error.clone();
return Self::Failed {
required_skills,
selection: starting
.config_origin
.into_input_selection(starting.selection),
@@ -1006,6 +1010,7 @@ impl TurnEnvironmentState {
if matches!(selection.config, EnvironmentConfigState::Pending) {
let Some(config) = environment.installed_config else {
return Self::Failed {
required_skills,
selection: starting.config_origin.into_input_selection(selection),
error: "Environment configuration was not supplied.".to_string(),
};
@@ -1035,6 +1040,7 @@ impl TurnEnvironmentState {
"skipping failed turn environment: {err}"
);
Self::Failed {
required_skills,
selection: starting
.config_origin
.into_input_selection(starting.selection),
@@ -1211,6 +1217,35 @@ impl TurnEnvironmentSnapshot {
.collect()
}
/// Captures requirements from the selected registration even when connection setup fails.
pub(crate) fn required_skills(&self) -> impl Iterator<Item = EnvironmentSkillRequirements<'_>> {
self.environments.iter().filter_map(|environment| {
let (selection, required) = match environment {
TurnEnvironmentState::Ready(environment) => (
&environment.selection,
environment.environment.required_skills(),
),
TurnEnvironmentState::Starting(environment) => (
&environment.selection,
environment.environment.required_skills(),
),
TurnEnvironmentState::Failed {
selection,
required_skills,
..
} => (selection, required_skills.as_slice()),
};
if required.is_empty() {
None
} else {
Some(EnvironmentSkillRequirements {
environment_id: &selection.environment_id,
skill_names: required,
})
}
})
}
/// Native children retain ready and starting attachments captured by their spawning step.
pub(crate) fn inheritable_selections(&self) -> Vec<TurnEnvironmentSelection> {
self.environments
+1
View File
@@ -747,6 +747,7 @@ async fn guardian_mcp_uses_thread_permissions_for_an_unavailable_captured_enviro
selection.config = codex_protocol::protocol::EnvironmentConfigState::Failed("offline".into());
let captured = crate::environment_selection::TurnEnvironmentSnapshot {
environments: vec![TurnEnvironmentState::Failed {
required_skills: Vec::new(),
selection,
error: "offline".into(),
}],
+11
View File
@@ -115,6 +115,7 @@ use codex_skills::collect_explicit_skill_mentions;
use codex_skills::tool_kind_for_path;
use codex_skills_extension::HostSkillPrompts;
use codex_skills_extension::InjectedHostSkillPrompts;
use codex_skills_extension::validate_required_skills;
use codex_thread_store::PersistContext;
use codex_tools::DiscoverableTool;
use codex_tools::ToolName;
@@ -508,6 +509,16 @@ pub(crate) async fn run_turn(
.record_step_world_state_if_changed(step_context.as_ref())
.await?;
// Isolated Guardian reviewers deliberately have no skill catalog.
if !crate::guardian::is_basic_session_source(&turn_context.session_source) {
validate_required_skills(
&sess.services.thread_extension_data,
&turn_context.extension_data,
step_context.environments.required_skills(),
)
.map_err(CodexErr::Fatal)?;
}
// Keep the override after accepted input so history truncation removes them together.
sess.record_reasoning_effort_override(step_context.as_ref())
.await;
@@ -135,11 +135,9 @@ impl ToolExecutor<ToolInvocation> for WaitForEnvironmentHandler {
.environments
.iter()
.find_map(|state| match state {
TurnEnvironmentState::Failed { selection, error }
if selection.environment_id == environment_id =>
{
Some(error)
}
TurnEnvironmentState::Failed {
selection, error, ..
} if selection.environment_id == environment_id => Some(error),
_ => None,
})
{