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:
@@ -208,6 +208,12 @@ class Playbook(BaseModel):
|
||||
total = self.success_count + self.failure_count
|
||||
return self.success_count / total if total > 0 else 0.0
|
||||
|
||||
@property
|
||||
def failure_rate(self) -> float:
|
||||
"""失敗率 (2026-03-30 Claude Code: Learning Service 信心度調整用)"""
|
||||
total = self.success_count + self.failure_count
|
||||
return self.failure_count / total if total > 0 else 0.0
|
||||
|
||||
@property
|
||||
def is_high_quality(self) -> bool:
|
||||
"""
|
||||
|
||||
@@ -244,6 +244,39 @@ class IPlaybookRepository(Protocol):
|
||||
"""更新執行統計"""
|
||||
...
|
||||
|
||||
async def find_by_source_incident(
|
||||
self,
|
||||
incident_id: str,
|
||||
) -> list[Playbook]:
|
||||
"""
|
||||
根據來源 Incident ID 找 Playbook
|
||||
|
||||
2026-03-30 Claude Code: Learning Service 信心度調整用
|
||||
尋找 source_incident_ids 包含此 incident_id 的 Playbooks
|
||||
"""
|
||||
...
|
||||
|
||||
async def adjust_confidence(
|
||||
self,
|
||||
playbook_id: str,
|
||||
delta: float,
|
||||
reason: str,
|
||||
) -> Playbook | None:
|
||||
"""
|
||||
調整 Playbook 信心度
|
||||
|
||||
2026-03-30 Claude Code: Learning Service 信心度調整用
|
||||
|
||||
Args:
|
||||
playbook_id: Playbook ID
|
||||
delta: 調整量 (+/- 0.0~1.0)
|
||||
reason: 調整原因 (審計用)
|
||||
|
||||
Returns:
|
||||
更新後的 Playbook,或 None (如果不存在)
|
||||
"""
|
||||
...
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class ILearningRepository(Protocol):
|
||||
|
||||
@@ -393,6 +393,118 @@ class PlaybookRepository:
|
||||
except Exception as e:
|
||||
logger.warning("playbook_index_update_failed", error=str(e))
|
||||
|
||||
# === Learning Service 信心度調整 (2026-03-30 Claude Code) ===
|
||||
|
||||
async def find_by_source_incident(
|
||||
self,
|
||||
incident_id: str,
|
||||
) -> list[Playbook]:
|
||||
"""
|
||||
根據來源 Incident ID 找 Playbook
|
||||
|
||||
2026-03-30 Claude Code: Learning Service 信心度調整用
|
||||
尋找 source_incident_ids 包含此 incident_id 的 Playbooks
|
||||
"""
|
||||
try:
|
||||
redis_client = get_redis()
|
||||
|
||||
# 掃描所有 Playbook keys
|
||||
pattern = f"{PLAYBOOK_KEY_PREFIX}PB-*"
|
||||
results: list[Playbook] = []
|
||||
|
||||
async for key in redis_client.scan_iter(match=pattern, count=100):
|
||||
data = await redis_client.get(key)
|
||||
if data:
|
||||
playbook = Playbook.from_redis_dict(json.loads(data))
|
||||
if incident_id in playbook.source_incident_ids:
|
||||
results.append(playbook)
|
||||
|
||||
return results
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"playbook_find_by_incident_failed",
|
||||
incident_id=incident_id,
|
||||
error=str(e),
|
||||
)
|
||||
return []
|
||||
|
||||
async def adjust_confidence(
|
||||
self,
|
||||
playbook_id: str,
|
||||
delta: float,
|
||||
reason: str,
|
||||
) -> Playbook | None:
|
||||
"""
|
||||
調整 Playbook 信心度
|
||||
|
||||
2026-03-30 Claude Code: Learning Service 信心度調整用
|
||||
|
||||
邏輯:
|
||||
- ai_confidence += delta (clamp 到 0.0~1.0)
|
||||
- 若信心度 >= 0.9 且 status == DRAFT → 自動升級為 APPROVED
|
||||
- 若信心度 < 0.3 且 failure_rate > 50% → 自動降級為 DEPRECATED
|
||||
"""
|
||||
try:
|
||||
playbook = await self.get_by_id(playbook_id)
|
||||
if not playbook:
|
||||
return None
|
||||
|
||||
old_confidence = playbook.ai_confidence
|
||||
|
||||
# 調整信心度 (clamp 到 0.0~1.0)
|
||||
playbook.ai_confidence = max(0.0, min(1.0, playbook.ai_confidence + delta))
|
||||
|
||||
# 狀態自動轉換
|
||||
old_status = playbook.status
|
||||
|
||||
# 高信心度自動升級
|
||||
if (
|
||||
playbook.ai_confidence >= 0.9
|
||||
and playbook.status == PlaybookStatus.DRAFT
|
||||
):
|
||||
playbook.status = PlaybookStatus.APPROVED
|
||||
playbook.approved_by = "auto_learning"
|
||||
playbook.approved_at = now_taipei()
|
||||
note = f"\n[Auto-approved: confidence {playbook.ai_confidence:.2f}]"
|
||||
playbook.notes = (playbook.notes or "") + note
|
||||
|
||||
# 低信心度 + 高失敗率 → 棄用
|
||||
elif (
|
||||
playbook.ai_confidence < 0.3
|
||||
and playbook.total_executions >= 5
|
||||
and playbook.failure_rate > 0.5
|
||||
):
|
||||
playbook.status = PlaybookStatus.DEPRECATED
|
||||
conf = playbook.ai_confidence
|
||||
fail = playbook.failure_rate
|
||||
note = f"\n[Auto-deprecated: conf={conf:.2f}, fail={fail:.0%}]"
|
||||
playbook.notes = (playbook.notes or "") + note
|
||||
|
||||
# 儲存
|
||||
updated = await self.update(playbook)
|
||||
|
||||
logger.info(
|
||||
"playbook_confidence_adjusted",
|
||||
playbook_id=playbook_id,
|
||||
old_confidence=old_confidence,
|
||||
new_confidence=playbook.ai_confidence,
|
||||
old_status=old_status.value,
|
||||
new_status=playbook.status.value,
|
||||
reason=reason,
|
||||
)
|
||||
|
||||
return updated
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"playbook_confidence_adjust_failed",
|
||||
playbook_id=playbook_id,
|
||||
delta=delta,
|
||||
error=str(e),
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Singleton
|
||||
|
||||
@@ -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 修正: 新增方法
|
||||
|
||||
Reference in New Issue
Block a user