import assert from 'node:assert/strict'; const words = b => Array.from({length: b.length / 8}, (_, i) => b.readBigUInt64LE(i * 8)); const word = (b, offset) => b.readBigUInt64LE(offset); const zero = xs => assert.ok(xs.every(v => v === 0n)); const asNumber = n => { const value = Number(n); assert.ok(Number.isSafeInteger(value)); return value; }; // Reconstruct the conserved inventory from the captured central owner. No // summary counters are used to create live leases or reservation records. export function inspectDurationLedger(raw, layout, memoryMiB) { const l = words(layout).map(asNumber); assert.equal(l.length, 24); assert.equal(raw.length, l[0]); assert.equal(l[4], 64); assert.equal(l[10], 80); assert.equal(l[16], 64); const generation = word(raw, l[17]), owner = raw.readUInt32LE(l[18]); assert.ok(owner > 0 && generation > 0n); const end = 0x40000000n + BigInt(memoryMiB) * 1048576n; const free = [], live = [], reserved = [], intervals = []; function region(a, b, kind) { assert.ok(a >= 0x40000000n && a < b && b <= end); assert.equal(a % 4096n, 0n); assert.equal(b % 4096n, 0n); intervals.push({a, b, kind}); return (b - a) / 4096n; } let freePages = 0n, reservedPages = 0n; const count = asNumber(word(raw, l[1] + l[3])); assert.ok(count > 0 && count <= l[4]); for (let i = 0; i < count; i++) { const at = l[1] + l[2] + i * l[23]; const a = word(raw, at + l[21]), b = word(raw, at + l[22]); if (i) assert.ok(a > free.at(-1)[1], 'canonical free intervals'); freePages += region(a, b, 'free'); free.push([a, b]); } for (let i = 0; i < l[10]; i++) { const at = l[5] + i * l[6], active = raw[at + l[9]]; assert.ok(active === 0 || active === 1); if (!active) continue; const address = word(raw, at + l[7]), issued = word(raw, at + l[8]); assert.ok(issued > 0n && issued <= generation); region(address, address + 4096n, 'live'); live.push([BigInt(i), address, issued]); } for (let i = 0; i < l[16]; i++) { const at = l[11] + i * l[12], active = raw[at + l[15]]; assert.ok(active === 0 || active === 1); if (!active) continue; const a = word(raw, at + l[13] + l[21]), b = word(raw, at + l[13] + l[22]); const issued = word(raw, at + l[14]); assert.ok(issued > 0n && issued <= generation); reservedPages += region(a, b, 'reserved'); reserved.push([BigInt(i), a, b, issued]); } intervals.sort((a, b) => a.a < b.a ? -1 : a.a > b.a ? 1 : 0); for (let i = 1; i < intervals.length; i++) assert.ok(intervals[i - 1].b <= intervals[i].a, 'central owners do not overlap'); return {owner, generation, free, live, reserved, freePages, reservedPages, livePages: BigInt(live.length), authorizedPages: freePages + reservedPages + BigInt(live.length)}; } export function inspectDurationClock(clock, {requireDuration = false} = {}) { assert.equal(clock.phase, 'paused'); assert.equal(clock.minimumRunningMilliseconds, 7_200_000); assert.ok(Array.isArray(clock.segments) && clock.segments.length > 0); let sum = 0, end = -1; for (const s of clock.segments) { for (const v of [s.start, s.end, s.milliseconds]) assert.ok(Number.isFinite(v) && v >= 0); assert.ok(s.start >= end && s.end >= s.start); assert.equal(s.milliseconds, s.end - s.start); sum += s.milliseconds; end = s.end; } assert.equal(clock.runningMilliseconds, sum); assert.ok(Number.isFinite(clock.wallMilliseconds) && clock.wallMilliseconds >= sum); assert.equal(clock.excludedMilliseconds, clock.wallMilliseconds - sum); assert.equal(clock.durationMet, sum >= 7_200_000); if (requireDuration) assert.equal(clock.durationMet, true, 'actual host running duration'); return sum; } // This checks additional physical invariants in the duration component. The // service frames, page tables, QMP/RSP provenance and compiled control flow are // independent proof obligations; this function does not accept a milestone. export function inspectDurationWindow({data, baseline, compiled, symbols, cpuCount, memoryMiB, window, previousCounter, previousLate = [0n, 0n], previousStaleRefusals = 0n, final = false, long = false}) { assert.ok([1, 2, 4].includes(cpuCount)); assert.ok(Number.isSafeInteger(window) && window > 0); const e = words(data.OS_K2045_EVIDENCE), summary = words(data.OS_K2100_WORKLOAD_SUMMARY); const monitor = words(data.OS_K2100_WORKLOAD_MONITOR), bindings = words(data.OS_K2100_WORKLOAD_BINDINGS); const completed = words(data.OS_K2100_WORKLOAD_PROGRESS), count = BigInt(cpuCount), windows = BigInt(window); assert.equal(summary.length, 32); assert.equal(monitor.length, 53); assert.deepEqual(summary.slice(0, 2), [2100n, count]); const first = words(baseline.OS_K2100_WORKLOAD_SUMMARY); assert.deepEqual(summary.slice(2, 7), first.slice(2, 7)); assert.ok(summary[2] > 0n && summary[3] > 0n && summary[3] <= 1_000_000_000n); assert.equal(summary[4], summary[2] + summary[3] * 7200n); assert.equal(summary[5], BigInt(symbols.OS_K2100_WORKLOAD_MONITOR.address)); assert.equal(summary[6], 424n); assert.equal(summary[7], windows); assert.equal(summary[12], final ? (long ? 1n : 2n) : 0n); zero(summary.slice(13)); assert.deepEqual(monitor.slice(0, 4), [e[6], summary[3], count, summary[2]]); assert.equal(monitor[4], summary[11]); if (final) assert.equal(monitor[4], previousCounter); else assert.ok(monitor[4] > previousCounter); assert.equal(monitor[49], windows); assert.equal(monitor[50], summary[10]); assert.equal(monitor[52], final && long ? 1n : 0n); assert.equal(monitor[48], 0n); const l = words(compiled.lifecycleLayout).map(asNumber), sharedLayout = words(compiled.sharedLayout).map(asNumber); assert.equal(data.lifecycle.length, l[0]); assert.deepEqual(data.lifecycle, baseline.lifecycle, 'retained online generations'); assert.equal(data.shared.length, sharedLayout[0]); assert.deepEqual(data.shared, baseline.shared, 'shared custody at window boundary'); assert.equal(word(data.shared, sharedLayout[1]), 0n, 'primary owns Shared'); assert.deepEqual(data.shared.subarray(sharedLayout[2] + sharedLayout[5]), data.lifecycle); const life = offset => word(data.lifecycle, offset); assert.deepEqual([life(l[1] + l[4]), life(l[1] + l[5]), life(l[1] + l[6]), life(l[1] + l[9])], [1n, 0x2100n, count, 1n]); for (let cpu = 0; cpu < 4; cpu++) { const b = bindings.slice(cpu * 4, cpu * 4 + 4), done = completed.slice(cpu * 6, cpu * 6 + 6); const state = monitor.slice(5 + cpu * 10, 15 + cpu * 10); if (cpu >= cpuCount) { zero(b); zero(done); zero(state); continue; } const record = l[2] + cpu * l[15]; assert.equal(life(record + l[18]), 4n); const generation = life(record + l[19]); assert.ok(generation > 0n); assert.deepEqual(b, [1n, 0x2100n, BigInt(cpu), generation]); assert.deepEqual(done, [3n, 2n, 1n, 1n, 1n, 1n].map(n => n * windows)); // Rust Binding is issuer, epoch, generation, cpu; the adapter's capture // array deliberately stores cpu before generation. assert.deepEqual(state, [b[0], b[1], b[3], b[2], ...done]); } const layout = compiled.authorityLayout, al = words(layout).map(asNumber); assert.deepEqual(e.slice(102, 126), words(layout)); const initial = inspectDurationLedger(baseline.authority, layout, memoryMiB); const actual = inspectDurationLedger(data.authority, layout, memoryMiB); assert.deepEqual(data.authority.subarray(0, al[5]), baseline.authority.subarray(0, al[5]), 'immutable central authority plan'); for (const key of ['owner', 'free', 'live', 'reserved', 'freePages', 'livePages', 'reservedPages', 'authorizedPages']) assert.deepEqual(actual[key], initial[key], 'retained central inventory ' + key); assert.ok(actual.generation > initial.generation, 'real temporary allocations occurred'); assert.deepEqual(monitor.slice(45, 48), [actual.authorizedPages, count, count]); const worker = words(data.OS_K2100_WORKER_EVIDENCE); assert.equal(actual.livePages, worker[3]); const records = words(data.OS_K2100_WORKER_RECORDS), leaseLayout = words(data.OS_K2045_LEASE_LAYOUT).map(asNumber); const backend = words(data.OS_K2045_BACKEND_LAYOUT).map(asNumber); for (let cpu = 1; cpu < cpuCount; cpu++) { const r = records.slice(cpu * 32, (cpu + 1) * 32), stack = data['stackOwner' + cpu]; for (let page = 0; page < 5; page++) { const offset = backend[4] + page * leaseLayout[0], address = word(stack, offset + leaseLayout[1]); const generation = word(stack, offset + leaseLayout[3]), slot = word(stack, offset + leaseLayout[4]); assert.equal(address, r[5] + BigInt(page) * 4096n); assert.equal(stack.readUInt32LE(offset + leaseLayout[2]), actual.owner); assert.ok(actual.live.some(row => row[0] === slot && row[1] === address && row[2] === generation), 'private worker lease is central live owner'); } } const stale = words(data.OS_K2100_WORKLOAD_STALE), process = words(data.OS_K2100_CPU_PROCESS).slice(0, 32); const service = words(data.OS_K2100_CPU_PROCESS_SUMMARY).slice(0, 16); assert.deepEqual(stale.slice(0, 6), [2100n, 0n, process[1], process[2], process[3], process[4]]); assert.ok(process[1] > 0n && process[3] > 0n && process[4] === process[3] + 1n); assert.ok(stale[6] > service[2] && stale[6] < process[19] && process[19] < service[3] && service[3] < monitor[4]); assert.deepEqual(stale.slice(7, 14), [actual.authorizedPages, process[10], actual.reservedPages, actual.authorizedPages - process[10] - actual.reservedPages, count, count, 1n]); assert.deepEqual(stale.slice(14, 18), process.slice(5, 9)); zero(stale.slice(18)); assert.ok(monitor[50] > 0n && monitor[50] <= count * windows); if (summary[8] === 0n) { assert.equal(summary[9], 0n); assert.equal(monitor[50], count * windows); assert.equal(monitor[51], stale[6]); assert.deepEqual(previousLate, [0n, 0n]); } else { assert.ok(long); assert.ok(summary[8] <= windows && summary[9] >= summary[4] && summary[9] < monitor[4]); assert.equal(monitor[51], summary[9]); const firstInWindow = (summary[8] - 1n) * count + 1n; assert.ok(monitor[50] >= firstInWindow && monitor[50] <= summary[8] * count); if (previousLate[0]) { assert.deepEqual(summary.slice(8, 10), previousLate, 'first late receipt stays pinned'); assert.equal(monitor[50], previousStaleRefusals, 'accepted refusal count stays pinned after first late receipt'); } else { assert.equal(summary[8], windows); const ordinal = asNumber(monitor[50] - (windows - 1n) * count); assert.ok(ordinal >= 1 && ordinal <= cpuCount); const cpu = ordinal === cpuCount ? 0 : ordinal; const p = words(data.OS_K2100_CPU_PROCESS).slice(cpu * 32, (cpu + 1) * 32); const s = words(data.OS_K2100_CPU_PROCESS_SUMMARY).slice(cpu * 16, (cpu + 1) * 16); assert.equal(p[16], BigInt(cpu)); assert.ok(p[1] > 0n && p[3] > 0n && p[4] === p[3] + 1n); assert.ok(summary[9] > s[2] && summary[9] < p[19] && p[19] < s[3], 'late receipt belongs to that CPU service interval'); if (cpu === 0) assert.equal(summary[9], stale[6]); } } const control = words(data.OS_K2100_HOST_DURATION_CONTROL)[0]; if (!long) assert.equal(control, 2n); else assert.ok(control === 0n || control === 1n); assert.equal(words(data.OS_K2100_WORKLOAD_WINDOW_ACK)[0], final ? windows : windows - 1n); if (final && long) { assert.ok(windows >= 3n); assert.equal(control, 1n); assert.ok(summary[8] > 0n && windows > summary[8]); assert.ok(monitor[4] >= summary[4] && monitor[4] > summary[9]); } return {counter: monitor[4], late: summary.slice(8, 10), ledger: actual, guestElapsedSeconds: Number(monitor[4] - monitor[3]) / Number(monitor[1]), windows: window, staleRefusals: asNumber(monitor[50])}; }