@kungfu-tech/buildchain 4.0.2-alpha.0 → 4.0.2-alpha.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.
Files changed (82) hide show
  1. package/architecture/agent-change-map.md +52 -5
  2. package/architecture/ci-lane-change-budget.json +408 -8
  3. package/architecture/internal-capabilities.json +44 -1
  4. package/architecture/maintainability-debt.json +65 -133
  5. package/architecture/maintainability-policy.json +20 -15
  6. package/architecture/release-tail-contract-inventory.json +4 -4
  7. package/architecture/v3-core-mechanism-inventory.json +1 -0
  8. package/architecture/v3-v4-live-capability-inventory.json +17 -17
  9. package/architecture/v4-delivery-warrant-shadow-fixtures.json +5 -5
  10. package/architecture/v4-release-invocation-fixtures.json +139 -0
  11. package/architecture/v4-release-topology.json +317 -0
  12. package/architecture/v4-runtime-semantic-closure.json +4 -1
  13. package/architecture/v4-universal-workflow-bootstrap.json +181 -0
  14. package/architecture/v4-universal-workflow-fault-campaign.json +72 -0
  15. package/architecture/v4-universal-workflow-train-admission.json +40 -0
  16. package/contracts/buildchain-v2-residuals-v1.json +0 -9
  17. package/contracts/fixtures/v4-tail-reseal-v1/valid.json +1 -1
  18. package/contracts/v4-release-invocation-v1.schema.json +134 -0
  19. package/dist/site/buildchain-contract.json +87 -42
  20. package/dist/site/buildchain-site.json +15 -10
  21. package/dist/site/capability-registry.json +1 -1
  22. package/dist/site/controller-registry.json +46 -10
  23. package/dist/site/kfd-claims.json +176 -69
  24. package/dist/site/kfd-upstream-aggregate.json +1 -1
  25. package/dist/site/manual-registry.json +2 -2
  26. package/dist/site/node-api-registry.json +97 -22
  27. package/dist/site/page-registry.json +9 -4
  28. package/dist/site/public-surface-audit.json +190 -71
  29. package/dist/site/publication-authority-registry.json +68 -3
  30. package/dist/site/publication-registry.json +4 -4
  31. package/dist/site/site-manifest.json +6 -6
  32. package/dist/site/workflow-registry.json +221 -72
  33. package/docs/node-api-reference.md +28 -25
  34. package/docs/reusable-build-surface.md +16 -0
  35. package/package.json +2 -2
  36. package/packages/core/buildchain-publication-authority.js +3 -0
  37. package/packages/core/ci-lane-change-budget.js +14 -1
  38. package/packages/core/dev-delivery-candidate-identity.js +45 -17
  39. package/packages/core/dev-delivery-execution-transfer.js +6 -7
  40. package/packages/core/dev-delivery-provider-heartbeat.js +22 -6
  41. package/packages/core/dev-delivery-warrant-legacy-recovery.js +4 -2
  42. package/packages/core/dev-delivery-warrant-native-compatibility.js +33 -0
  43. package/packages/core/dev-delivery-warrant-state.js +54 -54
  44. package/packages/core/dev-delivery-warrant.js +8 -0
  45. package/packages/core/dev-delivery-writer-protocol-transition.js +72 -0
  46. package/packages/core/release-tail-product-capabilities.js +29 -0
  47. package/packages/core/release-tail-provider-plane.js +3 -3
  48. package/packages/core/v4-canonical-contracts.js +14 -0
  49. package/packages/core/v4-floating-consumer-policy.js +4 -1
  50. package/packages/core/v4-product-publication.js +387 -0
  51. package/packages/core/v4-protected-publication-source.js +93 -0
  52. package/packages/core/v4-publication-qualification.js +1 -0
  53. package/packages/core/v4-release-invocation.js +425 -0
  54. package/packages/core/v4-universal-workflow-bootstrap.js +596 -0
  55. package/scripts/audit-publication-control-plane.mjs +0 -1
  56. package/scripts/check-inventory.mjs +27 -59
  57. package/scripts/check-maintainability.mjs +29 -118
  58. package/scripts/check-v3-v4-capability-inventory.mjs +3 -61
  59. package/scripts/check-v4-floating-consumer-policy-contract.mjs +10 -20
  60. package/scripts/check-v4-release-topology.mjs +493 -0
  61. package/scripts/check-v4-universal-workflow-bootstrap.mjs +257 -0
  62. package/scripts/dev-delivery-warrant.mjs +26 -5
  63. package/scripts/generate-channel-build-workflow.mjs +5 -1
  64. package/scripts/generate-channel-promotion-workflow.mjs +17 -57
  65. package/scripts/generate-v4-universal-workflow-facades.mjs +415 -0
  66. package/scripts/maintainability-public-surface.mjs +115 -0
  67. package/scripts/release-candidate-resolver.mjs +23 -24
  68. package/scripts/resume-from-candidate-run.mjs +16 -17
  69. package/scripts/universal-facade-maintainability.mjs +82 -0
  70. package/scripts/v3-v4-capability-catalog.mjs +107 -0
  71. package/scripts/v4-declarative-promotion-admission.mjs +4 -1
  72. package/scripts/v4-product-publication-intent.mjs +114 -0
  73. package/scripts/v4-release-candidate-adapter.mjs +41 -0
  74. package/scripts/v4-universal-workflow-backflow.mjs +212 -0
  75. package/scripts/v4-universal-workflow-engine.mjs +586 -0
  76. package/scripts/v4-universal-workflow-self-dogfood.mjs +205 -0
  77. package/templates/universal-buildchain-bootstrap-recovery.yml +282 -0
  78. package/templates/universal-buildchain-bootstrap.yml +28 -0
  79. package/scripts/capture-package-release-propagation.mjs +0 -263
  80. package/scripts/publication-commit-evidence.mjs +0 -444
  81. package/scripts/publish-github-artifact-attestation-evidence.mjs +0 -201
  82. package/scripts/stage-github-artifact-attestation-inputs.mjs +0 -65
@@ -0,0 +1,596 @@
1
+ import crypto from "node:crypto";
2
+
3
+ export const V4_UNIVERSAL_WORKFLOW_REQUEST =
4
+ "kungfu-buildchain-v4-universal-workflow-request/v1";
5
+ export const V4_UNIVERSAL_WORKFLOW_ADMISSION_POLICY =
6
+ "kungfu-buildchain-v4-universal-workflow-admission-policy/v1";
7
+ export const V4_UNIVERSAL_WORKFLOW_ADMISSION =
8
+ "kungfu-buildchain-v4-universal-workflow-admission/v1";
9
+ export const V4_UNIVERSAL_WORKFLOW_TERMINAL_RECEIPT =
10
+ "kungfu-buildchain-v4-universal-workflow-terminal-receipt/v1";
11
+
12
+ const EXACT_SHA = /^[0-9a-f]{40}$/u;
13
+ const ROOT = /^sha256:[0-9a-f]{64}$/u;
14
+ const REPOSITORY = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u;
15
+ const WORKFLOW = /^\.github\/workflows\/[A-Za-z0-9_.-]+\.ya?ml$/u;
16
+ const TOKEN = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/u;
17
+ const TRAIN = /^train\/v4\/v4\.\d+\/[A-Za-z0-9][A-Za-z0-9._/-]*$/u;
18
+ const MODES = new Set(["stable", "alpha", "train", "exact"]);
19
+ const PERMISSIONS = new Set([
20
+ "actions",
21
+ "artifact-metadata",
22
+ "attestations",
23
+ "checks",
24
+ "contents",
25
+ "deployments",
26
+ "id-token",
27
+ "issues",
28
+ "packages",
29
+ "pages",
30
+ "pull-requests",
31
+ "security-events",
32
+ "statuses",
33
+ ]);
34
+ const LEVELS = new Map([
35
+ ["none", 0],
36
+ ["read", 1],
37
+ ["write", 2],
38
+ ]);
39
+
40
+ function fail(code, message) {
41
+ const error = new Error(message);
42
+ error.code = code;
43
+ throw error;
44
+ }
45
+ function exactKeys(value, keys, label) {
46
+ if (!value || typeof value !== "object" || Array.isArray(value))
47
+ fail("invalid-object", `${label} must be an object`);
48
+ const actual = Object.keys(value).sort();
49
+ const expected = [...keys].sort();
50
+ if (
51
+ actual.length !== expected.length ||
52
+ actual.some((key, index) => key !== expected[index])
53
+ )
54
+ fail("invalid-field-set", `${label} has an invalid field set`);
55
+ }
56
+ function nonEmpty(value, label) {
57
+ const normalized = String(value || "").trim();
58
+ if (!normalized) fail("required-value", `${label} must be non-empty`);
59
+ return normalized;
60
+ }
61
+ function exactSha(value, label) {
62
+ const normalized = nonEmpty(value, label).toLowerCase();
63
+ if (!EXACT_SHA.test(normalized))
64
+ fail("invalid-exact-sha", `${label} must be an exact lowercase Git SHA`);
65
+ return normalized;
66
+ }
67
+ function positiveInteger(value, label) {
68
+ if (!Number.isSafeInteger(value) || value < 1)
69
+ fail("invalid-positive-integer", `${label} must be a positive integer`);
70
+ return value;
71
+ }
72
+ function root(value, label) {
73
+ const normalized = nonEmpty(value, label).toLowerCase();
74
+ if (!ROOT.test(normalized))
75
+ fail("invalid-root", `${label} must be a lowercase sha256 root`);
76
+ return normalized;
77
+ }
78
+ function canonical(value) {
79
+ if (Array.isArray(value)) return value.map(canonical);
80
+ if (value && typeof value === "object")
81
+ return Object.fromEntries(
82
+ Object.keys(value)
83
+ .sort((left, right) => Buffer.from(left).compare(Buffer.from(right)))
84
+ .map((key) => [key, canonical(value[key])]),
85
+ );
86
+ if (
87
+ value === null ||
88
+ typeof value === "string" ||
89
+ typeof value === "boolean" ||
90
+ (typeof value === "number" && Number.isSafeInteger(value))
91
+ )
92
+ return value;
93
+ fail("unsupported-json", "value is not canonical JSON");
94
+ }
95
+ function documentRoot(domain, value) {
96
+ const hash = crypto.createHash("sha256");
97
+ hash.update(domain, "utf8");
98
+ hash.update(Buffer.from([0]));
99
+ hash.update(`${JSON.stringify(canonical(value))}\n`, "utf8");
100
+ return `sha256:${hash.digest("hex")}`;
101
+ }
102
+ function sortedValues(values, label, normalize, kind) {
103
+ if (!Array.isArray(values) || values.length === 0)
104
+ fail(`required-${kind}`, `${label} must be a non-empty array`);
105
+ const normalized = values.map((value, index) =>
106
+ normalize(value, `${label}[${index}]`),
107
+ );
108
+ const expected = [...new Set(normalized)].sort((left, right) =>
109
+ Buffer.from(left).compare(Buffer.from(right)),
110
+ );
111
+ if (
112
+ normalized.length !== expected.length ||
113
+ normalized.some((value, index) => value !== expected[index])
114
+ )
115
+ fail(
116
+ `non-canonical-${kind}`,
117
+ `${label} must be byte-sorted and duplicate-free`,
118
+ );
119
+ return normalized;
120
+ }
121
+ function token(value, label) {
122
+ const token = nonEmpty(value, label);
123
+ if (!TOKEN.test(token))
124
+ fail("invalid-token", `${label} must be an ASCII token`);
125
+ return token;
126
+ }
127
+ const sortedRoots = (values, label) =>
128
+ sortedValues(values, label, root, "roots");
129
+ const sortedTokens = (values, label) =>
130
+ sortedValues(values, label, token, "tokens");
131
+ const sortedStrings = (values, label) =>
132
+ sortedValues(values, label, nonEmpty, "strings");
133
+ function permissions(value, label) {
134
+ if (!value || typeof value !== "object" || Array.isArray(value))
135
+ fail("invalid-permissions", `${label} must be an object`);
136
+ const result = {};
137
+ for (const key of Object.keys(value).sort()) {
138
+ if (!PERMISSIONS.has(key))
139
+ fail("unknown-permission", `${label}.${key} is not governed`);
140
+ const level = nonEmpty(value[key], `${label}.${key}`);
141
+ if (!LEVELS.has(level))
142
+ fail("invalid-permission-level", `${label}.${key} has an invalid level`);
143
+ result[key] = level;
144
+ }
145
+ if (Object.keys(result).length === 0)
146
+ fail("required-permissions", `${label} must not be empty`);
147
+ return result;
148
+ }
149
+ function repository(value, label) {
150
+ const normalized = nonEmpty(value, label);
151
+ if (!REPOSITORY.test(normalized))
152
+ fail("invalid-repository", `${label} must be an owner/repository slug`);
153
+ return normalized;
154
+ }
155
+
156
+ function workflow(value, label) {
157
+ const normalized = nonEmpty(value, label);
158
+ if (!WORKFLOW.test(normalized))
159
+ fail("invalid-workflow", `${label} must be a tracked workflow path`);
160
+ return normalized;
161
+ }
162
+
163
+ function timestamp(value, label) {
164
+ const normalized = nonEmpty(value, label);
165
+ const parsed = new Date(normalized);
166
+ if (!Number.isFinite(parsed.getTime()) || parsed.toISOString() !== normalized)
167
+ fail("invalid-timestamp", `${label} must be a canonical UTC timestamp`);
168
+ return normalized;
169
+ }
170
+
171
+ export function validateV4UniversalWorkflowRequest(value) {
172
+ exactKeys(
173
+ value,
174
+ ["schema", "mode", "candidate", "consumer", "capability", "payload"],
175
+ "request",
176
+ );
177
+ if (value.schema !== V4_UNIVERSAL_WORKFLOW_REQUEST)
178
+ fail("unsupported-request-schema", "request schema is unsupported");
179
+ if (!MODES.has(value.mode))
180
+ fail("unsupported-mode", "request mode is unsupported");
181
+ exactKeys(
182
+ value.candidate,
183
+ [
184
+ "repository",
185
+ "discoveryRef",
186
+ "expectedSha",
187
+ "admissionRoot",
188
+ "reviewPullRequest",
189
+ ],
190
+ "request.candidate",
191
+ );
192
+ const candidateRepository = repository(
193
+ value.candidate.repository,
194
+ "request.candidate.repository",
195
+ );
196
+ if (candidateRepository !== "kungfu-systems/buildchain")
197
+ fail(
198
+ "untrusted-candidate-repository",
199
+ "candidate repository must be kungfu-systems/buildchain",
200
+ );
201
+ const discoveryRef = nonEmpty(
202
+ value.candidate.discoveryRef,
203
+ "request.candidate.discoveryRef",
204
+ ).replace(/^refs\/(?:heads|tags)\//u, "");
205
+ if (value.mode === "train" && !TRAIN.test(discoveryRef))
206
+ fail("invalid-train-ref", "train mode requires a governed v4 Train ref");
207
+ if (value.mode === "exact" && !EXACT_SHA.test(discoveryRef.toLowerCase()))
208
+ fail("invalid-exact-ref", "exact mode requires an exact Git SHA selector");
209
+ if (value.mode === "alpha" && discoveryRef !== "v4-alpha")
210
+ fail("invalid-alpha-ref", "alpha mode requires v4-alpha discovery");
211
+ if (value.mode === "stable" && discoveryRef !== "v4")
212
+ fail("invalid-stable-ref", "stable mode requires v4 discovery");
213
+ exactKeys(
214
+ value.consumer,
215
+ ["repository", "workflow", "sourceSha"],
216
+ "request.consumer",
217
+ );
218
+ exactKeys(
219
+ value.capability,
220
+ ["id", "contractRoots", "permissions"],
221
+ "request.capability",
222
+ );
223
+ const capabilityId = nonEmpty(value.capability.id, "request.capability.id");
224
+ if (!TOKEN.test(capabilityId))
225
+ fail("invalid-capability", "request.capability.id must be an ASCII token");
226
+ canonical(value.payload);
227
+ return {
228
+ schema: value.schema,
229
+ mode: value.mode,
230
+ candidate: {
231
+ repository: candidateRepository,
232
+ discoveryRef,
233
+ expectedSha: exactSha(
234
+ value.candidate.expectedSha,
235
+ "request.candidate.expectedSha",
236
+ ),
237
+ admissionRoot: root(
238
+ value.candidate.admissionRoot,
239
+ "request.candidate.admissionRoot",
240
+ ),
241
+ reviewPullRequest: positiveInteger(
242
+ value.candidate.reviewPullRequest,
243
+ "request.candidate.reviewPullRequest",
244
+ ),
245
+ },
246
+ consumer: {
247
+ repository: repository(
248
+ value.consumer.repository,
249
+ "request.consumer.repository",
250
+ ),
251
+ workflow: workflow(value.consumer.workflow, "request.consumer.workflow"),
252
+ sourceSha: exactSha(
253
+ value.consumer.sourceSha,
254
+ "request.consumer.sourceSha",
255
+ ),
256
+ },
257
+ capability: {
258
+ id: capabilityId,
259
+ contractRoots: sortedRoots(
260
+ value.capability.contractRoots,
261
+ "request.capability.contractRoots",
262
+ ),
263
+ permissions: permissions(
264
+ value.capability.permissions,
265
+ "request.capability.permissions",
266
+ ),
267
+ },
268
+ payload: canonical(value.payload),
269
+ };
270
+ }
271
+
272
+ export function v4UniversalWorkflowRequestRoot(value) {
273
+ return documentRoot(
274
+ "universal-workflow-request",
275
+ validateV4UniversalWorkflowRequest(value),
276
+ );
277
+ }
278
+
279
+ function validatePolicy(value) {
280
+ exactKeys(
281
+ value,
282
+ [
283
+ "schema",
284
+ "sourceRepository",
285
+ "consumerAdmission",
286
+ "allowedCapabilities",
287
+ "permissionCeiling",
288
+ "contractRoots",
289
+ "targetRef",
290
+ "allowedReviewers",
291
+ "minimumApprovals",
292
+ "requiredChecks",
293
+ "validFrom",
294
+ "expiresAt",
295
+ ],
296
+ "policy",
297
+ );
298
+ if (value.schema !== V4_UNIVERSAL_WORKFLOW_ADMISSION_POLICY)
299
+ fail("unsupported-policy-schema", "policy schema is unsupported");
300
+ const consumerAdmission = nonEmpty(
301
+ value.consumerAdmission,
302
+ "consumer admission",
303
+ );
304
+ if (consumerAdmission !== "verified-caller")
305
+ fail("unsupported-consumer-admission", "unsupported admission");
306
+ const policy = {
307
+ schema: value.schema,
308
+ sourceRepository: repository(
309
+ value.sourceRepository,
310
+ "policy.sourceRepository",
311
+ ),
312
+ consumerAdmission,
313
+ allowedCapabilities: sortedTokens(
314
+ value.allowedCapabilities,
315
+ "policy.allowedCapabilities",
316
+ ),
317
+ permissionCeiling: permissions(
318
+ value.permissionCeiling,
319
+ "policy.permissionCeiling",
320
+ ),
321
+ contractRoots: sortedRoots(value.contractRoots, "policy.contractRoots"),
322
+ targetRef: nonEmpty(value.targetRef, "policy.targetRef"),
323
+ allowedReviewers: sortedTokens(
324
+ value.allowedReviewers,
325
+ "policy.allowedReviewers",
326
+ ),
327
+ minimumApprovals: positiveInteger(
328
+ value.minimumApprovals,
329
+ "policy.minimumApprovals",
330
+ ),
331
+ requiredChecks: sortedStrings(
332
+ value.requiredChecks,
333
+ "policy.requiredChecks",
334
+ ),
335
+ validFrom: timestamp(value.validFrom, "policy.validFrom"),
336
+ expiresAt: timestamp(value.expiresAt, "policy.expiresAt"),
337
+ };
338
+ if (Date.parse(policy.validFrom) >= Date.parse(policy.expiresAt))
339
+ fail("invalid-policy-window", "policy validity window is empty");
340
+ return policy;
341
+ }
342
+
343
+ function validateReviewEvidence(value) {
344
+ exactKeys(
345
+ value,
346
+ [
347
+ "repository",
348
+ "pullRequest",
349
+ "headSha",
350
+ "baseRef",
351
+ "approvals",
352
+ "checks",
353
+ "observedAt",
354
+ ],
355
+ "reviewEvidence",
356
+ );
357
+ const approvals = value.approvals.map((approval, index) => {
358
+ exactKeys(
359
+ approval,
360
+ ["reviewer", "commitSha", "submittedAt"],
361
+ `reviewEvidence.approvals[${index}]`,
362
+ );
363
+ return {
364
+ reviewer: nonEmpty(
365
+ approval.reviewer,
366
+ `reviewEvidence.approvals[${index}].reviewer`,
367
+ ),
368
+ commitSha: exactSha(
369
+ approval.commitSha,
370
+ `reviewEvidence.approvals[${index}].commitSha`,
371
+ ),
372
+ submittedAt: timestamp(
373
+ approval.submittedAt,
374
+ `reviewEvidence.approvals[${index}].submittedAt`,
375
+ ),
376
+ };
377
+ });
378
+ const checks = value.checks.map((check, index) => {
379
+ exactKeys(
380
+ check,
381
+ ["name", "status", "conclusion"],
382
+ `reviewEvidence.checks[${index}]`,
383
+ );
384
+ return {
385
+ name: nonEmpty(check.name, `reviewEvidence.checks[${index}].name`),
386
+ status: nonEmpty(check.status, `reviewEvidence.checks[${index}].status`),
387
+ conclusion: nonEmpty(
388
+ check.conclusion,
389
+ `reviewEvidence.checks[${index}].conclusion`,
390
+ ),
391
+ };
392
+ });
393
+ return {
394
+ repository: repository(value.repository, "reviewEvidence.repository"),
395
+ pullRequest: positiveInteger(
396
+ value.pullRequest,
397
+ "reviewEvidence.pullRequest",
398
+ ),
399
+ headSha: exactSha(value.headSha, "reviewEvidence.headSha"),
400
+ baseRef: nonEmpty(value.baseRef, "reviewEvidence.baseRef"),
401
+ approvals,
402
+ checks,
403
+ observedAt: timestamp(value.observedAt, "reviewEvidence.observedAt"),
404
+ };
405
+ }
406
+
407
+ export function v4UniversalWorkflowAdmissionRoot(value) {
408
+ return documentRoot("universal-workflow-admission", validatePolicy(value));
409
+ }
410
+
411
+ function assertPermissionCeiling(requested, ceiling) {
412
+ for (const [name, level] of Object.entries(requested)) {
413
+ const allowed = ceiling[name] || "none";
414
+ if (LEVELS.get(level) > LEVELS.get(allowed))
415
+ fail(
416
+ "permission-widening",
417
+ `requested ${name}:${level} exceeds admitted ${name}:${allowed}`,
418
+ );
419
+ }
420
+ }
421
+
422
+ export function admitV4UniversalWorkflow({
423
+ request: requestValue,
424
+ policy: policyValue,
425
+ observedRefSha,
426
+ observedConsumerRepository,
427
+ observedConsumerSha,
428
+ observedConsumerWorkflowRef,
429
+ reviewEvidence: reviewEvidenceValue,
430
+ now,
431
+ } = {}) {
432
+ const request = validateV4UniversalWorkflowRequest(requestValue);
433
+ const policy = validatePolicy(policyValue);
434
+ const admissionRoot = v4UniversalWorkflowAdmissionRoot(policy);
435
+ if (request.candidate.admissionRoot !== admissionRoot)
436
+ fail("stale-admission", "request is not bound to the current admission");
437
+ if (policy.sourceRepository !== request.candidate.repository)
438
+ fail("source-repository-mismatch", "admission source repository mismatch");
439
+ const workflowRef = nonEmpty(observedConsumerWorkflowRef, "workflow ref");
440
+ const expectedWorkflowPrefix = `${request.consumer.repository}/${request.consumer.workflow}@`;
441
+ if (
442
+ request.consumer.repository !== observedConsumerRepository ||
443
+ request.consumer.sourceSha !==
444
+ exactSha(observedConsumerSha, "observedConsumerSha") ||
445
+ !workflowRef.startsWith(expectedWorkflowPrefix) ||
446
+ workflowRef.length === expectedWorkflowPrefix.length
447
+ )
448
+ fail("consumer-identity-mismatch", "caller identity mismatch");
449
+ if (!policy.allowedCapabilities.includes(request.capability.id))
450
+ fail("capability-not-admitted", "capability is not admitted");
451
+ if (
452
+ request.capability.contractRoots.length !== policy.contractRoots.length ||
453
+ request.capability.contractRoots.some(
454
+ (value, index) => value !== policy.contractRoots[index],
455
+ )
456
+ )
457
+ fail("contract-root-mismatch", "candidate contract roots are not admitted");
458
+ assertPermissionCeiling(
459
+ request.capability.permissions,
460
+ policy.permissionCeiling,
461
+ );
462
+ const observedAt = timestamp(now, "now");
463
+ if (
464
+ Date.parse(observedAt) < Date.parse(policy.validFrom) ||
465
+ Date.parse(observedAt) >= Date.parse(policy.expiresAt)
466
+ )
467
+ fail("stale-admission", "admission is outside its validity window");
468
+ const runtimeSha = exactSha(observedRefSha, "observedRefSha");
469
+ if (runtimeSha !== request.candidate.expectedSha)
470
+ fail(
471
+ "candidate-ref-moved",
472
+ "candidate ref readback does not match expected SHA",
473
+ );
474
+ const reviewEvidence = validateReviewEvidence(reviewEvidenceValue);
475
+ if (
476
+ reviewEvidence.repository !== request.candidate.repository ||
477
+ reviewEvidence.pullRequest !== request.candidate.reviewPullRequest ||
478
+ reviewEvidence.headSha !== runtimeSha ||
479
+ reviewEvidence.baseRef !== policy.targetRef
480
+ )
481
+ fail(
482
+ "review-identity-mismatch",
483
+ "review evidence does not bind the candidate",
484
+ );
485
+ const approvedReviewers = new Set(
486
+ reviewEvidence.approvals
487
+ .filter((approval) => approval.commitSha === runtimeSha)
488
+ .map((approval) => approval.reviewer),
489
+ );
490
+ const admittedApprovalCount = policy.allowedReviewers.filter((reviewer) =>
491
+ approvedReviewers.has(reviewer),
492
+ ).length;
493
+ if (admittedApprovalCount < policy.minimumApprovals)
494
+ fail(
495
+ "independent-review-missing",
496
+ "candidate lacks exact-head independent review",
497
+ );
498
+ const checksByName = new Map();
499
+ for (const check of reviewEvidence.checks) {
500
+ const entries = checksByName.get(check.name) || [];
501
+ entries.push(check);
502
+ checksByName.set(check.name, entries);
503
+ }
504
+ if (
505
+ policy.requiredChecks.some((name) => {
506
+ const checks = checksByName.get(name) || [];
507
+ return (
508
+ checks.length === 0 ||
509
+ checks.some(
510
+ (check) =>
511
+ check.status !== "completed" || check.conclusion !== "success",
512
+ )
513
+ );
514
+ })
515
+ )
516
+ fail(
517
+ "exact-head-checks-incomplete",
518
+ "candidate exact-head checks are not successful",
519
+ );
520
+ const requestRoot = v4UniversalWorkflowRequestRoot(request);
521
+ const discovery = {
522
+ repository: request.candidate.repository,
523
+ ref: request.candidate.discoveryRef,
524
+ expectedSha: request.candidate.expectedSha,
525
+ observedSha: runtimeSha,
526
+ observedAt,
527
+ };
528
+ return {
529
+ schema: V4_UNIVERSAL_WORKFLOW_ADMISSION,
530
+ status: "admitted",
531
+ requestRoot,
532
+ admissionRoot,
533
+ discoveryRoot: documentRoot("universal-workflow-discovery", discovery),
534
+ reviewRoot: documentRoot("universal-workflow-review", reviewEvidence),
535
+ consumerRoot: documentRoot("universal-workflow-consumer", request.consumer),
536
+ capabilityRoot: documentRoot(
537
+ "universal-workflow-capability",
538
+ request.capability,
539
+ ),
540
+ runtime: {
541
+ repository: request.candidate.repository,
542
+ sha: runtimeSha,
543
+ },
544
+ permissions: request.capability.permissions,
545
+ contractRoots: request.capability.contractRoots,
546
+ };
547
+ }
548
+
549
+ export function completeV4UniversalWorkflow({ admission, resultRoot, status }) {
550
+ exactKeys(
551
+ admission,
552
+ [
553
+ "schema",
554
+ "status",
555
+ "requestRoot",
556
+ "admissionRoot",
557
+ "discoveryRoot",
558
+ "reviewRoot",
559
+ "consumerRoot",
560
+ "capabilityRoot",
561
+ "runtime",
562
+ "permissions",
563
+ "contractRoots",
564
+ ],
565
+ "admission",
566
+ );
567
+ if (
568
+ admission.schema !== V4_UNIVERSAL_WORKFLOW_ADMISSION ||
569
+ admission.status !== "admitted"
570
+ )
571
+ fail("invalid-admission", "terminal receipt requires an admitted request");
572
+ if (!new Set(["succeeded", "failed", "cancelled"]).has(status))
573
+ fail("invalid-terminal-status", "terminal status is unsupported");
574
+ const receipt = {
575
+ schema: V4_UNIVERSAL_WORKFLOW_TERMINAL_RECEIPT,
576
+ status,
577
+ requestRoot: root(admission.requestRoot, "admission.requestRoot"),
578
+ admissionRoot: root(admission.admissionRoot, "admission.admissionRoot"),
579
+ discoveryRoot: root(admission.discoveryRoot, "admission.discoveryRoot"),
580
+ reviewRoot: root(admission.reviewRoot, "admission.reviewRoot"),
581
+ consumerRoot: root(admission.consumerRoot, "admission.consumerRoot"),
582
+ capabilityRoot: root(admission.capabilityRoot, "admission.capabilityRoot"),
583
+ runtime: {
584
+ repository: repository(
585
+ admission.runtime?.repository,
586
+ "admission.runtime.repository",
587
+ ),
588
+ sha: exactSha(admission.runtime?.sha, "admission.runtime.sha"),
589
+ },
590
+ resultRoot: root(resultRoot, "resultRoot"),
591
+ };
592
+ return {
593
+ ...receipt,
594
+ receiptRoot: documentRoot("universal-workflow-receipt", receipt),
595
+ };
596
+ }
@@ -196,7 +196,6 @@ function normalizeNpmPublisher(value, { packageName, repository, workflowFilenam
196
196
  configurationRead: true,
197
197
  };
198
198
  }
199
-
200
199
  function main() {
201
200
  const repository = flag("repository");
202
201
  const workflowRepository = flag("workflow-repository", repository);