Skip to content

The first exam for code agents in the physical world: frozen robot tasks from many benchmark suites, each answered with a program and verified in a separate sandbox. Runs on Harbor.

7.1 KB
Raw Blame
#!/usr/bin/env python3
"""game - your seat in the match (the same commands for every sport; the actions are the sport's, see `game spec`).

  game spec                  the sport: actions (types, fields, ranges), cameras, units, rules, timing
  game status                the current state, no images
  game start                 you are ready (preparation over); the match begins when every seat has started
  game wait [SECONDS]        turn-based: block until it is your turn; real time: let the world run SECONDS (<= 1)
  game observe [QUERY_JSON]  the state and the camera images (saved as PNG under /app/output/views/)
  game act ACTION_JSON       one action, e.g. game act '{"type": "throw", ...}' (if the sport shows the result
                             in images, they are saved like observe's, as reply_*.png)
  game ready "WHY" "PLAN"    your review (after each of your actions, turn-based; at each review, real time)
  game say "TEXT"            team formats: a message to your teammates (never shown to the other side)
  game team [SINCE]          team formats: your teammates' messages after seq SINCE (default 0); reading never
                             consumes them (observe / status show the recent ones and team_seq, the newest seq)
  game skill serve [PATH]    low level with your skill in the loop: serve your skill (default /app/output/skill) from
                             your container to the simulator (a background server; it reloads the skill whenever its
                             files change: the new version plays from the next call); run it before `game start`
  game skill status          your skill server: the loaded version (sha256), its functions and any load error
Sport shortcuts (generated from the sport's declaration):
@@SHORTCUTS@@"""
import base64
import json
import os
import sys
import tempfile
import urllib.error
import urllib.request

COMMANDS = json.loads('@@COMMANDS@@')       # shortcut -> [action type, field, field, ...]
VIEWS = "/app/output/views"
BASE = os.environ.get("SIM_URL", "http://sim:8000") + "/" + os.environ.get("SEAT", "A")


def call(ep, body=None):
    req = urllib.request.Request(BASE + "/" + ep, data=None if body is None else json.dumps(body).encode(),
                                 headers={"Content-Type": "application/json"})
    try:
        with urllib.request.urlopen(req, timeout=600) as r:
            return json.load(r)
    except urllib.error.HTTPError as e:
        try:
            out = json.load(e)
        except ValueError:
            out = {"error": str(e)}
        out["http_status"] = e.code
        return out


def save(b64, name):
    os.makedirs(VIEWS, exist_ok=True)
    fd, p = tempfile.mkstemp(dir=VIEWS, prefix=name + "_", suffix=".png")
    with os.fdopen(fd, "wb") as f:
        f.write(base64.b64decode(b64))
    return p


def show(o):
    ob = o.get("observation")
    if isinstance(ob, dict) and ob.get("image_png_b64"):
        ob["image_file"] = save(ob.pop("image_png_b64"), "reply" if ob.get("of") else "obs")
    for k in [k for k in o if k.endswith("_image_b64")]:
        if o[k]:
            o[k[:-4] + "_file"] = save(o.pop(k), k[:-10])
        else:
            o.pop(k)
    print(json.dumps(o, indent=1))


SKIP_DIRS = ("__pycache__", ".git", ".ipynb_checkpoints")


SKILL_PORT = int(os.environ.get("RCB_SKILL_PORT", "8765"))
SKILLKIT = os.environ.get("RCB_SKILLKIT", "/opt/skillkit/skillhost.py")


def skill_server(q, timeout=5.0):
    """One request to your own skill server (127.0.0.1:RCB_SKILL_PORT), or None if it does not answer."""
    import socket
    try:
        with socket.create_connection(("127.0.0.1", SKILL_PORT), timeout=timeout) as s, s.makefile("rwb") as f:
            f.write((json.dumps(q) + "\n").encode())
            f.flush()
            line = f.readline()
            return json.loads(line) if line else None
    except (OSError, ValueError):
        return None


def skill_serve(path):
    """Start the skill server in the background (once): the simulator calls it for every skill call of the match."""
    import subprocess
    import time
    path = os.path.abspath(path)
    up = skill_server({"op": "status"}, 1.0)
    if up is not None:
        return {"ok": True, "already_serving": True, **up}
    if not os.path.isfile(SKILLKIT):
        return {"ok": False, "error": f"no skill server kit at {SKILLKIT}"}
    log = open("/tmp/skill_server.log", "ab")
    subprocess.Popen([sys.executable, "-I", SKILLKIT, "serve", "--tcp", f"127.0.0.1:{SKILL_PORT}", "--skill", path,
                      "--live"], stdout=log, stderr=log, stdin=subprocess.DEVNULL, start_new_session=True)
    for _ in range(50):
        time.sleep(0.1)
        up = skill_server({"op": "status"}, 1.0)
        if up is not None:
            return {"ok": True, "serving": path, "port": SKILL_PORT, "log": "/tmp/skill_server.log", **up}
    return {"ok": False, "error": "the skill server did not start (see /tmp/skill_server.log)"}


def arg(name, x):
    """A shortcut's argument as the act body carries it: `entity` as written (an id), true / false as booleans, a
    number as a number, anything else (an enum value) as written; the core checks every field against the schema."""
    if name == "entity":
        return x
    if x in ("true", "false"):
        return x == "true"
    try:
        return float(x)
    except ValueError:
        return x


def main(a):
    turn = call("spec").get("protocol", "").startswith("eai-standard-turn") if a[:1] == ["wait"] else None
    if a == ["spec"]:
        show(call("spec"))
    elif a == ["status"]:
        show(call("status"))
    elif a == ["start"]:
        show(call("start", {}))
    elif a[:1] == ["wait"] and len(a) <= 2:
        if turn:
            while True:
                o = call("wait", {"timeout_s": 60})
                st = o.get("state") or {}
                if "error" in o or st.get("done") or st.get("your_turn"):
                    break
            show(o)
        else:
            show(call("wait", {"dt": float(a[1]) if len(a) == 2 else 0.1}))
    elif a[:1] == ["observe"] and len(a) <= 2:
        show(call("observe", {"query": json.loads(a[1])} if len(a) == 2 else {}))
    elif a[:1] == ["act"] and len(a) == 2:
        show(call("act", json.loads(a[1])))
    elif a[:1] == ["ready"] and len(a) == 3:
        show(call("ready", {"why": a[1], "plan": a[2]}))
    elif a[:1] == ["say"] and len(a) == 2:
        show(call("say", {"text": a[1]}))
    elif a[:1] == ["team"] and len(a) <= 2:
        show(call("team", {"since": int(a[1]) if len(a) == 2 else 0}))
    elif a[:2] == ["skill", "serve"] and len(a) <= 3:
        show(skill_serve(a[2] if len(a) == 3 else "/app/output/skill"))
    elif a == ["skill", "status"]:              # your own skill server (core 0.2.34; option c: the only skill host)
        show(skill_server({"op": "status"}) or {"ok": False, "error": "no skill server (game skill serve)"})
    elif a and a[0] in COMMANDS and len(a) == len(COMMANDS[a[0]]):
        kind, *names = COMMANDS[a[0]]
        show(call("act", {"type": kind, **{n: arg(n, x) for n, x in zip(names, a[1:])}}))
    else:
        sys.exit(__doc__)


if __name__ == "__main__":
    main(sys.argv[1:])