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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
"""

Primitives β€” the safe callables that skills can use

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



Every self-written skill runs in a sandboxed exec context where the only

callables available are:



  β€’ VM control primitives      (screenshot, click, type_text, hotkey, …)

  β€’ Local agent primitives     (execute_shell via the SafeDesktopControl stub)

  β€’ Skill library primitives   (call_skill β€” invoke another skill by name)

  β€’ Cognitive primitives       (ai_reason, consult_queen, consult_pillars,

                                 synthesise_insight, remember, recall)

  β€’ Math + time + random utils (deterministic, safe standard library)

  β€’ Logging primitive          (emit_event)



Nothing else. No `open()` writes, no imports, no eval/exec, no __import__,

no network calls beyond what the VM control layer exposes.



The safe globals dict is assembled by `get_safe_globals(executor)` and

passed into exec() by the SkillExecutor.

"""

from __future__ import annotations

import json
import logging
import math
import random
import time
from typing import Any, Callable, Dict, List

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


# ─────────────────────────────────────────────────────────────────────────────
# VM primitive factory
# ─────────────────────────────────────────────────────────────────────────────


def _vm_primitive_factory(dispatcher: Any, action: str) -> Callable:
    """Build a primitive that routes into the VM dispatcher."""

    def primitive(**kwargs) -> Dict[str, Any]:
        session_id = kwargs.pop("session_id", None)
        return dispatcher.dispatch(
            action_name=action,
            params=dict(kwargs),
            session_id=session_id,
            source="code_architect:skill",
        )

    primitive.__name__ = f"vm_{action}"
    return primitive


VM_ACTION_NAMES: List[str] = [
    "screenshot",
    "mouse_move",
    "left_click",
    "right_click",
    "middle_click",
    "double_click",
    "triple_click",
    "left_click_drag",
    "scroll",
    "type_text",
    "press_key",
    "hotkey",
    "get_cursor_position",
    "get_screen_size",
    "list_windows",
    "get_active_window",
    "focus_window",
    "wait",
    "execute_shell",
    "execute_powershell",
]


# ─────────────────────────────────────────────────────────────────────────────
# Non-VM primitives
# ─────────────────────────────────────────────────────────────────────────────


def _emit_event_factory(thought_bus: Any, source: str = "skill") -> Callable:
    def emit_event(topic: str, payload: Any = None) -> Dict[str, Any]:
        if not thought_bus:
            return {"ok": False, "error": "no_thought_bus"}
        try:
            from aureon.core.aureon_thought_bus import Thought
            thought_bus.publish(Thought(
                source=source,
                topic=str(topic),
                payload=payload if isinstance(payload, dict) else {"value": payload},
            ))
            return {"ok": True, "topic": topic}
        except Exception as e:
            return {"ok": False, "error": str(e)}

    return emit_event


def _call_skill_factory(executor: Any) -> Callable:
    """

    Returns the `call_skill(name, **kwargs)` primitive so a compound skill

    can invoke lower-level skills.

    """

    def call_skill(name: str, **kwargs) -> Dict[str, Any]:
        if not executor:
            return {"ok": False, "error": "no_executor"}
        try:
            result = executor.execute_by_name(
                skill_name=str(name),
                params=kwargs,
                _depth=1,
            )
            return {
                "ok": result.ok,
                "skill": name,
                "duration_s": result.duration_s,
                "return_value": result.return_value,
                "error": result.error,
            }
        except Exception as e:
            return {"ok": False, "skill": name, "error": str(e)}

    return call_skill


def _safe_sleep(seconds: float) -> Dict[str, Any]:
    """Sleep, capped at 30 seconds for safety."""
    seconds = max(0.0, min(float(seconds), 30.0))
    time.sleep(seconds)
    return {"ok": True, "waited_s": seconds}


def _safe_log(message: str, level: str = "info") -> Dict[str, Any]:
    """Emit a log line from within a skill."""
    level_lower = str(level).lower()
    fn = getattr(logger, level_lower, logger.info)
    fn("[skill] %s", message)
    return {"ok": True}


# ─────────────────────────────────────────────────────────────────────────────
# Cognitive primitives β€” the building blocks of LLM-level capabilities
# ─────────────────────────────────────────────────────────────────────────────


def _ai_reason_factory() -> Callable:
    """

    ai_reason(query, context=None, max_tokens=512) β€” wraps the in-house

    AureonBrainAdapter.prompt() so skills can reason in-sandbox.



    The adapter is loaded lazily and cached at module level.

    """
    _state = {"adapter": None, "loaded": False}

    def _get_adapter():
        if _state["loaded"]:
            return _state["adapter"]
        _state["loaded"] = True
        try:
            from aureon.inhouse_ai.llm_adapter import AureonBrainAdapter
            _state["adapter"] = AureonBrainAdapter()
        except Exception:
            _state["adapter"] = None
        return _state["adapter"]

    def ai_reason(query: str, context: Any = None, max_tokens: int = 512) -> Dict[str, Any]:
        adapter = _get_adapter()
        if adapter is None:
            return {"ok": False, "error": "no_adapter", "text": ""}

        messages = [{"role": "user", "content": str(query)}]
        system = "You are the Aureon reasoning engine. Be precise and actionable."
        if context is not None:
            try:
                ctx_str = json.dumps(context, default=str)[:1200]
                system = f"{system}\nContext: {ctx_str}"
            except Exception:
                pass

        try:
            response = adapter.prompt(
                messages=messages,
                system=system,
                max_tokens=int(max_tokens),
            )
            return {
                "ok": True,
                "text": response.text,
                "stop_reason": response.stop_reason,
                "model": response.model,
            }
        except Exception as e:
            return {"ok": False, "error": str(e), "text": ""}

    return ai_reason


def _consult_queen_factory() -> Callable:
    """consult_queen(query, context=None) β€” calls QueenAIBridge.reason()."""
    _state = {"bridge": None, "loaded": False}

    def _get_bridge():
        if _state["loaded"]:
            return _state["bridge"]
        _state["loaded"] = True
        try:
            from aureon.queen.queen_inhouse_ai_bridge import get_queen_ai_bridge
            _state["bridge"] = get_queen_ai_bridge()
        except Exception:
            _state["bridge"] = None
        return _state["bridge"]

    def consult_queen(query: str, context: Any = None) -> Dict[str, Any]:
        bridge = _get_bridge()
        if bridge is None or not getattr(bridge, "is_alive", False):
            return {"ok": False, "error": "queen_unavailable", "text": ""}
        try:
            text = bridge.reason(query=str(query), context=context if isinstance(context, dict) else None)
            return {"ok": True, "text": text, "source": "queen"}
        except Exception as e:
            return {"ok": False, "error": str(e), "text": ""}

    return consult_queen


def _consult_pillars_factory() -> Callable:
    """consult_pillars(signals=None) β€” runs PillarAlignment.run_synthetic_cycle."""
    _state = {"alignment": None, "loaded": False}

    def _get_alignment():
        if _state["loaded"]:
            return _state["alignment"]
        _state["loaded"] = True
        try:
            from aureon.alignment.pillar_alignment import PillarAlignment, AlignmentConfig
            _state["alignment"] = PillarAlignment(AlignmentConfig(auto_load_pillars=False))
        except Exception:
            _state["alignment"] = None
        return _state["alignment"]

    def consult_pillars(signals: Any = None) -> Dict[str, Any]:
        alignment = _get_alignment()
        if alignment is None:
            return {"ok": False, "error": "alignment_unavailable"}
        try:
            result = alignment.run_synthetic_cycle(signals=signals if isinstance(signals, list) else None)
            return {
                "ok": True,
                "signal": result.consensus_signal,
                "confidence": result.consensus_confidence,
                "alignment_score": result.alignment_score,
                "lighthouse": result.lighthouse_cleared,
                "agreeing_pillars": result.agreeing_pillars,
                "total_pillars": result.total_pillars,
            }
        except Exception as e:
            return {"ok": False, "error": str(e)}

    return consult_pillars


def _synthesise_insight_factory() -> Callable:
    """synthesise_insight(market_data, signals=None) β€” calls MinerAIBridge."""
    _state = {"bridge": None, "loaded": False}

    def _get_bridge():
        if _state["loaded"]:
            return _state["bridge"]
        _state["loaded"] = True
        try:
            from aureon.miner.miner_inhouse_ai_bridge import get_miner_ai_bridge
            _state["bridge"] = get_miner_ai_bridge()
        except Exception:
            _state["bridge"] = None
        return _state["bridge"]

    def synthesise_insight(market_data: Any = None, signals: Any = None) -> Dict[str, Any]:
        bridge = _get_bridge()
        if bridge is None or not getattr(bridge, "is_alive", False):
            return {"ok": False, "error": "miner_unavailable"}
        try:
            md = market_data if isinstance(market_data, dict) else {}
            sigs = signals if isinstance(signals, list) else None
            insight = bridge.synthesise_insight(market_data=md, system_signals=sigs)
            return {
                "ok": True,
                "signal": insight.signal,
                "confidence": insight.confidence,
                "coherence": insight.coherence,
                "reasoning": insight.reasoning[:300] if insight.reasoning else "",
            }
        except Exception as e:
            return {"ok": False, "error": str(e)}

    return synthesise_insight


# Global in-memory scratch store for skill-local memory when the target
# isn't a real skill in the library. Thread-safe.
_SCRATCH_STORE: Dict[str, Dict[str, str]] = {}
_SCRATCH_LOCK: Any = None  # lazy init


def _get_scratch_lock():
    global _SCRATCH_LOCK
    if _SCRATCH_LOCK is None:
        import threading as _th
        _SCRATCH_LOCK = _th.RLock()
    return _SCRATCH_LOCK


def _remember_factory(executor: Any) -> Callable:
    """

    remember(target, key, value) β€” stores a key/value pair.



    If `target` matches a skill in the library, the value is written as a

    `mem:key=value` tag on the skill (persistent across saves).



    Otherwise the pair is stored in a global scratch dict keyed by target.

    This is always OK unless the arguments are unusable.

    """

    def remember(target: str, key: str, value: Any) -> Dict[str, Any]:
        target_s = str(target or "default")
        key_s = str(key or "k")
        value_s = str(value)[:200]

        # Path 1: target is a real skill in the library
        if executor is not None and getattr(executor, "library", None) is not None:
            skill = executor.library.get(target_s)
            if skill is not None:
                try:
                    tag_prefix = f"mem:{key_s}="
                    skill.tags = [t for t in (skill.tags or []) if not t.startswith(tag_prefix)]
                    skill.tags.append(tag_prefix + value_s)
                    return {"ok": True, "target": target_s, "key": key_s, "store": "skill_tag"}
                except Exception as e:
                    return {"ok": False, "error": str(e)}

        # Path 2: scratch store fallback
        lock = _get_scratch_lock()
        with lock:
            bucket = _SCRATCH_STORE.setdefault(target_s, {})
            bucket[key_s] = value_s
        return {"ok": True, "target": target_s, "key": key_s, "store": "scratch"}

    return remember


def _recall_factory(executor: Any) -> Callable:
    """

    recall(target, key) β€” retrieves a previously-remembered value.



    Searches the skill tag store first, then the scratch store. Returns

    ok=True if the target exists (even if the specific key doesn't), so

    enumeration-style calls don't fail.

    """

    def recall(target: str, key: str) -> Dict[str, Any]:
        target_s = str(target or "default")
        key_s = str(key or "k")

        # Path 1: skill tag store
        if executor is not None and getattr(executor, "library", None) is not None:
            skill = executor.library.get(target_s)
            if skill is not None:
                tag_prefix = f"mem:{key_s}="
                for tag in skill.tags or []:
                    if tag.startswith(tag_prefix):
                        return {
                            "ok": True,
                            "target": target_s,
                            "key": key_s,
                            "value": tag[len(tag_prefix):],
                            "store": "skill_tag",
                        }
                # Target exists but key doesn't β€” still ok (read success, no value)
                return {
                    "ok": True,
                    "target": target_s,
                    "key": key_s,
                    "value": None,
                    "store": "skill_tag",
                    "note": "key_not_found",
                }

        # Path 2: scratch store
        lock = _get_scratch_lock()
        with lock:
            bucket = _SCRATCH_STORE.get(target_s, {})
            if key_s in bucket:
                return {
                    "ok": True,
                    "target": target_s,
                    "key": key_s,
                    "value": bucket[key_s],
                    "store": "scratch",
                }
            return {
                "ok": True,
                "target": target_s,
                "key": key_s,
                "value": None,
                "store": "scratch",
                "note": "key_not_found",
            }

    return recall


def _list_skills_factory(executor: Any) -> Callable:
    """list_skills(level=None) β€” returns the names of skills in the library."""

    def list_skills(level: Any = None) -> Dict[str, Any]:
        if executor is None or getattr(executor, "library", None) is None:
            return {"ok": False, "error": "no_library"}
        lib = executor.library
        try:
            if level is not None:
                from aureon.code_architect.skill import SkillLevel
                lvl = SkillLevel(int(level))
                names = [s.name for s in lib.by_level(lvl)]
            else:
                names = [s.name for s in lib.all()]
            return {"ok": True, "skills": names, "count": len(names)}
        except Exception as e:
            return {"ok": False, "error": str(e)}

    return list_skills


# ─────────────────────────────────────────────────────────────────────────────
# Assembly
# ─────────────────────────────────────────────────────────────────────────────


PRIMITIVE_NAMES: List[str] = [
    # VM
    *[f"vm_{a}" for a in VM_ACTION_NAMES],
    # Utilities
    "emit_event",
    "call_skill",
    "safe_sleep",
    "safe_log",
    # Cognitive β€” LLM-level building blocks
    "ai_reason",
    "consult_queen",
    "consult_pillars",
    "synthesise_insight",
    "remember",
    "recall",
    "list_skills",
    # Math / std
    "math",
    "random",
    "json",
    # Helpers
    "time_now",
]


def get_primitives(

    dispatcher: Any = None,

    thought_bus: Any = None,

    executor: Any = None,

) -> Dict[str, Any]:
    """

    Build the full primitives dict.



    Args:

        dispatcher: VMControlDispatcher (optional) β€” enables vm_* primitives

        thought_bus: ThoughtBus (optional) β€” enables emit_event

        executor: SkillExecutor (optional) β€” enables call_skill + memory

    """
    primitives: Dict[str, Any] = {}

    # VM primitives
    if dispatcher is not None:
        for action in VM_ACTION_NAMES:
            primitives[f"vm_{action}"] = _vm_primitive_factory(dispatcher, action)

    # Event emitter
    primitives["emit_event"] = _emit_event_factory(thought_bus, source="skill")

    # Skill-calls-skill
    primitives["call_skill"] = _call_skill_factory(executor)

    # Safe utils
    primitives["safe_sleep"] = _safe_sleep
    primitives["safe_log"] = _safe_log
    primitives["time_now"] = lambda: time.time()

    # Cognitive primitives β€” the building blocks of LLM capabilities
    primitives["ai_reason"] = _ai_reason_factory()
    primitives["consult_queen"] = _consult_queen_factory()
    primitives["consult_pillars"] = _consult_pillars_factory()
    primitives["synthesise_insight"] = _synthesise_insight_factory()
    primitives["remember"] = _remember_factory(executor)
    primitives["recall"] = _recall_factory(executor)
    primitives["list_skills"] = _list_skills_factory(executor)

    # Standard-library-but-safe namespaces (read-only access)
    primitives["math"] = math
    primitives["random"] = random
    primitives["json"] = json

    return primitives


# ─────────────────────────────────────────────────────────────────────────────
# Safe globals for exec()
# ─────────────────────────────────────────────────────────────────────────────


# A minimal __builtins__ dict β€” no open(), no __import__, no exec, no eval
SAFE_BUILTINS: Dict[str, Any] = {
    # Types
    "bool": bool,
    "int": int,
    "float": float,
    "str": str,
    "bytes": bytes,
    "list": list,
    "tuple": tuple,
    "dict": dict,
    "set": set,
    "frozenset": frozenset,
    # Iteration
    "len": len,
    "range": range,
    "enumerate": enumerate,
    "zip": zip,
    "map": map,
    "filter": filter,
    "reversed": reversed,
    "sorted": sorted,
    "all": all,
    "any": any,
    "sum": sum,
    "min": min,
    "max": max,
    "abs": abs,
    "round": round,
    # Introspection (read-only)
    "isinstance": isinstance,
    "issubclass": issubclass,
    "hasattr": hasattr,
    "getattr": getattr,           # we'll reject dunder access at AST level
    "type": type,
    # Output (harmless)
    "print": print,
    "repr": repr,
    # None/True/False are keywords, not builtins
}


def get_safe_globals(

    dispatcher: Any = None,

    thought_bus: Any = None,

    executor: Any = None,

) -> Dict[str, Any]:
    """

    Return the globals dict to pass to exec() when running a skill.



    This dict contains:

      β€’ SAFE_BUILTINS in __builtins__

      β€’ All primitives

      β€’ Nothing else.

    """
    primitives = get_primitives(
        dispatcher=dispatcher,
        thought_bus=thought_bus,
        executor=executor,
    )
    globals_dict: Dict[str, Any] = {
        "__builtins__": SAFE_BUILTINS,
        **primitives,
    }
    return globals_dict