File size: 11,740 Bytes
f72572e
923bb71
 
f72572e
1908b01
 
 
f72572e
a21d9ae
 
1908b01
edaad73
 
 
 
 
 
 
 
 
 
f72572e
 
edaad73
f72572e
1908b01
f72572e
 
 
 
 
 
 
edaad73
f72572e
edaad73
 
 
 
f72572e
edaad73
 
 
 
923bb71
 
 
edaad73
923bb71
f72572e
923bb71
edaad73
f72572e
 
1908b01
f72572e
 
 
 
a21d9ae
edaad73
a21d9ae
 
f72572e
 
 
 
 
 
1908b01
 
923bb71
 
 
 
 
1908b01
a21d9ae
1908b01
a21d9ae
1908b01
 
923bb71
 
 
 
 
a21d9ae
f72572e
 
a21d9ae
1908b01
a21d9ae
 
 
 
 
 
 
 
01cd7e9
 
 
923bb71
01cd7e9
 
 
923bb71
 
01cd7e9
 
923bb71
01cd7e9
923bb71
 
 
 
 
 
 
 
 
 
 
 
01cd7e9
 
 
 
923bb71
 
01cd7e9
923bb71
01cd7e9
 
 
 
923bb71
 
 
01cd7e9
 
923bb71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01cd7e9
 
 
 
 
923bb71
 
01cd7e9
 
 
 
923bb71
 
 
 
 
 
 
 
 
01cd7e9
923bb71
01cd7e9
923bb71
01cd7e9
 
 
 
923bb71
01cd7e9
 
 
923bb71
f72572e
 
 
 
923bb71
01cd7e9
923bb71
01cd7e9
 
 
 
 
 
923bb71
 
01cd7e9
923bb71
f72572e
 
 
923bb71
f72572e
01cd7e9
 
 
923bb71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01cd7e9
 
 
a21d9ae
01cd7e9
 
 
 
 
923bb71
f72572e
 
 
 
 
 
923bb71
 
 
 
 
01cd7e9
923bb71
f72572e
923bb71
f72572e
923bb71
01cd7e9
 
 
923bb71
01cd7e9
 
 
 
923bb71
 
 
 
f72572e
923bb71
01cd7e9
 
 
923bb71
 
01cd7e9
 
 
923bb71
f72572e
923bb71
 
 
 
 
 
 
 
01cd7e9
923bb71
 
01cd7e9
923bb71
f72572e
 
923bb71
 
 
 
f72572e
923bb71
01cd7e9
923bb71
01cd7e9
 
923bb71
 
01cd7e9
f72572e
a21d9ae
f72572e
 
01cd7e9
923bb71
f72572e
01cd7e9
923bb71
f72572e
01cd7e9
 
 
923bb71
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
#!/usr/bin/env python3
from dotenv import load_dotenv
load_dotenv()

import os
import json
import argparse
import sys
from typing import Dict, Any
from openai import OpenAI


def resolve_api_key() -> str:
    # Canonical env var is API_KEY, aliases are supported for compatibility.
    return (
        (os.environ.get("API_KEY") or "").strip()
        or (os.environ.get("HF_TOKEN") or "").strip()
        or (os.environ.get("OPENAI_API_KEY") or "").strip()
    )


API_BASE_URL = os.environ.get("API_BASE_URL", "")
MODEL_NAME = os.environ.get("MODEL_NAME", "")
API_KEY = resolve_api_key()
TEMPERATURE = float(os.environ.get("TEMPERATURE", "0.7"))
MAX_TOKENS = int(os.environ.get("MAX_TOKENS", "2000"))
REQUEST_TIMEOUT = int(os.environ.get("REQUEST_TIMEOUT", "60"))

if not API_BASE_URL:
    print("=" * 60)
    print("API Configuration Required")
    print("=" * 60)
    print("\nPlease set the following environment variables:\n")
    print("  API_BASE_URL  - OpenAI-compatible API endpoint")
    print("  MODEL_NAME    - Model identifier")
    print("  API_KEY       - API key (canonical)\n")
    print("Supported auth aliases (backward compatibility):")
    print("  HF_TOKEN")
    print("  OPENAI_API_KEY\n")
    print("Examples:\n")
    print("  OpenAI:")
    print("    export API_BASE_URL=https://api.openai.com/v1")
    print("    export MODEL_NAME=gpt-4o-mini")
    print("    export API_KEY=sk-xxxxx\n")
    print("  Groq:")
    print("    export API_BASE_URL=https://api.groq.com/openai/v1")
    print("    export MODEL_NAME=llama-3.3-70b-versatile")
    print("    export API_KEY=gsk_xxxxx\n")
    print("  Local Ollama:")
    print("    export API_BASE_URL=http://localhost:11434/v1")
    print("    export MODEL_NAME=llama3")
    print("    export API_KEY=not-needed\n")
    print("=" * 60)
    sys.exit(1)

if not MODEL_NAME:
    print("ERROR: MODEL_NAME environment variable is required")
    sys.exit(1)

if not API_KEY:
    print("ERROR: Missing auth token. Set API_KEY (preferred), or HF_TOKEN/OPENAI_API_KEY.")
    sys.exit(1)

FALLBACK_ACTION = json.dumps({
    "action_type": "request_changes",
    "comments": [],
    "suggestions": [],
    "final_decision": "changes_requested"
})


def add_line_numbers(code: str) -> str:
    lines = code.split("\n")
    return "\n".join(f"{i+1}: {line}" for i, line in enumerate(lines))


class LLMClient:

    def __init__(self, base_url: str, api_key: str, model: str):
        self.base_url = base_url.rstrip("/")
        self.api_key = api_key
        self.model = model
        self.client = OpenAI(
            base_url=self.base_url,
            api_key=self.api_key,
            timeout=REQUEST_TIMEOUT
        )
        print("Connected using OpenAI client")
        print(f"Endpoint: {self.base_url}")
        print(f"Model: {self.model}\n")

    def chat_completion(self, messages: list, temperature: float = 0.7, max_tokens: int = 2000) -> str:
        completion = self.client.chat.completions.create(
            model=self.model,
            messages=messages,
            temperature=temperature,
            max_tokens=max_tokens,
            stream=False,
        )
        return completion.choices[0].message.content or ""


class CodeReviewAgent:

    def __init__(self):
        self.client = LLMClient(API_BASE_URL, API_KEY, MODEL_NAME)
        self.history = []
        self.phase = 1

    def get_action(self, observation: Dict[str, Any]) -> str:

        system_prompt = """You are an expert code reviewer. You MUST follow this exact sequence:

PHASE 1 - Add Comments: Use action_type "add_comment" to identify ALL bugs with exact line numbers
PHASE 2 - Suggest Fixes: Use action_type "suggest_fix" to provide fixes for every bug found
PHASE 3 - Final Decision: Use action_type "request_changes" with final_decision "changes_requested"

RULES:
- NEVER skip straight to approve or request_changes without first adding comments and suggestions
- NEVER combine phases - each action should do ONE thing
- ALWAYS use the exact line numbers shown in the code diff
- ALWAYS set severity for comments: "critical", "high", "medium", or "low"
- If no bugs found in Phase 1, skip to Phase 3 with "approved"

Respond ONLY with a valid JSON object, no extra text:
{
    "action_type": "add_comment" | "suggest_fix" | "approve" | "request_changes",
    "comments": [
        {
            "line_number": <exact line number>,
            "content": "Detailed explanation of the bug",
            "is_issue": true,
            "severity": "critical" | "high" | "medium" | "low"
        }
    ],
    "suggestions": [
        {
            "original_line": <exact line number>,
            "suggested_code": "corrected code here",
            "explanation": "why this fix works"
        }
    ],
    "final_decision": "approved" | "changes_requested"
}"""

        prev_comments = observation.get('previous_comments', [])
        prev_suggestions = observation.get('previous_suggestions', [])

        comments_text = "\n".join([
            f"  Line {c.get('line_number') if isinstance(c, dict) else c.line_number}: "
            f"{c.get('content') if isinstance(c, dict) else c.content}"
            for c in prev_comments
        ]) or "None yet"

        suggestions_text = "\n".join([
            f"  Line {s.get('original_line') if isinstance(s, dict) else s.original_line}: "
            f"{s.get('suggested_code') if isinstance(s, dict) else s.suggested_code}"
            for s in prev_suggestions
        ]) or "None yet"

        if self.phase == 1:
            phase_instruction = """
YOUR TASK NOW (Phase 1 - Add Comments):
- action_type MUST be "add_comment"
- Carefully read the code diff line by line
- Find ALL bugs, vulnerabilities, or issues
- Comment on each one with the EXACT line number shown
- Do NOT make a final decision yet
- Do NOT suggest fixes yet
"""
        elif self.phase == 2:
            phase_instruction = """
YOUR TASK NOW (Phase 2 - Suggest Fixes):
- action_type MUST be "suggest_fix"
- For every bug you commented on, provide a concrete code fix
- Use the same line numbers as your comments
- Do NOT make a final decision yet
"""
        else:
            phase_instruction = """
YOUR TASK NOW (Phase 3 - Final Decision):
- action_type MUST be "request_changes"
- Set final_decision to "changes_requested"
- No new comments or suggestions needed
"""

        user_prompt = f"""
Code Review Task:
{observation.get('task_description', 'Review the following code changes')}

Code Diff (USE THESE EXACT LINE NUMBERS in your response):
{add_line_numbers(observation.get('code_diff', ''))}

File Context:
{observation.get('file_context', '')}

Current Step: {observation.get('current_step', 0)}/{observation.get('max_steps', 50)}

Comments already made:
{comments_text}

Suggestions already made:
{suggestions_text}

{phase_instruction}

Respond with JSON only.
"""

        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}
        ]

        try:
            response = self.client.chat_completion(messages, TEMPERATURE, MAX_TOKENS)
            response = response.strip()

            if "```json" in response:
                response = response.split("```json")[1].split("```")[0]
            elif "```" in response:
                response = response.split("```")[1].split("```")[0]

            action_data = json.loads(response.strip())

            if "action_type" not in action_data:
                action_data["action_type"] = "request_changes"
            if "comments" not in action_data:
                action_data["comments"] = []
            if "suggestions" not in action_data:
                action_data["suggestions"] = []

            self.phase += 1
            return json.dumps(action_data)

        except json.JSONDecodeError as e:
            print(f"Failed to parse JSON response: {e}")
            print(f"Raw response: {response[:200]}...")
            self.phase += 1
            return FALLBACK_ACTION
        except Exception as e:
            print(f"Error getting action from LLM: {e}")
            return FALLBACK_ACTION

    def validate_action(self, action: Dict, observation: Dict) -> Dict:
        line_count = observation.get('line_count', 999)

        for comment in action.get("comments", []):
            comment["line_number"] = max(1, min(comment.get("line_number", 1), line_count))
            if not comment.get("severity"):
                comment["severity"] = "medium"
            if "is_issue" not in comment:
                comment["is_issue"] = True

        for suggestion in action.get("suggestions", []):
            suggestion["original_line"] = max(1, min(suggestion.get("original_line", 1), line_count))

        return action

    def parse_action(self, action_str: str) -> Dict[str, Any]:
        try:
            return json.loads(action_str)
        except json.JSONDecodeError:
            return {"action_type": "request_changes", "comments": [], "suggestions": []}


def main():
    sys.path.append('.')

    try:
        from environment.env import CodeReviewEnv
    except ImportError as e:
        print(f"Failed to import environment: {e}")
        print("Make sure you're in the correct directory and environment is installed.")
        sys.exit(1)

    parser = argparse.ArgumentParser(description="Run code review agent")
    parser.add_argument("--task-id", type=str, default="bug_detection_easy_1")
    parser.add_argument("--max-steps", type=int, default=50)
    parser.add_argument("--output", type=str, default="baseline_results.json")
    args = parser.parse_args()

    print("=" * 60)
    print("Code Review Agent")
    print("=" * 60)

    env = CodeReviewEnv()
    env.max_steps = args.max_steps
    agent = CodeReviewAgent()

    obs = env.reset(task_id=args.task_id)
    done = False
    step = 0
    total_reward = 0.0

    print(f"\nTask    : {args.task_id}")
    print(f"Desc    : {obs.get('task_description', 'N/A')}")
    print(f"Model   : {MODEL_NAME}")
    print("-" * 60)

    while not done and step < args.max_steps:
        action_str = agent.get_action(obs)
        action = agent.parse_action(action_str)
        action = agent.validate_action(action, obs)

        obs, reward, done, info = env.step(action)
        total_reward += reward
        step += 1

        print(f"\nStep {step}/{args.max_steps}:")
        print(f"  Phase       : {agent.phase - 1}")
        print(f"  Action      : {action.get('action_type')}")
        print(f"  Comments    : {len(action.get('comments', []))}")
        print(f"  Suggestions : {len(action.get('suggestions', []))}")
        print(f"  Reward      : {reward:.3f}")
        print(f"  Total       : {total_reward:.3f}")
        print(f"  Score       : {info.get('task_score', 0):.3f}")

        if info.get('last_action_valid') is False:
            print(f"  Warning     : {info.get('error', 'Invalid action')}")

    final_score = env.get_task_score()

    print("\n" + "=" * 60)
    print("Final Results:")
    print(f"  Task         : {args.task_id}")
    print(f"  Total Reward : {total_reward:.3f}")
    print(f"  Task Score   : {final_score:.3f}/1.0")
    print(f"  Steps        : {step}")
    print("=" * 60)

    env.close()

    results = {
        "task_id": args.task_id,
        "total_reward": round(total_reward, 4),
        "task_score": round(final_score, 4),
        "steps": step,
        "max_steps": args.max_steps,
        "provider": "openai-client",
        "model": MODEL_NAME,
        "api_base_url": API_BASE_URL
    }

    with open(args.output, "w") as f:
        json.dump(results, f, indent=2)

    print(f"\nResults saved to {args.output}")


if __name__ == "__main__":
    main()