Muhammad Faraz Malik commited on
Commit Β·
e497738
1
Parent(s): 9ccc0da
Add automatic repository bug detection when running on custom/cloned repos
Browse files- src/agent/llm.py +25 -0
- src/agent/nodes.py +33 -1
- src/agent/prompts.py +11 -0
- src/ui/streamlit_app.py +24 -9
src/agent/llm.py
CHANGED
|
@@ -68,6 +68,26 @@ class FixProposal(BaseModel):
|
|
| 68 |
)
|
| 69 |
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def load_dotenv_into_environ(dotenv_path: Path | None = None) -> None:
|
| 72 |
"""Load key=value pairs from `.env` (handling UTF-8 BOM safely) without printing values."""
|
| 73 |
candidates = [dotenv_path] if dotenv_path else [Path.cwd() / ".env", Path(__file__).resolve().parents[2] / ".env"]
|
|
@@ -100,6 +120,11 @@ def default_groq_api_key() -> str:
|
|
| 100 |
return os.environ.get("GROQ_API_KEY", "").strip()
|
| 101 |
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def create_groq_llm(
|
| 104 |
model: str | None = None,
|
| 105 |
api_key: str | None = None,
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
|
| 71 |
+
class DetectedBugProposal(BaseModel):
|
| 72 |
+
"""Structured output when auto-scanning a repository for a testable bug."""
|
| 73 |
+
|
| 74 |
+
title: str = Field(
|
| 75 |
+
min_length=3,
|
| 76 |
+
description="Short issue title naming the file/function and the bug.",
|
| 77 |
+
)
|
| 78 |
+
body: str = Field(
|
| 79 |
+
min_length=10,
|
| 80 |
+
description=(
|
| 81 |
+
"Detailed bug description naming the exact module/function, the input that triggers "
|
| 82 |
+
"the bug, the current wrong behavior, and the expected correct behavior."
|
| 83 |
+
),
|
| 84 |
+
)
|
| 85 |
+
file_path: str = Field(
|
| 86 |
+
min_length=1,
|
| 87 |
+
description="Relative path of the primary Python file containing the bug.",
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
def load_dotenv_into_environ(dotenv_path: Path | None = None) -> None:
|
| 92 |
"""Load key=value pairs from `.env` (handling UTF-8 BOM safely) without printing values."""
|
| 93 |
candidates = [dotenv_path] if dotenv_path else [Path.cwd() / ".env", Path(__file__).resolve().parents[2] / ".env"]
|
|
|
|
| 120 |
return os.environ.get("GROQ_API_KEY", "").strip()
|
| 121 |
|
| 122 |
|
| 123 |
+
def default_groq_api_key() -> str:
|
| 124 |
+
load_dotenv_into_environ()
|
| 125 |
+
return os.environ.get("GROQ_API_KEY", "").strip()
|
| 126 |
+
|
| 127 |
+
|
| 128 |
def create_groq_llm(
|
| 129 |
model: str | None = None,
|
| 130 |
api_key: str | None = None,
|
src/agent/nodes.py
CHANGED
|
@@ -18,6 +18,7 @@ from langchain_core.runnables import RunnableConfig
|
|
| 18 |
from langgraph.types import interrupt
|
| 19 |
|
| 20 |
from agent.llm import (
|
|
|
|
| 21 |
FixProposal,
|
| 22 |
ReproTestProposal,
|
| 23 |
create_groq_llm,
|
|
@@ -26,6 +27,7 @@ from agent.llm import (
|
|
| 26 |
)
|
| 27 |
from agent.prompts import (
|
| 28 |
ANALYZE_SYSTEM_PROMPT,
|
|
|
|
| 29 |
FIX_SYSTEM_PROMPT,
|
| 30 |
REPRO_SYSTEM_PROMPT,
|
| 31 |
build_fix_user_prompt,
|
|
@@ -133,7 +135,37 @@ def inspect_repo(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
|
|
| 133 |
test_command="pytest -q -p no:cacheprovider",
|
| 134 |
language="python",
|
| 135 |
)
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
def write_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
|
|
|
|
| 18 |
from langgraph.types import interrupt
|
| 19 |
|
| 20 |
from agent.llm import (
|
| 21 |
+
DetectedBugProposal,
|
| 22 |
FixProposal,
|
| 23 |
ReproTestProposal,
|
| 24 |
create_groq_llm,
|
|
|
|
| 27 |
)
|
| 28 |
from agent.prompts import (
|
| 29 |
ANALYZE_SYSTEM_PROMPT,
|
| 30 |
+
DETECT_BUG_SYSTEM_PROMPT,
|
| 31 |
FIX_SYSTEM_PROMPT,
|
| 32 |
REPRO_SYSTEM_PROMPT,
|
| 33 |
build_fix_user_prompt,
|
|
|
|
| 135 |
test_command="pytest -q -p no:cacheprovider",
|
| 136 |
language="python",
|
| 137 |
)
|
| 138 |
+
|
| 139 |
+
# If the user left the default calc.py sample issue on a custom repository that has no calc.py,
|
| 140 |
+
# or requested auto-detection, let the LLM audit the repo and formulate a real testable issue.
|
| 141 |
+
updates: dict[str, Any] = {"repo": repo, "status": AgentStatus.INSPECTED}
|
| 142 |
+
has_calc = any(p.endswith("calc.py") for p in py_files)
|
| 143 |
+
is_default_calc_issue = "average()" in issue.title and "calc.average" in issue.body
|
| 144 |
+
wants_auto = issue.title.strip().lower().startswith("auto") or (
|
| 145 |
+
is_default_calc_issue and not has_calc and bool(relevant)
|
| 146 |
+
)
|
| 147 |
+
if wants_auto:
|
| 148 |
+
try:
|
| 149 |
+
llm = _get_llm(config)
|
| 150 |
+
files_block = "\n\n".join(
|
| 151 |
+
f"### File: `{p}`\n```python\n{src}\n```" for p, src in relevant.items()
|
| 152 |
+
)
|
| 153 |
+
detected = invoke_structured(
|
| 154 |
+
llm,
|
| 155 |
+
DetectedBugProposal,
|
| 156 |
+
DETECT_BUG_SYSTEM_PROMPT,
|
| 157 |
+
f"Repository Tree:\n```\n{tree}\n```\n\nSource Files:\n{files_block}",
|
| 158 |
+
)
|
| 159 |
+
updates["issue"] = issue.model_copy(
|
| 160 |
+
update={"title": detected.title, "body": detected.body}
|
| 161 |
+
)
|
| 162 |
+
if detected.file_path in py_files and detected.file_path not in relevant:
|
| 163 |
+
relevant[detected.file_path] = ws.read(
|
| 164 |
+
detected.file_path, max_chars=MAX_FILE_SNIPPET_CHARS
|
| 165 |
+
)
|
| 166 |
+
except Exception as exc: # noqa: BLE001
|
| 167 |
+
logger.warning("Auto-detect bug fallback skipped: %s", exc)
|
| 168 |
+
return updates
|
| 169 |
|
| 170 |
|
| 171 |
def write_repro_test(state: AgentState, config: RunnableConfig) -> dict[str, Any]:
|
src/agent/prompts.py
CHANGED
|
@@ -31,6 +31,17 @@ Summarize in 2-3 concise sentences why the latest fix attempt failed (or what th
|
|
| 31 |
requested) and what the next attempt should change.
|
| 32 |
"""
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def summarize_past_attempts(attempts: list[FixAttempt], max_Summary_chars: int = 1200) -> str:
|
| 36 |
"""Prune past attempts into one-line summaries so context never blows Groq's 8K TPM limit."""
|
|
|
|
| 31 |
requested) and what the next attempt should change.
|
| 32 |
"""
|
| 33 |
|
| 34 |
+
DETECT_BUG_SYSTEM_PROMPT = """You are a senior Python code auditor.
|
| 35 |
+
Inspect the repository files below and identify ONE concrete, unit-testable bug, edge-case defect,
|
| 36 |
+
or unhandled input error in a pure Python function/class that can be reproduced with a fast,
|
| 37 |
+
standalone `pytest` test (without requiring external API keys, databases, or servers).
|
| 38 |
+
|
| 39 |
+
Rules:
|
| 40 |
+
- Name the exact module path and function/method in both `title` and `body`.
|
| 41 |
+
- Specify the exact input values, what the code currently does (e.g. wrong return value or
|
| 42 |
+
unhandled exception), and what it SHOULD return/raise so the test writer and fixer agree.
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
|
| 46 |
def summarize_past_attempts(attempts: list[FixAttempt], max_Summary_chars: int = 1200) -> str:
|
| 47 |
"""Prune past attempts into one-line summaries so context never blows Groq's 8K TPM limit."""
|
src/ui/streamlit_app.py
CHANGED
|
@@ -200,6 +200,9 @@ def render_event(node: str, update: dict[str, Any]) -> None:
|
|
| 200 |
with st.expander(f"π§ͺ {label} (`{update['repro_test'].path}`)", expanded=False):
|
| 201 |
st.code(update["repro_test"].content, language="python")
|
| 202 |
elif node == "inspect_repo" and "repo" in update:
|
|
|
|
|
|
|
|
|
|
| 203 |
with st.expander(f"π {label}", expanded=False):
|
| 204 |
for path, src in update["repo"].relevant_files.items():
|
| 205 |
st.caption(path)
|
|
@@ -383,17 +386,29 @@ def main() -> None:
|
|
| 383 |
st.caption(f"Using `{FIXTURE_REPO.name}` (`calc.py` with off-by-one bug)")
|
| 384 |
|
| 385 |
st.header("3. Bug Report / Issue")
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
)
|
| 391 |
-
issue_body = st.text_area(
|
| 392 |
-
"Description",
|
| 393 |
-
value=DEMO_ISSUE_BODY,
|
| 394 |
-
height=110,
|
| 395 |
disabled=is_demo_choice,
|
|
|
|
| 396 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
with st.expander("βοΈ LLM & Sandbox Settings", expanded=False):
|
| 399 |
model_options = list(AVAILABLE_GROQ_MODELS)
|
|
|
|
| 200 |
with st.expander(f"π§ͺ {label} (`{update['repro_test'].path}`)", expanded=False):
|
| 201 |
st.code(update["repro_test"].content, language="python")
|
| 202 |
elif node == "inspect_repo" and "repo" in update:
|
| 203 |
+
if "issue" in update:
|
| 204 |
+
det = update["issue"]
|
| 205 |
+
st.info(f"π **Auto-detected bug in repository:** {det.title}\n\n{det.body}")
|
| 206 |
with st.expander(f"π {label}", expanded=False):
|
| 207 |
for path, src in update["repo"].relevant_files.items():
|
| 208 |
st.caption(path)
|
|
|
|
| 386 |
st.caption(f"Using `{FIXTURE_REPO.name}` (`calc.py` with off-by-one bug)")
|
| 387 |
|
| 388 |
st.header("3. Bug Report / Issue")
|
| 389 |
+
auto_detect_default = repo_source != "Built-in sample buggy repo"
|
| 390 |
+
auto_detect = st.checkbox(
|
| 391 |
+
"π Auto-detect bug from repository code",
|
| 392 |
+
value=auto_detect_default,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
disabled=is_demo_choice,
|
| 394 |
+
help="When enabled, the Groq LLM inspects the repository first and automatically identifies a testable bug/edge-case to reproduce and fix.",
|
| 395 |
)
|
| 396 |
+
if auto_detect and not is_demo_choice:
|
| 397 |
+
issue_title = "Auto-detect bug in repository"
|
| 398 |
+
issue_body = "Scan repository files, identify a testable bug or edge-case defect, write a failing pytest reproduction test, and fix it."
|
| 399 |
+
st.caption("Groq will scan the repository files and formulate the bug report automatically.")
|
| 400 |
+
else:
|
| 401 |
+
issue_title = st.text_input(
|
| 402 |
+
"Title",
|
| 403 |
+
value=DEMO_ISSUE_TITLE,
|
| 404 |
+
disabled=is_demo_choice,
|
| 405 |
+
)
|
| 406 |
+
issue_body = st.text_area(
|
| 407 |
+
"Description",
|
| 408 |
+
value=DEMO_ISSUE_BODY,
|
| 409 |
+
height=110,
|
| 410 |
+
disabled=is_demo_choice,
|
| 411 |
+
)
|
| 412 |
|
| 413 |
with st.expander("βοΈ LLM & Sandbox Settings", expanded=False):
|
| 414 |
model_options = list(AVAILABLE_GROQ_MODELS)
|