-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel-integration.test.ts
More file actions
300 lines (223 loc) · 10.8 KB
/
kernel-integration.test.ts
File metadata and controls
300 lines (223 loc) · 10.8 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
/**
* Kernel Integration Tests
*
* Comprehensive test suite for kernel v2.0.0 integration
* Tests pattern detection, agent delegation, routing, and orchestration
*
* @version 2.0.0-SECURITY-ENHANCED
*/
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import { KernelAnalyzer, getKernel, resetKernel } from '../core/kernel-patterns.js';
// Test data for pattern matching - must match kernel's actual pattern triggers
const TEST_OBSERVATIONS = {
securityVulnerability: 'P6 security_vulnerability detected in authentication system',
releaseReadiness: 'P7 precommit_fails blocking release process',
infrastructureHardening: 'P8 execution_failures due to chmod+typecheck issues',
securityOptional: 'security optional until after feature completion',
worksLocallySecure: 'code works in dev and works locally',
optimizationOver75: 'Attempting optimization beyond 75% threshold',
infiniteLoop: 'Process enters infinite recursive loop without termination',
implementationDrift: 'Code changes without test updates cause hidden bugs',
consumerPathTrap: 'Code works with dist/ but fails with consumer paths',
};
describe('Kernel Integration', () => {
let kernel: KernelAnalyzer;
beforeAll(() => {
// Reset to ensure clean state
resetKernel();
kernel = getKernel();
});
afterAll(() => {
resetKernel();
});
describe('Kernel Pattern Detection', () => {
describe('P6: Security Vulnerability Pattern', () => {
it('should detect security vulnerability when H-005 mentioned', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityVulnerability);
expect(result.confidence).toBeGreaterThan(0.8);
expect(result.level).toBeDefined();
expect(result.cascadePatterns).toBeDefined();
expect(result.cascadePatterns!.length).toBeGreaterThan(0);
expect(result.cascadePatterns!.some(p => p.id === 'P6')).toBe(true);
expect(result.actionRequired).toBeDefined();
});
it('should recommend security transformation actions', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityVulnerability);
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
});
describe('P7: Release Readiness Pattern', () => {
it('should detect release readiness when validation blocks', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.releaseReadiness);
expect(result.confidence).toBeGreaterThan(0.8);
expect(result.level).toBeDefined();
expect(result.cascadePatterns).toBeDefined();
expect(result.cascadePatterns!.some(p => p.id === 'P7')).toBe(true);
expect(result.actionRequired).toBeDefined();
});
it('should recommend comprehensive validation', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.releaseReadiness);
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
});
describe('P8: Infrastructure Hardening Pattern', () => {
it('should detect infrastructure issues when scripts fail', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.infrastructureHardening);
expect(result.confidence).toBeGreaterThan(0.8);
expect(result.level).toBeDefined();
expect(result.cascadePatterns).toBeDefined();
expect(result.cascadePatterns!.some(p => p.id === 'P8')).toBe(true);
expect(result.actionRequired).toBeDefined();
});
it('should recommend script permission fixes', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.infrastructureHardening);
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
});
describe('A8: Security Foundation Pattern', () => {
it('should detect optional security assumptions', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityOptional);
expect(result.fatalAssumptions).toBeDefined();
expect(result.fatalAssumptions!.length).toBeGreaterThan(0);
expect(result.fatalAssumptions!.some(a => a.id === 'A8')).toBe(true);
expect(result.level).toBe('L3'); // Assumption surfacing
expect(result.actionRequired).toBeDefined();
});
it('should provide security foundation guidance', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityOptional);
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
});
describe('A9: Production Environment Testing Pattern', () => {
it('should detect local vs production assumptions', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.worksLocallySecure);
expect(result.fatalAssumptions).toBeDefined();
expect(result.fatalAssumptions!.length).toBeGreaterThan(0);
expect(result.fatalAssumptions!.some(a => a.id === 'A9')).toBe(true);
expect(result.level).toBe('L3');
expect(result.actionRequired).toBeDefined();
});
it('should recommend production testing', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.worksLocallySecure);
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
});
});
describe('Kernel Confidence Scoring', () => {
it('should provide high confidence for pattern matches', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityVulnerability);
expect(result.confidence).toBeGreaterThan(0.7);
});
it('should provide moderate confidence for related patterns', async () => {
const result = kernel.analyze('Optimization over 75% threshold');
expect(result.confidence).toBeGreaterThan(0.5);
});
it('should provide low confidence for uncertain patterns', async () => {
const result = kernel.analyze('Unknown security situation');
expect(result.confidence).toBeLessThan(0.8);
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
it('should respect confidence threshold configuration', async () => {
kernel.updateConfig({ confidenceThreshold: 0.8 });
const result = kernel.analyze('Uncertain pattern');
expect(result.recommendations).toBeDefined();
// Reset config
kernel.updateConfig({ confidenceThreshold: 0.75 });
});
});
describe('Inference Levels (L1-L5)', () => {
it('should return L1 for pattern recognition', async () => {
const result = kernel.analyze('Simple task with clear pattern');
expect(result.level).toBeDefined();
expect(['L1', 'L2', 'L3', 'L4', 'L5']).toContain(result.level);
});
it('should return L2 for causal analysis', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityVulnerability);
expect(result.level).toBeDefined();
});
it('should return L3 for assumption surfacing', async () => {
const result = kernel.analyze(TEST_OBSERVATIONS.securityOptional);
expect(result.level).toBe('L3');
});
it('should return L4 for counterfactual thinking', async () => {
const result = kernel.analyze('Works locally but fails in production');
expect(result.level).toBeDefined();
});
it('should return L5 for meta-inference', async () => {
const result = kernel.analyze('Complex task with unknown requirements');
expect(result.level).toBeDefined();
});
});
describe('Kernel Learning & Confidence Adjustment', () => {
it('should learn from successful pattern usage', async () => {
kernel.learn({
success: true,
patternUsed: 'P6',
feedback: 'Security transformation successful'
});
// Learning updates internal pattern confidence
// The kernel analyze() uses cascades, so this tests that learn() doesn't error
const result = kernel.analyze('P6 security_vulnerability detected');
expect(result.confidence).toBeGreaterThan(0.7);
});
it('should learn from failed pattern usage', async () => {
kernel.learn({
success: false,
patternUsed: 'P8',
feedback: 'Script fix failed, still experiencing issues'
});
const result = kernel.analyze('P8 execution_failures detected');
expect(result.confidence).toBeDefined();
});
it('should not adjust confidence without explicit learning', () => {
const config = kernel.getConfig();
const originalConfidence = config.confidenceThreshold;
kernel.analyze('Any observation');
const newConfig = kernel.getConfig();
expect(newConfig.confidenceThreshold).toEqual(originalConfidence);
});
});
describe('Kernel Edge Cases', () => {
it('should handle unknown patterns gracefully', async () => {
const result = kernel.analyze('Completely unknown situation');
expect(result.confidence).toBeDefined();
expect(result.recommendations).toBeDefined();
expect(result.recommendations!.length).toBeGreaterThan(0);
});
it('should handle multiple pattern matches', async () => {
const result = kernel.analyze('Security issue discovered while optimizing');
expect(result.cascadePatterns).toBeDefined();
expect(result.cascadePatterns!.length).toBeGreaterThanOrEqual(0);
});
it('should respect disabled kernel', async () => {
kernel.updateConfig({ enabled: false });
const result = kernel.analyze('Security vulnerability');
expect(result.confidence).toBe(0);
expect(result.recommendations).toContain('Kernel is disabled');
// Re-enable
kernel.updateConfig({ enabled: true });
});
});
describe('Kernel Performance', () => {
it('should analyze patterns with minimal overhead', async () => {
const start = Date.now();
for (let i = 0; i < 100; i++) {
kernel.analyze(TEST_OBSERVATIONS.securityVulnerability);
}
const duration = Date.now() - start;
expect(duration).toBeLessThan(500); // Should complete in <500ms
});
it('should handle concurrent analysis requests', async () => {
const analysis1 = kernel.analyze(TEST_OBSERVATIONS.securityVulnerability);
const analysis2 = kernel.analyze(TEST_OBSERVATIONS.releaseReadiness);
expect(analysis1.confidence).toBeDefined();
expect(analysis2.confidence).toBeDefined();
});
});
});