File size: 15,964 Bytes
1ccf652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""

SkillExecutor β€” runs validated skills in the safe exec sandbox

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



Executes skills against:

  β€’ A VMControlDispatcher      (VM target)

  β€’ The local system           (local target β€” future; currently the same

    dispatcher can be pointed at a real Windows VM via WinRM)



For compound / workflow / role skills, the executor resolves dependencies

via the SkillLibrary and caches compiled function objects to avoid

recompiling on every call.



Safety:

  β€’ Uses `primitives.get_safe_globals(dispatcher, thought_bus, executor)`

    as the exec globals dict (SAFE_BUILTINS only).

  β€’ Re-runs the static safety check before first execution.

  β€’ Bounded recursion depth (max 10) to prevent infinite compound chains.

  β€’ Per-execution timeout via watchdog thread (soft β€” Python can't hard

    cancel arbitrary code, but we record and flag long runs).

"""

from __future__ import annotations

import logging
import threading
import time
import traceback
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List, Optional

from aureon.code_architect.skill import Skill, SkillStatus
from aureon.code_architect.skill_library import SkillLibrary
from aureon.code_architect.primitives import get_safe_globals
from aureon.code_architect.validator import SkillValidator

logger = logging.getLogger("aureon.code_architect.executor")


MAX_DEPTH = 10


# ─────────────────────────────────────────────────────────────────────────────
# Result dataclass
# ─────────────────────────────────────────────────────────────────────────────


@dataclass
class SkillExecutionResult:
    skill_name: str = ""
    ok: bool = False
    return_value: Any = None
    error: Optional[str] = None
    traceback: Optional[str] = None
    duration_s: float = 0.0
    depth: int = 0
    timestamp: float = field(default_factory=time.time)
    sub_results: List[Dict[str, Any]] = field(default_factory=list)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "skill_name": self.skill_name,
            "ok": self.ok,
            "return_value": self.return_value,
            "error": self.error,
            "traceback": self.traceback,
            "duration_s": round(self.duration_s, 4),
            "depth": self.depth,
            "timestamp": self.timestamp,
            "sub_results_count": len(self.sub_results),
        }


# ─────────────────────────────────────────────────────────────────────────────
# SkillExecutor
# ─────────────────────────────────────────────────────────────────────────────


class SkillExecutor:
    """

    Runs skills in a safe exec sandbox.



    Attributes:

        library    : the SkillLibrary (source of skills)

        dispatcher : the VMControlDispatcher (backends for vm_* primitives)

        validator  : the SkillValidator (re-runs static safety on first use)

        thought_bus: optional ThoughtBus for emit_event primitive

    """

    def __init__(

        self,

        library: Optional[SkillLibrary] = None,

        dispatcher: Any = None,

        validator: Optional[SkillValidator] = None,

        thought_bus: Any = None,

        require_static_check: bool = True,

    ):
        self.library = library
        self.dispatcher = dispatcher
        self.validator = validator or SkillValidator()
        self.thought_bus = thought_bus
        self.require_static_check = require_static_check

        self._compiled_cache: Dict[str, Callable] = {}           # skill_name β†’ entry function
        self._lock = threading.RLock()

        # Metrics
        self._total_executions = 0
        self._successes = 0
        self._failures = 0

    # ─────────────────────────────────────────────────────────────────────
    # Compilation
    # ─────────────────────────────────────────────────────────────────────

    def _safe_globals(self) -> Dict[str, Any]:
        return get_safe_globals(
            dispatcher=self.dispatcher,
            thought_bus=self.thought_bus,
            executor=self,
        )

    def _compile(self, skill: Skill) -> Callable:
        """Compile a skill's source into a callable (cached)."""
        with self._lock:
            cached = self._compiled_cache.get(skill.name)
            if cached is not None:
                return cached

        # Belt & braces: re-run static check before compile
        if self.require_static_check:
            safe, errors = self.validator.static_check(skill.code)
            if not safe:
                raise RuntimeError(f"skill {skill.name} failed static safety: {errors[:3]}")

        # exec inside a *fresh* globals dict for compilation so function refs
        # are properly bound. The execution will use _safe_globals() again.
        compile_globals = self._safe_globals()
        local_scope: Dict[str, Any] = {}
        try:
            compiled_code = compile(skill.code, f"<skill:{skill.name}>", "exec")
            exec(compiled_code, compile_globals, local_scope)
        except Exception as e:
            raise RuntimeError(f"skill {skill.name} compile failed: {e}")

        entry = skill.entry_function or skill.name
        func = local_scope.get(entry)
        if not callable(func):
            raise RuntimeError(f"skill {skill.name} has no callable entry function '{entry}'")

        with self._lock:
            self._compiled_cache[skill.name] = func
        return func

    def invalidate_cache(self, skill_name: Optional[str] = None) -> None:
        with self._lock:
            if skill_name is None:
                self._compiled_cache.clear()
            else:
                self._compiled_cache.pop(skill_name, None)

    def _execution_is_simulated(self, params: Optional[Dict[str, Any]]) -> bool:
        """Return True only when the selected dispatcher session is in-memory."""
        if self.dispatcher is None:
            return False
        try:
            session_id = (params or {}).get("session_id")
            controller = self.dispatcher.get_session(session_id)
            session = getattr(controller, "session", None)
            return str(getattr(session, "backend", "")).casefold() == "simulated"
        except Exception:
            return False

    def _blocked_execution(

        self,

        skill: Skill,

        *,

        error: str,

        depth: int,

        started_at: float,

    ) -> SkillExecutionResult:
        self.invalidate_cache(skill.name)
        result = SkillExecutionResult(
            skill_name=skill.name,
            ok=False,
            error=error,
            depth=depth,
            duration_s=time.time() - started_at,
        )
        self._failures += 1
        if self.library:
            self.library.record_execution(
                skill.name,
                success=False,
                duration_s=result.duration_s,
                error=result.error,
            )
        return result

    # ─────────────────────────────────────────────────────────────────────
    # Execution
    # ─────────────────────────────────────────────────────────────────────

    def execute(

        self,

        skill: Skill,

        params: Optional[Dict[str, Any]] = None,

        _depth: int = 0,

    ) -> SkillExecutionResult:
        """Execute a Skill object."""
        if _depth > MAX_DEPTH:
            return SkillExecutionResult(
                skill_name=skill.name,
                ok=False,
                error=f"max_recursion_depth_exceeded: {_depth}",
                depth=_depth,
            )

        self._total_executions += 1
        start = time.time()
        result = SkillExecutionResult(skill_name=skill.name, depth=_depth)

        status_value = str(getattr(skill.status, "value", skill.status) or "").casefold()
        non_executable_statuses = {
            "proposed",
            "pending",
            "pending_review",
            "rejected",
            "blocked",
            "deprecated",
        }
        if status_value in non_executable_statuses:
            return self._blocked_execution(
                skill,
                error=f"skill_status_not_executable:{status_value or 'unknown'}",
                depth=_depth,
                started_at=start,
            )

        tags = set(skill.tags or [])
        if (
            "requires_explicit_approval" in tags
            and status_value not in {SkillStatus.APPROVED.value, SkillStatus.ACTIVE.value}
        ):
            return self._blocked_execution(
                skill,
                error="skill_approval_required",
                depth=_depth,
                started_at=start,
            )
        if "live_execution_disabled" in tags and not self._execution_is_simulated(params):
            return self._blocked_execution(
                skill,
                error="skill_live_execution_disabled",
                depth=_depth,
                started_at=start,
            )

        # Compile (uses cache)
        try:
            func = self._compile(skill)
        except Exception as e:
            result.ok = False
            result.error = str(e)
            result.traceback = traceback.format_exc()
            result.duration_s = time.time() - start
            self._failures += 1
            if self.library:
                self.library.record_execution(skill.name, success=False,
                                              duration_s=result.duration_s,
                                              error=result.error)
            return result

        # We need the globals dict from the compiled function so primitives
        # like call_skill/emit_event see the right executor. The function
        # was exec'd with our globals, so it already captures them.

        # Run the entry function
        try:
            ret = func(**(params or {}))
            result.ok = True
            result.return_value = ret
            self._successes += 1
        except Exception as e:
            result.ok = False
            result.error = str(e)
            result.traceback = traceback.format_exc()
            self._failures += 1

        result.duration_s = time.time() - start

        if self.library:
            self.library.record_execution(
                name=skill.name,
                success=result.ok,
                duration_s=result.duration_s,
                error=result.error,
            )

        # Publish
        self._publish(skill, result)

        return result

    def execute_by_name(

        self,

        skill_name: str,

        params: Optional[Dict[str, Any]] = None,

        _depth: int = 0,

    ) -> SkillExecutionResult:
        """Look up a skill by name and execute it."""
        if self.library is None:
            return SkillExecutionResult(
                skill_name=skill_name,
                ok=False,
                error="no_library",
            )
        skill = self.library.get(skill_name)
        if not skill:
            return SkillExecutionResult(
                skill_name=skill_name,
                ok=False,
                error="skill_not_found",
            )
        return self.execute(skill, params=params, _depth=_depth)

    def execute_with_dependencies(

        self,

        skill_name: str,

        params: Optional[Dict[str, Any]] = None,

    ) -> SkillExecutionResult:
        """

        Resolve the dependency tree and execute the top-level skill.

        The dependencies themselves are executed lazily via call_skill,

        but this method pre-compiles them for speed.

        """
        if self.library is None:
            return SkillExecutionResult(
                skill_name=skill_name,
                ok=False,
                error="no_library",
            )

        deps = self.library.resolve_dependencies(skill_name)
        # Pre-compile all dependencies so call_skill finds them ready
        for dep in deps:
            try:
                self._compile(dep)
            except Exception as e:
                logger.warning("Failed to pre-compile dep %s: %s", dep.name, e)

        return self.execute_by_name(skill_name, params=params)

    # ─────────────────────────────────────────────────────────────────────
    # Publishing
    # ─────────────────────────────────────────────────────────────────────

    def _publish(self, skill: Skill, result: SkillExecutionResult) -> None:
        if not self.thought_bus:
            return
        try:
            from aureon.core.aureon_thought_bus import Thought
            self.thought_bus.publish(Thought(
                source="code_architect.executor",
                topic=f"skill.executed.{'ok' if result.ok else 'fail'}",
                payload={
                    "skill_name": skill.name,
                    "level": skill.level.name,
                    "ok": result.ok,
                    "duration_s": result.duration_s,
                    "depth": result.depth,
                    "error": result.error,
                },
            ))
        except Exception:
            pass

    # ─────────────────────────────────────────────────────────────────────
    # Status
    # ─────────────────────────────────────────────────────────────────────

    def get_status(self) -> Dict[str, Any]:
        return {
            "total_executions": self._total_executions,
            "successes": self._successes,
            "failures": self._failures,
            "success_rate": (
                self._successes / self._total_executions if self._total_executions > 0 else 0.0
            ),
            "cached_compilations": len(self._compiled_cache),
            "dispatcher_wired": self.dispatcher is not None,
        }