feat: canary агентного пути (Claude CLI) + алерт оператору на падении

Инцидент 2026-07-15: OAuth Claude истёк 03.07, агентный путь 12 дней молча слал
юзеру сырой 401 — мониторинг не поймал. Canary раз в N часов пингует Claude CLI
тем же docker-путём, что и приложение; при падении (401/таймаут/пустой ответ) шлёт
письмо оператору + WARN в лог, с троттлингом (не чаще RENOTIFY ч). Детекция покрыта
mutation-safe тестом. send_alert добавлен в email_service (reuse SMTP).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-07-15 17:10:53 +10:00
co-authored by Claude Opus 4.8
parent 82c6f7726e
commit 2973c6aa9b
4 changed files with 284 additions and 0 deletions
+29
View File
@@ -52,3 +52,32 @@ def send_verification_code(to_email: str, code: str) -> bool:
except Exception as e:
logger.error("Failed to send email to %s: %s", to_email, e)
return False
def send_alert(subject: str, text: str, to_email: str | None = None) -> bool:
"""Send a plain-text operational alert to the operator. Returns True on success.
Recipient: explicit arg → ALERT_EMAIL env → SMTP_USER (self). Used by
scripts/agent_canary.sh when the Claude agent path goes down.
"""
to = to_email or os.environ.get("ALERT_EMAIL") or SMTP_USER
if not SMTP_USER or not SMTP_PASS or not to:
logger.warning("SMTP/ALERT not configured, skipping alert")
return False
from email.header import Header
msg = MIMEText(text, "plain", "utf-8")
msg["Subject"] = subject
msg["From"] = f"{Header('Сметный ассистент', 'utf-8').encode()} <{SMTP_USER}>"
msg["To"] = to
try:
context = ssl.create_default_context()
with smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT, context=context, timeout=10) as server:
server.login(SMTP_USER, SMTP_PASS)
server.sendmail(SMTP_USER, to, msg.as_string())
logger.info("Alert email sent to %s", to)
return True
except Exception as e:
logger.error("Failed to send alert to %s: %s", to, e)
return False