import assert from'node:assert/strict'; const u=(b,p)=>b.readBigUInt64LE(p),W=b=>Array.from({length:b.length/8},(_,i)=>u(b,i*8)); function regions(raw,l){const count=Number(u(raw,l[1]+l[3]));assert.ok(count>0&&count<=l[4]);return Array.from({length:count},(_,i)=>[u(raw,l[1]+i*16),u(raw,l[1]+i*16+8)]);} function writeRegions(raw,l,rs){assert.ok(rs.length>0&&rs.length<=l[4]);raw.fill(0,l[1],l[1]+l[3]+8);rs.forEach(([a,b],i)=>{assert.ok(ars[i-1][1]);raw.writeBigUInt64LE(a,l[1]+i*16);raw.writeBigUInt64LE(b,l[1]+i*16+8)});raw.writeBigUInt64LE(BigInt(rs.length),l[1]+l[3]);} function allocate(raw,l,address,generation){const rs=regions(raw,l);assert.equal(generation,u(raw,l[17])+1n);const index=rs.findIndex(([a,b])=>address>=a&&address+4096n<=b);assert.ok(index>=0);const[a,b]=rs[index],next=[...rs.slice(0,index),...(a=0);const at=l[5]+slot*l[6];raw.writeBigUInt64LE(address,at+l[7]);raw.writeBigUInt64LE(generation,at+l[8]);raw[at+l[9]]=1;raw.writeBigUInt64LE(generation,l[17]);return slot;} function release(raw,l,slot,address,generation){const at=l[5]+slot*l[6];assert.equal(raw[at+l[9]],1);assert.equal(u(raw,at+l[7]),address);assert.equal(u(raw,at+l[8]),generation);const rs=[...regions(raw,l),[address,address+4096n]].sort((a,b)=>a[0]=last[1]);if(a===last[1]){last[1]=b;continue}}merged.push([a,b]);}writeRegions(raw,l,merged);raw[at+l[9]]=0;} export function frameServiceInventory(base,snapshots,compiled){const l=W(compiled.OS_K1000_AUTHORITY_LAYOUT_EXPECTED).map(Number),initial=base[3].authority;assert.equal(l[6],24);assert.equal(l[9],16);assert.equal(initial.length,6080);const generation=u(initial,l[17]),owner=initial.readUInt32LE(l[18]),work=Buffer.from(initial),frames=[[],[]],slots=[]; for(let round=0;round<2;round++){const records=snapshots[round===0?3:8].OS_K2075_USE;for(let cpu=0;cpu<3;cpu++){const row=W(records.subarray((round*3+cpu)*128,(round*3+cpu+1)*128)),address=row[2],g=row[3];assert.equal(g,generation+BigInt(round*3+cpu+1));assert.equal(row[4],BigInt(round*3+cpu+1));if(round)assert.equal(address,frames[0][cpu].address);const slot=allocate(work,l,address,g);if(round)assert.equal(slot,slots[cpu]);else slots.push(slot);frames[round].push({address,generation:g,slot,owner,serial:BigInt(round*3+cpu+1)});}for(const f of frames[round])release(work,l,f.slot,f.address,f.generation);} const batchCounts=[0,0,1,1,2,3,3,4,4,4,5,6];for(let index=0;index<12;index++){const expected=Buffer.from(initial),n=batchCounts[index];if(n>=1)for(const f of frames[0])assert.equal(allocate(expected,l,f.address,f.generation),f.slot);if(n>=3)for(const f of frames[0])release(expected,l,f.slot,f.address,f.generation);if(n>=4)for(const f of frames[1])assert.equal(allocate(expected,l,f.address,f.generation),f.slot);if(n>=5)for(const f of frames[1])release(expected,l,f.slot,f.address,f.generation); const actual=snapshots[index].authority;assert.equal(actual.length,expected.length);if(n>=1)for(const slot of slots){const at=l[5]+slot*24;actual.subarray(at+17,at+24).copy(expected,at+17);}assert.deepEqual(actual,expected,'exact central allocator transition '+(index+60)); } // Parent milestones allocate no additional pages during their queue phase. // Validate the new inactive records and generation before projecting only // those proven historical fields out of the parent's restoration check. const parent=base.map((data,index)=>{if(index<4)return data;const actual=data.authority,copy=Buffer.from(actual);assert.equal(u(actual,l[17]),generation+6n);for(let cpu=0;cpu<3;cpu++){const f=frames[1][cpu],at=l[5]+f.slot*24;assert.equal(u(actual,at),f.address);assert.equal(u(actual,at+8),f.generation);assert.equal(actual[at+16],0);initial.subarray(at,at+24).copy(copy,at);}copy.writeBigUInt64LE(generation,l[17]);return{...data,authority:copy};}); return{layout:l,initial,generation,owner,frames,slots,parent,batchCounts};} export function inspectFrameLease(raw,frame,inventory){const f=frame;assert.equal(raw.length,32);assert.equal(u(raw,0),f.address);assert.equal(u(raw,8),f.generation);assert.equal(u(raw,16),BigInt(f.slot));assert.equal(raw.readUInt32LE(24),inventory.owner);}