import assert from'node:assert/strict'; const u=(b,p)=>b.readBigUInt64LE(p),n=(b,p)=>Number(u(b,p)),W=b=>Array.from({length:b.length/8},(_,i)=>u(b,i*8)); export function inspectThreadMigrationGraph({data,cpu,stage,layout,ledger,symbols}){ assert.ok(stage>=10&&stage<=14);assert.ok(cpu>=0&&cpu<3);const e=W(data.OS_K2045_EVIDENCE),gw=W(data.OS_K2055_GRAPH.subarray(cpu*512,(cpu+1)*512)),g=gw.map(Number),r=W(data.OS_K2045_RECEIPTS.subarray(cpu*512,(cpu+1)*512)),raw=data['local'+cpu],base=g[0],id=(BigInt(cpu+1)<<32n)|1n; const {local:l,queue:q,context:c,thread:t,backend:b,guard:gd,lease:le}=layout; assert.equal(raw.length,l[0]);assert.equal(g[1],l[0]);assert.equal(BigInt(base),r[31]);assert.equal(BigInt(g[1]),r[32]);assert.ok(BigInt(base)>=e[33+cpu]+16n&&BigInt(base+raw.length)<=e[33+cpu]+131072n-16n); const absent=cpu===0&&[10,11,13].includes(stage)||cpu===1&&stage===13; const generation=cpu===0?(stage>=14?3n:stage>=12?2n:1n):cpu===1&&stage===14?2n:1n; const swapped=stage===14&&cpu<2; const order=absent?[1n,0n]:(cpu===0&&stage===12||swapped)?[1n,0n]:[0n,1n]; assert.deepEqual(gw.slice(28,32),[absent?1n:2n,absent?1n:2n,0n,BigInt(cpu+1)]);assert.ok(gw.slice(48).every(v=>v===0n));const span=(p,len)=>{assert.ok(Number.isSafeInteger(p)&&p>=base&&p+len<=base+raw.length,'private graph extent');return raw.subarray(p-base,p-base+len)},word=p=>u(span(p,8),0),byte=p=>span(p,1)[0],u32=p=>span(p,4).readUInt32LE(); const queue=base+l[3],binding=base+l[2],owner=base+l[1],backend=queue+q[1],bank=backend+b[14],cb=bank+c[1],table=g[8]; assert.deepEqual(g.slice(2,8),[backend,b[13],bank,c[0],cb,b[9]]);assert.equal(g[9],t[0]);assert.equal(g[3],b[13]);assert.equal(g[5],c[0]);assert.equal(g[7],b[9]);assert.equal(table,cb+b[10]+8,'RefCell value follows 64-bit borrow flag');assert.equal(word(cb+b[10]),0n,'no table loan survives checkpoint'); const launch=W(data.OS_K2045_LAUNCH.subarray(cpu*256,(cpu+1)*256)),shared=launch[9];assert.equal(word(owner+b[1]),shared);assert.equal(word(cb+b[11]),shared);assert.equal(u32(cb+b[12]),cpu+1);assert.ok(shared>=BigInt(symbols.OS_K2045_PRIMARY_STACK.address+4096)&&shared+560n=BigInt(symbols._text_start.address)&&pc=0&&slot<80);assert.equal(ledger.slots[slot].address,address);assert.equal(ledger.slots[slot].generation,generation);assert.equal(ledger.slots[slot].active,true);} const bytes=data['thread'+origin+'-'+i];assert.equal(bytes.length,5*4096);assert.ok(bytes.subarray(0,4096).every(x=>x===0));assert.equal(u(bytes,4096),0x4b313032304c4f57n);assert.equal(u(bytes,bytes.length-8),0x4b31303230484947n);assert.ok(bytes.subarray(4104,bytes.length-8).every(x=>x===0)); } return{cpu:cpu+1,localBytes:l[0],actualMembers:absent?1:2,actualContextPins:absent?1:2,actualThreadStacks:absent?1:2,actualLeases:absent?5:10,privateOnlineRetained:true}; }