/**
 * shared.ts -- Helpers for the orchestrator adapter substrate.
 *
 * Provides: plan-tree cycle detection, canonical audit-event builder,
 * reviewer-tier validation. Consumed by BaseOrchestratorAdapter and
 * concrete adapters. No runtime dependencies beyond Node built-ins.
 *
 * Spec: P3-ORCH-01, AC 3
 * @connexum/ai-governance
 */
import * as crypto from 'crypto';
// ---------------------------------------------------------------------------
// Plan-tree cycle detection (AC 22)
// ---------------------------------------------------------------------------
/**
 * Detects cycles in a plan's dependency graph using depth-first search.
 * Returns the IDs of steps that form a cycle, or an empty array if none.
 */
export function detectPlanCycle(steps) {
    const stepIds = new Set(steps.map((s) => s.stepId));
    const visited = new Set();
    const inStack = new Set();
    const cycleNodes = [];
    // Build adjacency map: stepId -> dependencies
    const deps = new Map();
    for (const step of steps) {
        deps.set(step.stepId, step.dependencies);
    }
    function dfs(nodeId) {
        visited.add(nodeId);
        inStack.add(nodeId);
        const nodeDeps = deps.get(nodeId) ?? [];
        for (const dep of nodeDeps) {
            if (inStack.has(dep)) {
                cycleNodes.push(dep);
                return true;
            }
            if (!visited.has(dep) && dfs(dep)) {
                return true;
            }
        }
        inStack.delete(nodeId);
        return false;
    }
    for (const step of steps) {
        if (!visited.has(step.stepId)) {
            if (dfs(step.stepId)) {
                break;
            }
        }
    }
    return cycleNodes;
}
/**
 * Validates that all step dependency IDs reference known step IDs.
 * Returns the undefined dependency IDs, or an empty array if all valid.
 */
export function findUndefinedDependencies(steps) {
    const stepIds = new Set(steps.map((s) => s.stepId));
    const undefined_ = [];
    for (const step of steps) {
        for (const dep of step.dependencies) {
            if (!stepIds.has(dep)) {
                undefined_.push(dep);
            }
        }
    }
    return undefined_;
}
/** Severity map for each canonical event (Section 3.2) */
export const CANONICAL_SEVERITY = {
    'orchestrator:workflow-start': 'INFO',
    'orchestrator:workflow-complete': 'INFO',
    'orchestrator:plan-proposed': 'INFO',
    'orchestrator:plan-approved': 'INFO',
    'orchestrator:plan-denied': 'WARN',
    'orchestrator:plan-step-begin': 'DEBUG',
    'orchestrator:plan-step-end': 'DEBUG',
    'orchestrator:task-approved': 'INFO',
    'orchestrator:task-denied': 'WARN',
    'orchestrator:delegation-approved': 'INFO',
    'orchestrator:delegation-denied': 'WARN',
    'orchestrator:role-edge-invoked': 'DEBUG',
    'orchestrator:reviewer-routed': 'INFO',
    'orchestrator:reviewer-timeout': 'WARN',
    'orchestrator:unregistered-role': 'CRITICAL',
    'orchestrator:termination-by-exhaustion': 'WARN',
    'orchestrator:framework-dispatch': 'DEBUG',
    'orchestrator:circuit-breaker-tripped': 'CRITICAL',
    // C1 fix: G-series classification result for tool outputs
    'orchestrator:tool-output-classified': 'INFO',
};
/**
 * Build a canonical audit event with a generated eventId and current
 * timestamp. The caller provides the payload; severity is auto-resolved
 * from the taxonomy.
 */
export function buildCanonicalAuditEvent(adapterId, orchestratorId, eventName, payload) {
    return {
        eventId: crypto.randomUUID(),
        eventName,
        severity: CANONICAL_SEVERITY[eventName],
        timestamp: new Date().toISOString(),
        adapterId,
        orchestratorId,
        payload,
    };
}
// ---------------------------------------------------------------------------
// Reviewer-tier validation (Section 3.4)
// ---------------------------------------------------------------------------
export const VALID_REVIEWER_TIERS = [
    'agent-supervisor',
    'human-reviewer',
    'legal-reviewer',
    'security-reviewer',
];
export function isValidReviewerTier(tier) {
    return VALID_REVIEWER_TIERS.includes(tier);
}
//# sourceMappingURL=shared.js.map