File size: 10,935 Bytes
5a07a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ca0aa
5a07a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ca0aa
81a8217
 
 
 
 
 
a9ca0aa
 
 
 
 
 
 
 
 
 
 
 
 
5a07a58
 
 
 
81a8217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a07a58
 
a9ca0aa
5a07a58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""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": "<aws.scheduler.scheduled-time>"}


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)))