-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsensus.test.js
More file actions
485 lines (444 loc) · 17.1 KB
/
Copy pathconsensus.test.js
File metadata and controls
485 lines (444 loc) · 17.1 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import {
aggregate,
BLOCK_THRESHOLD,
buildReviewPrompt,
docsDriftLens,
impactLens,
LENSES,
parseReviewProposal,
reviewerLens,
secretsLens,
speclockLens,
symbolsLens,
testsLens,
verifyDeep,
} from "../src/consensus.js";
import { fakeAnthropic } from "./_fixtures.js";
// ---------------------------------------------------------------------------
// LENSES table — the taxonomy the whole module hangs off.
// ---------------------------------------------------------------------------
test("LENSES: every weight in (0,1), every family named; only tests+secrets are solo", () => {
for (const [name, l] of Object.entries(LENSES)) {
assert.ok(l.weight > 0 && l.weight < 1, `${name} weight bounded`);
assert.ok(typeof l.family === "string" && l.family, `${name} has a family`);
}
assert.equal(LENSES.tests.solo, true);
assert.equal(LENSES.secrets.solo, true);
for (const name of ["symbols", "impact", "docsdrift", "speclock", "reviewer"])
assert.ok(!LENSES[name].solo, `${name} must never be trusted solo`);
});
// ---------------------------------------------------------------------------
// aggregate — noisy-OR + cross-family gate (the scoreMistake shape).
// ---------------------------------------------------------------------------
test("aggregate: a single structural lens never blocks, however loud", () => {
const r = aggregate([{ lens: "symbols", s: 1 }]);
assert.equal(r.p, LENSES.symbols.weight);
assert.equal(r.fires, false);
assert.equal(r.block, false);
});
test("aggregate: many same-family structural signals still can't block (correlated evidence)", () => {
const r = aggregate([
{ lens: "symbols", s: 1 },
{ lens: "speclock", s: 1 },
{ lens: "docsdrift", s: 1 },
{ lens: "impact", s: 1 },
]);
assert.ok(r.p >= BLOCK_THRESHOLD, "noisy-OR piles up");
assert.deepEqual(r.families, ["structural"]);
assert.equal(r.fires, false, "one family — the gate holds");
assert.equal(r.block, false);
});
test("aggregate: a solo test failure blocks on its own", () => {
const r = aggregate([{ lens: "tests", s: 1 }]);
assert.equal(r.fires, true);
assert.ok(r.p >= BLOCK_THRESHOLD);
assert.equal(r.block, true);
});
test("aggregate: a leaked secret blocks on its own", () => {
const r = aggregate([{ lens: "secrets", s: 1 }]);
assert.equal(r.block, true);
});
test("aggregate: two families cross the gate (reviewer can block only WITH a second family)", () => {
const alone = aggregate([{ lens: "reviewer", s: 1 }]);
assert.equal(alone.block, false, "model lens never blocks alone");
const paired = aggregate([
{ lens: "reviewer", s: 1 },
{ lens: "docsdrift", s: 1 },
]);
assert.equal(paired.fires, true);
assert.ok(Math.abs(paired.p - 0.51) < 1e-9, "1 − 0.7·0.7");
assert.equal(paired.block, true);
});
test("aggregate: P(defect) stays bounded < 1 with every lens firing", () => {
const all = Object.keys(LENSES).map((lens) => ({ lens, s: 1 }));
const r = aggregate(all);
assert.ok(r.p < 1);
});
test("aggregate: residual is ∏(1−w) over lenses that RAN — clean lenses count, skipped don't", () => {
const r = aggregate([
{ lens: "tests", s: 0 },
{ lens: "symbols", s: 0 },
{ lens: "reviewer", ran: false },
]);
assert.equal(r.p, 0);
assert.equal(r.fires, false);
assert.ok(Math.abs(r.residual - 0.2 * 0.6) < 1e-9, "0.2·0.6 — reviewer skipped");
const none = aggregate([{ lens: "tests", ran: false }]);
assert.equal(none.residual, 1, "nothing ran → no coverage claimed");
});
test("aggregate: clean lenses contribute no family; unknown lens names are ignored", () => {
const r = aggregate([
{ lens: "tests", s: 0 },
{ lens: "docsdrift", s: 1 },
{ lens: "not-a-lens", s: 1 },
]);
assert.deepEqual(r.families, ["structural"]);
assert.equal(r.fires, false);
});
// ---------------------------------------------------------------------------
// Deterministic lenses.
// ---------------------------------------------------------------------------
test("testsLens: abstains when no suite ran; fires on fail; clean on pass", () => {
assert.equal(testsLens({ ran: false }).ran, false);
assert.equal(testsLens(undefined).ran, false);
assert.equal(testsLens({ ran: true, passed: false }).s, 1);
assert.equal(testsLens({ ran: true, passed: true }).s, 0);
});
test("symbolsLens fires only on unknown symbols", () => {
assert.equal(symbolsLens([]).s, 0);
const l = symbolsLens(["ghostFn"]);
assert.equal(l.s, 1);
assert.deepEqual(l.unknown, ["ghostFn"]);
});
test("docsDriftLens: code without docs fires; code+docs and docs-only stay clean", () => {
assert.equal(docsDriftLens(["src/x.js"]).s, 1);
assert.equal(docsDriftLens(["src/x.js", "README.md"]).s, 0);
assert.equal(docsDriftLens(["README.md"]).s, 0);
assert.equal(docsDriftLens([]).s, 0);
});
test("secretsLens fires on a secret-shaped token in the added lines", () => {
assert.equal(secretsLens("const x = 1;").s, 0);
assert.equal(secretsLens(`key = "${fakeAnthropic()}"`).s, 1);
});
test("impactLens: null atlas abstains; dependents not in the diff fire, graded by count", () => {
assert.equal(impactLens(null, ["a.js"]).ran, false);
const atlas = {
nodes: [
{ id: "mod:a", name: "a.js", file: "a.js" },
{ id: "mod:b", name: "b.js", file: "b.js" },
],
edges: [{ source: "mod:b", target: "mod:a", kind: "imports" }],
symbols: [],
};
const l = impactLens(atlas, ["a.js"]);
assert.equal(l.ran, true);
assert.deepEqual(l.dependents, ["b.js"]);
assert.ok(Math.abs(l.s - 0.2) < 1e-9, "1 dependent / 5 = 0.2");
// dependent already IN the diff → reviewed → clean
assert.equal(impactLens(atlas, ["a.js", "b.js"]).s, 0);
});
test("speclockLens abstains honestly when no spec lock exists", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
assert.equal(speclockLens(dir).ran, false);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// ---------------------------------------------------------------------------
// Reviewer lens — majority-of-N with an injected runner (never the network).
// ---------------------------------------------------------------------------
// Injected reviewer runner: replays scripted replies in order, repeating the last —
// three "independent" samples without a model or the network anywhere near the test.
const scripted = (outputs) => {
let i = 0;
return () => outputs[Math.min(i++, outputs.length - 1)];
};
test("reviewerLens: off unless opted in — deterministic result untouched", () => {
const r = reviewerLens({
llm: false,
run: scripted(['{"verdict":"defect"}']),
});
assert.equal(r.ran, false);
assert.equal(r.verdict, "off");
});
test("reviewerLens: strict majority of usable votes says defect", () => {
const run = scripted([
'{"verdict":"defect","reason":"off-by-one"}',
'{"verdict":"pass"}',
'{"verdict":"defect","reason":"same"}',
]);
const r = reviewerLens({ llm: true, n: 3, run, added: "x" });
assert.equal(r.ran, true);
assert.equal(r.verdict, "defect");
assert.ok(Math.abs(r.s - 2 / 3) < 1e-9);
});
test("reviewerLens: all-pass panel is clean (s=0)", () => {
const r = reviewerLens({
llm: true,
n: 3,
run: scripted(['{"verdict":"pass"}']),
});
assert.equal(r.verdict, "pass");
assert.equal(r.s, 0);
});
test("reviewerLens: abstains when fewer than ⌈n/2⌉ replies are usable", () => {
const run = scripted(["garbage", "not json either", '{"verdict":"defect"}']);
const r = reviewerLens({ llm: true, n: 3, run });
assert.equal(r.ran, false);
assert.equal(r.verdict, "abstain");
});
test("reviewerLens: a tie among usable votes passes (no strict majority)", () => {
const run = scripted(["garbage", '{"verdict":"defect"}', '{"verdict":"pass"}']);
const r = reviewerLens({ llm: true, n: 3, run });
assert.equal(r.ran, true, "2 usable ≥ ⌈3/2⌉");
assert.equal(r.verdict, "pass");
assert.equal(r.s, 0);
});
test("parseReviewProposal: only a clear defect/pass verdict is usable", () => {
assert.equal(parseReviewProposal({ verdict: "maybe" }), null);
assert.equal(parseReviewProposal({}), null);
assert.equal(parseReviewProposal({ verdict: "DEFECT", reason: "x" }).verdict, "defect");
assert.equal(parseReviewProposal({ verdict: " pass " }).verdict, "pass");
});
test("buildReviewPrompt: names the files, caps the diff, demands strict JSON", () => {
const p = buildReviewPrompt({ files: ["src/a.js"], added: "y".repeat(9000) });
assert.ok(p.includes("src/a.js"));
assert.ok(p.includes('{"verdict":"defect|pass"'));
assert.ok(p.length < 6000, "added lines are truncated");
});
// ---------------------------------------------------------------------------
// verifyDeep — orchestration with an injected core verify (no real test-suite run).
// ---------------------------------------------------------------------------
const fakeCore = (over = {}) => ({
ok: true,
provenance: {
base: "HEAD",
changedFiles: [],
tests: {},
symbolsChecked: 0,
unknownSymbols: [],
},
unknown: [],
tests: { ran: true, passed: true, runner: "npm test" },
changedFiles: ["src/x.js", "README.md"],
added: "const y = 2;",
...over,
});
test("verifyDeep: clean diff passes, persists provenance.deep + one verify metrics record", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore(),
});
assert.equal(r.ok, true);
assert.deepEqual(r.findings, []);
assert.equal(r.p, 0);
assert.ok(r.residual > 0 && r.residual < 1, "some lenses ran — coverage is claimed");
const prov = JSON.parse(readFileSync(join(dir, ".forge", "provenance.json"), "utf8"));
assert.equal(prov.deep.block, false);
assert.ok(Array.isArray(prov.deep.lenses) && prov.deep.lenses.length === 7);
assert.equal(prov.deep.residual, r.residual);
const metrics = readFileSync(join(dir, ".forge", "metrics.jsonl"), "utf8")
.trim()
.split("\n")
.map((l) => JSON.parse(l));
assert.equal(metrics.length, 1);
assert.equal(metrics[0].stage, "verify");
assert.equal(metrics[0].outcome, "pass");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: a NOT_CONFIGURED core is NOT recorded as an outcome 'pass' (ME-01)", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore({ tests: { ran: false, status: "NOT_CONFIGURED" } }),
});
assert.equal(r.status, "NOT_CONFIGURED");
assert.equal(r.ok, false, "an unverified core is never ok");
const metrics = readFileSync(join(dir, ".forge", "metrics.jsonl"), "utf8")
.trim()
.split("\n")
.map((l) => JSON.parse(l));
assert.notEqual(metrics[0].outcome, "pass", "unverified must not count as a pass");
assert.equal(metrics[0].outcome, "not_configured");
assert.equal(metrics[0].status, "NOT_CONFIGURED");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: a failing test suite blocks solo; findings + block land in provenance", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore({ tests: { ran: true, passed: false, runner: "npm test" } }),
});
assert.equal(r.ok, false);
assert.equal(r.block, true);
assert.ok(r.findings.some((f) => f.includes("tests failed")));
const prov = JSON.parse(readFileSync(join(dir, ".forge", "provenance.json"), "utf8"));
assert.equal(prov.deep.block, true);
const metrics = readFileSync(join(dir, ".forge", "metrics.jsonl"), "utf8");
assert.ok(metrics.includes('"outcome":"block"'));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: structural findings alone stay advisory (no block)", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () =>
fakeCore({
unknown: ["ghostFn"],
changedFiles: ["src/x.js"] /* no docs → drift too */,
}),
});
assert.equal(r.ok, true, "two structural lenses — one family, the gate holds");
assert.ok(r.findings.length >= 2);
assert.equal(r.fires, false);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: reviewer panel joins via injected runner and can tip a second family", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: true,
run: scripted(['{"verdict":"defect","reason":"wrong edge"}']),
verifyImpl: () => fakeCore({ changedFiles: ["src/x.js"] }), // docsdrift fires (structural)
});
assert.equal(r.fires, true, "model + structural = two families");
assert.equal(r.ok, false);
assert.ok(r.findings.some((f) => f.includes("reviewer panel")));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// ---------------------------------------------------------------------------
// RA-01 — deep ok is a conjunction: core tests PASS AND no consensus block.
// ---------------------------------------------------------------------------
test("verifyDeep: NOT_CONFIGURED core is never ok, even with every lens clean (RA-01)", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore({ tests: { ran: false, status: "NOT_CONFIGURED" } }),
});
assert.equal(r.ok, false, "nothing ran must never be deep-ok");
assert.equal(r.status, "NOT_CONFIGURED");
assert.equal(r.block, false, "the lenses did not block — the CORE verdict did");
const prov = JSON.parse(readFileSync(join(dir, ".forge", "provenance.json"), "utf8"));
assert.equal(prov.deep.status, "NOT_CONFIGURED", "deep status persisted additively");
assert.equal(prov.deep.block, false, "existing provenance fields untouched");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: INCOMPLETE core (detected but unexecutable runner) is not ok (RA-01)", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () =>
fakeCore({
tests: {
ran: false,
status: "INCOMPLETE",
detected: ["go test ./..."],
},
}),
});
assert.equal(r.ok, false);
assert.equal(r.status, "INCOMPLETE");
assert.deepEqual(r.tests.detected, ["go test ./..."], "the detected runner rides along");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: explicit four-state FAIL core blocks with status FAIL", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore({ tests: { ran: true, passed: false, status: "FAIL" } }),
});
assert.equal(r.ok, false);
assert.equal(r.status, "FAIL");
assert.equal(r.block, true, "a failing suite still blocks solo");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: passing core + clean lenses → ok:true with status PASS", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore(),
});
assert.equal(r.ok, true, "ran/passed fallback: a status-less passing core still derives PASS");
assert.equal(r.status, "PASS");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: passing core + blocking finding → ok:false with status FAIL", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () =>
fakeCore({
tests: {
ran: true,
passed: true,
status: "PASS",
runner: "npm test",
},
added: `token = "${fakeAnthropic()}"`, // secrets lens blocks solo
}),
});
assert.equal(r.ok, false);
assert.equal(r.status, "FAIL", "consensus block on a PASSing core reads as FAIL");
assert.equal(r.block, true);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("verifyDeep: a secret in the added lines blocks solo", () => {
const dir = mkdtempSync(join(tmpdir(), "forge-consensus-"));
try {
const r = verifyDeep({
targetRoot: dir,
llm: false,
verifyImpl: () => fakeCore({ added: `token = "${fakeAnthropic()}"` }),
});
assert.equal(r.ok, false);
assert.ok(r.findings.some((f) => f.includes("secret")));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});