import assert from 'node:assert/strict'; import {copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync} from 'node:fs'; import {spawnSync} from 'node:child_process'; import {createHash} from 'node:crypto'; import {join, relative, resolve} from 'node:path'; assert.equal(process.argv.length, 3, 'usage: node virtio-backpressure-local.mjs '); const root = process.cwd(); const predecessorPath = resolve(process.argv[2]); const predecessor = JSON.parse(readFileSync(predecessorPath, 'utf8')); assert.equal(predecessor.status, 'K1495_VIRTIO_COMPLETIONS_DEVELOPMENT_MATRIX_VALIDATED'); const dir = mkdtempSync(join(root, 'reports/os-implementation/K1500-virtio-backpressure-local-')); const sha = path => createHash('sha256').update(readFileSync(path)).digest('hex'); const artifact = path => ({path: relative(root, path), sha256: sha(path)}); const copy = (source, target) => { mkdirSync(join(target, '..'), {recursive: true}); copyFileSync(join(root, source), target); return artifact(target); }; const sourceRoot = join(dir, 'source'); const model = join(sourceRoot, 'os-virtio-backpressure/src/lib.rs'); const fixture = join(sourceRoot, 'kernel/tests/os/virtio_backpressure.rs'); const noStdRoot = join(sourceRoot, 'no-std-root.rs'); mkdirSync(sourceRoot, {recursive: true}); writeFileSync(noStdRoot, '#![no_std]\n#[path = "os-virtio-backpressure/src/lib.rs"]\npub mod virtio_backpressure;\n'); const sources = { model: copy('os-virtio-backpressure/src/lib.rs', model), fixture: copy('kernel/tests/os/virtio_backpressure.rs', fixture), contract: copy('kernel/VIRTIO_BACKPRESSURE_CONTRACT.md', join(sourceRoot, 'kernel/VIRTIO_BACKPRESSURE_CONTRACT.md')), noStdRoot: artifact(noStdRoot), completionReference: copy('os-virtio-completions/src/lib.rs', join(sourceRoot, 'os-virtio-completions/src/lib.rs')), ringReference: copy('kernel/src/virtio_ring.rs', join(sourceRoot, 'kernel/src/virtio_ring.rs')), harness: artifact(join(root, 'scripts/os-acceptance/virtio-backpressure-local.mjs')), }; const run = (name, program, args, expected) => { const result = spawnSync(program, args, {cwd: root, encoding: 'utf8'}); const log = join(dir, `${name}.log`); writeFileSync(log, `${result.stdout ?? ''}${result.stderr ?? ''}`); assert.equal(result.status, expected, `${name}: expected exit ${expected}, observed ${result.status}; ${artifact(log).path}`); return {exitCode: result.status, log: artifact(log)}; }; const binary = join(dir, 'virtio-backpressure-tests'); const tests = { compile: run('host-compile', 'rustc', ['--edition=2024', '--test', fixture, '-o', binary], 0), green: run('host-tests', binary, [], 0), target: run('aarch64-no-std-check', 'rustc', ['--edition=2024', '--crate-type=lib', '--target=aarch64-unknown-none', '--emit=metadata', noStdRoot, '-o', join(dir, 'model.rmeta')], 0), }; const greenText = readFileSync(join(dir, 'host-tests.log'), 'utf8'); assert.match(greenText, /os_virtqueue_backpressure_limit_and_retry \.\.\. ok/); assert.match(greenText, /os_virtqueue_backpressure_reject_and_reset \.\.\. ok/); assert.match(greenText, /2 passed; 0 failed/); const mutantRoot = join(dir, 'mutant'); const mutantModel = join(mutantRoot, 'os-virtio-backpressure/src/lib.rs'); const mutantFixture = join(mutantRoot, 'kernel/tests/os/virtio_backpressure.rs'); copy('kernel/tests/os/virtio_backpressure.rs', mutantFixture); mkdirSync(join(mutantModel, '..'), {recursive: true}); const original = readFileSync(model, 'utf8'); const guard = 'if descriptors > self.capacity - self.used_descriptors {'; assert.equal(original.split(guard).length, 2, 'descriptor capacity guard must exist once'); writeFileSync(mutantModel, original.replace(guard, 'if false {')); const mutant = artifact(mutantModel); const mutantBinary = join(dir, 'virtio-descriptors-mutant-tests'); tests.mutantCompile = run('mutant-compile', 'rustc', ['--edition=2024', '--test', mutantFixture, '-o', mutantBinary], 0); tests.red = run('mutant-tests', mutantBinary, [], 101); const redText = readFileSync(join(dir, 'mutant-tests.log'), 'utf8'); assert.match(redText, /os_virtqueue_backpressure_reject_and_reset \.\.\. FAILED/); const report = { schema: 'turkos-k1500-virtio-backpressure-local/v1', status: 'K1496_K1499_LOCAL_VALIDATED', guestAccepted: false, productAccepted: false, predecessor: artifact(predecessorPath), sources, mutant, tests: {...tests, greenCount: 2, mutationRejected: true}, scope: 'The bounded descriptor budget returns EAGAIN without changing live claims when full, preserves existing work, frees only completed heads, and allows retry after progress or retirement after observed reset. Two host tests, AArch64 no_std compilation, and a removed-capacity-guard mutant are recorded. Physical queue backpressure remains for K1500.'}; const reportPath = join(dir, 'report.json'); writeFileSync(reportPath, JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({status: report.status, report: relative(root, reportPath), tests: report.tests.greenCount, mutationRejected: report.tests.mutationRejected}));