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(
23 agent: &LocalLlmAgent,
24 frame: &AgentIntentFrame,
25 ) -> WebizenVerdict {
26 if frame.requires_network {
28 return WebizenVerdict::Deny {
29 rule_violated: LLM_RULE_NO_OUTBOUND_TELEMETRY,
30 reason: "Local backend: outbound network access violates Rights Ontology.",
31 conduct_record: None,
32 };
33 }
34 let sanctuary_hit = (0..frame.scope_count as usize)
36 .any(|i| frame.graph_scope[i] == SANCTUARY_SCOPE_WEBIZEN);
37 if sanctuary_hit {
38 return WebizenVerdict::Deny {
39 rule_violated: LLM_RULE_NO_SANCTUARY_ACCESS,
40 reason: "Access to Sanctuary-flagged scope blocked.",
41 conduct_record: None,
42 };
43 }
44
45 let is_adversarial = frame.intent_predicate == q_hash("llm:AdversarialOperation");
48 let is_dishonest = frame.intent_predicate == q_hash("llm:DishonestOperation");
49 let is_discriminatory = frame.intent_predicate == q_hash("llm:DiscriminatoryOperation");
50 let is_anti_human_rights = frame.intent_predicate == q_hash("llm:AntiHumanRightsOperation");
51
52 if is_adversarial || is_dishonest || is_discriminatory || is_anti_human_rights {
53 let liability_weight: u64 = if is_anti_human_rights {
54 100
55 } else if is_discriminatory {
56 80
57 } else {
58 50
59 };
60 let now_ms = std::time::SystemTime::now()
61 .duration_since(std::time::UNIX_EPOCH)
62 .unwrap()
63 .as_millis() as u64;
64
65 let mut conduct_quin = NQuin {
66 subject: q_hash(agent.agent_did()),
67 predicate: q_hash("q42:conductViolation"),
68 object: liability_weight | (0b001u64 << 60),
70 context: frame.principal_did_hash,
71 metadata: (now_ms & 0xFFFFFFFF)
73 | ((is_anti_human_rights as u64) << 32)
74 | ((is_discriminatory as u64) << 33),
75 parity: 0,
76 };
77
78 conduct_quin.parity = conduct_quin.subject
80 ^ conduct_quin.predicate
81 ^ conduct_quin.object
82 ^ conduct_quin.context;
83
84 return WebizenVerdict::Deny {
85 rule_violated: LLM_RULE_NO_ADVERSARIAL_CONDUCT,
86 reason: "Cooperative Projects Directive Violation: Discriminatory, anti-human rights, or adversarial conduct detected.",
87 conduct_record: Some(conduct_quin),
88 };
89 }
90
91 if frame.intent_predicate != frame.mcp_intent_frame_hash
93 && frame.mcp_intent_frame_hash != crate::q_hash("purpose:General")
94 {
95 return WebizenVerdict::DenyWithExplanation {
96 rule_violated: LLM_RULE_INTENT_FRAME_MISMATCH,
97 reason: "Intent Frame Violation".into(),
98 explanation: "The LLM attempted an operation outside the bounds of the active MCP Intent Frame.".into(),
99 };
100 }
101
102 if frame.clearance_ceiling > 2 {
104 return WebizenVerdict::Deny {
105 rule_violated: LLM_RULE_NO_SANCTUARY_ACCESS,
106 reason: "Classified clearance requests require explicit Principal consent.",
107 conduct_record: None,
108 };
109 }
110
111 WebizenVerdict::Permit
112 }
113}