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 {dirname, join, relative, resolve} from 'node:path'; assert.equal(process.argv.length, 3, 'usage: node turkfs-allocation-local.mjs '); const root = process.cwd(); const predecessorPath = resolve(process.argv[2]); const predecessor = JSON.parse(readFileSync(predecessorPath, 'utf8')); assert.equal(predecessor.status, 'K1620_TURKFS_DIRECTORY_DEVELOPMENT_MATRIX_VALIDATED'); assert.equal(predecessor.guestAccepted, true); const directory = mkdtempSync(join(root, 'reports/os-implementation/K1625-turkfs-allocation-local-')); const sha = path => createHash('sha256').update(readFileSync(path)).digest('hex'); const artifact = path => ({path: relative(root, path), sha256: sha(path)}); const copy = (path, target) => { mkdirSync(dirname(target), {recursive: true}); copyFileSync(join(root, path), target); return artifact(target); }; const source = join(directory, 'source'); const model = join(source, 'os-turkfs-allocation/src/lib.rs'); const fixture = join(source, 'kernel/tests/os/turkfs_allocation.rs'); const sources = { model: copy('os-turkfs-allocation/src/lib.rs', model), fixture: copy('kernel/tests/os/turkfs_allocation.rs', fixture), contract: copy('kernel/TURKFS_BITMAP_OWNERSHIP_CONTRACT.md', join(source, 'kernel/TURKFS_BITMAP_OWNERSHIP_CONTRACT.md')), harness: artifact(join(root, 'scripts/os-acceptance/turkfs-allocation-local.mjs')), }; const run = (name, program, args, expected) => { const result = spawnSync(program, args, {cwd: root, encoding: 'utf8'}); const log = join(directory, `${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(directory, 'turkfs-allocation-tests'); const tests = { compile: run('host-compile', 'rustc', ['--edition=2024', '--test', fixture, '-o', binary], 0), green: run('host-tests', binary, [], 0), }; const greenText = readFileSync(join(directory, 'host-tests.log'), 'utf8'); assert.match(greenText, /os_turkfs_allocation_bitmap_positive \.\.\. ok/); assert.match(greenText, /os_turkfs_allocation_bitmap_negative \.\.\. ok/); assert.match(greenText, /2 passed; 0 failed/); const noStdRoot = join(source, 'no-std-root.rs'); writeFileSync(noStdRoot, '#![no_std]\n#[path = "os-turkfs-allocation/src/lib.rs"]\npub mod allocation;\n'); sources.noStdRoot = artifact(noStdRoot); tests.target = run('aarch64-no-std-check', 'rustc', ['--edition=2024', '--crate-type=lib', '--target=aarch64-unknown-none', '--emit=metadata', noStdRoot, '-o', join(directory, 'model.rmeta')], 0); const mutantRoot = join(directory, 'mutant'); const mutantModel = join(mutantRoot, 'os-turkfs-allocation/src/lib.rs'); const mutantFixture = join(mutantRoot, 'kernel/tests/os/turkfs_allocation.rs'); copy('kernel/tests/os/turkfs_allocation.rs', mutantFixture); mkdirSync(dirname(mutantModel), {recursive: true}); const original = readFileSync(model, 'utf8'); const guard = 'if allocated & reserved != reserved { return Err(Error::ReservedFree); }'; assert.equal(original.split(guard).length, 2, 'reserved-block guard must occur once'); writeFileSync(mutantModel, original.replace(guard, 'if false { return Err(Error::ReservedFree); }')); const mutant = artifact(mutantModel); const mutantBinary = join(directory, 'turkfs-allocation-mutant-tests'); tests.mutantCompile = run('mutant-compile', 'rustc', ['--edition=2024', '--test', mutantFixture, '-o', mutantBinary], 0); tests.red = run('mutant-tests', mutantBinary, [], 101); assert.match(readFileSync(join(directory, 'mutant-tests.log'), 'utf8'), /os_turkfs_allocation_bitmap_negative \.\.\. FAILED/); const report = { schema: 'turkos-k1625-turkfs-allocation-local/v1', status: 'K1621_K1624_LOCAL_VALIDATED', guestAccepted: false, productAccepted: false, predecessor: artifact(predecessorPath), sources, mutant, tests: {...tests, greenCount: 2, mutationRejected: true}, scope: 'A bounded block bitmap validates reserved metadata bits before publication, allocates the last free block, refuses no-space without state change, and protects reserved blocks from release. Two host tests, AArch64 no_std compilation, and a red omitted-reservation-guard mutant are recorded. Real on-disk metadata and six guest profiles remain for K1625.' }; const reportPath = join(directory, '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}));