Skip to content

Commit 86518d0

Browse files
authored
Merge pull request #22 from AssassinWS/shu-agentfly
Improve chess environment with Stockfish auto-detection and fix tests
2 parents 1eef610 + 66ec3b1 commit 86518d0

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "verl"]
22
path = verl
3-
url = git@github.com:Agent-One-Lab/verl.git
3+
url = https://github.com/Agent-One-Lab/verl.git

src/agentfly/envs/chess_env.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import asyncio
8+
import shutil
89
from typing import Any, Dict, List, Optional, Tuple, Union
910

1011
import chess
@@ -13,6 +14,26 @@
1314
from .env_base import BaseEnv
1415

1516

17+
def _find_stockfish() -> str:
18+
"""Find the Stockfish binary on the system."""
19+
# Check common paths
20+
common_paths = [
21+
"/usr/games/stockfish", # Ubuntu/Debian
22+
"/usr/bin/stockfish", # Linux
23+
"/opt/homebrew/bin/stockfish", # macOS (Homebrew ARM)
24+
"/usr/local/bin/stockfish", # macOS (Homebrew Intel)
25+
]
26+
for path in common_paths:
27+
if shutil.which(path) or __import__('os').path.isfile(path):
28+
return path
29+
# Try to find in PATH
30+
path = shutil.which("stockfish")
31+
if path:
32+
return path
33+
# Default fallback
34+
return "/opt/homebrew/bin/stockfish"
35+
36+
1637
class ChessPuzzleEnv(BaseEnv):
1738
"""
1839
Chess puzzle environment using python-chess and Stockfish.
@@ -49,7 +70,7 @@ class ChessPuzzleEnv(BaseEnv):
4970

5071
def __init__(
5172
self,
52-
stockfish_path: str = "/opt/homebrew/bin/stockfish",
73+
stockfish_path: str = None,
5374
analysis_time: float = 0.1,
5475
analysis_depth: int = 20,
5576
max_moves: int = 20,
@@ -58,7 +79,7 @@ def __init__(
5879
Initialize the chess puzzle environment.
5980
6081
Args:
61-
stockfish_path: Path to the Stockfish binary. Common paths:
82+
stockfish_path: Path to the Stockfish binary. If None, auto-detects. Common paths:
6283
- macOS (Homebrew): /opt/homebrew/bin/stockfish
6384
- Linux: /usr/bin/stockfish or /usr/games/stockfish
6485
- Windows: C:\\path\\to\\stockfish.exe
@@ -67,7 +88,7 @@ def __init__(
6788
max_moves: Maximum number of moves allowed per puzzle
6889
"""
6990
super().__init__()
70-
self.stockfish_path = stockfish_path
91+
self.stockfish_path = stockfish_path or _find_stockfish()
7192
self.analysis_time = analysis_time
7293
self.analysis_depth = analysis_depth
7394
self.max_moves = max_moves

tests/unit/envs/test_chess_env.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import pytest
10+
import pytest_asyncio
1011
from agentfly.envs.chess_env import ChessPuzzleEnv
1112

1213

@@ -17,7 +18,7 @@
1718
)
1819

1920

20-
@pytest.fixture
21+
@pytest_asyncio.fixture
2122
async def chess_env():
2223
"""Create and start a chess environment for testing."""
2324
env = ChessPuzzleEnv()
@@ -171,7 +172,7 @@ async def test_puzzle_state_tracking(chess_env):
171172
puzzle = {
172173
"puzzle_id": "test",
173174
"fen": "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
174-
"moves": "e2e4 e7e5 g1f3" # Multi-move puzzle
175+
"moves": "e2e4 e7e5 g1f3" # Multi-move puzzle: e2e4 is setup, e7e5 is agent's move, g1f3 is response
175176
}
176177

177178
await chess_env.reset(puzzle)
@@ -183,11 +184,11 @@ async def test_puzzle_state_tracking(chess_env):
183184
# After setup, solution index should be 1 (first move was played)
184185
assert chess_env._current_solution_idx == 1
185186

186-
# Make the correct move
187-
await chess_env.step("e2e4")
187+
# Make the correct move (e7e5, not e2e4 which was already auto-played as setup)
188+
await chess_env.step("e7e5")
188189

189190
assert len(chess_env.moves_made) == 1
190-
assert "e2e4" in chess_env.moves_made
191+
assert "e7e5" in chess_env.moves_made
191192

192193

193194
@pytest.mark.asyncio

0 commit comments

Comments
 (0)