refactor: share orchestrator cli core

This commit is contained in:
Eve
2026-05-08 16:23:22 +08:00
parent e99423da97
commit 72397df976
6 changed files with 357 additions and 298 deletions

View File

@@ -0,0 +1,127 @@
#!/usr/bin/env node
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import process from 'node:process';
import { spawnSync } from 'node:child_process';
const ROOT_DIR = path.resolve(import.meta.dirname, '..');
const REPO_SHIM = path.join(ROOT_DIR, 'scripts', 'watchdog_auto_notify_orchestrator.mjs');
const PACKAGE_ENTRY = path.join(ROOT_DIR, 'plugins', 'reporting-governance', 'scripts', 'watchdog_auto_notify_orchestrator.mjs');
function createFixture() {
const fixtureRoot = mkdtempSync(path.join(tmpdir(), 'watchdog-auto-notify-shim-regression-'));
const statePath = path.join(fixtureRoot, 'watchdog-state.json');
const evidenceDir = path.join(fixtureRoot, 'evidence');
const eventDir = path.join(fixtureRoot, 'events');
const queueDir = path.join(fixtureRoot, 'queue');
const spoolDir = path.join(fixtureRoot, 'spool');
const receiptDir = path.join(fixtureRoot, 'receipts');
[evidenceDir, eventDir, queueDir, spoolDir, receiptDir].forEach((dir) => mkdirSync(dir, { recursive: true }));
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 { fixtureRoot, statePath, evidenceDir, eventDir, queueDir, spoolDir, receiptDir };
}
function run(script, args = []) {
const result = spawnSync(process.execPath, [script, ...args], {
cwd: ROOT_DIR,
encoding: 'utf8',
});
return {
status: result.status,
stdout: result.stdout ?? '',
stderr: result.stderr ?? '',
};
}
const tests = [];
function test(name, fn) { tests.push({ name, fn }); }
function printResult(prefix, name, detail = '') { process.stdout.write(`${prefix} ${name}${detail ? ` ${detail}` : ''}\n`); }
function normalizeStdout(stdout) {
const payload = JSON.parse(stdout);
delete payload.orchestration?.script;
return payload;
}
function buildArgs(fixture) {
return [
'--state', fixture.statePath,
'--evidence-dir', fixture.evidenceDir,
'--event-dir', fixture.eventDir,
'--queue-dir', fixture.queueDir,
'--spool-dir', fixture.spoolDir,
'--receipt-dir', fixture.receiptDir,
'--write-state',
'--sender-command', `node -e "process.stdout.write(JSON.stringify({state:'sent'}))"`,
'--now', '2026-05-07T08:20:00.000Z',
'--compact',
];
}
test('repo-root shim forwards help text and exits like package entrypoint', () => {
const shim = run(REPO_SHIM, ['--help']);
const pkg = run(PACKAGE_ENTRY, ['--help']);
assert.equal(shim.status, 0);
assert.equal(pkg.status, 0);
assert.equal(shim.stderr, '');
assert.equal(pkg.stderr, '');
assert.equal(shim.stdout, pkg.stdout);
});
test('repo-root shim forwards args and preserves success payload semantics', () => {
const shimFixture = createFixture();
const pkgFixture = createFixture();
try {
const shim = run(REPO_SHIM, buildArgs(shimFixture));
const pkg = run(PACKAGE_ENTRY, buildArgs(pkgFixture));
assert.equal(shim.status, 0, shim.stderr);
assert.equal(pkg.status, 0, pkg.stderr);
assert.deepEqual(normalizeStdout(shim.stdout), normalizeStdout(pkg.stdout));
} finally {
rmSync(shimFixture.fixtureRoot, { recursive: true, force: true });
rmSync(pkgFixture.fixtureRoot, { recursive: true, force: true });
}
});
test('repo-root shim preserves non-zero exit semantics from package core', () => {
const shim = run(REPO_SHIM, ['--watchdog-script', path.join(ROOT_DIR, 'missing-watchdog-script.mjs'), '--compact']);
const pkg = run(PACKAGE_ENTRY, ['--watchdog-script', path.join(ROOT_DIR, 'missing-watchdog-script.mjs'), '--compact']);
assert.equal(shim.status, 1);
assert.equal(pkg.status, 1);
assert.equal(shim.stdout, '');
assert.equal(pkg.stdout, '');
assert.equal(shim.stderr, pkg.stderr);
});
let failures = 0;
for (const { name, fn } of tests) {
try {
fn();
printResult('ok', name);
} catch (error) {
failures += 1;
printResult('not ok', name, `- ${error instanceof Error ? error.message : String(error)}`);
}
}
if (failures > 0) {
process.exit(1);
}

View File

@@ -1,94 +1,6 @@
#!/usr/bin/env node
import process from 'node:process';
import { runOrchestratorAdapter } from '../plugins/reporting-governance/src/adapters/orchestrator.mjs';
import { main } from '../plugins/reporting-governance/src/adapters/orchestrator-cli.mjs';
const cliArgs = parseCliArgs(process.argv.slice(2));
try {
const payload = runOrchestratorAdapter({
compact: cliArgs.compact,
...cliArgs,
});
process.stdout.write(`${JSON.stringify(payload, null, cliArgs.compact ? 0 : 2)}\n`);
} catch (error) {
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
}
function parseCliArgs(argv) {
const args = {
state: null,
evidenceDir: null,
eventDir: null,
queueDir: null,
spoolDir: null,
receiptDir: null,
watchdogScript: null,
dispatcherScript: null,
supervisorScript: null,
senderCommand: null,
senderMode: null,
openclawBin: 'openclaw',
now: null,
compact: false,
writeState: false,
claim: false,
dryRun: false,
help: false,
};
for (let i = 0; i < argv.length; i += 1) {
const token = argv[i];
if (token === '--compact') { args.compact = true; continue; }
if (token === '--write-state') { args.writeState = true; continue; }
if (token === '--claim') { args.claim = true; continue; }
if (token === '--dry-run') { args.dryRun = true; continue; }
if (token === '--help' || token === '-h') { args.help = true; continue; }
const pairs = [
['--state', 'state'],
['--evidence-dir', 'evidenceDir'],
['--event-dir', 'eventDir'],
['--queue-dir', 'queueDir'],
['--spool-dir', 'spoolDir'],
['--receipt-dir', 'receiptDir'],
['--watchdog-script', 'watchdogScript'],
['--dispatcher-script', 'dispatcherScript'],
['--supervisor-script', 'supervisorScript'],
['--sender-command', 'senderCommand'],
['--sender-mode', 'senderMode'],
['--openclaw-bin', 'openclawBin'],
['--now', 'now'],
];
let matched = false;
for (const [flag, key] of pairs) {
if (token === flag) {
args[key] = argv[i + 1] ?? args[key];
i += 1;
matched = true;
break;
}
if (token.startsWith(`${flag}=`)) {
args[key] = token.slice(flag.length + 1) || args[key];
matched = true;
break;
}
}
if (matched) continue;
}
if (args.help) {
process.stdout.write([
'Usage:',
' node scripts/watchdog_auto_notify_orchestrator.mjs [--write-state] [--claim] [--dry-run] [--sender-command <shell>] [--sender-mode shim|openclaw-cli] [--openclaw-bin <path>] [--now <iso>] [--compact]',
'',
'Repo-root shim that forwards to package-owned orchestrator implementation.',
'Default runtime binding now resolves package script first; env/override can still point elsewhere.',
].join('\n') + '\n');
process.exit(0);
}
return args;
}
main(process.argv.slice(2));