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8
25e2686
 
e63f982
25e2686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc71cad
 
 
25e2686
dc71cad
e63f982
25e2686
e63f982
 
dc71cad
 
e63f982
dc71cad
 
e63f982
dc71cad
e63f982
 
dc71cad
 
 
e63f982
dc71cad
e63f982
dc71cad
 
 
 
 
 
 
 
 
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
"""
agent/reflection_agent.py
──────────────────────────
Agentic Reflection Loop β€” self-correcting bug-fix agent.

Loop (max 3 attempts):
  1. Localise relevant files (from Phase 3 pipeline)
  2. Build prompt: issue + file contents + (on retry) error context
  3. Call LLM β†’ get unified diff
  4. Apply patch (git apply)
  5. Run tests (sandbox)
  6. If PASS β†’ done βœ…
  7. If FAIL β†’ categorise failure, update prompt with error context β†’ goto 2

On each iteration the agent:
  - Reads the exact pytest error output
  - Appends it to the prompt with a targeted correction request
  - The LLM sees the code it wrote AND the test failure it caused

This is the "genuinely ML hard" part:
  - Each trajectory is logged as JSONL (for Phase 7 fine-tuning)
  - Failure categories are tracked in MLflow
  - Token cost is metered per attempt

LangGraph is used to model the state machine: each node is one step,
edges have conditional routing based on test outcome.
"""
from __future__ import annotations

import logging
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Literal, Optional

logger = logging.getLogger(__name__)

# ── State ─────────────────────────────────────────────────────────────────────

@dataclass
class AgentState:
    """Mutable state passed between LangGraph nodes."""
    instance_id: str
    repo: str
    problem_statement: str
    base_commit: str
    fail_to_pass: list[str]
    pass_to_pass: list[str]
    workspace_dir: Path

    # Filled during execution
    localised_files: list[str] = field(default_factory=list)
    file_contents: dict[str, str] = field(default_factory=dict)  # path β†’ content
    attempts: list[dict] = field(default_factory=list)           # attempt records
    current_attempt: int = 0
    last_patch: str = ""
    last_test_stdout: str = ""
    last_failure_category: str = "unknown"
    resolved: bool = False
    error: str = ""  # non-empty if agent crashed

    # Token tracking
    total_tokens: int = 0


# ── Prompt templates ──────────────────────────────────────────────────────────

SYSTEM_PROMPT = """\
You are an expert Python software engineer specialising in bug fixes.
Your task is to fix a bug in a Python repository by generating a minimal unified diff.

Rules:
- Output ONLY the unified diff. No explanations, no markdown code fences.
- Start with '--- a/<file>' and use proper unified diff format.
- Be minimal: only change what is necessary to fix the bug.
- If multiple files need changes, include all in one diff.
- Do not remove or modify unrelated code.
- Ensure your Python syntax is valid.
"""

INITIAL_PROMPT_TEMPLATE = """\
## GitHub Issue
{problem_statement}

## Relevant Files
{file_context}

Generate a unified diff patch that fixes this issue.
"""

REFLECTION_PROMPT_TEMPLATE = """\
## GitHub Issue
{problem_statement}

## Relevant Files
{file_context}

## Previous Attempt #{attempt_num} FAILED
Failure category: {failure_category}

### Test Output (showing failures)
{error_context}

### Your Previous Patch
{previous_patch}

The patch above did not fully fix the issue. Carefully analyse the test failures
and generate a CORRECTED unified diff. Focus specifically on the error shown above.
"""


# ── LangGraph node functions ──────────────────────────────────────────────────

def node_localise(state: AgentState, pipeline=None) -> AgentState:
    """
    Node: run the localisation pipeline to find relevant files.
    If pipeline is None, reads file_contents from state (already provided).
    """
    if pipeline and not state.file_contents:
        result = pipeline.localise(state.problem_statement, top_k=5)
        state.localised_files = result.top_k_paths
        logger.info(
            "Localised %d files for %s", len(state.localised_files), state.instance_id
        )

    # Read file contents from workspace
    from agent.tools import AgentTools
    tools = AgentTools(state.workspace_dir)
    for fp in state.localised_files:
        read_result = tools.read_file(fp, max_lines=150)
        if read_result.success:
            state.file_contents[fp] = read_result.output
        else:
            logger.debug("Could not read %s: %s", fp, read_result.error)

    return state


def node_generate_patch(state: AgentState, llm_client=None, model: str = "gpt-4o") -> AgentState:
    """
    Node: call LLM to generate a patch.
    First attempt uses initial prompt; subsequent attempts use reflection prompt.
    """
    state.current_attempt += 1

    file_context = _build_file_context(state.file_contents)

    if state.current_attempt == 1:
        user_prompt = INITIAL_PROMPT_TEMPLATE.format(
            problem_statement=state.problem_statement[:2000],
            file_context=file_context,
        )
    else:
        from agent.failure_categoriser import extract_first_error_context
        error_context = extract_first_error_context(state.last_test_stdout)

        user_prompt = REFLECTION_PROMPT_TEMPLATE.format(
            problem_statement=state.problem_statement[:1500],
            file_context=file_context,
            attempt_num=state.current_attempt - 1,
            failure_category=state.last_failure_category,
            error_context=error_context[:800],
            previous_patch=state.last_patch[:1000],
        )

    logger.info(
        "Generating patch for %s (attempt %d/%d)",
        state.instance_id, state.current_attempt, 3
    )

    patch_text, usage = _call_llm(user_prompt, llm_client, model)
    state.last_patch = _strip_code_fences(patch_text)
    state.total_tokens += usage.get("total_tokens", 0)
    return state


def node_apply_and_test(state: AgentState, sandbox=None) -> AgentState:
    """
    Node: apply the patch and run tests.
    Populates state.resolved and state.last_test_stdout.
    """
    from agent.tools import AgentTools
    tools = AgentTools(state.workspace_dir, sandbox)

    # Write and apply patch
    write_result = tools.write_patch(state.last_patch)
    patch_apply_success = False

    if write_result.success:
        if sandbox:
            from sandbox.executor import SandboxExecutor
            apply_result = sandbox.apply_patch(state.last_patch, state.workspace_dir)
            patch_apply_success = apply_result.success
        else:
            import subprocess
            try:
                proc = subprocess.run(
                    ["git", "apply", "--whitespace=fix", "_agent_patch.diff"],
                    capture_output=True, text=True, cwd=str(state.workspace_dir), timeout=10
                )
                patch_apply_success = proc.returncode == 0
            except Exception:
                patch_apply_success = False

    # Run tests
    all_test_ids = state.fail_to_pass + state.pass_to_pass
    test_result_obj = tools.run_tests(all_test_ids)
    state.last_test_stdout = test_result_obj.metadata.get("full_output", test_result_obj.output)

    # Parse results
    if sandbox:
        from sandbox.executor import SandboxExecutor
        test_result = sandbox.run_tests(state.workspace_dir, all_test_ids)
        resolved, ftp_results, ptp_results = test_result.check_tests(
            state.fail_to_pass, state.pass_to_pass
        )
        state.last_test_stdout = test_result.raw_output
    else:
        # Minimal local parse
        ftp_results = _parse_local_test_results(
            state.last_test_stdout, state.fail_to_pass
        )
        ptp_results = _parse_local_test_results(
            state.last_test_stdout, state.pass_to_pass
        )
        resolved = all(ftp_results.values()) and all(ptp_results.values())

    state.resolved = resolved

    # Categorise failure
    from agent.failure_categoriser import categorise_failure
    prev_cats = [a.get("failure_category", "unknown") for a in state.attempts]
    state.last_failure_category = categorise_failure(
        test_stdout=state.last_test_stdout,
        patch_apply_success=patch_apply_success,
        fail_to_pass_results=ftp_results,
        pass_to_pass_results=ptp_results,
        attempt_num=state.current_attempt,
        previous_categories=prev_cats,
    )

    # Record attempt
    state.attempts.append({
        "attempt_num": state.current_attempt,
        "patch": state.last_patch,
        "test_stdout": state.last_test_stdout[:3000],
        "fail_to_pass_results": ftp_results,
        "pass_to_pass_results": ptp_results,
        "resolved": resolved,
        "failure_category": state.last_failure_category,
    })

    logger.info(
        "Attempt %d: resolved=%s category=%s",
        state.current_attempt, resolved, state.last_failure_category
    )
    return state


def should_retry(state: AgentState, max_attempts: int = 3) -> Literal["retry", "done"]:
    """LangGraph conditional edge: retry if not resolved and budget remains."""
    if state.resolved:
        return "done"
    if state.current_attempt >= max_attempts:
        return "done"
    return "retry"


# ── Full agent ────────────────────────────────────────────────────────────────

class ReflectionAgent:
    """
    Self-correcting bug-fix agent with configurable retry budget.

    Uses LangGraph for state machine management if available,
    falls back to a simple Python loop otherwise.
    """

    def __init__(
        self,
        model: str = "gpt-4o",
        max_attempts: int = 3,
        sandbox=None,
        localisation_pipeline=None,
        trajectory_logger=None,
    ):
        self.model = model
        self.max_attempts = max_attempts
        self.sandbox = sandbox
        self.pipeline = localisation_pipeline
        self.traj_logger = trajectory_logger
        self._use_langgraph = self._check_langgraph()

    def _check_langgraph(self) -> bool:
        try:
            import langgraph  # noqa: F401
            return True
        except ImportError:
            logger.debug("LangGraph not installed β€” using simple loop")
            return False

    def run(
        self,
        instance_id: str,
        repo: str,
        problem_statement: str,
        base_commit: str,
        fail_to_pass: list[str],
        pass_to_pass: list[str],
        workspace_dir: Path,
        localised_files: list[str] | None = None,
    ) -> AgentState:
        """
        Run the full reflection loop on one SWE-bench instance.

        Returns final AgentState (resolved/not, all attempts recorded).
        """
        state = AgentState(
            instance_id=instance_id,
            repo=repo,
            problem_statement=problem_statement,
            base_commit=base_commit,
            fail_to_pass=fail_to_pass,
            pass_to_pass=pass_to_pass,
            workspace_dir=Path(workspace_dir),
            localised_files=localised_files or [],
        )

        if self._use_langgraph:
            state = self._run_with_langgraph(state)
        else:
            state = self._run_simple_loop(state)

        # Log trajectories
        if self.traj_logger:
            self._log_trajectories(state)

        return state

    def _run_simple_loop(self, state: AgentState) -> AgentState:
        """Fallback: plain Python loop (no LangGraph dependency)."""
        # Localise files
        state = node_localise(state, self.pipeline)

        for _ in range(self.max_attempts):
            # Generate patch
            state = node_generate_patch(state, model=self.model)
            # Apply and test
            state = node_apply_and_test(state, self.sandbox)
            # Check outcome
            if should_retry(state, self.max_attempts) == "done":
                break

        return state

    def _run_with_langgraph(self, state: AgentState) -> AgentState:
        """LangGraph state machine β€” same logic, better observability."""
        try:
            from langgraph.graph import StateGraph, END

            pipeline = self.pipeline
            sandbox = self.sandbox
            model = self.model
            max_attempts = self.max_attempts

            graph = StateGraph(AgentState)

            graph.add_node("localise", lambda s: node_localise(s, pipeline))
            graph.add_node("generate", lambda s: node_generate_patch(s, model=model))
            graph.add_node("test",     lambda s: node_apply_and_test(s, sandbox))

            graph.set_entry_point("localise")
            graph.add_edge("localise", "generate")
            graph.add_edge("generate", "test")
            graph.add_conditional_edges(
                "test",
                lambda s: should_retry(s, max_attempts),
                {"retry": "generate", "done": END},
            )

            app = graph.compile()
            final = app.invoke(state)

            # LangGraph may return a plain dict instead of AgentState.
            # Normalise back to the dataclass so downstream code is consistent.
            if isinstance(final, dict):
                final = AgentState(**{
                    k: final[k] for k in AgentState.__dataclass_fields__
                    if k in final
                })

            return final

        except Exception as e:
            logger.warning("LangGraph failed (%s) β€” falling back to simple loop", e)
            return self._run_simple_loop(state)

    def _log_trajectories(self, state: AgentState) -> None:
        """Write all attempt records to the trajectory logger."""
        from agent.trajectory_logger import TrajectoryEntry

        # Handle both AgentState dataclass and plain dict (LangGraph compat)
        if isinstance(state, dict):
            attempts    = state.get("attempts", [])
            instance_id = state.get("instance_id", "")
            repo        = state.get("repo", "")
            localised   = state.get("localised_files", [])
            problem     = state.get("problem_statement", "")
        else:
            attempts    = state.attempts
            instance_id = state.instance_id
            repo        = state.repo
            localised   = state.localised_files
            problem     = state.problem_statement

        for attempt_data in attempts:
            entry = TrajectoryEntry(
                instance_id=instance_id,
                repo=repo,
                attempt=attempt_data["attempt_num"],
                patch=attempt_data["patch"],
                test_stdout=attempt_data["test_stdout"],
                fail_to_pass_results=attempt_data["fail_to_pass_results"],
                pass_to_pass_results=attempt_data["pass_to_pass_results"],
                resolved=attempt_data["resolved"],
                failure_category=attempt_data["failure_category"],
                elapsed_seconds=0.0,
                localised_files=localised,
                problem_statement=problem,
                token_cost={},
            )
            self.traj_logger.log(entry)


# ── Helpers ───────────────────────────────────────────────────────────────────

def _build_file_context(file_contents: dict[str, str], max_files: int = 5) -> str:
    """Build a formatted string of file contents for the LLM prompt."""
    parts = []
    for fp, content in list(file_contents.items())[:max_files]:
        parts.append(f"### {fp}\n```python\n{content[:1500]}\n```")
    return "\n\n".join(parts)


def _strip_code_fences(text: str) -> str:
    """Remove ```diff``` / ``` fences from LLM output."""
    import re
    text = re.sub(r"```(?:diff|patch)?\s*\n", "", text)
    text = re.sub(r"\n?```\s*$", "", text, flags=re.MULTILINE)
    return text.strip()


def _call_llm(
    user_prompt: str,
    client=None,
    model: str = "gpt-4o",
) -> tuple[str, dict]:
    """
    Call the configured LLM provider via httpx (Groq) or OpenAI SDK.
    Uses httpx directly for Groq to avoid SDK connection issues in HF Spaces.
    Returns (patch_text, usage_dict).
    """
    import os
    from configs.settings import settings

    provider = (os.environ.get("LLM_PROVIDER") or settings.llm_provider).lower()
    effective_model = os.environ.get("LLM_MODEL") or settings.llm_model

    # ── Groq via httpx directly (most reliable in containerised envs) ──────
    if client is None and provider == "groq":
        import httpx
        api_key = (os.environ.get("GROQ_API_KEY") or settings.groq_api_key).strip()
        if not api_key:
            raise ValueError("GROQ_API_KEY is not set. Add it as an env var or HF Space secret.")

        logger.info("Calling Groq API: model=%s", effective_model)
        try:
            with httpx.Client(timeout=120.0) as http:
                resp = http.post(
                    "https://api.groq.com/openai/v1/chat/completions",
                    headers={
                        "Authorization": f"Bearer {api_key}",
                        "Content-Type": "application/json",
                    },
                    json={
                        "model": effective_model,
                        "messages": [
                            {"role": "system", "content": SYSTEM_PROMPT},
                            {"role": "user",   "content": user_prompt},
                        ],
                        "max_tokens": settings.llm_max_tokens,
                        "temperature": settings.llm_temperature,
                    },
                )
                resp.raise_for_status()
                data = resp.json()

            patch_text = data["choices"][0]["message"]["content"] or ""
            usage_raw  = data.get("usage", {})
            return patch_text, {
                "prompt_tokens":     usage_raw.get("prompt_tokens", 0),
                "completion_tokens": usage_raw.get("completion_tokens", 0),
                "total_tokens":      usage_raw.get("total_tokens", 0),
            }
        except httpx.HTTPStatusError as e:
            raise RuntimeError(f"Groq API error {e.response.status_code}: {e.response.text[:300]}") from e
        except httpx.ConnectError as e:
            raise RuntimeError(f"Cannot reach Groq API β€” check network / GROQ_API_KEY: {e}") from e

    # ── OpenAI SDK fallback ────────────────────────────────────────────────
    if client is None:
        try:
            from openai import OpenAI
            client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY") or settings.openai_api_key or None)
        except ImportError as e:
            raise ImportError(
                "No LLM client available. Set LLM_PROVIDER=groq + GROQ_API_KEY, "
                "or install openai: pip install openai"
            ) from e

    response = client.chat.completions.create(
        model=effective_model,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user",   "content": user_prompt},
        ],
        max_tokens=settings.llm_max_tokens,
        temperature=settings.llm_temperature,
    )
    patch_text = response.choices[0].message.content or ""
    usage = {
        "prompt_tokens":     response.usage.prompt_tokens,
        "completion_tokens": response.usage.completion_tokens,
        "total_tokens":      response.usage.total_tokens,
    }
    return patch_text, usage


def _parse_local_test_results(test_stdout: str, test_ids: list[str]) -> dict[str, bool]:
    """Parse local pytest output to get pass/fail per test ID."""
    import re
    passed = set(re.findall(r"^(.+?::[\w\[\]-]+)\s+PASSED", test_stdout, re.MULTILINE))
    return {tid: tid in passed for tid in test_ids}