Chowdarysaritha311/autonomous-incident-response
GitHub: Chowdarysaritha311/autonomous-incident-response
一个基于多智能体协作的自主事件响应系统,通过纯规则和统计推理对多源运维数据进行取证分析,实现零幻觉的根因定位与事件报告生成。
Stars: 0 | Forks: 0
# 自主事件响应系统
一个生产级、模块化、多智能体的 AI 系统,用于自动化事件调查。该系统使用基于工具的推理处理来自多源的原始事件数据(日志、指标、警报、聊天、运维手册),并严格执行证据溯源——每一项声明都引用了源文件和行号。
## 架构
```
╔══════════════════════════════════════════════════════════════════════╗
║ AUTONOMOUS INCIDENT RESPONSE SYSTEM ║
╠══════════════════════════════════════════════════════════════════════╣
║ ║
║ ┌─────────────────────────────────────────────────────────────┐ ║
║ │ DATA INGESTION LAYER │ ║
║ │ alerts.json metrics.csv logs/*.log chat.txt runbook.md │ ║
║ │ (Safety: chat + runbook treated as untrusted data only) │ ║
║ └─────────────────────┬───────────────────────────────────────┘ ║
║ │ normalized IncidentState.raw_data ║
║ ┌─────────────────────▼───────────────────────────────────────┐ ║
║ │ TOOLING LAYER (8 tools) │ ║
║ │ │ ║
║ │ LogSearchTool MetricsAnalyzerTool AnomalyDetector │ ║
║ │ EntityExtractor RunbookEngine EvidenceIndexer │ ║
║ │ TimelineBuilder ConflictResolver │ ║
║ │ │ ║
║ │ Each tool: typed input/output schema, independent, audited │ ║
║ └─────────────────────┬───────────────────────────────────────┘ ║
║ │ ToolResult(data, evidence, confidence) ║
║ ┌─────────────────────▼───────────────────────────────────────┐ ║
║ │ STATE MACHINE (LangGraph-style flow) │ ║
║ │ │ ║
║ │ INGEST → INDEX → TRIAGE → INVESTIGATE → HYPOTHESIZE │ ║
║ │ ↓ │ ║
║ │ VERIFY ──(fail)──→ retry loop │ ║
║ │ ↓ (pass) │ ║
║ │ REPORT │ ║
║ └─────────────────────┬───────────────────────────────────────┘ ║
║ │ ║
║ ┌─────────────────────▼───────────────────────────────────────┐ ║
║ │ MULTI-AGENT SYSTEM (5 agents) │ ║
║ │ │ ║
║ │ CoordinatorAgent ─── orchestrates flow + retry │ ║
║ │ TriageAgent ─── severity, services, start time │ ║
║ │ ForensicsAgent ─── deep log/metric/anomaly analysis │ ║
║ │ HypothesisAgent ─── 3 ranked root-cause hypotheses │ ║
║ │ VerifierAgent ─── evidence audit, hallucination guard │ ║
║ └─────────────────────┬───────────────────────────────────────┘ ║
║ │ ║
║ ┌─────────────────────▼───────────────────────────────────────┐ ║
║ │ OUTPUT LAYER │ ║
║ │ incident_report.md action_items.json exec_log.json │ ║
║ └─────────────────────────────────────────────────────────────┘ ║
╚══════════════════════════════════════════════════════════════════════╝
```
## 工具说明
| 工具 | 目的 | 核心设计 |
|---|---|---|
| `LogSearchTool` | 跨多服务日志文件的正则表达式 + 时间窗口搜索 | 每次匹配返回 `file:line` 的证据引文 |
| `MetricsAnalyzerTool` | 时序分析:基线统计、阈值突破检测、首次突破时间戳 | 处理逆向指标(hit_rate:越低越差) |
| `AnomalyDetectorTool` | 基于滚动基线的统计 z-score 异常值检测 | 可配置 z 阈值,返回逐点偏差百分比 |
| `EntityExtractorTool` | 从原始文本中提取服务、HTTP 状态码、端点和负责人 | 仅安全提取——除模式匹配外不作任何推断 |
| `RunbookEngineTool` | 应用 SOP 规则,根据症状分类严重程度 | **提示注入安全**:检测并忽略运维手册中类似指令的内容 |
| `EvidenceIndexerTool` | 集中式声明注册表:映射 声明 → 引文,计算幻觉风险百分比 | 将没有任何证据的声明标记为 `UNVERIFIED` |
| `TimelineBuilderTool` | 将异构事件合并为带有置信度分数的按时间顺序排列的时间线 | 相互印证提升:来自不同来源的相近事件会提高置信度 |
| `ConflictResolverTool` | 使用来源优先级排序解决矛盾信号 | 优先级:logs(10) > metrics(9) > alerts(7) > runbook(5) > chat(2) |
## 智能体角色
### TriageAgent
**特质:** 快速、果断的第一响应者。不过度思考——迅速确立事实。
**职责:** 识别受影响的服务,分类 P1/P2/P3 严重程度,确定事件开始时间。
**工具:** `log_search`, `metrics_analyzer`, `entity_extractor`, `runbook_engine`
**输出:** `state.severity`, `state.impacted_services`, `state.incident_start`
### ForensicsAgent
**特质:** 细致严谨的侦探。只追踪证据,不依赖假设。
**职责:** 深入日志调查因果事件(VACUUM、连接池、断路器、重启循环)。对指标序列运行异常检测。构建初始时间线。
**工具:** `log_search`, `metrics_analyzer`, `anomaly_detector`, `entity_extractor`, `timeline_builder`
**输出:** `state.log_findings`, `state.anomalies`, `state.timeline`
### HypothesisAgent
**特质:** 具备系统思维的分析师。推理因果链,而不仅仅是相关性。
**职责:** 生成精确的 3 个按概率排序的假设。验证事件的因果顺序。向 EvidenceIndexer 注册声明。
**工具:** `log_search`, `conflict_resolver`, `evidence_indexer`
**输出:** `state.hypotheses`(按概率排序,包含因果链和证据参考)
### VerifierAgent(关键)
**特质:** 怀疑一切的审计员。没有引文就不信任任何内容。
**职责:**
- 拒绝证据覆盖率 < 70% 的假设
- 将没有 file:line 支持的声明标记为 UNVERIFIED
- 计算幻觉风险分数
- 如果发现硬性失败,强制重新进入流水线(最多重试 2 次)
- 通过 ConflictResolver 解决时间戳冲突
**工具:** `evidence_indexer`, `conflict_resolver`, `log_search`
**输出:** `state.verified`, `state.hallucination_risk`, `state.verification_issues`
### CoordinatorAgent
**特质:** 空中交通管制员。管理流程,不干涉内容。
**职责:** 状态机执行、数据摄取、重试编排、聊天输入的安全清理。
**不是 BaseAgent 的子类**——管理的是智能体而不是工具。
## 安全设计
| 威胁 | 缓解措施 |
|---|---|
| `chat.txt` 中的提示注入 | `CoordinatorAgent._sanitize_chat()` 在摄取前剥离类似指令的行 |
| `runbook.md` 中的提示注入 | `RunbookEngineTool` 扫描注入信号,记录警告,并将文件仅视为数据 |
| 智能体滥用工具 | `BaseAgent._call_tool()` 强制执行每个智能体的工具白名单——违规时引发 `PermissionError` |
| `eval()` / shell 执行 | 代码库中任何地方都不存在。没有 `subprocess`,没有 `eval`,没有 `exec` |
| 幻觉 | `EvidenceIndexerTool` 追踪所有声明;`VerifierAgent` 拒绝未验证率过高的报告 |
| 伪造证据 | 所有证据对象都需要 `source_file` + `start_line` 或 `timestamp`——不允许纯文本引文 |
## 高级特性
| 特性 | 实现 |
|---|---|
| **置信度评分** | 每个假设都会获得 probability + evidence_coverage + causal_order_verified |
| **因果图** | `HypothesisAgent` 为每个假设构建明确的因果链数组 |
| **重试循环** | `VerifierAgent` 返回 `VERIFICATION_FAILED_RETRY`;Coordinator 最多重新运行流水线 2 次 |
| **服务依赖映射** | 追踪级联故障:DB → payment-service → api-gateway → order-service |
| **时间关联引擎** | `ConflictResolverTool` 解决时间戳冲突;`TimelineBuilderTool` 评估相互印证性 |
| **噪声过滤** | 聊天清理消除注入内容 + `EntityExtractorTool` 处理混乱输入 |
| **可解释性层** | 每个 `ToolResult` 都包含 `reasoning` 字符串,解释该工具为何产生该输出 |
| **来源优先级排序** | `ConflictResolverTool` 使用数字优先级层级来确定性地解决矛盾 |
## 评估结果
在 12 个场景下对标黄金用例 `gold/expected.json` 进行了测试:
| 指标 | 结果 |
|---|---|
| 测试通过率 | **12/12 (100%)** |
| 工具调用正确率 | **100%** |
| 幻觉率 | **0.0%** |
| 证据覆盖率 | **100%** |
| 时间线事件数 | **34** |
| 黄金时间线覆盖率 | **10/10 锚点 (100%)** |
| 严重程度匹配 | **✅ P1** |
| 根本原因匹配 | **✅ H1** |
| 最高假设置信度 | **89.3%** |
| 流水线所需重试次数 | **0** |
| 端到端延迟 | **~112ms** |
### 测试场景
| ID | 场景 | 类型 |
|---|---|---|
| T01 | 基础日志模式匹配 | 单元 |
| T02 | 时间窗口排除(事件前无误报) | 单元 |
| T03 | 指标飙升检测(error_rate) | 单元 |
| T04 | 统计异常检测(z-score) | 单元 |
| T05 | 运维手册中的提示注入防御 | 安全 |
| T06 | 对无支撑声明的幻觉检测 | 安全 |
| T07 | 冲突解决:时间戳以日志 > 聊天为准 | 正确性 |
| T08 | 时间线按时间顺序排序 | 正确性 |
| T09 | 日志缺失——优雅地返回空结果 | 边界情况 |
| T10 | 无日志相互印证的指标飙升检测 | 边界情况 |
| T11 | 从充满噪声的聊天输入中提取实体 | 边界情况 |
| T12 | 完整流水线对标黄金用例(集成) | 集成 |
## 项目结构
```
/incident_response
main.py # Entry point + tool wiring
report_generator.py # MD + JSON output generator
agents/
__init__.py
base.py # IncidentState, AgentResult, BaseAgent
triage.py # TriageAgent
forensics.py # ForensicsAgent
hypothesis.py # HypothesisAgent
verifier.py # VerifierAgent (critic)
coordinator.py # CoordinatorAgent (state machine)
tools/
__init__.py
base.py # BaseTool, ToolResult, Evidence schema
log_search.py # LogSearchTool
metrics_analyzer.py # MetricsAnalyzerTool
anomaly_detector.py # AnomalyDetectorTool + EntityExtractorTool + RunbookEngineTool
evidence_indexer.py # EvidenceIndexerTool + TimelineBuilderTool + ConflictResolverTool
data/
alerts.json # 7 Prometheus/Datadog/CloudWatch/K8s alerts
metrics.csv # 75 metric data points across 4 services
chat.txt # Noisy Slack-style incident channel (with injection attempt)
runbook.md # SOP runbook (with injection attempt)
logs/
payment-service.log # 34 log lines covering full incident lifecycle
database-primary.log # 32 log lines: VACUUM, lock, connection exhaustion
api-gateway.log # 15 log lines: latency, circuit breaker
gold/
expected.json # Ground truth: services, severity, timeline anchors, root cause
tests/
test_suite.py # 12 test scenarios with evaluation metrics
output/
incident_report.md # Generated incident report with evidence citations
action_items.json # 7 prioritized action items with justifications
execution_log.json # Full pipeline audit log + reasoning trace
```
## 前提假设
1. 所有日志时间戳均为 UTC ISO-8601 格式(`YYYY-MM-DDTHH:MM:SSZ`)
2. `metrics.csv` 使用一致的列结构:`timestamp,service,metric_name,value,unit,threshold_warn,threshold_crit`
3. 服务通过在日志、指标和警报之间匹配的名称字符串进行识别
4. 所有指标“越高越差”,除了 `hit_rate`(越低越差)
5. 来自任何来源(Prometheus、Datadog、CloudWatch、PagerDuty、K8s)的警报在摄取时均已规范化
## 局限性
1. **无 LLM 集成**——推理基于规则和统计。对于模板库中没有的新型故障模式,LLM 可以改进假设的生成。
2. **单次运行因果模板**——`HypothesisAgent` 使用预定义的假设模板。新的故障模式需要添加模板。
3. **无分布式追踪**——在没有 trace ID 的情况下,系统无法关联跨服务的单个请求。
4. **同步流水线**——智能体按顺序运行。生产部署将受益于并行取证分析。
5. **仅限英文聊天解析**——`EntityExtractor` 和聊天清理仅支持英文。
## 运行系统
```
# 运行完整流水线
python main.py
# 自定义数据目录
python main.py --data-dir /path/to/incident/data --output-dir /path/to/output
# 运行测试套件
python tests/test_suite.py
```
无需外部依赖——仅需纯 Python 3.8+ 标准库。
标签:AIOps, AI Pipeline, AI网络安全, IT运维, LLM推理, PyRIT, Runbook自动化, SecOps, Socks5代理, 云安全架构, 告警处理, 基础设施事件, 多智能体系统, 多源数据融合, 安全运营, 扫描框架, 指标监控, 数据接入, 模块化架构, 生产级, 自主事件响应系统, 自动化调查, 证据溯源, 逆向工具, 零幻觉