"""BOOTSTRAP - the only thing that has to be run after a relaunch. The container root is an OVERLAY filesystem. /home/marimo does not survive. Hugging Face is therefore the real disk, and this script is the recovery path: 1. pull the working state back from the Hub 2. restore the workplan and its done-markers 3. restart every service (sync, cpu lane, workplan manager) 4. keep running: if it is killed, the next bootstrap resumes from HF again Usage, on a fresh box with only a token: export HF_TOKEN_BOOT= python bootstrap.py If even this file is gone: huggingface-cli download dragonlimited/dragon-state ops/bootstrap.py --repo-type model python bootstrap.py """ import json, os, pathlib, subprocess, sys, time REPO = "dragonlimited/dragon-state" W = pathlib.Path("/home/marimo/dragon_fsds") TOKEN = os.environ.get("HF_TOKEN_BOOT") or os.environ.get("HF_TOKEN") if not TOKEN: sys.exit("set HF_TOKEN_BOOT first") import huggingface_hub as hh api = hh.HfApi(token=TOKEN) W.mkdir(parents=True, exist_ok=True) def log(s): line = time.strftime("%H:%M:%S") + " [boot] " + s print(line, flush=True) with open(W/"bootstrap.log", "a") as f: f.write(line + "\n") log("=" * 60) log("CRITICAL PATH: restoring from Hugging Face") log("=" * 60) # ---- 1. restore state ---------------------------------------------------- def pull(prefix, dest_root): got = 0 try: files = [f.rfilename for f in (api.repo_info(REPO, repo_type="model").siblings or [])] except Exception as e: log(f"repo listing failed: {type(e).__name__}"); return 0 for f in files: if not f.startswith(prefix): continue rel = f[len(prefix):].lstrip("/") if not rel: continue dest = dest_root / rel dest.parent.mkdir(parents=True, exist_ok=True) if dest.exists() and dest.stat().st_size > 0: continue try: p = hh.hf_hub_download(REPO, f, repo_type="model", token=TOKEN, cache_dir="/home/marimo/hf_cache") dest.write_bytes(pathlib.Path(p).read_bytes()) got += 1 except Exception as e: log(f" miss {f}: {type(e).__name__}") return got n = pull("state/", W) log(f"restored {n} files into {W}") n2 = pull("workplan/", W) log(f"restored {n2} workplan files") # ---- 2. if the scripts themselves are missing, pull the ops copy --------- needed = ["run_workplan.py", "cpu_lane.py", "bench_agent.py", "train_agent_metric.py", "rlvr_v2.py", "index_corpus.py", "sync_state.py", "workplan.json"] missing = [x for x in needed if not (W/x).exists()] if missing: log(f"missing locally, pulling ops copies: {missing}") for f in missing: for cand in (f"ops/{f}", f"workplan/{f}", f"state/{f}"): try: p = hh.hf_hub_download(REPO, cand, repo_type="model", token=TOKEN, cache_dir="/home/marimo/hf_cache") (W/f).write_bytes(pathlib.Path(p).read_bytes()) log(f" restored {f} from {cand}"); break except Exception: continue # ---- 3. restart the services -------------------------------------------- log("starting services") env = dict(os.environ, HF_TOKEN_BOOT=TOKEN) def start(script, logname, **extra): p = W/script if not p.exists(): log(f" cannot start {script}: absent"); return None e = dict(env, **extra) pr = subprocess.Popen(["python", str(p)], stdout=open(W/logname, "a"), stderr=subprocess.STDOUT, env=e, start_new_session=True) log(f" {script} -> pid {pr.pid}") return pr # keep state flowing to the Hub, frequently: this is what makes resume possible start("sync_state.py", "sync_watch.log") if not subprocess.run( ["bash","-lc","pgrep -f sync_state.py >/dev/null"]).returncode == 0 else log(" sync already running") start("cpu_lane.py", "cpu_lane_log.txt") start("run_workplan.py", "workplan_log.txt", WP_GPU_SLOTS="3") # ---- 4. supervise ourselves -------------------------------------------- log("entering supervision loop") fails = 0 while True: time.sleep(60) alive = subprocess.run(["bash","-lc","pgrep -f 'bootstrap.py' >/dev/null; echo $?"], capture_output=True, text=True).stdout.strip() for script, logname in (("sync_state.py","sync_watch.log"), ("cpu_lane.py","cpu_lane_log.txt"), ("run_workplan.py","workplan_log.txt")): r = subprocess.run(["bash","-lc",f"pgrep -f {script} >/dev/null"], capture_output=True) if r.returncode != 0: fails += 1 log(f"{script} is down (failure {fails}) - restarting") start(script, logname, **({"WP_GPU_SLOTS":"3"} if "workplan" in script else {}))