chore(observability): clarify quick review completion copy
All checks were successful
CD Pipeline / deploy (push) Successful in 1m4s

This commit is contained in:
OoO
2026-05-06 19:49:28 +08:00
parent dc7fe371bd
commit 308efdce25
23 changed files with 863 additions and 341 deletions

View File

@@ -1,7 +1,8 @@
import asyncio
def test_run_with_timeout_supports_sync_function():
def test_run_with_timeout_supports_sync_function(monkeypatch):
monkeypatch.setenv("ELEPHANT_ALPHA_CACHE_DB", ":memory:")
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
result = asyncio.run(ElephantAlphaAutonomousEngine._run_with_timeout(lambda value: value + 1, 41))
@@ -9,7 +10,8 @@ def test_run_with_timeout_supports_sync_function():
assert result == 42
def test_execute_step_rejects_unknown_action():
def test_execute_step_rejects_unknown_action(monkeypatch):
monkeypatch.setenv("ELEPHANT_ALPHA_CACHE_DB", ":memory:")
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
engine = ElephantAlphaAutonomousEngine()
@@ -23,6 +25,7 @@ def test_execute_step_rejects_unknown_action():
def test_execute_step_routes_code_fix_to_autoheal(monkeypatch):
monkeypatch.setenv("ELEPHANT_ALPHA_CACHE_DB", ":memory:")
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
calls = []
@@ -43,6 +46,7 @@ def test_execute_step_routes_code_fix_to_autoheal(monkeypatch):
def test_execute_step_routes_price_adjustment_to_human_review(monkeypatch):
monkeypatch.setenv("ELEPHANT_ALPHA_CACHE_DB", ":memory:")
from services.elephant_alpha_autonomous_engine import ElephantAlphaAutonomousEngine
calls = []
@@ -82,3 +86,118 @@ def test_autoheal_derives_python_exception_from_traceback():
svc = AutoHealService()
assert svc._derive_error_type({"traceback_str": "Traceback (most recent call last):\nNameError"}) == "python_exception"
def test_action_queue_counts_only_operational_actions(monkeypatch):
from sqlalchemy import create_engine, text
import services.elephant_alpha_autonomous_engine as engine_mod
monkeypatch.setattr(engine_mod, "CACHE_DB_PATH", ":memory:")
db = create_engine("sqlite:///:memory:")
conn = db.connect()
conn.execute(text("CREATE TABLE action_plans (status TEXT, action_type TEXT)"))
conn.execute(text("""
INSERT INTO action_plans (status, action_type) VALUES
('pending', 'openclaw_recommendation'),
('pending', 'human_review'),
('pending', 'auto_heal'),
('auto_pending', 'code_review_fix'),
('auto_disabled', 'code_review_fix')
"""))
conn.commit()
class Session:
def execute(self, stmt):
return conn.execute(stmt)
def close(self):
pass
monkeypatch.setattr(engine_mod, "get_session", lambda: Session())
try:
engine = engine_mod.ElephantAlphaAutonomousEngine()
assert engine._get_action_queue_size() == 2
finally:
conn.close()
db.dispose()
def test_trigger_check_respects_persistent_cooldown(monkeypatch, tmp_path):
import services.elephant_alpha_autonomous_engine as engine_mod
monkeypatch.setattr(engine_mod, "CACHE_DB_PATH", str(tmp_path / "ea_cache.db"))
engine = engine_mod.ElephantAlphaAutonomousEngine()
trigger = engine_mod.AutonomousTrigger("resource_optimization", {}, 0.6, True)
engine._store_escalation("resource_optimization")
next_engine = engine_mod.ElephantAlphaAutonomousEngine()
next_engine.triggers = [trigger]
called = {"evaluated": False}
async def evaluate(_trigger):
called["evaluated"] = True
return True
async def execute(_trigger):
raise AssertionError("cooldown should skip execution")
monkeypatch.setattr(next_engine, "_evaluate_trigger", evaluate)
monkeypatch.setattr(next_engine, "_execute_autonomous_decision", execute)
asyncio.run(next_engine._check_triggers())
assert called["evaluated"] is False
def test_no_evidence_resource_escalation_is_suppressed(monkeypatch, tmp_path):
import services.elephant_alpha_autonomous_engine as engine_mod
from services.elephant_alpha_orchestrator import StrategicDecision
monkeypatch.setattr(engine_mod, "CACHE_DB_PATH", str(tmp_path / "ea_cache.db"))
engine = engine_mod.ElephantAlphaAutonomousEngine()
trigger = engine_mod.AutonomousTrigger("resource_optimization", {}, 0.6, True)
decision = StrategicDecision(
priority="medium",
agents_required=["elephant_alpha"],
reasoning="Hermes pre-fetch 失敗,沒有可驗證資料",
expected_outcome="",
confidence=0.60,
execution_plan=[],
resource_requirements={},
)
monkeypatch.setattr(engine, "_get_action_queue_size", lambda: 0)
monkeypatch.setattr(engine, "_get_system_load_percentage", lambda: 0.0)
monkeypatch.setattr(
engine_mod,
"get_session",
lambda: (_ for _ in ()).throw(AssertionError("suppressed escalation should not write DB")),
)
async def fail_send(*_args, **_kwargs):
raise AssertionError("suppressed escalation should not send Telegram")
monkeypatch.setattr(engine, "_run_with_timeout", fail_send)
asyncio.run(engine._escalate_to_human(decision, trigger))
assert engine._load_escalation("no_evidence:resource_optimization") is not None
assert engine._is_trigger_in_cooldown(trigger) is True
def test_resource_escalation_uses_operational_evidence(monkeypatch, tmp_path):
import services.elephant_alpha_autonomous_engine as engine_mod
monkeypatch.setattr(engine_mod, "CACHE_DB_PATH", str(tmp_path / "ea_cache.db"))
engine = engine_mod.ElephantAlphaAutonomousEngine()
monkeypatch.setattr(engine, "_get_action_queue_size", lambda: 17)
monkeypatch.setattr(engine, "_get_system_load_percentage", lambda: 42.0)
actions = engine._build_resource_escalation_actions()
assert actions is not None
assert "auto action queue 17 筆" in actions[0]
assert "Hermes" not in "\n".join(actions)