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 { executeRuntimeIntegratedGovernance } from '../src/index.mjs'; import capabilityDescriptor from '../capabilities/openclaw-watchdog-reference.json' with { type: 'json' }; const packageRoot = path.resolve(import.meta.dirname, '..'); const repoRoot = path.resolve(packageRoot, '..', '..'); const noSilencePack = { metadata: { id: 'no-silence', severity_default: 'high' }, spec: { evaluation_mode: 'any_rule_match', rules: [ { id: 'no-silence.missed-checkpoint', title: 'Missed checkpoint requires visible recovery', triggers: { event_types: ['silence_timeout'] }, conditions: { all: [ { fact: 'checkpoint.is_overdue', equals: true } ] }, decision_output: { decision: 'force_checkpoint', severity: 'high', reason: 'checkpoint overdue triggered forced operator-visible recovery', required_actions: [ { action: 'notify_operator', target: 'operator_channel', mandatory: true }, { action: 'emit_event', target: 'event_stream', mandatory: true } ], operator_notice: { required: true, channel: 'telegram', urgency: 'high', message: 'Required update: checkpoint overdue.', deadline: '2026-01-01T00:00:00.000Z' } } } ] } }; const unknownActionPack = { metadata: { id: 'unknown-action-pack', severity_default: 'high' }, spec: { evaluation_mode: 'any_rule_match', rules: [ { id: 'unknown-action-pack.route-to-unsupported-action', title: 'Route to unsupported action for matrix coverage', triggers: { event_types: ['silence_timeout'] }, conditions: { all: [ { fact: 'checkpoint.is_overdue', equals: true } ] }, decision_output: { decision: 'force_checkpoint', severity: 'high', reason: 'checkpoint overdue triggered unsupported adapter action for route matrix coverage', required_actions: [ { action: 'rewrite_message', target: 'message_body', mandatory: true } ] } } ] } }; const strictProfileArtifact = { kind: 'DeploymentProfileArtifact', apiVersion: 'reporting-governance/v1alpha1', metadata: { id: 'strict-manager-mode', runtime: 'openclaw', compatibility_mode: 'strict_envelope', }, spec: { package: { pluginVersion: '0.1.0-mainline' }, policies: { overrides: { checkpoints: { overdueAction: 'force_checkpoint' } } }, notifications: { operatorVisibleRecoveryRequired: true }, bindings: { runtime: 'openclaw', entrypoint: 'scripts/watchdog_auto_notify_orchestrator.mjs', scripts: { watchdog: 'scripts/long_task_watchdog.mjs', dispatcher: 'scripts/operator_notify_dispatcher.mjs', bridgeSupervisor: 'scripts/operator_notify_bridge_supervisor.mjs', senderBinding: 'scripts/operator_notify_sender_binding.mjs', orchestrator: 'scripts/watchdog_auto_notify_orchestrator.mjs' }, artifact_roots: { queueItems: 'state/operator-notify-queue' } } }, capability_expectations: { required: [ 'emit_canonical_events', 'evaluate_watchdog_overdue', 'create_queue_items', 'create_spool_handoff', 'write_bridge_receipts' ], preferred: ['direct_sender_binding', 'final_delivery_ack'] } }; function createFixtureRoot() { return fs.mkdtempSync(path.join(os.tmpdir(), 'reporting-governance-runtime-integrated-')); } 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 JSON.parse(fs.readFileSync(path.join(dirPath, files[0]), 'utf8')); } function createBaseArgs(overrides = {}) { return { event: { type: 'silence_timeout', payload: { checkpoint_overdue: true, } }, evidence: [ { id: 'ev-watchdog', quality: 'moderate', is_new: true } ], capabilityDescriptor, policyPacks: [noSilencePack], context: { signals: ['checkpoint_overdue'], }, profile: strictProfileArtifact, packageVersion: '0.1.0-mainline', repoRootOverride: repoRoot, ...overrides, }; } function createStubRuntime() { return { state: path.join(repoRoot, 'tmp', 'unused-state.json'), evidenceDir: path.join(repoRoot, 'tmp', 'unused-evidence'), eventDir: path.join(repoRoot, 'tmp', 'unused-events'), queueDir: path.join(repoRoot, 'tmp', 'unused-queue'), spoolDir: path.join(repoRoot, 'tmp', 'unused-spool'), receiptDir: path.join(repoRoot, 'tmp', 'unused-receipts'), dryRun: true, now: '2026-05-07T08:20:00.000Z', }; } function createDegradedDescriptor() { return { ...capabilityDescriptor, metadata: { ...capabilityDescriptor.metadata, id: 'degraded-openclaw-watchdog-reference' }, capabilities: { ...capabilityDescriptor.capabilities, notification_path: { ...capabilityDescriptor.capabilities.notification_path, sender_binding: { supported: false, level: 'none' }, direct_send: { supported: false, level: 'none' } } } }; } function createNoAdapterActionPack() { return { metadata: { id: 'no-adapter-action-pack', severity_default: 'high' }, spec: { evaluation_mode: 'any_rule_match', rules: [ { id: 'no-adapter-action-pack.package-core-only', title: 'Package-core-only route for degraded matrix coverage', triggers: { event_types: ['silence_timeout'] }, conditions: { all: [ { fact: 'checkpoint.is_overdue', equals: true } ] }, decision_output: { decision: 'force_checkpoint', severity: 'high', reason: 'checkpoint overdue triggered package-core-only route for degraded matrix coverage', required_actions: [ { action: 'emit_event', target: 'event_stream', mandatory: true } ] } } ] } }; } function assertNoAckedOrFinalDeliveredClaim(result) { assert.notEqual(result.contract.delivery_state, 'acked'); assert.notEqual(result.contract.delivery_state, 'final_delivered'); assert.notEqual(result.planning.receipt.delivery_state, 'acked'); assert.notEqual(result.planning.receipt.delivery_state, 'final_delivered'); assert.notEqual(result.contract.receipt_status, 'acked'); assert.notEqual(result.contract.receipt_status, 'final_delivered'); assert.notEqual(result.planning.receipt.status, 'acked'); assert.notEqual(result.planning.receipt.status, 'final_delivered'); assert.notEqual(result.runtimeExecution?.result?.supervisor?.deliveryState, 'acked'); assert.notEqual(result.runtimeExecution?.result?.supervisor?.deliveryState, 'final_delivered'); } function assertDeferredTruthState(result) { assert.equal(result.contract.delivery_state, 'pending_external_send'); assert.equal(result.planning.receipt.delivery_state, 'pending_external_send'); assertNoAckedOrFinalDeliveredClaim(result); } function assertAckedTruthState(result) { assert.equal(result.contract.delivery_state, 'acked'); assert.equal(result.contract.receipt_status, 'acked'); assert.equal(result.planning.receipt.delivery_state, 'acked'); assert.equal(result.planning.receipt.status, 'acked'); assert.match( result.planning.receipt.notes.at(-1) ?? '', /truth state promoted to acked/ ); } const futureTruthStateMatrix = Object.freeze({ deferred: { contractDeliveryState: 'pending_external_send', attempted: true, terminalClaimForbidden: ['acked', 'final_delivered'] }, acked: { contractDeliveryState: 'acked', attempted: true, terminalClaimRequired: ['acked'] }, planningOnly: { attempted: false, terminalClaimForbidden: ['acked', 'final_delivered'] } }); test('runtime-integrated route matrix: no runtime stays planning-only', () => { const result = executeRuntimeIntegratedGovernance(createBaseArgs()); assert.equal(result.preflight.status, 'pass'); assert.equal(result.runtimeIntegration.attempted, false); assert.equal(result.runtimeIntegration.reason, 'runtime execution not attempted'); assert.equal(result.runtimeExecution, null); }); test('runtime-integrated route matrix: preflight fail_closed blocks runtime route', () => { const result = executeRuntimeIntegratedGovernance(createBaseArgs({ profile: { ...strictProfileArtifact, spec: { ...strictProfileArtifact.spec, package: { pluginVersion: '0.9.9-mismatch' }, }, }, runtime: createStubRuntime(), })); assert.equal(result.preflight.status, 'fail_closed'); assert.equal(result.runtimeIntegration.attempted, false); assert.equal(result.runtimeIntegration.reason, 'runtime execution not attempted: compatibility preflight failed closed'); assert.equal(result.runtimeExecution, null); }); test('runtime-integrated route matrix: missing deploymentBinding blocks runtime route', () => { const profileWithoutBindings = { ...strictProfileArtifact, spec: { ...strictProfileArtifact.spec, }, }; delete profileWithoutBindings.spec.bindings; const result = executeRuntimeIntegratedGovernance(createBaseArgs({ profile: profileWithoutBindings, runtime: createStubRuntime(), })); assert.equal(result.preflight.status, 'pass'); assert.equal(result.deploymentBinding, null); assert.equal(result.runtimeIntegration.attempted, false); assert.equal(result.runtimeIntegration.reason, 'runtime execution not attempted: deployment binding is missing'); assert.equal(result.runtimeExecution, null); }); test('runtime-integrated route matrix: unknown adapter_action stays planning-only', () => { const result = executeRuntimeIntegratedGovernance(createBaseArgs({ policyPacks: [unknownActionPack], runtime: createStubRuntime(), })); assert.equal(result.preflight.status, 'pass'); assert.deepEqual(result.contract.adapter_actions, []); assert.deepEqual(result.contract.blocked_actions, ['rewrite_message']); assert.equal(result.runtimeIntegration.attempted, false); assert.equal(result.runtimeIntegration.reason, 'runtime execution not attempted: no adapter_action matched an adapter runner route'); assert.equal(result.runtimeExecution, null); }); test('runtime-integrated route matrix: degraded preflight still runs queue/bridge route honestly', () => { const root = createFixtureRoot(); try { mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']); const statePath = writeState(root); const result = executeRuntimeIntegratedGovernance(createBaseArgs({ capabilityDescriptor: createDegradedDescriptor(), runtime: { 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.preflight.status, 'degraded'); assert.equal(result.contract.adapter_actions[0], 'notify_operator'); assertDeferredTruthState(result); assert.equal(result.runtimeIntegration.attempted, futureTruthStateMatrix.deferred.attempted); assert.equal(result.runtimeIntegration.adapter, 'orchestrator'); assert.equal(result.runtimeIntegration.action, 'notify_operator'); assert.equal(result.runtimeExecution.ok, true); assert.equal(result.runtimeExecution.result.dispatcher.dispatchedCount, 1); assert.equal(result.runtimeExecution.result.supervisor.pendingCount, 1); const queueItem = readSingleJson(path.join(root, 'queue')); assert.equal(queueItem.status, 'dispatched'); const receipt = readSingleJson(path.join(root, 'receipts')); assert.equal(receipt.state, 'pending_external_send'); assert.equal(receipt.supervisor_mode, 'dry_run'); } finally { fs.rmSync(root, { recursive: true, force: true }); } }); test('runtime-integrated route matrix: matched adapter_action runs orchestrator adapter runner', () => { const root = createFixtureRoot(); try { mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']); const statePath = writeState(root); const result = executeRuntimeIntegratedGovernance(createBaseArgs({ runtime: { 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.preflight.status, 'pass'); assert.equal(result.contract.decision, 'force_checkpoint'); assert.equal(result.runtimeIntegration.attempted, true); assert.equal(result.runtimeIntegration.adapter, 'orchestrator'); assert.equal(result.runtimeIntegration.action, 'notify_operator'); assert.equal(result.runtimeIntegration.reason, 'adapter_action notify_operator routed to orchestrator adapter runner'); assertDeferredTruthState(result); assert.equal(result.runtimeExecution.ok, true); assert.equal(result.runtimeExecution.result.dispatcher.dispatchedCount, 1); assert.equal(result.runtimeExecution.result.supervisor.pendingCount, 1); const queueItem = readSingleJson(path.join(root, 'queue')); assert.equal(queueItem.status, 'dispatched'); const receipt = readSingleJson(path.join(root, 'receipts')); assert.equal(receipt.state, 'pending_external_send'); assert.equal(receipt.supervisor_mode, 'dry_run'); } finally { fs.rmSync(root, { recursive: true, force: true }); } }); test('runtime-integrated route matrix: full sender capability promotes truth state to acked', () => { const root = createFixtureRoot(); try { mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']); const statePath = writeState(root); const result = executeRuntimeIntegratedGovernance(createBaseArgs({ runtime: { 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.preflight.status, 'pass'); assert.equal(result.runtimeIntegration.attempted, futureTruthStateMatrix.acked.attempted); assert.equal(result.runtimeExecution.ok, true); assert.equal(result.runtimeExecution.result.dispatcher.dispatchedCount, 1); assert.equal(result.runtimeExecution.result.supervisor.ackedCount, 1); assertAckedTruthState(result); const queueItem = readSingleJson(path.join(root, 'queue')); assert.equal(queueItem.status, 'acked'); const receipt = readSingleJson(path.join(root, 'receipts')); assert.equal(receipt.state, 'acked'); assert.equal(receipt.sender_binding.state, 'sent'); assert.equal(receipt.sender_status, 0); } finally { fs.rmSync(root, { recursive: true, force: true }); } }); test('runtime-integrated route matrix: degraded without adapter_action stays not attempted', () => { const result = executeRuntimeIntegratedGovernance(createBaseArgs({ capabilityDescriptor: createDegradedDescriptor(), policyPacks: [createNoAdapterActionPack()], runtime: createStubRuntime(), })); assert.equal(result.preflight.status, 'degraded'); assert.deepEqual(result.contract.adapter_actions, []); assert.deepEqual(result.contract.package_actions, ['emit_event']); assert.equal(result.runtimeIntegration.attempted, futureTruthStateMatrix.planningOnly.attempted); assert.equal(result.runtimeIntegration.reason, 'runtime execution not attempted: degraded compatibility preflight produced no adapter_action route'); assert.equal(result.runtimeExecution, null); assertNoAckedOrFinalDeliveredClaim(result); }); test('runtime-integrated route matrix: degraded runtime path never claims acked or final delivered', () => { const root = createFixtureRoot(); try { mkdirs(root, ['evidence', 'events', 'queue', 'spool', 'receipts']); const statePath = writeState(root); const result = executeRuntimeIntegratedGovernance(createBaseArgs({ capabilityDescriptor: createDegradedDescriptor(), runtime: { 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.preflight.status, 'degraded'); assertDeferredTruthState(result); assert.equal(result.runtimeExecution.result.supervisor.pendingCount, 1); const receipt = readSingleJson(path.join(root, 'receipts')); assert.equal(receipt.state, 'pending_external_send'); assert.notEqual(receipt.state, 'acked'); assert.notEqual(receipt.state, 'final_delivered'); } finally { fs.rmSync(root, { recursive: true, force: true }); } });