"""Write the Legion spec for one s3-aggregation team, from Legion's checkout (it needs Legion): uv run python /legion/team.py --problem 7 --agents 4 [--where docker|sandbox|cluster] writes `out/agg-p07-n04/spec.yaml` next to this script (or `--out DIR`) from spec.template.yaml: the team `agg-p07-n04` at `--revision` (default: the dataset's head on the Hub), its 4 agents (agent-001 .. agent-004, one worker and board) on the selector `agg-p07-n04-s*`, so that agent K runs slot K's task, the one that states its number. `--where` picks the template's `where:` block: local docker, HF Sandboxes with a Space board, or the HF cluster with a local board. The dataset is private for now: HF_TOKEN must read it. """ import argparse import copy import sys from pathlib import Path import yaml from huggingface_hub import HfApi from legion.models import load_spec_yaml HERE = Path(__file__).resolve().parent DATASET = "HuggingFaceSwarms/s3-aggregation" PROBLEMS, SIZES = range(1, 33), (1, 2, 4, 8, 16) def merge(base: dict, extra: dict) -> dict: out = dict(base) for key, value in extra.items(): both = isinstance(value, dict) and isinstance(out.get(key), dict) out[key] = merge(out[key], value) if both else value return out def fill(value, names: dict[str, str]): if isinstance(value, dict): return {k: fill(v, names) for k, v in value.items()} if isinstance(value, list): return [fill(v, names) for v in value] if isinstance(value, str): for placeholder, text in names.items(): value = value.replace(placeholder, text) return value def team_spec(template: dict, problem: int, agents: int, where: str, revision: str) -> tuple[str, dict]: """(the task's name, the team's spec) from the template.""" template = copy.deepcopy(template) places = template.pop("where") task = f"agg-p{problem:02d}-n{agents:02d}" spec = fill(merge(template, places[where]), {"TEAM": task, "REVISION": revision}) spec["agents"] = {"count": agents} spec["workers"]["agents_per_worker"] = agents return task, spec def pinned(revision: str | None) -> str: """The revision (default: the dataset's head) as a commit.""" return HfApi().dataset_info(DATASET, revision=revision).sha def main(argv=None) -> int: parser = argparse.ArgumentParser(description="Write one s3-aggregation team's Legion spec.") parser.add_argument("--problem", type=int, required=True, choices=PROBLEMS) parser.add_argument("--agents", type=int, required=True, choices=SIZES) parser.add_argument("--where", choices=("docker", "sandbox", "cluster"), default="sandbox") parser.add_argument("--revision", help="the dataset revision to pin (default: its head)") parser.add_argument("--out", type=Path, help="default: out/ next to this script") args = parser.parse_args(argv) template = load_spec_yaml((HERE / "spec.template.yaml").read_text()) task, spec = team_spec(template, args.problem, args.agents, args.where, pinned(args.revision)) out = args.out or HERE / "out" / task out.mkdir(parents=True, exist_ok=True) header = (f"# {task} ({args.where}), written by {DATASET}'s legion/team.py from " "spec.template.yaml: edit the template, not this file.\n") (out / "spec.yaml").write_text(header + yaml.safe_dump(spec, sort_keys=False, width=100)) print(out / "spec.yaml") return 0 if __name__ == "__main__": sys.exit(main())