claudepluginhub 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -3
- package/dist/doctor/args.d.ts +12 -0
- package/dist/doctor/args.js +111 -0
- package/dist/doctor/claude-source.d.ts +27 -0
- package/dist/doctor/claude-source.js +115 -0
- package/dist/doctor/color.d.ts +11 -0
- package/dist/doctor/color.js +23 -0
- package/dist/doctor/equivalence.d.ts +9 -0
- package/dist/doctor/equivalence.js +263 -0
- package/dist/doctor/estimate.d.ts +30 -0
- package/dist/doctor/estimate.js +47 -0
- package/dist/doctor/html-report.d.ts +9 -0
- package/dist/doctor/html-report.js +385 -0
- package/dist/doctor/inventory.d.ts +69 -0
- package/dist/doctor/inventory.js +1175 -0
- package/dist/doctor/open-browser.d.ts +11 -0
- package/dist/doctor/open-browser.js +38 -0
- package/dist/doctor/overlap.d.ts +22 -0
- package/dist/doctor/overlap.js +23 -0
- package/dist/doctor/paths.d.ts +9 -0
- package/dist/doctor/paths.js +23 -0
- package/dist/doctor/render.d.ts +3 -0
- package/dist/doctor/render.js +353 -0
- package/dist/doctor/report.d.ts +347 -0
- package/dist/doctor/report.js +70 -0
- package/dist/doctor/rules.d.ts +167 -0
- package/dist/doctor/rules.js +342 -0
- package/dist/doctor/run.d.ts +24 -0
- package/dist/doctor/run.js +315 -0
- package/dist/doctor/sanitize.d.ts +28 -0
- package/dist/doctor/sanitize.js +150 -0
- package/dist/doctor/select.d.ts +53 -0
- package/dist/doctor/select.js +100 -0
- package/dist/doctor/wrap.d.ts +2 -0
- package/dist/doctor/wrap.js +84 -0
- package/dist/index.js +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1175 @@
|
|
|
1
|
+
import { accessSync, constants, existsSync, lstatSync, readdirSync, readFileSync, realpathSync, statSync, } from 'node:fs';
|
|
2
|
+
import { basename, delimiter, join, resolve } from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { isWithin, canonicalize } from './paths.js';
|
|
5
|
+
import { boundName, sanitizeCommand, sanitizeUrlOrigin } from './sanitize.js';
|
|
6
|
+
import { VALID_NAME_RE } from '../validation.js';
|
|
7
|
+
export const DEFAULT_LIMITS = {
|
|
8
|
+
maxPlugins: 200,
|
|
9
|
+
maxFilesPerPlugin: 2000,
|
|
10
|
+
maxFileBytes: 512 * 1024,
|
|
11
|
+
maxPluginBytes: 32 * 1024 * 1024,
|
|
12
|
+
maxTotalBytes: 128 * 1024 * 1024,
|
|
13
|
+
maxDepth: 12,
|
|
14
|
+
};
|
|
15
|
+
/** Documented tool-matched hook events (hooks reference). */
|
|
16
|
+
export const TOOL_MATCHED_EVENTS = new Set([
|
|
17
|
+
'PreToolUse',
|
|
18
|
+
'PostToolUse',
|
|
19
|
+
'PostToolUseFailure',
|
|
20
|
+
'PermissionRequest',
|
|
21
|
+
'PermissionDenied',
|
|
22
|
+
]);
|
|
23
|
+
const FRONTMATTER_MAX_LINES = 100;
|
|
24
|
+
const FRONTMATTER_MAX_BYTES = 16 * 1024;
|
|
25
|
+
const MAX_KEYS = 50;
|
|
26
|
+
/**
|
|
27
|
+
* Aggregate cap on hook/MCP/LSP/monitor facts per plugin. Parse helpers stop
|
|
28
|
+
* producing at MAX_FACTS_PER_TYPE + 1 so the scanner can DETECT the overflow
|
|
29
|
+
* and flag it ('max-facts' → scan_incomplete) instead of capping silently.
|
|
30
|
+
*/
|
|
31
|
+
export const MAX_FACTS_PER_TYPE = 100;
|
|
32
|
+
export function createScanner(overrides) {
|
|
33
|
+
const limits = { ...DEFAULT_LIMITS, ...overrides };
|
|
34
|
+
const memo = new Map();
|
|
35
|
+
let pluginsScanned = 0;
|
|
36
|
+
let totalBytesRead = 0;
|
|
37
|
+
let truncated = false;
|
|
38
|
+
function emptyManifest() {
|
|
39
|
+
return {
|
|
40
|
+
present: false,
|
|
41
|
+
inspected: false,
|
|
42
|
+
parseError: false,
|
|
43
|
+
name: null,
|
|
44
|
+
nameInvalid: false,
|
|
45
|
+
invalidFields: [],
|
|
46
|
+
declaredMissing: [],
|
|
47
|
+
dependencies: [],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function emptyResult() {
|
|
51
|
+
return {
|
|
52
|
+
pathIssue: null,
|
|
53
|
+
manifest: emptyManifest(),
|
|
54
|
+
components: [],
|
|
55
|
+
themeFiles: [],
|
|
56
|
+
hooks: [],
|
|
57
|
+
mcp: [],
|
|
58
|
+
lsp: [],
|
|
59
|
+
monitors: [],
|
|
60
|
+
bin: [],
|
|
61
|
+
parseErrors: [],
|
|
62
|
+
symlinksSkipped: [],
|
|
63
|
+
filesScanned: 0,
|
|
64
|
+
bytesRead: 0,
|
|
65
|
+
limitsHit: [],
|
|
66
|
+
incomplete: false,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function readBounded(absPath, size, state) {
|
|
70
|
+
if (size > limits.maxFileBytes) {
|
|
71
|
+
state.limitsHit.add('max-file-bytes');
|
|
72
|
+
state.incomplete = true;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (state.bytesRead + size > limits.maxPluginBytes) {
|
|
76
|
+
state.limitsHit.add('max-plugin-bytes');
|
|
77
|
+
state.incomplete = true;
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (totalBytesRead + size > limits.maxTotalBytes) {
|
|
81
|
+
state.limitsHit.add('max-total-bytes');
|
|
82
|
+
state.incomplete = true;
|
|
83
|
+
truncated = true;
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
let content;
|
|
87
|
+
try {
|
|
88
|
+
content = readFileSync(absPath, 'utf-8');
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
state.bytesRead += size;
|
|
94
|
+
totalBytesRead += size;
|
|
95
|
+
return content;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Deterministic bounded walk. Calls visit(relPath, absPath, size) for every
|
|
99
|
+
* regular file reached. Returns false when a budget aborted the walk.
|
|
100
|
+
*/
|
|
101
|
+
function walk(rootCanonical, startRel, state, visit) {
|
|
102
|
+
const visitedDirs = new Set();
|
|
103
|
+
function recurse(dirAbs, dirRel, depth) {
|
|
104
|
+
let entries;
|
|
105
|
+
try {
|
|
106
|
+
entries = readdirSync(dirAbs).sort();
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
for (const entry of entries) {
|
|
112
|
+
const abs = join(dirAbs, entry);
|
|
113
|
+
const rel = dirRel ? `${dirRel}/${entry}` : entry;
|
|
114
|
+
if (state.filesScanned >= limits.maxFilesPerPlugin) {
|
|
115
|
+
state.limitsHit.add('max-files');
|
|
116
|
+
state.incomplete = true;
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
let info;
|
|
120
|
+
try {
|
|
121
|
+
info = lstatSync(abs);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
let targetAbs = abs;
|
|
127
|
+
let isDir = info.isDirectory();
|
|
128
|
+
let isFile = info.isFile();
|
|
129
|
+
let size = info.size;
|
|
130
|
+
if (info.isSymbolicLink()) {
|
|
131
|
+
let real;
|
|
132
|
+
try {
|
|
133
|
+
real = realpathSync(abs);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
state.symlinksSkipped.push(rel);
|
|
137
|
+
state.incomplete = true;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (!isWithin(rootCanonical, real)) {
|
|
141
|
+
state.symlinksSkipped.push(rel);
|
|
142
|
+
state.incomplete = true;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
let realInfo;
|
|
146
|
+
try {
|
|
147
|
+
realInfo = statSync(real);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
state.symlinksSkipped.push(rel);
|
|
151
|
+
state.incomplete = true;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
targetAbs = real;
|
|
155
|
+
isDir = realInfo.isDirectory();
|
|
156
|
+
isFile = realInfo.isFile();
|
|
157
|
+
size = realInfo.size;
|
|
158
|
+
}
|
|
159
|
+
if (isDir) {
|
|
160
|
+
if (depth + 1 > limits.maxDepth) {
|
|
161
|
+
state.limitsHit.add('max-depth');
|
|
162
|
+
state.incomplete = true;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const canonicalDir = info.isSymbolicLink() ? targetAbs : abs;
|
|
166
|
+
if (visitedDirs.has(canonicalDir))
|
|
167
|
+
continue; // cycle guard
|
|
168
|
+
visitedDirs.add(canonicalDir);
|
|
169
|
+
if (!recurse(targetAbs, rel, depth + 1))
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
else if (isFile) {
|
|
173
|
+
state.filesScanned += 1;
|
|
174
|
+
visit(rel, targetAbs, size);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
// Unconditional realpath containment before any traversal: catches both
|
|
180
|
+
// a symlinked start entry and a symlink on an INTERMEDIATE component of
|
|
181
|
+
// the declared path (lstat on the final entry alone would miss it).
|
|
182
|
+
const startReal = containedReal(rootCanonical, startRel, state);
|
|
183
|
+
if (startReal === null)
|
|
184
|
+
return;
|
|
185
|
+
let startInfo;
|
|
186
|
+
try {
|
|
187
|
+
startInfo = statSync(startReal);
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (startInfo.isDirectory()) {
|
|
193
|
+
visitedDirs.add(startReal);
|
|
194
|
+
recurse(startReal, startRel, 1);
|
|
195
|
+
}
|
|
196
|
+
else if (startInfo.isFile()) {
|
|
197
|
+
if (state.filesScanned < limits.maxFilesPerPlugin) {
|
|
198
|
+
state.filesScanned += 1;
|
|
199
|
+
visit(startRel, startReal, startInfo.size);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function scan(installPath) {
|
|
204
|
+
const rootCanonical = canonicalize(installPath);
|
|
205
|
+
const memoized = memo.get(rootCanonical);
|
|
206
|
+
if (memoized)
|
|
207
|
+
return memoized;
|
|
208
|
+
const result = emptyResult();
|
|
209
|
+
if (pluginsScanned >= limits.maxPlugins) {
|
|
210
|
+
result.limitsHit = ['max-plugins'];
|
|
211
|
+
result.incomplete = true;
|
|
212
|
+
truncated = true;
|
|
213
|
+
memo.set(rootCanonical, result);
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
let rootInfo;
|
|
217
|
+
try {
|
|
218
|
+
rootInfo = statSync(rootCanonical);
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
result.pathIssue = 'missing';
|
|
222
|
+
memo.set(rootCanonical, result);
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
if (!rootInfo.isDirectory()) {
|
|
226
|
+
result.pathIssue = 'not-directory';
|
|
227
|
+
memo.set(rootCanonical, result);
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
try {
|
|
231
|
+
readdirSync(rootCanonical);
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
result.pathIssue = 'unreadable';
|
|
235
|
+
memo.set(rootCanonical, result);
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
pluginsScanned += 1;
|
|
239
|
+
const state = {
|
|
240
|
+
filesScanned: 0,
|
|
241
|
+
bytesRead: 0,
|
|
242
|
+
limitsHit: new Set(),
|
|
243
|
+
symlinksSkipped: [],
|
|
244
|
+
parseErrors: [],
|
|
245
|
+
incomplete: false,
|
|
246
|
+
};
|
|
247
|
+
// Direct reads (manifest, root SKILL.md, config files) go through the
|
|
248
|
+
// same containment and budgets as the walk: realpath must stay inside the
|
|
249
|
+
// canonical root (escapes are reported, never read), the per-plugin file
|
|
250
|
+
// count applies, and bytes are charged through readBounded.
|
|
251
|
+
const openDirect = (rel) => {
|
|
252
|
+
const opened = safeOpen(rootCanonical, rel, state);
|
|
253
|
+
if (!opened)
|
|
254
|
+
return { found: false, content: null };
|
|
255
|
+
if (state.filesScanned >= limits.maxFilesPerPlugin) {
|
|
256
|
+
state.limitsHit.add('max-files');
|
|
257
|
+
state.incomplete = true;
|
|
258
|
+
return { found: true, content: null };
|
|
259
|
+
}
|
|
260
|
+
state.filesScanned += 1;
|
|
261
|
+
return { found: true, content: readBounded(opened.abs, opened.size, state) };
|
|
262
|
+
};
|
|
263
|
+
scanPlugin(rootCanonical, state, result, (abs, size) => readBounded(abs, size, state), walk, openDirect);
|
|
264
|
+
result.filesScanned = state.filesScanned;
|
|
265
|
+
result.bytesRead = state.bytesRead;
|
|
266
|
+
result.limitsHit = [...state.limitsHit].sort();
|
|
267
|
+
result.symlinksSkipped = state.symlinksSkipped.sort();
|
|
268
|
+
result.parseErrors = state.parseErrors.sort();
|
|
269
|
+
result.incomplete = state.incomplete;
|
|
270
|
+
memo.set(rootCanonical, result);
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
scan,
|
|
275
|
+
stats: () => ({
|
|
276
|
+
pluginsScanned,
|
|
277
|
+
filesScanned: [...memo.values()].reduce((sum, r) => sum + r.filesScanned, 0),
|
|
278
|
+
bytesRead: totalBytesRead,
|
|
279
|
+
truncated: truncated || [...memo.values()].some((r) => r.incomplete),
|
|
280
|
+
}),
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Contained path resolution: the lexical resolve AND the realpath must both
|
|
285
|
+
* stay inside the canonical root. Applied unconditionally — a symlink on an
|
|
286
|
+
* INTERMEDIATE component ("bridge/commands" where bridge points outside)
|
|
287
|
+
* escapes even though the final entry is a plain file or directory, and only
|
|
288
|
+
* the realpath sees that. An escaping path is recorded as skipped and never
|
|
289
|
+
* touched; a missing path is simply absent (null without a report).
|
|
290
|
+
*/
|
|
291
|
+
function containedReal(rootCanonical, rel, state) {
|
|
292
|
+
const lexical = resolve(rootCanonical, rel);
|
|
293
|
+
if (!isWithin(rootCanonical, lexical))
|
|
294
|
+
return null;
|
|
295
|
+
let real;
|
|
296
|
+
try {
|
|
297
|
+
real = realpathSync(lexical);
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
try {
|
|
301
|
+
lstatSync(lexical);
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
return null; // truly absent
|
|
305
|
+
}
|
|
306
|
+
// Exists but unresolvable (dangling link, loop) — report, don't hide.
|
|
307
|
+
state.symlinksSkipped.push(rel);
|
|
308
|
+
state.incomplete = true;
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
if (!isWithin(rootCanonical, real)) {
|
|
312
|
+
state.symlinksSkipped.push(rel);
|
|
313
|
+
state.incomplete = true;
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
return real;
|
|
317
|
+
}
|
|
318
|
+
function safeOpen(rootCanonical, rel, state) {
|
|
319
|
+
const real = containedReal(rootCanonical, rel, state);
|
|
320
|
+
if (real === null)
|
|
321
|
+
return null;
|
|
322
|
+
let info;
|
|
323
|
+
try {
|
|
324
|
+
info = statSync(real);
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
if (!info.isFile())
|
|
330
|
+
return null;
|
|
331
|
+
return { abs: real, size: info.size };
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Cap a list of externally sourced items. Truncation NEVER happens silently:
|
|
335
|
+
* exceeding the cap records the given limit marker and marks the scan
|
|
336
|
+
* incomplete.
|
|
337
|
+
*/
|
|
338
|
+
function boundList(items, state, marker) {
|
|
339
|
+
if (items.length > MAX_KEYS) {
|
|
340
|
+
state.limitsHit.add(marker);
|
|
341
|
+
state.incomplete = true;
|
|
342
|
+
return items.slice(0, MAX_KEYS);
|
|
343
|
+
}
|
|
344
|
+
return items;
|
|
345
|
+
}
|
|
346
|
+
/** Cap a manifest path list so a hostile manifest cannot fan out the scan. */
|
|
347
|
+
function boundPaths(paths, state) {
|
|
348
|
+
return boundList(paths, state, 'max-manifest-paths');
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Whether a raw `skills` manifest value explicitly declares the plugin root
|
|
352
|
+
* ("." or "./"), checked BEFORE normalization — normalizeManifestPath strips
|
|
353
|
+
* "./" to "", so this must look at the raw strings (mirrors the platform's
|
|
354
|
+
* manifestIncludesRootPath).
|
|
355
|
+
*/
|
|
356
|
+
function declaresRootPath(rawSkills) {
|
|
357
|
+
const values = typeof rawSkills === 'string' ? [rawSkills] : Array.isArray(rawSkills) ? rawSkills : [];
|
|
358
|
+
return values.some((v) => typeof v === 'string' && v.trim().replace(/\/+$/, '') === '.');
|
|
359
|
+
}
|
|
360
|
+
/** True when skills/ exists (contained) and has at least one entry. */
|
|
361
|
+
function skillsDirHasEntries(rootCanonical) {
|
|
362
|
+
try {
|
|
363
|
+
const real = realpathSync(join(rootCanonical, 'skills'));
|
|
364
|
+
if (!isWithin(rootCanonical, real))
|
|
365
|
+
return false;
|
|
366
|
+
return readdirSync(real).length > 0;
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/** Strip a leading "./" and any trailing "/" from a manifest path. */
|
|
373
|
+
function normalizeManifestPath(value) {
|
|
374
|
+
let out = value.trim();
|
|
375
|
+
while (out.startsWith('./'))
|
|
376
|
+
out = out.slice(2);
|
|
377
|
+
while (out.endsWith('/'))
|
|
378
|
+
out = out.slice(0, -1);
|
|
379
|
+
return out;
|
|
380
|
+
}
|
|
381
|
+
function stringPaths(value) {
|
|
382
|
+
if (typeof value === 'string')
|
|
383
|
+
return value.trim() ? [normalizeManifestPath(value)] : [];
|
|
384
|
+
if (Array.isArray(value)) {
|
|
385
|
+
return value
|
|
386
|
+
.filter((v) => typeof v === 'string' && v.trim().length > 0)
|
|
387
|
+
.map(normalizeManifestPath);
|
|
388
|
+
}
|
|
389
|
+
return [];
|
|
390
|
+
}
|
|
391
|
+
const BLOCK_SCALAR_RE = /^([|>])([+-]?)$/;
|
|
392
|
+
/** Detects explicit indent indicators like |2, >2, |+2, >-2, |2+, >2- that we don't handle. */
|
|
393
|
+
const BLOCK_SCALAR_EXPLICIT_INDENT_RE = /^[|>][+-]?\d|^[|>]\d[+-]?/;
|
|
394
|
+
function readBlockScalar(lines, startIndex, limit, indicator, chomping) {
|
|
395
|
+
let indent = -1;
|
|
396
|
+
const bodyLines = [];
|
|
397
|
+
let i = startIndex;
|
|
398
|
+
for (; i < limit; i++) {
|
|
399
|
+
const line = lines[i];
|
|
400
|
+
if (line.trim() === '') {
|
|
401
|
+
bodyLines.push('');
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const leadingSpaces = line.match(/^( *)/)?.[1].length ?? 0;
|
|
405
|
+
if (indent === -1) {
|
|
406
|
+
indent = leadingSpaces;
|
|
407
|
+
if (indent === 0) {
|
|
408
|
+
if (/^---\s*$/.test(line))
|
|
409
|
+
break;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (leadingSpaces < indent)
|
|
414
|
+
break;
|
|
415
|
+
bodyLines.push(line.slice(indent));
|
|
416
|
+
}
|
|
417
|
+
if (i >= limit && lines.length > limit) {
|
|
418
|
+
return { value: '', nextIndex: i, truncated: true };
|
|
419
|
+
}
|
|
420
|
+
let raw;
|
|
421
|
+
if (indicator === '|') {
|
|
422
|
+
raw = bodyLines.join('\n');
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
// Folded mode: fold adjacent normal-indented lines into one, but preserve
|
|
426
|
+
// literal newlines around blank lines (paragraph breaks) and more-indented
|
|
427
|
+
// lines (lines with extra indentation beyond the block indent).
|
|
428
|
+
//
|
|
429
|
+
// YAML spec folding: N consecutive blank lines between content produce
|
|
430
|
+
// exactly N newlines in output (1 blank = paragraph separator = single \n
|
|
431
|
+
// between paragraphs; 2 blanks = \n\n; 3 blanks = \n\n\n; etc.).
|
|
432
|
+
// More-indented lines always get a literal newline before/after them
|
|
433
|
+
// (they are never folded into surrounding content).
|
|
434
|
+
const stripped = [...bodyLines];
|
|
435
|
+
while (stripped.length > 0 && stripped[stripped.length - 1] === '')
|
|
436
|
+
stripped.pop();
|
|
437
|
+
let output = '';
|
|
438
|
+
let currentRun = [];
|
|
439
|
+
// Track whether we just emitted blank-line newlines; if so, the next
|
|
440
|
+
// content should NOT add its own \n separator — the blanks already did.
|
|
441
|
+
let afterBlanks = false;
|
|
442
|
+
// Track whether the last emitted content was a more-indented line (which
|
|
443
|
+
// is NOT newline-terminated in the output). Blank lines following a
|
|
444
|
+
// more-indented line must add an extra \n to terminate it first.
|
|
445
|
+
let afterMoreIndented = false;
|
|
446
|
+
const flushRun = () => {
|
|
447
|
+
if (currentRun.length > 0) {
|
|
448
|
+
output += currentRun.join(' ');
|
|
449
|
+
currentRun = [];
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
let idx = 0;
|
|
453
|
+
while (idx < stripped.length) {
|
|
454
|
+
const bl = stripped[idx];
|
|
455
|
+
if (bl === '') {
|
|
456
|
+
// Count consecutive blank lines
|
|
457
|
+
flushRun();
|
|
458
|
+
let blankCount = 0;
|
|
459
|
+
while (idx < stripped.length && stripped[idx] === '') {
|
|
460
|
+
blankCount++;
|
|
461
|
+
idx++;
|
|
462
|
+
}
|
|
463
|
+
// If the preceding content was a more-indented line, it didn't end
|
|
464
|
+
// with a newline — add one to terminate it before the blank separation.
|
|
465
|
+
if (afterMoreIndented) {
|
|
466
|
+
output += '\n';
|
|
467
|
+
afterMoreIndented = false;
|
|
468
|
+
}
|
|
469
|
+
// N blank lines → N newlines in output
|
|
470
|
+
output += '\n'.repeat(blankCount);
|
|
471
|
+
afterBlanks = true;
|
|
472
|
+
}
|
|
473
|
+
else if (bl.startsWith(' ') || bl.startsWith('\t')) {
|
|
474
|
+
// More-indented line: not folded with adjacent lines
|
|
475
|
+
flushRun();
|
|
476
|
+
output += '\n';
|
|
477
|
+
output += bl;
|
|
478
|
+
afterBlanks = false;
|
|
479
|
+
afterMoreIndented = true;
|
|
480
|
+
idx++;
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
// Normal line: fold with adjacent normal lines
|
|
484
|
+
if (currentRun.length === 0 && output.length > 0 && !afterBlanks) {
|
|
485
|
+
// Separator between adjacent content (no intervening blank lines)
|
|
486
|
+
output += '\n';
|
|
487
|
+
}
|
|
488
|
+
currentRun.push(bl);
|
|
489
|
+
afterBlanks = false;
|
|
490
|
+
afterMoreIndented = false;
|
|
491
|
+
idx++;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
flushRun();
|
|
495
|
+
const folded = output;
|
|
496
|
+
let trailingCount = bodyLines.length - stripped.length;
|
|
497
|
+
// When folded content is blank-only, trailing newlines ARE the value —
|
|
498
|
+
// subtract one so the keep-chomping branch's unconditional +'\n' doesn't
|
|
499
|
+
// produce an off-by-one (there is no "final content line break" to add).
|
|
500
|
+
if (folded === '' && trailingCount > 0)
|
|
501
|
+
trailingCount -= 1;
|
|
502
|
+
raw = folded + '\n'.repeat(trailingCount);
|
|
503
|
+
}
|
|
504
|
+
let value;
|
|
505
|
+
if (chomping === '-') {
|
|
506
|
+
value = raw.replace(/\n+$/, '');
|
|
507
|
+
}
|
|
508
|
+
else if (chomping === '+') {
|
|
509
|
+
value = raw.length > 0 || bodyLines.length > 0 ? raw + '\n' : '';
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
const stripped = raw.replace(/\n+$/, '');
|
|
513
|
+
value = stripped.length > 0 || bodyLines.length > 0 ? stripped + '\n' : '';
|
|
514
|
+
}
|
|
515
|
+
return { value, nextIndex: i, truncated: false };
|
|
516
|
+
}
|
|
517
|
+
export function parseFrontmatter(content) {
|
|
518
|
+
if (!content.startsWith('---'))
|
|
519
|
+
return { name: null, description: null, understood: true };
|
|
520
|
+
const slice = content.slice(0, FRONTMATTER_MAX_BYTES);
|
|
521
|
+
const lines = slice.split('\n');
|
|
522
|
+
let name = null;
|
|
523
|
+
let description = null;
|
|
524
|
+
let closed = false;
|
|
525
|
+
const lineLimit = Math.min(lines.length, FRONTMATTER_MAX_LINES);
|
|
526
|
+
for (let i = 1; i < lineLimit; i += 1) {
|
|
527
|
+
const line = lines[i];
|
|
528
|
+
if (/^---\s*$/.test(line) && !line.startsWith(' ') && !line.startsWith('\t')) {
|
|
529
|
+
closed = true;
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
const match = line.match(/^(name|description)\s*:\s*(.*)$/);
|
|
533
|
+
if (!match)
|
|
534
|
+
continue;
|
|
535
|
+
const value = match[2].trim();
|
|
536
|
+
// Explicit indent indicators (|2, >2, >+1, etc.) are uncommon and complex —
|
|
537
|
+
// mark as not understood rather than risk a wrong parse.
|
|
538
|
+
if (BLOCK_SCALAR_EXPLICIT_INDENT_RE.test(value)) {
|
|
539
|
+
return { name, description, understood: false };
|
|
540
|
+
}
|
|
541
|
+
const blockMatch = value.match(BLOCK_SCALAR_RE);
|
|
542
|
+
if (blockMatch) {
|
|
543
|
+
const indicator = blockMatch[1];
|
|
544
|
+
const chomping = blockMatch[2];
|
|
545
|
+
const result = readBlockScalar(lines, i + 1, lineLimit, indicator, chomping);
|
|
546
|
+
if (result.truncated) {
|
|
547
|
+
return { name, description, understood: false };
|
|
548
|
+
}
|
|
549
|
+
i = result.nextIndex - 1;
|
|
550
|
+
if (match[1] === 'name')
|
|
551
|
+
name = result.value;
|
|
552
|
+
else
|
|
553
|
+
description = result.value;
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
if (value === '')
|
|
557
|
+
continue;
|
|
558
|
+
const unquoted = value.replace(/^["']/, '').replace(/["']$/, '');
|
|
559
|
+
if (match[1] === 'name')
|
|
560
|
+
name = unquoted;
|
|
561
|
+
else
|
|
562
|
+
description = unquoted;
|
|
563
|
+
}
|
|
564
|
+
return { name, description, understood: closed };
|
|
565
|
+
}
|
|
566
|
+
function normalizeForHash(content) {
|
|
567
|
+
return content.replace(/\r\n/g, '\n').replace(/\s+$/, '');
|
|
568
|
+
}
|
|
569
|
+
export function hashComponentContent(content) {
|
|
570
|
+
return createHash('sha256').update(normalizeForHash(content), 'utf-8').digest('hex');
|
|
571
|
+
}
|
|
572
|
+
function fileStem(relPath) {
|
|
573
|
+
const base = basename(relPath);
|
|
574
|
+
const dot = base.lastIndexOf('.');
|
|
575
|
+
return dot > 0 ? base.slice(0, dot) : base;
|
|
576
|
+
}
|
|
577
|
+
function buildComponent(type, relPath, content) {
|
|
578
|
+
if (content === null) {
|
|
579
|
+
return {
|
|
580
|
+
type,
|
|
581
|
+
relPath,
|
|
582
|
+
name: type === 'skill' ? basename(relPath) : fileStem(relPath),
|
|
583
|
+
description: null,
|
|
584
|
+
estimable: false,
|
|
585
|
+
nameDescChars: 0,
|
|
586
|
+
bodyChars: 0,
|
|
587
|
+
hash: null,
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
const frontmatter = parseFrontmatter(content);
|
|
591
|
+
const fallbackName = type === 'skill' ? basename(relPath.replace(/\/SKILL\.md$/, '')) : fileStem(relPath);
|
|
592
|
+
const name = frontmatter.name ?? (fallbackName === 'SKILL.md' ? 'skill' : fallbackName);
|
|
593
|
+
return {
|
|
594
|
+
type,
|
|
595
|
+
relPath,
|
|
596
|
+
name,
|
|
597
|
+
description: frontmatter.description,
|
|
598
|
+
estimable: frontmatter.understood,
|
|
599
|
+
nameDescChars: name.length + (frontmatter.description?.length ?? 0),
|
|
600
|
+
bodyChars: content.length,
|
|
601
|
+
hash: type === 'outputStyle' ? null : hashComponentContent(content),
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function parseDependencies(raw) {
|
|
605
|
+
if (!Array.isArray(raw))
|
|
606
|
+
return [];
|
|
607
|
+
const out = [];
|
|
608
|
+
for (const entry of raw.slice(0, MAX_KEYS)) {
|
|
609
|
+
if (typeof entry === 'string') {
|
|
610
|
+
const valid = VALID_NAME_RE.test(entry);
|
|
611
|
+
out.push({
|
|
612
|
+
name: valid ? entry : null,
|
|
613
|
+
versionRange: null,
|
|
614
|
+
marketplace: null,
|
|
615
|
+
parseable: valid,
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
else if (entry && typeof entry === 'object' && !Array.isArray(entry)) {
|
|
619
|
+
const dep = entry;
|
|
620
|
+
const nameValid = typeof dep.name === 'string' && VALID_NAME_RE.test(dep.name);
|
|
621
|
+
out.push({
|
|
622
|
+
name: nameValid ? dep.name : null,
|
|
623
|
+
versionRange: typeof dep.version === 'string' ? dep.version : null,
|
|
624
|
+
marketplace: typeof dep.marketplace === 'string' && VALID_NAME_RE.test(dep.marketplace)
|
|
625
|
+
? dep.marketplace
|
|
626
|
+
: null,
|
|
627
|
+
parseable: nameValid,
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
else {
|
|
631
|
+
out.push({ name: null, versionRange: null, marketplace: null, parseable: false });
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return out;
|
|
635
|
+
}
|
|
636
|
+
function isPathOrArray(value) {
|
|
637
|
+
return (typeof value === 'string' ||
|
|
638
|
+
(Array.isArray(value) && value.every((v) => typeof v === 'string')));
|
|
639
|
+
}
|
|
640
|
+
function isPathArrayOrObject(value) {
|
|
641
|
+
if (typeof value === 'string')
|
|
642
|
+
return true;
|
|
643
|
+
if (Array.isArray(value)) {
|
|
644
|
+
return value.every((v) => typeof v === 'string' || (v !== null && typeof v === 'object'));
|
|
645
|
+
}
|
|
646
|
+
return value !== null && typeof value === 'object';
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* PATH-style prerequisite lookup. Pure fs — the command is never executed.
|
|
650
|
+
* Returns null when the declaration cannot be resolved statically
|
|
651
|
+
* (env expansion other than ${CLAUDE_PLUGIN_ROOT}, relative paths):
|
|
652
|
+
* "unresolvable" must never be reported as "missing".
|
|
653
|
+
*/
|
|
654
|
+
export function commandOnPath(rawCommand, args, rootCanonical, env = process.env) {
|
|
655
|
+
const form = Array.isArray(args) ? 'exec' : 'shell';
|
|
656
|
+
let target = form === 'shell' ? rawCommand.trim().split(/\s+/)[0] : rawCommand.trim();
|
|
657
|
+
if (target.length === 0)
|
|
658
|
+
return null;
|
|
659
|
+
target = target.replace(/\$\{CLAUDE_PLUGIN_ROOT\}/g, rootCanonical);
|
|
660
|
+
if (target.includes('${'))
|
|
661
|
+
return null;
|
|
662
|
+
const isExecutable = (candidate) => {
|
|
663
|
+
try {
|
|
664
|
+
if (process.platform === 'win32') {
|
|
665
|
+
return existsSync(candidate);
|
|
666
|
+
}
|
|
667
|
+
accessSync(candidate, constants.X_OK);
|
|
668
|
+
return statSync(candidate).isFile();
|
|
669
|
+
}
|
|
670
|
+
catch {
|
|
671
|
+
return false;
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
if (target.includes('/') || target.includes('\\')) {
|
|
675
|
+
if (target.startsWith('/') || /^[A-Za-z]:[\\/]/.test(target))
|
|
676
|
+
return isExecutable(target);
|
|
677
|
+
return null; // relative path — base directory is ambiguous at runtime
|
|
678
|
+
}
|
|
679
|
+
const pathEntries = (env.PATH ?? '').split(delimiter).filter(Boolean);
|
|
680
|
+
const suffixes = process.platform === 'win32'
|
|
681
|
+
? (env.PATHEXT ?? '.EXE;.CMD;.BAT;.COM').split(';').map((s) => s.toLowerCase())
|
|
682
|
+
: [''];
|
|
683
|
+
for (const dir of pathEntries) {
|
|
684
|
+
for (const suffix of suffixes) {
|
|
685
|
+
if (isExecutable(join(dir, target + suffix)))
|
|
686
|
+
return true;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
return false;
|
|
690
|
+
}
|
|
691
|
+
/** Bounded key-name list keeping the ORIGINAL key count as total/omitted. */
|
|
692
|
+
function boundedKeys(value) {
|
|
693
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
694
|
+
return { items: [], total: 0, omitted: 0 };
|
|
695
|
+
}
|
|
696
|
+
const keys = Object.keys(value).filter((k) => typeof k === 'string');
|
|
697
|
+
const items = keys.slice(0, MAX_KEYS).map(boundName).sort();
|
|
698
|
+
return { items, total: keys.length, omitted: keys.length - items.length };
|
|
699
|
+
}
|
|
700
|
+
function parseHooksConfig(raw) {
|
|
701
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
702
|
+
return [];
|
|
703
|
+
const container = raw;
|
|
704
|
+
const events = container.hooks && typeof container.hooks === 'object' && !Array.isArray(container.hooks)
|
|
705
|
+
? container.hooks
|
|
706
|
+
: container;
|
|
707
|
+
const facts = [];
|
|
708
|
+
for (const [event, entriesRaw] of Object.entries(events)) {
|
|
709
|
+
if (facts.length > MAX_FACTS_PER_TYPE)
|
|
710
|
+
break;
|
|
711
|
+
if (!Array.isArray(entriesRaw))
|
|
712
|
+
continue;
|
|
713
|
+
for (const entryRaw of entriesRaw) {
|
|
714
|
+
if (facts.length > MAX_FACTS_PER_TYPE)
|
|
715
|
+
break;
|
|
716
|
+
if (!entryRaw || typeof entryRaw !== 'object' || Array.isArray(entryRaw))
|
|
717
|
+
continue;
|
|
718
|
+
const entry = entryRaw;
|
|
719
|
+
const matcher = entry.matcher;
|
|
720
|
+
const handlers = Array.isArray(entry.hooks)
|
|
721
|
+
? entry.hooks
|
|
722
|
+
: typeof entry.type === 'string'
|
|
723
|
+
? [entry]
|
|
724
|
+
: [];
|
|
725
|
+
const entryConditional = typeof entry.if === 'string' && entry.if.length > 0;
|
|
726
|
+
for (const handlerRaw of handlers) {
|
|
727
|
+
if (facts.length > MAX_FACTS_PER_TYPE)
|
|
728
|
+
break;
|
|
729
|
+
if (!handlerRaw || typeof handlerRaw !== 'object' || Array.isArray(handlerRaw))
|
|
730
|
+
continue;
|
|
731
|
+
const handler = handlerRaw;
|
|
732
|
+
const type = typeof handler.type === 'string' ? handler.type : 'unknown';
|
|
733
|
+
const toolMatched = TOOL_MATCHED_EVENTS.has(event);
|
|
734
|
+
const broad = toolMatched && (matcher === undefined || matcher === '' || matcher === '*');
|
|
735
|
+
facts.push({
|
|
736
|
+
event: boundName(event),
|
|
737
|
+
toolMatched,
|
|
738
|
+
broad,
|
|
739
|
+
conditional: entryConditional || (typeof handler.if === 'string' && handler.if.length > 0),
|
|
740
|
+
type: boundName(type),
|
|
741
|
+
command: sanitizeCommand(handler.command, handler.args),
|
|
742
|
+
targetOrigin: type === 'http' ? sanitizeUrlOrigin(handler.url) : null,
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return facts;
|
|
748
|
+
}
|
|
749
|
+
function parseMcpConfig(raw, rootCanonical) {
|
|
750
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
751
|
+
return [];
|
|
752
|
+
const container = raw;
|
|
753
|
+
const servers = container.mcpServers &&
|
|
754
|
+
typeof container.mcpServers === 'object' &&
|
|
755
|
+
!Array.isArray(container.mcpServers)
|
|
756
|
+
? container.mcpServers
|
|
757
|
+
: container;
|
|
758
|
+
const facts = [];
|
|
759
|
+
for (const [name, cfgRaw] of Object.entries(servers)) {
|
|
760
|
+
if (facts.length > MAX_FACTS_PER_TYPE)
|
|
761
|
+
break;
|
|
762
|
+
if (!cfgRaw || typeof cfgRaw !== 'object' || Array.isArray(cfgRaw))
|
|
763
|
+
continue;
|
|
764
|
+
const cfg = cfgRaw;
|
|
765
|
+
const hasUrl = typeof cfg.url === 'string' && cfg.url.length > 0;
|
|
766
|
+
const hasCommand = typeof cfg.command === 'string' && cfg.command.length > 0;
|
|
767
|
+
const kind = hasUrl ? 'remote' : hasCommand ? 'stdio' : 'unknown';
|
|
768
|
+
facts.push({
|
|
769
|
+
name: boundName(name),
|
|
770
|
+
kind,
|
|
771
|
+
command: hasCommand ? sanitizeCommand(cfg.command, cfg.args) : null,
|
|
772
|
+
targetOrigin: hasUrl ? sanitizeUrlOrigin(cfg.url) : null,
|
|
773
|
+
envKeys: boundedKeys(cfg.env),
|
|
774
|
+
headerKeys: boundedKeys(cfg.headers),
|
|
775
|
+
commandFound: hasCommand
|
|
776
|
+
? commandOnPath(cfg.command, cfg.args, rootCanonical)
|
|
777
|
+
: null,
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
return facts;
|
|
781
|
+
}
|
|
782
|
+
function parseLspConfig(raw, rootCanonical) {
|
|
783
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
784
|
+
return [];
|
|
785
|
+
const container = raw;
|
|
786
|
+
const entries = [];
|
|
787
|
+
if (typeof container.command === 'string') {
|
|
788
|
+
entries.push(['lsp', container]);
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
const map = container.lspServers &&
|
|
792
|
+
typeof container.lspServers === 'object' &&
|
|
793
|
+
!Array.isArray(container.lspServers)
|
|
794
|
+
? container.lspServers
|
|
795
|
+
: container;
|
|
796
|
+
for (const [name, cfg] of Object.entries(map)) {
|
|
797
|
+
if (entries.length > MAX_FACTS_PER_TYPE)
|
|
798
|
+
break;
|
|
799
|
+
if (cfg && typeof cfg === 'object' && !Array.isArray(cfg)) {
|
|
800
|
+
entries.push([name, cfg]);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
const facts = [];
|
|
805
|
+
for (const [name, cfg] of entries) {
|
|
806
|
+
const hasCommand = typeof cfg.command === 'string' && cfg.command.length > 0;
|
|
807
|
+
const extMap = cfg.extensionToLanguage &&
|
|
808
|
+
typeof cfg.extensionToLanguage === 'object' &&
|
|
809
|
+
!Array.isArray(cfg.extensionToLanguage)
|
|
810
|
+
? cfg.extensionToLanguage
|
|
811
|
+
: {};
|
|
812
|
+
const uniqueLanguages = [
|
|
813
|
+
...new Set(Object.values(extMap)
|
|
814
|
+
.filter((v) => typeof v === 'string')
|
|
815
|
+
.map(boundName)),
|
|
816
|
+
].sort();
|
|
817
|
+
facts.push({
|
|
818
|
+
name: boundName(name),
|
|
819
|
+
command: hasCommand ? sanitizeCommand(cfg.command, cfg.args) : null,
|
|
820
|
+
extensions: boundedKeys(extMap),
|
|
821
|
+
languages: {
|
|
822
|
+
items: uniqueLanguages.slice(0, MAX_KEYS),
|
|
823
|
+
total: uniqueLanguages.length,
|
|
824
|
+
omitted: Math.max(0, uniqueLanguages.length - MAX_KEYS),
|
|
825
|
+
},
|
|
826
|
+
commandFound: hasCommand
|
|
827
|
+
? commandOnPath(cfg.command, cfg.args, rootCanonical)
|
|
828
|
+
: null,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
return facts;
|
|
832
|
+
}
|
|
833
|
+
function parseMonitorEntries(raw) {
|
|
834
|
+
const list = Array.isArray(raw)
|
|
835
|
+
? raw
|
|
836
|
+
: raw && typeof raw === 'object' && Array.isArray(raw.monitors)
|
|
837
|
+
? raw.monitors
|
|
838
|
+
: [];
|
|
839
|
+
const facts = [];
|
|
840
|
+
for (const entry of list) {
|
|
841
|
+
if (facts.length > MAX_FACTS_PER_TYPE)
|
|
842
|
+
break;
|
|
843
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry))
|
|
844
|
+
continue;
|
|
845
|
+
const monitor = entry;
|
|
846
|
+
facts.push({
|
|
847
|
+
name: typeof monitor.name === 'string' ? boundName(monitor.name) : null,
|
|
848
|
+
when: typeof monitor.when === 'string' ? boundName(monitor.when) : null,
|
|
849
|
+
command: sanitizeCommand(monitor.command, undefined),
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
return facts;
|
|
853
|
+
}
|
|
854
|
+
function scanPlugin(rootCanonical, state, result, read, walk, openDirect) {
|
|
855
|
+
// --- manifest -------------------------------------------------------------
|
|
856
|
+
const manifestRel = '.claude-plugin/plugin.json';
|
|
857
|
+
const manifestAbs = join(rootCanonical, manifestRel);
|
|
858
|
+
let manifest = {};
|
|
859
|
+
let manifestExists = false;
|
|
860
|
+
try {
|
|
861
|
+
lstatSync(manifestAbs);
|
|
862
|
+
manifestExists = true;
|
|
863
|
+
}
|
|
864
|
+
catch {
|
|
865
|
+
manifestExists = false;
|
|
866
|
+
}
|
|
867
|
+
if (manifestExists) {
|
|
868
|
+
result.manifest.present = true;
|
|
869
|
+
const direct = openDirect(manifestRel);
|
|
870
|
+
// A refused read (escaping symlink → symlink_skipped, or a tripped budget
|
|
871
|
+
// → scan_incomplete) is already reported through those channels. The
|
|
872
|
+
// manifest stays UNINSPECTED: only content that was actually read may
|
|
873
|
+
// produce content-derived claims (invalid JSON / name / fields).
|
|
874
|
+
if (direct.content !== null) {
|
|
875
|
+
result.manifest.inspected = true;
|
|
876
|
+
try {
|
|
877
|
+
const parsed = JSON.parse(direct.content);
|
|
878
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
879
|
+
manifest = parsed;
|
|
880
|
+
}
|
|
881
|
+
else {
|
|
882
|
+
result.manifest.parseError = true;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
catch {
|
|
886
|
+
result.manifest.parseError = true;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
if (result.manifest.inspected && !result.manifest.parseError) {
|
|
891
|
+
const name = manifest.name;
|
|
892
|
+
if (typeof name === 'string' && VALID_NAME_RE.test(name)) {
|
|
893
|
+
result.manifest.name = name;
|
|
894
|
+
}
|
|
895
|
+
else {
|
|
896
|
+
result.manifest.nameInvalid = true;
|
|
897
|
+
if (typeof name === 'string')
|
|
898
|
+
result.manifest.name = null;
|
|
899
|
+
}
|
|
900
|
+
const experimental = manifest.experimental && typeof manifest.experimental === 'object' && !Array.isArray(manifest.experimental)
|
|
901
|
+
? manifest.experimental
|
|
902
|
+
: {};
|
|
903
|
+
const themesValue = manifest.themes !== undefined ? manifest.themes : experimental.themes;
|
|
904
|
+
const monitorsValue = manifest.monitors !== undefined ? manifest.monitors : experimental.monitors;
|
|
905
|
+
const checks = [
|
|
906
|
+
['commands', manifest.commands, isPathOrArray],
|
|
907
|
+
['agents', manifest.agents, isPathOrArray],
|
|
908
|
+
['skills', manifest.skills, isPathOrArray],
|
|
909
|
+
['outputStyles', manifest.outputStyles, isPathOrArray],
|
|
910
|
+
['themes', themesValue, isPathOrArray],
|
|
911
|
+
['hooks', manifest.hooks, isPathArrayOrObject],
|
|
912
|
+
['mcpServers', manifest.mcpServers, isPathArrayOrObject],
|
|
913
|
+
['lspServers', manifest.lspServers, isPathArrayOrObject],
|
|
914
|
+
['monitors', monitorsValue, isPathArrayOrObject],
|
|
915
|
+
['dependencies', manifest.dependencies, (v) => Array.isArray(v)],
|
|
916
|
+
];
|
|
917
|
+
for (const [field, value, valid] of checks) {
|
|
918
|
+
if (value !== undefined && !valid(value)) {
|
|
919
|
+
result.manifest.invalidFields.push(field);
|
|
920
|
+
// invalid declarations are ignored below — defaults apply
|
|
921
|
+
if (field === 'themes')
|
|
922
|
+
delete experimental.themes;
|
|
923
|
+
delete manifest[field];
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
if (Array.isArray(manifest.dependencies) && manifest.dependencies.length > MAX_KEYS) {
|
|
927
|
+
state.limitsHit.add('max-dependencies');
|
|
928
|
+
state.incomplete = true;
|
|
929
|
+
}
|
|
930
|
+
result.manifest.dependencies = parseDependencies(manifest.dependencies);
|
|
931
|
+
}
|
|
932
|
+
else if (result.manifest.parseError) {
|
|
933
|
+
manifest = {};
|
|
934
|
+
}
|
|
935
|
+
const experimental = manifest.experimental && typeof manifest.experimental === 'object' && !Array.isArray(manifest.experimental)
|
|
936
|
+
? manifest.experimental
|
|
937
|
+
: {};
|
|
938
|
+
// --- component path resolution (docs: skills ADD, others REPLACE) ---------
|
|
939
|
+
const commandsRoots = boundPaths(stringPaths(manifest.commands), state);
|
|
940
|
+
const agentsRoots = boundPaths(stringPaths(manifest.agents), state);
|
|
941
|
+
const outputStylesRoots = boundPaths(stringPaths(manifest.outputStyles), state);
|
|
942
|
+
const themesRoots = boundPaths(stringPaths(manifest.themes !== undefined ? manifest.themes : experimental.themes), state);
|
|
943
|
+
// A declared "." / "./" means the root SKILL.md (handled below as the
|
|
944
|
+
// documented special case) — it must not become a whole-tree walk root.
|
|
945
|
+
const explicitRootSkill = declaresRootPath(manifest.skills);
|
|
946
|
+
const declaredSkillsRoots = boundPaths(stringPaths(manifest.skills), state).filter((p) => p !== '' && p !== '.');
|
|
947
|
+
const skillsRoots = ['skills', ...declaredSkillsRoots];
|
|
948
|
+
const declared = [
|
|
949
|
+
...commandsRoots,
|
|
950
|
+
...agentsRoots,
|
|
951
|
+
...outputStylesRoots,
|
|
952
|
+
...themesRoots,
|
|
953
|
+
...declaredSkillsRoots,
|
|
954
|
+
...boundPaths(stringPaths(manifest.hooks), state),
|
|
955
|
+
...boundPaths(stringPaths(manifest.mcpServers), state),
|
|
956
|
+
...boundPaths(stringPaths(manifest.lspServers), state),
|
|
957
|
+
...boundPaths(stringPaths(manifest.monitors !== undefined ? manifest.monitors : experimental.monitors), state),
|
|
958
|
+
];
|
|
959
|
+
for (const rel of [...new Set(declared)].sort()) {
|
|
960
|
+
// Contained lexical resolve first: a "../…" or absolute declaration is
|
|
961
|
+
// reported as missing without ever touching the filesystem outside the
|
|
962
|
+
// root. Presence uses lstat (no symlink follow) — reads are contained
|
|
963
|
+
// separately when the path is actually opened or walked.
|
|
964
|
+
const lexical = resolve(rootCanonical, rel);
|
|
965
|
+
if (!isWithin(rootCanonical, lexical)) {
|
|
966
|
+
result.manifest.declaredMissing.push(rel);
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
try {
|
|
970
|
+
lstatSync(lexical);
|
|
971
|
+
}
|
|
972
|
+
catch {
|
|
973
|
+
result.manifest.declaredMissing.push(rel);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
const collectMd = (roots, type) => {
|
|
977
|
+
for (const root of [...new Set(roots)].sort()) {
|
|
978
|
+
walk(rootCanonical, root, state, (relPath, absPath, size) => {
|
|
979
|
+
if (!relPath.endsWith('.md'))
|
|
980
|
+
return;
|
|
981
|
+
if (type === 'skill' && basename(relPath) !== 'SKILL.md')
|
|
982
|
+
return;
|
|
983
|
+
result.components.push(buildComponent(type, relPath, read(absPath, size)));
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
collectMd(commandsRoots.length > 0 ? commandsRoots : ['commands'], 'command');
|
|
988
|
+
collectMd(agentsRoots.length > 0 ? agentsRoots : ['agents'], 'agent');
|
|
989
|
+
collectMd(outputStylesRoots.length > 0 ? outputStylesRoots : ['output-styles'], 'outputStyle');
|
|
990
|
+
collectMd(skillsRoots, 'skill');
|
|
991
|
+
// Documented special case: a single-skill plugin may keep SKILL.md at root.
|
|
992
|
+
// It counts ONLY when the manifest explicitly declares "." / "./", or when
|
|
993
|
+
// there is no `skills` manifest field AND no entries under skills/ — a root
|
|
994
|
+
// SKILL.md alongside a skills/ directory or a declared skills path is
|
|
995
|
+
// repository housekeeping, not a component.
|
|
996
|
+
const rootSkillEligible = explicitRootSkill || (manifest.skills === undefined && !skillsDirHasEntries(rootCanonical));
|
|
997
|
+
if (rootSkillEligible) {
|
|
998
|
+
const direct = openDirect('SKILL.md');
|
|
999
|
+
if (direct.found) {
|
|
1000
|
+
result.components.push(buildComponent('skill', 'SKILL.md', direct.content));
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
// --- themes ----------------------------------------------------------------
|
|
1004
|
+
for (const root of themesRoots.length > 0 ? [...new Set(themesRoots)].sort() : ['themes']) {
|
|
1005
|
+
walk(rootCanonical, root, state, (relPath, absPath, size) => {
|
|
1006
|
+
if (!relPath.endsWith('.json'))
|
|
1007
|
+
return;
|
|
1008
|
+
result.themeFiles.push(relPath);
|
|
1009
|
+
const content = read(absPath, size);
|
|
1010
|
+
if (content !== null) {
|
|
1011
|
+
try {
|
|
1012
|
+
JSON.parse(content);
|
|
1013
|
+
}
|
|
1014
|
+
catch {
|
|
1015
|
+
state.parseErrors.push(relPath);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
// --- JSON config helpers -----------------------------------------------------
|
|
1021
|
+
const readJsonConfig = (rel) => {
|
|
1022
|
+
const direct = openDirect(rel);
|
|
1023
|
+
if (direct.content === null)
|
|
1024
|
+
return undefined;
|
|
1025
|
+
try {
|
|
1026
|
+
return JSON.parse(direct.content);
|
|
1027
|
+
}
|
|
1028
|
+
catch {
|
|
1029
|
+
state.parseErrors.push(rel);
|
|
1030
|
+
return undefined;
|
|
1031
|
+
}
|
|
1032
|
+
};
|
|
1033
|
+
const configSources = (value, defaultRel) => {
|
|
1034
|
+
if (value === undefined)
|
|
1035
|
+
return { files: [defaultRel], inline: [] };
|
|
1036
|
+
if (typeof value === 'string')
|
|
1037
|
+
return { files: [normalizeManifestPath(value)], inline: [] };
|
|
1038
|
+
if (Array.isArray(value)) {
|
|
1039
|
+
return {
|
|
1040
|
+
files: boundPaths(value.filter((v) => typeof v === 'string').map(normalizeManifestPath), state),
|
|
1041
|
+
inline: boundList(value.filter((v) => v !== null && typeof v === 'object'), state, 'max-inline-configs'),
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
if (value !== null && typeof value === 'object')
|
|
1045
|
+
return { files: [], inline: [value] };
|
|
1046
|
+
return { files: [defaultRel], inline: [] };
|
|
1047
|
+
};
|
|
1048
|
+
// Aggregate fact cap per plugin: whatever the parse helpers produced,
|
|
1049
|
+
// never keep more than MAX_FACTS_PER_TYPE — and never drop silently.
|
|
1050
|
+
const pushFacts = (target, incoming) => {
|
|
1051
|
+
for (const fact of incoming) {
|
|
1052
|
+
if (target.length >= MAX_FACTS_PER_TYPE) {
|
|
1053
|
+
state.limitsHit.add('max-facts');
|
|
1054
|
+
state.incomplete = true;
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
target.push(fact);
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
1060
|
+
// --- hooks -------------------------------------------------------------------
|
|
1061
|
+
{
|
|
1062
|
+
const { files, inline } = configSources(manifest.hooks, 'hooks/hooks.json');
|
|
1063
|
+
for (const rel of files) {
|
|
1064
|
+
const parsed = readJsonConfig(rel);
|
|
1065
|
+
if (parsed !== undefined)
|
|
1066
|
+
pushFacts(result.hooks, parseHooksConfig(parsed));
|
|
1067
|
+
}
|
|
1068
|
+
for (const cfg of inline)
|
|
1069
|
+
pushFacts(result.hooks, parseHooksConfig(cfg));
|
|
1070
|
+
}
|
|
1071
|
+
// --- MCP servers ---------------------------------------------------------------
|
|
1072
|
+
{
|
|
1073
|
+
const { files, inline } = configSources(manifest.mcpServers, '.mcp.json');
|
|
1074
|
+
for (const rel of files) {
|
|
1075
|
+
const parsed = readJsonConfig(rel);
|
|
1076
|
+
if (parsed !== undefined)
|
|
1077
|
+
pushFacts(result.mcp, parseMcpConfig(parsed, rootCanonical));
|
|
1078
|
+
}
|
|
1079
|
+
for (const cfg of inline)
|
|
1080
|
+
pushFacts(result.mcp, parseMcpConfig({ mcpServers: cfg }, rootCanonical));
|
|
1081
|
+
}
|
|
1082
|
+
// --- LSP servers -----------------------------------------------------------------
|
|
1083
|
+
{
|
|
1084
|
+
const { files, inline } = configSources(manifest.lspServers, '.lsp.json');
|
|
1085
|
+
for (const rel of files) {
|
|
1086
|
+
const parsed = readJsonConfig(rel);
|
|
1087
|
+
if (parsed !== undefined)
|
|
1088
|
+
pushFacts(result.lsp, parseLspConfig(parsed, rootCanonical));
|
|
1089
|
+
}
|
|
1090
|
+
for (const cfg of inline)
|
|
1091
|
+
pushFacts(result.lsp, parseLspConfig(cfg, rootCanonical));
|
|
1092
|
+
}
|
|
1093
|
+
// Per-fact key lists (env/header keys, extensions, languages) are bounded
|
|
1094
|
+
// at parse time with true totals — any omission marks the scan incomplete.
|
|
1095
|
+
const keysOmitted = result.mcp.some((m) => m.envKeys.omitted > 0 || m.headerKeys.omitted > 0) ||
|
|
1096
|
+
result.lsp.some((l) => l.extensions.omitted > 0 || l.languages.omitted > 0);
|
|
1097
|
+
if (keysOmitted) {
|
|
1098
|
+
state.limitsHit.add('max-keys');
|
|
1099
|
+
state.incomplete = true;
|
|
1100
|
+
}
|
|
1101
|
+
// --- monitors ----------------------------------------------------------------------
|
|
1102
|
+
{
|
|
1103
|
+
const monitorsValue = manifest.monitors !== undefined ? manifest.monitors : experimental.monitors;
|
|
1104
|
+
const { files, inline } = configSources(monitorsValue, 'monitors/monitors.json');
|
|
1105
|
+
for (const rel of files) {
|
|
1106
|
+
const parsed = readJsonConfig(rel);
|
|
1107
|
+
if (parsed !== undefined)
|
|
1108
|
+
pushFacts(result.monitors, parseMonitorEntries(parsed));
|
|
1109
|
+
}
|
|
1110
|
+
if (inline.length > 0)
|
|
1111
|
+
pushFacts(result.monitors, parseMonitorEntries(inline));
|
|
1112
|
+
}
|
|
1113
|
+
// --- bin/ (spec: on PATH while the plugin is enabled; tree-only, no manifest) --------
|
|
1114
|
+
// The directory itself goes through the contained-realpath helper: a bin/
|
|
1115
|
+
// that is a symlink pointing outside the root must not be listed at all.
|
|
1116
|
+
const binReal = containedReal(rootCanonical, 'bin', state);
|
|
1117
|
+
if (binReal !== null) {
|
|
1118
|
+
let entries = [];
|
|
1119
|
+
try {
|
|
1120
|
+
entries = readdirSync(binReal).sort();
|
|
1121
|
+
}
|
|
1122
|
+
catch {
|
|
1123
|
+
entries = [];
|
|
1124
|
+
}
|
|
1125
|
+
if (entries.length > MAX_KEYS) {
|
|
1126
|
+
state.limitsHit.add('max-facts');
|
|
1127
|
+
state.incomplete = true;
|
|
1128
|
+
}
|
|
1129
|
+
for (const entry of entries.slice(0, MAX_KEYS)) {
|
|
1130
|
+
const abs = join(binReal, entry);
|
|
1131
|
+
let info;
|
|
1132
|
+
try {
|
|
1133
|
+
info = lstatSync(abs);
|
|
1134
|
+
}
|
|
1135
|
+
catch {
|
|
1136
|
+
continue;
|
|
1137
|
+
}
|
|
1138
|
+
if (info.isSymbolicLink()) {
|
|
1139
|
+
try {
|
|
1140
|
+
const real = realpathSync(abs);
|
|
1141
|
+
if (!isWithin(rootCanonical, real)) {
|
|
1142
|
+
state.symlinksSkipped.push(`bin/${entry}`);
|
|
1143
|
+
state.incomplete = true;
|
|
1144
|
+
continue;
|
|
1145
|
+
}
|
|
1146
|
+
info = statSync(real);
|
|
1147
|
+
}
|
|
1148
|
+
catch {
|
|
1149
|
+
state.symlinksSkipped.push(`bin/${entry}`);
|
|
1150
|
+
state.incomplete = true;
|
|
1151
|
+
continue;
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
if (!info.isFile())
|
|
1155
|
+
continue;
|
|
1156
|
+
state.filesScanned += 1;
|
|
1157
|
+
result.bin.push({
|
|
1158
|
+
relPath: `bin/${entry}`,
|
|
1159
|
+
executable: process.platform === 'win32' ? true : (info.mode & 0o111) !== 0,
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
// Overlapping declared roots (e.g. "cmds" and "cmds/sub") reach the same
|
|
1164
|
+
// file twice — one file is one component.
|
|
1165
|
+
const seenComponents = new Set();
|
|
1166
|
+
result.components = result.components.filter((component) => {
|
|
1167
|
+
const key = `${component.type} ${component.relPath}`;
|
|
1168
|
+
if (seenComponents.has(key))
|
|
1169
|
+
return false;
|
|
1170
|
+
seenComponents.add(key);
|
|
1171
|
+
return true;
|
|
1172
|
+
});
|
|
1173
|
+
result.components.sort((a, b) => (a.relPath < b.relPath ? -1 : a.relPath > b.relPath ? 1 : 0));
|
|
1174
|
+
result.themeFiles.sort();
|
|
1175
|
+
}
|