@northbridge-security/secureai 0.1.13

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 (50) hide show
  1. package/.claude/README.md +122 -0
  2. package/.claude/commands/architect/clean.md +978 -0
  3. package/.claude/commands/architect/kiss.md +762 -0
  4. package/.claude/commands/architect/review.md +704 -0
  5. package/.claude/commands/catchup.md +90 -0
  6. package/.claude/commands/code.md +115 -0
  7. package/.claude/commands/commit.md +1218 -0
  8. package/.claude/commands/cover.md +1298 -0
  9. package/.claude/commands/fmea.md +275 -0
  10. package/.claude/commands/kaizen.md +312 -0
  11. package/.claude/commands/pr.md +503 -0
  12. package/.claude/commands/todo.md +99 -0
  13. package/.claude/commands/worktree.md +738 -0
  14. package/.claude/commands/wrapup.md +103 -0
  15. package/LICENSE +183 -0
  16. package/README.md +108 -0
  17. package/dist/cli.js +75634 -0
  18. package/docs/agents/devops-reviewer.md +889 -0
  19. package/docs/agents/kiss-simplifier.md +1088 -0
  20. package/docs/agents/typescript.md +8 -0
  21. package/docs/guides/README.md +109 -0
  22. package/docs/guides/agents.clean.arch.md +244 -0
  23. package/docs/guides/agents.clean.arch.ts.md +1314 -0
  24. package/docs/guides/agents.gotask.md +1037 -0
  25. package/docs/guides/agents.markdown.md +1209 -0
  26. package/docs/guides/agents.onepassword.md +285 -0
  27. package/docs/guides/agents.sonar.md +857 -0
  28. package/docs/guides/agents.tdd.md +838 -0
  29. package/docs/guides/agents.tdd.ts.md +1062 -0
  30. package/docs/guides/agents.typesript.md +1389 -0
  31. package/docs/guides/github-mcp.md +1075 -0
  32. package/package.json +130 -0
  33. package/packages/secureai-cli/src/cli.ts +21 -0
  34. package/tasks/README.md +880 -0
  35. package/tasks/aws.yml +64 -0
  36. package/tasks/bash.yml +118 -0
  37. package/tasks/bun.yml +738 -0
  38. package/tasks/claude.yml +183 -0
  39. package/tasks/docker.yml +420 -0
  40. package/tasks/docs.yml +127 -0
  41. package/tasks/git.yml +1336 -0
  42. package/tasks/gotask.yml +132 -0
  43. package/tasks/json.yml +77 -0
  44. package/tasks/markdown.yml +95 -0
  45. package/tasks/onepassword.yml +350 -0
  46. package/tasks/security.yml +102 -0
  47. package/tasks/sonar.yml +437 -0
  48. package/tasks/template.yml +74 -0
  49. package/tasks/vscode.yml +103 -0
  50. package/tasks/yaml.yml +121 -0
@@ -0,0 +1,1088 @@
1
+ ---
2
+ name: kiss-simplifier
3
+ description: Analyzes codebases for over-engineering and KISS principle violations, proposing concrete simplifications in isolated git worktrees
4
+ tools: Read, Grep, Glob, Bash, LS, Edit
5
+ model: sonnet
6
+ ---
7
+
8
+ # KISS Simplifier Agent
9
+
10
+ ## Purpose
11
+
12
+ The KISS Simplifier Agent is a specialized subagent designed to analyze codebases for over-engineering, unnecessary complexity, and violations of the KISS (Keep It Simple, Stupid) principle. It identifies opportunities for simplification and generates concrete refactoring proposals in isolated git worktrees.
13
+
14
+ ## Target Audience
15
+
16
+ This agent is invoked by Claude Code via the `/kiss` slash command to perform deep code analysis and propose simplifications without modifying the main working tree.
17
+
18
+ ## Core Responsibilities
19
+
20
+ 1. **Analyze code for over-engineering** using multiple static analysis tools
21
+ 2. **Identify KISS principle violations** based on project-specific guidelines
22
+ 3. **Create isolated git worktree** for proposed changes
23
+ 4. **Apply concrete simplifications** in the worktree
24
+ 5. **Generate structured reports** with metrics and recommendations
25
+ 6. **Report findings back to Claude Code** for human review
26
+
27
+ ## Agent Capabilities
28
+
29
+ ### Static Analysis Tools
30
+
31
+ The agent leverages industry-standard tools for comprehensive analysis:
32
+
33
+ #### 1. Dead Code Detection (knip)
34
+
35
+ **Purpose:** Identify unused code that can be safely removed
36
+
37
+ **Detects:**
38
+
39
+ - Unused exports (functions, types, classes, interfaces)
40
+ - Unused files and modules
41
+ - Unused dependencies and devDependencies
42
+ - Mutually recursive dead code
43
+ - Unused class members and enum members
44
+
45
+ **Configuration:**
46
+
47
+ ```json
48
+ // knip.json
49
+ {
50
+ "entry": ["src/main.ts", "src/cli.ts"],
51
+ "project": ["src/**/*.ts"],
52
+ "ignore": ["src/**/*.test.ts", "**/*.d.ts"],
53
+ "ignoreDependencies": ["@types/*"]
54
+ }
55
+ ```
56
+
57
+ **Usage:**
58
+
59
+ ```bash
60
+ knip --reporter json > .reports/dead-code.json
61
+ ```
62
+
63
+ #### 2. Complexity Analysis (FTA - Fast TypeScript Analyzer)
64
+
65
+ **Purpose:** Measure code complexity and maintainability
66
+
67
+ **Metrics:**
68
+
69
+ - Cyclomatic complexity (number of decision paths)
70
+ - Halstead metrics (difficulty, effort, predicted bugs)
71
+ - FTA score (overall maintainability, 0-100)
72
+ - Line count analysis
73
+
74
+ **Thresholds:**
75
+
76
+ - Cyclomatic complexity: >15 (flag as complex)
77
+ - FTA score: <60 (flag as needs improvement)
78
+ - Function length: >50 lines (flag as too long)
79
+
80
+ **Usage:**
81
+
82
+ ```bash
83
+ fta src/**/*.ts --json > .reports/complexity.json
84
+ ```
85
+
86
+ #### 3. Code Smell Detection (eslint-plugin-sonarjs)
87
+
88
+ **Purpose:** Detect code smells and anti-patterns
89
+
90
+ **Rules:**
91
+
92
+ - `cognitive-complexity`: Mental effort to understand code
93
+ - `no-duplicate-string`: Magic strings (DRY violation)
94
+ - `no-identical-functions`: Copy-pasted code
95
+ - `no-collapsible-if`: Unnecessary nesting
96
+ - `no-inverted-boolean-check`: Confusing boolean logic
97
+ - `prefer-immediate-return`: Unnecessary variables
98
+ - `no-small-switch`: Switch that should be if/else
99
+
100
+ **Usage:**
101
+
102
+ ```bash
103
+ eslint src --ext .ts --format json > .reports/code-smells.json
104
+ ```
105
+
106
+ #### 4. Architecture Analysis (dependency-cruiser)
107
+
108
+ **Purpose:** Detect architectural violations
109
+
110
+ **Detects:**
111
+
112
+ - Circular dependencies
113
+ - Layer violations (e.g., UI importing from database layer)
114
+ - Orphaned modules (isolated files)
115
+ - Improper coupling
116
+
117
+ **Configuration:**
118
+
119
+ ```javascript
120
+ // .dependency-cruiser.js
121
+ module.exports = {
122
+ forbidden: [
123
+ {
124
+ name: "no-circular",
125
+ severity: "error",
126
+ from: {},
127
+ to: { circular: true },
128
+ },
129
+ {
130
+ name: "no-orphans",
131
+ severity: "warn",
132
+ from: { orphan: true },
133
+ to: {},
134
+ },
135
+ ],
136
+ };
137
+ ```
138
+
139
+ **Usage:**
140
+
141
+ ```bash
142
+ depcruise --validate .dependency-cruiser.js src
143
+ ```
144
+
145
+ ### AI-Pattern Detection (Heuristic)
146
+
147
+ The agent uses custom heuristics to detect AI-generated over-engineering:
148
+
149
+ **Indicators:**
150
+
151
+ 1. **Verbose naming** - Identifiers >30 characters with >4 words
152
+ 2. **Generic comments** - Comments like "Process data", "Execute action"
153
+ 3. **Perfect formatting consistency** - No human inconsistencies
154
+ 4. **Repetitive patterns** - Copy-paste code with only parameter names changed
155
+ 5. **Unnecessary abstraction** - Low complexity but high line count
156
+
157
+ **Implementation:**
158
+
159
+ ```typescript
160
+ interface AIPatternScore {
161
+ file: string;
162
+ score: number; // 0-100, higher = more likely AI-generated
163
+ indicators: string[];
164
+ }
165
+
166
+ function detectAIPatterns(filePath: string): AIPatternScore {
167
+ const ast = parseTypeScript(filePath);
168
+ let score = 0;
169
+ const indicators: string[] = [];
170
+
171
+ // Verbose naming
172
+ const avgNameLength = calculateAvgNameLength(ast);
173
+ if (avgNameLength > 25) {
174
+ score += 20;
175
+ indicators.push("Verbose naming patterns");
176
+ }
177
+
178
+ // Generic comments
179
+ const genericCommentRatio = analyzeComments(ast);
180
+ if (genericCommentRatio > 0.5) {
181
+ score += 15;
182
+ indicators.push("Generic comments");
183
+ }
184
+
185
+ // Repetitive code
186
+ const repetitivePatterns = detectRepetitiveCode(ast);
187
+ if (repetitivePatterns.length > 3) {
188
+ score += 20;
189
+ indicators.push(`${repetitivePatterns.length} repetitive patterns`);
190
+ }
191
+
192
+ // Over-abstraction
193
+ const complexity = calculateComplexity(ast);
194
+ const lineCount = countLines(filePath);
195
+ if (complexity < 5 && lineCount > 100) {
196
+ score += 15;
197
+ indicators.push("Over-abstraction (low complexity, high LOC)");
198
+ }
199
+
200
+ return { file: filePath, score, indicators };
201
+ }
202
+ ```
203
+
204
+ ### Legacy Pattern Detection
205
+
206
+ **Detects outdated patterns:**
207
+
208
+ - `var` usage (should be `const`/`let`)
209
+ - Callback hell (nested callbacks >3 levels)
210
+ - God objects (files >500 LOC or >30 methods)
211
+ - No error handling (missing try-catch in async code)
212
+ - Promises without await (should use async/await)
213
+
214
+ ## Operational Workflow
215
+
216
+ ### Phase 1: Environment Setup
217
+
218
+ **1.1 Read KISS Principles**
219
+
220
+ ```bash
221
+ # Load project-specific KISS principles
222
+ if [ ! -f detecting-overengineered-code.local.md ]; then
223
+ ERROR: "KISS principles file not found"
224
+ EXIT: 1
225
+ fi
226
+
227
+ PRINCIPLES=$(cat detecting-overengineered-code.local.md)
228
+ ```
229
+
230
+ **1.2 Install Analysis Tools**
231
+
232
+ ```bash
233
+ # Check for required tools
234
+ MISSING_TOOLS=()
235
+
236
+ command -v knip >/dev/null || MISSING_TOOLS+=("knip")
237
+ command -v fta >/dev/null || MISSING_TOOLS+=("fta-cli")
238
+ command -v depcruise >/dev/null || MISSING_TOOLS+=("dependency-cruiser")
239
+ command -v eslint >/dev/null || MISSING_TOOLS+=("eslint")
240
+
241
+ if [ ${#MISSING_TOOLS[@]} -gt 0 ]; then
242
+ echo "Installing missing tools: ${MISSING_TOOLS[*]}"
243
+ bun add -D "${MISSING_TOOLS[@]}"
244
+ fi
245
+ ```
246
+
247
+ **1.3 Create Reports Directory**
248
+
249
+ ```bash
250
+ mkdir -p .reports
251
+ ```
252
+
253
+ ### Phase 2: Static Analysis Execution
254
+
255
+ **2.1 Run All Analysis Tools in Parallel**
256
+
257
+ ```bash
258
+ # Run tools in parallel for speed
259
+ {
260
+ knip --reporter json > .reports/dead-code.json 2>&1 &
261
+ PID_KNIP=$!
262
+
263
+ fta src/**/*.ts --json > .reports/complexity.json 2>&1 &
264
+ PID_FTA=$!
265
+
266
+ eslint src --ext .ts,.tsx,.js,.jsx --format json > .reports/code-smells.json 2>&1 &
267
+ PID_ESLINT=$!
268
+
269
+ depcruise --validate .dependency-cruiser.js src --output-type json > .reports/architecture.json 2>&1 &
270
+ PID_DEPCRUISE=$!
271
+
272
+ # Wait for all to complete
273
+ wait $PID_KNIP $PID_FTA $PID_ESLINT $PID_DEPCRUISE
274
+ }
275
+ ```
276
+
277
+ **2.2 Parse Analysis Results**
278
+
279
+ ```typescript
280
+ interface AnalysisResults {
281
+ deadCode: {
282
+ unusedExports: string[];
283
+ unusedFiles: string[];
284
+ unusedDependencies: string[];
285
+ };
286
+ complexity: {
287
+ file: string;
288
+ cyclomatic: number;
289
+ ftaScore: number;
290
+ assessment: string;
291
+ }[];
292
+ codeSmells: {
293
+ file: string;
294
+ rule: string;
295
+ severity: string;
296
+ message: string;
297
+ line: number;
298
+ }[];
299
+ architecture: {
300
+ type: string; // "circular" | "orphan" | "layer-violation"
301
+ from: string;
302
+ to: string;
303
+ severity: string;
304
+ }[];
305
+ }
306
+
307
+ function parseAnalysisResults(): AnalysisResults {
308
+ const deadCode = JSON.parse(readFileSync(".reports/dead-code.json"));
309
+ const complexity = JSON.parse(readFileSync(".reports/complexity.json"));
310
+ const codeSmells = JSON.parse(readFileSync(".reports/code-smells.json"));
311
+ const architecture = JSON.parse(readFileSync(".reports/architecture.json"));
312
+
313
+ return {
314
+ deadCode: {
315
+ unusedExports: deadCode.exports || [],
316
+ unusedFiles: deadCode.files || [],
317
+ unusedDependencies: deadCode.dependencies || [],
318
+ },
319
+ complexity: complexity.filter((f) => f.fta_score < 60 || f.cyclo > 15),
320
+ codeSmells: flattenESLintResults(codeSmells),
321
+ architecture: architecture.violations || [],
322
+ };
323
+ }
324
+ ```
325
+
326
+ **2.3 Run AI Pattern Detection**
327
+
328
+ ```typescript
329
+ function analyzeAIPatterns(scope: string[]): AIPatternScore[] {
330
+ return scope.map(detectAIPatterns).filter((result) => result.score > 60);
331
+ }
332
+ ```
333
+
334
+ **2.4 Run Legacy Pattern Detection**
335
+
336
+ ```bash
337
+ # Detect 'var' usage
338
+ grep -rn "var " src --include="*.ts" --include="*.js" > .reports/legacy-var.txt
339
+
340
+ # Detect god objects (files >500 LOC)
341
+ fta src/**/*.ts --json | jq '.[] | select(.line_count > 500)' > .reports/god-objects.json
342
+ ```
343
+
344
+ ### Phase 3: Issue Categorization and Prioritization
345
+
346
+ **3.1 Categorize Issues**
347
+
348
+ ```typescript
349
+ type IssueCategory =
350
+ | "dead-code"
351
+ | "complexity"
352
+ | "code-smell"
353
+ | "architecture"
354
+ | "ai-pattern"
355
+ | "legacy";
356
+
357
+ type IssueSeverity = "critical" | "high" | "medium" | "low";
358
+
359
+ interface Issue {
360
+ id: string;
361
+ category: IssueCategory;
362
+ severity: IssueSeverity;
363
+ file: string;
364
+ line?: number;
365
+ title: string;
366
+ description: string;
367
+ currentCode: string;
368
+ proposedCode?: string;
369
+ metrics?: {
370
+ before: number;
371
+ after: number;
372
+ improvement: string;
373
+ };
374
+ }
375
+
376
+ function categorizeIssues(results: AnalysisResults): Issue[] {
377
+ const issues: Issue[] = [];
378
+
379
+ // Dead code issues
380
+ results.deadCode.unusedExports.forEach((exp, idx) => {
381
+ issues.push({
382
+ id: `dead-${idx}`,
383
+ category: "dead-code",
384
+ severity: "high",
385
+ file: exp.file,
386
+ line: exp.line,
387
+ title: `Unused export: ${exp.name}`,
388
+ description: "This export is not used anywhere and can be safely removed.",
389
+ currentCode: exp.code,
390
+ });
391
+ });
392
+
393
+ // Complexity issues
394
+ results.complexity.forEach((comp, idx) => {
395
+ const severity: IssueSeverity =
396
+ comp.ftaScore < 40
397
+ ? "critical"
398
+ : comp.ftaScore < 60
399
+ ? "high"
400
+ : comp.cyclo > 20
401
+ ? "high"
402
+ : "medium";
403
+
404
+ issues.push({
405
+ id: `complex-${idx}`,
406
+ category: "complexity",
407
+ severity,
408
+ file: comp.file,
409
+ title: `High complexity in ${comp.file}`,
410
+ description: `FTA score: ${comp.ftaScore}, Cyclomatic: ${comp.cyclomatic}. Consider refactoring.`,
411
+ currentCode: readFunction(comp.file, comp.line),
412
+ });
413
+ });
414
+
415
+ // Architecture issues
416
+ results.architecture.forEach((arch, idx) => {
417
+ issues.push({
418
+ id: `arch-${idx}`,
419
+ category: "architecture",
420
+ severity: arch.type === "circular" ? "critical" : "high",
421
+ file: arch.from,
422
+ title: `${arch.type}: ${arch.from} → ${arch.to}`,
423
+ description: arch.message,
424
+ currentCode: arch.code,
425
+ });
426
+ });
427
+
428
+ return issues;
429
+ }
430
+ ```
431
+
432
+ **3.2 Prioritize Issues**
433
+
434
+ ```typescript
435
+ function prioritizeIssues(issues: Issue[]): Issue[] {
436
+ // Sort by severity, then by impact
437
+ return issues.sort((a, b) => {
438
+ const severityOrder = { critical: 0, high: 1, medium: 2, low: 3 };
439
+ const severityDiff = severityOrder[a.severity] - severityOrder[b.severity];
440
+
441
+ if (severityDiff !== 0) return severityDiff;
442
+
443
+ // Within same severity, prioritize by category impact
444
+ const categoryOrder = {
445
+ architecture: 0, // Most impactful
446
+ complexity: 1,
447
+ "code-smell": 2,
448
+ "ai-pattern": 3,
449
+ "dead-code": 4,
450
+ legacy: 5,
451
+ };
452
+
453
+ return categoryOrder[a.category] - categoryOrder[b.category];
454
+ });
455
+ }
456
+ ```
457
+
458
+ ### Phase 4: Worktree Creation and Simplification
459
+
460
+ **4.1 Create Git Worktree**
461
+
462
+ ```bash
463
+ # Generate timestamp for unique worktree name
464
+ TIMESTAMP=$(date +%Y%m%d-%H%M%S)
465
+ WORKTREE_PATH=".worktree/kiss-${TIMESTAMP}"
466
+
467
+ # Create worktree from current HEAD
468
+ git worktree add "$WORKTREE_PATH" HEAD
469
+
470
+ if [ $? -ne 0 ]; then
471
+ ERROR: "Failed to create git worktree"
472
+ EXIT: 3
473
+ fi
474
+
475
+ echo "Created worktree: $WORKTREE_PATH"
476
+ ```
477
+
478
+ **4.2 Apply Simplifications in Worktree**
479
+
480
+ ```typescript
481
+ function applySimplifications(issues: Issue[], worktreePath: string): void {
482
+ // Change to worktree directory
483
+ process.chdir(worktreePath);
484
+
485
+ for (const issue of issues) {
486
+ try {
487
+ switch (issue.category) {
488
+ case "dead-code":
489
+ removeDeadCode(issue);
490
+ break;
491
+
492
+ case "complexity":
493
+ simplifyComplexFunction(issue);
494
+ break;
495
+
496
+ case "code-smell":
497
+ fixCodeSmell(issue);
498
+ break;
499
+
500
+ case "architecture":
501
+ refactorArchitecture(issue);
502
+ break;
503
+
504
+ case "ai-pattern":
505
+ simplifyAIPattern(issue);
506
+ break;
507
+
508
+ case "legacy":
509
+ modernizeLegacyCode(issue);
510
+ break;
511
+ }
512
+
513
+ issue.status = "applied";
514
+ } catch (error) {
515
+ issue.status = "failed";
516
+ issue.error = error.message;
517
+ }
518
+ }
519
+
520
+ // Return to original directory
521
+ process.chdir("../..");
522
+ }
523
+ ```
524
+
525
+ **4.3 Simplification Strategies**
526
+
527
+ **Dead Code Removal:**
528
+
529
+ ```typescript
530
+ function removeDeadCode(issue: Issue): void {
531
+ const filePath = issue.file;
532
+ const code = readFileSync(filePath, "utf-8");
533
+ const ast = parseTypeScript(code);
534
+
535
+ // Remove unused export
536
+ const newAst = removeNode(ast, issue.line);
537
+ const newCode = generateCode(newAst);
538
+
539
+ writeFileSync(filePath, newCode);
540
+ }
541
+ ```
542
+
543
+ **Complexity Reduction:**
544
+
545
+ ```typescript
546
+ function simplifyComplexFunction(issue: Issue): void {
547
+ // Extract nested logic into separate functions
548
+ // Replace nested if/else with early returns
549
+ // Use guard clauses to reduce nesting
550
+ // Extract magic numbers to constants
551
+
552
+ const simplified = refactorFunction(issue.currentCode, {
553
+ maxNesting: 2,
554
+ maxCyclomaticComplexity: 10,
555
+ extractMagicNumbers: true,
556
+ useEarlyReturns: true,
557
+ });
558
+
559
+ replaceInFile(issue.file, issue.currentCode, simplified);
560
+ }
561
+ ```
562
+
563
+ **Code Smell Fixes:**
564
+
565
+ ```typescript
566
+ function fixCodeSmell(issue: Issue): void {
567
+ switch (issue.rule) {
568
+ case "no-duplicate-string":
569
+ extractToConstant(issue);
570
+ break;
571
+
572
+ case "no-identical-functions":
573
+ extractToSharedFunction(issue);
574
+ break;
575
+
576
+ case "no-collapsible-if":
577
+ mergeNestedIfs(issue);
578
+ break;
579
+
580
+ case "cognitive-complexity":
581
+ simplifyLogic(issue);
582
+ break;
583
+ }
584
+ }
585
+ ```
586
+
587
+ **Architecture Refactoring:**
588
+
589
+ ```typescript
590
+ function refactorArchitecture(issue: Issue): void {
591
+ if (issue.type === "circular") {
592
+ // Break circular dependency by:
593
+ // 1. Extract common interface to shared module
594
+ // 2. Use dependency injection
595
+ // 3. Introduce mediator pattern
596
+ breakCircularDependency(issue);
597
+ } else if (issue.type === "layer-violation") {
598
+ // Fix layer violation by:
599
+ // 1. Move code to appropriate layer
600
+ // 2. Use proper abstraction
601
+ fixLayerViolation(issue);
602
+ }
603
+ }
604
+ ```
605
+
606
+ **AI Pattern Simplification:**
607
+
608
+ ```typescript
609
+ function simplifyAIPattern(issue: Issue): void {
610
+ // Shorten verbose names
611
+ if (issue.indicator === "verbose-naming") {
612
+ const newName = suggestShorterName(issue.name);
613
+ renameIdentifier(issue.file, issue.name, newName);
614
+ }
615
+
616
+ // Improve generic comments
617
+ if (issue.indicator === "generic-comments") {
618
+ removeOrImproveComment(issue.file, issue.line);
619
+ }
620
+
621
+ // Extract repetitive code
622
+ if (issue.indicator === "repetitive-patterns") {
623
+ extractToReusableFunction(issue.file, issue.patterns);
624
+ }
625
+ }
626
+ ```
627
+
628
+ **Legacy Modernization:**
629
+
630
+ ```typescript
631
+ function modernizeLegacyCode(issue: Issue): void {
632
+ // var → const/let
633
+ if (issue.pattern === "var-usage") {
634
+ replaceVarWithConstLet(issue.file);
635
+ }
636
+
637
+ // Callbacks → async/await
638
+ if (issue.pattern === "callback-hell") {
639
+ convertToAsyncAwait(issue.file, issue.line);
640
+ }
641
+
642
+ // Split god objects
643
+ if (issue.pattern === "god-object") {
644
+ splitIntoModules(issue.file);
645
+ }
646
+ }
647
+ ```
648
+
649
+ **4.4 Validate Changes**
650
+
651
+ ```bash
652
+ # Ensure TypeScript compiles
653
+ cd "$WORKTREE_PATH"
654
+ tsc --noEmit
655
+
656
+ if [ $? -ne 0 ]; then
657
+ echo "⚠️ TypeScript errors detected in worktree. Some simplifications may need review."
658
+ fi
659
+
660
+ # Run tests to ensure functionality preserved
661
+ bun test
662
+
663
+ if [ $? -ne 0 ]; then
664
+ echo "⚠️ Test failures detected. Functionality may have changed."
665
+ fi
666
+
667
+ cd ../..
668
+ ```
669
+
670
+ ### Phase 5: Generate Report
671
+
672
+ **5.1 Calculate Metrics**
673
+
674
+ ```typescript
675
+ interface Metrics {
676
+ issuesFound: number;
677
+ issuesBySeverity: Record<IssueSeverity, number>;
678
+ issuesByCategory: Record<IssueCategory, number>;
679
+ filesModified: number;
680
+ linesAdded: number;
681
+ linesRemoved: number;
682
+ netChange: number;
683
+ complexityReduction: {
684
+ before: number;
685
+ after: number;
686
+ improvement: string;
687
+ };
688
+ }
689
+
690
+ function calculateMetrics(issues: Issue[], worktreePath: string): Metrics {
691
+ // Count issues by severity and category
692
+ const issuesBySeverity = countBy(issues, "severity");
693
+ const issuesByCategory = countBy(issues, "category");
694
+
695
+ // Get diff stats from worktree
696
+ const diffStats = execSync(`git -C ${worktreePath} diff --shortstat HEAD`, { encoding: "utf-8" });
697
+
698
+ const [filesChanged, insertions, deletions] = parseDiffStats(diffStats);
699
+
700
+ // Calculate complexity improvement
701
+ const complexityBefore = issues
702
+ .filter((i) => i.category === "complexity")
703
+ .reduce((sum, i) => sum + (i.metrics?.before || 0), 0);
704
+
705
+ const complexityAfter = issues
706
+ .filter((i) => i.category === "complexity")
707
+ .reduce((sum, i) => sum + (i.metrics?.after || 0), 0);
708
+
709
+ return {
710
+ issuesFound: issues.length,
711
+ issuesBySeverity,
712
+ issuesByCategory,
713
+ filesModified: filesChanged,
714
+ linesAdded: insertions,
715
+ linesRemoved: deletions,
716
+ netChange: insertions - deletions,
717
+ complexityReduction: {
718
+ before: complexityBefore,
719
+ after: complexityAfter,
720
+ improvement: `${(((complexityBefore - complexityAfter) / complexityBefore) * 100).toFixed(1)}%`,
721
+ },
722
+ };
723
+ }
724
+ ```
725
+
726
+ **5.2 Generate Structured Report**
727
+
728
+ ```typescript
729
+ interface KISSReport {
730
+ metadata: {
731
+ timestamp: string;
732
+ scope: string;
733
+ mode: string;
734
+ worktree: string;
735
+ agent: string;
736
+ };
737
+ summary: {
738
+ totalIssues: number;
739
+ critical: number;
740
+ high: number;
741
+ medium: number;
742
+ low: number;
743
+ potentialImpact: string;
744
+ };
745
+ issues: Issue[];
746
+ metrics: Metrics;
747
+ recommendations: string[];
748
+ worktreeChanges: {
749
+ location: string;
750
+ filesModified: number;
751
+ diffSummary: string;
752
+ };
753
+ }
754
+
755
+ function generateReport(issues: Issue[], metrics: Metrics, worktreePath: string): KISSReport {
756
+ return {
757
+ metadata: {
758
+ timestamp: new Date().toISOString(),
759
+ scope: process.env.SCOPE || "all",
760
+ mode: process.env.MODE || "full",
761
+ worktree: worktreePath,
762
+ agent: "kiss-simplifier",
763
+ },
764
+ summary: {
765
+ totalIssues: issues.length,
766
+ critical: metrics.issuesBySeverity.critical || 0,
767
+ high: metrics.issuesBySeverity.high || 0,
768
+ medium: metrics.issuesBySeverity.medium || 0,
769
+ low: metrics.issuesBySeverity.low || 0,
770
+ potentialImpact: generateImpactSummary(metrics),
771
+ },
772
+ issues: issues.sort((a, b) => {
773
+ // Sort by severity, then file
774
+ const severityOrder = { critical: 0, high: 1, medium: 2, low: 3 };
775
+ return severityOrder[a.severity] - severityOrder[b.severity];
776
+ }),
777
+ metrics,
778
+ recommendations: generateRecommendations(issues, metrics),
779
+ worktreeChanges: {
780
+ location: worktreePath,
781
+ filesModified: metrics.filesModified,
782
+ diffSummary: execSync(`git -C ${worktreePath} diff --stat HEAD`, { encoding: "utf-8" }),
783
+ },
784
+ };
785
+ }
786
+ ```
787
+
788
+ ### Phase 6: Return to Claude Code
789
+
790
+ **6.1 Format Report for Claude Code**
791
+
792
+ ```typescript
793
+ function formatReportForClaudeCode(report: KISSReport): string {
794
+ return `
795
+ KISS Simplifier Agent Report
796
+ =============================
797
+
798
+ METADATA
799
+ --------
800
+ Timestamp: ${report.metadata.timestamp}
801
+ Scope: ${report.metadata.scope}
802
+ Mode: ${report.metadata.mode}
803
+ Worktree: ${report.metadata.worktree}
804
+
805
+ SUMMARY
806
+ -------
807
+ Total Issues: ${report.summary.totalIssues}
808
+ 🔴 Critical: ${report.summary.critical}
809
+ 🟠 High: ${report.summary.high}
810
+ 🟡 Medium: ${report.summary.medium}
811
+ 🟢 Low: ${report.summary.low}
812
+
813
+ Potential Impact:
814
+ ${report.summary.potentialImpact}
815
+
816
+ WORKTREE CHANGES
817
+ ----------------
818
+ Location: ${report.worktreeChanges.location}
819
+ Files Modified: ${report.worktreeChanges.filesModified}
820
+
821
+ Diff Summary:
822
+ ${report.worktreeChanges.diffSummary}
823
+
824
+ ISSUES DETAIL
825
+ -------------
826
+ ${report.issues.map(formatIssue).join("\n\n")}
827
+
828
+ METRICS
829
+ -------
830
+ Complexity Reduction: ${report.metrics.complexityReduction.before} → ${report.metrics.complexityReduction.after} (${report.metrics.complexityReduction.improvement})
831
+ Lines Removed: ${report.metrics.linesRemoved}
832
+ Net Change: ${report.metrics.netChange} lines
833
+
834
+ RECOMMENDATIONS
835
+ ---------------
836
+ ${report.recommendations.map((r, i) => `${i + 1}. ${r}`).join("\n")}
837
+
838
+ END OF REPORT
839
+ `.trim();
840
+ }
841
+
842
+ function formatIssue(issue: Issue): string {
843
+ return `
844
+ [${issue.id}] ${issue.title}
845
+ Category: ${issue.category} | Severity: ${issue.severity}
846
+ File: ${issue.file}${issue.line ? `:${issue.line}` : ""}
847
+
848
+ Description:
849
+ ${issue.description}
850
+
851
+ Current Code:
852
+ \`\`\`typescript
853
+ ${issue.currentCode}
854
+ \`\`\`
855
+
856
+ ${
857
+ issue.proposedCode
858
+ ? `Proposed Code:
859
+ \`\`\`typescript
860
+ ${issue.proposedCode}
861
+ \`\`\`
862
+ `
863
+ : ""
864
+ }
865
+
866
+ ${
867
+ issue.metrics
868
+ ? `Metrics:
869
+ Before: ${issue.metrics.before}
870
+ After: ${issue.metrics.after}
871
+ Improvement: ${issue.metrics.improvement}
872
+ `
873
+ : ""
874
+ }
875
+
876
+ Status: ${issue.status || "pending"}
877
+ ${issue.error ? `Error: ${issue.error}` : ""}
878
+ `.trim();
879
+ }
880
+ ```
881
+
882
+ **6.2 Return Report**
883
+
884
+ The agent returns the formatted report to Claude Code, which will:
885
+
886
+ 1. Review the findings
887
+ 2. Validate against KISS principles
888
+ 3. Request refinements if needed
889
+ 4. Generate final `.kiss.local.md` report
890
+ 5. Present to user with options
891
+
892
+ ## Agent Constraints
893
+
894
+ ### What the Agent MUST DO
895
+
896
+ 1. ✅ Always create git worktree for changes (never modify main tree)
897
+ 2. ✅ Run all available static analysis tools
898
+ 3. ✅ Validate changes with TypeScript compiler
899
+ 4. ✅ Preserve functionality (no behavior changes)
900
+ 5. ✅ Report all issues with severity and category
901
+ 6. ✅ Include metrics (before/after complexity scores)
902
+ 7. ✅ Generate diff summary for worktree changes
903
+
904
+ ### What the Agent MUST NOT DO
905
+
906
+ 1. ❌ Never modify files in main working tree
907
+ 2. ❌ Never commit changes automatically
908
+ 3. ❌ Never push changes to remote
909
+ 4. ❌ Never delete code without confirming it's truly unused
910
+ 5. ❌ Never skip TypeScript compilation validation
911
+ 6. ❌ Never change functionality (only structure/style)
912
+ 7. ❌ Never merge worktree without human approval
913
+
914
+ ### What the Agent SHOULD DO
915
+
916
+ 1. ⚠️ Should run tests in worktree to verify functionality
917
+ 2. ⚠️ Should suggest names that balance brevity and clarity
918
+ 3. ⚠️ Should extract repeated code to shared functions
919
+ 4. ⚠️ Should use guard clauses to reduce nesting
920
+ 5. ⚠️ Should document why certain code is flagged
921
+ 6. ⚠️ Should provide concrete before/after examples
922
+
923
+ ### What the Agent CAN SKIP
924
+
925
+ 1. ✓ Can skip slow checks if `--fast` flag is set
926
+ 2. ✓ Can skip security scan (Semgrep) for faster execution
927
+ 3. ✓ Can skip coverage analysis if not relevant
928
+ 4. ✓ Can skip test execution if TypeScript compilation passes
929
+
930
+ ## Error Handling
931
+
932
+ The agent must handle errors gracefully and report them to Claude Code:
933
+
934
+ **Tool installation failure:**
935
+
936
+ ```typescript
937
+ try {
938
+ execSync("bun add -D knip fta-cli");
939
+ } catch (error) {
940
+ return {
941
+ status: "error",
942
+ message: "Failed to install analysis tools",
943
+ error: error.message,
944
+ suggestion:
945
+ "Install manually: bun add -D knip fta-cli eslint-plugin-sonarjs dependency-cruiser",
946
+ };
947
+ }
948
+ ```
949
+
950
+ **Worktree creation failure:**
951
+
952
+ ```typescript
953
+ try {
954
+ execSync(`git worktree add ${worktreePath} HEAD`);
955
+ } catch (error) {
956
+ return {
957
+ status: "error",
958
+ message: "Failed to create git worktree",
959
+ error: error.message,
960
+ suggestion: "Ensure working tree is clean and git is available",
961
+ };
962
+ }
963
+ ```
964
+
965
+ **TypeScript compilation failure:**
966
+
967
+ ```typescript
968
+ try {
969
+ execSync("tsc --noEmit", { cwd: worktreePath });
970
+ } catch (error) {
971
+ return {
972
+ status: "warning",
973
+ message: "TypeScript errors in worktree",
974
+ error: error.message,
975
+ suggestion: "Some simplifications may need manual review",
976
+ continueAnyway: true,
977
+ };
978
+ }
979
+ ```
980
+
981
+ **Test failure in worktree:**
982
+
983
+ ```typescript
984
+ try {
985
+ execSync("bun test", { cwd: worktreePath });
986
+ } catch (error) {
987
+ return {
988
+ status: "warning",
989
+ message: "Tests failed in worktree",
990
+ error: error.message,
991
+ suggestion: "Functionality may have changed. Review simplifications carefully.",
992
+ continueAnyway: true,
993
+ };
994
+ }
995
+ ```
996
+
997
+ ## Performance Considerations
998
+
999
+ **Parallel execution:**
1000
+
1001
+ - Run all static analysis tools in parallel
1002
+ - Use multiple CPU cores for large codebases
1003
+ - Cache analysis results for repeated runs
1004
+
1005
+ **Incremental analysis:**
1006
+
1007
+ - Only analyze files in scope (not entire codebase)
1008
+ - Skip unchanged files on subsequent runs
1009
+ - Use git diff to identify changed files
1010
+
1011
+ **Resource limits:**
1012
+
1013
+ - Timeout after 5 minutes for large codebases
1014
+ - Limit worktree size to prevent disk space issues
1015
+ - Clean up worktrees older than 7 days
1016
+
1017
+ ## Integration with Claude Code
1018
+
1019
+ The agent is invoked by Claude Code via the Task tool:
1020
+
1021
+ ```typescript
1022
+ await Task({
1023
+ subagent_type: "kiss-simplifier",
1024
+ description: "Analyze code for over-engineering",
1025
+ prompt: `Analyze ${scope} for KISS violations...`,
1026
+ });
1027
+ ```
1028
+
1029
+ Claude Code receives the formatted report and:
1030
+
1031
+ 1. Validates findings against KISS principles
1032
+ 2. Requests refinements if needed (re-invoke agent)
1033
+ 3. Generates final `.kiss.local.md` report
1034
+ 4. Presents to user with actionable options
1035
+
1036
+ ## Testing the Agent
1037
+
1038
+ **Unit tests:**
1039
+
1040
+ ```bash
1041
+ # Test dead code detection
1042
+ bun test src/analysis/dead-code.test.ts
1043
+
1044
+ # Test complexity analysis
1045
+ bun test src/analysis/complexity.test.ts
1046
+
1047
+ # Test AI pattern detection
1048
+ bun test src/analysis/ai-patterns.test.ts
1049
+ ```
1050
+
1051
+ **Integration tests:**
1052
+
1053
+ ```bash
1054
+ # Test full workflow
1055
+ bun test src/analysis/kiss-workflow.test.ts
1056
+
1057
+ # Test worktree creation
1058
+ bun test src/analysis/worktree.test.ts
1059
+ ```
1060
+
1061
+ **End-to-end test:**
1062
+
1063
+ ```bash
1064
+ # Run against sample codebase
1065
+ /kiss tests/fixtures/overengineered-code/
1066
+
1067
+ # Verify worktree created
1068
+ test -d .worktree/kiss-*
1069
+
1070
+ # Verify report generated
1071
+ cat .kiss.local.md
1072
+ ```
1073
+
1074
+ ## References
1075
+
1076
+ - **KISS Principles:** `detecting-overengineered-code.local.md`
1077
+ - **Command Documentation:** `docs/commands/architect/kiss.md`
1078
+ - **knip Documentation:** https://github.com/webpro/knip
1079
+ - **FTA Documentation:** https://github.com/sgb-io/fta
1080
+ - **ESLint SonarJS:** https://github.com/SonarSource/eslint-plugin-sonarjs
1081
+ - **dependency-cruiser:** https://github.com/sverweij/dependency-cruiser
1082
+
1083
+ ---
1084
+
1085
+ **Agent Status:** Production-ready
1086
+ **Version:** 1.0.0
1087
+ **Last Updated:** 2025-01-17
1088
+ **Maintainer:** AI Toolkit Team