Spaces:
Runtime error

File size: 16,083 Bytes
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
 
 
 
 
e85cc77
 
 
 
 
 
 
 
 
 
 
 
49eb382
 
e85cc77
 
49eb382
 
 
 
 
 
 
 
 
 
 
 
e85cc77
49eb382
 
e85cc77
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
49eb382
 
e85cc77
49eb382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e85cc77
 
 
 
49eb382
e85cc77
 
 
 
 
 
49eb382
 
e85cc77
49eb382
e85cc77
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
 
 
e85cc77
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
49eb382
 
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
49eb382
 
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
 
 
e85cc77
49eb382
e85cc77
 
 
49eb382
 
 
 
 
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
49eb382
 
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
49eb382
 
 
 
 
 
 
e85cc77
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
49eb382
 
 
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eb382
e85cc77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
ACRS CLI β€” Autonomous Cloud Recovery System Command Line Interface

A developer tool to control and monitor the ACRS agent via terminal.
Interacts with the ACRS FastAPI backend.
"""

import argparse
import sys
import os
import json
import requests
import colorama
from colorama import Fore, Style
from shared_formatter import (
    StepData, format_step, format_episode_summary,
    C_CYAN, C_TEXT, C_MUTED, C_GREEN, C_RED, C_GOLD, R,
    _format_services,
)

colorama.init(autoreset=True)

DEFAULT_HOST = "http://localhost:8000"

VIEW_MODE = "clean"  # set globally once args are parsed


# ── BANNER ────────────────────────────────────────────────────────────────────

def print_welcome_banner():
    C_BLUE = '\033[38;2;0;130;255m'

    cwd     = os.getcwd()
    cwd_str = cwd if len(cwd) <= 14 else "..." + cwd[-11:]

    print(f"\n{C_CYAN}╔══ ACRS CLI {C_TEXT}v1.0.0{C_CYAN} {'═'*58}β•—\033[0m")
    print(f"{C_CYAN}β•‘\033[0m                                                                             {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_BLUE}      β–„β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–„        {C_CYAN}SYSTEM STATUS: {C_GREEN}ONLINE                                   {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_BLUE}      β–ˆβ–ˆβ–„β–„β–„β–ˆβ–ˆ        {C_MUTED}{'-'*52}    {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_BLUE}      β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ        {C_TEXT}Quick Start:                                            {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_BLUE}      β–ˆβ–ˆβ–„β–„β–„β–ˆβ–ˆ        {C_TEXT}β€’ Run {C_CYAN}`acrs reset`{C_TEXT} to init a chaos scenario             {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_BLUE}      β–€β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€        {C_TEXT}β€’ Run {C_CYAN}`acrs run`{C_TEXT} to start the autonomous agent          {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘\033[0m                                                                             {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_CYAN}   Autonomous SRE    {C_TEXT}Recent Activity:                                        {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘{C_MUTED}   {cwd_str:<14}    {C_MUTED}No recent incidents                                     {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•‘\033[0m                                                                             {C_CYAN}β•‘\033[0m")
    print(f"{C_CYAN}β•š{'═'*77}╝\033[0m\n")


# ── PRINT HELPERS ─────────────────────────────────────────────────────────────

def print_error(msg):
    print(f"{C_RED}[ERROR] {msg}{R}")

def print_success(msg):
    print(f"{C_GREEN}[OK] {msg}{R}")

def print_warning(msg):
    print(f"{C_GOLD}[WARN] {msg}{R}")


def _api_response_to_step_data(data: dict, step_num: int) -> StepData:
    """
    Convert a raw API step response dict into a StepData object
    so format_step() can render it identically to inference output.
    """
    obs = data.get("observation", {})
    state_summary = None
    if obs:
        state_summary = {
            "services": obs.get("services", {}),
            "latency":  obs.get("latency"),
        }
    elif "state_summary" in data:
        state_summary = data["state_summary"]
    elif "state" in data:
        state_summary = data["state"]

    action = data.get("action") or data.get("tool", "unknown")

    return StepData(
        step          = data.get("step", step_num),
        state_summary = state_summary,
        action        = action,
        params        = data.get("params", {}),
        result        = data.get("result", ""),
        reward        = data.get("reward", 0.0),
        total_reward  = data.get("total_reward", 0.0),
        done          = data.get("done", False),
        hypothesis    = data.get("hypothesis", ""),
        why           = data.get("why", ""),
        source        = data.get("source", ""),
    )


# ── FINAL SUMMARY ─────────────────────────────────────────────────────────────

def print_final_summary(data: dict) -> None:
    """Print the clean final summary block."""
    status   = data.get('status', 'UNKNOWN')
    resolved = status == "RESOLVED"
    color    = C_GREEN if resolved else C_RED
    icon     = "βœ”" if resolved else "✘"

    _sep = lambda c='─', w=52: f"{C_MUTED}{c * w}{R}"

    print()
    print(_sep('═'))
    print(f"{C_CYAN}  FINAL SUMMARY{R}")
    print(_sep('═'))
    print(f"  {color}{icon} Scenario:{R}       {data.get('scenario', '?')}")
    print(f"  {color}{icon} Status:{R}         {color}{status}{R}")
    print(f"  {color}{icon} Steps Taken:{R}    {data.get('steps_taken', '?')}")
    print(f"  {color}{icon} Signals Used:{R}   {data.get('signals_used', '?')}")

    fixes = data.get('fixes_applied', [])
    if fixes:
        chain = f"{C_MUTED} β†’ {R}".join(f"{C_TEXT}{f}{R}" for f in fixes)
        print(f"  {color}{icon} Fix Chain:{R}      {chain}")

    print(f"  {color}{icon} Final Latency:{R}  {data.get('final_latency', '?')}ms")
    print(f"  {color}{icon} Total Reward:{R}   {C_GREEN if resolved else C_RED}{data.get('total_reward', 0):+.2f}{R}")

    if not resolved:
        print()
        print(f"  {C_RED}Reason:{R}  {data.get('failure_reason', 'Unknown')}")
        print(f"  {C_GOLD}Tip:{R}     {data.get('suggested_improvement', '')}")

    print(_sep('═'))
    print()


# ── COMMANDS ──────────────────────────────────────────────────────────────────

def cmd_reset(args):
    print(f"Resetting environment at {args.host}...")
    try:
        res = requests.post(f"{args.host}/api/agent/reset", timeout=10)
        res.raise_for_status()
        data = res.json()
        print_success("Environment reset successfully.")
        print(f"  {C_CYAN}Scenario:{R} {data.get('scenario')}")
        print(f"  {C_CYAN}Phase:{R}    {data.get('phase')}")
        obs     = data.get('observation', {})
        svc_str = _format_services(obs.get('services', {}))
        print(f"  {C_CYAN}State:{R}    {svc_str} | Latency: {obs.get('latency')}ms")
    except Exception as e:
        print_error(f"Failed to reset environment: {e}")


def cmd_state(args):
    try:
        res = requests.get(f"{args.host}/api/state", timeout=5)
        res.raise_for_status()
        data = res.json()

        if not data.get("initialized"):
            print_warning("Environment not initialized. Run 'reset' first.")
            return

        _sep = lambda: print(f"{C_MUTED}{'─' * 52}{R}")

        _sep()
        print(f"{C_CYAN}CURRENT STATE{R}")
        print(f"  Scenario: {data.get('scenario')}")

        phase   = data.get('phase', '')
        p_color = C_GREEN if phase == 'NORMAL' else C_GOLD
        print(f"  Phase:    {p_color}{phase}{R}")
        print(f"  Latency:  {data.get('latency')}ms")
        print(f"  Reward:   {data.get('total_reward')}")
        print()
        print(f"{C_CYAN}SERVICES{R}")

        for name, info in data.get('services', {}).items():
            status = info.get('status', 'unknown').upper()
            color  = C_GREEN if status == 'RUNNING' else C_RED if status in ('DOWN', 'OVERLOADED') else C_GOLD
            print(f"  {name:<18} {color}{status:<12}{R} CPU: {info.get('cpu')}%  MEM: {info.get('memory')}%")

        _sep()
    except Exception as e:
        print_error(f"Failed to fetch state: {e}")


def cmd_logs(args):
    try:
        res = requests.get(f"{args.host}/api/logs", timeout=5)
        res.raise_for_status()
        logs = res.json().get("logs", [])

        print(f"{C_MUTED}{'─' * 52}{R}")
        print(f"{C_CYAN}SYSTEM LOGS{R}")

        if not logs:
            print(f"  {C_MUTED}No logs available.{R}")
        else:
            for log in logs:
                if "CRITICAL" in log or "[CHAOS]" in log:
                    print(f"  {C_RED}{log}{R}")
                elif "ERROR" in log:
                    print(f"  {C_RED}{log}{R}")
                elif "WARN" in log:
                    print(f"  {C_GOLD}{log}{R}")
                else:
                    print(f"  {C_MUTED}{log}{R}")

        print(f"{C_MUTED}{'─' * 52}{R}")
    except Exception as e:
        print_error(f"Failed to fetch logs: {e}")


def cmd_step(args):
    print(f"{C_CYAN}── Manual Step Mode ──────────────────────────────────{R}")
    tool = input("Tool name (e.g. get_db_metrics, restart_service): ").strip()
    if not tool:
        print_warning("No tool entered. Aborting.")
        return

    params = {}
    if tool in ["restart_service", "scale_service"]:
        service = input("Service name (api-service, db-service, cache-service): ").strip()
        if service:
            params["service"] = service

    action_type = "tool_call" if (tool.startswith("get_") or tool == "clear_db_connections") else "system_action"
    payload     = {"action_type": action_type, "tool": tool, "params": params}

    try:
        res = requests.post(f"{args.host}/api/agent/step", json=payload, timeout=10)
        res.raise_for_status()
        data = res.json()

        step_data = _api_response_to_step_data(data, step_num=1)
        format_step(step_data, mode=args.view)

        if data.get("done"):
            print_success("Incident resolved!")

    except Exception as e:
        print_error(f"Step failed: {e}")


def cmd_explain(args):
    try:
        res = requests.get(f"{args.host}/api/state", timeout=5)
        res.raise_for_status()
        data = res.json()

        if not data.get("initialized"):
            print_warning("No active or completed incident to explain.")
            return

        _sep = lambda c='─', w=52: print(f"{C_MUTED}{c * w}{R}")

        _sep('═')
        print(f"{C_CYAN}  POST-MORTEM REPORT{R}")
        _sep('═')

        print(f"\n{C_CYAN}Root Cause:{R}    {data.get('scenario', '?')}")

        req_fixes  = data.get('required_fixes', [])
        diagnosis  = []
        if "scale_db"          in req_fixes: diagnosis.append("DB connections saturated under load")
        if "restart_api"       in req_fixes: diagnosis.append("API thread pool exhausted")
        if "flush_cache"       in req_fixes: diagnosis.append("Cache locks not released")
        if "clear_connections" in req_fixes: diagnosis.append("DB connection pool exhausted")
        if not diagnosis:
            diagnosis.append("System experiencing unexpected load or failure")

        print(f"\n{C_CYAN}Signals Used:{R}")
        for line in diagnosis:
            print(f"  {C_MUTED}β€’ {line}{R}")

        print(f"\n{C_CYAN}Fix Strategy:{R}")
        for i, fix in enumerate(req_fixes, 1):
            print(f"  {i}. {fix.replace('_', ' ').title()}")

        outcome = "System recovered" if data.get('done') else "Incident unresolved"
        o_color = C_GREEN if data.get('done') else C_RED
        print(f"\n{C_CYAN}Outcome:{R}")
        print(f"  {o_color}{outcome} in {data.get('step', 0)} steps.{R}")

        _sep('═')

    except Exception as e:
        print_error(f"Failed to fetch state for explanation: {e}")


def cmd_run(args):
    view = args.view
    print(f"Starting autonomous agent at {args.host}...\n")
    import time

    step_counter = 0

    try:
        with requests.get(f"{args.host}/api/agent/run", stream=True, timeout=60) as res:
            res.raise_for_status()
            for line in res.iter_lines():
                if not line:
                    continue
                decoded = line.decode('utf-8')
                if not decoded.startswith("data: "):
                    continue

                try:
                    data = json.loads(decoded[6:])
                except json.JSONDecodeError:
                    continue

                event_type = data.get("type")

                if event_type == "init":
                    if view != "silent":
                        print(f"{C_CYAN}>>> Scenario: {C_TEXT}{data.get('scenario')}{R}  {C_MUTED}({data.get('phase')}){R}")
                        print(f"{C_MUTED}Agent analyzing system state...{R}\n")
                    step_counter = 0
                    time.sleep(0.3)

                elif event_type == "summary":
                    summary = {
                        "scenario":              data.get("scenario", "?"),
                        "status":                data.get("status", "?"),
                        "steps_taken":           data.get("steps_taken"),
                        "signals_used":          data.get("signals_used", "?"),
                        "fixes_applied":         data.get("fixes_applied", []),
                        "final_latency":         data.get("final_latency"),
                        "total_reward":          data.get("total_reward", 0),
                        "failure_reason":        data.get("failure_reason"),
                        "suggested_improvement": data.get("suggested_improvement"),
                    }
                    print_final_summary(summary)

                elif event_type == "done":
                    break

                elif event_type == "error":
                    print_error(f"Agent Error: {data.get('message')}")
                    break

                else:
                    step_counter += 1
                    step_data = _api_response_to_step_data(data, step_num=step_counter)
                    format_step(step_data, mode=view)
                    time.sleep(0.2)

    except requests.exceptions.ReadTimeout:
        print_error("Request timed out waiting for agent.")
    except Exception as e:
        print_error(f"Agent run failed: {e}")


# ── MAIN ──────────────────────────────────────────────────────────────────────

def main():
    global VIEW_MODE

    if len(sys.argv) == 1:
        print_welcome_banner()
        print(f"  {C_TEXT}Available commands:{R} run, step, reset, state, explain, logs")
        print(f"  {C_TEXT}Use {C_CYAN}python {os.path.basename(sys.argv[0])} -h{C_TEXT} for more info.{R}\n")
        sys.exit(0)

    parser = argparse.ArgumentParser(description="ACRS Command Line Interface")
    parser.add_argument("--host", default=DEFAULT_HOST, help="FastAPI server URL")
    parser.add_argument(
        "--view",
        default="clean",
        choices=["clean", "debug", "silent"],
        help="Output verbosity: clean (default), debug (full LLM trace), silent (summary only)",
    )

    subparsers = parser.add_subparsers(dest="command", required=True)
    subparsers.add_parser("run",     help="Run full autonomous agent")
    subparsers.add_parser("step",    help="Run step-by-step mode")
    subparsers.add_parser("reset",   help="Reset environment")
    subparsers.add_parser("state",   help="Show current system state")
    subparsers.add_parser("explain", help="Post-mortem explanation")
    subparsers.add_parser("logs",    help="Show recent logs")

    args      = parser.parse_args()
    VIEW_MODE = args.view

    try:
        requests.get(f"{args.host}/api/health", timeout=3)
    except requests.exceptions.RequestException:
        print_error(f"Could not connect to ACRS server at {args.host}.")
        print(f"  Ensure it is running: {C_GOLD}uvicorn auto_sre_env.server:app{R}")
        sys.exit(1)

    dispatch = {
        "run":     cmd_run,
        "step":    cmd_step,
        "reset":   cmd_reset,
        "state":   cmd_state,
        "explain": cmd_explain,
        "logs":    cmd_logs,
    }
    dispatch[args.command](args)


if __name__ == "__main__":
    main()