PYAE1994 commited on
Commit
d045ad7
·
verified ·
1 Parent(s): 4133259

Update app/agent/self_heal.py

Browse files
Files changed (1) hide show
  1. app/agent/self_heal.py +113 -9
app/agent/self_heal.py CHANGED
@@ -1,10 +1,12 @@
1
  import json
2
 
3
 
4
- def check_and_fix(result, agent):
5
-
 
 
6
  """
7
- If error ask AI to fix and retry
8
  """
9
 
10
  if isinstance(result, dict) and "error" in result:
@@ -18,12 +20,114 @@ Error:
18
  Fix it and return corrected tool call JSON.
19
  """
20
 
21
- fixed = agent.run(fix_prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- try:
24
- tool_call = json.loads(fixed)
25
- return tool_call
26
- except:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  return None
28
 
29
- return None
 
 
 
 
 
 
 
 
 
 
1
  import json
2
 
3
 
4
+ # =====================================================
5
+ # LEGACY FUNCTION (Backward Compatibility)
6
+ # =====================================================
7
+ def check_and_fix(result, agent=None):
8
  """
9
+ Legacy fallback healer (old system support)
10
  """
11
 
12
  if isinstance(result, dict) and "error" in result:
 
20
  Fix it and return corrected tool call JSON.
21
  """
22
 
23
+ if agent:
24
+ fixed = agent.run(fix_prompt)
25
+
26
+ try:
27
+ tool_call = json.loads(fixed)
28
+ return tool_call
29
+ except:
30
+ return None
31
+
32
+ return None
33
+
34
+
35
+ # =====================================================
36
+ # MODERN SELF-HEALER CLASS (STEP 5.1 CORE)
37
+ # =====================================================
38
+ class SelfHealer:
39
+
40
+ def __init__(self, llm=None):
41
+ self.llm = llm
42
+ self.failure_history = []
43
+
44
+ # =========================
45
+ # DETECT FAILURE
46
+ # =========================
47
+ def is_failure(self, result: dict):
48
+ if not result:
49
+ return True
50
+
51
+ if isinstance(result, dict):
52
 
53
+ if result.get("exit_code", 0) != 0:
54
+ return True
55
+
56
+ if "error" in result:
57
+ return True
58
+
59
+ if result.get("stderr"):
60
+ return True
61
+
62
+ return False
63
+
64
+ # =========================
65
+ # ANALYZE ERROR
66
+ # =========================
67
+ def analyze(self, result: dict, step: str):
68
+
69
+ error_text = (
70
+ result.get("stderr")
71
+ or result.get("error")
72
+ or "unknown error"
73
+ )
74
+
75
+ prompt = f"""
76
+ You are a debugging and repair system.
77
+
78
+ A tool execution failed.
79
+
80
+ STEP:
81
+ {step}
82
+
83
+ ERROR:
84
+ {error_text}
85
+
86
+ Return ONLY a corrected tool call JSON if fix is possible:
87
+
88
+ Example:
89
+ {{
90
+ "tool": "run_shell",
91
+ "args": {{
92
+ "cmd": "fixed command"
93
+ }}
94
+ }}
95
+
96
+ If cannot fix → return:
97
+ {{"fix": null}}
98
+ """
99
+
100
+ if self.llm:
101
+ try:
102
+ response = self.llm([
103
+ {"role": "user", "content": prompt}
104
+ ])
105
+
106
+ content = response["choices"][0]["message"]["content"]
107
+
108
+ return json.loads(content)
109
+
110
+ except:
111
+ return {"fix": None}
112
+
113
+ return {"fix": None}
114
+
115
+ # =========================
116
+ # MAIN HEALING PIPELINE
117
+ # =========================
118
+ def heal(self, step: str, result: dict):
119
+
120
+ # no failure → skip
121
+ if not self.is_failure(result):
122
  return None
123
 
124
+ # store failure
125
+ self.failure_history.append({
126
+ "step": step,
127
+ "result": result
128
+ })
129
+
130
+ # analyze + generate fix
131
+ analysis = self.analyze(result, step)
132
+
133
+ return analysis.get("fix")