@planu/cli 8.3.0 → 8.4.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 +7 -0
- package/README.md +61 -2
- package/dist/sdd-engine/contracts.d.ts +5 -3
- package/dist/sdd-engine/contracts.js +23 -10
- package/dist/sdd-engine/feature-manifest.d.ts +2 -5
- package/dist/sdd-engine/feature-manifest.js +109 -184
- package/dist/sdd-engine/host-rule-pack.d.ts +5 -0
- package/dist/sdd-engine/host-rule-pack.js +102 -43
- package/dist/sdd-engine/host-skill-reader.d.ts +6 -0
- package/dist/sdd-engine/host-skill-reader.js +187 -0
- package/dist/sdd-engine/lock-integrity.d.ts +1 -0
- package/dist/sdd-engine/lock-integrity.js +25 -17
- package/dist/sdd-engine/receipt-assessment.d.ts +12 -0
- package/dist/sdd-engine/receipt-assessment.js +223 -0
- package/dist/sdd-engine/server.js +2 -4
- package/dist/sdd-engine/task-content.d.ts +11 -0
- package/dist/sdd-engine/task-content.js +28 -0
- package/dist/sdd-engine/task-window.d.ts +1 -0
- package/dist/sdd-engine/task-window.js +8 -1
- package/dist/sdd-engine/tools/execution-tools.d.ts +1 -11
- package/dist/sdd-engine/tools/execution-tools.js +5 -29
- package/dist/sdd-engine/tools/workspace-tools.d.ts +1 -1
- package/dist/sdd-engine/tools/workspace-tools.js +17 -9
- package/dist/sdd-engine/tools.d.ts +2 -1
- package/dist/sdd-engine/tools.js +4 -2
- package/dist/sdd-engine/trinity-structure.d.ts +25 -0
- package/dist/sdd-engine/trinity-structure.js +317 -0
- package/dist/sdd-engine/verification-preflight.d.ts +4 -0
- package/dist/sdd-engine/verification-preflight.js +271 -0
- package/dist/sdd-engine/verification-receipts.d.ts +2 -10
- package/dist/sdd-engine/verification-receipts.js +2 -169
- package/dist/sdd-engine/workspace-root.d.ts +1 -0
- package/dist/sdd-engine/workspace-root.js +1 -0
- package/dist/types/completion.d.ts +9 -2
- package/dist/types/sdd-engine.d.ts +71 -0
- package/package.json +1 -1
- package/templates/AGENTS.md +8 -0
- package/templates/host/planu-sdd.md +8 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { hasReceiptDirective, insideMarkdownBlock } from './task-content.js';
|
|
2
|
+
import { taskIdPattern } from './verification-receipts.js';
|
|
3
|
+
import { disjointPaths, isEvidencePath, CompletionGuardError } from './feature-authority.js';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
const CHECKBOX_TASK = /^[-*]\s*\[[ xX]\]/u;
|
|
6
|
+
const BDD_KEYWORDS = [/\bGIVEN\b/u, /\bWHEN\b/u, /\bTHEN\b/u];
|
|
7
|
+
export function assessTrinityCompleteness(specContent, planContent, tasksContent) {
|
|
8
|
+
const missing = [];
|
|
9
|
+
if (!hasHeading(specContent, 'Out-of-Scope')) {
|
|
10
|
+
missing.push('out_of_scope');
|
|
11
|
+
}
|
|
12
|
+
if (!hasHeading(specContent, 'Error Matrix')) {
|
|
13
|
+
missing.push('error_matrix');
|
|
14
|
+
}
|
|
15
|
+
if (!BDD_KEYWORDS.every((keyword) => keyword.test(specContent))) {
|
|
16
|
+
missing.push('bdd');
|
|
17
|
+
}
|
|
18
|
+
if (!hasHeading(planContent, 'Frozen Dependencies')) {
|
|
19
|
+
missing.push('frozen_dependencies');
|
|
20
|
+
}
|
|
21
|
+
const hasTask = tasksContent.split('\n').some((line) => CHECKBOX_TASK.test(line.trim()));
|
|
22
|
+
const manifest = scanManifest(planContent);
|
|
23
|
+
if (!manifest.hasPath) {
|
|
24
|
+
missing.push('manifest_path');
|
|
25
|
+
}
|
|
26
|
+
if (!hasTask) {
|
|
27
|
+
missing.push('checkbox_task');
|
|
28
|
+
}
|
|
29
|
+
const issues = boundedTrinityIssues([...missing.map(missingIssue), ...manifest.issues]);
|
|
30
|
+
if (manifest.globDelete) {
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
code: 'ERR_TRINITY_INCOMPLETE',
|
|
34
|
+
message: 'DELETE File Manifest entries must be exact paths, not globs.',
|
|
35
|
+
details: { issues },
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (manifest.protectedWrite !== null) {
|
|
39
|
+
return {
|
|
40
|
+
ok: false,
|
|
41
|
+
code: 'ERR_TRINITY_INCOMPLETE',
|
|
42
|
+
message: 'File Manifest cannot grant writes on protected root paths.',
|
|
43
|
+
details: { issues },
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
if (missing.length === 0) {
|
|
47
|
+
return { ok: true };
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
ok: false,
|
|
51
|
+
code: 'ERR_TRINITY_INCOMPLETE',
|
|
52
|
+
message: 'Lock requires Out-of-Scope, Error Matrix, GIVEN/WHEN/THEN acceptance criteria, Frozen Dependencies, a File Manifest path, and at least one checkbox task.',
|
|
53
|
+
details: { missing, issues },
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function hasHeading(content, needle) {
|
|
57
|
+
const escaped = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
58
|
+
return new RegExp(`^#{1,3}\\s+.*${escaped}`, 'imu').test(content);
|
|
59
|
+
}
|
|
60
|
+
const PROTECTED_ROOTS = ['.sdd', '.git', 'planu/specs'];
|
|
61
|
+
function scanManifest(planContent) {
|
|
62
|
+
const effective = new Map(manifestEntries(planContent).map((entry) => [toPosixPath(entry.rawPath), entry]));
|
|
63
|
+
const issues = [];
|
|
64
|
+
let globDelete = false;
|
|
65
|
+
let protectedWrite = null;
|
|
66
|
+
for (const [entryPath, { permission, line }] of effective) {
|
|
67
|
+
if (permission === 'DELETE' && entryPath.includes('*')) {
|
|
68
|
+
globDelete = true;
|
|
69
|
+
issues.push({ artifact: 'plan.md', rule: 'glob_delete', line });
|
|
70
|
+
}
|
|
71
|
+
const normalized = path.posix.normalize(entryPath).toLowerCase();
|
|
72
|
+
if (permission !== 'READ_ONLY' &&
|
|
73
|
+
PROTECTED_ROOTS.some((root) => normalized === root || normalized.startsWith(`${root}/`))) {
|
|
74
|
+
protectedWrite ??= entryPath;
|
|
75
|
+
issues.push({ artifact: 'plan.md', rule: 'protected_write', line });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return { hasPath: effective.size > 0, globDelete, protectedWrite, issues };
|
|
79
|
+
}
|
|
80
|
+
function missingIssue(rule) {
|
|
81
|
+
const artifact = ['out_of_scope', 'error_matrix', 'bdd'].includes(rule)
|
|
82
|
+
? 'spec.md'
|
|
83
|
+
: rule === 'checkbox_task'
|
|
84
|
+
? 'tasks.md'
|
|
85
|
+
: 'plan.md';
|
|
86
|
+
return { artifact, rule: rule };
|
|
87
|
+
}
|
|
88
|
+
function boundedTrinityIssues(issues) {
|
|
89
|
+
const order = ['spec.md', 'plan.md', 'tasks.md'];
|
|
90
|
+
return [...issues]
|
|
91
|
+
.sort((left, right) => order.indexOf(left.artifact) - order.indexOf(right.artifact) ||
|
|
92
|
+
(left.line ?? 0) - (right.line ?? 0) ||
|
|
93
|
+
(left.rule < right.rule ? -1 : left.rule > right.rule ? 1 : 0))
|
|
94
|
+
.slice(0, 12);
|
|
95
|
+
}
|
|
96
|
+
export function parseManifestContent(content) {
|
|
97
|
+
const allowedFiles = new Map();
|
|
98
|
+
for (const { rawPath, permission } of manifestEntries(content)) {
|
|
99
|
+
allowedFiles.set(toPosixPath(rawPath), permission);
|
|
100
|
+
}
|
|
101
|
+
return { allowedFiles };
|
|
102
|
+
}
|
|
103
|
+
export function manifestEntries(content) {
|
|
104
|
+
const entries = [];
|
|
105
|
+
let inManifestSection = false;
|
|
106
|
+
for (const [index, rawLine] of content.split('\n').entries()) {
|
|
107
|
+
const line = rawLine.trim();
|
|
108
|
+
if (/^#{1,3}\s+/.test(line)) {
|
|
109
|
+
inManifestSection = /^#{1,3}\s+.*file\s+manifest/i.test(line);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (inManifestSection && /^[-*]\s+/.test(line)) {
|
|
113
|
+
const entry = parseManifestLine(line);
|
|
114
|
+
if (entry && toPosixPath(entry.rawPath).toLowerCase() !== 'none') {
|
|
115
|
+
entries.push({ ...entry, line: index + 1 });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return entries;
|
|
120
|
+
}
|
|
121
|
+
function parseManifestLine(rawLine) {
|
|
122
|
+
const item = rawLine.replace(/^[-*]\s+/, '').trim();
|
|
123
|
+
const prefix = /^(CREATE|MODIFY|READ_ONLY|DELETE):\s*`?([^`\s#]+)`?\s*$/i.exec(item);
|
|
124
|
+
if (prefix?.[1] && prefix[2]) {
|
|
125
|
+
return {
|
|
126
|
+
rawPath: prefix[2],
|
|
127
|
+
permission: prefix[1].toUpperCase(),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const postfix = /^`?([^`\s:#]+)`?:\s*(CREATE|MODIFY|READ_ONLY|DELETE)\s*$/i.exec(item);
|
|
131
|
+
if (postfix?.[1] && postfix[2]) {
|
|
132
|
+
return {
|
|
133
|
+
rawPath: postfix[1],
|
|
134
|
+
permission: postfix[2].toUpperCase(),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
export function toPosixPath(value) {
|
|
140
|
+
const posix = value.replaceAll('\\', '/');
|
|
141
|
+
const absolute = posix.startsWith('/');
|
|
142
|
+
const joined = posix
|
|
143
|
+
.split('/')
|
|
144
|
+
.filter((part) => part !== '' && part !== '.')
|
|
145
|
+
.join('/');
|
|
146
|
+
return absolute ? `/${joined}` : joined;
|
|
147
|
+
}
|
|
148
|
+
export class TrinityReadError extends CompletionGuardError {
|
|
149
|
+
issues;
|
|
150
|
+
constructor(error, artifact) {
|
|
151
|
+
super(error.reason, error.message, error.code);
|
|
152
|
+
this.issues = [{ artifact, rule: 'artifact_unreadable' }];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export function readTrinityFailureIssues(error) {
|
|
156
|
+
return error instanceof TrinityReadError ? [...error.issues] : undefined;
|
|
157
|
+
}
|
|
158
|
+
function policySourceLocations(text, firstLine) {
|
|
159
|
+
const locations = new Map();
|
|
160
|
+
const stack = [];
|
|
161
|
+
for (const [index, line] of text.split('\n').entries()) {
|
|
162
|
+
const value = line.trim().replace(/,$/u, '');
|
|
163
|
+
const indent = line.length - line.trimStart().length;
|
|
164
|
+
if (index === 0 && value === '{') {
|
|
165
|
+
stack.push({ path: '', root: true, kind: 'object', indent, nextArrayIndex: 0 });
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const parent = stack.at(-1);
|
|
169
|
+
if (!parent) {
|
|
170
|
+
return new Map();
|
|
171
|
+
}
|
|
172
|
+
if (value === '}' || value === ']') {
|
|
173
|
+
if (indent !== parent.indent) {
|
|
174
|
+
return new Map();
|
|
175
|
+
}
|
|
176
|
+
stack.pop();
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const located = policyValueSource(value, parent);
|
|
180
|
+
if (parent.kind === 'array') {
|
|
181
|
+
stack[stack.length - 1] = { ...parent, nextArrayIndex: parent.nextArrayIndex + 1 };
|
|
182
|
+
}
|
|
183
|
+
if (!located || indent !== parent.indent + 2) {
|
|
184
|
+
return new Map();
|
|
185
|
+
}
|
|
186
|
+
locations.set(located.path, firstLine + index);
|
|
187
|
+
if (located.value === '{' || located.value === '[') {
|
|
188
|
+
stack.push({
|
|
189
|
+
path: located.path,
|
|
190
|
+
root: false,
|
|
191
|
+
kind: located.value === '{' ? 'object' : 'array',
|
|
192
|
+
indent,
|
|
193
|
+
nextArrayIndex: 0,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return stack.length === 0 ? locations : new Map();
|
|
198
|
+
}
|
|
199
|
+
function policyValueSource(value, parent) {
|
|
200
|
+
if (parent.kind === 'array') {
|
|
201
|
+
const result = { path: parent.path + '/' + String(parent.nextArrayIndex), value };
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
const property = /^("(?:[^"\\]|\\.)*"): (.+)$/u.exec(value);
|
|
205
|
+
if (!property?.[1] || property[2] === undefined) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
let key;
|
|
209
|
+
try {
|
|
210
|
+
key = JSON.parse(property[1]);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
if (typeof key !== 'string') {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
const segment = policySourceKey(key);
|
|
219
|
+
return {
|
|
220
|
+
path: parent.root ? segment : parent.path + '/' + segment,
|
|
221
|
+
value: property[2],
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
const POLICY_LIMIT = 65_536;
|
|
225
|
+
const RECEIPT_HEADING = /^#{1,6}\s+Verification\s+Receipts(?:\s*$|\s*v[0-9])/iu;
|
|
226
|
+
const FENCE = String.fromCharCode(96).repeat(3);
|
|
227
|
+
export function policyText(plan) {
|
|
228
|
+
const lines = plan.replaceAll('\r\n', '\n').split('\n');
|
|
229
|
+
const headings = lines.flatMap((line, index) => RECEIPT_HEADING.test(line.trim()) ? [index] : []);
|
|
230
|
+
const start = headings[0];
|
|
231
|
+
if (start === undefined) {
|
|
232
|
+
return { text: undefined };
|
|
233
|
+
}
|
|
234
|
+
const invalidStart = insideMarkdownBlock(lines.slice(0, start)) ||
|
|
235
|
+
lines[start]?.trim() !== '## Verification Receipts v1';
|
|
236
|
+
const offendingHeading = invalidStart ? start : (headings[1] ?? start);
|
|
237
|
+
const headingLine = offendingHeading + 1;
|
|
238
|
+
if (headings.length !== 1 || invalidStart) {
|
|
239
|
+
return { text: null, line: headingLine };
|
|
240
|
+
}
|
|
241
|
+
const after = lines.findIndex((line, index) => index > start && /^#{1,2}\s/u.test(line.trim()));
|
|
242
|
+
const sectionLines = lines.slice(start + 1, after < 0 ? undefined : after);
|
|
243
|
+
const section = sectionLines.join('\n').trim();
|
|
244
|
+
const prefix = FENCE + 'json\n';
|
|
245
|
+
const suffix = '\n' + FENCE;
|
|
246
|
+
if (!section.startsWith(prefix) || !section.endsWith(suffix)) {
|
|
247
|
+
return { text: null };
|
|
248
|
+
}
|
|
249
|
+
const payload = section.slice(prefix.length, -suffix.length);
|
|
250
|
+
const fenceIndex = sectionLines.findIndex((line) => line.trim().length > 0);
|
|
251
|
+
const blankLines = payload.split('\n').findIndex((line) => line.trim().length > 0);
|
|
252
|
+
const text = Buffer.byteLength(payload) <= POLICY_LIMIT ? payload.trim() : null;
|
|
253
|
+
return {
|
|
254
|
+
text,
|
|
255
|
+
locations: text === null
|
|
256
|
+
? undefined
|
|
257
|
+
: policySourceLocations(text, start + fenceIndex + Math.max(blankLines, 0) + 3),
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
export function invalidTask(block, duplicateLineIndex) {
|
|
261
|
+
const id = block.explicitId ?? block.id;
|
|
262
|
+
const directives = block.lines.flatMap((line, index) => hasReceiptDirective(line) ? [index] : []);
|
|
263
|
+
const offset = directives[1] ?? directives[0] ?? -1;
|
|
264
|
+
const issue = {
|
|
265
|
+
artifact: 'tasks.md',
|
|
266
|
+
rule: 'receipt_policy_invalid',
|
|
267
|
+
...(duplicateLineIndex !== undefined
|
|
268
|
+
? { line: duplicateLineIndex + 1 }
|
|
269
|
+
: offset < 0
|
|
270
|
+
? {}
|
|
271
|
+
: { line: block.lineIndex + offset + 2 }),
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
status: 'invalid',
|
|
275
|
+
...(taskIdPattern.test(id) ? { taskId: id } : {}),
|
|
276
|
+
issue,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
export function invalidCandidate(entries, paths) {
|
|
280
|
+
const bad = entries.find(({ rawPath, permission }, index) => !isEvidencePath(rawPath) ||
|
|
281
|
+
permission === 'DELETE' ||
|
|
282
|
+
!disjointPaths(paths.slice(0, index + 1)));
|
|
283
|
+
return bad && (!isEvidencePath(bad.rawPath) || bad.permission === 'DELETE') ? bad : undefined;
|
|
284
|
+
}
|
|
285
|
+
export function candidateIssue(entry) {
|
|
286
|
+
return {
|
|
287
|
+
artifact: 'plan.md',
|
|
288
|
+
rule: 'receipt_policy_invalid',
|
|
289
|
+
...(entry ? { line: entry.line } : {}),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
export function policyIssueLine(issue, locations) {
|
|
293
|
+
const unknownKey = issue.code === 'unrecognized_keys' ? issue.keys[0] : undefined;
|
|
294
|
+
const issuePath = issue.code === 'unrecognized_keys'
|
|
295
|
+
? issue.keys.length === 1 && unknownKey !== undefined
|
|
296
|
+
? [...issue.path, unknownKey]
|
|
297
|
+
: undefined
|
|
298
|
+
: issue.path;
|
|
299
|
+
return issuePath
|
|
300
|
+
? locations.get(issuePath.map((key) => policySourceKey(String(key))).join('/'))
|
|
301
|
+
: undefined;
|
|
302
|
+
}
|
|
303
|
+
function policySourceKey(key) {
|
|
304
|
+
return key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
305
|
+
}
|
|
306
|
+
export function candidateCreationIssue(rawPath, creates, requiredFrom, locations) {
|
|
307
|
+
const index = creates.findIndex(({ path: candidatePath }) => candidatePath === rawPath);
|
|
308
|
+
const line = requiredFrom === undefined
|
|
309
|
+
? locations.get('candidateCreates/' + String(index) + '/requiredFromTaskId')
|
|
310
|
+
: undefined;
|
|
311
|
+
return {
|
|
312
|
+
artifact: 'plan.md',
|
|
313
|
+
rule: 'receipt_policy_invalid',
|
|
314
|
+
...(line === undefined ? {} : { line }),
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
//# sourceMappingURL=trinity-structure.js.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ActiveTaskResult, SddResult } from '../types/sdd-engine.js';
|
|
2
|
+
import type { ScopedPolicy } from '../types/verification.js';
|
|
3
|
+
export declare function getVerificationPreflight(tasksPath: string, workspaceRoot: string, extractCommand: (markdown: string) => string | null, receipts?: unknown, scopedPolicy?: ScopedPolicy, signal?: AbortSignal): Promise<SddResult<ActiveTaskResult>>;
|
|
4
|
+
//# sourceMappingURL=verification-preflight.d.ts.map
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import { CompletionGuardError, captureDirectories, lastAuthorityDirectory, readArtifact, revalidateDirectories, sameIdentity, } from './feature-authority.js';
|
|
3
|
+
import { inspectCompletionGuard } from './completion-guard.js';
|
|
4
|
+
import { captureCompletionSnapshot, revalidateCompletionSnapshot } from './completion-snapshot.js';
|
|
5
|
+
import { parseManifestContent, parseVerificationReceiptPolicy } from './feature-manifest.js';
|
|
6
|
+
import { activeTaskFromContent } from './task-content.js';
|
|
7
|
+
import { completionError } from './task-failures.js';
|
|
8
|
+
import { captureReceiptEvidence, assessVerificationReceiptSet } from './verification-receipts.js';
|
|
9
|
+
import { captureVerificationRoot, revalidateVerificationInputs, VerificationInputError, } from './verification-inputs.js';
|
|
10
|
+
import { isScopedVerificationFeature } from './verification-policy.js';
|
|
11
|
+
function report(taskId, reasons, gates = [], applicable = true) {
|
|
12
|
+
const blockers = [...new Set(reasons)]
|
|
13
|
+
.sort()
|
|
14
|
+
.slice(0, 16)
|
|
15
|
+
.map((reason) => ({ reason }));
|
|
16
|
+
const rejectedGate = gates.some((gate) => gate.state !== 'receipt_valid');
|
|
17
|
+
const actionReasons = new Set(reasons);
|
|
18
|
+
for (const gate of gates) {
|
|
19
|
+
if (gate.reason) {
|
|
20
|
+
actionReasons.add(gate.reason);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
version: 1,
|
|
25
|
+
taskId,
|
|
26
|
+
status: !applicable
|
|
27
|
+
? 'not_applicable'
|
|
28
|
+
: blockers.length || rejectedGate
|
|
29
|
+
? 'blocked'
|
|
30
|
+
: 'ready_to_attempt',
|
|
31
|
+
gates,
|
|
32
|
+
blockers,
|
|
33
|
+
integration: 'not_run',
|
|
34
|
+
nextAction: applicable ? nextAction(actionReasons) : 'none',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function nextAction(reasons) {
|
|
38
|
+
if (reasons.has('unlocked')) {
|
|
39
|
+
return 'review_lock';
|
|
40
|
+
}
|
|
41
|
+
if (reasons.has('platform_unsupported') || reasons.has('receipt_scoped_unsupported')) {
|
|
42
|
+
return 'use_supported_runtime';
|
|
43
|
+
}
|
|
44
|
+
if (reasons.has('command_missing')) {
|
|
45
|
+
return 'restore_verification';
|
|
46
|
+
}
|
|
47
|
+
if (reasons.has('receipt_input_unavailable')) {
|
|
48
|
+
return 'fix_inputs';
|
|
49
|
+
}
|
|
50
|
+
if (reasons.has('receipt_observed_baseline_drift')) {
|
|
51
|
+
return 'review_observed_drift';
|
|
52
|
+
}
|
|
53
|
+
if (reasons.has('receipt_not_configured')) {
|
|
54
|
+
return 'restore_verification';
|
|
55
|
+
}
|
|
56
|
+
return reasons.size ? 'obtain_receipts' : 'attempt_integration';
|
|
57
|
+
}
|
|
58
|
+
function taskBlockers(active, task) {
|
|
59
|
+
const reasons = [];
|
|
60
|
+
if (!(task ? task.command : active.verificationCommand)) {
|
|
61
|
+
reasons.push('command_missing');
|
|
62
|
+
}
|
|
63
|
+
if (!['darwin', 'linux'].includes(process.platform)) {
|
|
64
|
+
reasons.push('platform_unsupported');
|
|
65
|
+
}
|
|
66
|
+
return reasons;
|
|
67
|
+
}
|
|
68
|
+
/** The same captured authority can be assessed by other read views without selecting again. */
|
|
69
|
+
async function assessVerificationPreflight(snapshot, root, workspaceRoot, active, receipts, scopedPolicy, signal) {
|
|
70
|
+
if (!active) {
|
|
71
|
+
return report(null, [], [], false);
|
|
72
|
+
}
|
|
73
|
+
const policy = parseVerificationReceiptPolicy(path.basename(snapshot.featDir), snapshot.planContent, snapshot.tasksContent);
|
|
74
|
+
if (policy.status === 'invalid') {
|
|
75
|
+
throw new CompletionGuardError('contract_drift', 'Preflight authority changed.');
|
|
76
|
+
}
|
|
77
|
+
const task = policy.status === 'ready' ? policy.tasks.get(active.taskId) : undefined;
|
|
78
|
+
const reasons = taskBlockers(active, task);
|
|
79
|
+
if (policy.status !== 'ready' || !task) {
|
|
80
|
+
if (receipts !== undefined) {
|
|
81
|
+
reasons.push('receipt_not_configured');
|
|
82
|
+
}
|
|
83
|
+
return report(active.taskId, reasons);
|
|
84
|
+
}
|
|
85
|
+
if (scopedPolicy && (await isScopedVerificationFeature(scopedPolicy, snapshot.featDir))) {
|
|
86
|
+
reasons.push('receipt_scoped_unsupported');
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
const captured = await captureReceiptEvidence(snapshot, workspaceRoot, policy, task, root, signal);
|
|
90
|
+
if (!captured.success) {
|
|
91
|
+
if (captured.error.details?.reason !== 'receipt_observed_baseline_drift') {
|
|
92
|
+
throw new CompletionGuardError('contract_drift', 'Preflight authority changed.');
|
|
93
|
+
}
|
|
94
|
+
return report(active.taskId, [...reasons, 'receipt_observed_baseline_drift']);
|
|
95
|
+
}
|
|
96
|
+
const assessed = assessVerificationReceiptSet(receipts, captured.data.evidence, policy.publicKey);
|
|
97
|
+
await revalidateVerificationInputs(captured.data.inputs, signal);
|
|
98
|
+
return report(active.taskId, [...reasons, ...assessed.blockers.map(({ reason }) => reason)], assessed.gates);
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (error instanceof VerificationInputError && error.reason === 'receipt_input_unavailable') {
|
|
102
|
+
return report(active.taskId, [...reasons, 'receipt_input_unavailable']);
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function optionalArtifact(feature, name) {
|
|
108
|
+
try {
|
|
109
|
+
return await readArtifact(feature, name);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (error.code === 'ENOENT') {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function requiredTaskArtifact(feature) {
|
|
119
|
+
const task = await optionalArtifact(feature, 'tasks.md');
|
|
120
|
+
if (!task) {
|
|
121
|
+
throw new CompletionGuardError('contract_drift', 'Task authority is unavailable.', 'ERR_TASK_NOT_FOUND');
|
|
122
|
+
}
|
|
123
|
+
return task;
|
|
124
|
+
}
|
|
125
|
+
async function captureReadSnapshot(feature, root) {
|
|
126
|
+
try {
|
|
127
|
+
const locked = await captureCompletionSnapshot(feature, {
|
|
128
|
+
workspaceRoot: root.canonical,
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
feature: locked.featDir,
|
|
132
|
+
directories: locked.directories,
|
|
133
|
+
artifacts: locked.artifacts,
|
|
134
|
+
locked,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (error.code === 'ENOENT') {
|
|
139
|
+
await requiredTaskArtifact(feature);
|
|
140
|
+
throw new CompletionGuardError('contract_drift', 'Preflight authority is unavailable.');
|
|
141
|
+
}
|
|
142
|
+
if (!(error instanceof CompletionGuardError) || error.code !== 'ERR_SPEC_NOT_LOCKED') {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const directories = await captureDirectories(feature, root.canonical);
|
|
147
|
+
const canonical = lastAuthorityDirectory(directories).directory;
|
|
148
|
+
const artifacts = {};
|
|
149
|
+
for (const name of ['.lock', 'spec.md', 'plan.md', 'tasks.md']) {
|
|
150
|
+
artifacts[name] =
|
|
151
|
+
name === 'tasks.md'
|
|
152
|
+
? await requiredTaskArtifact(canonical)
|
|
153
|
+
: await optionalArtifact(canonical, name);
|
|
154
|
+
}
|
|
155
|
+
if (artifacts['.lock']) {
|
|
156
|
+
throw new CompletionGuardError('contract_drift', 'Preflight authority changed.');
|
|
157
|
+
}
|
|
158
|
+
return { feature: canonical, directories, artifacts };
|
|
159
|
+
}
|
|
160
|
+
async function revalidateReadSnapshot(snapshot, root, workspaceRoot) {
|
|
161
|
+
if (snapshot.locked) {
|
|
162
|
+
await revalidateCompletionSnapshot(snapshot.locked);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
await revalidateDirectories(snapshot.directories);
|
|
166
|
+
for (const name of ['.lock', 'spec.md', 'plan.md', 'tasks.md']) {
|
|
167
|
+
const current = await optionalArtifact(snapshot.feature, name);
|
|
168
|
+
const expected = snapshot.artifacts[name];
|
|
169
|
+
if (current?.content !== expected?.content ||
|
|
170
|
+
(current && expected && !sameIdentity(current.identity, expected.identity))) {
|
|
171
|
+
throw new CompletionGuardError('contract_drift', 'Preflight authority changed.');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
await revalidateDirectories(snapshot.directories);
|
|
175
|
+
}
|
|
176
|
+
const currentRoot = await captureVerificationRoot(workspaceRoot);
|
|
177
|
+
if (root.canonical !== currentRoot.canonical ||
|
|
178
|
+
root.stat.dev !== currentRoot.stat.dev ||
|
|
179
|
+
root.stat.ino !== currentRoot.stat.ino ||
|
|
180
|
+
root.stat.mode !== currentRoot.stat.mode ||
|
|
181
|
+
root.stat.uid !== currentRoot.stat.uid ||
|
|
182
|
+
root.stat.gid !== currentRoot.stat.gid) {
|
|
183
|
+
throw new CompletionGuardError('contract_drift', 'Preflight authority changed.');
|
|
184
|
+
}
|
|
185
|
+
await assertGuardAbsent(snapshot.feature);
|
|
186
|
+
}
|
|
187
|
+
async function assertGuardAbsent(feature) {
|
|
188
|
+
const guard = await inspectCompletionGuard(feature);
|
|
189
|
+
if (guard) {
|
|
190
|
+
throw new CompletionGuardError(guard.reason, 'Preflight authority is guarded.', 'ERR_VERIFICATION_FAILED');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function receiptActiveTask(snapshot, active) {
|
|
194
|
+
if (!snapshot || !active) {
|
|
195
|
+
return active;
|
|
196
|
+
}
|
|
197
|
+
const policy = parseVerificationReceiptPolicy(path.basename(snapshot.featDir), snapshot.planContent, snapshot.tasksContent);
|
|
198
|
+
const task = policy.status === 'ready' ? policy.tasks.get(active.taskId) : undefined;
|
|
199
|
+
return task ? { ...active, verificationCommand: task.command } : active;
|
|
200
|
+
}
|
|
201
|
+
function preflightError(error) {
|
|
202
|
+
const failure = completionError(error, 'sdd_get_active_task');
|
|
203
|
+
if (failure.success) {
|
|
204
|
+
return {
|
|
205
|
+
success: false,
|
|
206
|
+
error: {
|
|
207
|
+
code: 'ERR_VERIFICATION_FAILED',
|
|
208
|
+
message: 'Verification preflight is unavailable.',
|
|
209
|
+
trigger: 'sdd_get_active_task',
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const rawReason = failure.error.details?.reason;
|
|
214
|
+
const reasons = new Set([
|
|
215
|
+
'guard_present',
|
|
216
|
+
'contract_drift',
|
|
217
|
+
'cancelled',
|
|
218
|
+
'persistence_uncertain',
|
|
219
|
+
'receipt_input_drift',
|
|
220
|
+
'receipt_input_unavailable',
|
|
221
|
+
]);
|
|
222
|
+
const reason = typeof rawReason === 'string' && reasons.has(rawReason) ? rawReason : 'failed';
|
|
223
|
+
return {
|
|
224
|
+
success: false,
|
|
225
|
+
error: {
|
|
226
|
+
code: failure.error.code,
|
|
227
|
+
message: 'Verification preflight is unavailable; inspect the bounded reason before retrying.',
|
|
228
|
+
trigger: 'sdd_get_active_task',
|
|
229
|
+
details: { reason },
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
export async function getVerificationPreflight(tasksPath, workspaceRoot, extractCommand, receipts, scopedPolicy, signal) {
|
|
234
|
+
try {
|
|
235
|
+
const root = await captureVerificationRoot(workspaceRoot);
|
|
236
|
+
const snapshot = await captureReadSnapshot(path.dirname(tasksPath), root);
|
|
237
|
+
await assertGuardAbsent(snapshot.feature);
|
|
238
|
+
const current = activeTaskFromContent(snapshot.artifacts['tasks.md']?.content ?? '', extractCommand);
|
|
239
|
+
if (!current.success) {
|
|
240
|
+
return current;
|
|
241
|
+
}
|
|
242
|
+
const data = {
|
|
243
|
+
...current.data,
|
|
244
|
+
activeTask: receiptActiveTask(snapshot.locked, current.data.activeTask),
|
|
245
|
+
};
|
|
246
|
+
const verificationPreflight = snapshot.locked
|
|
247
|
+
? await assessVerificationPreflight(snapshot.locked, root, workspaceRoot, data.activeTask, receipts, scopedPolicy, signal)
|
|
248
|
+
: report(data.activeTask?.taskId ?? null, ['unlocked']);
|
|
249
|
+
if (signal?.aborted) {
|
|
250
|
+
throw new CompletionGuardError('cancelled', 'Preflight was cancelled.');
|
|
251
|
+
}
|
|
252
|
+
await revalidateReadSnapshot(snapshot, root, workspaceRoot);
|
|
253
|
+
const manifest = [
|
|
254
|
+
...parseManifestContent(snapshot.artifacts['plan.md']?.content ?? '').allowedFiles.entries(),
|
|
255
|
+
].map(([filePath, permission]) => ({ path: filePath, permission }));
|
|
256
|
+
return {
|
|
257
|
+
success: true,
|
|
258
|
+
data: {
|
|
259
|
+
...data,
|
|
260
|
+
locked: !!snapshot.locked,
|
|
261
|
+
trinity: snapshot.locked ? 'ready' : 'unlocked',
|
|
262
|
+
manifest,
|
|
263
|
+
verificationPreflight,
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
return preflightError(error);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
//# sourceMappingURL=verification-preflight.js.map
|
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import type { CompleteTaskResult, ReceiptPolicyResult, ReceiptSetResult, ReceiptTaskPolicy, SddResult, VerificationEvidence, WorkspaceRoot } from '../types/sdd-engine.js';
|
|
1
|
+
export { maxVerificationReceipts, gateIdPattern, taskIdPattern, featureIdPattern, sha256Schema, importReceiptPublicKey, validateVerificationReceiptSet, assessVerificationReceiptSet, } from './receipt-assessment.js';
|
|
2
|
+
import type { CompleteTaskResult, ReceiptPolicyResult, ReceiptTaskPolicy, SddResult, VerificationEvidence, WorkspaceRoot } from '../types/sdd-engine.js';
|
|
4
3
|
import type { CompletionSnapshot } from '../types/completion.js';
|
|
5
4
|
import type { CompletionLease } from './completion-guard.js';
|
|
6
5
|
import { captureVerificationInputs } from './verification-inputs.js';
|
|
7
6
|
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
7
|
export declare function captureReceiptEvidence(snapshot: CompletionSnapshot, workspaceRoot: string, policy: Extract<ReceiptPolicyResult, {
|
|
16
8
|
status: 'ready';
|
|
17
9
|
}>, task: ReceiptTaskPolicy, root: WorkspaceRoot, signal?: AbortSignal): Promise<SddResult<{
|