清理 EA 過期行動隊列
All checks were successful
CD Pipeline / deploy (push) Successful in 1m3s

This commit is contained in:
OoO
2026-05-19 21:01:44 +08:00
parent d32b1f0e80
commit aaebd6b86d
6 changed files with 383 additions and 4 deletions

View File

@@ -0,0 +1,49 @@
from datetime import datetime, timedelta
def test_action_plan_hygiene_preview_closes_only_stale_advisory_sources():
from services.action_plan_hygiene import build_action_plan_hygiene_preview
now = datetime(2026, 5, 19, 12, 0, 0)
rows = [
{
"id": 1,
"status": "pending",
"priority": 1,
"created_at": now - timedelta(hours=100),
"action_type": "openclaw_recommendation",
"description": "old advisory",
},
{
"id": 2,
"status": "auto_pending",
"priority": 1,
"created_at": now - timedelta(hours=90),
"action_type": "code_review_fix",
"description": "old code review",
},
{
"id": 3,
"status": "pending",
"priority": 1,
"created_at": now - timedelta(hours=90),
"created_by": "nemotron",
"description": "must stay",
},
{
"id": 4,
"status": "pending",
"priority": 1,
"created_at": now - timedelta(hours=2),
"action_type": "openclaw_recommendation",
"description": "fresh advisory",
},
]
preview = build_action_plan_hygiene_preview(rows, now=now, stale_hours=72)
assert preview["candidate_count"] == 2
assert preview["by_source"] == {"openclaw_recommendation": 1, "code_review_fix": 1}
assert {item["id"] for item in preview["candidates"]} == {1, 2}
target_by_id = {item["id"]: item["to_status"] for item in preview["candidates"]}
assert target_by_id == {1: "rejected", 2: "auto_disabled"}

View File

@@ -252,6 +252,7 @@ def test_resource_optimization_bypasses_llm_orchestrator(monkeypatch):
sent.append(args)
monkeypatch.setattr(engine_module.elephant_orchestrator, "analyze_and_coordinate", _boom)
monkeypatch.setattr(engine, "_run_action_plan_hygiene", lambda: {"updated_count": 0})
monkeypatch.setattr(engine, "_record_resource_pressure_insight", lambda *args, **kwargs: 77)
monkeypatch.setattr(engine, "_send_resource_pressure_telegram", _capture_send)
monkeypatch.setattr(engine, "_store_escalation", lambda trigger_type: stored.append(trigger_type))
@@ -280,3 +281,40 @@ def test_resource_optimization_bypasses_llm_orchestrator(monkeypatch):
assert stored == ["resource_optimization"]
assert len(sent) == 1
assert engine.max_autonomous_decisions_per_hour == 8
def test_resource_pressure_message_reports_hygiene_result():
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
metrics = ElephantAlphaAutonomousEngine._classify_resource_pressure({
"action_queue_size": 9,
"high_priority_count": 0,
"human_review_count": 0,
"stale_count": 0,
"system_load_pct": 20.0,
"queue_threshold": 10,
"load_threshold_pct": 80,
"high_priority_threshold": 5,
"stale_threshold": 5,
})
metrics["pre_hygiene"] = {
"action_queue_size": 100,
"high_priority_count": 47,
"stale_count": 99,
}
metrics["hygiene_result"] = {
"updated_count": 91,
"by_source": {"code_review_fix": 66, "openclaw_recommendation": 25},
}
msg = ElephantAlphaAutonomousEngine._build_resource_pressure_telegram_message(
metrics,
insight_id=124,
previous_limit=10,
new_limit=10,
)
assert "P4 resolved" in msg
assert "清理前 Action queue100" in msg
assert "已自動關閉過期 action_plans 91 筆" in msg
assert "只改 status/metadata不刪除資料" in msg