File size: 11,506 Bytes
1175c0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""
Infrastructure state machine.

Manages the full service topology, cascade propagation, and the
validate β†’ mutate β†’ tick execution ordering.

This is the central coordinator that scenarios inject faults into
and the environment executes actions against.
"""

from __future__ import annotations

import random
from typing import Any, Dict, List, Optional, Set, Tuple

from .service import Deploy, ServiceState
from .alerts import evaluate_alerts
from .logs import (
    generate_noise_logs,
    generate_red_herring_logs,
)


# ------------------------------------------------------------------
# Service topology β€” the dependency graph
# ------------------------------------------------------------------

SERVICE_NAMES = [
    "api_gateway", "auth", "orders", "payment",
    "cache", "database", "queue",
]

# depends_on: service β†’ list of services it calls
DEPENDENCY_GRAPH: Dict[str, List[str]] = {
    "api_gateway": ["auth", "orders", "cache"],
    "auth":        ["database"],
    "orders":      ["database", "payment", "auth"],
    "payment":     ["queue", "database"],
    "cache":       [],
    "database":    [],
    "queue":       [],
}


def _depended_by_graph() -> Dict[str, List[str]]:
    """Invert the dependency graph: service β†’ who depends on it."""
    inv: Dict[str, List[str]] = {name: [] for name in SERVICE_NAMES}
    for service, deps in DEPENDENCY_GRAPH.items():
        for dep in deps:
            inv[dep].append(service)
    return inv


DEPENDED_BY = _depended_by_graph()


class Infrastructure:
    """
    Virtual infrastructure state machine.

    Owns all services, handles cascade propagation, tracks simulation
    time, and enforces action validation.
    """

    def __init__(self) -> None:
        self.services: Dict[str, ServiceState] = {}
        self.current_minute: int = 0
        self.time_budget_minutes: int = 30
        self._actions_taken: List[Tuple[str, Optional[str]]] = []  # (action_type, target)
        self._all_logs: List[Dict[str, Any]] = []
        self._setup_services()

    def _setup_services(self) -> None:
        """Create all seven services with their dependency graphs."""
        for name in SERVICE_NAMES:
            svc = ServiceState(
                name=name,
                dependencies=list(DEPENDENCY_GRAPH.get(name, [])),
            )
            # Give each service a "good" deploy history baseline
            svc.deploy_history = [
                Deploy(
                    version=f"v1.{random.randint(0, 9)}.{random.randint(0, 9)}",
                    timestamp_minutes=-120,
                    author=random.choice(["alice", "bob", "charlie", "deploy-bot"]),
                    commit_hash=f"{random.randint(0, 0xFFFFFF):06x}",
                    description="Routine release β€” bug fixes and performance improvements",
                ),
            ]
            # Populate 30 minutes of healthy metric history
            from .metrics import generate_healthy_history
            svc.metric_history = generate_healthy_history(30, start_minute=0)
            svc._reset_metrics_healthy()
            self.services[name] = svc

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def get_service(self, name: str) -> Optional[ServiceState]:
        return self.services.get(name)

    def get_all_statuses(self) -> Dict[str, str]:
        return {name: svc.status for name, svc in self.services.items()}

    def get_alerts(self) -> List[Dict[str, Any]]:
        return evaluate_alerts(self.services, self.current_minute)

    def record_action(self, action_type: str, target: Optional[str]) -> None:
        self._actions_taken.append((action_type, target))

    def was_action_taken(self, action_type: str, target: Optional[str] = None) -> bool:
        """Check if this exact action was already taken (for repeat detection)."""
        return (action_type, target) in self._actions_taken

    def action_count(self) -> int:
        return len(self._actions_taken)

    # ------------------------------------------------------------------
    # Tick β€” advances simulation by one minute (called after every step)
    # ------------------------------------------------------------------

    def tick(self) -> None:
        """
        Advance the simulation by one minute.
        Order: propagate cascades β†’ tick all services β†’ generate noise logs.
        """
        self.current_minute += 1
        self._propagate_cascades()

        for name, svc in self.services.items():
            new_logs = svc.tick(self.current_minute)
            self._all_logs.extend(new_logs)

            # Mix in noise and red herrings
            if random.random() < 0.4:
                noise = generate_noise_logs(name, self.current_minute, count=1)
                svc.logs.extend(noise)
                self._all_logs.extend(noise)
            if random.random() < 0.15:
                herrings = generate_red_herring_logs(name, self.current_minute, count=1)
                svc.logs.extend(herrings)
                self._all_logs.extend(herrings)

    # ------------------------------------------------------------------
    # Cascade propagation
    # ------------------------------------------------------------------

    def _propagate_cascades(self) -> None:
        """
        If a service is DOWN or DEGRADED, its downstream dependents
        should accumulate dependency_degraded faults.
        If a service recovers, clear the cascaded faults.
        """
        for name, svc in self.services.items():
            dependents = DEPENDED_BY.get(name, [])
            if svc.status in ("down", "degraded") and svc.active_faults:
                # Cascade to dependents
                for dep_name in dependents:
                    dep_svc = self.services[dep_name]
                    if not dep_svc.has_fault("dependency_degraded"):
                        dep_svc.inject_fault("dependency_degraded", upstream=name)
            elif svc.status == "healthy" and not svc.active_faults:
                # Service recovered β€” clear cascade on dependents
                for dep_name in dependents:
                    dep_svc = self.services[dep_name]
                    params = dep_svc.fault_params.get("dependency_degraded", {})
                    if params.get("upstream") == name:
                        dep_svc.recover_from_dependency(self.current_minute)

    # ------------------------------------------------------------------
    # Validation β€” Layer 5: validate BEFORE mutating
    # ------------------------------------------------------------------

    def validate_action(
        self,
        action_type: str,
        target_service: Optional[str],
    ) -> Tuple[bool, str]:
        """
        Validate that an action is legal in the current state.
        Returns (is_valid, error_message).
        """
        from ..models import ActionType, TARGETED_ACTIONS

        try:
            at = ActionType(action_type)
        except ValueError:
            return False, f"Unknown action type: {action_type}"

        if at in TARGETED_ACTIONS:
            if not target_service:
                return False, f"Action {action_type} requires a target_service"
            if target_service not in self.services:
                return False, f"Unknown service: {target_service}"

        # Specific validations (action masking)
        if at == ActionType.ROLLBACK_DEPLOY:
            svc = self.services.get(target_service, None)
            if svc and len(svc.deploy_history) < 2:
                return False, f"No previous deploy to rollback to for {target_service}"

        if at == ActionType.SCALE_SERVICE:
            svc = self.services.get(target_service, None)
            if svc and svc.status == "down":
                return False, f"Cannot scale {target_service} β€” service is DOWN"

        return True, ""

    def get_valid_actions(self) -> List[str]:
        """
        Return list of valid (action_type, target) descriptions.
        Used to populate valid_actions[] in the observation.
        """
        from ..models import ActionType, TARGETED_ACTIONS
        valid = []
        for at in ActionType:
            if at in TARGETED_ACTIONS:
                for svc_name in self.services:
                    is_valid, _ = self.validate_action(at.value, svc_name)
                    if is_valid:
                        valid.append(f"{at.value}:{svc_name}")
            else:
                valid.append(at.value)
        return valid

    # ------------------------------------------------------------------
    # Service queries (observation builders)
    # ------------------------------------------------------------------

    def get_logs_for_service(
        self,
        service_name: str,
        level_filter: Optional[str] = None,
        keyword: Optional[str] = None,
        limit: int = 20,
    ) -> List[Dict[str, Any]]:
        """Query logs for a service with optional filtering."""
        svc = self.services.get(service_name)
        if not svc:
            return []

        logs = list(svc.logs)
        if level_filter:
            logs = [l for l in logs if l.get("level", "").upper() == level_filter.upper()]
        if keyword:
            kw = keyword.lower()
            logs = [l for l in logs if kw in l.get("message", "").lower()]
        return logs[-limit:]

    def get_metrics_for_service(self, service_name: str) -> List[Dict[str, float]]:
        svc = self.services.get(service_name)
        return list(svc.metric_history) if svc else []

    def get_dependencies_for_service(self, service_name: str) -> Dict[str, List[str]]:
        return {
            "depends_on": list(DEPENDENCY_GRAPH.get(service_name, [])),
            "depended_by": list(DEPENDED_BY.get(service_name, [])),
        }

    def get_deploy_history_for_service(self, service_name: str) -> List[Dict[str, Any]]:
        svc = self.services.get(service_name)
        if not svc:
            return []
        return [
            {
                "version": d.version,
                "timestamp": f"2025-01-15T{14 + d.timestamp_minutes // 60:02d}:"
                             f"{d.timestamp_minutes % 60:02d}:00Z"
                             if d.timestamp_minutes >= 0
                             else f"2025-01-15T{12 + (d.timestamp_minutes + 120) // 60:02d}:"
                                  f"{(d.timestamp_minutes + 120) % 60:02d}:00Z",
                "author": d.author,
                "commit_hash": d.commit_hash,
                "description": d.description,
            }
            for d in svc.deploy_history
        ]

    def run_health_check(self, service_name: str) -> Dict[str, Any]:
        svc = self.services.get(service_name)
        if not svc:
            return {"status": "unknown", "response_time_ms": 0}
        response_time = {
            "healthy": random.randint(5, 50),
            "degraded": random.randint(200, 2000),
            "down": 30000,  # timeout
        }.get(svc.status, 0)
        return {
            "status": svc.status,
            "response_time_ms": response_time,
            "replicas": svc.replica_count,
            "active_faults_count": len(svc.active_faults),  # agent can see SOMETHING is wrong
        }

    def all_services_healthy(self) -> bool:
        return all(svc.status == "healthy" for svc in self.services.values())