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
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
evaluatePathBoundary,
|
|
5
|
+
evaluateShellCommand,
|
|
6
|
+
inspectUnifiedDiff
|
|
7
|
+
} from '../boundaries.js';
|
|
8
|
+
import { resolveRepoRoot } from '../git.js';
|
|
9
|
+
import { validateContract } from '../schema.js';
|
|
10
|
+
import { EXIT_INCOMPLETE, EXIT_PASS, EXIT_VIOLATION, } from '../types.js';
|
|
11
|
+
import { evaluateBoundedStopGate, } from './claude-code.js';
|
|
12
|
+
|
|
13
|
+
function formatAgyErrorEnvelope(eventLower , payload , reason ) {
|
|
14
|
+
if (eventLower === 'sessionstart') {
|
|
15
|
+
return {
|
|
16
|
+
exitCode: 0,
|
|
17
|
+
stdout: JSON.stringify({
|
|
18
|
+
injectSteps: [{ systemMessage: { systemMessage: reason } }]
|
|
19
|
+
})
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (eventLower === 'posttooluse') {
|
|
23
|
+
return { exitCode: 0, stdout: '{}' };
|
|
24
|
+
}
|
|
25
|
+
if (eventLower === 'stop') {
|
|
26
|
+
const term = String(payload.terminationReason ?? '');
|
|
27
|
+
if (payload.continuationActive === true || term === 'USER_CANCELED' || term === 'MAX_INVOCATIONS') {
|
|
28
|
+
return { exitCode: 0, stdout: '{}' };
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
exitCode: 0,
|
|
32
|
+
stdout: JSON.stringify({ decision: 'continue', reason })
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
exitCode: 0,
|
|
37
|
+
stdout: JSON.stringify({ decision: 'deny', reason })
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Antigravity (`agy`) `.agents/hooks.json` protocol handler (camelCase/PascalCase JSON I/O).
|
|
43
|
+
*/
|
|
44
|
+
export function handleAgyHook(
|
|
45
|
+
payload ,
|
|
46
|
+
contractPath ,
|
|
47
|
+
cwdOverride
|
|
48
|
+
) {
|
|
49
|
+
const workspacePaths = Array.isArray(payload.workspacePaths) ? payload.workspacePaths : [];
|
|
50
|
+
const cwd =
|
|
51
|
+
cwdOverride ??
|
|
52
|
+
(typeof workspacePaths[0] === 'string' ? workspacePaths[0] : process.cwd());
|
|
53
|
+
const contractAbs = path.resolve(cwd, contractPath);
|
|
54
|
+
const eventLower = String(payload.hookEventName ?? '').toLowerCase();
|
|
55
|
+
|
|
56
|
+
if (!fs.existsSync(contractAbs)) {
|
|
57
|
+
return formatAgyErrorEnvelope(
|
|
58
|
+
eventLower,
|
|
59
|
+
payload,
|
|
60
|
+
`diffpact [agy]: missing contract at "${contractAbs}"`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let contract;
|
|
65
|
+
try {
|
|
66
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
67
|
+
} catch (err) {
|
|
68
|
+
return formatAgyErrorEnvelope(eventLower, payload, `diffpact [agy]: ${(err ).message}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const repoRoot = resolveRepoRoot(cwd);
|
|
72
|
+
|
|
73
|
+
if (eventLower === 'sessionstart') {
|
|
74
|
+
const msg = `Active diffpact contract "${contract.task_id}": starting_sha=${contract.starting_sha}, allowed_paths=${JSON.stringify(contract.allowed_paths)}, forbidden_paths=${JSON.stringify(contract.forbidden_paths)}.`;
|
|
75
|
+
return {
|
|
76
|
+
exitCode: 0,
|
|
77
|
+
stdout: JSON.stringify({
|
|
78
|
+
injectSteps: [{ systemMessage: { systemMessage: msg } }]
|
|
79
|
+
})
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (eventLower === 'pretooluse') {
|
|
84
|
+
const toolCall =
|
|
85
|
+
typeof payload.toolCall === 'object' && payload.toolCall !== null
|
|
86
|
+
? (payload.toolCall )
|
|
87
|
+
: {};
|
|
88
|
+
const toolName = String(toolCall.name ?? '').toLowerCase();
|
|
89
|
+
const args =
|
|
90
|
+
typeof toolCall.args === 'object' && toolCall.args !== null
|
|
91
|
+
? (toolCall.args )
|
|
92
|
+
: {};
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
toolName === 'write_to_file' ||
|
|
96
|
+
toolName === 'replace_file_content' ||
|
|
97
|
+
toolName === 'multi_replace_file_content' ||
|
|
98
|
+
toolName === 'write_file' ||
|
|
99
|
+
toolName === 'edit_file'
|
|
100
|
+
) {
|
|
101
|
+
const target = String(
|
|
102
|
+
args.TargetFile ?? args.AbsolutePath ?? args.Path ?? args.file_path ?? ''
|
|
103
|
+
);
|
|
104
|
+
const v = evaluatePathBoundary(target, repoRoot, contract);
|
|
105
|
+
if (v) {
|
|
106
|
+
return {
|
|
107
|
+
exitCode: 0,
|
|
108
|
+
stdout: JSON.stringify({
|
|
109
|
+
decision: 'deny',
|
|
110
|
+
reason: `diffpact boundary violation (${v.code}): ${v.message}`
|
|
111
|
+
})
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (toolName === 'run_command') {
|
|
118
|
+
const cmd = String(args.CommandLine ?? args.command ?? '');
|
|
119
|
+
const v = evaluateShellCommand(cmd, contract);
|
|
120
|
+
if (v) {
|
|
121
|
+
return {
|
|
122
|
+
exitCode: 0,
|
|
123
|
+
stdout: JSON.stringify({
|
|
124
|
+
decision: 'deny',
|
|
125
|
+
reason: `diffpact command policy (${v.code}): ${v.message}`
|
|
126
|
+
})
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (eventLower === 'posttooluse') {
|
|
136
|
+
return { exitCode: 0, stdout: '{}' };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (eventLower === 'stop') {
|
|
140
|
+
const term = String(payload.terminationReason ?? '');
|
|
141
|
+
if (term === 'USER_CANCELED' || term === 'MAX_INVOCATIONS') {
|
|
142
|
+
return { exitCode: 0, stdout: '{}' };
|
|
143
|
+
}
|
|
144
|
+
const sessionId = typeof payload.conversationId === 'string' ? payload.conversationId : 'agy-default';
|
|
145
|
+
const gate = evaluateBoundedStopGate({
|
|
146
|
+
contractAbs,
|
|
147
|
+
repoRoot,
|
|
148
|
+
sessionId,
|
|
149
|
+
isContinuation: payload.continuationActive === true
|
|
150
|
+
});
|
|
151
|
+
if (gate.action === 'pass' || gate.action === 'capped') {
|
|
152
|
+
return { exitCode: 0, stdout: '{}' };
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
exitCode: 0,
|
|
156
|
+
stdout: JSON.stringify({
|
|
157
|
+
decision: 'continue',
|
|
158
|
+
reason: `diffpact verify failed (exit ${gate.exitCode}): ${gate.details}`
|
|
159
|
+
})
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return { exitCode: 0, stdout: '{}' };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Gemini CLI `.gemini/settings.json` protocol handler (snake_case I/O, strict JSON-only stdout).
|
|
168
|
+
*/
|
|
169
|
+
export function handleGeminiCliHook(
|
|
170
|
+
payload ,
|
|
171
|
+
contractPath ,
|
|
172
|
+
cwdOverride
|
|
173
|
+
) {
|
|
174
|
+
const cwd = cwdOverride ?? (typeof payload.cwd === 'string' ? payload.cwd : process.cwd());
|
|
175
|
+
const contractAbs = path.resolve(cwd, contractPath);
|
|
176
|
+
if (!fs.existsSync(contractAbs)) {
|
|
177
|
+
return {
|
|
178
|
+
exitCode: 0,
|
|
179
|
+
stdout: JSON.stringify({
|
|
180
|
+
decision: 'deny',
|
|
181
|
+
reason: `Missing diffpact contract at "${contractAbs}"`,
|
|
182
|
+
systemMessage: 'Blocked by diffpact: missing contract.json'
|
|
183
|
+
})
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let contract;
|
|
188
|
+
try {
|
|
189
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
190
|
+
} catch (err) {
|
|
191
|
+
return {
|
|
192
|
+
exitCode: 0,
|
|
193
|
+
stdout: JSON.stringify({
|
|
194
|
+
decision: 'deny',
|
|
195
|
+
reason: (err ).message,
|
|
196
|
+
systemMessage: 'Blocked by diffpact: invalid contract.json'
|
|
197
|
+
})
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const repoRoot = resolveRepoRoot(cwd);
|
|
202
|
+
const eventName = String(payload.hook_event_name ?? '');
|
|
203
|
+
const toolName = String(payload.tool_name ?? '');
|
|
204
|
+
const toolInput =
|
|
205
|
+
typeof payload.tool_input === 'object' && payload.tool_input !== null
|
|
206
|
+
? (payload.tool_input )
|
|
207
|
+
: {};
|
|
208
|
+
|
|
209
|
+
if (eventName === 'BeforeTool') {
|
|
210
|
+
if (toolName === 'write_file' || toolName === 'replace') {
|
|
211
|
+
const target = String(toolInput.file_path ?? toolInput.path ?? '');
|
|
212
|
+
const v = evaluatePathBoundary(target, repoRoot, contract);
|
|
213
|
+
if (v) {
|
|
214
|
+
return {
|
|
215
|
+
exitCode: 0,
|
|
216
|
+
stdout: JSON.stringify({
|
|
217
|
+
decision: 'deny',
|
|
218
|
+
reason: `diffpact (${v.code}): ${v.message}`,
|
|
219
|
+
systemMessage: `Blocked by diffpact: ${v.message}`
|
|
220
|
+
})
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (toolName === 'run_shell_command') {
|
|
227
|
+
const cmd = String(toolInput.command ?? '');
|
|
228
|
+
const v = evaluateShellCommand(cmd, contract);
|
|
229
|
+
if (v) {
|
|
230
|
+
return {
|
|
231
|
+
exitCode: 0,
|
|
232
|
+
stdout: JSON.stringify({
|
|
233
|
+
decision: 'deny',
|
|
234
|
+
reason: `diffpact (${v.code}): ${v.message}`,
|
|
235
|
+
systemMessage: `Blocked by diffpact: ${v.message}`
|
|
236
|
+
})
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (eventName === 'AfterTool') {
|
|
246
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (eventName === 'AfterAgent') {
|
|
250
|
+
const sessionId = typeof payload.session_id === 'string' ? payload.session_id : 'gemini-default';
|
|
251
|
+
const gate = evaluateBoundedStopGate({
|
|
252
|
+
contractAbs,
|
|
253
|
+
repoRoot,
|
|
254
|
+
sessionId,
|
|
255
|
+
isContinuation: payload.stop_hook_active === true
|
|
256
|
+
});
|
|
257
|
+
if (gate.action === 'pass') {
|
|
258
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
259
|
+
}
|
|
260
|
+
if (gate.action === 'capped') {
|
|
261
|
+
return {
|
|
262
|
+
exitCode: 0,
|
|
263
|
+
stdout: JSON.stringify({
|
|
264
|
+
decision: 'allow',
|
|
265
|
+
systemMessage: `diffpact WARNING: AfterAgent block cap (${gate.maxBlocks}) reached without passing receipt.`
|
|
266
|
+
})
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
exitCode: 0,
|
|
271
|
+
stdout: JSON.stringify({
|
|
272
|
+
decision: 'deny',
|
|
273
|
+
reason: `diffpact verify failed (exit ${gate.exitCode}): ${gate.details}`
|
|
274
|
+
})
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return { exitCode: 0, stdout: JSON.stringify({ decision: 'allow' }) };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function unquoteGitShortStatusPath(p ) {
|
|
282
|
+
const trimmed = p.trim();
|
|
283
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') && trimmed.length >= 2) {
|
|
284
|
+
return trimmed.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
|
285
|
+
}
|
|
286
|
+
return trimmed;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Verifies an unattended `agy-worker --diff` output (which prints `git status --short`
|
|
291
|
+
* followed by `git diff HEAD`) BEFORE the integrator runs `git apply`.
|
|
292
|
+
*/
|
|
293
|
+
export function verifyAgyWorkerPatch(options
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
) {
|
|
299
|
+
const cwd = options.cwd ?? process.cwd();
|
|
300
|
+
const contractAbs = path.resolve(cwd, options.contractPath);
|
|
301
|
+
const patchAbs = path.resolve(cwd, options.patchPath);
|
|
302
|
+
|
|
303
|
+
if (!fs.existsSync(contractAbs) || !fs.existsSync(patchAbs)) {
|
|
304
|
+
return {
|
|
305
|
+
exitCode: EXIT_INCOMPLETE,
|
|
306
|
+
paths: [],
|
|
307
|
+
violations: ['Missing contract.json or patch file']
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
let contract;
|
|
312
|
+
try {
|
|
313
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
314
|
+
} catch (err) {
|
|
315
|
+
return {
|
|
316
|
+
exitCode: EXIT_INCOMPLETE,
|
|
317
|
+
paths: [],
|
|
318
|
+
violations: [`Invalid contract.json: ${(err ).message}`]
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const repoRoot = resolveRepoRoot(cwd);
|
|
323
|
+
const rawText = fs.readFileSync(patchAbs, 'utf8');
|
|
324
|
+
|
|
325
|
+
const diffInfo = inspectUnifiedDiff(rawText);
|
|
326
|
+
const allPaths = new Set (diffInfo.paths);
|
|
327
|
+
|
|
328
|
+
for (const line of rawText.split(/\r?\n/)) {
|
|
329
|
+
if (line.startsWith('diff --git ')) break;
|
|
330
|
+
const statusMatch = line.match(/^[ MADRCU?!]{2}\s+(.+?)\s*$/);
|
|
331
|
+
if (statusMatch?.[1]) {
|
|
332
|
+
const targetPart = statusMatch[1];
|
|
333
|
+
if (targetPart.includes(' -> ')) {
|
|
334
|
+
for (const piece of targetPart.split(' -> ')) {
|
|
335
|
+
allPaths.add(unquoteGitShortStatusPath(piece));
|
|
336
|
+
}
|
|
337
|
+
} else {
|
|
338
|
+
allPaths.add(unquoteGitShortStatusPath(targetPart));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const paths = [...allPaths].sort();
|
|
344
|
+
if (paths.length === 0 && !options.allowEmpty) {
|
|
345
|
+
return {
|
|
346
|
+
exitCode: EXIT_INCOMPLETE,
|
|
347
|
+
paths: [],
|
|
348
|
+
violations: ['Patch contains zero touched files (No Silent Zero)']
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const violations = [];
|
|
353
|
+
if (diffInfo.hasSymlinkMode) {
|
|
354
|
+
violations.push(`Symlink mode 120000 forbidden in patch: ${diffInfo.symlinkPaths.join(', ')}`);
|
|
355
|
+
}
|
|
356
|
+
if (diffInfo.hasSubmoduleMode) {
|
|
357
|
+
violations.push(`Submodule mode 160000 forbidden in patch: ${diffInfo.submodulePaths.join(', ')}`);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
for (const p of paths) {
|
|
361
|
+
const v = evaluatePathBoundary(p, repoRoot, contract);
|
|
362
|
+
if (v) {
|
|
363
|
+
violations.push(`[${v.code}] ${v.message}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
exitCode: violations.length > 0 ? EXIT_VIOLATION : EXIT_PASS,
|
|
369
|
+
paths,
|
|
370
|
+
violations
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export function generateAgyConfig(contractPath = '.diffpact/contract.json') {
|
|
375
|
+
const cmd = `diffpact --contract ${contractPath} hook agy`;
|
|
376
|
+
return JSON.stringify(
|
|
377
|
+
{
|
|
378
|
+
'diffpact-guard': {
|
|
379
|
+
enabled: true,
|
|
380
|
+
SessionStart: [{ type: 'command', command: cmd, timeout: 10 }],
|
|
381
|
+
PreToolUse: [
|
|
382
|
+
{
|
|
383
|
+
matcher: 'run_command|write_to_file|replace_file_content|multi_replace_file_content|write_file|edit_file',
|
|
384
|
+
hooks: [{ type: 'command', command: cmd, timeout: 15 }]
|
|
385
|
+
}
|
|
386
|
+
],
|
|
387
|
+
PostToolUse: [
|
|
388
|
+
{
|
|
389
|
+
matcher: 'run_command|write_to_file|replace_file_content|multi_replace_file_content|write_file|edit_file',
|
|
390
|
+
hooks: [{ type: 'command', command: cmd, timeout: 15 }]
|
|
391
|
+
}
|
|
392
|
+
],
|
|
393
|
+
Stop: [{ type: 'command', command: cmd, timeout: 180 }]
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
null,
|
|
397
|
+
2
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function generateGeminiCliConfig(contractPath = '.diffpact/contract.json') {
|
|
402
|
+
const cmd = `diffpact --contract ${contractPath} hook gemini`;
|
|
403
|
+
return JSON.stringify(
|
|
404
|
+
{
|
|
405
|
+
hooks: {
|
|
406
|
+
BeforeTool: [
|
|
407
|
+
{
|
|
408
|
+
matcher: 'write_file|replace|run_shell_command',
|
|
409
|
+
hooks: [{ name: 'diffpact-before-tool', type: 'command', command: cmd }]
|
|
410
|
+
}
|
|
411
|
+
],
|
|
412
|
+
AfterTool: [
|
|
413
|
+
{
|
|
414
|
+
matcher: '*',
|
|
415
|
+
hooks: [{ name: 'diffpact-after-tool', type: 'command', command: cmd }]
|
|
416
|
+
}
|
|
417
|
+
],
|
|
418
|
+
AfterAgent: [
|
|
419
|
+
{
|
|
420
|
+
matcher: '*',
|
|
421
|
+
hooks: [{ name: 'diffpact-after-agent', type: 'command', command: cmd }]
|
|
422
|
+
}
|
|
423
|
+
]
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
null,
|
|
427
|
+
2
|
|
428
|
+
);
|
|
429
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { canonicalSha256 } from '../canonical-json.js';
|
|
5
|
+
import { checkStatus } from '../review.js';
|
|
6
|
+
import { validateContract } from '../schema.js';
|
|
7
|
+
import { EXIT_INCOMPLETE, EXIT_VIOLATION, } from '../types.js';
|
|
8
|
+
|
|
9
|
+
function invokeVb(vbBin , args , cwd ) {
|
|
10
|
+
const parts = vbBin.trim().split(/\s+/);
|
|
11
|
+
const bin = parts[0] ?? 'vb';
|
|
12
|
+
const prefixArgs = parts.slice(1);
|
|
13
|
+
return execFileSync(bin, [...prefixArgs, ...args], {
|
|
14
|
+
cwd,
|
|
15
|
+
encoding: 'utf8',
|
|
16
|
+
maxBuffer: 16 * 1024 * 1024
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function boardPostContract(options
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
) {
|
|
26
|
+
const cwd = options.cwd ?? process.cwd();
|
|
27
|
+
const vbBin = options.vbBin ?? process.env.DIFFPACT_VB_BIN ?? 'vb';
|
|
28
|
+
const contractAbs = path.resolve(cwd, options.contractPath);
|
|
29
|
+
if (!fs.existsSync(contractAbs)) {
|
|
30
|
+
return { exitCode: EXIT_INCOMPLETE, message: `Missing contract at "${contractAbs}"` };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let contract;
|
|
34
|
+
try {
|
|
35
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
36
|
+
} catch (err) {
|
|
37
|
+
return { exitCode: EXIT_INCOMPLETE, message: (err ).message };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const contractSha = canonicalSha256('diffpact/contract/v0.1', contract);
|
|
41
|
+
const msg = [
|
|
42
|
+
`[diffpact contract pinned] task_id=${contract.task_id}`,
|
|
43
|
+
`contract_sha256=${contractSha}`,
|
|
44
|
+
`starting_sha=${contract.starting_sha}`,
|
|
45
|
+
`allowed_paths=${JSON.stringify(contract.allowed_paths)}`,
|
|
46
|
+
`forbidden_paths=${JSON.stringify(contract.forbidden_paths)}`
|
|
47
|
+
].join(' | ');
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
invokeVb(vbBin, ['post', options.taskId, msg], cwd);
|
|
51
|
+
return { exitCode: 0, message: msg };
|
|
52
|
+
} catch (err) {
|
|
53
|
+
return {
|
|
54
|
+
exitCode: EXIT_VIOLATION,
|
|
55
|
+
message: `vb post failed: ${(err ).message}`
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Gates `@vercel/board` task completion (`vb tasks complete`):
|
|
62
|
+
* - Exit 0 (pass + independent review): posts hashes and calls `vb tasks complete`.
|
|
63
|
+
* - Exit 1 (boundary/test/review violation): posts failure details and calls `vb tasks fail`.
|
|
64
|
+
* - Exit 2 or 3 (incomplete / awaiting review / stale): posts hold reason and refuses `complete`
|
|
65
|
+
* without burning the task as terminal `failed`.
|
|
66
|
+
*/
|
|
67
|
+
export function boardCompleteGate(options
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
) {
|
|
76
|
+
const cwd = options.cwd ?? process.cwd();
|
|
77
|
+
const vbBin = options.vbBin ?? process.env.DIFFPACT_VB_BIN ?? 'vb';
|
|
78
|
+
|
|
79
|
+
function completeOrFailWithOptionalReclaim(subcmd , msg ) {
|
|
80
|
+
try {
|
|
81
|
+
invokeVb(vbBin, ['tasks', subcmd, options.taskId, msg], cwd);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
const errText = String((err ).stderr ?? (err ).message);
|
|
84
|
+
if (options.reclaimIfExpired !== false && /Lost the claim|claim on .* is over/i.test(errText)) {
|
|
85
|
+
invokeVb(vbBin, ['tasks', 'claim', options.taskId], cwd);
|
|
86
|
+
invokeVb(vbBin, ['tasks', subcmd, options.taskId, msg], cwd);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
throw err;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const status = checkStatus({
|
|
94
|
+
contractPath: options.contractPath,
|
|
95
|
+
cwd,
|
|
96
|
+
requireReview: options.requireReview ?? true
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (status.exit_code === 0) {
|
|
100
|
+
const summary =
|
|
101
|
+
options.summaryMessage ??
|
|
102
|
+
`diffpact PASS | candidate=${status.candidate_sha} | receipt=${status.receipt_sha256?.slice(0, 12)}${status.review_sha256 ? ` | review=${status.review_sha256.slice(0, 12)}` : ''}`;
|
|
103
|
+
try {
|
|
104
|
+
invokeVb(vbBin, ['post', options.taskId, summary], cwd);
|
|
105
|
+
completeOrFailWithOptionalReclaim('complete', summary);
|
|
106
|
+
return { exitCode: 0, action: 'completed', detail: summary };
|
|
107
|
+
} catch (err) {
|
|
108
|
+
return {
|
|
109
|
+
exitCode: EXIT_VIOLATION,
|
|
110
|
+
action: 'held',
|
|
111
|
+
detail: `vb tasks complete failed: ${(err ).message}`
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (status.exit_code === 1) {
|
|
117
|
+
const failMsg = `diffpact VIOLATION (exit 1): ${status.reason}`;
|
|
118
|
+
try {
|
|
119
|
+
invokeVb(vbBin, ['post', options.taskId, failMsg], cwd);
|
|
120
|
+
completeOrFailWithOptionalReclaim('fail', failMsg);
|
|
121
|
+
return { exitCode: 1, action: 'failed', detail: failMsg };
|
|
122
|
+
} catch (err) {
|
|
123
|
+
return {
|
|
124
|
+
exitCode: 1,
|
|
125
|
+
action: 'failed',
|
|
126
|
+
detail: `${failMsg} (vb notification error: ${(err ).message})`
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const holdMsg = `diffpact HOLD (exit ${status.exit_code}, ${status.status}): ${status.reason}`;
|
|
132
|
+
try {
|
|
133
|
+
invokeVb(vbBin, ['post', options.taskId, holdMsg], cwd);
|
|
134
|
+
} catch {
|
|
135
|
+
// preserve hold exit code
|
|
136
|
+
}
|
|
137
|
+
return { exitCode: status.exit_code, action: 'held', detail: holdMsg };
|
|
138
|
+
}
|