Files
awoooi/docs/adr/ADR-100-ai-autonomous-slo.md
Your Name 025a493f06
Some checks failed
run-migration / migrate (push) Failing after 12s
CD Pipeline / build-and-deploy (push) Has been cancelled
feat(p3.2+adr-100): Model Version Tracker + SLO 自治 + KB rot cleaner
Wave 8 P3.2 模型版本追蹤 + ADR-100 SLO 自我治理 + 配套:

P3.2 — Model Version Tracking:
- model_version_probe.py (268 行) — 探測 Ollama / OpenRouter 等 provider 的 model version
- model_version_tracker.py (101 行) — 對齊 PG provider_version_history 表
- migrations/p3_2_provider_version_history.sql + rollback — 25 行 schema
- db/models.py +32 行 — ProviderVersionHistory ORM

ADR-100 — AI 自主化 SLO:
- docs/adr/ADR-100-ai-autonomous-slo.md (167 行) — 飛輪 SLO 設計與閾值
- ops/monitoring/slo-rules.yml (254 行) — Prometheus SLO recording rules + alerts
- ops/monitoring/tests/test_slo_rules.yaml (242 行) — promtool unit tests

整合修改:
- main.py +72 行 — Lifespan 啟動 model_version_probe + KB rot cleaner schedule
- gitea_webhook.py +45 行 — webhook 接收 model 版本變化通知
- ci_auto_repair.py / evidence_snapshot.py / pre_decision_investigator.py — 配合接線

新測試:
- test_kb_rot_cleaner_schedule.py (120 行) — 9 tests pass
- test_slo_rules.yaml — promtool 驗收

Tests: 9 passed (test_kb_rot_cleaner_schedule)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Multiple Engineers (P3.2 + ADR-100) <noreply@anthropic.com>
2026-04-27 14:54:19 +08:00

168 lines
5.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ADR-100: AI 自主化飛輪 SLO
<!-- 2026-04-27 P3.4 by Claude — AI SLO -->
## 狀態Active2026-04-27
## 背景
plan_complete_v3.0 P3.4 要求為 AI 自主化飛輪定義可量測的 SLOService Level Objectives
以便持續追蹤飛輪健康度,並在 SLO 違反時自動降級行為,防止錯誤決策擴散。
## 4 個 SLO
---
### SLO 1 — 自主化率 ≥ 80%
**定義**AI 自動執行的操作佔全部處理操作(含人工審核)的比例
**SLI 計算式**:
```promql
sum(rate(automation_operation_log_total{outcome="auto_executed"}[5m]))
/
sum(rate(automation_operation_log_total{}[5m]))
```
**Recording rule**: `sli:autonomy_rate:5m`
**目標值SLO**: ≥ 0.80
**Error budget28d**: 20%(即 28d × 20% = 5.6d 容許人工審核比例偏高)
**Burn rate alerts**:
| 視窗 | 消耗閾值 | 動作 |
|------|---------|------|
| Fast (1h) | budget × 14.4 = 2.88 | pagecritical |
| Medium (6h) | budget × 6 = 1.2 | ticketwarning |
| Slow (3d) | budget × 1 → 累積 10% | reviewinfo |
**SLO 違反降級行為**:
- `< 0.70`(硬紅線):降低 fusion auto-execute 閾值要求,發送 Telegram P0 告警
- `< 0.75`governance_agent 加強監控頻率30min → 15min
---
### SLO 2 — 決策準確率 ≥ 90%
**定義**自動執行後verifier 驗證通過的比例
**SLI 計算式**:
```promql
sum(rate(post_execution_verification_total{outcome="success"}[5m]))
/
sum(rate(automation_operation_log_total{outcome="auto_executed"}[5m]))
```
**Recording rule**: `sli:decision_accuracy:5m`
**目標值SLO**: ≥ 0.90
**Error budget28d**: 10%(比自主化率更嚴格)
**Burn rate alerts**:
| 視窗 | 消耗閾值 | 動作 |
|------|---------|------|
| Fast (1h) | budget × 14.4 = 1.44 | pagecritical |
| Medium (6h) | budget × 6 = 0.6 | ticketwarning |
| Slow (3d) | budget × 1 → 累積 10% | reviewinfo |
**SLO 違反降級行為**:
- `< 0.85`(硬紅線):**凍結 auto_execute**,全部降級為 `human_required`
直到滑動窗口回到 ≥ 0.90 才解凍
- `< 0.88`:增加 verifier 嚴格度(多一輪二次驗證)
---
### SLO 3 — 信心校準準確度 ≥ 80%
**定義**AI 信心值 ≥ 0.8 的決策中,實際驗證通過的比例(高信心不能亂說)
**SLI 計算式**:
```promql
sum(rate(approval_records_high_confidence_success_total[1h]))
/
sum(rate(approval_records_high_confidence_total[1h]))
```
**Recording rule**: `sli:confidence_calibration:1h`
**目標值SLO**: ≥ 0.80
**Error budget28d**: 20%
**Burn rate alerts**:
| 視窗 | 消耗閾值 | 動作 |
|------|---------|------|
| Fast (1h) | budget × 14.4 = 2.88 | pagecritical |
| Medium (6h) | budget × 6 = 1.2 | ticketwarning |
| Slow (3d) | budget × 1 → 累積 10% | reviewinfo |
**SLO 違反降級行為**:
- `< 0.70`(硬紅線):觸發 P3.3 fine-tune 重訓流程Telegram 通知人工介入
- `< 0.75`:將高信心閾值從 0.8 上調至 0.85(更嚴格的信心要求)
---
### SLO 4 — KM 增長率 ≥ +20 筆/day
**定義**:每 24h 新增的知識條目數,衡量飛輪學習輸出是否健康
**SLI 計算式**:
```promql
increase(knowledge_entries_total[24h])
```
**Recording rule**: `sli:km_growth_rate:24h`
**目標值SLO**: ≥ 20 筆/day
**Error budget**:不適用標準 burn rate絕對值 SLO改用閾值告警
**告警閾值**:
| 值 | 動作 |
|----|------|
| `< 20/day` | warning調查 KM 寫入路徑 |
| `< 5/day` | critical疑似 KM 鏈斷裂Telegram P0 告警 |
| `= 0` (持續 2h) | emergencygovernance_agent 立即執行診斷 |
**SLO 違反降級行為**:
- `< 5/day`(硬紅線):告警,疑似 KM 鏈又斷,自動觸發 `check_knowledge_degradation`
- `= 0` 持續 2h立即執行 `governance_agent.run_self_check()`
---
## SLO 違反時的降級行為矩陣
| SLO | 輕度違反 | 硬紅線違反 | 降級行為 |
|-----|---------|-----------|---------|
| SLO 1 自主化率 | < 0.75 | < 0.70 | 降低 fusion 閾值 + Telegram |
| SLO 2 決策準確率 | < 0.88 | < 0.85 | **凍結 auto_execute** |
| SLO 3 信心校準 | < 0.75 | < 0.70 | 觸發 fine-tune + 提高信心閾值 |
| SLO 4 KM 增長率 | < 20/day | < 5/day | 告警 + 觸發 KM 診斷 |
## 與 governance_agent 整合
`GovernanceAgent.check_slo_compliance()` 實作(`apps/api/src/services/governance_agent.py`
- 每 1h 執行(與既有 4 項自檢合併為第 5 項)
- 從 Prometheus Recording rules 讀取 SLI 值(`PROMETHEUS_URL` from settings
- 違反硬紅線時呼叫 `self._alert()` 寫 PG + 推 Telegram
- 異常隔離:任一 SLO 查詢失敗不阻斷其他項目
## 實作檔案
| 檔案 | 用途 |
|------|------|
| `ops/monitoring/slo-rules.yml` | Prometheus recording rules + 12 burn rate alerts |
| `ops/monitoring/tests/test_slo_rules.yaml` | promtool 單元測試 |
| `ops/monitoring/grafana/dashboards/ai-slo-dashboard.json` | Grafana SLO Dashboard |
| `apps/api/src/services/governance_agent.py` | `check_slo_compliance()` 整合 |
## 決策理由
1. **Recording rules 優先**SLI 計算複雜,先存 recording rule 避免 Grafana/alert 重複計算
2. **3 窗口 burn rate**:參考 Google SRE 書 6.5 節fast/medium/slow 三層防禦
3. **SLO 4 用絕對值**KM 增長率是累積計數burn rate 模型不適用
4. **governance_agent 整合**SLO 違反直接觸發降級行為,閉合飛輪的自我修復迴圈