fix(solver+execution): Checkpoint-1 假成功修復 + Checkpoint-2 K8s 環境感知
All checks were successful
CD Pipeline / build-and-deploy (push) Successful in 10m55s

## Checkpoint-1: 假成功根治
- approval_execution.py: execute_approved_action 改返回 bool
  (原返回 None,呼叫端無法判斷 K8s 是否接受指令)
- decision_manager.py auto-execute 路徑: 用 _exec_success 取代硬編 success=True
  修復: K8s 拒絕指令時正確發  而非  自動修復完成

## Checkpoint-2: K8s 環境感知 (Inventory Pre-flight)
- solver_agent.py: 新增 _fetch_k8s_inventory() — 生成 kubectl 指令前先拉
  kubectl get deployments,statefulsets -n awoooi-prod,將真實名稱清單
  注入 Solver prompt,LLM 必須從清單選擇,防止幻覺(awooiii-api 三個 i)
- 超時 5s 或失敗 → 返回 "",prompt 顯示警示但不中斷主流程

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-17 23:08:19 +08:00
parent bf835e51ac
commit cf50a5ce25
3 changed files with 80 additions and 6 deletions

View File

@@ -107,7 +107,7 @@ class ApprovalExecutionService:
# 瞬態錯誤 → 可重試
return any(kw in lower for kw in cls._TRANSIENT_ERROR_KEYWORDS)
async def execute_approved_action(self, approval: ApprovalRequest) -> None:
async def execute_approved_action(self, approval: ApprovalRequest) -> bool:
"""
背景執行已批准的操作
@@ -115,8 +115,16 @@ class ApprovalExecutionService:
Phase 5: 執行後更新資料庫狀態
Phase 6: 執行後發送通知 (Post-Execution Hook)
2026-04-17 ogt + Claude Sonnet 4.6: 返回 bool 表示 K8s 執行成功與否
根本原因: 原本返回 None → decision_manager.py auto-execute 路徑無法得知結果
→ 永遠傳 success=True 給 _push_auto_repair_result → 假成功廣播
修復: 返回 result.success讓呼叫端自行決定 Telegram 訊息
Args:
approval: 已批准的授權請求
Returns:
bool: True = K8s 執行成功False = 執行失敗(含解析失敗)
"""
from src.services.notifications import ExecutionStatus
@@ -164,7 +172,7 @@ class ApprovalExecutionService:
error_message="Could not parse operation type",
)
)
return
return False # 解析失敗 → 執行未發生
# ADR-076 Task 3: 執行失敗重試機制
# 瞬態錯誤 (connection refused, timeout 等) 自動重試,最多 MAX_RETRY 次
@@ -317,6 +325,8 @@ class ApprovalExecutionService:
except Exception as _resolve_e:
logger.warning("incident_resolve_after_execution_failed", error=str(_resolve_e))
return True # K8s 執行成功
else:
logger.error(
"background_execution_failed",
@@ -379,6 +389,7 @@ class ApprovalExecutionService:
approval_id=str(approval.id),
timeout_sec=30.0,
)
return False # K8s 執行失敗
async def _push_execution_result_to_alert(
self,

View File

@@ -2040,23 +2040,29 @@ class DecisionManager:
incident_id=incident.incident_id, error=str(_mb_err))
# 執行
# 2026-04-17 ogt + Claude Sonnet 4.6 (Checkpoint-1 假成功修復):
# 舊 bug: execute_approved_action 返回 None → 此處永遠傳 success=True 給
# _push_auto_repair_result → Telegram 顯示 ✅ 自動修復完成,即使 K8s 拒絕了指令
# 修復: execute_approved_action 現在返回 bool正確透傳給通知函數
executor = ApprovalExecutionService()
await executor.execute_approved_action(approval)
_exec_success = await executor.execute_approved_action(approval)
# 更新狀態
token.state = DecisionState.COMPLETED
token.proposal_data["auto_executed"] = True
token.proposal_data["exec_success"] = _exec_success
await self._save_token(token)
logger.info(
"auto_execute_completed",
incident_id=incident.incident_id,
action=approval.action,
exec_success=_exec_success,
)
# 2026-04-09 Claude Sonnet 4.6: 執行成 → 發 Telegram 結果通知
# 2026-04-09 Claude Sonnet 4.6: 執行成 → 發 Telegram 結果通知(成功或失敗皆發)
_fire_and_forget(
_push_auto_repair_result(incident, action, success=True)
_push_auto_repair_result(incident, action, success=_exec_success)
)
except Exception as e: