Muhammad Faraz Malik commited on
Commit
eb60226
·
1 Parent(s): 96b8c55

Add Streamlit demo UI with scripted agent; merge_attempts reducer; docs

Browse files
README.md CHANGED
@@ -38,8 +38,9 @@ src/
38
  agent/ state.py (done) · graph.py · nodes.py · prompts.py
39
  sandbox/ docker_runner.py (done) · Dockerfile
40
  tools/ file_tools.py · git_tools.py
 
41
  tests/
42
- unit/ test_state.py · test_docker_runner.py · test_routing.py
43
  fixtures/ buggy_repo/
44
  SPEC.md
45
  ```
@@ -61,6 +62,15 @@ Build the sandbox image (the test suite also builds it automatically if missing)
61
  docker build -t bugfix-agent-sandbox:latest src/sandbox
62
  ```
63
 
 
 
 
 
 
 
 
 
 
64
  ## Running the tests
65
 
66
  ```bash
@@ -85,3 +95,4 @@ Tests marked `docker` execute real containers and are **skipped automatically**
85
  - [ ] **Phase 2:** `file_tools.py` (path-jailed) and `git_tools.py`
86
  - [ ] **Phase 3:** prompts, nodes, graph, Groq client with backoff and pruning, routing tests
87
  - [ ] **Phase 4:** end-to-end run on `tests/fixtures/buggy_repo`, CLI, demo
 
 
38
  agent/ state.py (done) · graph.py · nodes.py · prompts.py
39
  sandbox/ docker_runner.py (done) · Dockerfile
40
  tools/ file_tools.py · git_tools.py
41
+ ui/ streamlit_app.py · mock_graph.py (scripted demo)
42
  tests/
43
+ unit/ test_state.py · test_docker_runner.py · test_mock_graph.py · test_streamlit_app.py · test_routing.py
44
  fixtures/ buggy_repo/
45
  SPEC.md
46
  ```
 
62
  docker build -t bugfix-agent-sandbox:latest src/sandbox
63
  ```
64
 
65
+ ## Demo UI (scripted agent)
66
+
67
+ A Streamlit demo shows the workflow end to end: step-by-step progress, a failed first fix that is retried, and a real LangGraph `interrupt()` that pauses for Approve / Request changes / Reject. **The agent behind it is scripted** (no LLM, no Docker, nothing is committed), so it demonstrates the control flow and the human-approval gate, not real bug fixing.
68
+
69
+ ```bash
70
+ pip install -e ".[ui]"
71
+ streamlit run src/ui/streamlit_app.py
72
+ ```
73
+
74
  ## Running the tests
75
 
76
  ```bash
 
95
  - [ ] **Phase 2:** `file_tools.py` (path-jailed) and `git_tools.py`
96
  - [ ] **Phase 3:** prompts, nodes, graph, Groq client with backoff and pruning, routing tests
97
  - [ ] **Phase 4:** end-to-end run on `tests/fixtures/buggy_repo`, CLI, demo
98
+
SPEC.md CHANGED
@@ -1,6 +1,6 @@
1
- # Autonomous Bug-Fixing Agent — Architecture Spec
2
 
3
- Status: **DRAFT v0.1 — for review.** No implementation exists yet; every file under `src/` is a stub.
4
 
5
  ## 1. Goal & Flow
6
 
@@ -39,7 +39,7 @@ The graph state is a `TypedDict` (LangGraph requirement for reducers). Structure
39
  | Model | Fields |
40
  |---|---|
41
  | `IssueInput` | `title: str`, `body: str`, `url: str \| None`, `number: int \| None`, `repo_path: Path` |
42
- | `RepoContext` | `tree: str` (truncated listing), `relevant_files: dict[str, str]` (path → content snippet), `test_command: str`, `language: str` |
43
  | `ReproTest` | `path: str`, `content: str`, `framework: Literal["pytest"]`, `confirmed_failing: bool` |
44
  | `FilePatch` | `path: str`, `unified_diff: str` |
45
  | `FixAttempt` | `iteration: int`, `rationale: str`, `patches: list[FilePatch]`, `result: SandboxResult \| None` |
@@ -58,7 +58,7 @@ class AgentState(TypedDict, total=False):
58
  issue: IssueInput
59
  repo: RepoContext
60
  repro_test: ReproTest
61
- attempts: Annotated[list[FixAttempt], operator.add] # append-only history
62
  messages: Annotated[list[AnyMessage], add_messages] # LLM conversation (pruned)
63
  iteration: int # fix iterations consumed (0..max_iterations)
64
  repro_attempts: int # repro-test generations consumed
@@ -73,7 +73,7 @@ class AgentState(TypedDict, total=False):
73
 
74
  Rules:
75
  - Nodes return **partial updates only**; they never mutate the input state.
76
- - `attempts` is full history for the audit trail. The LLM never sees it raw; the pruner builds a compact summary (section 6).
77
  - `messages` is the only field the pruner rewrites.
78
 
79
  ## 3. Node Functions (`src/agent/nodes.py`)
@@ -121,7 +121,7 @@ def route_after_approval(state) -> Literal["commit_and_pr", "analyze_failure", "
121
  ```
122
 
123
  Invariants (each gets a test):
124
- 1. `iteration` never exceeds `max_iterations` (8). LangGraph `recursion_limit` is additionally set to a safe bound (about 4× nodes × iterations) as a hard backstop.
125
  2. `commit_and_pr` has exactly one inbound edge: from `human_approval` via `route_after_approval`.
126
  3. Timeouts and `SandboxResult.timed_out` count as failures, never as passes.
127
 
@@ -187,30 +187,30 @@ Design decisions:
187
 
188
  ```
189
  .
190
- ├── SPEC.md
191
- ├── pyproject.toml
192
- ├── .env.example
193
- ├── .gitignore
194
- ├── src/
195
- │ ├── agent/
196
- │ │ ├── state.py # models + AgentState (phase 1)
197
- │ │ ├── graph.py # build_graph, routers, checkpointer (phase 3)
198
- │ │ ├── nodes.py # node functions + pruner (phase 3)
199
- │ │ └── prompts.py # system / task prompts (phase 3)
200
- │ ├── sandbox/
201
- │ │ ├── docker_runner.py # DockerSandbox (phase 1)
202
- │ │ └── Dockerfile # python:3.12-slim + pytest, non-root user
203
- │ └── tools/
204
- │ ├── file_tools.py # read/list/grep/apply_patch, path-jail (phase 2)
205
- │ └── git_tools.py # branch/commit/push/PR (phase 2)
206
- └── tests/
207
- ├── conftest.py # `docker` marker, skip if daemon down
208
- ├── unit/
209
- │ ├── test_state.py (phase 1)
210
- │ ├── test_docker_runner.py (phase 1; integration-marked)
211
- │ └── test_routing.py (phase 3)
212
- └── fixtures/
213
- └── buggy_repo/ # tiny repo with a known bug + expected repro test
214
  ```
215
 
216
  ## 8. Phases
 
1
+ # Autonomous Bug-Fixing Agent — Architecture Spec
2
 
3
+ Status: **DRAFT v0.1 — for review.** No implementation exists yet; every file under `src/` is a stub.
4
 
5
  ## 1. Goal & Flow
6
 
 
39
  | Model | Fields |
40
  |---|---|
41
  | `IssueInput` | `title: str`, `body: str`, `url: str \| None`, `number: int \| None`, `repo_path: Path` |
42
+ | `RepoContext` | `tree: str` (truncated listing), `relevant_files: dict[str, str]` (path → content snippet), `test_command: str`, `language: str` |
43
  | `ReproTest` | `path: str`, `content: str`, `framework: Literal["pytest"]`, `confirmed_failing: bool` |
44
  | `FilePatch` | `path: str`, `unified_diff: str` |
45
  | `FixAttempt` | `iteration: int`, `rationale: str`, `patches: list[FilePatch]`, `result: SandboxResult \| None` |
 
58
  issue: IssueInput
59
  repo: RepoContext
60
  repro_test: ReproTest
61
+ attempts: Annotated[list[FixAttempt], merge_attempts] # history, upserted by iteration
62
  messages: Annotated[list[AnyMessage], add_messages] # LLM conversation (pruned)
63
  iteration: int # fix iterations consumed (0..max_iterations)
64
  repro_attempts: int # repro-test generations consumed
 
73
 
74
  Rules:
75
  - Nodes return **partial updates only**; they never mutate the input state.
76
+ - `attempts` is full history for the audit trail. Its reducer `merge_attempts` upserts by `iteration`, so `run_tests` can return an updated copy of the latest attempt with its `SandboxResult` attached (a plain append reducer cannot update an entry). The LLM never sees it raw; the pruner builds a compact summary (section 6).
77
  - `messages` is the only field the pruner rewrites.
78
 
79
  ## 3. Node Functions (`src/agent/nodes.py`)
 
121
  ```
122
 
123
  Invariants (each gets a test):
124
+ 1. `iteration` never exceeds `max_iterations` (8). LangGraph `recursion_limit` is additionally set to a safe bound (about 4× nodes × iterations) as a hard backstop.
125
  2. `commit_and_pr` has exactly one inbound edge: from `human_approval` via `route_after_approval`.
126
  3. Timeouts and `SandboxResult.timed_out` count as failures, never as passes.
127
 
 
187
 
188
  ```
189
  .
190
+ ├── SPEC.md
191
+ ├── pyproject.toml
192
+ ├��─ .env.example
193
+ ├── .gitignore
194
+ ├── src/
195
+ │ ├── agent/
196
+ │ │ ├── state.py # models + AgentState (phase 1)
197
+ │ │ ├── graph.py # build_graph, routers, checkpointer (phase 3)
198
+ │ │ ├── nodes.py # node functions + pruner (phase 3)
199
+ │ │ └── prompts.py # system / task prompts (phase 3)
200
+ │ ├── sandbox/
201
+ │ │ ├── docker_runner.py # DockerSandbox (phase 1)
202
+ │ │ └── Dockerfile # python:3.12-slim + pytest, non-root user
203
+ │ └── tools/
204
+ │ ├── file_tools.py # read/list/grep/apply_patch, path-jail (phase 2)
205
+ │ └── git_tools.py # branch/commit/push/PR (phase 2)
206
+ └── tests/
207
+ ├── conftest.py # `docker` marker, skip if daemon down
208
+ ├── unit/
209
+ │ ├── test_state.py (phase 1)
210
+ │ ├── test_docker_runner.py (phase 1; integration-marked)
211
+ │ └── test_routing.py (phase 3)
212
+ └── fixtures/
213
+ └── buggy_repo/ # tiny repo with a known bug + expected repro test
214
  ```
215
 
216
  ## 8. Phases
pyproject.toml CHANGED
@@ -18,6 +18,7 @@ dependencies = [
18
 
19
  [project.optional-dependencies]
20
  postgres = ["langgraph-checkpoint-postgres>=2.0", "psycopg[binary,pool]>=3.2"]
 
21
  dev = ["pytest>=8.0", "pytest-cov>=5.0", "ruff>=0.6"]
22
 
23
  [tool.setuptools.packages.find]
 
18
 
19
  [project.optional-dependencies]
20
  postgres = ["langgraph-checkpoint-postgres>=2.0", "psycopg[binary,pool]>=3.2"]
21
+ ui = ["streamlit>=1.40"]
22
  dev = ["pytest>=8.0", "pytest-cov>=5.0", "ruff>=0.6"]
23
 
24
  [tool.setuptools.packages.find]
src/agent/state.py CHANGED
@@ -7,7 +7,6 @@ See SPEC.md section 2.
7
 
8
  from __future__ import annotations
9
 
10
- import operator
11
  from datetime import UTC, datetime
12
  from enum import StrEnum
13
  from pathlib import Path
@@ -31,6 +30,7 @@ __all__ = [
31
  "RunBudget",
32
  "SandboxResult",
33
  "initial_state",
 
34
  ]
35
 
36
 
@@ -112,13 +112,24 @@ class RunBudget(_Model):
112
  return self
113
 
114
 
 
 
 
 
 
 
 
 
 
 
 
115
  class AgentState(TypedDict, total=False):
116
  """Graph state. Nodes return partial updates and never mutate their input."""
117
 
118
  issue: IssueInput
119
  repo: RepoContext
120
  repro_test: ReproTest
121
- attempts: Annotated[list[FixAttempt], operator.add] # append-only audit trail
122
  messages: Annotated[list[AnyMessage], add_messages] # LLM conversation (pruned)
123
  iteration: int # fix iterations consumed, 0..budget.max_iterations
124
  repro_attempts: int # repro-test generations consumed
 
7
 
8
  from __future__ import annotations
9
 
 
10
  from datetime import UTC, datetime
11
  from enum import StrEnum
12
  from pathlib import Path
 
30
  "RunBudget",
31
  "SandboxResult",
32
  "initial_state",
33
+ "merge_attempts",
34
  ]
35
 
36
 
 
112
  return self
113
 
114
 
115
+ def merge_attempts(existing: list[FixAttempt] | None, new: list[FixAttempt] | None) -> list[FixAttempt]:
116
+ """Reducer for ``attempts``: upsert by ``iteration``, ordered by iteration.
117
+
118
+ Lets a later node (e.g. ``run_tests``) return an updated copy of an attempt
119
+ with its sandbox result attached, while earlier attempts stay in the history.
120
+ """
121
+ merged = {a.iteration: a for a in existing or []}
122
+ merged.update({a.iteration: a for a in new or []})
123
+ return [merged[k] for k in sorted(merged)]
124
+
125
+
126
  class AgentState(TypedDict, total=False):
127
  """Graph state. Nodes return partial updates and never mutate their input."""
128
 
129
  issue: IssueInput
130
  repo: RepoContext
131
  repro_test: ReproTest
132
+ attempts: Annotated[list[FixAttempt], merge_attempts] # audit trail, upserted by iteration
133
  messages: Annotated[list[AnyMessage], add_messages] # LLM conversation (pruned)
134
  iteration: int # fix iterations consumed, 0..budget.max_iterations
135
  repro_attempts: int # repro-test generations consumed
src/ui/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+
src/ui/mock_graph.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """DEMO ONLY: a scripted LangGraph that mimics the real agent's control flow.
2
+
3
+ No LLM, no Docker, nothing is committed. It uses the real ``AgentState`` models, the
4
+ real conditional-routing shape from SPEC.md section 4, a real ``interrupt()`` before
5
+ ``commit_and_pr``, and a real checkpointer, so the Streamlit UI exercises the same
6
+ pause/resume mechanics the production graph will use.
7
+
8
+ Scenario: ``calc.average()`` has an off-by-one bug. The scripted agent first proposes a
9
+ fix that is wrong (integer division), recovers on the second attempt, and, if the
10
+ reviewer requests changes, adds an empty-list guard on the third.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import hashlib
16
+ import time
17
+ from typing import Any, Literal
18
+
19
+ from langchain_core.runnables import RunnableConfig
20
+ from langgraph.checkpoint.base import BaseCheckpointSaver
21
+ from langgraph.checkpoint.memory import MemorySaver
22
+ from langgraph.graph import END, START, StateGraph
23
+ from langgraph.types import interrupt
24
+
25
+ from agent.state import (
26
+ AgentState,
27
+ AgentStatus,
28
+ ApprovalDecision,
29
+ FilePatch,
30
+ FixAttempt,
31
+ RepoContext,
32
+ ReproTest,
33
+ SandboxResult,
34
+ )
35
+
36
+ DEMO_ISSUE_TITLE = "average() returns wrong values"
37
+ DEMO_ISSUE_BODY = (
38
+ "`calc.average([2, 4, 6])` returns `3.0` instead of `4`, and "
39
+ "`calc.average([1, 2])` returns `1.0` instead of `1.5`."
40
+ )
41
+
42
+ BUGGY_SOURCE = "def average(xs):\n return sum(xs) / (len(xs) + 1)\n"
43
+
44
+ REPRO_TEST = '''from calc import average
45
+
46
+
47
+ def test_average_of_three():
48
+ assert average([2, 4, 6]) == 4
49
+
50
+
51
+ def test_average_of_two_is_fractional():
52
+ assert average([1, 2]) == 1.5
53
+ '''
54
+
55
+ REPRO_FAIL_OUTPUT = """FAILED test_repro.py::test_average_of_three - assert 3.0 == 4
56
+ FAILED test_repro.py::test_average_of_two_is_fractional - assert 1.0 == 1.5
57
+ 2 failed in 0.04s"""
58
+
59
+ # (rationale, new line(s) of the function body, diff, test passes?, pytest output)
60
+ _FIXES: list[tuple[str, str, bool, str]] = [
61
+ (
62
+ "The denominator has an extra +1. Remove it and use integer division.",
63
+ " return sum(xs) // len(xs)",
64
+ False,
65
+ "FAILED test_repro.py::test_average_of_two_is_fractional - assert 1 == 1.5\n"
66
+ "1 failed, 1 passed in 0.04s",
67
+ ),
68
+ (
69
+ "Integer division truncated the fractional result. Use true division.",
70
+ " return sum(xs) / len(xs)",
71
+ True,
72
+ "2 passed in 0.03s",
73
+ ),
74
+ (
75
+ "Reviewer asked for edge-case handling: reject empty input explicitly "
76
+ "instead of raising ZeroDivisionError.",
77
+ " if not xs:\n"
78
+ ' raise ValueError("average() requires at least one value")\n'
79
+ " return sum(xs) / len(xs)",
80
+ True,
81
+ "2 passed in 0.03s",
82
+ ),
83
+ ]
84
+
85
+ _OLD_LINE = " return sum(xs) / (len(xs) + 1)"
86
+
87
+
88
+ def _pause(config: RunnableConfig) -> None:
89
+ time.sleep(float(config.get("configurable", {}).get("delay", 0.6)))
90
+
91
+
92
+ def _diff_for(new_body: str) -> str:
93
+ old = f"-{_OLD_LINE}"
94
+ new = "\n".join(f"+{line}" for line in new_body.split("\n"))
95
+ n_new = len(new_body.split("\n"))
96
+ 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"
97
+
98
+
99
+ # --------------------------------------------------------------------------- nodes
100
+
101
+
102
+ def ingest_issue(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
103
+ _pause(config)
104
+ return {"status": AgentStatus.INGESTED}
105
+
106
+
107
+ def inspect_repo(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
108
+ _pause(config)
109
+ repo = RepoContext(
110
+ tree="calc.py\ntests/\n",
111
+ relevant_files={"calc.py": BUGGY_SOURCE},
112
+ test_command="pytest -q -p no:cacheprovider",
113
+ )
114
+ return {"repo": repo, "status": AgentStatus.INSPECTED}
115
+
116
+
117
+ def write_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
118
+ _pause(config)
119
+ return {
120
+ "repro_test": ReproTest(path="test_repro.py", content=REPRO_TEST),
121
+ "repro_attempts": state.get("repro_attempts", 0) + 1,
122
+ "status": AgentStatus.REPRO_WRITTEN,
123
+ }
124
+
125
+
126
+ def run_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
127
+ _pause(config)
128
+ repro = state["repro_test"].model_copy(update={"confirmed_failing": True})
129
+ return {"repro_test": repro, "status": AgentStatus.REPRO_CONFIRMED}
130
+
131
+
132
+ def propose_fix(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
133
+ _pause(config)
134
+ iteration = state.get("iteration", 0)
135
+ rationale, body, _passes, _out = _FIXES[min(iteration, len(_FIXES) - 1)]
136
+ attempt = FixAttempt(
137
+ iteration=iteration + 1,
138
+ rationale=rationale,
139
+ patches=[FilePatch(path="calc.py", unified_diff=_diff_for(body))],
140
+ )
141
+ return {"attempts": [attempt], "status": AgentStatus.FIXING}
142
+
143
+
144
+ def run_tests(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
145
+ _pause(config)
146
+ iteration = state.get("iteration", 0)
147
+ _r, _b, passes, output = _FIXES[min(iteration, len(_FIXES) - 1)]
148
+ result = SandboxResult(exit_code=0 if passes else 1, stdout=output, stderr="", duration_s=0.04)
149
+ attempt = state["attempts"][-1].model_copy(update={"result": result})
150
+ return {
151
+ "attempts": [attempt], # same iteration -> upserted by merge_attempts
152
+ "iteration": iteration + 1,
153
+ "status": AgentStatus.TESTS_PASSING if passes else AgentStatus.FIXING,
154
+ }
155
+
156
+
157
+ def analyze_failure(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
158
+ _pause(config)
159
+ return {}
160
+
161
+
162
+ def human_approval(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
163
+ """Pause the graph. Nothing before this line has side effects (the node re-runs on resume)."""
164
+ attempt = state["attempts"][-1]
165
+ decision = interrupt(
166
+ {
167
+ "iteration": attempt.iteration,
168
+ "rationale": attempt.rationale,
169
+ "diff": "\n".join(p.unified_diff for p in attempt.patches),
170
+ "test_output": attempt.result.stdout if attempt.result else "",
171
+ "repro_test": state["repro_test"].content,
172
+ }
173
+ )
174
+ approval = ApprovalDecision.model_validate(decision)
175
+ return {
176
+ "approval": approval,
177
+ "status": AgentStatus.APPROVED if approval.approved else AgentStatus.REJECTED,
178
+ }
179
+
180
+
181
+ def commit_and_pr(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
182
+ _pause(config)
183
+ diff = state["attempts"][-1].patches[0].unified_diff
184
+ sha = hashlib.sha1(diff.encode()).hexdigest()[:7] # noqa: S324 - demo id only
185
+ return {"branch": "agent/fix-demo", "commit_sha": sha, "status": AgentStatus.COMMITTED}
186
+
187
+
188
+ def escalate(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
189
+ _pause(config)
190
+ approval = state.get("approval")
191
+ reason = (
192
+ "Reviewer rejected the fix."
193
+ if approval is not None and not approval.approved
194
+ else "Iteration budget exhausted without a passing fix."
195
+ )
196
+ return {"error": reason, "status": AgentStatus.ESCALATED}
197
+
198
+
199
+ # -------------------------------------------------------------------------- routing
200
+
201
+
202
+ def _under_budget(state: AgentState) -> bool:
203
+ return state["iteration"] < state["budget"].max_iterations
204
+
205
+
206
+ def route_after_tests(
207
+ state: AgentState,
208
+ ) -> Literal["human_approval", "analyze_failure", "escalate"]:
209
+ result = state["attempts"][-1].result
210
+ if result is not None and result.passed:
211
+ return "human_approval"
212
+ return "analyze_failure" if _under_budget(state) else "escalate"
213
+
214
+
215
+ def route_after_approval(
216
+ state: AgentState,
217
+ ) -> Literal["commit_and_pr", "analyze_failure", "escalate"]:
218
+ approval = state["approval"]
219
+ if approval is not None and approval.approved:
220
+ return "commit_and_pr"
221
+ if approval is not None and approval.feedback and _under_budget(state):
222
+ return "analyze_failure"
223
+ return "escalate"
224
+
225
+
226
+ def build_mock_graph(checkpointer: BaseCheckpointSaver | None = None):
227
+ g = StateGraph(AgentState)
228
+ for fn in (
229
+ ingest_issue, inspect_repo, write_repro_test, run_repro_test, propose_fix,
230
+ run_tests, analyze_failure, human_approval, commit_and_pr, escalate,
231
+ ): # fmt: skip
232
+ g.add_node(fn.__name__, fn)
233
+
234
+ g.add_edge(START, "ingest_issue")
235
+ g.add_edge("ingest_issue", "inspect_repo")
236
+ g.add_edge("inspect_repo", "write_repro_test")
237
+ g.add_edge("write_repro_test", "run_repro_test")
238
+ g.add_edge("run_repro_test", "propose_fix") # scripted: the bug always reproduces
239
+ g.add_edge("propose_fix", "run_tests")
240
+ g.add_conditional_edges("run_tests", route_after_tests)
241
+ g.add_edge("analyze_failure", "propose_fix")
242
+ g.add_conditional_edges("human_approval", route_after_approval)
243
+ g.add_edge("commit_and_pr", END)
244
+ g.add_edge("escalate", END)
245
+ return g.compile(checkpointer=checkpointer or MemorySaver())
246
+
src/ui/streamlit_app.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Streamlit front-end for the bug-fixing agent (DEMO MODE).
2
+
3
+ Drives ``ui.mock_graph`` -- a scripted agent with no LLM and no Docker -- through the same
4
+ LangGraph pause/resume mechanism the real agent uses. Nothing is ever committed.
5
+
6
+ Run: streamlit run src/ui/streamlit_app.py
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import uuid
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ import streamlit as st
16
+ from langgraph.types import Command
17
+
18
+ from agent.state import AgentState, FixAttempt, IssueInput, initial_state
19
+ from ui.mock_graph import (
20
+ DEMO_ISSUE_BODY,
21
+ DEMO_ISSUE_TITLE,
22
+ build_mock_graph,
23
+ )
24
+
25
+ STEP_LABELS = {
26
+ "ingest_issue": "Ingest issue",
27
+ "inspect_repo": "Inspect repository",
28
+ "write_repro_test": "Write reproduction test",
29
+ "run_repro_test": "Run reproduction test (expect failure)",
30
+ "propose_fix": "Propose fix",
31
+ "run_tests": "Run tests in sandbox",
32
+ "analyze_failure": "Analyze failure",
33
+ "human_approval": "Human approval",
34
+ "commit_and_pr": "Commit",
35
+ "escalate": "Escalate",
36
+ }
37
+
38
+
39
+ @st.cache_resource
40
+ def get_graph():
41
+ """One graph + MemorySaver per server process; runs are keyed by thread_id."""
42
+ return build_mock_graph()
43
+
44
+
45
+ def _config() -> dict[str, Any]:
46
+ return {
47
+ "configurable": {
48
+ "thread_id": st.session_state.thread_id,
49
+ "delay": st.session_state.delay,
50
+ }
51
+ }
52
+
53
+
54
+ # ------------------------------------------------------------------------ running
55
+
56
+
57
+ def advance(payload: AgentState | Command) -> None:
58
+ """Stream the graph until it finishes or pauses, recording each step, then rerun."""
59
+ graph = get_graph()
60
+ with st.status("Agent running...", expanded=True) as status:
61
+ for chunk in graph.stream(payload, _config(), stream_mode="updates"):
62
+ for node, update in chunk.items():
63
+ if node.startswith("__"):
64
+ continue
65
+ st.session_state.log.append((node, update or {}))
66
+ status.update(label=f"Ran: {STEP_LABELS.get(node, node)}")
67
+ st.write(f"✅ {STEP_LABELS.get(node, node)}")
68
+ status.update(label="Agent paused or finished", state="complete", expanded=False)
69
+ st.rerun()
70
+
71
+
72
+ def start_run() -> None:
73
+ st.session_state.thread_id = f"demo-{uuid.uuid4().hex[:8]}"
74
+ st.session_state.log = []
75
+ issue = IssueInput(title=DEMO_ISSUE_TITLE, body=DEMO_ISSUE_BODY, repo_path=Path("demo-repo"))
76
+ advance(initial_state(issue))
77
+
78
+
79
+ # ---------------------------------------------------------------------- rendering
80
+
81
+
82
+ def render_event(node: str, update: dict[str, Any]) -> None:
83
+ label = STEP_LABELS.get(node, node)
84
+ attempts: list[FixAttempt] = update.get("attempts", [])
85
+
86
+ if node == "propose_fix" and attempts:
87
+ a = attempts[-1]
88
+ with st.expander(f"🛠️ {label} (attempt {a.iteration})", expanded=False):
89
+ st.write(a.rationale)
90
+ st.code("\n".join(p.unified_diff for p in a.patches), language="diff")
91
+ elif node == "run_tests" and attempts:
92
+ a = attempts[-1]
93
+ icon = "🟢" if a.result and a.result.passed else "🔴"
94
+ with st.expander(f"{icon} {label} (attempt {a.iteration})", expanded=False):
95
+ st.code(a.result.stdout if a.result else "", language="text")
96
+ elif node == "write_repro_test" and "repro_test" in update:
97
+ with st.expander(f"🧪 {label}", expanded=False):
98
+ st.code(update["repro_test"].content, language="python")
99
+ elif node == "inspect_repo" and "repo" in update:
100
+ with st.expander(f"🔎 {label}", expanded=False):
101
+ for path, src in update["repo"].relevant_files.items():
102
+ st.caption(path)
103
+ st.code(src, language="python")
104
+ elif node == "run_repro_test":
105
+ st.write(f"✅ {label}: bug reproduced")
106
+ elif node == "analyze_failure":
107
+ st.write(f"🔁 {label}: feeding the failure back into the next attempt")
108
+ elif node == "human_approval":
109
+ approval = update.get("approval")
110
+ verdict = "approved" if approval and approval.approved else "changes requested / rejected"
111
+ st.write(f"🧑 {label}: {verdict}")
112
+ if approval and approval.feedback:
113
+ st.caption(f"Feedback: {approval.feedback}")
114
+ else:
115
+ st.write(f"✅ {label}")
116
+
117
+
118
+ def render_approval(payload: dict[str, Any]) -> None:
119
+ st.subheader("⏸️ Waiting for your approval")
120
+ st.caption(
121
+ "The agent is paused by a LangGraph `interrupt()`. "
122
+ "Nothing is committed until you approve."
123
+ )
124
+ st.markdown(f"**Fix proposed (iteration {payload['iteration']}):** {payload['rationale']}")
125
+ st.code(payload["diff"], language="diff")
126
+ st.markdown("**Test results**")
127
+ st.code(payload["test_output"], language="text")
128
+ with st.expander("Reproduction test"):
129
+ st.code(payload["repro_test"], language="python")
130
+
131
+ feedback = st.text_area(
132
+ "Feedback for the agent (required to request changes)",
133
+ key=f"fb-{payload['iteration']}",
134
+ placeholder="e.g. handle the empty-list case",
135
+ )
136
+ approve, changes, reject = st.columns(3)
137
+ if approve.button("✅ Approve", type="primary", use_container_width=True):
138
+ advance(Command(resume={"approved": True, "reviewer": "demo-user"}))
139
+ if changes.button("✏️ Request changes", use_container_width=True):
140
+ if feedback.strip():
141
+ advance(
142
+ Command(resume={"approved": False, "feedback": feedback.strip(),
143
+ "reviewer": "demo-user"})
144
+ ) # fmt: skip
145
+ else:
146
+ st.warning("Add feedback so the agent knows what to change.")
147
+ if reject.button("⛔ Reject", use_container_width=True):
148
+ advance(Command(resume={"approved": False, "reviewer": "demo-user"}))
149
+
150
+
151
+ def render_outcome(values: dict[str, Any]) -> None:
152
+ status = str(values.get("status"))
153
+ if status == "committed":
154
+ st.success(
155
+ f"Approved. Demo commit `{values['commit_sha']}` on `{values['branch']}` "
156
+ "(simulated: nothing was written to any repository)."
157
+ )
158
+ attempts: list[FixAttempt] = values["attempts"]
159
+ patch = "\n".join(p.unified_diff for p in attempts[-1].patches)
160
+ st.download_button("⬇️ Download patch", patch, file_name="fix.patch")
161
+ elif status == "escalated":
162
+ st.error(f"Escalated: {values.get('error')}")
163
+
164
+
165
+ # --------------------------------------------------------------------------- page
166
+
167
+
168
+ def main() -> None:
169
+ st.set_page_config(page_title="Autonomous Bug-Fixing Agent", page_icon="🐞", layout="wide")
170
+ st.session_state.setdefault("thread_id", None)
171
+ st.session_state.setdefault("log", [])
172
+ st.session_state.setdefault("delay", 0.6)
173
+
174
+ st.title("🐞 Autonomous Bug-Fixing Agent")
175
+ st.warning(
176
+ "**Demo mode.** This is a scripted agent: no LLM, no Docker, nothing is committed. "
177
+ "It shows the real workflow, the pause-for-approval mechanism and the retry loop. "
178
+ "The real agent is still under construction."
179
+ )
180
+
181
+ with st.sidebar:
182
+ st.header("Issue")
183
+ st.text_input("Title", DEMO_ISSUE_TITLE, disabled=True)
184
+ st.text_area("Description", DEMO_ISSUE_BODY, disabled=True, height=120)
185
+ st.caption("The scripted scenario is fixed; these fields are read-only.")
186
+ st.slider("Step delay (s)", 0.0, 1.5, key="delay", step=0.1)
187
+ if st.button("▶️ Start run", type="primary", use_container_width=True):
188
+ start_run()
189
+
190
+ if st.session_state.thread_id is None:
191
+ st.info("Press **Start run** in the sidebar to watch the agent work on the sample bug.")
192
+ return
193
+
194
+ snap = get_graph().get_state(_config())
195
+ values = snap.values
196
+ left, right = st.columns([3, 1])
197
+ left.subheader("Timeline")
198
+ right.metric("Fix iterations", f"{values.get('iteration', 0)} / 8")
199
+
200
+ for node, update in st.session_state.log:
201
+ render_event(node, update)
202
+
203
+ if snap.next: # paused at human_approval
204
+ render_approval(snap.tasks[0].interrupts[0].value)
205
+ else:
206
+ render_outcome(values)
207
+
208
+
209
+ main()
210
+
tests/unit/test_mock_graph.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tests for the scripted demo graph: control flow, HITL pause, and the attempts reducer."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ import pytest
8
+ from langgraph.types import Command
9
+
10
+ from agent.state import (
11
+ AgentStatus,
12
+ FixAttempt,
13
+ IssueInput,
14
+ SandboxResult,
15
+ initial_state,
16
+ merge_attempts,
17
+ )
18
+ from ui.mock_graph import DEMO_ISSUE_BODY, DEMO_ISSUE_TITLE, build_mock_graph
19
+
20
+ CFG = {"configurable": {"thread_id": "t", "delay": 0}}
21
+
22
+
23
+ @pytest.fixture
24
+ def graph():
25
+ return build_mock_graph()
26
+
27
+
28
+ @pytest.fixture
29
+ def start_state():
30
+ return initial_state(
31
+ IssueInput(title=DEMO_ISSUE_TITLE, body=DEMO_ISSUE_BODY, repo_path=Path("demo"))
32
+ )
33
+
34
+
35
+ def _nodes(graph, payload, cfg=CFG) -> list[str]:
36
+ return [n for chunk in graph.stream(payload, cfg, stream_mode="updates") for n in chunk]
37
+
38
+
39
+ def _run_to_approval(graph, start_state):
40
+ nodes = _nodes(graph, start_state)
41
+ return nodes, graph.get_state(CFG)
42
+
43
+
44
+ # ----------------------------------------------------------------------- reducer
45
+
46
+
47
+ def _attempt(i: int, rationale: str = "", result: SandboxResult | None = None) -> FixAttempt:
48
+ return FixAttempt(iteration=i, rationale=rationale, result=result)
49
+
50
+
51
+ def test_merge_attempts_appends_new_iterations_in_order():
52
+ merged = merge_attempts([_attempt(1)], [_attempt(2)])
53
+ assert [a.iteration for a in merged] == [1, 2]
54
+
55
+
56
+ def test_merge_attempts_upserts_same_iteration_keeping_history():
57
+ res = SandboxResult(exit_code=1, stdout="F", stderr="")
58
+ merged = merge_attempts([_attempt(1, "a"), _attempt(2, "b")], [_attempt(2, "b", res)])
59
+ assert [a.iteration for a in merged] == [1, 2]
60
+ assert merged[1].result == res and merged[0].result is None
61
+
62
+
63
+ def test_merge_attempts_handles_none():
64
+ assert merge_attempts(None, None) == []
65
+
66
+
67
+ # ------------------------------------------------------------------- control flow
68
+
69
+
70
+ def test_graph_pauses_before_commit_and_a_failed_first_attempt_is_retried(graph, start_state):
71
+ nodes, snap = _run_to_approval(graph, start_state)
72
+
73
+ assert snap.next == ("human_approval",) # paused, waiting for a human
74
+ assert "commit_and_pr" not in nodes
75
+ assert nodes.count("propose_fix") == 2 and nodes.count("analyze_failure") == 1
76
+ assert snap.values["iteration"] == 2
77
+ attempts = snap.values["attempts"]
78
+ assert [a.result.passed for a in attempts] == [False, True] # history kept, results attached
79
+ assert snap.values.get("commit_sha") is None
80
+
81
+
82
+ def test_interrupt_payload_contains_review_material(graph, start_state):
83
+ _run_to_approval(graph, start_state)
84
+ payload = graph.get_state(CFG).tasks[0].interrupts[0].value
85
+ assert "+ return sum(xs) / len(xs)" in payload["diff"]
86
+ assert payload["test_output"].startswith("2 passed")
87
+ assert "def test_average_of_three" in payload["repro_test"]
88
+
89
+
90
+ def test_approve_commits(graph, start_state):
91
+ _run_to_approval(graph, start_state)
92
+ nodes = _nodes(graph, Command(resume={"approved": True, "reviewer": "me"}))
93
+
94
+ assert nodes[-1] == "commit_and_pr"
95
+ final = graph.get_state(CFG)
96
+ assert final.next == ()
97
+ assert final.values["status"] == AgentStatus.COMMITTED
98
+ assert len(final.values["commit_sha"]) == 7
99
+ assert final.values["approval"].reviewer == "me"
100
+
101
+
102
+ def test_request_changes_loops_back_then_pauses_again(graph, start_state):
103
+ _run_to_approval(graph, start_state)
104
+ nodes = _nodes(graph, Command(resume={"approved": False, "feedback": "handle empty list"}))
105
+
106
+ assert "analyze_failure" in nodes and "propose_fix" in nodes
107
+ assert "commit_and_pr" not in nodes
108
+ snap = graph.get_state(CFG)
109
+ assert snap.next == ("human_approval",) # a second approval is required
110
+ assert snap.values["iteration"] == 3
111
+ assert "ValueError" in snap.values["attempts"][-1].patches[0].unified_diff
112
+
113
+
114
+ def test_reject_without_feedback_escalates_and_never_commits(graph, start_state):
115
+ _run_to_approval(graph, start_state)
116
+ nodes = _nodes(graph, Command(resume={"approved": False}))
117
+
118
+ assert nodes[-1] == "escalate" and "commit_and_pr" not in nodes
119
+ final = graph.get_state(CFG).values
120
+ assert final["status"] == AgentStatus.ESCALATED
121
+ assert final["commit_sha"] is None and "rejected" in final["error"].lower()
122
+
123
+
124
+ def test_repeated_change_requests_stop_at_iteration_budget(graph, start_state):
125
+ _run_to_approval(graph, start_state)
126
+ for _ in range(20):
127
+ if not graph.get_state(CFG).next:
128
+ break
129
+ _nodes(graph, Command(resume={"approved": False, "feedback": "again"}))
130
+
131
+ final = graph.get_state(CFG)
132
+ assert final.next == ()
133
+ assert final.values["iteration"] <= final.values["budget"].max_iterations == 8
134
+ assert final.values["status"] == AgentStatus.ESCALATED
135
+ assert final.values["commit_sha"] is None
136
+
tests/unit/test_streamlit_app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Headless smoke tests for the Streamlit demo UI (no browser needed)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ import pytest
8
+
9
+ pytest.importorskip("streamlit")
10
+ from streamlit.testing.v1 import AppTest # noqa: E402
11
+
12
+ APP = str(Path(__file__).resolve().parents[2] / "src" / "ui" / "streamlit_app.py")
13
+
14
+
15
+ def _start() -> AppTest:
16
+ at = AppTest.from_file(APP, default_timeout=30).run()
17
+ assert not at.exception
18
+ at.slider[0].set_value(0.0)
19
+ _button(at, "Start run").click()
20
+ return at.run()
21
+
22
+
23
+ def _button(at: AppTest, label_part: str):
24
+ return next(b for b in at.button if label_part in b.label)
25
+
26
+
27
+ def _subheaders(at: AppTest) -> list[str]:
28
+ return [s.value for s in at.subheader]
29
+
30
+
31
+ def test_initial_page_shows_demo_banner_and_prompt():
32
+ at = AppTest.from_file(APP, default_timeout=30).run()
33
+ assert not at.exception
34
+ assert any("Demo mode" in w.value for w in at.warning)
35
+ assert any("Start run" in i.value for i in at.info)
36
+
37
+
38
+ def test_run_pauses_for_approval_with_fix_and_tests():
39
+ at = _start()
40
+ assert not at.exception
41
+ assert any("Waiting for your approval" in s for s in _subheaders(at))
42
+ code = " ".join(c.value for c in at.code)
43
+ assert "sum(xs) / len(xs)" in code and "2 passed" in code
44
+ assert at.metric[0].value == "2 / 8"
45
+ assert not any("commit" in str(s.value).lower() for s in at.success) # nothing committed yet
46
+
47
+
48
+ def test_approve_shows_simulated_commit():
49
+ at = _start()
50
+ _button(at, "Approve").click()
51
+ at.run()
52
+ assert not at.exception
53
+ assert any("Demo commit" in s.value for s in at.success)
54
+ assert not any("Waiting for your approval" in s for s in _subheaders(at))
55
+
56
+
57
+ def test_request_changes_requires_feedback_then_loops_back():
58
+ at = _start()
59
+ _button(at, "Request changes").click()
60
+ at.run()
61
+ assert any("Add feedback" in w.value for w in at.warning) # blocked, still paused
62
+ assert any("Waiting for your approval" in s for s in _subheaders(at))
63
+
64
+ at.text_area[0].set_value("handle the empty list")
65
+ _button(at, "Request changes").click()
66
+ at.run()
67
+ assert not at.exception
68
+ assert at.metric[0].value == "3 / 8"
69
+ assert any("ValueError" in c.value for c in at.code)
70
+ assert any("Waiting for your approval" in s for s in _subheaders(at))
71
+
72
+
73
+ def test_reject_escalates():
74
+ at = _start()
75
+ _button(at, "Reject").click()
76
+ at.run()
77
+ assert not at.exception
78
+ assert any("Escalated" in e.value for e in at.error)
79
+