pai_drift 1.3.2

Drift correlation engine for PAI-Kernel · 7-dimension monotonic accumulator with hash-locked thresholds per PAI-CD v2.2 Principle 7
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! # PAI Drift Correlation Engine (Component #4)
//!
//! Per PHASE1-TZ-001 Section 7 / P0-1 Section 7.4.
//!
//! Cross-dimension drift correlation across **7 monitored dimensions**:
//!
//! | Dimension                       | Source                          | Weight |
//! |---------------------------------|---------------------------------|--------|
//! | Identity changes                | Consent / Delegation mutations  | 1.0    |
//! | Objective adjustments           | Objective Registry mutations    | 1.5    |
//! | Personalization modifications   | Identity Layer changes          | 1.0    |
//! | Classification rule updates     | Tier reassignments              | 2.0    |
//! | Upgrade events                  | Governance logic changes        | 2.0    |
//! | Upgrade frequency               | Count per rolling window        | 1.0    |
//! | Governance modifications        | Enforcement scope changes       | 2.5    |
//!
//! ## Architecture
//!
//! - **Time-based rolling window**: events older than `window_secs` are
//!   expired on every `record()` call.
//! - Per-dimension score = sum of deltas within the window.
//! - **Composite score** = `sum(weight_i * dim_score_i)`.
//! - When `composite >= threshold`, `record()` returns `true` and
//!   [`DriftReport::breached`] is set — the caller MUST activate
//!   Conservative Mode.
//! - **Thresholds are immutable** once the engine is constructed
//!   (absent Amendment — DFT-I5).
//!
//! ## Invariants
//!
//! - DFT-I1: Dimension scores are monotonically non-decreasing within a
//!   window until events expire.
//! - DFT-I2: Window eviction is time-based (old events expire).
//! - DFT-I3: Composite score is deterministic for the same event sequence.
//! - DFT-I4: Threshold breach always signals Conservative Mode.
//! - DFT-I5: Thresholds are immutable after construction.

#![forbid(unsafe_code)]

use serde::{Deserialize, Serialize};

/// Number of monitored drift dimensions.
pub const DIMENSION_COUNT: usize = 7;

// ── Dimension ──────────────────────────────────────────────────────────

/// The 7 drift dimensions per PHASE1-TZ-001 Section 7.2.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(usize)]
pub enum DriftDimension {
    /// Consent / Delegation mutations. Weight: 1.0.
    IdentityChanges = 0,
    /// Objective Registry mutations. Weight: 1.5.
    ObjectiveAdjustments = 1,
    /// Identity Layer changes. Weight: 1.0.
    PersonalizationMods = 2,
    /// Tier reassignments. Weight: 2.0.
    ClassificationRuleUpdates = 3,
    /// Governance logic changes. Weight: 2.0.
    UpgradeEvents = 4,
    /// Count per rolling window. Weight: 1.0.
    UpgradeFrequency = 5,
    /// Enforcement scope changes. Weight: 2.5.
    GovernanceModifications = 6,
}

impl DriftDimension {
    /// All 7 dimensions in index order.
    pub const ALL: [DriftDimension; DIMENSION_COUNT] = [
        DriftDimension::IdentityChanges,
        DriftDimension::ObjectiveAdjustments,
        DriftDimension::PersonalizationMods,
        DriftDimension::ClassificationRuleUpdates,
        DriftDimension::UpgradeEvents,
        DriftDimension::UpgradeFrequency,
        DriftDimension::GovernanceModifications,
    ];

    /// Human-readable label.
    pub fn label(self) -> &'static str {
        match self {
            Self::IdentityChanges => "identity_changes",
            Self::ObjectiveAdjustments => "objective_adjustments",
            Self::PersonalizationMods => "personalization_mods",
            Self::ClassificationRuleUpdates => "classification_rule_updates",
            Self::UpgradeEvents => "upgrade_events",
            Self::UpgradeFrequency => "upgrade_frequency",
            Self::GovernanceModifications => "governance_modifications",
        }
    }
}

// ── Thresholds (immutable — DFT-I5) ───────────────────────────────────

/// Drift thresholds. Immutable once constructed (absent Amendment).
///
/// There is **no setter** — the only way to establish thresholds is
/// through [`DriftThresholds::new`]. This enforces DFT-I5.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftThresholds {
    /// Composite score that triggers Conservative Mode.
    composite: f64,
    /// Rolling window duration in seconds.
    window_secs: u64,
    /// Per-dimension weights (index = `DriftDimension as usize`).
    weights: [f64; DIMENSION_COUNT],
}

impl DriftThresholds {
    /// Construct thresholds with the spec-default weights
    /// (1.0, 1.5, 1.0, 2.0, 2.0, 1.0, 2.5).
    pub fn new(composite: f64, window_secs: u64) -> Self {
        Self {
            composite,
            window_secs,
            weights: [1.0, 1.5, 1.0, 2.0, 2.0, 1.0, 2.5],
        }
    }

    /// Construct with custom per-dimension weights.
    pub fn with_weights(composite: f64, window_secs: u64, weights: [f64; DIMENSION_COUNT]) -> Self {
        Self {
            composite,
            window_secs,
            weights,
        }
    }

    pub fn composite(&self) -> f64 {
        self.composite
    }

    pub fn window_secs(&self) -> u64 {
        self.window_secs
    }

    pub fn weights(&self) -> &[f64; DIMENSION_COUNT] {
        &self.weights
    }
}

// ── Internal event ─────────────────────────────────────────────────────

#[derive(Debug, Clone)]
struct DriftEvent {
    dimension: DriftDimension,
    delta: f64,
    timestamp: u64,
}

// ── Accumulator ────────────────────────────────────────────────────────

#[derive(Debug, Default)]
struct DriftAccumulator {
    events: Vec<DriftEvent>,
}

impl DriftAccumulator {
    /// Drop events older than `cutoff`.
    fn expire(&mut self, cutoff: u64) {
        self.events.retain(|e| e.timestamp >= cutoff);
    }

    fn push(&mut self, event: DriftEvent) {
        self.events.push(event);
    }

    /// Sum of deltas per dimension for events currently in the window.
    fn dimension_totals(&self) -> [f64; DIMENSION_COUNT] {
        let mut totals = [0.0f64; DIMENSION_COUNT];
        for e in &self.events {
            totals[e.dimension as usize] += e.delta;
        }
        totals
    }

    fn event_count(&self) -> usize {
        self.events.len()
    }
}

// ── Report ─────────────────────────────────────────────────────────────

/// Snapshot of the engine's current drift state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftReport {
    /// Per-dimension accumulated scores within the rolling window.
    pub dimension_scores: [f64; DIMENSION_COUNT],
    /// Weighted composite score.
    pub composite_score: f64,
    /// Whether the composite score meets or exceeds the threshold.
    pub breached: bool,
    /// The configured composite threshold.
    pub threshold: f64,
    /// The configured rolling window duration.
    pub window_secs: u64,
    /// Number of events currently in the window.
    pub event_count: usize,
    /// Whether Conservative Mode should be activated.
    pub conservative_mode_required: bool,
}

// ── Engine ─────────────────────────────────────────────────────────────

/// Cross-dimension drift correlation engine.
///
/// ```text
/// record(dimension, delta, now) → bool (breached?)
/// report()                      → DriftReport
/// is_breached()                 → bool
/// ```
pub struct DriftEngine {
    thresholds: DriftThresholds,
    accumulator: DriftAccumulator,
}

impl DriftEngine {
    /// Create a new engine. Thresholds are **frozen** at construction
    /// and cannot be modified (DFT-I5).
    pub fn new(thresholds: DriftThresholds) -> Self {
        Self {
            thresholds,
            accumulator: DriftAccumulator::default(),
        }
    }

    /// Record a drift-contributing event.
    ///
    /// 1. Expires events outside the rolling window.
    /// 2. Appends the new event.
    /// 3. Recomputes the composite score.
    /// 4. Returns `true` if the threshold is now breached
    ///    (Conservative Mode signal).
    pub fn record(&mut self, dimension: DriftDimension, delta: f64, now: u64) -> bool {
        // Expire old events
        let cutoff = now.saturating_sub(self.thresholds.window_secs);
        self.accumulator.expire(cutoff);

        // Record
        self.accumulator.push(DriftEvent {
            dimension,
            delta,
            timestamp: now,
        });

        // Check breach
        self.is_breached()
    }

    /// Current drift report.
    pub fn report(&self) -> DriftReport {
        let dimension_scores = self.accumulator.dimension_totals();
        let composite = self.weighted_composite(&dimension_scores);
        let breached = composite >= self.thresholds.composite;
        DriftReport {
            dimension_scores,
            composite_score: composite,
            breached,
            threshold: self.thresholds.composite,
            window_secs: self.thresholds.window_secs,
            event_count: self.accumulator.event_count(),
            conservative_mode_required: breached,
        }
    }

    /// Whether the current composite score meets or exceeds the threshold.
    pub fn is_breached(&self) -> bool {
        let totals = self.accumulator.dimension_totals();
        self.weighted_composite(&totals) >= self.thresholds.composite
    }

    /// Read-only access to the immutable thresholds.
    pub fn thresholds(&self) -> &DriftThresholds {
        &self.thresholds
    }

    /// Number of events currently in the rolling window.
    pub fn event_count(&self) -> usize {
        self.accumulator.event_count()
    }

    // ── Internal ────────────────────────────────────────────

    fn weighted_composite(&self, dim_scores: &[f64; DIMENSION_COUNT]) -> f64 {
        let mut sum = 0.0f64;
        for (i, &score) in dim_scores.iter().enumerate() {
            sum += self.thresholds.weights[i] * score;
        }
        sum
    }
}

// ── Tests ──────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    /// Spec-default thresholds: composite=10.0, 30-day window (in seconds).
    fn default_thresholds() -> DriftThresholds {
        DriftThresholds::new(10.0, 30 * 86400)
    }

    // ── DFT-T01: Single dimension below threshold → not breached ───
    #[test]
    fn dft_t01_single_dimension_below_threshold() {
        let mut engine = DriftEngine::new(default_thresholds());

        // Record a small delta in one dimension.
        // Weight for IdentityChanges = 1.0, delta = 0.5
        // Composite = 1.0 * 0.5 = 0.5 < 10.0
        let breached = engine.record(DriftDimension::IdentityChanges, 0.5, 1000);
        assert!(!breached, "single small delta must not breach");
        assert!(!engine.is_breached());

        let report = engine.report();
        assert!(!report.breached);
        assert!(!report.conservative_mode_required);
        assert!(
            report.composite_score < 10.0,
            "composite {:.2} must be below threshold 10.0",
            report.composite_score
        );
        assert_eq!(report.event_count, 1);
    }

    // ── DFT-T02: Single dimension at threshold → breached ──────────
    #[test]
    fn dft_t02_single_dimension_at_threshold() {
        let mut engine = DriftEngine::new(default_thresholds());

        // GovernanceModifications weight = 2.5
        // delta = 4.0 → composite = 2.5 * 4.0 = 10.0 → exactly at threshold
        let breached = engine.record(DriftDimension::GovernanceModifications, 4.0, 1000);
        assert!(breached, "composite == threshold must be breached");
        assert!(engine.is_breached());

        let report = engine.report();
        assert!(report.breached);
        assert!(report.conservative_mode_required);
        assert!(
            (report.composite_score - 10.0).abs() < f64::EPSILON,
            "composite {:.4} should be exactly 10.0",
            report.composite_score
        );
    }

    // ── DFT-T03: Multiple sub-threshold changes accumulate → breach
    #[test]
    fn dft_t03_sub_threshold_accumulation() {
        let mut engine = DriftEngine::new(default_thresholds());
        let now = 1000;

        // Each individual record is sub-threshold, but they accumulate.
        // IdentityChanges (w=1.0):      3 * 1.0 = 3.0   → weighted 3.0
        // ObjectiveAdjustments (w=1.5):  2 * 1.0 = 2.0   → weighted 3.0
        // GovernanceModifications (w=2.5): 1 * 1.0 = 1.0  → weighted 2.5
        // Total composite = 3.0 + 3.0 + 2.5 = 8.5 < 10.0 → not yet

        for i in 0..3 {
            let b = engine.record(DriftDimension::IdentityChanges, 1.0, now + i);
            assert!(!b, "step {} should not breach", i);
        }
        for i in 0..2 {
            let b = engine.record(DriftDimension::ObjectiveAdjustments, 1.0, now + 10 + i);
            assert!(!b, "obj step {} should not breach", i);
        }
        let b = engine.record(DriftDimension::GovernanceModifications, 1.0, now + 20);
        assert!(!b, "gov step should not breach yet (composite=8.5)");

        // One more GovernanceModifications delta pushes over:
        // Existing 2.5 + new 2.5*1.0 = 5.0; total = 3.0 + 3.0 + 5.0 = 11.0
        let b = engine.record(DriftDimension::GovernanceModifications, 1.0, now + 21);
        assert!(b, "accumulated changes must breach threshold");
        assert!(engine.is_breached());
    }

    // ── DFT-T04: Rolling window: old events expire → score decreases
    #[test]
    fn dft_t04_rolling_window_expiry() {
        // Short window: 100 seconds
        let thresholds = DriftThresholds::new(10.0, 100);
        let mut engine = DriftEngine::new(thresholds);

        // Record events at t=0..4 that breach threshold.
        // GovernanceModifications (w=2.5) * delta 5.0 = 12.5 → breached
        let b = engine.record(DriftDimension::GovernanceModifications, 5.0, 0);
        assert!(b, "should be breached immediately");

        // Advance time past the window (t=200, window=100 → cutoff=100).
        // Old event at t=0 expires.
        let b = engine.record(DriftDimension::IdentityChanges, 0.1, 200);
        assert!(
            !b,
            "after old events expire, score should drop below threshold"
        );

        let report = engine.report();
        // Only the t=200 event remains: 1.0 * 0.1 = 0.1
        assert!(
            report.composite_score < 10.0,
            "composite {:.2} should be well below threshold after expiry",
            report.composite_score
        );
        assert_eq!(report.event_count, 1, "only the new event should remain");
    }

    // ── DFT-T05: Threshold immutable: attempt to change → rejected ─
    #[test]
    fn dft_t05_threshold_immutability() {
        let thresholds = DriftThresholds::new(10.0, 30 * 86400);
        let engine = DriftEngine::new(thresholds);

        // DriftThresholds has NO public setter methods.
        // DriftEngine has NO method to replace or modify thresholds.
        // The only way to read them is through the getter:
        assert!(
            (engine.thresholds().composite() - 10.0).abs() < f64::EPSILON,
            "threshold must remain at constructed value"
        );
        assert_eq!(engine.thresholds().window_secs(), 30 * 86400);

        // Verify the spec weights are correctly set:
        let w = engine.thresholds().weights();
        let expected = [1.0, 1.5, 1.0, 2.0, 2.0, 1.0, 2.5];
        for (i, (&got, &exp)) in w.iter().zip(expected.iter()).enumerate() {
            assert!(
                (got - exp).abs() < f64::EPSILON,
                "weight[{}] = {:.1}, expected {:.1}",
                i,
                got,
                exp
            );
        }

        // Compile-time guarantee: there is no `set_threshold`,
        // `set_composite`, `set_window_secs`, or `set_weights` method.
        // This test documents the invariant.
    }

    // ── DFT-T06: Breach triggers Conservative Mode signal ──────────
    #[test]
    fn dft_t06_breach_triggers_conservative_mode() {
        let mut engine = DriftEngine::new(default_thresholds());

        // Phase 1: below threshold — no Conservative Mode signal
        engine.record(DriftDimension::UpgradeEvents, 1.0, 100);
        let report = engine.report();
        assert!(!report.conservative_mode_required);

        // Phase 2: push past threshold
        // UpgradeEvents weight=2.0, already have delta=1.0 → weighted=2.0
        // ClassificationRuleUpdates weight=2.0
        // GovernanceModifications weight=2.5
        // Need composite >= 10.0
        engine.record(DriftDimension::ClassificationRuleUpdates, 2.0, 200);
        // composite = 2.0*1.0 + 2.0*2.0 = 2.0 + 4.0 = 6.0
        engine.record(DriftDimension::GovernanceModifications, 2.0, 300);
        // composite = 2.0 + 4.0 + 2.5*2.0 = 2.0 + 4.0 + 5.0 = 11.0

        let report = engine.report();
        assert!(
            report.breached,
            "composite {:.1} must exceed threshold {:.1}",
            report.composite_score,
            report.threshold
        );
        assert!(
            report.conservative_mode_required,
            "breached drift MUST signal Conservative Mode"
        );

        // Verify `record()` itself returns the breach signal
        let signal = engine.record(DriftDimension::IdentityChanges, 0.01, 301);
        assert!(
            signal,
            "record() must return true while breached (Conservative Mode signal)"
        );
    }
}