import assert from 'node:assert/strict'; import {readFileSync, writeFileSync, copyFileSync, mkdtempSync} from 'node:fs'; import {createHash} from 'node:crypto'; import {join, dirname} from 'node:path'; import {fileURLToPath} from 'node:url'; import {createProfileReplay, inspectProfileClosure, inspectProfileProvenance} from './k2100-profile-closure-proof.mjs'; assert.equal(process.argv.length, 3, 'pass a completed duration-window proof report'); const proofPath = process.argv[2], read = p => JSON.parse(readFileSync(p)); const artifact = path => ({path, sha256: createHash('sha256').update(readFileSync(path)).digest('hex')}); const directory = mkdtempSync('reports/os-implementation/K2100-profile-closure-proof-'); copyFileSync(fileURLToPath(import.meta.url), join(directory, 'harness.mjs.txt')); copyFileSync('scripts/os-acceptance/k2100-profile-closure-proof.mjs', join(directory, 'proof.mjs.txt')); copyFileSync('scripts/os-acceptance/k2100-duration-window-proof.mjs', join(directory, 'ledger.mjs.txt')); const cache = new Map(); const checked = a => { if (!cache.has(a.path)) cache.set(a.path, artifact(a.path).sha256); assert.equal(cache.get(a.path), a.sha256, a.path); return readFileSync(a.path); }; try { const proof = read(proofPath); assert.equal(proof.status, 'DURATION_WINDOW_PHYSICAL_COMPONENT_PASSED'); assert.equal(proof.guestAccepted, false); assert.equal(proof.productAccepted, false); const review = JSON.parse(checked(proof.review)), candidate = JSON.parse(checked(review.candidate)); const physical = JSON.parse(checked(proof.physical)), diagnostic = JSON.parse(checked(proof.diagnostic)); assert.deepEqual(review.physical, proof.physical); assert.deepEqual(diagnostic.smpWorkers, proof.physical); assert.equal(diagnostic.status, 'GUEST_RETURNED'); const lifecycle = JSON.parse(checked(review.lifecycle)); assert.equal(lifecycle.status, 'RUNNER_PASSED'); assert.equal(lifecycle.allOwnedGuestsStopped, true); assert.equal(lifecycle.sourceInputsStable, true); assert.deepEqual(review.sourceBefore, review.sourceAfter); assert.deepEqual(proof.image, candidate.artifacts.image); const image = checked(proof.image), symbols = {}; for (const line of checked(candidate.artifacts.nm).toString().split('\n')) { const m = line.trim().match(/^([0-9a-f]+)\s+([0-9a-f]+)\s+\S\s+(\S+)$/i); if (m) symbols[m[3]] = {address: parseInt(m[1], 16), size: parseInt(m[2], 16)}; } assert.deepEqual(review.profileCaptureHarness, physical.profileCaptureHarness); const replay = await createProfileReplay({source: review.profileCaptureHarness, outputDirectory: directory, readArtifact: checked}); const context = {physical, cpuCount: review.cpuCount, memoryMiB: review.memoryMiB ?? 256, symbols, image, readArtifact: checked}; for (const original of physical.profileCheckpoints) await replay.replay({...context, original, stage: original.stage}); const closure = inspectProfileClosure(context); const provenanceContext = {...context, qmp: JSON.parse(checked(diagnostic.qmp)), timeline: JSON.parse(checked(diagnostic.timeline)), wholeRsp: JSON.parse(checked(diagnostic.wholeRsp))}; const provenance = inspectProfileProvenance(provenanceContext); const controls = []; async function rejectBytes(name, stage, key, mutate, check = 'both') { const original = physical.profileCheckpoints[stage - 1], a = original.dumps[key]; assert.ok(a, name); const source = checked(a), buffer = Buffer.from(source); mutate(buffer); assert.ok(!buffer.equals(source), name + ' changes actual bytes'); const reader = item => item.path === a.path ? buffer : checked(item); // Deduplicated files keep distinct paths: only the chosen observation is modified. if (check !== 'closure') await assert.rejects(() => replay.replay({...context, original, stage, readArtifact: reader}), undefined, name); if (check !== 'replay') assert.throws(() => inspectProfileClosure({...context, readArtifact: reader}), undefined, name); controls.push(name); } const put = (offset, value) => b => b.writeBigUInt64LE(BigInt(value), offset); await rejectBytes('primary stack not zero', 9, 'OS_K2045_PRIMARY_STACK', b => {b[8192] = 1;}); await rejectBytes('original primary SP lost', 9, 'OS_K2045_EVIDENCE', put(83 * 8, 1)); await rejectBytes('primary descriptor not restored', 9, 'OS_K2045_EVIDENCE', put(86 * 8, 0)); await rejectBytes('primary return incomplete', 9, 'OS_K2045_EVIDENCE', put(87 * 8, 0)); await rejectBytes('boot pin remains live', 6, 'OS_K2100_BOOT_OWNER', put(16, 1), 'replay'); await rejectBytes('returned total changed', 8, 'OS_K2045_EVIDENCE', b => b.writeBigUInt64LE(b.readBigUInt64LE(24) - 1n, 24)); await rejectBytes('central owner substituted', 8, 'authority', b => {b[6072] ^= 1;}, 'closure'); await rejectBytes('immutable memory plan changed', 8, 'authority', b => {b[0] ^= 1;}, 'closure'); if (review.cpuCount > 1) { await rejectBytes('mailbox retained bytes', 7, 'mailbox0', b => {b[16] = 1;}); await rejectBytes('executive retained bytes', 7, 'executive0', b => {b[4096] = 1;}); await rejectBytes('released mailbox changed', 8, 'mailbox0', b => {b[24] = 1;}); await rejectBytes('firmware CPU still online', 6, 'OS_K2045_CALLS', put((10 * 6 + 4) * 8, 0), 'closure'); await rejectBytes('firmware query absent', 6, 'OS_K2045_CALLS', put((10 * 6 + 5) * 8, 0), 'closure'); await rejectBytes('quiescent task still live', 5, 'OS_K2045_RECEIPTS', put(35 * 8, 1), 'replay'); await rejectBytes('offline receipt missing', 6, 'OS_K2045_RECEIPTS', put(0, 5), 'closure'); } for (const [name, mutate] of [ ['profile physical address substituted', p => {const id = p.physical.profileCheckpoints[0].captures[0].requestId; p.qmp.find(r => r.direction === 'tx' && r.message.id === id).message.arguments.val += 8;}], ['profile resume before acknowledgement', p => {const i = p.timeline.findIndex(r => r.stage === 21051 && r.kind === 'rsp'); const j = p.timeline.findIndex(r => r.stage === 21051 && r.execute === 'cont'); [p.timeline[i], p.timeline[j]] = [p.timeline[j], p.timeline[i]];}], ['profile unreported debugger write', p => {const first = p.physical.profileCheckpoints[0].rsp[0]; const i = p.wholeRsp.findIndex(r => r.id === first.id && r.direction === 'tx'); p.wholeRsp.splice(i + 1, 0, {direction: 'tx', id: 'INJECTED-RESULT-WRITE', payload: 'M40080000,1:01'});}] ]) { const copy = {...provenanceContext, qmp: structuredClone(provenanceContext.qmp), timeline: structuredClone(provenanceContext.timeline), wholeRsp: structuredClone(provenanceContext.wholeRsp)}; mutate(copy); assert.throws(() => inspectProfileProvenance(copy), undefined, name); controls.push(name); } const report = {status: 'PROFILE_CLOSURE_PHYSICAL_COMPONENT_PASSED', guestAccepted: false, productAccepted: false, scope: 'Nine existing native profile captures replayed from frozen assertions and bound to ordered physical QMP reads. Central live/free/reserved inventories, worker-page returns, fresh firmware-off receipts, mailbox/executive zero bytes and primary-stack return are checked. No new guest evidence is created. Worker stack zeroing and native control-flow semantics require the compiled implementation proof; all eighteen long profiles and consolidated milestone admission remain separate.', proof: artifact(proofPath), review: proof.review, physical: proof.physical, diagnostic: proof.diagnostic, image: proof.image, cpuCount: review.cpuCount, memoryMiB: review.memoryMiB ?? 256, route: review.route ?? 'direct', closure, provenance, rejectedControls: controls, rejectedControlCount: controls.length, replay: {source: review.profileCaptureHarness, generated: artifact(replay.path), guestEvidenceCreated: false}, harness: artifact(join(directory, 'harness.mjs.txt')), source: artifact(join(directory, 'proof.mjs.txt')), ledger: artifact(join(directory, 'ledger.mjs.txt')), checkedArtifacts: cache.size}; const path = join(directory, 'report.json'); writeFileSync(path, JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({status: report.status, report: path, closure, controls: controls.length})); } catch (error) { const path = join(directory, 'report.json'); writeFileSync(path, JSON.stringify({status: 'PROFILE_CLOSURE_PHYSICAL_COMPONENT_FAILED', guestAccepted: false, productAccepted: false, proof: artifact(proofPath), error: error.stack, harness: artifact(join(directory, 'harness.mjs.txt')), source: artifact(join(directory, 'proof.mjs.txt'))}, null, 2) + '\n'); console.error(path); throw error; }