File size: 5,039 Bytes
2291861
3f3918a
e36ea50
2291861
 
 
 
3f3918a
aca7e7a
3f3918a
 
 
e36ea50
3f3918a
2291861
 
1bf29fe
 
 
aca7e7a
 
1bf29fe
aca7e7a
9ef3a3e
 
aca7e7a
3f3918a
 
 
e36ea50
c7fc003
 
e36ea50
c7fc003
 
e36ea50
 
aca7e7a
e36ea50
 
1bf29fe
e36ea50
2291861
 
c7fc003
3f3918a
 
2291861
 
1bf29fe
 
 
 
aca7e7a
 
e36ea50
aca7e7a
c7fc003
aca7e7a
e36ea50
c7fc003
 
 
e36ea50
 
 
 
 
c7fc003
e36ea50
9ef3a3e
e36ea50
 
 
 
 
c7fc003
e36ea50
 
 
c7fc003
e36ea50
c7fc003
e36ea50
c7fc003
 
 
 
 
 
 
e36ea50
c7fc003
e36ea50
c7fc003
e36ea50
c7fc003
 
aca7e7a
c7fc003
e36ea50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7fc003
 
1bf29fe
c7fc003
 
 
aca7e7a
c7fc003
aca7e7a
e36ea50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca7e7a
c7fc003
e36ea50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from tools_runtime import execute_tool_call
from self_heal import SelfHealer
import shlex


class Executor:

    def __init__(self, llm=None):
        self.llm = llm
        self.healer = SelfHealer(llm=llm)

    # =====================================================
    # MAIN PIPELINE
    # =====================================================
    def run_plan(self, plan):

        if not isinstance(plan, dict):
            return {"error": "Plan must be dict"}

        steps = plan.get("steps", [])
        if not isinstance(steps, list):
            return {"error": "Invalid steps format"}

        results = []

        for step in steps:

            print(f"🚀 Executing step: {step}")

            # STEP 1: convert to action
            action = self._interpret_step(step)

            # STEP 2: execute tool
            tool_result = self._execute_tool(action)

            # STEP 3: self-heal (safe retry)
            tool_result = self._safe_heal(step, tool_result)

            # STEP 4: optional LLM analysis
            llm_result = self._llm_analyze(step, action, tool_result)

            # STEP 5: store
            results.append({
                "step": step,
                "action": action,
                "tool_result": tool_result,
                "llm_result": llm_result
            })

        return {
            "status": "completed",
            "results": results
        }

    # =====================================================
    # STEP INTERPRETER (FIXED - NO NOOP TRAP)
    # =====================================================
    def _interpret_step(self, step):

        # already structured tool
        if isinstance(step, dict):
            return step

        if not isinstance(step, str):
            return {
                "tool": "run_shell",
                "args": {"cmd": "echo invalid_step"}
            }

        cmd = step.strip()

        if not cmd:
            return {
                "tool": "run_shell",
                "args": {"cmd": "echo empty_step"}
            }

        # 🔥 IMPORTANT FIX:
        # DO NOT block natural language anymore
        # Instead: ALWAYS convert to shell-safe fallback

        shell_like = self._looks_like_shell(cmd)

        if shell_like:
            return {
                "tool": "run_shell",
                "args": {
                    "cmd": cmd
                }
            }

        # fallback: convert to safe echo (never noop)
        return {
            "tool": "run_shell",
            "args": {
                "cmd": f"echo '[interpreted step] {shlex.quote(cmd)}'"
            }
        }

    # =====================================================
    # SIMPLE HEURISTIC (NOT BLOCKING)
    # =====================================================
    def _looks_like_shell(self, cmd: str):

        shell_signals = [
            "ls", "cd", "mkdir", "touch",
            "python", "pip", "rm", "echo",
            "./", "git", "npm", "curl"
        ]

        first = cmd.split()[0].lower() if cmd.split() else ""

        return first in shell_signals

    # =====================================================
    # TOOL EXECUTION
    # =====================================================
    def _execute_tool(self, action):

        try:
            if not isinstance(action, dict):
                return {"error": "invalid_action"}

            return execute_tool_call(action)

        except Exception as e:
            return {"error": str(e)}

    # =====================================================
    # SELF HEAL (SAFE RETRY ONLY)
    # =====================================================
    def _safe_heal(self, step, tool_result):

        try:
            fix = self.healer.heal(step, tool_result)

            if isinstance(fix, dict) and "tool" in fix:
                print("🛠️ Self-healing triggered")
                return execute_tool_call(fix)

        except Exception as e:
            return {
                "error": f"healer_error: {str(e)}"
            }

        return tool_result

    # =====================================================
    # LLM ANALYSIS (NO EXECUTION)
    # =====================================================
    def _llm_analyze(self, step, action, tool_result):

        if not self.llm:
            return None

        try:
            response = self.llm([
                {
                    "role": "system",
                    "content": "Analyze only. NEVER execute tools."
                },
                {
                    "role": "user",
                    "content": f"""
Step: {step}
Action: {action}
Result: {tool_result}
"""
                }
            ])

            if isinstance(response, dict):
                return (
                    response.get("choices", [{}])[0]
                    .get("message", {})
                    .get("content", "LLM_EMPTY")
                )

            return str(response)

        except Exception as e:
            return f"llm_error: {str(e)}"