File size: 15,417 Bytes
cb00361
26228ef
cb00361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e497738
cb00361
 
 
 
 
 
 
 
e497738
cb00361
 
 
 
 
 
 
 
 
 
 
 
89076a2
cb00361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4928c2c
 
cb00361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e497738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89076a2
 
 
 
 
 
 
 
 
 
 
e497738
 
89076a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e497738
cb00361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Production LangGraph node functions (`src/agent/nodes.py`).

Every node takes `(state: AgentState, config: RunnableConfig)` and returns a partial
state update dict. Dependencies (`llm`, `sandbox_backend`, `commit_in_git`) are read
from `config["configurable"]`, with sensible defaults so tests can inject fakes while
production uses Groq + auto sandbox selection.
"""

from __future__ import annotations

import logging
import re
from typing import Any

from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.runnables import RunnableConfig
from langgraph.types import interrupt

from agent.llm import (
    DetectedBugProposal,
    FixProposal,
    ReproTestProposal,
    create_groq_llm,
    invoke_structured,
    invoke_with_backoff,
)
from agent.prompts import (
    ANALYZE_SYSTEM_PROMPT,
    DETECT_BUG_SYSTEM_PROMPT,
    FIX_SYSTEM_PROMPT,
    REPRO_SYSTEM_PROMPT,
    build_fix_user_prompt,
    build_repro_user_prompt,
)
from agent.state import (
    AgentState,
    AgentStatus,
    ApprovalDecision,
    FilePatch,
    FixAttempt,
    RepoContext,
    RepoFinding,
    ReproTest,
    RunBudget,
    SandboxResult,
)
from sandbox.base import Sandbox
from sandbox.factory import create_sandbox
from tools.file_tools import (
    EditError,
    PathEscapeError,
    Workspace,
    validate_repro_path,
)
from tools.git_tools import apply_workspace_to_repo

logger = logging.getLogger(__name__)

MAX_RELEVANT_FILES = 4
MAX_FILE_SNIPPET_CHARS = 2200


def _get_llm(config: RunnableConfig) -> BaseChatModel:
    cfg = config.get("configurable", {})
    if "llm" in cfg and cfg["llm"] is not None:
        return cfg["llm"]
    return create_groq_llm(
        model=cfg.get("groq_model"),
        api_key=cfg.get("groq_api_key"),
    )


def _run_in_sandbox(ws: Workspace, command: str, config: RunnableConfig) -> SandboxResult:
    cfg = config.get("configurable", {})
    injected: Sandbox | None = cfg.get("sandbox")
    if injected is not None:
        return injected.run_tests(command)
    backend = cfg.get("sandbox_backend", "auto")
    with create_sandbox(backend) as sb:
        sb.start(ws.work)
        return sb.run_tests(command)


def _extract_tokens(text: str) -> list[str]:
    words = re.findall(r"[A-Za-z_][A-Za-z0-9_]{2,}", text)
    stop = {"the", "and", "for", "with", "that", "this", "from", "returns", "instead", "should", "when"}
    return [w for w in dict.fromkeys(words) if w.lower() not in stop]


# --------------------------------------------------------------------------- nodes


def ingest_issue(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Validate issue input, initialize workspace copy and budget."""
    issue = state["issue"]
    budget = state.get("budget") or RunBudget()
    ws_root = state.get("workspace_root")
    if not ws_root:
        ws = Workspace.create(issue.repo_path)
        ws_root = str(ws.root)
    return {
        "budget": budget,
        "iteration": state.get("iteration", 0),
        "repro_attempts": state.get("repro_attempts", 0),
        "workspace_root": ws_root,
        "status": AgentStatus.INGESTED,
    }


def inspect_repo(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Build directory tree and pick the top relevant Python files for the issue."""
    ws = Workspace.open(state["workspace_root"])
    issue = state["issue"]
    tree = ws.list_tree()

    tokens = _extract_tokens(f"{issue.title}\n{issue.body}")
    grep_hits = ws.grep_counts(tokens)
    py_files = ws.list_python_files()

    # Prefer source files over tests when ranking relevance, then fall back to all .py files
    ranked = sorted(
        py_files,
        key=lambda p: (
            0 if not (p.startswith("test") or "/test" in p or p.endswith("_test.py")) else 1,
            -grep_hits.get(p, 0),
            len(p),
        ),
    )
    selected = ranked[:MAX_RELEVANT_FILES]
    relevant = {p: ws.read(p, max_chars=MAX_FILE_SNIPPET_CHARS) for p in selected}

    repo = RepoContext(
        tree=tree,
        relevant_files=relevant,
        test_command="pytest -q -p no:cacheprovider",
        language="python",
    )

    # If the user left the default calc.py sample issue on a custom repository that has no calc.py,
    # or requested auto-detection, let the LLM audit the repo and formulate a real testable issue.
    updates: dict[str, Any] = {"repo": repo, "status": AgentStatus.INSPECTED}
    has_calc = any(p.endswith("calc.py") for p in py_files)
    is_default_calc_issue = "average()" in issue.title and "calc.average" in issue.body
    wants_auto = issue.title.strip().lower().startswith("auto") or (
        is_default_calc_issue and not has_calc and bool(relevant)
    )
    if wants_auto:
        try:
            llm = _get_llm(config)
            files_block = "\n\n".join(
                f"### File: `{p}`\n```python\n{src}\n```" for p, src in relevant.items()
            )
            detected = invoke_structured(
                llm,
                DetectedBugProposal,
                DETECT_BUG_SYSTEM_PROMPT,
                f"Repository Tree:\n```\n{tree}\n```\n\nSource Files:\n{files_block}",
            )
            updates["issue"] = issue.model_copy(
                update={"title": detected.title, "body": detected.body}
            )
            if detected.file_path in py_files and detected.file_path not in relevant:
                relevant[detected.file_path] = ws.read(
                    detected.file_path, max_chars=MAX_FILE_SNIPPET_CHARS
                )
            findings = list(detected.findings) if detected.findings else [
                RepoFinding(
                    severity="High",
                    file_path=detected.file_path,
                    title=detected.title,
                    description=detected.body,
                    suggested_fix="Apply automated search/replace fix verified by pytest.",
                )
            ]
            repo = repo.model_copy(update={"relevant_files": relevant, "findings": findings})
            updates["repo"] = repo
        except Exception as exc:  # noqa: BLE001
            logger.warning("Auto-detect bug fallback skipped: %s", exc)
    if not updates["repo"].findings:
        primary_file = next(iter(relevant.keys()), "repository")
        updates["repo"] = updates["repo"].model_copy(
            update={
                "findings": [
                    RepoFinding(
                        severity="High",
                        file_path=primary_file,
                        title=updates.get("issue", issue).title,
                        description=updates.get("issue", issue).body,
                        suggested_fix="Apply targeted code fix and verify with regression test.",
                    )
                ]
            }
        )
    return updates


def write_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Ask the LLM to write a minimal failing pytest reproduction test."""
    ws = Workspace.open(state["workspace_root"])
    llm = _get_llm(config)

    prev_out = ""
    if state.get("repro_result") is not None:
        r = state["repro_result"]
        prev_out = f"{r.stdout}\n{r.stderr}".strip()

    user_prompt = build_repro_user_prompt(state, last_repro_stdout=prev_out)
    proposal = invoke_structured(llm, ReproTestProposal, REPRO_SYSTEM_PROMPT, user_prompt)

    try:
        rel_path = validate_repro_path(proposal.path)
    except PathEscapeError:
        rel_path = "test_repro_bug.py"

    ws.write(rel_path, proposal.content)
    repro = ReproTest(path=rel_path, content=proposal.content, confirmed_failing=False)
    return {
        "repro_test": repro,
        "repro_attempts": state.get("repro_attempts", 0) + 1,
        "status": AgentStatus.REPRO_WRITTEN,
    }


def run_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Run the reproduction test in the sandbox. We expect it to FAIL on buggy code."""
    ws = Workspace.open(state["workspace_root"])
    repro = state["repro_test"]
    cmd = f"pytest -q -p no:cacheprovider {repro.path}"
    res = _run_in_sandbox(ws, cmd, config)

    # Exit code 1 = tests collected and failed (desired). Exit codes 2/3/4 = syntax/import error.
    confirmed = (not res.passed) and (not res.timed_out) and res.exit_code == 1
    updated = repro.model_copy(update={"confirmed_failing": confirmed})
    return {
        "repro_test": updated,
        "repro_result": res,
        "status": AgentStatus.REPRO_CONFIRMED if confirmed else AgentStatus.REPRO_WRITTEN,
    }


def propose_fix(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Reset working copy to baseline and ask the LLM for search/replace edits."""
    ws = Workspace.open(state["workspace_root"])
    ws.reset()  # Always propose edits against clean baseline files

    current_files = {
        p: ws.read(p, max_chars=MAX_FILE_SNIPPET_CHARS)
        for p in state["repo"].relevant_files
        if ws.exists(p)
    }
    llm = _get_llm(config)
    user_prompt = build_fix_user_prompt(state, current_files)
    proposal = invoke_structured(llm, FixProposal, FIX_SYSTEM_PROMPT, user_prompt)

    next_iter = state.get("iteration", 0) + 1
    attempt = FixAttempt(
        iteration=next_iter,
        rationale=proposal.rationale,
        edits=proposal.edits,
    )
    return {
        "attempts": [attempt],
        "status": AgentStatus.FIXING,
    }


def apply_fix(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Apply the latest attempt's search/replace edits in the workspace and compute unified diffs."""
    ws = Workspace.open(state["workspace_root"])
    ws.reset()
    latest = state["attempts"][-1]
    repro = state.get("repro_test")

    for edit in latest.edits:
        if repro and edit.path.replace("\\", "/") == repro.path:
            updated = latest.model_copy(
                update={"patch_error": f"Refusing to modify reproduction test {repro.path!r}"}
            )
            return {"attempts": [updated], "iteration": latest.iteration}

    try:
        modified = ws.apply_edits(latest.edits)
    except (EditError, PathEscapeError) as exc:
        updated = latest.model_copy(update={"patch_error": str(exc)})
        return {"attempts": [updated], "iteration": latest.iteration}

    patches = [
        FilePatch(path=rel, unified_diff=ws.diff(rel))
        for rel in modified
        if ws.diff(rel).strip()
    ]
    if not patches:
        updated = latest.model_copy(update={"patch_error": "Edits produced no net change."})
        return {"attempts": [updated], "iteration": latest.iteration}

    updated = latest.model_copy(update={"patches": patches, "patch_error": None})
    return {"attempts": [updated]}


def run_tests(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Run the reproduction test + full test suite inside the sandbox."""
    ws = Workspace.open(state["workspace_root"])
    latest = state["attempts"][-1]
    res = _run_in_sandbox(ws, state["repo"].test_command, config)
    updated = latest.model_copy(update={"result": res})
    return {
        "attempts": [updated],
        "iteration": latest.iteration,
        "status": AgentStatus.TESTS_PASSING if res.passed else AgentStatus.FIXING,
    }


def analyze_failure(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Produce a short failure diagnosis for the next `propose_fix` iteration."""
    approval = state.get("approval")
    if approval and not approval.approved and approval.feedback:
        return {
            "diagnosis": f"Reviewer requested changes: {approval.feedback}",
            "approval": None,
        }

    latest = state["attempts"][-1]
    if latest.patch_error:
        return {
            "diagnosis": (
                f"Attempt {latest.iteration} edits could not be applied: {latest.patch_error}. "
                "Copy `search` blocks verbatim from the baseline file."
            )
        }

    out = ""
    if latest.result:
        out = (latest.result.stdout + "\n" + latest.result.stderr).strip()[
            -state["budget"].max_tool_output_chars :
        ]
    try:
        llm = _get_llm(config)
        msg = invoke_with_backoff(
            llm,
            [
                SystemMessage(content=ANALYZE_SYSTEM_PROMPT),
                HumanMessage(
                    content=(
                        f"Issue: {state['issue'].title}\n"
                        f"Attempt {latest.iteration} rationale: {latest.rationale}\n"
                        f"Test output:\n{out}"
                    )
                ),
            ],
        )
        text = msg.content if isinstance(msg.content, str) else str(msg.content)
    except Exception:  # noqa: BLE001
        text = f"Attempt {latest.iteration} failed tests; inspect assertion output and adjust logic."
    return {"diagnosis": text.strip()[:600]}


def human_approval(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Pause the graph with `interrupt()` for human review before any git commit."""
    attempt = state["attempts"][-1]
    decision = interrupt(
        {
            "iteration": attempt.iteration,
            "rationale": attempt.rationale,
            "diff": "\n".join(p.unified_diff for p in attempt.patches),
            "test_output": (attempt.result.stdout + "\n" + attempt.result.stderr).strip()
            if attempt.result
            else "",
            "repro_test": state["repro_test"].content,
        }
    )
    approval = ApprovalDecision.model_validate(decision)
    return {
        "approval": approval,
        "status": AgentStatus.APPROVED if approval.approved else AgentStatus.REJECTED,
    }


def commit_and_pr(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Commit approved changes (reachable ONLY after `human_approval`)."""
    ws = Workspace.open(state["workspace_root"])
    issue = state["issue"]
    cfg = config.get("configurable", {})
    commit_in_git = bool(cfg.get("commit_in_git", False))
    slug = f"agent/fix-{issue.number or 'issue'}"
    msg = f"Fix: {issue.title}"

    res = apply_workspace_to_repo(
        ws,
        issue.repo_path,
        branch_name=slug,
        commit_message=msg,
        commit_in_git=commit_in_git,
    )
    return {
        "branch": res.branch,
        "commit_sha": res.commit_sha,
        "pr_url": res.pr_url,
        "status": AgentStatus.COMMITTED,
    }


def escalate(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Terminal failure handler when reproduction or fix iterations are exhausted, or rejected."""
    approval = state.get("approval")
    repro = state.get("repro_test")
    if approval is not None and not approval.approved and not approval.feedback:
        reason = "Human reviewer rejected the proposed fix."
    elif repro is not None and not repro.confirmed_failing:
        reason = (
            f"Could not reproduce the bug with a failing pytest test after "
            f"{state.get('repro_attempts', 0)} attempt(s)."
        )
    else:
        reason = (
            f"Exhausted {state.get('iteration', 0)}/{state['budget'].max_iterations} "
            "fix iterations without all tests passing."
        )
    return {"error": reason, "status": AgentStatus.ESCALATED}