Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

5.6 KB
Raw Blame
#!/usr/bin/env python3
"""Generate code constants from the image sources of truth.

Writes ``libs/cua-bench/cua_bench/images.py`` (cua-bench is a published
package, so it cannot read repo files at run time):

- ``CANONICAL``: the default image per OS, from the constants of
  ``libs/cua/crates/cua-image/src/canonical.rs`` (which the docs generator
  already checks against ``libs/images/sandbox-images.json``);
- ``BENCH_<NAME>``: each benchmark image of the catalog, pinned by digest to
  the rootfs index its ``lock`` file records (``libs/images/bench/<id>/lock.json``,
  written by ``scripts/bench-images``).

    gen-image-constants.py [--check]

``--check`` exits 1 when the checked-in file differs (the drift gate CI runs).
Python 3 standard library only.
"""

from __future__ import annotations

import argparse
import json
import os
import re
import sys

ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".."))
CATALOG = "libs/images/sandbox-images.json"
CANONICAL_RS = "libs/cua/crates/cua-image/src/canonical.rs"
OUT = "libs/cua-bench/cua_bench/images.py"


def read(rel: str) -> str:
    with open(os.path.join(ROOT, rel), encoding="utf-8") as fh:
        return fh.read()


def rust_const(src: str, name: str) -> str:
    m = re.search(rf'^pub const {name}: &str = "([^"]+)";', src, re.M)
    if not m:
        raise SystemExit(f"{CANONICAL_RS}: no pub const {name}")
    return m.group(1)


def canonical_defaults() -> dict[str, str]:
    src = read(CANONICAL_RS)
    return {
        os_: f"{rust_const(src, f'{os_.upper()}_REPO')}:{rust_const(src, f'{os_.upper()}_DEFAULT_VERSION')}"
        for os_ in ("linux", "windows", "macos")
    }


def bench_pins() -> list[dict[str, str]]:
    catalog = json.loads(read(CATALOG))
    out = []
    for image in catalog["images"]:
        if image["group"] != "benchmark" or image["variant"] != "container":
            continue
        lock = json.loads(read(image["lock"]))
        repo = image["ref"].split(":", 1)[0]
        if lock["repository"] != repo:
            raise SystemExit(f"{image['lock']} is for {lock['repository']}, not {repo}")
        index = lock["index"]
        name = repo.rsplit("/", 1)[1].upper().replace("-", "_")
        out.append(
            {
                "name": name,
                "pinned": f"{repo}@{index['digest']}",
                "stamped": index["ref"],
                "floating": image["ref"],
                "promoted": "yes" if lock.get("promoted") else "not yet",
                "title": image["name"],
                "summary": image["summary"],
                "guide": image.get("guide", ""),
                "lock": image["lock"],
            }
        )
    return out


def render() -> str:
    canonical = canonical_defaults()
    pins = bench_pins()
    lines = [
        "# Generated by scripts/images/gen-image-constants.py from libs/images/sandbox-images.json,",
        "# the bench lock files and cua-image's canonical.rs. Do not edit: change a source and",
        "# rerun `python3 scripts/images/gen-image-constants.py`.",
        '"""The images cua-bench runs: the canonical defaults and the benchmark images.',
        "",
        "Each benchmark's Linux guest ships as two indexes linked by",
        "``ai.cua.image.variants``: ``<ver>`` (rootfs: docker / gVisor) and",
        "``<ver>-disk`` (containerDisk: QEMU / KubeVirt). ``BENCH_<NAME>`` pins the",
        "rootfs index by digest (the one its lock file records); the resolver follows",
        "its link to the disk index when ``--kind vm`` needs it. Override one for a",
        "run with ``CUA_BENCH_IMAGE_<NAME>`` (e.g. ``CUA_BENCH_IMAGE_BENCH_WEB``) or",
        "``--image``.",
        '"""',
        "",
        "from __future__ import annotations",
        "",
        "import os",
        "",
        "#: The default image per OS (``cua_image::canonical``), used when the SDK",
        "#: cannot be loaded.",
        "CANONICAL = {",
        *[f'    "{k}": "{v}",' for k, v in canonical.items()],
        "}",
    ]
    for p in pins:
        lines += [
            "",
            f"#: {p['title']}: {p['summary']}",
            f"#: Guide: {p['guide']}. Lock: {p['lock']}.",
            f"#: Pinned to {p['stamped']}.",
            f"#: Floating {p['floating']} (promoted: {p['promoted']}).",
            f'{p["name"]} = "{p["pinned"]}"',
        ]
    names = [p["name"] for p in pins]
    lines += [
        "",
        "",
        "def image(name: str) -> str:",
        f'    """{" / ".join(f"``{n}``" for n in names)}, or its ``CUA_BENCH_IMAGE_<NAME>`` override."""',
        "    default = globals()[name]",
        '    return os.environ.get(f"CUA_BENCH_IMAGE_{name}", "").strip() or default',
        "",
        "",
        f"__all__ = {json.dumps(['CANONICAL', *sorted(names), 'image'])}",
    ]
    return "\n".join(lines) + "\n"


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        "--check", action="store_true", help="fail when the checked-in file drifted"
    )
    args = parser.parse_args(argv)
    want = render()
    path = os.path.join(ROOT, OUT)
    have = open(path, encoding="utf-8").read() if os.path.exists(path) else None
    if have == want:
        print(f"{OUT} is up to date")
        return 0
    if args.check:
        print(
            f"error: {OUT} is stale: run python3 scripts/images/gen-image-constants.py",
            file=sys.stderr,
        )
        return 1
    with open(path, "w", encoding="utf-8") as fh:
        fh.write(want)
    print(f"wrote {OUT}")
    return 0


if __name__ == "__main__":
    sys.exit(main())