diff --git a/src/daemon/mail_notify.py b/src/daemon/mail_notify.py index 9e7d632..90b9fc7 100644 --- a/src/daemon/mail_notify.py +++ b/src/daemon/mail_notify.py @@ -13,6 +13,12 @@ from src.blackboard.operations import Blackboard logger = logging.getLogger(__name__) +# 有效 Agent ID 集合(用于校验通知目标) +_VALID_AGENT_IDS = frozenset({ + "pangtong-fujunshi", "simayi-challenger", "zhangfei-dev", + "guanyu-dev", "zhaoyun-data", "jiangwei-infra", +}) + # 邮件通知正文模板(统一模板,包含所有可能的失败原因和建议) _NOTIFY_TEMPLATE = """你的邮件投递失败了。 @@ -42,6 +48,7 @@ def notify_mail_failed(db_path: Path, original_mail_id: str, 直接通过 Blackboard 创建 Task,不走 HTTP API。 防递归:检查原邮件 must_hives.system_notify,为 true 则跳过。 + 发件人不是有效 Agent(如 system)→ 通知庞统代处理,避免广播风暴。 """ try: bb = Blackboard(db_path) @@ -67,6 +74,13 @@ def notify_mail_failed(db_path: Path, original_mail_id: str, logger.warning("notify_mail_failed: cannot determine sender for mail %s", original_mail_id) return + # 发件人不是有效 Agent(如 system)→ 通知庞统代处理,不触发广播 + target_agent = from_agent + if from_agent not in _VALID_AGENT_IDS: + logger.warning("Mail %s: sender '%s' is not a valid agent, routing failure notice to pangtong-fujunshi", + original_mail_id, from_agent) + target_agent = "pangtong-fujunshi" + # 构造通知正文 text = _NOTIFY_TEMPLATE.format( title=title, @@ -90,15 +104,15 @@ def notify_mail_failed(db_path: Path, original_mail_id: str, id=notify_id, title=f"[投递失败] {title}", description=text, - assignee=from_agent, + assignee=target_agent, assigned_by="system", must_haves=json.dumps(notify_meta, ensure_ascii=False), task_type="mail", status="pending", ) bb.create_task(notify_task) - logger.info("Mail %s: sent failure notification to %s (reason=%s, notify_id=%s)", - original_mail_id, from_agent, reason, notify_id) + logger.info("Mail %s: sent failure notification to %s (original_sender=%s, reason=%s, notify_id=%s)", + original_mail_id, target_agent, from_agent, reason, notify_id) except Exception as e: logger.warning("notify_mail_failed: failed to send notification for mail %s: %s", original_mail_id, e)