File size: 29,984 Bytes
922c4d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
"""
IncidentOps Simulation Engine.

Tracks live service states and translates agent commands into state mutations
and rich terminal-style output. This module is the 'physics engine' of the
environment β€” it knows how services respond to restarts, config changes, etc.
"""

from __future__ import annotations

import textwrap
from typing import Any, Dict, List, Optional, Tuple

from .scenarios import Scenario, ServiceState


# ---------------------------------------------------------
#  Helpers
# ---------------------------------------------------------

def _bar(value: float, max_val: float, width: int = 20) -> str:
    """Render a text progress bar, e.g. [########----] 73%"""
    filled = int(round(width * value / max(max_val, 0.001)))
    filled = min(filled, width)
    bar = "#" * filled + "-" * (width - filled)
    pct = value / max(max_val, 0.001) * 100
    return f"[{bar}] {pct:.1f}%"


def _severity_icon(severity: str) -> str:
    icons = {
        "critical": "[CRIT]",
        "high": "[HIGH]",
        "medium": "[MED]",
        "low": "[LOW]",
    }
    return icons.get(severity, "[NONE]")


def _status_icon(status: str) -> str:
    icons = {
        "running": "[OK]",
        "degraded": "[WARN] ",
        "down": "[ERROR]",
    }
    return icons.get(status, "[?]")


# ---------------------------------------------------------
#  SimulationEngine
# ---------------------------------------------------------

class SimulationEngine:
    """
    Manages the live state of all services in a scenario and processes
    agent commands, returning human-readable terminal output.
    """

    AVAILABLE_COMMANDS = [
        ("help",                              "List all available commands"),
        ("status",                            "System-wide dashboard of all services"),
        ("alerts",                            "Show active alerts with severity and details"),
        ("logs <service>",                    "View recent log entries for a service"),
        ("metrics <service>",                 "View CPU / memory / latency / error-rate"),
        ("trace <request_id>",               "Follow a request through the service mesh"),
        ("diagnose <service>",               "Run a full diagnostic report on a service"),
        ("restart <service>",                "Restart a service (brings healthy replicas up)"),
        ("scale <service> <replicas>",       "Scale service to N replicas"),
        ("rollback <service>",               "Roll back service to the previous deployment"),
        ("failover <service>",               "Promote standby and demote primary"),
        ("config <service> <key> <value>",   "Update a live configuration value"),
        ("notify <channel> <message>",       "Post a status update (e.g. notify oncall 'msg')"),
        ("resolve",                          "Declare the incident resolved and end the episode"),
    ]

    def __init__(self, scenario: Scenario) -> None:
        self._scenario = scenario
        # Deep-copy service states so mutations don't affect the scenario definition
        self._services: Dict[str, ServiceState] = {
            k: v for k, v in scenario.services.items()
        }
        self._resolved = False
        self._actions_taken: List[str] = []
        self._notified_channels: List[str] = []
        self._alert_list = list(scenario.alerts)  # mutable copy

    # -- Accessors --------------------------------------

    @property
    def resolved(self) -> bool:
        return self._resolved

    @property
    def actions_taken(self) -> List[str]:
        return list(self._actions_taken)

    @property
    def active_alerts(self) -> list:
        return list(self._alert_list)

    @property
    def affected_services(self) -> List[str]:
        return [
            name for name, svc in self._services.items()
            if svc.status in ("degraded", "down")
        ]

    @property
    def current_severity(self) -> str:
        sevs = [a.severity for a in self._alert_list]
        for level in ("critical", "high", "medium", "low"):
            if level in sevs:
                return level
        return "none"

    # -- Command dispatcher -----------------------------

    def execute(self, command: str) -> Tuple[str, float, bool]:
        """
        Execute a command string.

        Returns:
            (output_text, incremental_reward, done)
        """
        cmd = command.strip()
        if not cmd:
            return "[WARN]  Empty command. Type 'help' to see available commands.", 0.0, False

        self._actions_taken.append(cmd)
        parts = cmd.split(None, 3)
        verb = parts[0].lower()

        dispatch = {
            "help":     self._cmd_help,
            "status":   self._cmd_status,
            "alerts":   self._cmd_alerts,
            "logs":     self._cmd_logs,
            "metrics":  self._cmd_metrics,
            "trace":    self._cmd_trace,
            "diagnose": self._cmd_diagnose,
            "restart":  self._cmd_restart,
            "scale":    self._cmd_scale,
            "rollback": self._cmd_rollback,
            "failover": self._cmd_failover,
            "config":   self._cmd_config,
            "notify":   self._cmd_notify,
            "resolve":  self._cmd_resolve,
        }

        handler = dispatch.get(verb)
        if handler is None:
            return (
                f"[ERROR]  Unknown command: '{verb}'. Type 'help' to see available commands.",
                0.0,
                False,
            )

        try:
            return handler(parts)
        except Exception as exc:
            return f"[ERROR]  Command error: {exc}", 0.0, False

    # -- Individual command handlers --------------------

    def _cmd_help(self, parts: list) -> Tuple[str, float, bool]:
        lines = ["", "+------------------------------------------------------β•—"]
        lines.append(  "|         IncidentOps Command Reference                |")
        lines.append(  "+------------------------------------------------------╝")
        lines.append("")
        for cmd, desc in self.AVAILABLE_COMMANDS:
            lines.append(f"  {cmd:<42} {desc}")
        lines.append("")
        lines.append("TIP: Start with 'status' and 'alerts' to orient yourself.")
        return "\n".join(lines), 0.0, False

    def _cmd_status(self, parts: list) -> Tuple[str, float, bool]:
        lines = [
            "",
            "+------------------------------------------------------β•—",
            "|         System Status Dashboard                      |",
           f"|  Sim Time: {self._scenario.start_time:<43}|",
            "+------------------------------------------------------╝",
            "",
            f"  Active Alerts: {len(self._alert_list)}   "
            f"Severity: {self.current_severity.upper()}   "
            f"Affected Services: {len(self.affected_services)}",
            "",
            f"  {'SERVICE':<28} {'STATUS':<12} {'ERR%':<8} {'P99ms':<8} {'CPU%':<8}",
            "  " + "-" * 68,
        ]
        for name, svc in self._services.items():
            icon = _status_icon(svc.status)
            m = svc.metrics
            lines.append(
                f"  {icon} {svc.display_name:<26} {svc.status:<12} "
                f"{m.error_rate_pct:<8.1f} {m.latency_p99_ms:<8.0f} {m.cpu_pct:<8.1f}"
            )
        lines.append("")
        return "\n".join(lines), 0.02, False

    def _cmd_alerts(self, parts: list) -> Tuple[str, float, bool]:
        if not self._alert_list:
            return "\n  [OK]  No active alerts. All systems nominal.\n", 0.02, False

        lines = [
            "",
            "+------------------------------------------------------β•—",
            "|         Active Alerts                                |",
            "+------------------------------------------------------╝",
            "",
        ]
        for alert in sorted(self._alert_list, key=lambda a: ["critical","high","medium","low"].index(a.severity)):
            icon = _severity_icon(alert.severity)
            lines.append(f"  {icon} [{alert.severity.upper()}] {alert.title}")
            lines.append(f"     Service:  {alert.service}")
            lines.append(f"     Fired at: {alert.fired_at}")
            lines.append(f"     Details:  {alert.description}")
            lines.append("")
        return "\n".join(lines), 0.03, False

    def _cmd_logs(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: logs <service>\nExample: logs api-gateway", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        # Determine investigative value
        is_root_cause = svc.name == self._scenario.root_cause_service
        is_affected = svc.status in ("degraded", "down")
        reward = 0.05 if is_root_cause else (0.03 if is_affected else 0.01)

        lines = [
            "",
            f"  Log stream β€” {svc.display_name} ({svc.name})",
            f"  Status: {svc.status}  |  Version: {svc.version}  |  Restarts: {svc.restart_count}",
            "  " + "-" * 60,
            "",
        ]
        if not svc.logs:
            lines.append("  (no log entries)")
        else:
            for entry in svc.logs:
                lines.append(f"  {entry}")
        lines.append("")
        return "\n".join(lines), reward, False

    def _cmd_metrics(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: metrics <service>\nExample: metrics payment-processor", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        is_root_cause = svc.name == self._scenario.root_cause_service
        is_affected = svc.status in ("degraded", "down")
        reward = 0.05 if is_root_cause else (0.03 if is_affected else 0.01)

        m = svc.metrics
        lines = [
            "",
            f"  Metrics β€” {svc.display_name} ({svc.name})",
            f"  Status: {_status_icon(svc.status)} {svc.status}  |  Version: {svc.version}",
            "  " + "-" * 60,
            "",
            f"  CPU Usage        {_bar(m.cpu_pct, 100)}  {m.cpu_pct:.1f}%",
            f"  Memory           {_bar(m.memory_mb, m.memory_limit_mb)}  {m.memory_mb:.0f}/{m.memory_limit_mb:.0f} MB",
            "",
            f"  Latency P50:     {m.latency_p50_ms:.0f} ms",
            f"  Latency P99:     {m.latency_p99_ms:.0f} ms",
            f"  Error Rate:      {m.error_rate_pct:.1f}%",
            f"  Throughput:      {m.rps:.0f} req/s",
            f"  Replicas:        {m.replica_count}/{m.max_replicas}",
        ]

        if svc.oom_killed:
            lines.append("")
            lines.append("  [WARN]   OOMKilled: YES β€” container was terminated due to memory limit exceeded")

        if svc.config.current:
            lines.append("")
            lines.append("  Configuration:")
            for k, v in svc.config.current.items():
                desired = svc.config.desired.get(k)
                flag = "  [WARN]  DRIFTED" if desired is not None and str(v) != str(desired) else ""
                lines.append(f"    {k}: {v}{flag}")

        if svc.dependencies:
            lines.append("")
            lines.append(f"  Dependencies: {', '.join(svc.dependencies)}")

        lines.append("")
        return "\n".join(lines), reward, False

    def _cmd_trace(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: trace <request_id>\nExample: trace req-48219", 0.0, False

        req_id = parts[1]
        valid_ids = self._scenario.request_ids
        if req_id not in valid_ids:
            return (
                f"[ERROR]  Request ID '{req_id}' not found in traces.\n"
                f"   Known request IDs: {', '.join(valid_ids[:3])} ...",
                0.0,
                False,
            )

        scenario_name = self._scenario.name
        lines = [
            "",
            f"  Distributed Trace β€” {req_id}",
            "  " + "-" * 60,
            "",
        ]

        if scenario_name == "service-restart":
            lines += [
                f"  api-gateway          β†’ RECEIVED  {req_id}  +0ms",
                f"  api-gateway          β†’ ROUTING   /api/v1/payments       +1ms",
                f"  payment-processor    β†’ TIMEOUT   upstream not responding +5000ms",
                f"  api-gateway          β†’ RESPONDED 503 Service Unavailable +5001ms",
                "",
                "  [CRIT]  Trace shows payment-processor as the failing upstream.",
            ]
        elif scenario_name == "config-drift":
            lines += [
                f"  api-gateway          β†’ RECEIVED  {req_id}  +0ms",
                f"  api-gateway          β†’ POOL WAIT awaiting connection slot +4200ms",
                f"  api-gateway          β†’ TIMEOUT   connection pool exhausted +4201ms",
                f"  api-gateway          β†’ RESPONDED 504 Gateway Timeout     +4202ms",
                "",
                "  [HIGH]  Trace shows api-gateway connection pool starvation.",
                "      Pool size config may have regressed (check 'metrics api-gateway').",
            ]
        else:  # cascading-failure
            lines += [
                f"  api-gateway          β†’ RECEIVED  {req_id}  +0ms",
                f"  api-gateway          β†’ ROUTING   /api/v1/products       +1ms",
                f"  read-service         β†’ CACHE-HIT? NO β€” cache miss         +2ms",
                f"  cache-layer          β†’ TIMEOUT   12,000ms wait           +12002ms",
                f"  read-service         β†’ DB-FETCH   falling back to replica +12003ms",
                f"  database-replica     β†’ STALE DATA lag=182s               +12050ms",
                f"  api-gateway          β†’ RESPONDED 503 upstream error       +12100ms",
                "",
                "  [CRIT]  Trace shows: cache miss β†’ cache timeout β†’ stale DB replica.",
                "      The underlying cause is further upstream β€” check database-primary.",
            ]

        return "\n".join(lines), 0.05, False

    def _cmd_diagnose(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: diagnose <service>\nExample: diagnose database-primary", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        is_root_cause = svc.name == self._scenario.root_cause_service
        reward = 0.08 if is_root_cause else 0.02

        lines = [
            "",
            f"  -- Diagnostic Report: {svc.display_name} ------------------",
            f"  Status:   {_status_icon(svc.status)} {svc.status.upper()}",
            f"  Version:  {svc.version}",
            f"  Restarts: {svc.restart_count}",
            "",
        ]

        if svc.name == "payment-processor" and svc.oom_killed:
            lines += [
                "  ROOT CAUSE INDICATORS:",
                "  βœ— OOMKilled:    Container exceeded memory limit and was killed by the OS.",
                "  βœ— Replicas:     0/4 healthy (CrashLoopBackOff)",
                "  βœ— Heap usage:   Was at 430MB / 480MB limit before OOM event",
                "",
                "  RECOMMENDATION: Restart payment-processor to restore service.",
                "  (Consider increasing heap limit in a follow-up change.)",
            ]
        elif svc.name == "api-gateway" and self._scenario.name == "config-drift":
            lines += [
                "  ANOMALY DETECTED:",
                "  βœ— Connection pool exhausted: pool_size=5 (expected: 100)",
                "  βœ— Deployment v3.1.5 occurred 8 min ago β€” config may have regressed",
                "  βœ“ CPU and memory are within normal bounds",
                "  βœ“ No code changes β€” config-only regression suspected",
                "",
                "  RECOMMENDATION: Rollback api-gateway OR fix config:",
                "    config api-gateway pool_size 100",
            ]
        elif svc.name == "database-primary" and self._scenario.name == "cascading-failure":
            lines += [
                "  ROOT CAUSE INDICATORS:",
                "  βœ— Disk I/O:      iowait=82% β€” disk throughput severely degraded",
                "  βœ— WAL writes:    Checkpoint taking 45s (normal: 2s)",
                "  βœ— Replication:   Lag building (182s) β€” replica serving stale data",
                "  βœ“ CPU/Memory:    Within normal limits (disk I/O is the bottleneck)",
                "  βœ“ Standby:       database-standby is healthy and ready for promotion",
                "",
                "  RECOMMENDATION:",
                "  1. failover database-primary  (promote standby, reduce replication lag)",
                "  2. restart cache-layer        (flush stale cache, recover from stampede)",
            ]
        else:
            # Generic degraded service
            m = svc.metrics
            lines += [
                f"  Metrics Summary:",
                f"  {'cpu_pct':<20} {m.cpu_pct:.1f}%",
                f"  {'memory_mb':<20} {m.memory_mb:.0f}/{m.memory_limit_mb:.0f} MB",
                f"  {'error_rate_pct':<20} {m.error_rate_pct:.1f}%",
                f"  {'latency_p99_ms':<20} {m.latency_p99_ms:.0f} ms",
                "",
                "  Config values:",
            ]
            for k, v in svc.config.current.items():
                desired = svc.config.desired.get(k)
                flag = "  ← DRIFTED (expected: {})".format(desired) if desired is not None and str(v) != str(desired) else ""
                lines.append(f"    {k}: {v}{flag}")

            lines.append("")
            if svc.status == "running":
                lines.append("  [OK]  No anomalies detected. This service appears healthy.")
            else:
                lines.append("  [WARN]   Service is degraded but no direct root cause identified here.")
                lines.append("       Check its dependencies.")

        lines.append("")
        return "\n".join(lines), reward, False

    def _cmd_restart(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: restart <service>\nExample: restart payment-processor", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        scenario = self._scenario
        is_correct = service_name in [s.lower() for s in scenario.resolution_services]

        if svc.status == "running":
            # Restarting a healthy service is penalised
            return (
                f"  [WARN]   {svc.display_name} is already healthy (status: running).\n"
                f"       Restarting a healthy service may cause unnecessary downtime.\n"
                f"       Restart aborted. Use 'status' to review which services need attention.\n",
                -0.05,
                False,
            )

        # Perform restart
        svc.restart_count += 1
        was_oom = svc.oom_killed

        if is_correct and "restart" in " ".join(scenario.resolution_actions):
            # Resolving restart
            svc.status = "running"
            svc.oom_killed = False
            svc.metrics.error_rate_pct = 0.1
            svc.metrics.latency_p50_ms = 14
            svc.metrics.latency_p99_ms = 52
            svc.metrics.rps = svc.metrics.rps if svc.metrics.rps > 0 else 200
            svc.metrics.replica_count = max(svc.metrics.replica_count, 1)
            # Clear alerts for this service
            self._alert_list = [a for a in self._alert_list if a.service != service_name]
            suffix = "\n  [OK]  Service successfully restarted. Replicas healthy. Alerts cleared."
            reward = 0.35
        else:
            # Partial restart (wrong service or not the fix)
            svc.metrics.restart_count = svc.restart_count
            suffix = f"\n  [WARN]   {svc.display_name} restarted but root cause may still be present."
            reward = -0.03

        lines = [
            "",
            f"  [RESTART]  Restarting {svc.display_name} ...",
            f"      Sending SIGTERM to {svc.metrics.replica_count} pod(s) ...",
            "      Waiting for graceful shutdown (30s timeout) ...",
            "      Pulling image ... done",
            "      Starting new pod(s) ...",
            "      Health checks passing [OK]",
            suffix,
            "",
        ]
        return "\n".join(lines), reward, False

    def _cmd_scale(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 3:
            return "Usage: scale <service> <replicas>\nExample: scale web-server 5", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        try:
            target = int(parts[2])
        except ValueError:
            return f"[ERROR]  '{parts[2]}' is not a valid replica count.", 0.0, False

        if target > svc.metrics.max_replicas:
            return (
                f"  [ERROR]  Cannot scale beyond max replicas ({svc.metrics.max_replicas}).\n"
                f"       Requested: {target}. Use a value ≀ {svc.metrics.max_replicas}.",
                0.0,
                False,
            )

        old = svc.metrics.replica_count
        svc.metrics.replica_count = target
        return (
            f"\n  [OK]  {svc.display_name} scaled from {old} β†’ {target} replicas.\n",
            0.02,
            False,
        )

    def _cmd_rollback(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: rollback <service>\nExample: rollback api-gateway", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        scenario = self._scenario
        is_correct = (
            service_name in [s.lower() for s in scenario.resolution_services]
            and any("rollback" in act for act in scenario.resolution_actions)
        )

        if svc.status == "running" and not is_correct:
            return (
                f"  [WARN]   {svc.display_name} appears healthy. Rollback unnecessary.\n"
                f"       Check 'metrics {service_name}' before rolling back.\n",
                -0.02,
                False,
            )

        if is_correct:
            # Apply the desired config values
            svc.config.current = dict(svc.config.desired)
            svc.status = "running"
            svc.metrics.error_rate_pct = 0.05
            svc.metrics.latency_p50_ms = 10
            svc.metrics.latency_p99_ms = 42
            self._alert_list = [a for a in self._alert_list if a.service != service_name]
            suffix = "  [OK]  Rollback successful. Config restored to previous known-good values."
            reward = 0.35
        else:
            suffix = f"  [WARN]   {svc.display_name} rolled back but underlying problem may persist."
            reward = -0.02

        lines = [
            "",
            f"  [ROLLBACK]   Rolling back {svc.display_name} ...",
            f"      Previous version restore initiated ...",
            "      Config values reverted to pre-deployment snapshot ...",
            "      Health checks passing [OK]",
            "",
            suffix,
            "",
        ]
        return "\n".join(lines), reward, False

    def _cmd_failover(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 2:
            return "Usage: failover <service>\nExample: failover database-primary", 0.0, False

        service_name = parts[1].lower()
        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        scenario = self._scenario
        is_correct = (
            service_name in [s.lower() for s in scenario.resolution_services]
            and any("failover" in act for act in scenario.resolution_actions)
        )

        if is_correct:
            svc.status = "degraded"          # old primary now demoted
            svc.metrics.iowait = 0            # type: ignore[attr-defined] β€” illustrative
            # Promote standby if it exists
            standby_key = service_name.replace("primary", "standby")
            if standby_key in self._services:
                self._services[standby_key].status = "running"
            # Update replica too
            replica_key = service_name.replace("primary", "replica")
            if replica_key in self._services:
                self._services[replica_key].metrics.latency_p99_ms = 25
                self._services[replica_key].metrics.error_rate_pct = 0.0

            self._alert_list = [
                a for a in self._alert_list
                if not (a.service == service_name and "replication" in a.title.lower())
            ]
            suffix = (
                "  [OK]  Failover complete. Standby promoted to primary.\n"
                "      Replication lag is now resolving. Cache should recover shortly."
            )
            reward = 0.30
        else:
            suffix = (
                f"  [WARN]   Failover for {svc.display_name} completed but may not be necessary.\n"
                f"       Verify with 'diagnose {service_name}'."
            )
            reward = -0.05

        lines = [
            "",
            f"  [FAILOVER]  Initiating failover for {svc.display_name} ...",
            "      Verifying standby readiness ...",
            "      Promoting standby to primary ...",
            "      Updating DNS / connection strings ...",
            "      Draining connections from old primary ...",
            "      Failover complete [OK]",
            "",
            suffix,
            "",
        ]
        return "\n".join(lines), reward, False

    def _cmd_config(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 4:
            return (
                "Usage: config <service> <key> <value>\n"
                "Example: config api-gateway pool_size 100",
                0.0,
                False,
            )

        service_name = parts[1].lower()
        key = parts[2]
        value = parts[3]

        svc = self._find_service(service_name)
        if svc is None:
            return self._unknown_service(service_name), 0.0, False

        scenario = self._scenario
        desired = svc.config.desired.get(key)
        is_correct_service = service_name in [s.lower() for s in scenario.resolution_services]
        is_correct_value = desired is not None and str(value) == str(desired)

        old_value = svc.config.current.get(key, "N/A")
        svc.config.current[key] = value

        if is_correct_service and is_correct_value:
            svc.status = "running"
            svc.metrics.error_rate_pct = 0.05
            svc.metrics.latency_p50_ms = 11
            svc.metrics.latency_p99_ms = 44
            self._alert_list = [a for a in self._alert_list if a.service != service_name]
            suffix = f"  [OK]  Config applied and verified. Service recovering."
            reward = 0.35
        else:
            suffix = f"  [WARN]   Config updated. Monitor service to confirm stability."
            reward = 0.01

        lines = [
            "",
            f"  [CONFIG]   Updating config on {svc.display_name} ...",
            f"       {key}: {old_value} β†’ {value}",
            "      Hot-reload applied (no restart required) [OK]",
            "",
            suffix,
            "",
        ]
        return "\n".join(lines), reward, False

    def _cmd_notify(self, parts: list) -> Tuple[str, float, bool]:
        if len(parts) < 3:
            return (
                "Usage: notify <channel> <message>\n"
                "Example: notify oncall 'investigating high error rate on payment-processor'",
                0.0,
                False,
            )
        channel = parts[1]
        message = " ".join(parts[2:]).strip("'\"")
        self._notified_channels.append(channel)
        return (
            f"\n  [NOTIFY]  Notification sent to #{channel}:\n"
            f"       \"{message}\"\n",
            0.02,
            False,
        )

    def _cmd_resolve(self, parts: list) -> Tuple[str, float, bool]:
        self._resolved = True
        lines = [
            "",
            "+------------------------------------------------------β•—",
            "|         Incident Resolved                            |",
            "+------------------------------------------------------╝",
            "",
            "  The incident has been marked as resolved.",
            "  Final score will be calculated based on your investigation",
            "  and remediation quality.",
            "",
        ]
        return "\n".join(lines), 0.0, True

    # -- Utilities --------------------------------------

    def _find_service(self, name: str) -> Optional[ServiceState]:
        # Exact match first
        if name in self._services:
            return self._services[name]
        # Prefix / partial match
        for key, svc in self._services.items():
            if key.startswith(name) or name in key:
                return svc
        return None

    def _unknown_service(self, name: str) -> str:
        known = ", ".join(sorted(self._services.keys()))
        return (
            f"[ERROR]  Service '{name}' not found.\n"
            f"   Known services: {known}"
        )