refactor: extract orchestrator execution core

This commit is contained in:
Eve
2026-05-08 16:30:32 +08:00
parent 72397df976
commit 55fe51483b
6 changed files with 222 additions and 150 deletions

View File

@@ -0,0 +1,75 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { runOrchestratorExecution } from '../src/adapters/orchestrator-execution.mjs';
const packageRoot = path.resolve(import.meta.dirname, '..');
function createFixtureRoot() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'reporting-governance-orchestrator-core-'));
}
function mkdirs(root, names) {
for (const name of names) fs.mkdirSync(path.join(root, name), { recursive: true });
}
function writeState(root) {
const statePath = path.join(root, 'watchdog-state.json');
fs.writeFileSync(statePath, `${JSON.stringify({
version: 1,
watchdogs: [{
id: 'reporting-governance-plugin-watchdog',
task: 'reporting-governance plugin spec development',
status: 'active',
ownerSessionKey: 'agent:coder:main',
reportChannel: 'telegram',
reportTarget: '864811879',
intervalMinutes: 10,
lastMilestoneAt: '2026-05-07T08:00:00.000Z',
lastAlertAt: null,
}],
}, null, 2)}\n`, 'utf8');
return statePath;
}
test('execution core runs adapter chain directly without orchestrator script shell hop', () => {
const root = createFixtureRoot();
try {
mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']);
const statePath = writeState(root);
const payload = runOrchestratorExecution({
state: statePath,
evidenceDir: path.join(root, 'evidence'),
eventDir: path.join(root, 'events'),
queueDir: path.join(root, 'queue'),
spoolDir: path.join(root, 'spool'),
receiptDir: path.join(root, 'receipts'),
watchdogScript: path.join(packageRoot, 'scripts', 'long_task_watchdog.mjs'),
dispatcherScript: path.join(packageRoot, 'scripts', 'operator_notify_dispatcher.mjs'),
supervisorScript: path.join(packageRoot, 'scripts', 'operator_notify_bridge_supervisor.mjs'),
senderCommand: `node -e "process.stdout.write(JSON.stringify({state:'sent'}))"`,
now: '2026-05-07T08:20:00.000Z',
writeState: true,
}, {
senderBindingScript: path.join(packageRoot, 'scripts', 'operator_notify_sender_binding.mjs'),
});
assert.equal(payload.ok, true);
assert.deepEqual(payload.executionOrder, ['runner', 'queue', 'dispatcher', 'bridge', 'sender', 'ack_or_blocked_or_pending']);
assert.equal(payload.orchestration.senderCommandConfigured, true);
assert.equal(payload.result.watchdog.notificationCount, 1);
assert.equal(payload.result.dispatcher.dispatchedCount, 1);
assert.equal(payload.result.supervisor.ackedCount, 1);
const queueFiles = fs.readdirSync(path.join(root, 'queue')).filter((name) => name.endsWith('.json'));
assert.equal(queueFiles.length, 1);
const queueItem = JSON.parse(fs.readFileSync(path.join(root, 'queue', queueFiles[0]), 'utf8'));
assert.equal(queueItem.status, 'acked');
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});