"""Language profiles (tools/eval/languages/*.json). A profile says how to invoke a toolchain and where its pinned local documentation is. It MUST NOT contain solution hints (protocol §5). Fields: name language id used in task directories and records display human name toolchain {"binaries": [...], "version": [argv, ...], "bind": [paths]} binaries are resolved on the host and bound read-only into the sandbox; "version" commands are recorded (§28) docs {"env": VAR, "default": path}: directory or file mounted at /docs (read-only) instructions text appended to the shared system prompt classify {"check": [prefix, ...], "build": [...], "test": [...]}: command prefixes counted as verifier/compiler and test runs for the repair metrics (§14, §15) """ import glob import os import shutil import subprocess from . import util HERE = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "languages") def available(): return sorted(os.path.splitext(os.path.basename(p))[0] for p in glob.glob(os.path.join(HERE, "*.json"))) def load(name): path = os.path.join(HERE, name + ".json") if not os.path.exists(path): raise SystemExit(f"unknown language profile {name!r}; available: {', '.join(available())}") prof = util.read_json(path) prof["profile_hash"] = util.sha256_path(path) return prof def expand(s): return os.path.expandvars(os.path.expanduser(s.replace("${REPO}", util.REPO))) def resolve_binaries(prof): """Host paths of the toolchain binaries and the directories to bind.""" found, binds = {}, set() cmd = prof["toolchain"].get("bind_command") if cmd: r = subprocess.run(cmd, capture_output=True, text=True) if r.returncode != 0 or not os.path.isdir(r.stdout.strip()): raise SystemExit(f"{prof['name']}: {' '.join(cmd)} did not print a directory") binds.add(os.path.realpath(r.stdout.strip())) for b in prof["toolchain"].get("binaries", []): path = shutil.which(expand(b)) if os.sep not in b else expand(b) if path is None or not os.path.exists(path): raise SystemExit(f"{prof['name']}: toolchain binary {b!r} not found") real = os.path.realpath(path) found[os.path.basename(b)] = real binds.add(os.path.dirname(real)) for extra in prof["toolchain"].get("bind", []): for p in glob.glob(expand(extra)): binds.add(os.path.realpath(p)) # A rustc sysroot or a zig installation is self-contained: bind the # directory above `bin` so libraries and the standard library resolve. out = set() for d in binds: out.add(os.path.dirname(d) if os.path.basename(d) == "bin" else d) return found, sorted(out) def sandbox_path(binds): """PATH inside the sandbox: `/bin` where it exists, else ``.""" return [os.path.join(d, "bin") if os.path.isdir(os.path.join(d, "bin")) else d for d in binds] def versions(prof, env=None): out = [] for argv in prof["toolchain"].get("version", []): try: r = subprocess.run([expand(a) for a in argv], capture_output=True, text=True, timeout=60, env=env) out.append({"argv": argv, "output": (r.stdout + r.stderr).strip()}) except OSError as e: out.append({"argv": argv, "error": str(e)}) return out def docs_mounts(prof): """Documentation mounted under /docs, as [(name, host path)]; name "" means the directory is /docs itself. The environment variable wins; otherwise `default`, or `from_toolchain` (paths relative to the bound toolchain directory, so the documentation always matches the compiler being run; each is mounted as /docs/).""" d = prof.get("docs") or {} path = os.environ.get(d.get("env", ""), "") or (expand(d["default"]) if d.get("default") else "") if path: if not os.path.exists(path): return [] return [("", path)] if os.path.isdir(path) else [(os.path.basename(path), path)] rels = d.get("from_toolchain") or [] if isinstance(rels, str): rels = [rels] if not rels: return [] _, binds = resolve_binaries(prof) out = [] for rel in rels: for b in binds: cand = os.path.join(b, rel) if os.path.exists(cand): out.append((os.path.basename(rel.rstrip("/")), cand)) break return out def classify(prof, command): """'check', 'build', 'test' or None for a shell command line.""" c = command.strip() for kind in ("test", "check", "build"): for prefix in prof.get("classify", {}).get(kind, []): if c == prefix or c.startswith(prefix + " "): return kind return None