// Supersede the four local records only when all previously tested runtime // components are unchanged and the replacement compiled candidate is verified. import assert from 'node:assert/strict'; import {readFileSync,writeFileSync,mkdtempSync,renameSync,copyFileSync} from 'node:fs'; import {createHash} from 'node:crypto'; import {join} from 'node:path'; import {buildOsPlan} from '../os-roadmap.mjs'; import {validateDevelopment} from '../os-development.mjs'; import {frozenSnapshot} from './boot-contract-report.mjs'; const [newLocalPath,matrixPath]=process.argv.slice(2);assert.equal(process.argv.length,4); const read=p=>JSON.parse(readFileSync(p)); const artifact=path=>({path,sha256:createHash('sha256').update(readFileSync(path)).digest('hex')}); const checked=a=>{assert.deepEqual(artifact(a.path),a);return read(a.path);}; const verify=v=>{if(!v||typeof v!=='object')return;if(typeof v.path==='string'&&/^[a-f0-9]{64}$/.test(v.sha256??''))assert.equal(artifact(v.path).sha256,v.sha256);for(const x of Object.values(v))verify(x);}; const path='reports/os-implementation/progress.json',oldText=readFileSync(path,'utf8'),before=JSON.parse(oldText),plan=buildOsPlan();validateDevelopment(before,plan); const local=read(newLocalPath),matrix=read(matrixPath),record=artifact(newLocalPath),ids=['K2146','K2147','K2148','K2149']; assert.equal(local.status,'K2146_K2149_LOCAL_VALIDATED');assert.equal(matrix.status,'K2150_FEATURE_DEVELOPMENT_MATRIX_VALIDATED'); assert.deepEqual(matrix.local,record);assert.deepEqual(matrix.candidate,local.candidate);assert.deepEqual(matrix.spec,local.spec); verify(local);verify(matrix); const oldRecord=before.moves.find(m=>m.id===ids[0]).evidence,old=checked(oldRecord); assert.equal(old.status,local.status);assert.deepEqual(old.components,local.components);assert.deepEqual(old.tests,local.tests); assert.deepEqual(old.userBuild,local.userBuild);assert.deepEqual(old.checkerControls,local.checkerControls);assert.equal(old.quiesce.sha256,local.quiesce.sha256); assert.deepEqual(old.bindings.map(b=>[b.source.sha256,b.tested]),local.bindings.map(b=>[b.source.sha256,b.tested])); assert.deepEqual(frozenSnapshot(process.cwd()),local.frozenAfter);assert.deepEqual(local.frozenBefore,local.frozenAfter); assert.equal(before.moves.find(m=>m.id==='K2150').status,'PLANNED'); function rebind(input){ const result=structuredClone(input); assert.equal(result.moves.find(m=>m.id==='K2150').status,'PLANNED'); for(const id of ids){const move=result.moves.find(m=>m.id===id);assert.equal(move.status,'LOCAL_VALIDATED');assert.deepEqual(move.evidence,oldRecord);move.evidence=record;} for(const [i,m] of result.moves.entries())if(!ids.includes(m.id))assert.deepEqual(m,input.moves[i]); assert.deepEqual(result.counts,input.counts);validateDevelopment(result,plan);return result; } const controls=[]; for(const [name,mutate] of [ ['already-guest-accepted',p=>p.moves.find(m=>m.id==='K2150').status='DEVELOPMENT_GUEST_ACCEPTED'], ['wrong-local-status',p=>p.moves.find(m=>m.id==='K2147').status='PLANNED'], ['mismatched-old-evidence',p=>p.moves.find(m=>m.id==='K2148').evidence.sha256='0'.repeat(64)], ['missing-local-step',p=>p.moves=p.moves.filter(m=>m.id!=='K2149')], ['false-counts',p=>p.counts.locallyValidated++], ]){const input=structuredClone(before);mutate(input);assert.throws(()=>rebind(input),name);controls.push({name,rejected:true});} const after=rebind(before);after.generatedAt=new Date().toISOString(); const directory=mkdtempSync('reports/os-implementation/K2150-local-evidence-rebind-'); copyFileSync(new URL(import.meta.url),join(directory,'harness.mjs.txt')); writeFileSync(join(directory,'previous-progress.json'),oldText);writeFileSync(join(directory,'current-progress.json'),JSON.stringify(after,null,2)+'\n'); assert.equal(readFileSync(path,'utf8'),oldText,'concurrent progress write'); writeFileSync(path+'.rebind-tmp',JSON.stringify(after,null,2)+'\n');renameSync(path+'.rebind-tmp',path); const report={status:'K2150_LOCAL_EVIDENCE_REBIND_PASSED',guestAccepted:false,productAccepted:false,moves:ids,oldLocal:oldRecord,newLocal:record,physicalMatrix:artifact(matrixPath),runtimeComponentsUnchanged:true,controls,countsUnchanged:true, previous:artifact(join(directory,'previous-progress.json')),current:artifact(join(directory,'current-progress.json')),harness:artifact(join(directory,'harness.mjs.txt'))}; writeFileSync(join(directory,'report.json'),JSON.stringify(report,null,2)+'\n'); console.log(JSON.stringify({status:report.status,report:join(directory,'report.json'),negativeControls:controls.length}));