Files
ewoooc/docs/adr/ADR-007-ai-learning-dual-write.md
ogt 1b4f3a7bbe
Some checks failed
CD Pipeline / deploy (push) Failing after 59s
feat: EwoooC 初始化 — 完整專案推版至 Gitea
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml)
- 部署模式: rsync Python 檔案至 188 → docker restart (volume mount)
- Dockerfile/requirements 變動時自動重建 Docker image
- 部署通知: Telegram (開始/成功/失敗)
- 健康檢查: https://mo.wooo.work/health (最多 5 次重試)
- 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 01:21:13 +08:00

83 lines
2.7 KiB
Markdown
Raw 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.
# ADR-007AI 學習雙寫規範DB + KM 同步寫入)
- **Status**: Accepted
- **Date**: 2026-04-18
- **Decision Maker**: 統帥
- **Author**: Claude
## Context
在 EwoooC 三 Agent 分工架構中ADR-001所有 AI 產出洞察、競品分析、對話、PPT 內容)最終需要沉澱。
若只寫入一個 sink會導致
- 只寫 DBRAG 無法語意搜尋過去的洞察
- 只寫 KMvector精確 period/sku 查詢無法命中
歷史教訓2026-04-18
- 舊版 `openclaw_learning_service.py``store_insight()` API但只寫入 DB未向量化
- 導致 PPT 每次重新呼叫 AI 生成,歷史簡報失真(內容不一致)
## Decision
**所有 AI 產出必須雙寫 DB + KMpgvector**且為同一事務atomic
```
AI 產出(洞察/分析/對話)
NemoTron: tool call store_insight()
├─→ PostgreSQL ai_insights結構化欄位 + metadata
│ ↓ async
└─→ Hermes: embed → pgvector embedding 欄位更新同一行
```
**What 必須雙寫**
| 產出類型 | DB 表 | embedding 欄位 |
|---|---|---|
| PPT 洞察 | `ai_insights` | `embedding vector(1024)` |
| 競品分析 | `ai_insights` | `embedding vector(1024)` |
| 用戶對話記錄 | `conversations` | `embedding vector(1024)` |
| 週報 meta-analysis | `ai_insights` | `embedding vector(1024)` |
**What 不需雙寫**(不需語意搜尋):
- Telegram 告警派發記錄(只做審計)
- 原始爬蟲資料(`price_records`, `competitor_prices`
- 用戶操作 log`audit_logs`
## Consequences
### Positive
- RAG 永遠能找到歷史洞察PPT 內容跨時一致
- cache-aside 精確命中(同 period+type → 0.3s 回)+ 語意 fallback近似查詢
### Negative / Trade-offs
- 每次寫入有兩個 I/ODB + embedding延遲增加約 100-200ms
- Hermes 主機 OOM 時 embedding 會失敗(需 retry queue
## Implementation Notes
```python
# 雙寫入口點atomic
async def store_insight_with_embedding(insight: InsightModel):
# Step 1: 寫 DB同步
insight_id = db.execute(INSERT INTO ai_insights ...)
# Step 2: 排隊 embedding異步不阻塞主流
embedding_queue.put({"id": insight_id, "text": insight.text})
# Hermes worker獨立進程
def embedding_worker():
while True:
item = embedding_queue.get()
vec = embed_text(item["text"]) # 呼叫 Ollama bge-m3
db.execute(UPDATE ai_insights SET embedding = :vec WHERE id = :id)
```
## Related ADRs
- ADR-001三 Agent 分工NemoTron 負責寫入觸發Hermes 負責 embedding
- ADR-002pgvectorKM 儲存層)
- ADR-003本地 embeddingembedding 引擎)
- ADR-005時間衰減品質分數策略