@kungfu-tech/buildchain 4.0.2-alpha.2 → 4.0.2-alpha.20
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/architecture/agent-change-map.md +52 -1
- package/architecture/ci-lane-change-budget.json +349 -0
- package/architecture/internal-capabilities.json +44 -1
- package/architecture/maintainability-debt.json +53 -33
- package/architecture/maintainability-policy.json +20 -15
- package/architecture/v3-core-mechanism-inventory.json +1 -0
- package/architecture/v4-delivery-warrant-shadow-fixtures.json +5 -5
- package/architecture/v4-release-invocation-fixtures.json +20 -0
- package/architecture/v4-release-topology.json +160 -84
- package/architecture/v4-runtime-semantic-closure.json +4 -1
- package/architecture/v4-universal-workflow-bootstrap.json +171 -0
- package/architecture/v4-universal-workflow-fault-campaign.json +72 -0
- package/architecture/v4-universal-workflow-train-admission.json +40 -0
- package/contracts/buildchain-v2-residuals-v1.json +0 -54
- package/contracts/v4-release-invocation-v1.schema.json +50 -2
- package/dist/site/buildchain-contract.json +87 -42
- package/dist/site/buildchain-site.json +15 -10
- package/dist/site/capability-registry.json +1 -1
- package/dist/site/controller-registry.json +46 -10
- package/dist/site/kfd-claims.json +174 -196
- package/dist/site/kfd-upstream-aggregate.json +1 -1
- package/dist/site/manual-registry.json +2 -2
- package/dist/site/node-api-registry.json +95 -20
- package/dist/site/page-registry.json +9 -4
- package/dist/site/public-surface-audit.json +178 -198
- package/dist/site/publication-authority-registry.json +67 -57
- package/dist/site/publication-registry.json +4 -4
- package/dist/site/site-manifest.json +6 -6
- package/dist/site/workflow-registry.json +209 -259
- package/docs/node-api-reference.md +25 -22
- package/docs/reusable-build-surface.md +16 -0
- package/package.json +2 -2
- package/packages/core/buildchain-publication-authority.js +3 -2
- package/packages/core/ci-lane-change-budget.js +14 -1
- package/packages/core/dev-delivery-candidate-identity.js +18 -0
- package/packages/core/dev-delivery-execution-transfer.js +6 -7
- package/packages/core/dev-delivery-warrant-legacy-recovery.js +4 -2
- package/packages/core/dev-delivery-warrant-native-compatibility.js +33 -0
- package/packages/core/dev-delivery-warrant-state.js +42 -26
- package/packages/core/dev-delivery-warrant.js +8 -0
- package/packages/core/dev-delivery-writer-protocol-transition.js +72 -0
- package/packages/core/release-tail-product-capabilities.js +29 -0
- package/packages/core/release-tail-provider-plane.js +3 -3
- package/packages/core/v4-canonical-contracts.js +6 -0
- package/packages/core/v4-product-publication.js +387 -0
- package/packages/core/v4-protected-publication-source.js +93 -0
- package/packages/core/v4-publication-qualification.js +1 -0
- package/packages/core/v4-release-invocation.js +72 -3
- package/packages/core/v4-universal-workflow-bootstrap.js +586 -0
- package/scripts/check-inventory.mjs +13 -22
- package/scripts/check-maintainability.mjs +16 -113
- package/scripts/check-v4-release-topology.mjs +296 -40
- package/scripts/check-v4-universal-workflow-bootstrap.mjs +265 -0
- package/scripts/dev-delivery-warrant-store.mjs +73 -7
- package/scripts/dev-delivery-warrant-transition.mjs +109 -0
- package/scripts/dev-delivery-warrant.mjs +84 -100
- package/scripts/dev-pr-auto-merge.mjs +15 -15
- package/scripts/generate-channel-build-workflow.mjs +5 -1
- package/scripts/generate-channel-promotion-workflow.mjs +5 -3
- package/scripts/generate-v4-universal-workflow-facades.mjs +415 -0
- package/scripts/maintainability-public-surface.mjs +115 -0
- package/scripts/release-candidate-resolver.mjs +23 -24
- package/scripts/resume-from-candidate-run.mjs +16 -17
- package/scripts/universal-facade-maintainability.mjs +82 -0
- package/scripts/v4-product-publication-intent.mjs +114 -0
- package/scripts/v4-release-candidate-adapter.mjs +41 -0
- package/scripts/v4-universal-workflow-backflow.mjs +212 -0
- package/scripts/v4-universal-workflow-engine.mjs +617 -0
- package/scripts/v4-universal-workflow-self-dogfood.mjs +205 -0
- package/templates/universal-buildchain-bootstrap-recovery.yml +282 -0
- package/templates/universal-buildchain-bootstrap.yml +28 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import crypto from "node:crypto";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
|
|
8
|
+
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
9
|
+
const contract = JSON.parse(
|
|
10
|
+
fs.readFileSync(
|
|
11
|
+
path.join(root, "architecture/v4-universal-workflow-bootstrap.json"),
|
|
12
|
+
"utf8",
|
|
13
|
+
),
|
|
14
|
+
);
|
|
15
|
+
const admissionPolicy = JSON.parse(
|
|
16
|
+
fs.readFileSync(
|
|
17
|
+
path.join(root, "architecture/v4-universal-workflow-train-admission.json"),
|
|
18
|
+
"utf8",
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
const fileRoot = (relative) =>
|
|
22
|
+
`sha256:${crypto
|
|
23
|
+
.createHash("sha256")
|
|
24
|
+
.update(fs.readFileSync(path.join(root, relative)))
|
|
25
|
+
.digest("hex")}`;
|
|
26
|
+
|
|
27
|
+
const workflowRoot = path.join(root, ".github/workflows");
|
|
28
|
+
const discovered = fs
|
|
29
|
+
.readdirSync(workflowRoot)
|
|
30
|
+
.filter((name) => /\.ya?ml$/u.test(name))
|
|
31
|
+
.map((name) => `.github/workflows/${name}`)
|
|
32
|
+
.filter((relative) =>
|
|
33
|
+
/(?:^|\n)\s*workflow_call:\s*(?:\n|$)/u.test(
|
|
34
|
+
fs.readFileSync(path.join(root, relative), "utf8"),
|
|
35
|
+
),
|
|
36
|
+
)
|
|
37
|
+
.sort();
|
|
38
|
+
|
|
39
|
+
assert.equal(
|
|
40
|
+
contract.schema,
|
|
41
|
+
"kungfu-buildchain-v4-universal-workflow-bootstrap/v1",
|
|
42
|
+
);
|
|
43
|
+
assert.deepEqual(
|
|
44
|
+
contract.inventoryWorkflows,
|
|
45
|
+
[...new Set(contract.inventoryWorkflows)].sort(),
|
|
46
|
+
"inventoried public workflows must be sorted and duplicate-free",
|
|
47
|
+
);
|
|
48
|
+
assert.deepEqual(
|
|
49
|
+
contract.inventoryWorkflows,
|
|
50
|
+
discovered,
|
|
51
|
+
"every active public workflow_call surface must be inventoried",
|
|
52
|
+
);
|
|
53
|
+
for (const relative of contract.inventoryWorkflows)
|
|
54
|
+
assert.ok(
|
|
55
|
+
fs.statSync(path.join(root, relative)).isFile(),
|
|
56
|
+
`inventoried workflow is unavailable: ${relative}`,
|
|
57
|
+
);
|
|
58
|
+
assert.deepEqual(
|
|
59
|
+
contract.bootstrapGovernedWorkflows,
|
|
60
|
+
[...new Set(contract.bootstrapGovernedWorkflows)].sort(),
|
|
61
|
+
"Bootstrap-governed workflows must be sorted and duplicate-free",
|
|
62
|
+
);
|
|
63
|
+
assert.deepEqual(
|
|
64
|
+
contract.retiredWorkflowSurfaces,
|
|
65
|
+
[...new Set(contract.retiredWorkflowSurfaces)].sort(),
|
|
66
|
+
"retired workflow surfaces must be sorted and duplicate-free",
|
|
67
|
+
);
|
|
68
|
+
for (const relative of contract.retiredWorkflowSurfaces) {
|
|
69
|
+
assert.ok(
|
|
70
|
+
contract.inventoryWorkflows.includes(relative),
|
|
71
|
+
`retired workflow is outside the inventory: ${relative}`,
|
|
72
|
+
);
|
|
73
|
+
assert.match(
|
|
74
|
+
fs.readFileSync(path.join(root, relative), "utf8"),
|
|
75
|
+
/retired/u,
|
|
76
|
+
`retired workflow does not fail closed explicitly: ${relative}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const activeWorkflows = contract.inventoryWorkflows.filter(
|
|
80
|
+
(relative) =>
|
|
81
|
+
!contract.retiredWorkflowSurfaces.includes(relative) &&
|
|
82
|
+
relative !== contract.bootstrap.consumerRecoveryWorkflow,
|
|
83
|
+
);
|
|
84
|
+
assert.deepEqual(
|
|
85
|
+
contract.bootstrapGovernedWorkflows,
|
|
86
|
+
activeWorkflows,
|
|
87
|
+
"every active public reusable workflow must be Bootstrap-governed",
|
|
88
|
+
);
|
|
89
|
+
for (const relative of contract.bootstrapGovernedWorkflows) {
|
|
90
|
+
assert.ok(
|
|
91
|
+
contract.inventoryWorkflows.includes(relative),
|
|
92
|
+
`Bootstrap-governed workflow is outside the inventory: ${relative}`,
|
|
93
|
+
);
|
|
94
|
+
const source = fs.readFileSync(path.join(root, relative), "utf8");
|
|
95
|
+
assert.ok(
|
|
96
|
+
relative === contract.bootstrap.publicWorkflow ||
|
|
97
|
+
/uses:\s+(?:\.\/)?\.github\/workflows\/bootstrap\.yml/u.test(source) ||
|
|
98
|
+
/uses:\s+kungfu-systems\/buildchain\/\.github\/workflows\/bootstrap\.yml@/u.test(
|
|
99
|
+
source,
|
|
100
|
+
),
|
|
101
|
+
`workflow is declared governed without a Bootstrap edge: ${relative}`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
assert.deepEqual(contract.complexityBudget, {
|
|
106
|
+
bootstrapAbis: 1,
|
|
107
|
+
requestEnvelopes: 1,
|
|
108
|
+
candidateResolvers: 1,
|
|
109
|
+
candidateEngines: 1,
|
|
110
|
+
terminalReceiptAuthorities: 1,
|
|
111
|
+
perWorkflowBootstrapImplementations: 0,
|
|
112
|
+
buildchainOnlySelfReleasePaths: 0,
|
|
113
|
+
perIncidentConsumerEdits: 0,
|
|
114
|
+
});
|
|
115
|
+
assert.equal(contract.bootstrap.discoveryRefExecutionAuthority, false);
|
|
116
|
+
assert.equal(contract.bootstrap.exactShaExecutionAuthority, true);
|
|
117
|
+
assert.equal(contract.bootstrap.consumerSourceMutationPerIncident, false);
|
|
118
|
+
assert.equal(contract.bootstrap.buildchainReleasePromotionPerIncident, false);
|
|
119
|
+
assert.equal(contract.bootstrap.selfDogfoodUsesPublicContract, true);
|
|
120
|
+
assert.equal(contract.bootstrap.recoveryDependsOnPublishedBuildchain, false);
|
|
121
|
+
assert.equal(
|
|
122
|
+
contract.bootstrap.reviewRuntimeBinding,
|
|
123
|
+
"reviewed-head-or-protected-alpha-merge-to-exact-runtime",
|
|
124
|
+
);
|
|
125
|
+
assert.deepEqual(
|
|
126
|
+
contract.capabilityAdapters.map(({ id }) => id).sort(),
|
|
127
|
+
[...admissionPolicy.allowedCapabilities].sort(),
|
|
128
|
+
"every admitted capability must have one real candidate adapter",
|
|
129
|
+
);
|
|
130
|
+
for (const adapter of contract.capabilityAdapters) {
|
|
131
|
+
assert.match(adapter.execution, /^exact-candidate-/u);
|
|
132
|
+
assert.ok(adapter.payloadSchema.endsWith("/v1"));
|
|
133
|
+
}
|
|
134
|
+
assert.ok(!admissionPolicy.allowedCapabilities.includes("workflow-contract"));
|
|
135
|
+
assert.deepEqual(
|
|
136
|
+
admissionPolicy.contractRoots,
|
|
137
|
+
[
|
|
138
|
+
fileRoot("architecture/v4-universal-workflow-bootstrap.json"),
|
|
139
|
+
fileRoot(contract.bootstrap.faultCampaign),
|
|
140
|
+
fileRoot("packages/core/v4-universal-workflow-bootstrap.js"),
|
|
141
|
+
fileRoot("scripts/v4-universal-workflow-backflow.mjs"),
|
|
142
|
+
fileRoot("scripts/v4-universal-workflow-engine.mjs"),
|
|
143
|
+
fileRoot("scripts/v4-universal-workflow-self-dogfood.mjs"),
|
|
144
|
+
fileRoot(contract.bootstrap.publicWorkflow),
|
|
145
|
+
].sort(),
|
|
146
|
+
);
|
|
147
|
+
assert.doesNotMatch(
|
|
148
|
+
fs.readFileSync(
|
|
149
|
+
path.join(root, contract.bootstrap.candidateEnginePath),
|
|
150
|
+
"utf8",
|
|
151
|
+
),
|
|
152
|
+
/request\.capability\.id\s*===\s*["']workflow-contract["']/u,
|
|
153
|
+
"contract-only validation must not be admitted as successful execution",
|
|
154
|
+
);
|
|
155
|
+
const bootstrapSource = fs.readFileSync(
|
|
156
|
+
path.join(root, contract.bootstrap.publicWorkflow),
|
|
157
|
+
"utf8",
|
|
158
|
+
);
|
|
159
|
+
assert.doesNotMatch(
|
|
160
|
+
bootstrapSource,
|
|
161
|
+
/admission-policy-json:/u,
|
|
162
|
+
"callers must not supply their own admission authority",
|
|
163
|
+
);
|
|
164
|
+
execFileSync(
|
|
165
|
+
process.execPath,
|
|
166
|
+
[path.join(root, contract.migration.compatibilityFacadeGenerator), "--check"],
|
|
167
|
+
{ stdio: "inherit" },
|
|
168
|
+
);
|
|
169
|
+
assert.doesNotMatch(bootstrapSource, /^ permissions:/mu);
|
|
170
|
+
assert.match(bootstrapSource, /settle:[\s\S]*needs: \[admit, execute\]/u);
|
|
171
|
+
assert.match(
|
|
172
|
+
bootstrapSource,
|
|
173
|
+
/ascii_downcase\) == "kungfu-origin"[\s\S]*\.name == "check"[\s\S]*map\(select\(\.status == "completed" and \(\.conclusion \/\/ ""\) != ""\)\)[\s\S]*candidate\/architecture\/v4-universal-workflow-train-admission\.json/u,
|
|
174
|
+
);
|
|
175
|
+
assert.match(
|
|
176
|
+
bootstrapSource,
|
|
177
|
+
/CANDIDATE_MODE:[\s\S]*\.merged == true[\s\S]*\.merge_commit_sha == \$expected[\s\S]*\.base\.ref == "alpha\/v4\/v4\.0"[\s\S]*commitSha: \$expected[\s\S]*runtimeBinding:/u,
|
|
178
|
+
);
|
|
179
|
+
const consumerTemplate = fs.readFileSync(
|
|
180
|
+
path.join(root, contract.bootstrap.consumerTemplate),
|
|
181
|
+
"utf8",
|
|
182
|
+
);
|
|
183
|
+
const consumerRecoveryTemplate = fs.readFileSync(
|
|
184
|
+
path.join(root, contract.bootstrap.consumerRecoveryTemplate),
|
|
185
|
+
"utf8",
|
|
186
|
+
);
|
|
187
|
+
const consumerRecoveryWorkflow = fs.readFileSync(
|
|
188
|
+
path.join(root, contract.bootstrap.consumerRecoveryWorkflow),
|
|
189
|
+
"utf8",
|
|
190
|
+
);
|
|
191
|
+
const selfDogfood = fs.readFileSync(
|
|
192
|
+
path.join(root, contract.bootstrap.selfDogfoodWorkflow),
|
|
193
|
+
"utf8",
|
|
194
|
+
);
|
|
195
|
+
assert.match(
|
|
196
|
+
consumerTemplate,
|
|
197
|
+
/uses:\s+kungfu-systems\/buildchain\/\.github\/workflows\/bootstrap\.yml@v4/u,
|
|
198
|
+
);
|
|
199
|
+
assert.match(
|
|
200
|
+
consumerRecoveryTemplate,
|
|
201
|
+
/recovery-admit:[\s\S]*recovery-execute:/u,
|
|
202
|
+
);
|
|
203
|
+
assert.match(
|
|
204
|
+
consumerRecoveryTemplate,
|
|
205
|
+
/Parse exact recovery coordinates without Buildchain code[\s\S]*Prove exact independent review before candidate code runs[\s\S]*ascii_downcase\) == "kungfu-origin"[\s\S]*\.name == "check"[\s\S]*Admit candidate policy and exact-head checks[\s\S]*map\(select\(\.status == "completed" and \(\.conclusion \/\/ ""\) != ""\)\)/u,
|
|
206
|
+
);
|
|
207
|
+
assert.match(
|
|
208
|
+
consumerRecoveryTemplate,
|
|
209
|
+
/recovery-execute:[\s\S]*needs: recovery-admit[\s\S]*contents: write/u,
|
|
210
|
+
);
|
|
211
|
+
assert.doesNotMatch(
|
|
212
|
+
consumerRecoveryTemplate,
|
|
213
|
+
/uses:\s+kungfu-systems\/buildchain\/\.github\/workflows\//u,
|
|
214
|
+
"consumer recovery must not parse any published Buildchain workflow",
|
|
215
|
+
);
|
|
216
|
+
assert.equal(
|
|
217
|
+
consumerRecoveryWorkflow,
|
|
218
|
+
consumerRecoveryTemplate,
|
|
219
|
+
"the Buildchain self-consumer must exercise the exact distributable recovery shell",
|
|
220
|
+
);
|
|
221
|
+
assert.match(
|
|
222
|
+
selfDogfood,
|
|
223
|
+
/uses:\s+kungfu-systems\/buildchain\/\.github\/workflows\/bootstrap\.yml@train\/v4\/v4\.0\/universal-reusable-workflow-bootstrap/u,
|
|
224
|
+
);
|
|
225
|
+
assert.match(
|
|
226
|
+
selfDogfood,
|
|
227
|
+
/uses:\s+\.\/\.github\/workflows\/universal-bootstrap-recovery\.yml/u,
|
|
228
|
+
);
|
|
229
|
+
assert.doesNotMatch(
|
|
230
|
+
selfDogfood,
|
|
231
|
+
/\.github\/workflows\/bootstrap\.yml@(?:v4|v4-alpha)(?:\s|$)/u,
|
|
232
|
+
"Train self-dogfood must not depend on an alpha or stable Bootstrap tag",
|
|
233
|
+
);
|
|
234
|
+
for (const channel of ["conformance", "alpha", "stable"]) {
|
|
235
|
+
assert.match(selfDogfood, new RegExp(`primary-${channel}:`, "u"));
|
|
236
|
+
assert.match(selfDogfood, new RegExp(`recovery-${channel}:`, "u"));
|
|
237
|
+
}
|
|
238
|
+
assert.match(
|
|
239
|
+
selfDogfood,
|
|
240
|
+
/\.state == "APPROVED"[\s\S]*ascii_downcase\) == "kungfu-origin"[\s\S]*\.name == "check"[\s\S]*\.conclusion == "success"/u,
|
|
241
|
+
);
|
|
242
|
+
assert.match(
|
|
243
|
+
consumerRecoveryTemplate,
|
|
244
|
+
/result-json:[\s\S]*value:\s*\$\{\{ jobs\.recovery-execute\.outputs\.result-json \}\}/u,
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
console.log(
|
|
248
|
+
JSON.stringify({
|
|
249
|
+
ok: true,
|
|
250
|
+
schema: contract.schema,
|
|
251
|
+
publicWorkflowCount: discovered.length,
|
|
252
|
+
activePublicWorkflowCount: activeWorkflows.length,
|
|
253
|
+
retiredWorkflowCount: contract.retiredWorkflowSurfaces.length,
|
|
254
|
+
inventoriedWorkflowCount: contract.inventoryWorkflows.length,
|
|
255
|
+
inventoryCoveragePercent: 100,
|
|
256
|
+
governedWorkflowCount: contract.bootstrapGovernedWorkflows.length,
|
|
257
|
+
governedCoveragePercent: Number(
|
|
258
|
+
(
|
|
259
|
+
(contract.bootstrapGovernedWorkflows.length / activeWorkflows.length) *
|
|
260
|
+
100
|
|
261
|
+
).toFixed(2),
|
|
262
|
+
),
|
|
263
|
+
migration: contract.migration,
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
import { normalizeDevDeliveryAuthorityState } from "../packages/core/dev-delivery-authority-state.js";
|
|
8
8
|
|
|
9
9
|
const STATE_PATH = "queue.json";
|
|
10
|
+
const READ_ATTEMPTS = 3;
|
|
10
11
|
|
|
11
12
|
function text(value = "") {
|
|
12
13
|
return String(value ?? "").trim();
|
|
@@ -20,6 +21,14 @@ function exactSha(value, label) {
|
|
|
20
21
|
return normalized;
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
function positiveInteger(value, label) {
|
|
25
|
+
const parsed = Number(value);
|
|
26
|
+
if (!Number.isInteger(parsed) || parsed < 1) {
|
|
27
|
+
throw new Error(`${label} must be a positive integer`);
|
|
28
|
+
}
|
|
29
|
+
return parsed;
|
|
30
|
+
}
|
|
31
|
+
|
|
23
32
|
function normalizeRepository(value) {
|
|
24
33
|
const normalized = text(value);
|
|
25
34
|
const match = normalized.match(/^([^/\s]+)\/([^/\s]+)$/);
|
|
@@ -35,11 +44,10 @@ function encodeRef(ref) {
|
|
|
35
44
|
return ref.split("/").map(encodeURIComponent).join("/");
|
|
36
45
|
}
|
|
37
46
|
|
|
38
|
-
function
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
);
|
|
47
|
+
function decodeGitHubBlobEnvelope(raw) {
|
|
48
|
+
if (!/"encoding"\s*:\s*"base64"/.test(raw)) return raw;
|
|
49
|
+
const blob = JSON.parse(raw);
|
|
50
|
+
if (blob?.encoding !== "base64") return raw;
|
|
43
51
|
return Buffer.from(
|
|
44
52
|
String(blob.content || "").replace(/\s+/g, ""),
|
|
45
53
|
"base64",
|
|
@@ -71,6 +79,7 @@ export class GitHubDevDeliveryStore {
|
|
|
71
79
|
apiUrl = "https://api.github.com",
|
|
72
80
|
fetchImpl = globalThis.fetch,
|
|
73
81
|
createInitialState = createDevDeliveryQueue,
|
|
82
|
+
readAttempts = READ_ATTEMPTS,
|
|
74
83
|
} = {}) {
|
|
75
84
|
this.repository = normalizeRepository(repository);
|
|
76
85
|
if (!fetchImpl) throw new Error("fetch is required");
|
|
@@ -82,6 +91,22 @@ export class GitHubDevDeliveryStore {
|
|
|
82
91
|
this.apiUrl = apiUrl.replace(/\/+$/, "");
|
|
83
92
|
this.fetch = fetchImpl;
|
|
84
93
|
this.createInitialState = createInitialState;
|
|
94
|
+
this.readAttempts = positiveInteger(readAttempts, "readAttempts");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async retryParseableRead(operation) {
|
|
98
|
+
let failure;
|
|
99
|
+
for (let attempt = 1; attempt <= this.readAttempts; attempt += 1) {
|
|
100
|
+
try {
|
|
101
|
+
return await operation();
|
|
102
|
+
} catch (error) {
|
|
103
|
+
failure = error;
|
|
104
|
+
if (!(error instanceof SyntaxError) || attempt === this.readAttempts) {
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
throw failure;
|
|
85
110
|
}
|
|
86
111
|
|
|
87
112
|
async request(method, requestPath, body) {
|
|
@@ -108,7 +133,42 @@ export class GitHubDevDeliveryStore {
|
|
|
108
133
|
return data;
|
|
109
134
|
}
|
|
110
135
|
|
|
136
|
+
async requestRaw(method, requestPath) {
|
|
137
|
+
const response = await this.fetch(`${this.apiUrl}${requestPath}`, {
|
|
138
|
+
method,
|
|
139
|
+
headers: {
|
|
140
|
+
accept: "application/vnd.github.raw+json",
|
|
141
|
+
authorization: `Bearer ${this.token}`,
|
|
142
|
+
"x-github-api-version": "2022-11-28",
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
const raw = await response.text();
|
|
146
|
+
if (!response.ok) {
|
|
147
|
+
let message = raw || `${method} ${requestPath} failed`;
|
|
148
|
+
try {
|
|
149
|
+
message = JSON.parse(raw)?.message || message;
|
|
150
|
+
} catch {
|
|
151
|
+
// Raw blob responses are not required to be JSON.
|
|
152
|
+
}
|
|
153
|
+
const error = new Error(message);
|
|
154
|
+
error.status = response.status;
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
return raw;
|
|
158
|
+
}
|
|
159
|
+
|
|
111
160
|
async read({ stateRef, protectedBase, now, allowLegacyV3Readback = false }) {
|
|
161
|
+
return this.retryParseableRead(async () =>
|
|
162
|
+
this.readOnce({ stateRef, protectedBase, now, allowLegacyV3Readback }),
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async readOnce({
|
|
167
|
+
stateRef,
|
|
168
|
+
protectedBase,
|
|
169
|
+
now,
|
|
170
|
+
allowLegacyV3Readback = false,
|
|
171
|
+
}) {
|
|
112
172
|
let ref;
|
|
113
173
|
try {
|
|
114
174
|
ref = await this.request(
|
|
@@ -135,6 +195,12 @@ export class GitHubDevDeliveryStore {
|
|
|
135
195
|
}
|
|
136
196
|
|
|
137
197
|
async readCommit(commitShaInput, { allowLegacyV3Readback = false } = {}) {
|
|
198
|
+
return this.retryParseableRead(async () =>
|
|
199
|
+
this.readCommitOnce(commitShaInput, { allowLegacyV3Readback }),
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async readCommitOnce(commitShaInput, { allowLegacyV3Readback = false } = {}) {
|
|
138
204
|
const commitSha = exactSha(commitShaInput, "state commit");
|
|
139
205
|
const commit = await this.request(
|
|
140
206
|
"GET",
|
|
@@ -149,11 +215,11 @@ export class GitHubDevDeliveryStore {
|
|
|
149
215
|
);
|
|
150
216
|
if (!entry?.sha)
|
|
151
217
|
throw new Error(`${commitSha} does not contain ${STATE_PATH}`);
|
|
152
|
-
const
|
|
218
|
+
const raw = await this.requestRaw(
|
|
153
219
|
"GET",
|
|
154
220
|
`/repos/${this.repository.fullName}/git/blobs/${entry.sha}`,
|
|
155
221
|
);
|
|
156
|
-
const bytes =
|
|
222
|
+
const bytes = decodeGitHubBlobEnvelope(raw);
|
|
157
223
|
const queue = validateStoredState(JSON.parse(bytes), {
|
|
158
224
|
allowLegacyV3Readback,
|
|
159
225
|
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { recoverExpiredDevDeliveryWarrant } from "../packages/core/dev-delivery-warrant.js";
|
|
2
|
+
|
|
3
|
+
async function recoverExpiredLeaseBeforeReselection({
|
|
4
|
+
store,
|
|
5
|
+
options,
|
|
6
|
+
loaded,
|
|
7
|
+
initialLoaded,
|
|
8
|
+
}) {
|
|
9
|
+
const recovery = recoverExpiredDevDeliveryWarrant(loaded.queue, {
|
|
10
|
+
now: options.now,
|
|
11
|
+
});
|
|
12
|
+
if (recovery.receipt.expectedOldStateRoot !== loaded.queue.stateRoot) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
"expired Warrant recovery receipt expected-old root does not match the loaded authority",
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
await store.write({
|
|
18
|
+
stateRef: options.stateRef,
|
|
19
|
+
queue: recovery.queue,
|
|
20
|
+
expectedCommitSha: loaded.commitSha,
|
|
21
|
+
expectedStateRoot: loaded.queue.stateRoot,
|
|
22
|
+
receiptRoot: recovery.receiptRoot,
|
|
23
|
+
});
|
|
24
|
+
const reloaded = await store.read({
|
|
25
|
+
stateRef: options.stateRef,
|
|
26
|
+
protectedBase: options.branch,
|
|
27
|
+
now: options.now,
|
|
28
|
+
});
|
|
29
|
+
return {
|
|
30
|
+
loaded: reloaded,
|
|
31
|
+
concurrencyRecovery: {
|
|
32
|
+
schema: "kungfu.buildchain.dev-delivery-concurrency-recovery/v1",
|
|
33
|
+
action: "expired-lease-recovered-before-reselection",
|
|
34
|
+
initialCommitSha: initialLoaded.commitSha,
|
|
35
|
+
recoveredStateRoot: recovery.queue.stateRoot,
|
|
36
|
+
observedCommitSha: reloaded.commitSha,
|
|
37
|
+
observedStateRoot: reloaded.queue.stateRoot,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function persistDevDeliveryTransition({
|
|
43
|
+
store,
|
|
44
|
+
options,
|
|
45
|
+
loaded,
|
|
46
|
+
initialLoaded,
|
|
47
|
+
changed,
|
|
48
|
+
transitionFor,
|
|
49
|
+
}) {
|
|
50
|
+
let concurrencyRecovery = null;
|
|
51
|
+
if (
|
|
52
|
+
options.execute &&
|
|
53
|
+
options.command === "select" &&
|
|
54
|
+
changed.recoveryReceipt?.action === "recovered-expired-lease"
|
|
55
|
+
) {
|
|
56
|
+
const recovery = await recoverExpiredLeaseBeforeReselection({
|
|
57
|
+
store,
|
|
58
|
+
options,
|
|
59
|
+
loaded,
|
|
60
|
+
initialLoaded,
|
|
61
|
+
});
|
|
62
|
+
loaded = recovery.loaded;
|
|
63
|
+
changed = transitionFor(options.command, loaded.queue, options);
|
|
64
|
+
concurrencyRecovery = recovery.concurrencyRecovery;
|
|
65
|
+
}
|
|
66
|
+
if (!options.execute || changed.queue.stateRoot === loaded.queue.stateRoot)
|
|
67
|
+
return { loaded, changed, write: null, concurrencyRecovery };
|
|
68
|
+
if (changed.receipt.expectedOldStateRoot !== loaded.queue.stateRoot) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"transition receipt expected-old root does not match the loaded authority",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const write = await store.write({
|
|
75
|
+
stateRef: options.stateRef,
|
|
76
|
+
queue: changed.queue,
|
|
77
|
+
expectedCommitSha: loaded.commitSha,
|
|
78
|
+
expectedStateRoot: loaded.queue.stateRoot,
|
|
79
|
+
receiptRoot: changed.receiptRoot,
|
|
80
|
+
});
|
|
81
|
+
return { loaded, changed, write, concurrencyRecovery };
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (options.command !== "settle" || options.expectedOldStateRoot)
|
|
84
|
+
throw error;
|
|
85
|
+
const latest = await store.read({
|
|
86
|
+
stateRef: options.stateRef,
|
|
87
|
+
protectedBase: options.branch,
|
|
88
|
+
now: options.now,
|
|
89
|
+
});
|
|
90
|
+
const reconciled = transitionFor(options.command, latest.queue, options);
|
|
91
|
+
if (
|
|
92
|
+
reconciled.queue.stateRoot !== latest.queue.stateRoot ||
|
|
93
|
+
reconciled.receipt.action !== "duplicate-terminal-event-noop"
|
|
94
|
+
)
|
|
95
|
+
throw error;
|
|
96
|
+
return {
|
|
97
|
+
loaded: latest,
|
|
98
|
+
changed: reconciled,
|
|
99
|
+
write: null,
|
|
100
|
+
concurrencyRecovery: {
|
|
101
|
+
schema: "kungfu.buildchain.dev-delivery-concurrency-recovery/v1",
|
|
102
|
+
action: "terminal-settlement-race-noop",
|
|
103
|
+
initialCommitSha: initialLoaded.commitSha,
|
|
104
|
+
observedCommitSha: latest.commitSha,
|
|
105
|
+
observedStateRoot: latest.queue.stateRoot,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|