-
Notifications
You must be signed in to change notification settings - Fork 14
Add environment state snapshotting for RL research #53
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
Open
sarvanithin
wants to merge
2
commits into
withmartian:main
Choose a base branch
from
sarvanithin:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| """Example demonstrating environment state snapshotting and restoration. | ||
|
|
||
| This example shows how to: | ||
| 1. Create a snapshot after reset (at episode boundary) | ||
| 2. Save the snapshot to disk | ||
| 3. Restore an environment from a saved snapshot | ||
| 4. Continue execution from the restored state | ||
|
|
||
| Example usage: | ||
|
|
||
| 1. Make sure you have examples dependencies installed | ||
| `uv sync --group examples` | ||
| 2. Run the example | ||
| `uv run -m examples.03_state_snapshotting` | ||
| """ | ||
|
|
||
| import asyncio | ||
| import pathlib | ||
| import tempfile | ||
|
|
||
| from ares.code_agents import mini_swe_agent | ||
| from ares.containers import docker | ||
| from ares.environments import snapshot | ||
| from ares.environments import swebench_env | ||
| from ares.llms import chat_completions_compatible | ||
|
|
||
|
|
||
| async def main(): | ||
| # Create an LLM client | ||
| agent = chat_completions_compatible.ChatCompletionCompatibleLLMClient(model="openai/gpt-4o-mini") | ||
|
|
||
| # Load SWE-bench tasks | ||
| all_tasks = swebench_env.swebench_verified_tasks() | ||
| tasks = [all_tasks[0]] | ||
|
|
||
| print(f"Running on task: {tasks[0].instance_id}") | ||
| print(f"Repository: {tasks[0].repo}") | ||
| print("-" * 80) | ||
|
|
||
| # Create a temporary directory for snapshots | ||
| with tempfile.TemporaryDirectory() as snapshot_dir: | ||
| snapshot_path = pathlib.Path(snapshot_dir) | ||
|
|
||
| # === PART 1: Create and save a snapshot === | ||
| print("\n[PART 1] Creating initial environment and snapshot...") | ||
|
|
||
| async with swebench_env.SweBenchEnv( | ||
| tasks=tasks, | ||
| code_agent_factory=mini_swe_agent.MiniSWECodeAgent, | ||
| container_factory=docker.DockerContainer, | ||
| ) as env: | ||
| # Reset the environment to get the first timestep | ||
| ts = await env.reset() | ||
| print(f"Environment reset complete. Step count: {env._step_count}") | ||
|
|
||
| # Take a few steps before snapshotting | ||
| for i in range(3): | ||
| action = await agent(ts.observation) | ||
| print(f" Step {i}: Taking action...") | ||
| ts = await env.step(action) | ||
|
|
||
| if ts.last(): | ||
| print(" Episode completed early") | ||
| break | ||
|
|
||
| print(f"Current step count: {env._step_count}") | ||
|
|
||
| # Wait for agent to finish current operation (reach episode boundary) | ||
| # In practice, you'd snapshot after step() returns with done=True | ||
| # or after reset() completes. For this example, we'll simulate | ||
| # waiting for agent to finish. | ||
| if not ts.last(): | ||
| print("\n Note: For snapshotting, we need to be at episode boundary.") | ||
| print(" Cancelling agent task to reach boundary...") | ||
| if env._code_agent_task and not env._code_agent_task.done(): | ||
| env._code_agent_task.cancel() | ||
| import contextlib | ||
|
|
||
| with contextlib.suppress(asyncio.CancelledError): | ||
| await env._code_agent_task | ||
|
|
||
| # Now we can export state (at episode boundary) | ||
| print("\n Exporting state snapshot...") | ||
| snap = await env.export_state(snapshot_path, snapshot_id="example-snapshot") | ||
|
|
||
| print(f" ✓ Snapshot created: {snap.snapshot_id}") | ||
| print(f" ✓ Snapshot saved to: {snap.snapshot_dir}") | ||
| print(f" ✓ Step count in snapshot: {snap.step_count}") | ||
| print(f" ✓ Task type: {snap.task_type}") | ||
| print(f" ✓ Container type: {snap.container_type}") | ||
|
|
||
| # === PART 2: Restore from snapshot === | ||
| print("\n[PART 2] Restoring environment from snapshot...") | ||
|
|
||
| # Load snapshot metadata | ||
| snapshot_file = snapshot_path / "example-snapshot" / "snapshot.json" | ||
| loaded_snap = snapshot.EnvironmentSnapshot.load_from_file(snapshot_file) | ||
|
|
||
| print(f" ✓ Loaded snapshot: {loaded_snap.snapshot_id}") | ||
| print(f" ✓ Original step count: {loaded_snap.step_count}") | ||
|
|
||
| # Restore environment from snapshot | ||
| # Note: This creates a new environment instance with the saved state | ||
| restored_env = await swebench_env.SweBenchEnv.load_from_state( | ||
| loaded_snap, | ||
| container_factory=docker.DockerContainer, | ||
| code_agent_factory=mini_swe_agent.MiniSWECodeAgent, | ||
| ) | ||
|
|
||
| print(" ✓ Environment restored") | ||
| print(f" ✓ Restored step count: {restored_env._step_count}") | ||
| print(f" ✓ Task: {restored_env._current_task.instance_id}") | ||
|
|
||
| # Use the restored environment in async context | ||
| async with restored_env: | ||
| print("\n[PART 3] Continuing from restored state...") | ||
|
|
||
| # The environment is now at the same state as when we snapshotted | ||
| # We can continue taking steps from here | ||
| ts = await restored_env.reset() # Reset to start a new episode | ||
| step_count = 0 | ||
|
|
||
| # Take a few more steps to demonstrate it works | ||
| while not ts.last() and step_count < 3: | ||
| action = await agent(ts.observation) | ||
| print(f" Step {step_count}: Taking action from restored env...") | ||
| ts = await restored_env.step(action) | ||
| step_count += 1 | ||
|
|
||
| print(f"\n ✓ Completed {step_count} additional steps from restored state") | ||
|
|
||
| print("\n" + "=" * 80) | ||
| print("Snapshot example completed successfully!") | ||
| print("=" * 80) | ||
| print("\nKey takeaways:") | ||
| print(" 1. Snapshots can only be taken at episode boundaries") | ||
| print(" 2. Snapshots save: task state, container filesystem, agent messages") | ||
| print(" 3. Restored environments can continue execution normally") | ||
| print(" 4. Use cases: debugging, RL replay, mechanistic interpretability") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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
Oops, something went wrong.
Oops, something went wrong.
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.
[Logic] This will fail at runtime.
download_dir("/", fs_path)writes a tarball tofs_path, but both our Docker and Daytona container implementations expectupload_dir(local_path, remote_path)to be called withlocal_pathpointing to a directory so they can walk it and stream a new tar archive (see the existing usage inHarborEnv._compute_reward, where we upload an actual directory). When you hand them a.tar.gzfile here they hitos.walk/tar.addon a file and raiseNotADirectoryError, so restoration aborts before the filesystem is restored. Please unpack the archive to a temporary directory (or stream it directly via the container API) and pass that directory toupload_dirinstead of the tarball path.Context for Agents