import copy import threading import time from collections import deque from datetime import datetime, timezone from typing import Any, ClassVar from croniter import croniter from bot.control_plane_config_helpers import ( budget_usage, compute_next_goal_due_at, goal_target_met, infer_goal_observed_value, normalize_goals, ) from bot.control_plane_config_helpers import ( runtime_base_snapshot as build_runtime_base_snapshot, ) from bot.control_plane_constants import ( clip_text, default_goals_copy, to_float, to_int, utc_iso, ) class ControlPlaneConfigRuntime: _SIMPLE_BOOL_FIELDS: ClassVar[tuple[str, ...]] = ( "autonomy_enabled", "clip_pipeline_enabled", "clip_api_enabled", ) _INT_FIELD_RULES: ClassVar[dict[str, tuple[int, int]]] = { "heartbeat_interval_seconds": (15, 3600), "min_cooldown_seconds": (15, 3600), "budget_messages_10m": (0, 100), "budget_messages_60m": (0, 600), "budget_messages_daily": (0, 5000), "action_ignore_after_seconds": (60, 86_400), } def __init__(self) -> None: self._lock = threading.Lock() self._config: dict[str, Any] = { "version": 1, "autonomy_enabled": False, "agent_suspended": False, "clip_pipeline_enabled": False, "clip_api_enabled": False, "clip_mode_default": "live", "heartbeat_interval_seconds": 60, "min_cooldown_seconds": 90, "budget_messages_10m": 2, "budget_messages_60m": 8, "budget_messages_daily": 30, "action_ignore_after_seconds": 900, "goals": default_goals_copy(), "updated_at": utc_iso(time.time()), } self._runtime: dict[str, Any] = { "loop_running": False, "agent_suspended_at": 0.0, "agent_suspend_reason": "", "agent_last_resumed_at": 0.0, "agent_last_resume_reason": "", "last_heartbeat_at": 0.0, "last_tick_at": 0.0, "last_tick_reason": "boot", "last_goal_id": "", "last_goal_risk": "", "autonomy_ticks_total": 0, "autonomy_goal_runs_total": 0, "autonomy_budget_blocked_total": 0, "autonomy_dispatch_failures_total": 0, "autonomy_auto_chat_sent_total": 0, "autonomy_goal_kpi_met_total": 0, "autonomy_goal_kpi_missed_total": 0, "autonomy_last_goal_kpi_status": "", "autonomy_last_goal_kpi_goal_id": "", "autonomy_last_goal_kpi_name": "", "autonomy_last_goal_kpi_observed_value": 0.0, "autonomy_last_goal_kpi_target_value": 0.0, "autonomy_last_goal_kpi_comparison": "", "autonomy_last_block_reason": "", } self._auto_chat_sent_timestamps: deque[float] = deque(maxlen=3000) self._next_goal_due_at: dict[str, float] = {} def get_config(self) -> dict[str, Any]: with self._lock: return copy.deepcopy(self._config) def _apply_simple_bool_fields( self, *, config: dict[str, Any], payload: dict[str, Any], ) -> None: for field_name in self._SIMPLE_BOOL_FIELDS: if field_name in payload: config[field_name] = bool(payload.get(field_name)) def _apply_agent_suspended_field( self, *, config: dict[str, Any], payload: dict[str, Any], now: float, ) -> None: if "agent_suspended" not in payload: return next_suspended = bool(payload.get("agent_suspended")) previous_suspended = bool(self._config.get("agent_suspended", False)) config["agent_suspended"] = next_suspended if next_suspended == previous_suspended: return if next_suspended: self._runtime["agent_suspended_at"] = now self._runtime["agent_suspend_reason"] = "config_update" self._runtime["last_heartbeat_at"] = now return self._runtime["agent_suspended_at"] = 0.0 self._runtime["agent_suspend_reason"] = "" self._runtime["agent_last_resumed_at"] = now self._runtime["agent_last_resume_reason"] = "config_update" self._runtime["last_heartbeat_at"] = now def _apply_clip_mode_default_field( self, *, config: dict[str, Any], payload: dict[str, Any], ) -> None: if "clip_mode_default" not in payload: return clip_mode = str(payload.get("clip_mode_default") or "live").strip().lower() if clip_mode in {"live", "vod"}: config["clip_mode_default"] = clip_mode def _apply_int_fields( self, *, config: dict[str, Any], payload: dict[str, Any], ) -> None: for field_name, (minimum, maximum) in self._INT_FIELD_RULES.items(): if field_name not in payload: continue config[field_name] = to_int( payload.get(field_name), minimum=minimum, maximum=maximum, fallback=config[field_name], ) def _goal_session_results_by_id(self) -> dict[str, dict[str, Any]]: return { str(item.get("id", "") or ""): copy.deepcopy(item.get("session_result") or {}) for item in self._config.get("goals", []) if isinstance(item, dict) } def _apply_goals_field( self, *, config: dict[str, Any], payload: dict[str, Any], ) -> None: if "goals" not in payload: return previous_results = self._goal_session_results_by_id() normalized_goals = normalize_goals(payload.get("goals")) for goal in normalized_goals: goal_id = str(goal.get("id", "") or "") if not goal.get("session_result") and goal_id in previous_results: goal["session_result"] = previous_results[goal_id] config["goals"] = normalized_goals active_goal_ids = {goal["id"] for goal in config["goals"]} self._next_goal_due_at = { goal_id: due_at for goal_id, due_at in self._next_goal_due_at.items() if goal_id in active_goal_ids } def update_config(self, payload: dict[str, Any]) -> dict[str, Any]: if not isinstance(payload, dict): raise ValueError("Payload de configuracao invalido.") now = time.time() with self._lock: config = copy.deepcopy(self._config) self._apply_simple_bool_fields(config=config, payload=payload) self._apply_agent_suspended_field(config=config, payload=payload, now=now) self._apply_clip_mode_default_field(config=config, payload=payload) self._apply_int_fields(config=config, payload=payload) self._apply_goals_field(config=config, payload=payload) config["updated_at"] = utc_iso(now) self._config = config return copy.deepcopy(self._config) def suspend_agent( self, *, reason: str = "manual_dashboard", timestamp: float | None = None, ) -> dict[str, Any]: now = time.time() if timestamp is None else float(timestamp) safe_reason = clip_text(reason or "manual_dashboard", max_chars=120) with self._lock: self._config["agent_suspended"] = True self._config["updated_at"] = utc_iso(now) self._runtime["agent_suspended_at"] = now self._runtime["agent_suspend_reason"] = safe_reason self._runtime["last_heartbeat_at"] = now return copy.deepcopy(self._config) def resume_agent( self, *, reason: str = "manual_dashboard", timestamp: float | None = None, ) -> dict[str, Any]: now = time.time() if timestamp is None else float(timestamp) safe_reason = clip_text(reason or "manual_dashboard", max_chars=120) with self._lock: self._config["agent_suspended"] = False self._config["updated_at"] = utc_iso(now) self._runtime["agent_suspended_at"] = 0.0 self._runtime["agent_suspend_reason"] = "" self._runtime["agent_last_resumed_at"] = now self._runtime["agent_last_resume_reason"] = safe_reason self._runtime["last_heartbeat_at"] = now return copy.deepcopy(self._config) def set_loop_running(self, running: bool) -> None: with self._lock: self._runtime["loop_running"] = bool(running) def touch_heartbeat(self, timestamp: float | None = None) -> None: now = time.time() if timestamp is None else float(timestamp) with self._lock: self._runtime["last_heartbeat_at"] = now def register_tick(self, reason: str, timestamp: float | None = None) -> None: now = time.time() if timestamp is None else float(timestamp) with self._lock: self._runtime["last_tick_at"] = now self._runtime["last_tick_reason"] = clip_text(reason or "tick", max_chars=40) self._runtime["autonomy_ticks_total"] += 1 def register_goal_run( self, goal_id: str, risk: str, timestamp: float | None = None, ) -> None: now = time.time() if timestamp is None else float(timestamp) with self._lock: self._runtime["autonomy_goal_runs_total"] += 1 self._runtime["last_goal_id"] = clip_text(goal_id, max_chars=60) self._runtime["last_goal_risk"] = clip_text(risk, max_chars=32) self._runtime["last_heartbeat_at"] = now def register_budget_block(self, reason: str, timestamp: float | None = None) -> None: now = time.time() if timestamp is None else float(timestamp) with self._lock: self._runtime["autonomy_budget_blocked_total"] += 1 self._runtime["autonomy_last_block_reason"] = clip_text(reason, max_chars=80) self._runtime["last_heartbeat_at"] = now def register_dispatch_failure(self, reason: str, timestamp: float | None = None) -> None: now = time.time() if timestamp is None else float(timestamp) with self._lock: self._runtime["autonomy_dispatch_failures_total"] += 1 self._runtime["autonomy_last_block_reason"] = clip_text(reason, max_chars=80) self._runtime["last_heartbeat_at"] = now def register_goal_session_result( self, *, goal_id: str, outcome: str, observed_value: float | None = None, details: str = "", timestamp: float | None = None, ) -> dict[str, Any] | None: now = time.time() if timestamp is None else float(timestamp) safe_goal_id = str(goal_id or "").strip() safe_outcome = clip_text(str(outcome or "").strip().lower(), max_chars=80) if not safe_goal_id: return None with self._lock: matching_goal: dict[str, Any] | None = None for goal in self._config.get("goals", []): if not isinstance(goal, dict): continue if str(goal.get("id", "") or "").strip() == safe_goal_id: matching_goal = goal break if matching_goal is None: return None target_value = to_float( matching_goal.get("target_value", 1.0), minimum=0.0, maximum=10_000.0, fallback=1.0, ) comparison = str(matching_goal.get("comparison", "gte") or "gte").strip().lower() observed = ( to_float( observed_value, minimum=0.0, maximum=10_000.0, fallback=0.0, ) if observed_value is not None else infer_goal_observed_value(matching_goal, safe_outcome) ) met = goal_target_met(observed, target_value, comparison) result = { "kpi_name": str(matching_goal.get("kpi_name", "")), "target_value": target_value, "comparison": comparison, "observed_value": observed, "met": met, "outcome": safe_outcome, "details": clip_text(str(details or "").strip(), max_chars=180), "evaluated_at": utc_iso(now), } matching_goal["session_result"] = result self._config["updated_at"] = utc_iso(now) if met: self._runtime["autonomy_goal_kpi_met_total"] += 1 else: self._runtime["autonomy_goal_kpi_missed_total"] += 1 self._runtime["autonomy_last_goal_kpi_status"] = "met" if met else "missed" self._runtime["autonomy_last_goal_kpi_goal_id"] = clip_text(safe_goal_id, max_chars=60) self._runtime["autonomy_last_goal_kpi_name"] = clip_text( str(matching_goal.get("kpi_name", "")), max_chars=60, ) self._runtime["autonomy_last_goal_kpi_observed_value"] = observed self._runtime["autonomy_last_goal_kpi_target_value"] = target_value self._runtime["autonomy_last_goal_kpi_comparison"] = comparison self._runtime["last_heartbeat_at"] = now return copy.deepcopy(result) def can_send_auto_chat( self, timestamp: float | None = None ) -> tuple[bool, str, dict[str, int]]: now = time.time() if timestamp is None else float(timestamp) with self._lock: usage = budget_usage(self._auto_chat_sent_timestamps, now) cfg = self._config if bool(cfg.get("agent_suspended", False)): return False, "agent_suspended", usage if int(cfg["budget_messages_daily"]) <= 0: return False, "budget_daily_disabled", usage last_sent = ( self._auto_chat_sent_timestamps[-1] if self._auto_chat_sent_timestamps else 0.0 ) min_cooldown = int(cfg["min_cooldown_seconds"]) if last_sent > 0 and now - last_sent < min_cooldown: return False, "cooldown_active", usage if int(cfg["budget_messages_10m"]) >= 0 and usage["messages_10m"] >= int( cfg["budget_messages_10m"] ): return False, "budget_10m_exceeded", usage if int(cfg["budget_messages_60m"]) >= 0 and usage["messages_60m"] >= int( cfg["budget_messages_60m"] ): return False, "budget_60m_exceeded", usage if usage["messages_daily"] >= int(cfg["budget_messages_daily"]): return False, "budget_daily_exceeded", usage return True, "ok", usage def register_auto_chat_sent(self, timestamp: float | None = None) -> None: now = time.time() if timestamp is None else float(timestamp) with self._lock: self._auto_chat_sent_timestamps.append(now) self._runtime["autonomy_auto_chat_sent_total"] += 1 self._runtime["last_heartbeat_at"] = now def consume_due_goals( self, *, force: bool = False, timestamp: float | None = None ) -> list[dict[str, Any]]: now = time.time() if timestamp is None else float(timestamp) with self._lock: if bool(self._config.get("agent_suspended", False)) and not force: return [] if not self._config.get("autonomy_enabled", False) and not force: return [] due_goals: list[dict[str, Any]] = [] active_goal_ids = set() for goal in self._config.get("goals", []): if not isinstance(goal, dict): continue goal_id = str(goal.get("id", "") or "").strip() if not goal_id: continue active_goal_ids.add(goal_id) if not bool(goal.get("enabled", True)): continue schedule_type = str(goal.get("schedule_type", "interval")).strip().lower() if force: due_goals.append(copy.deepcopy(goal)) self._next_goal_due_at[goal_id] = compute_next_goal_due_at(goal, now) if schedule_type == "fixed_time": goal["enabled"] = False continue due_at = self._next_goal_due_at.get(goal_id) if due_at is None: initial_due = compute_next_goal_due_at(goal, now) if schedule_type == "fixed_time" and initial_due < now - 60: goal["enabled"] = False continue self._next_goal_due_at[goal_id] = initial_due continue if now >= due_at: due_goals.append(copy.deepcopy(goal)) if schedule_type == "fixed_time": goal["enabled"] = False else: self._next_goal_due_at[goal_id] = compute_next_goal_due_at(goal, now) self._next_goal_due_at = { goal_id: due_at for goal_id, due_at in self._next_goal_due_at.items() if goal_id in active_goal_ids } return due_goals def action_ignore_after_seconds(self) -> int: with self._lock: return max(60, int(self._config.get("action_ignore_after_seconds", 900))) def runtime_base_snapshot(self, *, timestamp: float | None = None) -> dict[str, Any]: now = time.time() if timestamp is None else float(timestamp) with self._lock: usage = budget_usage(self._auto_chat_sent_timestamps, now) cfg = dict(self._config) rt = dict(self._runtime) return build_runtime_base_snapshot(config=cfg, runtime=rt, usage=usage)