ReviewOS

also looking at this

stacks/drone

feat: own runtime firmware candidates

#49
Merged chris wants to merge codex/runtime-candidate-ownership into main
24 files +1245 -92
tests/fixtures/updates/store-interruptions.tsmodified+62-20
Changes to tests/fixtures/updates/store-interruptions.ts
@@ -3,12 +3,13 @@ import { spawnSync } from 'node:child_process'
33import { mkdir, readFile, writeFile } from 'node:fs/promises'
44import { join, resolve } from 'node:path'
55import { enrollRuntimeTrust } from '../../../resources/functions/drone/updates/enrollment'
6import { createRuntimeTrustStore, inspectRuntimeTrustStore, recoverRuntimeTrustStore, refreshRuntimeTrustStore } from '../../../resources/functions/drone/updates/store'
6import { createRuntimeTrustStore, inspectRuntimeFirmwareCandidates, inspectRuntimeTrustStore, prepareRuntimeFirmwareUpdate, recoverRuntimeTrustStore, refreshRuntimeTrustStore, reserveRuntimeFirmwareCandidate, retireRuntimeFirmwareCandidate } from '../../../resources/functions/drone/updates/store'
7import type { RuntimeUpdateProgress } from '../../../resources/functions/drone/updates/store'
78import { platform, repository, target } from './repository'
89
910type Reference = Parameters<typeof inspectRuntimeTrustStore>[0]
10type Cut = 'input-committed' | 'metadata-committed'
11interface WorkerInput { reference: Reference, repository: string, cut: Cut }
11type Cut = RuntimeUpdateProgress['phase']
12interface WorkerInput { reference: Reference, repository: string, cut: Cut, output?: string, managed?: boolean, candidateId?: string }
1213const clock = { source: 'synthetic-independent-clock', read: () => new Date('2030-01-01T00:00:00Z') }
1314
1415async function worker(mode: string, file: string) {
@@ -23,27 +24,35 @@ async function worker(mode: string, file: string) {
2324 return
2425 }
2526 if (mode !== 'hold') throw new Error('Unknown interruption worker mode')
26 await refreshRuntimeTrustStore({ ...input.reference, repository: input.repository, clock, async onProgress(event) {
27 const options = { ...input.reference, repository: input.repository, clock, async onProgress(event: Readonly<RuntimeUpdateProgress>) {
2728 if (event.phase !== input.cut || event.ordinal !== (input.cut === 'input-committed' ? 1 : 0)) return
2829 console.log(JSON.stringify({ ready: true, phase: event.phase, ordinal: event.ordinal }))
2930 await Bun.stdin.text()
30 } })
31 } }
32 if (input.candidateId) await retireRuntimeFirmwareCandidate({ ...options, candidateId: input.candidateId })
33 else if (input.managed) await reserveRuntimeFirmwareCandidate(options)
34 else if (input.output) await prepareRuntimeFirmwareUpdate({ ...options, output: input.output })
35 else await refreshRuntimeTrustStore(options)
3136 throw new Error('Interruption worker unexpectedly finished')
3237}
3338
3439/** Keep generated signed repositories and actual host process outcomes for inspection. No power-loss claim. */
35export async function qualifyStoreInterruptions(directory: string) {
40async function qualifyInterruptions(directory: string, operation: 'refresh' | 'prepare' | 'reserve') {
3641 await mkdir(directory, { mode: 0o700 })
3742 const results = []
38 for (const cut of ['input-committed', 'metadata-committed'] as const) {
43 const managed = operation === 'reserve', preparation = operation !== 'refresh'
44 const cuts: Cut[] = managed ? ['candidate-reserved', 'image-written', 'candidate-completed', 'candidate-recorded', 'candidate-retired'] : preparation ? ['image-written', 'candidate-completed'] : ['input-committed', 'metadata-committed']
45 for (const cut of cuts) {
3946 const root = join(directory, cut); await mkdir(root, { mode: 0o700 })
4047 const source = join(root, 'repo'), fixture = await repository(source, { custom: { releaseSequence: 9 } })
4148 const enrollmentDirectory = join(root, 'enrollment')
4249 const enrolled = enrollRuntimeTrust({ target, platform, trustedRootSha256: fixture.rootSha256, minimumReleaseSequence: 7, trustedRoot: fixture.rootPath, output: enrollmentDirectory })
4350 const created = await createRuntimeTrustStore({ enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, output: join(root, 'store') })
4451 const reference = { enrollmentDirectory, enrollmentSha256: enrolled.enrollmentSha256, directory: join(root, 'store'), storeSha256: created.storeSha256 }
52 const prior = cut === 'candidate-retired' ? await reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock }) : null
4553 const file = join(root, 'worker.json')
46 await writeFile(file, JSON.stringify({ reference, repository: source, cut } satisfies WorkerInput), { mode: 0o600 })
54 let output = prior?.candidate?.directory ?? (!managed && preparation ? join(root, 'candidate') : undefined)
55 await writeFile(file, JSON.stringify({ reference, repository: source, cut, output: managed ? undefined : output, managed, candidateId: prior?.candidate?.id } satisfies WorkerInput), { mode: 0o600 })
4756 const child = Bun.spawn([process.execPath, import.meta.path, 'hold', file], { stdin: 'pipe', stdout: 'pipe', stderr: 'pipe' })
4857 const timer = setTimeout(() => child.kill('SIGKILL'), 15000)
4958 const reader = child.stdout.getReader(), decoder = new TextDecoder()
@@ -64,15 +73,42 @@ export async function qualifyStoreInterruptions(directory: string) {
6473 const exit = await child.exited, signal = child.signalCode
6574 assert.equal(signal, 'SIGKILL'); assert.notEqual(exit, 0)
6675 const interrupted = await inspectRuntimeTrustStore(reference)
67 assert.equal(interrupted.attempt?.phase, 'pending')
68 assert.equal(interrupted.generation, cut === 'input-committed' ? 0 : 1)
69 const recovered = await recoverRuntimeTrustStore(reference)
70 assert.equal(recovered.generation, 1); assert.equal(recovered.attempt?.phase, 'recovered')
71 assert.equal(recovered.releaseFloor, 7)
72 const fresh = await refreshRuntimeTrustStore({ ...reference, repository: source, clock })
73 assert.equal(fresh.generation, 3); assert.equal(fresh.releaseFloor, 9)
76 assert.equal(interrupted.attempt?.phase, cut === 'candidate-retired' ? 'verified' : 'pending')
77 assert.equal(interrupted.generation, preparation ? 3 : cut === 'input-committed' ? 0 : 1)
78 assert.equal(interrupted.releaseFloor, preparation ? 9 : 7)
79 const candidateBefore = managed ? await inspectRuntimeFirmwareCandidates(reference) : null
80 if (managed && cut !== 'candidate-retired') {
81 assert.equal(candidateBefore!.candidate!.phase, cut === 'candidate-recorded' ? 'prepared' : 'reserved')
82 assert.equal(candidateBefore!.candidate!.currentTrustValidated, false)
83 output = candidateBefore!.candidate!.directory
84 }
85 if (cut === 'candidate-retired') assert.equal(candidateBefore!.candidate, null)
86 const candidateStagePresent = output ? await Bun.file(join(output, 'stage.json')).exists() : false
87 if (output) {
88 if (cut !== 'candidate-reserved') assert.deepEqual(await readFile(join(output, 'image.bin')), fixture.image)
89 assert.equal(candidateStagePresent, ['candidate-completed', 'candidate-recorded', 'candidate-retired'].includes(cut))
90 }
91 const recovered = cut === 'candidate-retired' ? await inspectRuntimeTrustStore(reference) : await recoverRuntimeTrustStore(reference)
92 assert.equal(recovered.generation, preparation ? 3 : 1); assert.equal(recovered.attempt?.phase, cut === 'candidate-retired' ? 'verified' : 'recovered')
93 assert.equal(recovered.releaseFloor, preparation ? 9 : 7)
94 assert.equal(Object.hasOwn(recovered, 'candidate'), false)
95 if (managed) {
96 assert.deepEqual((await inspectRuntimeFirmwareCandidates(reference)).candidate, candidateBefore!.candidate)
97 if (candidateBefore!.candidate) await retireRuntimeFirmwareCandidate({ ...reference, candidateId: candidateBefore!.candidate.id })
98 }
99 const completed = managed ? await reserveRuntimeFirmwareCandidate({ ...reference, repository: source, clock }) : preparation ? await prepareRuntimeFirmwareUpdate({ ...reference, repository: source, clock, output: join(root, 'fresh-candidate') }) : await refreshRuntimeTrustStore({ ...reference, repository: source, clock })
100 const fresh = completed.schema === 'drone-runtime-prepared-image-v1' || completed.schema === 'drone-runtime-candidates-v1' ? completed.store : completed
101 if (completed.schema === 'drone-runtime-candidates-v1') {
102 assert.equal(completed.retainedCandidates, 2)
103 assert.equal(completed.candidate!.integrity, 'recorded-bytes-match')
104 assert.notEqual(completed.candidate!.id, candidateBefore!.candidate?.id ?? prior!.candidate!.id)
105 if (candidateStagePresent) assert.deepEqual(await readFile(join(output!, 'image.bin')), fixture.image)
106 }
107 assert.equal(fresh.generation >= interrupted.generation, true)
108 if (!preparation) assert.equal(fresh.generation, 3)
109 assert.equal(fresh.releaseFloor, 9)
74110 assert.equal(fresh.attempt?.phase, 'verified'); assert.equal(fresh.physicalDeploymentAllowed, false)
75 results.push({ cut, ready, killedExit: exit, killedSignal: signal, contenderExit: contender.status, contender: contender.stdout, interrupted, recovered, fresh })
111 results.push({ cut, ready, killedExit: exit, killedSignal: signal, contenderExit: contender.status, contender: contender.stdout, candidateStagePresent, candidateBefore, interrupted, recovered, fresh })
76112 }
77113 finally {
78114 clearTimeout(timer)
@@ -81,14 +117,20 @@ export async function qualifyStoreInterruptions(directory: string) {
81117 reader.releaseLock()
82118 await writeFile(join(root, 'worker.stdout'), stdout)
83119 await writeFile(join(root, 'worker.stderr'), await new Response(child.stderr).text())
84 await writeFile(join(directory, 'qualification.json'), JSON.stringify({ evidence: 'host-process-interruption-only', results, physicalDeploymentAllowed: false }, null, 2))
120 await writeFile(join(directory, 'qualification.json'), JSON.stringify({ evidence: 'host-process-interruption-only', operation, results, physicalDeploymentAllowed: false }, null, 2))
85121 }
86122 }
87 return { evidence: 'host-process-interruption-only', cuts: results.length, physicalDeploymentAllowed: false }
123 return { evidence: 'host-process-interruption-only', operation, cuts: results.length, physicalDeploymentAllowed: false }
88124}
89125
126export async function qualifyStoreInterruptions(directory: string) { return qualifyInterruptions(directory, 'refresh') }
127export async function qualifyPreparationInterruptions(directory: string) { return qualifyInterruptions(directory, 'prepare') }
128export async function qualifyCandidateInterruptions(directory: string) { return qualifyInterruptions(directory, 'reserve') }
129
90130if (import.meta.main) {
91 if (process.argv.length === 4) await worker(process.argv[2]!, process.argv[3]!)
131 if (process.argv.length === 4 && process.argv[2] === 'reserve') console.log(JSON.stringify(await qualifyCandidateInterruptions(resolve(process.argv[3]!))))
132 else if (process.argv.length === 4 && process.argv[2] === 'prepare') console.log(JSON.stringify(await qualifyPreparationInterruptions(resolve(process.argv[3]!))))
133 else if (process.argv.length === 4) await worker(process.argv[2]!, process.argv[3]!)
92134 else if (process.argv.length === 3) console.log(JSON.stringify(await qualifyStoreInterruptions(resolve(process.argv[2]!))))
93 else throw new Error('Use store-interruptions.ts NEW_DIRECTORY')
135 else throw new Error('Use store-interruptions.ts [prepare|reserve] NEW_DIRECTORY')
94136}