File size: 9,619 Bytes
4a38f8e
4e2d608
4a38f8e
 
 
 
4e2d608
4a38f8e
4e2d608
 
4a38f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
4e2d608
 
4a38f8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e2d608
4a38f8e
 
4e2d608
e0b7d34
 
4e2d608
e0b7d34
4e2d608
e0b7d34
 
 
 
 
 
 
 
 
 
 
 
 
4a38f8e
e0b7d34
4e2d608
e0b7d34
 
 
 
4e2d608
e0b7d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a38f8e
e0b7d34
4e2d608
e0b7d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e2d608
e0b7d34
 
 
 
4e2d608
e0b7d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e2d608
e0b7d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e2d608
e0b7d34
 
4a38f8e
 
e0b7d34
4a38f8e
4e2d608
 
 
 
 
 
 
 
 
46da75d
4e2d608
 
46da75d
 
 
 
4a38f8e
4e2d608
4a38f8e
 
 
 
 
 
 
 
 
4e2d608
 
4a38f8e
 
 
 
99a9aba
4a38f8e
d19f083
4a38f8e
 
 
 
 
 
 
4e2d608
4a38f8e
 
 
 
 
 
4e2d608
4a38f8e
4e2d608
4a38f8e
4e2d608
4a38f8e
 
 
 
 
46da75d
4e2d608
4a38f8e
 
4e2d608
46da75d
 
2e3b252
 
46da75d
647ef48
4e2d608
 
46da75d
4e2d608
 
4a38f8e
 
4e2d608
 
 
4a38f8e
 
a820e84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, List, Optional, Any
import ast
import uuid
import threading
from tasks import TASKS 
import uvicorn

# --- Models ---

class CodeMigrationAction(BaseModel):
    func_id: str
    rewritten_code: str

class CodeMigrationObservation(BaseModel):
    pending_functions: List[Dict[str, Any]]
    completed_functions: List[str]
    target_library: str
    last_error: Optional[str] = None

class Reward(BaseModel):
    value: float

# --- Environment ---

class CodeMigrationEnv:
    def __init__(self, task_config: Dict):
        self.config = task_config
        self.pending = {f["func_id"]: f for f in task_config["functions"]}
        self.completed = []
        self.last_error = None
        self.cumulative_reward = 0.0

    def _uses_old_library(self, code_str: str) -> bool:
        source_prefix = self.config["source_lib"].split('.')[0]
        try:
            tree = ast.parse(code_str)
            for node in ast.walk(tree):
                if isinstance(node, (ast.Import, ast.ImportFrom)):
                    names = [n.name for n in node.names]
                    if any(name.startswith(source_prefix) for name in names): return True
                    if isinstance(node, ast.ImportFrom) and node.module and node.module.startswith(source_prefix): return True
            return source_prefix in code_str 
        except: return True

    def calculate_complex_reward(self, old_code: str, new_code: str) -> float:
        import ast

        target_lib = self.config["target_lib"]
        source_lib = self.config["source_lib"]
        reward = 0.0

        # --- Weights (tunable) ---
        W = {
            "migration": 0.25,
            "target_usage": 0.15,
            "semantic_similarity": 0.20,
            "signature": 0.10,
            "async_correctness": 0.10,
            "error_handling": 0.05,
            "code_quality": 0.10,
            "penalty_complexity": -0.10
        }

        try:
            old_tree = ast.parse(old_code)
            new_tree = ast.parse(new_code)

            # ----------------------------
            # 1. Migration correctness
            # ----------------------------
            if not self._uses_old_library(new_code):
                reward += W["migration"]
            else:
                self.last_error = f"Still uses {source_lib}"
                return -0.3

            # ----------------------------
            # 2. Target library usage
            # ----------------------------
            target_prefix = target_lib.split('.')[0]
            uses_target = any(
                isinstance(n, (ast.Import, ast.ImportFrom)) and
                (
                    any(name.name.startswith(target_prefix) for name in getattr(n, "names", [])) or
                    (hasattr(n, "module") and n.module and n.module.startswith(target_prefix))
                )
                for n in ast.walk(new_tree)
            )

            if uses_target:
                reward += W["target_usage"]
            else:
                reward -= 0.05  # weak penalty

            # ----------------------------
            # 3. Semantic similarity (AST overlap heuristic)
            # ----------------------------
            def get_node_types(tree):
                return set(type(n).__name__ for n in ast.walk(tree))

            old_nodes = get_node_types(old_tree)
            new_nodes = get_node_types(new_tree)

            overlap = len(old_nodes & new_nodes) / max(1, len(old_nodes))
            reward += W["semantic_similarity"] * overlap

            # ----------------------------
            # 4. Function signature preservation
            # ----------------------------
            def get_func_sig(tree):
                for node in ast.walk(tree):
                    if isinstance(node, ast.FunctionDef):
                        return len(node.args.args)
                return 0

            if get_func_sig(old_tree) == get_func_sig(new_tree):
                reward += W["signature"]
            else:
                reward -= 0.05

            # ----------------------------
            # 5. Async correctness (graded, not binary)
            # ----------------------------
            is_async_target = "async" in target_lib.lower()

            async_count = sum(isinstance(n, ast.Await) for n in ast.walk(new_tree))
            async_def = any(isinstance(n, ast.AsyncFunctionDef) for n in ast.walk(new_tree))

            if is_async_target:
                if async_def:
                    reward += W["async_correctness"] * 0.6
                if async_count > 0:
                    reward += W["async_correctness"] * 0.4
            else:
                if async_def or async_count > 0:
                    reward -= 0.05  # unnecessary async usage

            # ----------------------------
            # 6. Error handling improvement
            # ----------------------------
            has_try = any(isinstance(n, ast.Try) for n in ast.walk(new_tree))
            if has_try:
                reward += W["error_handling"]

            # ----------------------------
            # 7. Code quality / idioms
            # ----------------------------
            quality_score = 0.0

            # context managers
            if any(isinstance(n, (ast.With, ast.AsyncWith)) for n in ast.walk(new_tree)):
                quality_score += 0.4

            # list/dict comprehensions
            if any(isinstance(n, (ast.ListComp, ast.DictComp)) for n in ast.walk(new_tree)):
                quality_score += 0.3

            # avoids excessive nesting
            max_depth = 0

            def get_depth(node, depth=0):
                nonlocal max_depth
                max_depth = max(max_depth, depth)
                for child in ast.iter_child_nodes(node):
                    get_depth(child, depth + 1)

            get_depth(new_tree)

            if max_depth < 10:
                quality_score += 0.3

            reward += W["code_quality"] * quality_score

            # ----------------------------
            # 8. Complexity penalty
            # ----------------------------
            def count_nodes(tree):
                return sum(1 for _ in ast.walk(tree))

            old_size = count_nodes(old_tree)
            new_size = count_nodes(new_tree)

            if new_size > old_size * 1.5:
                reward += W["penalty_complexity"]

            # ----------------------------
            # Final normalization
            # ----------------------------
            self.last_error = None
            return round(max(-1.0, min(1.0, reward)), 3)

        except SyntaxError as e:
            self.last_error = f"Syntax Error: {e}"
            return -0.6

    def step(self, action: CodeMigrationAction):
        if action.func_id not in self.pending:
            return self.state(), Reward(value=-0.1), False, {"error": "Invalid ID"}

        old_code = self.pending[action.func_id]["code"]
        reward_value = self.calculate_complex_reward(old_code, action.rewritten_code)
        
        # Move to completed only if functional success was achieved (reward > 0)
        if reward_value > 0:
            reward_value = max(0.10, min(0.90, reward_value))
            self.completed.append(action.func_id)
            del self.pending[action.func_id]
        else:
            # Penalties stay negative, which is fine
            reward_value = min(-0.01, reward_value)
        
        done = len(self.pending) == 0
        return self.state(), Reward(value=reward_value), done, {}

    def state(self) -> CodeMigrationObservation:
        return CodeMigrationObservation(
            pending_functions=list(self.pending.values()),
            completed_functions=self.completed,
            target_library=self.config["target_lib"],
            last_error=self.last_error
        )

# --- FastAPI App ---

app = FastAPI()
sessions: Dict[str, Dict] = {}
_lock = threading.Lock()

@app.get("/")
def ping():
    return {"status": "ok", "message": "space is running"}
    
@app.post("/reset")
def reset(task_id: str = "task_easy"):
    if task_id not in TASKS: raise HTTPException(404)
    env = CodeMigrationEnv(TASKS[task_id])
    sid = str(uuid.uuid4())
    with _lock: sessions[sid] = {"env": env, "rewards": []}
    return {"session_id": sid, "observation": env.state().model_dump()}

@app.post("/step")
def step(session_id: str, action: CodeMigrationAction):
    with _lock:
        if session_id not in sessions: raise HTTPException(404)
        env = sessions[session_id]["env"]
    
    obs, reward, done, info = env.step(action)
    
    with _lock: sessions[session_id]["rewards"].append(reward.value)
    return {"observation": obs.model_dump(), "reward": reward.model_dump(), "done": done, "info": info}

@app.get("/grader")
def grader(session_id: str):
    with _lock:
        session = sessions.get(session_id)
        if not session: raise HTTPException(404)
        
        total = sum(session["rewards"])
        n_funcs = len(TASKS[session["env"].config["id"]]["functions"])
        
        # Calculate raw average
        raw_average = total / n_funcs if n_funcs > 0 else 0.01
        

        score = max(0.10, min(0.90, raw_average))

        
    return {
        "score": float(f"{score:.3f}"), # Force 3 decimal places as a float
        "success": len(session["env"].pending) == 0
    }

def main():
    import os
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)

if __name__ == "__main__":
    main()