File size: 2,362 Bytes
e09df37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Verify a deployed IncidentCommander Space is healthy and stepping correctly.

Hits the live Space URL with the standard OpenEnv loop:

  GET  /health
  POST /reset
  POST /step (one read-only call, one fix)
  GET  /state

Use this immediately after ``deploy_to_space.py`` finishes building the Space.

Usage::

    python scripts/verify_space.py https://glitchghost-incident-commander.hf.space
"""

from __future__ import annotations

import json
import sys
import time
from typing import Any

import httpx


def verify(base_url: str) -> int:
    base_url = base_url.rstrip("/")
    print(f"checking {base_url}/health ...")
    deadline = time.time() + 240
    while True:
        try:
            r = httpx.get(f"{base_url}/health", timeout=10)
            if r.status_code == 200 and r.json().get("status") == "healthy":
                print("  /health OK")
                break
        except Exception as exc:  # noqa: BLE001
            print("  not ready:", exc)
        if time.time() > deadline:
            print("ERROR: /health never went healthy", file=sys.stderr)
            return 2
        time.sleep(8)

    with httpx.Client(base_url=base_url, timeout=30) as c:
        obs = c.post("/reset", json={"seed": 42, "stage": 3}).json()
        print("\n/reset alert:", obs["alert"][:120])

        step1 = c.post(
            "/step",
            json={"action": {"tool": "trace_request", "args": {"path": "/checkout"}}},
        ).json()
        print(
            "\n/step trace_request reward =", step1["reward"], " breakdown =",
            json.dumps(step1["info"]["breakdown"], indent=2),
        )

        step2 = c.post(
            "/step",
            json={"action": {"tool": "get_metrics", "args": {"service": "payments"}}},
        ).json()
        print(
            "\n/step get_metrics reward =", step2["reward"], " result =",
            step2["info"]["result"],
        )

        state = c.get("/state").json()
        print(
            "\n/state total_reward =", state["total_reward"],
            " steps =", state["steps_taken"],
        )

    print("\nSpace looks healthy and stepping correctly.")
    return 0


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python scripts/verify_space.py <base_url>", file=sys.stderr)
        raise SystemExit(2)
    raise SystemExit(verify(sys.argv[1]))