add-skill-kit 3.2.3 → 3.2.5

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 (126) hide show
  1. package/README.md +1 -1
  2. package/bin/lib/commands/help.js +0 -4
  3. package/bin/lib/commands/install.js +90 -9
  4. package/bin/lib/ui.js +1 -1
  5. package/lib/agent-cli/__tests__/adaptive_engine.test.js +190 -0
  6. package/lib/agent-cli/__tests__/integration/cross_script.test.js +222 -0
  7. package/lib/agent-cli/__tests__/integration/full_cycle.test.js +230 -0
  8. package/lib/agent-cli/__tests__/pattern_analyzer.test.js +173 -0
  9. package/lib/agent-cli/__tests__/pre_execution_check.test.js +167 -0
  10. package/lib/agent-cli/__tests__/skill_injector.test.js +191 -0
  11. package/lib/agent-cli/bin/agent.js +191 -0
  12. package/lib/agent-cli/dashboard/dashboard_server.js +340 -0
  13. package/lib/agent-cli/dashboard/index.html +538 -0
  14. package/lib/agent-cli/lib/audit.js +154 -0
  15. package/lib/agent-cli/lib/audit.test.js +100 -0
  16. package/lib/agent-cli/lib/auto-learn.js +319 -0
  17. package/lib/agent-cli/lib/auto_preview.py +148 -0
  18. package/lib/agent-cli/lib/backup.js +138 -0
  19. package/lib/agent-cli/lib/backup.test.js +78 -0
  20. package/lib/agent-cli/lib/checklist.py +222 -0
  21. package/lib/agent-cli/lib/cognitive-lesson.js +476 -0
  22. package/lib/agent-cli/lib/completion.js +149 -0
  23. package/lib/agent-cli/lib/config.js +35 -0
  24. package/lib/agent-cli/lib/eslint-fix.js +238 -0
  25. package/lib/agent-cli/lib/evolution-signal.js +215 -0
  26. package/lib/agent-cli/lib/export.js +86 -0
  27. package/lib/agent-cli/lib/export.test.js +65 -0
  28. package/lib/agent-cli/lib/fix.js +337 -0
  29. package/lib/agent-cli/lib/fix.test.js +80 -0
  30. package/lib/agent-cli/lib/gemini-export.js +83 -0
  31. package/lib/agent-cli/lib/generate-registry.js +42 -0
  32. package/lib/agent-cli/lib/hooks/install-hooks.js +152 -0
  33. package/lib/agent-cli/lib/hooks/lint-learn.js +172 -0
  34. package/lib/agent-cli/lib/ignore.js +116 -0
  35. package/lib/agent-cli/lib/ignore.test.js +58 -0
  36. package/lib/agent-cli/lib/init.js +124 -0
  37. package/lib/agent-cli/lib/learn.js +255 -0
  38. package/lib/agent-cli/lib/learn.test.js +70 -0
  39. package/lib/agent-cli/lib/migrate-to-v4.js +322 -0
  40. package/lib/agent-cli/lib/proposals.js +199 -0
  41. package/lib/agent-cli/lib/proposals.test.js +56 -0
  42. package/lib/agent-cli/lib/recall.js +820 -0
  43. package/lib/agent-cli/lib/recall.test.js +107 -0
  44. package/lib/agent-cli/lib/selfevolution-bridge.js +167 -0
  45. package/lib/agent-cli/lib/session_manager.py +120 -0
  46. package/lib/agent-cli/lib/settings.js +227 -0
  47. package/lib/agent-cli/lib/skill-learn.js +296 -0
  48. package/lib/agent-cli/lib/stats.js +132 -0
  49. package/lib/agent-cli/lib/stats.test.js +94 -0
  50. package/lib/agent-cli/lib/types.js +33 -0
  51. package/lib/agent-cli/lib/ui/audit-ui.js +146 -0
  52. package/lib/agent-cli/lib/ui/backup-ui.js +107 -0
  53. package/lib/agent-cli/lib/ui/clack-helpers.js +317 -0
  54. package/lib/agent-cli/lib/ui/common.js +83 -0
  55. package/lib/agent-cli/lib/ui/completion-ui.js +126 -0
  56. package/lib/agent-cli/lib/ui/custom-select.js +69 -0
  57. package/lib/agent-cli/lib/ui/dashboard-ui.js +222 -0
  58. package/lib/agent-cli/lib/ui/evolution-signals-ui.js +107 -0
  59. package/lib/agent-cli/lib/ui/export-ui.js +94 -0
  60. package/lib/agent-cli/lib/ui/fix-all-ui.js +191 -0
  61. package/lib/agent-cli/lib/ui/help-ui.js +49 -0
  62. package/lib/agent-cli/lib/ui/index.js +199 -0
  63. package/lib/agent-cli/lib/ui/init-ui.js +56 -0
  64. package/lib/agent-cli/lib/ui/knowledge-ui.js +55 -0
  65. package/lib/agent-cli/lib/ui/learn-ui.js +706 -0
  66. package/lib/agent-cli/lib/ui/lessons-ui.js +148 -0
  67. package/lib/agent-cli/lib/ui/pretty.js +145 -0
  68. package/lib/agent-cli/lib/ui/proposals-ui.js +99 -0
  69. package/lib/agent-cli/lib/ui/recall-ui.js +342 -0
  70. package/lib/agent-cli/lib/ui/routing-demo.js +79 -0
  71. package/lib/agent-cli/lib/ui/routing-ui.js +325 -0
  72. package/lib/agent-cli/lib/ui/settings-ui.js +381 -0
  73. package/lib/agent-cli/lib/ui/stats-ui.js +123 -0
  74. package/lib/agent-cli/lib/ui/watch-ui.js +236 -0
  75. package/lib/agent-cli/lib/verify_all.py +327 -0
  76. package/lib/agent-cli/lib/watcher.js +181 -0
  77. package/lib/agent-cli/lib/watcher.test.js +85 -0
  78. package/lib/agent-cli/package.json +51 -0
  79. package/lib/agent-cli/scripts/adaptive_engine.js +381 -0
  80. package/lib/agent-cli/scripts/dashboard_server.js +224 -0
  81. package/lib/agent-cli/scripts/error_sensor.js +565 -0
  82. package/lib/agent-cli/scripts/learn_from_failure.js +225 -0
  83. package/lib/agent-cli/scripts/pattern_analyzer.js +781 -0
  84. package/lib/agent-cli/scripts/pre_execution_check.js +623 -0
  85. package/lib/agent-cli/scripts/rule_sharing.js +374 -0
  86. package/lib/agent-cli/scripts/skill_injector.js +387 -0
  87. package/lib/agent-cli/scripts/success_sensor.js +500 -0
  88. package/lib/agent-cli/scripts/user_correction_sensor.js +426 -0
  89. package/lib/agent-cli/services/auto-learn-service.js +247 -0
  90. package/lib/agent-cli/src/MIGRATION.md +418 -0
  91. package/lib/agent-cli/src/README.md +367 -0
  92. package/lib/agent-cli/src/core/evolution/evolution-signal.js +42 -0
  93. package/lib/agent-cli/src/core/evolution/index.js +17 -0
  94. package/lib/agent-cli/src/core/evolution/review-gate.js +40 -0
  95. package/lib/agent-cli/src/core/evolution/signal-detector.js +137 -0
  96. package/lib/agent-cli/src/core/evolution/signal-queue.js +79 -0
  97. package/lib/agent-cli/src/core/evolution/threshold-checker.js +79 -0
  98. package/lib/agent-cli/src/core/index.js +15 -0
  99. package/lib/agent-cli/src/core/learning/cognitive-enhancer.js +282 -0
  100. package/lib/agent-cli/src/core/learning/index.js +12 -0
  101. package/lib/agent-cli/src/core/learning/lesson-synthesizer.js +83 -0
  102. package/lib/agent-cli/src/core/scanning/index.js +14 -0
  103. package/lib/agent-cli/src/data/index.js +13 -0
  104. package/lib/agent-cli/src/data/repositories/index.js +8 -0
  105. package/lib/agent-cli/src/data/repositories/lesson-repository.js +130 -0
  106. package/lib/agent-cli/src/data/repositories/signal-repository.js +119 -0
  107. package/lib/agent-cli/src/data/storage/index.js +8 -0
  108. package/lib/agent-cli/src/data/storage/json-storage.js +64 -0
  109. package/lib/agent-cli/src/data/storage/yaml-storage.js +66 -0
  110. package/lib/agent-cli/src/infrastructure/index.js +13 -0
  111. package/lib/agent-cli/src/presentation/formatters/skill-formatter.js +232 -0
  112. package/lib/agent-cli/src/services/export-service.js +162 -0
  113. package/lib/agent-cli/src/services/index.js +13 -0
  114. package/lib/agent-cli/src/services/learning-service.js +99 -0
  115. package/lib/agent-cli/types/index.d.ts +343 -0
  116. package/lib/agent-cli/utils/benchmark.js +269 -0
  117. package/lib/agent-cli/utils/logger.js +303 -0
  118. package/lib/agent-cli/utils/ml_patterns.js +300 -0
  119. package/lib/agent-cli/utils/recovery.js +312 -0
  120. package/lib/agent-cli/utils/telemetry.js +290 -0
  121. package/lib/agentskillskit-cli/README.md +21 -0
  122. package/{node_modules/agentskillskit-cli/bin → lib/agentskillskit-cli}/ag-smart.js +15 -15
  123. package/lib/agentskillskit-cli/package.json +51 -0
  124. package/package.json +19 -9
  125. /package/bin/{cli.js → kit.js} +0 -0
  126. /package/{node_modules/agentskillskit-cli → lib/agent-cli}/README.md +0 -0
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Smart Watcher - Real-Time File Monitoring
4
+ *
5
+ * Watches for file changes and runs recall automatically.
6
+ * Features:
7
+ * - Debounced file change detection
8
+ * - Pattern frequency tracking
9
+ * - Live feedback on violations
10
+ *
11
+ * Usage: agent watch [directory]
12
+ */
13
+
14
+ import fs from "fs";
15
+ import path from "path";
16
+ import { scanFile, loadKnowledge, saveKnowledge } from "./recall.js";
17
+ import { VERSION } from "./config.js";
18
+
19
+ // ============================================================================
20
+ // CONFIGURATION
21
+ // ============================================================================
22
+
23
+ const WATCH_EXTENSIONS = [".js", ".ts", ".tsx", ".jsx", ".mjs"];
24
+ const DEBOUNCE_MS = 300;
25
+ const SKIP_DIRS = ["node_modules", ".git", "dist", "build", ".next", "coverage"];
26
+
27
+ // ============================================================================
28
+ // WATCHER
29
+ // ============================================================================
30
+
31
+ class SmartWatcher {
32
+ constructor(rootDir) {
33
+ this.rootDir = path.resolve(rootDir);
34
+ this.db = loadKnowledge();
35
+ this.debounceTimers = new Map();
36
+ this.sessionStats = {
37
+ filesChecked: 0,
38
+ violationsFound: 0,
39
+ startTime: Date.now()
40
+ };
41
+ }
42
+
43
+ /**
44
+ * Start watching
45
+ */
46
+ start() {
47
+ console.log(`\n👁️ Smart Watcher v${VERSION}`);
48
+ console.log(`📂 Watching: ${this.rootDir}`);
49
+ console.log(`📚 Loaded ${this.db.lessons.length} pattern(s) from memory`);
50
+ console.log("─".repeat(50));
51
+ console.log("Watching for changes... (Ctrl+C to stop)\n");
52
+
53
+ this.watch(this.rootDir);
54
+ }
55
+
56
+ /**
57
+ * Recursively watch directories
58
+ */
59
+ watch(dir) {
60
+ try {
61
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
62
+
63
+ // Watch current directory
64
+ fs.watch(dir, { persistent: true }, (eventType, filename) => {
65
+ if (filename && eventType === "change") {
66
+ this.handleChange(path.join(dir, filename));
67
+ }
68
+ });
69
+
70
+ // Recurse into subdirs
71
+ for (const entry of entries) {
72
+ if (entry.isDirectory() && !SKIP_DIRS.includes(entry.name)) {
73
+ this.watch(path.join(dir, entry.name));
74
+ }
75
+ }
76
+ } catch (error) {
77
+ // Ignore permission errors
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Handle file change with debouncing
83
+ */
84
+ handleChange(filePath) {
85
+ const ext = path.extname(filePath);
86
+ if (!WATCH_EXTENSIONS.includes(ext)) return;
87
+ if (!fs.existsSync(filePath)) return;
88
+
89
+ // Debounce
90
+ if (this.debounceTimers.has(filePath)) {
91
+ clearTimeout(this.debounceTimers.get(filePath));
92
+ }
93
+
94
+ this.debounceTimers.set(filePath, setTimeout(() => {
95
+ this.checkFile(filePath);
96
+ this.debounceTimers.delete(filePath);
97
+ }, DEBOUNCE_MS));
98
+ }
99
+
100
+ /**
101
+ * Check a file against memory
102
+ */
103
+ checkFile(filePath) {
104
+ const relativePath = path.relative(this.rootDir, filePath);
105
+ const result = scanFile(filePath, this.db, true);
106
+
107
+ this.sessionStats.filesChecked++;
108
+
109
+ if (result.violations.length === 0) {
110
+ console.log(`✅ ${relativePath}`);
111
+ return;
112
+ }
113
+
114
+ console.log(`\n⚠️ ${relativePath}`);
115
+
116
+ result.violations.forEach(({ lesson, matches }) => {
117
+ this.sessionStats.violationsFound += matches.length;
118
+
119
+ const icon = lesson.severity === "ERROR" ? "❌" : "⚠️";
120
+ console.log(` ${icon} [${lesson.id}] ${lesson.message}`);
121
+
122
+ matches.slice(0, 3).forEach(m => {
123
+ console.log(` L${m.line}: ${m.content}`);
124
+ });
125
+
126
+ if (matches.length > 3) {
127
+ console.log(` ... and ${matches.length - 3} more`);
128
+ }
129
+ });
130
+
131
+ console.log("");
132
+
133
+ // Save updated stats
134
+ saveKnowledge(this.db);
135
+ }
136
+
137
+ /**
138
+ * Print session summary
139
+ */
140
+ printSummary() {
141
+ const duration = Math.round((Date.now() - this.sessionStats.startTime) / 1000);
142
+ console.log("\n" + "─".repeat(50));
143
+ console.log("📊 Session Summary:");
144
+ console.log(` ⏱️ Duration: ${duration}s`);
145
+ console.log(` 📄 Files checked: ${this.sessionStats.filesChecked}`);
146
+ console.log(` ⚠️ Violations found: ${this.sessionStats.violationsFound}`);
147
+ }
148
+ }
149
+
150
+ // ============================================================================
151
+ // CLI
152
+ // ============================================================================
153
+
154
+ const args = process.argv.slice(2);
155
+ const targetDir = args[0] || process.cwd();
156
+
157
+ if (args.includes("--help")) {
158
+ console.log(`
159
+ 👁️ Smart Watcher - Real-Time Monitor
160
+
161
+ Usage:
162
+ agent watch [directory]
163
+
164
+ Options:
165
+ --help Show this help
166
+
167
+ The watcher monitors file changes and checks them against
168
+ learned patterns in real-time.
169
+ `);
170
+ process.exit(0);
171
+ }
172
+
173
+ const watcher = new SmartWatcher(targetDir);
174
+
175
+ // Handle graceful shutdown
176
+ process.on("SIGINT", () => {
177
+ watcher.printSummary();
178
+ process.exit(0);
179
+ });
180
+
181
+ watcher.start();
@@ -0,0 +1,85 @@
1
+ /**
2
+ * @fileoverview Tests for watcher.js functionality
3
+ */
4
+ import { describe, it, expect } from "vitest";
5
+ import path from "path";
6
+
7
+ describe("Watcher - Extension Filtering", () => {
8
+ const WATCH_EXTENSIONS = [".js", ".ts", ".tsx", ".jsx", ".mjs"];
9
+
10
+ it("includes JavaScript files", () => {
11
+ const file = "app.js";
12
+ const ext = path.extname(file);
13
+ expect(WATCH_EXTENSIONS.includes(ext)).toBe(true);
14
+ });
15
+
16
+ it("includes TypeScript files", () => {
17
+ const file = "app.tsx";
18
+ const ext = path.extname(file);
19
+ expect(WATCH_EXTENSIONS.includes(ext)).toBe(true);
20
+ });
21
+
22
+ it("excludes CSS files", () => {
23
+ const file = "styles.css";
24
+ const ext = path.extname(file);
25
+ expect(WATCH_EXTENSIONS.includes(ext)).toBe(false);
26
+ });
27
+
28
+ it("excludes markdown files", () => {
29
+ const file = "README.md";
30
+ const ext = path.extname(file);
31
+ expect(WATCH_EXTENSIONS.includes(ext)).toBe(false);
32
+ });
33
+ });
34
+
35
+ describe("Watcher - Directory Filtering", () => {
36
+ const SKIP_DIRS = ["node_modules", ".git", "dist", "build", ".next", "coverage"];
37
+
38
+ it("skips node_modules", () => {
39
+ expect(SKIP_DIRS.includes("node_modules")).toBe(true);
40
+ });
41
+
42
+ it("skips .git directory", () => {
43
+ expect(SKIP_DIRS.includes(".git")).toBe(true);
44
+ });
45
+
46
+ it("skips dist directory", () => {
47
+ expect(SKIP_DIRS.includes("dist")).toBe(true);
48
+ });
49
+
50
+ it("allows src directory", () => {
51
+ expect(SKIP_DIRS.includes("src")).toBe(false);
52
+ });
53
+
54
+ it("allows lib directory", () => {
55
+ expect(SKIP_DIRS.includes("lib")).toBe(false);
56
+ });
57
+ });
58
+
59
+ describe("Watcher - Session Stats", () => {
60
+ it("initializes session stats correctly", () => {
61
+ const sessionStats = {
62
+ filesChecked: 0,
63
+ violationsFound: 0,
64
+ startTime: Date.now()
65
+ };
66
+
67
+ expect(sessionStats.filesChecked).toBe(0);
68
+ expect(sessionStats.violationsFound).toBe(0);
69
+ expect(typeof sessionStats.startTime).toBe("number");
70
+ });
71
+
72
+ it("increments file count correctly", () => {
73
+ const sessionStats = { filesChecked: 0 };
74
+ sessionStats.filesChecked++;
75
+ sessionStats.filesChecked++;
76
+ expect(sessionStats.filesChecked).toBe(2);
77
+ });
78
+
79
+ it("calculates duration correctly", () => {
80
+ const startTime = Date.now() - 5000; // 5 seconds ago
81
+ const duration = Math.round((Date.now() - startTime) / 1000);
82
+ expect(duration).toBeGreaterThanOrEqual(4);
83
+ expect(duration).toBeLessThanOrEqual(6);
84
+ });
85
+ });
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "agentskillskit-cli",
3
+ "version": "3.2.4",
4
+ "description": "CLI for Agent Skill Kit",
5
+ "type": "module",
6
+ "main": "lib/config.js",
7
+ "bin": {
8
+ "agent": "bin/agent.js",
9
+ "agent": "bin/agent.js",
10
+ "agent-skills-kit": "bin/agent.js"
11
+ },
12
+ "scripts": {
13
+ "test": "vitest run",
14
+ "test:watch": "vitest"
15
+ },
16
+ "dependencies": {
17
+ "@clack/core": "^0.5.0",
18
+ "@clack/prompts": "^0.11.0",
19
+ "boxen": "^8.0.1",
20
+ "clipboardy": "^5.1.0",
21
+ "js-yaml": "^4.1.0",
22
+ "ora": "^9.1.0",
23
+ "picocolors": "^1.1.1"
24
+ },
25
+ "devDependencies": {
26
+ "vitest": "^1.6.0"
27
+ },
28
+ "author": "Agent Skill Kit Authors",
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/agentskillkit/agent-skills.git",
33
+ "directory": "packages/cli"
34
+ },
35
+ "keywords": [
36
+ "agent",
37
+ "ai",
38
+ "skills",
39
+ "cli",
40
+ "tooling",
41
+ "antigravity"
42
+ ],
43
+ "files": [
44
+ "bin",
45
+ "lib",
46
+ "README.md"
47
+ ],
48
+ "publishConfig": {
49
+ "access": "public"
50
+ }
51
+ }
@@ -0,0 +1,381 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Adaptive Engine - Auto-adjust severity and promote/demote rules
4
+ *
5
+ * Part of FAANG-Grade Auto-Learn System Phase 5
6
+ *
7
+ * Features:
8
+ * - Auto-adjust severity based on hit count
9
+ * - Auto-promote rules with 95%+ accuracy to core
10
+ * - Auto-demote rules with <50% accuracy
11
+ * - Track rule effectiveness
12
+ *
13
+ * Usage:
14
+ * node adaptive_engine.js --analyze
15
+ * node adaptive_engine.js --promote
16
+ * node adaptive_engine.js --demote
17
+ */
18
+
19
+ import fs from 'fs';
20
+ import path from 'path';
21
+ import { fileURLToPath } from 'url';
22
+
23
+ const __filename = fileURLToPath(import.meta.url);
24
+ const __dirname = path.dirname(__filename);
25
+
26
+ // Colors
27
+ const c = {
28
+ reset: '\x1b[0m',
29
+ red: '\x1b[31m',
30
+ green: '\x1b[32m',
31
+ yellow: '\x1b[33m',
32
+ blue: '\x1b[34m',
33
+ cyan: '\x1b[36m',
34
+ gray: '\x1b[90m',
35
+ bold: '\x1b[1m'
36
+ };
37
+
38
+ // Paths
39
+ function findProjectRoot() {
40
+ let current = process.cwd();
41
+ while (current !== path.dirname(current)) {
42
+ if (fs.existsSync(path.join(current, '.agent'))) {
43
+ return current;
44
+ }
45
+ current = path.dirname(current);
46
+ }
47
+ return process.cwd();
48
+ }
49
+
50
+ const projectRoot = findProjectRoot();
51
+ const knowledgePath = path.join(projectRoot, '.agent', 'knowledge');
52
+
53
+ // Severity levels
54
+ const SEVERITY_LEVELS = ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL'];
55
+
56
+ // Load JSON file
57
+ function loadJson(filename) {
58
+ const filePath = path.join(knowledgePath, filename);
59
+ try {
60
+ if (fs.existsSync(filePath)) {
61
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
62
+ }
63
+ } catch { }
64
+ return null;
65
+ }
66
+
67
+ // Save JSON file
68
+ function saveJson(filename, data) {
69
+ const filePath = path.join(knowledgePath, filename);
70
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
71
+ }
72
+
73
+ // ==================== SEVERITY ADJUSTMENT ====================
74
+
75
+ /**
76
+ * Auto-adjust severity based on hit count
77
+ * Rules hit 10+ times -> increase severity
78
+ * Rules hit 0 times in 30 days -> decrease severity
79
+ */
80
+ function adjustSeverity() {
81
+ const lessons = loadJson('lessons-learned.json');
82
+ if (!lessons?.lessons?.length) {
83
+ console.log(`${c.gray}No lessons to analyze${c.reset}`);
84
+ return [];
85
+ }
86
+
87
+ const adjustments = [];
88
+ const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
89
+
90
+ for (const lesson of lessons.lessons) {
91
+ const hitCount = lesson.hitCount || 0;
92
+ const currentSeverity = lesson.severity || 'MEDIUM';
93
+ const currentIndex = SEVERITY_LEVELS.indexOf(currentSeverity);
94
+ const lastHit = lesson.lastHit ? new Date(lesson.lastHit) : null;
95
+
96
+ let newSeverity = currentSeverity;
97
+ let reason = null;
98
+
99
+ // Increase severity if hit 10+ times
100
+ if (hitCount >= 10 && currentIndex < SEVERITY_LEVELS.length - 1) {
101
+ newSeverity = SEVERITY_LEVELS[currentIndex + 1];
102
+ reason = `Hit ${hitCount} times - increasing severity`;
103
+ }
104
+ // Further increase if hit 25+ times
105
+ else if (hitCount >= 25 && currentIndex < SEVERITY_LEVELS.length - 2) {
106
+ newSeverity = SEVERITY_LEVELS[currentIndex + 2];
107
+ reason = `Hit ${hitCount} times - promoting to ${newSeverity}`;
108
+ }
109
+ // Decrease severity if not hit in 30 days
110
+ else if (lastHit && lastHit < thirtyDaysAgo && currentIndex > 0) {
111
+ newSeverity = SEVERITY_LEVELS[currentIndex - 1];
112
+ reason = `Not hit in 30 days - decreasing severity`;
113
+ }
114
+
115
+ if (reason) {
116
+ lesson.severity = newSeverity;
117
+ lesson.adjustedAt = new Date().toISOString();
118
+ adjustments.push({
119
+ id: lesson.id,
120
+ from: currentSeverity,
121
+ to: newSeverity,
122
+ reason
123
+ });
124
+ }
125
+ }
126
+
127
+ if (adjustments.length > 0) {
128
+ saveJson('lessons-learned.json', lessons);
129
+ }
130
+
131
+ return adjustments;
132
+ }
133
+
134
+ // ==================== AUTO-PROMOTION ====================
135
+
136
+ /**
137
+ * Promote rules with 95%+ accuracy to core rules
138
+ */
139
+ function promoteRules() {
140
+ const lessons = loadJson('lessons-learned.json');
141
+ if (!lessons?.lessons?.length) {
142
+ console.log(`${c.gray}No lessons to analyze${c.reset}`);
143
+ return [];
144
+ }
145
+
146
+ const promoted = [];
147
+
148
+ for (const lesson of lessons.lessons) {
149
+ const hitCount = lesson.hitCount || 0;
150
+ const successCount = lesson.successCount || 0;
151
+ const totalAttempts = hitCount + successCount;
152
+
153
+ // Need at least 10 attempts to evaluate
154
+ if (totalAttempts < 10) continue;
155
+
156
+ const accuracy = successCount / totalAttempts;
157
+
158
+ // Promote if 95%+ accuracy and not already CRITICAL
159
+ if (accuracy >= 0.95 && lesson.severity !== 'CRITICAL' && !lesson.promoted) {
160
+ lesson.promoted = true;
161
+ lesson.promotedAt = new Date().toISOString();
162
+ lesson.previousSeverity = lesson.severity;
163
+ lesson.severity = 'CRITICAL';
164
+
165
+ promoted.push({
166
+ id: lesson.id,
167
+ pattern: lesson.pattern,
168
+ accuracy: Math.round(accuracy * 100),
169
+ status: 'PROMOTED_TO_CORE'
170
+ });
171
+ }
172
+ }
173
+
174
+ if (promoted.length > 0) {
175
+ saveJson('lessons-learned.json', lessons);
176
+ }
177
+
178
+ return promoted;
179
+ }
180
+
181
+ /**
182
+ * Demote rules with <50% accuracy
183
+ */
184
+ function demoteRules() {
185
+ const lessons = loadJson('lessons-learned.json');
186
+ if (!lessons?.lessons?.length) {
187
+ console.log(`${c.gray}No lessons to analyze${c.reset}`);
188
+ return [];
189
+ }
190
+
191
+ const demoted = [];
192
+
193
+ for (const lesson of lessons.lessons) {
194
+ const hitCount = lesson.hitCount || 0;
195
+ const successCount = lesson.successCount || 0;
196
+ const totalAttempts = hitCount + successCount;
197
+
198
+ // Need at least 10 attempts to evaluate
199
+ if (totalAttempts < 10) continue;
200
+
201
+ const accuracy = successCount / totalAttempts;
202
+
203
+ // Demote if <50% accuracy and higher than LOW
204
+ if (accuracy < 0.50 && lesson.severity !== 'LOW' && !lesson.demoted) {
205
+ lesson.demoted = true;
206
+ lesson.demotedAt = new Date().toISOString();
207
+ lesson.previousSeverity = lesson.severity;
208
+ lesson.severity = 'LOW';
209
+
210
+ demoted.push({
211
+ id: lesson.id,
212
+ pattern: lesson.pattern,
213
+ accuracy: Math.round(accuracy * 100),
214
+ status: 'DEMOTED_TO_LOW'
215
+ });
216
+ }
217
+ }
218
+
219
+ if (demoted.length > 0) {
220
+ saveJson('lessons-learned.json', lessons);
221
+ }
222
+
223
+ return demoted;
224
+ }
225
+
226
+ // ==================== ANALYSIS ====================
227
+
228
+ /**
229
+ * Analyze all rules and provide recommendations
230
+ */
231
+ function analyzeRules() {
232
+ const lessons = loadJson('lessons-learned.json');
233
+
234
+ console.log(`${c.cyan}╔════════════════════════════════════════╗${c.reset}`);
235
+ console.log(`${c.cyan}║${c.reset} 🧠 Adaptive Engine Analysis ${c.cyan}║${c.reset}`);
236
+ console.log(`${c.cyan}╚════════════════════════════════════════╝${c.reset}\n`);
237
+
238
+ if (!lessons?.lessons?.length) {
239
+ console.log(`${c.gray}No lessons found. Run error_sensor first.${c.reset}`);
240
+ return;
241
+ }
242
+
243
+ // Group by severity
244
+ const bySeverity = {};
245
+ for (const lesson of lessons.lessons) {
246
+ const sev = lesson.severity || 'MEDIUM';
247
+ bySeverity[sev] = (bySeverity[sev] || 0) + 1;
248
+ }
249
+
250
+ console.log(`${c.bold}Current Distribution:${c.reset}`);
251
+ for (const sev of SEVERITY_LEVELS) {
252
+ const count = bySeverity[sev] || 0;
253
+ const bar = '█'.repeat(Math.min(count, 20));
254
+ const color = { LOW: c.gray, MEDIUM: c.blue, HIGH: c.yellow, CRITICAL: c.red }[sev];
255
+ console.log(` ${color}${sev.padEnd(8)}${c.reset} ${bar} ${count}`);
256
+ }
257
+
258
+ // Find candidates for promotion/demotion
259
+ const promotionCandidates = [];
260
+ const demotionCandidates = [];
261
+
262
+ for (const lesson of lessons.lessons) {
263
+ const hitCount = lesson.hitCount || 0;
264
+
265
+ if (hitCount >= 10 && lesson.severity !== 'CRITICAL') {
266
+ promotionCandidates.push(lesson);
267
+ }
268
+ if (hitCount === 0 && lesson.severity !== 'LOW') {
269
+ demotionCandidates.push(lesson);
270
+ }
271
+ }
272
+
273
+ if (promotionCandidates.length > 0) {
274
+ console.log(`\n${c.green}${c.bold}Promotion Candidates (high hit count):${c.reset}`);
275
+ for (const l of promotionCandidates.slice(0, 5)) {
276
+ console.log(` ${c.green}↑${c.reset} ${l.id}: ${l.pattern?.slice(0, 40)}... (${l.hitCount} hits)`);
277
+ }
278
+ }
279
+
280
+ if (demotionCandidates.length > 0) {
281
+ console.log(`\n${c.yellow}${c.bold}Demotion Candidates (no hits):${c.reset}`);
282
+ for (const l of demotionCandidates.slice(0, 5)) {
283
+ console.log(` ${c.yellow}↓${c.reset} ${l.id}: ${l.pattern?.slice(0, 40)}...`);
284
+ }
285
+ }
286
+
287
+ console.log(`\n${c.gray}Total lessons: ${lessons.lessons.length}${c.reset}`);
288
+ console.log(`${c.gray}Run --adjust to apply severity changes${c.reset}`);
289
+ }
290
+
291
+ /**
292
+ * Run full adaptive cycle
293
+ */
294
+ function runAdaptiveCycle() {
295
+ console.log(`${c.cyan}🔄 Running Adaptive Cycle...${c.reset}\n`);
296
+
297
+ // Step 1: Adjust severity
298
+ console.log(`${c.bold}Step 1: Severity Adjustment${c.reset}`);
299
+ const adjustments = adjustSeverity();
300
+ if (adjustments.length > 0) {
301
+ for (const adj of adjustments) {
302
+ console.log(` ${adj.id}: ${adj.from} → ${adj.to} (${adj.reason})`);
303
+ }
304
+ } else {
305
+ console.log(` ${c.gray}No adjustments needed${c.reset}`);
306
+ }
307
+
308
+ // Step 2: Promote high-performers
309
+ console.log(`\n${c.bold}Step 2: Auto-Promotion${c.reset}`);
310
+ const promoted = promoteRules();
311
+ if (promoted.length > 0) {
312
+ for (const p of promoted) {
313
+ console.log(` ${c.green}↑ PROMOTED${c.reset}: ${p.id} (${p.accuracy}% accuracy)`);
314
+ }
315
+ } else {
316
+ console.log(` ${c.gray}No rules meet promotion threshold (95%+)${c.reset}`);
317
+ }
318
+
319
+ // Step 3: Demote low-performers
320
+ console.log(`\n${c.bold}Step 3: Auto-Demotion${c.reset}`);
321
+ const demoted = demoteRules();
322
+ if (demoted.length > 0) {
323
+ for (const d of demoted) {
324
+ console.log(` ${c.yellow}↓ DEMOTED${c.reset}: ${d.id} (${d.accuracy}% accuracy)`);
325
+ }
326
+ } else {
327
+ console.log(` ${c.gray}No rules meet demotion threshold (<50%)${c.reset}`);
328
+ }
329
+
330
+ console.log(`\n${c.green}✓ Adaptive cycle complete!${c.reset}`);
331
+ console.log(` Adjusted: ${adjustments.length}`);
332
+ console.log(` Promoted: ${promoted.length}`);
333
+ console.log(` Demoted: ${demoted.length}`);
334
+ }
335
+
336
+ // ==================== MAIN ====================
337
+
338
+ const args = process.argv.slice(2);
339
+
340
+ if (args.includes('--analyze') || args.includes('-a')) {
341
+ analyzeRules();
342
+ } else if (args.includes('--adjust')) {
343
+ console.log(`${c.cyan}Adjusting severity levels...${c.reset}`);
344
+ const adjustments = adjustSeverity();
345
+ console.log(`${c.green}✓ Made ${adjustments.length} adjustments${c.reset}`);
346
+ for (const adj of adjustments) {
347
+ console.log(` ${adj.id}: ${adj.from} → ${adj.to}`);
348
+ }
349
+ } else if (args.includes('--promote')) {
350
+ console.log(`${c.cyan}Checking for promotion candidates...${c.reset}`);
351
+ const promoted = promoteRules();
352
+ console.log(`${c.green}✓ Promoted ${promoted.length} rules${c.reset}`);
353
+ } else if (args.includes('--demote')) {
354
+ console.log(`${c.cyan}Checking for demotion candidates...${c.reset}`);
355
+ const demoted = demoteRules();
356
+ console.log(`${c.yellow}✓ Demoted ${demoted.length} rules${c.reset}`);
357
+ } else if (args.includes('--run') || args.includes('-r')) {
358
+ runAdaptiveCycle();
359
+ } else {
360
+ console.log(`${c.cyan}adaptive_engine - Auto-adjust severity and promote/demote rules${c.reset}
361
+
362
+ ${c.bold}Usage:${c.reset}
363
+ node adaptive_engine.js --analyze Analyze rules and show recommendations
364
+ node adaptive_engine.js --adjust Adjust severity based on hit count
365
+ node adaptive_engine.js --promote Promote high-accuracy rules
366
+ node adaptive_engine.js --demote Demote low-accuracy rules
367
+ node adaptive_engine.js --run Run full adaptive cycle
368
+
369
+ ${c.bold}Thresholds:${c.reset}
370
+ Severity increase: 10+ hits
371
+ Severity decrease: 0 hits in 30 days
372
+ Promotion: 95%+ accuracy with 10+ attempts
373
+ Demotion: <50% accuracy with 10+ attempts
374
+
375
+ ${c.bold}Examples:${c.reset}
376
+ node adaptive_engine.js --analyze View current state
377
+ node adaptive_engine.js --run Apply all adaptations
378
+ `);
379
+ }
380
+
381
+ export { adjustSeverity, promoteRules, demoteRules, analyzeRules, runAdaptiveCycle };