fix(aiops): suppress repeated llm alert loops
Some checks failed
CD Pipeline / tests (push) Successful in 1m37s
Code Review / ai-code-review (push) Successful in 28s
CD Pipeline / post-deploy-checks (push) Has been cancelled
CD Pipeline / build-and-deploy (push) Has been cancelled

This commit is contained in:
Your Name
2026-05-01 13:02:07 +08:00
parent 3691402561
commit 9db87f177e
9 changed files with 244 additions and 24 deletions

View File

@@ -4,6 +4,10 @@ from src.api.v1.webhooks import (
_should_bypass_alertmanager_llm,
_should_use_alertmanager_rule_first,
)
from src.services.alertmanager_llm_guard import (
ALERTMANAGER_LLM_INFLIGHT_LOCK_TTL_SECONDS,
alertmanager_llm_inflight_key,
)
from src.services.decision_manager import (
_is_host_layer_ssh_category,
_is_non_k8s_host_category,
@@ -100,6 +104,13 @@ def test_backup_failure_blocks_k8s_auto_execute():
assert _is_non_k8s_host_category("infrastructure") is False
def test_alertmanager_llm_inflight_lock_key_is_fingerprint_scoped():
fingerprint = "abc123"
assert alertmanager_llm_inflight_key(fingerprint) == "alertmanager:llm_inflight:abc123"
assert ALERTMANAGER_LLM_INFLIGHT_LOCK_TTL_SECONDS == 600
def test_resolved_guard_stamp_without_timestamp_is_clean():
assert _format_resolved_guard_stamp(None) == "✅ 此事件已解決"

View File

@@ -0,0 +1,42 @@
from src.services.openclaw import OpenClawService, _build_alert_cache_context_hash
def test_openclaw_cache_key_uses_stable_alert_scope_when_context_hash_exists():
service = object.__new__(OpenClawService)
context_hash = "HostBackupFailed:backup_failure:awoooi-prod:awoooi-frequent:critical:fp-1"
prompt_a = "System prompt and instructions\n\n## Alert Data:\ncurrent_value=1"
prompt_b = "System prompt and instructions\n\n## Alert Data:\ncurrent_value=2"
assert service._generate_cache_key(prompt_a, context_hash) == service._generate_cache_key(
prompt_b,
context_hash,
)
def test_openclaw_cache_key_keeps_full_prompt_specificity_without_context_hash():
service = object.__new__(OpenClawService)
prompt_a = "System prompt and instructions\n\n## Alert Data:\ncurrent_value=1"
prompt_b = "System prompt and instructions\n\n## Alert Data:\ncurrent_value=2"
assert service._generate_cache_key(prompt_a) != service._generate_cache_key(prompt_b)
def test_openclaw_alert_cache_context_hash_ignores_dynamic_annotations():
base_context = {
"alertname": "HostBackupFailed",
"alert_category": "backup_failure",
"namespace": "awoooi-prod",
"target_resource": "awoooi-frequent",
"severity": "critical",
"fingerprint": "fp-1",
"annotations": {"description": "failed at 14:00"},
}
next_context = {
**base_context,
"annotations": {"description": "failed at 14:01"},
"message": "new volatile message",
}
assert _build_alert_cache_context_hash(base_context) == _build_alert_cache_context_hash(
next_context
)