PMFlow/backend/tests/test_ai_agent_kernel.py

87 lines
3.7 KiB
Python
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.

import os
import unittest
from app.services.ai_agent_kernel import (
compute_capability_score,
gate_execution,
resolve_correction,
select_intent_route,
)
class AIAgentKernelTest(unittest.TestCase):
def test_select_intent_route_prefers_complex_for_multi_clause_instruction(self) -> None:
decision = select_intent_route(
"给我新增个项目,openclaw自动化部署,为了后续方便统一发布。",
project_names=["OpenClaw 自动化部署"],
)
self.assertEqual(decision.function_name, "classify_intent_complex")
self.assertEqual(decision.tier, "high")
self.assertTrue(decision.is_complex)
def test_select_intent_route_simple_instruction(self) -> None:
decision = select_intent_route("归档项目#51")
self.assertEqual(decision.function_name, "classify_intent_ai_pmo_kernel")
self.assertEqual(decision.tier, "low")
self.assertFalse(decision.is_complex)
def test_resolve_correction_detects_rollback(self) -> None:
result = resolve_correction("这条不是新增项目,回滚刚才那次操作。")
self.assertIn(result.action, {"rollback", "replace_target"})
self.assertGreaterEqual(result.confidence, 0.78)
self.assertTrue(result.matched_keywords)
def test_gate_execution_respects_threshold_and_risk(self) -> None:
old_auto = os.environ.get("AGENT_GATE_AUTO_THRESHOLD")
old_review = os.environ.get("AGENT_GATE_REVIEW_THRESHOLD")
old_review_below_auto = os.environ.get("AGENT_GATE_REVIEW_BELOW_AUTO")
os.environ["AGENT_GATE_AUTO_THRESHOLD"] = "0.82"
os.environ["AGENT_GATE_REVIEW_THRESHOLD"] = "0.60"
try:
low = gate_execution(confidence=0.42, risk_level="low")
self.assertEqual(low.action, "reject")
pending = gate_execution(confidence=0.70, risk_level="medium")
self.assertEqual(pending.action, "auto_execute")
high_risk = gate_execution(confidence=0.86, risk_level="high")
self.assertEqual(high_risk.action, "pending_manual")
allowed = gate_execution(confidence=0.93, risk_level="high")
self.assertEqual(allowed.action, "auto_execute")
os.environ["AGENT_GATE_REVIEW_BELOW_AUTO"] = "1"
pending_with_switch = gate_execution(confidence=0.70, risk_level="medium")
self.assertEqual(pending_with_switch.action, "pending_manual")
forced = gate_execution(
confidence=0.99,
risk_level="low",
force_manual=True,
force_manual_code="intent_conflict_write",
force_manual_reason="规则与LLM冲突",
)
self.assertEqual(forced.action, "pending_manual")
self.assertEqual(forced.gate_code, "intent_conflict_write")
finally:
if old_auto is None:
os.environ.pop("AGENT_GATE_AUTO_THRESHOLD", None)
else:
os.environ["AGENT_GATE_AUTO_THRESHOLD"] = old_auto
if old_review is None:
os.environ.pop("AGENT_GATE_REVIEW_THRESHOLD", None)
else:
os.environ["AGENT_GATE_REVIEW_THRESHOLD"] = old_review
if old_review_below_auto is None:
os.environ.pop("AGENT_GATE_REVIEW_BELOW_AUTO", None)
else:
os.environ["AGENT_GATE_REVIEW_BELOW_AUTO"] = old_review_below_auto
def test_compute_capability_score(self) -> None:
score = compute_capability_score(
intent_accuracy=0.82,
gate_precision=0.80,
exec_success=0.78,
correction_success=0.76,
)
self.assertAlmostEqual(score, 0.795, places=3)
if __name__ == "__main__":
unittest.main()