add-skill-kit 3.2.3 → 3.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/agent-cli/lib/audit.js +154 -0
- package/lib/agent-cli/lib/audit.test.js +100 -0
- package/lib/agent-cli/lib/auto-learn.js +319 -0
- package/lib/agent-cli/lib/auto_preview.py +148 -0
- package/lib/agent-cli/lib/backup.js +138 -0
- package/lib/agent-cli/lib/backup.test.js +78 -0
- package/lib/agent-cli/lib/checklist.py +222 -0
- package/lib/agent-cli/lib/cognitive-lesson.js +476 -0
- package/lib/agent-cli/lib/completion.js +149 -0
- package/lib/agent-cli/lib/config.js +35 -0
- package/lib/agent-cli/lib/eslint-fix.js +238 -0
- package/lib/agent-cli/lib/evolution-signal.js +215 -0
- package/lib/agent-cli/lib/export.js +86 -0
- package/lib/agent-cli/lib/export.test.js +65 -0
- package/lib/agent-cli/lib/fix.js +337 -0
- package/lib/agent-cli/lib/fix.test.js +80 -0
- package/lib/agent-cli/lib/gemini-export.js +83 -0
- package/lib/agent-cli/lib/generate-registry.js +42 -0
- package/lib/agent-cli/lib/hooks/install-hooks.js +152 -0
- package/lib/agent-cli/lib/hooks/lint-learn.js +172 -0
- package/lib/agent-cli/lib/ignore.js +116 -0
- package/lib/agent-cli/lib/ignore.test.js +58 -0
- package/lib/agent-cli/lib/init.js +124 -0
- package/lib/agent-cli/lib/learn.js +255 -0
- package/lib/agent-cli/lib/learn.test.js +70 -0
- package/lib/agent-cli/lib/migrate-to-v4.js +322 -0
- package/lib/agent-cli/lib/proposals.js +199 -0
- package/lib/agent-cli/lib/proposals.test.js +56 -0
- package/lib/agent-cli/lib/recall.js +820 -0
- package/lib/agent-cli/lib/recall.test.js +107 -0
- package/lib/agent-cli/lib/selfevolution-bridge.js +167 -0
- package/lib/agent-cli/lib/session_manager.py +120 -0
- package/lib/agent-cli/lib/settings.js +203 -0
- package/lib/agent-cli/lib/skill-learn.js +296 -0
- package/lib/agent-cli/lib/stats.js +132 -0
- package/lib/agent-cli/lib/stats.test.js +94 -0
- package/lib/agent-cli/lib/types.js +33 -0
- package/lib/agent-cli/lib/ui/audit-ui.js +146 -0
- package/lib/agent-cli/lib/ui/backup-ui.js +107 -0
- package/lib/agent-cli/lib/ui/clack-helpers.js +317 -0
- package/lib/agent-cli/lib/ui/common.js +83 -0
- package/lib/agent-cli/lib/ui/completion-ui.js +126 -0
- package/lib/agent-cli/lib/ui/custom-select.js +69 -0
- package/lib/agent-cli/lib/ui/dashboard-ui.js +123 -0
- package/lib/agent-cli/lib/ui/evolution-signals-ui.js +107 -0
- package/lib/agent-cli/lib/ui/export-ui.js +94 -0
- package/lib/agent-cli/lib/ui/fix-all-ui.js +191 -0
- package/lib/agent-cli/lib/ui/help-ui.js +49 -0
- package/lib/agent-cli/lib/ui/index.js +169 -0
- package/lib/agent-cli/lib/ui/init-ui.js +56 -0
- package/lib/agent-cli/lib/ui/knowledge-ui.js +55 -0
- package/lib/agent-cli/lib/ui/learn-ui.js +706 -0
- package/lib/agent-cli/lib/ui/lessons-ui.js +148 -0
- package/lib/agent-cli/lib/ui/pretty.js +145 -0
- package/lib/agent-cli/lib/ui/proposals-ui.js +99 -0
- package/lib/agent-cli/lib/ui/recall-ui.js +342 -0
- package/lib/agent-cli/lib/ui/routing-demo.js +79 -0
- package/lib/agent-cli/lib/ui/routing-ui.js +325 -0
- package/lib/agent-cli/lib/ui/settings-ui.js +381 -0
- package/lib/agent-cli/lib/ui/stats-ui.js +123 -0
- package/lib/agent-cli/lib/ui/watch-ui.js +236 -0
- package/lib/agent-cli/lib/verify_all.py +327 -0
- package/lib/agent-cli/lib/watcher.js +181 -0
- package/lib/agent-cli/lib/watcher.test.js +85 -0
- package/lib/agent-cli/package.json +51 -0
- package/lib/agentskillskit-cli/README.md +21 -0
- package/lib/agentskillskit-cli/ag-smart.js +158 -0
- package/lib/agentskillskit-cli/package.json +51 -0
- package/package.json +10 -6
- /package/{node_modules/agentskillskit-cli → lib/agent-cli}/README.md +0 -0
- /package/{node_modules/agentskillskit-cli → lib/agent-cli}/bin/ag-smart.js +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Smart Audit Script (Production-Ready)
|
|
4
|
+
*
|
|
5
|
+
* The "Judge" - Orchestrates all compliance checks:
|
|
6
|
+
* 1. Memory Recall (Past Mistakes)
|
|
7
|
+
* 2. Constitution Checks (Governance)
|
|
8
|
+
* 3. Real-time Analysis
|
|
9
|
+
*
|
|
10
|
+
* Usage: ag-smart audit [directory]
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import fs from "fs";
|
|
14
|
+
import path from "path";
|
|
15
|
+
import { fileURLToPath } from "url";
|
|
16
|
+
import { scanDirectory, loadKnowledge, saveKnowledge, printResults } from "./recall.js";
|
|
17
|
+
import { AGENT_DIR, VERSION } from "./config.js";
|
|
18
|
+
import * as p from "@clack/prompts";
|
|
19
|
+
import pc from "picocolors";
|
|
20
|
+
|
|
21
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// AUDIT CONFIGURATION
|
|
25
|
+
// ============================================================================
|
|
26
|
+
|
|
27
|
+
const SCAN_EXTENSIONS = [".js", ".ts", ".tsx", ".jsx", ".mjs"];
|
|
28
|
+
|
|
29
|
+
// ============================================================================
|
|
30
|
+
// GOVERNANCE CHECKS
|
|
31
|
+
// ============================================================================
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Check if governance files exist
|
|
35
|
+
* @param {string} projectRoot
|
|
36
|
+
* @returns {{ passed: boolean, details: string[] }}
|
|
37
|
+
*/
|
|
38
|
+
function checkGovernance(projectRoot) {
|
|
39
|
+
const details = [];
|
|
40
|
+
let passed = true;
|
|
41
|
+
|
|
42
|
+
const governanceFiles = [
|
|
43
|
+
{ path: path.join(projectRoot, ".agent", "GEMINI.md"), name: "GEMINI.md" },
|
|
44
|
+
{ path: path.join(projectRoot, ".agent", "ARCHITECTURE.md"), name: "ARCHITECTURE.md" }
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
governanceFiles.forEach(file => {
|
|
48
|
+
if (fs.existsSync(file.path)) {
|
|
49
|
+
details.push(`${pc.green("✓")} ${file.name} found`);
|
|
50
|
+
} else {
|
|
51
|
+
details.push(`${pc.yellow("⚠")} ${file.name} not found (optional)`);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Check for skills
|
|
56
|
+
const skillsDir = path.join(projectRoot, ".agent", "skills");
|
|
57
|
+
if (fs.existsSync(skillsDir)) {
|
|
58
|
+
const skills = fs.readdirSync(skillsDir).filter(f =>
|
|
59
|
+
fs.statSync(path.join(skillsDir, f)).isDirectory()
|
|
60
|
+
);
|
|
61
|
+
details.push(`${pc.green("✓")} ${skills.length} skill(s) loaded`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return { passed, details };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// MAIN AUDIT
|
|
69
|
+
// ============================================================================
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Run full audit on a project
|
|
73
|
+
* @param {string} projectRoot
|
|
74
|
+
*/
|
|
75
|
+
async function runAudit(projectRoot) {
|
|
76
|
+
p.intro(pc.cyan(`⚖️ SMART AUDIT v${VERSION}`));
|
|
77
|
+
|
|
78
|
+
let exitCode = 0;
|
|
79
|
+
const startTime = Date.now();
|
|
80
|
+
|
|
81
|
+
// Phase 1: Memory Recall
|
|
82
|
+
const s1 = p.spinner();
|
|
83
|
+
s1.start("Phase 1: Memory Recall");
|
|
84
|
+
|
|
85
|
+
const db = loadKnowledge();
|
|
86
|
+
|
|
87
|
+
if (db.lessons.length === 0) {
|
|
88
|
+
s1.stop("Phase 1: No lessons learned yet");
|
|
89
|
+
} else {
|
|
90
|
+
const { results } = scanDirectory(projectRoot, db, SCAN_EXTENSIONS);
|
|
91
|
+
|
|
92
|
+
if (results.length > 0) {
|
|
93
|
+
const stats = printResults(results);
|
|
94
|
+
saveKnowledge(db);
|
|
95
|
+
|
|
96
|
+
if (stats.errors > 0) {
|
|
97
|
+
exitCode = 1;
|
|
98
|
+
}
|
|
99
|
+
s1.stop(`Phase 1: Found ${stats.total} violation(s)`);
|
|
100
|
+
} else {
|
|
101
|
+
s1.stop("Phase 1: No violations found");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Phase 2: Governance
|
|
106
|
+
const s2 = p.spinner();
|
|
107
|
+
s2.start("Phase 2: Governance Check");
|
|
108
|
+
|
|
109
|
+
const govResult = checkGovernance(projectRoot);
|
|
110
|
+
s2.stop("Phase 2: Governance checked");
|
|
111
|
+
|
|
112
|
+
p.note(govResult.details.join("\n"), pc.dim("Governance"));
|
|
113
|
+
|
|
114
|
+
// Phase 3: Summary
|
|
115
|
+
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
|
|
116
|
+
const totalHits = db.lessons.reduce((sum, l) => sum + (l.hitCount || 0), 0);
|
|
117
|
+
|
|
118
|
+
const summaryLines = [
|
|
119
|
+
`⏱️ Completed in ${duration}s`,
|
|
120
|
+
`📊 Lessons in memory: ${db.lessons.length}`,
|
|
121
|
+
`🎯 Total pattern hits: ${totalHits}`
|
|
122
|
+
];
|
|
123
|
+
p.note(summaryLines.join("\n"), pc.dim("Summary"));
|
|
124
|
+
|
|
125
|
+
if (exitCode === 0) {
|
|
126
|
+
p.outro(pc.green("✅ AUDIT PASSED: Code is smart and compliant"));
|
|
127
|
+
} else {
|
|
128
|
+
p.outro(pc.red("❌ AUDIT FAILED: Please fix ERROR violations"));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
process.exit(exitCode);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ============================================================================
|
|
135
|
+
// CLI
|
|
136
|
+
// ============================================================================
|
|
137
|
+
|
|
138
|
+
const args = process.argv.slice(2);
|
|
139
|
+
const projectRoot = args[0] || process.cwd();
|
|
140
|
+
|
|
141
|
+
if (args.includes("--help")) {
|
|
142
|
+
console.log(`
|
|
143
|
+
⚖️ Smart Audit - Compliance Checker
|
|
144
|
+
|
|
145
|
+
Usage:
|
|
146
|
+
ag-smart audit [directory]
|
|
147
|
+
|
|
148
|
+
Options:
|
|
149
|
+
--help Show this help
|
|
150
|
+
`);
|
|
151
|
+
process.exit(0);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
runAudit(projectRoot);
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Tests for audit.js functionality
|
|
3
|
+
*/
|
|
4
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import os from "os";
|
|
8
|
+
|
|
9
|
+
const TEST_DIR = path.join(os.tmpdir(), "agent-skill-kit-audit-test");
|
|
10
|
+
const AGENT_DIR = path.join(TEST_DIR, ".agent");
|
|
11
|
+
|
|
12
|
+
describe("Audit - Governance Check", () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
fs.mkdirSync(AGENT_DIR, { recursive: true });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
fs.rmSync(TEST_DIR, { recursive: true, force: true });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("detects GEMINI.md presence", () => {
|
|
22
|
+
const geminiPath = path.join(AGENT_DIR, "GEMINI.md");
|
|
23
|
+
|
|
24
|
+
// Before creation
|
|
25
|
+
expect(fs.existsSync(geminiPath)).toBe(false);
|
|
26
|
+
|
|
27
|
+
// After creation
|
|
28
|
+
fs.writeFileSync(geminiPath, "# Test", "utf8");
|
|
29
|
+
expect(fs.existsSync(geminiPath)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("detects ARCHITECTURE.md presence", () => {
|
|
33
|
+
const archPath = path.join(AGENT_DIR, "ARCHITECTURE.md");
|
|
34
|
+
fs.writeFileSync(archPath, "# Architecture", "utf8");
|
|
35
|
+
|
|
36
|
+
expect(fs.existsSync(archPath)).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("counts skills in skills directory", () => {
|
|
40
|
+
const skillsDir = path.join(AGENT_DIR, "skills");
|
|
41
|
+
fs.mkdirSync(skillsDir, { recursive: true });
|
|
42
|
+
|
|
43
|
+
// Create mock skills
|
|
44
|
+
fs.mkdirSync(path.join(skillsDir, "skill-1"));
|
|
45
|
+
fs.mkdirSync(path.join(skillsDir, "skill-2"));
|
|
46
|
+
|
|
47
|
+
const skills = fs.readdirSync(skillsDir).filter(f =>
|
|
48
|
+
fs.statSync(path.join(skillsDir, f)).isDirectory()
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(skills.length).toBe(2);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("Audit - Compliance Results", () => {
|
|
56
|
+
it("calculates pass/fail status correctly", () => {
|
|
57
|
+
const results = {
|
|
58
|
+
errors: 0,
|
|
59
|
+
warnings: 2,
|
|
60
|
+
total: 2
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// Errors = 0 means pass
|
|
64
|
+
expect(results.errors === 0).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("fails when errors exist", () => {
|
|
68
|
+
const results = {
|
|
69
|
+
errors: 1,
|
|
70
|
+
warnings: 0,
|
|
71
|
+
total: 1
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
expect(results.errors > 0).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("calculates total correctly", () => {
|
|
78
|
+
const violations = [
|
|
79
|
+
{ lesson: { severity: "ERROR" }, matches: [1, 2] },
|
|
80
|
+
{ lesson: { severity: "WARNING" }, matches: [1] }
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
let total = 0;
|
|
84
|
+
let errors = 0;
|
|
85
|
+
let warnings = 0;
|
|
86
|
+
|
|
87
|
+
violations.forEach(v => {
|
|
88
|
+
total += v.matches.length;
|
|
89
|
+
if (v.lesson.severity === "ERROR") {
|
|
90
|
+
errors += v.matches.length;
|
|
91
|
+
} else {
|
|
92
|
+
warnings += v.matches.length;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(total).toBe(3);
|
|
97
|
+
expect(errors).toBe(2);
|
|
98
|
+
expect(warnings).toBe(1);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Smart Auto-Learn - True Self-Learning Engine
|
|
4
|
+
*
|
|
5
|
+
* Automatically learns from:
|
|
6
|
+
* 1. ESLint output (runs ESLint and parses)
|
|
7
|
+
* 2. Test failures (runs tests and parses)
|
|
8
|
+
* 3. TypeScript errors (runs tsc and parses)
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* ag-smart auto-learn # Run all
|
|
12
|
+
* ag-smart auto-learn --eslint # ESLint only
|
|
13
|
+
* ag-smart auto-learn --test # Test failures only
|
|
14
|
+
* ag-smart auto-learn --typescript # TypeScript only
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import fs from "fs";
|
|
18
|
+
import path from "path";
|
|
19
|
+
import { execSync, spawnSync } from "child_process";
|
|
20
|
+
import { loadKnowledge, saveKnowledge } from "./recall.js";
|
|
21
|
+
import { VERSION } from "./config.js";
|
|
22
|
+
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// ESLINT AUTO-LEARN
|
|
25
|
+
// ============================================================================
|
|
26
|
+
|
|
27
|
+
const ESLINT_PATTERN_MAP = {
|
|
28
|
+
"no-console": { pattern: "console\\.(log|warn|error|info|debug)", message: "Avoid console statements in production" },
|
|
29
|
+
"no-debugger": { pattern: "\\bdebugger\\b", message: "Remove debugger statements" },
|
|
30
|
+
"no-var": { pattern: "\\bvar\\s+\\w", message: "Use const/let instead of var" },
|
|
31
|
+
"eqeqeq": { pattern: "[^!=]==[^=]", message: "Use === instead of ==" },
|
|
32
|
+
"no-eval": { pattern: "\\beval\\s*\\(", message: "Avoid eval() for security" },
|
|
33
|
+
"no-alert": { pattern: "\\balert\\s*\\(", message: "Avoid alert() in production" },
|
|
34
|
+
"no-implicit-globals": { pattern: "^(?!const|let|var|function|class|import|export)", message: "Avoid implicit globals" },
|
|
35
|
+
"prefer-const": { pattern: "\\blet\\s+\\w+\\s*=\\s*[^;]+;(?![\\s\\S]*\\1\\s*=)", message: "Use const for variables that are never reassigned" },
|
|
36
|
+
"no-unused-expressions": { pattern: "^\\s*['\"`]use strict['\"`];?\\s*$", message: "Remove unused expressions" },
|
|
37
|
+
"semi": { pattern: "[^;{}]\\s*$", message: "Missing semicolon" },
|
|
38
|
+
"quotes": { pattern: '(?<![\\w])"(?![^"]*"[,\\]\\}\\)])', message: "Prefer single quotes" },
|
|
39
|
+
"no-empty": { pattern: "\\{\\s*\\}", message: "Empty block statement" },
|
|
40
|
+
"no-extra-semi": { pattern: ";;", message: "Unnecessary semicolon" },
|
|
41
|
+
"no-unreachable": { pattern: "(?:return|throw|break|continue)[^}]*[\\n\\r]+[^}]+", message: "Unreachable code" }
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function runEslintAutoLearn(projectRoot) {
|
|
45
|
+
console.log("\n🔍 Running ESLint analysis...\n");
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
// Try to run ESLint with JSON output
|
|
49
|
+
const result = spawnSync("npx", ["eslint", ".", "--format", "json", "--no-error-on-unmatched-pattern"], {
|
|
50
|
+
cwd: projectRoot,
|
|
51
|
+
encoding: "utf8",
|
|
52
|
+
shell: true,
|
|
53
|
+
timeout: 60000
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!result.stdout) {
|
|
57
|
+
console.log(" ℹ️ ESLint not configured or no files to lint.");
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const eslintResults = JSON.parse(result.stdout);
|
|
62
|
+
const ruleViolations = {};
|
|
63
|
+
|
|
64
|
+
eslintResults.forEach(file => {
|
|
65
|
+
if (!file.messages) return;
|
|
66
|
+
file.messages.forEach(msg => {
|
|
67
|
+
if (!msg.ruleId) return;
|
|
68
|
+
if (!ruleViolations[msg.ruleId]) {
|
|
69
|
+
ruleViolations[msg.ruleId] = {
|
|
70
|
+
rule: msg.ruleId,
|
|
71
|
+
count: 0,
|
|
72
|
+
severity: msg.severity === 2 ? "ERROR" : "WARNING",
|
|
73
|
+
sample: msg.message
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
ruleViolations[msg.ruleId].count++;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return Object.values(ruleViolations).sort((a, b) => b.count - a.count);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
console.log(" ⚠️ ESLint analysis skipped:", e.message);
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ============================================================================
|
|
88
|
+
// TEST FAILURE AUTO-LEARN
|
|
89
|
+
// ============================================================================
|
|
90
|
+
|
|
91
|
+
const TEST_PATTERN_MAP = {
|
|
92
|
+
"undefined is not a function": { pattern: "\\w+\\s*\\(", message: "Function may be undefined before call" },
|
|
93
|
+
"cannot read property": { pattern: "\\w+\\.\\w+", message: "Object property access may fail if undefined" },
|
|
94
|
+
"null is not an object": { pattern: "\\.\\w+", message: "Null check needed before property access" },
|
|
95
|
+
"expected.*to equal": { pattern: "expect\\s*\\(", message: "Assertion expectation mismatch" },
|
|
96
|
+
"timeout": { pattern: "async|await|Promise", message: "Async operation may need longer timeout" },
|
|
97
|
+
"not defined": { pattern: "\\b\\w+\\b", message: "Variable may not be defined in scope" }
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
function runTestAutoLearn(projectRoot) {
|
|
101
|
+
console.log("\n🧪 Running test analysis...\n");
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
// Try common test runners
|
|
105
|
+
const testCommands = [
|
|
106
|
+
{ cmd: "npm", args: ["test", "--", "--json", "--passWithNoTests"], name: "npm test" },
|
|
107
|
+
{ cmd: "npx", args: ["vitest", "run", "--reporter=json"], name: "vitest" },
|
|
108
|
+
{ cmd: "npx", args: ["jest", "--json", "--passWithNoTests"], name: "jest" }
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
for (const tc of testCommands) {
|
|
112
|
+
const result = spawnSync(tc.cmd, tc.args, {
|
|
113
|
+
cwd: projectRoot,
|
|
114
|
+
encoding: "utf8",
|
|
115
|
+
shell: true,
|
|
116
|
+
timeout: 120000
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Look for test failures in output
|
|
120
|
+
const output = result.stdout + result.stderr;
|
|
121
|
+
const failures = [];
|
|
122
|
+
|
|
123
|
+
// Parse failure messages
|
|
124
|
+
const failurePatterns = [
|
|
125
|
+
/FAIL\s+(.+)/g,
|
|
126
|
+
/✕\s+(.+)/g,
|
|
127
|
+
/Error:\s+(.+)/g,
|
|
128
|
+
/AssertionError:\s+(.+)/g
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
failurePatterns.forEach(regex => {
|
|
132
|
+
let match;
|
|
133
|
+
while ((match = regex.exec(output)) !== null) {
|
|
134
|
+
failures.push(match[1].trim());
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
if (failures.length > 0) {
|
|
139
|
+
console.log(` Found ${failures.length} test failure(s) from ${tc.name}`);
|
|
140
|
+
return failures.map(f => ({ message: f, source: tc.name }));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
console.log(" ✅ All tests passing or no tests found.");
|
|
145
|
+
return [];
|
|
146
|
+
} catch (e) {
|
|
147
|
+
console.log(" ⚠️ Test analysis skipped:", e.message);
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ============================================================================
|
|
153
|
+
// TYPESCRIPT ERROR AUTO-LEARN
|
|
154
|
+
// ============================================================================
|
|
155
|
+
|
|
156
|
+
const TS_PATTERN_MAP = {
|
|
157
|
+
"TS2304": { pattern: "\\b\\w+\\b", message: "TypeScript: Cannot find name" },
|
|
158
|
+
"TS2322": { pattern: ":\\s*\\w+", message: "TypeScript: Type mismatch" },
|
|
159
|
+
"TS2345": { pattern: "\\(.*\\)", message: "TypeScript: Argument type mismatch" },
|
|
160
|
+
"TS2339": { pattern: "\\.\\w+", message: "TypeScript: Property does not exist" },
|
|
161
|
+
"TS7006": { pattern: "\\(\\w+\\)", message: "TypeScript: Parameter implicitly has 'any' type" },
|
|
162
|
+
"TS2531": { pattern: "\\w+\\.", message: "TypeScript: Object is possibly 'null'" },
|
|
163
|
+
"TS2532": { pattern: "\\w+\\.", message: "TypeScript: Object is possibly 'undefined'" }
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
function runTypescriptAutoLearn(projectRoot) {
|
|
167
|
+
console.log("\n📘 Running TypeScript analysis...\n");
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
const result = spawnSync("npx", ["tsc", "--noEmit", "--pretty", "false"], {
|
|
171
|
+
cwd: projectRoot,
|
|
172
|
+
encoding: "utf8",
|
|
173
|
+
shell: true,
|
|
174
|
+
timeout: 60000
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const output = result.stdout + result.stderr;
|
|
178
|
+
const errors = {};
|
|
179
|
+
|
|
180
|
+
// Parse TS errors: file(line,col): error TS1234: message
|
|
181
|
+
const tsErrorRegex = /error (TS\d+):\s*(.+)/g;
|
|
182
|
+
let match;
|
|
183
|
+
|
|
184
|
+
while ((match = tsErrorRegex.exec(output)) !== null) {
|
|
185
|
+
const code = match[1];
|
|
186
|
+
const message = match[2];
|
|
187
|
+
|
|
188
|
+
if (!errors[code]) {
|
|
189
|
+
errors[code] = { code, message, count: 0 };
|
|
190
|
+
}
|
|
191
|
+
errors[code].count++;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const errorList = Object.values(errors).sort((a, b) => b.count - a.count);
|
|
195
|
+
|
|
196
|
+
if (errorList.length > 0) {
|
|
197
|
+
console.log(` Found ${errorList.length} unique TypeScript error type(s)`);
|
|
198
|
+
} else {
|
|
199
|
+
console.log(" ✅ No TypeScript errors found.");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return errorList;
|
|
203
|
+
} catch (e) {
|
|
204
|
+
console.log(" ⚠️ TypeScript analysis skipped:", e.message);
|
|
205
|
+
return [];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ============================================================================
|
|
210
|
+
// MAIN AUTO-LEARN ENGINE
|
|
211
|
+
// ============================================================================
|
|
212
|
+
|
|
213
|
+
function autoLearn(projectRoot, options = {}) {
|
|
214
|
+
console.log(`\n🧠 Smart Auto-Learn Engine v${VERSION}`);
|
|
215
|
+
console.log(`📂 Project: ${projectRoot}\n`);
|
|
216
|
+
console.log("─".repeat(50));
|
|
217
|
+
|
|
218
|
+
const db = loadKnowledge();
|
|
219
|
+
let totalAdded = 0;
|
|
220
|
+
|
|
221
|
+
// ESLint
|
|
222
|
+
if (!options.onlyTest && !options.onlyTypescript) {
|
|
223
|
+
const eslintViolations = runEslintAutoLearn(projectRoot);
|
|
224
|
+
|
|
225
|
+
eslintViolations.forEach(v => {
|
|
226
|
+
const mapping = ESLINT_PATTERN_MAP[v.rule];
|
|
227
|
+
if (!mapping) return;
|
|
228
|
+
|
|
229
|
+
// Check if already exists
|
|
230
|
+
if (db.lessons.some(l => l.pattern === mapping.pattern)) return;
|
|
231
|
+
|
|
232
|
+
const id = `AUTO-${String(db.lessons.length + 1).padStart(3, "0")}`;
|
|
233
|
+
db.lessons.push({
|
|
234
|
+
id,
|
|
235
|
+
pattern: mapping.pattern,
|
|
236
|
+
message: `${mapping.message} (ESLint: ${v.rule})`,
|
|
237
|
+
severity: v.severity,
|
|
238
|
+
source: "auto-eslint",
|
|
239
|
+
hitCount: v.count,
|
|
240
|
+
autoEscalated: false,
|
|
241
|
+
addedAt: new Date().toISOString()
|
|
242
|
+
});
|
|
243
|
+
totalAdded++;
|
|
244
|
+
console.log(` ✅ Auto-learned: [${id}] ${v.rule} (${v.count} occurrences)`);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// TypeScript
|
|
249
|
+
if (!options.onlyEslint && !options.onlyTest) {
|
|
250
|
+
const tsErrors = runTypescriptAutoLearn(projectRoot);
|
|
251
|
+
|
|
252
|
+
tsErrors.slice(0, 5).forEach(e => { // Top 5 only
|
|
253
|
+
const mapping = TS_PATTERN_MAP[e.code];
|
|
254
|
+
if (!mapping) return;
|
|
255
|
+
|
|
256
|
+
if (db.lessons.some(l => l.message.includes(e.code))) return;
|
|
257
|
+
|
|
258
|
+
const id = `AUTO-${String(db.lessons.length + 1).padStart(3, "0")}`;
|
|
259
|
+
db.lessons.push({
|
|
260
|
+
id,
|
|
261
|
+
pattern: mapping.pattern,
|
|
262
|
+
message: `${mapping.message} (${e.code})`,
|
|
263
|
+
severity: "WARNING",
|
|
264
|
+
source: "auto-typescript",
|
|
265
|
+
hitCount: e.count,
|
|
266
|
+
autoEscalated: false,
|
|
267
|
+
addedAt: new Date().toISOString()
|
|
268
|
+
});
|
|
269
|
+
totalAdded++;
|
|
270
|
+
console.log(` ✅ Auto-learned: [${id}] ${e.code} (${e.count} occurrences)`);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Summary
|
|
275
|
+
console.log("\n" + "─".repeat(50));
|
|
276
|
+
|
|
277
|
+
if (totalAdded > 0) {
|
|
278
|
+
saveKnowledge(db);
|
|
279
|
+
console.log(`\n🎓 Auto-learned ${totalAdded} new pattern(s)!`);
|
|
280
|
+
console.log(`📊 Total lessons in memory: ${db.lessons.length}\n`);
|
|
281
|
+
} else {
|
|
282
|
+
console.log("\n✅ No new patterns discovered. Code looks clean!\n");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return totalAdded;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ============================================================================
|
|
289
|
+
// CLI
|
|
290
|
+
// ============================================================================
|
|
291
|
+
|
|
292
|
+
const args = process.argv.slice(2);
|
|
293
|
+
const projectRoot = process.cwd();
|
|
294
|
+
|
|
295
|
+
if (args.includes("--help")) {
|
|
296
|
+
console.log(`
|
|
297
|
+
🧠 Smart Auto-Learn - True Self-Learning Engine
|
|
298
|
+
|
|
299
|
+
Usage:
|
|
300
|
+
ag-smart auto-learn Run all analyzers
|
|
301
|
+
ag-smart auto-learn --eslint ESLint only
|
|
302
|
+
ag-smart auto-learn --typescript TypeScript only
|
|
303
|
+
ag-smart auto-learn --test Test failures only
|
|
304
|
+
|
|
305
|
+
The engine automatically:
|
|
306
|
+
1. Runs ESLint and learns from violations
|
|
307
|
+
2. Runs TypeScript and learns from type errors
|
|
308
|
+
3. Runs tests and learns from failures
|
|
309
|
+
|
|
310
|
+
Patterns are automatically added to knowledge base!
|
|
311
|
+
`);
|
|
312
|
+
process.exit(0);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
autoLearn(projectRoot, {
|
|
316
|
+
onlyEslint: args.includes("--eslint"),
|
|
317
|
+
onlyTypescript: args.includes("--typescript"),
|
|
318
|
+
onlyTest: args.includes("--test")
|
|
319
|
+
});
|