|
1 | | -"""Bounded dashboard startup checks after asynchronous systemd restart.""" |
| 1 | +"""Bounded renderer and collector readiness at the configured private bind.""" |
2 | 2 |
|
| 3 | +import ipaddress |
| 4 | +import json |
| 5 | +import math |
| 6 | +import re |
| 7 | +import subprocess |
3 | 8 | import time |
| 9 | +from pathlib import Path |
| 10 | +from urllib.parse import urlsplit |
4 | 11 |
|
5 | 12 | import requests |
6 | 13 |
|
7 | 14 |
|
8 | | -def wait(seconds: int = 60) -> dict: |
| 15 | +def endpoint(root: Path | None) -> str: |
| 16 | + if root is None: |
| 17 | + return "http://127.0.0.1:8765/" |
| 18 | + config = root / "research/remote-access/config.json" |
| 19 | + if config.is_file(): |
| 20 | + origin = json.loads(config.read_text())["dashboard"] |
| 21 | + else: |
| 22 | + command = subprocess.check_output( |
| 23 | + [ |
| 24 | + "systemctl", |
| 25 | + "--user", |
| 26 | + "show", |
| 27 | + "v100-dashboard.service", |
| 28 | + "--property=ExecStart", |
| 29 | + "--value", |
| 30 | + ], |
| 31 | + text=True, |
| 32 | + timeout=5, |
| 33 | + ) |
| 34 | + found = re.search(r"--bind(?:=|\s+)([0-9.]+)(?:\s|;|$)", command) |
| 35 | + if found is None: |
| 36 | + raise ValueError("Cannot identify the owned dashboard bind; no address guessed") |
| 37 | + origin = "http://" + found.group(1) + ":8765" |
| 38 | + parsed = urlsplit(origin) |
| 39 | + address = ipaddress.ip_address(parsed.hostname or "") |
| 40 | + if ( |
| 41 | + parsed.scheme != "http" |
| 42 | + or address.version != 4 |
| 43 | + or not address.is_private |
| 44 | + or address.is_unspecified |
| 45 | + or address.is_multicast |
| 46 | + or parsed.port != 8765 |
| 47 | + or parsed.username |
| 48 | + or parsed.password |
| 49 | + or parsed.query |
| 50 | + or parsed.fragment |
| 51 | + or parsed.path not in ("", "/") |
| 52 | + ): |
| 53 | + raise ValueError( |
| 54 | + "Dashboard readiness requires the configured private IPv4 origin on port 8765" |
| 55 | + ) |
| 56 | + return origin.rstrip("/") + "/" |
| 57 | + |
| 58 | + |
| 59 | +def wait(seconds: int = 60, *, root: Path | None = None) -> dict: |
9 | 60 | if type(seconds) is not int or not 1 <= seconds <= 90: |
10 | 61 | raise ValueError("Dashboard readiness budget must be 1..90 seconds") |
| 62 | + url = endpoint(root) |
11 | 63 | deadline = time.monotonic() + seconds |
12 | 64 | detail = "No HTTP attempt completed" |
13 | 65 | while time.monotonic() < deadline: |
14 | 66 | try: |
| 67 | + response = requests.get(url, timeout=min(2, max(0.1, deadline - time.monotonic()))) |
| 68 | + response.raise_for_status() |
| 69 | + if "mission-navigation" not in response.text or "Trade explorer" not in response.text: |
| 70 | + raise ValueError("HTTP response is not the expected host dashboard renderer") |
15 | 71 | response = requests.get( |
16 | | - "http://127.0.0.1:8765/", timeout=min(2, max(0.1, deadline - time.monotonic())) |
| 72 | + url + "api/status", timeout=min(2, max(0.1, deadline - time.monotonic())) |
17 | 73 | ) |
18 | 74 | response.raise_for_status() |
19 | | - if "mission-navigation" not in response.text or "Trade explorer" not in response.text: |
20 | | - detail = "HTTP response is not the expected host dashboard renderer" |
21 | | - else: |
22 | | - return { |
23 | | - "dashboard_ready": True, |
24 | | - "url": "http://127.0.0.1:8765/", |
25 | | - "scope": "HTTP renderer readiness only; not proof of model progress or phone routing", |
26 | | - } |
27 | | - except requests.RequestException as error: |
| 75 | + data = response.json() |
| 76 | + if not isinstance(data, dict): |
| 77 | + raise ValueError("Dashboard status must be a JSON object") |
| 78 | + collected = data.get("collected_at") |
| 79 | + if ( |
| 80 | + not isinstance(data.get("mission"), dict) |
| 81 | + or type(collected) not in (int, float) |
| 82 | + or not math.isfinite(collected) |
| 83 | + or not -5 <= time.time() - collected <= 30 |
| 84 | + or data.get("report_freshness", {}).get("source") |
| 85 | + == "previous snapshot after collector error" |
| 86 | + ): |
| 87 | + raise ValueError( |
| 88 | + "Dashboard HTTP is available but its collector has no fresh successful snapshot" |
| 89 | + ) |
| 90 | + return { |
| 91 | + "dashboard_ready": True, |
| 92 | + "collector_ready": True, |
| 93 | + "url": url, |
| 94 | + "scope": "Fresh local collector snapshot and renderer; not proof of model learning or phone routing", |
| 95 | + } |
| 96 | + except (requests.RequestException, ValueError, TypeError) as error: |
28 | 97 | detail = str(error)[:300] |
29 | 98 | time.sleep(min(1, max(0, deadline - time.monotonic()))) |
30 | 99 | raise RuntimeError("Dashboard did not become ready within the budget: " + detail) |
0 commit comments