Repository navigation
feat(skill): supports baseDir being treated as a single skill directory #1824
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
base: main
Are you sure you want to change the base?
Changes from 15 commits
f52607f
409fae0
1ecbe6a
91013e1
6c62b0d
78acb24
9851351
cd0adc8
5d7b2f0
521c512
e537acc
75b43da
bb25982
89b6cd3
d0561e9
62e787e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Root vs container mode is inferred from Two consequences of inferring rather than configuring:
A |
||
| 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)) { | ||
|
|
@@ -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 | ||
|
|
@@ -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)) { | ||
|
|
@@ -213,38 +240,74 @@ public static boolean saveSkills(Path baseDir, List<AgentSkill> skills, boolean | |
| try { | ||
| for (AgentSkill skill : skills) { | ||
| String skillName = skill.getName(); | ||
| Path skillDir = validateAndResolvePath(baseDir, skillName); | ||
|
|
||
| if (Files.exists(skillDir)) { | ||
| if (!force) { | ||
| logger.info( | ||
| "Skill directory already exists and force=false: {}", skillName); | ||
| continue; // Skip to the next skill if force=false | ||
| } else { | ||
| logger.info("Overwriting existing skill directory: {}", skillName); | ||
| deleteDirectory(skillDir); | ||
|
|
||
| Path skillDir; | ||
| Path skillFile; | ||
| // Root-level skill: baseDir itself contains SKILL.md | ||
| if (hasSkillFile(baseDir)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mode is re-decided inside the Path root = ...; // root/SKILL.md exists (name = "A")
saveSkills(root, List.of(skillA, skillB), true);
// iter A: hasSkillFile(root) -> skillFile exists -> walk root, delete everything -> write A
// iter B: hasSkillFile(root) -> skillFile exists -> walk root, delete everything (incl. A)
// -> write B
// returns allSaved == true; A's SKILL.md and resources are gone silentlyWith Since a root-level repository can hold exactly one skill by definition, the guard belongs above the loop: if |
||
| skillDir = baseDir.toAbsolutePath().normalize(); | ||
| skillFile = skillDir.resolve(SKILL_FILE_NAME); | ||
| if (Files.exists(skillFile)) { | ||
| if (!force) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The root branch never validates or even uses
Comparing |
||
| logger.info("Root skill already exists and force=false: {}", skillName); | ||
| continue; | ||
| } else { | ||
| logger.info("Overwriting existing root skill: {}", skillName); | ||
| // Delete all contents except baseDir itself | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
try (Stream<Path> paths = Files.walk(skillDir)) { // skillDir == baseDir here
paths.sorted(Comparator.reverseOrder())
.filter(path -> !path.equals(skillDir))
.forEach(path -> Files.delete(path));
}
Could the cleanup be limited to what the previous root skill actually owned — its |
||
| try (Stream<Path> paths = Files.walk(skillDir)) { | ||
| paths.sorted(Comparator.reverseOrder()) | ||
| .filter(path -> !path.equals(skillDir)) | ||
| .forEach( | ||
| path -> { | ||
| try { | ||
| Files.delete(path); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException( | ||
| "Failed to delete: " + path, e); | ||
| } | ||
| }); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException( | ||
| "Failed to clean root skill directory", e); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| // Subdirectory skill: create a new subdirectory | ||
| skillDir = validateAndResolvePath(baseDir, skillName); | ||
| if (Files.exists(skillDir)) { | ||
| if (!force) { | ||
| logger.info( | ||
| "Skill directory already exists and force=false: {}", | ||
| skillName); | ||
| continue; // Skip to the next skill if force=false | ||
| } else { | ||
| logger.info("Overwriting existing skill directory: {}", skillName); | ||
| deleteDirectory(skillDir); | ||
| } | ||
| } | ||
| Files.createDirectories(skillDir); | ||
| skillFile = skillDir.resolve(SKILL_FILE_NAME); | ||
| } | ||
|
|
||
| Files.createDirectories(skillDir); | ||
|
|
||
| String skillMdContent = | ||
| MarkdownSkillParser.generate(skill.getMetadata(), skill.getSkillContent()); | ||
|
|
||
| Path skillFile = skillDir.resolve(SKILL_FILE_NAME); | ||
| Files.writeString(skillFile, skillMdContent, StandardCharsets.UTF_8); | ||
|
|
||
| Map<String, String> resources = skill.getResources(); | ||
| for (Map.Entry<String, String> entry : resources.entrySet()) { | ||
| Path resourceFile = skillDir.resolve(entry.getKey()); | ||
| Files.createDirectories(resourceFile.getParent()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resource keys are still resolved without containment checks — Path abs = skillDir.toAbsolutePath().normalize();
Path target = abs.resolve(entry.getKey()).normalize();
if (!target.startsWith(abs)) {
throw new IllegalArgumentException("resource path escapes skill directory: " + entry.getKey());
}This pre-dates the PR, but root mode changes the blast radius: Small related nit on the same hunk: |
||
| String content = entry.getValue(); | ||
| if (content != null && content.startsWith("base64:")) { | ||
| String base64 = content.substring("base64:".length()); | ||
| byte[] bytes = Base64.getDecoder().decode(base64); | ||
| Files.write(resourceFile, bytes); | ||
| } else { | ||
| Files.writeString(resourceFile, content, StandardCharsets.UTF_8); | ||
| if (content != null) { | ||
| if (content.startsWith("base64:")) { | ||
| String base64 = content.substring("base64:".length()); | ||
| byte[] bytes = Base64.getDecoder().decode(base64); | ||
| Files.write(resourceFile, bytes); | ||
| } else { | ||
| Files.writeString(resourceFile, content, StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -277,8 +340,16 @@ public static boolean deleteSkill(Path baseDir, String skillName) { | |
| return false; | ||
| } | ||
|
|
||
| Path dirToDelete = skillDir.get().toAbsolutePath().normalize(); | ||
| Path absoluteBaseDir = baseDir.toAbsolutePath().normalize(); | ||
|
|
||
| try { | ||
| deleteDirectory(skillDir.get()); | ||
| if (dirToDelete.equals(absoluteBaseDir)) { | ||
| // Root-level skill: only remove SKILL.md, keep other files intact | ||
| Files.deleteIfExists(dirToDelete.resolve(SKILL_FILE_NAME)); | ||
| } else { | ||
| deleteDirectory(dirToDelete); | ||
| } | ||
| logger.info("Successfully deleted skill: {}", skillName); | ||
| return true; | ||
| } catch (IOException e) { | ||
|
|
@@ -463,6 +534,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) | ||
|
|
||
There was a problem hiding this comment.
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 beforeskillCache.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 oncebaseDirgains a rootSKILL.mdthose 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 malformedSKILL.mdblanks 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.