@punks/cli 3.1.10 → 3.1.11
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/dist/BASELINE_CHANGELOG.md +33 -0
- package/dist/CHANGELOG.md +30 -0
- package/dist/data/subagents/manifest.mjs +11 -6
- package/dist/index.js +29 -29
- package/dist/skills/agnostic/cli/hi-cli/references/post-command-flow.md +5 -0
- package/dist/skills/agnostic/planning/create-plan/REFERENCE.md +1 -1
- package/dist/skills/agnostic/planning/create-plan/SKILL.md +19 -7
- package/dist/skills/agnostic/planning/create-plan/references/plan-schema.md +19 -0
- package/dist/skills/agnostic/planning/create-plan/references/planner-task-graph.md +27 -10
- package/dist/skills/agnostic/planning/implement-spec/SKILL.md +6 -2
- package/dist/skills/agnostic/planning/implement-spec/assets/IMPLEMENTATION-NOTES-TEMPLATE.md +22 -6
- package/dist/skills/agnostic/planning/implement-spec/references/lifecycle.md +12 -0
- package/dist/skills/agnostic/planning/implement-spec/references/parallel-orchestration.md +10 -2
- package/dist/skills/agnostic/planning/implement-spec/references/parallel-worker-brief.md +10 -1
- package/dist/skills/agnostic/planning/implement-spec/references/parallel.md +2 -0
- package/dist/skills/agnostic/quality/autoreview/SKILL.md +11 -2
- package/dist/skills/agnostic/quality/review/SKILL.md +16 -11
- package/dist/skills/languages/python/python-backend-structure/SKILL.md +91 -0
- package/dist/skills/languages/python/python-backend-structure/agents/openai.yaml +4 -0
- package/dist/skills/languages/python/python-design-patterns/SKILL.md +93 -69
- package/dist/skills/languages/python/python-project-structure/SKILL.md +4 -247
- package/dist/skills/phases/delivery-phase/SKILL.md +13 -4
- package/dist/skills/phases/delivery-phase/phases/closeout.md +3 -0
- package/dist/skills/phases/delivery-phase/phases/debug.md +8 -1
- package/dist/skills/phases/delivery-phase/phases/implement.md +11 -2
- package/dist/skills/phases/delivery-phase/phases/review.md +50 -29
- package/dist/skills/phases/delivery-phase/phases/router.md +56 -36
- package/dist/skills/phases/delivery-phase/references/artifact-state.md +8 -2
- package/dist/skills/phases/delivery-phase/references/phase-handoff.md +60 -10
- package/dist/skills/phases/finder-phase/references/root-routing.md +6 -1
- package/dist/skills/phases/review-phase/REFERENCE.md +83 -145
- package/dist/skills/phases/review-phase/SKILL.md +41 -45
- package/dist/skills/phases/review-phase/agents/openai.yaml +2 -0
- package/dist/skills/phases/review-phase/references/durable-report.md +172 -0
- package/dist/skills/phases/review-phase/references/state-graph.md +69 -0
- package/dist/skills/phases/review-phase/references/targets.md +156 -0
- package/dist/skills/phases/review-phase/scripts/review-contract.mjs +819 -0
- package/package.json +9 -9
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
|
|
4
|
+
const RECORD_HEADER = Buffer.from("review-identity-v1\n", "utf8");
|
|
5
|
+
|
|
6
|
+
const asText = (value, field) => {
|
|
7
|
+
if (typeof value !== "string") {
|
|
8
|
+
throw new TypeError(`${field} must be a string`);
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const frame = (value) => {
|
|
14
|
+
const bytes = Buffer.from(value, "utf8");
|
|
15
|
+
return Buffer.concat([Buffer.from(`${bytes.length}:`, "ascii"), bytes]);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const canonicalRecord = (label, fields) =>
|
|
19
|
+
Buffer.concat([
|
|
20
|
+
RECORD_HEADER,
|
|
21
|
+
frame(asText(label, "label")),
|
|
22
|
+
...fields.map((value, index) => frame(asText(value, `field ${index}`))),
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
export const sha256Hex = (bytes) =>
|
|
26
|
+
createHash("sha256").update(bytes).digest("hex");
|
|
27
|
+
|
|
28
|
+
export const hashRecord = (label, fields) =>
|
|
29
|
+
sha256Hex(canonicalRecord(label, fields));
|
|
30
|
+
|
|
31
|
+
export const sortUtf8 = (values) =>
|
|
32
|
+
[...values].sort((left, right) =>
|
|
33
|
+
Buffer.compare(Buffer.from(left, "utf8"), Buffer.from(right, "utf8")),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
export const inclusiveScopeHash = (entries) =>
|
|
37
|
+
hashRecord("inclusive-scope", sortUtf8(entries));
|
|
38
|
+
|
|
39
|
+
export const acceptedBoundsHash = (identity, scopeEntries) =>
|
|
40
|
+
hashRecord("accepted-bounds", [identity, inclusiveScopeHash(scopeEntries)]);
|
|
41
|
+
|
|
42
|
+
export const deliveryLineageId = (deliveryGoalIdentity) =>
|
|
43
|
+
hashRecord("delivery-lineage", [deliveryGoalIdentity]);
|
|
44
|
+
|
|
45
|
+
export const standaloneLineageId = (locator, boundsHash) =>
|
|
46
|
+
hashRecord("standalone-lineage", [locator, boundsHash]);
|
|
47
|
+
|
|
48
|
+
export const deliveryRunId = (lineageId, ordinal) => {
|
|
49
|
+
if (!Number.isSafeInteger(ordinal) || ordinal < 1) {
|
|
50
|
+
throw new TypeError("ordinal must be a positive safe integer");
|
|
51
|
+
}
|
|
52
|
+
return hashRecord("delivery-run", [lineageId, String(ordinal)]);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const snapshot12 = (snapshotHash) => {
|
|
56
|
+
if (!/^[0-9a-f]{64}$/u.test(snapshotHash)) {
|
|
57
|
+
throw new TypeError("snapshot hash must be 64 lowercase hexadecimal digits");
|
|
58
|
+
}
|
|
59
|
+
return snapshotHash.slice(0, 12);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const standaloneRunId = (lineageId, snapshotHash) =>
|
|
63
|
+
hashRecord("standalone-run", [lineageId, snapshot12(snapshotHash)]);
|
|
64
|
+
|
|
65
|
+
export const reviewScopeSlug = (lineageId) => {
|
|
66
|
+
if (!/^[0-9a-f]{64}$/u.test(lineageId)) {
|
|
67
|
+
throw new TypeError("lineage id must be 64 lowercase hexadecimal digits");
|
|
68
|
+
}
|
|
69
|
+
return `review-${lineageId.slice(0, 20)}`;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const deliverySnapshotHash = ({
|
|
73
|
+
locator,
|
|
74
|
+
actualBaseRef,
|
|
75
|
+
fixedPointSha,
|
|
76
|
+
headIdentity,
|
|
77
|
+
scopeEntries,
|
|
78
|
+
canonicalPatchHash,
|
|
79
|
+
}) =>
|
|
80
|
+
hashRecord("delivery-snapshot", [
|
|
81
|
+
locator,
|
|
82
|
+
actualBaseRef,
|
|
83
|
+
fixedPointSha,
|
|
84
|
+
headIdentity,
|
|
85
|
+
inclusiveScopeHash(scopeEntries),
|
|
86
|
+
canonicalPatchHash,
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const orderedFiles = (files) =>
|
|
90
|
+
[...files].sort((left, right) =>
|
|
91
|
+
Buffer.compare(
|
|
92
|
+
Buffer.from(left.identity, "utf8"),
|
|
93
|
+
Buffer.from(right.identity, "utf8"),
|
|
94
|
+
),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
export const orderedBundleContentHash = (files) =>
|
|
98
|
+
hashRecord(
|
|
99
|
+
"artifact-bundle",
|
|
100
|
+
orderedFiles(files).flatMap(({ identity, bytes }) => [
|
|
101
|
+
identity,
|
|
102
|
+
sha256Hex(bytes),
|
|
103
|
+
]),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
export const standaloneSnapshotHash = ({ locator, files, scopeEntries }) => {
|
|
107
|
+
const ordered = orderedFiles(files);
|
|
108
|
+
return hashRecord("standalone-snapshot", [
|
|
109
|
+
locator,
|
|
110
|
+
hashRecord(
|
|
111
|
+
"ordered-files",
|
|
112
|
+
ordered.map(({ identity }) => identity),
|
|
113
|
+
),
|
|
114
|
+
inclusiveScopeHash(scopeEntries),
|
|
115
|
+
orderedBundleContentHash(ordered),
|
|
116
|
+
"no-git-fixed-point",
|
|
117
|
+
]);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const sourceSetHash = (sources) =>
|
|
121
|
+
hashRecord(
|
|
122
|
+
"source-set",
|
|
123
|
+
[...sources]
|
|
124
|
+
.sort((left, right) =>
|
|
125
|
+
Buffer.compare(
|
|
126
|
+
Buffer.from(left.path, "utf8"),
|
|
127
|
+
Buffer.from(right.path, "utf8"),
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
.flatMap(({ path, hash }) => [path, hash]),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
export const reviewReportPath = ({ lineageId, reviewedAt, snapshotHash }) => {
|
|
134
|
+
if (!/^\d{8}T\d{6}Z$/u.test(reviewedAt)) {
|
|
135
|
+
throw new TypeError("reviewedAt must use YYYYMMDDTHHMMSSZ UTC format");
|
|
136
|
+
}
|
|
137
|
+
return `apps/wiki/content/docs/project/reviews/${reviewScopeSlug(lineageId)}-${reviewedAt}-${snapshot12(snapshotHash)}-review-report.md`;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const REPORT_BLOCK_OPEN = "```review-report-json\n";
|
|
141
|
+
const REPORT_BLOCK_CLOSE = "\n```";
|
|
142
|
+
const FRONTMATTER_KEYS = [
|
|
143
|
+
"title",
|
|
144
|
+
"domain",
|
|
145
|
+
"type",
|
|
146
|
+
"surface",
|
|
147
|
+
"permission",
|
|
148
|
+
"links",
|
|
149
|
+
"review_lineage_id",
|
|
150
|
+
"review_run_id",
|
|
151
|
+
"review_mode",
|
|
152
|
+
"reviewed_at",
|
|
153
|
+
"accepted_bounds_identity",
|
|
154
|
+
"accepted_bounds_hash",
|
|
155
|
+
"snapshot_hash",
|
|
156
|
+
"created",
|
|
157
|
+
"updated",
|
|
158
|
+
];
|
|
159
|
+
const HEX_64 = /^[0-9a-f]{64}$/u;
|
|
160
|
+
const COMMIT_SHA = /^[0-9a-f]{40}(?:[0-9a-f]{24})?$/u;
|
|
161
|
+
const LENS_KEYS = [
|
|
162
|
+
"standards",
|
|
163
|
+
"skill_adherence",
|
|
164
|
+
"architecture",
|
|
165
|
+
"simplify",
|
|
166
|
+
"spec",
|
|
167
|
+
];
|
|
168
|
+
|
|
169
|
+
const exactKeys = (value, keys) =>
|
|
170
|
+
value !== null &&
|
|
171
|
+
typeof value === "object" &&
|
|
172
|
+
!Array.isArray(value) &&
|
|
173
|
+
isDeepStrictEqual(Object.keys(value).sort(), [...keys].sort());
|
|
174
|
+
|
|
175
|
+
const nonemptyString = (value) =>
|
|
176
|
+
typeof value === "string" && value.length > 0;
|
|
177
|
+
|
|
178
|
+
const sortedUniqueStrings = (values) =>
|
|
179
|
+
Array.isArray(values) &&
|
|
180
|
+
values.every(nonemptyString) &&
|
|
181
|
+
new Set(values).size === values.length &&
|
|
182
|
+
isDeepStrictEqual(values, sortUtf8(values));
|
|
183
|
+
|
|
184
|
+
const reportDate = (reviewedAt) => {
|
|
185
|
+
if (!/^\d{8}T\d{6}Z$/u.test(reviewedAt)) {
|
|
186
|
+
throw new TypeError("reviewed_at must use YYYYMMDDTHHMMSSZ");
|
|
187
|
+
}
|
|
188
|
+
return `${reviewedAt.slice(0, 4)}-${reviewedAt.slice(4, 6)}-${reviewedAt.slice(6, 8)}`;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const frontmatterFor = (report, domain) => {
|
|
192
|
+
const date = reportDate(report.reviewed_at);
|
|
193
|
+
return {
|
|
194
|
+
title: `${reviewScopeSlug(report.review_lineage_id)} review report`,
|
|
195
|
+
domain,
|
|
196
|
+
type: "review-report",
|
|
197
|
+
surface: "project",
|
|
198
|
+
permission: "internal",
|
|
199
|
+
links: [],
|
|
200
|
+
review_lineage_id: report.review_lineage_id,
|
|
201
|
+
review_run_id: report.review_run_id,
|
|
202
|
+
review_mode: report.mode,
|
|
203
|
+
reviewed_at: report.reviewed_at,
|
|
204
|
+
accepted_bounds_identity: report.accepted_bounds_identity,
|
|
205
|
+
accepted_bounds_hash: report.accepted_bounds_hash,
|
|
206
|
+
snapshot_hash: report.snapshot_hash,
|
|
207
|
+
created: date,
|
|
208
|
+
updated: date,
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const encodeFrontmatter = (frontmatter) =>
|
|
213
|
+
[
|
|
214
|
+
"---",
|
|
215
|
+
...FRONTMATTER_KEYS.map((key) =>
|
|
216
|
+
`${key}: ${key === "links" ? "[]" : JSON.stringify(frontmatter[key])}`,
|
|
217
|
+
),
|
|
218
|
+
"---",
|
|
219
|
+
].join("\n");
|
|
220
|
+
|
|
221
|
+
export const encodeReviewReport = (
|
|
222
|
+
report,
|
|
223
|
+
{ domain, trailingProse = "" },
|
|
224
|
+
) => {
|
|
225
|
+
if (!report || typeof report !== "object" || Array.isArray(report)) {
|
|
226
|
+
throw new TypeError("report must be an object");
|
|
227
|
+
}
|
|
228
|
+
if (!nonemptyString(domain)) throw new TypeError("domain must be a string");
|
|
229
|
+
if (typeof trailingProse !== "string") {
|
|
230
|
+
throw new TypeError("trailing prose must be a string");
|
|
231
|
+
}
|
|
232
|
+
const suffix = trailingProse.length === 0 ? "" : `\n${trailingProse}`;
|
|
233
|
+
return Buffer.from(
|
|
234
|
+
`${encodeFrontmatter(frontmatterFor(report, domain))}\n\n${REPORT_BLOCK_OPEN}${JSON.stringify(report)}${REPORT_BLOCK_CLOSE}${suffix}`,
|
|
235
|
+
"utf8",
|
|
236
|
+
);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const parseFrontmatter = (text) => {
|
|
240
|
+
if (!text.startsWith("---\n")) throw new TypeError("missing frontmatter");
|
|
241
|
+
const close = text.indexOf("\n---\n", 4);
|
|
242
|
+
if (close < 0) throw new TypeError("unclosed frontmatter");
|
|
243
|
+
const frontmatter = {};
|
|
244
|
+
for (const line of text.slice(4, close).split("\n")) {
|
|
245
|
+
const separator = line.indexOf(": ");
|
|
246
|
+
if (separator < 1) throw new TypeError("malformed frontmatter line");
|
|
247
|
+
const key = line.slice(0, separator);
|
|
248
|
+
if (Object.hasOwn(frontmatter, key)) throw new TypeError("duplicate frontmatter key");
|
|
249
|
+
const raw = line.slice(separator + 2);
|
|
250
|
+
frontmatter[key] = raw === "[]" ? [] : JSON.parse(raw);
|
|
251
|
+
}
|
|
252
|
+
const remainder = text.slice(close + 5);
|
|
253
|
+
if (!remainder.startsWith(`\n${REPORT_BLOCK_OPEN}`)) {
|
|
254
|
+
throw new TypeError("authority block must immediately follow frontmatter");
|
|
255
|
+
}
|
|
256
|
+
return { frontmatter, authority: remainder.slice(1) };
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
export const parseReviewReport = (bytes) => {
|
|
260
|
+
if (!Buffer.isBuffer(bytes)) throw new TypeError("report bytes must be a Buffer");
|
|
261
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
262
|
+
const { frontmatter, authority } = parseFrontmatter(text);
|
|
263
|
+
const close = authority.indexOf(REPORT_BLOCK_CLOSE, REPORT_BLOCK_OPEN.length);
|
|
264
|
+
if (close < 0) throw new TypeError("review-report-json block is unclosed");
|
|
265
|
+
const trailing = authority.slice(close + REPORT_BLOCK_CLOSE.length);
|
|
266
|
+
if (trailing !== "" && !trailing.startsWith("\n")) {
|
|
267
|
+
throw new TypeError("report prose must follow the closed authority block");
|
|
268
|
+
}
|
|
269
|
+
if (trailing.includes("```review-report-json")) {
|
|
270
|
+
throw new TypeError("report contains duplicate review-report-json blocks");
|
|
271
|
+
}
|
|
272
|
+
if (trailing.includes("\n---\n") || trailing.startsWith("\n---\n")) {
|
|
273
|
+
throw new TypeError("report contains duplicate frontmatter");
|
|
274
|
+
}
|
|
275
|
+
const report = JSON.parse(
|
|
276
|
+
authority.slice(REPORT_BLOCK_OPEN.length, close),
|
|
277
|
+
);
|
|
278
|
+
if (!report || typeof report !== "object" || Array.isArray(report)) {
|
|
279
|
+
throw new TypeError("review-report-json must decode to one object");
|
|
280
|
+
}
|
|
281
|
+
return { frontmatter, report };
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export const normalizeReviewTarget = (mode, evidence) => {
|
|
285
|
+
if (mode === "delivery") {
|
|
286
|
+
if (
|
|
287
|
+
!nonemptyString(evidence.locator) ||
|
|
288
|
+
!nonemptyString(evidence.actualBaseRef) ||
|
|
289
|
+
!COMMIT_SHA.test(evidence.fixedPointSha) ||
|
|
290
|
+
!nonemptyString(evidence.headIdentity) ||
|
|
291
|
+
!sortedUniqueStrings(sortUtf8(evidence.scopeEntries)) ||
|
|
292
|
+
!Buffer.isBuffer(evidence.canonicalPatchBytes)
|
|
293
|
+
) {
|
|
294
|
+
throw new TypeError("invalid delivery target evidence");
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
kind: "delivery-git-diff",
|
|
298
|
+
locator: evidence.locator,
|
|
299
|
+
actual_base_ref: evidence.actualBaseRef,
|
|
300
|
+
fixed_point_sha: evidence.fixedPointSha,
|
|
301
|
+
head_identity: evidence.headIdentity,
|
|
302
|
+
inclusive_scope: sortUtf8(evidence.scopeEntries),
|
|
303
|
+
canonical_patch_hash: sha256Hex(evidence.canonicalPatchBytes),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
if (mode === "standalone") {
|
|
307
|
+
const files = orderedFiles(evidence.files);
|
|
308
|
+
const identities = files.map(({ identity }) => identity);
|
|
309
|
+
const scope = sortUtf8(evidence.scopeEntries);
|
|
310
|
+
if (
|
|
311
|
+
!nonemptyString(evidence.locator) ||
|
|
312
|
+
!sortedUniqueStrings(identities) ||
|
|
313
|
+
!files.every(({ bytes }) => Buffer.isBuffer(bytes)) ||
|
|
314
|
+
!isDeepStrictEqual(scope, identities)
|
|
315
|
+
) {
|
|
316
|
+
throw new TypeError("invalid standalone target evidence");
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
kind: "standalone-artifact",
|
|
320
|
+
locator: evidence.locator,
|
|
321
|
+
ordered_file_identities: identities,
|
|
322
|
+
inclusive_scope: scope,
|
|
323
|
+
ordered_bundle_content_hash: orderedBundleContentHash(files),
|
|
324
|
+
git_fixed_point: null,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
throw new TypeError("unsupported review mode");
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
export const normalizeReviewSources = (sources) => {
|
|
331
|
+
if (
|
|
332
|
+
!Array.isArray(sources) ||
|
|
333
|
+
sources.some(
|
|
334
|
+
({ path, bytes }) => !nonemptyString(path) || !Buffer.isBuffer(bytes),
|
|
335
|
+
) ||
|
|
336
|
+
new Set(sources.map(({ path }) => path)).size !== sources.length
|
|
337
|
+
) {
|
|
338
|
+
throw new TypeError("invalid source evidence");
|
|
339
|
+
}
|
|
340
|
+
return [...sources]
|
|
341
|
+
.sort((left, right) =>
|
|
342
|
+
Buffer.compare(
|
|
343
|
+
Buffer.from(left.path, "utf8"),
|
|
344
|
+
Buffer.from(right.path, "utf8"),
|
|
345
|
+
),
|
|
346
|
+
)
|
|
347
|
+
.map(({ path, bytes }) => ({ path, hash: sha256Hex(bytes) }));
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
export const planDeliveryReviewGate = ({
|
|
351
|
+
currentState,
|
|
352
|
+
acceptedBoundsValid,
|
|
353
|
+
targetSupported,
|
|
354
|
+
recoveredReviewCount,
|
|
355
|
+
}) => {
|
|
356
|
+
if (!acceptedBoundsValid || !targetSupported) {
|
|
357
|
+
return { state: "review_failed", handoffWrites: [] };
|
|
358
|
+
}
|
|
359
|
+
if (!Number.isSafeInteger(recoveredReviewCount) || recoveredReviewCount < 0) {
|
|
360
|
+
return { state: "review_failed", handoffWrites: [] };
|
|
361
|
+
}
|
|
362
|
+
if (recoveredReviewCount >= 3) {
|
|
363
|
+
return {
|
|
364
|
+
state: "review_budget_exhausted",
|
|
365
|
+
priorState: currentState,
|
|
366
|
+
handoffWrites: [],
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
return { state: "review_due", handoffWrites: ["review_due_context"] };
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
export const assessReadonlyValidation = ({ beforeHash, afterHash }) => {
|
|
373
|
+
if (!HEX_64.test(beforeHash) || !HEX_64.test(afterHash)) {
|
|
374
|
+
return { state: "review_failed", passConsumed: false };
|
|
375
|
+
}
|
|
376
|
+
if (beforeHash !== afterHash) {
|
|
377
|
+
return { state: "review_due", passConsumed: false };
|
|
378
|
+
}
|
|
379
|
+
return { state: "validation_accepted", passConsumed: false };
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
export const requiredReportFields = [
|
|
383
|
+
"review_lineage_id",
|
|
384
|
+
"review_run_id",
|
|
385
|
+
"accepted_bounds_identity",
|
|
386
|
+
"accepted_bounds_hash",
|
|
387
|
+
"reviewed_at",
|
|
388
|
+
"mode",
|
|
389
|
+
"normalized_target",
|
|
390
|
+
"snapshot_hash",
|
|
391
|
+
"excluded_envelope",
|
|
392
|
+
"source_paths_and_hashes",
|
|
393
|
+
"source_set_hash",
|
|
394
|
+
"lens_outcomes",
|
|
395
|
+
"findings",
|
|
396
|
+
"routing",
|
|
397
|
+
"validation",
|
|
398
|
+
"delivery_goal_identity",
|
|
399
|
+
"review_ordinal",
|
|
400
|
+
"preceding_repair_ordinal",
|
|
401
|
+
];
|
|
402
|
+
|
|
403
|
+
const validateTargetSchema = (target, mode, errors) => {
|
|
404
|
+
const deliveryKeys = [
|
|
405
|
+
"kind",
|
|
406
|
+
"locator",
|
|
407
|
+
"actual_base_ref",
|
|
408
|
+
"fixed_point_sha",
|
|
409
|
+
"head_identity",
|
|
410
|
+
"inclusive_scope",
|
|
411
|
+
"canonical_patch_hash",
|
|
412
|
+
];
|
|
413
|
+
const standaloneKeys = [
|
|
414
|
+
"kind",
|
|
415
|
+
"locator",
|
|
416
|
+
"ordered_file_identities",
|
|
417
|
+
"inclusive_scope",
|
|
418
|
+
"ordered_bundle_content_hash",
|
|
419
|
+
"git_fixed_point",
|
|
420
|
+
];
|
|
421
|
+
if (!exactKeys(target, mode === "delivery" ? deliveryKeys : standaloneKeys)) {
|
|
422
|
+
errors.push("malformed:normalized_target_schema");
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (!nonemptyString(target.locator) || !sortedUniqueStrings(target.inclusive_scope)) {
|
|
426
|
+
errors.push("malformed:normalized_target_common_fields");
|
|
427
|
+
}
|
|
428
|
+
if (mode === "delivery") {
|
|
429
|
+
if (
|
|
430
|
+
target.kind !== "delivery-git-diff" ||
|
|
431
|
+
!nonemptyString(target.actual_base_ref) ||
|
|
432
|
+
!COMMIT_SHA.test(target.fixed_point_sha) ||
|
|
433
|
+
!nonemptyString(target.head_identity) ||
|
|
434
|
+
!HEX_64.test(target.canonical_patch_hash)
|
|
435
|
+
) {
|
|
436
|
+
errors.push("malformed:delivery_target_fields");
|
|
437
|
+
}
|
|
438
|
+
} else if (
|
|
439
|
+
target.kind !== "standalone-artifact" ||
|
|
440
|
+
!sortedUniqueStrings(target.ordered_file_identities) ||
|
|
441
|
+
!HEX_64.test(target.ordered_bundle_content_hash) ||
|
|
442
|
+
target.git_fixed_point !== null ||
|
|
443
|
+
!isDeepStrictEqual(target.inclusive_scope, target.ordered_file_identities)
|
|
444
|
+
) {
|
|
445
|
+
errors.push("malformed:standalone_target_fields");
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
const validateReportSemantics = (report, errors) => {
|
|
450
|
+
if (!exactKeys(report, requiredReportFields)) {
|
|
451
|
+
errors.push("malformed:report_schema");
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (!exactKeys(report.lens_outcomes, LENS_KEYS)) {
|
|
455
|
+
errors.push("malformed:lens_keys");
|
|
456
|
+
} else {
|
|
457
|
+
for (const [lens, outcome] of Object.entries(report.lens_outcomes)) {
|
|
458
|
+
if (outcome !== "clean" && outcome !== "findings") {
|
|
459
|
+
errors.push(`malformed:lens_outcome:${lens}`);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (!Array.isArray(report.findings)) {
|
|
465
|
+
errors.push("malformed:findings");
|
|
466
|
+
} else {
|
|
467
|
+
const findingKeys = [
|
|
468
|
+
"id",
|
|
469
|
+
"lens",
|
|
470
|
+
"severity",
|
|
471
|
+
"location",
|
|
472
|
+
"impact",
|
|
473
|
+
"evidence",
|
|
474
|
+
"action",
|
|
475
|
+
];
|
|
476
|
+
const ids = new Set();
|
|
477
|
+
const findingLenses = new Set();
|
|
478
|
+
for (const finding of report.findings) {
|
|
479
|
+
if (
|
|
480
|
+
!exactKeys(finding, findingKeys) ||
|
|
481
|
+
!/^[a-z0-9][a-z0-9._-]*$/u.test(finding.id ?? "") ||
|
|
482
|
+
ids.has(finding.id) ||
|
|
483
|
+
!LENS_KEYS.includes(finding.lens) ||
|
|
484
|
+
!["critical", "high", "medium", "low"].includes(finding.severity) ||
|
|
485
|
+
![finding.location, finding.impact, finding.evidence, finding.action].every(
|
|
486
|
+
nonemptyString,
|
|
487
|
+
)
|
|
488
|
+
) {
|
|
489
|
+
errors.push("malformed:finding");
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
ids.add(finding.id);
|
|
493
|
+
findingLenses.add(finding.lens);
|
|
494
|
+
}
|
|
495
|
+
if (exactKeys(report.lens_outcomes, LENS_KEYS)) {
|
|
496
|
+
for (const lens of LENS_KEYS) {
|
|
497
|
+
const hasFindings = findingLenses.has(lens);
|
|
498
|
+
if ((report.lens_outcomes[lens] === "findings") !== hasFindings) {
|
|
499
|
+
errors.push(`mismatch:lens_findings:${lens}`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (
|
|
506
|
+
!exactKeys(report.routing, ["primary", "secondary_architecture_follow_up"]) ||
|
|
507
|
+
![
|
|
508
|
+
"debugging",
|
|
509
|
+
"implementation",
|
|
510
|
+
"debt_follow_up",
|
|
511
|
+
"docs_ingest",
|
|
512
|
+
"closeout",
|
|
513
|
+
].includes(report.routing?.primary) ||
|
|
514
|
+
typeof report.routing?.secondary_architecture_follow_up !== "boolean"
|
|
515
|
+
) {
|
|
516
|
+
errors.push("malformed:routing");
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const validationKeys = [
|
|
520
|
+
"command",
|
|
521
|
+
"isolation",
|
|
522
|
+
"before_hash",
|
|
523
|
+
"after_hash",
|
|
524
|
+
"outcome",
|
|
525
|
+
"evidence",
|
|
526
|
+
];
|
|
527
|
+
if (!Array.isArray(report.validation)) {
|
|
528
|
+
errors.push("malformed:validation");
|
|
529
|
+
} else {
|
|
530
|
+
for (const validation of report.validation) {
|
|
531
|
+
if (
|
|
532
|
+
!exactKeys(validation, validationKeys) ||
|
|
533
|
+
!nonemptyString(validation.command) ||
|
|
534
|
+
!["proven-no-write", "disposable-checkout", "disposable-snapshot"].includes(
|
|
535
|
+
validation.isolation,
|
|
536
|
+
) ||
|
|
537
|
+
!HEX_64.test(validation.before_hash ?? "") ||
|
|
538
|
+
validation.before_hash !== validation.after_hash ||
|
|
539
|
+
!["passed", "failed", "skipped"].includes(validation.outcome) ||
|
|
540
|
+
!nonemptyString(validation.evidence)
|
|
541
|
+
) {
|
|
542
|
+
errors.push("malformed:validation_record");
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
const validateFrontmatter = (
|
|
549
|
+
frontmatter,
|
|
550
|
+
report,
|
|
551
|
+
expected,
|
|
552
|
+
{ lineageId, runId, snapshotHash },
|
|
553
|
+
errors,
|
|
554
|
+
) => {
|
|
555
|
+
if (!exactKeys(frontmatter, FRONTMATTER_KEYS)) {
|
|
556
|
+
errors.push("malformed:frontmatter_schema");
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
let date;
|
|
560
|
+
try {
|
|
561
|
+
date = reportDate(report.reviewed_at);
|
|
562
|
+
} catch {
|
|
563
|
+
errors.push("malformed:frontmatter_reviewed_at");
|
|
564
|
+
}
|
|
565
|
+
const required = {
|
|
566
|
+
title: `${reviewScopeSlug(lineageId)} review report`,
|
|
567
|
+
domain: expected.wikiDomain,
|
|
568
|
+
type: "review-report",
|
|
569
|
+
surface: "project",
|
|
570
|
+
permission: "internal",
|
|
571
|
+
links: [],
|
|
572
|
+
review_lineage_id: lineageId,
|
|
573
|
+
review_run_id: runId,
|
|
574
|
+
review_mode: report.mode,
|
|
575
|
+
reviewed_at: report.reviewed_at,
|
|
576
|
+
accepted_bounds_identity: report.accepted_bounds_identity,
|
|
577
|
+
accepted_bounds_hash: report.accepted_bounds_hash,
|
|
578
|
+
snapshot_hash: snapshotHash,
|
|
579
|
+
created: date,
|
|
580
|
+
updated: date,
|
|
581
|
+
};
|
|
582
|
+
for (const key of FRONTMATTER_KEYS) {
|
|
583
|
+
if (!isDeepStrictEqual(frontmatter[key], required[key])) {
|
|
584
|
+
errors.push(`mismatch:frontmatter:${key}`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
const inspectRetainedPass = (candidate, expected) => {
|
|
590
|
+
const errors = [];
|
|
591
|
+
if (Object.hasOwn(candidate, "report")) {
|
|
592
|
+
errors.push("unexpected:detached_report_sidecar");
|
|
593
|
+
}
|
|
594
|
+
let frontmatter;
|
|
595
|
+
let report;
|
|
596
|
+
try {
|
|
597
|
+
({ frontmatter, report } = parseReviewReport(candidate.reportBytes));
|
|
598
|
+
} catch {
|
|
599
|
+
return { valid: false, errors: [...errors, "malformed_report_blob"] };
|
|
600
|
+
}
|
|
601
|
+
validateReportSemantics(report, errors);
|
|
602
|
+
validateTargetSchema(report.normalized_target, expected.mode, errors);
|
|
603
|
+
|
|
604
|
+
let currentTarget;
|
|
605
|
+
let currentSources;
|
|
606
|
+
try {
|
|
607
|
+
currentTarget = normalizeReviewTarget(expected.mode, expected.targetEvidence);
|
|
608
|
+
currentSources = normalizeReviewSources(expected.sourceEvidence);
|
|
609
|
+
} catch {
|
|
610
|
+
return { valid: false, errors: [...errors, "malformed_current_evidence"] };
|
|
611
|
+
}
|
|
612
|
+
const scopeEntries = currentTarget.inclusive_scope;
|
|
613
|
+
const boundsHash = acceptedBoundsHash(
|
|
614
|
+
expected.acceptedBoundsIdentity,
|
|
615
|
+
scopeEntries,
|
|
616
|
+
);
|
|
617
|
+
const sourceHash = sourceSetHash(currentSources);
|
|
618
|
+
let lineageId;
|
|
619
|
+
try {
|
|
620
|
+
lineageId =
|
|
621
|
+
expected.mode === "delivery"
|
|
622
|
+
? deliveryLineageId(expected.deliveryGoalIdentity)
|
|
623
|
+
: standaloneLineageId(currentTarget.locator, boundsHash);
|
|
624
|
+
} catch {
|
|
625
|
+
errors.push("invalid:delivery_goal_identity");
|
|
626
|
+
}
|
|
627
|
+
const snapshotHash =
|
|
628
|
+
expected.mode === "delivery"
|
|
629
|
+
? deliverySnapshotHash({
|
|
630
|
+
locator: currentTarget.locator,
|
|
631
|
+
actualBaseRef: currentTarget.actual_base_ref,
|
|
632
|
+
fixedPointSha: currentTarget.fixed_point_sha,
|
|
633
|
+
headIdentity: currentTarget.head_identity,
|
|
634
|
+
scopeEntries,
|
|
635
|
+
canonicalPatchHash: currentTarget.canonical_patch_hash,
|
|
636
|
+
})
|
|
637
|
+
: hashRecord("standalone-snapshot", [
|
|
638
|
+
currentTarget.locator,
|
|
639
|
+
hashRecord("ordered-files", currentTarget.ordered_file_identities),
|
|
640
|
+
inclusiveScopeHash(scopeEntries),
|
|
641
|
+
currentTarget.ordered_bundle_content_hash,
|
|
642
|
+
"no-git-fixed-point",
|
|
643
|
+
]);
|
|
644
|
+
const ordinal =
|
|
645
|
+
expected.mode === "delivery" ? expected.reviewOrdinal : null;
|
|
646
|
+
let runId;
|
|
647
|
+
try {
|
|
648
|
+
runId =
|
|
649
|
+
expected.mode === "delivery"
|
|
650
|
+
? deliveryRunId(lineageId, ordinal)
|
|
651
|
+
: standaloneRunId(lineageId, snapshotHash);
|
|
652
|
+
} catch {
|
|
653
|
+
errors.push("invalid:review_ordinal");
|
|
654
|
+
}
|
|
655
|
+
let reportPath;
|
|
656
|
+
try {
|
|
657
|
+
reportPath = reviewReportPath({
|
|
658
|
+
lineageId,
|
|
659
|
+
reviewedAt: report.reviewed_at,
|
|
660
|
+
snapshotHash,
|
|
661
|
+
});
|
|
662
|
+
} catch {
|
|
663
|
+
errors.push("invalid:reviewed_at");
|
|
664
|
+
}
|
|
665
|
+
try {
|
|
666
|
+
validateFrontmatter(
|
|
667
|
+
frontmatter,
|
|
668
|
+
report,
|
|
669
|
+
expected,
|
|
670
|
+
{ lineageId, runId, snapshotHash },
|
|
671
|
+
errors,
|
|
672
|
+
);
|
|
673
|
+
} catch {
|
|
674
|
+
errors.push("malformed:frontmatter_values");
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const exactFields = [
|
|
678
|
+
["mode", expected.mode],
|
|
679
|
+
["review_lineage_id", lineageId],
|
|
680
|
+
["review_run_id", runId],
|
|
681
|
+
["accepted_bounds_identity", expected.acceptedBoundsIdentity],
|
|
682
|
+
["accepted_bounds_hash", boundsHash],
|
|
683
|
+
["snapshot_hash", snapshotHash],
|
|
684
|
+
["source_set_hash", sourceHash],
|
|
685
|
+
];
|
|
686
|
+
for (const [field, value] of exactFields) {
|
|
687
|
+
if (report[field] !== value) errors.push(`mismatch:${field}`);
|
|
688
|
+
}
|
|
689
|
+
if (!isDeepStrictEqual(report.normalized_target, currentTarget)) {
|
|
690
|
+
errors.push("mismatch:normalized_target");
|
|
691
|
+
}
|
|
692
|
+
const envelopePaths = reportPath
|
|
693
|
+
? [reportPath, ...expected.auxiliaryEnvelopePaths]
|
|
694
|
+
: expected.auxiliaryEnvelopePaths;
|
|
695
|
+
if (!isDeepStrictEqual(report.excluded_envelope, envelopePaths)) {
|
|
696
|
+
errors.push("mismatch:excluded_envelope");
|
|
697
|
+
}
|
|
698
|
+
if (!isDeepStrictEqual(report.source_paths_and_hashes, currentSources)) {
|
|
699
|
+
errors.push("mismatch:source_paths_and_hashes");
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (candidate.reportPath !== reportPath) {
|
|
703
|
+
errors.push("mismatch:report_path");
|
|
704
|
+
}
|
|
705
|
+
if (
|
|
706
|
+
!Buffer.isBuffer(candidate.reportBytes) ||
|
|
707
|
+
candidate.reportSha256 !== sha256Hex(candidate.reportBytes)
|
|
708
|
+
) {
|
|
709
|
+
errors.push("mismatch:report_sha256");
|
|
710
|
+
}
|
|
711
|
+
if (!candidate.refContainsCommit) errors.push("ref_missing_commit");
|
|
712
|
+
if (!COMMIT_SHA.test(candidate.reportCommitSha)) {
|
|
713
|
+
errors.push("invalid:report_commit_sha");
|
|
714
|
+
}
|
|
715
|
+
if (!expected.approvedRetainedRefs.includes(candidate.retainedRef)) {
|
|
716
|
+
errors.push("unapproved:retained_ref");
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const changedPaths = new Set(
|
|
720
|
+
Array.isArray(candidate.commitPaths) ? candidate.commitPaths : [],
|
|
721
|
+
);
|
|
722
|
+
const allowedPaths = new Set(envelopePaths);
|
|
723
|
+
if (!changedPaths.has(reportPath)) errors.push("commit_missing_report");
|
|
724
|
+
for (const path of changedPaths) {
|
|
725
|
+
if (!allowedPaths.has(path)) errors.push(`commit_path_outside_envelope:${path}`);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (expected.mode === "delivery") {
|
|
729
|
+
if (
|
|
730
|
+
report.delivery_goal_identity !== expected.deliveryGoalIdentity ||
|
|
731
|
+
report.review_ordinal !== ordinal ||
|
|
732
|
+
!Number.isSafeInteger(ordinal) ||
|
|
733
|
+
ordinal < 1 ||
|
|
734
|
+
ordinal > 3 ||
|
|
735
|
+
report.preceding_repair_ordinal !== (ordinal === 1 ? null : ordinal - 1)
|
|
736
|
+
) {
|
|
737
|
+
errors.push("invalid:delivery_identity_or_ordinal");
|
|
738
|
+
}
|
|
739
|
+
} else if (
|
|
740
|
+
report.delivery_goal_identity !== null ||
|
|
741
|
+
report.review_ordinal !== null ||
|
|
742
|
+
report.preceding_repair_ordinal !== null
|
|
743
|
+
) {
|
|
744
|
+
errors.push("invalid:standalone_delivery_fields");
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
return {
|
|
748
|
+
valid: errors.length === 0,
|
|
749
|
+
errors,
|
|
750
|
+
derived: { lineageId, runId, ordinal, reportPath },
|
|
751
|
+
};
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
export const validateRetainedPass = (candidate, expected) => {
|
|
755
|
+
const { valid, errors } = inspectRetainedPass(candidate, expected);
|
|
756
|
+
return { valid, errors };
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
const authoritySignature = (candidate) =>
|
|
760
|
+
[
|
|
761
|
+
candidate.reportPath,
|
|
762
|
+
candidate.reportSha256,
|
|
763
|
+
candidate.reportCommitSha,
|
|
764
|
+
].join("\0");
|
|
765
|
+
|
|
766
|
+
export const resolveRetainedRun = (entries) => {
|
|
767
|
+
const valid = entries
|
|
768
|
+
.map((entry) => ({ entry, inspection: inspectRetainedPass(entry.candidate, entry.expected) }))
|
|
769
|
+
.filter(({ inspection }) => inspection.valid);
|
|
770
|
+
if (valid.length === 0) return { status: "none", pass: null };
|
|
771
|
+
|
|
772
|
+
const runKeys = new Set(
|
|
773
|
+
valid.map(({ inspection }) =>
|
|
774
|
+
[inspection.derived.lineageId, inspection.derived.runId].join("\0"),
|
|
775
|
+
),
|
|
776
|
+
);
|
|
777
|
+
if (runKeys.size !== 1) return { status: "mixed_runs", pass: null };
|
|
778
|
+
|
|
779
|
+
const first = authoritySignature(valid[0].entry.candidate);
|
|
780
|
+
if (valid.every(({ entry }) => authoritySignature(entry.candidate) === first)) {
|
|
781
|
+
return {
|
|
782
|
+
status: valid.length === 1 ? "unique" : "reuse",
|
|
783
|
+
pass: valid[0].entry,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
return { status: "conflict", pass: null };
|
|
787
|
+
};
|
|
788
|
+
|
|
789
|
+
export const recoverDeliveryCount = (entries, lineageId) => {
|
|
790
|
+
const matching = entries
|
|
791
|
+
.map((entry) => ({ entry, inspection: inspectRetainedPass(entry.candidate, entry.expected) }))
|
|
792
|
+
.filter(
|
|
793
|
+
({ entry, inspection }) =>
|
|
794
|
+
entry.expected.mode === "delivery" &&
|
|
795
|
+
inspection.valid &&
|
|
796
|
+
inspection.derived.lineageId === lineageId,
|
|
797
|
+
);
|
|
798
|
+
const groups = new Map();
|
|
799
|
+
for (const { entry, inspection } of matching) {
|
|
800
|
+
const group = groups.get(inspection.derived.runId) ?? [];
|
|
801
|
+
group.push(entry);
|
|
802
|
+
groups.set(inspection.derived.runId, group);
|
|
803
|
+
}
|
|
804
|
+
let reviewCount = 0;
|
|
805
|
+
for (const group of groups.values()) {
|
|
806
|
+
const resolved = resolveRetainedRun(group);
|
|
807
|
+
if (resolved.status === "conflict") {
|
|
808
|
+
return { status: "conflict", reviewCount: null };
|
|
809
|
+
}
|
|
810
|
+
if (resolved.pass) {
|
|
811
|
+
const inspected = inspectRetainedPass(
|
|
812
|
+
resolved.pass.candidate,
|
|
813
|
+
resolved.pass.expected,
|
|
814
|
+
);
|
|
815
|
+
reviewCount = Math.max(reviewCount, inspected.derived.ordinal);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
return { status: "ok", reviewCount };
|
|
819
|
+
};
|