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-ordering-local.mjs '); const root = process.cwd(); const predecessorPath = resolve(process.argv[2]); const predecessor = JSON.parse(readFileSync(predecessorPath, 'utf8')); assert.equal(predecessor.status, 'K1665_TURKFS_JOURNAL_FORMAT_DEVELOPMENT_MATRIX_VALIDATED'); assert.equal(predecessor.guestAccepted, true); const directory = mkdtempSync(join(root, 'reports/os-implementation/K1670-turkfs-ordering-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-journal/src/ordering.rs'); const fixture = join(source, 'kernel/tests/os/turkfs_journal_ordering.rs'); const sources = { model: copy('os-turkfs-journal/src/ordering.rs', model), fixture: copy('kernel/tests/os/turkfs_journal_ordering.rs', fixture), contract: copy('kernel/TURKFS_JOURNAL_ORDERING_CONTRACT.md', join(source, 'kernel/TURKFS_JOURNAL_ORDERING_CONTRACT.md')), harness: artifact(join(root, 'scripts/os-acceptance/turkfs-ordering-local.mjs')), }; const wireNames = ['turkfs-journal-v1.bin', 'turkfs-journal-bad-order.bin', 'turkfs-journal-duplicate-block.bin', 'turkfs-journal-duplicate-kind.bin']; wireNames.forEach((name, i) => { sources[`wire${i}`] = copy( `kernel/tests/os/fixtures/${name}`, join(source, `kernel/tests/os/fixtures/${name}`)); }); 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-ordering-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_journal_ordering_positive \.\.\. ok/); assert.match(greenText, /os_turkfs_journal_ordering_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-journal/src/ordering.rs"]\npub mod journal;\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-journal/src/ordering.rs'); const mutantFixture = join(mutantRoot, 'kernel/tests/os/turkfs_journal_ordering.rs'); copy('kernel/tests/os/turkfs_journal_ordering.rs', mutantFixture); mkdirSync(dirname(mutantModel), {recursive: true}); for (const name of wireNames) copy(`kernel/tests/os/fixtures/${name}`, join(mutantRoot, `kernel/tests/os/fixtures/${name}`)); const original = readFileSync(model, 'utf8'); const guard = 'self.state != State::CommitFlushed'; assert.equal(original.split(guard).length, 2, 'publication after commit flush must occur once'); writeFileSync(mutantModel, original.replace(guard, 'false')); const mutant = artifact(mutantModel); const mutantBinary = join(directory, 'turkfs-ordering-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_journal_ordering_negative \.\.\. FAILED/); const report = { schema: 'turkos-k1670-turkfs-ordering-local/v1', status: 'K1666_K1669_LOCAL_VALIDATED', guestAccepted: false, productAccepted: false, predecessor: artifact(predecessorPath), sources, mutant, tests: {...tests, greenCount: 2, mutationRejected: true}, scope: 'A bounded writer issues data OUT/FLUSH, log OUT/FLUSH and commit OUT/FLUSH before publication. Two host tests inject failure at all six I/O boundaries and refuse premature or failed publication. An omitted-publication-guard mutant fails. AArch64 no_std compilation is recorded. Real disposable-disk writes and independent device trace remain for K1670; physical durability and product acceptance stay open.' }; 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}));