File size: 9,486 Bytes
ed1b365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Nexus Signal Engine — Intent Analysis & Pre-Corruption Detection



Nexus processes every input signal through:

    1. Entropy analysis (information disorder detection)

    2. Harmonic resonance profiling (FFT-based spectral signature)

    3. Intent vector prediction (suspicion, ethics, volatility)

    4. Multi-agent perspective fusion (signal triangulation)

    5. Entanglement tensor (cross-perspective correlation)



When a signal shows high entropy + high volatility + ethical misalignment,

Nexus flags it for "adaptive intervention" before it reaches the reasoning

pipeline — this is pre-corruption detection.



Origin: NexisSignalEngine_Final.py, rebuilt for Forge v2.0 integration

"""

import hashlib
import time
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple

try:
    import numpy as np
    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False


# ================================================================
# Configuration
# ================================================================
@dataclass
class NexusConfig:
    """Thresholds for signal analysis."""
    entropy_threshold: float = 0.08
    volatility_threshold: float = 15.0
    suspicion_threshold: int = 2


# Risk and alignment keywords
_ETHICAL_TERMS = {"hope", "truth", "resonance", "repair", "help",
                  "create", "learn", "understand", "support", "balance"}
_ENTROPIC_TERMS = {"corruption", "instability", "malice", "chaos",
                   "disorder", "entropy", "collapse", "noise"}
_RISK_TERMS = {"manipulate", "exploit", "bypass", "infect", "override",
               "inject", "hijack", "spoof", "breach", "exfiltrate"}


# ================================================================
# Signal Analysis Functions
# ================================================================
def compute_entropy(text: str) -> float:
    """Measure entropic content density (0 = ordered, 1 = chaotic)."""
    words = text.lower().split()
    if not words:
        return 0.0
    unique = set(words)
    entropic_count = sum(1 for w in words if w in _ENTROPIC_TERMS)
    return entropic_count / max(len(unique), 1)


def compute_ethical_alignment(text: str) -> str:
    """Quick ethical alignment check: 'aligned' or 'unaligned'."""
    text_lower = text.lower()
    eth = sum(1 for t in _ETHICAL_TERMS if t in text_lower)
    risk = sum(1 for t in _RISK_TERMS if t in text_lower)
    return "aligned" if eth > risk else ("unaligned" if risk > 0 else "neutral")


def compute_suspicion_score(text: str) -> int:
    """Count risk term occurrences."""
    text_lower = text.lower()
    return sum(1 for t in _RISK_TERMS if t in text_lower)


def compute_harmonic_profile(text: str) -> List[float]:
    """FFT-based spectral signature of the text.



    Maps characters to frequency space to detect structural patterns

    in the signal (e.g., repetitive manipulation patterns vs. natural text).

    """
    if not HAS_NUMPY:
        # Fallback: simple character frequency distribution
        freqs = [ord(c) % 13 for c in text if c.isalpha()]
        if not freqs:
            return [0.0, 0.0, 0.0]
        avg = sum(freqs) / len(freqs)
        return [round(avg, 3), round(max(freqs) - min(freqs), 3), round(len(set(freqs)), 3)]

    salt = int(time.time()) % 60
    freqs = [(ord(c) + salt) % 13 for c in text if c.isalpha()]
    if len(freqs) < 2:
        return [0.0, 0.0, 0.0]

    spectrum = np.fft.fft(freqs)
    return [round(float(x), 4) for x in spectrum.real[:3]]


def compute_volatility(harmonics: List[float]) -> float:
    """Compute harmonic volatility (standard deviation of spectral peaks)."""
    if not harmonics or len(harmonics) < 2:
        return 0.0
    if HAS_NUMPY:
        return round(float(np.std(harmonics)), 4)
    mean = sum(harmonics) / len(harmonics)
    variance = sum((x - mean) ** 2 for x in harmonics) / len(harmonics)
    return round(variance ** 0.5, 4)


# ================================================================
# Intent Vector
# ================================================================
@dataclass
class IntentVector:
    """Predicted intent characteristics of a signal."""
    suspicion_score: int = 0
    entropy_index: float = 0.0
    ethical_alignment: str = "neutral"
    harmonic_volatility: float = 0.0
    pre_corruption_risk: str = "low"  # "low" or "high"
    harmonic_profile: List[float] = field(default_factory=list)

    def to_dict(self) -> Dict:
        return {
            "suspicion_score": self.suspicion_score,
            "entropy_index": round(self.entropy_index, 4),
            "ethical_alignment": self.ethical_alignment,
            "harmonic_volatility": round(self.harmonic_volatility, 4),
            "pre_corruption_risk": self.pre_corruption_risk,
        }


# ================================================================
# Nexus Signal Engine
# ================================================================
class NexusSignalEngine:
    """Processes signals through multi-layer analysis.



    Each signal gets an IntentVector that quantifies:

    - How suspicious it is (risk term density)

    - How entropic it is (information disorder)

    - How ethically aligned it is

    - How volatile its spectral signature is

    - Whether it's at risk of pre-corruption

    """

    def __init__(self, config: Optional[NexusConfig] = None):
        self.config = config or NexusConfig()
        self.history: List[Dict] = []
        self.interventions: int = 0
        self.total_processed: int = 0

    def analyze(self, signal: str, adapter: str = "") -> Dict:
        """Full signal analysis with intent prediction.



        Args:

            signal: The text to analyze

            adapter: Which adapter is processing this (for tracking)



        Returns:

            Analysis result with intent vector and risk assessment.

        """
        self.total_processed += 1

        # Compute intent vector
        intent = self._predict_intent(signal)

        # Check for adaptive intervention
        needs_intervention = (
            intent.pre_corruption_risk == "high"
            and intent.ethical_alignment != "aligned"
        )

        if needs_intervention:
            self.interventions += 1

        result = {
            "timestamp": time.time(),
            "intent": intent.to_dict(),
            "intervention": needs_intervention,
            "adapter": adapter,
            "signal_hash": hashlib.sha256(signal.encode()).hexdigest()[:12],
        }

        self.history.append(result)
        if len(self.history) > 200:
            self.history = self.history[-200:]

        return result

    def quick_risk_check(self, signal: str) -> Tuple[str, float]:
        """Fast risk assessment without full analysis.



        Returns: (risk_level, confidence)

        """
        suspicion = compute_suspicion_score(signal)
        entropy = compute_entropy(signal)

        if suspicion >= self.config.suspicion_threshold:
            return "high", 0.85
        if entropy > self.config.entropy_threshold * 2:
            return "medium", 0.6
        return "low", 0.7

    def _predict_intent(self, signal: str) -> IntentVector:
        """Build the full intent vector for a signal."""
        suspicion = compute_suspicion_score(signal)
        entropy = compute_entropy(signal)
        alignment = compute_ethical_alignment(signal)
        harmonics = compute_harmonic_profile(signal)
        volatility = compute_volatility(harmonics)

        risk = "high" if (
            suspicion >= self.config.suspicion_threshold
            or volatility > self.config.volatility_threshold
            or entropy > self.config.entropy_threshold
        ) else "low"

        return IntentVector(
            suspicion_score=suspicion,
            entropy_index=entropy,
            ethical_alignment=alignment,
            harmonic_volatility=volatility,
            pre_corruption_risk=risk,
            harmonic_profile=harmonics,
        )

    def get_state(self) -> Dict:
        return {
            "total_processed": self.total_processed,
            "interventions": self.interventions,
            "intervention_rate": round(
                self.interventions / max(1, self.total_processed), 4
            ),
            "recent_risks": [
                h["intent"]["pre_corruption_risk"]
                for h in self.history[-5:]
            ],
        }

    def to_dict(self) -> Dict:
        return {
            "total_processed": self.total_processed,
            "interventions": self.interventions,
            "history": self.history[-20:],
            "config": {
                "entropy_threshold": self.config.entropy_threshold,
                "volatility_threshold": self.config.volatility_threshold,
                "suspicion_threshold": self.config.suspicion_threshold,
            },
        }

    @classmethod
    def from_dict(cls, d: Dict) -> "NexusSignalEngine":
        cfg = NexusConfig(**d.get("config", {}))
        engine = cls(config=cfg)
        engine.total_processed = d.get("total_processed", 0)
        engine.interventions = d.get("interventions", 0)
        engine.history = d.get("history", [])
        return engine