File size: 9,681 Bytes
f3b0a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""World → hidden checks (scenario groups, route/inhibit cases, preservation) and visible examples.

Checks are derived from the world *spec* (names, targets, windows, labels, receivers) through the
reference evaluator, never from oracle rule text (the structural freeze boundary, §4.3).
"""

from __future__ import annotations

import random
from fractions import Fraction as F

from . import promsim as ps
from . import routing
from .alerts import BURN, RECORD_PREFIX, record_rules
from .grader.loader import canon
from .grader.promrun import alert_count_expr
from .grader.static import am_hashes
from .scenarios import Built, build_alert_scenarios, record_scenarios
from .world import World

INPUT_METRICS = ["http_requests_total", "grpc_server_handled_total", "http_request_duration_seconds_bucket",
                 "http_request_duration_seconds_count", "container_memory_working_set_bytes",
                 "container_spec_memory_limit_bytes", "kube_pod_container_status_restarts_total", "up"]


def _series_json(scen: ps.Scenario) -> list[dict]:
    return [{"series": s.selector(), "values": ps.encode_values(s.values)} for s in scen.series]


def _alert_groups(rid: str, a, built: list[Built]) -> tuple[list[dict], dict]:
    groups, fire_point = [], None
    for b in built:
        gname = f"{rid}.{b.name}"
        probes = []
        for i, p in enumerate(b.probes):
            expr = alert_count_expr(a.name, p.service, op="!=" if p.other else "=")
            expect = 1 if (p.intent == "fire" and not p.other) else 0
            probes.append({"id": f"{rid}.{p.family}.{b.name}.{i}", "expr": expr, "t": p.t, "expect": expect,
                           "family": p.family})
        groups.append({"name": gname, "req": rid, "input_series": _series_json(b.scen), "probes": probes})
        if b.fire_point and fire_point is None:
            fire_point = {"group": gname, "t": b.fire_point[0], "service": b.fire_point[1]}
    return groups, fire_point


def _value_probe(rid: str, i: int, t: int, rec: str, svc: str, v: F) -> dict:
    tol = max(1e-12, abs(float(v)) * 1e-6)
    expr = f'count(abs({rec}{{service="{svc}"}} - {float(v)!r}) <= {tol!r}) or vector(0)'
    return {"id": f"{rid}.value.{i}", "expr": expr, "t": t, "expect": 1, "family": "value"}


def build_hidden(w: World) -> tuple[dict, list[dict], list[dict]]:
    """Returns (spec, scenario groups, oracle record groups)."""
    rng = random.Random(w.tseed ^ 0x5EED_4ED)
    ns = rng.choice(["prod", "production", "shop-prod", "apps"])
    groups: list[dict] = []
    reqs_json = []
    s0 = w.services[0]
    decoy = w.decoys[0]
    required_records = sorted({RECORD_PREFIX + win for r in w.reqs if r.kind == "recording" for win in r.record["windows"]})
    oracle_records = []
    for r in w.reqs:
        entry = {"id": r.id, "kind": r.kind, "category": r.category, "weight": r.weight, "q_pristine": 0.0,
                 "defect": r.defect}
        if r.alert is not None:
            a = r.alert
            svc = a.service or rng.choice([s.name for s in w.services])
            built = build_alert_scenarios(a, random.Random(rng.random()), svc, decoy, ns)
            g, fp = _alert_groups(r.id, a, built)
            groups += g
            entry["alert"] = {"name": a.name, "kind": a.kind, "service": a.service, "labels": a.labels,
                              "runbook": a.runbook, "expected_count": 1, "fire_point": fp,
                              "reads_required_records": bool(a.params.get("reads_required"))}
        elif r.kind == "recording":
            rc = r.record
            b = 1 - F(s0.target)
            probes, n = [], 0
            for scen, exp in record_scenarios(rc["family"], rc["windows"], random.Random(rng.random()), s0.name,
                                              decoy, ns, b, rc["slo_le"]):
                probes = []
                for t, rec, svc, v in exp:
                    probes.append(_value_probe(r.id, n, t, rec, svc, v))
                    n += 1
                last_t = max(t for t, *_ in exp)
                for win in rc["windows"]:
                    probes.append({"id": f"{r.id}.card.{scen.name}.{win}", "expr": f"count({RECORD_PREFIX}{win}) or vector(0)",
                                   "t": last_t, "expect": 2, "family": "card"})
                groups.append({"name": f"{r.id}.{scen.name}", "req": r.id, "input_series": _series_json(scen),
                               "probes": probes})
            counts = {}
            for stem_groups in w.oracle_files.values():
                for gr in stem_groups:
                    for rule in gr["rules"]:
                        if rule.get("record") in {RECORD_PREFIX + x for x in rc["windows"]}:
                            counts[rule["record"]] = counts.get(rule["record"], 0) + 1
            entry["records"] = {"names": [RECORD_PREFIX + x for x in rc["windows"]], "expected_count": counts}
            oracle_records.append({"name": f"af-oracle-{r.id}", "rules": record_rules(rc["family"], rc["windows"], rc["slo_le"])})
        elif r.kind == "route":
            entry["route"] = _route_entry(w, r)
        elif r.kind == "inhibit":
            entry["inhibit"] = {"cases": routing.inhibit_cases(
                r.inhibit["source"], r.inhibit["targets"], r.inhibit["service"], decoy, r.inhibit["team"],
                _bystander(w))}
        reqs_json.append(entry)
    spec = {"version": 1, "task_id": w.task_id, "rules_format": w.axes["rules_format"],
            "requirements": reqs_json, "required_records": required_records, "input_metrics": INPUT_METRICS,
            "preserve": _preserve(w), "decoys": w.decoys}
    return spec, groups, oracle_records


def _inhibit_names(w: World) -> set[str]:
    return {n for r in w.reqs if r.kind == "inhibit" for n in [r.inhibit["source"], *r.inhibit["targets"]]}


def _bystander(w: World) -> str:
    touched = {r.alert.name for r in w.reqs if r.alert} | _inhibit_names(w)
    names = [a.name for a in w.alerts if a.name not in touched and a.service is None]
    return names[0] if names else "HostHighCpuLoad"


def _route_entry(w: World, r) -> dict:
    team, svc = r.route["team"], r.route["service"]
    others = [t for t in w.extra_teams if t != team]
    by_sev = {}
    for a in w.alerts:
        if a.service == svc:
            by_sev.setdefault(a.labels["severity"], a.name)
    cases = routing.route_cases(team, svc, others[0], by_sev)
    linked = next((x.alert for x in w.reqs if x.alert and x.alert.service == svc), None)
    if linked is not None:
        cases["chain"] = {"alert": linked.name, "service": svc, "required_labels": linked.labels,
                          "runbook": linked.runbook, "expect": routing.policy(team, linked.labels["severity"])}
    return cases


def _preserve(w: World) -> dict:
    touched = {r.alert.name for r in w.reqs if r.alert}
    req_recs = {(r.record["family"], RECORD_PREFIX + win) for r in w.reqs if r.kind == "recording"
                for win in r.record["windows"]}
    rules = []
    for stem, groups in sorted(w.oracle_files.items()):
        for g in groups:
            fam = g["name"][4:] if g["name"].startswith("sli-") else None
            for rule in g["rules"]:
                if rule.get("alert") in touched or (fam, rule.get("record")) in req_recs:
                    continue
                kind = "alert" if "alert" in rule else "record"
                rules.append({"kind": kind, "name": rule[kind], "hash": canon(rule, g)})
    route_teams = {r.route["team"] for r in w.reqs if r.kind == "route"}
    routed = sorted({t for rt in w.am_oracle["route"]["routes"] for m in rt.get("matchers", [])
                     if m.startswith("team=") for t in [m.split('"')[1]]} - route_teams)
    svc_of = {s.team: s.name for s in w.services}
    untouched = [a for a in w.alerts if a.name not in touched and a.name not in _inhibit_names(w)]
    pairs = [(a.name, a.service or w.services[0].name, a.labels.get("team", w.platform_team)) for a in untouched[:2]]
    req_sources = {r.id: (r.inhibit["source"], r.inhibit["service"]) for r in w.reqs if r.kind == "inhibit"}
    return {"rules": rules, "am": am_hashes(w.am_oracle),
            "route_cases": routing.preserved_route_cases(routed, svc_of),
            "inhibit_cases": routing.preserved_inhibit_cases(pairs, req_sources)}


def build_visible(w: World) -> dict[str, str]:
    """Example promtool tests for the agent (different sub-seed, decoy services, fewer scenarios)."""
    import yaml
    rng = random.Random(w.tseed ^ 0xA11CE)
    ns = "staging"
    out = {}
    alert_reqs = [r for r in w.reqs if r.alert is not None][:2]
    for r in alert_reqs:
        a = r.alert
        svc = a.service or w.services[0].name
        built = build_alert_scenarios(a, random.Random(rng.random()), svc, w.decoys[1], ns,
                                      only={"sustained", "normal", "all_down", "crashing"})
        tests = []
        for b in built:
            pe = [{"expr": alert_count_expr(a.name, p.service, op="!=" if p.other else "="), "eval_time": f"{p.t}m",
                   "exp_samples": [{"labels": "{}", "value": 1 if (p.intent == "fire" and not p.other) else 0}]}
                  for p in b.probes]
            tests.append({"name": f"{a.name} {b.name}", "interval": "1m", "input_series": _series_json(b.scen),
                          "promql_expr_test": pe})
        doc = {"rule_files": ["../../rules/*.yml", "../../rules/*.yaml"], "evaluation_interval": "1m", "tests": tests}
        out[f"{a.name}.test.yml"] = yaml.safe_dump(doc, sort_keys=False, width=10**9)
    return out


def burn_windows(kind: str) -> list[str]:
    return list(BURN[kind][:2])