Skip to main content

qualia_core_db/modalities/logic/
n3logic.rs

1use crate::q_hash;
2use crate::NQuin;
3
4#[derive(Debug)]
5pub enum N3LogicError {
6    ParseError,
7    HardwareUnavailable(&'static str),
8}
9
10/// N3Logic is the foundational rule-based inference engine.
11/// It reads the raw triples/rules submitted in the Agent Intent
12/// and dynamically infers which specialized logic modalities must be invoked.
13pub fn infer_logic_bindings(quins: &[NQuin]) -> Result<Vec<&'static str>, N3LogicError> {
14    let mut bindings = Vec::new();
15
16    // We strictly enforce Epistemic Logic as the Sandbox Isolator
17    bindings.push("modality:epistemic");
18
19    // We strictly enforce Deontic Logic for human-rights alignment
20    bindings.push("modality:deontic");
21
22    let lock_predicate = q_hash("OP_INTENT_LOCK");
23    let action_payload = q_hash("q42:actionPayload");
24    let cost_predicate = q_hash("q42:burnsTokens");
25    let structural_refactor = q_hash("q42:structuralRefactor");
26
27    // Hardware acceleration and solver routing triggers
28    let uses_pga = q_hash("q42:usesPGA");
29    let quantum_opt = q_hash("q42:quantumOptimize");
30    let solve_calculus = q_hash("q42:solveCalculus");
31    let check_paraconsistent = q_hash("q42:paraconsistentCheck");
32    let spatial_region = q_hash("q42:spatialRegion");
33
34    // Specialized Libraries routing triggers
35    let chem_model = q_hash("q42:chemistryModeling");
36    let phys_sim = q_hash("q42:physicsSimulation");
37    let med_comp = q_hash("q42:medicalComputing");
38    let crypto_lib = q_hash("q42:cryptography");
39
40    for q in quins {
41        let opcode = (q.predicate & 0xFF) as u8;
42
43        // If the agent submits an Intent Lock or modifies the active payload, enforce Temporal Leases
44        if opcode == 0x23 || q.predicate == lock_predicate || q.predicate == action_payload {
45            if !bindings.contains(&"modality:temporal") {
46                bindings.push("modality:temporal");
47            }
48        }
49
50        // If the agent requests a structural refactor, lock the namespace
51        if q.predicate == structural_refactor {
52            if !bindings.contains(&"modality:epistemic:namespace_lock") {
53                bindings.push("modality:epistemic:namespace_lock");
54            }
55        }
56
57        // If the agent burns compute tokens, enforce Defeasible Logic for cost-overrun exceptions
58        if q.predicate == cost_predicate {
59            if !bindings.contains(&"modality:defeasible") {
60                bindings.push("modality:defeasible");
61            }
62        }
63
64        // Geometric Algebra / PGA routing
65        if q.predicate == uses_pga {
66            if !bindings.contains(&"modality:geometric_algebra") {
67                bindings.push("modality:geometric_algebra");
68            }
69        }
70
71        // Hardware acceleration and solver routing:
72        // When QPU hardware is offline, seamlessly route to the software fallback
73        // (classical Simulated Annealing QUBO optimization solver).
74        if q.predicate == quantum_opt {
75            if !bindings.contains(&"modality:solver:classical_qubo") {
76                bindings.push("modality:solver:classical_qubo");
77            }
78        }
79        if q.predicate == solve_calculus {
80            if !bindings.contains(&"modality:solver:calculus") {
81                bindings.push("modality:solver:calculus");
82            }
83        }
84
85        // Advanced Modalities
86        if q.predicate == check_paraconsistent {
87            if !bindings.contains(&"modality:paraconsistent") {
88                bindings.push("modality:paraconsistent");
89            }
90        }
91        if q.predicate == spatial_region {
92            if !bindings.contains(&"modality:spatio_temporal") {
93                bindings.push("modality:spatio_temporal");
94            }
95        }
96
97        // Specialized Libraries
98        if q.predicate == chem_model {
99            if !bindings.contains(&"modality:specialized:chemistry") {
100                bindings.push("modality:specialized:chemistry");
101            }
102        }
103        if q.predicate == phys_sim {
104            if !bindings.contains(&"modality:specialized:physics") {
105                bindings.push("modality:specialized:physics");
106            }
107        }
108        if q.predicate == med_comp {
109            if !bindings.contains(&"modality:specialized:medical") {
110                bindings.push("modality:specialized:medical");
111            }
112        }
113        if q.predicate == crypto_lib {
114            if !bindings.contains(&"modality:specialized:crypto") {
115                bindings.push("modality:specialized:crypto");
116            }
117        }
118    }
119
120    Ok(bindings)
121}
122
123/// Enforces strict hardware gating: rejects the intent if physical QPU hardware
124/// is unavailable and physical-only execution was requested.
125pub fn infer_logic_bindings_strict_hardware(
126    quins: &[NQuin],
127) -> Result<Vec<&'static str>, N3LogicError> {
128    let quantum_opt = q_hash("q42:quantumOptimize");
129    for q in quins {
130        if q.predicate == quantum_opt {
131            return Err(N3LogicError::HardwareUnavailable(
132                "QPU offline. Intent requested physical q42:quantumOptimize without fallback.",
133            ));
134        }
135    }
136    infer_logic_bindings(quins)
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142
143    #[test]
144    fn infer_logic_bindings_default_sandboxes() {
145        let quins = [NQuin::default()];
146        let bindings = infer_logic_bindings(&quins).unwrap();
147        assert!(bindings.contains(&"modality:epistemic"));
148        assert!(bindings.contains(&"modality:deontic"));
149    }
150
151    #[test]
152    fn quantum_opt_routes_to_software_fallback() {
153        let mut quin = NQuin::default();
154        quin.predicate = q_hash("q42:quantumOptimize");
155        let bindings = infer_logic_bindings(&[quin]).unwrap();
156        assert!(bindings.contains(&"modality:solver:classical_qubo"));
157    }
158
159    #[test]
160    fn strict_hardware_rejects_offline_qpu() {
161        let mut quin = NQuin::default();
162        quin.predicate = q_hash("q42:quantumOptimize");
163        let res = infer_logic_bindings_strict_hardware(&[quin]);
164        assert!(matches!(res, Err(N3LogicError::HardwareUnavailable(_))));
165    }
166}