claude-slim 2.2.2 → 2.3.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 +25 -8
- package/dist/cleaner.js +22 -3
- package/dist/paths.d.ts +1 -0
- package/dist/paths.js +10 -1
- package/dist/report.js +4 -4
- package/dist/scanner/claude-md.d.ts +5 -0
- package/dist/scanner/claude-md.js +45 -0
- package/dist/scanner/constants.d.ts +4 -0
- package/dist/scanner/constants.js +4 -0
- package/dist/scanner/detectors.d.ts +20 -0
- package/dist/scanner/detectors.js +183 -0
- package/dist/scanner/disabled-plugins.d.ts +2 -0
- package/dist/scanner/disabled-plugins.js +26 -0
- package/dist/scanner/fs-walk.d.ts +7 -0
- package/dist/scanner/fs-walk.js +82 -0
- package/dist/scanner/index.d.ts +2 -0
- package/dist/scanner/index.js +55 -0
- package/dist/scanner/local-skills.d.ts +12 -0
- package/dist/scanner/local-skills.js +97 -0
- package/dist/scanner/mcp.d.ts +5 -0
- package/dist/scanner/mcp.js +17 -0
- package/dist/scanner/memory.d.ts +13 -0
- package/dist/scanner/memory.js +50 -0
- package/dist/scanner/plugin-skills.d.ts +12 -0
- package/dist/scanner/plugin-skills.js +65 -0
- package/dist/scanner.d.ts +5 -15
- package/dist/scanner.js +8 -528
- package/dist/selection.js +3 -1
- package/dist/tokenizer.js +19 -6
- package/package.json +5 -1
package/dist/scanner.js
CHANGED
|
@@ -1,528 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export
|
|
9
|
-
async function safeReadFile(p) {
|
|
10
|
-
try {
|
|
11
|
-
return await readFile(p, 'utf-8');
|
|
12
|
-
}
|
|
13
|
-
catch {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
async function safeReaddir(p) {
|
|
18
|
-
try {
|
|
19
|
-
return await readdir(p);
|
|
20
|
-
}
|
|
21
|
-
catch {
|
|
22
|
-
return [];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
async function isDirectory(p) {
|
|
26
|
-
try {
|
|
27
|
-
return (await stat(p)).isDirectory();
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async function isBrokenSymlink(p) {
|
|
34
|
-
try {
|
|
35
|
-
const lstats = await lstat(p);
|
|
36
|
-
if (!lstats.isSymbolicLink())
|
|
37
|
-
return false;
|
|
38
|
-
await realpath(p);
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
catch {
|
|
42
|
-
try {
|
|
43
|
-
return (await lstat(p)).isSymbolicLink();
|
|
44
|
-
}
|
|
45
|
-
catch {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
async function runCommand(cmd) {
|
|
51
|
-
try {
|
|
52
|
-
const { exec } = await import('node:child_process');
|
|
53
|
-
return new Promise((resolve) => {
|
|
54
|
-
exec(cmd, { timeout: 10000 }, (_err, stdout) => {
|
|
55
|
-
resolve(stdout || '');
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
catch {
|
|
60
|
-
return '';
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async function getDirSize(dir) {
|
|
64
|
-
let total = 0;
|
|
65
|
-
const entries = await safeReaddir(dir);
|
|
66
|
-
for (const entry of entries) {
|
|
67
|
-
const p = join(dir, entry);
|
|
68
|
-
try {
|
|
69
|
-
const s = await stat(p);
|
|
70
|
-
if (s.isFile())
|
|
71
|
-
total += s.size;
|
|
72
|
-
else if (s.isDirectory())
|
|
73
|
-
total += await getDirSize(p);
|
|
74
|
-
}
|
|
75
|
-
catch { /* skip */ }
|
|
76
|
-
}
|
|
77
|
-
return total;
|
|
78
|
-
}
|
|
79
|
-
// Content cache: avoids re-reading files during classification.
|
|
80
|
-
// Reset on every scan() so repeat invocations (e.g. pre/post-cleanup) do
|
|
81
|
-
// not accumulate entries for paths that no longer exist.
|
|
82
|
-
const contentCache = new Map();
|
|
83
|
-
async function resolveRealPath(p) {
|
|
84
|
-
try {
|
|
85
|
-
return await realpath(p);
|
|
86
|
-
}
|
|
87
|
-
catch {
|
|
88
|
-
return p;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
export function dedupeBySymlink(candidates) {
|
|
92
|
-
const seen = new Map();
|
|
93
|
-
for (const { skill, realMdPath } of candidates) {
|
|
94
|
-
const existing = seen.get(realMdPath);
|
|
95
|
-
if (!existing) {
|
|
96
|
-
seen.set(realMdPath, skill);
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
// Prefer top-level name (no slash) over nested duplicate
|
|
100
|
-
const existingIsNested = existing.name.includes('/');
|
|
101
|
-
const currentIsNested = skill.name.includes('/');
|
|
102
|
-
if (existingIsNested && !currentIsNested) {
|
|
103
|
-
seen.set(realMdPath, skill);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return Array.from(seen.values());
|
|
107
|
-
}
|
|
108
|
-
async function scanLocalSkills() {
|
|
109
|
-
const skillsDir = getSkillsDir();
|
|
110
|
-
const candidates = [];
|
|
111
|
-
const brokenSymlinks = [];
|
|
112
|
-
const entries = await safeReaddir(skillsDir);
|
|
113
|
-
const scanPromises = entries.map(async (entry) => {
|
|
114
|
-
const dirPath = join(skillsDir, entry);
|
|
115
|
-
if (!(await isDirectory(dirPath)))
|
|
116
|
-
return;
|
|
117
|
-
const skillMd = join(dirPath, 'SKILL.md');
|
|
118
|
-
if (await isBrokenSymlink(skillMd)) {
|
|
119
|
-
let target = 'unknown';
|
|
120
|
-
try {
|
|
121
|
-
target = await readlink(skillMd);
|
|
122
|
-
}
|
|
123
|
-
catch { /* */ }
|
|
124
|
-
brokenSymlinks.push({ name: entry, path: skillMd, target });
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
const content = await safeReadFile(skillMd);
|
|
128
|
-
if (content !== null) {
|
|
129
|
-
contentCache.set(skillMd, content);
|
|
130
|
-
const tokens = countTokensCached(content, skillMd);
|
|
131
|
-
const realMdPath = await resolveRealPath(skillMd);
|
|
132
|
-
candidates.push({
|
|
133
|
-
skill: {
|
|
134
|
-
name: entry,
|
|
135
|
-
path: dirPath,
|
|
136
|
-
sizeBytes: Buffer.byteLength(content),
|
|
137
|
-
tokens,
|
|
138
|
-
source: 'local',
|
|
139
|
-
},
|
|
140
|
-
realMdPath,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
// Nested skills (e.g., @internal-sys/commit-guide)
|
|
144
|
-
const subEntries = await safeReaddir(dirPath);
|
|
145
|
-
for (const sub of subEntries) {
|
|
146
|
-
const subDir = join(dirPath, sub);
|
|
147
|
-
if (!(await isDirectory(subDir)))
|
|
148
|
-
continue;
|
|
149
|
-
const subSkillMd = join(subDir, 'SKILL.md');
|
|
150
|
-
if (await isBrokenSymlink(subSkillMd)) {
|
|
151
|
-
let target = 'unknown';
|
|
152
|
-
try {
|
|
153
|
-
target = await readlink(subSkillMd);
|
|
154
|
-
}
|
|
155
|
-
catch { /* */ }
|
|
156
|
-
brokenSymlinks.push({ name: `${entry}/${sub}`, path: subSkillMd, target });
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
const subContent = await safeReadFile(subSkillMd);
|
|
160
|
-
if (subContent !== null) {
|
|
161
|
-
const name = `${entry}/${sub}`;
|
|
162
|
-
contentCache.set(subSkillMd, subContent);
|
|
163
|
-
const tokens = countTokensCached(subContent, subSkillMd);
|
|
164
|
-
const realMdPath = await resolveRealPath(subSkillMd);
|
|
165
|
-
candidates.push({
|
|
166
|
-
skill: {
|
|
167
|
-
name,
|
|
168
|
-
path: subDir,
|
|
169
|
-
sizeBytes: Buffer.byteLength(subContent),
|
|
170
|
-
tokens,
|
|
171
|
-
source: 'local',
|
|
172
|
-
},
|
|
173
|
-
realMdPath,
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
await Promise.all(scanPromises);
|
|
179
|
-
const skills = dedupeBySymlink(candidates);
|
|
180
|
-
return { skills, brokenSymlinks };
|
|
181
|
-
}
|
|
182
|
-
async function scanPluginSkills() {
|
|
183
|
-
const skills = [];
|
|
184
|
-
const plugins = [];
|
|
185
|
-
const tempCaches = [];
|
|
186
|
-
const pluginsDir = getPluginsDir();
|
|
187
|
-
const pluginDirs = await safeReaddir(pluginsDir);
|
|
188
|
-
const scanPromises = pluginDirs.map(async (pluginName) => {
|
|
189
|
-
const pluginDir = join(pluginsDir, pluginName);
|
|
190
|
-
if (!(await isDirectory(pluginDir)))
|
|
191
|
-
return;
|
|
192
|
-
// Detect temp_local_* cache dirs (failed plugin installs)
|
|
193
|
-
if (pluginName.startsWith('temp_local_')) {
|
|
194
|
-
const size = await getDirSize(pluginDir);
|
|
195
|
-
tempCaches.push({ name: pluginName, path: pluginDir, sizeKB: Math.round(size / 1024) });
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
const pluginSkillNames = [];
|
|
199
|
-
const walkDir = async (dir) => {
|
|
200
|
-
const entries = await safeReaddir(dir);
|
|
201
|
-
for (const entry of entries) {
|
|
202
|
-
const entryPath = join(dir, entry);
|
|
203
|
-
if (!(await isDirectory(entryPath)))
|
|
204
|
-
continue;
|
|
205
|
-
if (entry === 'skills') {
|
|
206
|
-
const skillDirs = await safeReaddir(entryPath);
|
|
207
|
-
for (const skillDir of skillDirs) {
|
|
208
|
-
const skillPath = join(entryPath, skillDir);
|
|
209
|
-
if (!(await isDirectory(skillPath)))
|
|
210
|
-
continue;
|
|
211
|
-
const skillMd = join(skillPath, 'SKILL.md');
|
|
212
|
-
const content = await safeReadFile(skillMd);
|
|
213
|
-
if (content !== null) {
|
|
214
|
-
pluginSkillNames.push(skillDir);
|
|
215
|
-
skills.push({
|
|
216
|
-
name: skillDir,
|
|
217
|
-
path: skillPath,
|
|
218
|
-
sizeBytes: Buffer.byteLength(content),
|
|
219
|
-
tokens: countTokensCached(content, skillMd),
|
|
220
|
-
source: 'plugin',
|
|
221
|
-
pluginName,
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
await walkDir(entryPath);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
await walkDir(pluginDir);
|
|
232
|
-
if (pluginSkillNames.length > 0) {
|
|
233
|
-
plugins.push({
|
|
234
|
-
name: pluginName,
|
|
235
|
-
skillCount: pluginSkillNames.length,
|
|
236
|
-
skills: pluginSkillNames,
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
await Promise.all(scanPromises);
|
|
241
|
-
return { skills, plugins, tempCaches };
|
|
242
|
-
}
|
|
243
|
-
async function scanMemoryFiles() {
|
|
244
|
-
const memoryFiles = [];
|
|
245
|
-
const staleProjects = [];
|
|
246
|
-
const projectsDir = getProjectsDir();
|
|
247
|
-
const projectDirs = await safeReaddir(projectsDir);
|
|
248
|
-
const now = Date.now();
|
|
249
|
-
const scanPromises = projectDirs.map(async (project) => {
|
|
250
|
-
const memDir = join(projectsDir, project, 'memory');
|
|
251
|
-
const files = await safeReaddir(memDir);
|
|
252
|
-
const mdFiles = files.filter((f) => f.endsWith('.md'));
|
|
253
|
-
let newestMtime = 0;
|
|
254
|
-
let totalBytes = 0;
|
|
255
|
-
for (const file of mdFiles) {
|
|
256
|
-
const filePath = join(memDir, file);
|
|
257
|
-
const content = await safeReadFile(filePath);
|
|
258
|
-
if (content !== null) {
|
|
259
|
-
const sizeBytes = Buffer.byteLength(content);
|
|
260
|
-
memoryFiles.push({
|
|
261
|
-
project,
|
|
262
|
-
name: file,
|
|
263
|
-
path: filePath,
|
|
264
|
-
sizeBytes,
|
|
265
|
-
tokens: countTokensCached(content, filePath),
|
|
266
|
-
});
|
|
267
|
-
totalBytes += sizeBytes;
|
|
268
|
-
try {
|
|
269
|
-
const s = await stat(filePath);
|
|
270
|
-
if (s.mtimeMs > newestMtime)
|
|
271
|
-
newestMtime = s.mtimeMs;
|
|
272
|
-
}
|
|
273
|
-
catch { /* skip */ }
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
// Check for stale project (no files modified in 90+ days)
|
|
277
|
-
if (mdFiles.length > 0 && newestMtime > 0) {
|
|
278
|
-
const ageDays = Math.floor((now - newestMtime) / (1000 * 60 * 60 * 24));
|
|
279
|
-
if (ageDays > STALE_DAYS) {
|
|
280
|
-
staleProjects.push({ project, path: memDir, ageDays, fileCount: mdFiles.length, totalBytes });
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
await Promise.all(scanPromises);
|
|
285
|
-
return { memoryFiles, staleProjects };
|
|
286
|
-
}
|
|
287
|
-
export function parseDisabledPlugins(output) {
|
|
288
|
-
const disabled = new Set();
|
|
289
|
-
if (!output)
|
|
290
|
-
return disabled;
|
|
291
|
-
let currentName = null;
|
|
292
|
-
for (const line of output.split('\n')) {
|
|
293
|
-
const trimmed = line.trim();
|
|
294
|
-
if (trimmed.startsWith('\u276f')) {
|
|
295
|
-
const full = trimmed.split('\u276f')[1]?.trim() || '';
|
|
296
|
-
// Format: sub-plugin@marketplace — extract marketplace name for cache dir matching
|
|
297
|
-
currentName = full.includes('@') ? full.split('@')[1] : full;
|
|
298
|
-
}
|
|
299
|
-
else if (trimmed.toLowerCase().includes('disabled') && currentName) {
|
|
300
|
-
disabled.add(currentName);
|
|
301
|
-
currentName = null;
|
|
302
|
-
}
|
|
303
|
-
else if (trimmed.toLowerCase().includes('enabled')) {
|
|
304
|
-
currentName = null;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
return disabled;
|
|
308
|
-
}
|
|
309
|
-
async function getDisabledPlugins() {
|
|
310
|
-
return parseDisabledPlugins(await runCommand('claude plugin list'));
|
|
311
|
-
}
|
|
312
|
-
export function parseClaudeMdSections(content) {
|
|
313
|
-
const sections = [];
|
|
314
|
-
const lines = content.split('\n');
|
|
315
|
-
let currentName = null;
|
|
316
|
-
let currentContent = '';
|
|
317
|
-
for (const line of lines) {
|
|
318
|
-
if (line.startsWith('# ')) {
|
|
319
|
-
if (currentName !== null) {
|
|
320
|
-
sections.push({
|
|
321
|
-
name: currentName,
|
|
322
|
-
sizeBytes: Buffer.byteLength(currentContent),
|
|
323
|
-
tokens: countTokensCached(currentContent, `claude-md-section:${currentName}`),
|
|
324
|
-
});
|
|
325
|
-
}
|
|
326
|
-
else if (currentContent.trim()) {
|
|
327
|
-
sections.push({
|
|
328
|
-
name: '(preamble)',
|
|
329
|
-
sizeBytes: Buffer.byteLength(currentContent),
|
|
330
|
-
tokens: countTokensCached(currentContent, 'claude-md-section:preamble'),
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
currentName = line.slice(2).trim().slice(0, 60);
|
|
334
|
-
currentContent = line + '\n';
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
currentContent += line + '\n';
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
if (currentName !== null) {
|
|
341
|
-
sections.push({
|
|
342
|
-
name: currentName,
|
|
343
|
-
sizeBytes: Buffer.byteLength(currentContent),
|
|
344
|
-
tokens: countTokensCached(currentContent, `claude-md-section:${currentName}`),
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
else if (currentContent.trim()) {
|
|
348
|
-
sections.push({
|
|
349
|
-
name: '(preamble)',
|
|
350
|
-
sizeBytes: Buffer.byteLength(currentContent),
|
|
351
|
-
tokens: countTokensCached(currentContent, 'claude-md-section:preamble'),
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
return sections;
|
|
355
|
-
}
|
|
356
|
-
async function scanMcpServers() {
|
|
357
|
-
const content = await safeReadFile(join(getClaudeDir(), 'settings.json'));
|
|
358
|
-
if (!content)
|
|
359
|
-
return { count: 0, names: [] };
|
|
360
|
-
try {
|
|
361
|
-
const data = JSON.parse(content);
|
|
362
|
-
const servers = data.mcpServers || {};
|
|
363
|
-
const names = Object.keys(servers).sort();
|
|
364
|
-
return { count: names.length, names };
|
|
365
|
-
}
|
|
366
|
-
catch {
|
|
367
|
-
return { count: 0, names: [] };
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
function classifyIssues(localSkills, pluginSkills, brokenSymlinks, memoryFiles, tempCaches, staleProjects, disabledPlugins, plugins) {
|
|
371
|
-
const issues = [];
|
|
372
|
-
const pluginSkillNames = new Set(pluginSkills.map((s) => s.name));
|
|
373
|
-
// Tier 1: broken symlinks
|
|
374
|
-
for (const link of brokenSymlinks) {
|
|
375
|
-
issues.push({
|
|
376
|
-
type: 'broken_symlink',
|
|
377
|
-
tier: 1,
|
|
378
|
-
name: link.name,
|
|
379
|
-
detail: link.target,
|
|
380
|
-
tokens: 0,
|
|
381
|
-
path: link.path,
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
for (const skill of localSkills) {
|
|
385
|
-
const skillMdPath = join(skill.path, 'SKILL.md');
|
|
386
|
-
// Tier 1: template skills (use cached content instead of re-reading)
|
|
387
|
-
const content = contentCache.get(skillMdPath);
|
|
388
|
-
if (content && content.includes('Replace with description')) {
|
|
389
|
-
issues.push({
|
|
390
|
-
type: 'template',
|
|
391
|
-
tier: 1,
|
|
392
|
-
name: skill.name,
|
|
393
|
-
tokens: skill.tokens,
|
|
394
|
-
path: skill.path,
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
// Tier 2: duplicates (local + plugin) — check base name for nested skills
|
|
398
|
-
const baseName = skill.name.includes('/') ? skill.name.split('/').pop() : skill.name;
|
|
399
|
-
if (pluginSkillNames.has(baseName)) {
|
|
400
|
-
issues.push({
|
|
401
|
-
type: 'duplicate',
|
|
402
|
-
tier: 2,
|
|
403
|
-
name: skill.name,
|
|
404
|
-
detail: 'local+plugin',
|
|
405
|
-
tokens: skill.tokens,
|
|
406
|
-
path: skill.path,
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
// Tier 3: oversized skills
|
|
410
|
-
if (skill.sizeBytes > OVERSIZED_SKILL_BYTES) {
|
|
411
|
-
issues.push({
|
|
412
|
-
type: 'oversized_skill',
|
|
413
|
-
tier: 3,
|
|
414
|
-
name: skill.name,
|
|
415
|
-
detail: `${Math.round(skill.sizeBytes / 1024)}KB`,
|
|
416
|
-
tokens: skill.tokens,
|
|
417
|
-
path: skill.path,
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
// Tier 1: .skill/ duplicate directories
|
|
422
|
-
for (const skill of localSkills) {
|
|
423
|
-
const dotSkillDir = skill.path + '.skill';
|
|
424
|
-
if (localSkills.some((s) => s.path === dotSkillDir)) {
|
|
425
|
-
issues.push({
|
|
426
|
-
type: 'skill_dup',
|
|
427
|
-
tier: 1,
|
|
428
|
-
name: skill.name,
|
|
429
|
-
tokens: 0,
|
|
430
|
-
path: dotSkillDir,
|
|
431
|
-
});
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
// Tier 1: temp_local_* cache directories
|
|
435
|
-
for (const temp of tempCaches) {
|
|
436
|
-
issues.push({
|
|
437
|
-
type: 'temp_cache',
|
|
438
|
-
tier: 1,
|
|
439
|
-
name: temp.name,
|
|
440
|
-
detail: `${temp.sizeKB}KB`,
|
|
441
|
-
tokens: 0,
|
|
442
|
-
path: temp.path,
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
// Tier 2: oversized memory files
|
|
446
|
-
for (const mem of memoryFiles) {
|
|
447
|
-
if (mem.sizeBytes > OVERSIZED_MEMORY_BYTES) {
|
|
448
|
-
issues.push({
|
|
449
|
-
type: 'oversized_memory',
|
|
450
|
-
tier: 2,
|
|
451
|
-
name: `${mem.project}/${mem.name}`,
|
|
452
|
-
detail: `${Math.round(mem.sizeBytes / 1024)}KB`,
|
|
453
|
-
tokens: mem.tokens,
|
|
454
|
-
path: mem.path,
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
// Tier 2: stale project memory (90+ days inactive)
|
|
459
|
-
for (const stale of staleProjects) {
|
|
460
|
-
const memTokens = memoryFiles
|
|
461
|
-
.filter((m) => m.project === stale.project)
|
|
462
|
-
.reduce((sum, m) => sum + m.tokens, 0);
|
|
463
|
-
issues.push({
|
|
464
|
-
type: 'stale_project',
|
|
465
|
-
tier: 2,
|
|
466
|
-
name: stale.project,
|
|
467
|
-
detail: `${stale.ageDays}d, ${stale.fileCount} files, ${Math.round(stale.totalBytes / 1024)}KB`,
|
|
468
|
-
tokens: memTokens,
|
|
469
|
-
path: stale.path,
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
// Tier 2: disabled plugins still occupying cache
|
|
473
|
-
for (const plugin of plugins) {
|
|
474
|
-
if (disabledPlugins.has(plugin.name)) {
|
|
475
|
-
issues.push({
|
|
476
|
-
type: 'disabled_plugin',
|
|
477
|
-
tier: 2,
|
|
478
|
-
name: plugin.name,
|
|
479
|
-
detail: `${plugin.skillCount} skills`,
|
|
480
|
-
tokens: plugin.skillCount * SKILL_PROMPT_OVERHEAD_TOKENS,
|
|
481
|
-
path: join(getPluginsDir(), plugin.name),
|
|
482
|
-
});
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
// Sort by tier
|
|
486
|
-
issues.sort((a, b) => a.tier - b.tier);
|
|
487
|
-
return issues;
|
|
488
|
-
}
|
|
489
|
-
export async function scan() {
|
|
490
|
-
contentCache.clear();
|
|
491
|
-
const [{ skills: localSkills, brokenSymlinks }, { skills: pluginSkills, plugins, tempCaches }, { memoryFiles, staleProjects }, mcp, disabledPlugins,] = await Promise.all([
|
|
492
|
-
scanLocalSkills(),
|
|
493
|
-
scanPluginSkills(),
|
|
494
|
-
scanMemoryFiles(),
|
|
495
|
-
scanMcpServers(),
|
|
496
|
-
getDisabledPlugins(),
|
|
497
|
-
]);
|
|
498
|
-
// Annotate plugin status
|
|
499
|
-
for (const plugin of plugins) {
|
|
500
|
-
plugin.status = disabledPlugins.has(plugin.name) ? 'disabled' : 'enabled';
|
|
501
|
-
}
|
|
502
|
-
// CLAUDE.md
|
|
503
|
-
const claudeMdContent = await safeReadFile(join(getClaudeDir(), 'CLAUDE.md'));
|
|
504
|
-
const claudeMdBytes = claudeMdContent ? Buffer.byteLength(claudeMdContent) : 0;
|
|
505
|
-
const claudeMdTokens = claudeMdContent
|
|
506
|
-
? countTokensCached(claudeMdContent, join(getClaudeDir(), 'CLAUDE.md'))
|
|
507
|
-
: 0;
|
|
508
|
-
const claudeMdSections = claudeMdContent ? parseClaudeMdSections(claudeMdContent) : [];
|
|
509
|
-
const issues = classifyIssues(localSkills, pluginSkills, brokenSymlinks, memoryFiles, tempCaches, staleProjects, disabledPlugins, plugins);
|
|
510
|
-
// Estimate total tokens at startup
|
|
511
|
-
const skillListingTokens = (localSkills.length + pluginSkills.length) * SKILL_PROMPT_OVERHEAD_TOKENS;
|
|
512
|
-
const memoryTokens = memoryFiles.reduce((sum, m) => sum + m.tokens, 0);
|
|
513
|
-
const totalTokensBefore = skillListingTokens + claudeMdTokens + memoryTokens;
|
|
514
|
-
return {
|
|
515
|
-
localSkills,
|
|
516
|
-
pluginSkills,
|
|
517
|
-
plugins,
|
|
518
|
-
brokenSymlinks,
|
|
519
|
-
memoryFiles,
|
|
520
|
-
claudeMdBytes,
|
|
521
|
-
claudeMdTokens,
|
|
522
|
-
claudeMdSections,
|
|
523
|
-
mcpServers: mcp.count,
|
|
524
|
-
mcpServerNames: mcp.names,
|
|
525
|
-
issues,
|
|
526
|
-
totalTokensBefore,
|
|
527
|
-
};
|
|
528
|
-
}
|
|
1
|
+
// Public barrel — keeps the previously-exported surface stable while the
|
|
2
|
+
// implementation lives in src/scanner/*. External importers (cli.ts, tests,
|
|
3
|
+
// future consumers) do not need to know about the split.
|
|
4
|
+
export { scan } from './scanner/index.js';
|
|
5
|
+
export { SKILL_PROMPT_OVERHEAD_TOKENS } from './scanner/constants.js';
|
|
6
|
+
export { dedupeBySymlink } from './scanner/local-skills.js';
|
|
7
|
+
export { parseDisabledPlugins } from './scanner/disabled-plugins.js';
|
|
8
|
+
export { parseClaudeMdSections } from './scanner/claude-md.js';
|
package/dist/selection.js
CHANGED
|
@@ -28,9 +28,11 @@ export function resolveRestoreSelection(input, count) {
|
|
|
28
28
|
return Array.from({ length: count }, (_, i) => i);
|
|
29
29
|
}
|
|
30
30
|
const indices = [];
|
|
31
|
+
const seen = new Set();
|
|
31
32
|
for (const part of trimmed.split(',')) {
|
|
32
33
|
const num = parseInt(part.trim(), 10);
|
|
33
|
-
if (!isNaN(num) && num >= 1 && num <= count) {
|
|
34
|
+
if (!isNaN(num) && num >= 1 && num <= count && !seen.has(num)) {
|
|
35
|
+
seen.add(num);
|
|
34
36
|
indices.push(num - 1);
|
|
35
37
|
}
|
|
36
38
|
}
|
package/dist/tokenizer.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
2
|
+
import { readFile, writeFile, mkdir, rename } from 'node:fs/promises';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
|
-
import {
|
|
4
|
+
import { getClaudeDir } from './paths.js';
|
|
5
5
|
let encoder = null;
|
|
6
6
|
let useFallback = false;
|
|
7
|
-
|
|
7
|
+
// Resolved lazily so the HOME env stub used in tests is honored.
|
|
8
|
+
function getCachePath() {
|
|
9
|
+
return join(getClaudeDir(), '.token-cache.json');
|
|
10
|
+
}
|
|
8
11
|
let cache = { version: 1, entries: {} };
|
|
9
12
|
let cacheDirty = false;
|
|
10
13
|
export async function initTokenizer() {
|
|
@@ -16,8 +19,12 @@ export async function initTokenizer() {
|
|
|
16
19
|
catch {
|
|
17
20
|
useFallback = true;
|
|
18
21
|
}
|
|
22
|
+
// Reset in-memory state so repeated initTokenizer() calls (e.g. across
|
|
23
|
+
// test cases) don't bleed cache entries from a prior invocation.
|
|
24
|
+
cache = { version: 1, entries: {} };
|
|
25
|
+
cacheDirty = false;
|
|
19
26
|
try {
|
|
20
|
-
const raw = await readFile(
|
|
27
|
+
const raw = await readFile(getCachePath(), 'utf-8');
|
|
21
28
|
cache = JSON.parse(raw);
|
|
22
29
|
}
|
|
23
30
|
catch {
|
|
@@ -47,9 +54,15 @@ export function countTokensCached(text, filePath) {
|
|
|
47
54
|
export async function flushCache() {
|
|
48
55
|
if (!cacheDirty)
|
|
49
56
|
return;
|
|
57
|
+
const target = getCachePath();
|
|
58
|
+
const tmp = target + '.tmp';
|
|
50
59
|
try {
|
|
51
|
-
await mkdir(dirname(
|
|
52
|
-
|
|
60
|
+
await mkdir(dirname(target), { recursive: true });
|
|
61
|
+
// Atomic: write to a sibling tmp file first, then rename. A crash mid-write
|
|
62
|
+
// leaves the prior cache (or nothing) — never a torn JSON file.
|
|
63
|
+
await writeFile(tmp, JSON.stringify(cache, null, 2));
|
|
64
|
+
await rename(tmp, target);
|
|
65
|
+
cacheDirty = false;
|
|
53
66
|
}
|
|
54
67
|
catch {
|
|
55
68
|
// Non-critical
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-slim",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Analyze and reduce Claude Code token overhead",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -34,6 +34,10 @@
|
|
|
34
34
|
"type": "git",
|
|
35
35
|
"url": "git+https://github.com/iops-leo/claude-slim.git"
|
|
36
36
|
},
|
|
37
|
+
"homepage": "https://github.com/iops-leo/claude-slim#readme",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/iops-leo/claude-slim/issues"
|
|
40
|
+
},
|
|
37
41
|
"engines": {
|
|
38
42
|
"node": ">=18"
|
|
39
43
|
},
|