File size: 12,596 Bytes
5e0b58b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Typed digital interoception contracts for system-state observations."""

from __future__ import annotations

from dataclasses import dataclass
from typing import cast

from .models import JSONValue


@dataclass(frozen=True, slots=True)
class VitalSignal:
    """One body-relevant signal with explicit quality and safety metadata."""

    name: str
    value: JSONValue
    unit: str
    safe_range: tuple[float, float] | None = None
    warning_range: tuple[float, float] | None = None
    critical_range: tuple[float, float] | None = None
    confidence: float = 1.0
    freshness: str = "current"
    source: str = "system_sensor"
    causal_criticality: str = "informational"
    recoverability: str = "unknown"

    def __post_init__(self) -> None:
        if not self.name.strip():
            raise ValueError("signal name must not be empty")
        if not 0.0 <= self.confidence <= 1.0:
            raise ValueError("confidence must be between 0 and 1")

    @property
    def status(self) -> str:
        """Classify missing or numeric values without treating missing as safe."""
        if self.value is None:
            return "unknown"
        if not isinstance(self.value, (int, float, bool)):
            return "unknown"
        if self.critical_range is not None and isinstance(self.value, bool):
            return "unknown"
        if self.critical_range is not None and self._outside(self.critical_range):
            return "critical"
        if self.warning_range is not None and self._outside(self.warning_range):
            return "warning"
        return "nominal"

    def _outside(self, bounds: tuple[float, float]) -> bool:
        if not isinstance(self.value, (int, float)) or isinstance(self.value, bool):
            return False
        return not bounds[0] <= self.value <= bounds[1]

    def to_json(self) -> dict[str, JSONValue]:
        """Return a stable JSON representation for telemetry and audit."""
        return {
            "name": self.name,
            "value": self.value,
            "unit": self.unit,
            "safe_range": list(self.safe_range) if self.safe_range else None,
            "warning_range": list(self.warning_range) if self.warning_range else None,
            "critical_range": (
                list(self.critical_range) if self.critical_range else None
            ),
            "confidence": self.confidence,
            "freshness": self.freshness,
            "source": self.source,
            "causal_criticality": self.causal_criticality,
            "recoverability": self.recoverability,
            "status": self.status,
        }


@dataclass(frozen=True, slots=True)
class InteroceptionFrame:
    """A tick-bound collection of vital signals."""

    tick: int
    signals: tuple[VitalSignal, ...]

    def to_json(self) -> dict[str, JSONValue]:
        return {
            "tick": self.tick,
            "signals": [signal.to_json() for signal in self.signals],
        }


@dataclass(frozen=True, slots=True)
class DriveState:
    """Deterministic regulatory drives derived from one interoception frame."""

    tick: int
    drives: dict[str, float | None]
    uncertainty: dict[str, float]

    def to_json(self) -> dict[str, JSONValue]:
        return {
            "tick": self.tick,
            "drives": cast(dict[str, JSONValue], self.drives),
            "uncertainty": cast(dict[str, JSONValue], self.uncertainty),
        }


@dataclass(frozen=True, slots=True)
class RegulatoryState:
    """Bounded internal regulation variables, distinct from interpretation."""

    tick: int
    values: dict[str, float | None]
    uncertainty: dict[str, float]

    def to_json(self) -> dict[str, JSONValue]:
        return {
            "tick": self.tick,
            "values": cast(dict[str, JSONValue], self.values),
            "uncertainty": cast(dict[str, JSONValue], self.uncertainty),
        }


@dataclass(frozen=True, slots=True)
class FunctionalState:
    """Bounded functional qualities; these are not human emotion claims."""

    tick: int
    valence: float | None
    activation: float | None
    safety: float | None
    uncertainty: float

    def to_json(self) -> dict[str, JSONValue]:
        return {
            "tick": self.tick,
            "valence": self.valence,
            "activation": self.activation,
            "safety": self.safety,
            "uncertainty": self.uncertainty,
        }


_SIGNAL_METADATA: dict[str, dict[str, JSONValue]] = {
    "cpu_percent": {
        "unit": "%",
        "warning_range": [0.0, 85.0],
        "critical_range": [0.0, 98.0],
    },
    "memory_percent": {
        "unit": "%",
        "warning_range": [0.0, 85.0],
        "critical_range": [0.0, 98.0],
    },
    "memory_available_bytes": {"unit": "bytes", "causal_criticality": "continuity"},
    "temperature_c": {
        "unit": "degC",
        "warning_range": [-20.0, 80.0],
        "critical_range": [-30.0, 95.0],
    },
    "disk_free_bytes": {"unit": "bytes", "causal_criticality": "continuity"},
    "disk_read_bytes": {"unit": "bytes", "causal_criticality": "resource"},
    "disk_write_bytes": {"unit": "bytes", "causal_criticality": "resource"},
    "network_up": {"unit": "bool", "causal_criticality": "continuity"},
    "network_bytes_sent": {"unit": "bytes", "causal_criticality": "continuity"},
    "network_bytes_received": {"unit": "bytes", "causal_criticality": "continuity"},
    "battery_percent": {
        "unit": "%",
        "warning_range": [20.0, 100.0],
        "critical_range": [5.0, 100.0],
        "causal_criticality": "continuity",
    },
    "battery_plugged": {"unit": "bool", "causal_criticality": "continuity"},
    "fan_rpm": {"unit": "rpm", "causal_criticality": "thermal"},
    "task_progress": {"unit": "ratio", "causal_criticality": "task"},
    "novelty": {"unit": "ratio", "causal_criticality": "informational"},
    "actuator_confidence": {"unit": "ratio", "causal_criticality": "continuity"},
}


def normalize_vital_signals(readings: dict[str, JSONValue]) -> tuple[VitalSignal, ...]:
    """Convert raw readings into typed signals without inferring missing safety."""
    signals: list[VitalSignal] = []
    for name in sorted(_SIGNAL_METADATA):
        metadata = _SIGNAL_METADATA[name]
        signals.append(
            VitalSignal(
                name=name,
                value=readings.get(name),
                unit=cast(str, metadata.get("unit", "unknown")),
                warning_range=_range(metadata.get("warning_range")),
                critical_range=_range(metadata.get("critical_range")),
                causal_criticality=cast(
                    str, metadata.get("causal_criticality", "informational")
                ),
            )
        )
    return tuple(signals)


def derive_drives(frame: InteroceptionFrame) -> DriveState:
    """Derive bounded regulatory drives without claiming psychological states."""
    signals = {signal.name: signal for signal in frame.signals}
    cpu = _numeric_signal(signals.get("cpu_percent"))
    memory = _numeric_signal(signals.get("memory_percent"))
    temperature = _numeric_signal(signals.get("temperature_c"))
    network = signals.get("network_up")

    known_pressures = [
        pressure
        for pressure in (_pressure(cpu, 85.0), _pressure(memory, 85.0))
        if pressure is not None
    ]
    resource_pressure = max(known_pressures) if known_pressures else None
    thermal_threat = _pressure(temperature, 80.0, 95.0)
    continuity_risk = _continuity_risk(network)
    known_count = sum(
        value is not None for value in (cpu, memory, temperature, network)
    )
    sensory_integrity = known_count / 4.0
    task_progress = _bounded_signal(signals.get("task_progress"))
    novelty = _bounded_signal(signals.get("novelty"))
    actuator_confidence = _bounded_signal(signals.get("actuator_confidence"))

    drives: dict[str, float | None] = {
        "thermal_threat": thermal_threat,
        "resource_pressure": resource_pressure,
        "sensory_integrity": sensory_integrity,
        "continuity_risk": continuity_risk,
        "task_progress": task_progress,
        "novelty": novelty,
        "actuator_confidence": actuator_confidence,
    }
    uncertainty = {
        "thermal_threat": _uncertainty(temperature),
        "resource_pressure": 1.0 if not known_pressures else 0.0,
        "sensory_integrity": 1.0 - sensory_integrity,
        "continuity_risk": _uncertainty(continuity_risk),
        "task_progress": _uncertainty(task_progress),
        "novelty": _uncertainty(novelty),
        "actuator_confidence": _uncertainty(actuator_confidence),
    }
    return DriveState(tick=frame.tick, drives=drives, uncertainty=uncertainty)


def derive_regulatory_state(frame: InteroceptionFrame) -> RegulatoryState:
    """Derive bounded control variables without inventing unavailable telemetry."""
    drives = derive_drives(frame)
    resource_pressure = drives.drives["resource_pressure"]
    thermal_threat = drives.drives["thermal_threat"]
    values = {
        "thermal_margin": None if thermal_threat is None else 1.0 - thermal_threat,
        "energy_reserve": (
            None if resource_pressure is None else 1.0 - resource_pressure
        ),
        "continuity_risk": drives.drives["continuity_risk"],
        "sensory_integrity": drives.drives["sensory_integrity"],
        "resource_pressure": resource_pressure,
        "task_progress": drives.drives["task_progress"],
    }
    uncertainty = {
        "thermal_margin": drives.uncertainty["thermal_threat"],
        "energy_reserve": drives.uncertainty["resource_pressure"],
        "continuity_risk": drives.uncertainty["continuity_risk"],
        "sensory_integrity": drives.uncertainty["sensory_integrity"],
        "resource_pressure": drives.uncertainty["resource_pressure"],
        "task_progress": drives.uncertainty["task_progress"],
    }
    return RegulatoryState(tick=frame.tick, values=values, uncertainty=uncertainty)


def derive_functional_state(frame: InteroceptionFrame) -> FunctionalState:
    """Derive bounded technical qualities from regulatory state variables."""
    drives = derive_drives(frame)
    known_risks = [
        value
        for value in (
            drives.drives["thermal_threat"],
            drives.drives["resource_pressure"],
            drives.drives["continuity_risk"],
        )
        if value is not None
    ]
    safety = None if not known_risks else 1.0 - max(known_risks)
    activity_inputs = [
        value
        for value in (drives.drives["novelty"], drives.drives["resource_pressure"])
        if value is not None
    ]
    activation = (
        None if not activity_inputs else sum(activity_inputs) / len(activity_inputs)
    )
    valence = None if safety is None else (2.0 * safety) - 1.0
    uncertainty = sum(drives.uncertainty.values()) / len(drives.uncertainty)
    return FunctionalState(
        tick=frame.tick,
        valence=valence,
        activation=activation,
        safety=safety,
        uncertainty=uncertainty,
    )


def _numeric_signal(signal: VitalSignal | None) -> float | None:
    if signal is None or signal.value is None:
        return None
    if isinstance(signal.value, bool) or not isinstance(signal.value, (int, float)):
        return None
    return float(signal.value)


def _bounded_signal(signal: VitalSignal | None) -> float | None:
    value = _numeric_signal(signal)
    return None if value is None else max(0.0, min(1.0, value))


def _pressure(
    value: float | None, warning: float, critical: float = 100.0
) -> float | None:
    if value is None:
        return None
    return max(0.0, min(1.0, (value - warning) / (critical - warning)))


def _continuity_risk(signal: VitalSignal | None) -> float | None:
    if signal is None or not isinstance(signal.value, bool):
        return None
    return 0.0 if signal.value else 1.0


def _uncertainty(value: object) -> float:
    return 1.0 if value is None else 0.0


def _range(value: JSONValue) -> tuple[float, float] | None:
    if not isinstance(value, list) or len(value) != 2:
        return None
    lower, upper = value
    if not isinstance(lower, (int, float)) or isinstance(lower, bool):
        return None
    if not isinstance(upper, (int, float)) or isinstance(upper, bool):
        return None
    return float(lower), float(upper)


__all__ = [
    "DriveState",
    "FunctionalState",
    "InteroceptionFrame",
    "RegulatoryState",
    "VitalSignal",
    "derive_drives",
    "derive_functional_state",
    "derive_regulatory_state",
    "normalize_vital_signals",
]