qualia_core_db/inference/inference_agent/
validation.rs1use 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 pub fn validate_intent_frame(&self, frame: &AgentIntentFrame) -> WebizenVerdict {
16 Self::evaluate_intent_frame(self, frame)
17 }
18
19 pub(super) fn evaluate_intent_frame(
24 agent: &LocalLlmAgent,
25 frame: &AgentIntentFrame,
26 ) -> WebizenVerdict {
27 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 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 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 object: liability_weight | (0b001u64 << 60),
71 context: frame.principal_did_hash,
72 metadata: (now_ms & 0xFFFFFFFF)
74 | ((is_anti_human_rights as u64) << 32)
75 | ((is_discriminatory as u64) << 33),
76 parity: 0,
77 };
78
79 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 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 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}