code-foundry 1.18.0 → 1.19.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/docs/PERFORMANCE.md +8 -12
- package/docs/fleet-release-eligibility.md +80 -0
- package/docs/fleet-rollouts.md +4 -3
- package/package.json +1 -1
- package/src/cli.mjs +19 -6
- package/src/commands/fleet-core.mjs +567 -0
- package/src/commands/fleet.mjs +9 -562
- package/src/lib/fleet-eligibility.mjs +245 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { createHash } from 'node:crypto'
|
|
3
|
+
import { spawnSync } from 'node:child_process'
|
|
4
|
+
import { lstatSync, readFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
|
|
8
|
+
export const RELEASE_POLICY_FILE = '.code-foundry-release-policy.json'
|
|
9
|
+
/** @typedef {(command:string, args:string[]) => string} Run */
|
|
10
|
+
/** @param {unknown} condition @param {string} message @returns {asserts condition} */
|
|
11
|
+
function ensure(condition, message) {
|
|
12
|
+
if (!condition) throw new Error(message)
|
|
13
|
+
}
|
|
14
|
+
/** @type {Run} */
|
|
15
|
+
function runCommand(command, args) {
|
|
16
|
+
const result = spawnSync(command, args, {
|
|
17
|
+
encoding: 'utf8',
|
|
18
|
+
timeout: 120_000,
|
|
19
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
20
|
+
env: { ...process.env, GH_HOST: 'github.com', GH_PROMPT_DISABLED: '1' },
|
|
21
|
+
})
|
|
22
|
+
ensure(
|
|
23
|
+
!result.error && result.status === 0,
|
|
24
|
+
`Fleet eligibility is unverified: ${command} failed (${result.status ?? result.error?.message})`
|
|
25
|
+
)
|
|
26
|
+
return result.stdout.trim()
|
|
27
|
+
}
|
|
28
|
+
/** @param {Buffer} bytes */
|
|
29
|
+
function hash(bytes) {
|
|
30
|
+
return createHash('sha256').update(bytes).digest('hex')
|
|
31
|
+
}
|
|
32
|
+
/** @param {unknown} value */
|
|
33
|
+
export function validateReleasePolicy(value) {
|
|
34
|
+
const policy = /** @type {Record<string, any>} */ (value)
|
|
35
|
+
ensure(policy?.schema_version === 1, 'Expected a version-1 release policy')
|
|
36
|
+
ensure(
|
|
37
|
+
typeof policy.repository === 'string' &&
|
|
38
|
+
/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(policy.repository) &&
|
|
39
|
+
!policy.repository
|
|
40
|
+
.split('/')
|
|
41
|
+
.some((/** @type {string} */ part) => ['.', '..'].includes(part)),
|
|
42
|
+
'Invalid release-policy repository'
|
|
43
|
+
)
|
|
44
|
+
ensure(
|
|
45
|
+
typeof policy.workflow === 'string' &&
|
|
46
|
+
/^\.github\/workflows\/[A-Za-z0-9_-]+\.ya?ml$/.test(policy.workflow),
|
|
47
|
+
'Expected an exact qualification workflow path'
|
|
48
|
+
)
|
|
49
|
+
ensure(
|
|
50
|
+
policy.branch === 'main' || policy.branch === 'staging',
|
|
51
|
+
'Select a protected main or staging qualification branch'
|
|
52
|
+
)
|
|
53
|
+
ensure(
|
|
54
|
+
Array.isArray(policy.required_jobs) &&
|
|
55
|
+
policy.required_jobs.length > 0 &&
|
|
56
|
+
policy.required_jobs.every(
|
|
57
|
+
(/** @type {unknown} */ job) =>
|
|
58
|
+
typeof job === 'string' && job.trim() === job && job.length > 0
|
|
59
|
+
) &&
|
|
60
|
+
new Set(policy.required_jobs).size === policy.required_jobs.length,
|
|
61
|
+
'Expected unique nonempty required qualification job names'
|
|
62
|
+
)
|
|
63
|
+
return policy
|
|
64
|
+
}
|
|
65
|
+
/** @param {unknown} value @param {string} property */
|
|
66
|
+
function pages(value, property) {
|
|
67
|
+
ensure(Array.isArray(value) && value.length > 0, 'Missing paginated qualification evidence')
|
|
68
|
+
/** @type {Record<string, any>[]} */
|
|
69
|
+
const items = []
|
|
70
|
+
let expected = 0
|
|
71
|
+
for (const page of value) {
|
|
72
|
+
ensure(
|
|
73
|
+
Number.isInteger(page.total_count) && page.total_count >= 0 && Array.isArray(page[property]),
|
|
74
|
+
'Malformed paginated qualification evidence'
|
|
75
|
+
)
|
|
76
|
+
expected = Math.max(expected, page.total_count)
|
|
77
|
+
items.push(...page[property])
|
|
78
|
+
}
|
|
79
|
+
ensure(items.length >= expected, 'Incomplete qualification evidence pagination')
|
|
80
|
+
return items
|
|
81
|
+
}
|
|
82
|
+
/** @param {string} root @param {string} source @param {string} version @param {Run} [run] */
|
|
83
|
+
export function assertFleetEligibility(root, source, version, run = runCommand) {
|
|
84
|
+
const path = join(root, RELEASE_POLICY_FILE)
|
|
85
|
+
let entry
|
|
86
|
+
try {
|
|
87
|
+
entry = lstatSync(path)
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') return null
|
|
90
|
+
throw error
|
|
91
|
+
}
|
|
92
|
+
ensure(
|
|
93
|
+
entry.isFile() && !entry.isSymbolicLink(),
|
|
94
|
+
'Release policy must be a regular non-symlink file'
|
|
95
|
+
)
|
|
96
|
+
const bytes = readFileSync(path)
|
|
97
|
+
const policy = validateReleasePolicy(JSON.parse(bytes.toString('utf8')))
|
|
98
|
+
ensure(
|
|
99
|
+
/^v\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?$/.test(version),
|
|
100
|
+
'Qualified upgrades require an explicit version tag'
|
|
101
|
+
)
|
|
102
|
+
const git = (/** @type {string[]} */ args) => run('git', ['-C', source, ...args]).trim()
|
|
103
|
+
const sourceSha = git(['rev-parse', 'HEAD'])
|
|
104
|
+
ensure(
|
|
105
|
+
/^[a-f0-9]{40}$/.test(sourceSha),
|
|
106
|
+
'Qualified upgrades require a source Git checkout with an exact commit'
|
|
107
|
+
)
|
|
108
|
+
ensure(
|
|
109
|
+
git(['status', '--porcelain', '--untracked-files=all']) === '',
|
|
110
|
+
'Qualification cannot authorize a dirty runtime source'
|
|
111
|
+
)
|
|
112
|
+
// Execute the installed/trusted verifier, never code from the proposed source checkout.
|
|
113
|
+
const verifier = fileURLToPath(new URL('../commands/release-integrity.mjs', import.meta.url))
|
|
114
|
+
const verified = JSON.parse(
|
|
115
|
+
run(process.execPath, [
|
|
116
|
+
verifier,
|
|
117
|
+
'release',
|
|
118
|
+
'--repo',
|
|
119
|
+
policy.repository,
|
|
120
|
+
'--tag',
|
|
121
|
+
version,
|
|
122
|
+
'--expected-sha',
|
|
123
|
+
sourceSha,
|
|
124
|
+
])
|
|
125
|
+
)
|
|
126
|
+
ensure(
|
|
127
|
+
verified.status === 'passed' &&
|
|
128
|
+
verified.check === 'release-attestation' &&
|
|
129
|
+
verified.immutable === true &&
|
|
130
|
+
verified.repository === policy.repository &&
|
|
131
|
+
verified.tag === version &&
|
|
132
|
+
verified.sourceSha === sourceSha,
|
|
133
|
+
'Release identity was not cryptographically verified'
|
|
134
|
+
)
|
|
135
|
+
const api = (/** @type {string} */ endpoint, paginate = false) =>
|
|
136
|
+
JSON.parse(
|
|
137
|
+
run('gh', [
|
|
138
|
+
'api',
|
|
139
|
+
'--hostname',
|
|
140
|
+
'github.com',
|
|
141
|
+
endpoint,
|
|
142
|
+
...(paginate ? ['--paginate', '--slurp'] : []),
|
|
143
|
+
])
|
|
144
|
+
)
|
|
145
|
+
const workflow = api(
|
|
146
|
+
`repos/${policy.repository}/actions/workflows/${encodeURIComponent(policy.workflow.split('/').at(-1))}`
|
|
147
|
+
)
|
|
148
|
+
ensure(
|
|
149
|
+
Number.isSafeInteger(workflow.id) &&
|
|
150
|
+
workflow.path === policy.workflow &&
|
|
151
|
+
workflow.state === 'active',
|
|
152
|
+
'Qualification workflow is missing, disabled, or has a different identity'
|
|
153
|
+
)
|
|
154
|
+
const runs = pages(
|
|
155
|
+
api(
|
|
156
|
+
`repos/${policy.repository}/actions/workflows/${workflow.id}/runs?head_sha=${sourceSha}&per_page=100`,
|
|
157
|
+
true
|
|
158
|
+
),
|
|
159
|
+
'workflow_runs'
|
|
160
|
+
)
|
|
161
|
+
.filter(
|
|
162
|
+
(item) =>
|
|
163
|
+
['push', 'workflow_dispatch'].includes(item.event) && item.head_branch === policy.branch
|
|
164
|
+
)
|
|
165
|
+
// oxlint-disable-next-line unicorn/no-array-sort
|
|
166
|
+
.sort((a, b) => b.run_number - a.run_number || b.run_attempt - a.run_attempt)
|
|
167
|
+
ensure(
|
|
168
|
+
runs.every(
|
|
169
|
+
(item) => Number.isSafeInteger(item.run_number) && Number.isSafeInteger(item.run_attempt)
|
|
170
|
+
),
|
|
171
|
+
'Malformed qualification run ordering'
|
|
172
|
+
)
|
|
173
|
+
const current = runs[0]
|
|
174
|
+
ensure(
|
|
175
|
+
current &&
|
|
176
|
+
Number.isSafeInteger(current.id) &&
|
|
177
|
+
Number.isSafeInteger(current.run_attempt) &&
|
|
178
|
+
current.run_attempt > 0 &&
|
|
179
|
+
current.workflow_id === workflow.id &&
|
|
180
|
+
current.path === policy.workflow &&
|
|
181
|
+
current.head_sha === sourceSha &&
|
|
182
|
+
current.repository?.full_name === policy.repository &&
|
|
183
|
+
current.head_repository?.full_name === policy.repository,
|
|
184
|
+
'No trusted qualification run matches the exact source and workflow'
|
|
185
|
+
)
|
|
186
|
+
ensure(
|
|
187
|
+
current.status === 'completed' && current.conclusion === 'success',
|
|
188
|
+
'Latest matching qualification has not passed; older successes do not authorize rollout'
|
|
189
|
+
)
|
|
190
|
+
const jobs = pages(
|
|
191
|
+
api(
|
|
192
|
+
`repos/${policy.repository}/actions/runs/${current.id}/attempts/${current.run_attempt}/jobs?per_page=100`,
|
|
193
|
+
true
|
|
194
|
+
),
|
|
195
|
+
'jobs'
|
|
196
|
+
)
|
|
197
|
+
for (const name of policy.required_jobs) {
|
|
198
|
+
const matches = jobs.filter((job) => job.name === name)
|
|
199
|
+
ensure(
|
|
200
|
+
matches.length === 1 &&
|
|
201
|
+
matches[0].status === 'completed' &&
|
|
202
|
+
matches[0].conclusion === 'success',
|
|
203
|
+
`Required qualification job did not pass: ${name}`
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
const latest = api(`repos/${policy.repository}/actions/runs/${current.id}`)
|
|
207
|
+
ensure(
|
|
208
|
+
latest.run_attempt === current.run_attempt &&
|
|
209
|
+
latest.head_sha === sourceSha &&
|
|
210
|
+
latest.status === 'completed' &&
|
|
211
|
+
latest.conclusion === 'success',
|
|
212
|
+
'Qualification run changed during verification'
|
|
213
|
+
)
|
|
214
|
+
ensure(
|
|
215
|
+
git(['rev-parse', 'HEAD']) === sourceSha &&
|
|
216
|
+
git(['status', '--porcelain', '--untracked-files=all']) === '',
|
|
217
|
+
'Runtime source changed during eligibility verification'
|
|
218
|
+
)
|
|
219
|
+
ensure(
|
|
220
|
+
!lstatSync(path).isSymbolicLink() && hash(readFileSync(path)) === hash(bytes),
|
|
221
|
+
'Release policy changed during verification'
|
|
222
|
+
)
|
|
223
|
+
return {
|
|
224
|
+
schema_version: 1,
|
|
225
|
+
kind: 'code-foundry-fleet-eligibility',
|
|
226
|
+
status: 'passed',
|
|
227
|
+
repository: policy.repository,
|
|
228
|
+
tag: version,
|
|
229
|
+
source_sha: sourceSha,
|
|
230
|
+
policy_sha256: hash(bytes),
|
|
231
|
+
workflow_id: workflow.id,
|
|
232
|
+
run_id: current.id,
|
|
233
|
+
run_attempt: current.run_attempt,
|
|
234
|
+
required_jobs: policy.required_jobs,
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** Every public upgrade path, including dry run, passes the same opt-in guard.
|
|
238
|
+
* @template T @param {string} root @param {string} source @param {string} version
|
|
239
|
+
* @param {() => T} operation @param {Run} [run]
|
|
240
|
+
*/
|
|
241
|
+
export function guardedFleetUpgrade(root, source, version, operation, run) {
|
|
242
|
+
const eligibility = assertFleetEligibility(root, source, version, run)
|
|
243
|
+
if (eligibility) console.error(JSON.stringify(eligibility))
|
|
244
|
+
return operation()
|
|
245
|
+
}
|