feat(Phase 3): 學習閉環重建 — 三根因修復 + 2x EWMA + Evolver Agent
Some checks failed
CD Pipeline / build-and-deploy (push) Failing after 19m7s
Type Sync Check / check-type-sync (push) Failing after 1m18s

ADR-083 Phase 3 學習閉環重建:

**三根因修復**
- approval_execution.py: fire-and-forget create_task → await asyncio.wait_for(timeout=30) × 2
  (成功路徑 L265 + 失敗路徑 L353,超時記錄 learning_trigger_timeout metric,主流程不 crash)
- models/approval.py: ApprovalRequestBase 新增 matched_playbook_id 欄位
- decision_manager.py: _auto_execute 建立 ApprovalRequest 時填充 matched_playbook_id
- learning_service.py: 雙路徑查找 _matched_pb_id(matched_playbook_id + metadata fallback)

**2x EWMA 負向強化**
- models/playbook.py: 新增 trust_score: float = 0.3(EWMA 動態信任度欄位)
- repositories/playbook_repository.py: update_stats 加 EWMA
  成功: trust = 0.9 × old + 0.1 × 1.0
  失敗: trust = 0.8 × old + 0.2 × 0.0(衰減速度 2x)
  trust < 0.1 → log warning,等 Evolver 封存

**Evolver Agent(新建)**
- services/playbook_evolver.py: 三功能全靜態規則
  1. 低信任封存: trust < 0.1 → DEPRECATED
  2. 休眠封存: 30d 未使用 AND trust < 0.5 → DEPRECATED
  3. 相似合併: 症狀 Jaccard > 0.9 → 保留高 trust,封存低 trust
  AIOPS_P3_EVOLVER_ENABLED=False 預設關閉

**文件**
- ADR-083 學習閉環重建
- MASTER §8 Phase 3 完工記錄

AIOPS_P3_ENABLED=False(預設),骨架就位等統帥批准開啟

Co-Authored-By: Claude Sonnet 4.6(亞太)<noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-15 14:01:28 +08:00
parent 7edb298a75
commit 7da64eaad2
9 changed files with 565 additions and 20 deletions

View File

@@ -292,7 +292,15 @@ class PlaybookRepository:
playbook_id: str,
success: bool,
) -> bool:
"""更新執行統計"""
"""
更新執行統計 + EWMA 信任度
ADR-083 Phase 3: 負向 2x 強化 EWMA 公式
成功: trust_new = 0.9 * trust_old + 0.1 * 1.0
失敗: trust_new = 0.8 * trust_old + 0.2 * 0.0(衰減速度 2x
trust < 0.1 → 記錄警告,由 Evolver Agent 封存
2026-04-15 ogt + Claude Sonnet 4.6(亞太): Phase 3 EWMA 實裝
"""
try:
playbook = await self.get_by_id(playbook_id)
if not playbook:
@@ -300,17 +308,32 @@ class PlaybookRepository:
if success:
playbook.success_count += 1
# 正向 EWMAalpha=0.1,正向結果權重較小(保守更新)
playbook.trust_score = 0.9 * playbook.trust_score + 0.1 * 1.0
else:
playbook.failure_count += 1
# 負向 EWMAalpha=0.2,失敗懲罰 2x快速衰退
playbook.trust_score = 0.8 * playbook.trust_score + 0.2 * 0.0
# 邊界保護
playbook.trust_score = max(0.0, min(1.0, playbook.trust_score))
playbook.last_used_at = now_taipei()
await self.update(playbook)
if playbook.trust_score < 0.1:
logger.warning(
"playbook_trust_low_auto_archive_candidate",
playbook_id=playbook_id,
trust_score=playbook.trust_score,
hint="Evolver Agent 應將此 Playbook 封存",
)
logger.info(
"playbook_stats_updated",
playbook_id=playbook_id,
success=success,
success_rate=playbook.success_rate,
trust_score=playbook.trust_score,
)
return True