mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-23 00:05:27 +00:00
test: exercise native FIFO verification in bounded child processes
This commit is contained in:
2 files changed
+71
-33
No files matched your search
@@ -0,0 +1,60 @@
|
||||
'use strict';
|
||||
|
||||
// Run potentially blocking FIFO races in a disposable process. The parent owns
|
||||
// the timeout, so a missing nonblocking flag fails instead of hanging the suite.
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { spawnSync } = require('child_process');
|
||||
const { createStateStore } = require('../../../scripts/lib/state-store');
|
||||
const { claimWorkItem } = require('../../../scripts/lib/control-pane/work-item-mutations');
|
||||
const { MATRIX_BLOCK_START, MATRIX_BLOCK_END, renderMarkdownTable } = require('../../../scripts/lib/harness-adapter-compliance');
|
||||
|
||||
async function run() {
|
||||
const [, , requestedRoot, scenario] = process.argv;
|
||||
assert.ok(['fifo', 'replace'].includes(scenario));
|
||||
fs.mkdirSync(requestedRoot, { recursive: true });
|
||||
const root = fs.realpathSync(requestedRoot);
|
||||
const artifact = path.join(root, 'matrix.md');
|
||||
const store = await createStateStore({ dbPath: path.join(root, 'state.db') });
|
||||
const lstat = fs.lstatSync;
|
||||
let replaced = false;
|
||||
const makeFifo = () => {
|
||||
const result = spawnSync('mkfifo', [artifact], { encoding: 'utf8' });
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
};
|
||||
try {
|
||||
store.upsertSession({ id: 'fifo-session', adapterId: 'test', harness: 'test', state: 'active', snapshot: {} });
|
||||
store.upsertWorkItem({
|
||||
id: 'fifo-task', source: 'manual', title: 'Verify FIFO boundary', status: 'open', repoRoot: root,
|
||||
metadata: { verification: { contract: { verifier: 'harness-adapter-documentation/v1', artifact: 'matrix.md' } } },
|
||||
});
|
||||
claimWorkItem(store, { id: 'fifo-task', owner: 'agent', assigneeKind: 'agent', sessionId: 'fifo-session' });
|
||||
if (scenario === 'fifo') {
|
||||
makeFifo();
|
||||
} else {
|
||||
fs.writeFileSync(artifact, `${MATRIX_BLOCK_START}\n${renderMarkdownTable()}\n${MATRIX_BLOCK_END}\n`);
|
||||
fs.lstatSync = function (filename, ...args) {
|
||||
const stat = lstat.call(fs, filename, ...args);
|
||||
if (!replaced && filename === artifact && stat.isFile()) {
|
||||
fs.unlinkSync(artifact);
|
||||
makeFifo();
|
||||
replaced = true;
|
||||
}
|
||||
return stat;
|
||||
};
|
||||
}
|
||||
const result = store.verifyWorkItem('fifo-task');
|
||||
assert.strictEqual(result.attempt.outcome, 'unknown');
|
||||
if (scenario === 'replace') assert.strictEqual(replaced, true, 'the artifact race must actually run');
|
||||
process.stdout.write(JSON.stringify({ outcome: result.attempt.outcome, replaced }));
|
||||
} finally {
|
||||
fs.lstatSync = lstat;
|
||||
store.close();
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
console.error(error.stack || error.message);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -224,41 +224,19 @@ async function run() {
|
||||
assert.strictEqual(store.verifyWorkItem('docs-task').verified, true);
|
||||
});
|
||||
|
||||
await test('FIFO artifacts and regular files replaced by FIFOs never block verification', ({ root, store, register, claim, artifact }) => {
|
||||
await test('FIFO artifacts and regular files replaced by FIFOs never block verification', ({ root }) => {
|
||||
if (process.platform === 'win32') return;
|
||||
register(); claim();
|
||||
const makeFifo = () => {
|
||||
const result = spawnSync('mkfifo', [artifact], { encoding: 'utf8' });
|
||||
const worker = path.join(__dirname, 'helpers', 'work-item-fifo-worker.js');
|
||||
for (const scenario of ['fifo', 'replace']) {
|
||||
const result = spawnSync(process.execPath, [worker, path.join(root, scenario), scenario], {
|
||||
encoding: 'utf8', timeout: 5000,
|
||||
});
|
||||
assert.ifError(result.error);
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
};
|
||||
makeFifo();
|
||||
assert.strictEqual(store.verifyWorkItem('docs-task').attempt.outcome, 'unknown');
|
||||
fs.unlinkSync(artifact); fs.writeFileSync(artifact, validDoc());
|
||||
const canonicalArtifact = path.join(fs.realpathSync(root), 'matrix.md');
|
||||
const open = fs.openSync;
|
||||
const readFlags = fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0) | (fs.constants.O_NONBLOCK || 0);
|
||||
let observedArtifactFlags;
|
||||
fs.openSync = function (filename, flags, mode) {
|
||||
if (filename === canonicalArtifact) {
|
||||
observedArtifactFlags = flags;
|
||||
fs.unlinkSync(artifact); makeFifo();
|
||||
return open(filename, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0) | (fs.constants.O_NONBLOCK || 0));
|
||||
}
|
||||
// State persistence creates only private, exclusive staging files.
|
||||
if (flags === 'wx') return open(filename, 'wx', mode);
|
||||
if (flags === (fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW || 0))) {
|
||||
return open(filename, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW || 0), mode);
|
||||
}
|
||||
if (flags === (fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0))) {
|
||||
return open(filename, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0));
|
||||
}
|
||||
assert.strictEqual(flags, 'r', 'unexpected file-open mode during verification');
|
||||
return open(filename, 'r');
|
||||
};
|
||||
try {
|
||||
assert.strictEqual(store.verifyWorkItem('docs-task').attempt.outcome, 'unknown');
|
||||
assert.strictEqual(observedArtifactFlags, readFlags, 'artifact reads must retain nonblocking/no-follow flags');
|
||||
} finally { fs.openSync = open; }
|
||||
assert.deepStrictEqual(JSON.parse(result.stdout), {
|
||||
outcome: 'unknown', replaced: scenario === 'replace',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
await test('the validator comes from ECC, never a module supplied by the artifact repository', ({ root, store, register, claim, artifact }) => {
|
||||
|
||||
Reference in new issue
Block a user