專業化 EA 資源壓力告警
All checks were successful
CD Pipeline / deploy (push) Successful in 1m2s

This commit is contained in:
OoO
2026-05-19 20:47:46 +08:00
parent 1911c1c53d
commit 32013ae3b2
5 changed files with 524 additions and 14 deletions

View File

@@ -159,3 +159,124 @@ def test_escalate_resource_optimization_without_evidence_is_suppressed(monkeypat
assert cooldown == ["resource_optimization"]
assert suppressed == [("resource_optimization", "no_concrete_evidence")]
def test_resource_pressure_classifier_does_not_equate_backlog_with_cpu_load():
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
metrics = ElephantAlphaAutonomousEngine._classify_resource_pressure({
"action_queue_size": 34,
"high_priority_count": 0,
"human_review_count": 0,
"stale_count": 0,
"system_load_pct": 19.2,
"queue_threshold": 10,
"load_threshold_pct": 80,
"high_priority_threshold": 5,
"stale_threshold": 5,
})
assert metrics["pressure_level"] == "backlog_only"
assert metrics["should_alert"] is False
assert metrics["load_pressure"] is False
assert "system_load=19.2%/80%" in metrics["evidence"]
def test_resource_pressure_classifier_alerts_on_actionable_review_backlog():
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
metrics = ElephantAlphaAutonomousEngine._classify_resource_pressure({
"action_queue_size": 34,
"high_priority_count": 8,
"human_review_count": 6,
"stale_count": 0,
"system_load_pct": 22.0,
"queue_threshold": 10,
"load_threshold_pct": 80,
"high_priority_threshold": 5,
"stale_threshold": 5,
})
assert metrics["pressure_level"] == "warning"
assert metrics["should_alert"] is True
assert metrics["high_priority_pressure"] is True
assert metrics["load_pressure"] is False
def test_resource_pressure_message_is_measurement_based_not_llm_theatre():
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
metrics = ElephantAlphaAutonomousEngine._classify_resource_pressure({
"action_queue_size": 34,
"high_priority_count": 8,
"human_review_count": 6,
"stale_count": 5,
"system_load_pct": 22.0,
"queue_threshold": 10,
"load_threshold_pct": 80,
"high_priority_threshold": 5,
"stale_threshold": 5,
"stale_hours": 24,
})
msg = ElephantAlphaAutonomousEngine._build_resource_pressure_telegram_message(
metrics,
insight_id=123,
previous_limit=10,
new_limit=8,
)
assert "量測指標" in msg
assert "主機 CPU 未達高負載門檻" in msg
assert "未啟動 Hermes/NemoTron 價格分析" in msg
assert "預期效益" not in msg
assert "48小時" not in msg
assert "48 小時效益預測" in msg
def test_resource_optimization_bypasses_llm_orchestrator(monkeypatch):
import services.elephant_alpha_autonomous_engine as engine_module
from services.elephant_alpha_autonomous_engine import (
AutonomousTrigger,
ElephantAlphaAutonomousEngine,
)
engine = ElephantAlphaAutonomousEngine()
sent = []
stored = []
async def _boom(*args, **kwargs):
raise AssertionError("resource_optimization must not call LLM orchestrator")
async def _capture_send(*args, **kwargs):
sent.append(args)
monkeypatch.setattr(engine_module.elephant_orchestrator, "analyze_and_coordinate", _boom)
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))
trigger = AutonomousTrigger(
trigger_type="resource_optimization",
conditions={
"_resource_metrics": {
"action_queue_size": 34,
"high_priority_count": 9,
"human_review_count": 6,
"stale_count": 0,
"system_load_pct": 20.0,
"queue_threshold": 10,
"load_threshold_pct": 80,
"high_priority_threshold": 5,
"stale_threshold": 5,
}
},
threshold=0.6,
enabled=True,
)
asyncio.run(engine._execute_autonomous_decision(trigger))
assert stored == ["resource_optimization"]
assert len(sent) == 1
assert engine.max_autonomous_decisions_per_hour == 8