system-prompts-and-models-o.../dealix/auto_client_acquisition/v3/agents.py
Dealix Builder e34cc729aa feat(dealix): py3.10/3.11 compat shim + 54 unit tests for business/innovation/ai
PROBLEM
The codebase used Python 3.11+ stdlib features (`from datetime import UTC`,
`from enum import StrEnum`) in 22 files, breaking local dev on Python 3.10
(Windows users) and any pytest run that imports the affected modules.

SOLUTION
1. New `core/_py_compat.py` providing UTC + StrEnum shims that:
   - On 3.11+ re-export the stdlib names (zero overhead)
   - On 3.10 fall back to `timezone.utc` and a (str, Enum) backport

2. All 22 affected files patched to import from the shim:
   - core/utils.py, core/config/models.py
   - api/routers/admin.py
   - auto_client_acquisition/{ai/model_router, agents/{intake,icp_matcher},
     v3/{memory,agents,compliance_os,market_radar},
     personal_operator/{operator,memory,launch_report},
     innovation/{proof_ledger_repo,command_feed_live}}.py
   - autonomous_growth/agents/sector_intel.py
   - dealix/{trust/{approval,tool_verification,policy},
     observability/cost_tracker,
     contracts/{evidence_pack,event_envelope,audit_log,decision},
     classifications/__init__,
     governance/approvals}.py

3. Three new test suites for previously-untested layers (54 tests):
   - tests/unit/test_business_suite.py — gtm_plan, launch_metrics,
     market_positioning, pricing_strategy, proof_pack, unit_economics,
     verticals (28 tests covering plan recommendation, performance fee,
     ROI math, account health grading, vertical playbook structure)
   - tests/unit/test_innovation_suite.py — aeo_radar, command_feed,
     deal_rooms, experiments, growth_missions, proof_ledger, ten_in_ten
     (18 tests covering deterministic reproducibility, card type taxonomy,
     pending-approval invariant, kill-mission visibility)
   - tests/unit/test_ai_model_router.py — ModelTask + get_model_route +
     estimate_model_cost_class + requires_guardrail (8 tests covering
     enum integrity, route round-trip, guardrail bool contract)

VERIFICATION
- ast.parse green on all 22 patched files
- pytest tests/unit/ → 477 passed, 2 skipped (provider smoke needs API keys)
  on Python 3.10.12 venv with project requirements installed
- No behavior change on 3.11+: the shim re-exports stdlib symbols

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 14:50:04 +03:00

124 lines
4.3 KiB
Python

"""Safe AI Agent Runtime for Dealix v3.
This is intentionally deterministic and policy-first. It can later be backed by
LangGraph, OpenAI Agents SDK, CrewAI, or Google ADK without changing the public
contract.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from core._py_compat import StrEnum
from typing import Any
from uuid import uuid4
class AgentName(StrEnum):
PROSPECTING = "prospecting"
SIGNAL = "signal"
ENRICHMENT = "enrichment"
PERSONALIZATION = "personalization"
COMPLIANCE = "compliance"
OUTREACH = "outreach"
REPLY = "reply"
MEETING = "meeting"
DEAL_COACH = "deal_coach"
CUSTOMER_SUCCESS = "customer_success"
EXECUTIVE_ANALYST = "executive_analyst"
class TaskStatus(StrEnum):
CREATED = "created"
NEEDS_APPROVAL = "needs_approval"
APPROVED = "approved"
EXECUTED = "executed"
REJECTED = "rejected"
BLOCKED = "blocked"
@dataclass
class AgentTask:
agent: AgentName
objective: str
customer_id: str
context: dict[str, Any] = field(default_factory=dict)
requires_approval: bool = True
risk_level: str = "medium"
task_id: str = field(default_factory=lambda: f"task_{uuid4().hex[:12]}")
status: TaskStatus = TaskStatus.CREATED
def to_dict(self) -> dict[str, Any]:
return {
"task_id": self.task_id,
"agent": self.agent.value,
"objective": self.objective,
"customer_id": self.customer_id,
"context": self.context,
"requires_approval": self.requires_approval,
"risk_level": self.risk_level,
"status": self.status.value,
}
class SafeAgentRuntime:
"""Small policy-first runtime for agent tasks."""
restricted_actions = {"send_cold_whatsapp", "auto_linkedin_dm", "delete_data", "export_pii"}
def __init__(self) -> None:
self.tasks: dict[str, AgentTask] = {}
def create_task(self, task: AgentTask) -> AgentTask:
action = str(task.context.get("action", ""))
if action in self.restricted_actions:
task.status = TaskStatus.BLOCKED
task.risk_level = "blocked"
elif task.requires_approval:
task.status = TaskStatus.NEEDS_APPROVAL
else:
task.status = TaskStatus.APPROVED
self.tasks[task.task_id] = task
return task
def approve(self, task_id: str) -> AgentTask:
task = self.tasks[task_id]
if task.status == TaskStatus.BLOCKED:
return task
task.status = TaskStatus.APPROVED
return task
def reject(self, task_id: str) -> AgentTask:
task = self.tasks[task_id]
task.status = TaskStatus.REJECTED
return task
def execute(self, task_id: str) -> dict[str, Any]:
task = self.tasks[task_id]
if task.status not in {TaskStatus.APPROVED, TaskStatus.EXECUTED}:
return {"ok": False, "task": task.to_dict(), "reason": "approval_required_or_blocked"}
task.status = TaskStatus.EXECUTED
return {
"ok": True,
"task": task.to_dict(),
"result": {
"summary": f"{task.agent.value} completed objective: {task.objective}",
"next_step": "record_outcome_in_revenue_memory",
},
}
def agent_catalog() -> list[dict[str, str]]:
return [
{"agent": AgentName.PROSPECTING.value, "job": "Find high-fit Saudi B2B accounts."},
{"agent": AgentName.SIGNAL.value, "job": "Detect why-now buying triggers."},
{"agent": AgentName.ENRICHMENT.value, "job": "Complete company/contact context."},
{"agent": AgentName.PERSONALIZATION.value, "job": "Draft Arabic/English outreach."},
{"agent": AgentName.COMPLIANCE.value, "job": "Block unsafe PDPL/contactability actions."},
{"agent": AgentName.OUTREACH.value, "job": "Queue or send approved messages."},
{"agent": AgentName.REPLY.value, "job": "Classify replies and intent."},
{"agent": AgentName.MEETING.value, "job": "Convert positive replies into meetings."},
{"agent": AgentName.DEAL_COACH.value, "job": "Recommend next best deal action."},
{"agent": AgentName.CUSTOMER_SUCCESS.value, "job": "Prevent churn and surface expansion."},
{"agent": AgentName.EXECUTIVE_ANALYST.value, "job": "Write founder daily brief."},
]