@@ -109,61 +109,51 @@ function localSearchRoots(cwd: string): string[] {
109109 return roots;
110110 }
111111
112+const MAX_SKILL_SCAN_DEPTH = 5;
113+const MAX_SKILL_DIRS_SCANNED = 2000;
114+
112115 function listSkillCandidates(
113116 skillsDir: string,
114117 source: "global" | "local",
115118 rootPath: string,
116119 ): SkillCandidate[] {
117120 if (!existsSync(skillsDir)) return [];
118- let entries: string[];
119- try {
120- entries = readdirSync(skillsDir).sort((a, b) => a.localeCompare(b));
121- } catch {
122- return [];
123- }
124121 const candidates: SkillCandidate[] = [];
125- for (const entry of entries) {
126- const skillDir = join(skillsDir, entry);
127- try {
128- if (!statSync(skillDir).isDirectory()) continue;
129- } catch {
130- continue;
131- }
132- const filePath = join(skillDir, "SKILL.md");
133- if (existsSync(filePath)) {
134- candidates.push({
135- folderName: entry,
136- filePath,
137- rootPath,
138- source,
139- });
140- continue;
141- }
142- // Namespace directory: scan one level deeper for skill subdirectories
143- // (e.g., superpowers/brainstorming/SKILL.md)
144- let subEntries: string[];
122+ let dirsScanned = 0;
123+
124+ function walk(dir: string, depth: number): void {
125+ if (depth > MAX_SKILL_SCAN_DEPTH || dirsScanned >= MAX_SKILL_DIRS_SCANNED)
126+ return;
127+ let entries: string[];
145128 try {
146- subEntries = readdirSync(skillDir).sort((a, b) => a.localeCompare(b));
129+ entries = readdirSync(dir).sort((a, b) => a.localeCompare(b));
147130 } catch {
148- continue;
131+ return;
149132 }
150- for (const subEntry of subEntries) {
151- const subSkillDir = join(skillDir, subEntry);
133+ for (const entry of entries) {
134+ if (dirsScanned >= MAX_SKILL_DIRS_SCANNED) return;
135+ const entryPath = join(dir, entry);
152136 try {
153- if (!statSync(subSkillDir).isDirectory()) continue;
137+ if (!statSync(entryPath).isDirectory()) continue;
154138 } catch {
155139 continue;
156140 }
157- const subFilePath = join(subSkillDir, "SKILL.md");
158- if (!existsSync(subFilePath)) continue;
159- candidates.push({
160- folderName: subEntry,
161- filePath: subFilePath,
162- rootPath,
163- source,
164- });
141+ dirsScanned++;
142+ const filePath = join(entryPath, "SKILL.md");
143+ if (existsSync(filePath)) {
144+ candidates.push({
145+ folderName: entry,
146+ filePath,
147+ rootPath,
148+ source,
149+ });
150+ } else {
151+ walk(entryPath, depth + 1);
152+ }
165153 }
166154 }
155+
156+ walk(skillsDir, 1);
167157 return candidates;
168158 }