God Mode+ v3 fix: agents/debug_agent.py
Browse files- agents/debug_agent.py +14 -137
agents/debug_agent.py
CHANGED
|
@@ -1,29 +1,18 @@
|
|
| 1 |
"""
|
| 2 |
-
DebugAgent —
|
| 3 |
"""
|
| 4 |
-
import json
|
| 5 |
-
import re
|
| 6 |
-
from typing import Dict, List
|
| 7 |
import structlog
|
|
|
|
| 8 |
from .base_agent import BaseAgent
|
| 9 |
|
| 10 |
log = structlog.get_logger()
|
| 11 |
|
| 12 |
-
DEBUG_SYSTEM = """You are an expert
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
When given an error:
|
| 20 |
-
1. Identify the root cause precisely
|
| 21 |
-
2. Provide the EXACT fix (code patch or config change)
|
| 22 |
-
3. Explain WHY it failed
|
| 23 |
-
4. Suggest prevention strategies
|
| 24 |
-
|
| 25 |
-
Always return actionable fixes, not just explanations.
|
| 26 |
-
"""
|
| 27 |
|
| 28 |
|
| 29 |
class DebugAgent(BaseAgent):
|
|
@@ -31,123 +20,11 @@ class DebugAgent(BaseAgent):
|
|
| 31 |
super().__init__("DebugAgent", ws_manager, ai_router)
|
| 32 |
|
| 33 |
async def run(self, task: str, context: Dict = {}, **kwargs) -> str:
|
| 34 |
-
session_id = kwargs.get("session_id", "")
|
| 35 |
-
task_id
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
await self.emit(task_id, "agent_start", {
|
| 39 |
-
"agent": "DebugAgent",
|
| 40 |
-
"attempt": attempt,
|
| 41 |
-
"error": task[:100],
|
| 42 |
-
}, session_id)
|
| 43 |
-
|
| 44 |
-
messages = [
|
| 45 |
{"role": "system", "content": DEBUG_SYSTEM},
|
| 46 |
-
{"role": "user",
|
| 47 |
-
f"Debug and fix this issue (attempt {attempt}):\n\n"
|
| 48 |
-
f"{task}\n\n"
|
| 49 |
-
f"Provide:\n"
|
| 50 |
-
f"1. Root cause analysis\n"
|
| 51 |
-
f"2. Exact fix (code/config)\n"
|
| 52 |
-
f"3. Prevention strategy"
|
| 53 |
-
)},
|
| 54 |
]
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
messages,
|
| 58 |
-
task_id=task_id,
|
| 59 |
-
session_id=session_id,
|
| 60 |
-
temperature=0.1,
|
| 61 |
-
max_tokens=4096,
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
await self.emit(task_id, "debug_complete", {
|
| 65 |
-
"agent": "DebugAgent",
|
| 66 |
-
"has_fix": "```" in result or "fix" in result.lower(),
|
| 67 |
-
"attempt": attempt,
|
| 68 |
-
}, session_id)
|
| 69 |
-
|
| 70 |
-
return result
|
| 71 |
-
|
| 72 |
-
async def analyze_error(self, error_output: str, source_code: str = "", task_id: str = "", session_id: str = "") -> Dict:
|
| 73 |
-
"""Deep error analysis with structured output."""
|
| 74 |
-
messages = [
|
| 75 |
-
{"role": "system", "content": DEBUG_SYSTEM},
|
| 76 |
-
{"role": "user", "content": (
|
| 77 |
-
f"Analyze this error and provide structured diagnosis:\n\n"
|
| 78 |
-
f"Error:\n{error_output[:2000]}\n\n"
|
| 79 |
-
f"{'Source Code:\\n```\\n' + source_code[:1000] + '\\n```' if source_code else ''}\n\n"
|
| 80 |
-
f"Respond with JSON:\n"
|
| 81 |
-
f'{{"error_type": "...", "root_cause": "...", "fix": "...", "prevention": "...", "severity": "low|medium|high|critical"}}'
|
| 82 |
-
)},
|
| 83 |
-
]
|
| 84 |
-
raw = await self.llm(messages, task_id=task_id, session_id=session_id, temperature=0.1, max_tokens=1000)
|
| 85 |
-
try:
|
| 86 |
-
start = raw.find("{")
|
| 87 |
-
end = raw.rfind("}") + 1
|
| 88 |
-
return json.loads(raw[start:end])
|
| 89 |
-
except Exception:
|
| 90 |
-
return {"error_type": "unknown", "root_cause": error_output[:200], "fix": raw[:500], "severity": "medium"}
|
| 91 |
-
|
| 92 |
-
async def self_heal_loop(
|
| 93 |
-
self,
|
| 94 |
-
code: str,
|
| 95 |
-
error: str,
|
| 96 |
-
max_retries: int = 3,
|
| 97 |
-
task_id: str = "",
|
| 98 |
-
session_id: str = "",
|
| 99 |
-
) -> str:
|
| 100 |
-
"""Self-healing loop — generate fix, validate, retry."""
|
| 101 |
-
current_code = code
|
| 102 |
-
current_error = error
|
| 103 |
-
|
| 104 |
-
for attempt in range(1, max_retries + 1):
|
| 105 |
-
await self.emit(task_id, "self_heal_attempt", {
|
| 106 |
-
"attempt": attempt,
|
| 107 |
-
"max_retries": max_retries,
|
| 108 |
-
"error_snippet": current_error[:100],
|
| 109 |
-
}, session_id)
|
| 110 |
-
|
| 111 |
-
messages = [
|
| 112 |
-
{"role": "system", "content": DEBUG_SYSTEM},
|
| 113 |
-
{"role": "user", "content": (
|
| 114 |
-
f"Fix attempt {attempt}/{max_retries}:\n\n"
|
| 115 |
-
f"Error: {current_error}\n\n"
|
| 116 |
-
f"Code:\n```\n{current_code[:3000]}\n```\n\n"
|
| 117 |
-
f"Return ONLY the fixed code."
|
| 118 |
-
)},
|
| 119 |
-
]
|
| 120 |
-
|
| 121 |
-
fixed = await self.llm(messages, task_id=task_id, session_id=session_id, temperature=0.1, max_tokens=8192)
|
| 122 |
-
fixed = self._strip_fences(fixed)
|
| 123 |
-
|
| 124 |
-
# Validate syntax for Python
|
| 125 |
-
validation = await self._validate_python(fixed)
|
| 126 |
-
if validation["valid"]:
|
| 127 |
-
await self.emit(task_id, "self_heal_success", {
|
| 128 |
-
"attempt": attempt,
|
| 129 |
-
"code_lines": len(fixed.split("\n")),
|
| 130 |
-
}, session_id)
|
| 131 |
-
return fixed
|
| 132 |
-
else:
|
| 133 |
-
current_code = fixed
|
| 134 |
-
current_error = validation["error"]
|
| 135 |
-
log.warning("Self-heal attempt failed", attempt=attempt, error=current_error[:100])
|
| 136 |
-
|
| 137 |
-
await self.emit(task_id, "self_heal_failed", {"attempts": max_retries}, session_id)
|
| 138 |
-
return current_code # Return best attempt
|
| 139 |
-
|
| 140 |
-
async def _validate_python(self, code: str) -> Dict:
|
| 141 |
-
"""Quick Python syntax validation."""
|
| 142 |
-
try:
|
| 143 |
-
compile(code, "<string>", "exec")
|
| 144 |
-
return {"valid": True, "error": ""}
|
| 145 |
-
except SyntaxError as e:
|
| 146 |
-
return {"valid": False, "error": f"SyntaxError: {e}"}
|
| 147 |
-
except Exception as e:
|
| 148 |
-
return {"valid": False, "error": str(e)}
|
| 149 |
-
|
| 150 |
-
def _strip_fences(self, text: str) -> str:
|
| 151 |
-
text = re.sub(r"^```[\w]*\n", "", text.strip())
|
| 152 |
-
text = re.sub(r"\n```$", "", text)
|
| 153 |
-
return text
|
|
|
|
| 1 |
"""
|
| 2 |
+
DebugAgent — Code debugging via LLMRouter
|
| 3 |
"""
|
|
|
|
|
|
|
|
|
|
| 4 |
import structlog
|
| 5 |
+
from typing import Dict
|
| 6 |
from .base_agent import BaseAgent
|
| 7 |
|
| 8 |
log = structlog.get_logger()
|
| 9 |
|
| 10 |
+
DEBUG_SYSTEM = """You are an expert debugger and code reviewer.
|
| 11 |
+
When given code with errors, you:
|
| 12 |
+
1. Identify the root cause
|
| 13 |
+
2. Explain the issue clearly
|
| 14 |
+
3. Provide a complete fixed version
|
| 15 |
+
4. Suggest best practices to prevent similar issues"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
class DebugAgent(BaseAgent):
|
|
|
|
| 20 |
super().__init__("DebugAgent", ws_manager, ai_router)
|
| 21 |
|
| 22 |
async def run(self, task: str, context: Dict = {}, **kwargs) -> str:
|
| 23 |
+
session_id = kwargs.get("session_id", context.get("session_id", ""))
|
| 24 |
+
task_id = kwargs.get("task_id", context.get("task_id", ""))
|
| 25 |
+
messages = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
{"role": "system", "content": DEBUG_SYSTEM},
|
| 27 |
+
{"role": "user", "content": f"Debug this:\n\n{task}"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
]
|
| 29 |
+
await self.emit(task_id, "debug_started", {"task": task[:100]}, session_id)
|
| 30 |
+
return await self.ask_llm(messages=messages, task_id=task_id, session_id=session_id, temperature=0.2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|