@planu/cli 8.2.19 → 8.3.0
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 +10 -0
- package/README.md +58 -2
- package/dist/sdd-engine/completion-snapshot.d.ts +5 -1
- package/dist/sdd-engine/completion-snapshot.js +79 -2
- package/dist/sdd-engine/contracts.d.ts +1 -0
- package/dist/sdd-engine/contracts.js +14 -0
- package/dist/sdd-engine/feature-authority.d.ts +3 -1
- package/dist/sdd-engine/feature-authority.js +33 -1
- package/dist/sdd-engine/feature-manifest.d.ts +3 -1
- package/dist/sdd-engine/feature-manifest.js +207 -19
- package/dist/sdd-engine/host-rule-pack.js +1 -0
- package/dist/sdd-engine/lock-integrity.d.ts +2 -2
- package/dist/sdd-engine/lock-integrity.js +5 -1
- package/dist/sdd-engine/task-content.d.ts +8 -1
- package/dist/sdd-engine/task-content.js +100 -3
- package/dist/sdd-engine/task-failures.d.ts +2 -0
- package/dist/sdd-engine/task-failures.js +34 -0
- package/dist/sdd-engine/task-window.d.ts +2 -4
- package/dist/sdd-engine/task-window.js +41 -78
- package/dist/sdd-engine/tools/execution-tools.js +82 -29
- package/dist/sdd-engine/verification-inputs.d.ts +12 -0
- package/dist/sdd-engine/verification-inputs.js +302 -0
- package/dist/sdd-engine/verification-receipts.d.ts +24 -0
- package/dist/sdd-engine/verification-receipts.js +265 -0
- package/dist/types/sdd-engine.d.ts +132 -0
- package/package.json +1 -1
- package/templates/AGENTS.md +1 -1
- package/templates/host/planu-sdd.md +1 -0
- package/templates/skills/spec-architect.md +1 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import * as fs from 'node:fs/promises';
|
|
4
|
+
import * as nodePath from 'node:path';
|
|
5
|
+
import { disjointPaths, isEvidencePath } from './feature-authority.js';
|
|
6
|
+
const MAX_FILE_BYTES = 2 * 1024 * 1024;
|
|
7
|
+
const MAX_TOTAL_BYTES = 16 * 1024 * 1024;
|
|
8
|
+
export class VerificationInputError extends Error {
|
|
9
|
+
reason;
|
|
10
|
+
category;
|
|
11
|
+
path;
|
|
12
|
+
constructor(reason, category, path) {
|
|
13
|
+
super(reason === 'cancelled'
|
|
14
|
+
? 'Verification was cancelled.'
|
|
15
|
+
: reason === 'receipt_input_drift'
|
|
16
|
+
? 'A verification input changed during the attempt.'
|
|
17
|
+
: 'A declared verification input is unavailable or unsafe.');
|
|
18
|
+
this.reason = reason;
|
|
19
|
+
this.category = category;
|
|
20
|
+
this.path = path;
|
|
21
|
+
this.name = 'VerificationInputError';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function checkCancellation(signal) {
|
|
25
|
+
if (signal?.aborted) {
|
|
26
|
+
throw new VerificationInputError('cancelled');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function unavailable(request) {
|
|
30
|
+
throw new VerificationInputError('receipt_input_unavailable', request?.category, request?.path);
|
|
31
|
+
}
|
|
32
|
+
function sameDirectory(left, right) {
|
|
33
|
+
return (right.isDirectory() &&
|
|
34
|
+
left.dev === right.dev &&
|
|
35
|
+
left.ino === right.ino &&
|
|
36
|
+
left.mode === right.mode &&
|
|
37
|
+
left.uid === right.uid &&
|
|
38
|
+
left.gid === right.gid);
|
|
39
|
+
}
|
|
40
|
+
function ownedRegular(stat) {
|
|
41
|
+
return (stat.isFile() &&
|
|
42
|
+
stat.nlink === 1n &&
|
|
43
|
+
typeof process.getuid === 'function' &&
|
|
44
|
+
stat.uid === BigInt(process.getuid()));
|
|
45
|
+
}
|
|
46
|
+
function sameFile(left, right) {
|
|
47
|
+
return (ownedRegular(right) &&
|
|
48
|
+
left.dev === right.dev &&
|
|
49
|
+
left.ino === right.ino &&
|
|
50
|
+
left.mode === right.mode &&
|
|
51
|
+
left.uid === right.uid &&
|
|
52
|
+
left.gid === right.gid &&
|
|
53
|
+
left.size === right.size &&
|
|
54
|
+
left.mtimeNs === right.mtimeNs &&
|
|
55
|
+
left.ctimeNs === right.ctimeNs);
|
|
56
|
+
}
|
|
57
|
+
export async function captureVerificationRoot(workspaceRoot) {
|
|
58
|
+
try {
|
|
59
|
+
const canonical = await fs.realpath(workspaceRoot);
|
|
60
|
+
const stat = await fs.lstat(canonical, { bigint: true });
|
|
61
|
+
if (!stat.isDirectory()) {
|
|
62
|
+
unavailable();
|
|
63
|
+
}
|
|
64
|
+
return { canonical, stat };
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return unavailable();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function sameRoot(left, right) {
|
|
71
|
+
return left.canonical === right.canonical && sameDirectory(left.stat, right.stat);
|
|
72
|
+
}
|
|
73
|
+
// Check spelling through directory entries even on filesystems where lstat accepts aliases.
|
|
74
|
+
// Stream names so an unrelated large directory never causes an unbounded allocation.
|
|
75
|
+
async function hasExactName(parent, component, request, signal) {
|
|
76
|
+
const folded = component.toLowerCase();
|
|
77
|
+
let exact = false;
|
|
78
|
+
for await (const entry of await fs.opendir(parent)) {
|
|
79
|
+
checkCancellation(signal);
|
|
80
|
+
if (entry.name === component) {
|
|
81
|
+
exact = true;
|
|
82
|
+
}
|
|
83
|
+
else if (entry.name.toLowerCase() === folded) {
|
|
84
|
+
unavailable(request);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return exact;
|
|
88
|
+
}
|
|
89
|
+
async function inspectPath(root, request, signal) {
|
|
90
|
+
const parts = request.path.split('/');
|
|
91
|
+
const directories = [];
|
|
92
|
+
let parent = root.canonical;
|
|
93
|
+
for (const [index, component] of parts.entries()) {
|
|
94
|
+
const absolute = nodePath.join(parent, component);
|
|
95
|
+
const exact = await hasExactName(parent, component, request, signal);
|
|
96
|
+
let stat;
|
|
97
|
+
try {
|
|
98
|
+
stat = await fs.lstat(absolute, { bigint: true });
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (!exact &&
|
|
102
|
+
request.allowAbsent &&
|
|
103
|
+
error.code === 'ENOENT' &&
|
|
104
|
+
(await fs.realpath(parent)) === parent) {
|
|
105
|
+
return { state: 'absent', directories, absolute };
|
|
106
|
+
}
|
|
107
|
+
return unavailable(request);
|
|
108
|
+
}
|
|
109
|
+
if (!exact || stat.isSymbolicLink()) {
|
|
110
|
+
return unavailable(request);
|
|
111
|
+
}
|
|
112
|
+
if (index === parts.length - 1) {
|
|
113
|
+
if (!ownedRegular(stat) || (await fs.realpath(absolute)) !== absolute) {
|
|
114
|
+
return unavailable(request);
|
|
115
|
+
}
|
|
116
|
+
return { state: 'present', directories, absolute, stat };
|
|
117
|
+
}
|
|
118
|
+
if (!stat.isDirectory()) {
|
|
119
|
+
return unavailable(request);
|
|
120
|
+
}
|
|
121
|
+
directories.push(stat);
|
|
122
|
+
parent = absolute;
|
|
123
|
+
}
|
|
124
|
+
return unavailable(request);
|
|
125
|
+
}
|
|
126
|
+
async function readFileHash(walk, request, limit, signal) {
|
|
127
|
+
if (walk.stat.size > BigInt(limit) ||
|
|
128
|
+
!Number.isInteger(constants.O_NOFOLLOW) ||
|
|
129
|
+
!Number.isInteger(constants.O_NONBLOCK)) {
|
|
130
|
+
return unavailable(request);
|
|
131
|
+
}
|
|
132
|
+
const handle = await fs.open(walk.absolute, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
133
|
+
try {
|
|
134
|
+
const opened = await handle.stat({ bigint: true });
|
|
135
|
+
if (!sameFile(walk.stat, opened) || opened.size > BigInt(limit)) {
|
|
136
|
+
return unavailable(request);
|
|
137
|
+
}
|
|
138
|
+
const bytes = Buffer.alloc(Number(opened.size));
|
|
139
|
+
let offset = 0;
|
|
140
|
+
while (offset < bytes.length) {
|
|
141
|
+
checkCancellation(signal);
|
|
142
|
+
const { bytesRead } = await handle.read(bytes, offset, bytes.length - offset, offset);
|
|
143
|
+
if (bytesRead === 0) {
|
|
144
|
+
return unavailable(request);
|
|
145
|
+
}
|
|
146
|
+
offset += bytesRead;
|
|
147
|
+
}
|
|
148
|
+
const trailing = await handle.read(Buffer.alloc(1), 0, 1, offset);
|
|
149
|
+
if (trailing.bytesRead !== 0 || !sameFile(opened, await handle.stat({ bigint: true }))) {
|
|
150
|
+
return unavailable(request);
|
|
151
|
+
}
|
|
152
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
await handle.close();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function sameWalk(left, right) {
|
|
159
|
+
return (left.absolute === right.absolute &&
|
|
160
|
+
left.directories.length === right.directories.length &&
|
|
161
|
+
left.directories.every((stat, index) => {
|
|
162
|
+
const current = right.directories[index];
|
|
163
|
+
return current !== undefined && sameDirectory(stat, current);
|
|
164
|
+
}) &&
|
|
165
|
+
(left.state === 'absent'
|
|
166
|
+
? right.state === 'absent'
|
|
167
|
+
: right.state === 'present' && sameFile(left.stat, right.stat)));
|
|
168
|
+
}
|
|
169
|
+
async function withSafeDiagnostic(request, operation, signal) {
|
|
170
|
+
checkCancellation(signal);
|
|
171
|
+
try {
|
|
172
|
+
return await operation();
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
checkCancellation(signal);
|
|
176
|
+
return unavailable(request);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function jsonSha256(value) {
|
|
180
|
+
return createHash('sha256').update(JSON.stringify(value), 'utf8').digest('hex');
|
|
181
|
+
}
|
|
182
|
+
function digestCategory(inputs, category) {
|
|
183
|
+
const entries = inputs
|
|
184
|
+
.filter(({ request }) => request.category === category)
|
|
185
|
+
.map((input) => 'rawSha256' in input
|
|
186
|
+
? [input.request.path, 'present', input.mode, input.rawSha256]
|
|
187
|
+
: [input.request.path, 'absent']);
|
|
188
|
+
return jsonSha256(['planu.verification-inputs.v1', category, entries]);
|
|
189
|
+
}
|
|
190
|
+
async function capture(workspaceRoot, requests, signal, expectedRoot) {
|
|
191
|
+
checkCancellation(signal);
|
|
192
|
+
const root = await captureVerificationRoot(workspaceRoot);
|
|
193
|
+
checkCancellation(signal);
|
|
194
|
+
if (expectedRoot && !sameRoot(root, expectedRoot)) {
|
|
195
|
+
throw new VerificationInputError('receipt_input_drift');
|
|
196
|
+
}
|
|
197
|
+
const inputs = [];
|
|
198
|
+
let remaining = MAX_TOTAL_BYTES;
|
|
199
|
+
for (const request of requests) {
|
|
200
|
+
const input = await withSafeDiagnostic(request, async () => {
|
|
201
|
+
const walk = await inspectPath(root, request, signal);
|
|
202
|
+
if (walk.state === 'absent') {
|
|
203
|
+
return { request, walk };
|
|
204
|
+
}
|
|
205
|
+
const rawSha256 = await readFileHash(walk, request, Math.min(remaining, MAX_FILE_BYTES), signal);
|
|
206
|
+
remaining -= Number(walk.stat.size);
|
|
207
|
+
return { request, walk, rawSha256, mode: Number(walk.stat.mode & 4095n) };
|
|
208
|
+
}, signal);
|
|
209
|
+
inputs.push(input);
|
|
210
|
+
}
|
|
211
|
+
// Revisit names after all descriptor reads, including each proof of future absence.
|
|
212
|
+
for (const { request, walk } of inputs) {
|
|
213
|
+
await withSafeDiagnostic(request, async () => {
|
|
214
|
+
if (!sameWalk(walk, await inspectPath(root, request, signal))) {
|
|
215
|
+
unavailable(request);
|
|
216
|
+
}
|
|
217
|
+
}, signal);
|
|
218
|
+
}
|
|
219
|
+
if (!sameRoot(root, await captureVerificationRoot(workspaceRoot))) {
|
|
220
|
+
unavailable();
|
|
221
|
+
}
|
|
222
|
+
checkCancellation(signal);
|
|
223
|
+
return {
|
|
224
|
+
workspaceRoot,
|
|
225
|
+
root,
|
|
226
|
+
inputs,
|
|
227
|
+
workspaceSha256: jsonSha256([
|
|
228
|
+
'planu.workspace.v1',
|
|
229
|
+
root.canonical,
|
|
230
|
+
root.stat.dev.toString(10),
|
|
231
|
+
root.stat.ino.toString(10),
|
|
232
|
+
]),
|
|
233
|
+
candidateSha256: digestCategory(inputs, 'candidate'),
|
|
234
|
+
observedSha256: digestCategory(inputs, 'observed'),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
export async function captureVerificationInputs(workspaceRoot, candidates, task, signal, expectedRoot) {
|
|
238
|
+
checkCancellation(signal);
|
|
239
|
+
if (candidates.length < 1 || candidates.length > 64 || task.observed.length > 64) {
|
|
240
|
+
return unavailable();
|
|
241
|
+
}
|
|
242
|
+
const paths = [...candidates.map((candidate) => candidate.path), ...task.observed];
|
|
243
|
+
if (!paths.every(isEvidencePath) || !disjointPaths(paths)) {
|
|
244
|
+
return unavailable();
|
|
245
|
+
}
|
|
246
|
+
const requests = [
|
|
247
|
+
...candidates.map((candidate) => ({
|
|
248
|
+
category: 'candidate',
|
|
249
|
+
path: candidate.path,
|
|
250
|
+
allowAbsent: task.ordinal < candidate.requiredFrom,
|
|
251
|
+
})),
|
|
252
|
+
...task.observed.map((observed) => ({
|
|
253
|
+
category: 'observed',
|
|
254
|
+
path: observed,
|
|
255
|
+
allowAbsent: false,
|
|
256
|
+
})),
|
|
257
|
+
];
|
|
258
|
+
requests.sort((left, right) => (left.path < right.path ? -1 : Number(left.path > right.path)));
|
|
259
|
+
return capture(workspaceRoot, requests, signal, expectedRoot);
|
|
260
|
+
}
|
|
261
|
+
export async function checkReceiptPatchTarget(root, workspaceRoot, path, permission, searchContent) {
|
|
262
|
+
if (permission !== 'CREATE' && permission !== 'MODIFY') {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const request = {
|
|
266
|
+
category: 'candidate',
|
|
267
|
+
path,
|
|
268
|
+
allowAbsent: permission === 'CREATE' && searchContent === '',
|
|
269
|
+
};
|
|
270
|
+
await withSafeDiagnostic(request, async () => {
|
|
271
|
+
await inspectPath(root, request);
|
|
272
|
+
if (!sameRoot(root, await captureVerificationRoot(workspaceRoot))) {
|
|
273
|
+
unavailable(request);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
export async function revalidateVerificationInputs(snapshot, signal) {
|
|
278
|
+
let current;
|
|
279
|
+
try {
|
|
280
|
+
current = await capture(snapshot.workspaceRoot, snapshot.inputs.map(({ request }) => request), signal, snapshot.root);
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
checkCancellation(signal);
|
|
284
|
+
if (error instanceof VerificationInputError && error.reason === 'receipt_input_unavailable') {
|
|
285
|
+
throw new VerificationInputError('receipt_input_drift', error.category, error.path);
|
|
286
|
+
}
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
if (!sameRoot(snapshot.root, current.root)) {
|
|
290
|
+
throw new VerificationInputError('receipt_input_drift');
|
|
291
|
+
}
|
|
292
|
+
for (const [index, previous] of snapshot.inputs.entries()) {
|
|
293
|
+
const next = current.inputs[index];
|
|
294
|
+
if (!next ||
|
|
295
|
+
!sameWalk(previous.walk, next.walk) ||
|
|
296
|
+
('rawSha256' in previous && (!('rawSha256' in next) || previous.rawSha256 !== next.rawSha256))) {
|
|
297
|
+
throw new VerificationInputError('receipt_input_drift', previous.request.category, previous.request.path);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
checkCancellation(signal);
|
|
301
|
+
}
|
|
302
|
+
//# sourceMappingURL=verification-inputs.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type KeyObject } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { CompleteTaskResult, ReceiptPolicyResult, ReceiptSetResult, ReceiptTaskPolicy, SddResult, VerificationEvidence, WorkspaceRoot } from '../types/sdd-engine.js';
|
|
4
|
+
import type { CompletionSnapshot } from '../types/completion.js';
|
|
5
|
+
import type { CompletionLease } from './completion-guard.js';
|
|
6
|
+
import { captureVerificationInputs } from './verification-inputs.js';
|
|
7
|
+
import type { ScopedVerificationRun, ScopedVerificationRunnerOptions } from '../types/verification.js';
|
|
8
|
+
export declare const maxVerificationReceipts = 32;
|
|
9
|
+
export declare const gateIdPattern: RegExp;
|
|
10
|
+
export declare const taskIdPattern: RegExp;
|
|
11
|
+
export declare const featureIdPattern: RegExp;
|
|
12
|
+
export declare const sha256Schema: z.ZodString;
|
|
13
|
+
export declare function importReceiptPublicKey(value: string): KeyObject | undefined;
|
|
14
|
+
export declare function validateVerificationReceiptSet(input: unknown, challenge: VerificationEvidence, publicKey: KeyObject): ReceiptSetResult;
|
|
15
|
+
export declare function captureReceiptEvidence(snapshot: CompletionSnapshot, workspaceRoot: string, policy: Extract<ReceiptPolicyResult, {
|
|
16
|
+
status: 'ready';
|
|
17
|
+
}>, task: ReceiptTaskPolicy, root: WorkspaceRoot, signal?: AbortSignal): Promise<SddResult<{
|
|
18
|
+
evidence: VerificationEvidence;
|
|
19
|
+
inputs: Awaited<ReturnType<typeof captureVerificationInputs>>;
|
|
20
|
+
}>>;
|
|
21
|
+
export declare function completeReceiptTask(snapshot: CompletionSnapshot, workspaceRoot: string, policy: Extract<ReceiptPolicyResult, {
|
|
22
|
+
status: 'ready';
|
|
23
|
+
}>, task: ReceiptTaskPolicy, receipts: unknown, lease: CompletionLease, signal: AbortSignal, timeoutMs: number, runVerification: (options: ScopedVerificationRunnerOptions) => Promise<ScopedVerificationRun>): Promise<SddResult<CompleteTaskResult>>;
|
|
24
|
+
//# sourceMappingURL=verification-receipts.d.ts.map
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { createHash, createPublicKey, verify } from 'node:crypto';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { captureCompletionSnapshot, persistCompletionSnapshot, revalidateCompletionSnapshot, } from './completion-snapshot.js';
|
|
5
|
+
import { hashArtifact } from './feature-authority.js';
|
|
6
|
+
import { activeTaskFromContent, markReceiptTaskVerified, remainingTaskCount, } from './task-content.js';
|
|
7
|
+
import { completionFailure, receiptFailure, unsupportedVerificationPlatform, verificationFailed, } from './task-failures.js';
|
|
8
|
+
import { captureVerificationInputs, captureVerificationRoot, revalidateVerificationInputs, } from './verification-inputs.js';
|
|
9
|
+
export const maxVerificationReceipts = 32;
|
|
10
|
+
export const gateIdPattern = /^[a-z][a-z0-9-]{0,31}$/u;
|
|
11
|
+
export const taskIdPattern = /^TASK-[1-9][0-9]{0,18}$/u;
|
|
12
|
+
export const featureIdPattern = /^FEAT[-_][A-Z0-9_-]{1,123}$/u;
|
|
13
|
+
export const sha256Schema = z.string().regex(/^[0-9a-f]{64}$/u);
|
|
14
|
+
function decodeCanonicalBase64Url(value) {
|
|
15
|
+
if (!/^[A-Za-z0-9_-]+$/u.test(value)) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const decoded = Buffer.from(value, 'base64url');
|
|
19
|
+
return decoded.toString('base64url') === value ? decoded : undefined;
|
|
20
|
+
}
|
|
21
|
+
export function importReceiptPublicKey(value) {
|
|
22
|
+
const der = value.length <= 128 ? decodeCanonicalBase64Url(value) : undefined;
|
|
23
|
+
if (!der) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const key = createPublicKey({ key: der, format: 'der', type: 'spki' });
|
|
28
|
+
if (key.asymmetricKeyType !== 'ed25519') {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
return key.export({ format: 'der', type: 'spki' }).equals(der) ? key : undefined;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const receiptSchema = z.strictObject({
|
|
38
|
+
version: z.literal(1),
|
|
39
|
+
featureId: z.string().regex(featureIdPattern),
|
|
40
|
+
taskId: z.string().regex(taskIdPattern),
|
|
41
|
+
workspaceSha256: sha256Schema,
|
|
42
|
+
lockSha256: sha256Schema,
|
|
43
|
+
candidateSha256: sha256Schema,
|
|
44
|
+
observedSha256: sha256Schema,
|
|
45
|
+
gateId: z.string().regex(gateIdPattern),
|
|
46
|
+
procedureSha256: sha256Schema,
|
|
47
|
+
environmentSha256: sha256Schema,
|
|
48
|
+
outcome: z.enum(['pass', 'fail', 'skipped', 'partial']),
|
|
49
|
+
evidenceSha256: sha256Schema,
|
|
50
|
+
signature: z.string().regex(/^[A-Za-z0-9_-]{86}$/u),
|
|
51
|
+
});
|
|
52
|
+
const RECEIPT_KEY_COUNT = Object.keys(receiptSchema.shape).length;
|
|
53
|
+
function receiptMessage(receipt) {
|
|
54
|
+
return [
|
|
55
|
+
'planu.verification-receipt.v1',
|
|
56
|
+
receipt.featureId,
|
|
57
|
+
receipt.taskId,
|
|
58
|
+
receipt.workspaceSha256,
|
|
59
|
+
receipt.lockSha256,
|
|
60
|
+
receipt.candidateSha256,
|
|
61
|
+
receipt.observedSha256,
|
|
62
|
+
receipt.gateId,
|
|
63
|
+
receipt.procedureSha256,
|
|
64
|
+
receipt.environmentSha256,
|
|
65
|
+
receipt.outcome,
|
|
66
|
+
receipt.evidenceSha256,
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
function decodeReceipt(input) {
|
|
70
|
+
if (typeof input !== 'object' ||
|
|
71
|
+
input === null ||
|
|
72
|
+
Object.keys(input).length !== RECEIPT_KEY_COUNT) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
const parsed = receiptSchema.safeParse(input);
|
|
76
|
+
if (!parsed.success) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
const signature = decodeCanonicalBase64Url(parsed.data.signature);
|
|
80
|
+
if (signature?.length !== 64) {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
return { receipt: parsed.data, message: receiptMessage(parsed.data), signature };
|
|
84
|
+
}
|
|
85
|
+
function matchCoverage(signed, gates) {
|
|
86
|
+
const required = new Set(gates.map((gate) => gate.id));
|
|
87
|
+
const byGate = new Map();
|
|
88
|
+
for (const entry of signed) {
|
|
89
|
+
const gateId = entry.receipt.gateId;
|
|
90
|
+
if (!required.has(gateId)) {
|
|
91
|
+
return { ok: false, reason: 'receipt_unexpected' };
|
|
92
|
+
}
|
|
93
|
+
if (byGate.has(gateId)) {
|
|
94
|
+
return { ok: false, reason: 'receipt_duplicate', gateId };
|
|
95
|
+
}
|
|
96
|
+
byGate.set(gateId, entry);
|
|
97
|
+
}
|
|
98
|
+
const matched = [];
|
|
99
|
+
for (const gate of gates) {
|
|
100
|
+
const entry = byGate.get(gate.id);
|
|
101
|
+
if (!entry) {
|
|
102
|
+
return { ok: false, reason: 'receipt_missing', gateId: gate.id };
|
|
103
|
+
}
|
|
104
|
+
matched.push({ ...entry, gate });
|
|
105
|
+
}
|
|
106
|
+
return { ok: true, matched };
|
|
107
|
+
}
|
|
108
|
+
function validateClaims(matched, challenge) {
|
|
109
|
+
const checks = [
|
|
110
|
+
[
|
|
111
|
+
(receipt) => receipt.workspaceSha256 === challenge.workspaceSha256 &&
|
|
112
|
+
receipt.featureId === challenge.featureId &&
|
|
113
|
+
receipt.taskId === challenge.taskId &&
|
|
114
|
+
receipt.lockSha256 === challenge.lockSha256,
|
|
115
|
+
'receipt_context_mismatch',
|
|
116
|
+
],
|
|
117
|
+
[
|
|
118
|
+
(receipt) => receipt.candidateSha256 === challenge.candidateSha256,
|
|
119
|
+
'receipt_candidate_mismatch',
|
|
120
|
+
],
|
|
121
|
+
[(receipt) => receipt.observedSha256 === challenge.observedSha256, 'receipt_observed_mismatch'],
|
|
122
|
+
[
|
|
123
|
+
(receipt, gate) => receipt.procedureSha256 === gate.procedureSha256,
|
|
124
|
+
'receipt_procedure_mismatch',
|
|
125
|
+
],
|
|
126
|
+
[
|
|
127
|
+
(receipt, gate) => receipt.environmentSha256 === gate.environmentSha256,
|
|
128
|
+
'receipt_environment_mismatch',
|
|
129
|
+
],
|
|
130
|
+
[(receipt) => receipt.outcome === 'pass', 'receipt_non_pass'],
|
|
131
|
+
];
|
|
132
|
+
for (const [accepts, reason] of checks) {
|
|
133
|
+
const failed = matched.find(({ receipt, gate }) => !accepts(receipt, gate));
|
|
134
|
+
if (failed) {
|
|
135
|
+
return { ok: false, reason, gateId: failed.gate.id };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
export function validateVerificationReceiptSet(input, challenge, publicKey) {
|
|
141
|
+
const gates = [...challenge.gates].sort((left, right) => left.id < right.id ? -1 : left.id === right.id ? 0 : 1);
|
|
142
|
+
if (input === undefined) {
|
|
143
|
+
const gateId = gates[0]?.id;
|
|
144
|
+
return { ok: false, reason: 'receipt_missing', ...(gateId ? { gateId } : {}) };
|
|
145
|
+
}
|
|
146
|
+
if (!Array.isArray(input) || input.length < 1 || input.length > maxVerificationReceipts) {
|
|
147
|
+
return { ok: false, reason: 'receipt_malformed' };
|
|
148
|
+
}
|
|
149
|
+
const signed = [];
|
|
150
|
+
for (const raw of input) {
|
|
151
|
+
const receipt = decodeReceipt(raw);
|
|
152
|
+
if (!receipt) {
|
|
153
|
+
return { ok: false, reason: 'receipt_malformed' };
|
|
154
|
+
}
|
|
155
|
+
signed.push(receipt);
|
|
156
|
+
}
|
|
157
|
+
const coverage = matchCoverage(signed, gates);
|
|
158
|
+
if (!coverage.ok) {
|
|
159
|
+
return coverage;
|
|
160
|
+
}
|
|
161
|
+
for (const { receipt, message, signature } of coverage.matched) {
|
|
162
|
+
if (!verify(null, Buffer.from(JSON.stringify(message), 'utf8'), publicKey, signature)) {
|
|
163
|
+
return { ok: false, reason: 'receipt_invalid_signature', gateId: receipt.gateId };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const rejected = validateClaims(coverage.matched, challenge);
|
|
167
|
+
if (rejected) {
|
|
168
|
+
return rejected;
|
|
169
|
+
}
|
|
170
|
+
const entries = coverage.matched.map(({ message, receipt }) => [message, receipt.signature]);
|
|
171
|
+
const receiptSetSha256 = createHash('sha256')
|
|
172
|
+
.update(JSON.stringify(['planu.verification-receipt-set.v1', entries]), 'utf8')
|
|
173
|
+
.digest('hex');
|
|
174
|
+
return { ok: true, gateCount: entries.length, receiptSetSha256 };
|
|
175
|
+
}
|
|
176
|
+
export async function captureReceiptEvidence(snapshot, workspaceRoot, policy, task, root, signal) {
|
|
177
|
+
if (path.resolve(snapshot.featDir, '../../..') !== root.canonical) {
|
|
178
|
+
return completionFailure('contract_drift');
|
|
179
|
+
}
|
|
180
|
+
const inputs = await captureVerificationInputs(workspaceRoot, policy.candidates, task, signal, root);
|
|
181
|
+
if (inputs.observedSha256 !== task.observedBaselineSha256) {
|
|
182
|
+
return receiptFailure('receipt_observed_baseline_drift', {
|
|
183
|
+
expectedSha256: task.observedBaselineSha256,
|
|
184
|
+
actualSha256: inputs.observedSha256,
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
success: true,
|
|
189
|
+
data: {
|
|
190
|
+
inputs,
|
|
191
|
+
evidence: {
|
|
192
|
+
version: 1,
|
|
193
|
+
featureId: path.basename(snapshot.featDir),
|
|
194
|
+
taskId: task.taskId,
|
|
195
|
+
workspaceSha256: inputs.workspaceSha256,
|
|
196
|
+
lockSha256: hashArtifact(snapshot.lockContent),
|
|
197
|
+
candidateSha256: inputs.candidateSha256,
|
|
198
|
+
observedSha256: inputs.observedSha256,
|
|
199
|
+
gates: [...task.gates].sort((left, right) => left.id < right.id ? -1 : left.id === right.id ? 0 : 1),
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
export async function completeReceiptTask(snapshot, workspaceRoot, policy, task, receipts, lease, signal, timeoutMs, runVerification) {
|
|
205
|
+
const root = await captureVerificationRoot(workspaceRoot);
|
|
206
|
+
const locked = await captureCompletionSnapshot(snapshot.featDir, {
|
|
207
|
+
writable: true,
|
|
208
|
+
workspaceRoot: root.canonical,
|
|
209
|
+
});
|
|
210
|
+
if (locked.digest !== snapshot.digest) {
|
|
211
|
+
return completionFailure('contract_drift');
|
|
212
|
+
}
|
|
213
|
+
if (signal.aborted) {
|
|
214
|
+
return completionFailure('cancelled');
|
|
215
|
+
}
|
|
216
|
+
if (!['darwin', 'linux'].includes(process.platform)) {
|
|
217
|
+
return unsupportedVerificationPlatform();
|
|
218
|
+
}
|
|
219
|
+
const captured = await captureReceiptEvidence(locked, workspaceRoot, policy, task, root, signal);
|
|
220
|
+
if (!captured.success) {
|
|
221
|
+
return captured;
|
|
222
|
+
}
|
|
223
|
+
const { evidence, inputs } = captured.data;
|
|
224
|
+
const verified = validateVerificationReceiptSet(receipts, evidence, policy.publicKey);
|
|
225
|
+
if (!verified.ok) {
|
|
226
|
+
return receiptFailure(verified.reason, verified.gateId ? { gateId: verified.gateId } : {});
|
|
227
|
+
}
|
|
228
|
+
const recheck = () => revalidateVerificationInputs(inputs, signal);
|
|
229
|
+
await recheck();
|
|
230
|
+
await revalidateCompletionSnapshot(locked);
|
|
231
|
+
const run = await runVerification({
|
|
232
|
+
cwd: root.canonical,
|
|
233
|
+
command: task.command,
|
|
234
|
+
timeoutMs,
|
|
235
|
+
signal,
|
|
236
|
+
});
|
|
237
|
+
if (!run.cleanupComplete) {
|
|
238
|
+
lease.retain();
|
|
239
|
+
}
|
|
240
|
+
if (!run.ok || !run.cleanupComplete) {
|
|
241
|
+
return verificationFailed({ ...run, output: '' });
|
|
242
|
+
}
|
|
243
|
+
await recheck();
|
|
244
|
+
const summary = 'Integration PASS | Receipts SHA-256 ' + verified.receiptSetSha256;
|
|
245
|
+
const marked = markReceiptTaskVerified(locked.tasksContent, task.lineIndex, summary);
|
|
246
|
+
if (marked === undefined) {
|
|
247
|
+
return completionFailure('contract_drift');
|
|
248
|
+
}
|
|
249
|
+
await persistCompletionSnapshot(lease, locked, marked, signal, recheck);
|
|
250
|
+
const remainingTasks = remainingTaskCount(activeTaskFromContent(marked, () => null));
|
|
251
|
+
return {
|
|
252
|
+
success: true,
|
|
253
|
+
data: {
|
|
254
|
+
completedTaskId: task.taskId,
|
|
255
|
+
verificationSummary: summary,
|
|
256
|
+
remainingTasks,
|
|
257
|
+
verificationEvidenceSummary: {
|
|
258
|
+
version: 1,
|
|
259
|
+
gateCount: verified.gateCount,
|
|
260
|
+
receiptSetSha256: verified.receiptSetSha256,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=verification-receipts.js.map
|