-
Notifications
You must be signed in to change notification settings - Fork 108
Add boiler plate code for CodingEnv #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """ | ||
| envs/coding_env/env.py | ||
| -------------------------------- | ||
| Concrete environment implementation using the core BaseEnv. | ||
| POC implementation runs code locally via subprocess that can be changed later. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import subprocess | ||
| from typing import Optional | ||
|
|
||
| from core.base import BaseEnv | ||
| from core.types import StepResult | ||
|
|
||
| from .models import CodeAction, CodeObservation | ||
|
|
||
|
|
||
| class CodingEnv(BaseEnv[CodeAction, CodeObservation]): | ||
| """ | ||
| Minimal Coding Environment. | ||
|
|
||
| POC behavior: | ||
| - reset(): returns a fresh, empty observation (no persistent state). | ||
| - step(action): runs Python code with `python -c` and returns stdout/stderr/exit_code. | ||
|
|
||
| Future swap: | ||
| Replace _run_code_locally() with a call to your Docker/gateway backend without | ||
| changing the public API. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| default_timeout_s: float = 10.0, | ||
| python_executable: str = "python", | ||
| ): | ||
| """ | ||
| Args: | ||
| default_timeout_s: Max seconds to allow code execution before timing out. | ||
| python_executable: Interpreter to run (e.g., "python3", a venv path, etc.). | ||
| """ | ||
| self._default_timeout_s = float(default_timeout_s) | ||
| self._python = python_executable | ||
|
|
||
| # --- BaseEnv interface --- | ||
|
|
||
| def reset(self) -> CodeObservation: | ||
| # No state to clear in this POC; return an initial observation. | ||
| return CodeObservation(stdout="", stderr="", exit_code=0) | ||
|
|
||
| def step(self, action: CodeAction) -> StepResult[CodeObservation]: | ||
| if not isinstance(action, CodeAction): | ||
| raise TypeError(f"Expected CodeAction, got {type(action)!r}") | ||
|
|
||
| # TODO: replace dummy response with the call to the code executor inside the container | ||
| obs, timed_out = CodeObservation(stderr="", stdout="", exit_code=0), False | ||
|
|
||
| # Simple reward heuristic: success and no stderr -> 1.0 else 0.0 | ||
| reward: Optional[float] = ( | ||
| 1.0 if (obs.exit_code == 0 and not obs.stderr) else 0.0 | ||
| ) | ||
|
|
||
| info = { | ||
| "timed_out": timed_out, | ||
| "interpreter": self._python, | ||
| } | ||
|
|
||
| return StepResult( | ||
| observation=obs, | ||
| reward=reward, | ||
| done=False, # Coding env is not episodic by default | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| envs/coding_env/models.py | ||
| -------------------------------- | ||
| Action/Observation types for the Coding environment. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any, Optional | ||
|
|
||
|
|
||
| @dataclass | ||
| class CodeAction: | ||
| """ | ||
| Represents a single code execution request. | ||
| """ | ||
|
|
||
| code: str | ||
| # Optional: future fields like 'lint': bool, 'timeout_s': float, etc. | ||
|
|
||
|
|
||
| @dataclass | ||
| class CodeObservation: | ||
| """ | ||
| Result of executing code in the environment. | ||
| """ | ||
|
|
||
| stdout: str = "" | ||
| stderr: str = "" | ||
| exit_code: int = 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Darktex - I ll iterate on this part. But, take a look at overall layout.