PYAE1994 commited on
Commit
1bf29fe
·
verified ·
1 Parent(s): b530b5f

Update app/agent/executor.py

Browse files
Files changed (1) hide show
  1. app/agent/executor.py +47 -28
app/agent/executor.py CHANGED
@@ -9,62 +9,83 @@ class Executor:
9
  self.healer = SelfHealer(llm=llm)
10
 
11
  # =====================================================
12
- # PLAN EXECUTION (SAFE PIPELINE)
13
  # =====================================================
14
  def run_plan(self, plan):
15
 
16
  results = []
17
 
 
 
 
18
  steps = plan.get("steps", [])
 
19
  if not isinstance(steps, list):
20
- return {"error": "Invalid plan format"}
21
 
22
  for step in steps:
23
 
24
  print(f"🚀 Executing step: {step}")
25
 
26
  # ==============================
27
- # TOOL EXECUTION LAYER
28
  # ==============================
29
  tool_result = self._execute_as_tool(step)
30
 
31
  # ==============================
32
- # SELF HEALING LAYER
33
  # ==============================
34
  try:
35
  fix = self.healer.heal(step, tool_result)
36
 
37
- if fix:
38
  print("🛠️ Self-healing triggered")
39
- tool_result = execute_tool_call(fix)
 
 
 
 
 
40
 
41
  except Exception as e:
42
  tool_result = {
43
- "error": f"healer failed: {str(e)}"
44
  }
45
 
46
  # ==============================
47
- # LLM ANALYSIS (NO RECURSION)
48
  # ==============================
49
  llm_result = None
50
 
51
  if self.llm:
52
  try:
53
- llm_result = self.llm([
54
  {
55
  "role": "system",
56
- "content": "Analyze execution result only. Do NOT execute tools."
 
 
 
57
  },
58
  {
59
  "role": "user",
60
- "content": f"Step: {step}\nResult: {tool_result}"
61
  }
62
  ])
 
 
 
 
 
 
 
 
 
63
  except Exception as e:
64
  llm_result = {"error": str(e)}
65
 
66
  # ==============================
67
- # STORE RESULT
68
  # ==============================
69
  results.append({
70
  "step": step,
@@ -72,37 +93,35 @@ class Executor:
72
  "llm_result": llm_result
73
  })
74
 
75
- return results
 
 
 
76
 
77
  # =====================================================
78
- # TOOL EXECUTION SAFE WRAPPER
79
  # =====================================================
80
  def _execute_as_tool(self, step):
81
 
82
  try:
83
- # STEP NORMALIZATION
84
  if isinstance(step, str):
85
-
86
- # default safe shell execution
87
  return execute_tool_call({
88
  "tool": "run_shell",
89
  "args": {
90
- "cmd": step
91
  }
92
  })
93
 
94
- elif isinstance(step, dict):
 
 
 
 
95
 
96
  return execute_tool_call(step)
97
 
98
- else:
99
-
100
- return {
101
- "error": "Invalid step type"
102
- }
103
 
104
  except Exception as e:
105
-
106
- return {
107
- "error": str(e)
108
- }
 
9
  self.healer = SelfHealer(llm=llm)
10
 
11
  # =====================================================
12
+ # MAIN PLAN EXECUTION PIPELINE
13
  # =====================================================
14
  def run_plan(self, plan):
15
 
16
  results = []
17
 
18
+ if not isinstance(plan, dict):
19
+ return {"error": "Plan must be dict"}
20
+
21
  steps = plan.get("steps", [])
22
+
23
  if not isinstance(steps, list):
24
+ return {"error": "Invalid steps format"}
25
 
26
  for step in steps:
27
 
28
  print(f"🚀 Executing step: {step}")
29
 
30
  # ==============================
31
+ # 1. TOOL EXECUTION
32
  # ==============================
33
  tool_result = self._execute_as_tool(step)
34
 
35
  # ==============================
36
+ # 2. SELF HEAL LAYER (SAFE)
37
  # ==============================
38
  try:
39
  fix = self.healer.heal(step, tool_result)
40
 
41
+ if isinstance(fix, dict) and "tool" in fix:
42
  print("🛠️ Self-healing triggered")
43
+
44
+ healed_result = execute_tool_call(fix)
45
+
46
+ # only replace if fix is successful
47
+ if healed_result:
48
+ tool_result = healed_result
49
 
50
  except Exception as e:
51
  tool_result = {
52
+ "error": f"healer crashed: {str(e)}"
53
  }
54
 
55
  # ==============================
56
+ # 3. LLM ANALYSIS (SAFE NON-EXEC MODE)
57
  # ==============================
58
  llm_result = None
59
 
60
  if self.llm:
61
  try:
62
+ response = self.llm([
63
  {
64
  "role": "system",
65
+ "content": (
66
+ "You are a strict analysis engine. "
67
+ "Only analyze results. Never call tools."
68
+ )
69
  },
70
  {
71
  "role": "user",
72
+ "content": f"Step:\n{step}\n\nResult:\n{tool_result}"
73
  }
74
  ])
75
+
76
+ # SAFE extraction
77
+ if isinstance(response, dict):
78
+ llm_result = response.get("choices", [{}])[0] \
79
+ .get("message", {}) \
80
+ .get("content", response)
81
+ else:
82
+ llm_result = response
83
+
84
  except Exception as e:
85
  llm_result = {"error": str(e)}
86
 
87
  # ==============================
88
+ # 4. STORE RESULT
89
  # ==============================
90
  results.append({
91
  "step": step,
 
93
  "llm_result": llm_result
94
  })
95
 
96
+ return {
97
+ "status": "completed",
98
+ "results": results
99
+ }
100
 
101
  # =====================================================
102
+ # SAFE TOOL EXECUTION WRAPPER
103
  # =====================================================
104
  def _execute_as_tool(self, step):
105
 
106
  try:
107
+ # string step → shell execution
108
  if isinstance(step, str):
 
 
109
  return execute_tool_call({
110
  "tool": "run_shell",
111
  "args": {
112
+ "cmd": step.strip()
113
  }
114
  })
115
 
116
+ # dict tool call
117
+ if isinstance(step, dict):
118
+
119
+ if "tool" not in step or "args" not in step:
120
+ return {"error": "Invalid tool format"}
121
 
122
  return execute_tool_call(step)
123
 
124
+ return {"error": "Unsupported step type"}
 
 
 
 
125
 
126
  except Exception as e:
127
+ return {"error": str(e)}