robonix_executor/
config.rs1use anyhow::{Context, Result, bail};
8use clap::Parser;
9use serde::Deserialize;
10use std::collections::HashSet;
11use std::path::{Path, PathBuf};
12
13pub const DEFAULT_EXECUTOR_PROVIDER_ID: &str = "executor";
14pub const EXECUTOR_NAMESPACE: &str = "robonix/system/executor";
15pub const DEFAULT_ATLAS_ENDPOINT: &str = "127.0.0.1:50051";
16pub const DEFAULT_LISTEN: &str = "127.0.0.1:50061";
17
18#[derive(Debug, Clone)]
20pub struct ExecutorConfig {
21 pub atlas_endpoint: String,
23 pub listen: String,
25 pub id: String,
27 pub verification: VerificationConfig,
29}
30
31#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
33pub struct VerificationConfig {
34 #[serde(default)]
36 pub overlap: bool,
37 #[serde(default)]
39 pub rules: Vec<VerificationRule>,
40}
41
42#[derive(Debug, Clone, Deserialize, PartialEq)]
44pub struct VerificationRule {
45 pub target_contract_id: String,
47 #[serde(default)]
49 pub target_provider_id: Option<String>,
50 pub verifier_provider_id: String,
52 #[serde(default = "empty_json_object")]
54 pub verifier_args: serde_json::Value,
55}
56
57fn empty_json_object() -> serde_json::Value {
58 serde_json::json!({})
59}
60
61#[derive(Parser, Debug)]
63#[command(
64 name = "robonix-executor",
65 about = "Robonix Executor — tool-call dispatch runtime"
66)]
67pub struct Args {
68 #[arg(long, env = "ROBONIX_ATLAS_ENDPOINT")]
71 pub atlas: Option<String>,
72
73 #[arg(long, env = "ROBONIX_EXECUTOR_LISTEN")]
75 pub listen: Option<String>,
76
77 #[arg(long, env = "ROBONIX_EXECUTOR_PROVIDER_ID")]
79 pub id: Option<String>,
80
81 #[arg(long, env = "ROBONIX_CONFIG_PATH")]
83 pub config: Option<PathBuf>,
84
85 #[arg(long)]
89 pub log: Option<String>,
90
91 #[arg(long)]
96 pub config_json: Option<String>,
97}
98
99#[derive(Default, Deserialize)]
101struct FileConfig {
102 #[serde(default)]
104 atlas_endpoint: Option<String>,
105 #[serde(default)]
107 listen: Option<String>,
108 #[serde(default)]
110 id: Option<String>,
111 #[serde(default)]
113 verification: Option<VerificationConfig>,
114}
115
116#[derive(Default, Deserialize)]
118struct ManifestConfig {
119 #[serde(default)]
121 verification: Option<VerificationConfig>,
122}
123
124impl ExecutorConfig {
125 pub fn resolve(args: Args) -> Result<Self> {
128 let file_cfg: FileConfig = match &args.config {
129 Some(path) => load_yaml(path)?,
130 None => FileConfig::default(),
131 };
132 let manifest_cfg: ManifestConfig = match args.config_json.as_deref() {
133 Some(raw) => serde_json::from_str(raw).context("parse Executor --config-json")?,
134 None => ManifestConfig::default(),
135 };
136 let mut verification = manifest_cfg
137 .verification
138 .or(file_cfg.verification)
139 .unwrap_or_default();
140 verification.rules = validate_verification_rules(verification.rules)?;
141 Ok(Self {
142 atlas_endpoint: args
143 .atlas
144 .or_else(env_atlas)
145 .or(file_cfg.atlas_endpoint)
146 .unwrap_or_else(|| DEFAULT_ATLAS_ENDPOINT.to_string()),
147 listen: args
148 .listen
149 .or(file_cfg.listen)
150 .unwrap_or_else(|| DEFAULT_LISTEN.to_string()),
151 id: args
152 .id
153 .or(file_cfg.id)
154 .unwrap_or_else(|| DEFAULT_EXECUTOR_PROVIDER_ID.to_string()),
155 verification,
156 })
157 }
158}
159
160fn validate_verification_rules(rules: Vec<VerificationRule>) -> Result<Vec<VerificationRule>> {
162 let mut seen = HashSet::new();
163 let mut normalized = Vec::with_capacity(rules.len());
164 for mut rule in rules {
165 rule.target_contract_id = rule.target_contract_id.trim().to_string();
166 rule.verifier_provider_id = rule.verifier_provider_id.trim().to_string();
167 rule.target_provider_id = rule
168 .target_provider_id
169 .map(|value| value.trim().to_string())
170 .filter(|value| !value.is_empty());
171 if rule.target_contract_id.is_empty() {
172 bail!("verification target_contract_id must not be empty");
173 }
174 if rule.verifier_provider_id.is_empty() {
175 bail!(
176 "verification verifier_provider_id must not be empty for '{}'",
177 rule.target_contract_id
178 );
179 }
180 if !rule.verifier_args.is_object() {
181 bail!(
182 "verification verifier_args must be a JSON object for '{}'",
183 rule.target_contract_id
184 );
185 }
186 let key = (
187 rule.target_contract_id.clone(),
188 rule.target_provider_id.clone(),
189 );
190 if !seen.insert(key) {
191 bail!(
192 "duplicate verification rule for contract '{}' and provider '{}'",
193 rule.target_contract_id,
194 rule.target_provider_id.as_deref().unwrap_or("*")
195 );
196 }
197 normalized.push(rule);
198 }
199 Ok(normalized)
200}
201
202fn env_atlas() -> Option<String> {
214 std::env::var("ROBONIX_ATLAS")
215 .ok()
216 .filter(|v| !v.is_empty())
217}
218
219fn load_yaml(path: &Path) -> Result<FileConfig> {
220 let raw = std::fs::read_to_string(path)
221 .with_context(|| format!("read executor config '{}'", path.display()))?;
222 serde_yaml::from_str(&raw)
223 .with_context(|| format!("parse executor config '{}'", path.display()))
224}
225
226#[cfg(test)]
227mod tests {
228 use super::*;
229
230 fn args(config_json: Option<&str>) -> Args {
231 Args {
232 atlas: None,
233 listen: None,
234 id: None,
235 config: None,
236 log: None,
237 config_json: config_json.map(str::to_string),
238 }
239 }
240
241 #[test]
242 fn parses_manifest_verification_rules() {
243 let cfg = ExecutorConfig::resolve(args(Some(
244 r#"{
245 "verification": {
246 "overlap": true,
247 "rules": [{
248 "target_contract_id": "robonix/service/navigation/navigate",
249 "verifier_provider_id": "scene_verifier",
250 "verifier_args": {"scene_provider_id": "scene"}
251 }]
252 }
253 }"#,
254 )))
255 .unwrap();
256 assert_eq!(cfg.verification.rules.len(), 1);
257 assert!(cfg.verification.overlap);
258 assert_eq!(
259 cfg.verification.rules[0].verifier_args["scene_provider_id"],
260 "scene"
261 );
262 }
263
264 #[test]
266 fn verification_overlap_defaults_to_false() {
267 let cfg = ExecutorConfig::resolve(args(Some(
268 r#"{
269 "verification": {
270 "rules": [{
271 "target_contract_id": "cap/a",
272 "verifier_provider_id": "verifier"
273 }]
274 }
275 }"#,
276 )))
277 .unwrap();
278
279 assert!(!cfg.verification.overlap);
280 }
281
282 #[test]
283 fn explicit_empty_manifest_rules_disable_defaults() {
284 let cfg =
285 ExecutorConfig::resolve(args(Some(r#"{"verification": {"rules": []}}"#))).unwrap();
286 assert!(cfg.verification.rules.is_empty());
287 }
288
289 #[test]
290 fn rejects_duplicate_rules_at_the_same_specificity() {
291 let error = ExecutorConfig::resolve(args(Some(
292 r#"{
293 "verification": {"rules": [
294 {"target_contract_id":"cap/a","verifier_provider_id":"v1"},
295 {"target_contract_id":"cap/a","verifier_provider_id":"v2"}
296 ]}
297 }"#,
298 )))
299 .unwrap_err();
300 assert!(error.to_string().contains("duplicate verification rule"));
301 }
302
303 #[test]
304 fn rejects_non_object_verifier_args() {
305 let error = ExecutorConfig::resolve(args(Some(
306 r#"{
307 "verification": {"rules": [{
308 "target_contract_id":"cap/a",
309 "verifier_provider_id":"v1",
310 "verifier_args": ["bad"]
311 }]}
312 }"#,
313 )))
314 .unwrap_err();
315 assert!(error.to_string().contains("must be a JSON object"));
316 }
317}