# AI 自動化閉環記憶(2026-04-29) > 用途:讓新 Codex session 快速掌握 2026-04-29 這輪「AI 自動化」修復後的實況,不必重讀整串對話與所有 commit。 ## 核心結論 - AI 自動化控制面已從三 Agent 擴展為四 Agent:Hermes、NemoTron、OpenClaw、ElephantAlpha。 - EventRouter 是告警與 L2 安全 action 的入口;AI 掛掉時必須降級,不可中斷通知。 - AutoHeal 是自癒副作用入口;`momo-db` / `momo-postgres` 受保護,不可自動 restart / stop / recreate。 - OpenClaw learning 是 AI 記憶與 embedding queue 的橋接層;raw `ai_insights` 寫入者必須 enqueue embedding 或可回補。 - ElephantAlpha 只負責 orchestration / HITL / AutoHeal bridge,不可繞過 ADR-011、ADR-012、ADR-013。 - AI 自動化最小 Prometheus 指標已接入 `/metrics`,來源為 `services/ai_automation_metrics.py`。 - 線上 smoke dashboard 已接入 `/ai_automation_smoke`,JSON API 為 `/api/ai-automation/smoke`。 - Smoke API 會保存最近快檢 JSONL 趨勢,dashboard 顯示 OK / Warning / Critical 最近分布。 - Smoke history 已支援 JSONL 匯出、清理與每日摘要;清理只影響 smoke history,不碰 DB 或 EventRouter queue。 - Smoke 每日摘要已支援手動 Telegram 推播與 scheduler 09:10 排程入口;摘要只讀 JSONL history。 - Grafana provisioning 已新增 `MOMO AI Automation Overview`,由 Prometheus `/metrics` 觀測 EventRouter、safe action、replay 與 AutoHeal 趨勢。 - 2026-04-30 active Grafana 已載入 4 個 dashboard;AI dashboard 檔案同步到 188 實際掛載目錄 `monitoring/grafana/provisioning/dashboards/json/`。 - 2026-04-30 active Prometheus 補 `momo-app` scrape job,目標 `momo-pro-system:80/metrics`;Prometheus 需加入 `momo-network` 才能解析 app container DNS。 - 2026-05-01 active Blackbox HTTP target 改打 `/health`;188 stack 使用 `https://mo.wooo.work/health` + `http://momo-pro-system:80/health`,110 gateway stack 使用 `https://mo.wooo.work/health`。禁止再用 Dashboard 首頁 `/` 當探測目標,避免監控觸發重型查詢並和 `/health` 搶 worker。 - 2026-05-01 Gunicorn runtime 預設 `gthread` + `GUNICORN_THREADS=4`,保留 `preload_app = False` 供 HUP 熱重載,同時讓輕量 health check 不被少數 Dashboard 長請求完全排隊。 - 2026-04-30 發現並修復 `gunicorn.conf.py` `post_fork` 掃到 Flask/Werkzeug LocalProxy 導致 worker boot fail 的問題。 - 2026-04-30 CD 健康檢查曾因 rebuild 後短暫 502 太早失敗;已改為 internal `docker exec momo-pro-system /health` + external `https://mo.wooo.work/health` 雙檢查,重試約 3 分鐘。 - 2026-04-30 CD Sync 模式曾只 rsync + `docker compose up -d`,導致 host 檔案已是新版但 gunicorn process 仍跑舊版;已補 `docker compose restart momo-app scheduler telegram-bot`。 - 2026-04-30 `gunicorn.conf.py` 不是 app container bind mount,曾導致手動 restart 後回吃 image 內舊設定;`momo-app` 已補 `./gunicorn.conf.py:/app/gunicorn.conf.py:ro`。 - 2026-04-30 `/metrics` 對 `realtime_sales_monthly` 改用 raw `SELECT COUNT(*)`,避免 ORM 欄位與線上表 schema drift 時每次 Prometheus scrape 都產生 warning。 - 2026-04-30 CD Rebuild 模式曾先停三應用容器再 no-cache build,造成 build 時間全變成 502;已改為 build 成功後才短暫 stop/rm/recreate。 - 2026-04-30 production `NVIDIA_API_KEY` 可列出 Ultra 253B 但呼叫 `nvidia/llama-3.1-nemotron-ultra-253b-v1` 會 404;ElephantAlpha 預設改用 `nvidia/llama-3.3-nemotron-super-49b-v1.5` 並加入 fallback models。 - 2026-04-30 `DatabaseManager()` 多 route 重複建立曾有吃滿 PostgreSQL clients 風險;已重用 engine/session 並將每 worker pool 收斂為 `pool_size=2/max_overflow=3`。 - 2026-04-30 OpenClaw embedding worker 曾在舊 `/api/embeddings` 路徑遇到 Hermes timeout;Ollama client 已改為優先 `/api/embed`,舊節點才 fallback `/api/embeddings`。 - 2026-04-30 `scheduler.py` 殘留靜默 `except/pass`;已改為 warning/debug log,備份 insight 與 Telegram 通知失敗不再靜默。 - 2026-04-30 `/metrics` 已補 `momo_ai_*` zero-baseline series;app 重啟後即使尚無 EventRouter / AutoHeal 事件,Prometheus/Grafana 也能先看到 metric names。 - 2026-04-30 ElephantAlpha NIM fallback 已擴大到 timeout、connection error、429 與 5xx;primary model 暫時卡住時會嘗試下一個 `ELEPHANT_ALPHA_FALLBACK_MODELS`。 ## 已落地範圍 - EventRouter 增加同步 dispatch、AI 失敗降級、Telegram 失敗 file queue、成功後 replay、去重冷卻、靜音檢查、L2 SAFE_ACTIONS 限制。 - NemoTron 在缺 API key、quota、timeout、network/HTTP error、0 tool call 時 fallback 到 Hermes 規則。 - Code review pipeline 預設關閉 auto-fix,高風險只走 HITL/action_plan,內部 webhook token 預設必填。 - AutoHeal 補 incident / heal log / KM 沉澱,DB 不可用時安全降級,SSH_CMD 限 read-only allowlist。 - OpenClaw learning 補 raw insert embedding bridge、stale processing reset、missing embedding backfill、pgvector semantic RAG fallback。 - Scheduler 重要失敗路徑接入 EventRouter,減少裸 exception 漏通知。 - ElephantAlpha 執行引擎補 sync timeout、HITL reply_markup、未知 step fail fast、code/resource action 走 AutoHeal bridge。 - L2 `agent_actions.py` 的 `flag_for_human_review`、`route_to_km`、`mark_for_relearn` 已從 stub 改為可審計 OpenClaw memory 寫入。 - `action_plans` 是 CodeReview / OpenClaw Group A 與 NemoTron Group B 共用表;PostgreSQL 啟動期 metadata repair 必須補齊 Group A 欄位,避免既有 DB 只有 `session_id/plan_type/sku/payload` 時讓 CodeReview action plan 寫入失敗。 - `/metrics` 已匯出 EventRouter dispatch、latency、safe action、Telegram replay、AutoHeal action 與 duration 指標。 - Smoke dashboard read-only 檢查 EventRouter queue、AutoHeal protected resources、NemoTron fallback、OpenClaw embedding queue、ElephantAlpha HITL,不做外部網路呼叫。 - Smoke history 只保存精簡紀錄,不保存完整 details,避免長期檔案膨脹與敏感資訊堆積。 - Export API 回傳 `application/x-ndjson`,clear API 只刪除 `MOMO_AI_AUTOMATION_SMOKE_HISTORY` 指向檔案。 - Daily summary API:`POST /api/ai-automation/smoke/daily-summary/send`。 - Grafana dashboard 檔案:`docker/grafana/provisioning/dashboards/json/ai-automation-overview.json`;provider 會載入 JSON 目錄,不需要修改 dashboard provider。 - Active monitoring 使用 `monitoring/prometheus.yml`,不是 `docker/prometheus/prometheus.yml`;若線上 panel 無資料,先查 Prometheus 是否有 `momo-app` target。 - Monitoring compose 的 cAdvisor 只需在 `monitoring` network 內提供 `cadvisor:8080` 給 Prometheus,不應綁定 host `8080`,避免與其他服務衝突;blackbox target 需要 `blackbox-exporter` 容器存在於同一 network。 - 2026-05-01 線上驗證:188 active MOMO UAT blackbox HTTP target 保留 public `https://mo.wooo.work/health` 與 internal `http://momo-pro-system:80/health`;110 gateway Prometheus 也已把 `https://mo.wooo.work` 改為 `https://mo.wooo.work/health`;`momo.wooo.work` 與 `wooo.work` 需等 DNS/Nginx 恢復後再加入 active monitoring。 - 110 Gitea runner 必須只宣告 `ewoooc-host` label;若混入 `ubuntu-latest` / `awoooi-host`,EWOOOC 與 AWOOOI workflows 會互相搶同一個 runner,導致推版卡住或跨專案污染。 - CD sync 模式應對 `momo-pro-system` 發 Gunicorn `HUP` 熱重載,不重啟 app 容器;scheduler / telegram-bot 才用 compose restart。Gunicorn 必須維持 `preload_app = False`,否則 HUP 只重啟 worker、但 app object 仍來自舊 master 預載程式碼。 - App container 的 runtime `gunicorn.conf.py` 由 `docker-compose.yml` bind mount;若未來改 gunicorn 設定,不應再手動 `docker cp` 作為常態流程。 ## 驗證紀錄 - 2026-04-29 AI metrics 批次:`26 passed`。 - 2026-04-29 AI smoke dashboard 批次:`2 passed`(單檔 smoke service),後續核心組需持續納入。 - 2026-04-29 AI smoke trend 批次:`5 passed`(smoke + metrics)。 - 2026-04-29 AI smoke management 批次:`7 passed`(smoke + metrics)。 - 2026-04-29 AI smoke summary 批次:`9 passed`(smoke + metrics)。 - 2026-04-29 AI Grafana observability 批次:`3 passed`(Grafana dashboard JSON 結構與必要 metric)。 - 2026-04-29 AI Grafana observability + AI core 回歸:`36 passed`,collect-only:`36 tests collected`。 - 2026-04-30 Gunicorn LocalProxy 修復:新增 `tests/test_gunicorn_config.py`。 - 2026-04-30 Prometheus scrape 修復:新增 `tests/test_prometheus_ai_automation_scrape.py`。 - 2026-04-30 Monitoring exporter 修復:`tests/test_prometheus_ai_automation_scrape.py` 覆蓋 cAdvisor internal-only 與 blackbox exporter 宣告。 - 2026-05-01 Health-safe monitoring runtime:`tests/test_gunicorn_config.py` 覆蓋 gthread/threads 預設與 override,`tests/test_prometheus_ai_automation_scrape.py` 覆蓋 blackbox 必須打 `/health`,`tests/test_cd_health_check.py` 覆蓋 monitoring reload 與 gunicorn bind-mounted sync。 - 2026-04-30 CD health check hardening:新增 `tests/test_cd_health_check.py`。 - 2026-04-30 CD Gunicorn mount hardening:新增 `tests/test_docker_compose_runtime_mounts.py`。 - 2026-04-30 Metrics schema drift 降噪:`tests/test_ai_automation_metrics.py` 覆蓋 raw sales count query。 - 2026-04-30 CD rebuild cutover hardening:`tests/test_cd_health_check.py` 覆蓋 build-before-stop 順序。 - 2026-04-30 ElephantAlpha NIM fallback hardening:新增 `tests/test_elephant_service.py`。 - 2026-04-30 DatabaseManager pool convergence:`tests/test_database_manager_cache.py` 覆蓋 pool size/overflow 與 engine reuse。 - 2026-04-30 action_plans schema drift:`tests/test_database_manager_cache.py` 覆蓋 PostgreSQL repair 需補 Group A 欄位與 index。 - 2026-04-30 Ollama embedding API migration:新增 `tests/test_ollama_embedding.py`。 - 2026-04-30 Phase 3f cleanup contracts:`tests/test_phase3f_cleanup_contracts.py` 覆蓋 orphan services、env 範例、scheduler 靜默例外。 - 2026-04-30 AI metrics baseline:`tests/test_ai_automation_metrics.py` 覆蓋無事件 snapshot 仍匯出 `momo_ai_*` baseline。 - 2026-04-30 ElephantAlpha transient fallback:`tests/test_elephant_service.py` 覆蓋 timeout、503 fallback 與 400 不 fallback。 - 2026-04-29 L2 安全記憶批次:`24 passed`。 - collect-only:`48 tests collected`。 - `git diff --check` 已通過。 ## 相關 commit - `779b27f` 修復 P0 告警自癒鏈與測試收集 - `0875dd8` 補強 5.5 自癒安全回看 - `1c2dc6c` 補強 AI 自動化閉環與安全降級 - `f0e044a` 補齊自癒稽核與 OpenClaw 記憶閉環 - `0c2e9bb` 串接 AI 洞察向量化與漏通知入口 - `78eebfb` 加入告警去重與洞察向量回補 - `d486598` 補強 ElephantAlpha 執行與通知閉環 - `5b25f55` 補齊 EventRouter 失敗通知回放 - `162a76b` 落地 L2 安全記憶動作 - `d58e4d0` 同步四 Agent AI 自動化治理紀錄 - `e6a1c9d` 補齊 AI 自動化可觀測性指標 - `cde8b0c` 新增 AI 自動化 Smoke Dashboard - `81159b5` 保存 AI Smoke 趨勢紀錄 - `10bbd55` 補齊 AI Smoke 趨勢管理 - `d5f4fd7` 加入 AI Smoke 每日摘要推播 ## 下次進場先看 - ADR-018:四 AI Agent 自動化控制面。 - ADR-012:Action Ladder 與 L2 SAFE_ACTIONS。 - ADR-013:AutoHeal 安全邊界。 - `docs/AI_INTELLIGENCE_MODULE_SOT.md`:目前 AI 架構事實。 - `docs/guides/ai_automation_session_sop.md`:AI 自動化 session 執行與收尾 SOP。