From 2abc91e360e42abfed3ea8cc07b5c8decd20e85d Mon Sep 17 00:00:00 2001 From: OG T Date: Sun, 19 Apr 2026 11:27:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(drift-card):=20=E4=BF=AE=20drift=20?= =?UTF-8?q?=E5=8D=A1=E7=89=87=202=20bug=20=E2=80=94=20AI=20=E7=A0=94?= =?UTF-8?q?=E5=88=A4=20copy=20=E6=A8=A3=E5=BC=8F=20+=20Diff=20=E6=8C=89?= =?UTF-8?q?=E9=88=95=20AttributeError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug 1: 按「🔍 查看 Diff」失敗 錯誤: 'DriftReportRepository' object has no attribute 'get_by_id' 根因: DriftReportRepository 方法叫 get(), 其他 repo 都叫 get_by_id() 修法: 加 get_by_id() alias, 對齊 repo 介面慣例 Bug 2: AI 研判內容被渲染成 code block + copy 按鈕 根因: telegram_gateway line 1962 用
 包 diff_summary
       但 diff_summary 是 AI 研判敘述 + emoji 清單, 非 code
  修法: 移除 
, 改以分隔線 + html.escape 純文字顯示

驗收:
- 下次 drift 卡片: AI 研判段落純文字(無紫色 code block + copy)
- 按「🔍 查看 Diff」→ 送完整 diff 詳情(非 AttributeError)

Co-Authored-By: Claude Opus 4.7 (1M context) 
---
 apps/api/src/repositories/drift_repository.py | 5 +++++
 apps/api/src/services/telegram_gateway.py     | 5 ++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/apps/api/src/repositories/drift_repository.py b/apps/api/src/repositories/drift_repository.py
index d365223e..a40c16b3 100644
--- a/apps/api/src/repositories/drift_repository.py
+++ b/apps/api/src/repositories/drift_repository.py
@@ -116,6 +116,11 @@ class DriftReportRepository:
             row = result.fetchone()
         return _row_to_report(row) if row else None
 
+    # 2026-04-19 Claude Opus 4.7 修 drift_view 按鈕 AttributeError
+    # 其他 repo 慣例皆 get_by_id,對齊介面供 telegram_gateway 呼叫
+    async def get_by_id(self, report_id: str) -> DriftReport | None:
+        return await self.get(report_id)
+
     async def list_recent(self, limit: int = 50) -> list[DriftReport]:
         """列出最近 N 筆(倒序)"""
         async with get_db_context() as db:
diff --git a/apps/api/src/services/telegram_gateway.py b/apps/api/src/services/telegram_gateway.py
index 5aafdd0f..32b7f212 100644
--- a/apps/api/src/services/telegram_gateway.py
+++ b/apps/api/src/services/telegram_gateway.py
@@ -1957,9 +1957,12 @@ class TelegramGateway:
             diff_summary: Diff 摘要文字
             detected_at: 偵測時間
         """
+        # 2026-04-19 Claude Opus 4.7 修 diff_summary 被 
 包成 code block (copy 按鈕 UI)
+        # 根因:
 在 Telegram HTML mode 渲染為 code block,但 diff_summary 是 AI
+        #      研判敘述 + emoji 清單(非 code),應以純文字顯示
         # Diff 長度處理 (ADR-071, Section 14.9.6)
         if len(diff_summary) <= 500:
-            diff_block = f"\n
{html.escape(diff_summary)}
" + diff_block = f"\n━━━━━━━━━━━━━━━━━━━\n{html.escape(diff_summary)}" else: web_url = f"https://aiops.wooo.work/incidents/{incident_id}/drift-diff" diff_block = f"\n⚠️ 差異過大({len(diff_summary)} 字)\n🔗 查看完整 Diff"