/** * Policy Engine for Hard Constraint Enforcement * * Implements the IPRE governance layer's hard constraint enforcement, * ensuring AI agents operate within defined security, compliance, and * operational boundaries. */ /** * Policy categories for classification */ export type PolicyCategory = 'security' | 'compliance' | 'operational'; /** * Severity level for policy violations */ export type ViolationSeverity = 'low' | 'medium' | 'high' | 'critical'; /** * Represents a single policy definition */ export interface Policy { /** Unique policy identifier */ readonly id: string; /** Policy category */ readonly category: PolicyCategory; /** Human-readable name */ readonly name: string; /** Detailed description */ readonly description: string; /** Pattern or rule to check against */ readonly pattern?: RegExp | string; /** Custom validation function */ readonly validator?: (action: AgentAction) => boolean; /** Custom validator function name (for extensibility) */ readonly validatorName?: string; /** Severity when violated */ readonly severity: ViolationSeverity; /** Whether violation blocks the action */ readonly blocking: boolean; /** Whether policy is currently active */ readonly enabled: boolean; } /** * Collection of policies organized by category (immutable for external use) */ export interface PolicySet { readonly security: readonly Policy[]; readonly compliance: readonly Policy[]; readonly operational: readonly Policy[]; } /** * Represents an action taken by an AI agent */ export interface AgentAction { /** Unique action identifier */ readonly id: string; /** Session ID where action occurred */ readonly sessionId: string; /** Agent ID that performed the action */ readonly agentId: string; /** Type of action (e.g., 'commit', 'deploy', 'file_write') */ readonly type: string; /** Action description */ readonly description: string; /** Timestamp of action */ readonly timestamp: Date; /** File paths affected (if applicable) */ readonly affectedFiles?: string[]; /** Code content (if applicable) */ readonly codeContent?: string; /** Additional action metadata */ readonly metadata?: Record; } /** * Represents a policy violation */ export interface PolicyViolation { /** Unique violation identifier */ readonly id: string; /** The violated policy */ readonly policy: Policy; /** The action that caused the violation */ readonly action: AgentAction; /** Violation timestamp */ readonly timestamp: Date; /** Detailed violation message */ readonly message: string; /** Severity of the violation */ readonly severity: ViolationSeverity; /** Whether this violation blocks the action */ readonly blocking: boolean; } /** * Result of a policy check */ export interface PolicyCheckResult { /** Whether the action is allowed */ readonly allowed: boolean; /** List of violations found */ readonly violations: PolicyViolation[]; /** Non-blocking warnings */ readonly warnings: string[]; } /** * Policy configuration for loading */ export interface PolicyConfig { /** Security policies */ readonly security?: PolicyDefinition[]; /** Compliance policies */ readonly compliance?: PolicyDefinition[]; /** Operational policies */ readonly operational?: PolicyDefinition[]; } /** * Policy definition for configuration */ export interface PolicyDefinition { /** Unique policy identifier */ readonly id?: string; /** Human-readable name */ readonly name: string; /** Description of the policy */ readonly description?: string; /** Regex pattern to match against code content */ readonly pattern?: string; /** Custom validator function name (for extensibility) */ readonly validatorName?: string; /** Severity level */ readonly severity?: ViolationSeverity; /** Whether violation blocks the action */ readonly blocking?: boolean; /** Whether policy is enabled */ readonly enabled?: boolean; } /** * Logger interface for policy engine */ export interface PolicyEngineLogger { log: (message: string) => void; warn?: (message: string) => void; error?: (message: string) => void; } /** * Configuration for the PolicyEngine */ export interface PolicyEngineConfig { /** Enable debug logging */ readonly debug?: boolean; /** Maximum violations to track in history */ readonly maxHistorySize?: number; /** Default severity for policies without explicit severity */ readonly defaultSeverity?: ViolationSeverity; /** Default blocking behavior for policies */ readonly defaultBlocking?: boolean; /** Custom validators registry */ readonly customValidators?: Record boolean>; /** Custom logger for debug output */ readonly logger?: PolicyEngineLogger; } /** * Policy Engine for enforcing hard constraints on AI agent actions * * Implements the IPRE governance layer's policy enforcement mechanism, * checking agent actions against security, compliance, and operational * policies and recording violations for monitoring and intervention. * * @example * ```typescript * const engine = new PolicyEngine({ debug: true }); * * // Check an action * const result = engine.checkAction({ * id: 'action-1', * sessionId: 'session-1', * agentId: 'coder-1', * type: 'commit', * description: 'Add new feature', * timestamp: new Date(), * codeContent: 'const apiKey = "sk-secret123"' * }); * * if (!result.allowed) { * console.log('Action blocked:', result.violations); * } * ``` */ export declare class PolicyEngine { private policies; private violationHistory; private readonly config; private readonly customValidators; constructor(config?: PolicyEngineConfig); /** * Load policies from configuration * * @param config - Policy configuration to load */ loadPolicies(config: PolicyConfig): void; /** * Check an agent action against all policies * * @param action - The action to check * @returns Policy check result with allowed status, violations, and warnings */ checkAction(action: AgentAction): PolicyCheckResult; /** * Check if an action is blocked by any policy * * @param action - The action to check * @returns true if the action is blocked */ isBlocked(action: AgentAction): boolean; /** * Get all violations for an action without recording them * * @param action - The action to check * @returns Array of policy violations */ getViolations(action: AgentAction): PolicyViolation[]; /** * Record a policy violation * * @param violation - The violation to record */ recordViolation(violation: PolicyViolation): void; /** * Get violation history, optionally filtered by session ID * * @param sessionId - Optional session ID to filter by * @returns Array of policy violations */ getViolationHistory(sessionId?: string): PolicyViolation[]; /** * Calculate the violation rate over a time window * * @param timeWindowMs - Time window in milliseconds (default: 24 hours) * @returns Violation rate (violations per hour) */ getViolationRate(timeWindowMs?: number): number; /** * Get all policies organized by category */ getPolicies(): PolicySet; /** * Enable or disable a specific policy * * @param policyId - The policy ID to modify * @param enabled - Whether to enable or disable */ setPolicyEnabled(policyId: string, enabled: boolean): void; /** * Clear violation history * * @param sessionId - Optional session ID to clear only specific session violations */ clearViolationHistory(sessionId?: string): void; /** * Get violation statistics */ getViolationStats(): { total: number; byCategory: Record; bySeverity: Record; blockingCount: number; }; /** * Check if a policy is violated by an action */ private checkPolicyViolation; /** * Create a policy violation record */ private createViolation; /** * Create a policy from definition */ private createPolicyFromDefinition; /** * Generate unique violation ID */ private generateViolationId; /** * Log debug message */ private log; } //# sourceMappingURL=policy-engine.d.ts.map