pikakit 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -6
- package/bin/kit.mjs +6 -2
- package/bin/lib/commands/help.js +13 -8
- package/bin/lib/config.js +4 -2
- package/lib/agent-cli/lib/ab-testing.js +508 -0
- package/lib/agent-cli/lib/causality-engine.js +623 -0
- package/lib/agent-cli/lib/dashboard-data.js +365 -0
- package/lib/agent-cli/lib/fix.js +1 -1
- package/lib/agent-cli/lib/metrics-collector.js +523 -0
- package/lib/agent-cli/lib/metrics-schema.js +410 -0
- package/lib/agent-cli/lib/precision-skill-generator.js +584 -0
- package/lib/agent-cli/lib/recall.js +1 -1
- package/lib/agent-cli/lib/reinforcement.js +610 -0
- package/lib/agent-cli/lib/ui/index.js +37 -14
- package/package.json +4 -2
- package/lib/agent-cli/lib/auto-learn.js +0 -319
- package/lib/agent-cli/scripts/adaptive_engine.js +0 -381
- package/lib/agent-cli/scripts/error_sensor.js +0 -565
- package/lib/agent-cli/scripts/learn_from_failure.js +0 -225
- package/lib/agent-cli/scripts/pattern_analyzer.js +0 -781
- package/lib/agent-cli/scripts/skill_injector.js +0 -387
- package/lib/agent-cli/scripts/success_sensor.js +0 -500
- package/lib/agent-cli/scripts/user_correction_sensor.js +0 -426
- package/lib/agent-cli/services/auto-learn-service.js +0 -247
|
@@ -1,381 +0,0 @@
|
|
|
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 };
|