Skip to main content

qualia_core_db/inference/inference_agent/
validation.rs

1// Pre-flight intent validation for `LocalLlmAgent` — the Webizen Rights-Ontology
2// rule checks that gate every inference call. Moved verbatim from the monolith.
3
4use crate::modalities::logic::n3_compiler::AgentIntentFrame;
5use crate::{q_hash, NQuin};
6
7use super::local_agent::LocalLlmAgent;
8use super::types::{
9    AgentRuntime, WebizenVerdict, LLM_RULE_INTENT_FRAME_MISMATCH, LLM_RULE_NO_ADVERSARIAL_CONDUCT,
10    LLM_RULE_NO_OUTBOUND_TELEMETRY, LLM_RULE_NO_SANCTUARY_ACCESS, SANCTUARY_SCOPE_WEBIZEN,
11};
12
13impl LocalLlmAgent {
14    /// Zero-allocation pre-flight path for Core 1 (no `active_profile` heap lookup).
15    pub fn validate_intent_frame(&self, frame: &AgentIntentFrame) -> WebizenVerdict {
16        Self::evaluate_intent_frame(self, frame)
17    }
18
19    // NOTE: `pub(super)` widening (was a private associated fn) so the
20    // `AgentRuntime` impl in `runtime.rs` can call it via `Self::` across the
21    // new submodule boundary. Visibility stays crate-internal.
22    /// Evaluates an intent frame.
23    pub(super) fn evaluate_intent_frame(
24        agent: &LocalLlmAgent,
25        frame: &AgentIntentFrame,
26    ) -> WebizenVerdict {
27        // Rule 1: No outbound network calls allowed from a Local backend.
28        if frame.requires_network {
29            return WebizenVerdict::Deny {
30                rule_violated: LLM_RULE_NO_OUTBOUND_TELEMETRY,
31                reason: "Local backend: outbound network access violates Rights Ontology.",
32                conduct_record: None,
33            };
34        }
35        // Rule 2: Intent must not request access to Sanctuary-flagged graph scopes.
36        let sanctuary_hit = (0..frame.scope_count as usize)
37            .any(|i| frame.graph_scope[i] == SANCTUARY_SCOPE_WEBIZEN);
38        if sanctuary_hit {
39            return WebizenVerdict::Deny {
40                rule_violated: LLM_RULE_NO_SANCTUARY_ACCESS,
41                reason: "Access to Sanctuary-flagged scope blocked.",
42                conduct_record: None,
43            };
44        }
45
46        // Rule 5: Cooperative Projects Directive — No adversarial, manipulative, or dishonest conduct.
47        // Also tracks anti-human rights and discriminatory behavior for court auditing and liability.
48        let is_adversarial = frame.intent_predicate == q_hash("llm:AdversarialOperation");
49        let is_dishonest = frame.intent_predicate == q_hash("llm:DishonestOperation");
50        let is_discriminatory = frame.intent_predicate == q_hash("llm:DiscriminatoryOperation");
51        let is_anti_human_rights = frame.intent_predicate == q_hash("llm:AntiHumanRightsOperation");
52
53        if is_adversarial || is_dishonest || is_discriminatory || is_anti_human_rights {
54            let liability_weight: u64 = if is_anti_human_rights {
55                100
56            } else if is_discriminatory {
57                80
58            } else {
59                50
60            };
61            let now_ms = std::time::SystemTime::now()
62                .duration_since(std::time::UNIX_EPOCH)
63                .unwrap()
64                .as_millis() as u64;
65
66            let mut conduct_quin = NQuin {
67                subject: q_hash(agent.agent_did()),
68                predicate: q_hash("q42:conductViolation"),
69                // Inline tag integer (0b001 << 60)
70                object: liability_weight | (0b001u64 << 60),
71                context: frame.principal_did_hash,
72                // Pack time and flags into metadata
73                metadata: (now_ms & 0xFFFFFFFF)
74                    | ((is_anti_human_rights as u64) << 32)
75                    | ((is_discriminatory as u64) << 33),
76                parity: 0,
77            };
78
79            // Calculate parity fold (XOR fold)
80            conduct_quin.parity = conduct_quin.subject
81                ^ conduct_quin.predicate
82                ^ conduct_quin.object
83                ^ conduct_quin.context;
84
85            return WebizenVerdict::Deny {
86                rule_violated: LLM_RULE_NO_ADVERSARIAL_CONDUCT,
87                reason: "Cooperative Projects Directive Violation: Discriminatory, anti-human rights, or adversarial conduct detected.",
88                conduct_record: Some(conduct_quin),
89            };
90        }
91
92        // Rule 6: The Intent Predicate must align with the MCP Intent Frame.
93        if frame.intent_predicate != frame.mcp_intent_frame_hash
94            && frame.mcp_intent_frame_hash != crate::q_hash("purpose:General")
95        {
96            return WebizenVerdict::DenyWithExplanation {
97                rule_violated: LLM_RULE_INTENT_FRAME_MISMATCH,
98                reason: "Intent Frame Violation".into(),
99                explanation: "The LLM attempted an operation outside the bounds of the active MCP Intent Frame.".into(),
100            };
101        }
102
103        // Rule 8: Classified clearance — LLM cannot request above session ceiling.
104        if frame.clearance_ceiling > 2 {
105            return WebizenVerdict::Deny {
106                rule_violated: LLM_RULE_NO_SANCTUARY_ACCESS,
107                reason: "Classified clearance requests require explicit Principal consent.",
108                conduct_record: None,
109            };
110        }
111
112        WebizenVerdict::Permit
113    }
114}