Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,41 @@ public List<String> getAllSkillNames() {

@Override
public List<AgentSkill> getAllSkills() {
// If baseDir itself is a skill directory, return only that skill
Path baseDirKey = baseDir.toAbsolutePath().normalize();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root branch is ~35 lines copied from the container loop below (stat → mtime+size compare → loadSkillFromDirectory(..., !lazy) → skillCache.put), and it returns before skillCache.keySet().retainAll(seenDirs) at line 253, so root and container mode handle cache eviction differently. Concretely: a repository that was in container mode accumulates per-skill snapshots, and once baseDir gains a root SKILL.md those entries are never pruned again, because every subsequent call takes this early return.

Worth also deciding what this branch is meant to return when the root file fails to parse: the container branch skips one bad skill and keeps the others, while this branch yields List.of() for the whole repository — a defensible choice, but it is the only place a single malformed SKILL.md blanks the repository, so it should be stated in the javadoc.

Extracting the shared body into private Optional<AgentSkill> loadWithCache(Path dir) used by both branches would remove the copy-paste and the eviction asymmetry in one go.

Path baseSkillFile = baseDirKey.resolve(SKILL_FILE_NAME);
if (Files.exists(baseSkillFile)) {
try {
BasicFileAttributes attrs =
Files.readAttributes(baseSkillFile, BasicFileAttributes.class);
long mtime = attrs.lastModifiedTime().toMillis();
long size = attrs.size();
Snapshot cached = skillCache.get(baseDirKey);
if (cached != null && cached.mtime == mtime && cached.size == size) {
return List.of(cached.skill);
}
AgentSkill skill =
SkillFileSystemHelper.loadSkillFromDirectory(
baseDirKey, getSource(), !lazy);
skillCache.put(baseDirKey, new Snapshot(mtime, size, skill));
return List.of(skill);
} catch (IOException e) {
logger.warn(
"Failed to stat SKILL.md for baseDir '{}': {}",
baseDirKey,
e.getMessage(),
e);
return List.of();
} catch (Exception e) {
logger.warn(
"Failed to load skill from baseDir '{}': {}",
baseDirKey,
e.getMessage(),
e);
return List.of();
}
}

List<AgentSkill> out = new ArrayList<>();
Set<Path> seenDirs = new HashSet<>();
try (Stream<Path> subdirs = Files.list(baseDir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,22 @@ public static AgentSkill loadSkillFromDirectory(
/**
* Retrieves all skill names by parsing SKILL.md metadata in each skill folder.
*
* <p>If {@code baseDir} itself contains a {@code SKILL.md} file, it is treated as a single
* skill directory and only that skill's name is returned. Otherwise, subdirectories are
* scanned for skills.
*
* @param baseDir The base directory containing skill folders
* @return A list of skill names sorted alphabetically
*/
public static List<String> getAllSkillNames(Path baseDir) {
// If baseDir itself is a skill directory, return only that skill
if (hasSkillFile(baseDir)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Root vs container mode is inferred from hasSkillFile(baseDir) on every call, and hasSkillFile (line 407) is Files.walk(dir, 1).anyMatch(name equals "SKILL.md") — it also returns true for a directory named SKILL.md, and it costs a directory listing instead of one Files.isRegularFile(baseDir.resolve("SKILL.md")) stat. FileSystemSkillRepository.getAllSkills() uses Files.exists(baseDir/SKILL.md) for the same decision, so the two implementations infer the mode slightly differently.

Two consequences of inferring rather than configuring:

  1. An existing container-mode repository that gains a SKILL.md at its root (easy to do by accident — a saved skill, a doc file) instantly hides all of its sub-skills, with no warning.
  2. After deleteSkill removes the root SKILL.md, the mode flips back to container, and any leftover resource subdirectory that happens to contain a SKILL.md now surfaces as a skill — i.e. deleted skill data can come back into getAllSkills().

A singleSkillDirectory flag (explicit opt-in on the repository/helper entry points) plus one shared isRootSkillDir(Path) predicate would make the mode deterministic, cheaper, and consistent with FileSystemSkillRepository. The call count also matters in the save path: hasSkillFile(baseDir) runs once per skill in the list.

List<String> names = new ArrayList<>();
readSkillName(baseDir).ifPresent(names::add);
names.sort(String::compareTo);
return names;
}

List<String> skillNames = new ArrayList<>();

try (Stream<Path> subdirs = Files.list(baseDir)) {
Expand Down Expand Up @@ -160,6 +172,10 @@ public static List<AgentSkill> getAllSkills(Path baseDir, String source) {
/**
* Retrieves all skills from the base directory.
*
* <p>If {@code baseDir} itself contains a {@code SKILL.md} file, it is treated as a single
* skill directory and only that skill is returned. Otherwise, subdirectories are scanned for
* skills.
*
* @param baseDir The base directory containing skill folders
* @param source The source identifier for created skills
* @param includeResources when {@code false}, each skill's resource map is left empty and
Expand All @@ -168,6 +184,17 @@ public static List<AgentSkill> getAllSkills(Path baseDir, String source) {
*/
public static List<AgentSkill> getAllSkills(
Path baseDir, String source, boolean includeResources) {
// If baseDir itself is a skill directory, return only that skill
if (hasSkillFile(baseDir)) {
List<AgentSkill> skills = new ArrayList<>();
try {
skills.add(loadSkillFromDirectory(baseDir, source, includeResources));
} catch (Exception e) {
logger.warn("Failed to load skill from '{}': {}", baseDir, e.getMessage(), e);
}
return skills;
}

List<AgentSkill> skills = new ArrayList<>();

try (Stream<Path> subdirs = Files.list(baseDir)) {
Expand Down Expand Up @@ -463,6 +490,16 @@ private static Optional<Path> findSkillDirectoryByName(Path baseDir, String skil
return Optional.empty();
}

// Check if baseDir itself is the skill
if (hasSkillFile(baseDir)) {
String rootName = readSkillName(baseDir).orElse(null);
if (skillName.equals(rootName)) {
return Optional.of(baseDir);
}
// baseDir is a single skill directory; no subdirectory skills to scan
return Optional.empty();
}

try (Stream<Path> subdirs = Files.list(baseDir)) {
return subdirs.filter(Files::isDirectory)
.filter(SkillFileSystemHelper::hasSkillFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,61 @@ void testSave_SourceHasNoColon() {
assertTrue(source.startsWith("filesystem-"));
}

// ==================== Root-Level Skill Tests ====================

@Test
@DisplayName("Should return root skill when baseDir itself has SKILL.md")
void testGetAllSkills_RootLevelSkill() throws IOException {
Path rootDir = tempDir.resolve("root-repo");
Files.createDirectories(rootDir);
Files.writeString(
rootDir.resolve("SKILL.md"),
"---\nname: my-root-skill\ndescription: Root Skill\n---\nContent",
StandardCharsets.UTF_8);

FileSystemSkillRepository repo = new FileSystemSkillRepository(rootDir);
List<AgentSkill> skills = repo.getAllSkills();

assertEquals(1, skills.size());
assertEquals("my-root-skill", skills.get(0).getName());
}

@Test
@DisplayName("Should load root skill by name via getSkill")
void testGetSkill_RootLevelSkill() throws IOException {
Path rootDir = tempDir.resolve("root-repo2");
Files.createDirectories(rootDir);
Files.writeString(
rootDir.resolve("SKILL.md"),
"---\nname: my-root-skill\ndescription: Root Skill\n---\nContent",
StandardCharsets.UTF_8);

FileSystemSkillRepository repo = new FileSystemSkillRepository(rootDir);
AgentSkill skill = repo.getSkill("my-root-skill");

assertNotNull(skill);
assertEquals("my-root-skill", skill.getName());
}

@Test
@DisplayName("Should find root skill via skillExists and getAllSkillNames")
void testSkillExistsAndGetNames_RootLevelSkill() throws IOException {
Path rootDir = tempDir.resolve("root-repo3");
Files.createDirectories(rootDir);
Files.writeString(
rootDir.resolve("SKILL.md"),
"---\nname: my-root-skill\ndescription: Root Skill\n---\nContent",
StandardCharsets.UTF_8);

FileSystemSkillRepository repo = new FileSystemSkillRepository(rootDir);

assertTrue(repo.skillExists("my-root-skill"));
assertFalse(repo.skillExists("nonexistent"));
assertEquals(List.of("my-root-skill"), repo.getAllSkillNames());
}

// ==================== Helper Methods ====================

private void createSampleSkill(String name, String description, String content)
throws IOException {
Path skillDir = skillsBaseDir.resolve(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,88 @@ void shouldFilterOsHiddenFilesOnWindows() throws IOException {
"OS hidden file should be filtered out on Windows");
}

@Test
@DisplayName("Should return root skill name when baseDir itself has SKILL.md")
void testGetAllSkillNames_RootLevelSkill() throws IOException {
Path rootSkillDir = tempDir.resolve("root-skill-dir");
Files.createDirectories(rootSkillDir);
Files.writeString(
rootSkillDir.resolve("SKILL.md"),
"---\nname: root-skill\ndescription: Root Skill\n---\nRoot content",
StandardCharsets.UTF_8);
// Add a subdirectory (e.g. references/) that should be ignored
Files.createDirectories(rootSkillDir.resolve("references"));
Files.writeString(rootSkillDir.resolve("references/doc.md"), "doc");

List<String> names = SkillFileSystemHelper.getAllSkillNames(rootSkillDir);
assertEquals(1, names.size());
assertEquals("root-skill", names.get(0));
}

@Test
@DisplayName("Should return root skill when baseDir itself has SKILL.md")
void testGetAllSkills_RootLevelSkill() throws IOException {
Path rootSkillDir = tempDir.resolve("root-skill-dir2");
Files.createDirectories(rootSkillDir);
Files.writeString(
rootSkillDir.resolve("SKILL.md"),
"---\nname: root-skill2\ndescription: Root Skill 2\n---\nRoot content 2",
StandardCharsets.UTF_8);
Files.writeString(rootSkillDir.resolve("extra.txt"), "extra resource");

List<AgentSkill> skills = SkillFileSystemHelper.getAllSkills(rootSkillDir, "git-source");
assertEquals(1, skills.size());
assertEquals("root-skill2", skills.get(0).getName());
assertEquals("git-source", skills.get(0).getSource());
assertTrue(skills.get(0).getResources().containsKey("extra.txt"));
}

@Test
@DisplayName("Should load root skill by name when baseDir itself has SKILL.md")
void testLoadSkill_RootLevelSkill() throws IOException {
Path rootSkillDir = tempDir.resolve("root-skill-dir3");
Files.createDirectories(rootSkillDir);
Files.writeString(
rootSkillDir.resolve("SKILL.md"),
"---\nname: root-skill3\ndescription: Root Skill 3\n---\nRoot content 3",
StandardCharsets.UTF_8);

AgentSkill skill =
SkillFileSystemHelper.loadSkill(rootSkillDir, "root-skill3", "git-source");
assertNotNull(skill);
assertEquals("root-skill3", skill.getName());
assertEquals("Root Skill 3", skill.getDescription());
}

@Test
@DisplayName("Should find root skill via skillExists when baseDir itself has SKILL.md")
void testSkillExists_RootLevelSkill() throws IOException {
Path rootSkillDir = tempDir.resolve("root-skill-dir4");
Files.createDirectories(rootSkillDir);
Files.writeString(
rootSkillDir.resolve("SKILL.md"),
"---\nname: root-skill4\ndescription: Root Skill 4\n---\nContent",
StandardCharsets.UTF_8);

assertTrue(SkillFileSystemHelper.skillExists(rootSkillDir, "root-skill4"));
assertFalse(SkillFileSystemHelper.skillExists(rootSkillDir, "nonexistent"));
}

@Test
@DisplayName("Should throw when root skill name does not match requested name")
void testLoadSkill_RootLevelSkillNameMismatch() throws IOException {
Path rootSkillDir = tempDir.resolve("root-skill-dir5");
Files.createDirectories(rootSkillDir);
Files.writeString(
rootSkillDir.resolve("SKILL.md"),
"---\nname: actual-name\ndescription: Actual\n---\nContent",
StandardCharsets.UTF_8);

assertThrows(
IllegalArgumentException.class,
() -> SkillFileSystemHelper.loadSkill(rootSkillDir, "wrong-name", "source"));
}

private void createSampleSkill(String name, String description, String content)
throws IOException {
Path skillDir = skillsBaseDir.resolve(name);
Expand Down
Loading