feat(learning): 實作 Playbook 信心度調整機制 (ADR-030)

- 新增 _promote_playbook: 高評分提升信心度 +0.1
- 新增 _demote_playbook: 低評分降低信心度 -0.15
- 新增 find_by_source_incident: 按 incident_id 查詢 Playbook
- 新增 adjust_confidence: 信心度調整 + 狀態自動轉換
- 新增 Playbook.failure_rate 屬性

自動狀態轉換:
- ai_confidence >= 0.9 + DRAFT → 自動 APPROVED
- ai_confidence < 0.3 + failure_rate > 50% → 自動 DEPRECATED

測試: 13 案例全部通過

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-03-29 22:10:49 +08:00
parent a0ef323d75
commit f5b19cf108
6 changed files with 553 additions and 14 deletions

View File

@@ -432,16 +432,112 @@ class LearningService:
return None
async def _promote_playbook(self, incident_id: str) -> bool:
"""提升 Playbook 信心度 (高評分)"""
# TODO: 實作 Playbook 信心度提升邏輯
logger.debug("playbook_promoted", incident_id=incident_id)
return True
"""
提升 Playbook 信心度 (高評分)
2026-03-30 Claude Code: 實作信心度提升邏輯
邏輯:
- 尋找 source_incident_ids 包含此 incident_id 的 Playbooks
- 提升 ai_confidence +0.1 (上限 1.0)
- 若信心度 >= 0.9 且 status == DRAFT → 自動升級為 APPROVED
"""
try:
from src.repositories.playbook_repository import get_playbook_repository
repo = get_playbook_repository()
playbooks = await repo.find_by_source_incident(incident_id)
if not playbooks:
logger.debug(
"playbook_promote_no_match",
incident_id=incident_id,
)
return False
# 信心度提升參數
CONFIDENCE_BOOST = 0.1
updated_count = 0
for playbook in playbooks:
result = await repo.adjust_confidence(
playbook_id=playbook.playbook_id,
delta=CONFIDENCE_BOOST,
reason=f"High effectiveness rating from incident {incident_id}",
)
if result:
updated_count += 1
logger.info(
"playbook_promoted",
incident_id=incident_id,
updated_count=updated_count,
total_playbooks=len(playbooks),
)
return updated_count > 0
except Exception as e:
logger.warning(
"playbook_promote_failed",
incident_id=incident_id,
error=str(e),
)
return False
async def _demote_playbook(self, incident_id: str) -> bool:
"""降低 Playbook 信心度 (低評分)"""
# TODO: 實作 Playbook 信心度降低邏輯
logger.debug("playbook_demoted", incident_id=incident_id)
return True
"""
降低 Playbook 信心度 (低評分)
2026-03-30 Claude Code: 實作信心度降低邏輯
邏輯:
- 尋找 source_incident_ids 包含此 incident_id 的 Playbooks
- 降低 ai_confidence -0.15 (下限 0.0)
- 若信心度 < 0.3 且 failure_rate > 50% → 自動降級為 DEPRECATED
"""
try:
from src.repositories.playbook_repository import get_playbook_repository
repo = get_playbook_repository()
playbooks = await repo.find_by_source_incident(incident_id)
if not playbooks:
logger.debug(
"playbook_demote_no_match",
incident_id=incident_id,
)
return False
# 信心度降低參數 (懲罰比獎勵更重,避免低品質 Playbook 累積)
CONFIDENCE_PENALTY = -0.15
updated_count = 0
for playbook in playbooks:
result = await repo.adjust_confidence(
playbook_id=playbook.playbook_id,
delta=CONFIDENCE_PENALTY,
reason=f"Low effectiveness rating from incident {incident_id}",
)
if result:
updated_count += 1
logger.info(
"playbook_demoted",
incident_id=incident_id,
updated_count=updated_count,
total_playbooks=len(playbooks),
)
return updated_count > 0
except Exception as e:
logger.warning(
"playbook_demote_failed",
incident_id=incident_id,
error=str(e),
)
return False
# =========================================================================
# 🆕 Phase D-G P0 修正: 新增方法