Files
awoooi/apps/api/migrations/cleanup_duplicate_deprecated_playbooks.sql
Your Name 45dbe07188
Some checks failed
run-migration / migrate (push) Failing after 22s
Deploy Alert Rules / Deploy Prometheus Alert Rules (push) Successful in 53s
Type Sync Check / check-type-sync (push) Successful in 2m54s
CD Pipeline / build-and-deploy (push) Has been cancelled
Ansible Lint / lint (push) Has been cancelled
fix(flywheel): 自動化飛輪六大能力修復(ADR-092 B3)
【根因鏈修復】
MCP Provider bugs → PreDecisionInvestigator 失敗 → Agent Debate 無上下文
→ LLM 逾時 → description="待分析" → ADR-091 鐵閘攔截 → tg_sent 未設
→ W-2 Watchdog 誤報「靜默故障」

【六大修復】
1. MCP Provider 三蟲修復
   - ssh_provider: asyncssh.run() → conn.run()
   - prometheus_provider: KeyError 'query' → .get() 容錯
   - k8s_provider: 空 pod_name → 早返回錯誤字典

2. Agent Debate / 決策品質
   - decision_manager: 逾時降級文字改為明確描述(繞過 ADR-091 鐵閘)
   - intent_classifier: LLM 逾時降級至關鍵字分類(非 None)

3. Watchdog 誤報修復(ADR-092 B3)
   - W-2: tg_sent Redis TTL → telegram_message_id IS NULL(DB 真值)
   - W-5 新增: suggested_action IN 空/待分析/NO_ACTION + tg_id IS NULL
   - approval_timeout_resolver: 60min → 15min,batch 50 → 200

4. Config Drift 自動化
   - drift_adopt_service: auto_adopt_if_safe() 六條件安全閘
   - drift.py: 背景任務先嘗試自動採納再發人工 Telegram 卡片

5. Playbook 飛輪穩定
   - playbook_seed_service: 修復幂等性(deprecated 不視為缺失)
   - playbook_evolver: 只載 DRAFT+APPROVED(非全部 294 筆)

6. 可觀測性
   - alert_rule_engine: auto_rule 結構化日誌 + Redis 計數器(pipeline)
   - auto_approve: reject 原因 Redis 計數器
   - heartbeat_report_service: 新增「⚙️ 自動化統計(今日)」區塊

【待人工執行】
psql $DATABASE_URL -f apps/api/migrations/cleanup_duplicate_deprecated_playbooks.sql

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-24 10:55:50 +08:00

32 lines
1.3 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 清理重複的 deprecated yaml_rule Playbooks
-- 根因seeder 冪等 SQL 舊版排除 deprecated 記錄,導致每次啟動重建同名 Playbook
-- C1 保護evolver 不封存 yaml_rule加入前已存在的 deprecated 歷史記錄
-- 觸發無限重建迴圈294 deprecated25 approved
-- 修法:每個 name 只保留最新的一筆 deprecated其餘刪除
-- seeder 已同步修正status 過濾移除),此腳本清理歷史垃圾
-- 2026-04-24 ogt + Claude Sonnet 4.6(亞太)
BEGIN;
-- 診斷:執行前統計(可選,確認規模)
-- SELECT source, status, COUNT(*) FROM playbooks GROUP BY source, status ORDER BY source, status;
-- 找出每個 yaml_rule deprecated name 的最新 created_at保留基準
-- 刪除同名同 source=yaml_rule + status=deprecated 中非最新的記錄
DELETE FROM playbooks
WHERE status = 'deprecated'
AND source = 'yaml_rule'
AND playbook_id NOT IN (
-- 每個 name 保留 created_at 最新的那一筆
SELECT DISTINCT ON (name) playbook_id
FROM playbooks
WHERE status = 'deprecated'
AND source = 'yaml_rule'
ORDER BY name, created_at DESC
);
-- 執行後確認
-- SELECT source, status, COUNT(*) FROM playbooks GROUP BY source, status ORDER BY source, status;
COMMIT;