tinyharness-lib 0.5.6

Core library for TinyHarness: LLM providers, tools, sessions and context management
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

/// Maximum size for a skill content (characters).
/// Skills exceeding this are truncated with a notice.
const SKILL_MAX_CHARS: usize = 10_000;

/// A skill discovered from a SKILL.md file.
///
/// Skills are reusable instruction sets that can be invoked by the user
/// (via `/skill <name>`) or automatically by the model (via the `invoke_skill`
/// signal tool). They live in `<config_dir>/skills/<name>/SKILL.md` (personal,
/// e.g. `~/.config/tinyharness/skills/`), the legacy `~/.tinyharness/skills/`,
/// or `.tinyharness/skills/<name>/SKILL.md` (project-local).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Skill {
    /// The skill name (from frontmatter `name` field or directory name).
    pub name: String,
    /// A short description of when and how to use this skill.
    pub description: String,
    /// Optional hint shown to the user about what argument to pass (e.g. "file path to review").
    pub argument_hint: Option<String>,
    /// Optional compatibility string (e.g. "rust", "python", "any").
    pub compatibility: Option<String>,
    /// If true, the model may NOT auto-invoke this skill; only manual `/skill` invocation is allowed.
    pub disable_model_invocation: bool,
    /// Optional license identifier (e.g. "MIT", "Apache-2.0").
    pub license: Option<String>,
    /// Optional arbitrary metadata as key-value pairs.
    pub metadata: Option<HashMap<String, String>>,
    /// If true, the skill can be invoked by the user via `/skill <name>`.
    pub user_invocable: bool,
    /// The full markdown content of the SKILL.md file (after frontmatter).
    pub content: String,
    /// The filesystem path where this skill was found.
    pub path: PathBuf,
    /// Whether this is a personal (home) or project-local skill.
    pub source: SkillSource,
}

/// Where a skill was discovered from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SkillSource {
    /// `<config_dir>/skills/<name>/SKILL.md` (personal), or the legacy
    /// `~/.tinyharness/skills/<name>/SKILL.md`
    Personal,
    /// `.tinyharness/skills/<name>/SKILL.md` (in project directory)
    Project,
}

/// Parsed frontmatter fields from a SKILL.md file.
///
/// Supported fields (compatible with VS Code agent skills):
/// - `name` — skill name (defaults to directory name)
/// - `description` — short description
/// - `argument-hint` — hint about what argument to pass
/// - `compatibility` — compatibility info (e.g. "rust", "python")
/// - `disable-model-invocation` — if true, model cannot auto-invoke
/// - `license` — license identifier
/// - `metadata` — arbitrary key-value pairs (indented block)
/// - `user-invocable` — if true, user can invoke via `/skill <name>`
struct ParsedFrontmatter {
    name: Option<String>,
    description: Option<String>,
    argument_hint: Option<String>,
    compatibility: Option<String>,
    disable_model_invocation: bool,
    license: Option<String>,
    metadata: Option<HashMap<String, String>>,
    user_invocable: bool,
}

/// Parse simple YAML-like frontmatter (key: value pairs with optional
/// indented metadata block). This avoids pulling in serde_yaml for what
/// is essentially flat `key: value` data.
fn parse_frontmatter(text: &str) -> ParsedFrontmatter {
    let mut name = None;
    let mut description = None;
    let mut argument_hint = None;
    let mut compatibility = None;
    let mut disable_model_invocation = false;
    let mut license = None;
    let mut user_invocable = true;
    let mut metadata: Option<HashMap<String, String>> = None;

    let lines: Vec<&str> = text.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];

        // Skip blank lines
        if line.trim().is_empty() {
            i += 1;
            continue;
        }

        // Check for indented line — metadata sub-properties
        if line.starts_with("  ") || line.starts_with("\t") {
            // This is a continuation of a metadata block
            if let Some(ref mut map) = metadata {
                let trimmed = line.trim();
                if let Some((k, v)) = trimmed.split_once(':') {
                    map.insert(k.trim().to_string(), v.trim().trim_matches('"').to_string());
                }
            }
            i += 1;
            continue;
        }

        // Top-level key: value
        if let Some((key, value)) = line.split_once(':') {
            let key = key.trim();
            let value = value.trim();

            match key {
                "name" => name = Some(value.trim_matches('"').to_string()),
                "description" => description = Some(value.trim_matches('"').to_string()),
                "argument-hint" => argument_hint = Some(value.trim_matches('"').to_string()),
                "compatibility" => compatibility = Some(value.trim_matches('"').to_string()),
                "disable-model-invocation" => disable_model_invocation = value == "true",
                "license" => license = Some(value.trim_matches('"').to_string()),
                "user-invocable" => user_invocable = value == "true" || value.is_empty(),
                "metadata" => {
                    // Start a metadata block; value after "metadata:" is ignored
                    metadata.get_or_insert_with(HashMap::new);
                }
                _ => {} // ignore unknown keys
            }
        }

        i += 1;
    }

    ParsedFrontmatter {
        name,
        description,
        argument_hint,
        compatibility,
        disable_model_invocation,
        license,
        metadata,
        user_invocable,
    }
}

/// Parse a SKILL.md file into a `Skill`.
///
/// The file format is:
/// ```markdown
/// ---
/// name: my-skill
/// description: A short description of what this skill does
/// argument-hint: file path to review
/// compatibility: rust
/// disable-model-invocation: false
/// license: MIT
/// metadata:
///   version: "1.0"
/// user-invocable: true
/// ---
///
/// # My Skill
///
/// Instructions for the skill go here...
/// ```
///
/// The frontmatter is optional. If missing, the skill name is derived from
/// the directory name and description defaults to an empty string.
pub fn parse_skill_md(content: &str, path: &Path, source: SkillSource) -> Result<Skill, String> {
    let (frontmatter, body) = extract_frontmatter(content);

    let fm = frontmatter.map(|s| parse_frontmatter(&s));

    // Name: frontmatter > directory name
    let name = fm
        .as_ref()
        .and_then(|f| f.name.clone())
        .or_else(|| {
            path.parent()
                .and_then(|p| p.file_name())
                .map(|n| n.to_string_lossy().to_string())
        })
        .ok_or_else(|| format!("Cannot determine skill name for {}", path.display()))?;

    let description = fm
        .as_ref()
        .and_then(|f| f.description.clone())
        .unwrap_or_default();
    let argument_hint = fm.as_ref().and_then(|f| f.argument_hint.clone());
    let compatibility = fm.as_ref().and_then(|f| f.compatibility.clone());
    let disable_model_invocation = fm
        .as_ref()
        .map(|f| f.disable_model_invocation)
        .unwrap_or(false);
    let license = fm.as_ref().and_then(|f| f.license.clone());
    let metadata = fm.as_ref().and_then(|f| f.metadata.clone());
    let user_invocable = fm.as_ref().map(|f| f.user_invocable).unwrap_or(true);

    let content = truncate_skill_content(body.trim(), &name);

    Ok(Skill {
        name,
        description,
        argument_hint,
        compatibility,
        disable_model_invocation,
        license,
        metadata,
        user_invocable,
        content,
        path: path.to_path_buf(),
        source,
    })
}

/// Extract YAML frontmatter from a markdown file.
/// Returns (Some(frontmatter_str), body_str) if frontmatter exists,
/// or (None, full_content) if no frontmatter is found.
fn extract_frontmatter(content: &str) -> (Option<String>, String) {
    let trimmed = content.trim_start();
    if !trimmed.starts_with("---") {
        return (None, content.to_string());
    }

    // Find the closing ---
    let after_first = &trimmed[3..]; // skip opening ---
    // Skip whitespace after opening ---
    let after_first = after_first.trim_start();

    if let Some(end_pos) = after_first.find("\n---") {
        let yaml_content = after_first[..end_pos].to_string();
        // Body starts after the closing ---
        let body_start = end_pos + 4; // skip "\n---"
        let body = after_first[body_start..].to_string();
        (Some(yaml_content), body)
    } else if let Some(end_pos) = after_first.find("---") {
        let yaml_content = after_first[..end_pos].to_string();
        let body_start = end_pos + 3; // skip "---"
        let body = after_first[body_start..].to_string();
        (Some(yaml_content), body)
    } else {
        (None, content.to_string())
    }
}

/// Truncate skill content that exceeds the maximum size.
fn truncate_skill_content(content: &str, name: &str) -> String {
    if content.len() <= SKILL_MAX_CHARS {
        return content.to_string();
    }

    let head_ratio = 0.70;
    let head_end = (SKILL_MAX_CHARS as f64 * head_ratio) as usize;
    let tail_size = SKILL_MAX_CHARS - head_end;

    let head = &content[..content.floor_char_boundary(head_end)];
    let tail_start = content.len().saturating_sub(tail_size);
    let tail = &content[content.floor_char_boundary(tail_start)..];

    format!(
        "{head}\n\n[...truncated skill '{name}': showing first {head_end} + last {tail_size} chars. Use the read tool to view the full file.]\n\n{tail}"
    )
}

/// Personal skills directory: `<config_dir>/skills` (e.g.
/// `~/.config/tinyharness/skills`).
///
/// Returns the *primary* location. The legacy `~/.tinyharness/skills` is also
/// searched by [`discover_skills`] — use [`personal_skills_dirs`] to see the
/// full ordered list.
pub fn personal_skills_dir() -> PathBuf {
    crate::paths::Paths::default()
        .personal_skills_dirs()
        .into_iter()
        .next()
        .expect("personal_skills_dirs is never empty")
}

/// All personal skill directories, in decreasing precedence order.
///
/// Resolved from the ambient environment — see
/// [`Paths::personal_skills_dirs`](crate::paths::Paths::personal_skills_dirs)
/// for the injectable form.
pub fn personal_skills_dirs() -> Vec<PathBuf> {
    crate::paths::Paths::default().personal_skills_dirs()
}

/// Project-local skills directory: `.tinyharness/skills/` (relative to CWD)
pub fn project_skills_dir() -> PathBuf {
    crate::paths::Paths::default().project_skills_dir()
}

/// Discover all skills from the personal and project directories.
///
/// Resolved from the ambient environment and process CWD — use
/// [`discover_skills_in`] when the caller has a
/// [`Paths`](crate::paths::Paths).
pub fn discover_skills() -> Vec<Skill> {
    discover_skills_in(&crate::paths::Paths::default())
}

/// Discover all skills for an injected set of paths.
///
/// Scans `<dir>/*/SKILL.md` in each personal directory returned by
/// [`Paths::personal_skills_dirs`](crate::paths::Paths::personal_skills_dirs),
/// then the project directory. Precedence,
/// highest last (a later source overrides an earlier one on name collision):
///
/// 1. `<config_dir>/skills` (documented XDG location)
/// 2. `~/.tinyharness/skills` (legacy location, still supported)
/// 3. `<project_dir>/.tinyharness/skills`
pub fn discover_skills_in(paths: &crate::paths::Paths) -> Vec<Skill> {
    let mut skills = Vec::new();

    // Personal skills: earlier directories win, so the XDG location takes
    // precedence over the legacy dot-directory.
    for personal_dir in paths.personal_skills_dirs() {
        if let Ok(entries) = discover_skills_from_dir(&personal_dir, SkillSource::Personal) {
            for skill in entries {
                if !skills.iter().any(|s: &Skill| s.name == skill.name) {
                    skills.push(skill);
                }
            }
        }
    }

    // Project skills override personal skills with the same name.
    let project_dir = paths.project_skills_dir();
    if let Ok(entries) = discover_skills_from_dir(&project_dir, SkillSource::Project) {
        for skill in entries {
            if let Some(pos) = skills.iter().position(|s| s.name == skill.name) {
                skills[pos] = skill;
            } else {
                skills.push(skill);
            }
        }
    }

    // Sort alphabetically by name
    skills.sort_by(|a, b| a.name.cmp(&b.name));
    skills
}

/// Discover skills from a specific directory.
fn discover_skills_from_dir(dir: &Path, source: SkillSource) -> Result<Vec<Skill>, std::io::Error> {
    let mut skills = Vec::new();

    if !dir.is_dir() {
        return Ok(skills);
    }

    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if !path.is_dir() {
            continue;
        }

        let skill_file = path.join("SKILL.md");
        if !skill_file.is_file() {
            continue;
        }

        let content = match std::fs::read_to_string(&skill_file) {
            Ok(c) => c,
            Err(e) => {
                tracing::warn!("Failed to read skill file {}: {e}", skill_file.display(),);
                continue;
            }
        };

        match parse_skill_md(&content, &skill_file, source) {
            Ok(skill) => skills.push(skill),
            Err(e) => {
                tracing::warn!("Failed to parse skill file {}: {e}", skill_file.display(),);
                continue;
            }
        }
    }

    Ok(skills)
}

/// A registry of discovered skills, used by the agent loop and commands.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkillRegistry {
    pub skills: Vec<Skill>,
}

impl SkillRegistry {
    /// Discover and build a registry of all available skills.
    ///
    /// Resolved from the ambient environment — use [`SkillRegistry::discover_in`]
    /// when the caller has a [`Paths`](crate::paths::Paths).
    pub fn discover() -> Self {
        Self::discover_in(&crate::paths::Paths::default())
    }

    /// Discover and build a registry for an injected set of paths.
    pub fn discover_in(paths: &crate::paths::Paths) -> Self {
        SkillRegistry {
            skills: discover_skills_in(paths),
        }
    }

    /// Look up a skill by name (case-insensitive).
    pub fn get(&self, name: &str) -> Option<&Skill> {
        let name_lower = name.to_lowercase();
        self.skills
            .iter()
            .find(|s| s.name.to_lowercase() == name_lower)
    }

    /// Get all skills that can be auto-invoked by the model.
    pub fn auto_invocable_skills(&self) -> Vec<&Skill> {
        self.skills
            .iter()
            .filter(|s| !s.disable_model_invocation)
            .collect()
    }

    /// Format the skill index for injection into the system prompt.
    ///
    /// Returns a string listing all available skills with names and descriptions,
    /// and compatibility/argument hints for auto-invocable skills.
    pub fn format_index_for_prompt(&self) -> String {
        if self.skills.is_empty() {
            return String::new();
        }

        let mut lines = Vec::new();
        lines.push("## Available Skills\n".to_string());
        lines.push("You can invoke a skill using the `invoke_skill` tool with the skill name. Skills provide specialized instructions for specific tasks.\n".to_string());

        for skill in &self.skills {
            let mut parts = vec![format!("**{}**", skill.name)];

            if !skill.description.is_empty() {
                parts.push(format!(": {}", skill.description));
            }

            if let Some(hint) = &skill.argument_hint {
                parts.push(format!(" (arg: {})", hint));
            }

            if let Some(compat) = &skill.compatibility {
                parts.push(format!(" [{}]", compat));
            }

            if skill.disable_model_invocation {
                parts.push(" _(manual invocation only)_".to_string());
            }

            lines.push(format!("- {}", parts.join("")));
        }

        lines.push("\nUse `invoke_skill` to activate a skill. The skill's full instructions will be injected into the conversation.".to_string());
        lines.join("\n")
    }

    /// Format the full content of a skill for injection into the system prompt
    /// when the skill is active.
    pub fn format_skill_content(&self, skill: &Skill) -> String {
        format!(
            "## Active Skill: {}\n\n{}\n\n---\nSkill instructions:\n{}",
            skill.name, skill.description, skill.content
        )
    }
}

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

    #[test]
    fn test_parse_skill_md_with_frontmatter() {
        let content = r#"---
name: test-skill
description: A test skill for unit tests
argument-hint: file path to test
compatibility: rust
disable-model-invocation: false
license: MIT
metadata:
  version: "1.0"
user-invocable: true
---

# Test Skill

This is the body of the test skill.
"#;
        let dir = tempfile::tempdir().unwrap();
        let skill_path = dir.path().join("test-skill").join("SKILL.md");
        fs::create_dir_all(skill_path.parent().unwrap()).unwrap();
        fs::write(&skill_path, content).unwrap();

        let skill = parse_skill_md(content, &skill_path, SkillSource::Personal).unwrap();
        assert_eq!(skill.name, "test-skill");
        assert_eq!(skill.description, "A test skill for unit tests");
        assert_eq!(skill.argument_hint, Some("file path to test".to_string()));
        assert_eq!(skill.compatibility, Some("rust".to_string()));
        assert!(!skill.disable_model_invocation);
        assert_eq!(skill.license, Some("MIT".to_string()));
        assert!(skill.user_invocable);
        assert!(skill.content.contains("# Test Skill"));
        assert!(skill.content.contains("body of the test skill"));
    }

    #[test]
    fn test_parse_skill_md_without_frontmatter() {
        let content = "# My Skill\n\nThis is a skill without frontmatter.";
        let dir = tempfile::tempdir().unwrap();
        let skill_path = dir.path().join("my-skill").join("SKILL.md");
        fs::create_dir_all(skill_path.parent().unwrap()).unwrap();

        let skill = parse_skill_md(content, &skill_path, SkillSource::Project).unwrap();
        assert_eq!(skill.name, "my-skill"); // Falls back to directory name
        assert_eq!(skill.description, ""); // Defaults to empty
        assert!(skill.argument_hint.is_none());
        assert!(skill.compatibility.is_none());
        assert!(!skill.disable_model_invocation);
        assert!(skill.user_invocable); // Defaults to true
    }

    #[test]
    fn test_parse_skill_md_minimal_frontmatter() {
        let content = "---\nname: minimal\n---\n\nJust the name, no description.";
        let dir = tempfile::tempdir().unwrap();
        let skill_path = dir.path().join("minimal").join("SKILL.md");
        fs::create_dir_all(skill_path.parent().unwrap()).unwrap();

        let skill = parse_skill_md(content, &skill_path, SkillSource::Personal).unwrap();
        assert_eq!(skill.name, "minimal");
        assert_eq!(skill.description, "");
    }

    #[test]
    fn test_extract_frontmatter() {
        let content = "---\nname: test\n---\nBody text";
        let (fm, body) = extract_frontmatter(content);
        assert!(fm.is_some());
        assert!(body.contains("Body text"));
    }

    #[test]
    fn test_extract_frontmatter_none() {
        let content = "No frontmatter here, just text.";
        let (fm, body) = extract_frontmatter(content);
        assert!(fm.is_none());
        assert_eq!(body, content);
    }

    #[test]
    fn test_parse_frontmatter_with_metadata() {
        let fm = parse_frontmatter("metadata:\n  version: \"1.0\"\n  author: test");
        assert!(fm.metadata.is_some());
        let meta = fm.metadata.unwrap();
        assert_eq!(meta.get("version").unwrap(), "1.0");
        assert_eq!(meta.get("author").unwrap(), "test");
    }

    #[test]
    fn test_parse_frontmatter_boolean_fields() {
        let fm = parse_frontmatter("disable-model-invocation: true\nuser-invocable: false");
        assert!(fm.disable_model_invocation);
        assert!(!fm.user_invocable);
    }

    #[test]
    fn test_parse_frontmatter_defaults() {
        let fm = parse_frontmatter("name: hello");
        assert_eq!(fm.name, Some("hello".to_string()));
        assert!(!fm.disable_model_invocation); // defaults to false
        assert!(fm.user_invocable); // defaults to true
        assert!(fm.description.is_none());
        assert!(fm.argument_hint.is_none());
        assert!(fm.metadata.is_none());
    }

    #[test]
    fn test_skill_registry_format_index() {
        let skill = Skill {
            name: "rust-dev".to_string(),
            description: "Rust development best practices".to_string(),
            argument_hint: Some("Rust file or module".to_string()),
            compatibility: Some("rust".to_string()),
            disable_model_invocation: false,
            license: None,
            metadata: None,
            user_invocable: true,
            content: "Instructions...".to_string(),
            path: PathBuf::from("/test/SKILL.md"),
            source: SkillSource::Personal,
        };

        let registry = SkillRegistry {
            skills: vec![skill],
        };

        let index = registry.format_index_for_prompt();
        assert!(index.contains("rust-dev"));
        assert!(index.contains("invoke_skill"));
        assert!(index.contains("arg:"));
        assert!(index.contains("[rust]"));
    }

    #[test]
    fn test_skill_registry_format_disabled() {
        let skill = Skill {
            name: "manual-only".to_string(),
            description: "Manual invocation only".to_string(),
            argument_hint: None,
            compatibility: None,
            disable_model_invocation: true,
            license: None,
            metadata: None,
            user_invocable: true,
            content: "Instructions...".to_string(),
            path: PathBuf::from("/test/SKILL.md"),
            source: SkillSource::Personal,
        };

        let registry = SkillRegistry {
            skills: vec![skill],
        };

        let index = registry.format_index_for_prompt();
        assert!(index.contains("manual invocation only"));
    }
}