fix(strategist): cast metadata_json text to jsonb for stale alert dedupe

critic post-review #1 (HIGH): `_send_data_stale_alert` 的 dedupe SQL 對
`ai_insights.metadata_json` (Column(Text)) 直接套用 `->>` operator,PG 會
raise `operator does not exist: text ->> unknown`,被外層 try/except 吞掉,
導致 dedupe 完全失效,daily/weekly/monthly 同日 stale 會送 2-3 次告警噪音。

修法:`metadata_json::jsonb->>'report_type'` 即時 cast Text→JSONB 再取 key
(寫入端用 json.dumps,內容為合法 JSON)。

影響:
- 僅修 services/openclaw_strategist_service.py:353 一行
- grep 確認全 repo 僅此一處 `metadata_json->>` 用法
- 不動 dedupe 視窗 / telegram 發送 / _save_to_ai_insights
- 不動 tests / requirements.txt / 其他檔案

Regression:cast 每次 stale 告警跑一次,效能影響可忽略;若歷史 row
metadata_json 內容非合法 JSON,cast 會 raise 並被 try/except 吞掉,
行為退回現狀(dedupe 失效,但不影響告警送出)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
OoO
2026-05-03 12:29:22 +08:00
parent 958f705c8e
commit dda0a06bfd

View File

@@ -350,7 +350,7 @@ def _send_data_stale_alert(report_type: str, last_date: str, period: str) -> boo
WHERE insight_type = 'data_stale_alert'
AND created_by = 'openclaw'
AND created_at >= NOW() - INTERVAL '24 hours'
AND metadata_json->>'report_type' = :rt
AND metadata_json::jsonb->>'report_type' = :rt
ORDER BY created_at DESC
LIMIT 1
"""), {"rt": report_type}).fetchone()