diffpact 0.0.0-stage → 0.4.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/CHANGELOG.md +126 -0
- package/LICENSE +21 -0
- package/README.md +214 -2
- package/bin/diffpact.mjs +10 -0
- package/dist/adapters/claude-code.js +191 -0
- package/dist/adapters/codex.js +348 -0
- package/dist/adapters/gemini-agy.js +429 -0
- package/dist/adapters/vercel-board.js +138 -0
- package/dist/amend.js +470 -0
- package/dist/boundaries.js +309 -0
- package/dist/canonical-json.js +63 -0
- package/dist/cli.js +1433 -0
- package/dist/delegate.js +140 -0
- package/dist/doctor.js +293 -0
- package/dist/git.js +219 -0
- package/dist/index.js +19 -0
- package/dist/merge-verify.js +721 -0
- package/dist/report.js +220 -0
- package/dist/review.js +527 -0
- package/dist/runner.js +213 -0
- package/dist/schema.js +424 -0
- package/dist/types.js +220 -0
- package/dist/verify.js +952 -0
- package/dist/worktree.js +717 -0
- package/examples/harnesses/README.md +63 -0
- package/examples/quickstart/README.md +17 -0
- package/examples/quickstart/run.mjs +247 -0
- package/package.json +62 -4
- package/schemas/contract.schema.json +148 -0
- package/schemas/receipt.schema.json +49 -0
- package/schemas/review.schema.json +48 -0
package/dist/review.js
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { canonicalSha256 } from './canonical-json.js';
|
|
5
|
+
import { collectRepoChanges, resolveHeadSha, resolveRepoRoot } from './git.js';
|
|
6
|
+
import { validateContract, validateReceiptBasic, validateReviewBasic, validateSessionIdentity } from './schema.js';
|
|
7
|
+
import {
|
|
8
|
+
EXIT_INCOMPLETE,
|
|
9
|
+
EXIT_PASS,
|
|
10
|
+
EXIT_STALE,
|
|
11
|
+
EXIT_VIOLATION,
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
} from './types.js';
|
|
17
|
+
|
|
18
|
+
function verifyReceiptIntegrity(receipt ) {
|
|
19
|
+
const { receipt_sha256, ...body } = receipt;
|
|
20
|
+
return canonicalSha256('diffpact/receipt/v0.1', body) === receipt_sha256;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function verifyReviewIntegrity(review ) {
|
|
24
|
+
const { review_sha256, attestation_hmac_sha256: _hmac, ...body } = review;
|
|
25
|
+
return canonicalSha256('diffpact/review/v0.1', body) === review_sha256;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function computeReviewHmac(reviewSha256 , secret ) {
|
|
29
|
+
return crypto
|
|
30
|
+
.createHmac('sha256', secret)
|
|
31
|
+
.update(`diffpact/review-hmac/v0.1\0${reviewSha256}`)
|
|
32
|
+
.digest('hex');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function verifyReviewHmac(review , secret ) {
|
|
36
|
+
if (!review.attestation_hmac_sha256 || !/^[0-9a-f]{64}$/.test(review.attestation_hmac_sha256)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const expected = computeReviewHmac(review.review_sha256, secret);
|
|
40
|
+
return crypto.timingSafeEqual(
|
|
41
|
+
Buffer.from(review.attestation_hmac_sha256, 'hex'),
|
|
42
|
+
Buffer.from(expected, 'hex')
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
export function runReview(options )
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
{
|
|
65
|
+
const baseDir = options.cwd ?? process.cwd();
|
|
66
|
+
const contractAbs = path.resolve(baseDir, options.contractPath);
|
|
67
|
+
const pactDir = path.dirname(contractAbs);
|
|
68
|
+
const receiptAbs = path.join(pactDir, 'receipt.json');
|
|
69
|
+
|
|
70
|
+
if (!fs.existsSync(contractAbs) || !fs.existsSync(receiptAbs)) {
|
|
71
|
+
return {
|
|
72
|
+
exitCode: EXIT_INCOMPLETE,
|
|
73
|
+
error: 'Both contract.json and a passing receipt.json are required before review'
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let contract;
|
|
78
|
+
let receipt;
|
|
79
|
+
let reviewerSession ;
|
|
80
|
+
try {
|
|
81
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
82
|
+
receipt = validateReceiptBasic(JSON.parse(fs.readFileSync(receiptAbs, 'utf8')));
|
|
83
|
+
reviewerSession = validateSessionIdentity(options.reviewer, 'reviewer');
|
|
84
|
+
} catch (err) {
|
|
85
|
+
return { exitCode: EXIT_INCOMPLETE, error: (err ).message };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!verifyReceiptIntegrity(receipt)) {
|
|
89
|
+
return {
|
|
90
|
+
exitCode: EXIT_VIOLATION,
|
|
91
|
+
error: 'receipt.json failed cryptographic SHA-256 integrity verification'
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const contractSha256 = canonicalSha256('diffpact/contract/v0.1', contract);
|
|
96
|
+
if (receipt.contract_sha256 !== contractSha256) {
|
|
97
|
+
return {
|
|
98
|
+
exitCode: EXIT_STALE,
|
|
99
|
+
error: 'contract.json changed after receipt.json was generated'
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const repoRoot = resolveRepoRoot(path.dirname(pactDir));
|
|
104
|
+
const headSha = resolveHeadSha(repoRoot);
|
|
105
|
+
if (receipt.candidate_sha !== headSha) {
|
|
106
|
+
return {
|
|
107
|
+
exitCode: EXIT_STALE,
|
|
108
|
+
error: `Git HEAD (${headSha}) moved after receipt candidate_sha (${receipt.candidate_sha})`
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const snap = collectRepoChanges(repoRoot, contract.starting_sha, headSha);
|
|
113
|
+
if (snap.rawDiffSha256 !== receipt.raw_diff_sha256) {
|
|
114
|
+
return {
|
|
115
|
+
exitCode: EXIT_STALE,
|
|
116
|
+
error: 'Repository raw diff drifted after verification receipt was generated'
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (contract.worktree.require_clean_end !== false && !snap.workingTreeClean) {
|
|
120
|
+
return {
|
|
121
|
+
exitCode: EXIT_STALE,
|
|
122
|
+
error: 'Working tree became dirty after verification receipt was generated'
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (receipt.status !== 'pass' || receipt.exit_code !== EXIT_PASS) {
|
|
127
|
+
return {
|
|
128
|
+
exitCode: EXIT_INCOMPLETE,
|
|
129
|
+
error: `Cannot attest review on non-passing receipt (status=${receipt.status}, exit=${receipt.exit_code})`
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Enforce Anti-Self-Certification
|
|
134
|
+
const impl = contract.implementer_session;
|
|
135
|
+
if (contract.review_policy?.require_non_self_certified !== false) {
|
|
136
|
+
if (
|
|
137
|
+
reviewerSession.agent_id === impl.agent_id ||
|
|
138
|
+
reviewerSession.session_id === impl.session_id
|
|
139
|
+
) {
|
|
140
|
+
return {
|
|
141
|
+
exitCode: EXIT_VIOLATION,
|
|
142
|
+
error: `Self-certification forbidden: reviewer (${reviewerSession.agent_id}/${reviewerSession.session_id}) collides with implementer (${impl.agent_id}/${impl.session_id})`
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (
|
|
146
|
+
contract.review_policy?.require_distinct_harness &&
|
|
147
|
+
reviewerSession.harness === impl.harness
|
|
148
|
+
) {
|
|
149
|
+
return {
|
|
150
|
+
exitCode: EXIT_VIOLATION,
|
|
151
|
+
error: `Distinct harness required: reviewer harness "${reviewerSession.harness}" matches implementer`
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Enforce Raw-Diff Inspection + Optional Cryptographic Diff-SHA Challenge
|
|
157
|
+
if (options.expectedRawDiffSha256 !== undefined) {
|
|
158
|
+
const expected = options.expectedRawDiffSha256.trim().toLowerCase();
|
|
159
|
+
if (!/^[0-9a-f]{12,64}$/.test(expected) || !snap.rawDiffSha256.startsWith(expected)) {
|
|
160
|
+
return {
|
|
161
|
+
exitCode: EXIT_VIOLATION,
|
|
162
|
+
error: `Provided --diff-sha "${options.expectedRawDiffSha256}" does not match live raw_diff_sha256 "${snap.rawDiffSha256}"`
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const inspectedRawDiff =
|
|
168
|
+
options.expectedRawDiffSha256 !== undefined ? true : (options.inspectedRawDiff ?? true);
|
|
169
|
+
if (contract.review_policy?.require_raw_diff_inspection !== false && !inspectedRawDiff) {
|
|
170
|
+
return {
|
|
171
|
+
exitCode: EXIT_INCOMPLETE,
|
|
172
|
+
error: 'Reviewer must inspect primary raw Git diff, not only prose handoff summaries'
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const attestationMode =
|
|
177
|
+
options.attestationMode ?? (options.expectedRawDiffSha256 ? 'diff-sha-bound' : 'api');
|
|
178
|
+
|
|
179
|
+
const lineageId =
|
|
180
|
+
options.evidenceLineageId ??
|
|
181
|
+
`raw-diff:${snap.rawDiffSha256}:reviewer:${reviewerSession.agent_id}:${reviewerSession.session_id}`;
|
|
182
|
+
|
|
183
|
+
const reviewBody = {
|
|
184
|
+
schema_version: 'diffpact-review-v0.1' ,
|
|
185
|
+
contract_sha256: contractSha256,
|
|
186
|
+
receipt_sha256: receipt.receipt_sha256,
|
|
187
|
+
task_id: contract.task_id,
|
|
188
|
+
starting_sha: contract.starting_sha,
|
|
189
|
+
candidate_sha: headSha,
|
|
190
|
+
raw_diff_sha256: snap.rawDiffSha256,
|
|
191
|
+
evidence_lineage_id: lineageId,
|
|
192
|
+
inspected_raw_diff: inspectedRawDiff,
|
|
193
|
+
attestation_mode: attestationMode,
|
|
194
|
+
implementer_session: impl,
|
|
195
|
+
reviewer_session: reviewerSession,
|
|
196
|
+
verdict: options.verdict,
|
|
197
|
+
findings: options.findings ?? [],
|
|
198
|
+
summary: options.summary,
|
|
199
|
+
reviewed_at: new Date().toISOString()
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const reviewSha256 = canonicalSha256('diffpact/review/v0.1', reviewBody);
|
|
203
|
+
const secret = options.reviewerSecret ?? process.env.DIFFPACT_REVIEWER_SECRET;
|
|
204
|
+
const attestationHmac = secret && secret.length > 0 ? computeReviewHmac(reviewSha256, secret) : undefined;
|
|
205
|
+
const review = {
|
|
206
|
+
review_sha256: reviewSha256,
|
|
207
|
+
...(attestationHmac ? { attestation_hmac_sha256: attestationHmac } : {}),
|
|
208
|
+
...reviewBody
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const reviewsDir = path.join(pactDir, 'reviews');
|
|
212
|
+
fs.mkdirSync(reviewsDir, { recursive: true });
|
|
213
|
+
const safeAgentFile = reviewerSession.agent_id.replace(/[^a-zA-Z0-9._-]/g, '_');
|
|
214
|
+
fs.writeFileSync(path.join(reviewsDir, `${safeAgentFile}.json`), JSON.stringify(review, null, 2) + '\n');
|
|
215
|
+
fs.writeFileSync(path.join(pactDir, 'review.json'), JSON.stringify(review, null, 2) + '\n');
|
|
216
|
+
|
|
217
|
+
if (options.verdict === 'reject') {
|
|
218
|
+
return { exitCode: EXIT_VIOLATION, review, error: 'Reviewer rejected candidate' };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return { exitCode: EXIT_PASS, review };
|
|
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
|
+
* Fast, stateless read-only gate check against live Git HEAD, receipt, reviews,
|
|
250
|
+
* and optional canonical integrator / role_substitution policy.
|
|
251
|
+
*/
|
|
252
|
+
export function checkStatus(options ) {
|
|
253
|
+
const baseDir = options.cwd ?? process.cwd();
|
|
254
|
+
const contractAbs = path.resolve(baseDir, options.contractPath);
|
|
255
|
+
const pactDir = path.dirname(contractAbs);
|
|
256
|
+
const receiptAbs = path.join(pactDir, 'receipt.json');
|
|
257
|
+
|
|
258
|
+
if (!fs.existsSync(contractAbs)) {
|
|
259
|
+
return { exit_code: EXIT_INCOMPLETE, status: 'incomplete', reason: 'Missing contract.json' };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
let contract;
|
|
263
|
+
try {
|
|
264
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
265
|
+
} catch (err) {
|
|
266
|
+
return { exit_code: EXIT_INCOMPLETE, status: 'incomplete', reason: (err ).message };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const contractSha256 = canonicalSha256('diffpact/contract/v0.1', contract);
|
|
270
|
+
const repoRoot = resolveRepoRoot(path.dirname(pactDir));
|
|
271
|
+
const headSha = resolveHeadSha(repoRoot);
|
|
272
|
+
|
|
273
|
+
if (!fs.existsSync(receiptAbs)) {
|
|
274
|
+
return {
|
|
275
|
+
exit_code: EXIT_INCOMPLETE,
|
|
276
|
+
status: 'incomplete',
|
|
277
|
+
reason: 'Missing verification receipt.json',
|
|
278
|
+
task_id: contract.task_id,
|
|
279
|
+
candidate_sha: headSha,
|
|
280
|
+
contract_sha256: contractSha256
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let receipt;
|
|
285
|
+
try {
|
|
286
|
+
receipt = validateReceiptBasic(JSON.parse(fs.readFileSync(receiptAbs, 'utf8')));
|
|
287
|
+
} catch (err) {
|
|
288
|
+
return { exit_code: EXIT_INCOMPLETE, status: 'incomplete', reason: (err ).message };
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (!verifyReceiptIntegrity(receipt)) {
|
|
292
|
+
return { exit_code: EXIT_VIOLATION, status: 'violation', reason: 'Tampered receipt.json SHA-256' };
|
|
293
|
+
}
|
|
294
|
+
if (receipt.contract_sha256 !== contractSha256) {
|
|
295
|
+
return { exit_code: EXIT_STALE, status: 'stale', reason: 'Receipt contract_sha256 is stale' };
|
|
296
|
+
}
|
|
297
|
+
if (receipt.candidate_sha !== headSha) {
|
|
298
|
+
return {
|
|
299
|
+
exit_code: EXIT_STALE,
|
|
300
|
+
status: 'stale',
|
|
301
|
+
reason: `Receipt candidate_sha (${receipt.candidate_sha}) != HEAD (${headSha})`
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const snap = collectRepoChanges(repoRoot, contract.starting_sha, headSha);
|
|
306
|
+
if (snap.rawDiffSha256 !== receipt.raw_diff_sha256) {
|
|
307
|
+
return {
|
|
308
|
+
exit_code: EXIT_STALE,
|
|
309
|
+
status: 'stale',
|
|
310
|
+
reason: 'Repository raw diff has drifted since verification receipt was issued'
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (contract.worktree.require_clean_end !== false && !snap.workingTreeClean) {
|
|
314
|
+
return { exit_code: EXIT_STALE, status: 'stale', reason: 'Working tree dirty since receipt' };
|
|
315
|
+
}
|
|
316
|
+
if (receipt.status !== 'pass' || receipt.exit_code !== EXIT_PASS) {
|
|
317
|
+
return {
|
|
318
|
+
exit_code: receipt.exit_code,
|
|
319
|
+
status: receipt.status,
|
|
320
|
+
reason: `Receipt status is "${receipt.status}"`
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let latestReviewSha ;
|
|
325
|
+
let validReviewsCount = 0;
|
|
326
|
+
const requiredReviews = contract.review_policy?.required_reviews ?? 1;
|
|
327
|
+
const reviewerSecret = options.reviewerSecret ?? process.env.DIFFPACT_REVIEWER_SECRET;
|
|
328
|
+
|
|
329
|
+
const reviewFiles = [];
|
|
330
|
+
const reviewsDir = path.join(pactDir, 'reviews');
|
|
331
|
+
if (fs.existsSync(reviewsDir)) {
|
|
332
|
+
for (const f of fs.readdirSync(reviewsDir).filter((x) => x.endsWith('.json')).sort()) {
|
|
333
|
+
reviewFiles.push(path.join(reviewsDir, f));
|
|
334
|
+
}
|
|
335
|
+
} else if (fs.existsSync(path.join(pactDir, 'review.json'))) {
|
|
336
|
+
reviewFiles.push(path.join(pactDir, 'review.json'));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
let reviewStatus = 'missing';
|
|
340
|
+
let reviewGateFailure = null;
|
|
341
|
+
|
|
342
|
+
if (reviewFiles.length === 0) {
|
|
343
|
+
reviewStatus = 'missing';
|
|
344
|
+
reviewGateFailure = {
|
|
345
|
+
exit_code: EXIT_INCOMPLETE,
|
|
346
|
+
status: 'incomplete',
|
|
347
|
+
reason: 'Missing review attestation (--require-review)'
|
|
348
|
+
};
|
|
349
|
+
} else {
|
|
350
|
+
const distinctAgents = new Set ();
|
|
351
|
+
const distinctLineages = new Set ();
|
|
352
|
+
let sawStaleReview = false;
|
|
353
|
+
|
|
354
|
+
for (const rf of reviewFiles) {
|
|
355
|
+
let rev ;
|
|
356
|
+
try {
|
|
357
|
+
rev = validateReviewBasic(JSON.parse(fs.readFileSync(rf, 'utf8')));
|
|
358
|
+
} catch (err) {
|
|
359
|
+
reviewStatus = 'invalid';
|
|
360
|
+
reviewGateFailure = {
|
|
361
|
+
exit_code: EXIT_VIOLATION,
|
|
362
|
+
status: 'violation',
|
|
363
|
+
reason: `Malformed review attestation: ${(err ).message}`
|
|
364
|
+
};
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
if (!verifyReviewIntegrity(rev)) {
|
|
368
|
+
reviewStatus = 'invalid';
|
|
369
|
+
reviewGateFailure = {
|
|
370
|
+
exit_code: EXIT_VIOLATION,
|
|
371
|
+
status: 'violation',
|
|
372
|
+
reason: 'Tampered review SHA-256'
|
|
373
|
+
};
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
if (reviewerSecret && reviewerSecret.length > 0 && !verifyReviewHmac(rev, reviewerSecret)) {
|
|
377
|
+
reviewStatus = 'invalid';
|
|
378
|
+
reviewGateFailure = {
|
|
379
|
+
exit_code: EXIT_VIOLATION,
|
|
380
|
+
status: 'violation',
|
|
381
|
+
reason: `Review "${rev.reviewer_session.agent_id}" failed DIFFPACT_REVIEWER_SECRET HMAC verification`
|
|
382
|
+
};
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
if (
|
|
386
|
+
rev.candidate_sha !== headSha ||
|
|
387
|
+
rev.contract_sha256 !== contractSha256 ||
|
|
388
|
+
rev.receipt_sha256 !== receipt.receipt_sha256 ||
|
|
389
|
+
rev.raw_diff_sha256 !== snap.rawDiffSha256
|
|
390
|
+
) {
|
|
391
|
+
sawStaleReview = true;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (rev.verdict === 'reject') {
|
|
395
|
+
reviewStatus = 'rejected';
|
|
396
|
+
reviewGateFailure = {
|
|
397
|
+
exit_code: EXIT_VIOLATION,
|
|
398
|
+
status: 'violation',
|
|
399
|
+
reason: `Rejected by reviewer ${rev.reviewer_session.agent_id}`
|
|
400
|
+
};
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
if (
|
|
404
|
+
rev.reviewer_session.agent_id === contract.implementer_session.agent_id ||
|
|
405
|
+
rev.reviewer_session.session_id === contract.implementer_session.session_id
|
|
406
|
+
) {
|
|
407
|
+
reviewStatus = 'invalid';
|
|
408
|
+
reviewGateFailure = {
|
|
409
|
+
exit_code: EXIT_VIOLATION,
|
|
410
|
+
status: 'violation',
|
|
411
|
+
reason: 'Self-certified review detected'
|
|
412
|
+
};
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
if (
|
|
416
|
+
contract.review_policy?.require_distinct_harness &&
|
|
417
|
+
rev.reviewer_session.harness === contract.implementer_session.harness
|
|
418
|
+
) {
|
|
419
|
+
reviewStatus = 'invalid';
|
|
420
|
+
reviewGateFailure = {
|
|
421
|
+
exit_code: EXIT_VIOLATION,
|
|
422
|
+
status: 'violation',
|
|
423
|
+
reason: 'Reviewer harness collides with implementer harness'
|
|
424
|
+
};
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
if (contract.review_policy?.require_raw_diff_inspection !== false && !rev.inspected_raw_diff) {
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
distinctAgents.add(rev.reviewer_session.agent_id);
|
|
431
|
+
distinctLineages.add(rev.evidence_lineage_id);
|
|
432
|
+
latestReviewSha = rev.review_sha256;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (!reviewGateFailure) {
|
|
436
|
+
validReviewsCount = distinctAgents.size;
|
|
437
|
+
if (distinctAgents.size === 0 && sawStaleReview) {
|
|
438
|
+
reviewStatus = 'stale';
|
|
439
|
+
reviewGateFailure = {
|
|
440
|
+
exit_code: EXIT_STALE,
|
|
441
|
+
status: 'stale',
|
|
442
|
+
reason: 'Stale review attestation against current HEAD/receipt'
|
|
443
|
+
};
|
|
444
|
+
} else if (distinctAgents.size < requiredReviews) {
|
|
445
|
+
reviewStatus = 'incomplete';
|
|
446
|
+
reviewGateFailure = {
|
|
447
|
+
exit_code: EXIT_INCOMPLETE,
|
|
448
|
+
status: 'incomplete',
|
|
449
|
+
reason: `Have ${distinctAgents.size}/${requiredReviews} independent review(s)`
|
|
450
|
+
};
|
|
451
|
+
} else if (distinctLineages.size < requiredReviews) {
|
|
452
|
+
reviewStatus = 'incomplete';
|
|
453
|
+
reviewGateFailure = {
|
|
454
|
+
exit_code: EXIT_INCOMPLETE,
|
|
455
|
+
status: 'incomplete',
|
|
456
|
+
reason: `Echo-chamber lineage collision: ${distinctLineages.size} distinct evidence lineage(s) for ${requiredReviews} required reviews`
|
|
457
|
+
};
|
|
458
|
+
} else {
|
|
459
|
+
reviewStatus = 'approved';
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (options.requireReview && reviewGateFailure) {
|
|
465
|
+
return {
|
|
466
|
+
...reviewGateFailure,
|
|
467
|
+
task_id: contract.task_id,
|
|
468
|
+
candidate_sha: headSha,
|
|
469
|
+
contract_sha256: contractSha256,
|
|
470
|
+
receipt_sha256: receipt.receipt_sha256,
|
|
471
|
+
review_status: reviewStatus,
|
|
472
|
+
required_reviews: requiredReviews,
|
|
473
|
+
valid_reviews_count: validReviewsCount
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Optional Integrator / Role-Substitution Gate
|
|
478
|
+
const canonicalIntegrator = contract.review_policy?.canonical_integrator_agent_id;
|
|
479
|
+
if (options.integratorAgentId && canonicalIntegrator && options.integratorAgentId !== canonicalIntegrator) {
|
|
480
|
+
const sub = contract.role_substitution;
|
|
481
|
+
const validSub =
|
|
482
|
+
sub &&
|
|
483
|
+
sub.active &&
|
|
484
|
+
!sub.spent &&
|
|
485
|
+
sub.task_id === contract.task_id &&
|
|
486
|
+
sub.substitute_agent_id === options.integratorAgentId &&
|
|
487
|
+
options.integratorAgentId !== contract.implementer_session.agent_id;
|
|
488
|
+
if (!validSub) {
|
|
489
|
+
return {
|
|
490
|
+
exit_code: EXIT_VIOLATION,
|
|
491
|
+
status: 'violation',
|
|
492
|
+
reason: `Integrator "${options.integratorAgentId}" lacks active unspent role_substitution or authored the candidate itself`
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
let passReason = 'Verified pass';
|
|
498
|
+
if (options.requireReview) {
|
|
499
|
+
passReason = `Verified pass (review: approved ${validReviewsCount}/${requiredReviews})`;
|
|
500
|
+
} else if (reviewStatus === 'approved') {
|
|
501
|
+
passReason = `Checks verified; review: approved (${validReviewsCount}/${requiredReviews})`;
|
|
502
|
+
} else if (reviewStatus === 'missing') {
|
|
503
|
+
passReason = `Checks verified; review: missing (0/${requiredReviews} — run \`diffpact status --require-review\` before merge)`;
|
|
504
|
+
} else if (reviewStatus === 'stale') {
|
|
505
|
+
passReason = 'Checks verified; review: stale (re-review required before merge)';
|
|
506
|
+
} else if (reviewStatus === 'rejected') {
|
|
507
|
+
passReason = 'Checks verified; review: rejected (run `diffpact status --require-review` to enforce)';
|
|
508
|
+
} else if (reviewStatus === 'invalid') {
|
|
509
|
+
passReason = 'Checks verified; review: invalid (run `diffpact status --require-review` for details)';
|
|
510
|
+
} else {
|
|
511
|
+
passReason = `Checks verified; review: incomplete (${validReviewsCount}/${requiredReviews} — run \`diffpact status --require-review\` before merge)`;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
return {
|
|
515
|
+
exit_code: EXIT_PASS,
|
|
516
|
+
status: 'pass',
|
|
517
|
+
reason: passReason,
|
|
518
|
+
task_id: contract.task_id,
|
|
519
|
+
candidate_sha: headSha,
|
|
520
|
+
contract_sha256: contractSha256,
|
|
521
|
+
receipt_sha256: receipt.receipt_sha256,
|
|
522
|
+
...(latestReviewSha ? { review_sha256: latestReviewSha } : {}),
|
|
523
|
+
review_status: reviewStatus,
|
|
524
|
+
required_reviews: requiredReviews,
|
|
525
|
+
valid_reviews_count: validReviewsCount
|
|
526
|
+
};
|
|
527
|
+
}
|