/** * AnomalyDetector — Baseline agent behavior and flag statistical outliers. * * Maintains rolling statistics (mean, stddev) per agent for latency, token * usage, and error rate. New observations are checked against the baseline * and flagged as anomalies when they exceed a configurable z-score threshold. * * Uses Welford's online algorithm for numerically stable incremental * mean/variance — no batch recomputation needed. * * @module AnomalyDetector */ /** A detected anomaly */ export interface Anomaly { /** ISO 8601 timestamp */ timestamp: string; /** Agent that triggered the anomaly */ agentId: string; /** Which metric is anomalous */ metric: AnomalyMetric; /** Observed value */ observed: number; /** Baseline mean for this agent + metric */ baselineMean: number; /** Baseline standard deviation */ baselineStddev: number; /** Z-score of the observation */ zScore: number; /** Severity based on z-score magnitude */ severity: 'warning' | 'critical'; } /** Metrics that are monitored for anomalies */ export type AnomalyMetric = 'latency' | 'tokens' | 'errorRate'; /** Summary of an agent's baseline */ export interface BaselineSummary { agentId: string; latency: { mean: number; stddev: number; samples: number; }; tokens: { mean: number; stddev: number; samples: number; }; errorRate: { mean: number; stddev: number; samples: number; }; } /** * Monitors agent behavior and flags statistical anomalies. * * @example * ```ts * const detector = new AnomalyDetector(); * * // Feed observations as they occur: * detector.observe('agent-1', { latencyMs: 200, tokens: 500, success: true }); * detector.observe('agent-1', { latencyMs: 15000, tokens: 500, success: true }); * // ^ second call may flag latency anomaly if baseline is ~200ms * * const anomalies = detector.getAnomalies(); * const baseline = detector.getBaseline('agent-1'); * ``` */ export declare class AnomalyDetector { private baselines; private anomalies; private maxAnomalies; private warningThreshold; private criticalThreshold; private minSamples; private errorWindow; constructor(options?: { /** Z-score threshold for 'warning' level. Default 2.0 */ warningThreshold?: number; /** Z-score threshold for 'critical' level. Default 3.0 */ criticalThreshold?: number; /** Minimum samples before anomaly detection activates. Default 10 */ minSamples?: number; /** Max anomalies to retain. Default 1000 */ maxAnomalies?: number; }); /** * Record an observation for an agent and check for anomalies. * Returns any anomalies detected from this observation. */ observe(agentId: string, obs: { latencyMs: number; tokens: number; success: boolean; }): Anomaly[]; /** Get all recorded anomalies, optionally filtered by agent */ getAnomalies(agentId?: string): Anomaly[]; /** Get recent anomalies (last N) */ getRecentAnomalies(count: number): Anomaly[]; /** Get the baseline summary for an agent */ getBaseline(agentId: string): BaselineSummary | null; /** List all agents with baselines */ listAgents(): string[]; /** Total anomalies recorded */ get anomalyCount(): number; /** Clear all baselines and anomalies */ clear(): void; /** Clear data for a specific agent */ clearAgent(agentId: string): void; private checkMetric; } //# sourceMappingURL=anomaly-detector.d.ts.map