import assert from 'node:assert/strict'; import {spawn} from 'node:child_process'; // Calendar corrections are diagnostic only. The independent native heartbeat // uses mach_continuous_time, which includes suspend time. Neither clock grants // duration credit or changes the existing two-hour running/guest gates. export class HostContinuity { constructor({maximumGapMilliseconds = 30000, monotonic = () => performance.now(), wall = () => Date.now()} = {}) { assert.ok(Number.isFinite(maximumGapMilliseconds) && maximumGapMilliseconds > 0); Object.assign(this, {maximumGapMilliseconds, monotonic, wall, samples: 0, largestMonotonicGap: 0, largestWallGap: 0, wallRegressions: 0, continuousSamples: 0, largestContinuousGap: 0, failure: null}); this.sample(); } reject(reason, details = {}) {this.failure ??= {reason, ...details};} sample() { const current = {monotonic: this.monotonic(), wall: this.wall()}; if (!Number.isFinite(current.monotonic)) this.reject('invalid-monotonic-clock', {current}); if (this.previous) { const monotonicGap = current.monotonic - this.previous.monotonic, wallGap = current.wall - this.previous.wall; this.largestMonotonicGap = Math.max(this.largestMonotonicGap, monotonicGap); this.largestWallGap = Math.max(this.largestWallGap, wallGap); if (wallGap < 0) this.wallRegressions++; if (!Number.isFinite(monotonicGap) || monotonicGap < 0 || monotonicGap > this.maximumGapMilliseconds) this.reject('runner-sampling-gap', {previous: this.previous, current, monotonicGap, wallGap}); } if (this.continuous && current.monotonic - this.continuous.receivedAt > this.maximumGapMilliseconds) this.reject('native-heartbeat-missing', {last: this.continuous, current}); this.previous = current; this.samples++; return this.snapshot(); } observeContinuous(sequence, milliseconds) { this.sample(); if (!Number.isSafeInteger(sequence) || sequence !== this.continuousSamples + 1 || !Number.isFinite(milliseconds) || milliseconds < 0) return this.reject('invalid-native-heartbeat', {sequence, milliseconds}); if (this.continuous) { const gap = milliseconds - this.continuous.milliseconds; this.largestContinuousGap = Math.max(this.largestContinuousGap, gap); if (gap <= 0 || gap > this.maximumGapMilliseconds) this.reject('continuous-clock-gap', {previous: this.continuous, sequence, milliseconds, gap}); } this.continuous = {sequence, milliseconds, receivedAt: this.previous.monotonic}; this.continuousSamples++; } assertHealthy() { this.sample(); if (!this.continuousSamples) this.reject('native-clock-not-ready'); if (this.failure) { const error = new Error('HOST_EXECUTION_INTERRUPTED: ' + this.failure.reason + '; this run cannot be accepted'); error.code = 'HOST_EXECUTION_INTERRUPTED'; throw error; } } snapshot() { return {schema: 'turkos-host-continuity/v2', clockSource: 'mach_continuous_time', status: this.failure ? 'HOST_EXECUTION_INTERRUPTED' : 'HOST_EXECUTION_CONTINUITY_PASSED', guestAccepted: false, productAccepted: false, maximumGapMilliseconds: this.maximumGapMilliseconds, samples: this.samples, largestMonotonicGap: this.largestMonotonicGap, largestWallGap: this.largestWallGap, wallRegressions: this.wallRegressions, continuousSamples: this.continuousSamples, largestContinuousGap: this.largestContinuousGap, lastContinuous: this.continuous ?? null, failure: this.failure}; } } export async function createHostContinuity({command, ...options}) { assert.ok(typeof command === 'string' && command.length > 0); const guard = new HostContinuity(options), child = spawn(command, [], {stdio: ['ignore', 'pipe', 'pipe']}); let closing = false, pending = '', ready; const first = new Promise(resolve => {ready = resolve;}); const exited = new Promise(resolve => { child.once('error', error => {guard.reject('native-clock-spawn-failed', {message: error.message}); ready(); resolve();}); child.once('exit', (code, signal) => {if (!closing) guard.reject('native-clock-exited', {code, signal}); ready(); resolve();}); }); child.stderr.on('data', data => guard.reject('native-clock-stderr', {message: String(data).slice(0, 1000)})); child.stdout.on('data', data => { if (closing) return; pending += data; if (pending.length > 65536) {guard.reject('native-clock-output-overflow'); ready(); return;} let newline; while ((newline = pending.indexOf('\n')) >= 0) { const line = pending.slice(0, newline); pending = pending.slice(newline + 1); const match = /^(\d+) (\d+\.\d+)$/.exec(line); if (!match) guard.reject('invalid-native-clock-output', {line: line.slice(0, 100)}); else guard.observeContinuous(Number(match[1]), Number(match[2])); ready(); } }); guard.close = async () => {closing = true; child.kill('SIGTERM'); await exited;}; const timer = setTimeout(() => {guard.reject('native-clock-startup-timeout'); ready();}, 5000); await first; clearTimeout(timer); try {guard.assertHealthy();} catch (error) {await guard.close(); throw error;} return guard; }