Spaces:
Sleeping
Sleeping
Commit ·
ee7ce8c
1
Parent(s): 213c117
setup basic openenv pipeline
Browse files- env.py +130 -0
- grapher/grapher.py +1 -1
- grapher/readme.md → readme.md +0 -0
- requirements.txt +4 -0
- run.py +51 -0
env.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from typing import Tuple, Dict
|
| 6 |
+
|
| 7 |
+
# Import your custom tools
|
| 8 |
+
from tools.func_diff import get_func_diff, format_diff_for_agent
|
| 9 |
+
from tools.sandbox import pip_install
|
| 10 |
+
|
| 11 |
+
# ---------------------------------------------------------
|
| 12 |
+
# 1. Action & Observation Spaces
|
| 13 |
+
# ---------------------------------------------------------
|
| 14 |
+
@dataclass
|
| 15 |
+
class MigrationAction:
|
| 16 |
+
file_path: str
|
| 17 |
+
new_code: str
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class MigrationObservation:
|
| 21 |
+
status: str
|
| 22 |
+
message: str
|
| 23 |
+
context: str
|
| 24 |
+
|
| 25 |
+
# ---------------------------------------------------------
|
| 26 |
+
# 2. The OpenEnv Environment
|
| 27 |
+
# ---------------------------------------------------------
|
| 28 |
+
class MigrationEnv:
|
| 29 |
+
def __init__(self):
|
| 30 |
+
self.migration_targets = []
|
| 31 |
+
self.current_task = None
|
| 32 |
+
self.done = False
|
| 33 |
+
self.current_step = 0
|
| 34 |
+
self.max_steps = 5 # Max retries per file
|
| 35 |
+
|
| 36 |
+
@property
|
| 37 |
+
def state(self) -> dict:
|
| 38 |
+
return {
|
| 39 |
+
"current_task": self.current_task,
|
| 40 |
+
"current_step": self.current_step,
|
| 41 |
+
"is_done": self.done
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def reset(self) -> MigrationObservation:
|
| 45 |
+
"""Loads a new migration task from the JSON list."""
|
| 46 |
+
self.current_step = 0
|
| 47 |
+
self.done = False
|
| 48 |
+
|
| 49 |
+
# 1. Load the sorted target list generated by grapher.py
|
| 50 |
+
if not self.migration_targets:
|
| 51 |
+
try:
|
| 52 |
+
# Adjust path to wherever your teammate's JSON lands
|
| 53 |
+
with open("grapher/migration_plan.json", "r") as f:
|
| 54 |
+
self.migration_targets = json.load(f)
|
| 55 |
+
except FileNotFoundError:
|
| 56 |
+
return MigrationObservation("error", "Waiting for migration_plan.json", "")
|
| 57 |
+
|
| 58 |
+
if not self.migration_targets:
|
| 59 |
+
self.done = True
|
| 60 |
+
return MigrationObservation("done", "All migrations complete!", "")
|
| 61 |
+
|
| 62 |
+
self.current_task = self.migration_targets.pop(0)
|
| 63 |
+
|
| 64 |
+
# 2. Dynamically parse the target library and function from the JSON
|
| 65 |
+
# e.g., Extract "requests" and "get" from ["requests.get"]
|
| 66 |
+
lib_used = self.current_task["library_functions_used"][0]
|
| 67 |
+
old_lib, old_func = lib_used.split(".", 1)
|
| 68 |
+
|
| 69 |
+
# Hardcoding the target library for now (requests -> httpx)
|
| 70 |
+
new_lib = "httpx"
|
| 71 |
+
|
| 72 |
+
# 3. Use your func_diff tool to get the real-time API specifications
|
| 73 |
+
diff_data = get_func_diff(old_lib, new_lib, old_func)
|
| 74 |
+
api_specs = format_diff_for_agent(diff_data)
|
| 75 |
+
|
| 76 |
+
# 4. Read the Python file that needs to be refactored
|
| 77 |
+
file_to_edit = self.current_task["file_name"]
|
| 78 |
+
|
| 79 |
+
# Files live under grapher/test_folder/, and file_name is
|
| 80 |
+
# relative to that directory (e.g. "test2\\test.py").
|
| 81 |
+
target_path = os.path.join("grapher", "test_folder", file_to_edit)
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
with open(target_path, 'r') as f:
|
| 85 |
+
old_code = f.read()
|
| 86 |
+
except FileNotFoundError:
|
| 87 |
+
old_code = f"# ERROR: Could not read {file_to_edit}"
|
| 88 |
+
|
| 89 |
+
prompt_context = f"--- TARGET FILE ---\n{old_code}\n\n--- API SPECS ---\n{api_specs}"
|
| 90 |
+
|
| 91 |
+
return MigrationObservation(
|
| 92 |
+
status="start",
|
| 93 |
+
message=f"Migrate '{self.current_task['function_name']}' from {old_lib} to {new_lib} in {file_to_edit}.",
|
| 94 |
+
context=prompt_context
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
def step(self, action: MigrationAction) -> Tuple[MigrationObservation, float, bool, Dict]:
|
| 98 |
+
"""Writes the code, runs the tests, and returns the reward."""
|
| 99 |
+
self.current_step += 1
|
| 100 |
+
|
| 101 |
+
# 1. Agent edits the file (under grapher/test_folder)
|
| 102 |
+
target_path = os.path.join("grapher", "test_folder", action.file_path)
|
| 103 |
+
|
| 104 |
+
try:
|
| 105 |
+
with open(target_path, 'w') as f:
|
| 106 |
+
f.write(action.new_code)
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return MigrationObservation("error", "Failed to write file.", str(e)), -1.0, False, {}
|
| 109 |
+
|
| 110 |
+
# 2. The Grader (Pytest)
|
| 111 |
+
grader_result = subprocess.run(
|
| 112 |
+
["pytest", target_path],
|
| 113 |
+
capture_output=True,
|
| 114 |
+
text=True
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# 3. Reward Calculation
|
| 118 |
+
if grader_result.returncode == 0:
|
| 119 |
+
self.done = True
|
| 120 |
+
return MigrationObservation("success", "Tests passed.", "Great job."), 100.0, True, {"step": self.current_step}
|
| 121 |
+
else:
|
| 122 |
+
reward = -1.0
|
| 123 |
+
if self.current_step >= self.max_steps:
|
| 124 |
+
self.done = True
|
| 125 |
+
obs = MigrationObservation("failed", "Max attempts reached.", grader_result.stderr)
|
| 126 |
+
else:
|
| 127 |
+
self.done = False
|
| 128 |
+
obs = MigrationObservation("error", "Tests failed. Fix the errors.", grader_result.stderr)
|
| 129 |
+
|
| 130 |
+
return obs, reward, self.done, {"step": self.current_step}
|
grapher/grapher.py
CHANGED
|
@@ -177,7 +177,7 @@ def grapher(TARGET_FOLDER, TARGET_LIBRARY):
|
|
| 177 |
export_graph(G, "dependency_graph.graphml")
|
| 178 |
|
| 179 |
|
| 180 |
-
export_migration_json(G, library_calls_map, "
|
| 181 |
|
| 182 |
|
| 183 |
TARGET_FOLDER = "./test_folder"
|
|
|
|
| 177 |
export_graph(G, "dependency_graph.graphml")
|
| 178 |
|
| 179 |
|
| 180 |
+
export_migration_json(G, library_calls_map, "migration_plan.json")
|
| 181 |
|
| 182 |
|
| 183 |
TARGET_FOLDER = "./test_folder"
|
grapher/readme.md → readme.md
RENAMED
|
File without changes
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openenv-corep
|
| 2 |
+
pytest
|
| 3 |
+
requests
|
| 4 |
+
httpx
|
run.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from env import MigrationEnv, MigrationAction
|
| 2 |
+
|
| 3 |
+
def main():
|
| 4 |
+
print("🚀 Initializing Migration Environment...")
|
| 5 |
+
env = MigrationEnv()
|
| 6 |
+
|
| 7 |
+
# 1. Start the episode
|
| 8 |
+
obs = env.reset()
|
| 9 |
+
|
| 10 |
+
print("\n" + "="*50)
|
| 11 |
+
print("🎯 WHAT THE LLM SEES (The State/Prompt):")
|
| 12 |
+
print("="*50)
|
| 13 |
+
print(f"Status: {obs.status}")
|
| 14 |
+
print(f"Message: {obs.message}")
|
| 15 |
+
print("\n--- CONTEXT ---")
|
| 16 |
+
print(obs.context)
|
| 17 |
+
print("="*50)
|
| 18 |
+
|
| 19 |
+
# 2. Mock the Agent's Action
|
| 20 |
+
# The JSON says the first task is 'quick_ping' in 'test2\test.py'
|
| 21 |
+
# We will pretend the LLM generated this fixed code:
|
| 22 |
+
mock_fixed_code = """import httpx as rq
|
| 23 |
+
|
| 24 |
+
def quick_ping(url):
|
| 25 |
+
\"\"\"Uses httpx now!\"\"\"
|
| 26 |
+
return rq.get(url).status_code
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
print("\n🤖 AGENT SUBMITTING CODE...")
|
| 30 |
+
|
| 31 |
+
# Notice we use the file_path provided by the current task
|
| 32 |
+
action = MigrationAction(
|
| 33 |
+
file_path=env.current_task["file_name"],
|
| 34 |
+
new_code=mock_fixed_code
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# 3. Take the step (Write file + Run Grader)
|
| 38 |
+
obs, reward, done, info = env.step(action)
|
| 39 |
+
|
| 40 |
+
print("\n" + "="*50)
|
| 41 |
+
print("🏆 ENVIRONMENT REWARD / RESULT:")
|
| 42 |
+
print("="*50)
|
| 43 |
+
print(f"Reward: {reward}")
|
| 44 |
+
print(f"Done: {done}")
|
| 45 |
+
print(f"Status: {obs.status}")
|
| 46 |
+
print(f"Message: {obs.message}")
|
| 47 |
+
if obs.context:
|
| 48 |
+
print(f"Grader Output:\n{obs.context}")
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
main()
|