|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { describe, it, beforeEach, afterEach } = require('node:test'); |
| 4 | +const assert = require('node:assert/strict'); |
| 5 | +const fs = require('fs'); |
| 6 | +const os = require('os'); |
| 7 | + |
| 8 | +const { runLlmReview, parseReviewResponse } = require('../src/gep/llmReview'); |
| 9 | + |
| 10 | +const input = { |
| 11 | + diff: 'diff --git a/example.js b/example.js\n+const safe = true;', |
| 12 | + gene: { id: 'gene_llm_review_test', category: 'repair' }, |
| 13 | + signals: ['review-boundary'], |
| 14 | + mutation: { rationale: 'exercise the review boundary' }, |
| 15 | +}; |
| 16 | + |
| 17 | +let previousEnabled; |
| 18 | +beforeEach(function () { |
| 19 | + previousEnabled = process.env.EVOLVER_LLM_REVIEW; |
| 20 | + process.env.EVOLVER_LLM_REVIEW = 'true'; |
| 21 | +}); |
| 22 | +afterEach(function () { |
| 23 | + if (previousEnabled === undefined) delete process.env.EVOLVER_LLM_REVIEW; |
| 24 | + else process.env.EVOLVER_LLM_REVIEW = previousEnabled; |
| 25 | +}); |
| 26 | + |
| 27 | +function assertUnavailable(result, reason, attempts) { |
| 28 | + assert.equal(result.approved, false); |
| 29 | + assert.equal(result.status, 'unavailable'); |
| 30 | + assert.equal(result.reason, reason); |
| 31 | + assert.equal(result.retryable, true); |
| 32 | + assert.equal(result.attempts, attempts); |
| 33 | + assert.equal(result.trace.length, attempts); |
| 34 | + assert.ok(result.trace.every(entry => entry.status === 'unavailable')); |
| 35 | +} |
| 36 | + |
| 37 | +describe('llmReview fail-closed boundary', function () { |
| 38 | + it('does not approve malformed output after retry exhaustion', function () { |
| 39 | + const result = runLlmReview(input, { execute: () => 'not-json', maxAttempts: 2 }); |
| 40 | + assertUnavailable(result, 'malformed_output', 2); |
| 41 | + }); |
| 42 | + |
| 43 | + it('does not approve empty output after retry exhaustion', function () { |
| 44 | + const result = runLlmReview(input, { execute: () => ' \n', maxAttempts: 2 }); |
| 45 | + assertUnavailable(result, 'empty_output', 2); |
| 46 | + }); |
| 47 | + |
| 48 | + it('does not approve runner errors after retry exhaustion', function () { |
| 49 | + const result = runLlmReview(input, { |
| 50 | + execute: () => { const error = new Error('runner failed'); error.code = 'EIO'; throw error; }, |
| 51 | + maxAttempts: 2, |
| 52 | + }); |
| 53 | + assertUnavailable(result, 'runner_error', 2); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does not approve timeouts after retry exhaustion', function () { |
| 57 | + const result = runLlmReview(input, { |
| 58 | + execute: () => { const error = new Error('spawnSync node ETIMEDOUT'); error.code = 'ETIMEDOUT'; throw error; }, |
| 59 | + maxAttempts: 2, |
| 60 | + }); |
| 61 | + assertUnavailable(result, 'timeout', 2); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not approve truncated or structurally partial responses', function () { |
| 65 | + const truncated = runLlmReview(input, { |
| 66 | + execute: () => '{"approved":true,"confidence":', |
| 67 | + maxAttempts: 1, |
| 68 | + }); |
| 69 | + assertUnavailable(truncated, 'partial_response', 1); |
| 70 | + |
| 71 | + const incomplete = runLlmReview(input, { |
| 72 | + execute: () => JSON.stringify({ approved: true, confidence: 0.8 }), |
| 73 | + maxAttempts: 1, |
| 74 | + }); |
| 75 | + assertUnavailable(incomplete, 'partial_response', 1); |
| 76 | + }); |
| 77 | + |
| 78 | + it('retries a recoverable failure and preserves the valid success path', function () { |
| 79 | + let calls = 0; |
| 80 | + const result = runLlmReview(input, { |
| 81 | + execute: () => { |
| 82 | + calls += 1; |
| 83 | + if (calls === 1) return ''; |
| 84 | + return JSON.stringify({ approved: true, confidence: 0.9, concerns: [], summary: 'verified' }); |
| 85 | + }, |
| 86 | + maxAttempts: 2, |
| 87 | + }); |
| 88 | + |
| 89 | + assert.equal(result.approved, true); |
| 90 | + assert.equal(result.status, 'approved'); |
| 91 | + assert.equal(result.reason, 'review_approved'); |
| 92 | + assert.equal(result.retryable, false); |
| 93 | + assert.equal(result.attempts, 2); |
| 94 | + assert.deepEqual(result.trace, [ |
| 95 | + { attempt: 1, status: 'unavailable', reason: 'empty_output' }, |
| 96 | + { attempt: 2, status: 'approved', reason: 'review_approved' }, |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('preserves explicit rejection and compatibility fields', function () { |
| 101 | + const result = runLlmReview(input, { |
| 102 | + execute: () => JSON.stringify({ |
| 103 | + approved: false, |
| 104 | + confidence: 0.95, |
| 105 | + concerns: ['unsafe mutation'], |
| 106 | + summary: 'reject unsafe mutation', |
| 107 | + }), |
| 108 | + maxAttempts: 1, |
| 109 | + }); |
| 110 | + |
| 111 | + assert.deepEqual( |
| 112 | + { |
| 113 | + approved: result.approved, |
| 114 | + confidence: result.confidence, |
| 115 | + concerns: result.concerns, |
| 116 | + summary: result.summary, |
| 117 | + status: result.status, |
| 118 | + reason: result.reason, |
| 119 | + }, |
| 120 | + { |
| 121 | + approved: false, |
| 122 | + confidence: 0.95, |
| 123 | + concerns: ['unsafe mutation'], |
| 124 | + summary: 'reject unsafe mutation', |
| 125 | + status: 'rejected', |
| 126 | + reason: 'review_rejected', |
| 127 | + } |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('is idempotent for the same input and deterministic runner output', function () { |
| 132 | + const prompts = []; |
| 133 | + const execute = ({ prompt }) => { |
| 134 | + prompts.push(prompt); |
| 135 | + return JSON.stringify({ approved: true, confidence: 0.8, concerns: [], summary: 'stable review' }); |
| 136 | + }; |
| 137 | + |
| 138 | + const first = runLlmReview(input, { execute, maxAttempts: 1 }); |
| 139 | + const second = runLlmReview(input, { execute, maxAttempts: 1 }); |
| 140 | + |
| 141 | + assert.deepEqual(second, first); |
| 142 | + assert.equal(prompts.length, 2); |
| 143 | + assert.equal(prompts[1], prompts[0]); |
| 144 | + }); |
| 145 | + |
| 146 | + it('records a real subprocess trace and removes its temporary directory', function () { |
| 147 | + const before = new Set(fs.readdirSync(os.tmpdir()).filter(name => name.startsWith('evolver-review-'))); |
| 148 | + const result = runLlmReview(input, { maxAttempts: 1, timeoutMs: 5000 }); |
| 149 | + const after = fs.readdirSync(os.tmpdir()).filter(name => name.startsWith('evolver-review-')); |
| 150 | + |
| 151 | + assert.equal(result.approved, true); |
| 152 | + assert.deepEqual(result.trace, [{ attempt: 1, status: 'approved', reason: 'review_approved' }]); |
| 153 | + assert.deepEqual(after.filter(name => !before.has(name)), []); |
| 154 | + }); |
| 155 | + |
| 156 | + it('does not leak a temp directory when prompt write fails', function () { |
| 157 | + const before = new Set(fs.readdirSync(os.tmpdir()).filter(name => name.startsWith('evolver-review-'))); |
| 158 | + const originalWrite = fs.writeFileSync; |
| 159 | + fs.writeFileSync = function (file) { |
| 160 | + if (String(file).includes('evolver-review-') && String(file).endsWith('prompt.txt')) { |
| 161 | + throw new Error('ENOSPC: no space left on device'); |
| 162 | + } |
| 163 | + return originalWrite.apply(this, arguments); |
| 164 | + }; |
| 165 | + try { |
| 166 | + const result = runLlmReview(input, { maxAttempts: 1, timeoutMs: 5000 }); |
| 167 | + assertUnavailable(result, 'runner_error', 1); |
| 168 | + } finally { |
| 169 | + fs.writeFileSync = originalWrite; |
| 170 | + } |
| 171 | + const after = fs.readdirSync(os.tmpdir()).filter(name => name.startsWith('evolver-review-')); |
| 172 | + assert.deepEqual(after.filter(name => !before.has(name)), []); |
| 173 | + }); |
| 174 | + |
| 175 | + it('returns null when review is disabled', function () { |
| 176 | + process.env.EVOLVER_LLM_REVIEW = 'false'; |
| 177 | + assert.equal(runLlmReview(input, { execute: () => { throw new Error('must not run'); } }), null); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +describe('parseReviewResponse', function () { |
| 182 | + it('rejects out-of-range confidence', function () { |
| 183 | + const invalid = parseReviewResponse(JSON.stringify({ approved: true, confidence: 2, concerns: [], summary: 'bad' })); |
| 184 | + assert.equal(invalid.ok, false); |
| 185 | + assert.equal(invalid.reason, 'partial_response'); |
| 186 | + }); |
| 187 | +}); |
0 commit comments