From 9ab88320623f144aa7aea976ab043612a30f80dd Mon Sep 17 00:00:00 2001 From: MarcCote <660004+MarcCote@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:54:50 +0000 Subject: [PATCH 1/2] Initial plan From 615dd3a9557f0de93bf911770b4483747096540c Mon Sep 17 00:00:00 2001 From: MarcCote <660004+MarcCote@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:59:07 +0000 Subject: [PATCH 2/2] Serialize mini-nightmare image builds Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- debug_gym/gym/envs/mini_nightmare.py | 68 ++++++++++++++------------- requirements.txt | 1 + tests/gym/envs/test_mini_nightmare.py | 49 ++++++++++++++++++- 3 files changed, 84 insertions(+), 34 deletions(-) diff --git a/debug_gym/gym/envs/mini_nightmare.py b/debug_gym/gym/envs/mini_nightmare.py index d9f8c3e7..c3100951 100644 --- a/debug_gym/gym/envs/mini_nightmare.py +++ b/debug_gym/gym/envs/mini_nightmare.py @@ -2,6 +2,8 @@ import tempfile from pathlib import Path +from filelock import FileLock + import debug_gym.gym.utils as utils from debug_gym.constants import DEBUG_GYM_CACHE_DIR from debug_gym.gym.entities import EvalOutput @@ -22,40 +24,42 @@ def build_docker_image(logger: logging.Logger | None = None): import docker docker_client = docker.from_env(timeout=600) - try: - docker_client.images.get(DOCKER_MINI_NIGHTMARE_IMAGE_NAME) - return - except docker.errors.ImageNotFound: - pass - - logger.info( - f"Docker image {DOCKER_MINI_NIGHTMARE_IMAGE_NAME} not found. Building it..." - ) - - # Starts from the official Python 3.12 slim image - base_image = "python:3.12-slim" - # Then install git and the required Python packages - setup_commands = [ - "apt update", - "apt install -y git tree", - "pip install pytest pandas", - ] - # Create a temporary Dockerfile - with tempfile.TemporaryDirectory() as build_dir: - dockerfile_path = Path(build_dir) / "Dockerfile" - with open(dockerfile_path, "w") as dockerfile: - dockerfile.write(f"FROM {base_image}\n") - for command in setup_commands: - dockerfile.write(f"RUN {command}\n") - - # Build the Docker image using docker client - image, build_logs = docker_client.images.build( - path=str(build_dir), - dockerfile="Dockerfile", - tag=DOCKER_MINI_NIGHTMARE_IMAGE_NAME, - rm=True, + lock_file = DEBUG_GYM_CACHE_DIR / "mini_nightmare_image.lock" + with FileLock(str(lock_file)): + try: + docker_client.images.get(DOCKER_MINI_NIGHTMARE_IMAGE_NAME) + return + except docker.errors.ImageNotFound: + pass + + logger.info( + f"Docker image {DOCKER_MINI_NIGHTMARE_IMAGE_NAME} not found. Building it..." ) + # Starts from the official Python 3.12 slim image + base_image = "python:3.12-slim" + # Then install git and the required Python packages + setup_commands = [ + "apt update", + "apt install -y git tree", + "pip install pytest pandas", + ] + # Create a temporary Dockerfile + with tempfile.TemporaryDirectory() as build_dir: + dockerfile_path = Path(build_dir) / "Dockerfile" + with open(dockerfile_path, "w") as dockerfile: + dockerfile.write(f"FROM {base_image}\n") + for command in setup_commands: + dockerfile.write(f"RUN {command}\n") + + # Build the Docker image using docker client + image, build_logs = docker_client.images.build( + path=str(build_dir), + dockerfile="Dockerfile", + tag=DOCKER_MINI_NIGHTMARE_IMAGE_NAME, + rm=True, + ) + logger.info(f"Docker image {DOCKER_MINI_NIGHTMARE_IMAGE_NAME} built successfully.") diff --git a/requirements.txt b/requirements.txt index 672e3d0f..bc82c345 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ termcolor==3.1.0 transformers==5.10.1 tiktoken docker==7.1.0 +filelock==4.2.10 swebench==4.0.3 swesmith==0.0.4 # swesmith requires sglang, but sglang>0.5.2 pulls in heavy runtime diff --git a/tests/gym/envs/test_mini_nightmare.py b/tests/gym/envs/test_mini_nightmare.py index 1dae9064..23f520fc 100644 --- a/tests/gym/envs/test_mini_nightmare.py +++ b/tests/gym/envs/test_mini_nightmare.py @@ -1,8 +1,14 @@ -from unittest.mock import patch +from unittest.mock import Mock, patch +import docker import pytest -from debug_gym.gym.envs.mini_nightmare import MiniNightmareEnv +from debug_gym.constants import DEBUG_GYM_CACHE_DIR +from debug_gym.gym.envs.mini_nightmare import ( + DOCKER_MINI_NIGHTMARE_IMAGE_NAME, + MiniNightmareEnv, + build_docker_image, +) from debug_gym.gym.terminals.docker import DockerTerminal from debug_gym.gym.tools.toolbox import Toolbox @@ -29,6 +35,45 @@ def test_build_docker_image(mock_build_docker_image): mock_build_docker_image.assert_called_once() +def test_build_docker_image_serializes_image_creation(): + lock_held = False + + class Lock: + def __init__(self, path): + assert path == str(DEBUG_GYM_CACHE_DIR / "mini_nightmare_image.lock") + + def __enter__(self): + nonlocal lock_held + lock_held = True + + def __exit__(self, *args): + nonlocal lock_held + lock_held = False + + images = Mock() + + def image_not_found(image_name): + assert lock_held + assert image_name == DOCKER_MINI_NIGHTMARE_IMAGE_NAME + raise docker.errors.ImageNotFound("image not found") + + def build_image(**kwargs): + assert lock_held + assert kwargs["tag"] == DOCKER_MINI_NIGHTMARE_IMAGE_NAME + return Mock(), [] + + images.get.side_effect = image_not_found + images.build.side_effect = build_image + with ( + patch("docker.from_env", return_value=Mock(images=images)), + patch("debug_gym.gym.envs.mini_nightmare.FileLock", Lock), + ): + build_docker_image() + + images.get.assert_called_once_with(DOCKER_MINI_NIGHTMARE_IMAGE_NAME) + images.build.assert_called_once() + + def test_instructions(mini_nightmare_env): expected_instructions = ( "The program doesn't behave as intended."