@@ -45,17 +45,37 @@ export class SkillsManager {
4545 const skillsDirs = await this . getSkillsDirectories ( )
4646
4747 for ( const { dir, source, mode } of skillsDirs ) {
48- await this . scanSkillsDirectory ( dir , source , mode )
48+ // Use a fresh visited set for each top-level scan so traversal is
49+ // deduplicated within a single root without sharing state across
50+ // different source or mode roots.
51+ await this . scanSkillsDirectory ( dir , source , mode , 0 , new Set < string > ( ) )
4952 }
5053 }
5154
55+ /**
56+ * Maximum depth for recursive scanning of symlinked skill container directories.
57+ * Prevents infinite loops from circular symlinks.
58+ */
59+ private static readonly MAX_SCAN_DEPTH = 5
60+
5261 /**
5362 * Scan a skills directory for skill subdirectories.
54- * Handles two symlink cases:
63+ * Handles symlink cases:
5564 * 1. The skills directory itself is a symlink (resolved by directoryExists using realpath)
5665 * 2. Individual skill subdirectories are symlinks
66+ * 3. Symlinked container directories that contain skill subdirectories (e.g., ln -s ../../repo/skills .roo/skills/)
5767 */
58- private async scanSkillsDirectory ( dirPath : string , source : "global" | "project" , mode ?: string ) : Promise < void > {
68+ private async scanSkillsDirectory (
69+ dirPath : string ,
70+ source : "global" | "project" ,
71+ mode ?: string ,
72+ depth : number = 0 ,
73+ visited : Set < string > = new Set < string > ( ) ,
74+ ) : Promise < void > {
75+ if ( depth > SkillsManager . MAX_SCAN_DEPTH ) {
76+ return
77+ }
78+
5979 if ( ! ( await directoryExists ( dirPath ) ) ) {
6080 return
6181 }
@@ -64,6 +84,13 @@ export class SkillsManager {
6484 // Get the real path (resolves if dirPath is a symlink)
6585 const realDirPath = await fs . realpath ( dirPath )
6686
87+ // Skip directories we've already visited (by real path) to avoid
88+ // redundant scanning and infinite loops from circular symlinks.
89+ if ( visited . has ( realDirPath ) ) {
90+ return
91+ }
92+ visited . add ( realDirPath )
93+
6794 // Read directory entries
6895 const entries = await fs . readdir ( realDirPath )
6996
@@ -74,8 +101,17 @@ export class SkillsManager {
74101 const stats = await fs . stat ( entryPath ) . catch ( ( ) => null )
75102 if ( ! stats ?. isDirectory ( ) ) continue
76103
77- // Load skill metadata - the skill name comes from the entry name (symlink name if symlinked)
78- await this . loadSkillMetadata ( entryPath , source , mode , entryName )
104+ // Check if this directory contains a SKILL.md (i.e., it's a skill directory)
105+ const skillMdPath = path . join ( entryPath , "SKILL.md" )
106+ if ( await fileExists ( skillMdPath ) ) {
107+ // Load skill metadata - the skill name comes from the entry name (symlink name if symlinked)
108+ await this . loadSkillMetadata ( entryPath , source , mode , entryName )
109+ } else {
110+ // No SKILL.md found - this might be a container directory (e.g., a symlinked repo of skills)
111+ // Recursively scan it for skill subdirectories, sharing the same
112+ // visited set so traversal stays deduplicated within this scan.
113+ await this . scanSkillsDirectory ( entryPath , source , mode , depth + 1 , visited )
114+ }
79115 }
80116 } catch {
81117 // Directory doesn't exist or can't be read - this is fine
0 commit comments