interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / spark-supervisor / src / config.rs

159 lines · 5551 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use serde::Deserialize;
17use std::collections::HashMap;
18use std::path::Path;
19
20#[derive(Debug, Clone, Deserialize)]
21pub struct ServiceConfig {
22 pub command: String,
23 #[serde(default)]
24 pub args: Vec<String>,
25 #[serde(default)]
26 pub env: HashMap<String, String>,
27 #[serde(default = "default_restart")]
28 pub restart: String,
29 pub health_url: Option<String>,
30}
31
32fn default_restart() -> String {
33 "always".to_string()
34}
35
36#[derive(Debug, Clone, Deserialize)]
37pub struct SupervisorConfig {
38 #[serde(default)]
39 pub services: HashMap<String, ServiceConfig>,
40}
41
42impl SupervisorConfig {
43 /// Parses the file and refuses it when any `health_url` is unusable.
44 pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
45 let path = path.as_ref();
46 let content = std::fs::read_to_string(path)?;
47 let cfg: SupervisorConfig = toml::from_str(&content)?;
48 cfg.validate_health_urls()
49 .map_err(|e| format!("supervisor config {} is invalid: {e}", path.display()))?;
50 Ok(cfg)
51 }
52
53 /// Every `health_url` must be a plain `http://` URL: probes go through
54 /// aien-localhttp, which never speaks TLS. An `https://` URL is a config
55 /// error, reported with the service name, not a probe that fails forever.
56 pub fn validate_health_urls(&self) -> Result<(), aien_localhttp::Error> {
57 let mut names: Vec<&String> = self.services.keys().collect();
58 names.sort();
59 for name in names {
60 if let Some(url) = &self.services[name].health_url {
61 aien_localhttp::Url::parse(url).map_err(|e| match e {
62 aien_localhttp::Error::HttpsRefused { .. } => {
63 aien_localhttp::Error::HttpsRefused {
64 url: format!("{url} (service {name:?} health_url)"),
65 }
66 }
67 other => other,
68 })?;
69 }
70 }
71 Ok(())
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_parse_supervisor_config() {
81 let sample = r#"
82[services.cortex]
83command = "/usr/local/bin/cortex-rs"
84args = ["--port", "18080"]
85health_url = "http://127.0.0.1:18080/health"
86
87[services.cockpit]
88command = "/usr/local/bin/spark-cockpit-rs"
89restart = "on-failure"
90"#;
91 let cfg: SupervisorConfig = toml::from_str(sample).expect("valid toml");
92 assert_eq!(cfg.services.len(), 2);
93 let cortex = cfg.services.get("cortex").unwrap();
94 assert_eq!(cortex.command, "/usr/local/bin/cortex-rs");
95 assert_eq!(cortex.args, vec!["--port", "18080"]);
96 assert_eq!(cortex.restart, "always");
97 assert_eq!(
98 cortex.health_url.as_deref(),
99 Some("http://127.0.0.1:18080/health")
100 );
101
102 let cockpit = cfg.services.get("cockpit").unwrap();
103 assert_eq!(cockpit.restart, "on-failure");
104 assert!(cockpit.health_url.is_none());
105 }
106
107 #[test]
108 fn test_empty_config_defaults() {
109 let sample = "";
110 let cfg: SupervisorConfig = toml::from_str(sample).expect("empty toml is valid");
111 assert!(cfg.services.is_empty());
112 }
113
114 fn write_config(tag: &str, body: &str) -> std::path::PathBuf {
115 let path = std::env::temp_dir().join(format!(
116 "spark-supervisor-{tag}-{}.toml",
117 std::process::id()
118 ));
119 std::fs::write(&path, body).unwrap();
120 path
121 }
122
123 #[test]
124 fn load_from_file_accepts_http_health_urls() {
125 let path = write_config(
126 "http",
127 "[services.cortex]\ncommand = \"x\"\nhealth_url = \"http://127.0.0.1:18080/health\"\n",
128 );
129 let cfg = SupervisorConfig::load_from_file(&path).expect("http is valid");
130 std::fs::remove_file(&path).unwrap();
131 assert_eq!(cfg.services.len(), 1);
132 }
133
134 #[test]
135 fn load_from_file_refuses_https_health_url_loudly() {
136 let path = write_config(
137 "https",
138 "[services.web]\ncommand = \"x\"\nhealth_url = \"https://127.0.0.1:8443/health\"\n",
139 );
140 let err = SupervisorConfig::load_from_file(&path)
141 .unwrap_err()
142 .to_string();
143 std::fs::remove_file(&path).unwrap();
144 assert!(err.contains("is invalid"), "{err}");
145 assert!(err.contains("https is refused"), "{err}");
146 assert!(err.contains("service \"web\" health_url"), "{err}");
147 }
148
149 #[test]
150 fn validate_health_urls_refuses_other_schemes_and_garbage() {
151 for bad in ["ftp://127.0.0.1/health", "127.0.0.1:80/health", "http://"] {
152 let cfg: SupervisorConfig = toml::from_str(&format!(
153 "[services.s]\ncommand = \"x\"\nhealth_url = \"{bad}\"\n"
154 ))
155 .unwrap();
156 assert!(cfg.validate_health_urls().is_err(), "{bad} must be refused");
157 }
158 }
159}