aigent 0.7.1

A library, CLI, and Claude plugin for managing agent skill definitions
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
//! Command file (`.md`) validation.

use std::path::Path;

use crate::diagnostics::{Diagnostic, Severity, K001, K002, K003, K004, K005, K006, K007};

/// Valid model values for commands (no `inherit`).
const VALID_MODELS: &[&str] = &["sonnet", "opus", "haiku"];

/// Common English verbs for the "starts with verb" heuristic (K004).
const COMMON_VERBS: &[&str] = &[
    "add",
    "analyze",
    "apply",
    "build",
    "check",
    "clean",
    "commit",
    "configure",
    "convert",
    "copy",
    "create",
    "debug",
    "delete",
    "deploy",
    "describe",
    "detect",
    "display",
    "download",
    "edit",
    "enable",
    "execute",
    "export",
    "extract",
    "fetch",
    "find",
    "fix",
    "format",
    "generate",
    "get",
    "help",
    "import",
    "initialize",
    "insert",
    "inspect",
    "install",
    "launch",
    "lint",
    "list",
    "load",
    "log",
    "manage",
    "merge",
    "migrate",
    "monitor",
    "move",
    "open",
    "optimize",
    "output",
    "parse",
    "patch",
    "perform",
    "plan",
    "preview",
    "print",
    "process",
    "publish",
    "pull",
    "push",
    "query",
    "read",
    "refactor",
    "reload",
    "remove",
    "rename",
    "repair",
    "replace",
    "report",
    "reset",
    "resolve",
    "restart",
    "restore",
    "retrieve",
    "review",
    "run",
    "save",
    "scan",
    "search",
    "send",
    "serve",
    "set",
    "setup",
    "show",
    "sort",
    "start",
    "stop",
    "submit",
    "summarize",
    "sync",
    "test",
    "trace",
    "transform",
    "trigger",
    "uninstall",
    "update",
    "upgrade",
    "upload",
    "validate",
    "verify",
    "view",
    "watch",
    "write",
];

/// Validate a command `.md` file at the given path.
///
/// Commands have optional frontmatter. Returns a list of diagnostics.
/// Never panics — parse errors are reported as diagnostics.
#[must_use]
pub fn validate_command(path: &Path) -> Vec<Diagnostic> {
    let mut diags = Vec::new();

    let content = match crate::parser::read_file_checked(path) {
        Ok(c) => c,
        Err(e) => {
            diags.push(Diagnostic::new(
                Severity::Error,
                K001,
                format!("cannot read command file: {e}"),
            ));
            return diags;
        }
    };

    // K001: Parse frontmatter (optional — only errors if `---` present but invalid YAML)
    let (metadata, body) = match crate::parser::parse_optional_frontmatter(&content) {
        Ok(result) => result,
        Err(e) => {
            diags.push(Diagnostic::new(
                Severity::Error,
                K001,
                format!("frontmatter syntax error: {e}"),
            ));
            return diags;
        }
    };

    let has_frontmatter = content.starts_with("---");

    // Only validate frontmatter fields that are present
    if has_frontmatter {
        // K002: Description exceeds 60 chars
        if let Some(desc_val) = metadata.get("description") {
            if let Some(desc) = desc_val.as_str() {
                if desc.len() > 60 {
                    diags.push(
                        Diagnostic::new(
                            Severity::Warning,
                            K002,
                            format!("`description` is {} chars (recommended max 60)", desc.len()),
                        )
                        .with_field("description"),
                    );
                }

                // K004: Description should start with a verb
                let first_word = desc.split_whitespace().next().unwrap_or("");
                let first_lower = first_word.to_lowercase();
                if !first_word.is_empty() && !COMMON_VERBS.contains(&first_lower.as_str()) {
                    diags.push(
                        Diagnostic::new(
                            Severity::Warning,
                            K004,
                            format!(
                                "`description` does not start with a verb: \"{first_word}\""
                            ),
                        )
                        .with_field("description")
                        .with_suggestion(
                            "Start with an imperative verb (e.g., \"Run tests\", \"Generate docs\")",
                        ),
                    );
                }
            }
        } else {
            // K007: Missing description (info, not error)
            diags.push(
                Diagnostic::new(
                    Severity::Info,
                    K007,
                    "missing `description` field (recommended for discoverability)",
                )
                .with_field("description"),
            );
        }

        // K003: Model must be one of valid values
        if let Some(model_val) = metadata.get("model") {
            if let Some(model) = model_val.as_str() {
                if !VALID_MODELS.contains(&model) {
                    diags.push(
                        Diagnostic::new(
                            Severity::Error,
                            K003,
                            format!("`model` is not valid: \"{model}\""),
                        )
                        .with_field("model")
                        .with_suggestion(format!("Valid models: {}", VALID_MODELS.join(", "))),
                    );
                }
            }
        }

        // K006: allowed-tools format check
        if let Some(tools_val) = metadata.get("allowed-tools") {
            // allowed-tools should be a sequence of strings
            if let serde_yaml_ng::Value::Sequence(seq) = tools_val {
                for item in seq {
                    if !item.is_string() {
                        diags.push(
                            Diagnostic::new(
                                Severity::Warning,
                                K006,
                                "items in `allowed-tools` must be strings",
                            )
                            .with_field("allowed-tools"),
                        );
                        break;
                    }
                }
            } else if !tools_val.is_string() {
                // Single string is acceptable, but other types are not
                diags.push(
                    Diagnostic::new(
                        Severity::Warning,
                        K006,
                        "`allowed-tools` should be a list of tool names",
                    )
                    .with_field("allowed-tools"),
                );
            }
        }
    }

    // K005: Body must not be empty
    if body.trim().is_empty() {
        diags.push(
            Diagnostic::new(Severity::Error, K005, "command body is empty")
                .with_suggestion("Add the command prompt text after the frontmatter"),
        );
    }

    diags
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    fn write_command(content: &str) -> (tempfile::TempDir, std::path::PathBuf) {
        let dir = tempdir().unwrap();
        let path = dir.path().join("my-command.md");
        fs::write(&path, content).unwrap();
        (dir, path)
    }

    #[test]
    fn valid_command_with_frontmatter() {
        let (_dir, path) = write_command(
            "---\ndescription: Run project tests\nmodel: sonnet\n---\nRun the test suite and report results.\n",
        );
        let diags = validate_command(&path);
        let errors: Vec<_> = diags.iter().filter(|d| d.is_error()).collect();
        assert!(errors.is_empty(), "unexpected errors: {errors:?}");
    }

    #[test]
    fn valid_command_without_frontmatter() {
        let (_dir, path) = write_command("Just a plain command prompt with instructions.\n");
        let diags = validate_command(&path);
        let errors: Vec<_> = diags.iter().filter(|d| d.is_error()).collect();
        assert!(errors.is_empty(), "unexpected errors: {errors:?}");
    }

    #[test]
    fn invalid_yaml_k001() {
        let (_dir, path) = write_command("---\n: bad: yaml\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K001));
    }

    #[test]
    fn long_description_k002() {
        let long_desc = "a".repeat(61);
        let (_dir, path) = write_command(&format!(
            "---\ndescription: {long_desc}\n---\nCommand body text.\n"
        ));
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K002));
    }

    #[test]
    fn description_at_60_no_k002() {
        let desc = "a".repeat(60);
        let (_dir, path) = write_command(&format!(
            "---\ndescription: {desc}\n---\nCommand body text.\n"
        ));
        let diags = validate_command(&path);
        assert!(!diags.iter().any(|d| d.code == K002));
    }

    #[test]
    fn invalid_model_k003() {
        let (_dir, path) =
            write_command("---\nmodel: gpt-4\ndescription: Run tests\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K003));
    }

    #[test]
    fn inherit_model_invalid_for_commands_k003() {
        let (_dir, path) =
            write_command("---\nmodel: inherit\ndescription: Run tests\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K003));
    }

    #[test]
    fn valid_models_no_k003() {
        for model in VALID_MODELS {
            let (_dir, path) = write_command(&format!(
                "---\nmodel: {model}\ndescription: Run tests\n---\nBody text.\n"
            ));
            let diags = validate_command(&path);
            assert!(
                !diags.iter().any(|d| d.code == K003),
                "model {model} should be valid"
            );
        }
    }

    #[test]
    fn description_not_starting_with_verb_k004() {
        let (_dir, path) = write_command("---\ndescription: The project tests\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K004));
    }

    #[test]
    fn description_starting_with_verb_no_k004() {
        let (_dir, path) = write_command("---\ndescription: Run project tests\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(!diags.iter().any(|d| d.code == K004));
    }

    #[test]
    fn empty_body_k005() {
        let (_dir, path) = write_command("---\ndescription: Run tests\n---\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K005));
    }

    #[test]
    fn empty_body_no_frontmatter_k005() {
        let (_dir, path) = write_command("");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K005));
    }

    #[test]
    fn invalid_allowed_tools_k006() {
        let (_dir, path) =
            write_command("---\ndescription: Run tests\nallowed-tools: 42\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K006));
    }

    #[test]
    fn valid_allowed_tools_list() {
        let (_dir, path) = write_command(
            "---\ndescription: Run tests\nallowed-tools:\n  - Bash\n  - Read\n---\nBody text.\n",
        );
        let diags = validate_command(&path);
        assert!(!diags.iter().any(|d| d.code == K006));
    }

    #[test]
    fn missing_description_k007() {
        let (_dir, path) = write_command("---\nmodel: sonnet\n---\nBody text.\n");
        let diags = validate_command(&path);
        assert!(diags.iter().any(|d| d.code == K007));
    }

    #[test]
    fn no_frontmatter_no_k007() {
        // No frontmatter at all — K007 should not fire (it's about missing description
        // when frontmatter IS present)
        let (_dir, path) = write_command("Just command text.\n");
        let diags = validate_command(&path);
        assert!(!diags.iter().any(|d| d.code == K007));
    }

    #[test]
    fn nonexistent_file_returns_k001() {
        let diags = validate_command(Path::new("/nonexistent/command.md"));
        assert!(diags.iter().any(|d| d.code == K001));
    }
}