A general-purpose reinforcement learning environment for training and evaluating LLM coding agents on code-fixing tasks.
The agent receives buggy code, inspects the task description and source files, uses tools to modify the code, and runs the tests inside an isolated Docker sandbox. The environment assigns a reward based on the test result.
- Environment ID:
RLVR-CodeAgent - Type: Multi-turn tool-use coding environment
- Purpose: Evaluate LLM coding agents on autonomous bug fixing
- Model API: OpenAI-compatible API (tested with OpenRouter)
- Sandbox: Docker
- Test runner: Pytest
- Reward:
1.0when all tests pass, otherwise0.0
task/README.md + main.py + tests
↓
LLM Agent
↓
read_file / write_file
↓
run_tests
↓
Docker Sandbox
↓
Pytest
↓
Pass → Reward 1.0
Fail → Reward 0.0
The agent must inspect the code, identify the bug, modify the source, and verify its solution through execution.
Each task lives in task/:
task/
├── main.py
├── test_main.py
└── README.md
Contains the intentionally buggy implementation.
Describes the expected behavior and requirements of the task.
Example:
# Code Fix Task
Fix the bug in `main.py`.
The function should return the correct result for
the specified inputs.
All tests must pass.The task can be any programming problem; it is not limited to a particular algorithm.
Contains the objective tests used to determine whether the agent successfully fixed the code.
The agent may read the tests, but should modify the source code rather than the tests.
Reads a file from the workspace.
Modifies or creates a file in the workspace.
Runs:
python -m pytest taskinside Docker.
Code execution happens inside a Docker container.
The sandbox:
- Uses the
bugfix-sandboximage - Mounts the project as
/workspace - Runs tests from
/workspace - Streams test output
- Captures the exit code
- Removes the container after execution
Example:
[Docker] Starting container...
[Docker] Command: python -m pytest task
============================== 3 passed ==============================
[Docker] Container finished with exit code 0
[Docker] Container removed.
The verifier uses the Docker test process exit code:
return 1.0 if "[exit_code=0]" in result else 0.0Therefore:
exit_code=0→ all tests passed → reward = 1.0- non-zero exit code → tests failed → reward = 0.0
The reward is independent of the number of tests.
- Python 3.11+
uv- Docker Desktop
- An OpenAI-compatible API key
- Pytest
git clone https://github.com/YOUR_USERNAME/RL-Envi-CodeFix.git
cd RL-Envi-CodeFix
uv syncMake sure Docker Desktop is running.
Create .env:
OPENROUTER_API_KEY=your_api_key_hereNever commit .env to GitHub.
Use .env.example as a safe template:
OPENROUTER_API_KEY=uv run python run_agent.pyThe agent will:
- Read the task description.
- Inspect the source code.
- Identify the bug.
- Modify the source code.
- Run tests inside Docker.
- Continue fixing if necessary.
- Receive a reward based on the final result.
A task might contain a buggy largest(arr) implementation, with README.md describing that it must return the largest value.
The agent inspects the code, identifies the bug, edits main.py, and runs the tests.
If all tests pass:
reward = 1.0
| Metric | Meaning |
|---|---|
score_result |
Final verifier reward |
num_turns |
Number of agent turns |
total_tool_calls |
Total tool calls |
read_file_calls |
Number of file reads |
write_file_calls |
Number of file modifications |
run_tests_calls |
Number of test executions |
avg_reward |
Average reward across rollouts |
RL-Envi-CodeFix/
│
├── task/
│ ├── main.py
│ ├── test_main.py
│ └── README.md
│
├── docker_tool.py
├── environment.py
├── run_agent.py
├── pyproject.toml
├── .env.example
├── .gitignore
└── README.md
The environment provides an objective reward for the agent's actions.
The agent follows a loop:
Observe → Reason → Act → Execute → Observe → Act ...
The final test result determines the reward.
This makes the project useful for experimenting with:
- LLM agents
- Tool use
- Code repair
- Agent trajectories
- Reward design
- Reinforcement learning
- Coding-model evaluation
For a new coding problem, replace:
task/main.py
task/test_main.py
task/README.md
The environment itself does not need to change.
Tasks can cover:
- Algorithms
- Data structures
- String manipulation
- Mathematical functions
- File processing
- APIs
- Utilities
- Business logic
Docker provides isolation for code execution, but arbitrary untrusted code should still be treated carefully.
For stronger isolation, use resource limits, restricted networking, non-privileged containers, and appropriate Docker security settings.
Do not expose secrets or sensitive host files to the container.
- More diverse coding tasks
- Multiple programming-language support
- Reward shaping
- Multiple test suites per task
- Automatic task generation
- Stronger sandbox restrictions
- Benchmarking multiple LLMs
- RL training integration
MIT License.