diffpact 0.0.0-stage → 0.4.3
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 +142 -0
- package/LICENSE +21 -0
- package/README.md +223 -2
- package/bin/diffpact.mjs +15 -0
- package/dist/adapters/claude-code.d.ts +21 -0
- package/dist/adapters/claude-code.js +191 -0
- package/dist/adapters/codex.d.ts +31 -0
- package/dist/adapters/codex.js +348 -0
- package/dist/adapters/gemini-agy.d.ts +26 -0
- package/dist/adapters/gemini-agy.js +429 -0
- package/dist/adapters/vercel-board.d.ts +30 -0
- package/dist/adapters/vercel-board.js +138 -0
- package/dist/amend.d.ts +46 -0
- package/dist/amend.js +476 -0
- package/dist/boundaries.d.ts +43 -0
- package/dist/boundaries.js +309 -0
- package/dist/canonical-json.d.ts +6 -0
- package/dist/canonical-json.js +63 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1704 -0
- package/dist/delegate.d.ts +22 -0
- package/dist/delegate.js +140 -0
- package/dist/doctor.d.ts +36 -0
- package/dist/doctor.js +293 -0
- package/dist/git.d.ts +27 -0
- package/dist/git.js +234 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/merge-verify.d.ts +31 -0
- package/dist/merge-verify.js +721 -0
- package/dist/report.d.ts +25 -0
- package/dist/report.js +220 -0
- package/dist/review.d.ts +46 -0
- package/dist/review.js +527 -0
- package/dist/runner.d.ts +45 -0
- package/dist/runner.js +213 -0
- package/dist/schema.d.ts +6 -0
- package/dist/schema.js +424 -0
- package/dist/types.d.ts +190 -0
- package/dist/types.js +220 -0
- package/dist/verify.d.ts +45 -0
- package/dist/verify.js +952 -0
- package/dist/worktree.d.ts +82 -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/amend.js
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { canonicalSha256 } from './canonical-json.js';
|
|
4
|
+
import { commitExists, resolveCommitSha, resolveRepoRoot } from './git.js';
|
|
5
|
+
import { validateContract } from './schema.js';
|
|
6
|
+
import {
|
|
7
|
+
EXIT_INCOMPLETE,
|
|
8
|
+
EXIT_PASS,
|
|
9
|
+
EXIT_STALE,
|
|
10
|
+
EXIT_VIOLATION,
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
} from './types.js';
|
|
14
|
+
|
|
15
|
+
const HEX40 = /^[0-9a-f]{40}$/;
|
|
16
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
17
|
+
const AMENDMENT_SCHEMA_VERSION = 'diffpact-amendment-v0.2' ;
|
|
18
|
+
const AMENDMENT_HASH_DOMAIN = 'diffpact/amendment/v0.2';
|
|
19
|
+
const CONTRACT_HASH_DOMAIN = 'diffpact/contract/v0.1';
|
|
20
|
+
|
|
21
|
+
const ALLOWED_AMENDMENT_KEYS = new Set ([
|
|
22
|
+
'schema_version',
|
|
23
|
+
'sequence',
|
|
24
|
+
'amended_at',
|
|
25
|
+
'authorized_by',
|
|
26
|
+
'reason',
|
|
27
|
+
'previous_contract_sha256',
|
|
28
|
+
'new_contract_sha256',
|
|
29
|
+
'added_allowed_paths',
|
|
30
|
+
'added_read_only_paths',
|
|
31
|
+
'added_forbidden_paths',
|
|
32
|
+
'previous_starting_sha',
|
|
33
|
+
'new_starting_sha',
|
|
34
|
+
'entry_sha256'
|
|
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
|
+
function isPlainObject(value ) {
|
|
65
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isNonEmptySafeString(value ) {
|
|
69
|
+
return typeof value === 'string' && value.trim().length > 0 && !value.includes('\0');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isNonEmptyStringArray(value ) {
|
|
73
|
+
return Array.isArray(value) && value.every((item) => isNonEmptySafeString(item));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function mergeUniquePaths(
|
|
77
|
+
existing ,
|
|
78
|
+
additions
|
|
79
|
+
) {
|
|
80
|
+
const seen = new Set (existing);
|
|
81
|
+
const merged = [...existing];
|
|
82
|
+
const added = [];
|
|
83
|
+
|
|
84
|
+
for (const raw of additions) {
|
|
85
|
+
if (typeof raw === 'string') {
|
|
86
|
+
const normalized = raw.normalize('NFC').replace(/\\/g, '/');
|
|
87
|
+
if (seen.has(normalized)) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
seen.add(normalized);
|
|
91
|
+
merged.push(raw);
|
|
92
|
+
added.push(normalized);
|
|
93
|
+
} else {
|
|
94
|
+
merged.push(raw );
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { merged, added };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function parseAndValidateEntry(
|
|
102
|
+
raw ,
|
|
103
|
+
expectedSequence ,
|
|
104
|
+
lineNumber
|
|
105
|
+
) {
|
|
106
|
+
if (!isPlainObject(raw)) {
|
|
107
|
+
return { ok: false, error: `Amendment line ${lineNumber} must be a JSON object` };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
for (const key of Object.keys(raw)) {
|
|
111
|
+
if (!ALLOWED_AMENDMENT_KEYS.has(key)) {
|
|
112
|
+
return { ok: false, error: `Amendment line ${lineNumber} has unexpected property "${key}"` };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (raw.schema_version !== AMENDMENT_SCHEMA_VERSION) {
|
|
117
|
+
return {
|
|
118
|
+
ok: false,
|
|
119
|
+
error: `Amendment line ${lineNumber} has unsupported schema_version "${String(raw.schema_version)}"`
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (
|
|
124
|
+
typeof raw.sequence !== 'number' ||
|
|
125
|
+
!Number.isInteger(raw.sequence) ||
|
|
126
|
+
raw.sequence !== expectedSequence
|
|
127
|
+
) {
|
|
128
|
+
return {
|
|
129
|
+
ok: false,
|
|
130
|
+
error: `Amendment line ${lineNumber} has invalid sequence ${String(raw.sequence)} (expected ${expectedSequence})`
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!isNonEmptySafeString(raw.amended_at)) {
|
|
135
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid amended_at` };
|
|
136
|
+
}
|
|
137
|
+
if (!isNonEmptySafeString(raw.authorized_by)) {
|
|
138
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid authorized_by` };
|
|
139
|
+
}
|
|
140
|
+
if (!isNonEmptySafeString(raw.reason)) {
|
|
141
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid reason` };
|
|
142
|
+
}
|
|
143
|
+
if (typeof raw.previous_contract_sha256 !== 'string' || !HEX64.test(raw.previous_contract_sha256)) {
|
|
144
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid previous_contract_sha256` };
|
|
145
|
+
}
|
|
146
|
+
if (typeof raw.new_contract_sha256 !== 'string' || !HEX64.test(raw.new_contract_sha256)) {
|
|
147
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid new_contract_sha256` };
|
|
148
|
+
}
|
|
149
|
+
if (typeof raw.entry_sha256 !== 'string' || !HEX64.test(raw.entry_sha256)) {
|
|
150
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid entry_sha256` };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (raw.added_allowed_paths !== undefined && !isNonEmptyStringArray(raw.added_allowed_paths)) {
|
|
154
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid added_allowed_paths` };
|
|
155
|
+
}
|
|
156
|
+
if (raw.added_read_only_paths !== undefined && !isNonEmptyStringArray(raw.added_read_only_paths)) {
|
|
157
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid added_read_only_paths` };
|
|
158
|
+
}
|
|
159
|
+
if (raw.added_forbidden_paths !== undefined && !isNonEmptyStringArray(raw.added_forbidden_paths)) {
|
|
160
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid added_forbidden_paths` };
|
|
161
|
+
}
|
|
162
|
+
if (
|
|
163
|
+
raw.previous_starting_sha !== undefined &&
|
|
164
|
+
(typeof raw.previous_starting_sha !== 'string' || !HEX40.test(raw.previous_starting_sha))
|
|
165
|
+
) {
|
|
166
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid previous_starting_sha` };
|
|
167
|
+
}
|
|
168
|
+
if (
|
|
169
|
+
raw.new_starting_sha !== undefined &&
|
|
170
|
+
(typeof raw.new_starting_sha !== 'string' || !HEX40.test(raw.new_starting_sha))
|
|
171
|
+
) {
|
|
172
|
+
return { ok: false, error: `Amendment line ${lineNumber} has invalid new_starting_sha` };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const { entry_sha256: recordedEntrySha, ...bodyWithoutEntrySha } = raw;
|
|
176
|
+
let expectedEntrySha ;
|
|
177
|
+
try {
|
|
178
|
+
expectedEntrySha = canonicalSha256(AMENDMENT_HASH_DOMAIN, bodyWithoutEntrySha);
|
|
179
|
+
} catch (err) {
|
|
180
|
+
return {
|
|
181
|
+
ok: false,
|
|
182
|
+
error: `Amendment line ${lineNumber} cannot be canonicalized: ${(err ).message}`
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (recordedEntrySha !== expectedEntrySha) {
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
error: `Amendment line ${lineNumber} entry_sha256 mismatch: expected ${expectedEntrySha}, got ${recordedEntrySha}`
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const entry = {
|
|
194
|
+
schema_version: AMENDMENT_SCHEMA_VERSION,
|
|
195
|
+
sequence: raw.sequence,
|
|
196
|
+
amended_at: raw.amended_at,
|
|
197
|
+
authorized_by: raw.authorized_by,
|
|
198
|
+
reason: raw.reason,
|
|
199
|
+
previous_contract_sha256: raw.previous_contract_sha256,
|
|
200
|
+
new_contract_sha256: raw.new_contract_sha256,
|
|
201
|
+
...(raw.added_allowed_paths !== undefined ? { added_allowed_paths: raw.added_allowed_paths } : {}),
|
|
202
|
+
...(raw.added_read_only_paths !== undefined ? { added_read_only_paths: raw.added_read_only_paths } : {}),
|
|
203
|
+
...(raw.added_forbidden_paths !== undefined ? { added_forbidden_paths: raw.added_forbidden_paths } : {}),
|
|
204
|
+
...(raw.previous_starting_sha !== undefined ? { previous_starting_sha: raw.previous_starting_sha } : {}),
|
|
205
|
+
...(raw.new_starting_sha !== undefined ? { new_starting_sha: raw.new_starting_sha } : {}),
|
|
206
|
+
entry_sha256: recordedEntrySha
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return { ok: true, entry };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Reads `<pactDir>/amendments.jsonl` and verifies each entry's schema, `entry_sha256`,
|
|
214
|
+
* contiguous 1-based sequence numbers, and `previous_contract_sha256` hash chain.
|
|
215
|
+
*/
|
|
216
|
+
export function readAmendmentHistory(
|
|
217
|
+
contractPath ,
|
|
218
|
+
cwd
|
|
219
|
+
) {
|
|
220
|
+
const contractAbs = path.resolve(cwd ?? process.cwd(), contractPath);
|
|
221
|
+
const pactDir = path.dirname(contractAbs);
|
|
222
|
+
const amendmentsPath = path.join(pactDir, 'amendments.jsonl');
|
|
223
|
+
|
|
224
|
+
if (!fs.existsSync(amendmentsPath)) {
|
|
225
|
+
return { valid: true, entries: [] };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let content ;
|
|
229
|
+
try {
|
|
230
|
+
content = fs.readFileSync(amendmentsPath, 'utf8');
|
|
231
|
+
} catch (err) {
|
|
232
|
+
return {
|
|
233
|
+
valid: false,
|
|
234
|
+
entries: [],
|
|
235
|
+
error: `Failed to read amendments.jsonl: ${(err ).message}`
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const rawLines = content.split(/\r?\n/).filter((line) => line.trim().length > 0);
|
|
240
|
+
const entries = [];
|
|
241
|
+
|
|
242
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
243
|
+
const line = rawLines[i] ;
|
|
244
|
+
const lineNumber = i + 1;
|
|
245
|
+
let parsed ;
|
|
246
|
+
try {
|
|
247
|
+
parsed = JSON.parse(line);
|
|
248
|
+
} catch (err) {
|
|
249
|
+
return {
|
|
250
|
+
valid: false,
|
|
251
|
+
entries,
|
|
252
|
+
error: `Invalid JSON on line ${lineNumber} of amendments.jsonl: ${(err ).message}`
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const validated = parseAndValidateEntry(parsed, lineNumber, lineNumber);
|
|
257
|
+
if (!validated.ok) {
|
|
258
|
+
return { valid: false, entries, error: validated.error };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const prev = entries[i - 1];
|
|
262
|
+
if (prev && validated.entry.previous_contract_sha256 !== prev.new_contract_sha256) {
|
|
263
|
+
return {
|
|
264
|
+
valid: false,
|
|
265
|
+
entries,
|
|
266
|
+
error: `Amendment hash chain broken at sequence ${validated.entry.sequence}: previous_contract_sha256 (${validated.entry.previous_contract_sha256}) does not match prior new_contract_sha256 (${prev.new_contract_sha256})`
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
entries.push(validated.entry);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return { valid: true, entries };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Applies a validated, audited amendment to an existing task contract, invalidates
|
|
278
|
+
* any stale `receipt.json`, and appends a hash-chained record to `amendments.jsonl`.
|
|
279
|
+
*/
|
|
280
|
+
export function amendContract(options )
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
{
|
|
287
|
+
if (!isNonEmptySafeString(options.authorizedBy)) {
|
|
288
|
+
return {
|
|
289
|
+
exitCode: EXIT_VIOLATION,
|
|
290
|
+
error: 'authorizedBy must be a non-empty string'
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
if (!isNonEmptySafeString(options.reason)) {
|
|
294
|
+
return {
|
|
295
|
+
exitCode: EXIT_VIOLATION,
|
|
296
|
+
error: 'reason must be a non-empty string'
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const authorizedBy = options.authorizedBy.trim();
|
|
301
|
+
const reason = options.reason.trim();
|
|
302
|
+
|
|
303
|
+
const rawAddAllowed = options.addAllowedPaths ?? [];
|
|
304
|
+
const rawAddReadOnly = options.addReadOnlyPaths ?? [];
|
|
305
|
+
const rawAddForbidden = options.addForbiddenPaths ?? [];
|
|
306
|
+
const requestedStartingSha =
|
|
307
|
+
options.updateStartingSha !== undefined ? options.updateStartingSha.trim() : undefined;
|
|
308
|
+
const hasStartingShaRequest =
|
|
309
|
+
requestedStartingSha !== undefined && requestedStartingSha.length > 0;
|
|
310
|
+
|
|
311
|
+
if (
|
|
312
|
+
rawAddAllowed.length === 0 &&
|
|
313
|
+
rawAddReadOnly.length === 0 &&
|
|
314
|
+
rawAddForbidden.length === 0 &&
|
|
315
|
+
!hasStartingShaRequest
|
|
316
|
+
) {
|
|
317
|
+
return {
|
|
318
|
+
exitCode: EXIT_INCOMPLETE,
|
|
319
|
+
error:
|
|
320
|
+
'At least one amendment mutation (addAllowedPaths, addReadOnlyPaths, addForbiddenPaths, or updateStartingSha) is required'
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const contractAbs = path.resolve(options.cwd ?? process.cwd(), options.contractPath);
|
|
325
|
+
if (!fs.existsSync(contractAbs)) {
|
|
326
|
+
return {
|
|
327
|
+
exitCode: EXIT_INCOMPLETE,
|
|
328
|
+
error: `Contract file not found: "${contractAbs}"`
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let contract ;
|
|
333
|
+
try {
|
|
334
|
+
contract = validateContract(JSON.parse(fs.readFileSync(contractAbs, 'utf8')));
|
|
335
|
+
} catch (err) {
|
|
336
|
+
return {
|
|
337
|
+
exitCode: EXIT_INCOMPLETE,
|
|
338
|
+
error: `Invalid existing contract: ${(err ).message}`
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const previousContractSha256 = canonicalSha256(CONTRACT_HASH_DOMAIN, contract);
|
|
343
|
+
|
|
344
|
+
if (contract.task_type === 'research' && rawAddAllowed.length > 0) {
|
|
345
|
+
return {
|
|
346
|
+
exitCode: EXIT_VIOLATION,
|
|
347
|
+
error: 'Research contracts cannot be granted write paths via addAllowedPaths'
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const history = readAmendmentHistory(contractAbs, options.cwd);
|
|
352
|
+
if (!history.valid) {
|
|
353
|
+
return {
|
|
354
|
+
exitCode: EXIT_VIOLATION,
|
|
355
|
+
error: history.error ?? 'Existing amendment history failed integrity verification'
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const lastEntry = history.entries[history.entries.length - 1];
|
|
360
|
+
if (lastEntry && lastEntry.new_contract_sha256 !== previousContractSha256) {
|
|
361
|
+
return {
|
|
362
|
+
exitCode: EXIT_VIOLATION,
|
|
363
|
+
error: `Contract hash mismatch with amendment log: latest amendment recorded ${lastEntry.new_contract_sha256}, but current contract hashes to ${previousContractSha256}`
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
let resolvedUpdatedStartingSha ;
|
|
368
|
+
if (hasStartingShaRequest) {
|
|
369
|
+
const workDir = options.cwd ?? path.dirname(contractAbs);
|
|
370
|
+
let repoRoot ;
|
|
371
|
+
try {
|
|
372
|
+
repoRoot = resolveRepoRoot(workDir);
|
|
373
|
+
} catch (err) {
|
|
374
|
+
return {
|
|
375
|
+
exitCode: EXIT_INCOMPLETE,
|
|
376
|
+
error: `Git repository unavailable at "${workDir}": ${(err ).message}`
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const fullSha = resolveCommitSha(repoRoot, requestedStartingSha);
|
|
381
|
+
if (!fullSha || !commitExists(repoRoot, fullSha)) {
|
|
382
|
+
return {
|
|
383
|
+
exitCode: EXIT_STALE,
|
|
384
|
+
error: `Commit "${requestedStartingSha}" does not exist in Git repository`
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
resolvedUpdatedStartingSha = fullSha;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const allowedMerge = mergeUniquePaths(contract.allowed_paths, rawAddAllowed);
|
|
391
|
+
const readOnlyMerge = mergeUniquePaths(contract.read_only_paths ?? [], rawAddReadOnly);
|
|
392
|
+
const forbiddenMerge = mergeUniquePaths(contract.forbidden_paths, rawAddForbidden);
|
|
393
|
+
|
|
394
|
+
const nextStartingSha =
|
|
395
|
+
hasStartingShaRequest && resolvedUpdatedStartingSha
|
|
396
|
+
? resolvedUpdatedStartingSha
|
|
397
|
+
: contract.starting_sha;
|
|
398
|
+
const startingShaChanged = nextStartingSha !== contract.starting_sha;
|
|
399
|
+
|
|
400
|
+
const candidate = {
|
|
401
|
+
...contract,
|
|
402
|
+
starting_sha: nextStartingSha,
|
|
403
|
+
allowed_paths: allowedMerge.merged,
|
|
404
|
+
forbidden_paths: forbiddenMerge.merged,
|
|
405
|
+
...(contract.read_only_paths !== undefined || rawAddReadOnly.length > 0
|
|
406
|
+
? { read_only_paths: readOnlyMerge.merged }
|
|
407
|
+
: {})
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
let updatedContract ;
|
|
411
|
+
try {
|
|
412
|
+
updatedContract = validateContract(candidate);
|
|
413
|
+
} catch (err) {
|
|
414
|
+
return {
|
|
415
|
+
exitCode: EXIT_VIOLATION,
|
|
416
|
+
error: `Amended contract failed schema validation: ${(err ).message}`
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const newContractSha256 = canonicalSha256(CONTRACT_HASH_DOMAIN, updatedContract);
|
|
421
|
+
|
|
422
|
+
if (
|
|
423
|
+
allowedMerge.added.length === 0 &&
|
|
424
|
+
readOnlyMerge.added.length === 0 &&
|
|
425
|
+
forbiddenMerge.added.length === 0 &&
|
|
426
|
+
!startingShaChanged
|
|
427
|
+
) {
|
|
428
|
+
return {
|
|
429
|
+
exitCode: EXIT_INCOMPLETE,
|
|
430
|
+
error: 'Amendment produced no actual changes to the contract'
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const entryBody = {
|
|
435
|
+
schema_version: AMENDMENT_SCHEMA_VERSION,
|
|
436
|
+
sequence: history.entries.length + 1,
|
|
437
|
+
amended_at: new Date().toISOString(),
|
|
438
|
+
authorized_by: authorizedBy,
|
|
439
|
+
reason,
|
|
440
|
+
previous_contract_sha256: previousContractSha256,
|
|
441
|
+
new_contract_sha256: newContractSha256,
|
|
442
|
+
...(allowedMerge.added.length > 0 ? { added_allowed_paths: allowedMerge.added } : {}),
|
|
443
|
+
...(readOnlyMerge.added.length > 0 ? { added_read_only_paths: readOnlyMerge.added } : {}),
|
|
444
|
+
...(forbiddenMerge.added.length > 0 ? { added_forbidden_paths: forbiddenMerge.added } : {}),
|
|
445
|
+
...(startingShaChanged
|
|
446
|
+
? {
|
|
447
|
+
previous_starting_sha: contract.starting_sha,
|
|
448
|
+
new_starting_sha: updatedContract.starting_sha
|
|
449
|
+
}
|
|
450
|
+
: {})
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
const entrySha256 = canonicalSha256(AMENDMENT_HASH_DOMAIN, entryBody);
|
|
454
|
+
const entry = {
|
|
455
|
+
...entryBody,
|
|
456
|
+
entry_sha256: entrySha256
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const pactDir = path.dirname(contractAbs);
|
|
460
|
+
fs.writeFileSync(contractAbs, JSON.stringify(updatedContract, null, 2) + '\n', 'utf8');
|
|
461
|
+
|
|
462
|
+
const receiptPath = path.join(pactDir, 'receipt.json');
|
|
463
|
+
if (fs.existsSync(receiptPath)) {
|
|
464
|
+
fs.rmSync(receiptPath, { force: true });
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const amendmentsPath = path.join(pactDir, 'amendments.jsonl');
|
|
468
|
+
fs.appendFileSync(amendmentsPath, JSON.stringify(entry) + '\n', 'utf8');
|
|
469
|
+
|
|
470
|
+
return {
|
|
471
|
+
exitCode: EXIT_PASS,
|
|
472
|
+
contractSha256: newContractSha256,
|
|
473
|
+
entry,
|
|
474
|
+
amendmentCount: history.entries.length + 1
|
|
475
|
+
};
|
|
476
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { TaskContractV01, ViolationItem } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a path to a safe repo-relative POSIX path (Unicode NFC normalized),
|
|
4
|
+
* or returns a violation if the path escapes the repository root or uses ".." traversal.
|
|
5
|
+
*/
|
|
6
|
+
export declare function normalizeRepoRelativePath(inputPath: string, repoRoot: string): {
|
|
7
|
+
ok: true;
|
|
8
|
+
relPath: string;
|
|
9
|
+
} | {
|
|
10
|
+
ok: false;
|
|
11
|
+
reason: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function matchesAnyGlob(relPosixPath: string, globs?: readonly string[]): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Deterministic boundary check with strict precedence:
|
|
16
|
+
* 0. .git/ repository metadata & hooks are unconditionally forbidden regardless of allowed_paths.
|
|
17
|
+
* 1. forbidden_paths (deny wins)
|
|
18
|
+
* 2. read_only_paths (deny wins)
|
|
19
|
+
* 3. Internal .diffpact/ metadata files are allowed by the harness itself unless listed in forbidden/read_only.
|
|
20
|
+
* 4. allowed_paths (must match at least one; empty allowed_paths denies everything)
|
|
21
|
+
*/
|
|
22
|
+
export declare function evaluatePathBoundary(rawPath: string, repoRoot: string, contract: Pick<TaskContractV01, 'allowed_paths' | 'read_only_paths' | 'forbidden_paths'>): ViolationItem | null;
|
|
23
|
+
/**
|
|
24
|
+
* Extracts all target file paths from a Codex `apply_patch` payload.
|
|
25
|
+
*/
|
|
26
|
+
export declare function extractCodexApplyPatchPaths(patchText: string): string[];
|
|
27
|
+
/**
|
|
28
|
+
* Parses a standard Git unified diff (e.g., from `agy-worker --diff` or `git diff`),
|
|
29
|
+
* returning touched paths and detecting any symlink (`120000`) or submodule (`160000`) entries.
|
|
30
|
+
*/
|
|
31
|
+
export declare function inspectUnifiedDiff(diffText: string): {
|
|
32
|
+
paths: string[];
|
|
33
|
+
hasSymlinkMode: boolean;
|
|
34
|
+
symlinkPaths: string[];
|
|
35
|
+
hasSubmoduleMode: boolean;
|
|
36
|
+
submodulePaths: string[];
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Pre-flight defense-in-depth guard for agent shell commands
|
|
40
|
+
* (Claude Code Bash, Codex Bash, Gemini/agy shell).
|
|
41
|
+
* Authoritative enforcement always re-runs against Git tree & status in `runVerify`.
|
|
42
|
+
*/
|
|
43
|
+
export declare function evaluateShellCommand(commandLine: string, contract: Pick<TaskContractV01, 'allowed_paths' | 'read_only_paths' | 'forbidden_paths'>): ViolationItem | null;
|