sdlc-kit 0.0.0-stage → 0.1.1

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.
Files changed (35) hide show
  1. package/.claude-plugin/marketplace.json +13 -0
  2. package/.claude-plugin/plugin.json +12 -0
  3. package/CHANGELOG.md +44 -0
  4. package/LICENSE +21 -0
  5. package/README.md +168 -2
  6. package/commands/adr.md +15 -0
  7. package/commands/handoff-pr.md +20 -0
  8. package/commands/handoff.md +14 -0
  9. package/commands/pickup.md +9 -0
  10. package/commands/qa.md +15 -0
  11. package/commands/review.md +16 -0
  12. package/commands/ship.md +15 -0
  13. package/commands/spec.md +11 -0
  14. package/commands/start.md +20 -0
  15. package/dist/bin/cli.js +71 -0
  16. package/dist/lib/installer.js +693 -0
  17. package/dist/lib/statusline.js +46 -0
  18. package/package.json +54 -4
  19. package/skills/sdlc-orchestrator/SKILL.md +44 -0
  20. package/skills/sdlc-workflow/SKILL.md +98 -0
  21. package/skills/sdlc-workflow/references/architecture-checklist.md +26 -0
  22. package/skills/sdlc-workflow/references/clean-code.md +15 -0
  23. package/skills/sdlc-workflow/references/database-hygiene.md +20 -0
  24. package/skills/sdlc-workflow/references/developer-habits.md +29 -0
  25. package/skills/sdlc-workflow/references/incident-response.md +25 -0
  26. package/skills/sdlc-workflow/references/monolith-and-microservices.md +20 -0
  27. package/skills/sdlc-workflow/references/quality-gates.md +35 -0
  28. package/skills/sdlc-workflow/references/security-checklist.md +20 -0
  29. package/templates/local/LOCAL_PROMPT.md +8 -0
  30. package/templates/project/.github/PULL_REQUEST_TEMPLATE.md +10 -0
  31. package/templates/project/AGENTS.md +15 -0
  32. package/templates/project/CLAUDE.md +1 -0
  33. package/templates/project/docs/INDEX.md +7 -0
  34. package/templates/project/docs/adr/TEMPLATE.md +16 -0
  35. package/templates/project/docs/plans/.gitkeep +1 -0
@@ -0,0 +1,693 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ function findPackageRoot(start) {
8
+ let dir = start;
9
+ while (!fs.existsSync(path.join(dir, 'package.json'))) {
10
+ const parent = path.dirname(dir);
11
+ if (parent === dir)
12
+ throw new Error('package.json not found above ' + start);
13
+ dir = parent;
14
+ }
15
+ return dir;
16
+ }
17
+ export const DEFAULT_PACKAGE_ROOT = findPackageRoot(__dirname);
18
+ export const SKILL_NAMES = ['sdlc-workflow', 'sdlc-orchestrator'];
19
+ export const COMMAND_FILES = [
20
+ 'spec.md',
21
+ 'start.md',
22
+ 'handoff.md',
23
+ 'pickup.md',
24
+ 'handoff-pr.md',
25
+ 'adr.md',
26
+ 'qa.md',
27
+ 'review.md',
28
+ 'ship.md',
29
+ ];
30
+ // Agents with a custom slash-command directory that accepts our markdown commands.
31
+ const COMMAND_DIRS = {
32
+ claude: ['.claude', 'commands'],
33
+ cursor: ['.cursor', 'commands'],
34
+ opencode: ['.config', 'opencode', 'command'],
35
+ };
36
+ const CLAUDE_OPTIMIZE_ENV = {
37
+ CLAUDE_CODE_SUBAGENT_MODEL: 'sonnet',
38
+ CLAUDE_CODE_DISABLE_1M_CONTEXT: '1',
39
+ ENABLE_TOOL_SEARCH: 'auto:5',
40
+ CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: '70',
41
+ BASH_MAX_OUTPUT_LENGTH: '15000',
42
+ MAX_MCP_OUTPUT_TOKENS: '10000',
43
+ };
44
+ // Pre-TypeScript installs used a bash statusline; uninstall must still clean it up.
45
+ const LEGACY_STATUSLINE_FILE = 'statusline-command.sh';
46
+ const LEGACY_STATUSLINE_COMMAND = 'bash ~/.claude/statusline-command.sh';
47
+ const STATUSLINE_FILE = 'sdlc-kit-statusline.mjs';
48
+ // 0.1.0 shipped under the name agent-sdlc; installs made then are migrated on the next run.
49
+ const PRE_RENAME_STATUSLINE_FILE = 'agent-sdlc-statusline.mjs';
50
+ function statuslineCommand(homeDir, file = STATUSLINE_FILE) {
51
+ return `node "${path.join(homeDir, '.claude', file)}"`;
52
+ }
53
+ const PROJECT_GITIGNORE_ENTRIES = [
54
+ '.claude/HANDOFF.md',
55
+ 'CLAUDE.local.md',
56
+ '.claude/settings.local.json',
57
+ ];
58
+ /**
59
+ * Parse CLI arguments into installer options.
60
+ */
61
+ export function parseArgs(argv) {
62
+ const targets = new Set();
63
+ let optimize = false;
64
+ let includeOrchestrator = true;
65
+ let dryRun = false;
66
+ let uninstall = false;
67
+ let doctor = false;
68
+ let force = false;
69
+ let help = false;
70
+ let version = false;
71
+ for (const arg of argv) {
72
+ switch (arg) {
73
+ case '--claude':
74
+ targets.add('claude');
75
+ break;
76
+ case '--codex':
77
+ targets.add('codex');
78
+ break;
79
+ case '--antigravity':
80
+ targets.add('antigravity');
81
+ break;
82
+ case '--cursor':
83
+ targets.add('cursor');
84
+ break;
85
+ case '--opencode':
86
+ targets.add('opencode');
87
+ break;
88
+ case '--local':
89
+ targets.add('local');
90
+ break;
91
+ case '--project':
92
+ targets.add('project');
93
+ break;
94
+ case '--all':
95
+ targets.add('claude');
96
+ targets.add('codex');
97
+ targets.add('antigravity');
98
+ targets.add('cursor');
99
+ targets.add('opencode');
100
+ targets.add('local');
101
+ break;
102
+ case '--optimize':
103
+ optimize = true;
104
+ break;
105
+ case '--no-orchestrator':
106
+ includeOrchestrator = false;
107
+ break;
108
+ case '--dry-run':
109
+ dryRun = true;
110
+ break;
111
+ case '--uninstall':
112
+ uninstall = true;
113
+ break;
114
+ case '--doctor':
115
+ doctor = true;
116
+ break;
117
+ case '--force':
118
+ force = true;
119
+ break;
120
+ case '-h':
121
+ case '--help':
122
+ help = true;
123
+ break;
124
+ case '-v':
125
+ case '--version':
126
+ version = true;
127
+ break;
128
+ default:
129
+ throw new Error(`Unknown flag: ${arg}. Run with --help to see valid options.`);
130
+ }
131
+ }
132
+ return {
133
+ targets: [...targets],
134
+ optimize,
135
+ includeOrchestrator,
136
+ dryRun,
137
+ uninstall,
138
+ doctor,
139
+ force,
140
+ help,
141
+ version,
142
+ };
143
+ }
144
+ /**
145
+ * Auto-detect installed AI agent directories under homeDir.
146
+ * Falls back to ['antigravity'] (~/.agents/skills) when none are found.
147
+ */
148
+ export function detectTargets(homeDir = os.homedir()) {
149
+ const detected = [];
150
+ if (fs.existsSync(path.join(homeDir, '.claude')))
151
+ detected.push('claude');
152
+ if (fs.existsSync(path.join(homeDir, '.codex')))
153
+ detected.push('codex');
154
+ if (fs.existsSync(path.join(homeDir, '.agents')))
155
+ detected.push('antigravity');
156
+ if (fs.existsSync(path.join(homeDir, '.cursor')))
157
+ detected.push('cursor');
158
+ if (fs.existsSync(path.join(homeDir, '.config', 'opencode')))
159
+ detected.push('opencode');
160
+ return detected.length > 0 ? detected : ['antigravity'];
161
+ }
162
+ const MANIFEST_NAME = path.join('.sdlc-kit', 'manifest.json');
163
+ const PRE_RENAME_MANIFEST_NAME = path.join('.agent-sdlc', 'manifest.json');
164
+ const LOCAL_PROMPT_NAME = path.join('.sdlc-kit', 'LOCAL_PROMPT.md');
165
+ const PRE_RENAME_LOCAL_PROMPT_NAME = path.join('.agent-sdlc', 'LOCAL_PROMPT.md');
166
+ function sha256(file) {
167
+ return crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
168
+ }
169
+ function readPackageVersion(packageRoot) {
170
+ return JSON.parse(fs.readFileSync(path.join(packageRoot, 'package.json'), 'utf8')).version;
171
+ }
172
+ function loadManifest(homeDir) {
173
+ for (const name of [MANIFEST_NAME, PRE_RENAME_MANIFEST_NAME]) {
174
+ try {
175
+ const m = JSON.parse(fs.readFileSync(path.join(homeDir, name), 'utf8'));
176
+ return { version: m.version, files: m.files ?? {}, settings: m.settings ?? null };
177
+ }
178
+ catch {
179
+ // missing or unreadable: try the pre-rename location
180
+ }
181
+ }
182
+ return { version: null, files: {}, settings: null };
183
+ }
184
+ function removeManifestFile(homeDir, name) {
185
+ const file = path.join(homeDir, name);
186
+ fs.rmSync(file, { force: true });
187
+ try {
188
+ fs.rmdirSync(path.dirname(file));
189
+ }
190
+ catch {
191
+ // directory not empty or already gone
192
+ }
193
+ }
194
+ function saveManifest(homeDir, manifest, version, dryRun) {
195
+ if (dryRun)
196
+ return;
197
+ const file = path.join(homeDir, MANIFEST_NAME);
198
+ if (Object.keys(manifest.files).length === 0 && !manifest.settings) {
199
+ removeManifestFile(homeDir, MANIFEST_NAME);
200
+ removeManifestFile(homeDir, PRE_RENAME_MANIFEST_NAME);
201
+ return;
202
+ }
203
+ fs.mkdirSync(path.dirname(file), { recursive: true });
204
+ const body = { version, files: manifest.files, settings: manifest.settings };
205
+ fs.writeFileSync(file, `${JSON.stringify(body, null, 2)}\n`, 'utf8');
206
+ removeManifestFile(homeDir, PRE_RENAME_MANIFEST_NAME);
207
+ }
208
+ /** Copy a managed file unless it exists with content we did not write (unless --force). */
209
+ function installManagedFile(ctx, src, dst) {
210
+ const srcHash = sha256(src);
211
+ if (fs.existsSync(dst)) {
212
+ const cur = sha256(dst);
213
+ if (cur !== srcHash && cur !== ctx.manifest.files[dst] && !ctx.force) {
214
+ ctx.warnings.push(`Skipped ${dst}: exists and was not installed by sdlc-kit (use --force to overwrite)`);
215
+ return;
216
+ }
217
+ }
218
+ copyFileAction(src, dst, ctx.dryRun, ctx.actions);
219
+ ctx.manifest.files[dst] = srcHash;
220
+ }
221
+ /** Remove a managed file only if it is still exactly what we installed. */
222
+ function uninstallManagedFile(ctx, src, dst) {
223
+ if (!fs.existsSync(dst)) {
224
+ delete ctx.manifest.files[dst];
225
+ return;
226
+ }
227
+ const cur = sha256(dst);
228
+ if (cur === ctx.manifest.files[dst] || (src && cur === sha256(src)) || ctx.force) {
229
+ removePathAction(dst, ctx.dryRun, ctx.actions);
230
+ delete ctx.manifest.files[dst];
231
+ }
232
+ else {
233
+ ctx.warnings.push(`Kept ${dst}: modified since install (use --force to remove)`);
234
+ }
235
+ }
236
+ function listFiles(dir, base = dir) {
237
+ const out = [];
238
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
239
+ const full = path.join(dir, entry.name);
240
+ if (entry.isDirectory())
241
+ out.push(...listFiles(full, base));
242
+ else
243
+ out.push(path.relative(base, full));
244
+ }
245
+ return out;
246
+ }
247
+ function removeEmptyDirs(dir, dryRun) {
248
+ if (dryRun || !fs.existsSync(dir))
249
+ return;
250
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
251
+ if (entry.isDirectory())
252
+ removeEmptyDirs(path.join(dir, entry.name), dryRun);
253
+ }
254
+ if (fs.readdirSync(dir).length === 0)
255
+ fs.rmdirSync(dir);
256
+ }
257
+ function copyFileAction(src, dst, dryRun, actions) {
258
+ if (!dryRun) {
259
+ fs.mkdirSync(path.dirname(dst), { recursive: true });
260
+ fs.copyFileSync(src, dst);
261
+ }
262
+ actions.push(`${dryRun ? '[dry-run] ' : ''}install ${dst}`);
263
+ }
264
+ function removePathAction(targetPath, dryRun, actions) {
265
+ if (!fs.existsSync(targetPath))
266
+ return;
267
+ if (!dryRun) {
268
+ fs.rmSync(targetPath, { recursive: true, force: true });
269
+ }
270
+ actions.push(`${dryRun ? '[dry-run] ' : ''}remove ${targetPath}`);
271
+ }
272
+ function copyDirRecursive(srcDir, dstDir, dryRun, actions) {
273
+ for (const entry of fs.readdirSync(srcDir, { withFileTypes: true })) {
274
+ const srcPath = path.join(srcDir, entry.name);
275
+ const dstPath = path.join(dstDir, entry.name);
276
+ if (entry.isDirectory()) {
277
+ copyDirRecursive(srcPath, dstPath, dryRun, actions);
278
+ }
279
+ else {
280
+ copyFileAction(srcPath, dstPath, dryRun, actions);
281
+ }
282
+ }
283
+ }
284
+ function installSkillsToDir(ctx, skillsRoot, skillNames) {
285
+ for (const name of skillNames) {
286
+ const srcDir = path.join(ctx.packageRoot, 'skills', name);
287
+ const dstDir = path.join(skillsRoot, name);
288
+ const current = new Set();
289
+ for (const rel of listFiles(srcDir)) {
290
+ const dst = path.join(dstDir, rel);
291
+ current.add(dst);
292
+ installManagedFile(ctx, path.join(srcDir, rel), dst);
293
+ }
294
+ // Prune files from older versions that we installed and the user never edited.
295
+ for (const [file, hash] of Object.entries(ctx.manifest.files)) {
296
+ if (!file.startsWith(dstDir + path.sep) || current.has(file))
297
+ continue;
298
+ if (!fs.existsSync(file)) {
299
+ delete ctx.manifest.files[file];
300
+ }
301
+ else if (sha256(file) === hash) {
302
+ removePathAction(file, ctx.dryRun, ctx.actions);
303
+ delete ctx.manifest.files[file];
304
+ }
305
+ }
306
+ }
307
+ }
308
+ function uninstallSkillsFromDir(ctx, skillsRoot) {
309
+ for (const name of SKILL_NAMES) {
310
+ const srcDir = path.join(ctx.packageRoot, 'skills', name);
311
+ const dstDir = path.join(skillsRoot, name);
312
+ for (const rel of listFiles(srcDir)) {
313
+ uninstallManagedFile(ctx, path.join(srcDir, rel), path.join(dstDir, rel));
314
+ }
315
+ for (const file of Object.keys(ctx.manifest.files)) {
316
+ if (file.startsWith(dstDir + path.sep))
317
+ uninstallManagedFile(ctx, null, file);
318
+ }
319
+ removeEmptyDirs(dstDir, ctx.dryRun);
320
+ }
321
+ }
322
+ function applyClaudeOptimize(ctx) {
323
+ const claudeDir = path.join(ctx.homeDir, '.claude');
324
+ const settingsPath = path.join(claudeDir, 'settings.json');
325
+ const backupPath = path.join(claudeDir, 'settings.json.sdlc-kit.bak');
326
+ const preRenameBackupPath = path.join(claudeDir, 'settings.json.agent-sdlc.bak');
327
+ const statuslineSrc = path.join(ctx.packageRoot, 'dist', 'lib', 'statusline.js');
328
+ const statuslineDst = path.join(claudeDir, STATUSLINE_FILE);
329
+ const { dryRun, actions, warnings } = ctx;
330
+ let settings = {};
331
+ if (fs.existsSync(settingsPath)) {
332
+ const raw = fs.readFileSync(settingsPath, 'utf8');
333
+ try {
334
+ settings = JSON.parse(raw);
335
+ }
336
+ catch {
337
+ warnings.push(`Skipped --optimize on ${settingsPath}: existing file is not valid JSON.`);
338
+ return;
339
+ }
340
+ if (!fs.existsSync(backupPath) && !fs.existsSync(preRenameBackupPath)) {
341
+ if (!dryRun) {
342
+ fs.writeFileSync(backupPath, raw, 'utf8');
343
+ }
344
+ actions.push(`${dryRun ? '[dry-run] ' : ''}backup ${backupPath}`);
345
+ }
346
+ }
347
+ // Record only what we add, so --uninstall can revert exactly that and nothing else.
348
+ const rec = ctx.manifest.settings ?? { model: false, env: [], statusLine: false };
349
+ if (settings.model === undefined) {
350
+ settings.model = 'opusplan';
351
+ rec.model = true;
352
+ }
353
+ else if (settings.model !== 'opusplan' && !rec.model) {
354
+ warnings.push(`Kept existing model "${settings.model}" (opusplan recommended; set it manually to opt in).`);
355
+ }
356
+ const env = (settings.env ??= {});
357
+ for (const [key, value] of Object.entries(CLAUDE_OPTIMIZE_ENV)) {
358
+ if (env[key] === undefined) {
359
+ env[key] = value;
360
+ if (!rec.env.includes(key))
361
+ rec.env.push(key);
362
+ }
363
+ }
364
+ installManagedFile(ctx, statuslineSrc, statuslineDst);
365
+ uninstallManagedFile(ctx, null, path.join(claudeDir, PRE_RENAME_STATUSLINE_FILE));
366
+ if (rec.statusLine && settings.statusLine?.command === statuslineCommand(ctx.homeDir, PRE_RENAME_STATUSLINE_FILE)) {
367
+ settings.statusLine.command = statuslineCommand(ctx.homeDir);
368
+ }
369
+ if (!settings.statusLine) {
370
+ settings.statusLine = {
371
+ type: 'command',
372
+ command: statuslineCommand(ctx.homeDir),
373
+ };
374
+ rec.statusLine = true;
375
+ }
376
+ ctx.manifest.settings = rec;
377
+ if (!dryRun) {
378
+ fs.mkdirSync(claudeDir, { recursive: true });
379
+ fs.writeFileSync(settingsPath, `${JSON.stringify(settings, null, 2)}\n`, 'utf8');
380
+ }
381
+ actions.push(`${dryRun ? '[dry-run] ' : ''}optimize ${settingsPath}`);
382
+ }
383
+ /** Undo only the settings keys that --optimize added and the user has not changed since. */
384
+ function revertClaudeOptimize(ctx) {
385
+ const rec = ctx.manifest.settings;
386
+ if (!rec)
387
+ return;
388
+ const settingsPath = path.join(ctx.homeDir, '.claude', 'settings.json');
389
+ if (fs.existsSync(settingsPath)) {
390
+ let settings;
391
+ try {
392
+ settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
393
+ }
394
+ catch {
395
+ ctx.warnings.push(`Could not revert --optimize in ${settingsPath}: not valid JSON.`);
396
+ return;
397
+ }
398
+ if (rec.model && settings.model === 'opusplan')
399
+ delete settings.model;
400
+ for (const key of rec.env) {
401
+ if (settings.env && settings.env[key] === CLAUDE_OPTIMIZE_ENV[key])
402
+ delete settings.env[key];
403
+ }
404
+ if (settings.env && Object.keys(settings.env).length === 0)
405
+ delete settings.env;
406
+ const slCommand = settings.statusLine?.command;
407
+ if (rec.statusLine &&
408
+ (slCommand === statuslineCommand(ctx.homeDir) ||
409
+ slCommand === statuslineCommand(ctx.homeDir, PRE_RENAME_STATUSLINE_FILE) ||
410
+ slCommand === LEGACY_STATUSLINE_COMMAND)) {
411
+ delete settings.statusLine;
412
+ }
413
+ if (!ctx.dryRun) {
414
+ fs.writeFileSync(settingsPath, `${JSON.stringify(settings, null, 2)}\n`, 'utf8');
415
+ }
416
+ ctx.actions.push(`${ctx.dryRun ? '[dry-run] ' : ''}revert optimize ${settingsPath}`);
417
+ for (const suffix of ['sdlc-kit', 'agent-sdlc']) {
418
+ const backup = `${settingsPath}.${suffix}.bak`;
419
+ if (fs.existsSync(backup))
420
+ ctx.warnings.push(`Left ${backup} in place (original pre-optimize copy).`);
421
+ }
422
+ }
423
+ ctx.manifest.settings = null;
424
+ }
425
+ function scaffoldProject(cwd, packageRoot, dryRun, actions, warnings) {
426
+ const projectTemplateDir = path.join(packageRoot, 'templates', 'project');
427
+ const mappings = [
428
+ ['docs/INDEX.md', 'docs/INDEX.md'],
429
+ ['docs/adr/TEMPLATE.md', 'docs/adr/TEMPLATE.md'],
430
+ ['docs/plans/.gitkeep', 'docs/plans/.gitkeep'],
431
+ ['.github/PULL_REQUEST_TEMPLATE.md', '.github/PULL_REQUEST_TEMPLATE.md'],
432
+ ['AGENTS.md', 'AGENTS.md'],
433
+ ['CLAUDE.md', 'CLAUDE.md'],
434
+ ];
435
+ for (const [relSrc, relDst] of mappings) {
436
+ const src = path.join(projectTemplateDir, relSrc);
437
+ const dst = path.join(cwd, relDst);
438
+ if (fs.existsSync(dst)) {
439
+ warnings.push(`Skipped existing ${relDst}`);
440
+ continue;
441
+ }
442
+ copyFileAction(src, dst, dryRun, actions);
443
+ }
444
+ const gitignorePath = path.join(cwd, '.gitignore');
445
+ const existing = fs.existsSync(gitignorePath)
446
+ ? fs.readFileSync(gitignorePath, 'utf8')
447
+ : '';
448
+ const lines = new Set(existing.split(/\r?\n/).map((l) => l.trim()));
449
+ const missing = PROJECT_GITIGNORE_ENTRIES.filter((entry) => !lines.has(entry));
450
+ if (missing.length > 0) {
451
+ const prefix = existing.length > 0 && !existing.endsWith('\n') ? '\n' : '';
452
+ const block = `${prefix}${missing.join('\n')}\n`;
453
+ if (!dryRun) {
454
+ fs.writeFileSync(gitignorePath, existing + block, 'utf8');
455
+ }
456
+ actions.push(`${dryRun ? '[dry-run] ' : ''}update ${gitignorePath} (+${missing.length} entries)`);
457
+ }
458
+ }
459
+ /**
460
+ * Execute install or uninstall across requested targets.
461
+ */
462
+ export function runInstall(options = {}) {
463
+ const homeDir = options.homeDir ?? os.homedir();
464
+ const cwd = options.cwd ?? process.cwd();
465
+ const packageRoot = options.packageRoot ?? DEFAULT_PACKAGE_ROOT;
466
+ const dryRun = Boolean(options.dryRun);
467
+ const uninstall = Boolean(options.uninstall);
468
+ const optimize = Boolean(options.optimize);
469
+ const includeOrchestrator = options.includeOrchestrator ?? true;
470
+ const targets = options.targets && options.targets.length > 0
471
+ ? options.targets
472
+ : detectTargets(homeDir);
473
+ const skillNames = includeOrchestrator ? SKILL_NAMES : ['sdlc-workflow'];
474
+ const ctx = {
475
+ homeDir,
476
+ packageRoot,
477
+ dryRun,
478
+ force: Boolean(options.force),
479
+ actions: [],
480
+ warnings: [],
481
+ manifest: loadManifest(homeDir),
482
+ };
483
+ const { actions, warnings } = ctx;
484
+ const seenSkillDirs = new Set();
485
+ const addSkillsOnce = (dir) => {
486
+ if (seenSkillDirs.has(dir))
487
+ return;
488
+ seenSkillDirs.add(dir);
489
+ if (uninstall) {
490
+ uninstallSkillsFromDir(ctx, dir);
491
+ }
492
+ else {
493
+ installSkillsToDir(ctx, dir, skillNames);
494
+ }
495
+ };
496
+ const syncCommands = (target) => {
497
+ const cmdDir = path.join(homeDir, ...COMMAND_DIRS[target]);
498
+ for (const cmd of COMMAND_FILES) {
499
+ const src = path.join(packageRoot, 'commands', cmd);
500
+ const dst = path.join(cmdDir, cmd);
501
+ if (uninstall) {
502
+ uninstallManagedFile(ctx, src, dst);
503
+ }
504
+ else {
505
+ installManagedFile(ctx, src, dst);
506
+ }
507
+ }
508
+ };
509
+ for (const target of targets) {
510
+ if (target === 'claude') {
511
+ addSkillsOnce(path.join(homeDir, '.claude', 'skills'));
512
+ syncCommands('claude');
513
+ if (uninstall) {
514
+ revertClaudeOptimize(ctx);
515
+ uninstallManagedFile(ctx, path.join(packageRoot, 'dist', 'lib', 'statusline.js'), path.join(homeDir, '.claude', STATUSLINE_FILE));
516
+ uninstallManagedFile(ctx, null, path.join(homeDir, '.claude', PRE_RENAME_STATUSLINE_FILE));
517
+ uninstallManagedFile(ctx, null, path.join(homeDir, '.claude', LEGACY_STATUSLINE_FILE));
518
+ }
519
+ else if (optimize) {
520
+ applyClaudeOptimize(ctx);
521
+ }
522
+ }
523
+ else if (target === 'codex') {
524
+ addSkillsOnce(path.join(homeDir, '.codex', 'skills'));
525
+ addSkillsOnce(path.join(homeDir, '.agents', 'skills'));
526
+ }
527
+ else if (target === 'antigravity') {
528
+ addSkillsOnce(path.join(homeDir, '.agents', 'skills'));
529
+ }
530
+ else if (target === 'cursor') {
531
+ addSkillsOnce(path.join(homeDir, '.cursor', 'skills'));
532
+ syncCommands('cursor');
533
+ }
534
+ else if (target === 'opencode') {
535
+ addSkillsOnce(path.join(homeDir, '.config', 'opencode', 'skills'));
536
+ addSkillsOnce(path.join(homeDir, '.agents', 'skills'));
537
+ syncCommands('opencode');
538
+ }
539
+ else if (target === 'local') {
540
+ const src = path.join(packageRoot, 'templates', 'local', 'LOCAL_PROMPT.md');
541
+ const dst = path.join(homeDir, LOCAL_PROMPT_NAME);
542
+ const preRenameDst = path.join(homeDir, PRE_RENAME_LOCAL_PROMPT_NAME);
543
+ if (uninstall) {
544
+ uninstallManagedFile(ctx, src, dst);
545
+ }
546
+ else {
547
+ installManagedFile(ctx, src, dst);
548
+ }
549
+ uninstallManagedFile(ctx, src, preRenameDst);
550
+ }
551
+ else if (target === 'project') {
552
+ if (uninstall) {
553
+ warnings.push('--uninstall ignores --project to avoid deleting repository files.');
554
+ }
555
+ else {
556
+ scaffoldProject(cwd, packageRoot, dryRun, actions, warnings);
557
+ }
558
+ }
559
+ }
560
+ saveManifest(homeDir, ctx.manifest, readPackageVersion(packageRoot), dryRun);
561
+ return { targets, actions, warnings };
562
+ }
563
+ /**
564
+ * Run a read-only health and token-efficiency audit across global and current-project configs.
565
+ */
566
+ export function runDoctor(options = {}) {
567
+ const homeDir = options.homeDir ?? os.homedir();
568
+ const cwd = options.cwd ?? process.cwd();
569
+ const checks = [];
570
+ const add = (level, area, message) => {
571
+ checks.push({ level, area, message });
572
+ };
573
+ const locations = [
574
+ ['claude', path.join(homeDir, '.claude/skills/sdlc-workflow/SKILL.md')],
575
+ ['codex', path.join(homeDir, '.codex/skills/sdlc-workflow/SKILL.md')],
576
+ ['antigravity', path.join(homeDir, '.agents/skills/sdlc-workflow/SKILL.md')],
577
+ ['cursor', path.join(homeDir, '.cursor/skills/sdlc-workflow/SKILL.md')],
578
+ ['opencode', path.join(homeDir, '.config/opencode/skills/sdlc-workflow/SKILL.md')],
579
+ ['local', path.join(homeDir, LOCAL_PROMPT_NAME)],
580
+ ['local', path.join(homeDir, PRE_RENAME_LOCAL_PROMPT_NAME)],
581
+ ];
582
+ const installed = [...new Set(locations.filter(([, p]) => fs.existsSync(p)).map(([name]) => name))];
583
+ if (installed.length > 0) {
584
+ add('OK', 'installs', `Installed for: ${installed.join(', ')}`);
585
+ }
586
+ else {
587
+ add('WARN', 'installs', 'No sdlc-kit targets installed yet. Run npx sdlc-kit.');
588
+ }
589
+ const manifest = loadManifest(homeDir);
590
+ if (manifest.version) {
591
+ const current = readPackageVersion(options.packageRoot ?? DEFAULT_PACKAGE_ROOT);
592
+ if (manifest.version === current) {
593
+ add('OK', 'version', `Installed files match package version ${current}`);
594
+ }
595
+ else {
596
+ add('WARN', 'version', `Installed v${manifest.version} but package is v${current}. Re-run the installer to upgrade.`);
597
+ }
598
+ }
599
+ const claudeDir = path.join(homeDir, '.claude');
600
+ const pluginsFile = path.join(claudeDir, 'plugins', 'installed_plugins.json');
601
+ if (fs.existsSync(pluginsFile)) {
602
+ let pluginInstalled = false;
603
+ try {
604
+ pluginInstalled = Object.keys(JSON.parse(fs.readFileSync(pluginsFile, 'utf8')).plugins ?? {}).some((k) => k.startsWith('sdlc-kit@') || k.startsWith('agent-sdlc@'));
605
+ }
606
+ catch {
607
+ // unreadable plugin registry: skip the duplicate check
608
+ }
609
+ if (pluginInstalled && fs.existsSync(path.join(claudeDir, 'skills', 'sdlc-workflow'))) {
610
+ add('WARN', 'duplicate-install', 'sdlc-kit is installed as a Claude plugin AND via npx; skills load twice. Uninstall one (npx sdlc-kit --claude --uninstall).');
611
+ }
612
+ }
613
+ if (fs.existsSync(claudeDir)) {
614
+ const cmdCount = COMMAND_FILES.filter((f) => fs.existsSync(path.join(claudeDir, 'commands', f))).length;
615
+ add(cmdCount === COMMAND_FILES.length ? 'OK' : 'WARN', 'claude-commands', `${cmdCount}/${COMMAND_FILES.length} slash commands installed in ~/.claude/commands/`);
616
+ const skillsDir = path.join(claudeDir, 'skills');
617
+ if (fs.existsSync(skillsDir)) {
618
+ const skillEntries = fs.readdirSync(skillsDir).filter((e) => e !== 'synced');
619
+ if (skillEntries.length > 20) {
620
+ add('WARN', 'claude-skills', `${skillEntries.length} global skills in ~/.claude/skills/ (>20 increases baseline token overhead)`);
621
+ }
622
+ else {
623
+ add('OK', 'claude-skills', `${skillEntries.length} global skills in ~/.claude/skills/ (lean)`);
624
+ }
625
+ }
626
+ const settingsPath = path.join(claudeDir, 'settings.json');
627
+ if (fs.existsSync(settingsPath)) {
628
+ try {
629
+ const s = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
630
+ const missing = [];
631
+ if (s.model !== 'opusplan')
632
+ missing.push('model=opusplan');
633
+ const env = s.env || {};
634
+ for (const k of [
635
+ 'CLAUDE_CODE_DISABLE_1M_CONTEXT',
636
+ 'ENABLE_TOOL_SEARCH',
637
+ 'CLAUDE_AUTOCOMPACT_PCT_OVERRIDE',
638
+ 'BASH_MAX_OUTPUT_LENGTH',
639
+ 'MAX_MCP_OUTPUT_TOKENS',
640
+ ]) {
641
+ if (!env[k])
642
+ missing.push(k);
643
+ }
644
+ if (missing.length > 0) {
645
+ add('WARN', 'claude-settings', `Missing token guardrails (${missing.join(', ')}). Run --claude --optimize.`);
646
+ }
647
+ else {
648
+ add('OK', 'claude-settings', 'All token guardrails active (opusplan, 200k cap, tool search, auto-compact)');
649
+ }
650
+ }
651
+ catch {
652
+ add('WARN', 'claude-settings', '~/.claude/settings.json is not valid JSON');
653
+ }
654
+ }
655
+ else {
656
+ add('INFO', 'claude-settings', 'No ~/.claude/settings.json found');
657
+ }
658
+ }
659
+ const rulesDir = path.join(cwd, '.claude', 'rules');
660
+ if (fs.existsSync(rulesDir)) {
661
+ let uncondCount = 0;
662
+ let uncondBytes = 0;
663
+ for (const file of fs.readdirSync(rulesDir)) {
664
+ if (!file.endsWith('.md'))
665
+ continue;
666
+ const content = fs.readFileSync(path.join(rulesDir, file), 'utf8');
667
+ const m = content.match(/^---\r?\n([\s\S]*?)\r?\n---/);
668
+ if (!m || !m[1]?.includes('paths:')) {
669
+ uncondCount += 1;
670
+ uncondBytes += Buffer.byteLength(content, 'utf8');
671
+ }
672
+ }
673
+ if (uncondBytes > 16384) {
674
+ add('WARN', 'project-rules', `${uncondCount} unconditional rule(s) in .claude/rules/ (~${Math.round(uncondBytes / 4)} tokens/turn). Add YAML paths: scoping.`);
675
+ }
676
+ else {
677
+ add('OK', 'project-rules', `.claude/rules/ baseline is lean (${uncondCount} unconditional, ~${Math.round(uncondBytes / 4)} tokens)`);
678
+ }
679
+ }
680
+ if (fs.existsSync(path.join(cwd, '.git'))) {
681
+ const giPath = path.join(cwd, '.gitignore');
682
+ const gi = fs.existsSync(giPath) ? fs.readFileSync(giPath, 'utf8') : '';
683
+ if (!gi.includes('.claude/HANDOFF.md')) {
684
+ add('WARN', 'project-gitignore', '.gitignore is missing .claude/HANDOFF.md (run --project to add safely)');
685
+ }
686
+ else {
687
+ add('OK', 'project-gitignore', '.gitignore excludes .claude/HANDOFF.md');
688
+ }
689
+ }
690
+ const okCount = checks.filter((c) => c.level === 'OK').length;
691
+ const warnCount = checks.filter((c) => c.level === 'WARN').length;
692
+ return { checks, okCount, warnCount };
693
+ }