@rigour-labs/core 4.0.0 → 4.0.2
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/dist/deep/fact-extractor.test.d.ts +1 -0
- package/dist/deep/fact-extractor.test.js +547 -0
- package/dist/deep/prompts.test.d.ts +1 -0
- package/dist/deep/prompts.test.js +220 -0
- package/dist/deep/verifier.test.d.ts +1 -0
- package/dist/deep/verifier.test.js +514 -0
- package/dist/inference/sidecar-provider.d.ts +1 -0
- package/dist/inference/sidecar-provider.js +46 -4
- package/package.json +1 -1
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { verifyFindings } from './verifier.js';
|
|
3
|
+
// ── Test helpers ──
|
|
4
|
+
function makeFinding(overrides = {}) {
|
|
5
|
+
return {
|
|
6
|
+
category: 'god_class',
|
|
7
|
+
severity: 'high',
|
|
8
|
+
file: 'src/service.ts',
|
|
9
|
+
line: 10,
|
|
10
|
+
description: 'The UserService class has too many responsibilities',
|
|
11
|
+
suggestion: 'Split into smaller services',
|
|
12
|
+
confidence: 0.8,
|
|
13
|
+
...overrides,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function makeFileFacts(overrides = {}) {
|
|
17
|
+
return {
|
|
18
|
+
path: 'src/service.ts',
|
|
19
|
+
language: 'typescript',
|
|
20
|
+
lineCount: 300,
|
|
21
|
+
classes: [{
|
|
22
|
+
name: 'UserService',
|
|
23
|
+
lineStart: 5,
|
|
24
|
+
lineEnd: 290,
|
|
25
|
+
methodCount: 12,
|
|
26
|
+
methods: ['find', 'create', 'update', 'delete', 'validate', 'transform', 'cache', 'notify', 'log', 'serialize', 'auth', 'batch'],
|
|
27
|
+
publicMethods: ['find', 'create', 'update', 'delete'],
|
|
28
|
+
lineCount: 285,
|
|
29
|
+
dependencies: ['Database', 'Logger'],
|
|
30
|
+
}],
|
|
31
|
+
functions: [{
|
|
32
|
+
name: 'processData',
|
|
33
|
+
lineStart: 10,
|
|
34
|
+
lineEnd: 80,
|
|
35
|
+
lineCount: 70,
|
|
36
|
+
paramCount: 5,
|
|
37
|
+
params: ['a', 'b', 'c', 'd', 'e'],
|
|
38
|
+
maxNesting: 4,
|
|
39
|
+
hasReturn: true,
|
|
40
|
+
isAsync: true,
|
|
41
|
+
isExported: true,
|
|
42
|
+
}],
|
|
43
|
+
imports: ['express', './types'],
|
|
44
|
+
exports: ['UserService'],
|
|
45
|
+
errorHandling: [
|
|
46
|
+
{ type: 'try-catch', lineStart: 20, isEmpty: false, strategy: 'throw' },
|
|
47
|
+
{ type: 'try-catch', lineStart: 50, isEmpty: true, strategy: 'ignore' },
|
|
48
|
+
],
|
|
49
|
+
testAssertions: 0,
|
|
50
|
+
hasTests: false,
|
|
51
|
+
...overrides,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
describe('Verifier', () => {
|
|
55
|
+
// ── File existence ──
|
|
56
|
+
describe('file existence check', () => {
|
|
57
|
+
it('should drop findings for files not in facts', () => {
|
|
58
|
+
const findings = [makeFinding({ file: 'src/nonexistent.ts' })];
|
|
59
|
+
const facts = [makeFileFacts()];
|
|
60
|
+
const result = verifyFindings(findings, facts);
|
|
61
|
+
expect(result).toHaveLength(0);
|
|
62
|
+
});
|
|
63
|
+
it('should accept findings for files that exist', () => {
|
|
64
|
+
const findings = [makeFinding()];
|
|
65
|
+
const facts = [makeFileFacts()];
|
|
66
|
+
const result = verifyFindings(findings, facts);
|
|
67
|
+
expect(result).toHaveLength(1);
|
|
68
|
+
expect(result[0].verified).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
it('should handle path normalization (leading ./)', () => {
|
|
71
|
+
const findings = [makeFinding({ file: './src/service.ts' })];
|
|
72
|
+
const facts = [makeFileFacts()];
|
|
73
|
+
const result = verifyFindings(findings, facts);
|
|
74
|
+
expect(result).toHaveLength(1);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
// ── Class/Struct findings (SOLID) ──
|
|
78
|
+
describe('class/struct-based verification', () => {
|
|
79
|
+
it('should verify god_class when class has many methods', () => {
|
|
80
|
+
const findings = [makeFinding({ category: 'god_class' })];
|
|
81
|
+
const facts = [makeFileFacts()];
|
|
82
|
+
const result = verifyFindings(findings, facts);
|
|
83
|
+
expect(result).toHaveLength(1);
|
|
84
|
+
expect(result[0].verified).toBe(true);
|
|
85
|
+
expect(result[0].verificationNotes).toContain('12 methods');
|
|
86
|
+
});
|
|
87
|
+
it('should reject god_class when class is small', () => {
|
|
88
|
+
const findings = [makeFinding({ category: 'god_class' })];
|
|
89
|
+
const facts = [makeFileFacts({
|
|
90
|
+
classes: [{
|
|
91
|
+
name: 'UserService',
|
|
92
|
+
lineStart: 5,
|
|
93
|
+
lineEnd: 30,
|
|
94
|
+
methodCount: 3,
|
|
95
|
+
methods: ['find', 'create', 'update'],
|
|
96
|
+
publicMethods: ['find', 'create'],
|
|
97
|
+
lineCount: 25,
|
|
98
|
+
dependencies: [],
|
|
99
|
+
}],
|
|
100
|
+
})];
|
|
101
|
+
const result = verifyFindings(findings, facts);
|
|
102
|
+
expect(result).toHaveLength(0);
|
|
103
|
+
});
|
|
104
|
+
it('should accept god_class for Go structs', () => {
|
|
105
|
+
const findings = [makeFinding({
|
|
106
|
+
category: 'god_class',
|
|
107
|
+
file: 'pkg/server.go',
|
|
108
|
+
description: 'The Server struct has too many responsibilities',
|
|
109
|
+
})];
|
|
110
|
+
const facts = [makeFileFacts({
|
|
111
|
+
path: 'pkg/server.go',
|
|
112
|
+
language: 'go',
|
|
113
|
+
classes: [],
|
|
114
|
+
structs: [{
|
|
115
|
+
name: 'Server',
|
|
116
|
+
lineStart: 5,
|
|
117
|
+
lineEnd: 250,
|
|
118
|
+
fieldCount: 8,
|
|
119
|
+
methodCount: 10,
|
|
120
|
+
methods: ['Start', 'Stop', 'Handle', 'Route', 'Auth', 'Log', 'Cache', 'Validate', 'Transform', 'Serialize'],
|
|
121
|
+
lineCount: 245,
|
|
122
|
+
embeds: [],
|
|
123
|
+
}],
|
|
124
|
+
})];
|
|
125
|
+
const result = verifyFindings(findings, facts);
|
|
126
|
+
expect(result).toHaveLength(1);
|
|
127
|
+
expect(result[0].verified).toBe(true);
|
|
128
|
+
});
|
|
129
|
+
it('should accept Go module-level god_class when file has many functions', () => {
|
|
130
|
+
const findings = [makeFinding({
|
|
131
|
+
category: 'god_class',
|
|
132
|
+
file: 'pkg/utils.go',
|
|
133
|
+
description: 'This module has too many responsibilities',
|
|
134
|
+
})];
|
|
135
|
+
const facts = [makeFileFacts({
|
|
136
|
+
path: 'pkg/utils.go',
|
|
137
|
+
language: 'go',
|
|
138
|
+
classes: [],
|
|
139
|
+
structs: [],
|
|
140
|
+
functions: Array.from({ length: 15 }, (_, i) => ({
|
|
141
|
+
name: `func${i}`,
|
|
142
|
+
lineStart: i * 20,
|
|
143
|
+
lineEnd: i * 20 + 15,
|
|
144
|
+
lineCount: 15,
|
|
145
|
+
paramCount: 2,
|
|
146
|
+
params: ['a', 'b'],
|
|
147
|
+
maxNesting: 2,
|
|
148
|
+
hasReturn: true,
|
|
149
|
+
isAsync: false,
|
|
150
|
+
isExported: true,
|
|
151
|
+
})),
|
|
152
|
+
})];
|
|
153
|
+
const result = verifyFindings(findings, facts);
|
|
154
|
+
expect(result).toHaveLength(1);
|
|
155
|
+
expect(result[0].verified).toBe(true);
|
|
156
|
+
expect(result[0].verificationNotes).toContain('15 functions');
|
|
157
|
+
});
|
|
158
|
+
it('should reject when entity name not found in file', () => {
|
|
159
|
+
const findings = [makeFinding({
|
|
160
|
+
category: 'srp_violation',
|
|
161
|
+
description: 'The NonExistentClass has too many responsibilities',
|
|
162
|
+
})];
|
|
163
|
+
const facts = [makeFileFacts()];
|
|
164
|
+
const result = verifyFindings(findings, facts);
|
|
165
|
+
// Should still verify since file has classes with confidence >= 0.4
|
|
166
|
+
expect(result).toHaveLength(1);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
// ── Function findings ──
|
|
170
|
+
describe('function-based verification', () => {
|
|
171
|
+
it('should verify god_function when function is long', () => {
|
|
172
|
+
const findings = [makeFinding({
|
|
173
|
+
category: 'god_function',
|
|
174
|
+
description: 'processData is too long and complex',
|
|
175
|
+
})];
|
|
176
|
+
const facts = [makeFileFacts()];
|
|
177
|
+
const result = verifyFindings(findings, facts);
|
|
178
|
+
expect(result).toHaveLength(1);
|
|
179
|
+
expect(result[0].verified).toBe(true);
|
|
180
|
+
});
|
|
181
|
+
it('should reject god_function when function is short', () => {
|
|
182
|
+
const findings = [makeFinding({
|
|
183
|
+
category: 'god_function',
|
|
184
|
+
description: 'processData is too complex',
|
|
185
|
+
})];
|
|
186
|
+
const facts = [makeFileFacts({
|
|
187
|
+
functions: [{
|
|
188
|
+
name: 'processData',
|
|
189
|
+
lineStart: 10,
|
|
190
|
+
lineEnd: 25,
|
|
191
|
+
lineCount: 15,
|
|
192
|
+
paramCount: 2,
|
|
193
|
+
params: ['a', 'b'],
|
|
194
|
+
maxNesting: 1,
|
|
195
|
+
hasReturn: true,
|
|
196
|
+
isAsync: false,
|
|
197
|
+
isExported: true,
|
|
198
|
+
}],
|
|
199
|
+
})];
|
|
200
|
+
const result = verifyFindings(findings, facts);
|
|
201
|
+
expect(result).toHaveLength(0);
|
|
202
|
+
});
|
|
203
|
+
it('should verify long_params when function has many params', () => {
|
|
204
|
+
const findings = [makeFinding({
|
|
205
|
+
category: 'long_params',
|
|
206
|
+
description: 'processData has too many parameters',
|
|
207
|
+
})];
|
|
208
|
+
const facts = [makeFileFacts()];
|
|
209
|
+
const result = verifyFindings(findings, facts);
|
|
210
|
+
expect(result).toHaveLength(1);
|
|
211
|
+
expect(result[0].verified).toBe(true);
|
|
212
|
+
});
|
|
213
|
+
it('should reject long_params when function has few params', () => {
|
|
214
|
+
const findings = [makeFinding({
|
|
215
|
+
category: 'long_params',
|
|
216
|
+
description: 'processData has too many parameters',
|
|
217
|
+
})];
|
|
218
|
+
const facts = [makeFileFacts({
|
|
219
|
+
functions: [{
|
|
220
|
+
name: 'processData',
|
|
221
|
+
lineStart: 10,
|
|
222
|
+
lineEnd: 50,
|
|
223
|
+
lineCount: 40,
|
|
224
|
+
paramCount: 2,
|
|
225
|
+
params: ['a', 'b'],
|
|
226
|
+
maxNesting: 2,
|
|
227
|
+
hasReturn: true,
|
|
228
|
+
isAsync: false,
|
|
229
|
+
isExported: true,
|
|
230
|
+
}],
|
|
231
|
+
})];
|
|
232
|
+
const result = verifyFindings(findings, facts);
|
|
233
|
+
expect(result).toHaveLength(0);
|
|
234
|
+
});
|
|
235
|
+
it('should verify complex_conditional when nesting is deep', () => {
|
|
236
|
+
const findings = [makeFinding({
|
|
237
|
+
category: 'complex_conditional',
|
|
238
|
+
description: 'processData has deeply nested conditionals',
|
|
239
|
+
})];
|
|
240
|
+
const facts = [makeFileFacts()];
|
|
241
|
+
const result = verifyFindings(findings, facts);
|
|
242
|
+
expect(result).toHaveLength(1);
|
|
243
|
+
expect(result[0].verified).toBe(true);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
// ── Error handling findings ──
|
|
247
|
+
describe('error handling verification', () => {
|
|
248
|
+
it('should verify empty_catch when empty catches exist', () => {
|
|
249
|
+
const findings = [makeFinding({
|
|
250
|
+
category: 'empty_catch',
|
|
251
|
+
description: 'Empty catch block silently swallows errors',
|
|
252
|
+
})];
|
|
253
|
+
const facts = [makeFileFacts()];
|
|
254
|
+
const result = verifyFindings(findings, facts);
|
|
255
|
+
expect(result).toHaveLength(1);
|
|
256
|
+
expect(result[0].verified).toBe(true);
|
|
257
|
+
});
|
|
258
|
+
it('should reject empty_catch when no empty catches exist', () => {
|
|
259
|
+
const findings = [makeFinding({
|
|
260
|
+
category: 'empty_catch',
|
|
261
|
+
description: 'Empty catch block',
|
|
262
|
+
})];
|
|
263
|
+
const facts = [makeFileFacts({
|
|
264
|
+
errorHandling: [
|
|
265
|
+
{ type: 'try-catch', lineStart: 10, isEmpty: false, strategy: 'throw' },
|
|
266
|
+
],
|
|
267
|
+
})];
|
|
268
|
+
const result = verifyFindings(findings, facts);
|
|
269
|
+
expect(result).toHaveLength(0);
|
|
270
|
+
});
|
|
271
|
+
it('should verify error_inconsistency when multiple strategies exist', () => {
|
|
272
|
+
const findings = [makeFinding({
|
|
273
|
+
category: 'error_inconsistency',
|
|
274
|
+
description: 'Mixed error handling strategies',
|
|
275
|
+
})];
|
|
276
|
+
const facts = [makeFileFacts()]; // Has 'throw' and 'ignore' strategies
|
|
277
|
+
const result = verifyFindings(findings, facts);
|
|
278
|
+
expect(result).toHaveLength(1);
|
|
279
|
+
expect(result[0].verified).toBe(true);
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
// ── Concurrency findings ──
|
|
283
|
+
describe('concurrency verification', () => {
|
|
284
|
+
const goFacts = () => makeFileFacts({
|
|
285
|
+
path: 'pkg/worker.go',
|
|
286
|
+
language: 'go',
|
|
287
|
+
goroutines: 3,
|
|
288
|
+
channels: 2,
|
|
289
|
+
mutexes: 1,
|
|
290
|
+
defers: 2,
|
|
291
|
+
});
|
|
292
|
+
it('should verify race_condition when concurrency constructs exist', () => {
|
|
293
|
+
const findings = [makeFinding({
|
|
294
|
+
category: 'race_condition',
|
|
295
|
+
file: 'pkg/worker.go',
|
|
296
|
+
description: 'Potential race condition',
|
|
297
|
+
confidence: 0.6,
|
|
298
|
+
})];
|
|
299
|
+
const result = verifyFindings(findings, [goFacts()]);
|
|
300
|
+
expect(result).toHaveLength(1);
|
|
301
|
+
expect(result[0].verified).toBe(true);
|
|
302
|
+
});
|
|
303
|
+
it('should reject goroutine_leak when no goroutines exist', () => {
|
|
304
|
+
const findings = [makeFinding({
|
|
305
|
+
category: 'goroutine_leak',
|
|
306
|
+
file: 'pkg/worker.go',
|
|
307
|
+
description: 'Goroutine leak detected',
|
|
308
|
+
confidence: 0.7,
|
|
309
|
+
})];
|
|
310
|
+
const facts = goFacts();
|
|
311
|
+
facts.goroutines = 0;
|
|
312
|
+
const result = verifyFindings(findings, [facts]);
|
|
313
|
+
expect(result).toHaveLength(0);
|
|
314
|
+
});
|
|
315
|
+
it('should reject channel_misuse when no channels exist', () => {
|
|
316
|
+
const findings = [makeFinding({
|
|
317
|
+
category: 'channel_misuse',
|
|
318
|
+
file: 'pkg/worker.go',
|
|
319
|
+
description: 'Channel misuse detected',
|
|
320
|
+
confidence: 0.7,
|
|
321
|
+
})];
|
|
322
|
+
const facts = goFacts();
|
|
323
|
+
facts.channels = 0;
|
|
324
|
+
const result = verifyFindings(findings, [facts]);
|
|
325
|
+
expect(result).toHaveLength(0);
|
|
326
|
+
});
|
|
327
|
+
it('should reject concurrency finding when no concurrency exists', () => {
|
|
328
|
+
const findings = [makeFinding({
|
|
329
|
+
category: 'race_condition',
|
|
330
|
+
file: 'pkg/worker.go',
|
|
331
|
+
description: 'Race condition',
|
|
332
|
+
confidence: 0.8,
|
|
333
|
+
})];
|
|
334
|
+
const facts = goFacts();
|
|
335
|
+
facts.goroutines = 0;
|
|
336
|
+
facts.channels = 0;
|
|
337
|
+
facts.mutexes = 0;
|
|
338
|
+
facts.functions = facts.functions.map(f => ({ ...f, isAsync: false }));
|
|
339
|
+
const result = verifyFindings(findings, [facts]);
|
|
340
|
+
expect(result).toHaveLength(0);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
// ── Interface findings ──
|
|
344
|
+
describe('interface verification', () => {
|
|
345
|
+
it('should verify ISP violation on large interface', () => {
|
|
346
|
+
const findings = [makeFinding({
|
|
347
|
+
category: 'isp_violation_interface',
|
|
348
|
+
file: 'pkg/store.go',
|
|
349
|
+
description: 'Store interface has too many methods',
|
|
350
|
+
confidence: 0.8,
|
|
351
|
+
})];
|
|
352
|
+
const facts = [makeFileFacts({
|
|
353
|
+
path: 'pkg/store.go',
|
|
354
|
+
language: 'go',
|
|
355
|
+
interfaces: [{
|
|
356
|
+
name: 'Store',
|
|
357
|
+
lineStart: 5,
|
|
358
|
+
methodCount: 8,
|
|
359
|
+
methods: ['Get', 'Set', 'Delete', 'List', 'Close', 'Watch', 'Backup', 'Restore'],
|
|
360
|
+
}],
|
|
361
|
+
})];
|
|
362
|
+
const result = verifyFindings(findings, facts);
|
|
363
|
+
expect(result).toHaveLength(1);
|
|
364
|
+
expect(result[0].verified).toBe(true);
|
|
365
|
+
});
|
|
366
|
+
it('should reject ISP violation when no interfaces exist', () => {
|
|
367
|
+
const findings = [makeFinding({
|
|
368
|
+
category: 'isp_violation_interface',
|
|
369
|
+
file: 'pkg/store.go',
|
|
370
|
+
description: 'Interface has too many methods',
|
|
371
|
+
})];
|
|
372
|
+
const facts = [makeFileFacts({
|
|
373
|
+
path: 'pkg/store.go',
|
|
374
|
+
interfaces: [],
|
|
375
|
+
})];
|
|
376
|
+
const result = verifyFindings(findings, facts);
|
|
377
|
+
expect(result).toHaveLength(0);
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
// ── Test findings ──
|
|
381
|
+
describe('test verification', () => {
|
|
382
|
+
it('should verify missing_test for substantial code files', () => {
|
|
383
|
+
const findings = [makeFinding({
|
|
384
|
+
category: 'missing_test',
|
|
385
|
+
description: 'No tests for this module',
|
|
386
|
+
})];
|
|
387
|
+
const facts = [makeFileFacts({
|
|
388
|
+
functions: [
|
|
389
|
+
{ name: 'processData', lineStart: 10, lineEnd: 80, lineCount: 70, paramCount: 5, params: ['a', 'b', 'c', 'd', 'e'], maxNesting: 4, hasReturn: true, isAsync: true, isExported: true },
|
|
390
|
+
{ name: 'helperFn', lineStart: 85, lineEnd: 100, lineCount: 15, paramCount: 1, params: ['x'], maxNesting: 1, hasReturn: true, isAsync: false, isExported: false },
|
|
391
|
+
],
|
|
392
|
+
})]; // hasTests: false, 300 lines, 2 functions
|
|
393
|
+
const result = verifyFindings(findings, facts);
|
|
394
|
+
expect(result).toHaveLength(1);
|
|
395
|
+
expect(result[0].verified).toBe(true);
|
|
396
|
+
});
|
|
397
|
+
it('should reject missing_test when file already has tests', () => {
|
|
398
|
+
const findings = [makeFinding({
|
|
399
|
+
category: 'missing_test',
|
|
400
|
+
description: 'No tests',
|
|
401
|
+
})];
|
|
402
|
+
const facts = [makeFileFacts({ hasTests: true })];
|
|
403
|
+
const result = verifyFindings(findings, facts);
|
|
404
|
+
expect(result).toHaveLength(0);
|
|
405
|
+
});
|
|
406
|
+
it('should reject missing_test for trivial files', () => {
|
|
407
|
+
const findings = [makeFinding({
|
|
408
|
+
category: 'missing_test',
|
|
409
|
+
description: 'No tests',
|
|
410
|
+
})];
|
|
411
|
+
const facts = [makeFileFacts({ lineCount: 20, functions: [] })];
|
|
412
|
+
const result = verifyFindings(findings, facts);
|
|
413
|
+
expect(result).toHaveLength(0);
|
|
414
|
+
});
|
|
415
|
+
});
|
|
416
|
+
// ── File-level categories ──
|
|
417
|
+
describe('file-level verification', () => {
|
|
418
|
+
it('should verify long_file when file exceeds 300 lines', () => {
|
|
419
|
+
const findings = [makeFinding({ category: 'long_file' })];
|
|
420
|
+
const facts = [makeFileFacts({ lineCount: 500 })];
|
|
421
|
+
const result = verifyFindings(findings, facts);
|
|
422
|
+
expect(result).toHaveLength(1);
|
|
423
|
+
expect(result[0].verified).toBe(true);
|
|
424
|
+
});
|
|
425
|
+
it('should reject long_file when file is short', () => {
|
|
426
|
+
const findings = [makeFinding({ category: 'long_file' })];
|
|
427
|
+
const facts = [makeFileFacts({ lineCount: 100 })];
|
|
428
|
+
const result = verifyFindings(findings, facts);
|
|
429
|
+
expect(result).toHaveLength(0);
|
|
430
|
+
});
|
|
431
|
+
it('should verify magic_number when many magic numbers detected', () => {
|
|
432
|
+
const findings = [makeFinding({ category: 'magic_number' })];
|
|
433
|
+
const facts = [makeFileFacts({ magicNumbers: 10 })];
|
|
434
|
+
const result = verifyFindings(findings, facts);
|
|
435
|
+
expect(result).toHaveLength(1);
|
|
436
|
+
});
|
|
437
|
+
it('should reject magic_number when few magic numbers detected', () => {
|
|
438
|
+
const findings = [makeFinding({ category: 'magic_number' })];
|
|
439
|
+
const facts = [makeFileFacts({ magicNumbers: 1 })];
|
|
440
|
+
const result = verifyFindings(findings, facts);
|
|
441
|
+
expect(result).toHaveLength(0);
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
// ── Confidence-based categories ──
|
|
445
|
+
describe('confidence-based verification', () => {
|
|
446
|
+
const confidenceCategories = [
|
|
447
|
+
'dry_violation', 'feature_envy', 'architecture',
|
|
448
|
+
'naming_convention', 'dead_code', 'performance',
|
|
449
|
+
];
|
|
450
|
+
for (const category of confidenceCategories) {
|
|
451
|
+
it(`should accept ${category} with confidence >= 0.3`, () => {
|
|
452
|
+
const findings = [makeFinding({ category, confidence: 0.5 })];
|
|
453
|
+
const facts = [makeFileFacts()];
|
|
454
|
+
const result = verifyFindings(findings, facts);
|
|
455
|
+
expect(result).toHaveLength(1);
|
|
456
|
+
});
|
|
457
|
+
it(`should reject ${category} with low confidence`, () => {
|
|
458
|
+
const findings = [makeFinding({ category, confidence: 0.1 })];
|
|
459
|
+
const facts = [makeFileFacts()];
|
|
460
|
+
const result = verifyFindings(findings, facts);
|
|
461
|
+
expect(result).toHaveLength(0);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
// ── Resource leak (Go-specific) ──
|
|
466
|
+
describe('resource leak verification', () => {
|
|
467
|
+
it('should verify Go resource leak when resource imports present', () => {
|
|
468
|
+
const findings = [makeFinding({
|
|
469
|
+
category: 'resource_leak',
|
|
470
|
+
file: 'pkg/db.go',
|
|
471
|
+
confidence: 0.6,
|
|
472
|
+
})];
|
|
473
|
+
const facts = [makeFileFacts({
|
|
474
|
+
path: 'pkg/db.go',
|
|
475
|
+
language: 'go',
|
|
476
|
+
imports: ['database/sql', 'net/http'],
|
|
477
|
+
})];
|
|
478
|
+
const result = verifyFindings(findings, facts);
|
|
479
|
+
expect(result).toHaveLength(1);
|
|
480
|
+
expect(result[0].verified).toBe(true);
|
|
481
|
+
});
|
|
482
|
+
it('should reject Go resource leak when no resource imports', () => {
|
|
483
|
+
const findings = [makeFinding({
|
|
484
|
+
category: 'resource_leak',
|
|
485
|
+
file: 'pkg/util.go',
|
|
486
|
+
confidence: 0.6,
|
|
487
|
+
})];
|
|
488
|
+
const facts = [makeFileFacts({
|
|
489
|
+
path: 'pkg/util.go',
|
|
490
|
+
language: 'go',
|
|
491
|
+
imports: ['fmt', 'strings'],
|
|
492
|
+
})];
|
|
493
|
+
const result = verifyFindings(findings, facts);
|
|
494
|
+
expect(result).toHaveLength(0);
|
|
495
|
+
});
|
|
496
|
+
});
|
|
497
|
+
// ── Multiple findings batch ──
|
|
498
|
+
describe('batch verification', () => {
|
|
499
|
+
it('should process multiple findings and filter correctly', () => {
|
|
500
|
+
const findings = [
|
|
501
|
+
makeFinding({ category: 'god_class' }), // Should pass (12 methods)
|
|
502
|
+
makeFinding({ category: 'god_function', file: 'nonexistent.ts' }), // Should fail (no file)
|
|
503
|
+
makeFinding({ category: 'long_file' }), // Should fail (300 lines, need >300)
|
|
504
|
+
makeFinding({ category: 'magic_number' }), // Should fail (no magicNumbers set)
|
|
505
|
+
makeFinding({ category: 'dry_violation', confidence: 0.1 }), // Should fail (low confidence)
|
|
506
|
+
makeFinding({ category: 'dry_violation', confidence: 0.5 }), // Should pass
|
|
507
|
+
];
|
|
508
|
+
const facts = [makeFileFacts()];
|
|
509
|
+
const result = verifyFindings(findings, facts);
|
|
510
|
+
const verified = result.filter(r => r.verified);
|
|
511
|
+
expect(verified.length).toBeGreaterThanOrEqual(2); // god_class, dry_violation(0.5)
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
});
|
|
@@ -8,6 +8,7 @@ import { promisify } from 'util';
|
|
|
8
8
|
import path from 'path';
|
|
9
9
|
import os from 'os';
|
|
10
10
|
import fs from 'fs-extra';
|
|
11
|
+
import { createRequire } from 'module';
|
|
11
12
|
import { ensureModel, isModelCached, getModelInfo } from './model-manager.js';
|
|
12
13
|
const execFileAsync = promisify(execFile);
|
|
13
14
|
/** Platform → npm package mapping */
|
|
@@ -33,15 +34,22 @@ export class SidecarProvider {
|
|
|
33
34
|
return binary !== null;
|
|
34
35
|
}
|
|
35
36
|
async setup(onProgress) {
|
|
37
|
+
const platformKey = this.getPlatformKey();
|
|
38
|
+
const packageName = PLATFORM_PACKAGES[platformKey];
|
|
36
39
|
// 1. Check/resolve binary
|
|
37
40
|
this.binaryPath = await this.resolveBinaryPath();
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
// Auto-bootstrap local sidecar once before failing.
|
|
42
|
+
if (!this.binaryPath && packageName) {
|
|
43
|
+
const installed = await this.installSidecarBinary(packageName, onProgress);
|
|
44
|
+
if (installed) {
|
|
45
|
+
this.binaryPath = await this.resolveBinaryPath();
|
|
46
|
+
}
|
|
40
47
|
}
|
|
41
|
-
|
|
48
|
+
if (!this.binaryPath) {
|
|
42
49
|
onProgress?.('⚠ Inference engine not found. Install @rigour/brain-* or add llama-cli to PATH');
|
|
43
|
-
throw new Error('Sidecar binary not found. Run: npm install @rigour/brain-' +
|
|
50
|
+
throw new Error('Sidecar binary not found. Run: npm install @rigour/brain-' + platformKey);
|
|
44
51
|
}
|
|
52
|
+
onProgress?.('✓ Inference engine ready');
|
|
45
53
|
// 2. Ensure model is downloaded
|
|
46
54
|
if (!isModelCached(this.tier)) {
|
|
47
55
|
const modelInfo = getModelInfo(this.tier);
|
|
@@ -100,11 +108,28 @@ export class SidecarProvider {
|
|
|
100
108
|
// Strategy 1: Check @rigour/brain-{platform} optional dependency
|
|
101
109
|
const packageName = PLATFORM_PACKAGES[platformKey];
|
|
102
110
|
if (packageName) {
|
|
111
|
+
try {
|
|
112
|
+
const require = createRequire(import.meta.url);
|
|
113
|
+
const pkgJsonPath = require.resolve(path.posix.join(packageName, 'package.json'));
|
|
114
|
+
const pkgDir = path.dirname(pkgJsonPath);
|
|
115
|
+
const resolvedBin = path.join(pkgDir, 'bin', 'rigour-brain');
|
|
116
|
+
const resolvedBinPath = os.platform() === 'win32' ? resolvedBin + '.exe' : resolvedBin;
|
|
117
|
+
if (await fs.pathExists(resolvedBinPath)) {
|
|
118
|
+
return resolvedBinPath;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// Package not resolvable from current runtime
|
|
123
|
+
}
|
|
103
124
|
try {
|
|
104
125
|
// Try to resolve from node_modules
|
|
105
126
|
const possiblePaths = [
|
|
127
|
+
// From current working directory
|
|
128
|
+
path.join(process.cwd(), 'node_modules', ...packageName.split('/'), 'bin', 'rigour-brain'),
|
|
106
129
|
// From rigour-core node_modules
|
|
107
130
|
path.join(__dirname, '..', '..', '..', 'node_modules', ...packageName.split('/'), 'bin', 'rigour-brain'),
|
|
131
|
+
// From monorepo root when rigour-core is nested under packages/
|
|
132
|
+
path.join(__dirname, '..', '..', '..', '..', '..', 'node_modules', ...packageName.split('/'), 'bin', 'rigour-brain'),
|
|
108
133
|
// From global node_modules
|
|
109
134
|
path.join(os.homedir(), '.npm-global', 'lib', 'node_modules', ...packageName.split('/'), 'bin', 'rigour-brain'),
|
|
110
135
|
];
|
|
@@ -150,4 +175,21 @@ export class SidecarProvider {
|
|
|
150
175
|
}
|
|
151
176
|
return null;
|
|
152
177
|
}
|
|
178
|
+
async installSidecarBinary(packageName, onProgress) {
|
|
179
|
+
onProgress?.(`⬇ Inference engine missing. Attempting automatic install: ${packageName}`);
|
|
180
|
+
try {
|
|
181
|
+
await execFileAsync('npm', ['install', '--no-save', '--no-package-lock', packageName], {
|
|
182
|
+
cwd: process.cwd(),
|
|
183
|
+
timeout: 120000,
|
|
184
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
const reason = typeof error?.message === 'string' ? error.message : 'unknown install error';
|
|
189
|
+
onProgress?.(`⚠ Auto-install failed: ${reason}`);
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
onProgress?.(`✓ Installed ${packageName}`);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
153
195
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rigour-labs/core",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "Deterministic quality gate engine for AI-generated code. AST analysis, drift detection, and Fix Packet generation across TypeScript, JavaScript, Python, Go, Ruby, and C#.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://rigour.run",
|