fix(aiops): suppress repeated llm alert loops
This commit is contained in:
@@ -124,6 +124,21 @@ def _backfill_kubectl_command(proposal: dict, tools: list) -> None:
|
||||
# OpenClaw Service
|
||||
# =============================================================================
|
||||
|
||||
def _build_alert_cache_context_hash(alert_context: dict | None) -> str:
|
||||
"""Build a stable LLM cache scope for repeat alerts without dynamic annotations."""
|
||||
|
||||
if not alert_context:
|
||||
return ""
|
||||
|
||||
alertname = alert_context.get("alertname") or alert_context.get("alert_type", "")
|
||||
category = alert_context.get("alert_category", "")
|
||||
namespace = alert_context.get("namespace", "")
|
||||
target = alert_context.get("target_resource", "")
|
||||
severity = alert_context.get("severity", "")
|
||||
fingerprint = alert_context.get("fingerprint", "")
|
||||
return f"{alertname}:{category}:{namespace}:{target}:{severity}:{fingerprint}"
|
||||
|
||||
|
||||
class OpenClawService:
|
||||
"""
|
||||
OpenClaw AI 決策服務 - True LLM + SignOz Integration
|
||||
@@ -727,9 +742,19 @@ class OpenClawService:
|
||||
"""
|
||||
生成 LLM 快取鍵
|
||||
|
||||
使用 prompt 內容的 SHA256 作為快取鍵,確保相同問題不重複呼叫 LLM
|
||||
有告警上下文時,使用 prompt family + 穩定告警維度,避免 annotations /
|
||||
SignOz 即時數值讓同一告警每 20 秒打穿快取;沒有上下文時仍用完整 prompt。
|
||||
"""
|
||||
content = f"{prompt}:{context_hash}"
|
||||
if context_hash:
|
||||
prompt_family_source = (
|
||||
"openclaw_alert_analysis"
|
||||
if "## Alert Data:" in prompt
|
||||
else prompt[:512]
|
||||
)
|
||||
prompt_family = hashlib.sha256(prompt_family_source.encode()).hexdigest()[:8]
|
||||
content = f"{prompt_family}:{context_hash}"
|
||||
else:
|
||||
content = prompt
|
||||
hash_digest = hashlib.sha256(content.encode()).hexdigest()[:16]
|
||||
return f"llm_cache:{hash_digest}"
|
||||
|
||||
@@ -760,12 +785,7 @@ class OpenClawService:
|
||||
# 2026-04-16 ogt + Claude Sonnet 4.6: 修復 — alertname 才是主要識別符
|
||||
# 舊版用 alert_type:target_resource → 不同告警 (e.g. PostgreSQLDiskGrowth vs PodCrashLoop)
|
||||
# 在 alert_type="custom" 時共用同一快取鍵 → 全部回傳相同 LLM 結果
|
||||
context_hash = ""
|
||||
if alert_context:
|
||||
# alertname 優先;無 alertname 時 fallback 到 alert_type
|
||||
_alertname = alert_context.get("alertname") or alert_context.get("alert_type", "")
|
||||
_target = alert_context.get("target_resource", "")
|
||||
context_hash = f"{_alertname}:{_target}"
|
||||
context_hash = _build_alert_cache_context_hash(alert_context)
|
||||
|
||||
cache_key = self._generate_cache_key(prompt, context_hash)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user