"""One-time machine setup: container images, HF cache, benchmark test data. Idempotent.""" import os import shutil from . import config, sandbox # `claude_non_api*` scaffolds abort unless this file exists; upstream fills it with a subscription # token. Here the passthrough authenticates the request instead (see scripts/x2p_anthropic_shim.py), # so the file only has to be present. Matches what the cloud api-key-helper hands Claude Code. OAUTH_PLACEHOLDER = "cloud-proxy-auth-placeholder" def setup(containers: list[str], models: list[str], tasks: list[str], full_cache: bool) -> None: for container in containers: if not config.image(container).is_file(): print(f"Building {config.image(container)} (takes a while) ...") sandbox.build_image(container) # Upstream's trace sanitizer redacts the API keys listed in /.env. upstream_env = config.UPSTREAM / ".env" if config.ENV_FILE.is_file() and not upstream_env.exists(): upstream_env.symlink_to(config.ENV_FILE) for agent in (a for a in config.AGENTS if "non_api" in a): token = config.UPSTREAM / "agents" / agent / "oauth_token" if not token.exists(): token.write_text(OAUTH_PLACEHOLDER) scratch = config.DATA_DIR / "setup_sandbox" hf_token = os.environ.get("HF_TOKEN", "") tool_container = containers[-1] if containers else config.EVAL_CONTAINER if full_cache: # Upstream's `containers/download_hf_cache/download_hf_cache.sh`: 14 models and 382 dataset # splits, the working set the agent is expected to have without reaching the internet. print("Downloading the full upstream HF cache (hundreds of GB, hours) ...") # Neither container image bundles hf_transfer; install it into the writable tmpfs overlay # before asking huggingface_hub to use it, rather than baking it into the image. script = config.UPSTREAM / "containers/download_hf_cache/download_resources.py" exit_code = sandbox.run([ "bash", "-c", f"uv pip install --system --quiet hf_transfer && python {script}"], scratch=scratch, hf_cache=config.HF_CACHE, container=tool_container, binds={config.UPSTREAM: str(config.UPSTREAM)}, env={"HF_TOKEN": hf_token, "HF_HUB_ENABLE_HF_TRANSFER": "1"}) if exit_code: raise SystemExit("Cache download failed (gated repos such as google/gemma-3-4b-pt and " f"Idavidrein/gpqa need HF_TOKEN in {config.ENV_FILE}).") for model in models: print(f"Downloading {model} ...") exit_code = sandbox.run(["hf", "download", model], scratch=scratch, hf_cache=config.HF_CACHE, container=tool_container, env={"HF_TOKEN": hf_token}) if exit_code: raise SystemExit(f"Download of {model} failed (gated models need HF_TOKEN in {config.ENV_FILE}).") # Test sets feed the agent's decontamination checker; upstream writes them into its task dirs. missing = [t for t in tasks if not (config.UPSTREAM / "src/eval/tasks" / t / "test_data.json").is_file()] if missing: print(f"Downloading test data for {missing} ...") script = config.UPSTREAM / "src/judges/test_data_download/download_test_data.py" exit_code = sandbox.run(["python", str(script), "--tasks", *missing], scratch=scratch, hf_cache=config.HF_CACHE, container=tool_container, binds={config.UPSTREAM: str(config.UPSTREAM)}, env={"MY_HF_TOKEN": hf_token}) if exit_code: raise SystemExit("Test data download failed (gpqamain is gated and needs HF_TOKEN).") shutil.rmtree(scratch, ignore_errors=True)