# AutoVideo cloud worker (runs inside a private Kaggle notebook): one ComfyUI job per session.
# The cell before this one sets JOB_BUNDLE_SHA256, JOB_BUNDLE_B85 and JOB_CHUNK_SLUGS: a zip with job.json and the job's
# input files (pictures, voices, optional source patches); its first part is in that cell (base85), the further parts
# are the output of the listed input notebooks.
# Official ComfyUI at a pinned commit, models from the pinned Comfy-Org/MiniMax-H3 revision only; no tunnel, no token,
# no custom nodes. The outcome is always written to /kaggle/working/autovideo/receipt.json (status ok / fail), so the
# caller reads the receipt instead of guessing from the session status.
from __future__ import annotations
import base64, hashlib, importlib.metadata, io, json, os, re, shutil, signal, socket, subprocess, sys, threading, time, zipfile
import urllib.error, urllib.request
from pathlib import Path

ROOT = Path("/kaggle/temp/autovideo")
OUT = Path("/kaggle/working/autovideo")
COMFY = ROOT / "ComfyUI"
COMFY_GIT = "https://github.com/Comfy-Org/ComfyUI.git"
HF_BASE = "https://huggingface.co/Comfy-Org/MiniMax-H3/resolve/"
HOST, PORT = "127.0.0.1", 8189
BASE = "http://%s:%d" % (HOST, PORT)
TORCH_PIN = ("torch", "torchvision", "torchaudio")
FLAGS_PLAIN = {"--fast-disk", "--cache-none", "--lowvram", "--novram", "--enable-dynamic-vram", "--disable-dynamic-vram",
               "--disable-pinned-memory", "--disable-async-offload", "--disable-mmap", "--disable-smart-memory"}
FLAGS_VALUE = {"--reserve-vram": r"\d+(\.\d+)?", "--default-device": r"\d", "--cuda-device": r"\d(,\d)*",
               "--async-offload": r"\d"}
SAFE_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,80}$")
SAFE_REL = re.compile(r"^[A-Za-z0-9_][A-Za-z0-9._/-]{0,160}$")
STATE = {"proc": None, "log": None, "phase": "start", "t0": time.time(), "marks": {}}


class JobError(RuntimeError):
    def __init__(self, kind, text):
        super().__init__(text)
        self.kind = kind


def fail(kind, text):
    raise JobError(kind, text)


def mark(phase):
    now = time.time()
    STATE["marks"][STATE["phase"]] = round(now - STATE.get("phase_t0", STATE["t0"]), 1)
    STATE["phase"], STATE["phase_t0"] = phase, now
    print("== %s (%.0f s since start)" % (phase, now - STATE["t0"]), flush=True)


def sh(*args, check=True, timeout=900):
    print("+ " + " ".join(args[:8]) + (" ..." if len(args) > 8 else ""), flush=True)
    return subprocess.run(list(args), check=check, text=True, capture_output=True, timeout=timeout)


def write_json(path, obj):
    path.parent.mkdir(parents=True, exist_ok=True)
    tmp = path.with_name(path.name + ".tmp")
    tmp.write_text(json.dumps(obj, indent=1, ensure_ascii=False), encoding="utf-8")
    os.replace(tmp, path)


def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


def mem_available_gb():
    for line in Path("/proc/meminfo").read_text().splitlines():
        if line.startswith("MemAvailable:"):
            return int(line.split()[1]) / (1 << 20)
    return 0.0


def torch_versions():
    out = {}
    for name in TORCH_PIN:
        try:
            out[name] = importlib.metadata.version(name)
        except importlib.metadata.PackageNotFoundError:
            out[name] = None
    return out


# ---- the job --------------------------------------------------------------------------------------------------------
def load_bundle():
    """(job, {name: bytes}, bundle hash): the first part from the notebook's own cell, the further parts from the input
    notebooks' output (/kaggle/input/<slug>/chunk.bin), put together and checked against the bundle's hash."""
    packed = globals().get("JOB_BUNDLE_B85")
    want = globals().get("JOB_BUNDLE_SHA256")
    if not isinstance(packed, str) or not isinstance(want, str):
        fail("setup", "JOB_BUNDLE_B85 / JOB_BUNDLE_SHA256 missing in the cell before this one")
    raw = base64.b85decode(packed)
    for slug in globals().get("JOB_CHUNK_SLUGS") or []:
        if not SAFE_NAME.match(str(slug)):
            fail("setup", "input notebook name rejected")
        found = sorted(Path("/kaggle/input").glob("%s/chunk.bin" % slug)) or sorted(Path("/kaggle/input").glob("**/%s/**/chunk.bin" % slug))
        if not found:
            seen = sorted(str(q)[:100] for q in Path("/kaggle/input").glob("*"))[:30]
            fail("setup", "the part in %s is not mounted; /kaggle/input has: %s" % (slug, seen))
        raw += found[0].read_bytes()
    if hashlib.sha256(raw).hexdigest() != want:
        fail("setup", "job bundle does not match its hash (%d bytes)" % len(raw))
    files = {}
    with zipfile.ZipFile(io.BytesIO(raw)) as zf:
        total = 0
        for info in zf.infolist():
            if not SAFE_NAME.match(info.filename) or info.is_dir():
                fail("setup", "bundle entry rejected: %r" % info.filename)
            total += info.file_size
            if total > 64 << 20:
                fail("setup", "bundle unpacks to more than 64 MiB")
            files[info.filename] = zf.read(info)
    job = json.loads(files.pop("job.json").decode("utf-8"))
    for name, meta in (job.get("files") or {}).items():
        data = files.get(name)
        if data is None or len(data) != meta["size"] or hashlib.sha256(data).hexdigest() != meta["sha256"]:
            fail("setup", "input file %s is missing or does not match the job" % name)
    return job, files, hashlib.sha256(raw).hexdigest()


def comfy_flags(job):
    out, flags, i = [], list(job.get("flags") or []), 0
    while i < len(flags):
        flag = str(flags[i])
        if flag in FLAGS_PLAIN:
            out.append(flag)
        elif flag in FLAGS_VALUE and i + 1 < len(flags) and re.fullmatch(FLAGS_VALUE[flag], str(flags[i + 1])):
            out += [flag, str(flags[i + 1])]
            i += 1
        else:
            fail("setup", "ComfyUI flag not allowed: %r" % flag)
        i += 1
    return out


# ---- the machine ----------------------------------------------------------------------------------------------------
def preflight(job):
    if not Path("/kaggle").exists():
        fail("setup", "/kaggle missing: this runs on Kaggle only")
    smi = sh("nvidia-smi", "--query-gpu=index,name,memory.total,memory.free", "--format=csv,noheader,nounits", timeout=60)
    gpus = []
    for line in smi.stdout.strip().splitlines():
        idx, name, total, free = [x.strip() for x in line.split(",")]
        gpus.append({"index": int(idx), "name": name, "vram_total_mb": int(total), "vram_free_mb": int(free)})
    if len(gpus) < int(job.get("min_gpus", 1)):
        fail("setup", "this session has %d GPU(s), the job needs %d" % (len(gpus), int(job.get("min_gpus", 1))))
    need = sum(m["size"] for m in job.get("models") or []) + (8 << 30)
    disk = shutil.disk_usage("/kaggle/temp" if Path("/kaggle/temp").exists() else "/kaggle")
    if disk.free < need:
        fail("setup", "disk: %.0f GiB free, the models need %.0f GiB" % (disk.free / (1 << 30), need / (1 << 30)))
    sh("ffmpeg", "-version", timeout=60)
    return {"gpus": gpus, "ram_free_gb": round(mem_available_gb(), 1), "disk_free_gb": round(disk.free / (1 << 30), 1),
            "cpus": os.cpu_count(), "python": sys.version.split()[0], "torch": torch_versions()}


def ensure_comfy(job):
    rev = str(job["comfy_rev"])
    if not re.fullmatch(r"[0-9a-f]{40}", rev):
        fail("setup", "ComfyUI revision rejected")
    ROOT.mkdir(parents=True, exist_ok=True)
    if not (COMFY / ".git").exists():
        COMFY.mkdir(parents=True, exist_ok=True)
        sh("git", "init", str(COMFY), timeout=60)
        sh("git", "-C", str(COMFY), "remote", "add", "origin", COMFY_GIT, timeout=60)
    sh("git", "-C", str(COMFY), "fetch", "--depth", "1", "origin", rev, timeout=300)
    sh("git", "-C", str(COMFY), "checkout", "--force", rev, timeout=120)
    if sh("git", "-C", str(COMFY), "rev-parse", "HEAD", timeout=60).stdout.strip() != rev:
        fail("setup", "ComfyUI checkout is not the pinned revision")
    before = torch_versions()
    keep = []
    for line in (COMFY / "requirements.txt").read_text(encoding="utf-8").splitlines():
        name = re.split(r"[\[=<>~!@; ]", line.strip(), 1)[0].lower()
        if name and not name.startswith("#") and name not in TORCH_PIN:
            keep.append(line.strip())
    (ROOT / "constraints.txt").write_text("".join("%s==%s\n" % (k, v) for k, v in before.items() if v), encoding="utf-8")
    (ROOT / "requirements.txt").write_text("\n".join(keep) + "\n", encoding="utf-8")
    pip = sh(sys.executable, "-m", "pip", "install", "-q", "-c", str(ROOT / "constraints.txt"), "-r", str(ROOT / "requirements.txt"),
             check=False, timeout=1200)
    if pip.returncode:
        fail("setup", "pip install failed: %s" % (pip.stderr or pip.stdout)[-600:])
    if torch_versions() != before:
        fail("setup", "the torch packages changed during install")


def apply_patches(job, files):
    """Replaces listed ComfyUI source files with the job's own versions (only when the original is the expected one)."""
    done = {}
    for rel, meta in (job.get("patches") or {}).items():
        if not SAFE_REL.match(rel) or ".." in rel.split("/") or not rel.endswith(".py"):
            fail("setup", "patch target rejected: %r" % rel)
        target = COMFY / rel
        if not target.is_file() or sha256_file(target) != meta["original_sha256"]:
            fail("setup", "patch target %s is not the expected original" % rel)
        target.write_bytes(files[meta["file"]])
        done[rel] = sha256_file(target)
    return done


def download(model):
    rel, size, expect = model["rel"], int(model["size"]), model["sha256"]
    revision = str(model.get("revision") or "")
    if not SAFE_REL.match(rel) or ".." in rel.split("/") or not re.fullmatch(r"[0-9a-f]{40}", revision):
        fail("setup", "model entry rejected: %r" % rel)
    dest = COMFY / "models" / rel
    dest.parent.mkdir(parents=True, exist_ok=True)
    if dest.exists() and dest.stat().st_size == size and sha256_file(dest) == expect:
        return
    part = dest.with_name(dest.name + ".part")
    url = HF_BASE + revision + "/" + rel
    last = "not started"
    for attempt in range(1, 6):
        try:
            have = part.stat().st_size if part.exists() else 0
            if have > size:
                part.unlink()
                have = 0
            headers = {"User-Agent": "autovideo-worker"}
            if have:
                headers["Range"] = "bytes=%d-" % have
            with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=120) as r:
                if have and r.status != 206:
                    have = 0
                with open(part, "ab" if have else "wb") as out:
                    while True:
                        buf = r.read(4 << 20)
                        if not buf:
                            break
                        out.write(buf)
            if part.stat().st_size != size:
                raise RuntimeError("got %d of %d bytes" % (part.stat().st_size, size))
            if sha256_file(part) != expect:
                part.unlink()
                raise RuntimeError("hash mismatch")
            part.replace(dest)
            print("model ready: %s (%.1f GiB)" % (rel, size / (1 << 30)), flush=True)
            return
        except Exception as e:  # noqa: BLE001 - retried, then reported
            last = "%s: %s" % (type(e).__name__, str(e)[:160])
            print("download %s try %d/5: %s" % (rel, attempt, last), flush=True)
            time.sleep(5 * attempt)
    fail("download", "model %s did not arrive: %s" % (rel, last))


# ---- ComfyUI --------------------------------------------------------------------------------------------------------
def start_comfy(job):
    s = socket.socket()
    try:
        if s.connect_ex((HOST, PORT)) == 0:
            fail("setup", "port %d is already taken" % PORT)
    finally:
        s.close()
    for name in ("input", "temp", "user", "output"):
        (ROOT / name).mkdir(parents=True, exist_ok=True)
    cmd = [sys.executable, "main.py", "--listen", HOST, "--port", str(PORT), "--disable-auto-launch", "--disable-api-nodes",
           "--disable-all-custom-nodes", "--disable-metadata", "--database-url", "sqlite:///:memory:",
           "--output-directory", str(ROOT / "output"), "--input-directory", str(ROOT / "input"),
           "--temp-directory", str(ROOT / "temp"), "--user-directory", str(ROOT / "user")] + comfy_flags(job)
    STATE["log"] = open(ROOT / "comfy.log", "w", encoding="utf-8", errors="replace")
    STATE["proc"] = subprocess.Popen(cmd, cwd=str(COMFY), start_new_session=True, stdout=STATE["log"], stderr=subprocess.STDOUT)
    t0 = time.time()
    while time.time() - t0 < 300:
        try:
            with urllib.request.urlopen(BASE + "/system_stats", timeout=5) as r:
                return json.loads(r.read().decode())
        except Exception:  # noqa: BLE001 - not up yet
            if STATE["proc"].poll() is not None:
                fail("comfy", "ComfyUI ended while starting: %s" % log_tail(30))
            time.sleep(2)
    fail("comfy", "ComfyUI did not answer within 5 minutes: %s" % log_tail(30))


def stop_comfy():
    proc = STATE.get("proc")
    if proc is not None:
        for sig in (signal.SIGTERM, signal.SIGKILL):
            try:
                os.killpg(proc.pid, sig)
                proc.wait(timeout=30)
                break
            except Exception:  # noqa: BLE001 - already gone or needs the harder signal
                pass
        STATE["proc"] = None
    if STATE.get("log") is not None:
        try:
            STATE["log"].close()
        except Exception:  # noqa: BLE001
            pass
        STATE["log"] = None


def log_tail(n=12, width=220):
    try:
        if STATE.get("log") is not None:
            STATE["log"].flush()
        lines = (ROOT / "comfy.log").read_text(encoding="utf-8", errors="replace").replace("\r", "\n").splitlines()
        return " | ".join(re.sub(r"[\x00-\x1f]+", " ", x).strip()[:width] for x in lines[-n:] if x.strip())
    except OSError:
        return ""


def http_json(method, path, body=None, timeout=60):
    data = None if body is None else json.dumps(body).encode()
    req = urllib.request.Request(BASE + path, data=data, method=method,
                                 headers={"Content-Type": "application/json"} if body is not None else {})
    try:
        with urllib.request.urlopen(req, timeout=timeout) as r:
            raw = r.read().decode()
            return json.loads(raw) if raw else {}
    except urllib.error.HTTPError as e:
        fail("comfy", "%s %s -> HTTP %d: %s" % (method, path, e.code, e.read().decode(errors="replace")[:1500]))


class Telemetry:
    """Every 10 s: nvidia-smi and MemAvailable; keeps the peaks (and the low-memory count for the guard)."""

    def __init__(self):
        self.stop_event, self.peak, self.ram_min, self.samples = threading.Event(), {}, 1e9, 0
        self.thread = threading.Thread(target=self._loop, daemon=True)
        self.thread.start()

    def _loop(self):
        while not self.stop_event.wait(10):
            try:
                smi = subprocess.run(["nvidia-smi", "--query-gpu=index,memory.used,utilization.gpu", "--format=csv,noheader,nounits"],
                                     text=True, capture_output=True, timeout=10)
                for line in smi.stdout.strip().splitlines():
                    idx, used, util = [int(float(x)) for x in line.split(",")]
                    pk = self.peak.setdefault(str(idx), {"mem_mib": 0, "util": 0})
                    pk["mem_mib"], pk["util"] = max(pk["mem_mib"], used), max(pk["util"], util)
                self.ram_min = min(self.ram_min, mem_available_gb())
                self.samples += 1
            except Exception:  # noqa: BLE001 - a measurement only
                pass

    def summary(self):
        self.stop_event.set()
        return {"gpu_peak": self.peak, "ram_min_gb": round(self.ram_min, 1) if self.ram_min < 1e9 else None, "samples": self.samples}


def render(job, tel):
    info = http_json("GET", "/object_info", timeout=120)
    graph = job["graph"]
    missing = sorted({n["class_type"] for n in graph.values() if n["class_type"] not in info})
    if missing:
        fail("comfy", "this ComfyUI has no node(s): %s" % missing)
    resp = http_json("POST", "/prompt", {"prompt": graph, "client_id": str(job["job_id"])}, timeout=120)
    pid = resp.get("prompt_id")
    if not pid:
        fail("comfy", "ComfyUI refused the job: %s" % str(resp)[:1500])
    t0, low, noted = time.time(), 0, 0.0
    limit = int(job.get("render_timeout_s") or 5400)
    while time.time() - t0 < limit:
        time.sleep(5)
        if STATE["proc"].poll() is not None:
            fail("oom" if "Killed" in log_tail(5) or mem_available_gb() < 3 else "comfy", "ComfyUI ended during the render: %s" % log_tail(15))
        low = low + 1 if mem_available_gb() < 1.5 else 0
        if low >= 3:
            http_json("POST", "/interrupt", {}, timeout=30)
            fail("oom", "system memory under 1.5 GiB three times: the render was stopped")
        entry = http_json("GET", "/history/" + pid, timeout=30).get(pid)
        if entry:
            status = entry.get("status") or {}
            if status.get("status_str") in ("error", "interrupted"):
                errors = [v for k, v in (status.get("messages") or []) if k == "execution_error" and isinstance(v, dict)]
                text = "%s: %s" % (errors[-1].get("exception_type"), str(errors[-1].get("exception_message"))[:1500]) if errors else "no detail"
                fail("oom" if "OutOfMemory" in text or "out of memory" in text.lower() else "render", text)
            if status.get("completed") is True and status.get("status_str") == "success":
                return entry, round(time.time() - t0, 1)
        if time.time() - noted >= 60:
            noted = time.time()
            print("render %.0f s | RAM free %.1f GiB | %s" % (time.time() - t0, mem_available_gb(), log_tail(1, 150)), flush=True)
    http_json("POST", "/interrupt", {}, timeout=30)
    fail("timeout", "the render passed its limit of %d s: %s" % (limit, log_tail(3)))


def collect(job, entry):
    node = (entry.get("outputs") or {}).get(str(job["save_node"])) or {}
    root = (ROOT / "output").resolve()
    for value in node.values():
        for item in value if isinstance(value, list) else []:
            if isinstance(item, dict) and str(item.get("filename", "")).lower().endswith(".mp4") and item.get("type") == "output":
                path = (root / str(item.get("subfolder") or "") / str(item["filename"])).resolve()
                if root in path.parents and path.is_file():
                    return path
    fail("render", "the finished job lists no video file")


def validate(job, path):
    probe = json.loads(sh("ffprobe", "-v", "error", "-show_entries", "stream=codec_type,width,height,nb_frames:format=duration",
                          "-of", "json", str(path), timeout=120).stdout)
    video = [s for s in probe.get("streams") or [] if s.get("codec_type") == "video"]
    audio = [s for s in probe.get("streams") or [] if s.get("codec_type") == "audio"]
    if not video or not audio:
        fail("validate", "the video has %d picture and %d sound stream(s)" % (len(video), len(audio)))
    w, h = int(video[0]["width"]), int(video[0]["height"])
    duration = float((probe.get("format") or {}).get("duration") or 0)
    expect = job.get("expect") or {}
    if expect.get("width") and (w, h) != (expect["width"], expect["height"]):
        fail("validate", "the video is %dx%d, expected %dx%d" % (w, h, expect["width"], expect["height"]))
    if expect.get("seconds") and not (expect["seconds"][0] <= duration <= expect["seconds"][1]):
        fail("validate", "the video runs %.2f s, expected %s" % (duration, expect["seconds"]))
    sh("ffmpeg", "-v", "error", "-i", str(path), "-f", "null", "-", timeout=600)
    vol = sh("ffmpeg", "-v", "info", "-i", str(path), "-af", "volumedetect", "-f", "null", "-", timeout=600, check=False)
    m = re.search(r"mean_volume:\s*([-\d.]+)\s*dB", (vol.stderr or "") + (vol.stdout or ""))
    return {"width": w, "height": h, "duration": round(duration, 3), "mean_volume_db": float(m.group(1)) if m else None,
            "size": path.stat().st_size, "sha256": sha256_file(path)}


def probe(job):
    """What this machine's ComfyUI says about itself: the head of its log, the attention choices, the accelerated
    backend's load result, package versions. No model is loaded."""
    code = (
        "import json, os, sys\n"
        "out = {}\n"
        "try:\n"
        "    import torch\n"
        "    out.update(torch=torch.__version__, cuda=torch.version.cuda, gpus=[torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())],\n"
        "               capability=list(torch.cuda.get_device_capability(0)))\n"
        "    from comfy_kitchen.backends import cuda as c\n"
        "    out.update(ext=getattr(c, '_EXT_AVAILABLE', None), ext_error=str(getattr(c, '_EXT_ERROR', None))[:600],\n"
        "               cublaslt=getattr(c, '_CUBLASLT_AVAILABLE', None), files=sorted(os.listdir(os.path.dirname(c.__file__))))\n"
        "except Exception as e:\n"
        "    out['error'] = repr(e)[:600]\n"
        "print(json.dumps(out))\n")
    check = subprocess.run([sys.executable, "-c", code], text=True, capture_output=True, timeout=300, cwd=str(COMFY))
    versions = {}
    for name in ("comfy-kitchen", "comfy-aimdo", "torch", "triton", "av", "numpy", "safetensors", "nvidia-cublas", "nvidia-cublas-cu13",
                 "nvidia-cuda-runtime", "nvidia-cuda-runtime-cu13"):
        try:
            versions[name] = importlib.metadata.version(name)
        except importlib.metadata.PackageNotFoundError:
            versions[name] = None
    wheel = ""
    for dist in importlib.metadata.distributions():
        if (dist.metadata.get("Name") or "").lower().replace("_", "-") == "comfy-kitchen":
            wheel = " ".join(line for line in (dist.read_text("WHEEL") or "").splitlines() if line.startswith("Tag:"))
    info = http_json("GET", "/object_info/ModelAttentionBackend", timeout=120)
    spec = (((info.get("ModelAttentionBackend") or {}).get("input") or {}).get("required") or {}).get("attention") or []
    choices = spec[0] if spec and isinstance(spec[0], list) else ((spec[1] or {}).get("options") if len(spec) > 1 and isinstance(spec[1], dict) else None)
    if STATE.get("log") is not None:
        STATE["log"].flush()
    head = (ROOT / "comfy.log").read_text(encoding="utf-8", errors="replace").replace("\r", "\n").splitlines()[:260]
    (OUT / "comfy-head.log").write_text("\n".join(re.sub(r"[\x00-\x08\x0b-\x1f]", "", line)[:1200] for line in head), encoding="utf-8")
    smi = sh("nvidia-smi", "--query-gpu=name,driver_version,memory.total", "--format=csv,noheader", timeout=60).stdout.strip()
    libc = subprocess.run(["ldd", "--version"], text=True, capture_output=True).stdout.splitlines()[:1]
    return {"kitchen_check": (check.stdout.strip() or check.stderr.strip())[-2500:], "versions": versions, "kitchen_wheel": wheel,
            "attention_choices": choices, "nvidia_smi": smi, "libc": libc, "python": sys.version,
            "backend_lines": [line[:900] for line in head if "comfy_kitchen backend" in line or "aimdo" in line.lower() or "kitchen version" in line]}


# ---- run ------------------------------------------------------------------------------------------------------------
def main():
    OUT.mkdir(parents=True, exist_ok=True)
    receipt = {"status": "fail", "error": None, "error_kind": None, "started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
               "bundle_sha256": globals().get("JOB_BUNDLE_SHA256")}
    tel = None
    try:
        mark("bundle")
        job, files, bundle_sha = load_bundle()
        receipt.update(job_id=job["job_id"], bundle_sha256=bundle_sha, kind=job.get("kind"))
        if job.get("kind") == "selftest":                      # the transport alone: no ComfyUI
            try:
                smi = subprocess.run(["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"], text=True, capture_output=True)
                gpus = [x.strip() for x in smi.stdout.splitlines() if x.strip()] if smi.returncode == 0 else []
            except OSError:                                    # a CPU session has no nvidia-smi
                gpus = []
            internet = None
            if job.get("internet"):                            # the enable check: can this account's session reach the model files?
                import urllib.request
                try:
                    with urllib.request.urlopen("https://huggingface.co/api/models/Comfy-Org/MiniMax-H3", timeout=30) as response:
                        internet = response.status == 200
                except Exception:  # noqa: BLE001
                    internet = False
            receipt.update(status="ok", files={k: hashlib.sha256(v).hexdigest() for k, v in sorted(files.items())},
                           cpus=os.cpu_count(), ram_free_gb=round(mem_available_gb(), 1), gpus=gpus, internet=internet)
            (OUT / "selftest.bin").write_bytes(b"".join(files[k] for k in sorted(files)))
            receipt["output"] = {"path": "autovideo/selftest.bin", "sha256": sha256_file(OUT / "selftest.bin"),
                                 "size": (OUT / "selftest.bin").stat().st_size}
            return
        mark("preflight")
        receipt["hardware"] = preflight(job)
        mark("comfyui")
        ensure_comfy(job)
        receipt["comfy_rev"] = job["comfy_rev"]
        receipt["patches"] = apply_patches(job, files)
        if job.get("kind") == "probe":
            mark("start")
            stats = start_comfy(job)
            receipt["devices"] = [{k: d.get(k) for k in ("name", "type", "index", "vram_total")} for d in stats.get("devices") or []]
            receipt["probe"] = probe(job)
            head = OUT / "comfy-head.log"
            receipt.update(status="ok", output={"path": "autovideo/comfy-head.log", "sha256": sha256_file(head), "size": head.stat().st_size})
            return
        mark("models")
        for model in job.get("models") or []:
            download(model)
        mark("inputs")
        for name in job.get("files") or {}:
            if name in {p["file"] for p in (job.get("patches") or {}).values()}:
                continue
            target = ROOT / "input" / "job" / name
            target.parent.mkdir(parents=True, exist_ok=True)
            target.write_bytes(files[name])
        mark("start")
        stats = start_comfy(job)
        receipt["devices"] = [{k: d.get(k) for k in ("name", "type", "index", "vram_total")} for d in stats.get("devices") or []]
        tel = Telemetry()
        mark("render")
        entry, seconds = render(job, tel)
        receipt["render_s"] = seconds
        mark("collect")
        src = collect(job, entry)
        meta = validate(job, src)
        dest = OUT / ("%s.mp4" % job["job_id"])
        shutil.copy2(src, dest)
        meta["path"] = "autovideo/" + dest.name
        receipt.update(status="ok", output=meta)
    except JobError as e:
        receipt.update(error=str(e)[:3000], error_kind=e.kind)
        print("FAIL (%s): %s" % (e.kind, e), flush=True)
    except Exception as e:  # noqa: BLE001 - reported in the receipt
        receipt.update(error="%s: %s" % (type(e).__name__, str(e)[:3000]), error_kind="worker")
        print("FAIL (worker): %s: %s" % (type(e).__name__, e), flush=True)
    finally:
        mark("end")
        if tel is not None:
            receipt["telemetry"] = tel.summary()
        if receipt["status"] != "ok":
            receipt["log_tail"] = log_tail(25)
        stop_comfy()
        receipt["phases_s"] = {k: v for k, v in STATE["marks"].items() if k != "start"}
        receipt["total_s"] = round(time.time() - STATE["t0"], 1)
        write_json(OUT / "receipt.json", receipt)
        print("receipt: %s" % json.dumps({k: receipt.get(k) for k in ("status", "error_kind", "error", "total_s")}, ensure_ascii=False)[:1500],
              flush=True)


main()
