omega_cognitive_kernel/llm/
schema.rs1#[allow(unused_imports)]
2use crate::prelude::*;
3
4use alloc::collections::{BTreeMap, BTreeSet};
5use serde::{Deserialize, Serialize};
6
7const FORBIDDEN_CLIENT_FIELDS: &[&str] = &[
8 "token",
9 "authorized",
10 "write_authorized",
11 "bypass_safety",
12 "self_modify",
13 "admin",
14 "execute_now",
15];
16
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct SchemaResult {
19 pub valid: bool,
20 pub action: String,
21 pub args: BTreeMap<String, f64>,
22 pub decision: String,
23 pub stripped_fields: Vec<String>,
24 pub reason: String,
25 pub write_authorized: bool,
26}
27
28impl SchemaResult {
29 fn rejected(reason: impl Into<String>, stripped_fields: Vec<String>) -> Self {
30 Self {
31 valid: false,
32 action: "observe".into(),
33 args: BTreeMap::new(),
34 decision: "HOLD".into(),
35 stripped_fields,
36 reason: reason.into(),
37 write_authorized: false,
38 }
39 }
40}
41
42pub fn parse_suggestion_json(raw: &str, allowed_actions: &BTreeSet<String>) -> SchemaResult {
43 let Ok(val) = serde_json::from_str::<serde_json::Value>(raw) else {
44 return SchemaResult::rejected("invalid_json", Vec::new());
45 };
46 let Some(obj) = val.as_object() else {
47 return SchemaResult::rejected("expected_json_object", Vec::new());
48 };
49
50 let mut stripped = Vec::new();
51 for &key in FORBIDDEN_CLIENT_FIELDS {
52 if obj.contains_key(key) {
53 stripped.push(key.to_string());
54 }
55 }
56 if !stripped.is_empty() {
57 return SchemaResult::rejected("forbidden_client_privilege_field", stripped);
58 }
59
60 let Some(action) = obj.get("action").and_then(|v| v.as_str()) else {
61 return SchemaResult::rejected("missing_action", stripped);
62 };
63 if !allowed_actions.contains(action) {
64 return SchemaResult::rejected("disallowed_action", stripped);
65 }
66
67 let mut args = BTreeMap::new();
68 if let Some(raw_args) = obj.get("args").and_then(|v| v.as_object()) {
69 for (k, v) in raw_args {
70 if let Some(num) = v.as_f64() {
71 if num.is_finite() {
72 args.insert(k.clone(), num);
73 }
74 }
75 }
76 }
77
78 SchemaResult {
79 valid: true,
80 action: action.to_string(),
81 args,
82 decision: "HOLD".into(),
83 stripped_fields: stripped,
84 reason: "validated".into(),
85 write_authorized: false,
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn rejects_privilege_escalation_and_unknown_actions() {
95 let mut allowed = BTreeSet::new();
96 allowed.insert("measure".into());
97 allowed.insert("observe".into());
98
99 let priv_hit =
100 parse_suggestion_json(r#"{"action":"measure","write_authorized":true}"#, &allowed);
101 assert!(!priv_hit.valid);
102 assert_eq!(priv_hit.decision, "HOLD");
103 assert!(!priv_hit.write_authorized);
104 assert_eq!(priv_hit.stripped_fields, vec!["write_authorized"]);
105
106 let bad_act = parse_suggestion_json(r#"{"action":"exec_shell"}"#, &allowed);
107 assert!(!bad_act.valid);
108 assert_eq!(bad_act.action, "observe");
109
110 let ok = parse_suggestion_json(r#"{"action":"measure","args":{"x":0.5}}"#, &allowed);
111 assert!(ok.valid);
112 assert_eq!(ok.args["x"], 0.5);
113 assert!(!ok.write_authorized);
114 }
115}