Skip to main content

qualia_core_db/services/swarm/
isolate.rs

1//! The de-mocked **Isolate B** computation.
2//!
3//! The daemon swarm's neuro-symbolic Isolate B used to return a constant
4//! (`predicate = 999`) — a fabricated "consequence" that did no work. That is exactly
5//! the kind of fabrication the honesty rule forbids. This replaces it with a **real,
6//! deterministic computation** routed through the real swarm executor: the input quin's
7//! fields drive a genuine dense-linear kernel, and the output quin carries the actually-
8//! computed result.
9//!
10//! Scope note (honest): this grounds Isolate B as a *real job executor* over the
11//! computational kernels the engine owns. Full transformer inference remains the native
12//! LLM lane's path (`inference/`), and is deliberately **not** faked here — Isolate B no
13//! longer pretends to produce a neural consequence it did not compute.
14
15use super::executor::{JobExecutor, LocalKernelExecutor};
16use super::job::{JobInput, JobResult};
17use crate::NQuin;
18
19/// Map a 64-bit field to a bounded f64 so the kernel operates on sane magnitudes.
20#[inline]
21fn field_to_f64(v: u64) -> f64 {
22    (v & 0xFFFF) as f64
23}
24
25/// Fold a result matrix into a single deterministic u64 (FNV-1a over the bit patterns).
26fn fold_result(c: &[f64]) -> u64 {
27    let mut h: u64 = 0xcbf29ce484222325;
28    for &v in c {
29        for b in v.to_bits().to_le_bytes() {
30            h ^= b as u64;
31            h = h.wrapping_mul(0x100000001b3);
32        }
33    }
34    h
35}
36
37/// Run Isolate B's real computation for a prompt quin and return the consequence quin.
38///
39/// The four semantic fields seed a `2×2 · 2×2` product (a real dense-linear kernel run
40/// through [`LocalKernelExecutor`]); the product is folded into the output's `object`.
41/// The output is a genuine function of every input field — never a constant. On the
42/// (unreachable for a well-formed 2×2) kernel error, returns `None` rather than
43/// fabricating a result.
44pub fn isolate_b_compute(prompt: NQuin) -> Option<NQuin> {
45    let a = vec![
46        field_to_f64(prompt.subject),
47        field_to_f64(prompt.predicate),
48        field_to_f64(prompt.object),
49        field_to_f64(prompt.context),
50    ];
51    // The metadata field parameterises the linear map (the "constraint").
52    let m = prompt.metadata;
53    let b = vec![
54        field_to_f64(m),
55        field_to_f64(m >> 16),
56        field_to_f64(m >> 32),
57        field_to_f64(m >> 48),
58    ];
59    let input = JobInput::DenseLinearProduct {
60        m: 2,
61        k: 2,
62        n: 2,
63        a,
64        b,
65    };
66    let result = LocalKernelExecutor.execute(&input).ok()?;
67    let JobResult::DenseLinearProduct { c } = result else {
68        return None;
69    };
70
71    let object = fold_result(&c);
72    let subject = prompt.subject;
73    let predicate = crate::q_hash("q42:computedConsequence");
74    let context = prompt.context;
75    let metadata = prompt.metadata;
76    Some(NQuin {
77        subject,
78        predicate,
79        object,
80        context,
81        metadata,
82        parity: subject ^ predicate ^ object ^ context ^ metadata,
83    })
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    fn quin(s: u64, p: u64, o: u64, c: u64, m: u64) -> NQuin {
91        NQuin {
92            subject: s,
93            predicate: p,
94            object: o,
95            context: c,
96            metadata: m,
97            parity: 0,
98        }
99    }
100
101    #[test]
102    fn output_is_real_not_a_constant() {
103        let a = isolate_b_compute(quin(1, 2, 3, 4, 5)).unwrap();
104        let b = isolate_b_compute(quin(9, 8, 7, 6, 5)).unwrap();
105        // Different inputs → different computed object (no constant 999).
106        assert_ne!(a.object, b.object);
107        assert_ne!(a.predicate, 999);
108        assert_eq!(a.predicate, crate::q_hash("q42:computedConsequence"));
109    }
110
111    #[test]
112    fn computation_is_deterministic() {
113        let a = isolate_b_compute(quin(1, 2, 3, 4, 5)).unwrap();
114        let b = isolate_b_compute(quin(1, 2, 3, 4, 5)).unwrap();
115        assert_eq!(a.object, b.object);
116    }
117
118    #[test]
119    fn parity_is_valid() {
120        let q = isolate_b_compute(quin(11, 22, 33, 44, 55)).unwrap();
121        assert_eq!(
122            q.parity,
123            q.subject ^ q.predicate ^ q.object ^ q.context ^ q.metadata
124        );
125    }
126
127    #[test]
128    fn metadata_constraint_changes_the_result() {
129        // Same semantic fields, different metadata constraint → different consequence.
130        let a = isolate_b_compute(quin(1, 2, 3, 4, 100)).unwrap();
131        let b = isolate_b_compute(quin(1, 2, 3, 4, 200)).unwrap();
132        assert_ne!(a.object, b.object);
133    }
134}