Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/ai/ai-analyzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Alertmanager (continue:true)┤ 메시지 본문 끝에 `fingerprint=<value>`
| `AGENT_ENABLED` | `true` | tool-using agentic loop 활성화. `false` 시 single-shot 경로 fallback |
| `AGENT_MAX_ITERATIONS` | `5` | 한 알람 분석당 최대 model 호출 횟수 |
| `AM_URL` | `http://alertmanager.alertmanager:9093` | `list_recent_alerts` 도구의 Alertmanager v2 endpoint |
| `SLACK_HISTORY_LOOKBACK` | `100` | thread parent 검색 시 채널 history slot 개수 |
| `SLACK_PARENT_MAX_AGE_MIN` | `30` | N분 넘은 같은 fingerprint 부모는 stale로 간주 — 같은 알람 재발화 시 옛 thread 에 reply 박는 것 방지 |

## Agentic 도구 (Tier 1)

Expand Down
44 changes: 40 additions & 4 deletions apps/ai/ai-analyzer/app/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import logging
import os
import time

import httpx

Expand All @@ -22,7 +23,12 @@
SLACK_BOT_TOKEN = os.getenv("SLACK_BOT_TOKEN", "")
SLACK_CHANNEL = os.getenv("SLACK_CHANNEL", "")
SLACK_FINGERPRINT_MARKER = os.getenv("SLACK_FINGERPRINT_MARKER", "fingerprint=")
SLACK_HISTORY_LOOKBACK = int(os.getenv("SLACK_HISTORY_LOOKBACK", "30"))
SLACK_HISTORY_LOOKBACK = int(os.getenv("SLACK_HISTORY_LOOKBACK", "100"))
# Stale-parent guard: ignore Alertmanager parent messages older than this
# when matching fingerprints. Without it the same alert re-firing hours later
# would reply into the original (long-stale) thread because the marker is still
# present in channel history.
SLACK_PARENT_MAX_AGE_MIN = int(os.getenv("SLACK_PARENT_MAX_AGE_MIN", "30"))
HTTP_TIMEOUT = float(os.getenv("SLACK_HTTP_TIMEOUT", "8"))

# Per-message sender override so the AI analyzer card is visually distinct
Expand Down Expand Up @@ -75,6 +81,21 @@ async def _get(method: str, params: dict) -> dict:


async def _find_thread_ts(channel: str, fingerprint: str) -> str | None:
"""Locate the Alertmanager-posted parent message for `fingerprint`.

Two failure modes the older implementation hit:
- **Stale match**: a same-fingerprint parent from a previous firing was
still inside the history slice, so replies kept piling onto an
hours-old thread instead of the fresh one.
- **First-match bias**: relied on Slack returning messages newest-first;
under load that ordering can be perturbed and we'd grab whichever
match came back first.

Fix: collect *every* matching parent within the lookback window, drop
the ones older than `SLACK_PARENT_MAX_AGE_MIN`, and pick the newest by
ts. Returns None if nothing fresh matches — caller falls back to a new
channel message.
"""
if not fingerprint:
return None
needle = f"{SLACK_FINGERPRINT_MARKER}{fingerprint}"
Expand All @@ -83,16 +104,31 @@ async def _find_thread_ts(channel: str, fingerprint: str) -> str | None:
)
if not res.get("ok"):
return None

cutoff_epoch = time.time() - SLACK_PARENT_MAX_AGE_MIN * 60
candidates: list[tuple[float, str]] = []
for msg in res.get("messages", []):
text = msg.get("text", "") or ""
for att in msg.get("attachments", []) or []:
text += "\n" + (att.get("text", "") or "")
# blocks may contain the marker in section/context text fields
for block in msg.get("blocks", []) or []:
text += "\n" + str(block)
if needle in text:
return msg.get("ts")
return None
if needle not in text:
continue
ts_str = msg.get("ts", "")
try:
ts_f = float(ts_str)
except (TypeError, ValueError):
continue
if ts_f < cutoff_epoch:
continue
candidates.append((ts_f, ts_str))

if not candidates:
return None
candidates.sort(reverse=True)
return candidates[0][1]


def _format_blocks(result: dict) -> list[dict]:
Expand Down
9 changes: 8 additions & 1 deletion apps/ai/ai-analyzer/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ env:
# Marker that the Alertmanager Slack template appends to each message so
# ai-analyzer can match the parent message via conversations.history.
SLACK_FINGERPRINT_MARKER: "fingerprint="
SLACK_HISTORY_LOOKBACK: "30"
# Lookback over channel history when matching the parent message. Raised
# from 30 → 100 so the fresh parent isn't bumped out of the slice by
# ai-analyzer's own standalone fallback posts on busy channels.
SLACK_HISTORY_LOOKBACK: "100"
# Stale-parent guard: ignore Alertmanager parents older than N minutes
# when matching by fingerprint. Without it, the same alert re-firing
# hours later replies into the original (long-stale) thread.
SLACK_PARENT_MAX_AGE_MIN: "30"
# 분석 카드 sender override는 분리된 Slack App 도입으로 불필요 (#47 후속).
# 새 App 자체가 "AI 분석 봇" 이름 + 자체 icon을 가짐 → 채널 thumbnail까지 분리됨.
# override를 박으면 채널 thread preview의 thumbnail과 충돌하므로 비워둔다.
Expand Down
Loading