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,35 @@
import path from 'node:path';
import process from 'node:process';
import { spawnSync } from 'node:child_process';
const packageRoot = path.resolve(import.meta.dirname, '..', '..');
const repoRoot = path.resolve(packageRoot, '..', '..');
export function resolveRepoPath(...segments) {
return path.join(repoRoot, ...segments);
}
export function runNodeScript(scriptPath, args = [], options = {}) {
return spawnSync(process.execPath, [scriptPath, ...args], {
cwd: repoRoot,
encoding: 'utf8',
...options,
});
}
export function ensureSuccess(label, result) {
if (result.status !== 0) {
throw new Error(`${label} failed with status ${result.status ?? 'null'}: ${(result.stderr ?? '').trim() || '(no stderr)'}`);
}
}
export function parseJsonStdout(label, result) {
const stdout = result.stdout ?? '';
try {
return stdout.trim() ? JSON.parse(stdout) : null;
} catch (error) {
throw new Error(`${label} emitted non-JSON stdout: ${error instanceof Error ? error.message : String(error)}`);
}
}
export { packageRoot, repoRoot };

View File

@@ -0,0 +1,31 @@
import path from 'node:path';
import { ensureSuccess, parseJsonStdout, resolveRepoPath, runNodeScript } from './_script-runner.mjs';
const DEFAULT_SCRIPT = resolveRepoPath('scripts', 'operator_notify_bridge_supervisor.mjs');
const DEFAULT_DISPATCHER_SCRIPT = resolveRepoPath('scripts', 'operator_notify_dispatcher.mjs');
export function runBridgeSupervisorAdapter({
scriptPath = DEFAULT_SCRIPT,
spoolDir,
queueDir,
receiptDir,
dispatcherScript = DEFAULT_DISPATCHER_SCRIPT,
senderCommand = null,
now = null,
compact = true,
dryRun = false,
} = {}) {
const args = [];
if (spoolDir) args.push('--spool-dir', path.resolve(spoolDir));
if (queueDir) args.push('--queue-dir', path.resolve(queueDir));
if (receiptDir) args.push('--receipt-dir', path.resolve(receiptDir));
if (dispatcherScript) args.push('--dispatcher-script', path.resolve(dispatcherScript));
if (senderCommand) args.push('--sender-command', senderCommand);
if (now) args.push('--now', now);
if (dryRun) args.push('--dry-run');
if (compact) args.push('--compact');
const result = runNodeScript(path.resolve(scriptPath), args);
ensureSuccess('bridge supervisor adapter', result);
return parseJsonStdout('bridge supervisor adapter', result);
}

View File

@@ -0,0 +1,30 @@
import path from 'node:path';
import { ensureSuccess, parseJsonStdout, resolveRepoPath, runNodeScript } from './_script-runner.mjs';
const DEFAULT_SCRIPT = resolveRepoPath('scripts', 'operator_notify_dispatcher.mjs');
export function runDispatcherAdapter({
scriptPath = DEFAULT_SCRIPT,
queueDir,
spoolDir,
now = null,
compact = true,
claim = false,
ack = null,
block = null,
note = null,
} = {}) {
const args = [];
if (queueDir) args.push('--queue-dir', path.resolve(queueDir));
if (spoolDir) args.push('--spool-dir', path.resolve(spoolDir));
if (now) args.push('--now', now);
if (claim) args.push('--claim');
if (ack) args.push('--ack', ack);
if (block) args.push('--block', block);
if (note) args.push('--note', note);
if (compact) args.push('--compact');
const result = runNodeScript(path.resolve(scriptPath), args);
ensureSuccess('dispatcher adapter', result);
return parseJsonStdout('dispatcher adapter', result);
}

View File

@@ -0,0 +1,5 @@
export { runWatchdogAdapter } from './watchdog.mjs';
export { runDispatcherAdapter } from './dispatcher.mjs';
export { runBridgeSupervisorAdapter } from './bridge-supervisor.mjs';
export { runSenderBindingAdapter } from './sender-binding.mjs';
export { runOrchestratorAdapter } from './orchestrator.mjs';

View File

@@ -0,0 +1,51 @@
import path from 'node:path';
import { ensureSuccess, parseJsonStdout, resolveRepoPath, runNodeScript } from './_script-runner.mjs';
const DEFAULT_SCRIPT = resolveRepoPath('scripts', 'watchdog_auto_notify_orchestrator.mjs');
const DEFAULT_WATCHDOG_SCRIPT = resolveRepoPath('scripts', 'long_task_watchdog.mjs');
const DEFAULT_DISPATCHER_SCRIPT = resolveRepoPath('scripts', 'operator_notify_dispatcher.mjs');
const DEFAULT_SUPERVISOR_SCRIPT = resolveRepoPath('scripts', 'operator_notify_bridge_supervisor.mjs');
export function runOrchestratorAdapter({
scriptPath = DEFAULT_SCRIPT,
state,
evidenceDir,
eventDir,
queueDir,
spoolDir,
receiptDir,
watchdogScript = DEFAULT_WATCHDOG_SCRIPT,
dispatcherScript = DEFAULT_DISPATCHER_SCRIPT,
supervisorScript = DEFAULT_SUPERVISOR_SCRIPT,
senderCommand = null,
senderMode = null,
openclawBin = 'openclaw',
now = null,
compact = true,
writeState = false,
claim = false,
dryRun = false,
} = {}) {
const args = [];
if (state) args.push('--state', path.resolve(state));
if (evidenceDir) args.push('--evidence-dir', path.resolve(evidenceDir));
if (eventDir) args.push('--event-dir', path.resolve(eventDir));
if (queueDir) args.push('--queue-dir', path.resolve(queueDir));
if (spoolDir) args.push('--spool-dir', path.resolve(spoolDir));
if (receiptDir) args.push('--receipt-dir', path.resolve(receiptDir));
if (watchdogScript) args.push('--watchdog-script', path.resolve(watchdogScript));
if (dispatcherScript) args.push('--dispatcher-script', path.resolve(dispatcherScript));
if (supervisorScript) args.push('--supervisor-script', path.resolve(supervisorScript));
if (senderCommand) args.push('--sender-command', senderCommand);
if (senderMode) args.push('--sender-mode', senderMode);
if (openclawBin) args.push('--openclaw-bin', openclawBin);
if (now) args.push('--now', now);
if (writeState) args.push('--write-state');
if (claim) args.push('--claim');
if (dryRun) args.push('--dry-run');
if (compact) args.push('--compact');
const result = runNodeScript(path.resolve(scriptPath), args);
ensureSuccess('orchestrator adapter', result);
return parseJsonStdout('orchestrator adapter', result);
}

View File

@@ -0,0 +1,28 @@
import path from 'node:path';
import { ensureSuccess, parseJsonStdout, resolveRepoPath, runNodeScript } from './_script-runner.mjs';
const DEFAULT_SCRIPT = resolveRepoPath('scripts', 'operator_notify_sender_binding.mjs');
export function runSenderBindingAdapter({
scriptPath = DEFAULT_SCRIPT,
mode = 'shim',
attemptDir,
openclawBin = 'openclaw',
now = null,
compact = true,
env = {},
} = {}) {
const args = ['--mode', mode, '--openclaw-bin', openclawBin];
if (attemptDir) args.push('--attempt-dir', path.resolve(attemptDir));
if (now) args.push('--now', now);
if (compact) args.push('--compact');
const result = runNodeScript(path.resolve(scriptPath), args, {
env: {
...process.env,
...env,
},
});
ensureSuccess('sender binding adapter', result);
return parseJsonStdout('sender binding adapter', result);
}

View File

@@ -0,0 +1,28 @@
import path from 'node:path';
import { ensureSuccess, parseJsonStdout, resolveRepoPath, runNodeScript } from './_script-runner.mjs';
const DEFAULT_SCRIPT = resolveRepoPath('scripts', 'long_task_watchdog.mjs');
export function runWatchdogAdapter({
scriptPath = DEFAULT_SCRIPT,
state,
evidenceDir,
eventDir,
notificationDir,
now = null,
compact = true,
writeState = false,
} = {}) {
const args = [];
if (state) args.push('--state', path.resolve(state));
if (evidenceDir) args.push('--evidence-dir', path.resolve(evidenceDir));
if (eventDir) args.push('--event-dir', path.resolve(eventDir));
if (notificationDir) args.push('--notification-dir', path.resolve(notificationDir));
if (now) args.push('--now', now);
if (writeState) args.push('--write-state');
if (compact) args.push('--compact');
const result = runNodeScript(path.resolve(scriptPath), args);
ensureSuccess('watchdog adapter', result);
return parseJsonStdout('watchdog adapter', result);
}

View File

@@ -30,3 +30,11 @@ export const packageBoundaries = {
};
export { evaluatePolicyPack, evaluatePolicies, planDecisionExecution } from './core/index.mjs';
export {
runWatchdogAdapter,
runDispatcherAdapter,
runBridgeSupervisorAdapter,
runSenderBindingAdapter,
runOrchestratorAdapter,
} from './adapters/index.mjs';
export { runOrchestratorAdapter as runWatchdogChain } from './adapters/orchestrator.mjs';