""" monitor.py — orchestration watchdog, run inside the HF orchestrator sandbox. Phase 1: 1 hour after the seed-0 wave launch (WAVE1_LAUNCH_ISO), check health of the seed-0 Modal app (no 'failed rc' / IndexError / Traceback in the streaming client log AND the log still growing) — if healthy, auto-launch seeds 1-4 (detached). Otherwise leave a marker and wait for manual review. Phase 2 (budget guard, user condition): every 15 min compute an UPPER-BOUND cost estimate at Modal list rates (10 concurrent T4 + 2 CPU containers + 4 GiB mem). If it exceeds $60, `modal app stop` every stbr-repro app and halt. List rates (Nov 2026): T4 $0.59/h, CPU $0.047/physical-core/h (4 vCPU = 2 cores), memory $0.008/GiB/h. Builds and queueing are unbilled; estimate is deliberately generous (upper bound). """ import os import subprocess import time WAVE1_LAUNCH_ISO = "2026-12-02T20:25:00Z" # seed-0 wave launch time (UTC) T4_RATE = 0.59 # $/h list rate CORE_RATE = 0.047 # $/physical-core/h list rate (4 vCPU = 2 cores) MEM_RATE = 0.008 # $/GiB/h list rate CONCURRENCY = 10 # max_containers on run-training-t4 N_CPU = 2 # CPU-variant containers (safety margin; none planned) PRIOR = 0.75 # approx spend before full runs (list-rate estimate) BUDGET_STOP = 60.0 def log(msg): with open("/workspace/monitor.log", "a") as f: f.write("[" + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) + "] " + msg + "\n") def sh(cmd, timeout=120): return subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=timeout) def utc_ts(iso): import calendar return calendar.timegm(time.strptime(iso, "%Y-%m-%dT%H:%M:%SZ")) t0 = time.time() launch = utc_ts(WAVE1_LAUNCH_ISO) remaining = max(60, 3600 - (time.time() - launch)) log("monitor started (PRIOR=$%.2f, BUDGET_STOP=$%.0f); health check in %.0f min" % (PRIOR, BUDGET_STOP, remaining / 60)) # --- Phase 1: health check the seed-0 wave, then launch seeds 1-4 --- time.sleep(remaining) app1_log = "/workspace/full_seed0.log" try: txt = open(app1_log, errors="replace").read() s1 = len(txt) fails = txt.count("failed rc") + txt.count("IndexError") + txt.count("Traceback (") except FileNotFoundError: s1, fails = -1, -1 time.sleep(180) try: s2 = os.path.getsize(app1_log) except FileNotFoundError: s2 = -2 if fails == 0 and s2 > s1 >= 0: log("healthy (fails=0, client log %d->%d bytes) -> launching seeds 1-4" % (s1, s2)) r = sh("cd /workspace && modal run --detach modal_app.py --stage full --seeds 1,2,3,4" " > /workspace/full_seeds14.log 2>&1") log("wave2 launch rc=%s (see full_seeds14.log)" % r.returncode) else: log("NOT healthy (fails=%s, client log %s->%s bytes) -> wave 2 NOT launched;" " leaving for manual review" % (fails, s1, s2)) # --- Phase 2: budget guard --- while True: time.sleep(900) hrs = (time.time() - t0) / 3600.0 upper = PRIOR + hrs * (CONCURRENCY * T4_RATE + N_CPU * 2 * CORE_RATE + 4 * MEM_RATE) log("guard: elapsed=%.2fh upper_bound_cost=$%.2f" % (hrs, upper)) if upper > BUDGET_STOP: log("BUDGET_STOP: upper bound $%.2f > $%.0f -> stopping stbr-repro apps" % (upper, BUDGET_STOP)) out = sh("modal app list 2>/dev/null | grep stbr-rep", timeout=60).stdout for line in out.splitlines(): tok = line.split() if tok and tok[0].startswith("ap-"): r = sh("modal app stop %s" % tok[0], timeout=120) log("stop %s rc=%s" % (tok[0], r.returncode)) log("BUDGET_STOP complete; monitor halting") break log("monitor exit")