feat(wave6-8): P2.1 fusion + P2.2 governance + P2.4 consensus + Wave 7/8 BLOCKER 修復
承接 Wave 6/7/8 多 engineer 在 agent 限額前完成的代碼,補 commit 解 production HEAD 隱性 import error(decision_fusion 已被 decision_manager 引用但檔案 untracked)。 新增(後端核心): - decision_fusion.py (562 行) — P2.1 方法 III(OpenClaw + Hermes + Elephant 三 LLM 融合) - aiops_timeline.py + aiops_timeline_service.py — critic B4 修復 /api/v1/aiops/timeline endpoint,DB 存取抽到 service 層遵守 leWOOOgo 積木化 - migrations/p2_decision_fusion_columns.sql + rollback — approval_records fusion 欄位 修改(後端整合): - decision_manager.py — fusion 三斷鏈修補(critic B1+B2+B3): · B1: 寫 _evidence_snapshot_ref 到 token.proposal_data · B2: fusion 前計算 complexity_score 並寫 token · B3: fusion composite 寫 token.proposal_data["decision_fusion"] - auto_approve.py — fusion + consensus 認識(critic B3+B5): · composite > 0.7 → auto_execute_eligible bypass min_confidence · source=consensus_engine + score>=0.6 → 規則可信路徑 - consensus_engine.py — db-fix _save_consensus 重用 agent_sessions - governance_agent.py — db-fix _alert PG 寫入 ai_governance_events - approval_db.py — fusion 3 欄位 + 2 partial index + CheckConstraint - db/models.py — schema 對齊 migration - core/config.py — vuln #1 修復:OLLAMA_URL/_FALLBACK_URL field_validator 拒絕公網 IP + 外部域名,僅允許私網/loopback/K8s SVC 白名單 - core/feature_flags.py — P2 fusion + consensus flags - main.py — governance_agent lifespan 啟動 - failover_alerter.py — Wave8-X2: in-memory dedup fallback(Redis 拒絕後不 fail-open) - ollama_*.py — metrics 整合 + recovery 改善 - auto_repair_service.py — verifier 接線 新增(測試 2438 行): - test_decision_fusion.py / test_governance_agent.py / test_consensus_integration.py - test_p2_db_fixes.py / test_wave8_fusion_fixes.py - test_config_url_validation.py(vuln #1 12 tests) - test_failover_alerter.py +Wave8-X2 in-memory dedup 補測 驗收: 116 tests pass (decision_fusion + wave8_fusion + config_url + consensus + governance + p2_db_fixes + failover_alerter) Conflict resolution: - 3 檔(config.py + auto_approve.py + decision_manager.py)git stash pop 衝突 保留 stashed (engineer 最終版),補回 ValueError 「公網 IP」字樣對齊 test Note: 此 commit 解 production HEAD 隱性 import error 仍未修: vuln #4 prompt injection / debugger B14 quota fail-closed / B25-B26 drain_pending_tasks / B8 governance fail alert Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-Authored-By: Multiple Engineers (Wave 6/7/8) <noreply@anthropic.com>
This commit is contained in:
@@ -168,7 +168,65 @@ def test_configure_alerter_replaces_singleton(mock_redis):
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dedup_fail_open_when_no_redis():
|
||||
"""Redis 為 None 時 dedup 應 fail-open(允許送出)"""
|
||||
"""Redis 為 None 時 dedup 第一次應允許送出(in-memory dedup,非 fail-open 對所有次數)"""
|
||||
alerter = FailoverAlerter(redis_client=None)
|
||||
# _check_dedup 應返回 True(允許送出)
|
||||
# 第一次:無記錄 → 允許
|
||||
assert await alerter._check_dedup("any:key", ttl=600) is True
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Wave8-X2: dedup in-memory fallback 新增測試
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dedup_redis_unavailable_uses_memory():
|
||||
"""Redis 拋出例外時,in-memory dedup 仍生效(不 fail-open 狂發)
|
||||
|
||||
Wave8-X2 fix:原 fail-open 改為 in-memory dedup fallback。
|
||||
驗證:Redis set() raise → 第二次 _check_dedup 同 key 應回 False。
|
||||
"""
|
||||
bad_redis = MagicMock()
|
||||
bad_redis.set = AsyncMock(side_effect=ConnectionError("Redis is down"))
|
||||
|
||||
alerter = FailoverAlerter(redis_client=bad_redis)
|
||||
|
||||
key = "alert:test:dedup_memory"
|
||||
ttl = 600
|
||||
|
||||
# 第 1 次:in-memory 無記錄 → 允許
|
||||
result1 = await alerter._check_dedup(key, ttl=ttl)
|
||||
assert result1 is True
|
||||
|
||||
# 第 2 次:in-memory 已有記錄(未過 TTL)→ 拒絕
|
||||
result2 = await alerter._check_dedup(key, ttl=ttl)
|
||||
assert result2 is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory_dedup_max_size_gc():
|
||||
"""超過 1000 entries 時 GC 清除過期 entry,防 dict 無限成長
|
||||
|
||||
Wave8-X2 fix:_memory_dedup_max_size = 1000,超過時 GC。
|
||||
驗證:注入 999 個已過期 entry + 1 個未過期 → GC 後 dict size 應減少。
|
||||
"""
|
||||
import time
|
||||
|
||||
alerter = FailoverAlerter(redis_client=None)
|
||||
|
||||
# 注入 999 個「已過期」entry(last_sent = 0.0,TTL=600s,均已過期)
|
||||
for i in range(999):
|
||||
alerter._memory_dedup[f"stale:key:{i}"] = 0.0 # expired: now - 0.0 > 600
|
||||
|
||||
# 注入 1 個「未過期」entry
|
||||
alerter._memory_dedup["fresh:key"] = time.time()
|
||||
|
||||
# 此時 dict size = 1000,達 _memory_dedup_max_size
|
||||
assert len(alerter._memory_dedup) == 1000
|
||||
|
||||
# 觸發 GC:新 key check 讓 len >= max_size → 清理
|
||||
result = await alerter._check_dedup("trigger:gc:key", ttl=600)
|
||||
assert result is True # 新 key 應被允許
|
||||
|
||||
# GC 後:999 個 stale entry 被清除,只剩 fresh:key + trigger:gc:key
|
||||
assert len(alerter._memory_dedup) <= 3 # fresh + trigger + 可能有邊界差1
|
||||
|
||||
Reference in New Issue
Block a user