-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathconfirm-hooks.mjs
More file actions
432 lines (353 loc) · 15.7 KB
/
confirm-hooks.mjs
File metadata and controls
432 lines (353 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env node
/**
* Comprehensive functionality confirmation for Hook Tools
*/
import * as path from 'path';
import * as fs from 'fs';
console.log('🔬 Hook Tools Functionality Confirmation\n');
console.log('='.repeat(70));
// Setup
const INTEL_PATH = '.agentic-flow/intelligence.json';
const AGENTS_PATH = '.claude/agents';
// Clean previous test data
if (fs.existsSync('.agentic-flow')) fs.rmSync('.agentic-flow', { recursive: true });
if (fs.existsSync(AGENTS_PATH)) fs.rmSync(AGENTS_PATH, { recursive: true });
// ============================================================================
// SHARED UTILITIES (inline for testing)
// ============================================================================
const agentMapping = {
'.rs': 'rust-developer',
'.ts': 'typescript-developer',
'.tsx': 'react-developer',
'.js': 'javascript-developer',
'.py': 'python-developer',
'.go': 'go-developer',
'.sql': 'database-specialist',
'.md': 'documentation-specialist',
'.json': 'config-specialist',
'.yaml': 'config-specialist'
};
function getAgentForFile(filePath) {
const ext = path.extname(filePath);
if (filePath.includes('.test.') || filePath.includes('.spec.') || filePath.includes('_test.')) {
return 'test-engineer';
}
if (filePath.includes('.github/workflows')) return 'cicd-engineer';
if (agentMapping[path.basename(filePath)]) return agentMapping[path.basename(filePath)];
if (agentMapping[ext]) return agentMapping[ext];
return 'coder';
}
function loadIntelligence() {
try {
const fullPath = path.join(process.cwd(), INTEL_PATH);
if (fs.existsSync(fullPath)) {
return JSON.parse(fs.readFileSync(fullPath, 'utf-8'));
}
} catch (e) {}
return { patterns: {}, sequences: {}, memories: [], dirPatterns: {}, errorPatterns: [], metrics: { totalRoutes: 0, successfulRoutes: 0, routingHistory: [] } };
}
function saveIntelligence(data) {
const fullPath = path.join(process.cwd(), INTEL_PATH);
const dir = path.dirname(fullPath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(fullPath, JSON.stringify(data, null, 2));
}
function simpleEmbed(text) {
const embedding = new Array(64).fill(0);
const words = text.toLowerCase().split(/\s+/);
for (const word of words) {
for (let i = 0; i < word.length; i++) {
const idx = (word.charCodeAt(i) * (i + 1)) % 64;
embedding[idx] += 1;
}
}
const magnitude = Math.sqrt(embedding.reduce((sum, val) => sum + val * val, 0));
if (magnitude > 0) {
for (let i = 0; i < embedding.length; i++) embedding[i] /= magnitude;
}
return embedding;
}
function cosineSimilarity(a, b) {
if (a.length !== b.length) return 0;
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
const mag = Math.sqrt(normA) * Math.sqrt(normB);
return mag > 0 ? dot / mag : 0;
}
const dangerousPatterns = [/rm\s+-rf\s+\//, /sudo\s+rm/, /chmod\s+777/, /mkfs\./, /dd\s+if=/];
function assessCommandRisk(command) {
let risk = 0;
for (const p of dangerousPatterns) if (p.test(command)) risk = Math.max(risk, 0.9);
if (/sudo/.test(command)) risk = Math.max(risk, 0.5);
if (/rm\s+-/.test(command)) risk = Math.max(risk, 0.4);
return risk;
}
// ============================================================================
// TOOL IMPLEMENTATIONS
// ============================================================================
async function hookPreEdit({ filePath, task }) {
const start = Date.now();
const intel = loadIntelligence();
const ext = path.extname(filePath);
let suggestedAgent = getAgentForFile(filePath);
let confidence = 0.5;
const state = `edit:${ext}`;
if (intel.patterns[state]) {
let best = '', bestScore = 0;
for (const [agent, score] of Object.entries(intel.patterns[state])) {
if (score > bestScore) { bestScore = score; best = agent; }
}
if (bestScore > 0) { suggestedAgent = best; confidence = Math.min(0.9, 0.5 + bestScore / 10); }
}
const relatedFiles = intel.sequences[filePath]?.slice(0, 5) || [];
const memories = [];
if (task && intel.memories.length > 0) {
const taskEmbed = simpleEmbed(task);
for (const mem of intel.memories) {
if (mem.embedding) {
const score = cosineSimilarity(taskEmbed, mem.embedding);
if (score > 0.3) memories.push({ content: mem.content.slice(0, 100), score });
}
}
memories.sort((a, b) => b.score - a.score);
memories.splice(3);
}
return { success: true, suggestedAgent, confidence, relatedFiles, memories, latencyMs: Date.now() - start };
}
async function hookPostEdit({ filePath, success, agent, errorMessage }) {
const start = Date.now();
const intel = loadIntelligence();
const ext = path.extname(filePath);
const state = `edit:${ext}`;
const LEARNING_RATE = 0.1;
if (agent) {
if (!intel.patterns[state]) intel.patterns[state] = {};
const current = intel.patterns[state][agent] || 0;
const reward = success ? 1.0 : -0.3;
intel.patterns[state][agent] = current + LEARNING_RATE * (reward - current);
}
intel.metrics.totalRoutes++;
if (success) intel.metrics.successfulRoutes++;
intel.metrics.routingHistory.push({ timestamp: new Date().toISOString(), task: `edit:${filePath}`, agent: agent || 'unknown', success });
if (intel.metrics.routingHistory.length > 100) intel.metrics.routingHistory = intel.metrics.routingHistory.slice(-100);
if (!success && errorMessage) {
intel.errorPatterns.push({ errorType: errorMessage.split(':')[0], context: `${ext} file edit`, resolution: '', agentSuccess: { [agent || 'unknown']: -1 } });
}
if (success && agent) {
const content = `Successful ${ext} edit by ${agent}`;
intel.memories.push({ content, type: 'success', created: new Date().toISOString(), embedding: simpleEmbed(content) });
if (intel.memories.length > 200) intel.memories = intel.memories.slice(-200);
}
saveIntelligence(intel);
const accuracy = intel.metrics.totalRoutes > 0 ? intel.metrics.successfulRoutes / intel.metrics.totalRoutes : 0;
return { success: true, patternsUpdated: true, routingAccuracy: accuracy, latencyMs: Date.now() - start };
}
async function hookPreCommand({ command }) {
const start = Date.now();
const risk = assessCommandRisk(command);
let category = 'safe';
if (risk >= 0.9) category = 'blocked';
else if (risk >= 0.6) category = 'dangerous';
else if (risk >= 0.3) category = 'caution';
return { success: true, riskLevel: risk, riskCategory: category, approved: risk < 0.7, blocked: risk >= 0.9, latencyMs: Date.now() - start };
}
async function hookRoute({ task, context }) {
const start = Date.now();
const intel = loadIntelligence();
const scores = {};
const agents = ['coder', 'typescript-developer', 'test-engineer', 'security-specialist', 'researcher', 'optimizer'];
agents.forEach(a => scores[a] = 0);
if (context?.file) {
const agent = getAgentForFile(context.file);
scores[agent] = (scores[agent] || 0) + 2.0;
}
const taskLower = task.toLowerCase();
const keywords = {
'test-engineer': ['test', 'spec', 'coverage'],
'security-specialist': ['security', 'auth', 'vulnerability'],
'researcher': ['research', 'find', 'explore'],
'optimizer': ['optimize', 'performance', 'speed']
};
for (const [agent, words] of Object.entries(keywords)) {
for (const w of words) if (taskLower.includes(w)) scores[agent] = (scores[agent] || 0) + 1.5;
}
let best = 'coder', maxScore = 0;
for (const [agent, score] of Object.entries(scores)) {
if (score > maxScore) { maxScore = score; best = agent; }
}
const total = Object.values(scores).reduce((a, b) => a + b, 0);
const confidence = total > 0 ? maxScore / total : 0.5;
return { success: true, agent: best, confidence, score: maxScore, latencyMs: Date.now() - start };
}
async function hookMetrics({ timeframe }) {
const start = Date.now();
const intel = loadIntelligence();
const accuracy = intel.metrics.totalRoutes > 0 ? intel.metrics.successfulRoutes / intel.metrics.totalRoutes : 0;
return {
success: true,
timeframe,
routing: { accuracy, total: intel.metrics.totalRoutes, successful: intel.metrics.successfulRoutes },
patterns: Object.keys(intel.patterns).length,
memories: intel.memories.length,
errors: intel.errorPatterns.length,
latencyMs: Date.now() - start
};
}
async function hookPretrain({ depth = 10, skipGit = true }) {
const start = Date.now();
const intel = loadIntelligence();
const stats = { files: 0, patterns: 0, memories: 0 };
// Simulate file analysis
const testFiles = ['src/index.ts', 'src/auth.ts', 'lib/utils.rs', 'tests/auth.test.ts', 'README.md'];
for (const file of testFiles) {
stats.files++;
const ext = path.extname(file);
const agent = getAgentForFile(file);
const state = `edit:${ext || 'unknown'}`;
if (!intel.patterns[state]) intel.patterns[state] = {};
intel.patterns[state][agent] = (intel.patterns[state][agent] || 0) + 0.3;
stats.patterns++;
}
// Create memories
intel.memories.push({ content: '[Test] Pretrain memory entry', type: 'project', created: new Date().toISOString(), embedding: simpleEmbed('test pretrain') });
stats.memories++;
intel.pretrained = { date: new Date().toISOString(), stats };
saveIntelligence(intel);
return { success: true, filesAnalyzed: stats.files, patternsCreated: stats.patterns, memoriesStored: stats.memories, latencyMs: Date.now() - start };
}
async function hookBuildAgents({ focus = 'quality', output = '.claude/agents' }) {
const start = Date.now();
const intel = loadIntelligence();
const agents = [];
// Detect from patterns
const langs = new Set();
for (const state of Object.keys(intel.patterns)) {
if (state.includes('.ts')) langs.add('typescript');
if (state.includes('.rs')) langs.add('rust');
}
if (langs.has('typescript')) {
agents.push({ name: 'typescript-specialist', type: 'typescript-developer', capabilities: ['types', 'async'] });
}
if (langs.has('rust')) {
agents.push({ name: 'rust-specialist', type: 'rust-developer', capabilities: ['cargo', 'wasm'] });
}
agents.push({ name: 'project-coordinator', type: 'coordinator', capabilities: ['routing'] });
// Save to output directory
const outDir = path.join(process.cwd(), output);
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
for (const agent of agents) {
const content = `# ${agent.name}\ntype: ${agent.type}\ncapabilities:\n${agent.capabilities.map(c => ` - ${c}`).join('\n')}\n`;
fs.writeFileSync(path.join(outDir, `${agent.name}.yaml`), content);
}
return { success: true, focus, agentsGenerated: agents.length, agents: agents.map(a => a.name), latencyMs: Date.now() - start };
}
// ============================================================================
// RUN TESTS
// ============================================================================
async function runTests() {
const results = [];
let passed = 0, failed = 0;
function check(name, condition, details = '') {
if (condition) {
console.log(`✅ ${name}${details ? ` (${details})` : ''}`);
passed++;
results.push({ name, passed: true });
} else {
console.log(`❌ ${name}${details ? ` - ${details}` : ''}`);
failed++;
results.push({ name, passed: false });
}
}
// 1. Pre-Edit Tests
console.log('\n📝 hook_pre_edit');
console.log('-'.repeat(50));
let r = await hookPreEdit({ filePath: 'src/index.ts' });
check('Returns success', r.success);
check('Suggests TypeScript agent', r.suggestedAgent === 'typescript-developer');
check('Has confidence > 0', r.confidence > 0);
check('Latency < 50ms', r.latencyMs < 50, `${r.latencyMs}ms`);
r = await hookPreEdit({ filePath: 'tests/auth.test.ts' });
check('Detects test file', r.suggestedAgent === 'test-engineer');
// 2. Post-Edit Tests
console.log('\n📝 hook_post_edit');
console.log('-'.repeat(50));
r = await hookPostEdit({ filePath: 'src/a.ts', success: true, agent: 'typescript-developer' });
check('Records success', r.success && r.patternsUpdated);
r = await hookPostEdit({ filePath: 'src/b.ts', success: true, agent: 'typescript-developer' });
r = await hookPostEdit({ filePath: 'src/c.ts', success: false, agent: 'coder', errorMessage: 'TypeError: undefined' });
check('Calculates accuracy', Math.abs(r.routingAccuracy - 0.67) < 0.1, `${(r.routingAccuracy * 100).toFixed(0)}%`);
// 3. Pre-Command Tests
console.log('\n🔒 hook_pre_command');
console.log('-'.repeat(50));
r = await hookPreCommand({ command: 'npm test' });
check('Approves safe command', r.approved && r.riskCategory === 'safe');
r = await hookPreCommand({ command: 'rm -rf /' });
check('Blocks dangerous command', r.blocked && r.riskCategory === 'blocked');
r = await hookPreCommand({ command: 'sudo apt install vim' });
check('Warns about sudo', r.riskCategory === 'caution', r.riskCategory);
// 4. Route Tests
console.log('\n🧭 hook_route');
console.log('-'.repeat(50));
r = await hookRoute({ task: 'Write unit tests for auth' });
check('Routes test task', r.agent === 'test-engineer');
r = await hookRoute({ task: 'Fix security vulnerability', context: { file: 'src/auth.ts' } });
check('Uses context + keywords', r.confidence > 0);
r = await hookRoute({ task: 'Optimize database queries' });
check('Routes optimizer task', r.agent === 'optimizer');
// 5. Metrics Tests
console.log('\n📊 hook_metrics');
console.log('-'.repeat(50));
r = await hookMetrics({ timeframe: '24h' });
check('Returns metrics', r.success && r.routing !== undefined);
check('Tracks routes', r.routing.total === 3, `${r.routing.total} routes`);
// 6. Pretrain Tests
console.log('\n🧠 hook_pretrain');
console.log('-'.repeat(50));
r = await hookPretrain({ depth: 10, skipGit: true });
check('Analyzes files', r.filesAnalyzed > 0, `${r.filesAnalyzed} files`);
check('Creates patterns', r.patternsCreated > 0, `${r.patternsCreated} patterns`);
check('Stores memories', r.memoriesStored > 0);
// 7. Build-Agents Tests
console.log('\n🏗️ hook_build_agents');
console.log('-'.repeat(50));
r = await hookBuildAgents({ focus: 'quality' });
check('Generates agents', r.agentsGenerated > 0, `${r.agentsGenerated} agents`);
check('Includes coordinator', r.agents.includes('project-coordinator'));
check('Creates files', fs.existsSync('.claude/agents/project-coordinator.yaml'));
// 8. Learning Feedback Loop
console.log('\n🔄 Learning Feedback Loop');
console.log('-'.repeat(50));
// Pre-edit should now use learned patterns
r = await hookPreEdit({ filePath: 'src/new.ts' });
check('Uses learned patterns', r.confidence > 0.5, `confidence: ${r.confidence.toFixed(2)}`);
// 9. Performance Summary
console.log('\n⚡ Performance');
console.log('-'.repeat(50));
const iterations = 50;
const latencies = [];
for (let i = 0; i < iterations; i++) {
const result = await hookPreEdit({ filePath: `file${i}.ts` });
latencies.push(result.latencyMs);
}
const avg = latencies.reduce((a, b) => a + b, 0) / iterations;
const p95 = latencies.sort((a, b) => a - b)[Math.floor(iterations * 0.95)];
check(`Pre-edit avg < 10ms`, avg < 10, `avg=${avg.toFixed(2)}ms`);
check(`Pre-edit p95 < 20ms`, p95 < 20, `p95=${p95.toFixed(2)}ms`);
// Summary
console.log('\n' + '='.repeat(70));
console.log(`\n📊 RESULTS: ${passed}/${passed + failed} tests passed`);
if (failed === 0) {
console.log('\n🎉 ALL FUNCTIONALITY CONFIRMED!\n');
} else {
console.log(`\n⚠️ ${failed} test(s) need attention\n`);
}
// Cleanup
if (fs.existsSync('.agentic-flow')) fs.rmSync('.agentic-flow', { recursive: true });
if (fs.existsSync('.claude/agents')) fs.rmSync('.claude/agents', { recursive: true });
return { passed, failed, results };
}
runTests().catch(console.error);