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
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a path to a safe repo-relative POSIX path (Unicode NFC normalized),
|
|
6
|
+
* or returns a violation if the path escapes the repository root or uses ".." traversal.
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeRepoRelativePath(
|
|
9
|
+
inputPath ,
|
|
10
|
+
repoRoot
|
|
11
|
+
) {
|
|
12
|
+
if (!inputPath || inputPath.includes('\0')) {
|
|
13
|
+
return { ok: false, reason: 'Empty or NUL-containing path' };
|
|
14
|
+
}
|
|
15
|
+
const normalizedInput = inputPath.normalize('NFC');
|
|
16
|
+
const rootResolved = path.resolve(repoRoot.normalize('NFC'));
|
|
17
|
+
const candidateResolved = path.isAbsolute(normalizedInput)
|
|
18
|
+
? path.resolve(normalizedInput)
|
|
19
|
+
: path.resolve(rootResolved, normalizedInput);
|
|
20
|
+
|
|
21
|
+
const rel = path.relative(rootResolved, candidateResolved);
|
|
22
|
+
if (rel === '') {
|
|
23
|
+
return { ok: false, reason: 'Path resolves to repository root directory itself' };
|
|
24
|
+
}
|
|
25
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
26
|
+
return {
|
|
27
|
+
ok: false,
|
|
28
|
+
reason: `Path "${inputPath}" escapes repository root "${rootResolved}"`
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return { ok: true, relPath: rel.split(path.sep).join('/').normalize('NFC') };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function globToRegExp(glob ) {
|
|
35
|
+
const norm = glob.normalize('NFC').replace(/\\/g, '/');
|
|
36
|
+
let pattern = '';
|
|
37
|
+
let i = 0;
|
|
38
|
+
while (i < norm.length) {
|
|
39
|
+
if (norm.startsWith('**/', i)) {
|
|
40
|
+
pattern += '(?:.+/)?';
|
|
41
|
+
i += 3;
|
|
42
|
+
} else if (norm.startsWith('/**', i) && i + 3 === norm.length) {
|
|
43
|
+
pattern += '(?:/.*)?';
|
|
44
|
+
i += 3;
|
|
45
|
+
} else if (norm.startsWith('**', i)) {
|
|
46
|
+
pattern += '.*';
|
|
47
|
+
i += 2;
|
|
48
|
+
} else if (norm[i] === '*') {
|
|
49
|
+
pattern += '[^/]*';
|
|
50
|
+
i += 1;
|
|
51
|
+
} else if (norm[i] === '?') {
|
|
52
|
+
pattern += '[^/]';
|
|
53
|
+
i += 1;
|
|
54
|
+
} else {
|
|
55
|
+
pattern += norm[i] .replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
56
|
+
i += 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return new RegExp('^' + pattern + '$');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function matchesAnyGlob(relPosixPath , globs = []) {
|
|
63
|
+
const target = relPosixPath.normalize('NFC');
|
|
64
|
+
return globs.some((g) => globToRegExp(g).test(target));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Deterministic boundary check with strict precedence:
|
|
69
|
+
* 0. .git/ repository metadata & hooks are unconditionally forbidden regardless of allowed_paths.
|
|
70
|
+
* 1. forbidden_paths (deny wins)
|
|
71
|
+
* 2. read_only_paths (deny wins)
|
|
72
|
+
* 3. Internal .diffpact/ metadata files are allowed by the harness itself unless listed in forbidden/read_only.
|
|
73
|
+
* 4. allowed_paths (must match at least one; empty allowed_paths denies everything)
|
|
74
|
+
*/
|
|
75
|
+
export function evaluatePathBoundary(
|
|
76
|
+
rawPath ,
|
|
77
|
+
repoRoot ,
|
|
78
|
+
contract
|
|
79
|
+
) {
|
|
80
|
+
const norm = normalizeRepoRelativePath(rawPath, repoRoot);
|
|
81
|
+
if (!norm.ok) {
|
|
82
|
+
return {
|
|
83
|
+
code: 'PATH_ESCAPE',
|
|
84
|
+
path: rawPath,
|
|
85
|
+
message: norm.reason
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const rel = norm.relPath;
|
|
89
|
+
|
|
90
|
+
if (rel === '.git' || rel.startsWith('.git/')) {
|
|
91
|
+
return {
|
|
92
|
+
code: 'GIT_METADATA_FORBIDDEN',
|
|
93
|
+
path: rel,
|
|
94
|
+
message: `Direct modification of Git metadata or hooks path "${rel}" is strictly forbidden`
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (
|
|
99
|
+
/^(\.claude\/settings(?:\.local)?\.json|\.codex\/hooks\.json|\.gemini\/settings\.json|\.agents\/hooks\.json)$/.test(
|
|
100
|
+
rel
|
|
101
|
+
)
|
|
102
|
+
) {
|
|
103
|
+
return {
|
|
104
|
+
code: 'HARNESS_HOOK_TAMPERING_FORBIDDEN',
|
|
105
|
+
path: rel,
|
|
106
|
+
message: `Modification of active harness hook configuration "${rel}" is forbidden`
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (matchesAnyGlob(rel, contract.forbidden_paths)) {
|
|
111
|
+
return {
|
|
112
|
+
code: 'FORBIDDEN_PATH',
|
|
113
|
+
path: rel,
|
|
114
|
+
message: `Path "${rel}" matches forbidden_paths`
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (matchesAnyGlob(rel, contract.read_only_paths ?? [])) {
|
|
119
|
+
return {
|
|
120
|
+
code: 'READ_ONLY_PATH',
|
|
121
|
+
path: rel,
|
|
122
|
+
message: `Path "${rel}" matches read_only_paths`
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (rel === '.diffpact' || rel.startsWith('.diffpact/')) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (!matchesAnyGlob(rel, contract.allowed_paths)) {
|
|
131
|
+
return {
|
|
132
|
+
code: 'OUTSIDE_ALLOWED_PATHS',
|
|
133
|
+
path: rel,
|
|
134
|
+
message: `Path "${rel}" is outside allowed_paths`
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Extracts all target file paths from a Codex `apply_patch` payload.
|
|
143
|
+
*/
|
|
144
|
+
export function extractCodexApplyPatchPaths(patchText ) {
|
|
145
|
+
const paths = [];
|
|
146
|
+
const lines = patchText.split(/\r?\n/);
|
|
147
|
+
for (const line of lines) {
|
|
148
|
+
const m = line.match(/^\*\*\*\s+(?:Add File|Update File|Delete File|Move to):\s+(.+?)\s*$/);
|
|
149
|
+
if (m && m[1]) {
|
|
150
|
+
paths.push(m[1]);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return paths;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Parses a standard Git unified diff (e.g., from `agy-worker --diff` or `git diff`),
|
|
158
|
+
* returning touched paths and detecting any symlink (`120000`) or submodule (`160000`) entries.
|
|
159
|
+
*/
|
|
160
|
+
export function inspectUnifiedDiff(diffText )
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
{
|
|
167
|
+
const pathSet = new Set ();
|
|
168
|
+
const symlinkPaths = [];
|
|
169
|
+
const submodulePaths = [];
|
|
170
|
+
let currentPath = null;
|
|
171
|
+
|
|
172
|
+
for (const line of diffText.split(/\r?\n/)) {
|
|
173
|
+
const gitHeader = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
|
174
|
+
if (gitHeader) {
|
|
175
|
+
if (gitHeader[1]) pathSet.add(gitHeader[1]);
|
|
176
|
+
if (gitHeader[2]) {
|
|
177
|
+
pathSet.add(gitHeader[2]);
|
|
178
|
+
currentPath = gitHeader[2];
|
|
179
|
+
}
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const renameFrom = line.match(/^rename from (.+)$/);
|
|
183
|
+
if (renameFrom?.[1]) pathSet.add(renameFrom[1]);
|
|
184
|
+
const renameTo = line.match(/^rename to (.+)$/);
|
|
185
|
+
if (renameTo?.[1]) {
|
|
186
|
+
pathSet.add(renameTo[1]);
|
|
187
|
+
currentPath = renameTo[1];
|
|
188
|
+
}
|
|
189
|
+
const plusHeader = line.match(/^\+\+\+ b\/(.+)$/);
|
|
190
|
+
if (plusHeader?.[1] && plusHeader[1] !== '/dev/null') {
|
|
191
|
+
pathSet.add(plusHeader[1]);
|
|
192
|
+
currentPath = plusHeader[1];
|
|
193
|
+
}
|
|
194
|
+
const minusHeader = line.match(/^--- a\/(.+)$/);
|
|
195
|
+
if (minusHeader?.[1] && minusHeader[1] !== '/dev/null') {
|
|
196
|
+
pathSet.add(minusHeader[1]);
|
|
197
|
+
}
|
|
198
|
+
if (/^(?:new file mode|old mode|new mode) 120000\b/.test(line)) {
|
|
199
|
+
symlinkPaths.push(currentPath ?? '<unknown-symlink>');
|
|
200
|
+
}
|
|
201
|
+
if (
|
|
202
|
+
/^(?:new file mode|old mode|new mode) 160000\b/.test(line) ||
|
|
203
|
+
/^[+-]?Subproject commit [0-9a-f]{40}\b/.test(line)
|
|
204
|
+
) {
|
|
205
|
+
if (currentPath && !submodulePaths.includes(currentPath)) {
|
|
206
|
+
submodulePaths.push(currentPath);
|
|
207
|
+
} else if (!currentPath) {
|
|
208
|
+
submodulePaths.push('<unknown-submodule>');
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
paths: [...pathSet],
|
|
215
|
+
hasSymlinkMode: symlinkPaths.length > 0,
|
|
216
|
+
symlinkPaths,
|
|
217
|
+
hasSubmoduleMode: submodulePaths.length > 0,
|
|
218
|
+
submodulePaths
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const DESTRUCTIVE_GIT_PATTERNS = [
|
|
223
|
+
{
|
|
224
|
+
code: 'FORBIDDEN_GIT_PUSH',
|
|
225
|
+
regex: /(?:^|[;&|()\s])git\s+(?:\S+\s+)*push(?:\s|$)/,
|
|
226
|
+
reason: 'Direct git push is forbidden inside a bounded worker session'
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
code: 'FORBIDDEN_GIT_HISTORY_REWRITE',
|
|
230
|
+
regex: /(?:^|[;&|()\s])git\s+(?:\S+\s+)*(?:rebase|reset\s+--hard|clean\s+-[a-zA-Z]*f|checkout\s+-f|submodule\s+add)(?:\s|$)/,
|
|
231
|
+
reason: 'Destructive Git history/worktree rewrite or submodule add command is forbidden'
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
code: 'GIT_METADATA_FORBIDDEN',
|
|
235
|
+
regex: /(?:^|[\s'"=/])\.git\/(?:hooks|config|HEAD|info|objects|refs)\b/,
|
|
236
|
+
reason: 'Shell commands may not target .git metadata or hooks'
|
|
237
|
+
}
|
|
238
|
+
];
|
|
239
|
+
|
|
240
|
+
function extractStaticGlobPrefix(glob ) {
|
|
241
|
+
const norm = glob.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
242
|
+
const idx = norm.search(/[*?]/);
|
|
243
|
+
if (idx === -1) return norm;
|
|
244
|
+
const prefix = norm.slice(0, idx);
|
|
245
|
+
return prefix.length >= 2 ? prefix : null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function extractCandidatePathTokens(commandLine ) {
|
|
249
|
+
return commandLine
|
|
250
|
+
.split(/[\s;|&()'"`,=<>]+/)
|
|
251
|
+
.map((t) => t.replace(/^\.\//, '').trim())
|
|
252
|
+
.filter((t) => t.length > 0 && !t.startsWith('-'));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Pre-flight defense-in-depth guard for agent shell commands
|
|
257
|
+
* (Claude Code Bash, Codex Bash, Gemini/agy shell).
|
|
258
|
+
* Authoritative enforcement always re-runs against Git tree & status in `runVerify`.
|
|
259
|
+
*/
|
|
260
|
+
export function evaluateShellCommand(
|
|
261
|
+
commandLine ,
|
|
262
|
+
contract
|
|
263
|
+
) {
|
|
264
|
+
for (const rule of DESTRUCTIVE_GIT_PATTERNS) {
|
|
265
|
+
if (rule.regex.test(commandLine)) {
|
|
266
|
+
return { code: rule.code, message: rule.reason };
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const mutatingOrInterpreter =
|
|
271
|
+
/\b(?:rm|mv|cp|touch|truncate|chmod|chown|install|dd|patch|python[23]?|node|deno|bun|perl|ruby|php)\b|sed\s+-i|(?:^|[^0-9])>>?\s*\S|\btee\s+|\b(?:sh|bash|zsh)\s+-c\b/;
|
|
272
|
+
|
|
273
|
+
if (mutatingOrInterpreter.test(commandLine)) {
|
|
274
|
+
const isDiffpactCli = /^\s*(?:npx\s+)?diffpact\b/.test(commandLine);
|
|
275
|
+
if (!isDiffpactCli && /(?:^|[\s'"=/])\.diffpact(?:\/|['"\s]|$)/.test(commandLine)) {
|
|
276
|
+
return {
|
|
277
|
+
code: 'FORBIDDEN_CONTRACT_TAMPERING',
|
|
278
|
+
path: '.diffpact',
|
|
279
|
+
message: 'Shell commands and inline scripts may not modify or tamper with .diffpact/ artifacts'
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const protectedGlobs = [...contract.forbidden_paths, ...(contract.read_only_paths ?? [])];
|
|
284
|
+
if (protectedGlobs.length > 0) {
|
|
285
|
+
const tokens = extractCandidatePathTokens(commandLine);
|
|
286
|
+
for (const tok of tokens) {
|
|
287
|
+
if (matchesAnyGlob(tok, protectedGlobs)) {
|
|
288
|
+
return {
|
|
289
|
+
code: 'SHELL_TARGETS_PROTECTED_PATH',
|
|
290
|
+
path: tok,
|
|
291
|
+
message: `Mutating shell or script command references protected path "${tok}"`
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
for (const g of protectedGlobs) {
|
|
296
|
+
const prefix = extractStaticGlobPrefix(g);
|
|
297
|
+
if (prefix && commandLine.includes(prefix)) {
|
|
298
|
+
return {
|
|
299
|
+
code: 'SHELL_TARGETS_PROTECTED_PATH',
|
|
300
|
+
path: g,
|
|
301
|
+
message: `Mutating shell or script command references protected prefix "${prefix}" (from "${g}")`
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 8785 (JCS) deterministic JSON serializer for finite JSON values.
|
|
3
|
+
*/
|
|
4
|
+
export declare function canonicalJson(value: unknown): string;
|
|
5
|
+
export declare function sha256Hex(input: string | Buffer): string;
|
|
6
|
+
export declare function canonicalSha256(domain: string, projection: unknown): string;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Rejects lone UTF-16 surrogates so canonical UTF-8 bytes remain injective.
|
|
5
|
+
*/
|
|
6
|
+
function assertWellFormedString(str ) {
|
|
7
|
+
for (let i = 0; i < str.length; i++) {
|
|
8
|
+
const code = str.charCodeAt(i);
|
|
9
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
10
|
+
const next = str.charCodeAt(i + 1);
|
|
11
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) {
|
|
12
|
+
throw new Error('Lone high UTF-16 surrogate rejected in canonical JSON');
|
|
13
|
+
}
|
|
14
|
+
i++;
|
|
15
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
16
|
+
throw new Error('Lone low UTF-16 surrogate rejected in canonical JSON');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* RFC 8785 (JCS) deterministic JSON serializer for finite JSON values.
|
|
23
|
+
*/
|
|
24
|
+
export function canonicalJson(value ) {
|
|
25
|
+
if (value === null) return 'null';
|
|
26
|
+
const t = typeof value;
|
|
27
|
+
if (t === 'boolean') return value ? 'true' : 'false';
|
|
28
|
+
if (t === 'number') {
|
|
29
|
+
if (!Number.isFinite(value )) {
|
|
30
|
+
throw new Error('Non-finite number rejected in canonical JSON');
|
|
31
|
+
}
|
|
32
|
+
if (Object.is(value, -0)) return '0';
|
|
33
|
+
return JSON.stringify(value);
|
|
34
|
+
}
|
|
35
|
+
if (t === 'string') {
|
|
36
|
+
assertWellFormedString(value );
|
|
37
|
+
return JSON.stringify(value);
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
return '[' + value.map((item) => canonicalJson(item)).join(',') + ']';
|
|
41
|
+
}
|
|
42
|
+
if (t === 'object') {
|
|
43
|
+
const record = value ;
|
|
44
|
+
const keys = Object.keys(record)
|
|
45
|
+
.filter((k) => record[k] !== undefined)
|
|
46
|
+
.sort();
|
|
47
|
+
const parts = [];
|
|
48
|
+
for (const k of keys) {
|
|
49
|
+
assertWellFormedString(k);
|
|
50
|
+
parts.push(JSON.stringify(k) + ':' + canonicalJson(record[k]));
|
|
51
|
+
}
|
|
52
|
+
return '{' + parts.join(',') + '}';
|
|
53
|
+
}
|
|
54
|
+
throw new Error(`Unsupported value type in canonical JSON: ${t}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function sha256Hex(input ) {
|
|
58
|
+
return crypto.createHash('sha256').update(input).digest('hex');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function canonicalSha256(domain , projection ) {
|
|
62
|
+
return sha256Hex(canonicalJson({ domain, projection }));
|
|
63
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runCli(argv: string[]): Promise<number>;
|