Skip to main content

oxios_kernel/
config.rs

1//! Configuration loading from TOML files.
2//!
3//! Configuration is stored at `~/.oxios/config.toml` and controls
4//! kernel, gateway, and execution settings.
5
6use cron::Schedule;
7use serde::{Deserialize, Serialize};
8use std::str::FromStr;
9
10use crate::scheduler::Priority;
11
12/// Cron scheduler configuration.
13#[derive(Debug, Clone, Deserialize, Serialize)]
14pub struct CronConfig {
15    /// Enable the cron scheduler.
16    #[serde(default)]
17    pub enabled: bool,
18    /// Tick interval in seconds.
19    #[serde(default = "default_tick_interval")]
20    pub tick_interval_secs: u64,
21    /// Inline job definitions from config.toml.
22    #[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/// Inline cron job definition in config.toml.
41#[derive(Debug, Clone, Deserialize, Serialize)]
42pub struct InlineCronJob {
43    /// Cron expression (e.g. "0 */6 * * *").
44    pub schedule: String,
45    /// Goal description for the agent.
46    pub goal: String,
47    /// Constraints on agent behavior.
48    #[serde(default)]
49    pub constraints: Vec<String>,
50    /// Criteria that must be met for the job to be considered successful.
51    #[serde(default)]
52    pub acceptance_criteria: Vec<String>,
53    /// Toolchain preset name.
54    #[serde(default = "default_toolchain_inline")]
55    pub toolchain: String,
56    /// Job priority.
57    #[serde(default)]
58    pub priority: Priority,
59    /// Whether the job is active.
60    #[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/// Memory system configuration.
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct MemoryConfig {
75    /// Enable the memory system.
76    #[serde(default = "default_true")]
77    pub enabled: bool,
78    /// Maximum memories returned by recall.
79    #[serde(default = "default_max_recall")]
80    pub max_recall: usize,
81    /// Auto-summarize sessions on completion.
82    #[serde(default = "default_true")]
83    pub auto_summarize: bool,
84    /// Capture compaction summaries as conversation memory.
85    #[serde(default = "default_true")]
86    pub capture_compaction: bool,
87    /// Memory retention in days (0 = unlimited).
88    #[serde(default)]
89    pub retention_days: u32,
90    /// Enable embedding cache.
91    #[serde(default = "default_true")]
92    pub cache_enabled: bool,
93    /// Embedding cache TTL in seconds.
94    #[serde(default = "default_cache_ttl")]
95    pub cache_ttl_secs: u64,
96    /// Maximum embedding cache entries.
97    #[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 // 1 hour
111}
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/// Channel activation configuration.
133#[derive(Debug, Clone, Deserialize, Serialize)]
134pub struct ChannelsConfig {
135    /// List of channel names to activate on startup.
136    /// Default: ["web"]
137    #[serde(default = "default_channels_enabled")]
138    pub enabled: Vec<String>,
139
140    /// Telegram-specific configuration.
141    #[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/// Telegram channel configuration.
159#[derive(Debug, Clone, Deserialize, Serialize)]
160pub struct TelegramChannelConfig {
161    /// Environment variable name holding the bot token.
162    #[serde(default = "default_telegram_token_env")]
163    pub bot_token_env: String,
164    /// List of allowed Telegram user IDs (empty = allow all).
165    #[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/// LLM engine configuration.
183#[derive(Debug, Clone, Deserialize, Serialize)]
184#[allow(clippy::derivable_impls)]
185pub struct EngineConfig {
186    /// Default model in "provider/model" format.
187    /// Empty string means no model configured — onboarding required.
188    #[serde(default)]
189    pub default_model: String,
190    /// Explicit API key override (highest priority).
191    /// If empty/None, falls back to oxi auth store, then env vars.
192    /// Masked when serialized to API responses.
193    #[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/// Daemon mode configuration.
208#[derive(Debug, Clone, Deserialize, Serialize)]
209pub struct DaemonConfig {
210    /// PID file path.
211    #[serde(default = "default_pid_file")]
212    pub pid_file: String,
213    /// Log directory.
214    #[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/// Top-level Oxios configuration.
240#[derive(Debug, Clone, Deserialize, Serialize, Default)]
241pub struct OxiosConfig {
242    /// Kernel settings.
243    pub kernel: KernelConfig,
244    /// LLM engine settings.
245    #[serde(default)]
246    pub engine: EngineConfig,
247    /// Daemon mode settings.
248    #[serde(default)]
249    pub daemon: DaemonConfig,
250    /// Gateway settings.
251    #[serde(default)]
252    pub gateway: GatewayConfig,
253    /// Scheduler settings (AIOS-inspired task scheduling).
254    #[serde(default)]
255    pub scheduler: SchedulerConfig,
256    /// Orchestrator settings (Ouroboros protocol execution).
257    #[serde(default)]
258    pub orchestrator: OrchestratorConfig,
259    /// Context manager settings (LLM context window management).
260    #[serde(default)]
261    pub context: ContextConfig,
262    /// Security/access control settings.
263    #[serde(default)]
264    pub security: SecurityConfig,
265    /// Persona system settings.
266    #[serde(default)]
267    pub persona: PersonaConfig,
268    /// Memory system settings.
269    #[serde(default)]
270    pub memory: MemoryConfig,
271    /// Cron scheduler settings.
272    #[serde(default)]
273    pub cron: CronConfig,
274    /// MCP server configurations.
275    #[serde(default)]
276    pub mcp: McpConfig,
277    /// Git version control settings.
278    #[serde(default)]
279    pub git: GitConfig,
280    /// Audit trail configuration.
281    #[serde(default)]
282    pub audit: AuditConfig,
283    /// Budget enforcement configuration.
284    #[serde(default)]
285    pub budget: BudgetConfig,
286    /// Exec configuration (host command execution bridge).
287    #[serde(default)]
288    pub exec: ExecConfig,
289    /// Resource monitor configuration.
290    #[serde(default)]
291    pub resource_monitor: ResourceMonitorConfig,
292    /// OpenTelemetry tracing configuration.
293    #[serde(default)]
294    pub otel: OtelConfig,
295    /// Logging configuration.
296    #[serde(default)]
297    pub logging: LoggingConfig,
298    /// Channel activation configuration.
299    #[serde(default)]
300    pub channels: ChannelsConfig,
301    /// Headless browser configuration.
302    #[serde(default)]
303    pub browser: BrowserConfig,
304}
305
306/// Kernel configuration.
307#[derive(Debug, Clone, Deserialize, Serialize)]
308pub struct KernelConfig {
309    /// Path to the workspace directory.
310    #[serde(default = "default_workspace")]
311    pub workspace: String,
312    /// Broadcast capacity for the event bus.
313    #[serde(default = "default_event_bus_capacity")]
314    pub event_bus_capacity: usize,
315    /// Maximum number of concurrent agents.
316    #[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/// Gateway configuration.
347#[derive(Debug, Clone, Deserialize, Serialize)]
348pub struct GatewayConfig {
349    /// Host to bind the gateway to.
350    #[serde(default = "default_gateway_host")]
351    pub host: String,
352    /// Port for the gateway server.
353    #[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/// Execution mode for commands.
375///
376/// - `Structured`: Binary allowlist + metacharacter blocking (recommended)
377/// - `Shell`: Raw bash execution (dangerous, requires `allow_shell_mode=true`)
378#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
379#[serde(rename_all = "lowercase")]
380pub enum ExecMode {
381    /// Structured binary execution with allowlist and metacharacter blocking.
382    #[default]
383    Structured,
384    /// Shell execution via `bash -c`. DANGEROUS — requires explicit enable.
385    Shell,
386}
387
388/// Exec configuration.
389///
390/// Governs how the kernel dispatches commands for execution.
391#[derive(Debug, Clone, Deserialize, Serialize)]
392pub struct ExecConfig {
393    /// Default execution mode.
394    #[serde(default)]
395    pub default_mode: ExecMode,
396    /// Allow shell mode. DANGEROUS — should be false in production.
397    #[serde(default = "default_false")]
398    pub allow_shell_mode: bool,
399    /// Commands allowed to run on the host.
400    /// If empty, *all* bare-name commands are permitted (development mode).
401    #[serde(default)]
402    pub allowed_commands: Vec<String>,
403    /// Default timeout for an exec call in seconds.
404    #[serde(default = "default_exec_timeout")]
405    pub default_timeout_secs: u64,
406    /// Maximum allowed timeout for an exec call in seconds.
407    #[serde(default = "default_exec_max_timeout")]
408    pub max_timeout_secs: u64,
409    /// Host tools that MUST be present (checked on startup).
410    #[serde(default)]
411    pub required_host_tools: Vec<String>,
412    /// Host tools that are optional (checked lazily when needed).
413    #[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    /// Check whether a binary / command name is allowed to execute.
431    ///
432    /// Returns `true` when `allowed_commands` is empty (permissive dev mode)
433    /// **or** when the name is present in the allow-list.
434    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/// Scheduler configuration (inspired by AIOS / AgentRM).
454#[derive(Debug, Clone, Deserialize, Serialize)]
455pub struct SchedulerConfig {
456    /// Maximum number of concurrent agent tasks.
457    #[serde(default = "default_max_concurrent")]
458    pub max_concurrent: usize,
459    /// Maximum LLM API calls per minute (rate limiting).
460    #[serde(default = "default_rate_limit")]
461    pub rate_limit_per_minute: u32,
462    /// Timeout in seconds before a running task is considered a zombie.
463    #[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/// Orchestrator configuration (Ouroboros protocol execution).
490#[derive(Debug, Clone, Default, Deserialize, Serialize)]
491pub struct OrchestratorConfig {}
492
493// (removed manual impl Default — now derived)
494
495/// Context manager configuration (inspired by AIOS).
496#[derive(Debug, Clone, Deserialize, Serialize)]
497pub struct ContextConfig {
498    /// Maximum tokens in the active (in-context) tier.
499    #[serde(default = "default_active_limit")]
500    pub active_limit_tokens: usize,
501    /// Maximum entries in the cache tier.
502    #[serde(default = "default_cache_limit")]
503    pub cache_limit_entries: usize,
504}
505
506fn default_active_limit() -> usize {
507    100_000
508}
509
510fn default_cache_limit() -> usize {
511    50
512}
513
514impl Default for ContextConfig {
515    fn default() -> Self {
516        Self {
517            active_limit_tokens: default_active_limit(),
518            cache_limit_entries: default_cache_limit(),
519        }
520    }
521}
522
523/// Security/access control configuration (inspired by OWASP Agentic AI).
524#[derive(Debug, Clone, Deserialize, Serialize)]
525pub struct SecurityConfig {
526    /// Default allowed tools for agents (least privilege).
527    #[serde(default = "default_allowed_tools")]
528    pub allowed_tools: Vec<String>,
529    /// Whether agents can make network requests by default.
530    #[serde(default)]
531    pub network_access: bool,
532    /// Maximum execution time in seconds for agent tasks.
533    #[serde(default = "default_max_exec_time")]
534    pub max_execution_time_secs: u64,
535    /// Maximum memory in MB for agent tasks.
536    #[serde(default = "default_max_memory")]
537    pub max_memory_mb: u64,
538    /// Whether agents can fork sub-agents by default.
539    #[serde(default)]
540    pub can_fork: bool,
541    /// Maximum audit log entries to retain.
542    #[serde(default = "default_max_audit")]
543    pub max_audit_entries: usize,
544    /// Enable API key authentication.
545    #[serde(default)]
546    pub auth_enabled: bool,
547    /// Allowed CORS origins.
548    #[serde(default = "default_cors_origins")]
549    pub cors_origins: Vec<String>,
550    /// Path for audit log file (optional, enables file-based persistence).
551    #[serde(default)]
552    pub audit_log_path: Option<String>,
553    /// Rate limit for API endpoints (requests per minute).
554    #[serde(default = "default_rate_limit_per_minute")]
555    pub rate_limit_per_minute: u32,
556}
557
558fn default_allowed_tools() -> Vec<String> {
559    vec![
560        "read".to_string(),
561        "write".to_string(),
562        "edit".to_string(),
563        "bash".to_string(),
564        "grep".to_string(),
565        "find".to_string(),
566    ]
567}
568
569fn default_max_exec_time() -> u64 {
570    300
571}
572
573fn default_max_memory() -> u64 {
574    512
575}
576
577fn default_max_audit() -> usize {
578    10_000
579}
580
581fn default_rate_limit_per_minute() -> u32 {
582    120
583}
584
585fn default_cors_origins() -> Vec<String> {
586    vec!["http://localhost:4200".to_string()]
587}
588
589impl Default for SecurityConfig {
590    fn default() -> Self {
591        Self {
592            allowed_tools: default_allowed_tools(),
593            network_access: false,
594            max_execution_time_secs: default_max_exec_time(),
595            max_memory_mb: default_max_memory(),
596            can_fork: false,
597            max_audit_entries: default_max_audit(),
598            auth_enabled: false,
599            cors_origins: default_cors_origins(),
600            audit_log_path: None,
601            rate_limit_per_minute: default_rate_limit_per_minute(),
602        }
603    }
604}
605
606/// Persona system configuration.
607#[derive(Debug, Clone, Deserialize, Serialize)]
608pub struct PersonaConfig {
609    /// Default persona ID to activate on startup.
610    #[serde(default)]
611    pub default_persona_id: Option<String>,
612    /// Maximum concurrent personas.
613    #[serde(default = "default_max_concurrent_personas")]
614    pub max_concurrent_personas: usize,
615}
616
617fn default_max_concurrent_personas() -> usize {
618    5
619}
620
621impl Default for PersonaConfig {
622    fn default() -> Self {
623        Self {
624            default_persona_id: Some("dev".to_string()),
625            max_concurrent_personas: default_max_concurrent_personas(),
626        }
627    }
628}
629
630/// MCP server configuration loaded from config.toml.
631///
632/// Each key is a server name; the value is a table with:
633/// - `command`: executable to run (e.g. "npx", "python")
634/// - `args`: arguments array
635/// - `env`: optional map of environment variables
636/// - `enabled`: whether to start this server on boot (default: true)
637#[derive(Debug, Clone, Deserialize, Serialize, Default)]
638pub struct McpConfig {
639    /// Map of server-name → server definition.
640    #[serde(default)]
641    pub servers: std::collections::HashMap<String, McpServerDef>,
642}
643
644/// A single MCP server definition in config.toml.
645#[derive(Debug, Clone, Deserialize, Serialize)]
646pub struct McpServerDef {
647    /// Command to execute.
648    pub command: String,
649    /// Arguments passed to the command.
650    #[serde(default)]
651    pub args: Vec<String>,
652    /// Environment variables.
653    #[serde(default)]
654    pub env: std::collections::HashMap<String, String>,
655    /// Whether this server is enabled (default: true).
656    #[serde(default = "default_mcp_enabled")]
657    pub enabled: bool,
658}
659
660fn default_mcp_enabled() -> bool {
661    true
662}
663
664/// Git version control configuration.
665#[derive(Debug, Clone, Deserialize, Serialize)]
666pub struct GitConfig {
667    /// Enable automatic commits for state changes.
668    #[serde(default = "default_true")]
669    pub auto_commit: bool,
670}
671
672impl Default for GitConfig {
673    fn default() -> Self {
674        Self { auto_commit: true }
675    }
676}
677
678/// Audit trail configuration.
679#[derive(Debug, Clone, Deserialize, Serialize)]
680pub struct AuditConfig {
681    /// Maximum audit entries before pruning.
682    #[serde(default = "default_audit_max_entries")]
683    pub max_entries: usize,
684    /// Enable audit trail.
685    #[serde(default = "default_true")]
686    pub enabled: bool,
687}
688
689fn default_audit_max_entries() -> usize {
690    100_000
691}
692
693impl Default for AuditConfig {
694    fn default() -> Self {
695        Self {
696            max_entries: default_audit_max_entries(),
697            enabled: true,
698        }
699    }
700}
701
702/// Budget enforcement configuration.
703#[derive(Debug, Clone, Deserialize, Serialize)]
704pub struct BudgetConfig {
705    /// Default token budget per agent (0 = unlimited).
706    #[serde(default)]
707    pub default_token_budget: u64,
708    /// Default call budget per agent (0 = unlimited).
709    #[serde(default)]
710    pub default_calls_budget: u64,
711    /// Default budget window in seconds.
712    #[serde(default = "default_budget_window")]
713    pub default_window_secs: u64,
714    /// Enable budget enforcement.
715    #[serde(default = "default_true")]
716    pub enabled: bool,
717}
718
719fn default_budget_window() -> u64 {
720    3600
721}
722
723impl Default for BudgetConfig {
724    fn default() -> Self {
725        Self {
726            default_token_budget: 0,
727            default_calls_budget: 0,
728            default_window_secs: default_budget_window(),
729            enabled: true,
730        }
731    }
732}
733
734/// Resource monitor configuration.
735#[derive(Debug, Clone, Deserialize, Serialize)]
736pub struct ResourceMonitorConfig {
737    /// Snapshot interval in seconds.
738    #[serde(default = "default_rm_interval")]
739    pub interval_secs: u64,
740    /// Maximum history entries.
741    #[serde(default = "default_rm_history_max")]
742    pub history_max: usize,
743    /// CPU threshold for overload.
744    #[serde(default = "default_rm_cpu_threshold")]
745    pub cpu_threshold: f32,
746    /// Memory threshold for overload (percentage).
747    #[serde(default = "default_rm_mem_threshold")]
748    pub memory_threshold: f32,
749    /// Load average threshold for overload.
750    #[serde(default = "default_rm_load_threshold")]
751    pub load_threshold: f32,
752}
753
754fn default_rm_interval() -> u64 {
755    60
756}
757
758fn default_rm_history_max() -> usize {
759    60
760}
761
762fn default_rm_cpu_threshold() -> f32 {
763    90.0
764}
765
766fn default_rm_mem_threshold() -> f32 {
767    90.0
768}
769
770fn default_rm_load_threshold() -> f32 {
771    8.0
772}
773
774impl Default for ResourceMonitorConfig {
775    fn default() -> Self {
776        Self {
777            interval_secs: default_rm_interval(),
778            history_max: default_rm_history_max(),
779            cpu_threshold: default_rm_cpu_threshold(),
780            memory_threshold: default_rm_mem_threshold(),
781            load_threshold: default_rm_load_threshold(),
782        }
783    }
784}
785
786/// OpenTelemetry tracing configuration.
787#[derive(Debug, Clone, Deserialize, Serialize)]
788pub struct OtelConfig {
789    /// Enable OTLP export (default: false).
790    #[serde(default)]
791    pub enabled: bool,
792    /// OTLP gRPC endpoint.
793    #[serde(default = "default_otel_endpoint")]
794    pub endpoint: String,
795    /// Service name for traces.
796    #[serde(default = "default_otel_service_name")]
797    pub service_name: String,
798    /// Sampling ratio (0.0 to 1.0).
799    #[serde(default = "default_otel_sampling_ratio")]
800    pub sampling_ratio: f64,
801}
802
803fn default_otel_endpoint() -> String {
804    "http://localhost:4317".into()
805}
806
807fn default_otel_service_name() -> String {
808    "oxios".into()
809}
810
811fn default_otel_sampling_ratio() -> f64 {
812    1.0
813}
814
815impl Default for OtelConfig {
816    fn default() -> Self {
817        Self {
818            enabled: false,
819            endpoint: default_otel_endpoint(),
820            service_name: default_otel_service_name(),
821            sampling_ratio: default_otel_sampling_ratio(),
822        }
823    }
824}
825
826/// Logging configuration.
827#[derive(Debug, Clone, Deserialize, Serialize)]
828pub struct LoggingConfig {
829    /// Log format: "pretty", "json", or "compact".
830    #[serde(default = "default_log_format")]
831    pub format: String,
832    /// Log level override (e.g. "info", "debug"). Falls back to RUST_LOG env var.
833    #[serde(default)]
834    pub level: Option<String>,
835}
836
837fn default_log_format() -> String {
838    "pretty".into()
839}
840
841impl Default for LoggingConfig {
842    fn default() -> Self {
843        Self {
844            format: default_log_format(),
845            level: None,
846        }
847    }
848}
849
850/// Headless browser configuration.
851///
852/// Wraps `oxibrowser_core::BrowserConfig` (Deserialize/Serialize supported)
853/// with an `enabled` toggle. The engine config is passed through directly
854/// to the browser — no field-by-field duplication.
855#[derive(Debug, Clone, Deserialize, Serialize)]
856pub struct BrowserConfig {
857    /// Enable the browser integration.
858    #[serde(default = "default_browser_enabled")]
859    pub enabled: bool,
860
861    /// Engine configuration — passed directly to `oxibrowser_core::Browser::new()`.
862    ///
863    /// All fields have sensible defaults; override only what you need:
864    ///
865    /// ```toml
866    /// [browser.engine]
867    /// user_agent = "MyBot/1.0"
868    /// obey_robots = false
869    /// js_timeout_ms = 10000
870    /// ```
871    #[serde(default)]
872    pub engine: oxibrowser_core::BrowserConfig,
873}
874
875fn default_browser_enabled() -> bool {
876    true
877}
878
879impl Default for BrowserConfig {
880    fn default() -> Self {
881        Self {
882            enabled: true,
883            engine: oxibrowser_core::BrowserConfig::headless(),
884        }
885    }
886}
887
888/// Loads configuration from a TOML file.
889pub fn load_config(path: &std::path::Path) -> anyhow::Result<OxiosConfig> {
890    let content = std::fs::read_to_string(path)?;
891    let config: OxiosConfig = toml::from_str(&content)?;
892    let (errors, warnings) = config.validate();
893    for w in warnings {
894        tracing::warn!("config: {}", w);
895    }
896    if !errors.is_empty() {
897        let msg = errors.join("; ");
898        anyhow::bail!("Configuration validation failed: {}", msg);
899    }
900    Ok(config)
901}
902
903impl OxiosConfig {
904    /// Returns the effective API key from the engine config.
905    pub fn api_key(&self) -> Option<String> {
906        self.engine.api_key.clone().filter(|k| !k.is_empty())
907    }
908
909    /// Validate configuration values and return a list of warnings.
910    /// Returns (errors, warnings). Empty errors = valid config.
911    pub fn validate(&self) -> (Vec<String>, Vec<String>) {
912        let mut errors = Vec::new();
913        let mut warnings = Vec::new();
914
915        // Kernel validation
916        if self.kernel.max_agents == 0 {
917            errors.push("kernel.max_agents must be > 0".into());
918        }
919        if self.kernel.workspace.is_empty() {
920            errors.push("kernel.workspace must not be empty".into());
921        }
922
923        // Gateway validation
924        if self.gateway.port == 0 {
925            errors.push("gateway.port must be > 0".into());
926        }
927        if self.gateway.port < 1024 && self.gateway.host == "0.0.0.0" {
928            warnings.push("Running on port <1024 as 0.0.0.0 may require root".into());
929        }
930
931        // Scheduler validation
932        if self.scheduler.max_concurrent == 0 {
933            warnings.push("scheduler.max_concurrent is 0 — no tasks will run".into());
934        }
935        if self.scheduler.zombie_timeout_secs == 0 {
936            errors.push("scheduler.zombie_timeout_secs must be > 0".into());
937        }
938
939        // Cron validation
940        for (name, job) in &self.cron.jobs {
941            if job.schedule.is_empty() {
942                errors.push(format!("cron.jobs.{}: schedule is empty", name));
943            } else {
944                // Normalize 5-field to 6-field (prepend "0 " for seconds)
945                let normalized = {
946                    let fields: Vec<&str> = job.schedule.split_whitespace().collect();
947                    match fields.len() {
948                        5 => format!("0 {}", job.schedule),
949                        _ => job.schedule.clone(),
950                    }
951                };
952                if Schedule::from_str(&normalized).is_err() {
953                    errors.push(format!(
954                        "cron.jobs.{}: invalid cron expression '{}'",
955                        name, job.schedule
956                    ));
957                }
958            }
959            if job.goal.is_empty() {
960                errors.push(format!("cron.jobs.{}: goal is empty", name));
961            }
962        }
963
964        // Security validation
965        if self.security.max_execution_time_secs == 0 {
966            warnings.push("security.max_execution_time_secs is 0 — no timeout".into());
967        }
968
969        // Audit validation
970        if self.audit.max_entries == 0 {
971            warnings.push("audit.max_entries is 0 — audit will never prune".into());
972        }
973
974        // Budget validation
975        if self.budget.default_window_secs == 0 {
976            warnings.push("budget.default_window_secs is 0 — no time window".into());
977        }
978
979        // Exec validation
980        if self.exec.default_timeout_secs == 0 {
981            errors.push("exec.default_timeout_secs must be > 0".into());
982        }
983        if self.exec.max_timeout_secs == 0 {
984            errors.push("exec.max_timeout_secs must be > 0".into());
985        }
986        if self.exec.default_timeout_secs > self.exec.max_timeout_secs {
987            errors.push(format!(
988                "exec.default_timeout_secs ({}) must not exceed max_timeout_secs ({})",
989                self.exec.default_timeout_secs, self.exec.max_timeout_secs
990            ));
991        }
992
993        // Resource monitor validation
994        if self.resource_monitor.cpu_threshold > 100.0 {
995            errors.push("resource_monitor.cpu_threshold must be <= 100".into());
996        }
997        if self.resource_monitor.memory_threshold > 100.0 {
998            errors.push("resource_monitor.memory_threshold must be <= 100".into());
999        }
1000
1001        // Channels validation
1002        for name in &self.channels.enabled {
1003            let valid = ["web", "cli", "telegram"];
1004            if !valid.contains(&name.as_str()) {
1005                warnings.push(format!("channels.enabled: unknown channel '{}'", name));
1006            }
1007        }
1008        if self.channels.enabled.iter().any(|c| c == "telegram")
1009            && std::env::var(&self.channels.telegram.bot_token_env).is_err()
1010        {
1011            warnings.push(format!(
1012                "channels.telegram: {} env var not set — telegram channel will fail",
1013                self.channels.telegram.bot_token_env
1014            ));
1015        }
1016
1017        (errors, warnings)
1018    }
1019}
1020
1021/// Expand `~/` in paths to the user's home directory.
1022///
1023/// Shared utility for path expansion across the binary and kernel.
1024pub fn expand_home(path: &str) -> std::path::PathBuf {
1025    if let Some(rest) = path.strip_prefix("~/") {
1026        if let Ok(home) = std::env::var("HOME") {
1027            return std::path::PathBuf::from(format!("{home}/{rest}"));
1028        }
1029    }
1030    std::path::PathBuf::from(path)
1031}
1032
1033#[cfg(test)]
1034mod tests {
1035    use super::*;
1036
1037    #[test]
1038    fn test_default_config_validates() {
1039        let config = OxiosConfig::default();
1040        let (errors, _warnings) = config.validate();
1041        assert!(
1042            errors.is_empty(),
1043            "Default config should have no errors: {:?}",
1044            errors
1045        );
1046    }
1047
1048    #[test]
1049    fn test_exec_config_default_allowed_commands() {
1050        let config = ExecConfig::default();
1051        // Empty allowed_commands means all commands are permitted.
1052        assert!(config.allowed_commands.is_empty());
1053        assert!(config.is_binary_allowed("anything"));
1054        assert!(config.is_binary_allowed("bash"));
1055        assert!(config.is_binary_allowed("rm"));
1056    }
1057
1058    #[test]
1059    fn test_is_binary_allowed_with_allowlist() {
1060        let config = ExecConfig {
1061            allowed_commands: vec!["git".into(), "echo".into()],
1062            ..Default::default()
1063        };
1064        assert!(config.is_binary_allowed("git"));
1065        assert!(config.is_binary_allowed("echo"));
1066        assert!(!config.is_binary_allowed("bash"));
1067        assert!(!config.is_binary_allowed("rm"));
1068        assert!(!config.is_binary_allowed("sudo"));
1069    }
1070
1071    #[test]
1072    fn test_expand_home() {
1073        // With HOME set.
1074        let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp/testhome".into());
1075        let expanded = expand_home("~/projects/test");
1076        assert_eq!(
1077            expanded.to_str().unwrap(),
1078            format!("{}/projects/test", home)
1079        );
1080
1081        // Non-tilde path should pass through unchanged.
1082        let abs = expand_home("/absolute/path");
1083        assert_eq!(abs, std::path::PathBuf::from("/absolute/path"));
1084
1085        // Just ~ without slash should not expand.
1086        let bare = expand_home("~something");
1087        assert_eq!(bare, std::path::PathBuf::from("~something"));
1088    }
1089
1090    #[test]
1091    fn test_invalid_cron_expression() {
1092        let mut config = OxiosConfig::default();
1093        config.cron.enabled = true;
1094        config.cron.jobs.insert(
1095            "bad-job".to_string(),
1096            InlineCronJob {
1097                schedule: "not a valid cron".to_string(),
1098                goal: "Test goal".to_string(),
1099                constraints: vec![],
1100                acceptance_criteria: vec![],
1101                toolchain: "default".to_string(),
1102                priority: Priority::Normal,
1103                enabled: true,
1104            },
1105        );
1106
1107        let (errors, _warnings) = config.validate();
1108        assert!(
1109            !errors.is_empty(),
1110            "Expected validation error for invalid cron"
1111        );
1112        let has_cron_error = errors.iter().any(|e| e.contains("invalid cron expression"));
1113        assert!(
1114            has_cron_error,
1115            "Expected 'invalid cron expression' error, got: {:?}",
1116            errors
1117        );
1118    }
1119
1120    #[test]
1121    fn test_config_serialization_roundtrip() {
1122        let config = OxiosConfig::default();
1123
1124        // Serialize to TOML string.
1125        let toml_str = toml::to_string(&config).expect("serialization should succeed");
1126
1127        // Deserialize back.
1128        let deserialized: OxiosConfig =
1129            toml::from_str(&toml_str).expect("deserialization should succeed");
1130
1131        // Key fields should match.
1132        assert_eq!(config.kernel.max_agents, deserialized.kernel.max_agents);
1133        assert_eq!(config.kernel.workspace, deserialized.kernel.workspace);
1134        assert_eq!(config.gateway.host, deserialized.gateway.host);
1135        assert_eq!(config.gateway.port, deserialized.gateway.port);
1136        assert_eq!(
1137            config.exec.default_timeout_secs,
1138            deserialized.exec.default_timeout_secs
1139        );
1140        assert_eq!(
1141            config.exec.max_timeout_secs,
1142            deserialized.exec.max_timeout_secs
1143        );
1144    }
1145
1146    #[test]
1147    fn test_exec_timeout_validation() {
1148        let mut config = OxiosConfig::default();
1149        // default_timeout > max_timeout should be an error.
1150        config.exec.default_timeout_secs = 999;
1151        config.exec.max_timeout_secs = 100;
1152        let (errors, _warnings) = config.validate();
1153        let has_error = errors.iter().any(|e| e.contains("must not exceed"));
1154        assert!(
1155            has_error,
1156            "Expected timeout ordering error, got: {:?}",
1157            errors
1158        );
1159    }
1160
1161    #[test]
1162    fn test_zero_max_agents_error() {
1163        let mut config = OxiosConfig::default();
1164        config.kernel.max_agents = 0;
1165        let (errors, _warnings) = config.validate();
1166        assert!(errors.iter().any(|e| e.contains("max_agents must be > 0")));
1167    }
1168}