omega_cognitive_kernel/
config_toml.rs1pub use crate::config::KernelConfig;
4use crate::error::{OmegaError, Result};
5use std::path::Path;
6use toml;
7
8impl KernelConfig {
9 pub fn from_toml(s: &str) -> Result<Self> {
11 toml::from_str(s).map_err(|e| OmegaError::Config(format!("parse TOML: {e}")))
12 }
13
14 pub fn load(path: impl AsRef<Path>) -> Result<Self> {
16 let path = path.as_ref();
17 let raw = std::fs::read_to_string(path)
18 .map_err(|e| OmegaError::Config(format!("read {}: {e}", path.display())))?;
19 Self::from_toml(&raw)
20 }
21
22 pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
24 let path = path.as_ref();
25 let body = toml::to_string_pretty(self)
26 .map_err(|e| OmegaError::Config(format!("serialize TOML: {e}")))?;
27 std::fs::write(path, body)
28 .map_err(|e| OmegaError::Config(format!("write {}: {e}", path.display())))?;
29 Ok(())
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn roundtrip_defaults() {
39 let c = KernelConfig::default();
40 let s = toml::to_string_pretty(&c).unwrap();
41 let c2 = KernelConfig::from_toml(&s).unwrap();
42 assert!((c2.error_threshold - 0.30).abs() < 1e-9);
43 assert_eq!(c2.min_steps_between_patches, 3);
44 assert!((c2.max_risk - 0.65).abs() < 1e-9);
45 assert_eq!(c2.intervention.friction_threshold, 2);
46 assert!(!c2.self_modify_enabled);
47 assert!(c2.dump_trajectory);
48 assert!(!c2.privacy.redact_user_content);
49 assert!(c2.l2.enabled);
50 assert_eq!(c2.l2.commit_cooldown_steps, 5);
51 assert_eq!(c2.l2.dayan_relaxation, 0.35);
52 assert_eq!(c2.bandit.min_dwell_steps, 3);
53 assert!((c2.bandit.switch_margin - 0.05).abs() < 1e-9);
54 }
55
56 #[test]
57 fn parse_partial() {
58 let s = r#"
59max_risk = 0.4
60error_threshold = 0.25
61autosave = false
62self_modify_enabled = true
63"#;
64 let c = KernelConfig::from_toml(s).unwrap();
65 assert!((c.max_risk - 0.4).abs() < 1e-9);
66 assert!((c.error_threshold - 0.25).abs() < 1e-9);
67 assert!(!c.autosave);
68 assert!(c.auto_checkpoint_on_patch);
69 assert_eq!(
70 c.intervention.mode,
71 crate::config::InterventionMode::Exploration
72 );
73 assert!(c.self_modify_enabled);
74 assert!(!c.privacy.redact_user_content);
75 }
76
77 #[test]
78 fn parse_privacy_redaction_table() {
79 let config = KernelConfig::from_toml(
80 r#"
81[privacy]
82redact_user_content = true
83"#,
84 )
85 .unwrap();
86 assert!(config.privacy.redact_user_content);
87 }
88
89 #[test]
90 fn parse_l2_table_and_preserve_bandit() {
91 let c = KernelConfig::from_toml(
92 r#"
93[bandit]
94min_dwell_steps = 4
95switch_margin = 0.08
96rollback_penalty = 0.4
97switch_cost = 0.01
98[l2]
99enabled = true
100commit_threshold = 0.65
101commit_cooldown_steps = 4
102dayan_relaxation = 0.4
103meta_cache_hit_threshold = 0.8
104"#,
105 )
106 .unwrap();
107 assert_eq!(c.bandit.min_dwell_steps, 4);
108 assert_eq!(c.bandit.switch_margin, 0.08);
109 assert!(c.l2.enabled);
110 assert_eq!(c.l2.commit_threshold, 0.65);
111 assert_eq!(c.l2.commit_cooldown_steps, 4);
112 assert_eq!(c.l2.dayan_relaxation, 0.4);
113 assert_eq!(c.l2.meta_cache_hit_threshold, 0.8);
114 }
115
116 #[test]
117 fn parse_intervention_table() {
118 let s = r#"
119[intervention]
120mode = "strict"
121max_level = "refuse_to_decide"
122friction_threshold = 5
123"#;
124 let c = KernelConfig::from_toml(s).unwrap();
125 assert_eq!(c.intervention.mode, crate::config::InterventionMode::Strict);
126 assert_eq!(
127 c.intervention.max_level,
128 crate::config::InterventionLevel::RefuseToDecide
129 );
130 assert_eq!(c.intervention.friction_threshold, 5);
131 }
132
133 #[test]
134 fn intervention_exploration_allows_after_threshold() {
135 let s = r#"
136[intervention]
137mode = "exploration"
138max_level = "suggest_alternative"
139friction_threshold = 2
140"#;
141 let c = KernelConfig::from_toml(s).unwrap();
142 assert_eq!(
143 c.intervention.mode,
144 crate::config::InterventionMode::Exploration
145 );
146 assert_eq!(
147 c.intervention.max_level,
148 crate::config::InterventionLevel::SuggestAlternative
149 );
150 assert_eq!(c.intervention.friction_threshold, 2);
151 }
152
153 #[test]
154 fn intervention_strict_blocks_alternative() {
155 let s = r#"
156[intervention]
157mode = "strict"
158max_level = "in_frame"
159friction_threshold = 1
160"#;
161 let c = KernelConfig::from_toml(s).unwrap();
162 assert_eq!(c.intervention.mode, crate::config::InterventionMode::Strict);
163 assert_eq!(
164 c.intervention.max_level,
165 crate::config::InterventionLevel::InFrame
166 );
167 assert_eq!(c.intervention.friction_threshold, 1);
168 }
169
170 #[test]
171 fn roundtrip_preserves_intervention() {
172 let mut c = KernelConfig::default();
173 c.intervention.mode = crate::config::InterventionMode::Strict;
174 c.intervention.max_level = crate::config::InterventionLevel::RefuseToDecide;
175 c.intervention.friction_threshold = 3;
176 let s = toml::to_string_pretty(&c).unwrap();
177 let c2 = KernelConfig::from_toml(&s).unwrap();
178 assert_eq!(
179 c2.intervention.mode,
180 crate::config::InterventionMode::Strict
181 );
182 assert_eq!(
183 c2.intervention.max_level,
184 crate::config::InterventionLevel::RefuseToDecide
185 );
186 assert_eq!(c2.intervention.friction_threshold, 3);
187 }
188}