File size: 14,052 Bytes
c8fbdf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Longitudinal Drift Detector

============================



Reads the accumulated LivingMemoryKernelV2 store and surfaces four categories

of drift that matter for RC+ξ continuity:



    1. Epsilon trend       — is epistemic tension rising, falling, or stable?

    2. Perspective lock    — is one perspective dominating at >LOCK_THRESHOLD?

    3. Recurring tensions  — which unresolved_tensions appear in 3+ cocoons?

    4. Hook accumulation   — how many follow-up hooks are piling up unresolved?



Designed for periodic reads (e.g., session start, /api/drift endpoint), not

for every inference call.  All computation is O(n) over the memory store.



Usage:

    detector = DriftDetector()

    report   = detector.detect(engine.memory_kernel)

    print(report.summary())

"""

from __future__ import annotations

import time
from collections import Counter
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Tuple

LOCK_THRESHOLD     = 0.60   # one perspective > 60% usage → perspective_lock
RECURRING_MIN      = 3      # tension must appear in ≥3 cocoons to be "recurring"
EPSILON_WINDOW     = 10     # number of recent cocoons for windowed epsilon
STABLE_BAND        = 0.05   # |slope| < this → "stable" trend
CONSECUTIVE_RISING = 3      # N consecutive "rising" windows → calibration warning


# ── Band encoding ────────────────────────────────────────────────────────────

_BAND_TO_FLOAT: Dict[str, float] = {
    "low": 0.2,
    "medium": 0.5,
    "high": 0.75,
    "max": 0.95,
}


def _band_value(band: str) -> float:
    return _BAND_TO_FLOAT.get(band.lower().strip(), 0.5)


# ── Linear regression (no numpy dependency) ──────────────────────────────────

def _slope(values: List[float]) -> float:
    """Return the least-squares slope of a list of scalars indexed 0..n-1."""
    n = len(values)
    if n < 2:
        return 0.0
    x_mean = (n - 1) / 2.0
    y_mean = sum(values) / n
    num = sum((i - x_mean) * (v - y_mean) for i, v in enumerate(values))
    den = sum((i - x_mean) ** 2 for i in range(n))
    return num / den if den else 0.0


# ── Report ────────────────────────────────────────────────────────────────────

@dataclass
class DriftReport:
    """

    Snapshot of longitudinal drift in the memory store.



    All fields are read-only aggregates — nothing is written back to the kernel.

    """
    generated_at: float = field(default_factory=time.time)

    # Epsilon
    epsilon_trend: str = "unknown"          # "rising" | "falling" | "stable" | "unknown"
    epsilon_slope: float = 0.0              # raw slope over last EPSILON_WINDOW cocoons
    epsilon_mean: float = 0.0              # mean epsilon across all cocoons
    epsilon_distribution: Dict[str, int] = field(default_factory=dict)

    # Perspective
    dominant_perspective: str = ""
    perspective_usage: Dict[str, int] = field(default_factory=dict)
    perspective_lock: bool = False          # True if one perspective > LOCK_THRESHOLD
    perspective_lock_ratio: float = 0.0

    # Tensions
    recurring_tensions: List[Tuple[str, int]] = field(default_factory=list)
    # [(tension_label, cocoon_count), ...] sorted by count desc

    # Hooks
    open_hook_count: int = 0
    hooks_sample: List[str] = field(default_factory=list)  # up to 5 example hooks

    # psi_r time-series (last EPSILON_WINDOW cocoons, chronological)
    psi_r_history: List[float] = field(default_factory=list)

    # Meta
    total_cocoons: int = 0
    observation_window: int = EPSILON_WINDOW

    def summary(self) -> str:
        """Human-readable one-paragraph summary."""
        lines = [
            f"Drift report over {self.total_cocoons} cocoons "
            f"(window={self.observation_window}):",
        ]

        lines.append(
            f"  ε trend: {self.epsilon_trend} "
            f"(slope={self.epsilon_slope:+.3f}, mean={self.epsilon_mean:.2f})"
        )

        if self.perspective_lock:
            lines.append(
                f"  ⚠ Perspective lock: '{self.dominant_perspective}' "
                f"at {self.perspective_lock_ratio:.0%} usage"
            )
        else:
            lines.append(
                f"  Perspective balance: dominant='{self.dominant_perspective}' "
                f"({self.perspective_lock_ratio:.0%})"
            )

        if self.recurring_tensions:
            top = self.recurring_tensions[:3]
            tension_str = ", ".join(f"'{t}' ×{n}" for t, n in top)
            lines.append(f"  Recurring tensions: {tension_str}")
        else:
            lines.append("  No recurring tensions detected.")

        lines.append(f"  Open hooks: {self.open_hook_count}")
        return "\n".join(lines)

    def to_dict(self) -> Dict[str, Any]:
        return {
            "generated_at": self.generated_at,
            "epsilon_trend": self.epsilon_trend,
            "epsilon_slope": round(self.epsilon_slope, 4),
            "epsilon_mean": round(self.epsilon_mean, 4),
            "epsilon_distribution": self.epsilon_distribution,
            "dominant_perspective": self.dominant_perspective,
            "perspective_usage": self.perspective_usage,
            "perspective_lock": self.perspective_lock,
            "perspective_lock_ratio": round(self.perspective_lock_ratio, 4),
            "recurring_tensions": [
                {"tension": t, "count": n} for t, n in self.recurring_tensions
            ],
            "open_hook_count": self.open_hook_count,
            "hooks_sample": self.hooks_sample,
            "psi_r_history": [round(v, 4) for v in self.psi_r_history],
            "total_cocoons": self.total_cocoons,
            "observation_window": self.observation_window,
        }


# ── Intervention ─────────────────────────────────────────────────────────────

@dataclass
class InterventionPlan:
    """

    Action recommendations derived from a DriftReport.



    Produced by DriftDetector.should_intervene() — the caller (forge_engine)

    decides whether to act on each flag.

    """
    inject_perspective: Optional[str] = None  # name of underused perspective to force-inject
    calibration_warning: bool = False          # epsilon rising ≥ CONSECUTIVE_RISING windows
    reasons: List[str] = field(default_factory=list)

    @property
    def active(self) -> bool:
        return bool(self.inject_perspective or self.calibration_warning)


# ── Detector ─────────────────────────────────────────────────────────────────

class DriftDetector:
    """

    Stateless analyser — call detect() as often as needed.



    Accepts any object that implements:

        .memories        → list of MemoryCocoonV2

        .continuity_profile() → dict (used for perspective_usage, epsilon_distribution,

                                       follow-up hooks, unresolved_tensions)

        .recall_with_hooks()  → list of MemoryCocoonV2 with open hooks

        .recall_recent(n)     → list of MemoryCocoonV2, newest-first (for psi_r_history)

    Falls back gracefully if any of those attributes are absent.

    """

    def should_intervene(

        self,

        report: DriftReport,

        trend_history: Optional[List[str]] = None,

    ) -> InterventionPlan:
        """

        Convert a DriftReport into concrete intervention recommendations.



        trend_history — caller-maintained list of recent epsilon_trend strings

        (e.g. ["rising","rising","rising"]); used for calibration warning.

        """
        plan = InterventionPlan()

        if report.perspective_lock and report.perspective_usage:
            # Find the least-used perspective that isn't the dominant one
            least = min(
                report.perspective_usage,
                key=lambda p: report.perspective_usage[p],
            )
            if least != report.dominant_perspective:
                plan.inject_perspective = least
                plan.reasons.append(
                    f"Perspective lock: '{report.dominant_perspective}' at "
                    f"{report.perspective_lock_ratio:.0%}; injecting '{least}'"
                )

        if trend_history and len(trend_history) >= CONSECUTIVE_RISING:
            window = trend_history[-CONSECUTIVE_RISING:]
            if all(t == "rising" for t in window):
                plan.calibration_warning = True
                plan.reasons.append(
                    f"Epsilon rising for {CONSECUTIVE_RISING} consecutive sessions; "
                    "query domain may exceed confidence calibration"
                )

        return plan

    def detect(self, kernel: Any) -> DriftReport:
        report = DriftReport()

        if kernel is None:
            return report

        # ── Pull raw data ─────────────────────────────────────────────────────
        memories = getattr(kernel, 'memories', [])
        report.total_cocoons = len(memories)

        # continuity_profile gives us the pre-aggregated view
        try:
            profile = kernel.continuity_profile()
        except Exception:
            profile = {}

        # ── Epsilon trend ─────────────────────────────────────────────────────
        epsilon_dist = profile.get("epsilon_distribution", {})
        report.epsilon_distribution = epsilon_dist

        # Windowed slope from recent cocoons (ordered by storage position)
        recent = memories[-EPSILON_WINDOW:] if len(memories) >= 2 else memories
        eps_values: List[float] = []
        for m in recent:
            band = getattr(m, 'epsilon_band', None)
            if band:
                eps_values.append(_band_value(band))

        if len(eps_values) >= 2:
            s = _slope(eps_values)
            report.epsilon_slope = s
            report.epsilon_mean = sum(eps_values) / len(eps_values)
            if s > STABLE_BAND:
                report.epsilon_trend = "rising"
            elif s < -STABLE_BAND:
                report.epsilon_trend = "falling"
            else:
                report.epsilon_trend = "stable"
        elif eps_values:
            report.epsilon_mean = eps_values[0]
            report.epsilon_trend = "stable"

        # ── psi_r history (chronological, newest-last) ───────────────────────
        try:
            recent_for_psi = getattr(kernel, 'recall_recent', None)
            if callable(recent_for_psi):
                psi_cocoons = list(reversed(recent_for_psi(EPSILON_WINDOW)))
            else:
                psi_cocoons = memories[-EPSILON_WINDOW:]
        except Exception:
            psi_cocoons = memories[-EPSILON_WINDOW:]

        for m in psi_cocoons:
            psi_val = getattr(m, 'psi_r', None)
            if isinstance(psi_val, (int, float)):
                report.psi_r_history.append(float(psi_val))

        # ── Perspective dominance ─────────────────────────────────────────────
        perspective_usage: Dict[str, int] = profile.get("perspective_usage", {})
        report.perspective_usage = perspective_usage
        report.dominant_perspective = profile.get("dominant_perspective", "")

        total_perspective_uses = sum(perspective_usage.values())
        if total_perspective_uses > 0 and report.dominant_perspective:
            ratio = perspective_usage.get(report.dominant_perspective, 0) / total_perspective_uses
            report.perspective_lock_ratio = ratio
            report.perspective_lock = ratio > LOCK_THRESHOLD

        # ── Recurring tensions ────────────────────────────────────────────────
        tension_counter: Counter = Counter()
        for m in memories:
            tensions = getattr(m, 'unresolved_tensions', [])
            for t in tensions:
                t_clean = t.strip().lower()
                if t_clean:
                    tension_counter[t_clean] += 1

        report.recurring_tensions = [
            (t, n) for t, n in tension_counter.most_common()
            if n >= RECURRING_MIN
        ]

        # ── Open hooks ────────────────────────────────────────────────────────
        try:
            hooked = kernel.recall_with_hooks(limit=50)
        except Exception:
            hooked = [m for m in memories if getattr(m, 'follow_up_hooks', [])]

        all_hooks: List[str] = []
        for m in hooked:
            all_hooks.extend(getattr(m, 'follow_up_hooks', []))

        report.open_hook_count = len(all_hooks)
        seen: set = set()
        for h in all_hooks:
            if h not in seen:
                seen.add(h)
                report.hooks_sample.append(h)
                if len(report.hooks_sample) >= 5:
                    break

        return report