Make skills follow model-step state

This commit is contained in:
jif-oai
2026-06-25 17:41:49 +01:00
parent c6f639becb
commit 3a27647da3
30 changed files with 1246 additions and 524 deletions
+3 -20
View File
@@ -9,7 +9,6 @@ use codex_core::NewThread;
use codex_core::StartThreadOptions;
use codex_core::ThreadManager;
use codex_core::config::Config;
use codex_exec_server::EnvironmentManager;
use codex_extension_api::AgentSpawnFuture;
use codex_extension_api::AgentSpawner;
use codex_extension_api::ExtensionEventSink;
@@ -35,8 +34,6 @@ pub(crate) struct ThreadExtensionDependencies {
pub(crate) analytics_events_client: AnalyticsEventsClient,
pub(crate) thread_manager: Weak<ThreadManager>,
pub(crate) goal_service: Arc<GoalService>,
pub(crate) environment_manager: Arc<EnvironmentManager>,
pub(crate) executor_skill_provider: Arc<dyn codex_skills_extension::SkillProvider>,
/// Process-scoped persistence backend for extensions that need stored thread history.
pub(crate) thread_store: Arc<dyn ThreadStore>,
}
@@ -55,8 +52,6 @@ where
analytics_events_client,
thread_manager,
goal_service,
environment_manager,
executor_skill_provider,
thread_store: _thread_store,
} = dependencies;
let mut builder = ExtensionRegistryBuilder::<Config>::with_event_sink(event_sink);
@@ -74,25 +69,13 @@ where
codex_guardian::install(&mut builder, guardian_agent_spawner);
codex_memories_extension::install(&mut builder, codex_otel::global());
codex_mcp_extension::install(&mut builder);
codex_mcp_extension::install_executor_plugins(&mut builder, environment_manager);
codex_web_search_extension::install(&mut builder, auth_manager.clone());
codex_image_generation_extension::install(&mut builder, auth_manager, |config: &Config| {
Some(config.codex_home.clone())
});
let skill_providers = codex_skills_extension::SkillProviders::new()
.with_executor_provider(executor_skill_provider)
.with_orchestrator_provider(Arc::new(
codex_skills_extension::OrchestratorSkillProvider::new(),
));
codex_skills_extension::install_with_providers(
&mut builder,
skill_providers,
|config: &Config| codex_skills_extension::SkillsExtensionConfig {
include_instructions: config.include_skill_instructions,
bundled_skills_enabled: config.bundled_skills_enabled(),
orchestrator_skills_enabled: config.orchestrator_skills_enabled,
},
);
codex_skills_extension::install(&mut builder, |config: &Config| {
config.orchestrator_skills_enabled
});
Arc::new(builder.build())
}
-7
View File
@@ -216,12 +216,6 @@ mod tests {
.expect("refresh tests require state db");
let thread_store = thread_store_from_config(&good_config, Some(state_db.clone()));
let environment_manager = Arc::new(EnvironmentManager::default_for_tests());
let executor_skill_provider: Arc<dyn codex_skills_extension::SkillProvider> = Arc::new(
codex_skills_extension::ExecutorSkillProvider::new_with_restriction_product(
Arc::clone(&environment_manager),
SessionSource::Exec.restriction_product(),
),
);
let thread_manager = Arc::new_cyclic(|thread_manager| {
ThreadManager::new(
&good_config,
@@ -238,7 +232,6 @@ mod tests {
thread_manager: thread_manager.clone(),
goal_service: Arc::new(codex_goal_extension::GoalService::new()),
environment_manager: Arc::clone(&environment_manager),
executor_skill_provider: Arc::clone(&executor_skill_provider),
thread_store: Arc::clone(&thread_store),
},
),
@@ -336,14 +336,6 @@ impl MessageProcessor {
// resumed, or forked threads to a different persistence backend/root.
let thread_store = codex_core::thread_store_from_config(config.as_ref(), state_db.clone());
let environment_manager_for_requests = Arc::clone(&environment_manager);
let environment_manager_for_extensions = Arc::clone(&environment_manager);
let restriction_product = session_source.restriction_product();
let executor_skill_provider: Arc<dyn codex_skills_extension::SkillProvider> = Arc::new(
codex_skills_extension::ExecutorSkillProvider::new_with_restriction_product(
Arc::clone(&environment_manager_for_extensions),
restriction_product,
),
);
let goal_service = Arc::new(GoalService::new());
let thread_manager = Arc::new_cyclic(|thread_manager| {
ThreadManager::new(
@@ -363,8 +355,6 @@ impl MessageProcessor {
analytics_events_client: analytics_events_client.clone(),
thread_manager: thread_manager.clone(),
goal_service: Arc::clone(&goal_service),
environment_manager: Arc::clone(&environment_manager_for_extensions),
executor_skill_provider: Arc::clone(&executor_skill_provider),
thread_store: Arc::clone(&thread_store),
},
),
+29 -8
View File
@@ -33,7 +33,13 @@ pub struct McpResourceReadResult {
/// snapshot, so calls automatically use replacements installed during startup and refresh.
#[derive(Clone)]
pub struct McpResourceClient {
manager: Arc<ArcSwap<McpConnectionManager>>,
manager: ResourceManager,
}
#[derive(Clone)]
enum ResourceManager {
Live(Arc<ArcSwap<McpConnectionManager>>),
Snapshot(Arc<McpConnectionManager>),
}
/// Opaque identity for the manager currently used by an MCP resource client.
@@ -59,19 +65,36 @@ impl std::fmt::Debug for McpResourceClient {
impl McpResourceClient {
/// Creates a resource client backed by the session's replaceable MCP manager.
pub fn new(manager: Arc<ArcSwap<McpConnectionManager>>) -> Self {
Self { manager }
Self {
manager: ResourceManager::Live(manager),
}
}
/// Pins resource calls to the manager generation that is current now.
pub fn snapshot(&self) -> Self {
Self {
manager: ResourceManager::Snapshot(self.manager_snapshot()),
}
}
/// Returns the exact manager generation used by this client right now.
pub fn manager_snapshot(&self) -> Arc<McpConnectionManager> {
match &self.manager {
ResourceManager::Live(manager) => manager.load_full(),
ResourceManager::Snapshot(manager) => Arc::clone(manager),
}
}
/// Returns an identity that changes whenever the published manager changes.
pub fn cache_key(&self) -> McpResourceClientCacheKey {
McpResourceClientCacheKey(Arc::downgrade(&self.manager.load_full()))
McpResourceClientCacheKey(Arc::downgrade(&self.manager_snapshot()))
}
/// Returns whether the current manager contains the named server.
///
/// This does not wait for server startup or imply that startup succeeded.
pub async fn has_server(&self, server: &str) -> bool {
self.manager.load_full().contains_server(server)
self.manager_snapshot().contains_server(server)
}
/// Lists one resource page from the named server.
@@ -83,8 +106,7 @@ impl McpResourceClient {
let params =
cursor.map(|cursor| PaginatedRequestParams::default().with_cursor(Some(cursor)));
let result = self
.manager
.load_full()
.manager_snapshot()
.list_resources(server, params)
.await?;
let resources = result
@@ -101,8 +123,7 @@ impl McpResourceClient {
/// Reads one resource from the named server.
pub async fn read_resource(&self, server: &str, uri: &str) -> Result<McpResourceReadResult> {
let result = self
.manager
.load_full()
.manager_snapshot()
.read_resource(server, ReadResourceRequestParams::new(uri.to_string()))
.await?;
let contents = result
+5
View File
@@ -9,6 +9,7 @@ pub mod render;
mod root_loader;
pub mod runtime;
mod runtime_selection;
mod runtime_snapshot;
pub mod service;
pub mod system;
@@ -35,5 +36,9 @@ pub use render::default_skill_metadata_budget;
pub use render::render_available_skills_body;
pub use root_loader::PluginSkillSnapshots;
pub use runtime_selection::collect_runtime_skill_mentions;
pub use runtime_snapshot::RuntimeSkillInjection;
pub use runtime_snapshot::RuntimeSkillInjections;
pub use runtime_snapshot::SkillInjectionIdentity;
pub use runtime_snapshot::SkillsSnapshot;
pub use service::SkillsLoadInput;
pub use service::SkillsService;
+4
View File
@@ -148,6 +148,10 @@ impl HostSkillsSnapshot {
self.outcome.as_ref()
}
pub(crate) fn outcome_arc(&self) -> Arc<SkillLoadOutcome> {
Arc::clone(&self.outcome)
}
pub async fn read_skill_text(&self, skill: &SkillMetadata) -> io::Result<String> {
let fs = self
.outcome
+16 -15
View File
@@ -24,18 +24,23 @@ pub fn collect_runtime_skill_mentions(
plain_name_conflicts: &HashSet<String>,
) -> Vec<SkillCatalogEntry> {
let mut selected = Vec::new();
let mut seen = HashSet::new();
let mut seen_names = HashSet::new();
let mut blocked_plain_names = HashSet::new();
for input in inputs {
match input {
UserInput::Skill { name, path } => {
blocked_plain_names.insert(name.clone());
select_by_path(catalog, &path.to_string_lossy(), &mut seen, &mut selected);
select_by_path(
catalog,
&path.to_string_lossy(),
&mut seen_names,
&mut selected,
);
}
UserInput::Mention { name, path } if path_is_skill(path) => {
blocked_plain_names.insert(name.clone());
select_by_path(catalog, path, &mut seen, &mut selected);
select_by_path(catalog, path, &mut seen_names, &mut selected);
}
_ => {}
}
@@ -51,7 +56,7 @@ pub fn collect_runtime_skill_mentions(
&mentions,
&blocked_plain_names,
plain_name_conflicts,
&mut seen,
&mut seen_names,
&mut selected,
);
}
@@ -64,7 +69,7 @@ fn select_text_mentions(
mentions: &crate::injection::ToolMentions<'_>,
blocked_plain_names: &HashSet<String>,
plain_name_conflicts: &HashSet<String>,
seen: &mut HashSet<SkillCatalogEntryKey>,
seen_names: &mut HashSet<String>,
selected: &mut Vec<SkillCatalogEntry>,
) {
let mentioned_paths = mentions
@@ -77,7 +82,7 @@ fn select_text_mentions(
.into_iter()
.any(|path| mentioned_paths.contains(normalize_skill_path(path)))
{
push_selected(entry, seen, selected);
push_selected(entry, seen_names, selected);
}
}
@@ -90,7 +95,7 @@ fn select_text_mentions(
.collect::<HashSet<_>>();
for entry in &catalog.entries {
if selected_names.contains(&SkillCatalogEntryKey::from(entry)) {
push_selected(entry, seen, selected);
push_selected(entry, seen_names, selected);
}
}
}
@@ -98,7 +103,7 @@ fn select_text_mentions(
fn select_by_path(
catalog: &SkillCatalog,
path: &str,
seen: &mut HashSet<SkillCatalogEntryKey>,
seen_names: &mut HashSet<String>,
selected: &mut Vec<SkillCatalogEntry>,
) {
let path = normalize_skill_path(path);
@@ -107,7 +112,7 @@ fn select_by_path(
.into_iter()
.any(|candidate| normalize_skill_path(candidate) == path)
{
push_selected(entry, seen, selected);
push_selected(entry, seen_names, selected);
}
}
}
@@ -140,10 +145,10 @@ fn select_by_name<'a>(catalog: &'a SkillCatalog, name: &str) -> Option<&'a Skill
fn push_selected(
entry: &SkillCatalogEntry,
seen: &mut HashSet<SkillCatalogEntryKey>,
seen_names: &mut HashSet<String>,
selected: &mut Vec<SkillCatalogEntry>,
) {
if seen.insert(SkillCatalogEntryKey::from(entry)) {
if seen_names.insert(entry.name.clone()) {
selected.push(entry.clone());
}
}
@@ -174,7 +179,3 @@ impl From<&SkillCatalogEntry> for SkillCatalogEntryKey {
}
}
}
#[cfg(test)]
#[path = "runtime_selection_tests.rs"]
mod tests;
@@ -1,66 +0,0 @@
use std::collections::HashSet;
use codex_protocol::user_input::UserInput;
use pretty_assertions::assert_eq;
use super::*;
fn entry(name: &str, path: &str) -> SkillCatalogEntry {
SkillCatalogEntry::new(
SkillPackageId(path.to_string()),
SkillAuthority::new(SkillSourceKind::Host, "host"),
name,
format!("Use {name}."),
crate::runtime::SkillResourceId::new(path),
)
}
fn text_input(text: &str) -> UserInput {
UserInput::Text {
text: text.to_string(),
text_elements: Vec::new(),
}
}
#[test]
fn text_mentions_follow_catalog_order() {
let beta = entry("beta", "/skills/beta/SKILL.md");
let alpha = entry("alpha", "/skills/alpha/SKILL.md");
let catalog = SkillCatalog {
entries: vec![beta.clone(), alpha.clone()],
warnings: Vec::new(),
};
let selected = collect_runtime_skill_mentions(
&[text_input("Use $alpha and $beta")],
&catalog,
&HashSet::new(),
);
assert_eq!(selected, vec![beta, alpha]);
}
#[test]
fn connector_collision_blocks_plain_name_but_not_exact_locator() {
let calendar = entry("calendar", "skill://executor/calendar/SKILL.md");
let catalog = SkillCatalog {
entries: vec![calendar.clone()],
warnings: Vec::new(),
};
let conflicts = HashSet::from(["calendar".to_string()]);
assert_eq!(
collect_runtime_skill_mentions(&[text_input("Use $calendar")], &catalog, &conflicts),
Vec::new()
);
assert_eq!(
collect_runtime_skill_mentions(
&[text_input(
"Use [$calendar](skill://executor/calendar/SKILL.md)",
)],
&catalog,
&conflicts,
),
vec![calendar]
);
}
@@ -0,0 +1,451 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::sync::Arc;
use codex_exec_server::ResolvedSelectedCapabilityRoot;
use codex_protocol::capabilities::CapabilityRootLocation;
use codex_protocol::protocol::Product;
use codex_protocol::user_input::UserInput;
use crate::AvailableSkills;
use crate::HostSkillsSnapshot;
use crate::SkillMetadata;
use crate::collect_runtime_skill_mentions;
use crate::default_skill_metadata_budget;
use crate::loader::EnvironmentSkillMetadata;
use crate::loader::load_environment_skills_from_root;
use crate::render::SkillRenderSideEffects;
use crate::render::build_available_skills_from_catalog;
use crate::runtime::SkillAuthority;
use crate::runtime::SkillCatalog;
use crate::runtime::SkillCatalogEntry;
use crate::runtime::SkillPackageId;
use crate::runtime::SkillReadRequest;
use crate::runtime::SkillReadResult;
use crate::runtime::SkillResourceId;
use crate::runtime::SkillSource;
use crate::runtime::SkillSourceError;
use crate::runtime::SkillSourceFuture;
use crate::runtime::SkillSourceIdentity;
use crate::runtime::SkillSourceKind;
use crate::runtime::SkillSources;
const HOST_AUTHORITY_ID: &str = "host";
type EntryKey = (SkillAuthority, SkillPackageId);
/// Identity of one injected package and the runtime owner that supplied it.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct SkillInjectionIdentity {
source: SkillSourceIdentity,
authority: SkillAuthority,
package: SkillPackageId,
}
#[derive(Debug)]
pub struct RuntimeSkillInjection {
pub identity: SkillInjectionIdentity,
pub entry: SkillCatalogEntry,
pub contents: String,
}
#[derive(Debug, Default)]
pub struct RuntimeSkillInjections {
pub items: Vec<RuntimeSkillInjection>,
pub warnings: Vec<String>,
}
/// One immutable, authority-aware skill view used by a model sampling step.
#[derive(Clone)]
pub struct SkillsSnapshot {
host: HostSkillsSnapshot,
catalog: Arc<SkillCatalog>,
sources: Arc<HashMap<EntryKey, Arc<dyn SkillSource>>>,
available: Arc<Option<AvailableSkills>>,
warnings: Arc<Vec<String>>,
}
impl SkillsSnapshot {
pub fn from_host(host: HostSkillsSnapshot, context_window: Option<i64>) -> Self {
let source: Arc<dyn SkillSource> = Arc::new(HostSkillSource::new(host.clone()));
let catalog = host_catalog(&host);
let source_by_entry = catalog
.entries
.iter()
.map(|entry| {
(
(entry.authority.clone(), entry.id.clone()),
Arc::clone(&source),
)
})
.collect();
Self::new(host, catalog, source_by_entry, context_window)
}
pub async fn load(
host: HostSkillsSnapshot,
executor_roots: &[ResolvedSelectedCapabilityRoot],
extra_sources: Option<&SkillSources>,
restriction_product: Option<Product>,
context_window: Option<i64>,
) -> Self {
let mut sources =
SkillSources::new().with_source("host", Arc::new(HostSkillSource::new(host.clone())));
for root in executor_roots {
sources = sources.with_source(
format!("executor root `{}`", root.selected_root().id),
Arc::new(ExecutorSkillSource::new(root.clone(), restriction_product)),
);
}
if let Some(extra_sources) = extra_sources {
sources.extend(extra_sources.clone());
}
let (catalog, source_by_entry) = sources.list_with_sources().await;
Self::new(host, catalog, source_by_entry, context_window)
}
fn new(
host: HostSkillsSnapshot,
catalog: SkillCatalog,
source_by_entry: HashMap<EntryKey, Arc<dyn SkillSource>>,
context_window: Option<i64>,
) -> Self {
let available = build_available_skills_from_catalog(
&catalog,
Some(host.outcome()),
default_skill_metadata_budget(context_window),
SkillRenderSideEffects::None,
);
let mut warnings = catalog.warnings.clone();
if let Some(warning) = available
.as_ref()
.and_then(|available| available.warning_message.clone())
{
warnings.push(warning);
}
Self {
host,
catalog: Arc::new(catalog),
sources: Arc::new(source_by_entry),
available: Arc::new(available),
warnings: Arc::new(warnings),
}
}
pub fn available(&self) -> Option<&AvailableSkills> {
self.available.as_ref().as_ref()
}
pub fn warnings(&self) -> &[String] {
self.warnings.as_ref()
}
pub fn skill_name_counts_lower(&self) -> HashMap<String, usize> {
let mut counts = HashMap::new();
for entry in self.catalog.entries.iter().filter(|entry| entry.enabled) {
*counts.entry(entry.name.to_ascii_lowercase()).or_default() += 1;
}
counts
}
pub fn host_skill(&self, entry: &SkillCatalogEntry) -> Option<&SkillMetadata> {
if entry.authority.kind != SkillSourceKind::Host {
return None;
}
self.host
.outcome()
.skills
.iter()
.find(|skill| skill.path_to_skills_md.to_string_lossy() == entry.main_prompt.as_str())
}
pub async fn injections(
&self,
input: &[UserInput],
plain_name_conflicts: &HashSet<String>,
active_skills: &HashMap<String, SkillInjectionIdentity>,
remaining_items: usize,
) -> RuntimeSkillInjections {
let selected = collect_runtime_skill_mentions(input, &self.catalog, plain_name_conflicts);
let mut result = RuntimeSkillInjections::default();
for entry in &selected {
let key = (entry.authority.clone(), entry.id.clone());
let Some(source) = self.sources.get(&key) else {
result.warnings.push(format!(
"Failed to load skill `{}`: its runtime source is unavailable.",
entry.name
));
continue;
};
let identity = SkillInjectionIdentity {
source: source.identity(),
authority: entry.authority.clone(),
package: entry.id.clone(),
};
if active_skills.get(&entry.name) == Some(&identity) {
continue;
}
if result.items.len() == remaining_items {
result.warnings.push(format!(
"Only the first {remaining_items} newly selected skills were loaded because this turn reached its skill instruction limit."
));
break;
}
let request = SkillReadRequest {
authority: entry.authority.clone(),
package: entry.id.clone(),
resource: entry.main_prompt.clone(),
};
match source.read(request).await {
Ok(read) => result.items.push(RuntimeSkillInjection {
identity,
entry: entry.clone(),
contents: read.contents,
}),
Err(err) => result
.warnings
.push(format!("Failed to load skill `{}`: {err}", entry.name)),
}
}
result
}
pub fn contains_injection(&self, identity: &SkillInjectionIdentity) -> bool {
let key = (identity.authority.clone(), identity.package.clone());
self.catalog
.entries
.iter()
.any(|entry| entry.enabled && entry.authority == key.0 && entry.id == key.1)
&& self
.sources
.get(&key)
.is_some_and(|source| source.identity() == identity.source)
}
}
impl fmt::Debug for SkillsSnapshot {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("SkillsSnapshot")
.field("catalog", &self.catalog)
.field("available", &self.available)
.finish_non_exhaustive()
}
}
struct HostSkillSource {
snapshot: HostSkillsSnapshot,
identity: SkillSourceIdentity,
}
impl HostSkillSource {
fn new(snapshot: HostSkillsSnapshot) -> Self {
Self {
identity: SkillSourceIdentity::from_owner(snapshot.outcome_arc()),
snapshot,
}
}
}
impl SkillSource for HostSkillSource {
fn identity(&self) -> SkillSourceIdentity {
self.identity.clone()
}
fn list(&self) -> SkillSourceFuture<'_, SkillCatalog> {
Box::pin(async move { Ok(host_catalog(&self.snapshot)) })
}
fn read(&self, request: SkillReadRequest) -> SkillSourceFuture<'_, SkillReadResult> {
Box::pin(async move {
if request.authority != SkillAuthority::new(SkillSourceKind::Host, HOST_AUTHORITY_ID) {
return Err(SkillSourceError::new("host skill authority does not match"));
}
let Some(skill) = self.snapshot.outcome().skills.iter().find(|skill| {
skill.path_to_skills_md.to_string_lossy() == request.resource.as_str()
}) else {
return Err(SkillSourceError::new(format!(
"host skill resource is not loaded: {}",
request.resource.as_str()
)));
};
let contents = self.snapshot.read_skill_text(skill).await.map_err(|err| {
SkillSourceError::new(format!(
"failed to read host skill resource {}: {err}",
request.resource.as_str()
))
})?;
Ok(SkillReadResult {
resource: request.resource,
contents,
})
})
}
}
fn host_catalog(snapshot: &HostSkillsSnapshot) -> SkillCatalog {
let outcome = snapshot.outcome();
let mut catalog = SkillCatalog {
warnings: outcome
.errors
.iter()
.map(|error| {
format!(
"Failed to load skill at {}: {}",
error.path.display(),
error.message
)
})
.collect(),
..Default::default()
};
for (skill, enabled) in outcome.skills_with_enabled() {
let path = skill.path_to_skills_md.to_string_lossy().into_owned();
let mut entry = SkillCatalogEntry::new(
SkillPackageId(path.clone()),
SkillAuthority::new(SkillSourceKind::Host, HOST_AUTHORITY_ID),
skill.name.clone(),
skill.description.clone(),
SkillResourceId::new(path.clone()),
)
.with_short_description(skill.short_description.clone())
.with_display_path(path.replace('\\', "/"))
.with_dependencies(skill.dependencies.clone());
if !enabled {
entry = entry.disabled();
}
if !skill.allows_implicit_invocation() {
entry = entry.hidden_from_prompt();
}
catalog.push_entry(entry);
}
catalog
}
struct ExecutorSkillSource {
root: ResolvedSelectedCapabilityRoot,
authority: SkillAuthority,
restriction_product: Option<Product>,
identity: SkillSourceIdentity,
}
impl ExecutorSkillSource {
fn new(root: ResolvedSelectedCapabilityRoot, restriction_product: Option<Product>) -> Self {
Self {
authority: SkillAuthority::new(
SkillSourceKind::Executor,
root.selected_root().id.clone(),
),
identity: SkillSourceIdentity::from_owner(Arc::clone(root.environment())),
root,
restriction_product,
}
}
}
impl SkillSource for ExecutorSkillSource {
fn identity(&self) -> SkillSourceIdentity {
self.identity.clone()
}
fn list(&self) -> SkillSourceFuture<'_, SkillCatalog> {
Box::pin(async move {
let CapabilityRootLocation::Environment {
environment_id,
path,
} = &self.root.selected_root().location;
let outcome = load_environment_skills_from_root(
self.root.file_system().as_ref(),
path,
self.restriction_product,
)
.await;
let mut catalog = SkillCatalog {
warnings: outcome.warnings,
..Default::default()
};
for skill in outcome.skills {
catalog.push_entry(executor_catalog_entry(
skill,
self.authority.clone(),
&self.root.selected_root().id,
environment_id,
));
}
Ok(catalog)
})
}
fn read(&self, request: SkillReadRequest) -> SkillSourceFuture<'_, SkillReadResult> {
Box::pin(async move {
if request.authority != self.authority || request.package.0 != request.resource.as_str()
{
return Err(SkillSourceError::new(
"executor skill resource does not match its captured source",
));
}
let CapabilityRootLocation::Environment { environment_id, .. } =
&self.root.selected_root().location;
let Some((resource_environment, path)) = request.resource.environment_path() else {
return Err(SkillSourceError::new(
"executor skill resource has no environment path",
));
};
if resource_environment != environment_id {
return Err(SkillSourceError::new(
"executor skill resource belongs to a different environment",
));
}
let contents = self
.root
.file_system()
.read_file_text(path, /*sandbox*/ None)
.await
.map_err(|err| {
SkillSourceError::new(format!(
"failed to read executor skill resource {}: {err}",
request.resource.as_str()
))
})?;
Ok(SkillReadResult {
resource: request.resource,
contents,
})
})
}
}
fn executor_catalog_entry(
skill: EnvironmentSkillMetadata,
authority: SkillAuthority,
root_id: &str,
environment_id: &str,
) -> SkillCatalogEntry {
let prompt_visible = skill.allows_implicit_invocation();
let path = skill.path_to_skills_md.inferred_native_path_string();
let display_path = format!(
"skill://{root_id}/{}",
path.replace('\\', "/").trim_start_matches('/')
);
let entry = SkillCatalogEntry::new(
SkillPackageId(display_path.clone()),
authority,
skill.name,
skill.description,
SkillResourceId::environment(
display_path.clone(),
environment_id,
skill.path_to_skills_md,
),
)
.with_short_description(skill.short_description)
.with_display_path(display_path)
.with_dependencies(skill.dependencies);
if prompt_visible {
entry
} else {
entry.hidden_from_prompt()
}
}
@@ -1,69 +0,0 @@
use codex_context_fragments::ContextualUserFragment;
use crate::injection::SkillInjection;
use crate::runtime::SkillCatalogEntry;
const MAX_SKILL_NAME_BYTES: usize = 256;
const MAX_SKILL_PATH_BYTES: usize = 1_024;
const MAX_SKILL_BODY_BYTES: usize = 8_000;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkillInstructions {
name: String,
path: String,
contents: String,
}
impl From<&SkillInjection> for SkillInstructions {
fn from(skill: &SkillInjection) -> Self {
Self {
name: skill.name.clone(),
path: skill.path.clone(),
contents: skill.contents.clone(),
}
}
}
impl SkillInstructions {
/// Builds one hard-bounded instruction fragment from a runtime catalog entry.
pub fn from_runtime(entry: &SkillCatalogEntry, contents: &str) -> (Self, bool) {
let (contents, truncated) = truncate_utf8(contents, MAX_SKILL_BODY_BYTES);
(
Self {
name: truncate_utf8(&entry.name, MAX_SKILL_NAME_BYTES).0,
path: truncate_utf8(entry.rendered_path(), MAX_SKILL_PATH_BYTES).0,
contents,
},
truncated,
)
}
}
fn truncate_utf8(value: &str, max_bytes: usize) -> (String, bool) {
let mut end = value.len().min(max_bytes);
while !value.is_char_boundary(end) {
end = end.saturating_sub(1);
}
(value[..end].to_string(), end < value.len())
}
impl ContextualUserFragment for SkillInstructions {
fn role(&self) -> &'static str {
"user"
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
("<skill>", "</skill>")
}
fn body(&self) -> String {
format!(
"\n<name>{}</name>\n<path>{}</path>\n{}\n",
self.name, self.path, self.contents
)
}
}
@@ -1,50 +0,0 @@
use codex_core_skills::AvailableSkills;
use codex_core_skills::render_available_skills_body;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_CLOSE_TAG;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_OPEN_TAG;
use super::ContextualUserFragment;
/// Model-context fragment describing the skills available to Codex.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AvailableSkillsInstructions {
skill_root_lines: Vec<String>,
skill_lines: Vec<String>,
}
impl AvailableSkillsInstructions {
/// Creates a skills context fragment from pre-rendered catalog lines.
pub fn from_skill_lines(skill_lines: Vec<String>) -> Self {
Self {
skill_root_lines: Vec::new(),
skill_lines,
}
}
}
impl From<AvailableSkills> for AvailableSkillsInstructions {
fn from(available_skills: AvailableSkills) -> Self {
Self {
skill_root_lines: available_skills.skill_root_lines,
skill_lines: available_skills.skill_lines,
}
}
}
impl ContextualUserFragment for AvailableSkillsInstructions {
fn role(&self) -> &'static str {
"developer"
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
(SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG)
}
fn body(&self) -> String {
render_available_skills_body(&self.skill_root_lines, &self.skill_lines)
}
}
+2 -3
View File
@@ -3,7 +3,6 @@
mod approved_command_prefix_saved;
mod apps_instructions;
mod available_plugins_instructions;
mod available_skills_instructions;
mod collaboration_mode_instructions;
mod contextual_user_message;
mod current_time_reminder;
@@ -27,6 +26,7 @@ mod realtime_start_instructions;
mod realtime_start_with_instructions;
mod recommended_plugins_instructions;
mod rollout_budget;
mod skill_instructions;
mod subagent_notification;
mod token_budget_context;
mod turn_aborted;
@@ -37,13 +37,11 @@ pub(crate) mod world_state;
pub(crate) use approved_command_prefix_saved::ApprovedCommandPrefixSaved;
pub(crate) use apps_instructions::AppsInstructions;
pub(crate) use available_plugins_instructions::AvailablePluginsInstructions;
pub use available_skills_instructions::AvailableSkillsInstructions;
pub(crate) use codex_context_fragments::AdditionalContextDeveloperFragment;
pub(crate) use codex_context_fragments::AdditionalContextUserFragment;
pub use codex_context_fragments::ContextualUserFragment;
pub(crate) use codex_context_fragments::FragmentRegistration;
pub(crate) use codex_context_fragments::FragmentRegistrationProxy;
pub(crate) use codex_core_skills::SkillInstructions;
pub(crate) use collaboration_mode_instructions::CollaborationModeInstructions;
pub(crate) use contextual_user_message::is_contextual_user_fragment;
pub(crate) use contextual_user_message::parse_visible_hook_prompt_message;
@@ -70,6 +68,7 @@ pub(crate) use realtime_start_instructions::RealtimeStartInstructions;
pub(crate) use realtime_start_with_instructions::RealtimeStartWithInstructions;
pub(crate) use recommended_plugins_instructions::RecommendedPluginsInstructions;
pub(crate) use rollout_budget::RolloutBudgetContext;
pub(crate) use skill_instructions::SkillInstructions;
pub(crate) use subagent_notification::SubagentNotification;
pub(crate) use token_budget_context::ContextWindowGuidance;
pub(crate) use token_budget_context::TokenBudgetContext;
@@ -0,0 +1,99 @@
use codex_core_skills::runtime::SkillCatalogEntry;
use super::ContextualUserFragment;
const MAX_SKILL_NAME_BYTES: usize = 256;
const MAX_SKILL_PATH_BYTES: usize = 1_024;
const MAX_SKILL_INSTRUCTIONS_BYTES: usize = 4_000;
const REPLACEMENT_NOTICE: &str = "<replacement_notice>These instructions replace the previously provided instructions for this skill.</replacement_notice>\n";
const UNAVAILABLE_NOTICE: &str = "<unavailable_notice>The previously provided instructions for this skill no longer apply because the skill is unavailable in the current runtime.</unavailable_notice>\n";
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SkillInstructions {
name: String,
path: Option<String>,
contents: String,
update: SkillInstructionsUpdate,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SkillInstructionsUpdate {
Initial,
Replacement,
Unavailable,
}
impl SkillInstructions {
pub(crate) fn from_runtime(
entry: &SkillCatalogEntry,
contents: &str,
max_total_bytes: usize,
replaces_previous: bool,
) -> Option<(Self, bool)> {
let mut instructions = Self {
name: truncate_utf8(&entry.name, MAX_SKILL_NAME_BYTES).0,
path: Some(truncate_utf8(entry.rendered_path(), MAX_SKILL_PATH_BYTES).0),
contents: String::new(),
update: if replaces_previous {
SkillInstructionsUpdate::Replacement
} else {
SkillInstructionsUpdate::Initial
},
};
let envelope_bytes = instructions.render().len();
let max_body_bytes = max_total_bytes
.min(MAX_SKILL_INSTRUCTIONS_BYTES)
.checked_sub(envelope_bytes)?;
let (contents, truncated) = truncate_utf8(contents, max_body_bytes);
instructions.contents = contents;
Some((instructions, truncated))
}
pub(crate) fn unavailable(name: &str) -> Self {
Self {
name: truncate_utf8(name, MAX_SKILL_NAME_BYTES).0,
path: None,
contents: String::new(),
update: SkillInstructionsUpdate::Unavailable,
}
}
}
fn truncate_utf8(value: &str, max_bytes: usize) -> (String, bool) {
let mut end = value.len().min(max_bytes);
while !value.is_char_boundary(end) {
end = end.saturating_sub(1);
}
(value[..end].to_string(), end < value.len())
}
impl ContextualUserFragment for SkillInstructions {
fn role(&self) -> &'static str {
"user"
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
("<skill>", "</skill>")
}
fn body(&self) -> String {
let path = self
.path
.as_ref()
.map(|path| format!("<path>{path}</path>\n"))
.unwrap_or_default();
let notice = match self.update {
SkillInstructionsUpdate::Initial => "",
SkillInstructionsUpdate::Replacement => REPLACEMENT_NOTICE,
SkillInstructionsUpdate::Unavailable => UNAVAILABLE_NOTICE,
};
format!(
"\n<name>{}</name>\n{path}{notice}{}\n",
self.name, self.contents
)
}
}
@@ -1,5 +1,6 @@
mod agents_md;
mod environment;
mod skills;
use crate::context::ContextualUserFragment;
use codex_protocol::models::ContentItem;
@@ -14,6 +15,7 @@ use std::fmt;
pub(crate) use agents_md::AgentsMdState;
pub(crate) use environment::EnvironmentsState;
pub(crate) use skills::SkillsState;
trait ErasedWorldStateSection: Send + Sync {
fn snapshot(&self) -> Option<Value>;
@@ -0,0 +1,140 @@
use codex_core_skills::SkillsSnapshot;
use codex_core_skills::render_available_skills_body;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_CLOSE_TAG;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_OPEN_TAG;
use serde::Deserialize;
use serde::Serialize;
use super::PreviousSectionState;
use super::WorldStateSection;
use crate::context::ContextualUserFragment;
const REPLACEMENT_NOTICE: &str = "This skills list replaces the previously provided skills list.";
const REMOVAL_NOTICE: &str = "The previously provided skills list no longer applies.";
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
pub(crate) struct SkillsState {
skill_root_lines: Vec<String>,
skill_lines: Vec<String>,
omitted_count: usize,
}
impl SkillsState {
pub(crate) fn new(skills: &SkillsSnapshot, enabled: bool) -> Self {
if !enabled {
return Self::default();
}
let Some(available) = skills.available() else {
return Self::default();
};
Self {
skill_root_lines: available.skill_root_lines.clone(),
skill_lines: available.skill_lines.clone(),
omitted_count: available.report.omitted_count,
}
}
fn has_catalog(&self) -> bool {
!self.skill_lines.is_empty()
}
}
impl WorldStateSection for SkillsState {
const ID: &'static str = "skills";
type Snapshot = Self;
fn snapshot(&self) -> Self::Snapshot {
self.clone()
}
fn matches_legacy_fragment(role: &str, text: &str) -> bool {
let text = text.trim();
role == "developer"
&& text.starts_with(SKILLS_INSTRUCTIONS_OPEN_TAG)
&& text.ends_with(SKILLS_INSTRUCTIONS_CLOSE_TAG)
}
fn render_diff(
&self,
previous: PreviousSectionState<'_, Self::Snapshot>,
) -> Option<Box<dyn ContextualUserFragment>> {
let current = self.snapshot();
if matches!(previous, PreviousSectionState::Known(previous) if previous == &current) {
return None;
}
let previous_had_catalog = match previous {
PreviousSectionState::Known(previous) => !previous.skill_lines.is_empty(),
PreviousSectionState::Unknown => true,
PreviousSectionState::Absent => false,
};
if !self.has_catalog() && !previous_had_catalog {
return None;
}
Some(Box::new(SkillsStateFragment {
skill_root_lines: self.skill_root_lines.clone(),
skill_lines: self.skill_lines.clone(),
omitted_count: self.omitted_count,
include_policy: !previous_had_catalog,
notice: if !self.has_catalog() {
Some(REMOVAL_NOTICE)
} else if previous_had_catalog {
Some(REPLACEMENT_NOTICE)
} else {
None
},
}))
}
}
struct SkillsStateFragment {
skill_root_lines: Vec<String>,
skill_lines: Vec<String>,
omitted_count: usize,
include_policy: bool,
notice: Option<&'static str>,
}
impl ContextualUserFragment for SkillsStateFragment {
fn role(&self) -> &'static str {
"developer"
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
(SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG)
}
fn body(&self) -> String {
let catalog = (!self.skill_lines.is_empty()).then(|| {
if self.include_policy {
render_available_skills_body(&self.skill_root_lines, &self.skill_lines)
} else {
let mut lines = vec!["## Skills".to_string()];
if !self.skill_root_lines.is_empty() {
lines.push("### Skill roots".to_string());
lines.extend(self.skill_root_lines.iter().cloned());
}
lines.push("### Available skills".to_string());
lines.extend(self.skill_lines.iter().cloned());
format!("\n{}\n", lines.join("\n"))
}
}).map(|mut catalog| {
if self.omitted_count > 0 {
catalog.push_str(&format!(
"\n{} additional skills are omitted from this model-visible list because of the catalog size limit.\n",
self.omitted_count
));
}
catalog
});
match (self.notice, catalog) {
(Some(notice), Some(catalog)) => format!("\n{notice}\n{catalog}"),
(Some(notice), None) => format!("\n{notice}\n"),
(None, Some(catalog)) => catalog,
(None, None) => String::new(),
}
}
}
-8
View File
@@ -78,7 +78,6 @@ pub(crate) mod prompt_debug;
pub use prompt_debug::build_prompt_input;
pub(crate) mod mentions {
pub(crate) use crate::plugins::build_connector_slug_counts;
pub(crate) use crate::plugins::build_skill_name_counts;
pub(crate) use crate::plugins::collect_explicit_app_ids;
pub(crate) use crate::plugins::collect_explicit_plugin_mentions;
pub(crate) use crate::plugins::collect_tool_mentions_from_messages;
@@ -88,14 +87,7 @@ pub mod sandboxing;
mod session_prefix;
mod session_startup_prewarm;
pub mod skills;
pub(crate) use skills::SkillInjections;
pub(crate) use skills::SkillMetadata;
pub(crate) use skills::SkillsService;
pub(crate) use skills::build_available_skills;
pub(crate) use skills::build_skill_injections;
pub(crate) use skills::build_skill_name_counts;
pub(crate) use skills::collect_explicit_skill_mentions;
pub(crate) use skills::default_skill_metadata_budget;
pub(crate) use skills::injection;
pub(crate) use skills::maybe_emit_implicit_skill_invocation;
pub(crate) use skills::skills_load_input_from_config;
+19 -17
View File
@@ -15,10 +15,10 @@ use codex_rmcp_client::perform_oauth_login;
use tokio_util::sync::CancellationToken;
use tracing::warn;
use crate::SkillMetadata;
use crate::session::session::Session;
use crate::session::turn_context::TurnContext;
use crate::skills::model::SkillToolDependency;
use codex_core_skills::runtime::SkillCatalogEntry;
use codex_mcp::ElicitationReviewerHandle;
use codex_mcp::McpOAuthLoginSupport;
use codex_mcp::McpPermissionPromptAutoApproveContext;
@@ -35,13 +35,13 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies(
sess: &Session,
turn_context: &TurnContext,
cancellation_token: &CancellationToken,
mentioned_skills: &[SkillMetadata],
mentioned_skills: &[SkillCatalogEntry],
elicitation_reviewer: Option<ElicitationReviewerHandle>,
) {
) -> bool {
let originator_value = originator().value;
if !is_first_party_originator(originator_value.as_str()) {
// Only support first-party clients for now.
return;
return false;
}
let config = turn_context.config.clone();
@@ -50,24 +50,24 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies(
.features
.enabled(codex_features::Feature::SkillMcpDependencyInstall)
{
return;
return false;
}
let installed = sess.runtime_mcp_servers(config.as_ref()).await;
let missing = collect_missing_mcp_dependencies(mentioned_skills, &installed);
if missing.is_empty() {
return;
return false;
}
let unprompted_missing = filter_prompted_mcp_dependencies(sess, &missing).await;
if unprompted_missing.is_empty() {
return;
return false;
}
if should_install_mcp_dependencies(sess, turn_context, &unprompted_missing, cancellation_token)
.await
{
maybe_install_mcp_dependencies(
return maybe_install_mcp_dependencies(
sess,
turn_context,
config.as_ref(),
@@ -76,35 +76,36 @@ pub(crate) async fn maybe_prompt_and_install_mcp_dependencies(
)
.await;
}
false
}
pub(crate) async fn maybe_install_mcp_dependencies(
sess: &Session,
turn_context: &TurnContext,
config: &crate::config::Config,
mentioned_skills: &[SkillMetadata],
mentioned_skills: &[SkillCatalogEntry],
elicitation_reviewer: Option<ElicitationReviewerHandle>,
) {
) -> bool {
if mentioned_skills.is_empty()
|| !config
.features
.enabled(codex_features::Feature::SkillMcpDependencyInstall)
{
return;
return false;
}
let codex_home = config.codex_home.clone();
let installed = sess.runtime_mcp_servers(config).await;
let missing = collect_missing_mcp_dependencies(mentioned_skills, &installed);
if missing.is_empty() {
return;
return false;
}
let mut servers = match load_global_mcp_servers(&codex_home).await {
Ok(servers) => servers,
Err(err) => {
warn!("failed to load MCP servers while installing skill dependencies: {err}");
return;
return false;
}
};
@@ -120,7 +121,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
}
if !updated {
return;
return false;
}
if let Err(err) = ConfigEditsBuilder::new(&codex_home)
@@ -129,7 +130,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
.await
{
warn!("failed to persist MCP dependencies for mentioned skills: {err}");
return;
return false;
}
for (name, server_config) in added {
@@ -197,7 +198,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
}
if let Err(err) = refresh_config.mcp_servers.set(configured_servers) {
warn!("failed to refresh MCP dependencies for mentioned skills: {err}");
return;
return false;
}
let refresh_servers = sess.runtime_mcp_servers(&refresh_config).await;
sess.refresh_mcp_servers_now(
@@ -208,6 +209,7 @@ pub(crate) async fn maybe_install_mcp_dependencies(
elicitation_reviewer,
)
.await;
true
}
async fn should_install_mcp_dependencies(
@@ -416,7 +418,7 @@ fn mcp_dependency_to_server_config(
}
fn collect_missing_mcp_dependencies(
mentioned_skills: &[SkillMetadata],
mentioned_skills: &[SkillCatalogEntry],
installed: &HashMap<String, McpServerConfig>,
) -> HashMap<String, McpServerConfig> {
let mut missing = HashMap::new();
-2
View File
@@ -102,8 +102,6 @@ pub(crate) fn collect_explicit_plugin_mentions(
.collect()
}
pub(crate) use crate::build_skill_name_counts;
pub(crate) fn build_connector_slug_counts(
connectors: &[connectors::AppInfo],
) -> HashMap<String, usize> {
-1
View File
@@ -12,7 +12,6 @@ pub(crate) use injection::build_plugin_injections;
pub(crate) use render::render_explicit_plugin_instructions;
pub(crate) use mentions::build_connector_slug_counts;
pub(crate) use mentions::build_skill_name_counts;
pub(crate) use mentions::collect_explicit_app_ids;
pub(crate) use mentions::collect_explicit_plugin_mentions;
pub(crate) use mentions::collect_tool_mentions_from_messages;
+17 -29
View File
@@ -15,7 +15,6 @@ use crate::agent::AgentStatus;
use crate::agent::agent_status_from_event;
use crate::agent::status::is_final;
use crate::attestation::AttestationProvider;
use crate::build_available_skills;
use crate::compact;
use crate::config::ManagedFeatures;
use crate::config::resolve_tool_suggest_config_from_layer_stack;
@@ -23,7 +22,6 @@ use crate::connectors;
use crate::context::ApprovedCommandPrefixSaved;
use crate::context::AppsInstructions;
use crate::context::AvailablePluginsInstructions;
use crate::context::AvailableSkillsInstructions;
use crate::context::CollaborationModeInstructions;
use crate::context::ContextualUserFragment;
use crate::context::MultiAgentModeInstructions;
@@ -33,7 +31,6 @@ use crate::context::PersonalitySpecInstructions;
use crate::context::RecommendedPluginsInstructions;
use crate::context::world_state::WorldState;
use crate::current_time::TimeProvider;
use crate::default_skill_metadata_budget;
use crate::environment_selection::TurnEnvironmentSnapshot;
use crate::exec_policy::ExecPolicyManager;
use crate::image_preparation::prepare_response_items;
@@ -42,7 +39,6 @@ use crate::realtime_conversation::RealtimeConversationManager;
use crate::session::step_context::StepContext;
use crate::session::turn_context::TurnEnvironment;
use crate::session_prefix::format_inter_agent_completion_message;
use crate::skills::SkillRenderSideEffects;
use crate::skills_load_input_from_config;
use crate::turn_metadata::TurnMetadataState;
use crate::turn_timing::now_unix_timestamp_ms;
@@ -303,8 +299,6 @@ pub(crate) struct PreviousTurnSettings {
pub(crate) realtime_active: Option<bool>,
}
#[cfg(test)]
use crate::SkillMetadata;
use crate::SkillsService;
use crate::exec_policy::ExecPolicyUpdateError;
use crate::guardian::GuardianReviewSessionManager;
@@ -315,6 +309,8 @@ use crate::session_startup_prewarm::SessionStartupPrewarmHandle;
use crate::shell;
#[cfg(test)]
use crate::skills::SkillLoadOutcome;
#[cfg(test)]
use crate::skills::SkillMetadata;
use crate::state::AutoCompactWindowIds;
use crate::state::AutoCompactWindowSnapshot;
use crate::state::PendingRequestPermissions;
@@ -2837,10 +2833,25 @@ impl Session {
&environments.captured_environment_availability(),
)
.await;
let extra_skill_sources = self
.services
.thread_extension_data
.get::<codex_core_skills::runtime::SkillSources>();
let skills = Arc::new(
codex_core_skills::SkillsSnapshot::load(
turn_context.turn_skills.snapshot.clone(),
&selected_capability_roots,
extra_skill_sources.as_deref(),
turn_context.session_source.restriction_product(),
turn_context.model_context_window(),
)
.await,
);
Arc::new(StepContext::new(
turn_context,
environments,
selected_capability_roots,
skills,
loaded_agents_md,
))
}
@@ -3212,29 +3223,6 @@ impl Session {
developer_sections.push(apps_instructions.render());
}
}
if turn_context.config.include_skill_instructions {
let available_skills = build_available_skills(
turn_context.turn_skills.snapshot.outcome(),
default_skill_metadata_budget(turn_context.model_info.context_window),
SkillRenderSideEffects::ThreadStart {
session_telemetry: &self.services.session_telemetry,
},
);
if let Some(available_skills) = available_skills {
let warning_message = available_skills.warning_message.clone();
let skills_instructions = AvailableSkillsInstructions::from(available_skills);
if let Some(warning_message) = warning_message {
self.send_event_raw(Event {
id: String::new(),
msg: EventMsg::Warning(WarningEvent {
message: warning_message,
}),
})
.await;
}
developer_sections.push(skills_instructions.render());
}
}
let loaded_plugins = self
.services
.plugins_manager
@@ -3,6 +3,7 @@ use std::sync::Arc;
use crate::agents_md::LoadedAgentsMd;
use crate::environment_selection::TurnEnvironmentSnapshot;
use crate::session::turn_context::TurnContext;
use codex_core_skills::SkillsSnapshot;
use codex_exec_server::ResolvedSelectedCapabilityRoot;
/// Request-scoped state that may change between model sampling requests.
@@ -12,6 +13,8 @@ pub(crate) struct StepContext {
pub(crate) environments: TurnEnvironmentSnapshot,
/// Capability roots bound to ready environments in this exact step.
pub(crate) selected_capability_roots: Vec<ResolvedSelectedCapabilityRoot>,
/// Complete skill catalog and exact read routes captured for this step.
pub(crate) skills: Arc<SkillsSnapshot>,
/// The canonical AGENTS.md value observed with this environment snapshot.
pub(crate) loaded_agents_md: Option<Arc<LoadedAgentsMd>>,
}
@@ -21,12 +24,14 @@ impl StepContext {
turn: Arc<TurnContext>,
environments: TurnEnvironmentSnapshot,
selected_capability_roots: Vec<ResolvedSelectedCapabilityRoot>,
skills: Arc<SkillsSnapshot>,
loaded_agents_md: Option<Arc<LoadedAgentsMd>>,
) -> Self {
Self {
turn,
environments,
selected_capability_roots,
skills,
loaded_agents_md,
}
}
+6 -56
View File
@@ -13,6 +13,7 @@ use crate::session::step_context::StepContext;
use crate::shell::default_user_shell;
use crate::shell_snapshot::ShellSnapshot;
use crate::skills::SkillRenderSideEffects;
use crate::skills::build_available_skills;
use crate::skills::render::SkillMetadataBudget;
use crate::test_support::models_manager_with_provider;
use crate::tools::format_exec_output_str;
@@ -187,10 +188,15 @@ use std::time::Duration as StdDuration;
impl StepContext {
pub(crate) fn for_test(turn: Arc<TurnContext>) -> Arc<Self> {
let environments = turn.environments.clone();
let skills = Arc::new(codex_core_skills::SkillsSnapshot::from_host(
turn.turn_skills.snapshot.clone(),
turn.model_context_window(),
));
Arc::new(Self::new(
turn,
environments,
Vec::new(),
skills,
/*loaded_agents_md*/ None,
))
}
@@ -8451,62 +8457,6 @@ fn emit_thread_start_skill_metrics_records_description_truncated_chars_without_o
);
}
#[tokio::test]
async fn build_initial_context_emits_thread_start_skill_warning_on_repeated_builds() {
let (session, turn_context, rx) = make_session_and_context_with_rx().await;
let mut turn_context = Arc::into_inner(turn_context).expect("sole thread settings owner");
let mut outcome = SkillLoadOutcome::default();
outcome.skills = vec![
SkillMetadata {
name: "admin-skill".to_string(),
description: "desc".to_string(),
short_description: None,
interface: None,
dependencies: None,
policy: None,
path_to_skills_md: test_path_buf("/tmp/admin-skill/SKILL.md").abs(),
scope: SkillScope::Admin,
plugin_id: None,
},
SkillMetadata {
name: "repo-skill".to_string(),
description: "desc".to_string(),
short_description: None,
interface: None,
dependencies: None,
policy: None,
path_to_skills_md: test_path_buf("/tmp/repo-skill/SKILL.md").abs(),
scope: SkillScope::Repo,
plugin_id: None,
},
];
turn_context.model_info.context_window = Some(100);
turn_context.turn_skills = TurnSkillsContext::new(HostSkillsSnapshot::new(Arc::new(outcome)));
let turn_context = Arc::new(turn_context);
let _ = build_initial_context(&session, &turn_context).await;
let warning_event = timeout(Duration::from_secs(1), rx.recv())
.await
.expect("warning event should arrive")
.expect("warning event should be readable");
assert!(matches!(
warning_event.msg,
EventMsg::Warning(WarningEvent { message })
if message == "Exceeded skills context budget of 2%. All skill descriptions were removed and 2 additional skills were not included in the model-visible skills list."
));
let _ = build_initial_context(&session, &turn_context).await;
let warning_event = timeout(Duration::from_secs(1), rx.recv())
.await
.expect("warning event should arrive on repeated build")
.expect("warning event should be readable");
assert!(matches!(
warning_event.msg,
EventMsg::Warning(WarningEvent { message })
if message == "Exceeded skills context budget of 2%. All skill descriptions were removed and 2 additional skills were not included in the model-visible skills list."
));
}
#[tokio::test]
async fn handle_output_item_done_records_image_save_history_message() {
let (session, turn_context) = make_session_and_context().await;
+258 -97
View File
@@ -4,12 +4,9 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use crate::SkillInjections;
use crate::build_skill_injections;
use crate::client::ModelClientSession;
use crate::client_common::Prompt;
use crate::client_common::ResponseEvent;
use crate::collect_explicit_skill_mentions;
use crate::compact::InitialContextInjection;
use crate::compact::run_inline_auto_compact_task;
use crate::compact::should_use_remote_compact_task;
@@ -17,6 +14,7 @@ use crate::compact_remote::run_inline_remote_auto_compact_task;
use crate::compact_remote_v2::run_inline_remote_auto_compact_task as run_inline_remote_auto_compact_task_v2;
use crate::connectors;
use crate::context::ContextualUserFragment;
use crate::context::SkillInstructions;
use crate::feedback_tags;
use crate::hook_runtime::inspect_pending_input;
use crate::hook_runtime::record_additional_contexts;
@@ -30,7 +28,6 @@ use crate::injection::tool_kind_for_path;
use crate::mcp_skill_dependencies::maybe_prompt_and_install_mcp_dependencies;
use crate::mcp_tool_exposure::build_mcp_tool_exposure;
use crate::mentions::build_connector_slug_counts;
use crate::mentions::build_skill_name_counts;
use crate::mentions::collect_explicit_app_ids;
use crate::mentions::collect_explicit_plugin_mentions;
use crate::mentions::collect_tool_mentions_from_messages;
@@ -71,11 +68,12 @@ use codex_analytics::AppInvocation;
use codex_analytics::CompactionPhase;
use codex_analytics::CompactionReason;
use codex_analytics::InvocationType;
use codex_analytics::SkillInvocation;
use codex_analytics::TurnResolvedConfigFact;
use codex_analytics::build_track_events_context;
use codex_async_utils::OrCancelExt;
use codex_core_plugins::RecommendedPluginCandidatesInput;
use codex_core_skills::injection::InjectedHostSkillPrompts;
use codex_core_skills::SkillInjectionIdentity;
use codex_extension_api::TurnInputContext;
use codex_extension_api::TurnInputEnvironment;
use codex_features::Feature;
@@ -171,19 +169,28 @@ pub(crate) async fn run_turn(
.record_context_updates_and_set_reference_context_item(first_step_context.as_ref())
.await;
let Some((injection_items, explicitly_enabled_connectors)) =
build_skills_and_plugins(&sess, turn_context.as_ref(), &input, &cancellation_token).await
else {
return Ok(None);
};
if run_pending_session_start_hooks(&sess, &turn_context).await {
return Ok(None);
}
let mut can_drain_pending_input = input.is_empty();
if run_hooks_and_record_inputs(&sess, &turn_context, &input).await {
let (accepted_input, should_stop) =
run_hooks_and_record_inputs(&sess, &turn_context, &input).await;
if should_stop {
return Ok(None);
}
let mut accepted_user_input = user_input_from_turn_input(&accepted_input);
let Some((injection_items, explicitly_enabled_connectors, available_connectors)) =
build_skills_and_plugins(
&sess,
turn_context.as_ref(),
&accepted_input,
&cancellation_token,
)
.await
else {
return Ok(None);
};
sess.merge_connector_selection(explicitly_enabled_connectors.clone())
.await;
@@ -217,6 +224,7 @@ pub(crate) async fn run_turn(
// 2. After auto-compact, when model/tool continuation needs to resume before any steer.
let mut next_step_context = Some(first_step_context);
let mut skill_injection_state = SkillInjectionState::default();
loop {
// Note that pending_input would be something like a message the user
// submitted through the UI while the model was running. Though the UI
@@ -227,9 +235,12 @@ pub(crate) async fn run_turn(
Vec::new()
};
if run_hooks_and_record_inputs(&sess, &turn_context, &pending_input).await {
let (accepted_pending_input, should_stop) =
run_hooks_and_record_inputs(&sess, &turn_context, &pending_input).await;
if should_stop {
break;
}
accepted_user_input.extend(user_input_from_turn_input(&accepted_pending_input));
let window_id = sess.current_window_id().await;
super::rollout_budget::maybe_record_reminder(
@@ -252,15 +263,19 @@ pub(crate) async fn run_turn(
)
.await?;
if turn_context
.config
.features
.enabled(Feature::DeferredExecutor)
{
world_state = sess
.record_step_world_state_if_changed(&world_state, step_context.as_ref())
.await;
}
world_state = sess
.record_step_world_state_if_changed(&world_state, step_context.as_ref())
.await;
let _mcp_refreshed = record_skill_injections(
&sess,
turn_context.as_ref(),
step_context.as_ref(),
&accepted_user_input,
&available_connectors,
&cancellation_token,
&mut skill_injection_state,
)
.await;
// Construct the input that we will send to the model.
let sampling_request_input: Vec<ResponseItem> = async {
@@ -360,6 +375,7 @@ pub(crate) async fn run_turn(
return Ok(None);
}
can_drain_pending_input = !model_needs_follow_up;
skill_injection_state = SkillInjectionState::default();
continue;
}
@@ -477,9 +493,10 @@ async fn run_hooks_and_record_inputs(
sess: &Arc<Session>,
turn_context: &Arc<TurnContext>,
input: &[TurnInput],
) -> bool {
) -> (Vec<TurnInput>, bool) {
let mut blocked_input = false;
let mut accepted_user_input = false;
let mut accepted_input = Vec::with_capacity(input.len());
for input_item in input {
let hook_outcome = inspect_pending_input(sess, turn_context, input_item).await;
if hook_outcome.should_stop {
@@ -496,9 +513,216 @@ async fn run_hooks_and_record_inputs(
hook_outcome.additional_contexts,
)
.await;
accepted_input.push(input_item.clone());
}
}
blocked_input && !accepted_user_input
(accepted_input, blocked_input && !accepted_user_input)
}
fn user_input_from_turn_input(input: &[TurnInput]) -> Vec<UserInput> {
input
.iter()
.filter_map(|item| match item {
TurnInput::UserInput { content, .. } => Some(content.as_slice()),
TurnInput::ResponseItem(_) | TurnInput::InterAgentCommunication(_) => None,
})
.flatten()
.cloned()
.collect()
}
const MAX_SKILL_INJECTIONS_PER_TURN: usize = 8;
const MAX_SKILL_INJECTION_BYTES_PER_TURN: usize = 32 * 1024;
#[derive(Default)]
struct SkillInjectionState {
active: HashMap<String, SkillInjectionIdentity>,
injected_items: usize,
injected_bytes: usize,
emitted_warnings: HashSet<String>,
}
async fn record_skill_injections(
sess: &Arc<Session>,
turn_context: &TurnContext,
step_context: &StepContext,
input: &[UserInput],
available_connectors: &[connectors::AppInfo],
cancellation_token: &CancellationToken,
state: &mut SkillInjectionState,
) -> bool {
if crate::guardian::is_guardian_reviewer_source(&turn_context.session_source) {
return false;
}
for warning in step_context.skills.warnings() {
emit_skill_warning_once(
sess,
turn_context,
warning.clone(),
&mut state.emitted_warnings,
)
.await;
}
let plain_name_conflicts = build_connector_slug_counts(available_connectors)
.into_keys()
.map(|slug| slug.to_ascii_lowercase())
.collect();
let injections = step_context
.skills
.injections(
input,
&plain_name_conflicts,
&state.active,
MAX_SKILL_INJECTIONS_PER_TURN.saturating_sub(state.injected_items),
)
.await;
for warning in injections.warnings {
emit_skill_warning_once(sess, turn_context, warning, &mut state.emitted_warnings).await;
}
let mut accepted = Vec::new();
let mut planned_bytes = state.injected_bytes;
for injection in &injections.items {
let remaining_bytes = MAX_SKILL_INJECTION_BYTES_PER_TURN.saturating_sub(planned_bytes);
let Some((instructions, truncated)) = SkillInstructions::from_runtime(
&injection.entry,
&injection.contents,
remaining_bytes,
state.active.contains_key(&injection.entry.name),
) else {
emit_skill_warning_once(
sess,
turn_context,
"Additional selected skills were not loaded because this turn reached the 32 KB skill instruction limit."
.to_string(),
&mut state.emitted_warnings,
)
.await;
break;
};
if truncated {
emit_skill_warning_once(
sess,
turn_context,
format!(
"Skill `{}` exceeded the per-skill instruction limit and was truncated.",
injection.entry.name
),
&mut state.emitted_warnings,
)
.await;
}
let rendered_bytes = instructions.render().len();
planned_bytes = planned_bytes.saturating_add(rendered_bytes);
accepted.push((injection, instructions, rendered_bytes));
}
let replacement_names = accepted
.iter()
.map(|(injection, _, _)| injection.entry.name.as_str())
.collect::<HashSet<_>>();
let unavailable_names = state
.active
.iter()
.filter(|(name, identity)| {
!replacement_names.contains(name.as_str())
&& !step_context.skills.contains_injection(identity)
})
.map(|(name, _)| name.clone())
.collect::<Vec<_>>();
let mut items = unavailable_names
.iter()
.map(|name| ContextualUserFragment::into(SkillInstructions::unavailable(name)))
.collect::<Vec<ResponseItem>>();
for name in unavailable_names {
state.active.remove(&name);
}
if accepted.is_empty() {
if !items.is_empty() {
sess.record_conversation_items(turn_context, &items).await;
}
return false;
}
let selected_entries = accepted
.iter()
.map(|(injection, _, _)| injection.entry.clone())
.collect::<Vec<_>>();
let mcp_refreshed = maybe_prompt_and_install_mcp_dependencies(
sess,
turn_context,
cancellation_token,
&selected_entries,
Some(sess.mcp_elicitation_reviewer()),
)
.await;
items.extend(
accepted
.iter()
.map(|(_, instructions, _)| ContextualUserFragment::into(instructions.clone())),
);
let connector_ids = collect_explicit_app_ids_from_skill_items(
&items,
available_connectors,
&step_context.skills.skill_name_counts_lower(),
);
sess.merge_connector_selection(connector_ids).await;
let skill_invocations = accepted
.iter()
.filter_map(|(injection, _, _)| {
step_context
.skills
.host_skill(&injection.entry)
.map(|skill| SkillInvocation {
skill_name: skill.name.clone(),
skill_scope: skill.scope,
skill_path: skill.path_to_skills_md.to_path_buf(),
plugin_id: skill.plugin_id.clone(),
invocation_type: InvocationType::Explicit,
})
})
.collect::<Vec<_>>();
sess.services
.analytics_events_client
.track_skill_invocations(
build_track_events_context(
turn_context.model_info.slug.clone(),
sess.thread_id.to_string(),
turn_context.sub_id.clone(),
turn_context.originator.clone(),
),
skill_invocations,
);
for (injection, _, rendered_bytes) in accepted {
turn_context.session_telemetry.counter(
"codex.skill.injected",
/*inc*/ 1,
&[("status", "ok"), ("skill", injection.entry.name.as_str())],
);
state.injected_items = state.injected_items.saturating_add(1);
state.injected_bytes = state.injected_bytes.saturating_add(rendered_bytes);
state
.active
.insert(injection.entry.name.clone(), injection.identity.clone());
}
if !items.is_empty() {
sess.record_conversation_items(turn_context, &items).await;
}
mcp_refreshed
}
async fn emit_skill_warning_once(
sess: &Session,
turn_context: &TurnContext,
message: String,
emitted: &mut HashSet<String>,
) {
if emitted.insert(message.clone()) {
sess.send_event(turn_context, EventMsg::Warning(WarningEvent { message }))
.await;
}
}
#[instrument(level = "trace", skip_all)]
@@ -507,22 +731,14 @@ async fn build_skills_and_plugins(
turn_context: &TurnContext,
input: &[TurnInput],
cancellation_token: &CancellationToken,
) -> Option<(Vec<ResponseItem>, HashSet<String>)> {
) -> Option<(Vec<ResponseItem>, HashSet<String>, Vec<connectors::AppInfo>)> {
// Guardian input embeds the parent transcript as untrusted evidence. Do not interpret skill or
// plugin mentions from that generated prompt as requests to inject additional instructions.
if crate::guardian::is_guardian_reviewer_source(&turn_context.session_source) {
return Some((Vec::new(), HashSet::new()));
return Some((Vec::new(), HashSet::new(), Vec::new()));
}
let user_input = input
.iter()
.filter_map(|item| match item {
TurnInput::UserInput { content, .. } => Some(content.as_slice()),
TurnInput::ResponseItem(_) | TurnInput::InterAgentCommunication(_) => None,
})
.flatten()
.cloned()
.collect::<Vec<_>>();
let user_input = user_input_from_turn_input(input);
let tracking = build_track_events_context(
turn_context.model_info.slug.clone(),
sess.thread_id.to_string(),
@@ -572,61 +788,12 @@ async fn build_skills_and_plugins(
} else {
Vec::new()
};
let skills_outcome = turn_context.turn_skills.snapshot.outcome();
let connector_slug_counts = build_connector_slug_counts(&available_connectors);
let extension_injection_items =
build_extension_turn_input_items(sess, turn_context, &user_input, cancellation_token)
.await?;
let skill_name_counts_lower =
build_skill_name_counts(&skills_outcome.skills, &skills_outcome.disabled_paths).1;
let mentioned_skills = collect_explicit_skill_mentions(
&user_input,
&skills_outcome.skills,
&skills_outcome.disabled_paths,
&connector_slug_counts,
);
maybe_prompt_and_install_mcp_dependencies(
sess,
turn_context,
cancellation_token,
&mentioned_skills,
Some(sess.mcp_elicitation_reviewer()),
)
.await;
let injected_host_skill_prompts = turn_context
.extension_data
.get::<InjectedHostSkillPrompts>();
let SkillInjections {
items: skill_injections,
warnings: skill_warnings,
} = build_skill_injections(
&mentioned_skills,
Some(skills_outcome),
Some(&turn_context.session_telemetry),
&sess.services.analytics_events_client,
tracking.clone(),
)
.await;
for message in skill_warnings {
sess.send_event(turn_context, EventMsg::Warning(WarningEvent { message }))
.await;
}
let skill_items: Vec<ResponseItem> = skill_injections
.iter()
.map(|skill| ContextualUserFragment::into(crate::context::SkillInstructions::from(skill)))
.collect();
let skill_connector_ids = collect_explicit_app_ids_from_skill_items(
&skill_items,
&available_connectors,
&skill_name_counts_lower,
);
let plugin_items =
build_plugin_injections(&mentioned_plugins, &mcp_tools, &available_connectors);
let mut explicitly_enabled_connectors = collect_explicit_app_ids(&user_input);
explicitly_enabled_connectors.extend(skill_connector_ids);
let explicitly_enabled_connectors = collect_explicit_app_ids(&user_input);
let connector_names_by_id = available_connectors
.iter()
.map(|connector| (connector.id.as_str(), connector.name.as_str()))
@@ -656,19 +823,13 @@ async fn build_skills_and_plugins(
}
}
let mut injection_items: Vec<ResponseItem> = match injected_host_skill_prompts {
Some(injected_host_skill_prompts) => skill_injections
.iter()
.filter(|skill| !injected_host_skill_prompts.contains_path(&skill.path))
.map(|skill| {
ContextualUserFragment::into(crate::context::SkillInstructions::from(skill))
})
.collect(),
None => skill_items,
};
injection_items.extend(plugin_items);
let mut injection_items = plugin_items;
injection_items.extend(extension_injection_items);
Some((injection_items, explicitly_enabled_connectors))
Some((
injection_items,
explicitly_enabled_connectors,
available_connectors,
))
}
#[tracing::instrument(
+5
View File
@@ -2,6 +2,7 @@ use super::session::Session;
use super::step_context::StepContext;
use crate::context::world_state::AgentsMdState;
use crate::context::world_state::EnvironmentsState;
use crate::context::world_state::SkillsState;
use crate::context::world_state::WorldState;
impl Session {
@@ -25,6 +26,10 @@ impl Session {
let mut world_state = WorldState::default();
world_state.add_section(AgentsMdState::new(step_context.loaded_agents_md.as_deref()));
world_state.add_section(SkillsState::new(
step_context.skills.as_ref(),
turn_context.config.include_skill_instructions,
));
if turn_context.config.include_environment_context {
world_state.add_section(
EnvironmentsState::from_turn_context_with_environments(
-3
View File
@@ -22,9 +22,6 @@ pub use codex_core_skills::default_skill_metadata_budget;
pub use codex_core_skills::detect_implicit_skill_invocation_for_command;
pub use codex_core_skills::filter_skill_load_outcome_for_product;
pub use codex_core_skills::injection;
pub use codex_core_skills::injection::SkillInjections;
pub use codex_core_skills::injection::build_skill_injections;
pub use codex_core_skills::injection::collect_explicit_skill_mentions;
pub use codex_core_skills::loader;
pub use codex_core_skills::model;
pub use codex_core_skills::remote;
@@ -685,11 +685,16 @@ async fn environment_tools_follow_the_step_context() {
turn.model_info.apply_patch_tool_type = Some(ApplyPatchToolType::Freeform);
let environments = turn.environments.clone();
let skills = Arc::new(codex_core_skills::SkillsSnapshot::from_host(
turn.turn_skills.snapshot.clone(),
turn.model_context_window(),
));
turn.environments.turn_environments.clear();
let step_context = Arc::new(StepContext::new(
Arc::new(turn),
environments,
Vec::new(),
skills,
/*loaded_agents_md*/ None,
));
+9 -1
View File
@@ -26,6 +26,7 @@ use codex_core::thread_store_from_config;
use codex_exec_server::CreateDirectoryOptions;
use codex_exec_server::ExecutorFileSystem;
use codex_exec_server::RemoveOptions;
use codex_extension_api::ExtensionDataInit;
use codex_extension_api::ExtensionRegistry;
use codex_extension_api::LoadUserInstructionsFuture;
use codex_extension_api::UserInstructionsProvider;
@@ -287,6 +288,7 @@ pub struct TestCodexBuilder {
user_shell_override: Option<Shell>,
exec_server_url: Option<String>,
extensions: Arc<ExtensionRegistry<Config>>,
thread_extension_init: ExtensionDataInit,
user_instructions_provider: Option<Arc<dyn UserInstructionsProvider>>,
supports_openai_form_elicitation: bool,
external_time_provider: Option<Arc<dyn TimeProvider>>,
@@ -378,6 +380,11 @@ impl TestCodexBuilder {
self
}
pub fn with_thread_extension_init(mut self, thread_extension_init: ExtensionDataInit) -> Self {
self.thread_extension_init = thread_extension_init;
self
}
pub fn with_user_instructions_provider(
mut self,
provider: Arc<dyn UserInstructionsProvider>,
@@ -665,7 +672,7 @@ impl TestCodexBuilder {
metrics_service_name: None,
parent_trace: None,
environments,
thread_extension_init: Default::default(),
thread_extension_init: std::mem::take(&mut self.thread_extension_init),
supports_openai_form_elicitation: self.supports_openai_form_elicitation,
}),
)
@@ -1214,6 +1221,7 @@ pub fn test_codex() -> TestCodexBuilder {
user_shell_override: None,
exec_server_url: None,
extensions: empty_extension_registry(),
thread_extension_init: ExtensionDataInit::new(),
user_instructions_provider: None,
supports_openai_form_elicitation: false,
external_time_provider: None,
+171
View File
@@ -12,6 +12,8 @@ use codex_exec_server::LOCAL_ENVIRONMENT_ID;
use codex_exec_server::REMOTE_ENVIRONMENT_ID;
use codex_exec_server::RemoveOptions;
use codex_features::Feature;
use codex_protocol::capabilities::CapabilityRootLocation;
use codex_protocol::capabilities::SelectedCapabilityRoot;
use codex_protocol::models::FileSystemPermissions;
use codex_protocol::models::PermissionProfile;
use codex_protocol::permissions::FileSystemAccessMode;
@@ -468,6 +470,54 @@ async fn serve_environment_with_agents_md(
}
}
async fn serve_environment_with_skill(
listener: TcpListener,
skill_path: PathUri,
skill_contents: String,
attach: tokio::sync::oneshot::Receiver<()>,
mut shutdown: tokio::sync::oneshot::Receiver<()>,
) -> usize {
let mut websocket = accept_initialized_exec_server(listener).await;
attach.await.expect("attach signal");
send_environment_info(&mut websocket).await;
let mut reads = 0;
let skill_path_string = skill_path.to_string();
loop {
let request = tokio::select! {
request = read_exec_server_json(&mut websocket) => request,
_ = &mut shutdown => return reads,
};
let response = match request["method"].as_str() {
Some("fs/walk") => json!({
"id": request["id"],
"result": {
"entries": [{ "path": skill_path, "kind": "file" }],
"errors": [],
"truncated": false,
}
}),
Some("fs/getMetadata") => json!({
"id": request["id"],
"error": { "code": -32004, "message": "not found" }
}),
Some("fs/readFile")
if request["params"]["path"].as_str() == Some(skill_path_string.as_str()) =>
{
reads += 1;
json!({
"id": request["id"],
"result": { "dataBase64": BASE64_STANDARD.encode(&skill_contents) }
})
}
method => panic!("unexpected exec-server request: {method:?}"),
};
websocket
.send(Message::Text(response.to_string().into()))
.await
.expect("filesystem response");
}
}
fn tool_names(body: &Value) -> Vec<String> {
body["tools"]
.as_array()
@@ -730,6 +780,127 @@ async fn deferred_executor_loads_agents_md_when_environment_becomes_ready() -> R
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn deferred_executor_replaces_a_colliding_host_skill_for_the_next_step() -> Result<()> {
const HOST_BODY: &str = "HOST_SKILL_BODY_MARKER";
const EXECUTOR_BODY: &str = "EXECUTOR_SKILL_BODY_MARKER";
const SKILL_REPLACEMENT_NOTICE: &str =
"These instructions replace the previously provided instructions for this skill.";
let root_path = PathUri::from_host_native_path("/remote-capability")?;
let skill_path = root_path.join("skills/deploy/SKILL.md")?;
let skill_contents = format!(
"---\nname: deploy\ndescription: Deploy through the ready executor.\n---\n\n{EXECUTOR_BODY}\n"
);
let selected_capability_roots = vec![SelectedCapabilityRoot {
id: "remote-capability@1".to_string(),
location: CapabilityRootLocation::Environment {
environment_id: REMOTE_ENVIRONMENT_ID.to_string(),
path: root_path,
},
}];
let listener = TcpListener::bind("127.0.0.1:0").await?;
let server = start_mock_server().await;
let response_mock = mount_sse_sequence(
&server,
vec![
sse(vec![
ev_response_created("resp-1"),
ev_function_call(
"wait-1",
"wait_for_environment",
&json!({ "environment_id": REMOTE_ENVIRONMENT_ID }).to_string(),
),
ev_completed("resp-1"),
]),
sse(vec![
ev_response_created("resp-2"),
ev_assistant_message("msg-2", "done"),
ev_completed("resp-2"),
]),
],
)
.await;
let mut builder = test_codex()
.with_exec_server_url(format!("ws://{}", listener.local_addr()?))
.with_selected_capability_roots(selected_capability_roots)
.with_pre_build_hook(|home| {
let skill_dir = home.join("skills/deploy");
std::fs::create_dir_all(&skill_dir).expect("create host skill directory");
std::fs::write(
skill_dir.join("SKILL.md"),
format!(
"---\nname: deploy\ndescription: Deploy from the host.\n---\n\n{HOST_BODY}\n"
),
)
.expect("write host skill");
})
.with_config(|config| {
config.project_doc_max_bytes = 0;
assert!(config.features.enable(Feature::DeferredExecutor).is_ok());
});
let (attach_tx, attach_rx) = tokio::sync::oneshot::channel();
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
let exec_server = tokio::spawn(serve_environment_with_skill(
listener,
skill_path,
skill_contents,
attach_rx,
shutdown_rx,
));
let test = timeout(Duration::from_secs(5), builder.build(&server))
.await
.context("thread startup should not wait for the remote environment")??;
test.codex
.submit(Op::UserInput {
items: vec![UserInput::Text {
text: "Use $deploy after the environment is ready".into(),
text_elements: Vec::new(),
}],
final_output_json_schema: None,
responsesapi_client_metadata: None,
additional_context: Default::default(),
thread_settings: Default::default(),
})
.await?;
wait_for_response_request_count(&response_mock, /*expected_count*/ 1).await;
attach_tx.send(()).expect("attach environment");
wait_for_event(&test.codex, |event| {
matches!(event, EventMsg::TurnComplete(_))
})
.await;
shutdown_tx.send(()).expect("stop exec server");
assert_eq!(exec_server.await?, 2);
let requests = response_mock.requests();
assert_eq!(requests.len(), 2);
let first_user_context = requests[0].message_input_texts("user");
let host_skill = first_user_context
.iter()
.find(|text| text.starts_with("<skill>") && text.contains(HOST_BODY))
.expect("first request should contain host skill instructions");
assert!(!host_skill.contains(SKILL_REPLACEMENT_NOTICE));
assert!(
first_user_context
.iter()
.all(|text| !text.contains(EXECUTOR_BODY))
);
let second_user_context = requests[1].message_input_texts("user");
let executor_skill = second_user_context
.iter()
.find(|text| text.starts_with("<skill>") && text.contains(EXECUTOR_BODY))
.expect("second request should contain executor skill instructions");
assert!(executor_skill.contains(SKILL_REPLACEMENT_NOTICE));
let second_developer_context = requests[1].message_input_texts("developer");
let replacement_catalog = second_developer_context
.iter()
.find(|text| text.contains("This skills list replaces"))
.expect("second request should contain the replacement skill catalog");
assert!(replacement_catalog.contains("(environment resource:"));
Ok(())
}
fn agents_md_occurrences(request: &ResponsesRequest, contents: &str) -> usize {
request
.message_input_texts("user")
-38
View File
@@ -1,38 +0,0 @@
use codex_core_skills::AvailableSkills;
use codex_core_skills::render_available_skills_body;
use codex_extension_api::ContextualUserFragment;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_CLOSE_TAG;
use codex_protocol::protocol::SKILLS_INSTRUCTIONS_OPEN_TAG;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct AvailableSkillsInstructions {
skill_root_lines: Vec<String>,
skill_lines: Vec<String>,
}
impl From<AvailableSkills> for AvailableSkillsInstructions {
fn from(available: AvailableSkills) -> Self {
Self {
skill_root_lines: available.skill_root_lines,
skill_lines: available.skill_lines,
}
}
}
impl ContextualUserFragment for AvailableSkillsInstructions {
fn role(&self) -> &'static str {
"developer"
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
(SKILLS_INSTRUCTIONS_OPEN_TAG, SKILLS_INSTRUCTIONS_CLOSE_TAG)
}
fn body(&self) -> String {
render_available_skills_body(&self.skill_root_lines, &self.skill_lines)
}
}
-24
View File
@@ -1,34 +1,10 @@
use std::borrow::Cow;
use codex_core_skills::SkillRenderSideEffects;
use codex_core_skills::build_available_skills_from_catalog;
use codex_core_skills::default_skill_metadata_budget;
use codex_utils_string::take_bytes_at_char_boundary;
use crate::catalog::SkillCatalog;
use crate::fragments::AvailableSkillsInstructions;
const MAX_CATALOG_SKILL_DESCRIPTION_CHARS: usize = 1_024;
const TRUNCATED_SKILL_DESCRIPTION_SUFFIX: &str = "...";
#[tracing::instrument(
level = "trace",
skip_all,
fields(catalog_entry_count = catalog.entries.len())
)]
pub(crate) fn available_skills_fragment(
catalog: &SkillCatalog,
) -> Option<(AvailableSkillsInstructions, Option<String>)> {
let available = build_available_skills_from_catalog(
catalog,
/*host_outcome*/ None,
default_skill_metadata_budget(/*context_window*/ None),
SkillRenderSideEffects::None,
)?;
let warning = available.warning_message.clone();
Some((available.into(), warning))
}
pub(crate) fn truncate_catalog_skill_description(description: &str) -> Cow<'_, str> {
if description
.char_indices()