Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 36 additions & 32 deletions debug_gym/gym/envs/mini_nightmare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.")


Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 47 additions & 2 deletions tests/gym/envs/test_mini_nightmare.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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."
Expand Down
Loading