/** * DriftDetector (L14) * * Detects behavioral drift from intended agent purpose. * Monitors for rogue agent behavior and goal misalignment. * * Threat Model: * - ASI10: Rogue Agents * - Goal misalignment * - Behavioral drift over time * * Protection Capabilities: * - Baseline behavior profiling * - Anomaly detection * - Goal alignment verification * - Continuous monitoring * - Alert thresholds */ export interface DriftDetectorConfig { /** Minimum samples before drift detection activates */ minimumSamples?: number; /** Standard deviation threshold for anomaly detection */ anomalyThreshold?: number; /** Time window for baseline calculation in milliseconds */ baselineWindow?: number; /** Enable automatic baseline updates */ autoUpdateBaseline?: boolean; /** Maximum drift score before alert (0-100) */ alertThreshold?: number; /** Enable goal alignment checking */ checkGoalAlignment?: boolean; /** Callback when drift is detected */ onDrift?: (agentId: string, analysis: DriftAnalysis) => void; /** Callback when agent returns to baseline */ onRecovery?: (agentId: string) => void; } export interface BehaviorSample { /** Timestamp of the sample */ timestamp: number; /** Tools/actions used */ tools: string[]; /** Topics/domains accessed */ topics: string[]; /** Sentiment indicator (-1 to 1) */ sentiment: number; /** Response length */ responseLength: number; /** Time to respond in milliseconds */ responseTime: number; /** Error occurred */ hadError: boolean; /** User satisfaction (if available, 0-1) */ satisfaction?: number; /** Goal alignment indicators */ goalIndicators?: Record; /** Custom metrics */ customMetrics?: Record; } export interface BaselineProfile { /** Average tool usage distribution */ toolDistribution: Record; /** Average topic distribution */ topicDistribution: Record; /** Average sentiment */ avgSentiment: number; /** Sentiment standard deviation */ sentimentStdDev: number; /** Average response length */ avgResponseLength: number; /** Response length standard deviation */ responseLengthStdDev: number; /** Average response time */ avgResponseTime: number; /** Response time standard deviation */ responseTimeStdDev: number; /** Error rate */ errorRate: number; /** Average satisfaction */ avgSatisfaction: number; /** Sample count used for baseline */ sampleCount: number; /** When baseline was last updated */ lastUpdated: number; } export interface DriftAnalysis { /** Overall drift score (0-100) */ driftScore: number; /** Is currently drifting */ isDrifting: boolean; /** Specific drift indicators */ indicators: DriftIndicator[]; /** Comparison with baseline */ baselineComparison: { toolDrift: number; topicDrift: number; sentimentDrift: number; responseLengthDrift: number; responseTimeDrift: number; errorRateDrift: number; }; /** Goal alignment score (if enabled) */ goalAlignment?: number; /** Recommendations */ recommendations: string[]; } export interface DriftIndicator { type: string; severity: "low" | "medium" | "high" | "critical"; description: string; currentValue: number | string; baselineValue: number | string; deviation: number; } export interface DriftDetectorResult { allowed: boolean; reason: string; request_id: string; analysis: DriftAnalysis; requires_review: boolean; kill_switch_recommended: boolean; } export declare class DriftDetector { private config; private samples; private baselines; private driftState; private goalDefinitions; constructor(config?: DriftDetectorConfig); /** * Record a behavior sample */ recordSample(agentId: string, sample: BehaviorSample): void; /** * Analyze current behavior for drift */ analyze(agentId: string, currentSample?: BehaviorSample, requestId?: string): DriftDetectorResult; /** * Set baseline manually */ setBaseline(agentId: string, baseline: BaselineProfile): void; /** * Get current baseline for an agent */ getBaseline(agentId: string): BaselineProfile | null; /** * Update baseline from collected samples */ updateBaseline(agentId: string): void; /** * Define goals for goal alignment checking */ defineGoals(agentId: string, goals: Record): void; /** * Get drift state for an agent */ isDrifting(agentId: string): boolean; /** * Get all agents with drift */ getDriftingAgents(): string[]; /** * Reset agent state */ resetAgent(agentId: string): void; /** * Get sample count for an agent */ getSampleCount(agentId: string): number; private calculateBaseline; private performAnalysis; private calculateToolDistribution; private calculateTopicDistribution; private distributionDivergence; private checkGoalAlignment; private mean; private stdDev; private generateRecommendations; }