run_outcome.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """判定 find_agent 调度执行是否真正成功完成。"""
  2. from __future__ import annotations
  3. from dataclasses import dataclass
  4. from supply_agent.types import AgentResult
  5. from supply_infra.services.video_discovery_service import get_video_discovery_service
  6. @dataclass(frozen=True)
  7. class FindAgentRunOutcome:
  8. """一次运行的技术完成状态与独立业务结果。"""
  9. succeeded: bool
  10. business_outcome: str
  11. goal_met: bool
  12. valid_primary_count: int
  13. failure_reason: str | None = None
  14. def evaluate_find_agent_run(
  15. run_id: str,
  16. agent_result: AgentResult | None = None,
  17. ) -> FindAgentRunOutcome:
  18. """技术完成与业务达标分开判定;业务不足 5 条不会触发重跑。"""
  19. del agent_result
  20. run = get_video_discovery_service().lookup_run(run_id)
  21. if run is None:
  22. return FindAgentRunOutcome(
  23. succeeded=False,
  24. business_outcome="failed",
  25. goal_met=False,
  26. valid_primary_count=0,
  27. failure_reason="run_not_found",
  28. )
  29. status = str(run.get("status") or "")
  30. valid_primary_count = int(run.get("valid_primary_count") or 0)
  31. business_outcome = str(run.get("outcome_status") or "")
  32. if status == "finished" and business_outcome in {
  33. "goal_met",
  34. "partial",
  35. "no_match",
  36. }:
  37. return FindAgentRunOutcome(
  38. succeeded=True,
  39. business_outcome=business_outcome,
  40. goal_met=business_outcome == "goal_met",
  41. valid_primary_count=valid_primary_count,
  42. )
  43. failure_reason = "run_failed" if status == "failed" else "run_not_finished"
  44. return FindAgentRunOutcome(
  45. succeeded=False,
  46. business_outcome="failed",
  47. goal_met=False,
  48. valid_primary_count=valid_primary_count,
  49. failure_reason=failure_reason,
  50. )