airframe-observe 0.1.3

FSE-based inference observation layer for Airframe. Selector-first, single-pass capture of layer outputs, logits, and text during inference.
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
//! Built-in observers for the vault pipeline.
//!
//! Each observer is a dzero rule registered on specific InferenceFact keys.
//! When the forward pass emits a fact, all rules indexed on that fact's alpha key
//! fire simultaneously — zero additional extraction cost per shared selector.

use crate::facts::{bits_to_f32, InferenceFact, KEY_FINAL_LOGITS, KEY_LAYER_OUTPUT};
use dzero::{AlphaKey, FactStore};
use std::sync::{Arc, Mutex};

/// Captured oracle data from a single layer.
#[derive(Clone, Debug)]
pub struct OracleCapture {
    pub layer_idx: u32,
    pub position: u32,
    pub rms: f32,
    pub checksum: i64,
}

/// Captured logit data for candle cross-validation.
#[derive(Clone, Debug)]
pub struct LogitCapture {
    pub position: u32,
    pub rms: f32,
    pub checksum: i64,
}

/// VaultOracleObserver captures layer outputs and stores them for vault import.
///
/// Registered on KEY_LAYER_OUTPUT. Every LayerOutput fact broadcasts here.
pub struct VaultOracleObserver {
    pub captures: Arc<Mutex<Vec<OracleCapture>>>,
}

impl VaultOracleObserver {
    pub fn new() -> Self {
        Self {
            captures: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Returns the rule closure to register with dzero.
    /// The closure fires on every LayerOutput fact.
    pub fn rule(
        &self,
    ) -> impl Fn(&InferenceFact, &FactStore<InferenceFact>) -> Vec<InferenceFact> + Send + Sync
    {
        let captures = self.captures.clone();
        move |fact, _store| {
            if let InferenceFact::LayerOutput {
                layer_idx,
                position,
                rms_bits,
                checksum,
            } = fact
            {
                captures.lock().unwrap().push(OracleCapture {
                    layer_idx: *layer_idx,
                    position: *position,
                    rms: bits_to_f32(*rms_bits),
                    checksum: *checksum,
                });
                // Emit a consequent to trigger oracle row write
                vec![InferenceFact::WriteOracleRow {
                    layer_idx: *layer_idx,
                }]
            } else {
                vec![]
            }
        }
    }

    /// Alpha key this observer registers on.
    pub fn alpha_key() -> AlphaKey {
        AlphaKey(KEY_LAYER_OUTPUT)
    }

    /// Drain captured oracle data after saturation.
    pub fn drain(&self) -> Vec<OracleCapture> {
        self.captures.lock().unwrap().drain(..).collect()
    }
}

impl Default for VaultOracleObserver {
    fn default() -> Self {
        Self::new()
    }
}

/// CandleCompareObserver captures final logits for cross-validation.
///
/// Registered on KEY_FINAL_LOGITS. Fires once per forward pass.
/// The candle_probe binary generates the reference to compare against.
pub struct CandleCompareObserver {
    pub capture: Arc<Mutex<Option<LogitCapture>>>,
}

impl CandleCompareObserver {
    pub fn new() -> Self {
        Self {
            capture: Arc::new(Mutex::new(None)),
        }
    }

    /// Returns the rule closure to register with dzero.
    pub fn rule(
        &self,
    ) -> impl Fn(&InferenceFact, &FactStore<InferenceFact>) -> Vec<InferenceFact> + Send + Sync
    {
        let capture = self.capture.clone();
        move |fact, _store| {
            if let InferenceFact::FinalLogits {
                position,
                rms_bits,
                checksum,
            } = fact
            {
                *capture.lock().unwrap() = Some(LogitCapture {
                    position: *position,
                    rms: bits_to_f32(*rms_bits),
                    checksum: *checksum,
                });
                // Emit consequent to trigger candle comparison
                vec![InferenceFact::TriggerCandleCompare {
                    rms_bits: *rms_bits,
                    checksum: *checksum,
                }]
            } else {
                vec![]
            }
        }
    }

    /// Alpha key this observer registers on.
    pub fn alpha_key() -> AlphaKey {
        AlphaKey(KEY_FINAL_LOGITS)
    }

    /// Take the captured logit data.
    pub fn take(&self) -> Option<LogitCapture> {
        self.capture.lock().unwrap().take()
    }
}

impl Default for CandleCompareObserver {
    fn default() -> Self {
        Self::new()
    }
}

/// Sentinel `layer_idx` used in certification facts for the final-logits check
/// (the vault stores final logits as an oracle row with `layer_idx = -1`).
pub const FINAL_LOGITS_LAYER: u32 = u32::MAX;

/// One certification comparison result: a live observation vs its VaultOracle.
#[derive(Clone, Debug)]
pub struct CertResult {
    /// Layer index, or `FINAL_LOGITS_LAYER` for the final-logits comparison.
    pub layer_idx: u32,
    pub position: u32,
    pub observed_rms: f32,
    pub expected_rms: f32,
    /// Relative delta: `|observed - expected| / expected`.
    pub rel_delta: f32,
    pub checksum_match: bool,
    pub passed: bool,
    pub is_final_logits: bool,
}

/// Relative RMS delta `|observed - expected| / |expected|`.
/// Zero when both are zero; +inf when only expected is zero.
fn rel_delta(observed: f32, expected: f32) -> f32 {
    if expected == 0.0 {
        if observed == 0.0 {
            0.0
        } else {
            f32::INFINITY
        }
    } else {
        (observed - expected).abs() / expected.abs()
    }
}

/// CertificationObserver compares live observation facts against the
/// `VaultOracle` reference facts loaded from the golden vault (V1).
///
/// Two rules, sharing one results buffer:
/// - on KEY_LAYER_OUTPUT: each `LayerOutput` is matched to the `VaultOracle`
///   with the same `(layer_idx, position)` (layer_idx >= 0). PASS if the
///   relative RMS delta is below `layer_tolerance` (default 2.0).
/// - on KEY_FINAL_LOGITS: each `FinalLogits` is matched to the `VaultOracle`
///   final-logits row (`layer_idx == -1`, same position). PASS if the relative
///   RMS delta is below `logits_tolerance` (default 4.0). Emitted certification
///   facts use `FINAL_LOGITS_LAYER` as the layer index.
///
/// No matching oracle → no certification fact (that point is uncovered).
pub struct CertificationObserver {
    /// Max relative RMS delta for a layer-output PASS.
    pub layer_tolerance: f32,
    /// Max relative RMS delta for a final-logits PASS.
    pub logits_tolerance: f32,
    /// All comparison results, in fire order. Drained after saturation.
    pub results: Arc<Mutex<Vec<CertResult>>>,
}

impl CertificationObserver {
    /// Default relative-delta tolerance for a layer-output pass (per bead V2 /
    /// AGENTS.md: per-layer RMS ratio < 2.0 = OK).
    pub const DEFAULT_LAYER_TOLERANCE: f32 = 2.0;
    /// Default relative-delta tolerance for a final-logits pass.
    pub const DEFAULT_LOGITS_TOLERANCE: f32 = 4.0;

    pub fn new(layer_tolerance: f32, logits_tolerance: f32) -> Self {
        Self {
            layer_tolerance,
            logits_tolerance,
            results: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Construct with the standard V2 gate tolerances (2.0 / 4.0).
    pub fn with_defaults() -> Self {
        Self::new(
            Self::DEFAULT_LAYER_TOLERANCE,
            Self::DEFAULT_LOGITS_TOLERANCE,
        )
    }

    /// Rule for KEY_LAYER_OUTPUT: certify each LayerOutput against its oracle.
    pub fn layer_rule(
        &self,
    ) -> impl Fn(&InferenceFact, &FactStore<InferenceFact>) -> Vec<InferenceFact> + Send + Sync
    {
        let results = self.results.clone();
        let tolerance = self.layer_tolerance;
        move |fact, store| {
            let InferenceFact::LayerOutput {
                layer_idx,
                position,
                rms_bits,
                checksum,
            } = fact
            else {
                return vec![];
            };
            for f in store.facts.iter() {
                let InferenceFact::VaultOracle {
                    layer_idx: oracle_layer,
                    position: oracle_pos,
                    expected_rms_bits,
                    checksum: oracle_checksum,
                    ..
                } = f
                else {
                    continue;
                };
                if *oracle_layer != *layer_idx as i32 || *oracle_pos != *position {
                    continue;
                }
                return certify(
                    &results,
                    *layer_idx,
                    *position,
                    *rms_bits,
                    *checksum,
                    *expected_rms_bits,
                    *oracle_checksum,
                    tolerance,
                    false,
                );
            }
            vec![]
        }
    }

    /// Rule for KEY_FINAL_LOGITS: certify FinalLogits against the -1 oracle row.
    pub fn logits_rule(
        &self,
    ) -> impl Fn(&InferenceFact, &FactStore<InferenceFact>) -> Vec<InferenceFact> + Send + Sync
    {
        let results = self.results.clone();
        let tolerance = self.logits_tolerance;
        move |fact, store| {
            let InferenceFact::FinalLogits {
                position,
                rms_bits,
                checksum,
            } = fact
            else {
                return vec![];
            };
            for f in store.facts.iter() {
                let InferenceFact::VaultOracle {
                    layer_idx: oracle_layer,
                    position: oracle_pos,
                    expected_rms_bits,
                    checksum: oracle_checksum,
                    ..
                } = f
                else {
                    continue;
                };
                if *oracle_layer != -1 || *oracle_pos != *position {
                    continue;
                }
                return certify(
                    &results,
                    FINAL_LOGITS_LAYER,
                    *position,
                    *rms_bits,
                    *checksum,
                    *expected_rms_bits,
                    *oracle_checksum,
                    tolerance,
                    true,
                );
            }
            vec![]
        }
    }

    /// Alpha key for the layer-output rule.
    pub fn layer_alpha_key() -> AlphaKey {
        AlphaKey(KEY_LAYER_OUTPUT)
    }

    /// Alpha key for the final-logits rule.
    pub fn logits_alpha_key() -> AlphaKey {
        AlphaKey(KEY_FINAL_LOGITS)
    }

    /// Drain the accumulated certification results after saturation.
    pub fn drain(&self) -> Vec<CertResult> {
        self.results.lock().unwrap().drain(..).collect()
    }
}

/// Shared comparison: record a result and emit the Pass/Fail consequent.
#[allow(clippy::too_many_arguments)]
fn certify(
    results: &Arc<Mutex<Vec<CertResult>>>,
    layer_idx: u32,
    position: u32,
    observed_rms_bits: u32,
    observed_checksum: i64,
    expected_rms_bits: u32,
    oracle_checksum: i64,
    tolerance: f32,
    is_final_logits: bool,
) -> Vec<InferenceFact> {
    let observed_rms = bits_to_f32(observed_rms_bits);
    let expected_rms = bits_to_f32(expected_rms_bits);
    let delta = rel_delta(observed_rms, expected_rms);
    let checksum_match = observed_checksum == oracle_checksum;
    let passed = delta < tolerance;

    results.lock().unwrap().push(CertResult {
        layer_idx,
        position,
        observed_rms,
        expected_rms,
        rel_delta: delta,
        checksum_match,
        passed,
        is_final_logits,
    });

    if passed {
        vec![InferenceFact::CertificationPass {
            layer_idx,
            position,
            rms_delta_bits: delta.to_bits(),
        }]
    } else {
        vec![InferenceFact::CertificationFail {
            layer_idx,
            position,
            rms_delta_bits: delta.to_bits(),
            observed_rms_bits,
            expected_rms_bits,
            checksum_match,
        }]
    }
}

/// LayerStabilityObserver derives semantic facts about layer health.
///
/// Registered on KEY_LAYER_OUTPUT. Fires alongside VaultOracleObserver
/// (same alpha key, zero additional extraction cost — FSE broadcast).
pub struct LayerStabilityObserver;

impl LayerStabilityObserver {
    pub fn alpha_key() -> AlphaKey {
        AlphaKey(KEY_LAYER_OUTPUT) // shares the key — free broadcast
    }

    pub fn rule(
    ) -> impl Fn(&InferenceFact, &FactStore<InferenceFact>) -> Vec<InferenceFact> + Send + Sync
    {
        |fact, _store| {
            if let InferenceFact::LayerOutput {
                layer_idx,
                rms_bits,
                ..
            } = fact
            {
                let rms = bits_to_f32(*rms_bits);
                // Derive stability fact if RMS is in sane range
                if rms > 0.0 && rms < 1000.0 && !rms.is_nan() && !rms.is_infinite() {
                    vec![InferenceFact::LayerOutputStable {
                        layer_idx: *layer_idx,
                    }]
                } else {
                    vec![] // No stability fact — something is wrong
                }
            } else {
                vec![]
            }
        }
    }
}