1use cron::Schedule;
7use serde::{Deserialize, Serialize};
8use std::str::FromStr;
9
10use crate::scheduler::Priority;
11
12#[derive(Debug, Clone, Deserialize, Serialize)]
14pub struct CronConfig {
15 #[serde(default)]
17 pub enabled: bool,
18 #[serde(default = "default_tick_interval")]
20 pub tick_interval_secs: u64,
21 #[serde(default)]
23 pub jobs: std::collections::HashMap<String, InlineCronJob>,
24}
25
26impl Default for CronConfig {
27 fn default() -> Self {
28 Self {
29 enabled: false,
30 tick_interval_secs: default_tick_interval(),
31 jobs: std::collections::HashMap::new(),
32 }
33 }
34}
35
36fn default_tick_interval() -> u64 {
37 60
38}
39
40#[derive(Debug, Clone, Deserialize, Serialize)]
42pub struct InlineCronJob {
43 pub schedule: String,
45 pub goal: String,
47 #[serde(default)]
49 pub constraints: Vec<String>,
50 #[serde(default)]
52 pub acceptance_criteria: Vec<String>,
53 #[serde(default = "default_toolchain_inline")]
55 pub toolchain: String,
56 #[serde(default)]
58 pub priority: Priority,
59 #[serde(default = "default_true_inline")]
61 pub enabled: bool,
62}
63
64fn default_toolchain_inline() -> String {
65 "default".into()
66}
67
68fn default_true_inline() -> bool {
69 true
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct MemoryConfig {
75 #[serde(default = "default_true")]
77 pub enabled: bool,
78 #[serde(default = "default_max_recall")]
80 pub max_recall: usize,
81 #[serde(default = "default_true")]
83 pub auto_summarize: bool,
84 #[serde(default = "default_true")]
86 pub capture_compaction: bool,
87 #[serde(default)]
89 pub retention_days: u32,
90 #[serde(default = "default_true")]
92 pub cache_enabled: bool,
93 #[serde(default = "default_cache_ttl")]
95 pub cache_ttl_secs: u64,
96 #[serde(default = "default_cache_max_entries")]
98 pub cache_max_entries: usize,
99}
100
101fn default_true() -> bool {
102 true
103}
104
105fn default_max_recall() -> usize {
106 10
107}
108
109fn default_cache_ttl() -> u64 {
110 3600 }
112
113fn default_cache_max_entries() -> usize {
114 10000
115}
116
117impl Default for MemoryConfig {
118 fn default() -> Self {
119 Self {
120 enabled: true,
121 max_recall: 10,
122 auto_summarize: true,
123 capture_compaction: true,
124 retention_days: 0,
125 cache_enabled: true,
126 cache_ttl_secs: 3600,
127 cache_max_entries: 10000,
128 }
129 }
130}
131
132#[derive(Debug, Clone, Deserialize, Serialize)]
134pub struct ChannelsConfig {
135 #[serde(default = "default_channels_enabled")]
138 pub enabled: Vec<String>,
139
140 #[serde(default)]
142 pub telegram: TelegramChannelConfig,
143}
144
145fn default_channels_enabled() -> Vec<String> {
146 vec!["web".to_string()]
147}
148
149impl Default for ChannelsConfig {
150 fn default() -> Self {
151 Self {
152 enabled: default_channels_enabled(),
153 telegram: TelegramChannelConfig::default(),
154 }
155 }
156}
157
158#[derive(Debug, Clone, Deserialize, Serialize)]
160pub struct TelegramChannelConfig {
161 #[serde(default = "default_telegram_token_env")]
163 pub bot_token_env: String,
164 #[serde(default)]
166 pub allowed_users: Vec<i64>,
167}
168
169fn default_telegram_token_env() -> String {
170 "TELEGRAM_BOT_TOKEN".to_string()
171}
172
173impl Default for TelegramChannelConfig {
174 fn default() -> Self {
175 Self {
176 bot_token_env: default_telegram_token_env(),
177 allowed_users: Vec::new(),
178 }
179 }
180}
181
182#[derive(Debug, Clone, Deserialize, Serialize)]
184#[allow(clippy::derivable_impls)]
185pub struct EngineConfig {
186 #[serde(default)]
189 pub default_model: String,
190 #[serde(default, skip_serializing)]
194 pub api_key: Option<String>,
195}
196
197#[allow(clippy::derivable_impls)]
198impl Default for EngineConfig {
199 fn default() -> Self {
200 Self {
201 default_model: String::new(),
202 api_key: None,
203 }
204 }
205}
206
207#[derive(Debug, Clone, Deserialize, Serialize)]
209pub struct DaemonConfig {
210 #[serde(default = "default_pid_file")]
212 pub pid_file: String,
213 #[serde(default = "default_daemon_log_dir")]
215 pub log_dir: String,
216}
217
218fn default_pid_file() -> String {
219 dirs::home_dir()
220 .map(|h| format!("{}/.oxios/oxios.pid", h.display()))
221 .unwrap_or_else(|| "./oxios.pid".into())
222}
223
224fn default_daemon_log_dir() -> String {
225 dirs::home_dir()
226 .map(|h| format!("{}/.oxios/logs", h.display()))
227 .unwrap_or_else(|| "./logs".into())
228}
229
230impl Default for DaemonConfig {
231 fn default() -> Self {
232 Self {
233 pid_file: default_pid_file(),
234 log_dir: default_daemon_log_dir(),
235 }
236 }
237}
238
239#[derive(Debug, Clone, Deserialize, Serialize, Default)]
241pub struct OxiosConfig {
242 pub kernel: KernelConfig,
244 #[serde(default)]
246 pub engine: EngineConfig,
247 #[serde(default)]
249 pub daemon: DaemonConfig,
250 #[serde(default)]
252 pub gateway: GatewayConfig,
253 #[serde(default)]
255 pub scheduler: SchedulerConfig,
256 #[serde(default)]
258 pub orchestrator: OrchestratorConfig,
259 #[serde(default)]
261 pub context: ContextConfig,
262 #[serde(default)]
264 pub security: SecurityConfig,
265 #[serde(default)]
267 pub persona: PersonaConfig,
268 #[serde(default)]
270 pub memory: MemoryConfig,
271 #[serde(default)]
273 pub cron: CronConfig,
274 #[serde(default)]
276 pub mcp: McpConfig,
277 #[serde(default)]
279 pub git: GitConfig,
280 #[serde(default)]
282 pub audit: AuditConfig,
283 #[serde(default)]
285 pub budget: BudgetConfig,
286 #[serde(default)]
288 pub exec: ExecConfig,
289 #[serde(default)]
291 pub resource_monitor: ResourceMonitorConfig,
292 #[serde(default)]
294 pub otel: OtelConfig,
295 #[serde(default)]
297 pub logging: LoggingConfig,
298 #[serde(default)]
300 pub channels: ChannelsConfig,
301 #[serde(default)]
303 pub browser: BrowserConfig,
304}
305
306#[derive(Debug, Clone, Deserialize, Serialize)]
308pub struct KernelConfig {
309 #[serde(default = "default_workspace")]
311 pub workspace: String,
312 #[serde(default = "default_event_bus_capacity")]
314 pub event_bus_capacity: usize,
315 #[serde(default = "default_max_agents")]
317 pub max_agents: usize,
318}
319
320fn default_workspace() -> String {
321 dirs_home().unwrap_or_else(|| ".".into())
322}
323
324fn dirs_home() -> Option<String> {
325 dirs::home_dir().map(|h| format!("{}/.oxios/workspace", h.display()))
326}
327
328fn default_event_bus_capacity() -> usize {
329 256
330}
331
332fn default_max_agents() -> usize {
333 16
334}
335
336impl Default for KernelConfig {
337 fn default() -> Self {
338 Self {
339 workspace: default_workspace(),
340 event_bus_capacity: default_event_bus_capacity(),
341 max_agents: default_max_agents(),
342 }
343 }
344}
345
346#[derive(Debug, Clone, Deserialize, Serialize)]
348pub struct GatewayConfig {
349 #[serde(default = "default_gateway_host")]
351 pub host: String,
352 #[serde(default = "default_gateway_port")]
354 pub port: u16,
355}
356
357fn default_gateway_host() -> String {
358 "127.0.0.1".into()
359}
360
361fn default_gateway_port() -> u16 {
362 4200
363}
364
365impl Default for GatewayConfig {
366 fn default() -> Self {
367 Self {
368 host: default_gateway_host(),
369 port: default_gateway_port(),
370 }
371 }
372}
373
374#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
379#[serde(rename_all = "lowercase")]
380pub enum ExecMode {
381 #[default]
383 Structured,
384 Shell,
386}
387
388#[derive(Debug, Clone, Deserialize, Serialize)]
392pub struct ExecConfig {
393 #[serde(default)]
395 pub default_mode: ExecMode,
396 #[serde(default = "default_false")]
398 pub allow_shell_mode: bool,
399 #[serde(default)]
402 pub allowed_commands: Vec<String>,
403 #[serde(default = "default_exec_timeout")]
405 pub default_timeout_secs: u64,
406 #[serde(default = "default_exec_max_timeout")]
408 pub max_timeout_secs: u64,
409 #[serde(default)]
411 pub required_host_tools: Vec<String>,
412 #[serde(default)]
414 pub optional_host_tools: Vec<String>,
415}
416
417fn default_false() -> bool {
418 false
419}
420
421fn default_exec_timeout() -> u64 {
422 120
423}
424
425fn default_exec_max_timeout() -> u64 {
426 600
427}
428
429impl ExecConfig {
430 pub fn is_binary_allowed(&self, name: &str) -> bool {
435 self.allowed_commands.is_empty() || self.allowed_commands.iter().any(|c| c == name)
436 }
437}
438
439impl Default for ExecConfig {
440 fn default() -> Self {
441 Self {
442 default_mode: ExecMode::default(),
443 allow_shell_mode: default_false(),
444 allowed_commands: Vec::new(),
445 default_timeout_secs: default_exec_timeout(),
446 max_timeout_secs: default_exec_max_timeout(),
447 required_host_tools: Vec::new(),
448 optional_host_tools: Vec::new(),
449 }
450 }
451}
452
453#[derive(Debug, Clone, Deserialize, Serialize)]
455pub struct SchedulerConfig {
456 #[serde(default = "default_max_concurrent")]
458 pub max_concurrent: usize,
459 #[serde(default = "default_rate_limit")]
461 pub rate_limit_per_minute: u32,
462 #[serde(default = "default_zombie_timeout")]
464 pub zombie_timeout_secs: u64,
465}
466
467fn default_max_concurrent() -> usize {
468 5
469}
470
471fn default_rate_limit() -> u32 {
472 60
473}
474
475fn default_zombie_timeout() -> u64 {
476 300
477}
478
479impl Default for SchedulerConfig {
480 fn default() -> Self {
481 Self {
482 max_concurrent: default_max_concurrent(),
483 rate_limit_per_minute: default_rate_limit(),
484 zombie_timeout_secs: default_zombie_timeout(),
485 }
486 }
487}
488
489#[derive(Debug, Clone, Deserialize, Serialize)]
503pub struct OrchestratorConfig {
504 #[serde(default = "default_max_evolution_iterations")]
506 pub max_evolution_iterations: usize,
507 #[serde(default = "default_min_evaluation_score")]
510 pub min_evaluation_score: f64,
511}
512
513fn default_max_evolution_iterations() -> usize {
514 3
515}
516
517fn default_min_evaluation_score() -> f64 {
518 0.8
519}
520
521impl Default for OrchestratorConfig {
522 fn default() -> Self {
523 Self {
524 max_evolution_iterations: default_max_evolution_iterations(),
525 min_evaluation_score: default_min_evaluation_score(),
526 }
527 }
528}
529
530#[derive(Debug, Clone, Deserialize, Serialize)]
532pub struct ContextConfig {
533 #[serde(default = "default_active_limit")]
535 pub active_limit_tokens: usize,
536 #[serde(default = "default_cache_limit")]
538 pub cache_limit_entries: usize,
539}
540
541fn default_active_limit() -> usize {
542 100_000
543}
544
545fn default_cache_limit() -> usize {
546 50
547}
548
549impl Default for ContextConfig {
550 fn default() -> Self {
551 Self {
552 active_limit_tokens: default_active_limit(),
553 cache_limit_entries: default_cache_limit(),
554 }
555 }
556}
557
558#[derive(Debug, Clone, Deserialize, Serialize)]
560pub struct SecurityConfig {
561 #[serde(default = "default_allowed_tools")]
563 pub allowed_tools: Vec<String>,
564 #[serde(default)]
566 pub network_access: bool,
567 #[serde(default = "default_max_exec_time")]
569 pub max_execution_time_secs: u64,
570 #[serde(default = "default_max_memory")]
572 pub max_memory_mb: u64,
573 #[serde(default)]
575 pub can_fork: bool,
576 #[serde(default = "default_max_audit")]
578 pub max_audit_entries: usize,
579 #[serde(default)]
581 pub auth_enabled: bool,
582 #[serde(default = "default_cors_origins")]
584 pub cors_origins: Vec<String>,
585 #[serde(default)]
587 pub audit_log_path: Option<String>,
588 #[serde(default = "default_rate_limit_per_minute")]
590 pub rate_limit_per_minute: u32,
591}
592
593fn default_allowed_tools() -> Vec<String> {
594 vec![
595 "read".to_string(),
596 "write".to_string(),
597 "edit".to_string(),
598 "bash".to_string(),
599 "grep".to_string(),
600 "find".to_string(),
601 ]
602}
603
604fn default_max_exec_time() -> u64 {
605 300
606}
607
608fn default_max_memory() -> u64 {
609 512
610}
611
612fn default_max_audit() -> usize {
613 10_000
614}
615
616fn default_rate_limit_per_minute() -> u32 {
617 120
618}
619
620fn default_cors_origins() -> Vec<String> {
621 vec!["http://localhost:4200".to_string()]
622}
623
624impl Default for SecurityConfig {
625 fn default() -> Self {
626 Self {
627 allowed_tools: default_allowed_tools(),
628 network_access: false,
629 max_execution_time_secs: default_max_exec_time(),
630 max_memory_mb: default_max_memory(),
631 can_fork: false,
632 max_audit_entries: default_max_audit(),
633 auth_enabled: false,
634 cors_origins: default_cors_origins(),
635 audit_log_path: None,
636 rate_limit_per_minute: default_rate_limit_per_minute(),
637 }
638 }
639}
640
641#[derive(Debug, Clone, Deserialize, Serialize)]
643pub struct PersonaConfig {
644 #[serde(default)]
646 pub default_persona_id: Option<String>,
647 #[serde(default = "default_max_concurrent_personas")]
649 pub max_concurrent_personas: usize,
650}
651
652fn default_max_concurrent_personas() -> usize {
653 5
654}
655
656impl Default for PersonaConfig {
657 fn default() -> Self {
658 Self {
659 default_persona_id: Some("dev".to_string()),
660 max_concurrent_personas: default_max_concurrent_personas(),
661 }
662 }
663}
664
665#[derive(Debug, Clone, Deserialize, Serialize, Default)]
673pub struct McpConfig {
674 #[serde(default)]
676 pub servers: std::collections::HashMap<String, McpServerDef>,
677}
678
679#[derive(Debug, Clone, Deserialize, Serialize)]
681pub struct McpServerDef {
682 pub command: String,
684 #[serde(default)]
686 pub args: Vec<String>,
687 #[serde(default)]
689 pub env: std::collections::HashMap<String, String>,
690 #[serde(default = "default_mcp_enabled")]
692 pub enabled: bool,
693}
694
695fn default_mcp_enabled() -> bool {
696 true
697}
698
699#[derive(Debug, Clone, Deserialize, Serialize)]
701pub struct GitConfig {
702 #[serde(default = "default_true")]
704 pub auto_commit: bool,
705}
706
707impl Default for GitConfig {
708 fn default() -> Self {
709 Self { auto_commit: true }
710 }
711}
712
713#[derive(Debug, Clone, Deserialize, Serialize)]
715pub struct AuditConfig {
716 #[serde(default = "default_audit_max_entries")]
718 pub max_entries: usize,
719 #[serde(default = "default_true")]
721 pub enabled: bool,
722}
723
724fn default_audit_max_entries() -> usize {
725 100_000
726}
727
728impl Default for AuditConfig {
729 fn default() -> Self {
730 Self {
731 max_entries: default_audit_max_entries(),
732 enabled: true,
733 }
734 }
735}
736
737#[derive(Debug, Clone, Deserialize, Serialize)]
739pub struct BudgetConfig {
740 #[serde(default)]
742 pub default_token_budget: u64,
743 #[serde(default)]
745 pub default_calls_budget: u64,
746 #[serde(default = "default_budget_window")]
748 pub default_window_secs: u64,
749 #[serde(default = "default_true")]
751 pub enabled: bool,
752}
753
754fn default_budget_window() -> u64 {
755 3600
756}
757
758impl Default for BudgetConfig {
759 fn default() -> Self {
760 Self {
761 default_token_budget: 0,
762 default_calls_budget: 0,
763 default_window_secs: default_budget_window(),
764 enabled: true,
765 }
766 }
767}
768
769#[derive(Debug, Clone, Deserialize, Serialize)]
771pub struct ResourceMonitorConfig {
772 #[serde(default = "default_rm_interval")]
774 pub interval_secs: u64,
775 #[serde(default = "default_rm_history_max")]
777 pub history_max: usize,
778 #[serde(default = "default_rm_cpu_threshold")]
780 pub cpu_threshold: f32,
781 #[serde(default = "default_rm_mem_threshold")]
783 pub memory_threshold: f32,
784 #[serde(default = "default_rm_load_threshold")]
786 pub load_threshold: f32,
787}
788
789fn default_rm_interval() -> u64 {
790 60
791}
792
793fn default_rm_history_max() -> usize {
794 60
795}
796
797fn default_rm_cpu_threshold() -> f32 {
798 90.0
799}
800
801fn default_rm_mem_threshold() -> f32 {
802 90.0
803}
804
805fn default_rm_load_threshold() -> f32 {
806 8.0
807}
808
809impl Default for ResourceMonitorConfig {
810 fn default() -> Self {
811 Self {
812 interval_secs: default_rm_interval(),
813 history_max: default_rm_history_max(),
814 cpu_threshold: default_rm_cpu_threshold(),
815 memory_threshold: default_rm_mem_threshold(),
816 load_threshold: default_rm_load_threshold(),
817 }
818 }
819}
820
821#[derive(Debug, Clone, Deserialize, Serialize)]
823pub struct OtelConfig {
824 #[serde(default)]
826 pub enabled: bool,
827 #[serde(default = "default_otel_endpoint")]
829 pub endpoint: String,
830 #[serde(default = "default_otel_service_name")]
832 pub service_name: String,
833 #[serde(default = "default_otel_sampling_ratio")]
835 pub sampling_ratio: f64,
836}
837
838fn default_otel_endpoint() -> String {
839 "http://localhost:4317".into()
840}
841
842fn default_otel_service_name() -> String {
843 "oxios".into()
844}
845
846fn default_otel_sampling_ratio() -> f64 {
847 1.0
848}
849
850impl Default for OtelConfig {
851 fn default() -> Self {
852 Self {
853 enabled: false,
854 endpoint: default_otel_endpoint(),
855 service_name: default_otel_service_name(),
856 sampling_ratio: default_otel_sampling_ratio(),
857 }
858 }
859}
860
861#[derive(Debug, Clone, Deserialize, Serialize)]
863pub struct LoggingConfig {
864 #[serde(default = "default_log_format")]
866 pub format: String,
867 #[serde(default)]
869 pub level: Option<String>,
870}
871
872fn default_log_format() -> String {
873 "pretty".into()
874}
875
876impl Default for LoggingConfig {
877 fn default() -> Self {
878 Self {
879 format: default_log_format(),
880 level: None,
881 }
882 }
883}
884
885#[derive(Debug, Clone, Deserialize, Serialize)]
891pub struct BrowserConfig {
892 #[serde(default = "default_browser_enabled")]
894 pub enabled: bool,
895
896 #[serde(default)]
907 pub engine: oxibrowser_core::BrowserConfig,
908}
909
910fn default_browser_enabled() -> bool {
911 true
912}
913
914impl Default for BrowserConfig {
915 fn default() -> Self {
916 Self {
917 enabled: true,
918 engine: oxibrowser_core::BrowserConfig::headless(),
919 }
920 }
921}
922
923pub fn load_config(path: &std::path::Path) -> anyhow::Result<OxiosConfig> {
925 let content = std::fs::read_to_string(path)?;
926 let config: OxiosConfig = toml::from_str(&content)?;
927 let (errors, warnings) = config.validate();
928 for w in warnings {
929 tracing::warn!("config: {}", w);
930 }
931 if !errors.is_empty() {
932 let msg = errors.join("; ");
933 anyhow::bail!("Configuration validation failed: {}", msg);
934 }
935 Ok(config)
936}
937
938impl OxiosConfig {
939 pub fn api_key(&self) -> Option<String> {
941 self.engine.api_key.clone().filter(|k| !k.is_empty())
942 }
943
944 pub fn validate(&self) -> (Vec<String>, Vec<String>) {
947 let mut errors = Vec::new();
948 let mut warnings = Vec::new();
949
950 if self.kernel.max_agents == 0 {
952 errors.push("kernel.max_agents must be > 0".into());
953 }
954 if self.kernel.workspace.is_empty() {
955 errors.push("kernel.workspace must not be empty".into());
956 }
957
958 if self.gateway.port == 0 {
960 errors.push("gateway.port must be > 0".into());
961 }
962 if self.gateway.port < 1024 && self.gateway.host == "0.0.0.0" {
963 warnings.push("Running on port <1024 as 0.0.0.0 may require root".into());
964 }
965
966 if self.scheduler.max_concurrent == 0 {
968 warnings.push("scheduler.max_concurrent is 0 — no tasks will run".into());
969 }
970 if self.scheduler.zombie_timeout_secs == 0 {
971 errors.push("scheduler.zombie_timeout_secs must be > 0".into());
972 }
973
974 for (name, job) in &self.cron.jobs {
976 if job.schedule.is_empty() {
977 errors.push(format!("cron.jobs.{}: schedule is empty", name));
978 } else {
979 let normalized = {
981 let fields: Vec<&str> = job.schedule.split_whitespace().collect();
982 match fields.len() {
983 5 => format!("0 {}", job.schedule),
984 _ => job.schedule.clone(),
985 }
986 };
987 if Schedule::from_str(&normalized).is_err() {
988 errors.push(format!(
989 "cron.jobs.{}: invalid cron expression '{}'",
990 name, job.schedule
991 ));
992 }
993 }
994 if job.goal.is_empty() {
995 errors.push(format!("cron.jobs.{}: goal is empty", name));
996 }
997 }
998
999 if self.security.max_execution_time_secs == 0 {
1001 warnings.push("security.max_execution_time_secs is 0 — no timeout".into());
1002 }
1003
1004 if self.audit.max_entries == 0 {
1006 warnings.push("audit.max_entries is 0 — audit will never prune".into());
1007 }
1008
1009 if self.budget.default_window_secs == 0 {
1011 warnings.push("budget.default_window_secs is 0 — no time window".into());
1012 }
1013
1014 if self.exec.default_timeout_secs == 0 {
1016 errors.push("exec.default_timeout_secs must be > 0".into());
1017 }
1018 if self.exec.max_timeout_secs == 0 {
1019 errors.push("exec.max_timeout_secs must be > 0".into());
1020 }
1021 if self.exec.default_timeout_secs > self.exec.max_timeout_secs {
1022 errors.push(format!(
1023 "exec.default_timeout_secs ({}) must not exceed max_timeout_secs ({})",
1024 self.exec.default_timeout_secs, self.exec.max_timeout_secs
1025 ));
1026 }
1027
1028 if self.resource_monitor.cpu_threshold > 100.0 {
1030 errors.push("resource_monitor.cpu_threshold must be <= 100".into());
1031 }
1032 if self.resource_monitor.memory_threshold > 100.0 {
1033 errors.push("resource_monitor.memory_threshold must be <= 100".into());
1034 }
1035
1036 for name in &self.channels.enabled {
1038 let valid = ["web", "cli", "telegram"];
1039 if !valid.contains(&name.as_str()) {
1040 warnings.push(format!("channels.enabled: unknown channel '{}'", name));
1041 }
1042 }
1043 if self.channels.enabled.iter().any(|c| c == "telegram")
1044 && std::env::var(&self.channels.telegram.bot_token_env).is_err()
1045 {
1046 warnings.push(format!(
1047 "channels.telegram: {} env var not set — telegram channel will fail",
1048 self.channels.telegram.bot_token_env
1049 ));
1050 }
1051
1052 (errors, warnings)
1053 }
1054}
1055
1056pub fn expand_home(path: &str) -> std::path::PathBuf {
1060 if let Some(rest) = path.strip_prefix("~/") {
1061 if let Ok(home) = std::env::var("HOME") {
1062 return std::path::PathBuf::from(format!("{home}/{rest}"));
1063 }
1064 }
1065 std::path::PathBuf::from(path)
1066}
1067
1068#[cfg(test)]
1069mod tests {
1070 use super::*;
1071
1072 #[test]
1073 fn test_default_config_validates() {
1074 let config = OxiosConfig::default();
1075 let (errors, _warnings) = config.validate();
1076 assert!(
1077 errors.is_empty(),
1078 "Default config should have no errors: {:?}",
1079 errors
1080 );
1081 }
1082
1083 #[test]
1084 fn test_exec_config_default_allowed_commands() {
1085 let config = ExecConfig::default();
1086 assert!(config.allowed_commands.is_empty());
1088 assert!(config.is_binary_allowed("anything"));
1089 assert!(config.is_binary_allowed("bash"));
1090 assert!(config.is_binary_allowed("rm"));
1091 }
1092
1093 #[test]
1094 fn test_is_binary_allowed_with_allowlist() {
1095 let config = ExecConfig {
1096 allowed_commands: vec!["git".into(), "echo".into()],
1097 ..Default::default()
1098 };
1099 assert!(config.is_binary_allowed("git"));
1100 assert!(config.is_binary_allowed("echo"));
1101 assert!(!config.is_binary_allowed("bash"));
1102 assert!(!config.is_binary_allowed("rm"));
1103 assert!(!config.is_binary_allowed("sudo"));
1104 }
1105
1106 #[test]
1107 fn test_expand_home() {
1108 let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp/testhome".into());
1110 let expanded = expand_home("~/projects/test");
1111 assert_eq!(
1112 expanded.to_str().unwrap(),
1113 format!("{}/projects/test", home)
1114 );
1115
1116 let abs = expand_home("/absolute/path");
1118 assert_eq!(abs, std::path::PathBuf::from("/absolute/path"));
1119
1120 let bare = expand_home("~something");
1122 assert_eq!(bare, std::path::PathBuf::from("~something"));
1123 }
1124
1125 #[test]
1126 fn test_invalid_cron_expression() {
1127 let mut config = OxiosConfig::default();
1128 config.cron.enabled = true;
1129 config.cron.jobs.insert(
1130 "bad-job".to_string(),
1131 InlineCronJob {
1132 schedule: "not a valid cron".to_string(),
1133 goal: "Test goal".to_string(),
1134 constraints: vec![],
1135 acceptance_criteria: vec![],
1136 toolchain: "default".to_string(),
1137 priority: Priority::Normal,
1138 enabled: true,
1139 },
1140 );
1141
1142 let (errors, _warnings) = config.validate();
1143 assert!(
1144 !errors.is_empty(),
1145 "Expected validation error for invalid cron"
1146 );
1147 let has_cron_error = errors.iter().any(|e| e.contains("invalid cron expression"));
1148 assert!(
1149 has_cron_error,
1150 "Expected 'invalid cron expression' error, got: {:?}",
1151 errors
1152 );
1153 }
1154
1155 #[test]
1156 fn test_config_serialization_roundtrip() {
1157 let config = OxiosConfig::default();
1158
1159 let toml_str = toml::to_string(&config).expect("serialization should succeed");
1161
1162 let deserialized: OxiosConfig =
1164 toml::from_str(&toml_str).expect("deserialization should succeed");
1165
1166 assert_eq!(config.kernel.max_agents, deserialized.kernel.max_agents);
1168 assert_eq!(config.kernel.workspace, deserialized.kernel.workspace);
1169 assert_eq!(config.gateway.host, deserialized.gateway.host);
1170 assert_eq!(config.gateway.port, deserialized.gateway.port);
1171 assert_eq!(
1172 config.exec.default_timeout_secs,
1173 deserialized.exec.default_timeout_secs
1174 );
1175 assert_eq!(
1176 config.exec.max_timeout_secs,
1177 deserialized.exec.max_timeout_secs
1178 );
1179 }
1180
1181 #[test]
1182 fn test_exec_timeout_validation() {
1183 let mut config = OxiosConfig::default();
1184 config.exec.default_timeout_secs = 999;
1186 config.exec.max_timeout_secs = 100;
1187 let (errors, _warnings) = config.validate();
1188 let has_error = errors.iter().any(|e| e.contains("must not exceed"));
1189 assert!(
1190 has_error,
1191 "Expected timeout ordering error, got: {:?}",
1192 errors
1193 );
1194 }
1195
1196 #[test]
1197 fn test_zero_max_agents_error() {
1198 let mut config = OxiosConfig::default();
1199 config.kernel.max_agents = 0;
1200 let (errors, _warnings) = config.validate();
1201 assert!(errors.iter().any(|e| e.contains("max_agents must be > 0")));
1202 }
1203}