-
Notifications
You must be signed in to change notification settings - Fork 14.2k
perf(skills): resolve plugin namespaces per root #31348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anp-oai
wants to merge
2
commits into
codex/skill-namespace-loader-tests
Choose a base branch
from
codex/skill-namespace-root-probes
base: codex/skill-namespace-loader-tests
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−77
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| use codex_exec_server::ExecutorFileSystem; | ||
| use codex_utils_path_uri::PathUri; | ||
| use codex_utils_plugins::plugin_namespace_for_root_uri; | ||
| use codex_utils_plugins::plugin_namespace_for_skill_uri; | ||
| use futures::future::join_all; | ||
| use std::collections::HashSet; | ||
|
|
||
| /// Resolves the namespace prefix applied to skill names during one skills scan. | ||
| /// | ||
| /// A plugin namespace is the plugin name from the nearest valid plugin manifest | ||
| /// above a skill path. For example, a skill named `search` beneath a plugin named | ||
| /// `sample` is exposed as `sample:search`. | ||
| /// | ||
| /// Resolving the namespace separately for every `SKILL.md` repeats the same | ||
| /// ancestor manifest probes for sibling skills. This resolver resolves relevant | ||
| /// roots once per scan, then selects the nearest matching root for each skill. | ||
| /// | ||
| /// Namespace precedence is: | ||
| /// | ||
| /// 1. an explicitly provided plugin namespace; | ||
| /// 2. the deepest matching canonical symlink root or nested plugin root; | ||
| /// 3. the namespace inherited from the scanned skills root. | ||
| pub(crate) struct SkillNamespaceResolver { | ||
| inherited_namespace: ResolvedSkillNamespace, | ||
| nested_namespaces: Vec<(PathUri, ResolvedSkillNamespace)>, | ||
| } | ||
|
|
||
| impl SkillNamespaceResolver { | ||
| /// Builds a resolver whose explicit plugin-owned namespace overrides discovery. | ||
| pub(crate) fn with_provided_namespace(namespace: &str) -> Self { | ||
| Self { | ||
| inherited_namespace: ResolvedSkillNamespace::Plugin(namespace.to_string()), | ||
| nested_namespaces: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) async fn discover( | ||
| fs: &dyn ExecutorFileSystem, | ||
| root: &PathUri, | ||
| skill_paths: &[PathUri], | ||
| plugin_roots: HashSet<PathUri>, | ||
| namespace_roots: HashSet<PathUri>, | ||
| ) -> Self { | ||
| // Only probe plugin roots above loaded skills; unused siblings cannot affect names. | ||
| let mut skill_ancestors = HashSet::new(); | ||
| for skill_path in skill_paths { | ||
| let mut ancestor = skill_path.parent(); | ||
| while let Some(path) = ancestor { | ||
| skill_ancestors.insert(path.clone()); | ||
| ancestor = path.parent(); | ||
| } | ||
| } | ||
| let plugin_roots = plugin_roots | ||
| .into_iter() | ||
| .filter(|plugin_root| skill_ancestors.contains(plugin_root)) | ||
| .collect::<HashSet<_>>(); | ||
|
|
||
| // Ordinary descendants fall back to the nearest valid manifest at or above the scan root. | ||
| let inherited_namespace = plugin_namespace_for_skill_uri(fs, root) | ||
| .await | ||
| .map(ResolvedSkillNamespace::Plugin) | ||
| .unwrap_or(ResolvedSkillNamespace::Plain); | ||
|
anp-oai marked this conversation as resolved.
|
||
| // The scan root is already the fallback above if nothing else matches, exclude from the search. | ||
| let namespace_roots = namespace_roots | ||
| .into_iter() | ||
| .filter(|namespace_root| namespace_root != root) | ||
| .collect::<Vec<_>>(); | ||
| let namespace_root_set = namespace_roots.iter().cloned().collect::<HashSet<_>>(); | ||
| // Keep independent probes concurrent for remote executor latency. | ||
| let namespace_lookups = join_all(namespace_roots.into_iter().map(|namespace_root| async { | ||
| let namespace = plugin_namespace_for_skill_uri(fs, &namespace_root) | ||
| .await | ||
| .map(ResolvedSkillNamespace::Plugin) | ||
| .unwrap_or(ResolvedSkillNamespace::Plain); | ||
| (namespace_root, namespace) | ||
| })); | ||
| // Invalid nested manifests are omitted, so the deepest remaining match wins. | ||
| let plugin_lookups = join_all( | ||
| plugin_roots | ||
| .into_iter() | ||
| .filter(|plugin_root| { | ||
| plugin_root != root && !namespace_root_set.contains(plugin_root) | ||
| }) | ||
| .map(|plugin_root| async move { | ||
| plugin_namespace_for_root_uri(fs, &plugin_root) | ||
| .await | ||
| .map(|namespace| (plugin_root, ResolvedSkillNamespace::Plugin(namespace))) | ||
| }), | ||
| ); | ||
| let (namespace_lookups, plugin_lookups) = tokio::join!(namespace_lookups, plugin_lookups); | ||
| let nested_namespaces = namespace_lookups | ||
| .into_iter() | ||
| .chain(plugin_lookups.into_iter().flatten()) | ||
| .collect(); | ||
|
|
||
| Self { | ||
| inherited_namespace, | ||
| nested_namespaces, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn for_skill(&self, root: &PathUri, path: &PathUri) -> &ResolvedSkillNamespace { | ||
| // Ancestor symlink targets cannot override skills still owned by the scan root. | ||
| let path_is_under_root = path.starts_with(root); | ||
| // The deepest matching path prefix is the nearest applicable namespace. | ||
| self.nested_namespaces | ||
| .iter() | ||
| .filter(|(namespace_root, _)| { | ||
| path.starts_with(namespace_root) | ||
| && (!path_is_under_root || !root.starts_with(namespace_root)) | ||
| }) | ||
| .max_by_key(|(namespace_root, _)| namespace_root.ancestors().count()) | ||
| .map(|(_, namespace)| namespace) | ||
| .unwrap_or(&self.inherited_namespace) | ||
| } | ||
| } | ||
|
|
||
| /// The completed namespace resolution for a skill root. | ||
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub(crate) enum ResolvedSkillNamespace { | ||
| /// No plugin namespace applies to matching skills. | ||
| Plain, | ||
| /// Qualify matching skill names with this plugin namespace. | ||
| Plugin(String), | ||
| } | ||
|
|
||
| impl ResolvedSkillNamespace { | ||
| pub(crate) fn qualify(&self, base_name: &str) -> String { | ||
| match self { | ||
| Self::Plain => base_name.to_string(), | ||
| Self::Plugin(namespace) => format!("{namespace}:{base_name}"), | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.