File size: 9,569 Bytes
eb60226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89076a2
eb60226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89076a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb60226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""DEMO ONLY: a scripted LangGraph that mimics the real agent's control flow.

No LLM, no Docker, nothing is committed. It uses the real ``AgentState`` models, the
real conditional-routing shape from SPEC.md section 4, a real ``interrupt()`` before
``commit_and_pr``, and a real checkpointer, so the Streamlit UI exercises the same
pause/resume mechanics the production graph will use.

Scenario: ``calc.average()`` has an off-by-one bug. The scripted agent first proposes a
fix that is wrong (integer division), recovers on the second attempt, and, if the
reviewer requests changes, adds an empty-list guard on the third.
"""

from __future__ import annotations

import hashlib
import time
from typing import Any, Literal

from langchain_core.runnables import RunnableConfig
from langgraph.checkpoint.base import BaseCheckpointSaver
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, StateGraph
from langgraph.types import interrupt

from agent.state import (
    AgentState,
    AgentStatus,
    ApprovalDecision,
    FilePatch,
    FixAttempt,
    RepoContext,
    RepoFinding,
    ReproTest,
    SandboxResult,
)

DEMO_ISSUE_TITLE = "average() returns wrong values"
DEMO_ISSUE_BODY = (
    "`calc.average([2, 4, 6])` returns `3.0` instead of `4`, and "
    "`calc.average([1, 2])` returns `1.0` instead of `1.5`."
)

BUGGY_SOURCE = "def average(xs):\n    return sum(xs) / (len(xs) + 1)\n"

REPRO_TEST = '''from calc import average


def test_average_of_three():
    assert average([2, 4, 6]) == 4


def test_average_of_two_is_fractional():
    assert average([1, 2]) == 1.5
'''

REPRO_FAIL_OUTPUT = """FAILED test_repro.py::test_average_of_three - assert 3.0 == 4
FAILED test_repro.py::test_average_of_two_is_fractional - assert 1.0 == 1.5
2 failed in 0.04s"""

# (rationale, new line(s) of the function body, diff, test passes?, pytest output)
_FIXES: list[tuple[str, str, bool, str]] = [
    (
        "The denominator has an extra +1. Remove it and use integer division.",
        "    return sum(xs) // len(xs)",
        False,
        "FAILED test_repro.py::test_average_of_two_is_fractional - assert 1 == 1.5\n"
        "1 failed, 1 passed in 0.04s",
    ),
    (
        "Integer division truncated the fractional result. Use true division.",
        "    return sum(xs) / len(xs)",
        True,
        "2 passed in 0.03s",
    ),
    (
        "Reviewer asked for edge-case handling: reject empty input explicitly "
        "instead of raising ZeroDivisionError.",
        "    if not xs:\n"
        '        raise ValueError("average() requires at least one value")\n'
        "    return sum(xs) / len(xs)",
        True,
        "2 passed in 0.03s",
    ),
]

_OLD_LINE = "    return sum(xs) / (len(xs) + 1)"


def _pause(config: RunnableConfig) -> None:
    time.sleep(float(config.get("configurable", {}).get("delay", 0.6)))


def _diff_for(new_body: str) -> str:
    old = f"-{_OLD_LINE}"
    new = "\n".join(f"+{line}" for line in new_body.split("\n"))
    n_new = len(new_body.split("\n"))
    return f"--- a/calc.py\n+++ b/calc.py\n@@ -1,2 +1,{1 + n_new} @@\n def average(xs):\n{old}\n{new}\n"


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


def ingest_issue(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    return {"status": AgentStatus.INGESTED}


def inspect_repo(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    repo = RepoContext(
        tree="calc.py\ntests/\n",
        relevant_files={"calc.py": BUGGY_SOURCE},
        findings=[
            RepoFinding(
                severity="High",
                file_path="calc.py",
                title="Off-by-one denominator in average(xs)",
                description="`return sum(xs) / (len(xs) + 1)` divides by `len(xs) + 1`, returning wrong arithmetic averages for all inputs.",
                suggested_fix="Replace `(len(xs) + 1)` with `len(xs)`.",
            ),
            RepoFinding(
                severity="Medium",
                file_path="calc.py",
                title="Missing empty-sequence validation in average(xs)",
                description="Calling `average([])` raises an unhandled `ZeroDivisionError` instead of a descriptive `ValueError`.",
                suggested_fix="Add `if not xs: raise ValueError('average() requires at least one value')`.",
            ),
        ],
        test_command="pytest -q -p no:cacheprovider",
    )
    return {"repo": repo, "status": AgentStatus.INSPECTED}


def write_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    return {
        "repro_test": ReproTest(path="test_repro.py", content=REPRO_TEST),
        "repro_attempts": state.get("repro_attempts", 0) + 1,
        "status": AgentStatus.REPRO_WRITTEN,
    }


def run_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    repro = state["repro_test"].model_copy(update={"confirmed_failing": True})
    return {"repro_test": repro, "status": AgentStatus.REPRO_CONFIRMED}


def propose_fix(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    iteration = state.get("iteration", 0)
    rationale, body, _passes, _out = _FIXES[min(iteration, len(_FIXES) - 1)]
    attempt = FixAttempt(
        iteration=iteration + 1,
        rationale=rationale,
        patches=[FilePatch(path="calc.py", unified_diff=_diff_for(body))],
    )
    return {"attempts": [attempt], "status": AgentStatus.FIXING}


def run_tests(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    iteration = state.get("iteration", 0)
    _r, _b, passes, output = _FIXES[min(iteration, len(_FIXES) - 1)]
    result = SandboxResult(exit_code=0 if passes else 1, stdout=output, stderr="", duration_s=0.04)
    attempt = state["attempts"][-1].model_copy(update={"result": result})
    return {
        "attempts": [attempt],  # same iteration -> upserted by merge_attempts
        "iteration": iteration + 1,
        "status": AgentStatus.TESTS_PASSING if passes else AgentStatus.FIXING,
    }


def analyze_failure(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    return {}


def human_approval(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    """Pause the graph. Nothing before this line has side effects (the node re-runs on resume)."""
    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 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]:
    _pause(config)
    diff = state["attempts"][-1].patches[0].unified_diff
    sha = hashlib.sha1(diff.encode()).hexdigest()[:7]  # noqa: S324 - demo id only
    return {"branch": "agent/fix-demo", "commit_sha": sha, "status": AgentStatus.COMMITTED}


def escalate(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
    _pause(config)
    approval = state.get("approval")
    reason = (
        "Reviewer rejected the fix."
        if approval is not None and not approval.approved
        else "Iteration budget exhausted without a passing fix."
    )
    return {"error": reason, "status": AgentStatus.ESCALATED}


# -------------------------------------------------------------------------- routing


def _under_budget(state: AgentState) -> bool:
    return state["iteration"] < state["budget"].max_iterations


def route_after_tests(
    state: AgentState,
) -> Literal["human_approval", "analyze_failure", "escalate"]:
    result = state["attempts"][-1].result
    if result is not None and result.passed:
        return "human_approval"
    return "analyze_failure" if _under_budget(state) else "escalate"


def route_after_approval(
    state: AgentState,
) -> Literal["commit_and_pr", "analyze_failure", "escalate"]:
    approval = state["approval"]
    if approval is not None and approval.approved:
        return "commit_and_pr"
    if approval is not None and approval.feedback and _under_budget(state):
        return "analyze_failure"
    return "escalate"


def build_mock_graph(checkpointer: BaseCheckpointSaver | None = None):
    g = StateGraph(AgentState)
    for fn in (
        ingest_issue, inspect_repo, write_repro_test, run_repro_test, propose_fix,
        run_tests, analyze_failure, human_approval, commit_and_pr, escalate,
    ):  # fmt: skip
        g.add_node(fn.__name__, fn)

    g.add_edge(START, "ingest_issue")
    g.add_edge("ingest_issue", "inspect_repo")
    g.add_edge("inspect_repo", "write_repro_test")
    g.add_edge("write_repro_test", "run_repro_test")
    g.add_edge("run_repro_test", "propose_fix")  # scripted: the bug always reproduces
    g.add_edge("propose_fix", "run_tests")
    g.add_conditional_edges("run_tests", route_after_tests)
    g.add_edge("analyze_failure", "propose_fix")
    g.add_conditional_edges("human_approval", route_after_approval)
    g.add_edge("commit_and_pr", END)
    g.add_edge("escalate", END)
    return g.compile(checkpointer=checkpointer or MemorySaver())