| """Small helpers.""" | |
| from __future__ import annotations | |
| import os | |
| from pathlib import Path | |
| from typing import Optional | |
| def find_latest_ckpt(run_dir: str | Path) -> Optional[str]: | |
| """Locate the latest `*.ckpt` under run_dir, for auto-resume after server restart.""" | |
| rd = Path(run_dir) | |
| if not rd.exists(): | |
| return None | |
| ckpts = sorted(rd.rglob("*.ckpt"), key=lambda p: p.stat().st_mtime, reverse=True) | |
| return str(ckpts[0]) if ckpts else None | |
| def env_or(default: str, key: str) -> str: | |
| return os.environ.get(key, default) | |