From 2778f8cc438f5da5cc3f85bfb161934111aa150c Mon Sep 17 00:00:00 2001 From: Sahil Jagtap Date: Tue, 17 Nov 2026 19:04:49 -0500 Subject: [PATCH 1/4] drums preflight: check gateway, console, key, scopes, host capacity, admission, rate limit, quota, spend cap and task set before a run; drums.preflight() in Python --- drums/__init__.py | 5 +- drums/_preflight.py | 517 ++++++++++++++++++++++++++++++++++++++++ drums/cli.py | 6 + tests/test_preflight.py | 306 ++++++++++++++++++++++++ 4 files changed, 833 insertions(+), 1 deletion(-) create mode 100644 drums/_preflight.py create mode 100644 tests/test_preflight.py diff --git a/drums/__init__.py b/drums/__init__.py index 35ee461..ab8e9d0 100644 --- a/drums/__init__.py +++ b/drums/__init__.py @@ -55,7 +55,7 @@ __all__ = ["ActionFailed", "connect", "Client", "Env", "Reward", "Observation", "StepResult", "State", "ForkResult", "env", "__version__", "DrumsError", "AuthError", "BadRequest", "NotFound", "EnvGone", "Conflict", "DaemonUnavailable", "GuestError", "EnvFailed", "RestoreFailed", "GateFailed", "TaskSetupFailed", "ForkError", "BatchError", "InvalidEpisode", "CheckerError", "NotQuiescent", "Overloaded", - "CheckpointStale", "TaskUnsupported", "InsecureTransport", "RedirectRefused", "check_transport"] + "CheckpointStale", "TaskUnsupported", "InsecureTransport", "RedirectRefused", "check_transport", "preflight", "PreflightReport"] _STATUS = {400: BadRequest, 401: AuthError, 404: NotFound, 409: Conflict, 410: EnvGone, 422: BadRequest, 429: Overloaded, 502: GuestError, 503: DaemonUnavailable} @@ -878,3 +878,6 @@ def env(task: dict | None = None, *, from_state: str | None = None, checkpoint: e = Env(c, rec) e._check_up() return e + + +from ._preflight import PreflightReport, preflight # noqa: E402 (needs Client defined above) diff --git a/drums/_preflight.py b/drums/_preflight.py new file mode 100644 index 0000000..eee6633 --- /dev/null +++ b/drums/_preflight.py @@ -0,0 +1,517 @@ +"""`drums preflight` / `drums.preflight(...)`: before any GPU is paid for, check that the gateway, the key and the +workspace can carry N desktops for H hours. Every block of the D1 run (rate limit, spend cap, live-desktop quota, +host capacity, control-plane health, a task set nobody had validated) would have shown up here. + + drums preflight --desktops 128 --hours 3 --task-set 0b5c2a52-... [--model-gpus 2:6] [--json] + + report = drums.preflight(desktops=128, hours=3, task_set="0b5c2a52-...") + if not report.ok: + print(report.table()) + +What is checked, and where the numbers come from (only reads; nothing is created): + + gateway GET /v1/health answers, how fast (control-plane health; the gateway reads the database to answer) + console the console URL answers (sign-in page) + auth the key is accepted, not revoked, and does not expire during the run (GET /v1/api-keys, by prefix) + scopes computers:read + computers:write (required); runs:write (run object), usage:read (cost) wanted + host capacity free desktops on the bound host >= N (GET /v1/health `capacity`), and the host takes creates + (drumsd ok, no stale image checkpoint) + rate limit the per-key limit vs N x REQUESTS_PER_DESKTOP_S, and its burst vs N x BURST_PER_DESKTOP + quota the workspace's live-desktop limit, minus what is live now, >= N + spend cap spent this period + N x H x the desktop-hour price <= the cap + task set (with --task-set) registered for this workspace, and validated (oracle accepted, none rejected) + +The rate limit, quota, spend cap and price come from `GET /v1/limits`, a read-only summary the gateway does not serve +yet (expected shape in `_limits_view`). Until it does, those rows say "not checked" and why, and `--desktop-hour-usd` +supplies the price for the projection. A row that could not be checked never fails the preflight; `--strict` does. +""" +from __future__ import annotations + +import json +import os +import re +import time +import urllib.error +import urllib.parse +import urllib.request + +from .errors import AuthError, DrumsError, NotFound + +#: D1 rule of thumb (2026-11-16, 128 desktops on Qwen3.5-27B): each ACTIVE desktop costs about one action and one +#: screenshot per turn every few seconds through the gateway, about 0.5 requests per second (128 desktops needed about +#: 60 req/s). A run of N desktops needs a per-key rate of at least N x this. +REQUESTS_PER_DESKTOP_S = 0.5 +#: A run's start: every desktop is created and takes its first screenshot within a few seconds, so the burst +#: (bucket size) should hold about one request per desktop. +BURST_PER_DESKTOP = 1.0 +#: The gateway's default per-key limit (RATE_LIMIT_PER_S / RATE_LIMIT_BURST unset): a pilot setting. +DEFAULT_RATE_PER_S, DEFAULT_RATE_BURST = 20, 60 +REQUIRED_SCOPES = ("computers:read", "computers:write") +WANTED_SCOPES = {"runs:write": "the run object (client.run) cannot record the run", + "usage:read": "usage and cost reads are refused"} +#: /v1/health slower than this is a warning, ten times slower a failure (the 2026-11-16 outage answered in 10 to 90 s) +HEALTH_SLOW_S = 2.0 +DEFAULT_CONSOLE_URL = "https://console.drums.run" + +PASS, FAIL, WARN, SKIP, INFO = "pass", "fail", "warn", "not checked", "info" +_KEY = re.compile(r"^drums_sk_([a-z2-7]{12})_") +_UUID = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I) + + +class Check: + """One row: `name`, `status` (pass, fail, warn, not checked, info), `detail`, and for anything but a pass the + `fix` and a console `link`.""" + + def __init__(self, name: str, status: str, detail: str, fix: str | None = None, link: str | None = None): + self.name, self.status, self.detail, self.fix, self.link = name, status, detail, fix, link + + def to_dict(self) -> dict: + return {"name": self.name, "status": self.status, "detail": self.detail, "fix": self.fix, "link": self.link} + + def __repr__(self): + return f"" + + +class PreflightReport: + """The checks, in order. `ok` is False when any check failed; `exit_code(strict)` is what `drums preflight` + returns (1 on a failure; with strict also on a warning or a row that could not be checked).""" + + def __init__(self, desktops: int, hours: float, checks: list, need: dict): + self.desktops, self.hours, self.checks, self.need = desktops, hours, checks, need + + @property + def ok(self) -> bool: + return not any(c.status == FAIL for c in self.checks) + + def exit_code(self, strict: bool = False) -> int: + if not self.ok: + return 1 + if strict and any(c.status in (WARN, SKIP) for c in self.checks): + return 1 + return 0 + + def to_dict(self) -> dict: + return {"ok": self.ok, "desktops": self.desktops, "hours": self.hours, "need": self.need, + "checks": [c.to_dict() for c in self.checks]} + + def table(self) -> str: + w = max(len(c.name) for c in self.checks) if self.checks else 5 + lines = [f"drums preflight: {self.desktops} desktops for {self.hours:g} h", "", + f"{'CHECK'.ljust(w)} {'RESULT'.ljust(11)} DETAIL"] + for c in self.checks: + lines.append(f"{c.name.ljust(w)} {c.status.upper().ljust(11)} {c.detail}") + todo = [c for c in self.checks if c.status in (FAIL, WARN, SKIP) and (c.fix or c.link)] + if todo: + lines += ["", "To do:"] + for c in todo: + lines.append(f" [{c.status}] {c.name}: {c.fix or ''}{(' ' + c.link) if c.link else ''}") + n_fail = sum(c.status == FAIL for c in self.checks) + n_skip = sum(c.status == SKIP for c in self.checks) + lines += ["", ("PASS" if self.ok else f"FAIL ({n_fail} check{'s' if n_fail != 1 else ''} failed)") + + (f"; {n_skip} could not be checked" if n_skip else "")] + return "\n".join(lines) + "\n" + + +def parse_model_gpus(v) -> dict | None: + """'8' (total), '2:6' or '2,6' (inference:trainer) -> {"inference", "trainer", "total"}; None for None.""" + if v is None or v == "": + return None + if isinstance(v, int): + return {"inference": None, "trainer": None, "total": v} + parts = [p for p in re.split(r"[:,]", str(v).strip()) if p != ""] + try: + nums = [int(p) for p in parts] + except ValueError: + raise ValueError(f"--model-gpus takes a GPU count (8) or inference:trainer (2:6); got {v!r}") from None + if not nums or any(n < 0 for n in nums) or len(nums) > 2: + raise ValueError(f"--model-gpus takes a GPU count (8) or inference:trainer (2:6); got {v!r}") + if len(nums) == 1: + return {"inference": None, "trainer": None, "total": nums[0]} + return {"inference": nums[0], "trainer": nums[1], "total": nums[0] + nums[1]} + + +def _num(d, *keys): + for k in keys: + v = d.get(k) if isinstance(d, dict) else None + if isinstance(v, (int, float)) and not isinstance(v, bool): + return float(v) + if isinstance(v, str): + try: + return float(v) + except ValueError: + pass + return None + + +def _limits_view(doc) -> dict: + """The fields preflight reads from `GET /v1/limits` (a proposal; any of them may be missing): + + {"workspace": {"max_live_computers", "live_computers", "max_spend_usd", "spent_usd", "period_end"}, + "rate_limit": {"per_s", "burst"}, + "price": {"desktop_hour_usd"}, + "host": {"id", "status", "max_desktops", "live"}} + """ + d = doc if isinstance(doc, dict) else {} + ws = d.get("workspace") if isinstance(d.get("workspace"), dict) else d + rl = d.get("rate_limit") if isinstance(d.get("rate_limit"), dict) else {} + price = d.get("price") if isinstance(d.get("price"), dict) else {} + host = d.get("host") if isinstance(d.get("host"), dict) else {} + return { + "max_live": _num(ws, "max_live_computers", "max_live"), + "live": _num(ws, "live_computers", "live"), + "max_spend": _num(ws, "max_spend_usd"), + "cap_set": "max_spend_usd" in ws, + "spent": _num(ws, "spent_usd", "period_spend_usd"), + "period_end": ws.get("period_end"), + "rate_per_s": _num(rl, "per_s", "rate_per_s"), + "rate_burst": _num(rl, "burst"), + "desktop_hour_usd": _num(price, "desktop_hour_usd", "usd_per_desktop_hour"), + "host_id": host.get("id"), + "host_status": host.get("status"), + } + + +class _Links: + def __init__(self, console: str, workspace: str | None): + self.console, self.ws = console.rstrip("/"), workspace + + def page(self, path: str, what: str) -> str: + if self.ws: + return f"{self.console}/w/{urllib.parse.quote(self.ws)}/{path}" + return f"{self.console} ({what})" + + +def _console_up(url: str, timeout: float) -> tuple: + """(ok, detail, fix) for a GET of the console's sign-in page; no token is sent. A failed certificate check is tried + again with certifi's bundle when certifi is installed (python.org's macOS Python has no root certificates).""" + import ssl # noqa: PLC0415 + req = urllib.request.Request(url.rstrip("/") + "/sign-in", method="GET", headers={"User-Agent": "drums-preflight"}) + t0 = time.monotonic() + ctx = None + for _ in range(2): + try: + with urllib.request.urlopen(req, timeout=timeout, context=ctx) as r: # noqa: S310 (the user's or the default https URL) + return r.status < 500, f"HTTP {r.status} in {time.monotonic() - t0:.1f} s", None + except urllib.error.HTTPError as e: + return e.code < 500, f"HTTP {e.code} in {time.monotonic() - t0:.1f} s", "the console is failing: wait before launching" + except (urllib.error.URLError, OSError) as e: + reason = getattr(e, "reason", e) + if isinstance(reason, ssl.SSLCertVerificationError): + if ctx is None: + try: + import certifi # noqa: PLC0415 + ctx = ssl.create_default_context(cafile=certifi.where()) + continue + except ImportError: + pass + return False, "TLS certificate not verified by this Python", ("on macOS run \"Install Certificates.command\" " + "(in /Applications/Python 3.x/) or pip install certifi") + return False, f"no answer ({reason})", "the console is down: wait before launching" + return False, "no answer", "the console is down: wait before launching" + + +def preflight(desktops: int, hours: float, *, model_gpus=None, task_set: str | None = None, url: str | None = None, + token: str | None = None, console_url: str | None = None, workspace: str | None = None, + desktop_hour_usd: float | None = None, gpu_hour_usd: float | None = None, client=None, + check_console: bool = True, timeout: float = 20.0) -> PreflightReport: + """Check that the gateway, the key and the workspace can carry `desktops` desktops for `hours` hours. + + url/token default to $DRUMS_URL / $DRUMS_TOKEN (or pass a `client`); console_url to $DRUMS_CONSOLE_URL or + https://console.drums.run; workspace (the console slug, for links) to $DRUMS_WORKSPACE. desktop_hour_usd + overrides the gateway's desktop-hour price; gpu_hour_usd with model_gpus adds the GPU cost to the projection + (GPUs are not billed by Drums, so they are not checked against the spend cap). Reads only; never raises for a + failed check (see the report), only for bad arguments.""" + from . import Client # noqa: PLC0415 (import cycle) + + if not isinstance(desktops, int) or desktops < 1: + raise ValueError("desktops must be a positive integer") + if not isinstance(hours, (int, float)) or hours <= 0: + raise ValueError("hours must be a positive number") + gpus = parse_model_gpus(model_gpus) + console = (console_url or os.environ.get("DRUMS_CONSOLE_URL") or DEFAULT_CONSOLE_URL).rstrip("/") + links = _Links(console, workspace or os.environ.get("DRUMS_WORKSPACE")) + if client is None: + client = Client(url or os.environ.get("DRUMS_URL") or "", token if token is not None else os.environ.get("DRUMS_TOKEN"), + timeout=timeout, retry_s=3.0) + tok = client.token or "" + need_rate = desktops * REQUESTS_PER_DESKTOP_S + need_burst = desktops * BURST_PER_DESKTOP + need = {"requests_per_s": need_rate, "burst": need_burst, "requests_per_desktop_s": REQUESTS_PER_DESKTOP_S, + "desktop_hours": desktops * hours} + checks: list = [] + add = checks.append + + # ---- gateway (control plane) + auth ------------------------------------------------------------------------------ + health, t0 = None, time.monotonic() + try: + health = client.call("GET", "/v1/health", timeout=timeout) + took = time.monotonic() - t0 + if took > HEALTH_SLOW_S * 10: + add(Check("gateway", FAIL, f"/v1/health answered in {took:.1f} s: the control plane is overloaded", + "wait for it to recover before launching; a run would see 503s", links.page("overview", "Overview"))) + elif took > HEALTH_SLOW_S: + add(Check("gateway", WARN, f"/v1/health answered in {took:.1f} s (slow; normal is well under {HEALTH_SLOW_S:g} s)", + "launch with fewer desktops first, or wait", links.page("overview", "Overview"))) + else: + add(Check("gateway", PASS, f"{client.url} answered in {took:.2f} s (drumsd {health.get('version', '?') if isinstance(health, dict) else '?'})")) + except AuthError as e: + add(Check("gateway", PASS, f"{client.url} answers")) + add(Check("auth", FAIL, f"the key was refused: {e.message}", + "create a key (or check DRUMS_TOKEN has no stray space or newline)", links.page("api-keys", "API keys"))) + return _finish(desktops, hours, checks, need, gpus, gpu_hour_usd, check_console, console, timeout) + except DrumsError as e: + if e.status == 403: + add(Check("gateway", PASS, f"{client.url} answers")) + add(Check("auth", FAIL, f"the key was refused here: {e.message}", + "use the gateway URL of the key's own workspace (shown with the key in the console)", links.page("api-keys", "API keys"))) + return _finish(desktops, hours, checks, need, gpus, gpu_hour_usd, check_console, console, timeout) + add(Check("gateway", FAIL, f"{client.url or '(no DRUMS_URL)'}: {e.message if e.status else e}", + "check DRUMS_URL (it ends in /gw for Drums Cloud) and that the gateway is up", links.page("overview", "Overview"))) + return _finish(desktops, hours, checks, need, gpus, gpu_hour_usd, check_console, console, timeout, auth_unknown=True) + health = health if isinstance(health, dict) else {} + + # the key's own row: scopes and expiry + m = _KEY.match(tok) + key_row = None + try: + rows = (client.call("GET", "/v1/api-keys", timeout=timeout) or {}).get("items") or [] + key_row = next((r for r in rows if m and r.get("prefix") == m.group(1)), None) + except DrumsError as e: + add(Check("auth", PASS, f"the key is accepted (its metadata could not be read: {e.message})")) + if key_row is not None: + exp = key_row.get("expires_at") + exp_s = _epoch(exp) if exp else None + if key_row.get("revoked_at"): + add(Check("auth", FAIL, "the key is revoked", "create a new key", links.page("api-keys", "API keys"))) + elif exp_s is not None and exp_s < time.time() + hours * 3600: + left = max(0.0, (exp_s - time.time()) / 3600) + add(Check("auth", FAIL, f"the key expires in {left:.1f} h, before the {hours:g} h run ends ({exp})", + "create a key that outlives the run", links.page("api-keys", "API keys"))) + else: + add(Check("auth", PASS, f"key {key_row.get('name') or m.group(1)!s} accepted" + (f", expires {exp}" if exp else ", no expiry"))) + scopes = set(key_row.get("scopes") or []) + missing = [s for s in REQUIRED_SCOPES if s not in scopes] + wanted = [s for s in WANTED_SCOPES if s not in scopes] + if missing: + add(Check("scopes", FAIL, f"missing {', '.join(missing)} (has {', '.join(sorted(scopes)) or 'none'})", + "create a key with computers:read and computers:write", links.page("api-keys", "API keys"))) + elif wanted: + add(Check("scopes", WARN, f"no {', '.join(wanted)}: " + "; ".join(WANTED_SCOPES[s] for s in wanted), + f"add {', '.join(wanted)} to the key", links.page("api-keys", "API keys"))) + else: + add(Check("scopes", PASS, ", ".join(sorted(scopes)))) + else: + if not any(c.name == "auth" for c in checks): + add(Check("auth", PASS, "the key is accepted")) + add(Check("scopes", SKIP, "the key's row was not found by its prefix, so its scopes and expiry are unknown", + "check the key's scopes on the API keys page", links.page("api-keys", "API keys"))) + + # ---- the limits summary (rate limit, quota, spend, price) ------------------------------------------------------- + lim, lim_why = None, None + try: + lim = _limits_view(client.call("GET", "/v1/limits", timeout=timeout)) + except NotFound: + lim_why = "this gateway does not publish GET /v1/limits yet" + except DrumsError as e: + lim_why = f"GET /v1/limits failed: {e.message if e.status else e}" + + # host capacity + admission + cap = health.get("capacity") if isinstance(health.get("capacity"), dict) else {} + live, mx = _num(cap, "live"), _num(cap, "max") + if mx is not None and live is not None: + free = int(mx - live) + if free >= desktops: + add(Check("host capacity", PASS, f"{free} of {int(mx)} desktops free on the bound host")) + else: + add(Check("host capacity", FAIL, f"{free} of {int(mx)} desktops free on the bound host; {desktops} needed", + f"close idle desktops, or ask for a bigger host (or run {max(free, 0)} desktops)", links.page("computers", "Computers"))) + else: + add(Check("host capacity", SKIP, "the health reply carries no capacity")) + stale = [c.get("id") for c in health.get("checkpoints") or [] if isinstance(c, dict) and c.get("stale_reason")] + host_status = lim.get("host_status") if lim else None + if health.get("ok") is False: + add(Check("admission", FAIL, "drumsd on the bound host reports not ok: creates will be refused", + "the host needs an operator; report it with the request id", links.page("overview", "Overview"))) + elif host_status and host_status != "active": + add(Check("admission", FAIL, f"the bound host is {host_status}: new desktops are refused", "wait, or ask for another host", + links.page("overview", "Overview"))) + elif stale: + add(Check("admission", WARN, f"stale image checkpoints (creates from them are refused): {', '.join(map(str, stale))}", + "use another image or ask an operator to reinstall it", links.page("templates", "Desktop images"))) + else: + add(Check("admission", PASS, "drumsd ok, no stale checkpoint" + (f", host {host_status}" if host_status else ""))) + + # rate limit + if lim and lim["rate_per_s"] is not None: + per_s, burst = lim["rate_per_s"], lim["rate_burst"] + if per_s < need_rate or (burst is not None and burst < need_burst): + add(Check("rate limit", FAIL, f"{per_s:g} req/s, burst {burst if burst is not None else '?'}: {desktops} desktops need about " + f"{need_rate:g} req/s and a burst of {need_burst:g} ({REQUESTS_PER_DESKTOP_S:g} req/s per active desktop)", + "ask for a training rate profile", links.page("usage", "Usage"))) + else: + add(Check("rate limit", PASS, f"{per_s:g} req/s, burst {burst if burst is not None else '?'}; need about {need_rate:g} req/s")) + else: + verdict = WARN if need_rate > DEFAULT_RATE_PER_S or need_burst > DEFAULT_RATE_BURST else SKIP + add(Check("rate limit", verdict, f"not checked ({lim_why or 'GET /v1/limits has no rate_limit'}); {desktops} desktops need about " + f"{need_rate:g} req/s, burst {need_burst:g}. The gateway's default is {DEFAULT_RATE_PER_S} req/s, burst {DEFAULT_RATE_BURST}", + "confirm the per-key limit of your host with support before a large run" if verdict == WARN else None)) + + # quota + if lim and lim["max_live"] is not None: + in_use = lim["live"] or 0.0 + if lim["max_live"] - in_use >= desktops: + add(Check("quota", PASS, f"{int(lim['max_live'] - in_use)} more live desktops allowed (limit {int(lim['max_live'])}, {int(in_use)} live)")) + else: + add(Check("quota", FAIL, f"the workspace may run {int(lim['max_live'])} live desktops ({int(in_use)} live now); {desktops} needed", + "ask for a higher live-desktop quota", links.page("usage", "Usage"))) + else: + add(Check("quota", SKIP, f"not checked ({lim_why or 'GET /v1/limits has no max_live_computers'})", + "see the workspace's live-desktop limit", links.page("usage", "Usage"))) + + # spend + price = desktop_hour_usd if desktop_hour_usd is not None else (lim["desktop_hour_usd"] if lim else None) + if price is not None: + projected = desktops * hours * float(price) + need["desktop_hour_usd"] = float(price) + need["projected_desktop_usd"] = round(projected, 2) + if lim and lim["cap_set"]: + cap_usd, spent = lim["max_spend"], lim["spent"] or 0.0 + if cap_usd is None: + add(Check("spend cap", PASS, f"no cap; projected {projected:,.2f} USD ({desktops} x {hours:g} h x {price:g} USD)")) + elif spent + projected > cap_usd: + add(Check("spend cap", FAIL, f"{spent:,.2f} spent + {projected:,.2f} projected > cap {cap_usd:,.2f} USD " + f"({desktops} x {hours:g} h x {price:g} USD per desktop-hour)", + f"raise the cap to at least {spent + projected:,.0f} USD or run fewer desktop-hours", links.page("usage", "Usage"))) + else: + add(Check("spend cap", PASS, f"{spent:,.2f} spent + {projected:,.2f} projected <= cap {cap_usd:,.2f} USD")) + else: + add(Check("spend cap", SKIP, f"projected {projected:,.2f} USD ({desktops} x {hours:g} h x {price:g} USD); the cap was not checked " + f"({lim_why or 'GET /v1/limits has no max_spend_usd'})", + f"make sure the spend cap leaves at least {projected:,.0f} USD", links.page("usage", "Usage"))) + else: + add(Check("spend cap", SKIP, f"not checked: no desktop-hour price ({lim_why or 'GET /v1/limits has no price'}); " + f"{desktops * hours:g} desktop-hours", "pass --desktop-hour-usd to project the cost", links.page("usage", "Usage"))) + + # task set + if task_set: + add(_task_set_check(client, task_set, lim, links, timeout)) + + return _finish(desktops, hours, checks, need, gpus, gpu_hour_usd, check_console, console, timeout) + + +def _task_set_check(client, task_set: str, lim, links, timeout) -> Check: + try: + if _UUID.match(task_set): + ts = client.call("GET", f"/v1/task-sets/{urllib.parse.quote(task_set)}", timeout=timeout) + else: + items = (client.call("GET", "/v1/task-sets?limit=200", timeout=timeout) or {}).get("items") or [] + hit = [t for t in items if t.get("name") == task_set] + if not hit: + return Check("task set", FAIL, f"no task set named {task_set!r} in this workspace", + "register it (drums_tasks register, or Tasks > New in the console)", links.page("tasks", "Tasks")) + ts = hit[0] + except NotFound: + return Check("task set", FAIL, f"task set {task_set} is not registered for this workspace", + "register it (drums_tasks register, or Tasks > New in the console)", links.page("tasks", "Tasks")) + except DrumsError as e: + return Check("task set", SKIP, f"could not read task set {task_set}: {e.message if e.status else e}") + tid = ts.get("id") or task_set + link = links.page(f"tasks/{tid}", "Tasks") + v = ts.get("validation") if isinstance(ts.get("validation"), dict) else None + name = f"{ts.get('name') or tid} ({ts.get('task_count', '?')} tasks)" + if not v: + return Check("task set", FAIL, f"{name} is registered but was never validated on a host", + "run the validator on the host (oracle 1.0 and every near miss 0.0) and register the result", link) + acc, rej = int(v.get("accepted") or 0), int(v.get("rejected") or 0) + host = v.get("host_id") + where = f"on {host}" if host else "on a host" + if acc == 0 or rej > 0: + return Check("task set", FAIL, f"{name}: validation {where} accepted {acc}, rejected {rej}", + "fix or drop the rejected tasks and validate again", link) + bound = lim.get("host_id") if lim else None + if bound and host and bound != host: + return Check("task set", WARN, f"{name}: validated on {host}, but this workspace is bound to {bound}", + "validate it on the bound host", link) + return Check("task set", PASS, f"{name}: validated {where} at {v.get('at', '?')}, accepted {acc}, rejected 0" + + ("" if bound else " (the bound host could not be compared)")) + + +def _finish(desktops, hours, checks, need, gpus, gpu_hour_usd, check_console, console, timeout, auth_unknown=False) -> PreflightReport: + if check_console: + ok, detail, fix = _console_up(console, min(timeout, 10.0)) + checks.insert(1 if checks else 0, Check("console", PASS if ok else FAIL, f"{console}: {detail}", None if ok else fix, + None if ok else console)) + if auth_unknown: + checks.append(Check("auth", SKIP, "not checked: the gateway did not answer")) + if gpus: + split = f"{gpus['inference']} inference + {gpus['trainer']} trainer" if gpus["inference"] is not None else f"{gpus['total']}" + detail = f"{split} GPUs for the model (not billed by Drums)" + if gpu_hour_usd is not None: + gpu_usd = gpus["total"] * hours * float(gpu_hour_usd) + need["projected_gpu_usd"] = round(gpu_usd, 2) + detail += f"; projected {gpu_usd:,.2f} USD at {gpu_hour_usd:g} USD per GPU-hour" + detail += (". In D1 (Qwen3.5-27B, 2 inference + 6 trainer GPUs) the trainer kept 36 to 47 of 128 desktops in " + "flight; size N to what your trainer keeps in flight") + checks.append(Check("model GPUs", INFO, detail)) + return PreflightReport(desktops, hours, checks, need) + + +def _epoch(iso: str) -> float | None: + import datetime as dt # noqa: PLC0415 + try: + s = iso.replace("Z", "+00:00") + d = dt.datetime.fromisoformat(s) + if d.tzinfo is None: + d = d.replace(tzinfo=dt.timezone.utc) + return d.timestamp() + except (ValueError, AttributeError): + return None + + +def add_arguments(ap): + ap.add_argument("--desktops", type=int, required=True, metavar="N", help="desktops the run keeps live") + ap.add_argument("--hours", type=float, required=True, metavar="H", help="how long the run lasts") + ap.add_argument("--model-gpus", metavar="G", help="GPUs for the model: a count (8) or inference:trainer (2:6)") + ap.add_argument("--task-set", metavar="ID", help="a registered task set (id or name) the run uses") + ap.add_argument("--desktop-hour-usd", type=float, metavar="USD", help="desktop-hour price for the projection (default: the gateway's)") + ap.add_argument("--gpu-hour-usd", type=float, metavar="USD", help="with --model-gpus: GPU-hour price, for the total projection") + ap.add_argument("--url", metavar="URL", help="the gateway URL (default $DRUMS_URL)") + ap.add_argument("--console-url", metavar="URL", help=f"the console (default $DRUMS_CONSOLE_URL or {DEFAULT_CONSOLE_URL})") + ap.add_argument("--workspace", metavar="SLUG", help="the console workspace slug, for links (default $DRUMS_WORKSPACE)") + ap.add_argument("--no-console", action="store_true", help="do not check that the console answers") + ap.add_argument("--strict", action="store_true", help="exit 1 also on a warning or a check that could not run") + ap.add_argument("--json", action="store_true", help="print the report as JSON") + ap.add_argument("--allow-insecure-http", action="store_true", help="allow the token over plain http to a non-loopback host") + return ap + + +def run(a, out, err) -> int: + from . import Client # noqa: PLC0415 + from .errors import InsecureTransport # noqa: PLC0415 + from .mcp import read_token # noqa: PLC0415 + + url = (a.url or os.environ.get("DRUMS_URL") or "").strip() + token = read_token() + if not url: + err.write("drums preflight: no gateway URL: pass --url or set DRUMS_URL (Drums Cloud: https:///gw)\n") + return 2 + if not token: + err.write("drums preflight: no key: set DRUMS_TOKEN (or DRUMS_TOKEN_FILE)\n") + return 2 + if a.desktops < 1 or a.hours <= 0: + err.write("drums preflight: --desktops must be at least 1 and --hours above 0\n") + return 2 + try: + parse_model_gpus(a.model_gpus) + client = Client(url, token, timeout=20.0, retry_s=3.0, allow_insecure_http=True if a.allow_insecure_http else None) + except ValueError as e: + err.write(f"drums preflight: {e}\n") + return 2 + except InsecureTransport as e: + err.write(f"drums preflight: {e}\n") + return 2 + rep = preflight(a.desktops, a.hours, model_gpus=a.model_gpus, task_set=a.task_set, client=client, console_url=a.console_url, + workspace=a.workspace, desktop_hour_usd=a.desktop_hour_usd, gpu_hour_usd=a.gpu_hour_usd, + check_console=not a.no_console) + out.write(json.dumps(rep.to_dict(), indent=2) + "\n" if a.json else rep.table()) + return rep.exit_code(a.strict) diff --git a/drums/cli.py b/drums/cli.py index 1d18bc0..efcb0e0 100644 --- a/drums/cli.py +++ b/drums/cli.py @@ -2,6 +2,7 @@ drums mcp [--env ID] [--url URL] [--create] [--task FILE] # the MCP server on stdio (== python -m drums.mcp) drums mcp --print-config claude|claude-local|cursor|codex # a client config snippet (placeholders, never a key) + drums preflight --desktops N --hours H [--model-gpus 2:6] [--task-set ID] [--json] # can this run start? (exit 1: no) drums version """ from __future__ import annotations @@ -45,6 +46,8 @@ def build_parser() -> argparse.ArgumentParser: ap = argparse.ArgumentParser(prog="drums", description="Drums: isolated desktops for computer-use agents.") sub = ap.add_subparsers(dest="command", metavar="command") add_arguments(sub.add_parser("mcp", help="run the MCP server on stdio (or print a client config with --print-config)")) + from ._preflight import add_arguments as preflight_arguments + preflight_arguments(sub.add_parser("preflight", help="check that the gateway, key and workspace can carry N desktops for H hours")) sub.add_parser("version", help="print the SDK version") return ap @@ -55,6 +58,9 @@ def main(argv=None) -> int: if a.command == "version": sys.stdout.write(f"drums {__version__}\n") return 0 + if a.command == "preflight": + from ._preflight import run as preflight_run + return preflight_run(a, sys.stdout, sys.stderr) if a.command == "mcp": from .mcp import run return run(a) diff --git a/tests/test_preflight.py b/tests/test_preflight.py new file mode 100644 index 0000000..b0a7e91 --- /dev/null +++ b/tests/test_preflight.py @@ -0,0 +1,306 @@ +"""`drums preflight` / drums.preflight against a localhost fake gateway: today's gateway (no GET /v1/limits: those rows +say "not checked" and never fail), a gateway with the limits summary (every row decided), each failure with its fix +and link, the exit codes, --json, and no token in any output.""" +import contextlib +import datetime as dt +import io +import json +import os +import sys +import threading +import time +import unittest +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer + +sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) +import drums # noqa: E402 +from drums import _preflight as pf, cli # noqa: E402 + +PREFIX = "abcdefghijkl" +TOKEN = f"drums_sk_{PREFIX}_" + "Q" * 43 +TS = "0b5c2a52-77a0-4c0e-9a39-0a7a0f3c1d10" + + +def iso(hours_from_now): + return (dt.datetime.now(dt.timezone.utc) + dt.timedelta(hours=hours_from_now)).isoformat() + + +class Gw(BaseHTTPRequestHandler): + state = {} + + def log_message(self, *a): + pass + + def _reply(self, code, obj): + b = json.dumps(obj).encode() + self.send_response(code) + self.send_header("Content-Length", str(len(b))) + self.end_headers() + self.wfile.write(b) + + def do_GET(self): + st = Gw.state + if self.path == "/sign-in": + return self._reply(st.get("console", 200), {}) + if self.headers.get("Authorization") != f"Bearer {TOKEN}": + return self._reply(401, {"error": "missing, invalid, revoked or expired API key", "code": "unauthenticated"}) + if st.get("binding"): + return self._reply(403, {"error": "this endpoint serves another compute pool", "code": "forbidden"}) + if self.path == "/v1/health": + time.sleep(st.get("health_delay", 0)) + return self._reply(200, {"ok": st.get("ok", True), "version": "0.4.3", "checkpoints": st.get("checkpoints", [{"id": "osworld-ubuntu", "stale_reason": None}]), + "capacity": {"live": st.get("live", 10), "max": st.get("max", 200)}}) + if self.path == "/v1/api-keys": + row = {"id": "k1", "name": "trainer", "prefix": PREFIX, "scopes": st.get("scopes", ["computers:read", "computers:write", "runs:write", "usage:read"]), + "expires_at": st.get("expires_at"), "revoked_at": None} + return self._reply(200, {"items": [{"id": "k0", "prefix": "zzzzzzzzzzzz", "scopes": []}, row]}) + if self.path == "/v1/limits": + if "limits" not in st: + return self._reply(404, {"error": "no such route", "code": "not_found"}) + return self._reply(200, st["limits"]) + if self.path.startswith("/v1/task-sets/"): + if self.path.endswith(TS) and "task_set" in st: + return self._reply(200, st["task_set"]) + return self._reply(404, {"error": "no such task set", "code": "not_found"}) + if self.path.startswith("/v1/task-sets"): + return self._reply(200, {"items": [st["task_set"]] if "task_set" in st else [], "next_cursor": None}) + return self._reply(404, {"error": "no such route", "code": "not_found"}) + + +LIMITS = {"workspace": {"max_live_computers": 256, "live_computers": 10, "max_spend_usd": 500, "spent_usd": 40}, + "rate_limit": {"per_s": 600, "burst": 1800}, "price": {"desktop_hour_usd": 0.1}, "host": {"id": "drums-prod-2", "status": "active"}} +VALID_SET = {"id": TS, "name": "factory-v1", "task_count": 48, "validation": {"host_id": "drums-prod-2", "accepted": 48, "rejected": 0, "at": "2026-11-16T05:00:00Z"}} + + +class Preflight(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.srv = ThreadingHTTPServer(("127.0.0.1", 0), Gw) + threading.Thread(target=cls.srv.serve_forever, daemon=True).start() + cls.url = f"http://127.0.0.1:{cls.srv.server_address[1]}" + + @classmethod + def tearDownClass(cls): + cls.srv.shutdown() + + def setUp(self): + Gw.state = {} + + def pre(self, n=128, h=3.0, **kw): + kw.setdefault("check_console", False) + return drums.preflight(n, h, url=self.url, token=kw.pop("token", TOKEN), **kw) + + def row(self, rep, name): + hits = [c for c in rep.checks if c.name == name] + self.assertEqual(len(hits), 1, [c.name for c in rep.checks]) + return hits[0] + + def test_todays_gateway_passes_what_it_can_and_says_what_it_could_not_check(self): + rep = self.pre(16, 2.0) + self.assertTrue(rep.ok, rep.table()) + for name in ("gateway", "auth", "scopes", "host capacity", "admission"): + self.assertEqual(self.row(rep, name).status, "pass", name) + for name in ("rate limit", "quota", "spend cap"): + c = self.row(rep, name) + self.assertEqual(c.status, "not checked", name) + self.assertIn("does not publish GET /v1/limits", c.detail) + self.assertEqual(rep.exit_code(), 0) + self.assertEqual(rep.exit_code(strict=True), 1) + self.assertIn("3 could not be checked", rep.table()) + + def test_the_rate_rule_is_explicit(self): + self.assertEqual(pf.REQUESTS_PER_DESKTOP_S, 0.5) + rep = self.pre(128, 1.0) + self.assertEqual(rep.need["requests_per_s"], 64.0) + c = self.row(rep, "rate limit") + self.assertEqual(c.status, "warn") # 64 req/s > the 20/s default and nobody can tell us the real one + self.assertIn("64 req/s", c.detail) + self.assertIn("20 req/s", c.detail) + + def test_with_the_limits_summary_every_row_is_decided(self): + Gw.state = {"limits": LIMITS, "task_set": VALID_SET} + rep = self.pre(128, 3.0, task_set=TS) + self.assertTrue(rep.ok, rep.table()) + self.assertEqual({c.status for c in rep.checks}, {"pass"}, rep.table()) + self.assertEqual(rep.need["projected_desktop_usd"], 38.4) + self.assertEqual(rep.exit_code(strict=True), 0) + + def test_each_limit_fails_with_its_fix_and_link(self): + Gw.state = {"limits": {**LIMITS, "rate_limit": {"per_s": 20, "burst": 60}, "workspace": {"max_live_computers": 10, "live_computers": 0, + "max_spend_usd": 100, "spent_usd": 90}}} + rep = self.pre(128, 3.0, workspace="acme", console_url="https://console.example") + self.assertFalse(rep.ok) + for name, words in (("rate limit", "64 req/s"), ("quota", "10 live desktops"), ("spend cap", "cap 100.00")): + c = self.row(rep, name) + self.assertEqual(c.status, "fail", name) + self.assertIn(words, c.detail) + self.assertTrue(c.fix) + self.assertEqual(c.link, "https://console.example/w/acme/usage") + self.assertEqual(rep.exit_code(), 1) + self.assertIn("To do:", rep.table()) + + def test_the_price_flag_projects_the_cost(self): + rep = self.pre(128, 3.0, desktop_hour_usd=0.25) + c = self.row(rep, "spend cap") + self.assertEqual(c.status, "not checked") + self.assertIn("96.00 USD", c.detail) + self.assertEqual(rep.need["projected_desktop_usd"], 96.0) + + def test_host_capacity_and_admission(self): + Gw.state = {"live": 150, "max": 200} + self.assertEqual(self.row(self.pre(128), "host capacity").status, "fail") + Gw.state = {"ok": False} + self.assertEqual(self.row(self.pre(8), "admission").status, "fail") + Gw.state = {"checkpoints": [{"id": "osworld-ubuntu", "stale_reason": "runtime changed"}]} + self.assertEqual(self.row(self.pre(8), "admission").status, "warn") + Gw.state = {"limits": {**LIMITS, "host": {"id": "drums-prod-2", "status": "draining"}}} + self.assertEqual(self.row(self.pre(8), "admission").status, "fail") + + def test_key_expiring_during_the_run_or_missing_scopes(self): + Gw.state = {"expires_at": iso(1)} + c = self.row(self.pre(8, 3.0), "auth") + self.assertEqual(c.status, "fail") + self.assertIn("expires in", c.detail) + Gw.state = {"expires_at": iso(100)} + self.assertEqual(self.row(self.pre(8, 3.0), "auth").status, "pass") + Gw.state = {"scopes": ["computers:read"]} + self.assertEqual(self.row(self.pre(8), "scopes").status, "fail") + Gw.state = {"scopes": ["computers:read", "computers:write"]} + c = self.row(self.pre(8), "scopes") + self.assertEqual(c.status, "warn") + self.assertIn("runs:write", c.detail) + + def test_a_wrong_key_or_another_workspace(self): + rep = self.pre(8, token=f"drums_sk_{PREFIX}_" + "X" * 43) + self.assertEqual(self.row(rep, "auth").status, "fail") + self.assertFalse(rep.ok) + Gw.state = {"binding": True} + c = self.row(self.pre(8), "auth") + self.assertEqual(c.status, "fail") + self.assertIn("another compute pool", c.detail) + + def test_unreachable_gateway(self): + rep = drums.preflight(8, 1.0, url="http://127.0.0.1:9", token=TOKEN, check_console=False) + self.assertEqual(self.row(rep, "gateway").status, "fail") + self.assertEqual(self.row(rep, "auth").status, "not checked") + + def test_slow_control_plane_warns(self): + Gw.state = {"health_delay": 0.3} + old = pf.HEALTH_SLOW_S + pf.HEALTH_SLOW_S = 0.1 + try: + self.assertEqual(self.row(self.pre(8), "gateway").status, "warn") + finally: + pf.HEALTH_SLOW_S = old + + def test_task_set(self): + self.assertEqual(self.row(self.pre(8, task_set=TS), "task set").status, "fail") # not registered + Gw.state = {"task_set": {**VALID_SET, "validation": None}} + c = self.row(self.pre(8, task_set=TS), "task set") + self.assertEqual(c.status, "fail") + self.assertIn("never validated", c.detail) + Gw.state = {"task_set": {**VALID_SET, "validation": {**VALID_SET["validation"], "rejected": 2}}} + self.assertEqual(self.row(self.pre(8, task_set=TS), "task set").status, "fail") + Gw.state = {"task_set": VALID_SET} + c = self.row(self.pre(8, task_set="factory-v1"), "task set") # by name + self.assertEqual(c.status, "pass") + self.assertIn("could not be compared", c.detail) + Gw.state = {"task_set": VALID_SET, "limits": {**LIMITS, "host": {"id": "drums-rc1", "status": "active"}}} + self.assertEqual(self.row(self.pre(8, task_set=TS), "task set").status, "warn") + + def test_console_row(self): + rep = self.pre(8, check_console=True, console_url=self.url) + self.assertEqual(self.row(rep, "console").status, "pass") + Gw.state = {"console": 502} + self.assertEqual(self.row(self.pre(8, check_console=True, console_url=self.url), "console").status, "fail") + + def test_model_gpus(self): + self.assertEqual(pf.parse_model_gpus("2:6"), {"inference": 2, "trainer": 6, "total": 8}) + self.assertEqual(pf.parse_model_gpus("8")["total"], 8) + with self.assertRaises(ValueError): + pf.parse_model_gpus("lots") + rep = self.pre(128, 2.0, model_gpus="2:6", gpu_hour_usd=4.0) + c = self.row(rep, "model GPUs") + self.assertEqual(c.status, "info") + self.assertEqual(rep.need["projected_gpu_usd"], 64.0) + + def test_bad_arguments(self): + with self.assertRaises(ValueError): + drums.preflight(0, 1.0, url=self.url, token=TOKEN) + with self.assertRaises(ValueError): + drums.preflight(8, 0, url=self.url, token=TOKEN) + + +def cli_run(argv, env): + out, err = io.StringIO(), io.StringIO() + keys = ("DRUMS_TOKEN", "DRUMS_URL", "DRUMS_TOKEN_FILE", "DRUMS_CONSOLE_URL", "DRUMS_WORKSPACE") + saved = {k: os.environ.get(k) for k in keys} + for k in keys: + os.environ.pop(k, None) + os.environ.update(env) + try: + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + rc = cli.main(argv) + finally: + for k, v in saved.items(): + if v is None: + os.environ.pop(k, None) + else: + os.environ[k] = v + return rc, out.getvalue(), err.getvalue() + + +class PreflightCli(Preflight.__base__): + @classmethod + def setUpClass(cls): + Preflight.setUpClass.__func__(cls) + + @classmethod + def tearDownClass(cls): + cls.srv.shutdown() + + def setUp(self): + Gw.state = {} + + def test_table_and_exit_codes(self): + env = {"DRUMS_URL": self.url, "DRUMS_TOKEN": TOKEN} + rc, out, err = cli_run(["preflight", "--desktops", "16", "--hours", "2", "--no-console"], env) + self.assertEqual(rc, 0, out + err) + self.assertIn("CHECK", out) + self.assertIn("NOT CHECKED", out) + rc, _, _ = cli_run(["preflight", "--desktops", "16", "--hours", "2", "--no-console", "--strict"], env) + self.assertEqual(rc, 1) + Gw.state = {"live": 199} + rc, out, _ = cli_run(["preflight", "--desktops", "16", "--hours", "2", "--no-console"], env) + self.assertEqual(rc, 1) + self.assertIn("FAIL", out) + self.assertNotIn(TOKEN, out) + self.assertNotIn("Q" * 8, out) + + def test_json(self): + Gw.state = {"limits": LIMITS} + rc, out, _ = cli_run(["preflight", "--desktops", "128", "--hours", "3", "--no-console", "--json", "--model-gpus", "2:6"], + {"DRUMS_URL": self.url, "DRUMS_TOKEN": TOKEN}) + self.assertEqual(rc, 0) + d = json.loads(out) + self.assertTrue(d["ok"]) + self.assertEqual(d["need"]["requests_per_s"], 64.0) + self.assertEqual([c["name"] for c in d["checks"]][:2], ["gateway", "auth"]) + self.assertNotIn(TOKEN, out) + + def test_usage_errors_exit_2(self): + rc, _, err = cli_run(["preflight", "--desktops", "8", "--hours", "1"], {"DRUMS_TOKEN": TOKEN}) + self.assertEqual(rc, 2) + self.assertIn("DRUMS_URL", err) + rc, _, err = cli_run(["preflight", "--desktops", "8", "--hours", "1"], {"DRUMS_URL": self.url}) + self.assertEqual(rc, 2) + self.assertIn("DRUMS_TOKEN", err) + rc, _, err = cli_run(["preflight", "--desktops", "8", "--hours", "1", "--model-gpus", "x"], {"DRUMS_URL": self.url, "DRUMS_TOKEN": TOKEN}) + self.assertEqual(rc, 2) + rc, _, err = cli_run(["preflight", "--desktops", "8", "--hours", "1"], {"DRUMS_URL": "http://gw.example/gw", "DRUMS_TOKEN": TOKEN}) + self.assertEqual(rc, 2) + self.assertNotIn(TOKEN, err) + + +if __name__ == "__main__": + unittest.main() From b84b047968802f5da628fb82b13ee997670bb458 Mon Sep 17 00:00:00 2001 From: Sahil Jagtap Date: Tue, 17 Nov 2026 19:05:34 -0500 Subject: [PATCH 2/4] preflight: export preflight and PreflightReport at the end of the module --- drums/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drums/__init__.py b/drums/__init__.py index ab8e9d0..0052d7d 100644 --- a/drums/__init__.py +++ b/drums/__init__.py @@ -55,7 +55,7 @@ __all__ = ["ActionFailed", "connect", "Client", "Env", "Reward", "Observation", "StepResult", "State", "ForkResult", "env", "__version__", "DrumsError", "AuthError", "BadRequest", "NotFound", "EnvGone", "Conflict", "DaemonUnavailable", "GuestError", "EnvFailed", "RestoreFailed", "GateFailed", "TaskSetupFailed", "ForkError", "BatchError", "InvalidEpisode", "CheckerError", "NotQuiescent", "Overloaded", - "CheckpointStale", "TaskUnsupported", "InsecureTransport", "RedirectRefused", "check_transport", "preflight", "PreflightReport"] + "CheckpointStale", "TaskUnsupported", "InsecureTransport", "RedirectRefused", "check_transport"] _STATUS = {400: BadRequest, 401: AuthError, 404: NotFound, 409: Conflict, 410: EnvGone, 422: BadRequest, 429: Overloaded, 502: GuestError, 503: DaemonUnavailable} @@ -881,3 +881,5 @@ def env(task: dict | None = None, *, from_state: str | None = None, checkpoint: from ._preflight import PreflightReport, preflight # noqa: E402 (needs Client defined above) + +__all__ += ["preflight", "PreflightReport"] From d372421307ff60046c92bae7321eb19c2104472f Mon Sep 17 00:00:00 2001 From: Sahil Jagtap Date: Tue, 17 Nov 2026 19:08:22 -0500 Subject: [PATCH 3/4] preflight: a platform task set without a validation record is a warning; README section --- README.md | 31 +++++++++++++++++++++++++++++++ drums/_preflight.py | 3 +++ tests/test_preflight.py | 2 ++ 3 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 256a36b..2b393c6 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,37 @@ Use a score only when `valid` is `True`. With the default `strict=True`, `verify for an invalid episode instead of returning a reward. A `401` raises `drums.AuthError`: the key is mistyped, revoked or expired. The full walkthrough is the [docs quickstart](https://docs.drums.run/quickstart). +## Preflight: can this run start? + +Before you pay for GPUs, check that the gateway, your key and your workspace can carry the run: + +```bash +drums preflight --desktops 128 --hours 3 --task-set [--model-gpus 2:6] [--json] +``` + +```python +report = drums.preflight(desktops=128, hours=3, task_set=task_set_id) +print(report.table()); assert report.ok +``` + +| Check | Passes when | +|---|---| +| gateway | `GET /v1/health` answers, in under 2 s (over 20 s fails: the control plane is overloaded) | +| console | the console answers | +| auth | the key is accepted, not revoked, and does not expire before the run ends | +| scopes | `computers:read` and `computers:write` (required); `runs:write` and `usage:read` (warned) | +| host capacity, admission | the bound host has N free desktops, drumsd is ok and no image checkpoint is stale | +| rate limit | the per-key limit is at least N x 0.5 requests/s with a burst of N (the D1 rule: about 0.5 requests per second per active desktop, `drums._preflight.REQUESTS_PER_DESKTOP_S`) | +| quota | the workspace's live-desktop limit leaves room for N | +| spend cap | spent + N x H x the desktop-hour price stays under the cap | +| task set | registered for the workspace and validated on a host (oracle accepted, none rejected) | + +Every row that is not a pass says how to fix it and links the console page (`--workspace ` or `DRUMS_WORKSPACE` +fills in the workspace). The exit status is 1 when a check failed, 0 otherwise; `--strict` also fails on a warning or +on a row that could not be checked. Rate limit, quota and spend cap need the gateway's `GET /v1/limits` summary; a +gateway without it says so on those rows ("not checked") instead of guessing, and `--desktop-hour-usd` supplies the +price for the cost projection. Preflight only reads; it creates nothing. + ## How it works ```mermaid diff --git a/drums/_preflight.py b/drums/_preflight.py index eee6633..2a65210 100644 --- a/drums/_preflight.py +++ b/drums/_preflight.py @@ -419,6 +419,9 @@ def _task_set_check(client, task_set: str, lim, links, timeout) -> Check: link = links.page(f"tasks/{tid}", "Tasks") v = ts.get("validation") if isinstance(ts.get("validation"), dict) else None name = f"{ts.get('name') or tid} ({ts.get('task_count', '?')} tasks)" + if not v and ts.get("platform"): + return Check("task set", WARN, f"{name} is a platform set with no validation record for this host", + "validate the tasks you train on (oracle 1.0, near misses 0.0) before a long run", link) if not v: return Check("task set", FAIL, f"{name} is registered but was never validated on a host", "run the validator on the host (oracle 1.0 and every near miss 0.0) and register the result", link) diff --git a/tests/test_preflight.py b/tests/test_preflight.py index b0a7e91..4577a32 100644 --- a/tests/test_preflight.py +++ b/tests/test_preflight.py @@ -199,6 +199,8 @@ def test_task_set(self): c = self.row(self.pre(8, task_set=TS), "task set") self.assertEqual(c.status, "fail") self.assertIn("never validated", c.detail) + Gw.state = {"task_set": {**VALID_SET, "validation": None, "platform": True}} + self.assertEqual(self.row(self.pre(8, task_set=TS), "task set").status, "warn") # e.g. the OSWorld set Gw.state = {"task_set": {**VALID_SET, "validation": {**VALID_SET["validation"], "rejected": 2}}} self.assertEqual(self.row(self.pre(8, task_set=TS), "task set").status, "fail") Gw.state = {"task_set": VALID_SET} From 2eeea57b7d2c8ce5ce5676a74a703e5e979ca84e Mon Sep 17 00:00:00 2001 From: Sahil Jagtap Date: Tue, 17 Nov 2026 19:38:48 -0500 Subject: [PATCH 4/4] ci: run the test matrix on ubuntu-24.04 (ubuntu-latest has no Python 3.9; the same line as #10) --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69b32d1..ef8ea0d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,8 @@ permissions: jobs: test: - runs-on: ubuntu-latest + # ubuntu-latest (26.04) has no Python 3.9 build; 24.04 still has every version the SDK supports. + runs-on: ubuntu-24.04 strategy: fail-fast: false matrix: