Spaces:
Sleeping
Sleeping
Create src/macg/agents/reviewer.py
Browse files- src/macg/agents/reviewer.py +53 -0
src/macg/agents/reviewer.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import ast
|
| 3 |
+
from macg.llm import LLMClient
|
| 4 |
+
from macg.protocol import Artifact
|
| 5 |
+
|
| 6 |
+
REVIEWER_SYSTEM = """You are ReviewerAgent.
|
| 7 |
+
Goal: improve correctness, safety, style, and testability.
|
| 8 |
+
Rules:
|
| 9 |
+
- Be specific and actionable.
|
| 10 |
+
- If code likely fails tests, explain why.
|
| 11 |
+
- Output only bullet points (no code).
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
class ReviewerAgent:
|
| 15 |
+
def __init__(self, llm: LLMClient | None = None) -> None:
|
| 16 |
+
self.llm = llm # optional
|
| 17 |
+
|
| 18 |
+
def _static_checks(self, code: str) -> list[str]:
|
| 19 |
+
notes = []
|
| 20 |
+
try:
|
| 21 |
+
ast.parse(code)
|
| 22 |
+
except SyntaxError as e:
|
| 23 |
+
notes.append(f"- SyntaxError: {e}")
|
| 24 |
+
return notes
|
| 25 |
+
|
| 26 |
+
if "eval(" in code or "exec(" in code:
|
| 27 |
+
notes.append("- Avoid eval/exec unless absolutely necessary.")
|
| 28 |
+
if "import *" in code:
|
| 29 |
+
notes.append("- Avoid wildcard imports.")
|
| 30 |
+
return notes
|
| 31 |
+
|
| 32 |
+
def run(self, art: Artifact) -> Artifact:
|
| 33 |
+
notes = self._static_checks(art.code)
|
| 34 |
+
|
| 35 |
+
if self.llm:
|
| 36 |
+
prompt = f"""
|
| 37 |
+
Task:
|
| 38 |
+
{art.task}
|
| 39 |
+
|
| 40 |
+
Code:
|
| 41 |
+
{art.code}
|
| 42 |
+
|
| 43 |
+
Existing notes:
|
| 44 |
+
{chr(10).join(notes) if notes else "(none)"}
|
| 45 |
+
|
| 46 |
+
Return improved review notes as bullet points.
|
| 47 |
+
""".strip()
|
| 48 |
+
llm_notes = self.llm.complete(REVIEWER_SYSTEM, prompt).strip()
|
| 49 |
+
art.review_notes = (("\n".join(notes) + "\n") if notes else "") + llm_notes
|
| 50 |
+
else:
|
| 51 |
+
art.review_notes = "\n".join(notes) if notes else "- Looks OK (static checks only)."
|
| 52 |
+
|
| 53 |
+
return art
|