"""AWS automation control-plane client and exact schedule compiler. Production deliberately has no global tick. The API tells a small signed AWS control function exactly which schedules one automation needs; direct events start one worker immediately. With no enabled definitions the scheduler group is empty and ECS has no task. This module has no AWS dependency. The web process talks to the control function over its Function URL using an HMAC over ``timestamp + newline + body``. Local/test deployments with no control URL keep the historical in-process behavior; production Postgres fails closed if the control plane is missing. """ from __future__ import annotations import datetime as dt import hashlib import hmac import json import os import re import time import uuid from zoneinfo import ZoneInfo, ZoneInfoNotFoundError import requests class ControlPlaneError(RuntimeError): pass _FIELD_RANGES = ((0, 59), (0, 23), (1, 31), (1, 12), (0, 6)) _DOW_NAMES = ("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT") _MANAGED_RECURRING = ("schedule", "email") def configured() -> bool: return bool(str(os.environ.get("AIOS_AUTOMATION_CONTROL_URL") or "").strip() and str(os.environ.get("AIOS_AUTOMATION_CONTROL_TOKEN") or "").strip()) def _production_requires_control() -> bool: if str(os.environ.get("STORE_BACKEND") or "").lower() != "pg": return False try: from core import data_binding return data_binding.is_production_deployment() except Exception: return False def _parse_part(spec: str, lo: int, hi: int) -> set[int]: out: set[int] = set() for raw in str(spec).split(","): part = raw.strip() if not part: raise ValueError("empty cron field") step = 1 if "/" in part: part, step_raw = part.split("/", 1) step = int(step_raw) if step < 1: raise ValueError("cron step must be positive") if part in ("*", "?"): first, last = lo, hi elif "-" in part: first_raw, last_raw = part.split("-", 1) first, last = int(first_raw), int(last_raw) else: first = last = int(part) if first < lo or last > hi or first > last: raise ValueError(f"cron value {part!r} is outside {lo}..{hi}") out.update(range(first, last + 1, step)) return out def _field(values: set[int], lo: int, hi: int, names=None) -> str: if values == set(range(lo, hi + 1)): return "*" ordered = sorted(values) return ",".join(str(names[v] if names else v) for v in ordered) def aws_cron_expressions(expr: str) -> list[str]: """Translate one validated POSIX five-field cron into one or two AWS cron expressions. POSIX uses OR when both day-of-month and weekday are restricted. AWS requires one of those fields to be ``?``, so that case becomes two schedules. The durable worker job id includes scheduled time and deduplicates the overlap when both arms match. """ parts = str(expr or "").split() if len(parts) != 5: raise ValueError("a cron schedule has exactly five fields") vals = [_parse_part(p, lo, hi) for p, (lo, hi) in zip(parts, _FIELD_RANGES)] minute = _field(vals[0], 0, 59) hour = _field(vals[1], 0, 23) dom = _field(vals[2], 1, 31) month = _field(vals[3], 1, 12) dow = _field(vals[4], 0, 6, _DOW_NAMES) dom_restricted = parts[2] not in ("*", "?") dow_restricted = parts[4] not in ("*", "?") if dom_restricted and dow_restricted: return [f"cron({minute} {hour} {dom} {month} ? *)", f"cron({minute} {hour} ? {month} {dow} *)"] if dom_restricted: return [f"cron({minute} {hour} {dom} {month} ? *)"] if dow_restricted: return [f"cron({minute} {hour} ? {month} {dow} *)"] return [f"cron({minute} {hour} * {month} ? *)"] def generation(defn: dict) -> str: relevant = {k: defn.get(k) for k in ("id", "kind", "config", "schedule", "trigger", "flow")} body = json.dumps(relevant, sort_keys=True, separators=(",", ":"), default=str).encode() return hashlib.sha256(body).hexdigest()[:20] def schedule_scope(tenant: str, auto_id: str) -> str: digest = hashlib.sha256(f"{tenant}\0{auto_id}".encode()).hexdigest()[:20] return f"a-{digest}" def _base_job(tenant: str, defn: dict, mode: str) -> dict: return {"tenant": str(tenant), "automationId": str(defn.get("id") or ""), "generation": generation(defn), "mode": mode, "scheduledTime": ""} def _recurring_timezone(defn: dict) -> str: """Use the frozen IANA zone for Routes and once automations.""" trigger = defn.get("trigger") or {} if trigger.get("key") == "once": from automation_engine import once_instant once_instant(trigger) return trigger["timezone"] if str(defn.get("kind") or "") != "route_delivery": return "UTC" schedule = defn.get("schedule") if isinstance(defn.get("schedule"), dict) else {} config = defn.get("config") if isinstance(defn.get("config"), dict) else {} snapshot = config.get("routeSnapshot") if isinstance(config.get("routeSnapshot"), dict) else {} timezone = str(schedule.get("timezone") or snapshot.get("timezone") or "").strip() try: ZoneInfo(timezone) except (ZoneInfoNotFoundError, ValueError): raise ValueError("a scheduled route needs a recognized IANA timezone") from None return timezone def recurring_specs(tenant: str, defn: dict) -> list[dict]: scope = schedule_scope(tenant, defn.get("id")) specs: list[dict] = [] schedule = defn.get("schedule") or {} trigger = defn.get("trigger") or {} if trigger.get("key") == "once": if (schedule.get("enabled") and trigger.get("enabled", True) and not trigger.get("paused") and trigger.get("configured", True) and not (defn.get("state") or {}).get("onceClaimedAt")): from automation_engine import once_instant zone = _recurring_timezone(defn) instant = once_instant(trigger) if instant.microsecond: instant = instant.replace(microsecond=0) + dt.timedelta(seconds=1) local = instant.astimezone(ZoneInfo(zone)).strftime('%Y-%m-%dT%H:%M:%S') specs.append({"name": f"{scope}-schedule-0", "mode": "schedule", "expression": f"at({local})", "timezone": zone, "oneTime": True, "job": _base_job(tenant, defn, "schedule")}) elif schedule.get("enabled"): for index, expression in enumerate(aws_cron_expressions(schedule.get("cron") or "")): specs.append({"name": f"{scope}-schedule-{index}", "mode": "schedule", "expression": expression, "timezone": _recurring_timezone(defn), "job": _base_job(tenant, defn, "schedule")}) if (trigger.get("key") == "email" and trigger.get("enabled", True) and not trigger.get("paused") and trigger.get("configured", True)): specs.append({"name": f"{scope}-email-0", "mode": "email", "expression": "rate(15 minutes)", "timezone": "UTC", "job": _base_job(tenant, defn, "email")}) return specs def pending_specs(tenant: str, defn: dict, when: dt.datetime | None = None) -> list[dict]: from automation_engine import pending_collect_ids if str(defn.get("id") or "") not in pending_collect_ids( None, definitions={defn["id"]: defn}): return [] when = when or (dt.datetime.now(dt.timezone.utc) + dt.timedelta(minutes=15)) when = when.astimezone(dt.timezone.utc).replace(microsecond=0) expression = f"at({when.strftime('%Y-%m-%dT%H:%M:%S')})" scope = schedule_scope(tenant, defn.get("id")) return [{"name": f"{scope}-pending-0", "mode": "pending", "expression": expression, "timezone": "UTC", "oneTime": True, "job": _base_job(tenant, defn, "pending")}] def _signed_post(payload: dict) -> dict: url = str(os.environ.get("AIOS_AUTOMATION_CONTROL_URL") or "").strip() token = str(os.environ.get("AIOS_AUTOMATION_CONTROL_TOKEN") or "").strip() if not url or not token: if _production_requires_control(): raise ControlPlaneError("production automation control plane is not configured") return {"local": True} body = json.dumps(payload, sort_keys=True, separators=(",", ":")).encode() stamp = str(int(time.time())) signature = hmac.new(token.encode(), stamp.encode() + b"\n" + body, hashlib.sha256).hexdigest() try: response = requests.post(url, data=body, timeout=15, headers={"Content-Type": "application/json", "X-AIOS-Timestamp": stamp, "X-AIOS-Signature": signature}) except Exception as exc: raise ControlPlaneError(f"automation control request failed: {type(exc).__name__}") from exc if response.status_code >= 300: raise ControlPlaneError(f"automation control answered HTTP {response.status_code}") try: return response.json() except Exception as exc: raise ControlPlaneError("automation control returned invalid JSON") from exc def sync_definition(tenant: str, defn: dict) -> dict: return _signed_post({"action": "sync", "tenant": str(tenant), "automationId": str(defn.get("id") or ""), "managedModes": list(_MANAGED_RECURRING), "schedules": recurring_specs(tenant, defn)}) def sync_pending(tenant: str, defn: dict, when: dt.datetime | None = None) -> dict: return _signed_post({"action": "sync", "tenant": str(tenant), "automationId": str(defn.get("id") or ""), "managedModes": ["pending"], "schedules": pending_specs(tenant, defn, when=when)}) def delete_definition(tenant: str, auto_id: str) -> dict: return _signed_post({"action": "sync", "tenant": str(tenant), "automationId": str(auto_id), "managedModes": ["schedule", "email", "pending"], "schedules": []}) def dispatch(tenant: str, auto_id: str, *, mode="manual", username="automation", rows=None, request_id=None) -> dict: job = {"tenant": str(tenant), "automationId": str(auto_id), "mode": str(mode), "username": str(username or "automation"), "rows": list(rows or [])[:100], "requestId": str(request_id or uuid.uuid4())} return _signed_post({"action": "dispatch", "job": job}) def safe_schedule_name(name: str) -> bool: return bool(re.fullmatch(r"a-[0-9a-f]{20}-(schedule-[01]|email-0|pending-0)", str(name)))