feat: 事件异常 Worker 巡检告警

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-21 18:22:52 +08:00
co-authored by Cursor
parent 1bab19f1a2
commit fc1b85113d
3 changed files with 343 additions and 0 deletions
@@ -190,5 +190,63 @@ class TestEventExceptionCopy(unittest.TestCase):
self.assertIn("您已超过 48 工作小时无响应", card["sub_title_text"])
class TestPoll(unittest.TestCase):
def test_fires_once_on_timeout(self) -> None:
import importlib.util
from datetime import datetime, timedelta
from agent.ledger.memory_ledger import MemoryLedger
from agent.policy.deal_followup import SHANGHAI
path = os.path.join(ROOT, "agent", "jobs", "event_exception_poll.py")
spec = importlib.util.spec_from_file_location("ee_poll", path)
self.assertIsNotNone(spec)
self.assertIsNotNone(spec.loader)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
poll_event_exceptions = mod.poll_event_exceptions
book = MemoryLedger()
book.set_sea_staff(
[
{
"wecomId": "p1",
"name": "张三",
"roleCode": "sea",
"notifyEventException": 1,
"status": "active",
}
]
)
created = book.create_ticket(sender_id="s1", business_line="SEA", facts={"运输分类": "其他"})
wo = created["work_order_no"]
book.start_event_wait(work_order_no=wo, wait_kind="product_quote")
t = book.get_ticket(work_order_no=wo)
t.collab_chat_id = "wr1"
t.collab_member_ids = ["p1"]
t.event_wait_started_at = (datetime.now(SHANGHAI) - timedelta(hours=3)).strftime(
"%Y-%m-%d %H:%M:%S"
)
book._timeouts["sea_product_other"] = 0.25
groups: list[dict] = []
def send_group(*, chat_id: str, content: str, business_line: str = "") -> bool:
groups.append({"chat_id": chat_id, "content": content, "line": business_line})
return True
now = datetime.now(SHANGHAI)
n = poll_event_exceptions(
ledger=book, now=now, send_group=send_group, notify_ready=False
)
self.assertEqual(n, 1)
self.assertEqual(book.get_ticket(work_order_no=wo).event_exception, "产品响应超时")
self.assertTrue(groups)
self.assertIn("@张三", groups[0]["content"])
n2 = poll_event_exceptions(
ledger=book, now=now, send_group=send_group, notify_ready=False
)
self.assertEqual(n2, 0)
if __name__ == "__main__":
unittest.main()