Skip to content

Commit 40300fe

Browse files
committed
Implement symlinks for skills
1 parent 8bec7c1 commit 40300fe

2 files changed

Lines changed: 258 additions & 4 deletions

File tree

‎src/services/skills/SkillsManager.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,29 @@ export class SkillsManager {
4949
}
5050
}
5151

52+
/**
53+
* Maximum depth for recursive scanning of symlinked skill container directories.
54+
* Prevents infinite loops from circular symlinks.
55+
*/
56+
private static readonly MAX_SCAN_DEPTH = 5
57+
5258
/**
5359
* Scan a skills directory for skill subdirectories.
54-
* Handles two symlink cases:
60+
* Handles symlink cases:
5561
* 1. The skills directory itself is a symlink (resolved by directoryExists using realpath)
5662
* 2. Individual skill subdirectories are symlinks
63+
* 3. Symlinked container directories that contain skill subdirectories (e.g., ln -s ../../repo/skills .roo/skills/)
5764
*/
58-
private async scanSkillsDirectory(dirPath: string, source: "global" | "project", mode?: string): Promise<void> {
65+
private async scanSkillsDirectory(
66+
dirPath: string,
67+
source: "global" | "project",
68+
mode?: string,
69+
depth: number = 0,
70+
): Promise<void> {
71+
if (depth > SkillsManager.MAX_SCAN_DEPTH) {
72+
return
73+
}
74+
5975
if (!(await directoryExists(dirPath))) {
6076
return
6177
}
@@ -74,8 +90,16 @@ export class SkillsManager {
7490
const stats = await fs.stat(entryPath).catch(() => null)
7591
if (!stats?.isDirectory()) continue
7692

77-
// Load skill metadata - the skill name comes from the entry name (symlink name if symlinked)
78-
await this.loadSkillMetadata(entryPath, source, mode, entryName)
93+
// Check if this directory contains a SKILL.md (i.e., it's a skill directory)
94+
const skillMdPath = path.join(entryPath, "SKILL.md")
95+
if (await fileExists(skillMdPath)) {
96+
// Load skill metadata - the skill name comes from the entry name (symlink name if symlinked)
97+
await this.loadSkillMetadata(entryPath, source, mode, entryName)
98+
} else {
99+
// No SKILL.md found - this might be a container directory (e.g., a symlinked repo of skills)
100+
// Recursively scan it for skill subdirectories
101+
await this.scanSkillsDirectory(entryPath, source, mode, depth + 1)
102+
}
79103
}
80104
} catch {
81105
// Directory doesn't exist or can't be read - this is fine

‎src/services/skills/__tests__/SkillsManager.spec.ts‎

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,236 @@ Instructions here...`
616616
expect(skills[0].source).toBe("global")
617617
})
618618

619+
it("should discover skills from symlinked container directory with multiple skills", async () => {
620+
// Simulates: ln -s ../../REPO/skills .roo/skills/
621+
// This creates .roo/skills/skills -> ../../REPO/skills
622+
// Inside REPO/skills/ there are skill subdirectories: skill-a/ and skill-b/
623+
const containerDir = p(globalSkillsDir, "skills") // the symlinked container
624+
const repoSkillsDir = p("/repo", "skills") // the actual target
625+
const skillADir = p(repoSkillsDir, "skill-a")
626+
const skillAMd = p(skillADir, "SKILL.md")
627+
const skillBDir = p(repoSkillsDir, "skill-b")
628+
const skillBMd = p(skillBDir, "SKILL.md")
629+
630+
mockDirectoryExists.mockImplementation(async (dir: string) => {
631+
return dir === globalSkillsDir || dir === containerDir
632+
})
633+
634+
mockRealpath.mockImplementation(async (pathArg: string) => {
635+
if (pathArg === globalSkillsDir) return globalSkillsDir
636+
if (pathArg === containerDir) return repoSkillsDir
637+
return pathArg
638+
})
639+
640+
mockReaddir.mockImplementation(async (dir: string) => {
641+
if (dir === globalSkillsDir) return ["skills"] // the symlinked container entry
642+
if (dir === repoSkillsDir) return ["skill-a", "skill-b"]
643+
return []
644+
})
645+
646+
mockStat.mockImplementation(async (pathArg: string) => {
647+
if (pathArg === containerDir) return { isDirectory: () => true }
648+
if (pathArg === skillADir) return { isDirectory: () => true }
649+
if (pathArg === skillBDir) return { isDirectory: () => true }
650+
throw new Error("Not found")
651+
})
652+
653+
mockFileExists.mockImplementation(async (file: string) => {
654+
return file === skillAMd || file === skillBMd
655+
})
656+
657+
mockReadFile.mockImplementation(async (file: string) => {
658+
if (file === skillAMd) {
659+
return `---
660+
name: skill-a
661+
description: First skill from symlinked repo
662+
---
663+
664+
# Skill A`
665+
}
666+
if (file === skillBMd) {
667+
return `---
668+
name: skill-b
669+
description: Second skill from symlinked repo
670+
---
671+
672+
# Skill B`
673+
}
674+
throw new Error("File not found")
675+
})
676+
677+
await skillsManager.discoverSkills()
678+
679+
const skills = skillsManager.getAllSkills()
680+
expect(skills).toHaveLength(2)
681+
const names = skills.map((s) => s.name).sort()
682+
expect(names).toEqual(["skill-a", "skill-b"])
683+
expect(skills.every((s) => s.source === "global")).toBe(true)
684+
})
685+
686+
it("should discover skills from nested container directories", async () => {
687+
// Simulates a deeper nesting: .roo/skills/repo/category/my-skill/SKILL.md
688+
const repoDir = p(globalSkillsDir, "repo")
689+
const categoryDir = p(repoDir, "category")
690+
const skillDir = p(categoryDir, "nested-skill")
691+
const skillMd = p(skillDir, "SKILL.md")
692+
693+
mockDirectoryExists.mockImplementation(async (dir: string) => {
694+
return [globalSkillsDir, repoDir, categoryDir].includes(dir)
695+
})
696+
697+
mockRealpath.mockImplementation(async (pathArg: string) => pathArg)
698+
699+
mockReaddir.mockImplementation(async (dir: string) => {
700+
if (dir === globalSkillsDir) return ["repo"]
701+
if (dir === repoDir) return ["category"]
702+
if (dir === categoryDir) return ["nested-skill"]
703+
return []
704+
})
705+
706+
mockStat.mockImplementation(async (pathArg: string) => {
707+
if ([repoDir, categoryDir, skillDir].includes(pathArg)) {
708+
return { isDirectory: () => true }
709+
}
710+
throw new Error("Not found")
711+
})
712+
713+
mockFileExists.mockImplementation(async (file: string) => {
714+
return file === skillMd
715+
})
716+
717+
mockReadFile.mockImplementation(async (file: string) => {
718+
if (file === skillMd) {
719+
return `---
720+
name: nested-skill
721+
description: A deeply nested skill
722+
---
723+
724+
# Nested Skill`
725+
}
726+
throw new Error("File not found")
727+
})
728+
729+
await skillsManager.discoverSkills()
730+
731+
const skills = skillsManager.getAllSkills()
732+
expect(skills).toHaveLength(1)
733+
expect(skills[0].name).toBe("nested-skill")
734+
})
735+
736+
it("should stop scanning at max depth to prevent circular symlinks", async () => {
737+
// Simulate a directory structure that goes deeper than MAX_SCAN_DEPTH (5)
738+
// Each level is a directory without SKILL.md, forcing recursion
739+
const levels: string[] = [globalSkillsDir]
740+
for (let i = 0; i < 8; i++) {
741+
levels.push(p(levels[levels.length - 1], `level-${i}`))
742+
}
743+
// Put a skill at the deepest level (beyond max depth)
744+
const deepSkillDir = p(levels[levels.length - 1], "deep-skill")
745+
const deepSkillMd = p(deepSkillDir, "SKILL.md")
746+
747+
mockDirectoryExists.mockImplementation(async (dir: string) => {
748+
return levels.includes(dir)
749+
})
750+
751+
mockRealpath.mockImplementation(async (pathArg: string) => pathArg)
752+
753+
mockReaddir.mockImplementation(async (dir: string) => {
754+
const idx = levels.indexOf(dir)
755+
if (idx >= 0 && idx < levels.length - 1) {
756+
// Return the next level directory name
757+
const nextLevel = levels[idx + 1]
758+
return [path.basename(nextLevel)]
759+
}
760+
if (dir === levels[levels.length - 1]) {
761+
return ["deep-skill"]
762+
}
763+
return []
764+
})
765+
766+
mockStat.mockImplementation(async (pathArg: string) => {
767+
if (levels.includes(pathArg) || pathArg === deepSkillDir) {
768+
return { isDirectory: () => true }
769+
}
770+
throw new Error("Not found")
771+
})
772+
773+
mockFileExists.mockImplementation(async (file: string) => {
774+
return file === deepSkillMd
775+
})
776+
777+
mockReadFile.mockImplementation(async (file: string) => {
778+
if (file === deepSkillMd) {
779+
return `---
780+
name: deep-skill
781+
description: A skill too deep to find
782+
---
783+
784+
# Deep Skill`
785+
}
786+
throw new Error("File not found")
787+
})
788+
789+
await skillsManager.discoverSkills()
790+
791+
// The skill should NOT be found because it's beyond max depth
792+
const skills = skillsManager.getAllSkills()
793+
expect(skills).toHaveLength(0)
794+
})
795+
796+
it("should handle broken symlinks in container directories gracefully", async () => {
797+
// Simulate a container directory with a broken symlink entry
798+
const containerDir = p(globalSkillsDir, "repo-skills")
799+
const brokenDir = p(containerDir, "broken-link")
800+
const validSkillDir = p(containerDir, "valid-skill")
801+
const validSkillMd = p(validSkillDir, "SKILL.md")
802+
803+
mockDirectoryExists.mockImplementation(async (dir: string) => {
804+
return dir === globalSkillsDir || dir === containerDir
805+
})
806+
807+
mockRealpath.mockImplementation(async (pathArg: string) => {
808+
if (pathArg === containerDir) return containerDir
809+
return pathArg
810+
})
811+
812+
mockReaddir.mockImplementation(async (dir: string) => {
813+
if (dir === globalSkillsDir) return ["repo-skills"]
814+
if (dir === containerDir) return ["broken-link", "valid-skill"]
815+
return []
816+
})
817+
818+
mockStat.mockImplementation(async (pathArg: string) => {
819+
if (pathArg === p(globalSkillsDir, "repo-skills")) return { isDirectory: () => true }
820+
if (pathArg === brokenDir) throw new Error("ENOENT: no such file or directory")
821+
if (pathArg === validSkillDir) return { isDirectory: () => true }
822+
throw new Error("Not found")
823+
})
824+
825+
mockFileExists.mockImplementation(async (file: string) => {
826+
return file === validSkillMd
827+
})
828+
829+
mockReadFile.mockImplementation(async (file: string) => {
830+
if (file === validSkillMd) {
831+
return `---
832+
name: valid-skill
833+
description: A valid skill next to a broken symlink
834+
---
835+
836+
# Valid Skill`
837+
}
838+
throw new Error("File not found")
839+
})
840+
841+
await skillsManager.discoverSkills()
842+
843+
// Should still find the valid skill despite the broken symlink
844+
const skills = skillsManager.getAllSkills()
845+
expect(skills).toHaveLength(1)
846+
expect(skills[0].name).toBe("valid-skill")
847+
})
848+
619849
it("should discover skills from global .agents directory", async () => {
620850
const agentSkillDir = p(globalAgentsSkillsDir, "agent-skill")
621851
const agentSkillMd = p(agentSkillDir, "SKILL.md")

0 commit comments

Comments
 (0)