feat(reporting-governance): add watchdog chain adapters

This commit is contained in:
Eve
2026-05-08 08:42:52 +08:00
parent c2a775b62c
commit 145371fd23
12 changed files with 373 additions and 2 deletions

View File

@@ -0,0 +1,32 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packageRoot = path.resolve(__dirname, '..');
const requiredPaths = [
'README.md',
'package.json',
'src/index.mjs',
'src/core',
'src/adapters',
'src/adapters/watchdog.mjs',
'src/adapters/dispatcher.mjs',
'src/adapters/bridge-supervisor.mjs',
'src/adapters/sender-binding.mjs',
'src/adapters/orchestrator.mjs',
'src/storage',
'src/reference/openclaw-watchdog-chain.md',
'capabilities/openclaw-watchdog-reference.json',
'examples/openclaw-watchdog-reference.descriptor.example.json'
];
test('reporting-governance package skeleton paths exist', () => {
for (const relativePath of requiredPaths) {
const fullPath = path.join(packageRoot, relativePath);
assert.equal(fs.existsSync(fullPath), true, `expected path to exist: ${relativePath}`);
}
});

View File

@@ -0,0 +1,117 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import {
runOrchestratorAdapter,
runWatchdogChain,
} from '../src/index.mjs';
function createFixtureRoot() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'reporting-governance-plugin-'));
}
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;
}
function readSingleJson(dirPath) {
const files = fs.readdirSync(dirPath).filter((name) => name.endsWith('.json')).sort();
assert.equal(files.length, 1, `expected exactly one json file in ${dirPath}`);
return {
fileName: files[0],
payload: JSON.parse(fs.readFileSync(path.join(dirPath, files[0]), 'utf8')),
};
}
test('package entrypoint can run watchdog chain through orchestrator adapter', () => {
const root = createFixtureRoot();
try {
mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']);
const statePath = writeState(root);
const result = runWatchdogChain({
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'),
writeState: true,
senderCommand: `node -e "process.stdout.write(JSON.stringify({state:'sent'}))"`,
now: '2026-05-07T08:20:00.000Z',
});
assert.equal(result.ok, true);
assert.deepEqual(result.executionOrder, ['runner', 'queue', 'dispatcher', 'bridge', 'sender', 'ack_or_blocked_or_pending']);
assert.equal(result.result.watchdog.notificationCount, 1);
assert.equal(result.result.dispatcher.dispatchedCount, 1);
assert.equal(result.result.supervisor.ackedCount, 1);
const queueItem = readSingleJson(path.join(root, 'queue')).payload;
assert.equal(queueItem.status, 'acked');
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test('dry-run path produces verifiable pending receipt via package adapter', () => {
const root = createFixtureRoot();
try {
mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']);
const statePath = writeState(root);
const result = runOrchestratorAdapter({
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'),
writeState: true,
dryRun: true,
now: '2026-05-07T08:20:00.000Z',
});
assert.equal(result.ok, true);
assert.equal(result.orchestration.dryRun, true);
assert.equal(result.result.watchdog.notificationCount, 1);
assert.equal(result.result.dispatcher.dispatchedCount, 1);
assert.equal(result.result.supervisor.pendingCount, 1);
const queueItem = readSingleJson(path.join(root, 'queue')).payload;
assert.equal(queueItem.status, 'dispatched');
const receipt = readSingleJson(path.join(root, 'receipts')).payload;
assert.equal(receipt.state, 'pending_external_send');
assert.equal(receipt.supervisor_mode, 'dry_run');
assert.ok(receipt.suggested_command.includes('openclaw message send'));
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});