|
| 1 | +"""Optional runtime facade that wires the Braid subsystem.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import Any, Dict, Iterable, List, Sequence |
| 7 | + |
| 8 | +from core.braid import ( |
| 9 | + BraidRuntime, |
| 10 | + KernelAdapter, |
| 11 | + make_default_runtime, |
| 12 | +) |
| 13 | + |
| 14 | +from .information_flow import InformationFlow |
| 15 | +from .runtime_orchestrator import RuntimeOrchestrator |
| 16 | + |
| 17 | + |
| 18 | +@dataclass(slots=True) |
| 19 | +class BraidEnabledRuntime: |
| 20 | + """Integrate :class:`BraidRuntime` with the core orchestration pipeline.""" |
| 21 | + |
| 22 | + base_runtime: RuntimeOrchestrator |
| 23 | + info_flow: InformationFlow |
| 24 | + braid_runtime: BraidRuntime |
| 25 | + braid_adapter: KernelAdapter |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def with_defaults( |
| 29 | + cls, |
| 30 | + collector: Any, |
| 31 | + backend_adapter: Any, |
| 32 | + glue_sink: Any, |
| 33 | + info_flow: InformationFlow, |
| 34 | + ) -> "BraidEnabledRuntime": |
| 35 | + """Create a runtime that wires together the existing components.""" |
| 36 | + |
| 37 | + _ = backend_adapter # maintained for API symmetry; base runtime handles adapters internally |
| 38 | + base_runtime = RuntimeOrchestrator(collector=collector, sink=glue_sink) |
| 39 | + braid_runtime = make_default_runtime() |
| 40 | + braid_adapter = KernelAdapter(runtime=braid_runtime) |
| 41 | + return cls( |
| 42 | + base_runtime=base_runtime, |
| 43 | + info_flow=info_flow, |
| 44 | + braid_runtime=braid_runtime, |
| 45 | + braid_adapter=braid_adapter, |
| 46 | + ) |
| 47 | + |
| 48 | + def run_once(self, raw_input: Any) -> Dict[str, Any]: |
| 49 | + """Execute a single cycle through runtime, flow, and braid layers.""" |
| 50 | + |
| 51 | + runtime_result = self._run_base_runtime(raw_input) |
| 52 | + signal = self._extract_signal(runtime_result, raw_input) |
| 53 | + flow_result = self.info_flow.step(signal) |
| 54 | + |
| 55 | + prompt = self._make_prompt_from_flow(flow_result) |
| 56 | + context = self._make_context_from_flow(flow_result) |
| 57 | + |
| 58 | + self.braid_adapter.submit_prompt(prompt, context=context) |
| 59 | + braid_result = self.braid_adapter.step(max_loops=4) |
| 60 | + |
| 61 | + return { |
| 62 | + "runtime": runtime_result, |
| 63 | + "flow": flow_result, |
| 64 | + "braid": braid_result, |
| 65 | + } |
| 66 | + |
| 67 | + def _run_base_runtime(self, raw_input: Any) -> Dict[str, Any]: |
| 68 | + try: |
| 69 | + return self.base_runtime.run_once(raw_input) |
| 70 | + except TypeError: |
| 71 | + return self.base_runtime.run_once() |
| 72 | + |
| 73 | + def _extract_signal(self, runtime_payload: Any, raw_input: Any) -> Iterable[float]: |
| 74 | + signal = None |
| 75 | + if isinstance(runtime_payload, dict): |
| 76 | + signal = runtime_payload.get("signal") |
| 77 | + if signal is None: |
| 78 | + signal = runtime_payload.get("samples") |
| 79 | + if signal is None: |
| 80 | + signal = raw_input |
| 81 | + if signal is None: |
| 82 | + return [] |
| 83 | + if isinstance(signal, dict): |
| 84 | + signal = list(signal.values()) |
| 85 | + if isinstance(signal, (list, tuple)): |
| 86 | + return [self._ensure_float(value) for value in signal] |
| 87 | + if isinstance(signal, Sequence): |
| 88 | + return [self._ensure_float(value) for value in list(signal)] |
| 89 | + if hasattr(signal, "__iter__"): |
| 90 | + return [self._ensure_float(value) for value in signal] |
| 91 | + return [] |
| 92 | + |
| 93 | + def _ensure_float(self, value: Any) -> float: |
| 94 | + try: |
| 95 | + return float(value) |
| 96 | + except (TypeError, ValueError): |
| 97 | + return 0.0 |
| 98 | + |
| 99 | + def _make_prompt_from_flow(self, flow_payload: Dict[str, Any]) -> str: |
| 100 | + emotion = flow_payload.get("emotion") |
| 101 | + soul_invariant = flow_payload.get("soul_invariant") |
| 102 | + summary_parts: List[str] = [] |
| 103 | + if emotion is not None: |
| 104 | + summary_parts.append(f"emotion={emotion}") |
| 105 | + if soul_invariant is not None: |
| 106 | + summary_parts.append(f"soul_invariant={soul_invariant}") |
| 107 | + distribution = flow_payload.get("distribution") |
| 108 | + if distribution is not None: |
| 109 | + summary_parts.append(f"distribution={distribution}") |
| 110 | + if not summary_parts: |
| 111 | + summary_parts.append("no_flow_data") |
| 112 | + return " | ".join(summary_parts) |
| 113 | + |
| 114 | + def _make_context_from_flow(self, flow_payload: Dict[str, Any]) -> Dict[str, Any]: |
| 115 | + distribution = flow_payload.get("distribution") |
| 116 | + peak = 0.0 |
| 117 | + if isinstance(distribution, Iterable): |
| 118 | + peak = max((self._ensure_float(value) for value in distribution), default=0.0) |
| 119 | + return { |
| 120 | + "emotion": flow_payload.get("emotion"), |
| 121 | + "soul_invariant": flow_payload.get("soul_invariant"), |
| 122 | + "distribution_peak": peak, |
| 123 | + "coherence": self.braid_runtime.memory.coherence(), |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | +__all__ = ["BraidEnabledRuntime"] |
0 commit comments