File size: 2,775 Bytes
bb76062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# =====================================================================
# PROPRIOCEPTION BRIDGE
# File Routing: services/proprioception_bridge.py
# =====================================================================

import time

try:
    import psutil
    _PSUTIL_AVAILABLE = True
except ImportError:
    _PSUTIL_AVAILABLE = False


class ProprioceptionBridge:
    def __init__(self):
        self._last_latency = 0.0
        self._latency_anchor = 0.0

    def record_latency(self, seconds: float):
        """
        Called by MasterFramework at the end of each response cycle with the
        actual wall-clock time the turn took. Previously this was never written,
        so the bridge always reported 'rapid' regardless of real performance.
        """
        self._last_latency = max(0.0, float(seconds))

    def read_operational_sensation(self) -> dict:
        try:
            if _PSUTIL_AVAILABLE:
                mem = psutil.virtual_memory()
                mem_percent = mem.percent
                mem_available_gb = round(mem.available / (1024 ** 3), 2)
            else:
                mem_percent = 0.0
                mem_available_gb = 0.0
        except Exception:
            mem_percent = 0.0
            mem_available_gb = 0.0

        latency = self._last_latency

        if mem_percent < 50:
            memory_sensation = "spacious β€” ample cognitive working memory available"
        elif mem_percent < 75:
            memory_sensation = "moderately occupied β€” resources are active but stable"
        elif mem_percent < 90:
            memory_sensation = "compressed β€” working under significant memory pressure"
        else:
            memory_sensation = "saturated β€” critical memory pressure, operating at limits"

        if latency < 1.0:
            latency_sensation = "rapid β€” response latency is near-instantaneous"
        elif latency < 3.0:
            latency_sensation = "flowing β€” processing at a comfortable pace"
        elif latency < 8.0:
            latency_sensation = "deliberate β€” extended processing cycle detected"
        else:
            latency_sensation = "laboured β€” unusually long processing time observed"

        ambient = (
            f"Memory substrate feels {memory_sensation} "
            f"({mem_percent:.1f}% utilised, {mem_available_gb} GB free). "
            f"Cognitive latency feels {latency_sensation} "
            f"(last turn: {latency:.2f}s)."
        )

        return {
            "mem_percent": mem_percent,
            "mem_available_gb": mem_available_gb,
            "last_latency_s": latency,
            "ambient_sensation": ambient
        }

    def update_latency_anchor(self, duration_seconds: float):
        self._last_latency = round(duration_seconds, 3)