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/runner.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { runDoctor, } from './doctor.js';
|
|
6
|
+
import { runGitText } from './git.js';
|
|
7
|
+
|
|
8
|
+
function isPathWithinOrEqual(candidate , parent ) {
|
|
9
|
+
const rel = path.relative(parent, candidate);
|
|
10
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function safeRealpath(p ) {
|
|
14
|
+
try {
|
|
15
|
+
return fs.realpathSync(p);
|
|
16
|
+
} catch {
|
|
17
|
+
return path.resolve(p);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Resolves external Git metadata directories (`--git-dir` and `--git-common-dir`) that live
|
|
23
|
+
* outside `repoRoot` (e.g. linked Git worktrees) so sandboxed agents like Codex CLI can write
|
|
24
|
+
* `index.lock`, objects, and branch refs cleanly during `git add && git commit`.
|
|
25
|
+
*/
|
|
26
|
+
export function resolveExternalGitAddDirs(repoRoot ) {
|
|
27
|
+
const rootAbs = safeRealpath(repoRoot);
|
|
28
|
+
const extras = [];
|
|
29
|
+
const seen = new Set ([rootAbs]);
|
|
30
|
+
|
|
31
|
+
for (const flag of ['--git-dir', '--git-common-dir'] ) {
|
|
32
|
+
try {
|
|
33
|
+
const raw = runGitText(rootAbs, ['rev-parse', flag]).trim();
|
|
34
|
+
if (!raw) continue;
|
|
35
|
+
const resolved = safeRealpath(path.resolve(rootAbs, raw));
|
|
36
|
+
if (!isPathWithinOrEqual(resolved, rootAbs) && !seen.has(resolved)) {
|
|
37
|
+
seen.add(resolved);
|
|
38
|
+
extras.push(resolved);
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
// Non-git directory or git unavailable; skip extra dirs
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return extras;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Generates a known-good, non-interactive command line for the target agent CLI,
|
|
71
|
+
* accounting for root execution (Claude Code) and modern CLI flags (Codex 0.172+).
|
|
72
|
+
*/
|
|
73
|
+
export function buildHarnessCommand(options ) {
|
|
74
|
+
const isRoot =
|
|
75
|
+
options.isRoot ?? (typeof process.geteuid === 'function' ? process.geteuid() === 0 : false);
|
|
76
|
+
const doctor = options.doctorReport ?? runDoctor({ cwd: options.repoRoot, isRoot });
|
|
77
|
+
const notes = [];
|
|
78
|
+
|
|
79
|
+
if (options.harness === 'claude') {
|
|
80
|
+
const probed = doctor.harnesses.find((h) => h.id === 'claude-code');
|
|
81
|
+
const bin = probed?.resolvedPath ?? 'claude';
|
|
82
|
+
const args = [
|
|
83
|
+
'-p',
|
|
84
|
+
'--permission-mode',
|
|
85
|
+
'acceptEdits',
|
|
86
|
+
'--allowedTools',
|
|
87
|
+
'Bash,Read,Edit,Write,Glob,Grep'
|
|
88
|
+
];
|
|
89
|
+
if (options.model) {
|
|
90
|
+
args.push('--model', options.model);
|
|
91
|
+
}
|
|
92
|
+
// Always terminate variadic --allowedTools with `--` before positional prompt
|
|
93
|
+
args.push('--', options.prompt);
|
|
94
|
+
if (isRoot) {
|
|
95
|
+
notes.push('Root UID detected: omitted --dangerously-skip-permissions in favor of --permission-mode acceptEdits.');
|
|
96
|
+
}
|
|
97
|
+
return { harness: 'claude', bin, args, notes };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (options.harness === 'codex') {
|
|
101
|
+
const probed = doctor.harnesses.find((h) => h.id === 'codex');
|
|
102
|
+
const bin = probed?.resolvedPath ?? 'codex';
|
|
103
|
+
const externalGitDirs = resolveExternalGitAddDirs(options.repoRoot);
|
|
104
|
+
const args = [
|
|
105
|
+
'exec',
|
|
106
|
+
'-s',
|
|
107
|
+
'workspace-write',
|
|
108
|
+
'--add-dir',
|
|
109
|
+
options.repoRoot,
|
|
110
|
+
...externalGitDirs.flatMap((d) => ['--add-dir', d]),
|
|
111
|
+
'--ephemeral'
|
|
112
|
+
];
|
|
113
|
+
if (options.model) {
|
|
114
|
+
args.push('-m', options.model);
|
|
115
|
+
}
|
|
116
|
+
args.push(options.prompt);
|
|
117
|
+
notes.push(
|
|
118
|
+
externalGitDirs.length > 0
|
|
119
|
+
? `Using modern Codex CLI flags with linked-worktree Git dirs granted (${externalGitDirs.join(', ')}) so git add/commit succeed inside workspace-write.`
|
|
120
|
+
: 'Using modern Codex CLI flags (-s workspace-write --add-dir ... --ephemeral); host diffpact verify runs after exit.'
|
|
121
|
+
);
|
|
122
|
+
return { harness: 'codex', bin, args, notes };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (options.harness === 'crush') {
|
|
126
|
+
const probed = doctor.harnesses.find((h) => h.id === 'crush');
|
|
127
|
+
const bin = probed?.resolvedPath ?? 'crush';
|
|
128
|
+
const baseTmp = fs.existsSync('/var/tmp') ? '/var/tmp' : os.tmpdir();
|
|
129
|
+
const safeSlug = path.basename(options.repoRoot).replace(/[^a-zA-Z0-9._-]+/g, '_') || 'worktree';
|
|
130
|
+
const dataDir = options.dataDir
|
|
131
|
+
? path.resolve(options.dataDir)
|
|
132
|
+
: path.join(baseTmp, `diffpact-crush-${safeSlug}-${process.pid}`);
|
|
133
|
+
const args = ['run', '--cwd', options.repoRoot, '--data-dir', dataDir, '--quiet'];
|
|
134
|
+
if (options.model) {
|
|
135
|
+
args.push('--model', options.model);
|
|
136
|
+
}
|
|
137
|
+
if (options.reasoningEffort) {
|
|
138
|
+
args.push('--reasoning-effort', options.reasoningEffort);
|
|
139
|
+
}
|
|
140
|
+
args.push('--', options.prompt);
|
|
141
|
+
notes.push(
|
|
142
|
+
`Isolated Crush state to "${dataDir}" via --data-dir so .crush/ never pollutes the worktree.`
|
|
143
|
+
);
|
|
144
|
+
return { harness: 'crush', bin, args, notes, dataDir };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (options.harness === 'agy') {
|
|
148
|
+
const worker = doctor.harnesses.find((h) => h.id === 'agy-worker' && h.installed);
|
|
149
|
+
const agyCli = doctor.harnesses.find((h) => h.id === 'agy' && h.installed);
|
|
150
|
+
if (worker) {
|
|
151
|
+
const args = ['--cwd', options.repoRoot];
|
|
152
|
+
if (options.model) args.push('--model', options.model);
|
|
153
|
+
args.push(options.prompt);
|
|
154
|
+
return { harness: 'agy', bin: worker.resolvedPath ?? 'agy-worker', args, notes };
|
|
155
|
+
}
|
|
156
|
+
const args = ['--cwd', options.repoRoot];
|
|
157
|
+
if (options.model) args.push('--model', options.model);
|
|
158
|
+
args.push(options.prompt);
|
|
159
|
+
return { harness: 'agy', bin: agyCli?.resolvedPath ?? 'agy', args, notes };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const gem = doctor.harnesses.find((h) => h.id === 'gemini-cli');
|
|
163
|
+
const bin = gem?.resolvedPath ?? 'gemini';
|
|
164
|
+
const args = [];
|
|
165
|
+
if (options.model) args.push('-m', options.model);
|
|
166
|
+
args.push('-p', options.prompt);
|
|
167
|
+
return { harness: 'gemini', bin, args, notes };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
export function spawnCommandWithHeartbeat(
|
|
181
|
+
options
|
|
182
|
+
) {
|
|
183
|
+
return new Promise((resolve) => {
|
|
184
|
+
const startMs = Date.now();
|
|
185
|
+
let settled = false;
|
|
186
|
+
const child = spawn(options.bin, options.args, {
|
|
187
|
+
cwd: options.cwd,
|
|
188
|
+
env: options.env,
|
|
189
|
+
stdio: 'inherit'
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const hbTimer =
|
|
193
|
+
options.heartbeatSec > 0
|
|
194
|
+
? setInterval(() => {
|
|
195
|
+
const elapsedSec = Math.max(1, Math.round((Date.now() - startMs) / 1000));
|
|
196
|
+
const msg = `[diffpact] running ${options.label} (pid=${child.pid ?? 'unknown'}, elapsed ${elapsedSec}s)...`;
|
|
197
|
+
if (options.onHeartbeat) options.onHeartbeat(msg);
|
|
198
|
+
else process.stderr.write(msg + '\n');
|
|
199
|
+
}, options.heartbeatSec * 1000)
|
|
200
|
+
: null;
|
|
201
|
+
hbTimer?.unref?.();
|
|
202
|
+
|
|
203
|
+
const done = (res ) => {
|
|
204
|
+
if (settled) return;
|
|
205
|
+
settled = true;
|
|
206
|
+
if (hbTimer) clearInterval(hbTimer);
|
|
207
|
+
resolve({ ...res, durationMs: Date.now() - startMs });
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
child.on('error', (error) => done({ status: null, signal: null, error }));
|
|
211
|
+
child.on('close', (status, signal) => done({ status, signal }));
|
|
212
|
+
});
|
|
213
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SessionIdentity, TaskContractV01, VerificationReceiptV01, ReviewAttestationV01 } from './types.ts';
|
|
2
|
+
export declare function assertSafeRelativePath(v: unknown, field: string): string;
|
|
3
|
+
export declare function validateSessionIdentity(v: unknown, ctx: string): SessionIdentity;
|
|
4
|
+
export declare function validateContract(raw: unknown): TaskContractV01;
|
|
5
|
+
export declare function validateReceiptBasic(raw: unknown): VerificationReceiptV01;
|
|
6
|
+
export declare function validateReviewBasic(raw: unknown): ReviewAttestationV01;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const HEX40 = /^[0-9a-f]{40}$/;
|
|
14
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
15
|
+
const VALID_HARNESSES = new Set ([
|
|
16
|
+
'claude-code',
|
|
17
|
+
'codex',
|
|
18
|
+
'crush',
|
|
19
|
+
'gemini-cli',
|
|
20
|
+
'agy',
|
|
21
|
+
'vercel-board',
|
|
22
|
+
'manual',
|
|
23
|
+
'other'
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
function isPlainObject(v ) {
|
|
27
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function assertAllowedKeys(obj , allowed , ctx ) {
|
|
31
|
+
const allowedSet = new Set(allowed);
|
|
32
|
+
for (const key of Object.keys(obj)) {
|
|
33
|
+
if (!allowedSet.has(key)) {
|
|
34
|
+
throw new Error(`${ctx}: unexpected property "${key}"`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function assertNonEmptyString(v , field ) {
|
|
40
|
+
if (typeof v !== 'string' || v.trim().length === 0) {
|
|
41
|
+
throw new Error(`${field} must be a non-empty string`);
|
|
42
|
+
}
|
|
43
|
+
if (v.includes('\0')) {
|
|
44
|
+
throw new Error(`${field} must not contain NUL bytes`);
|
|
45
|
+
}
|
|
46
|
+
return v;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function assertSafeRelativePath(v , field ) {
|
|
50
|
+
const s = assertNonEmptyString(v, field).normalize('NFC');
|
|
51
|
+
const norm = s.replace(/\\/g, '/');
|
|
52
|
+
const segments = norm.split('/');
|
|
53
|
+
if (norm.startsWith('/') || segments.includes('..') || segments.includes('') && norm.endsWith('/')) {
|
|
54
|
+
throw new Error(`${field} must be a clean repo-relative path without leading "/" or ".." segments: "${s}"`);
|
|
55
|
+
}
|
|
56
|
+
if (segments.includes('.git')) {
|
|
57
|
+
throw new Error(`${field} must not target .git metadata: "${s}"`);
|
|
58
|
+
}
|
|
59
|
+
return norm;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function assertSafeGlobList(v , field ) {
|
|
63
|
+
if (!Array.isArray(v)) {
|
|
64
|
+
throw new Error(`${field} must be an array of path globs`);
|
|
65
|
+
}
|
|
66
|
+
return v.map((item, idx) => {
|
|
67
|
+
const s = assertNonEmptyString(item, `${field}[${idx}]`).normalize('NFC');
|
|
68
|
+
const norm = s.replace(/\\/g, '/');
|
|
69
|
+
if (norm.startsWith('/') || norm.split('/').includes('..')) {
|
|
70
|
+
throw new Error(`${field}[${idx}] must be a repo-relative path glob without ".." segments: "${s}"`);
|
|
71
|
+
}
|
|
72
|
+
return norm;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function assertFiniteNonNegativeNumber(v , field ) {
|
|
77
|
+
if (typeof v !== 'number' || !Number.isFinite(v) || v < 0) {
|
|
78
|
+
throw new Error(`${field} must be a finite non-negative number`);
|
|
79
|
+
}
|
|
80
|
+
return v;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function validateSessionIdentity(v , ctx ) {
|
|
84
|
+
if (!isPlainObject(v)) throw new Error(`${ctx} must be an object`);
|
|
85
|
+
assertAllowedKeys(v, ['agent_id', 'session_id', 'harness', 'model'], ctx);
|
|
86
|
+
const agent_id = assertNonEmptyString(v.agent_id, `${ctx}.agent_id`);
|
|
87
|
+
const session_id = assertNonEmptyString(v.session_id, `${ctx}.session_id`);
|
|
88
|
+
const harness = assertNonEmptyString(v.harness, `${ctx}.harness`) ;
|
|
89
|
+
if (!VALID_HARNESSES.has(harness)) {
|
|
90
|
+
throw new Error(`${ctx}.harness invalid: "${harness}"`);
|
|
91
|
+
}
|
|
92
|
+
const out = { agent_id, session_id, harness };
|
|
93
|
+
if (v.model !== undefined) {
|
|
94
|
+
out.model = assertNonEmptyString(v.model, `${ctx}.model`);
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function validateContract(raw ) {
|
|
100
|
+
if (!isPlainObject(raw)) {
|
|
101
|
+
throw new Error('Contract must be a JSON object');
|
|
102
|
+
}
|
|
103
|
+
assertAllowedKeys(
|
|
104
|
+
raw,
|
|
105
|
+
[
|
|
106
|
+
'schema_version',
|
|
107
|
+
'task_id',
|
|
108
|
+
'task_type',
|
|
109
|
+
'mission',
|
|
110
|
+
'starting_sha',
|
|
111
|
+
'candidate_sha',
|
|
112
|
+
'worktree',
|
|
113
|
+
'allowed_paths',
|
|
114
|
+
'read_only_paths',
|
|
115
|
+
'forbidden_paths',
|
|
116
|
+
'version_assertions',
|
|
117
|
+
'interfacedelta_lockfile',
|
|
118
|
+
'budget',
|
|
119
|
+
'required_checks',
|
|
120
|
+
'advisory_checks',
|
|
121
|
+
'allow_no_checks',
|
|
122
|
+
'allow_no_commit',
|
|
123
|
+
'implementer_session',
|
|
124
|
+
'review_policy',
|
|
125
|
+
'role_substitution'
|
|
126
|
+
],
|
|
127
|
+
'contract'
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
if (raw.schema_version !== 'diffpact-contract-v0.1') {
|
|
131
|
+
throw new Error(`Unsupported contract schema_version: "${String(raw.schema_version)}"`);
|
|
132
|
+
}
|
|
133
|
+
const task_id = assertNonEmptyString(raw.task_id, 'contract.task_id');
|
|
134
|
+
if (raw.task_type !== 'coding' && raw.task_type !== 'research') {
|
|
135
|
+
throw new Error('contract.task_type must be "coding" or "research"');
|
|
136
|
+
}
|
|
137
|
+
const mission = assertNonEmptyString(raw.mission, 'contract.mission');
|
|
138
|
+
const starting_sha = assertNonEmptyString(raw.starting_sha, 'contract.starting_sha');
|
|
139
|
+
if (!HEX40.test(starting_sha)) {
|
|
140
|
+
throw new Error('contract.starting_sha must be a 40-character lowercase hex SHA-1');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let candidate_sha = undefined;
|
|
144
|
+
if (raw.candidate_sha !== undefined && raw.candidate_sha !== null) {
|
|
145
|
+
candidate_sha = assertNonEmptyString(raw.candidate_sha, 'contract.candidate_sha');
|
|
146
|
+
if (!HEX40.test(candidate_sha)) {
|
|
147
|
+
throw new Error('contract.candidate_sha must be a 40-character lowercase hex SHA-1');
|
|
148
|
+
}
|
|
149
|
+
} else if (raw.candidate_sha === null) {
|
|
150
|
+
candidate_sha = null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!isPlainObject(raw.worktree)) {
|
|
154
|
+
throw new Error('contract.worktree must be an object');
|
|
155
|
+
}
|
|
156
|
+
assertAllowedKeys(
|
|
157
|
+
raw.worktree,
|
|
158
|
+
['branch', 'path', 'require_clean_start', 'require_clean_end'],
|
|
159
|
+
'contract.worktree'
|
|
160
|
+
);
|
|
161
|
+
const worktree = {
|
|
162
|
+
...(raw.worktree.branch !== undefined
|
|
163
|
+
? { branch: assertNonEmptyString(raw.worktree.branch, 'contract.worktree.branch') }
|
|
164
|
+
: {}),
|
|
165
|
+
...(raw.worktree.path !== undefined
|
|
166
|
+
? { path: assertNonEmptyString(raw.worktree.path, 'contract.worktree.path') }
|
|
167
|
+
: {}),
|
|
168
|
+
...(raw.worktree.require_clean_start !== undefined
|
|
169
|
+
? { require_clean_start: Boolean(raw.worktree.require_clean_start) }
|
|
170
|
+
: {}),
|
|
171
|
+
...(raw.worktree.require_clean_end !== undefined
|
|
172
|
+
? { require_clean_end: Boolean(raw.worktree.require_clean_end) }
|
|
173
|
+
: {})
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const allowed_paths = assertSafeGlobList(raw.allowed_paths, 'contract.allowed_paths');
|
|
177
|
+
if (raw.task_type === 'research' && allowed_paths.length > 0) {
|
|
178
|
+
throw new Error('Research task contracts must have an empty allowed_paths array');
|
|
179
|
+
}
|
|
180
|
+
const read_only_paths =
|
|
181
|
+
raw.read_only_paths !== undefined
|
|
182
|
+
? assertSafeGlobList(raw.read_only_paths, 'contract.read_only_paths')
|
|
183
|
+
: undefined;
|
|
184
|
+
const forbidden_paths = assertSafeGlobList(raw.forbidden_paths, 'contract.forbidden_paths');
|
|
185
|
+
|
|
186
|
+
let version_assertions = undefined;
|
|
187
|
+
if (raw.version_assertions !== undefined) {
|
|
188
|
+
if (!Array.isArray(raw.version_assertions)) {
|
|
189
|
+
throw new Error('contract.version_assertions must be an array');
|
|
190
|
+
}
|
|
191
|
+
version_assertions = raw.version_assertions.map((va, idx) => {
|
|
192
|
+
if (!isPlainObject(va)) throw new Error(`contract.version_assertions[${idx}] must be an object`);
|
|
193
|
+
assertAllowedKeys(va, ['path', 'kind', 'key', 'expected'], `contract.version_assertions[${idx}]`);
|
|
194
|
+
const p = assertSafeRelativePath(va.path, `contract.version_assertions[${idx}].path`);
|
|
195
|
+
if (va.kind !== 'sha256' && va.kind !== 'json_pointer' && va.kind !== 'regex_exact') {
|
|
196
|
+
throw new Error(`contract.version_assertions[${idx}].kind invalid: "${String(va.kind)}"`);
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
path: p,
|
|
200
|
+
kind: va.kind,
|
|
201
|
+
key: typeof va.key === 'string' ? va.key : assertNonEmptyString(va.key, `contract.version_assertions[${idx}].key`),
|
|
202
|
+
expected: assertNonEmptyString(va.expected, `contract.version_assertions[${idx}].expected`)
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const parseCheckArray = (list , fieldPath ) => {
|
|
208
|
+
if (!Array.isArray(list)) {
|
|
209
|
+
throw new Error(`${fieldPath} must be an array`);
|
|
210
|
+
}
|
|
211
|
+
return list.map((chk, i) => {
|
|
212
|
+
if (!isPlainObject(chk)) throw new Error(`${fieldPath}[${i}] must be an object`);
|
|
213
|
+
assertAllowedKeys(
|
|
214
|
+
chk,
|
|
215
|
+
['id', 'command', 'cwd', 'timeout_sec', 'forbid_skipped'],
|
|
216
|
+
`${fieldPath}[${i}]`
|
|
217
|
+
);
|
|
218
|
+
return {
|
|
219
|
+
id: assertNonEmptyString(chk.id, `${fieldPath}[${i}].id`),
|
|
220
|
+
command: assertNonEmptyString(chk.command, `${fieldPath}[${i}].command`),
|
|
221
|
+
...(chk.cwd !== undefined
|
|
222
|
+
? { cwd: chk.cwd === '.' ? '.' : assertSafeRelativePath(chk.cwd, `${fieldPath}[${i}].cwd`) }
|
|
223
|
+
: {}),
|
|
224
|
+
...(chk.timeout_sec !== undefined
|
|
225
|
+
? { timeout_sec: assertFiniteNonNegativeNumber(chk.timeout_sec, `${fieldPath}[${i}].timeout_sec`) }
|
|
226
|
+
: {}),
|
|
227
|
+
...(chk.forbid_skipped !== undefined ? { forbid_skipped: Boolean(chk.forbid_skipped) } : {})
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const required_checks = parseCheckArray(raw.required_checks, 'contract.required_checks');
|
|
233
|
+
const advisory_checks =
|
|
234
|
+
raw.advisory_checks !== undefined
|
|
235
|
+
? parseCheckArray(raw.advisory_checks, 'contract.advisory_checks')
|
|
236
|
+
: undefined;
|
|
237
|
+
|
|
238
|
+
let interfacedelta_lockfile = undefined;
|
|
239
|
+
if (raw.interfacedelta_lockfile !== undefined && raw.interfacedelta_lockfile !== null) {
|
|
240
|
+
if (!isPlainObject(raw.interfacedelta_lockfile)) {
|
|
241
|
+
throw new Error('contract.interfacedelta_lockfile must be an object or null');
|
|
242
|
+
}
|
|
243
|
+
assertAllowedKeys(
|
|
244
|
+
raw.interfacedelta_lockfile,
|
|
245
|
+
['path', 'sha256', 'must_remain_unmodified'],
|
|
246
|
+
'contract.interfacedelta_lockfile'
|
|
247
|
+
);
|
|
248
|
+
const lpath = assertSafeRelativePath(raw.interfacedelta_lockfile.path, 'contract.interfacedelta_lockfile.path');
|
|
249
|
+
const lsha = assertNonEmptyString(raw.interfacedelta_lockfile.sha256, 'contract.interfacedelta_lockfile.sha256');
|
|
250
|
+
if (!HEX64.test(lsha)) {
|
|
251
|
+
throw new Error('contract.interfacedelta_lockfile.sha256 must be 64 lowercase hex chars');
|
|
252
|
+
}
|
|
253
|
+
interfacedelta_lockfile = {
|
|
254
|
+
path: lpath,
|
|
255
|
+
sha256: lsha,
|
|
256
|
+
...(raw.interfacedelta_lockfile.must_remain_unmodified !== undefined
|
|
257
|
+
? { must_remain_unmodified: Boolean(raw.interfacedelta_lockfile.must_remain_unmodified) }
|
|
258
|
+
: {})
|
|
259
|
+
};
|
|
260
|
+
} else if (raw.interfacedelta_lockfile === null) {
|
|
261
|
+
interfacedelta_lockfile = null;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
let budget = undefined;
|
|
265
|
+
if (raw.budget !== undefined) {
|
|
266
|
+
if (!isPlainObject(raw.budget)) throw new Error('contract.budget must be an object');
|
|
267
|
+
assertAllowedKeys(
|
|
268
|
+
raw.budget,
|
|
269
|
+
[
|
|
270
|
+
'max_budget_usd',
|
|
271
|
+
'reserved_budget_usd',
|
|
272
|
+
'delegation_depth',
|
|
273
|
+
'max_delegation_depth',
|
|
274
|
+
'parent_contract_sha256'
|
|
275
|
+
],
|
|
276
|
+
'contract.budget'
|
|
277
|
+
);
|
|
278
|
+
if (
|
|
279
|
+
raw.budget.parent_contract_sha256 !== undefined &&
|
|
280
|
+
(typeof raw.budget.parent_contract_sha256 !== 'string' || !HEX64.test(raw.budget.parent_contract_sha256))
|
|
281
|
+
) {
|
|
282
|
+
throw new Error('contract.budget.parent_contract_sha256 must be 64 lowercase hex chars');
|
|
283
|
+
}
|
|
284
|
+
budget = {
|
|
285
|
+
...(raw.budget.max_budget_usd !== undefined
|
|
286
|
+
? { max_budget_usd: assertFiniteNonNegativeNumber(raw.budget.max_budget_usd, 'contract.budget.max_budget_usd') }
|
|
287
|
+
: {}),
|
|
288
|
+
...(raw.budget.reserved_budget_usd !== undefined
|
|
289
|
+
? { reserved_budget_usd: assertFiniteNonNegativeNumber(raw.budget.reserved_budget_usd, 'contract.budget.reserved_budget_usd') }
|
|
290
|
+
: {}),
|
|
291
|
+
...(raw.budget.delegation_depth !== undefined
|
|
292
|
+
? { delegation_depth: assertFiniteNonNegativeNumber(raw.budget.delegation_depth, 'contract.budget.delegation_depth') }
|
|
293
|
+
: {}),
|
|
294
|
+
...(raw.budget.max_delegation_depth !== undefined
|
|
295
|
+
? { max_delegation_depth: assertFiniteNonNegativeNumber(raw.budget.max_delegation_depth, 'contract.budget.max_delegation_depth') }
|
|
296
|
+
: {}),
|
|
297
|
+
...(raw.budget.parent_contract_sha256 !== undefined
|
|
298
|
+
? { parent_contract_sha256: raw.budget.parent_contract_sha256 }
|
|
299
|
+
: {})
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
let review_policy = undefined;
|
|
304
|
+
if (raw.review_policy !== undefined) {
|
|
305
|
+
if (!isPlainObject(raw.review_policy)) throw new Error('contract.review_policy must be an object');
|
|
306
|
+
assertAllowedKeys(
|
|
307
|
+
raw.review_policy,
|
|
308
|
+
[
|
|
309
|
+
'required_reviews',
|
|
310
|
+
'require_non_self_certified',
|
|
311
|
+
'require_distinct_harness',
|
|
312
|
+
'require_raw_diff_inspection',
|
|
313
|
+
'canonical_integrator_agent_id'
|
|
314
|
+
],
|
|
315
|
+
'contract.review_policy'
|
|
316
|
+
);
|
|
317
|
+
review_policy = {
|
|
318
|
+
...(raw.review_policy.required_reviews !== undefined
|
|
319
|
+
? { required_reviews: assertFiniteNonNegativeNumber(raw.review_policy.required_reviews, 'contract.review_policy.required_reviews') }
|
|
320
|
+
: {}),
|
|
321
|
+
...(raw.review_policy.require_non_self_certified !== undefined
|
|
322
|
+
? { require_non_self_certified: Boolean(raw.review_policy.require_non_self_certified) }
|
|
323
|
+
: {}),
|
|
324
|
+
...(raw.review_policy.require_distinct_harness !== undefined
|
|
325
|
+
? { require_distinct_harness: Boolean(raw.review_policy.require_distinct_harness) }
|
|
326
|
+
: {}),
|
|
327
|
+
...(raw.review_policy.require_raw_diff_inspection !== undefined
|
|
328
|
+
? { require_raw_diff_inspection: Boolean(raw.review_policy.require_raw_diff_inspection) }
|
|
329
|
+
: {}),
|
|
330
|
+
...(raw.review_policy.canonical_integrator_agent_id !== undefined
|
|
331
|
+
? {
|
|
332
|
+
canonical_integrator_agent_id: assertNonEmptyString(
|
|
333
|
+
raw.review_policy.canonical_integrator_agent_id,
|
|
334
|
+
'contract.review_policy.canonical_integrator_agent_id'
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
: {})
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
let role_substitution = undefined;
|
|
342
|
+
if (raw.role_substitution !== undefined && raw.role_substitution !== null) {
|
|
343
|
+
if (!isPlainObject(raw.role_substitution)) throw new Error('contract.role_substitution must be an object or null');
|
|
344
|
+
assertAllowedKeys(
|
|
345
|
+
raw.role_substitution,
|
|
346
|
+
['substitute_agent_id', 'authorized_by', 'task_id', 'active', 'spent', 'reason'],
|
|
347
|
+
'contract.role_substitution'
|
|
348
|
+
);
|
|
349
|
+
role_substitution = {
|
|
350
|
+
substitute_agent_id: assertNonEmptyString(
|
|
351
|
+
raw.role_substitution.substitute_agent_id,
|
|
352
|
+
'contract.role_substitution.substitute_agent_id'
|
|
353
|
+
),
|
|
354
|
+
authorized_by: assertNonEmptyString(raw.role_substitution.authorized_by, 'contract.role_substitution.authorized_by'),
|
|
355
|
+
task_id: assertNonEmptyString(raw.role_substitution.task_id, 'contract.role_substitution.task_id'),
|
|
356
|
+
active: Boolean(raw.role_substitution.active),
|
|
357
|
+
spent: Boolean(raw.role_substitution.spent),
|
|
358
|
+
...(raw.role_substitution.reason !== undefined
|
|
359
|
+
? { reason: assertNonEmptyString(raw.role_substitution.reason, 'contract.role_substitution.reason') }
|
|
360
|
+
: {})
|
|
361
|
+
};
|
|
362
|
+
} else if (raw.role_substitution === null) {
|
|
363
|
+
role_substitution = null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const implementer_session = validateSessionIdentity(
|
|
367
|
+
raw.implementer_session,
|
|
368
|
+
'contract.implementer_session'
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
schema_version: 'diffpact-contract-v0.1',
|
|
373
|
+
task_id,
|
|
374
|
+
task_type: raw.task_type,
|
|
375
|
+
mission,
|
|
376
|
+
starting_sha,
|
|
377
|
+
...(candidate_sha !== undefined ? { candidate_sha } : {}),
|
|
378
|
+
worktree,
|
|
379
|
+
allowed_paths,
|
|
380
|
+
...(read_only_paths !== undefined ? { read_only_paths } : {}),
|
|
381
|
+
forbidden_paths,
|
|
382
|
+
...(version_assertions !== undefined ? { version_assertions } : {}),
|
|
383
|
+
...(interfacedelta_lockfile !== undefined ? { interfacedelta_lockfile } : {}),
|
|
384
|
+
...(budget !== undefined ? { budget } : {}),
|
|
385
|
+
required_checks,
|
|
386
|
+
...(advisory_checks !== undefined ? { advisory_checks } : {}),
|
|
387
|
+
...(raw.allow_no_checks !== undefined ? { allow_no_checks: Boolean(raw.allow_no_checks) } : {}),
|
|
388
|
+
...(raw.allow_no_commit !== undefined ? { allow_no_commit: Boolean(raw.allow_no_commit) } : {}),
|
|
389
|
+
implementer_session,
|
|
390
|
+
...(review_policy !== undefined ? { review_policy } : {}),
|
|
391
|
+
...(role_substitution !== undefined ? { role_substitution } : {})
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function validateReceiptBasic(raw ) {
|
|
396
|
+
if (!isPlainObject(raw) || raw.schema_version !== 'diffpact-receipt-v0.1') {
|
|
397
|
+
throw new Error('Invalid diffpact receipt');
|
|
398
|
+
}
|
|
399
|
+
if (typeof raw.receipt_sha256 !== 'string' || !HEX64.test(raw.receipt_sha256)) {
|
|
400
|
+
throw new Error('Invalid receipt_sha256');
|
|
401
|
+
}
|
|
402
|
+
if (typeof raw.contract_sha256 !== 'string' || !HEX64.test(raw.contract_sha256)) {
|
|
403
|
+
throw new Error('Invalid contract_sha256 in receipt');
|
|
404
|
+
}
|
|
405
|
+
if (typeof raw.candidate_sha !== 'string' || !HEX40.test(raw.candidate_sha)) {
|
|
406
|
+
throw new Error('Invalid candidate_sha in receipt');
|
|
407
|
+
}
|
|
408
|
+
if (typeof raw.raw_diff_sha256 !== 'string' || !HEX64.test(raw.raw_diff_sha256)) {
|
|
409
|
+
throw new Error('Invalid raw_diff_sha256 in receipt');
|
|
410
|
+
}
|
|
411
|
+
return raw ;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export function validateReviewBasic(raw ) {
|
|
415
|
+
if (!isPlainObject(raw) || raw.schema_version !== 'diffpact-review-v0.1') {
|
|
416
|
+
throw new Error('Invalid diffpact review attestation');
|
|
417
|
+
}
|
|
418
|
+
if (typeof raw.review_sha256 !== 'string' || !HEX64.test(raw.review_sha256)) {
|
|
419
|
+
throw new Error('Invalid review_sha256');
|
|
420
|
+
}
|
|
421
|
+
validateSessionIdentity(raw.implementer_session, 'review.implementer_session');
|
|
422
|
+
validateSessionIdentity(raw.reviewer_session, 'review.reviewer_session');
|
|
423
|
+
return raw ;
|
|
424
|
+
}
|