mirror of
https://github.com/openai/codex.git
synced 2026-11-29 07:35:16 +00:00
Move executor skill bundle loading into the skills extension (#36877)
## What changed - Move parsing of pre-discovered executor skill bundles from `core-skills` to the skills extension. - Expose shared `SKILL.md` frontmatter parsing from `codex-skills` so direct and pre-discovered loading use the same validation and repair behavior. ## Testing - Add parity coverage for direct and pre-discovered executor skill catalogs, including plugin namespaces, metadata, product policy, and warnings. - Add a snapshot for the resulting pre-discovered executor catalog. GitOrigin-RevId: c5f888226fa5600bd8b90f5682400da39a5db5ff
This commit is contained in:
1 parent
2a16af8234
commit
4c25d6cc5c
17 files changed
+1138
-488
No files matched your search
@@ -0,0 +1,165 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use codex_exec_server::CapabilityRootDiscovery;
|
||||
use codex_protocol::protocol::Product;
|
||||
use codex_skills::EnvironmentSkillMetadata;
|
||||
use codex_skills::ParsedSkillFrontmatter;
|
||||
use codex_skills::parse_skill_frontmatter_metadata;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
|
||||
use super::MAX_QUALIFIED_NAME_LEN;
|
||||
use super::metadata::SkillMetadataFile;
|
||||
use super::metadata::resolve_dependencies;
|
||||
use super::metadata::resolve_policy;
|
||||
use super::metadata::sanitize_single_line;
|
||||
use super::metadata::validate_len;
|
||||
|
||||
/// Parsed executor skill plus the instructions already materialized by capability discovery.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct EnvironmentSkillSnapshot {
|
||||
pub metadata: EnvironmentSkillMetadata,
|
||||
pub instructions: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct EnvironmentSkillSnapshotOutcome {
|
||||
pub skills: Vec<EnvironmentSkillSnapshot>,
|
||||
pub warnings: Vec<String>,
|
||||
}
|
||||
|
||||
/// Parses an executor-produced manifest bundle without issuing additional filesystem requests.
|
||||
pub fn load_environment_skills_from_discovery(
|
||||
discovery: &CapabilityRootDiscovery,
|
||||
restriction_product: Option<Product>,
|
||||
) -> EnvironmentSkillSnapshotOutcome {
|
||||
let mut outcome = EnvironmentSkillSnapshotOutcome {
|
||||
warnings: discovery.warnings.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
if let Some(error) = &discovery.error {
|
||||
outcome.warnings.push(error.clone());
|
||||
return outcome;
|
||||
}
|
||||
|
||||
let mut plugin_namespaces = HashMap::new();
|
||||
for (plugin_root, name) in discovery.namespace_manifests.iter().filter_map(|manifest| {
|
||||
#[derive(serde::Deserialize)]
|
||||
struct ManifestName {
|
||||
#[serde(default)]
|
||||
name: String,
|
||||
}
|
||||
|
||||
let plugin_root = manifest.path.parent()?.parent()?;
|
||||
let parsed = serde_json::from_str::<ManifestName>(&manifest.contents).ok()?;
|
||||
let name = if parsed.name.trim().is_empty() {
|
||||
plugin_root.basename()?
|
||||
} else {
|
||||
parsed.name
|
||||
};
|
||||
Some((plugin_root, name))
|
||||
}) {
|
||||
// Exec-server orders manifests by the same precedence as local discovery. Preserve the
|
||||
// first manifest if an older or alternate server returns duplicates for one plugin root.
|
||||
plugin_namespaces.entry(plugin_root).or_insert(name);
|
||||
}
|
||||
|
||||
for skill in &discovery.skills {
|
||||
let plugin_namespace =
|
||||
nearest_plugin_namespace(&skill.instructions.path, &plugin_namespaces);
|
||||
let ParsedSkillFrontmatter {
|
||||
name: base_name,
|
||||
description,
|
||||
short_description,
|
||||
} = match parse_skill_frontmatter_metadata(&skill.instructions.contents, || {
|
||||
default_skill_name(&skill.instructions.path)
|
||||
}) {
|
||||
Ok(frontmatter) => frontmatter,
|
||||
Err(error) => {
|
||||
outcome.warnings.push(format!(
|
||||
"Failed to load environment skill at {}: {error}",
|
||||
skill.instructions.path
|
||||
));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let name = plugin_namespace
|
||||
.map(|namespace| format!("{namespace}:{base_name}"))
|
||||
.unwrap_or(base_name);
|
||||
if let Err(error) = validate_len(&name, MAX_QUALIFIED_NAME_LEN, "qualified name") {
|
||||
outcome.warnings.push(format!(
|
||||
"Failed to load environment skill at {}: {error}",
|
||||
skill.instructions.path
|
||||
));
|
||||
continue;
|
||||
}
|
||||
let (dependencies, policy) = skill
|
||||
.metadata
|
||||
.as_ref()
|
||||
.and_then(|metadata| {
|
||||
serde_yaml::from_str::<SkillMetadataFile>(&metadata.contents)
|
||||
.map_err(|error| {
|
||||
tracing::warn!(
|
||||
path = %metadata.path,
|
||||
"ignoring invalid discovered skill metadata: {error}"
|
||||
);
|
||||
})
|
||||
.ok()
|
||||
})
|
||||
.map(|metadata| {
|
||||
(
|
||||
resolve_dependencies(metadata.dependencies),
|
||||
resolve_policy(metadata.policy),
|
||||
)
|
||||
})
|
||||
.unwrap_or((None, None));
|
||||
let metadata = EnvironmentSkillMetadata {
|
||||
path_to_skills_md: skill.instructions.path.clone(),
|
||||
name,
|
||||
description,
|
||||
short_description,
|
||||
dependencies,
|
||||
policy,
|
||||
};
|
||||
if metadata.matches_product_restriction(restriction_product) {
|
||||
outcome.skills.push(EnvironmentSkillSnapshot {
|
||||
metadata,
|
||||
instructions: skill.instructions.contents.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
outcome.skills.sort_by(|left, right| {
|
||||
left.metadata.name.cmp(&right.metadata.name).then_with(|| {
|
||||
left.metadata
|
||||
.path_to_skills_md
|
||||
.to_string()
|
||||
.cmp(&right.metadata.path_to_skills_md.to_string())
|
||||
})
|
||||
});
|
||||
outcome
|
||||
}
|
||||
|
||||
fn nearest_plugin_namespace<'a>(
|
||||
skill_path: &PathUri,
|
||||
plugin_namespaces: &'a HashMap<PathUri, String>,
|
||||
) -> Option<&'a str> {
|
||||
let mut ancestor = skill_path.parent();
|
||||
while let Some(path) = ancestor {
|
||||
if let Some(namespace) = plugin_namespaces.get(&path) {
|
||||
return Some(namespace);
|
||||
}
|
||||
ancestor = path.parent();
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn default_skill_name(path: &PathUri) -> String {
|
||||
path.parent()
|
||||
.and_then(|parent| parent.basename())
|
||||
.map(|name| sanitize_single_line(&name))
|
||||
.filter(|name| !name.is_empty())
|
||||
.unwrap_or_else(|| "skill".to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "environment_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,151 @@
|
||||
use std::fs;
|
||||
|
||||
use codex_core_skills::loader::load_environment_skills_from_root;
|
||||
use codex_exec_server::CapabilityRootDiscoverRequest;
|
||||
use codex_exec_server::CapabilityRootsDiscoverParams;
|
||||
use codex_exec_server::LOCAL_FS;
|
||||
use codex_exec_server::discover_capability_roots;
|
||||
use codex_utils_path_uri::PathUri;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::tempdir;
|
||||
|
||||
use super::load_environment_skills_from_discovery;
|
||||
|
||||
#[tokio::test]
|
||||
async fn executor_bundle_parser_matches_the_existing_environment_loader() {
|
||||
let root = tempdir().expect("tempdir");
|
||||
let plugin_manifest = root.path().join(".codex-plugin/plugin.json");
|
||||
let nested_manifest = root.path().join("nested/.claude-plugin/plugin.json");
|
||||
let deploy_skill = root.path().join("skills/deploy/SKILL.md");
|
||||
let deploy_metadata = root.path().join("skills/deploy/agents/openai.yaml");
|
||||
let audit_skill = root.path().join("nested/skills/audit/SKILL.md");
|
||||
for (path, contents) in [
|
||||
(&plugin_manifest, r#"{"name":"demo"}"#),
|
||||
(&nested_manifest, r#"{"name":"nested"}"#),
|
||||
(
|
||||
&deploy_skill,
|
||||
"---\nname: deploy\ndescription: Deploy the service.\n---\n\nDeploy.\n",
|
||||
),
|
||||
(
|
||||
&deploy_metadata,
|
||||
"policy:\n allow_implicit_invocation: false\n",
|
||||
),
|
||||
(
|
||||
&audit_skill,
|
||||
"---\nname: audit\ndescription: Audit the service.\n---\n\nAudit.\n",
|
||||
),
|
||||
] {
|
||||
fs::create_dir_all(path.parent().expect("test file parent")).expect("test directory");
|
||||
fs::write(path, contents).expect("test file");
|
||||
}
|
||||
|
||||
let root_uri = PathUri::from_host_native_path(root.path()).expect("root URI");
|
||||
let existing = load_environment_skills_from_root(
|
||||
LOCAL_FS.as_ref(),
|
||||
&root_uri,
|
||||
/*restriction_product*/ None,
|
||||
)
|
||||
.await;
|
||||
let response = discover_capability_roots(
|
||||
LOCAL_FS.as_ref(),
|
||||
CapabilityRootsDiscoverParams {
|
||||
roots: vec![CapabilityRootDiscoverRequest {
|
||||
id: "demo@1".to_string(),
|
||||
path: root_uri,
|
||||
sandbox: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("capability discovery");
|
||||
let bundled = load_environment_skills_from_discovery(
|
||||
response.roots.first().expect("discovered root"),
|
||||
/*restriction_product*/ None,
|
||||
);
|
||||
|
||||
assert_eq!(bundled.warnings, existing.warnings);
|
||||
assert_eq!(
|
||||
bundled
|
||||
.skills
|
||||
.iter()
|
||||
.map(|skill| skill.metadata.clone())
|
||||
.collect::<Vec<_>>(),
|
||||
existing.skills
|
||||
);
|
||||
assert_eq!(
|
||||
bundled
|
||||
.skills
|
||||
.iter()
|
||||
.map(|skill| skill.instructions.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
"---\nname: deploy\ndescription: Deploy the service.\n---\n\nDeploy.\n",
|
||||
"---\nname: audit\ndescription: Audit the service.\n---\n\nAudit.\n",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn executor_bundle_preserves_parent_namespace_and_manifest_precedence() {
|
||||
let plugin = tempdir().expect("tempdir");
|
||||
for (relative_path, name) in [
|
||||
(".codex-plugin/plugin.json", "codex-name"),
|
||||
(".claude-plugin/plugin.json", "claude-name"),
|
||||
(".cursor-plugin/plugin.json", "cursor-name"),
|
||||
] {
|
||||
let manifest = plugin.path().join(relative_path);
|
||||
fs::create_dir_all(manifest.parent().expect("manifest parent"))
|
||||
.expect("manifest directory");
|
||||
fs::write(&manifest, format!(r#"{{"name":"{name}"}}"#)).expect("manifest");
|
||||
}
|
||||
let skills_root = plugin.path().join("skills");
|
||||
let skill_path = skills_root.join("search/SKILL.md");
|
||||
fs::create_dir_all(skill_path.parent().expect("skill parent")).expect("skill directory");
|
||||
fs::write(
|
||||
&skill_path,
|
||||
"---\nname: search\ndescription: Search the project.\n---\n\nSearch.\n",
|
||||
)
|
||||
.expect("skill");
|
||||
|
||||
let root_uri = PathUri::from_host_native_path(&skills_root).expect("skills root URI");
|
||||
let existing = load_environment_skills_from_root(
|
||||
LOCAL_FS.as_ref(),
|
||||
&root_uri,
|
||||
/*restriction_product*/ None,
|
||||
)
|
||||
.await;
|
||||
let response = discover_capability_roots(
|
||||
LOCAL_FS.as_ref(),
|
||||
CapabilityRootsDiscoverParams {
|
||||
roots: vec![CapabilityRootDiscoverRequest {
|
||||
id: "skills-only".to_string(),
|
||||
path: root_uri,
|
||||
sandbox: None,
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("capability discovery");
|
||||
let discovery = response.roots.first().expect("discovered root");
|
||||
let bundled =
|
||||
load_environment_skills_from_discovery(discovery, /*restriction_product*/ None);
|
||||
|
||||
assert_eq!(discovery.plugin, None);
|
||||
assert_eq!(discovery.namespace_manifests.len(), 1);
|
||||
assert!(
|
||||
discovery.namespace_manifests[0]
|
||||
.path
|
||||
.to_string()
|
||||
.ends_with("/.codex-plugin/plugin.json")
|
||||
);
|
||||
assert_eq!(bundled.warnings, existing.warnings);
|
||||
assert_eq!(
|
||||
bundled
|
||||
.skills
|
||||
.iter()
|
||||
.map(|skill| skill.metadata.clone())
|
||||
.collect::<Vec<_>>(),
|
||||
existing.skills
|
||||
);
|
||||
assert_eq!(bundled.skills[0].metadata.name, "codex-name:search");
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use codex_protocol::protocol::Product;
|
||||
use codex_skills::SkillDependencies;
|
||||
use codex_skills::SkillParseError;
|
||||
use codex_skills::SkillPolicy;
|
||||
use codex_skills::SkillToolDependency;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::MAX_DEPENDENCY_COMMAND_LEN;
|
||||
use super::MAX_DEPENDENCY_DESCRIPTION_LEN;
|
||||
use super::MAX_DEPENDENCY_TRANSPORT_LEN;
|
||||
use super::MAX_DEPENDENCY_TYPE_LEN;
|
||||
use super::MAX_DEPENDENCY_URL_LEN;
|
||||
use super::MAX_DEPENDENCY_VALUE_LEN;
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub(super) struct SkillMetadataFile {
|
||||
// Keep parsing the host-only interface fields so malformed interface metadata
|
||||
// invalidates the file exactly as it does in the legacy loader.
|
||||
#[serde(default, rename = "interface")]
|
||||
_interface: Option<Interface>,
|
||||
#[serde(default)]
|
||||
pub(super) dependencies: Option<Dependencies>,
|
||||
#[serde(default)]
|
||||
pub(super) policy: Option<Policy>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
struct Interface {
|
||||
#[serde(rename = "display_name")]
|
||||
_display_name: Option<String>,
|
||||
#[serde(rename = "short_description")]
|
||||
_short_description: Option<String>,
|
||||
#[serde(rename = "icon_small")]
|
||||
_icon_small: Option<PathBuf>,
|
||||
#[serde(rename = "icon_large")]
|
||||
_icon_large: Option<PathBuf>,
|
||||
#[serde(rename = "brand_color")]
|
||||
_brand_color: Option<String>,
|
||||
#[serde(rename = "default_prompt")]
|
||||
_default_prompt: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub(super) struct Dependencies {
|
||||
#[serde(default)]
|
||||
tools: Vec<DependencyTool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(super) struct Policy {
|
||||
#[serde(default)]
|
||||
allow_implicit_invocation: Option<bool>,
|
||||
#[serde(default)]
|
||||
products: Vec<Product>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
struct DependencyTool {
|
||||
#[serde(rename = "type")]
|
||||
kind: Option<String>,
|
||||
value: Option<String>,
|
||||
description: Option<String>,
|
||||
transport: Option<String>,
|
||||
command: Option<String>,
|
||||
url: Option<String>,
|
||||
}
|
||||
|
||||
pub(super) fn resolve_dependencies(
|
||||
dependencies: Option<Dependencies>,
|
||||
) -> Option<SkillDependencies> {
|
||||
let dependencies = dependencies?;
|
||||
let tools = dependencies
|
||||
.tools
|
||||
.into_iter()
|
||||
.filter_map(resolve_dependency_tool)
|
||||
.collect::<Vec<_>>();
|
||||
if tools.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(SkillDependencies { tools })
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn resolve_policy(policy: Option<Policy>) -> Option<SkillPolicy> {
|
||||
policy.map(|policy| SkillPolicy {
|
||||
allow_implicit_invocation: policy.allow_implicit_invocation,
|
||||
products: policy.products,
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_dependency_tool(tool: DependencyTool) -> Option<SkillToolDependency> {
|
||||
let r#type = resolve_required_str(
|
||||
tool.kind,
|
||||
MAX_DEPENDENCY_TYPE_LEN,
|
||||
"dependencies.tools.type",
|
||||
)?;
|
||||
let value = resolve_required_str(
|
||||
tool.value,
|
||||
MAX_DEPENDENCY_VALUE_LEN,
|
||||
"dependencies.tools.value",
|
||||
)?;
|
||||
let description = resolve_str(
|
||||
tool.description,
|
||||
MAX_DEPENDENCY_DESCRIPTION_LEN,
|
||||
"dependencies.tools.description",
|
||||
);
|
||||
let transport = resolve_str(
|
||||
tool.transport,
|
||||
MAX_DEPENDENCY_TRANSPORT_LEN,
|
||||
"dependencies.tools.transport",
|
||||
);
|
||||
let command = resolve_str(
|
||||
tool.command,
|
||||
MAX_DEPENDENCY_COMMAND_LEN,
|
||||
"dependencies.tools.command",
|
||||
);
|
||||
let url = resolve_str(tool.url, MAX_DEPENDENCY_URL_LEN, "dependencies.tools.url");
|
||||
|
||||
Some(SkillToolDependency {
|
||||
r#type,
|
||||
value,
|
||||
description,
|
||||
transport,
|
||||
command,
|
||||
url,
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn sanitize_single_line(raw: &str) -> String {
|
||||
raw.split_whitespace().collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
pub(super) fn validate_len(
|
||||
value: &str,
|
||||
max_len: usize,
|
||||
field_name: &'static str,
|
||||
) -> Result<(), SkillParseError> {
|
||||
if value.is_empty() {
|
||||
return Err(SkillParseError::MissingField(field_name));
|
||||
}
|
||||
if value.chars().count() > max_len {
|
||||
return Err(SkillParseError::InvalidField {
|
||||
field: field_name,
|
||||
reason: format!("exceeds maximum length of {max_len} characters"),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolve_str(value: Option<String>, max_len: usize, field: &'static str) -> Option<String> {
|
||||
let value = value?;
|
||||
let value = sanitize_single_line(&value);
|
||||
if value.is_empty() {
|
||||
tracing::warn!("ignoring {field}: value is empty");
|
||||
return None;
|
||||
}
|
||||
if value.chars().count() > max_len {
|
||||
tracing::warn!("ignoring {field}: exceeds maximum length of {max_len} characters");
|
||||
return None;
|
||||
}
|
||||
Some(value)
|
||||
}
|
||||
|
||||
fn resolve_required_str(
|
||||
value: Option<String>,
|
||||
max_len: usize,
|
||||
field: &'static str,
|
||||
) -> Option<String> {
|
||||
let Some(value) = value else {
|
||||
tracing::warn!("ignoring {field}: value is missing");
|
||||
return None;
|
||||
};
|
||||
resolve_str(Some(value), max_len, field)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
mod environment;
|
||||
mod metadata;
|
||||
|
||||
pub(crate) use environment::load_environment_skills_from_discovery;
|
||||
|
||||
pub(super) const MAX_NAME_LEN: usize = 64;
|
||||
pub(super) const MAX_QUALIFIED_NAME_LEN: usize = 128;
|
||||
pub(super) const MAX_DESCRIPTION_LEN: usize = 1024;
|
||||
pub(super) const MAX_DEPENDENCY_TYPE_LEN: usize = MAX_NAME_LEN;
|
||||
pub(super) const MAX_DEPENDENCY_TRANSPORT_LEN: usize = MAX_NAME_LEN;
|
||||
pub(super) const MAX_DEPENDENCY_VALUE_LEN: usize = MAX_DESCRIPTION_LEN;
|
||||
pub(super) const MAX_DEPENDENCY_DESCRIPTION_LEN: usize = MAX_DESCRIPTION_LEN;
|
||||
pub(super) const MAX_DEPENDENCY_COMMAND_LEN: usize = MAX_DESCRIPTION_LEN;
|
||||
pub(super) const MAX_DEPENDENCY_URL_LEN: usize = MAX_DESCRIPTION_LEN;
|
||||
Reference in new issue
Block a user