roboticus-agent 0.10.0

Agent core with ReAct loop, policy engine, injection defense, memory system, and skill loader
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
//! Skill authoring toolkit — validate, test, and scaffold skill files.
//!
//! Provides pre-persist validation for TOML (structured) and Markdown
//! (instruction) skill files, dry-run trigger testing, and template scaffolding.

use crate::skills::SkillRegistry;
use crate::tools::ToolRegistry;
use roboticus_core::types::{RiskLevel, SkillManifest};

/// Result of validating a skill file before persisting.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SkillValidationResult {
    pub valid: bool,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
}

/// Result of dry-running a skill against a test prompt.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SkillTestResult {
    pub would_match: bool,
    pub matched_triggers: Vec<String>,
}

/// Validate a TOML skill manifest without persisting.
///
/// Checks:
/// 1. All `tool_chain` tool references exist in the ToolRegistry
/// 2. Trigger keywords are specific enough (>= 4 chars, not matching > 50% of existing skills)
/// 3. Risk level is appropriate for the tool chain
/// 4. Required fields are populated
pub fn validate_manifest(
    manifest: &SkillManifest,
    tool_registry: &ToolRegistry,
    skill_registry: &SkillRegistry,
) -> SkillValidationResult {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    // Required field checks
    if manifest.name.trim().is_empty() {
        errors.push("Skill name must not be empty.".to_string());
    }
    if manifest.description.trim().is_empty() {
        errors.push("Skill description must not be empty.".to_string());
    }

    // Tool chain validation
    if let Some(chain) = &manifest.tool_chain {
        let mut chain_risk = RiskLevel::Safe;
        for step in chain {
            if tool_registry.get(&step.tool_name).is_none() {
                errors.push(format!(
                    "Tool '{}' referenced in tool_chain not found in registry.",
                    step.tool_name
                ));
            } else if let Some(tool) = tool_registry.get(&step.tool_name)
                && tool.risk_level() > chain_risk
            {
                chain_risk = tool.risk_level();
            }
        }
        // Risk level appropriateness
        if manifest.risk_level < chain_risk {
            warnings.push(format!(
                "Skill risk_level ({:?}) is lower than the highest tool in chain ({:?}). \
                 Consider raising to {:?}.",
                manifest.risk_level, chain_risk, chain_risk
            ));
        }
    }

    // Trigger specificity
    validate_triggers(&manifest.triggers.keywords, skill_registry, &mut warnings);

    SkillValidationResult {
        valid: errors.is_empty(),
        errors,
        warnings,
    }
}

/// Validate a markdown instruction skill's triggers and content.
pub fn validate_instruction(
    name: &str,
    description: &str,
    keywords: &[String],
    body: &str,
    skill_registry: &SkillRegistry,
) -> SkillValidationResult {
    let mut errors = Vec::new();
    let mut warnings = Vec::new();

    if name.trim().is_empty() {
        errors.push("Skill name must not be empty.".to_string());
    }
    if description.trim().is_empty() {
        errors.push("Skill description must not be empty.".to_string());
    }
    if body.trim().is_empty() {
        errors.push("Instruction body must not be empty.".to_string());
    }

    validate_triggers(keywords, skill_registry, &mut warnings);

    SkillValidationResult {
        valid: errors.is_empty(),
        errors,
        warnings,
    }
}

/// Dry-run a test prompt against the skill registry.
pub fn test_skill_match(prompt: &str, registry: &SkillRegistry) -> SkillTestResult {
    let keywords: Vec<&str> = prompt.split_whitespace().collect();
    let matches = registry.match_skills(&keywords);
    let matched_triggers: Vec<String> = matches.iter().map(|s| s.name().to_string()).collect();
    SkillTestResult {
        would_match: !matched_triggers.is_empty(),
        matched_triggers,
    }
}

/// Generate a TOML skill scaffold from a name, description, and tool list.
pub fn scaffold_toml(name: &str, description: &str, tools: &[String]) -> String {
    let name = escape_toml_string(name);
    let description = escape_toml_string(description);
    let tool_chain = if tools.is_empty() {
        String::new()
    } else {
        let steps: Vec<String> = tools
            .iter()
            .map(|t| format!("  {{ tool = \"{t}\", params = {{}} }}"))
            .collect();
        format!("\ntool_chain = [\n{}\n]\n", steps.join(",\n"))
    };

    let trigger_keywords: Vec<String> = name
        .split(|c: char| !c.is_alphanumeric())
        .filter(|w| w.len() >= 4)
        .map(|w| format!("\"{}\"", w.to_lowercase()))
        .collect();

    format!(
        r#"name = "{name}"
description = "{description}"
kind = "action"
risk_level = "Safe"
version = "0.1.0"
author = "local"

[triggers]
keywords = [{keywords}]
{tool_chain}"#,
        name = name,
        description = description,
        keywords = trigger_keywords.join(", "),
        tool_chain = tool_chain.trim_end(),
    )
}

/// Generate a Markdown instruction skill scaffold.
pub fn scaffold_markdown(name: &str, description: &str) -> String {
    let name = escape_toml_string(name);
    let description = escape_toml_string(description);
    let trigger_keywords: Vec<String> = name
        .split(|c: char| !c.is_alphanumeric())
        .filter(|w| w.len() >= 4)
        .map(|w| format!("\"{}\"", w.to_lowercase()))
        .collect();

    format!(
        r#"---
name: {name}
description: {description}
triggers:
  keywords: [{keywords}]
priority: 5
---

# {name}

Describe what this skill does and how the agent should behave when it activates.
"#,
        name = name,
        description = description,
        keywords = trigger_keywords.join(", "),
    )
}

/// Escape a string for safe inclusion in TOML quoted values.
fn escape_toml_string(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

fn validate_triggers(
    keywords: &[String],
    skill_registry: &SkillRegistry,
    warnings: &mut Vec<String>,
) {
    if keywords.is_empty() {
        warnings.push("No trigger keywords defined — skill will never activate.".to_string());
        return;
    }

    for kw in keywords {
        if kw.len() < 4 {
            warnings.push(format!(
                "Trigger keyword '{}' is very short (<4 chars) and may cause false activations.",
                kw
            ));
        }
    }

    // Check overlap with existing skills
    let kw_refs: Vec<&str> = keywords.iter().map(|s| s.as_str()).collect();
    let overlapping = skill_registry.match_skills(&kw_refs);
    if overlapping.len() > 5 {
        warnings.push(format!(
            "Triggers overlap with {} existing skills — consider more specific keywords.",
            overlapping.len()
        ));
    }
}

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

    #[test]
    fn validate_empty_name_is_error() {
        let result =
            validate_instruction("", "desc", &["kw".into()], "body", &SkillRegistry::new());
        assert!(!result.valid);
        assert!(result.errors.iter().any(|e| e.contains("name")));
    }

    #[test]
    fn validate_empty_body_is_error() {
        let result =
            validate_instruction("test", "desc", &["kw".into()], "", &SkillRegistry::new());
        assert!(!result.valid);
        assert!(result.errors.iter().any(|e| e.contains("body")));
    }

    #[test]
    fn validate_no_triggers_warns() {
        let result = validate_instruction("test", "desc", &[], "body text", &SkillRegistry::new());
        assert!(result.valid);
        assert!(result.warnings.iter().any(|w| w.contains("No trigger")));
    }

    #[test]
    fn validate_short_trigger_warns() {
        let result = validate_instruction(
            "test",
            "desc",
            &["ab".into()],
            "body text",
            &SkillRegistry::new(),
        );
        assert!(result.valid);
        assert!(result.warnings.iter().any(|w| w.contains("short")));
    }

    #[test]
    fn test_skill_match_no_registry() {
        let result = test_skill_match("hello world", &SkillRegistry::new());
        assert!(!result.would_match);
        assert!(result.matched_triggers.is_empty());
    }

    #[test]
    fn scaffold_toml_roundtrip() {
        let toml = scaffold_toml("my-skill", "Does things", &["read_file".into()]);
        assert!(toml.contains("name = \"my-skill\""));
        assert!(toml.contains("read_file"));
        assert!(toml.contains("kind = \"action\""));
    }

    #[test]
    fn scaffold_markdown_roundtrip() {
        let md = scaffold_markdown("my-skill", "Does things");
        assert!(md.contains("name: my-skill"));
        assert!(md.contains("# my-skill"));
    }

    // --- Stub tool for manifest validation tests ---
    struct StubTool {
        tool_name: String,
        tool_risk: RiskLevel,
    }

    #[async_trait::async_trait]
    impl crate::tools::Tool for StubTool {
        fn name(&self) -> &str {
            &self.tool_name
        }
        fn description(&self) -> &str {
            "stub"
        }
        fn risk_level(&self) -> RiskLevel {
            self.tool_risk
        }
        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({})
        }
        async fn execute(
            &self,
            _params: serde_json::Value,
            _ctx: &crate::tools::ToolContext,
        ) -> std::result::Result<crate::tools::ToolResult, crate::tools::ToolError> {
            Ok(crate::tools::ToolResult {
                output: "ok".into(),
                metadata: None,
            })
        }
    }

    fn make_manifest(
        name: &str,
        tools: &[(&str, RiskLevel)],
        risk: RiskLevel,
    ) -> (roboticus_core::types::SkillManifest, ToolRegistry) {
        use roboticus_core::types::{SkillKind, SkillTrigger, ToolChainStep};
        let mut registry = ToolRegistry::new();
        let tool_chain: Vec<ToolChainStep> = tools
            .iter()
            .map(|(t, r)| {
                registry.register(Box::new(StubTool {
                    tool_name: t.to_string(),
                    tool_risk: *r,
                }));
                ToolChainStep {
                    tool_name: t.to_string(),
                    params: serde_json::json!({}),
                }
            })
            .collect();
        let manifest = roboticus_core::types::SkillManifest {
            name: name.to_string(),
            description: format!("Test: {name}"),
            kind: SkillKind::Structured,
            triggers: SkillTrigger {
                keywords: vec!["testing".to_string()],
                tool_names: vec![],
                regex_patterns: vec![],
            },
            priority: 5,
            tool_chain: if tool_chain.is_empty() {
                None
            } else {
                Some(tool_chain)
            },
            policy_overrides: None,
            script_path: None,
            risk_level: risk,
            version: "0.1.0".to_string(),
            author: "test".to_string(),
        };
        (manifest, registry)
    }

    #[test]
    fn validate_manifest_missing_tools_errors() {
        use roboticus_core::types::{SkillKind, SkillTrigger, ToolChainStep};
        let manifest = roboticus_core::types::SkillManifest {
            name: "test-skill".to_string(),
            description: "desc".to_string(),
            kind: SkillKind::Structured,
            triggers: SkillTrigger {
                keywords: vec!["testing".to_string()],
                tool_names: vec![],
                regex_patterns: vec![],
            },
            priority: 5,
            tool_chain: Some(vec![ToolChainStep {
                tool_name: "nonexistent_tool".to_string(),
                params: serde_json::json!({}),
            }]),
            policy_overrides: None,
            script_path: None,
            risk_level: RiskLevel::Safe,
            version: "0.1.0".to_string(),
            author: "test".to_string(),
        };
        let empty_tools = ToolRegistry::new();
        let result = validate_manifest(&manifest, &empty_tools, &SkillRegistry::new());
        assert!(!result.valid);
        assert!(
            result
                .errors
                .iter()
                .any(|e| e.contains("nonexistent_tool") && e.contains("not found"))
        );
    }

    #[test]
    fn validate_manifest_risk_level_warning() {
        let (manifest, registry) = make_manifest(
            "risky",
            &[("cautious_tool", RiskLevel::Caution)],
            RiskLevel::Safe,
        );
        let result = validate_manifest(&manifest, &registry, &SkillRegistry::new());
        assert!(result.valid); // warnings don't make it invalid
        assert!(
            result.warnings.iter().any(|w| w.contains("risk_level")),
            "should warn about risk mismatch: {:?}",
            result.warnings
        );
    }

    #[test]
    fn validate_manifest_valid_passes() {
        let (manifest, registry) = make_manifest(
            "good-skill",
            &[("safe_tool", RiskLevel::Safe)],
            RiskLevel::Safe,
        );
        let result = validate_manifest(&manifest, &registry, &SkillRegistry::new());
        assert!(result.valid);
        assert!(result.errors.is_empty());
    }

    #[test]
    fn scaffold_toml_no_tools() {
        let toml = scaffold_toml("my-skill", "Does things", &[]);
        assert!(!toml.contains("tool_chain"));
    }

    #[test]
    fn scaffold_toml_skips_short_words() {
        let toml = scaffold_toml("a-big-test", "Does things", &[]);
        // "a" (1 char) and "big" (3 chars) are < 4 chars, so only "test" should be a keyword
        assert!(toml.contains("\"test\""));
        assert!(!toml.contains("\"a\""));
        assert!(!toml.contains("\"big\""));
    }
}