Fix skill discovery and listing with scoped skill creation
This commit is contained in:
@@ -13,7 +13,9 @@ use ignore::gitignore::Gitignore;
|
||||
use notify_debouncer_full::notify::WatchFilter;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::standing_queries::{StandingQueryDefinitions, StandingQueryResults};
|
||||
use crate::standing_queries::{
|
||||
should_discover_project_context_in_directory, StandingQueryDefinitions, StandingQueryResults,
|
||||
};
|
||||
|
||||
/// Maximum file size allowed for treesitter parsing (3MB).
|
||||
const MAX_FILE_SIZE: usize = 3 * 1000 * 1000;
|
||||
@@ -293,6 +295,10 @@ impl Entry {
|
||||
ignored,
|
||||
is_root: true,
|
||||
});
|
||||
} else if let Some(state) = standing_queries.as_deref_mut() {
|
||||
state
|
||||
.results
|
||||
.discover_in_unloaded_directory(&root_path, state.definitions);
|
||||
}
|
||||
|
||||
while let Some(job) = queue.pop_front() {
|
||||
@@ -314,6 +320,11 @@ impl Entry {
|
||||
}
|
||||
};
|
||||
if !should_expand {
|
||||
if let Some(state) = standing_queries.as_deref_mut() {
|
||||
state
|
||||
.results
|
||||
.discover_in_unloaded_directory(&job.path, state.definitions);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -411,6 +422,11 @@ impl Entry {
|
||||
ignored,
|
||||
is_root: false,
|
||||
});
|
||||
} else if let Some(state) = standing_queries.as_deref_mut() {
|
||||
state.results.discover_in_unloaded_directory(
|
||||
&child_path,
|
||||
state.definitions,
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
@@ -975,10 +991,9 @@ fn descend_allowlist_matches(suffix: &[Component<'_>]) -> bool {
|
||||
/// Returns whether a repository file watcher should descend into (and register
|
||||
/// a watch on) the directory at `path`.
|
||||
///
|
||||
/// Directories inside `.git/` follow the watcher allowlist, force-included
|
||||
/// paths are always watched even when gitignored, and any other gitignored
|
||||
/// directory is pruned so we don't register watches on `node_modules`, build
|
||||
/// output, vendored deps, etc.
|
||||
/// Directories inside `.git/` follow the watcher allowlist and force-included paths are always
|
||||
/// watched. Registered skill interests additionally search ignored source directories for nested
|
||||
/// providers, while generated dependencies and caches remain pruned.
|
||||
pub fn should_watch_repo_directory(
|
||||
path: &Path,
|
||||
gitignores: &[Gitignore],
|
||||
@@ -992,12 +1007,23 @@ pub fn should_watch_repo_directory(
|
||||
return true;
|
||||
}
|
||||
|
||||
!matches_gitignores(
|
||||
if !matches_gitignores(
|
||||
path,
|
||||
path.is_dir(),
|
||||
gitignores,
|
||||
/* check_ancestors */ true,
|
||||
)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let discovers_skills = force_included_paths
|
||||
.iter()
|
||||
.any(|interest| interest.file_name().is_some_and(|name| name == "skills"));
|
||||
discovers_skills
|
||||
&& path
|
||||
.ancestors()
|
||||
.take_while(|ancestor| gitignores.iter().all(|ignore| *ancestor != ignore.path()))
|
||||
.all(should_discover_project_context_in_directory)
|
||||
}
|
||||
|
||||
/// Returns the [`WatchFilter`] used by repository file watchers.
|
||||
@@ -1010,9 +1036,8 @@ pub fn should_watch_repo_directory(
|
||||
/// existing behavior.
|
||||
///
|
||||
/// Descend predicate: see [`should_watch_repo_directory`]. In addition to the
|
||||
/// `.git/` allowlist, it prunes gitignored directories (honoring registered
|
||||
/// force-included paths) so the recursive walk does not register watches on
|
||||
/// gitignored subtrees.
|
||||
/// `.git/` allowlist, it prunes gitignored directories unless skill-context interests require
|
||||
/// searching their source subtrees. Generated dependency/cache trees stay pruned.
|
||||
///
|
||||
/// `gitignores` should be the repo's root + global gitignores (as produced by
|
||||
/// [`gitignores_for_directory`]), matching `Repository::check_gitignore_status`
|
||||
|
||||
@@ -241,14 +241,57 @@ fn should_watch_descends_to_force_included_under_ignored_ancestor() {
|
||||
&gitignores,
|
||||
&force_included
|
||||
));
|
||||
// A sibling ignored dir that is not force-included is still pruned.
|
||||
assert!(!super::should_watch_repo_directory(
|
||||
// Other source directories can also contain a nested skill provider.
|
||||
assert!(super::should_watch_repo_directory(
|
||||
&root.join(".agents/other"),
|
||||
&gitignores,
|
||||
&force_included
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skill_interests_watch_ignored_source_parents_but_prune_generated_dependencies() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let root = dunce::canonicalize(temp_dir.path()).unwrap();
|
||||
fs::create_dir_all(root.join("app/server/.agents/skills/check")).unwrap();
|
||||
fs::create_dir_all(root.join("node_modules/package")).unwrap();
|
||||
fs::create_dir_all(root.join("target/debug")).unwrap();
|
||||
let gitignores = vec![gitignore_rooted(
|
||||
&root,
|
||||
"app/server/\nnode_modules/\ntarget/\n",
|
||||
)];
|
||||
let interests = vec![std::path::PathBuf::from(".agents/skills")];
|
||||
|
||||
assert!(!super::should_watch_repo_directory(
|
||||
&root.join("app/server"),
|
||||
&gitignores,
|
||||
&[]
|
||||
));
|
||||
for path in [
|
||||
"app/server",
|
||||
"app/server/.agents",
|
||||
"app/server/.agents/skills/check",
|
||||
] {
|
||||
assert!(super::should_watch_repo_directory(
|
||||
&root.join(path),
|
||||
&gitignores,
|
||||
&interests
|
||||
));
|
||||
}
|
||||
for path in [
|
||||
"node_modules",
|
||||
"node_modules/package",
|
||||
"target",
|
||||
"target/debug",
|
||||
] {
|
||||
assert!(!super::should_watch_repo_directory(
|
||||
&root.join(path),
|
||||
&gitignores,
|
||||
&interests
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_watch_handles_nested_ignored_ancestor_with_deeper_force_included() {
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
@@ -414,6 +457,115 @@ fn standing_queries_report_skills_below_an_ignored_directory() {
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_project_context_is_discovered_without_materializing_lazy_source_trees() {
|
||||
virtual_fs::VirtualFS::test("nested_context_in_lazy_source", |dirs, mut vfs| {
|
||||
vfs.mkdir("repo/app/server/.agents/skills/run-channel-verifier")
|
||||
.with_files(vec![
|
||||
virtual_fs::Stub::FileWithContent(
|
||||
"repo/app/server/.agents/skills/run-channel-verifier/SKILL.md",
|
||||
"name: run-channel-verifier",
|
||||
),
|
||||
virtual_fs::Stub::FileWithContent(
|
||||
"repo/app/server/AGENTS.md",
|
||||
"Server instructions",
|
||||
),
|
||||
virtual_fs::Stub::FileWithContent("repo/app/server/main.rs", "source"),
|
||||
]);
|
||||
let repo = dirs.tests().join("repo");
|
||||
let skill_path = repo.join("app/server/.agents/skills/run-channel-verifier/SKILL.md");
|
||||
let rule_path = repo.join("app/server/AGENTS.md");
|
||||
let mut definitions = StandingQueryDefinitions::default();
|
||||
definitions.set_project_skill_provider_paths([std::path::PathBuf::from(".agents/skills")]);
|
||||
|
||||
for (gitignore, max_depth, mut remaining_files) in [
|
||||
("app/server/\n", 200, None),
|
||||
("", 1, None),
|
||||
("", 200, Some(0)),
|
||||
] {
|
||||
fs::write(repo.join(".gitignore"), gitignore).unwrap();
|
||||
let mut files = Vec::new();
|
||||
let mut gitignores = Vec::new();
|
||||
let mut results = StandingQueryResults::default();
|
||||
let tree = Entry::build_tree_with_standing_queries(
|
||||
&repo,
|
||||
&mut files,
|
||||
&mut gitignores,
|
||||
remaining_files.as_mut(),
|
||||
super::BuildTreeOptions {
|
||||
max_depth,
|
||||
current_depth: 0,
|
||||
ignored_path_strategy: &IgnoredPathStrategy::IncludeLazy,
|
||||
force_included_paths: &[std::path::PathBuf::from(".agents/skills")],
|
||||
budget_exceeded_behavior: super::BudgetExceededBehavior::StopAndLazyLoad,
|
||||
},
|
||||
&mut results,
|
||||
&definitions,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(results.project_skills().any(|content| {
|
||||
content.path.to_local_path().as_deref() == Some(skill_path.as_path())
|
||||
&& !content.is_directory
|
||||
}));
|
||||
assert!(results.project_rules().any(|content| {
|
||||
content.path.to_local_path().as_deref() == Some(rule_path.as_path())
|
||||
}));
|
||||
assert!(find_entry(&tree, &skill_path).is_none());
|
||||
assert!(find_entry(&tree, &repo.join("app/server/main.rs")).is_none());
|
||||
assert!(files.iter().all(|file| {
|
||||
file.path.to_local_path().as_deref() != Some(skill_path.as_path())
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn lazy_project_context_preserves_linked_skills_without_following_directory_loops() {
|
||||
virtual_fs::VirtualFS::test("lazy_context_linked_skills", |dirs, mut vfs| {
|
||||
vfs.mkdir("repo/app/server/.agents/skills")
|
||||
.mkdir("repo/node_modules/pkg/.agents/skills/ignored")
|
||||
.mkdir("repo/target/.agents/skills/ignored")
|
||||
.mkdir("linked-skill")
|
||||
.with_files(vec![
|
||||
virtual_fs::Stub::FileWithContent("linked-skill/SKILL.md", "linked"),
|
||||
virtual_fs::Stub::FileWithContent(
|
||||
"repo/node_modules/pkg/.agents/skills/ignored/SKILL.md",
|
||||
"dependency",
|
||||
),
|
||||
virtual_fs::Stub::FileWithContent(
|
||||
"repo/target/.agents/skills/ignored/SKILL.md",
|
||||
"generated",
|
||||
),
|
||||
]);
|
||||
let repo = dirs.tests().join("repo");
|
||||
let linked = repo.join("app/server/.agents/skills/linked");
|
||||
std::os::unix::fs::symlink(dirs.tests().join("linked-skill"), &linked).unwrap();
|
||||
std::os::unix::fs::symlink(&repo, repo.join("app/server/loop")).unwrap();
|
||||
|
||||
let mut definitions = StandingQueryDefinitions::default();
|
||||
definitions.set_project_skill_provider_paths([std::path::PathBuf::from(".agents/skills")]);
|
||||
let mut results = StandingQueryResults::default();
|
||||
results.discover_in_unloaded_directory(&repo, &definitions);
|
||||
|
||||
let discovered = results
|
||||
.project_skills()
|
||||
.filter(|content| !content.is_directory)
|
||||
.map(|content| content.path.clone())
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
discovered,
|
||||
vec![
|
||||
galaxy_util::standardized_path::StandardizedPath::try_from_local(
|
||||
&linked.join("SKILL.md")
|
||||
)
|
||||
.unwrap()
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn standing_queries_report_symlinked_skills_without_materializing_symlinked_directories() {
|
||||
|
||||
@@ -466,8 +466,23 @@ impl LocalRepoMetadataModel {
|
||||
let Some(local_repo_path) = repo_path.to_local_path() else {
|
||||
return;
|
||||
};
|
||||
for interest in &self.force_included_paths {
|
||||
let Ok(entries) = std::fs::read_dir(local_repo_path.join(interest)) else {
|
||||
let mut provider_directories = self
|
||||
.force_included_paths
|
||||
.iter()
|
||||
.map(|interest| local_repo_path.join(interest))
|
||||
.collect::<HashSet<_>>();
|
||||
if let Some(results) = self.standing_results.get(repo_path) {
|
||||
provider_directories.extend(results.project_skills().filter_map(|content| {
|
||||
let path = content.path.to_local_path()?;
|
||||
if content.is_directory {
|
||||
Some(path)
|
||||
} else {
|
||||
path.parent()?.parent().map(Path::to_path_buf)
|
||||
}
|
||||
}));
|
||||
}
|
||||
for provider_directory in provider_directories {
|
||||
let Ok(entries) = std::fs::read_dir(provider_directory) else {
|
||||
continue;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
@@ -1180,6 +1195,8 @@ impl LocalRepoMetadataModel {
|
||||
// is created; insert it as an unloaded placeholder and build
|
||||
// the subtree on demand when the user expands it (see
|
||||
// `load_directory`).
|
||||
standing_results
|
||||
.discover_in_unloaded_directory(path_to_add, standing_query_definitions);
|
||||
mutations.push(FileTreeMutation::AddUnloadedDirectory {
|
||||
path: path_to_add.clone(),
|
||||
is_ignored,
|
||||
|
||||
@@ -1507,6 +1507,56 @@ fn removed_direct_skill_child_refreshes_provider_for_possible_symlink_removal()
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newly_added_nested_skills_refresh_standing_context_for_eager_and_lazy_roots() {
|
||||
VirtualFS::test("new_nested_skill_context", |dirs, mut vfs| {
|
||||
vfs.mkdir("repo/app/server/.agents/skills/run-channel-verifier")
|
||||
.with_files(vec![
|
||||
Stub::FileWithContent(
|
||||
"repo/app/server/.agents/skills/run-channel-verifier/SKILL.md",
|
||||
"name: run-channel-verifier",
|
||||
),
|
||||
Stub::FileWithContent("repo/app/server/AGENTS.md", "Server instructions"),
|
||||
]);
|
||||
let repo = dirs.tests().join("repo");
|
||||
let server = repo.join("app/server");
|
||||
let skill = server.join(".agents/skills/run-channel-verifier/SKILL.md");
|
||||
let rule = server.join("AGENTS.md");
|
||||
let mut definitions = StandingQueryDefinitions::default();
|
||||
definitions.set_project_skill_provider_paths([PathBuf::from(".agents/skills")]);
|
||||
let update = RepoUpdate {
|
||||
added: vec![server],
|
||||
..Default::default()
|
||||
};
|
||||
let (gitignore, _) = Gitignore::new({
|
||||
let gitignore_path = repo.join(".gitignore");
|
||||
std::fs::write(&gitignore_path, "app/server/\n").unwrap();
|
||||
gitignore_path
|
||||
});
|
||||
|
||||
for lazy_load in [false, true] {
|
||||
let (_, discovered, _) = block_on(LocalRepoMetadataModel::compute_file_tree_mutations(
|
||||
&update,
|
||||
std::slice::from_ref(&gitignore),
|
||||
&[PathBuf::from(".agents/skills")],
|
||||
&definitions,
|
||||
lazy_load,
|
||||
));
|
||||
|
||||
assert!(discovered.project_skills().any(|content| {
|
||||
content
|
||||
== &StandingQueryContent::file(
|
||||
StandardizedPath::try_from_local(&skill).unwrap(),
|
||||
)
|
||||
}));
|
||||
assert!(discovered.project_rules().any(|content| {
|
||||
content
|
||||
== &StandingQueryContent::file(StandardizedPath::try_from_local(&rule).unwrap())
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
#[cfg(all(unix, feature = "local_fs"))]
|
||||
#[test]
|
||||
fn added_external_target_skill_symlink_routes_to_lexical_repository() {
|
||||
@@ -1596,18 +1646,18 @@ fn added_external_target_skill_symlink_routes_to_lexical_repository() {
|
||||
}
|
||||
#[cfg(all(unix, feature = "local_fs"))]
|
||||
#[test]
|
||||
fn modified_external_symlink_target_upserts_lexical_project_skill() {
|
||||
fn modified_nested_external_symlink_target_upserts_lexical_project_skill() {
|
||||
VirtualFS::test(
|
||||
"modified_external_symlink_target_upserts_lexical_project_skill",
|
||||
"modified_nested_external_symlink_target_upserts_lexical_project_skill",
|
||||
|dirs, mut vfs| {
|
||||
vfs.mkdir("repo/.agents/skills")
|
||||
vfs.mkdir("repo/app/server/.agents/skills")
|
||||
.mkdir("outside/linked-skill")
|
||||
.with_files(vec![Stub::FileWithContent(
|
||||
"outside/linked-skill/SKILL.md",
|
||||
"linked skill",
|
||||
)]);
|
||||
let repo = dirs.tests().join("repo");
|
||||
let logical_skill_dir = repo.join(".agents/skills/linked-skill");
|
||||
let logical_skill_dir = repo.join("app/server/.agents/skills/linked-skill");
|
||||
let logical_skill_path = logical_skill_dir.join("SKILL.md");
|
||||
let target_skill_path = dirs.tests().join("outside/linked-skill/SKILL.md");
|
||||
std::os::unix::fs::symlink(
|
||||
|
||||
@@ -7,6 +7,28 @@ use std::collections::HashSet;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use galaxy_util::standardized_path::StandardizedPath;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
/// Source directories may be gitignored while still owning project instructions or skills.
|
||||
/// Generated dependency and cache trees are outside that project-context search.
|
||||
pub(crate) fn should_discover_project_context_in_directory(path: &Path) -> bool {
|
||||
!path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.is_some_and(|name| {
|
||||
matches!(
|
||||
name,
|
||||
".git"
|
||||
| "node_modules"
|
||||
| "target"
|
||||
| ".venv"
|
||||
| "venv"
|
||||
| ".next"
|
||||
| ".cache"
|
||||
| "__pycache__"
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Repository-scoped standing query configuration.
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -19,7 +41,12 @@ impl Default for StandingQueryDefinitions {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
project_skill_provider_paths: Vec::new(),
|
||||
project_rule_file_names: vec!["WARP.md".to_string(), "AGENTS.md".to_string()],
|
||||
project_rule_file_names: vec![
|
||||
"GALAXY.md".to_string(),
|
||||
"WARP.md".to_string(),
|
||||
"CLAUDE.md".to_string(),
|
||||
"AGENTS.md".to_string(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,6 +133,51 @@ impl StandingQueryResults {
|
||||
self.project_rules.iter()
|
||||
}
|
||||
|
||||
/// Discovers context below a directory omitted from the materialized tree because it is
|
||||
/// ignored, too deep, or beyond its file budget. Only context paths are retained; ordinary
|
||||
/// files do not consume the tree budget or become visible entries.
|
||||
pub(crate) fn discover_in_unloaded_directory(
|
||||
&mut self,
|
||||
path: &Path,
|
||||
definitions: &StandingQueryDefinitions,
|
||||
) {
|
||||
if path.is_symlink() {
|
||||
self.record_followed_project_skill_directory(path, definitions);
|
||||
return;
|
||||
}
|
||||
let mut entries = WalkDir::new(path).follow_links(false).into_iter();
|
||||
while let Some(entry) = entries.next() {
|
||||
let Ok(entry) = entry else {
|
||||
continue;
|
||||
};
|
||||
let path = entry.path();
|
||||
if !entry.file_type().is_dir() {
|
||||
if entry.file_type().is_symlink() && path.is_dir() {
|
||||
self.record_followed_project_skill_directory(path, definitions);
|
||||
} else {
|
||||
self.record_path(path, false, definitions);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !should_discover_project_context_in_directory(path) {
|
||||
entries.skip_current_dir();
|
||||
continue;
|
||||
}
|
||||
|
||||
self.record_path(path, true, definitions);
|
||||
if definitions.is_project_skill_provider_directory(path) {
|
||||
// Only direct skill entries are discoverable. Do not walk scripts, assets,
|
||||
// references, or arbitrary symlink targets beneath a skill installation.
|
||||
if let Ok(skills) = std::fs::read_dir(path) {
|
||||
for skill in skills.flatten() {
|
||||
self.record_followed_project_skill_directory(&skill.path(), definitions);
|
||||
}
|
||||
}
|
||||
entries.skip_current_dir();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Records a path encountered while traversing the repository.
|
||||
pub(crate) fn record_path(
|
||||
&mut self,
|
||||
|
||||
@@ -46,6 +46,38 @@ fn records_provider_skill_files_and_project_rules() {
|
||||
.any(|content| { content == &StandingQueryContent::file(standardized(&nested_rule)) }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_query_records_supported_project_rule_files() {
|
||||
let definitions = StandingQueryDefinitions::default();
|
||||
let mut results = StandingQueryResults::default();
|
||||
let rule_paths = [
|
||||
repo_path("GALAXY.md"),
|
||||
repo_path("packages/api/WARP.md"),
|
||||
repo_path("app/server/CLAUDE.md"),
|
||||
repo_path("app/client/agents.MD"),
|
||||
];
|
||||
|
||||
for path in &rule_paths {
|
||||
results.record_path(path, false, &definitions);
|
||||
}
|
||||
results.record_path(&repo_path("README.md"), false, &definitions);
|
||||
results.record_path(
|
||||
&repo_path(".agents/skills/check/SKILL.md"),
|
||||
false,
|
||||
&definitions,
|
||||
);
|
||||
results.record_path(&repo_path("generated/AGENTS.md"), true, &definitions);
|
||||
|
||||
let expected = rule_paths
|
||||
.iter()
|
||||
.map(|path| StandingQueryContent::file(standardized(path)))
|
||||
.collect::<HashSet<_>>();
|
||||
assert_eq!(
|
||||
results.project_rules().cloned().collect::<HashSet<_>>(),
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replacing_removed_direct_skill_child_can_reupsert_provider_for_hydration() {
|
||||
let definitions = definitions();
|
||||
|
||||
Reference in New Issue
Block a user