agnix-core 0.24.0

Core validation engine for agent configurations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Agent Skills schema (agentskills.io spec)

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// SKILL.md frontmatter schema
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillSchema {
    /// Required: skill name (lowercase, hyphens, 1-64 chars)
    pub name: String,

    /// Required: description (1-1024 chars)
    pub description: String,

    /// Optional: license identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,

    /// Optional: compatibility notes (1-500 chars)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compatibility: Option<String>,

    /// Optional: arbitrary metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,

    /// Optional: space-delimited list of allowed tools (experimental)
    #[serde(skip_serializing_if = "Option::is_none", rename = "allowed-tools")]
    pub allowed_tools: Option<String>,

    // Claude Code extensions
    /// Optional: argument hint for autocomplete
    #[serde(skip_serializing_if = "Option::is_none", rename = "argument-hint")]
    pub argument_hint: Option<String>,

    /// Optional: disable model invocation
    #[serde(
        skip_serializing_if = "Option::is_none",
        rename = "disable-model-invocation"
    )]
    pub disable_model_invocation: Option<bool>,

    /// Optional: user invocable
    #[serde(skip_serializing_if = "Option::is_none", rename = "user-invocable")]
    pub user_invocable: Option<bool>,

    /// Optional: model override
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,

    /// Optional: context mode
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<String>,

    /// Optional: agent type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent: Option<String>,

    /// Optional: reasoning effort level (low, medium, high, max)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<String>,

    /// Optional: comma-separated glob patterns or YAML list of file paths
    #[serde(skip_serializing_if = "Option::is_none")]
    pub paths: Option<serde_yaml::Value>,

    /// Optional: shell to use (bash, powershell)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shell: Option<String>,
}

/// Known top-level frontmatter fields for SKILL.md
#[cfg(test)]
pub const KNOWN_SKILL_FRONTMATTER_FIELDS: &[&str] = &[
    "name",
    "description",
    "license",
    "compatibility",
    "metadata",
    "allowed-tools",
    "argument-hint",
    "disable-model-invocation",
    "user-invocable",
    "model",
    "context",
    "agent",
    "hooks",
    "effort",
    "paths",
    "shell",
];

/// Valid model aliases for skill frontmatter
pub const VALID_MODEL_ALIASES: &[&str] = &["sonnet", "opus", "haiku", "inherit"];

/// Check whether a model value is valid.
pub fn is_valid_skill_model(model: &str) -> bool {
    VALID_MODEL_ALIASES.contains(&model) || model.starts_with("claude-")
}

/// Valid effort levels for skill frontmatter
pub const VALID_EFFORT_LEVELS: &[&str] = &["low", "medium", "high", "xhigh", "max"];

/// Valid shell values for skill frontmatter
pub const VALID_SHELLS: &[&str] = &["bash", "powershell"];

impl SkillSchema {
    /// Validate skill name format
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_name(&self) -> Result<(), String> {
        let name = &self.name;

        // Length check
        if name.is_empty() || name.len() > 64 {
            return Err(format!("Name must be 1-64 characters, got {}", name.len()));
        }

        // Character check
        for ch in name.chars() {
            if !ch.is_ascii_lowercase() && !ch.is_ascii_digit() && ch != '-' {
                return Err(format!(
                    "Name must contain only lowercase letters, digits, and hyphens, found '{}'",
                    ch
                ));
            }
        }

        // Start/end check
        if name.starts_with('-') || name.ends_with('-') {
            return Err("Name cannot start or end with hyphen".to_string());
        }

        // Consecutive hyphens
        if name.contains("--") {
            return Err("Name cannot contain consecutive hyphens".to_string());
        }

        Ok(())
    }

    /// Validate description length
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_description(&self) -> Result<(), String> {
        let len = self.description.len();
        if len == 0 || len > 1024 {
            return Err(format!(
                "Description must be 1-1024 characters, got {}",
                len
            ));
        }
        Ok(())
    }

    /// Validate compatibility length
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_compatibility(&self) -> Result<(), String> {
        if let Some(compat) = &self.compatibility {
            let len = compat.len();
            if len == 0 || len > 500 {
                return Err(format!(
                    "Compatibility must be 1-500 characters, got {}",
                    len
                ));
            }
        }
        Ok(())
    }

    /// Validate model value.
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_model(&self) -> Result<(), String> {
        if let Some(model) = &self.model {
            if !is_valid_skill_model(model) {
                return Err(format!(
                    "Model must be one of {:?} or a full model ID matching 'claude-*', got '{}'",
                    VALID_MODEL_ALIASES, model
                ));
            }
        }
        Ok(())
    }

    /// Validate effort value
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_effort(&self) -> Result<(), String> {
        if let Some(effort) = &self.effort {
            if !VALID_EFFORT_LEVELS.contains(&effort.as_str()) {
                return Err(format!(
                    "Effort must be one of: {:?}, got '{}'",
                    VALID_EFFORT_LEVELS, effort
                ));
            }
        }
        Ok(())
    }

    /// Validate shell value
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_shell(&self) -> Result<(), String> {
        if let Some(shell) = &self.shell {
            if !VALID_SHELLS.contains(&shell.as_str()) {
                return Err(format!(
                    "Shell must be one of: {:?}, got '{}'",
                    VALID_SHELLS, shell
                ));
            }
        }
        Ok(())
    }

    /// Validate context value
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate_context(&self) -> Result<(), String> {
        if let Some(context) = &self.context {
            if context != "fork" {
                return Err(format!("Context must be 'fork', got '{}'", context));
            }
        }
        Ok(())
    }

    /// Run all validations
    #[allow(dead_code)] // schema-level API; validation uses Validator trait
    pub fn validate(&self) -> Vec<String> {
        let mut errors = Vec::new();

        if let Err(e) = self.validate_name() {
            errors.push(e);
        }
        if let Err(e) = self.validate_description() {
            errors.push(e);
        }
        if let Err(e) = self.validate_compatibility() {
            errors.push(e);
        }
        if let Err(e) = self.validate_model() {
            errors.push(e);
        }
        if let Err(e) = self.validate_context() {
            errors.push(e);
        }
        if let Err(e) = self.validate_effort() {
            errors.push(e);
        }
        if let Err(e) = self.validate_shell() {
            errors.push(e);
        }

        errors
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_valid_skill_name() {
        let skill = SkillSchema {
            name: "code-review".to_string(),
            description: "Reviews code".to_string(),
            license: None,
            compatibility: None,
            metadata: None,
            allowed_tools: None,
            argument_hint: None,
            disable_model_invocation: None,
            user_invocable: None,
            model: None,
            context: None,
            agent: None,
            effort: None,
            paths: None,
            shell: None,
        };
        assert!(skill.validate_name().is_ok());
    }

    #[test]
    fn test_invalid_skill_name_uppercase() {
        let skill = SkillSchema {
            name: "Code-Review".to_string(),
            description: "Reviews code".to_string(),
            license: None,
            compatibility: None,
            metadata: None,
            allowed_tools: None,
            argument_hint: None,
            disable_model_invocation: None,
            user_invocable: None,
            model: None,
            context: None,
            agent: None,
            effort: None,
            paths: None,
            shell: None,
        };
        assert!(skill.validate_name().is_err());
    }

    #[test]
    fn test_invalid_model() {
        let skill = SkillSchema {
            name: "test".to_string(),
            description: "Test".to_string(),
            license: None,
            compatibility: None,
            metadata: None,
            allowed_tools: None,
            argument_hint: None,
            disable_model_invocation: None,
            user_invocable: None,
            model: Some("gpt-4".to_string()),
            context: None,
            agent: None,
            effort: None,
            paths: None,
            shell: None,
        };
        assert!(skill.validate_model().is_err());
    }

    fn make_skill(name: &str, description: &str) -> SkillSchema {
        SkillSchema {
            name: name.to_string(),
            description: description.to_string(),
            license: None,
            compatibility: None,
            metadata: None,
            allowed_tools: None,
            argument_hint: None,
            disable_model_invocation: None,
            user_invocable: None,
            model: None,
            context: None,
            agent: None,
            effort: None,
            paths: None,
            shell: None,
        }
    }

    #[test]
    fn test_empty_name_rejected() {
        let skill = make_skill("", "Valid description");
        assert!(skill.validate_name().is_err());
    }

    #[test]
    fn test_max_length_name_accepted() {
        // Exactly 64 chars - should be accepted
        let name = "a".repeat(64);
        let skill = make_skill(&name, "Valid description");
        assert!(skill.validate_name().is_ok());
    }

    #[test]
    fn test_over_max_length_name_rejected() {
        // 65 chars - should be rejected
        let name = "a".repeat(65);
        let skill = make_skill(&name, "Valid description");
        assert!(skill.validate_name().is_err());
    }

    #[test]
    fn test_empty_description_rejected() {
        let skill = make_skill("valid-name", "");
        assert!(skill.validate_description().is_err());
    }

    #[test]
    fn test_max_length_description_accepted() {
        let desc = "x".repeat(1024);
        let skill = make_skill("valid-name", &desc);
        assert!(skill.validate_description().is_ok());
    }

    #[test]
    fn test_over_max_length_description_rejected() {
        let desc = "x".repeat(1025);
        let skill = make_skill("valid-name", &desc);
        assert!(skill.validate_description().is_err());
    }

    #[test]
    fn test_empty_compatibility_rejected() {
        let mut skill = make_skill("valid-name", "Valid description");
        skill.compatibility = Some(String::new());
        assert!(skill.validate_compatibility().is_err());
    }

    #[test]
    fn test_over_max_compatibility_rejected() {
        let mut skill = make_skill("valid-name", "Valid description");
        skill.compatibility = Some("x".repeat(501));
        assert!(skill.validate_compatibility().is_err());
    }

    #[test]
    fn test_validate_collects_all_errors() {
        // Multiple invalid fields should all be reported
        let skill = make_skill("", "");
        let errors = skill.validate();
        assert!(
            errors.len() >= 2,
            "Should report errors for both name and description, got: {:?}",
            errors
        );
    }

    // ===== Model Validation =====

    #[test]
    fn test_valid_model_aliases() {
        for model in &["sonnet", "opus", "haiku", "inherit"] {
            let mut skill = make_skill("test", "Test skill");
            skill.model = Some(model.to_string());
            assert!(
                skill.validate_model().is_ok(),
                "Model alias '{}' should be valid",
                model
            );
        }
    }

    #[test]
    fn test_valid_model_full_ids() {
        for model in &[
            "claude-opus-4-6",
            "claude-sonnet-4-6",
            "claude-haiku-4-5-20251001",
            "claude-sonnet-4-5-20250929",
        ] {
            let mut skill = make_skill("test", "Test skill");
            skill.model = Some(model.to_string());
            assert!(
                skill.validate_model().is_ok(),
                "Full model ID '{}' should be valid",
                model
            );
        }
    }

    #[test]
    fn test_invalid_model_not_claude_prefix() {
        let mut skill = make_skill("test", "Test skill");
        skill.model = Some("gemini-pro".to_string());
        assert!(skill.validate_model().is_err());
    }

    #[test]
    fn test_is_valid_skill_model() {
        assert!(is_valid_skill_model("sonnet"));
        assert!(is_valid_skill_model("opus"));
        assert!(is_valid_skill_model("haiku"));
        assert!(is_valid_skill_model("inherit"));
        assert!(is_valid_skill_model("claude-opus-4-6"));
        assert!(is_valid_skill_model("claude-sonnet-4-5-20250929"));
        assert!(!is_valid_skill_model("gpt-4"));
        assert!(!is_valid_skill_model("gemini-pro"));
    }

    // ===== Effort Validation =====

    #[test]
    fn test_valid_effort_values() {
        for effort in &["low", "medium", "high", "max"] {
            let mut skill = make_skill("test", "Test skill");
            skill.effort = Some(effort.to_string());
            assert!(
                skill.validate_effort().is_ok(),
                "Effort '{}' should be valid",
                effort
            );
        }
    }

    #[test]
    fn test_invalid_effort_value() {
        let mut skill = make_skill("test", "Test skill");
        skill.effort = Some("extreme".to_string());
        assert!(skill.validate_effort().is_err());
    }

    #[test]
    fn test_effort_none_ok() {
        let skill = make_skill("test", "Test skill");
        assert!(skill.validate_effort().is_ok());
    }

    // ===== Shell Validation =====

    #[test]
    fn test_valid_shell_values() {
        for shell_val in &["bash", "powershell"] {
            let mut skill = make_skill("test", "Test skill");
            skill.shell = Some(shell_val.to_string());
            assert!(
                skill.validate_shell().is_ok(),
                "Shell '{}' should be valid",
                shell_val
            );
        }
    }

    #[test]
    fn test_invalid_shell_value() {
        let mut skill = make_skill("test", "Test skill");
        skill.shell = Some("zsh".to_string());
        assert!(skill.validate_shell().is_err());
    }

    #[test]
    fn test_shell_none_ok() {
        let skill = make_skill("test", "Test skill");
        assert!(skill.validate_shell().is_ok());
    }

    // ===== Paths Field =====

    #[test]
    fn test_paths_field_stores_string_value() {
        let mut skill = make_skill("test", "Test skill");
        skill.paths = Some(serde_yaml::Value::String(
            "src/**/*.rs, tests/**/*.rs".to_string(),
        ));
        assert!(skill.paths.is_some());
        match &skill.paths {
            Some(serde_yaml::Value::String(s)) => {
                assert_eq!(s, "src/**/*.rs, tests/**/*.rs");
            }
            _ => panic!("Expected String value"),
        }
    }

    #[test]
    fn test_paths_field_stores_sequence_value() {
        let mut skill = make_skill("test", "Test skill");
        skill.paths = Some(serde_yaml::Value::Sequence(vec![
            serde_yaml::Value::String("src/**/*.rs".to_string()),
            serde_yaml::Value::String("tests/**/*.rs".to_string()),
        ]));
        match &skill.paths {
            Some(serde_yaml::Value::Sequence(seq)) => {
                assert_eq!(seq.len(), 2);
            }
            _ => panic!("Expected Sequence value"),
        }
    }

    // ===== Known Frontmatter Fields =====

    #[test]
    fn test_known_fields_include_new_fields() {
        assert!(KNOWN_SKILL_FRONTMATTER_FIELDS.contains(&"effort"));
        assert!(KNOWN_SKILL_FRONTMATTER_FIELDS.contains(&"paths"));
        assert!(KNOWN_SKILL_FRONTMATTER_FIELDS.contains(&"shell"));
    }

    #[test]
    fn test_known_fields_include_existing_fields() {
        for field in &[
            "name",
            "description",
            "model",
            "context",
            "agent",
            "hooks",
            "allowed-tools",
        ] {
            assert!(
                KNOWN_SKILL_FRONTMATTER_FIELDS.contains(field),
                "Known fields should include '{}'",
                field
            );
        }
    }
}