fix(decision_manager): 修復 Telegram 重複發送問題

問題:
- Phase 6.5 (765ee39) 修改導致每次 poll 都建立新 decision
- 觸發 Telegram 轟炸 (INC-INC-INC- prefix bug)

修復:
- 移除 INC- 重複前綴 (line 83)
- 加入 Redis 去重機制 (10 分鐘 TTL)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-03-26 19:21:36 +08:00
parent d071019cf6
commit 35aa690bf1

View File

@@ -59,6 +59,18 @@ async def _push_decision_to_telegram(
get_telegram_gateway,
)
# 2026-03-26 修復: 防止重複發送 Telegram (每 incident 10 分鐘只發一次)
redis_client = get_redis()
dedup_key = f"telegram_sent:{incident.incident_id}"
already_sent = await redis_client.get(dedup_key)
if already_sent:
logger.debug(
"telegram_push_skipped_dedup",
incident_id=incident.incident_id,
reason="Already sent within 10 minutes",
)
return
# 檢查是否有設定 Bot Token
if not settings.OPENCLAW_TG_BOT_TOKEN:
logger.debug(
@@ -79,8 +91,8 @@ async def _push_decision_to_telegram(
confidence = proposal_data.get("confidence", 0.75)
source = proposal_data.get("source", "unknown")
# 建立 approval_id (使用 incident_id 作為追蹤)
approval_id = f"INC-{incident.incident_id}"
# 2026-03-26 修復: incident_id 已有 INC- 前綴,不要再加
approval_id = incident.incident_id
await gateway.send_approval_card(
approval_id=approval_id,
@@ -94,6 +106,9 @@ async def _push_decision_to_telegram(
namespace=incident.signals[0].labels.get("namespace", "default") if incident.signals else "default",
)
# 2026-03-26 修復: 標記已發送10 分鐘內不再發送
await redis_client.set(dedup_key, "1", ex=600)
logger.info(
"telegram_decision_pushed",
incident_id=incident.incident_id,