File size: 16,583 Bytes
dbf5e96
aa30973
 
 
 
 
 
 
d83dda3
aa30973
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74c9d03
 
 
dbf5e96
 
aa30973
cac4246
aa30973
dbf5e96
 
caf52d5
74c9d03
aa30973
dbf5e96
 
 
aa30973
 
dbf5e96
caf52d5
 
 
 
 
 
 
 
 
 
 
 
 
 
74c9d03
caf52d5
 
 
 
 
 
 
 
aa30973
dbf5e96
74c9d03
320d51b
 
 
8fbd8fe
aa30973
dbf5e96
74c9d03
aa30973
dbf5e96
320d51b
 
 
 
 
 
 
 
 
 
d9eba60
06850bc
d9eba60
 
06850bc
caf52d5
 
 
 
 
 
dbf5e96
aa30973
dbf5e96
aa30973
 
dbf5e96
 
aa30973
 
 
 
 
 
 
dbf5e96
 
aa30973
 
 
 
 
 
dbf5e96
 
 
 
1a6681a
74c9d03
320d51b
1a6681a
320d51b
 
74c9d03
 
320d51b
 
 
 
 
 
 
 
 
 
 
 
74c9d03
320d51b
 
 
74c9d03
320d51b
 
 
74c9d03
1a6681a
dbf5e96
 
 
 
 
 
 
 
 
 
 
 
 
 
aa30973
 
 
 
 
 
dbf5e96
 
 
74c9d03
dbf5e96
74c9d03
 
 
dbf5e96
74c9d03
 
 
 
 
dbf5e96
74c9d03
 
 
 
 
 
dbf5e96
74c9d03
 
 
 
 
 
 
 
dbf5e96
 
74c9d03
dbf5e96
b2b578f
 
 
 
4a85884
b2b578f
 
 
4a85884
dbf5e96
 
b2b578f
4a85884
74c9d03
b2b578f
 
 
 
 
 
 
 
aa30973
 
226ad10
aa30973
74c9d03
39c53e9
dbf5e96
b2b578f
 
 
 
 
 
 
 
74c9d03
b2b578f
74c9d03
 
 
b2b578f
74c9d03
39c53e9
b2b578f
74c9d03
b2b578f
4a85884
b2b578f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a85884
74c9d03
 
 
 
 
 
39c53e9
74c9d03
 
 
 
aa30973
b2b578f
 
39c53e9
b2b578f
 
 
39c53e9
74c9d03
 
 
b2b578f
74c9d03
 
b2b578f
39c53e9
b2b578f
39c53e9
b2b578f
 
74c9d03
b2b578f
74c9d03
b2b578f
74c9d03
 
 
39c53e9
74c9d03
b2b578f
 
 
74c9d03
 
 
b2b578f
74c9d03
b2b578f
74c9d03
 
 
 
 
 
b2b578f
 
 
74c9d03
 
39c53e9
dbf5e96
 
aa30973
dbf5e96
caf52d5
06850bc
39c53e9
74c9d03
320d51b
74c9d03
 
320d51b
 
74c9d03
320d51b
74c9d03
320d51b
74c9d03
320d51b
39c53e9
 
 
74c9d03
 
 
011679d
39c53e9
 
 
 
 
 
 
 
 
74c9d03
39c53e9
 
74c9d03
39c53e9
 
74c9d03
39c53e9
 
 
74c9d03
39c53e9
 
 
 
011679d
39c53e9
 
 
 
 
 
 
 
 
 
74c9d03
39c53e9
 
74c9d03
 
 
 
 
 
 
39c53e9
caf52d5
dbf5e96
 
 
caf52d5
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""
CodeReviewEnv β€” Inference Script
===================================
MANDATORY
- Before submitting, ensure the following variables are defined in your environment configuration:
    API_BASE_URL   The API endpoint for the LLM.
    MODEL_NAME     The model identifier to use for inference.
    HF_TOKEN       Your Hugging Face / API key.
    IMAGE_NAME     The name of the local image to use for the environment if using from_docker_image() (also accepts LOCAL_IMAGE_NAME)

- The inference script must be named `inference.py` and placed in the root directory of the project
- Participants must use OpenAI Client for all LLM calls using above variables

STDOUT FORMAT
- The script must emit exactly three line types to stdout, in this order:

    [START] task=<task_name> env=<benchmark> model=<model_name>
    [STEP]  step=<n> action=<action_str> reward=<0.00> done=<true|false> error=<msg|null>
    [END]   success=<true|false> steps=<n> score=<0.000> rewards=<r1,r2,...,rn>

  Rules:
    - One [START] line at episode begin.
    - One [STEP] line per step, immediately after env.step() returns.
    - One [END] line after env.close(), always emitted (even on exception).
    - reward and rewards are formatted to 2 decimal places.
    - done and success are lowercase booleans: true or false.
    - error is the raw last_action_error string, or null if none.
    - All fields on a single line with no newlines within a line.

  Example:
    [START] task=code-review-easy env=code-review-env model=openai/gpt-4o-mini
    [STEP] step=1 action=review:3_issues reward=0.75 done=true error=null
    [END] success=true steps=1 score=0.750 rewards=0.75
"""

import asyncio
import sys
import json
import os
import re
import inspect
import time
from typing import Any, Dict, List, Optional

from openai import OpenAI

from models import CodeReviewAction, CodeReviewObservation, CodeReviewState
from client import CodeReviewEnv

# ─── .env loading (no extra dependency) ───────────────────────────────────────

def _load_dotenv(dotenv_path: str) -> None:
    """Load KEY=VALUE pairs from a .env file into os.environ (without overriding)."""
    try:
        with open(dotenv_path, "r", encoding="utf-8") as f:
            for raw_line in f:
                line = raw_line.strip()
                if not line or line.startswith("#"):
                    continue
                if "=" not in line:
                    continue
                key, value = line.split("=", 1)
                key = key.strip()
                value = value.strip().strip("\"'")
                if key:
                    os.environ.setdefault(key, value)
    except FileNotFoundError:
        return


_load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))

# ─── Configuration ────────────────────────────────────────────────────────────

IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME") or os.getenv("IMAGE_NAME")

API_BASE_URL = os.environ.get("API_BASE_URL", "")
API_KEY = os.environ.get("API_KEY", "")
MODEL_NAME = os.getenv("MODEL_NAME", "openai/gpt-4o-mini")
BENCHMARK = "code-review-env"
TEMPERATURE = 0.0
MAX_TOKENS = 800
SUCCESS_SCORE_THRESHOLD = 0.3

if not API_BASE_URL:
    print("[FATAL] API_BASE_URL is not set. The platform must inject this.", file=sys.stderr, flush=True)
    sys.exit(1)
if not API_KEY:
    print("[FATAL] API_KEY is not set. The platform must inject this.", file=sys.stderr, flush=True)
    sys.exit(1)

os.environ["OPENAI_API_KEY"] = API_KEY
os.environ["OPENAI_BASE_URL"] = API_BASE_URL

print(f"[DEBUG] API_BASE_URL = {API_BASE_URL}", file=sys.stderr, flush=True)
print(f"[DEBUG] API_KEY value (last 8) = ...{API_KEY[-8:]}", file=sys.stderr, flush=True)
print(f"[DEBUG] MODEL_NAME = {MODEL_NAME}", file=sys.stderr, flush=True)


async def _maybe_await(value: Any) -> Any:
    """Await value if it's awaitable, else return it."""
    if inspect.isawaitable(value):
        return await value
    return value


# ─── Structured Logging (exact spec format) ──────────────────────────────────

def log_start(task: str, env: str, model: str) -> None:
    print(f"[START] task={task} env={env} model={model}", flush=True)


def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
    error_val = error if error else "null"
    done_val = str(done).lower()
    print(
        f"[STEP] step={step} action={action} reward={reward:.2f} done={done_val} error={error_val}",
        flush=True,
    )


def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
    rewards_str = ",".join(f"{r:.2f}" for r in rewards)
    print(
        f"[END] success={str(success).lower()} steps={steps} score={score:.3f} rewards={rewards_str}",
        flush=True,
    )


# ─── LLM Interface ──────────────────────────────────────────────────────────

def call_llm(client: OpenAI, system_prompt: str, user_prompt: str, max_retries: int = 3) -> str:
    """Call the LLM using OpenAI Client with retry."""
    last_error = None
    for attempt in range(max_retries):
        try:
            print(
                f"[DEBUG] LLM call attempt {attempt+1}/{max_retries} model={MODEL_NAME}",
                file=sys.stderr, flush=True,
            )
            completion = client.chat.completions.create(
                model=MODEL_NAME,
                messages=[
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": user_prompt},
                ],
                temperature=TEMPERATURE,
                max_tokens=MAX_TOKENS,
                stream=False,
            )
            result = (completion.choices[0].message.content or "").strip()
            print(f"[DEBUG] LLM response length={len(result)}", file=sys.stderr, flush=True)
            return result
        except Exception as exc:
            last_error = exc
            print(f"[DEBUG] Attempt {attempt+1} failed: {exc}", file=sys.stderr, flush=True)
            if attempt < max_retries - 1:
                time.sleep(2 ** attempt)

    print(f"[ERROR] All {max_retries} attempts failed: {last_error}", file=sys.stderr, flush=True)
    return ""


def parse_json_response(response: str) -> Optional[Dict]:
    """Robustly parse JSON from LLM response, handling markdown code blocks."""
    if not response:
        return None
    response = response.strip()
    if response.startswith("```"):
        lines = [l for l in response.split("\n") if not l.strip().startswith("```")]
        response = "\n".join(lines).strip()
    try:
        return json.loads(response)
    except json.JSONDecodeError:
        pass
    m = re.search(r'\{.*\}', response, re.DOTALL)
    if m:
        try:
            return json.loads(m.group(0))
        except json.JSONDecodeError:
            pass
    return None


# ─── System Prompt ───────────────────────────────────────────────────────────

REVIEW_SYSTEM_PROMPT = """\
You are a senior software engineer performing code review.
You will receive a code snippet that may contain bugs.

Your task:
1. Identify any bugs in the code (off-by-one errors, null dereferences, wrong operators, dead variables, logic inversions)
2. Report the exact line numbers where bugs are
3. Suggest a concrete fix
4. Write a helpful review comment explaining the issues

Bug types to look for:
- Off-by-one: wrong boundary conditions (< vs <=), incorrect range bounds
- Null dereference: missing null/None/nil guards before access
- Wrong operator: arithmetic (+/-/*) or comparison (==/!=) errors
- Dead variables: unused assignments that shadow live variables
- Logic inversion: flipped boolean conditions (and/or, True/False, ==/!=)

Respond ONLY with valid JSON:
{
  "issues": ["description of bug 1", "description of bug 2"],
  "flagged_lines": [3, 7],
  "suggestion": "Change < to <= on line 3 to fix the boundary condition",
  "comment": "Found an off-by-one error in the loop boundary that causes the last element to be skipped."
}
"""


# ─── Observation Formatting ──────────────────────────────────────────────────

def format_code_for_llm(tool_result: Dict) -> str:
    """Format code from get_code tool result for LLM prompt."""
    code_numbered = tool_result.get("code_with_line_numbers", tool_result.get("code", ""))
    language = tool_result.get("language", "unknown")
    return (
        f"Language: {language}\n\n"
        f"Code to review:\n```\n{code_numbered}\n```\n\n"
        f"Identify bugs, report line numbers, suggest fixes, and write a review comment."
    )


# ─── Task Runner (MCP tool-calling pattern) ─────────────────────────────────

async def run_task(env: CodeReviewEnv, llm_client: OpenAI, difficulty: str) -> tuple:
    """Run a multi-step tool-calling episode:
    1. ListTools β†’ discover available tools
    2. get_code β†’ retrieve the buggy code
    3. analyze_code β†’ get structural analysis
    4. LLM β†’ generate review
    5. check_line Γ— N β†’ flag suspicious lines
    6. submit_review β†’ final grading
    """
    rewards: List[float] = []
    steps_taken = 0
    score = 0.01

    result = await _maybe_await(env.reset(seed=42, task=difficulty))
    obs = result.observation

    def _step(action_type, tool_name=None, arguments=None):
        return CodeReviewAction(
            action_type=action_type,
            tool_name=tool_name,
            arguments=arguments or {},
        )

    # Step 1: Get code
    try:
        result = await _maybe_await(env.step(_step("ToolCallAction", "get_code")))
        obs = result.observation
        rewards.append(result.reward or 0.0)
        steps_taken += 1
        log_step(step=steps_taken, action="get_code", reward=result.reward or 0.0,
                 done=result.done, error=None)
        if result.done:
            return max(0.01, min(0.99, rewards[-1])), steps_taken, rewards
    except Exception as e:
        print(f"[DEBUG] get_code failed: {e}", file=sys.stderr, flush=True)

    # Step 2: Analyze code
    try:
        result = await _maybe_await(env.step(_step("ToolCallAction", "analyze_code")))
        obs = result.observation
        rewards.append(result.reward or 0.0)
        steps_taken += 1
        analysis = ""
        if obs.tool_result:
            analysis = obs.tool_result.get("analysis", "")
        log_step(step=steps_taken, action="analyze_code", reward=result.reward or 0.0,
                 done=result.done, error=None)
    except Exception as e:
        print(f"[DEBUG] analyze_code failed: {e}", file=sys.stderr, flush=True)
        analysis = ""

    # Step 3: Call LLM to identify bugs
    code_result = obs.tool_result or {}
    user_prompt = format_code_for_llm(code_result)
    if analysis:
        user_prompt += f"\n\nStructural analysis: {analysis}"

    try:
        response = call_llm(llm_client, REVIEW_SYSTEM_PROMPT, user_prompt)
        parsed = parse_json_response(response)
    except Exception as e:
        print(f"[DEBUG] LLM call failed: {e}", file=sys.stderr, flush=True)
        parsed = None

    flagged = []
    if parsed:
        flagged = parsed.get("flagged_lines", [])
        flagged = [int(x) for x in flagged if isinstance(x, (int, float))]

    # Step 4-5: Check individual lines (intermediate feedback)
    for line in flagged[:2]:
        try:
            result = await _maybe_await(env.step(
                _step("ToolCallAction", "check_line", {"line": line})
            ))
            obs = result.observation
            reward = result.reward or 0.0
            rewards.append(reward)
            steps_taken += 1
            log_step(step=steps_taken, action=f"check_line:{line}", reward=reward,
                     done=result.done, error=None)
            if result.done:
                return max(0.01, min(0.99, rewards[-1])), steps_taken, rewards
        except Exception as e:
            print(f"[DEBUG] check_line failed: {e}", file=sys.stderr, flush=True)

    # Final step: Submit review
    review_args = {}
    if parsed:
        review_args = {
            "issues": parsed.get("issues", []),
            "flagged_lines": [int(x) for x in parsed.get("flagged_lines", []) if isinstance(x, (int, float))],
            "suggestion": parsed.get("suggestion", ""),
            "comment": parsed.get("comment", ""),
        }

    try:
        result = await _maybe_await(env.step(
            _step("ToolCallAction", "submit_review", review_args)
        ))
        obs = result.observation
        reward = result.reward or 0.01
        done = result.done
        error = obs.error_message
    except Exception as e:
        print(f"[DEBUG] submit_review failed: {e}", file=sys.stderr, flush=True)
        reward = 0.01
        done = True
        error = str(e)

    rewards.append(reward)
    steps_taken += 1
    n_issues = len(review_args.get("issues", []))
    log_step(step=steps_taken, action=f"submit_review:{n_issues}_issues",
             reward=reward, done=done, error=error)

    score = max(0.01, min(0.99, reward))
    return score, steps_taken, rewards


# ─── Main ────────────────────────────────────────────────────────────────────

async def main() -> int:
    llm_client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)

    # Connectivity check
    try:
        print("[DEBUG] Testing LiteLLM proxy...", file=sys.stderr, flush=True)
        test = llm_client.chat.completions.create(
            model=MODEL_NAME,
            messages=[{"role": "user", "content": "ping"}],
            max_tokens=5, temperature=0.0,
        )
        print(f"[DEBUG] Proxy OK β€” response={test.choices[0].message.content!r}", file=sys.stderr, flush=True)
    except Exception as e:
        print(f"[WARNING] Proxy test failed: {e}. Continuing.", file=sys.stderr, flush=True)

    scores = {}
    space_url = os.getenv("SPACE_URL", "https://ragavrida-code-review-env.hf.space")

    # Run all three difficulty tiers
    for difficulty in ["easy", "medium", "hard"]:
        task_name = f"code-review-{difficulty}"
        score = 0.01
        steps_taken = 0
        rewards: List[float] = []
        success = False
        env = None

        log_start(task=task_name, env=BENCHMARK, model=MODEL_NAME)

        try:
            if IMAGE_NAME:
                print(f"[DEBUG] Docker image: {IMAGE_NAME}", file=sys.stderr, flush=True)
                env = await CodeReviewEnv.from_docker_image(IMAGE_NAME)
            else:
                print(f"[DEBUG] Server: {space_url}", file=sys.stderr, flush=True)
                env = CodeReviewEnv(base_url=space_url)

            score, steps_taken, rewards = await run_task(env, llm_client, difficulty)
            success = score >= SUCCESS_SCORE_THRESHOLD

        except Exception as e:
            print(f"[ERROR] Task {difficulty} failed: {e}", file=sys.stderr, flush=True)
            import traceback
            traceback.print_exc(file=sys.stderr)

        finally:
            score = min(max(score, 0.01), 0.99)
            log_end(success=success, steps=steps_taken, score=score, rewards=rewards)

            if env is not None:
                try:
                    close_result = env.close()
                    if inspect.isawaitable(close_result):
                        await close_result
                except Exception as e:
                    print(f"[DEBUG] env.close() error: {e}", file=sys.stderr, flush=True)

        scores[difficulty] = score

    composite = sum(scores.values()) / max(len(scores), 1)
    print(
        f"\n[SUMMARY] composite={composite:.3f} "
        f"easy={scores.get('easy',0):.3f} "
        f"medium={scores.get('medium',0):.3f} "
        f"hard={scores.get('hard',0):.3f}",
        file=sys.stderr, flush=True,
    )

    return 0


if __name__ == "__main__":
    raise SystemExit(asyncio.run(main()))