zag-agent 0.12.4

Core library for zag — a unified interface for AI coding agents
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
use super::*;
use std::fs;
use tempfile::TempDir;

fn make_skill_dir(parent: &Path, name: &str, description: &str, body: &str) -> PathBuf {
    let dir = parent.join(name);
    fs::create_dir_all(&dir).unwrap();
    let content = format!(
        "---\nname: {}\ndescription: {}\n---\n\n{}\n",
        name, description, body
    );
    fs::write(dir.join("SKILL.md"), content).unwrap();
    dir
}

#[test]
fn test_parse_skill_valid() {
    let tmp = TempDir::new().unwrap();
    make_skill_dir(
        tmp.path(),
        "my-skill",
        "Does stuff",
        "# My Skill\n\nHelps you do things.",
    );

    let skill = parse_skill(&tmp.path().join("my-skill")).unwrap();
    assert_eq!(skill.name, "my-skill");
    assert_eq!(skill.description, "Does stuff");
    assert!(skill.body.contains("# My Skill"));
    assert!(skill.body.contains("Helps you do things."));
}

#[test]
fn test_get_skill_found() {
    let tmp = TempDir::new().unwrap();
    make_skill_dir(
        tmp.path(),
        "my-skill",
        "Does stuff",
        "# My Skill\n\nHelps you do things.",
    );

    // get_skill uses skills_dir() which we can't override, so test via parse_skill directly
    let skill = parse_skill(&tmp.path().join("my-skill")).unwrap();
    assert_eq!(skill.name, "my-skill");
    assert_eq!(skill.description, "Does stuff");

    // Verify Serialize works
    let json = serde_json::to_string(&skill).unwrap();
    assert!(json.contains("\"name\":\"my-skill\""));
    assert!(json.contains("\"description\":\"Does stuff\""));
}

#[test]
fn test_parse_skill_no_frontmatter() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path().join("bad-skill");
    fs::create_dir_all(&dir).unwrap();
    fs::write(dir.join("SKILL.md"), "No frontmatter here").unwrap();

    assert!(parse_skill(&dir).is_err());
}

#[test]
fn test_parse_skill_unclosed_frontmatter() {
    let tmp = TempDir::new().unwrap();
    let dir = tmp.path().join("bad-skill");
    fs::create_dir_all(&dir).unwrap();
    fs::write(dir.join("SKILL.md"), "---\nname: test\n").unwrap();

    assert!(parse_skill(&dir).is_err());
}

#[test]
fn test_load_all_skills_empty_dir() {
    let tmp = TempDir::new().unwrap();
    // Simulate empty ~/.zag/skills/ by pointing skills_dir equivalent at tmp
    // Since we can't override skills_dir() easily, test load logic directly
    let skills = load_skills_from(tmp.path()).unwrap();
    assert!(skills.is_empty());
}

#[test]
fn test_load_all_skills_multiple() {
    let tmp = TempDir::new().unwrap();
    make_skill_dir(tmp.path(), "skill-a", "Skill A description", "Body A");
    make_skill_dir(tmp.path(), "skill-b", "Skill B description", "Body B");

    let skills = load_skills_from(tmp.path()).unwrap();
    assert_eq!(skills.len(), 2);
    // Should be sorted by name
    assert_eq!(skills[0].name, "skill-a");
    assert_eq!(skills[1].name, "skill-b");
}

#[test]
fn test_load_all_skills_skips_invalid() {
    let tmp = TempDir::new().unwrap();
    // Valid skill
    make_skill_dir(tmp.path(), "good-skill", "Good", "Body");
    // Invalid: no SKILL.md
    fs::create_dir_all(tmp.path().join("empty-dir")).unwrap();

    let skills = load_skills_from(tmp.path()).unwrap();
    assert_eq!(skills.len(), 1);
    assert_eq!(skills[0].name, "good-skill");
}

#[test]
fn test_format_skills_for_system_prompt_empty() {
    let result = format_skills_for_system_prompt(&[]);
    assert!(result.is_empty());
}

#[test]
fn test_format_skills_for_system_prompt_with_skills() {
    let tmp = TempDir::new().unwrap();
    let dir = make_skill_dir(
        tmp.path(),
        "test-skill",
        "Test description",
        "## Instructions\nDo this.",
    );
    let skill = parse_skill(&dir).unwrap();

    let result = format_skills_for_system_prompt(&[skill]);
    assert!(result.contains("## Agent Skills"));
    assert!(result.contains("### Skill: test-skill"));
    assert!(result.contains("Test description"));
    assert!(result.contains("## Instructions"));
}

#[test]
fn test_sync_skills_for_provider_creates_symlinks() {
    let tmp = TempDir::new().unwrap();
    let skills_src = tmp.path().join("zag-skills");
    let provider_skills = tmp.path().join("provider-skills");
    fs::create_dir_all(&skills_src).unwrap();
    fs::create_dir_all(&provider_skills).unwrap();

    make_skill_dir(&skills_src, "my-skill", "A skill", "Body here");
    let skill = parse_skill(&skills_src.join("my-skill")).unwrap();

    sync_skills_for_provider_to(&provider_skills, &[skill]).unwrap();

    let link = provider_skills.join("zag-my-skill");
    assert!(link.symlink_metadata().is_ok(), "symlink should exist");
    assert!(link.is_dir(), "symlink should resolve to a directory");
    assert!(
        link.join("SKILL.md").exists(),
        "SKILL.md should be accessible through symlink"
    );
}

#[test]
fn test_sync_skills_removes_stale_symlinks() {
    let tmp = TempDir::new().unwrap();
    let skills_src = tmp.path().join("zag-skills");
    let provider_skills = tmp.path().join("provider-skills");
    fs::create_dir_all(&skills_src).unwrap();
    fs::create_dir_all(&provider_skills).unwrap();

    // Create a skill, symlink it, then remove the skill and re-sync with empty list
    make_skill_dir(&skills_src, "old-skill", "Old", "Body");
    let skill = parse_skill(&skills_src.join("old-skill")).unwrap();
    sync_skills_for_provider_to(&provider_skills, &[skill]).unwrap();

    let link = provider_skills.join("zag-old-skill");
    assert!(link.symlink_metadata().is_ok());

    // Re-sync with no skills — stale link should be removed
    sync_skills_for_provider_to(&provider_skills, &[]).unwrap();
    assert!(
        link.symlink_metadata().is_err(),
        "stale symlink should be removed"
    );
}

#[test]
fn test_add_and_remove_skill() {
    let tmp = TempDir::new().unwrap();
    let base = tmp.path().join("agent").join("skills");
    fs::create_dir_all(&base).unwrap();

    // add
    let skill_dir = add_skill_to(&base, "test-skill", "A test skill").unwrap();
    assert!(skill_dir.exists());
    assert!(skill_dir.join("SKILL.md").exists());
    let skill = parse_skill(&skill_dir).unwrap();
    assert_eq!(skill.name, "test-skill");

    // remove
    remove_skill_from(&base, "test-skill", &[]).unwrap();
    assert!(!skill_dir.exists());
}

#[test]
fn test_add_skill_already_exists() {
    let tmp = TempDir::new().unwrap();
    let base = tmp.path().join("skills");
    fs::create_dir_all(&base).unwrap();

    add_skill_to(&base, "dupe", "First").unwrap();
    assert!(add_skill_to(&base, "dupe", "Second").is_err());
}

// Internal helpers that accept a base path for testability

pub(crate) fn load_skills_from(dir: &Path) -> Result<Vec<Skill>> {
    if !dir.exists() {
        return Ok(Vec::new());
    }
    let mut skills = Vec::new();
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if !path.is_dir() {
            continue;
        }
        if path.join("SKILL.md").exists() {
            match parse_skill(&path) {
                Ok(s) => skills.push(s),
                Err(e) => log::warn!("Skipping {}: {}", path.display(), e),
            }
        }
    }
    skills.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(skills)
}

pub(crate) fn sync_skills_for_provider_to(provider_dir: &Path, skills: &[Skill]) -> Result<usize> {
    fs::create_dir_all(provider_dir)?;

    let skill_names: std::collections::HashSet<String> =
        skills.iter().map(|s| s.name.clone()).collect();

    let mut skipped = 0usize;
    for skill in skills {
        // Skip if the provider already has this skill natively (not via our symlink)
        let native_path = provider_dir.join(&skill.name);
        if is_real_dir(&native_path) {
            skipped += 1;
            continue;
        }

        let link_name = format!("{}{}", SKILL_PREFIX, skill.name);
        let link_path = provider_dir.join(&link_name);
        let target = &skill.dir;

        if link_path.symlink_metadata().is_ok() {
            if fs::read_link(&link_path)
                .map(|t| t == *target)
                .unwrap_or(false)
            {
                continue;
            }
            let _ = fs::remove_file(&link_path).or_else(|_| fs::remove_dir(&link_path));
        }

        create_symlink_dir(target, &link_path)?;
    }

    // Remove stale
    if let Ok(entries) = fs::read_dir(provider_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            let file_name = entry.file_name();
            let name = file_name.to_string_lossy();
            if !name.starts_with(SKILL_PREFIX) {
                continue;
            }
            if path.symlink_metadata().is_err() {
                continue;
            }
            let skill_name = name.trim_start_matches(SKILL_PREFIX);
            let should_remove =
                !skill_names.contains(skill_name) || is_real_dir(&provider_dir.join(skill_name));
            if should_remove {
                let _ = fs::remove_file(&path).or_else(|_| fs::remove_dir(&path));
            }
        }
    }

    Ok(skipped)
}

pub(crate) fn add_skill_to(base: &Path, name: &str, description: &str) -> Result<PathBuf> {
    let dir = base.join(name);
    if dir.exists() {
        bail!("Skill '{}' already exists", name);
    }
    fs::create_dir_all(&dir)?;
    let content = format!(
        "---\nname: {}\ndescription: {}\n---\n\n# {}\n\nDescribe what this skill does here.\n",
        name, description, name
    );
    fs::write(dir.join("SKILL.md"), &content)?;
    Ok(dir)
}

pub(crate) fn remove_skill_from(base: &Path, name: &str, provider_dirs: &[&Path]) -> Result<()> {
    let dir = base.join(name);
    if !dir.exists() {
        bail!("Skill '{}' not found", name);
    }
    for provider_dir in provider_dirs {
        let link = provider_dir.join(format!("{}{}", SKILL_PREFIX, name));
        if link.symlink_metadata().is_ok() {
            let _ = fs::remove_file(&link).or_else(|_| fs::remove_dir(&link));
        }
    }
    fs::remove_dir_all(&dir)?;
    Ok(())
}

#[test]
fn test_sync_skills_skips_native_duplicate() {
    let tmp = TempDir::new().unwrap();
    let skills_src = tmp.path().join("zag-skills");
    let provider_skills = tmp.path().join("provider-skills");
    fs::create_dir_all(&skills_src).unwrap();
    fs::create_dir_all(&provider_skills).unwrap();

    // Provider already has "commit" natively
    make_skill_dir(&provider_skills, "commit", "Native commit", "Native body");

    // Agent also has "commit" (imported copy)
    make_skill_dir(&skills_src, "commit", "Imported commit", "Imported body");
    let skill = parse_skill(&skills_src.join("commit")).unwrap();

    let skipped = sync_skills_for_provider_to(&provider_skills, &[skill]).unwrap();

    // agent-commit symlink should NOT be created
    let link = provider_skills.join("zag-commit");
    assert!(
        link.symlink_metadata().is_err(),
        "should not create symlink when native dir exists"
    );
    assert_eq!(skipped, 1);
}

#[test]
fn test_sync_removes_stale_symlink_when_native_exists() {
    let tmp = TempDir::new().unwrap();
    let skills_src = tmp.path().join("zag-skills");
    let provider_skills = tmp.path().join("provider-skills");
    fs::create_dir_all(&skills_src).unwrap();
    fs::create_dir_all(&provider_skills).unwrap();

    // Create a skill and symlink it
    make_skill_dir(&skills_src, "commit", "Commit", "Body");
    let skill = parse_skill(&skills_src.join("commit")).unwrap();
    sync_skills_for_provider_to(&provider_skills, std::slice::from_ref(&skill)).unwrap();

    let link = provider_skills.join("zag-commit");
    assert!(
        link.symlink_metadata().is_ok(),
        "symlink should exist initially"
    );

    // Now add a native "commit" dir (simulating the original existing)
    make_skill_dir(&provider_skills, "commit", "Native commit", "Native body");

    // Re-sync — should remove the stale symlink and skip
    let skipped = sync_skills_for_provider_to(&provider_skills, &[skill]).unwrap();
    assert_eq!(skipped, 1);
    assert!(
        link.symlink_metadata().is_err(),
        "stale symlink should be removed when native dir exists"
    );
}

#[test]
fn test_import_writes_metadata() {
    let tmp = TempDir::new().unwrap();
    let source_dir = tmp.path().join("claude-skills");
    let dest_dir = tmp.path().join("zag-skills");
    fs::create_dir_all(&source_dir).unwrap();
    fs::create_dir_all(&dest_dir).unwrap();

    make_skill_dir(&source_dir, "my-skill", "Test skill", "Do things");

    let source_hash = hash_skill_md(&source_dir.join("my-skill")).unwrap();

    // Use the copy + metadata logic directly
    let src_path = source_dir.join("my-skill");
    let dst_path = dest_dir.join("my-skill");
    copy_dir_all(&src_path, &dst_path).unwrap();
    write_import_metadata(&dst_path, "claude", &source_hash).unwrap();

    let meta = read_import_metadata(&dst_path).unwrap();
    assert_eq!(meta.source_provider, "claude");
    assert_eq!(meta.source_hash, source_hash);
    assert!(!meta.imported_at.is_empty());
}

#[test]
fn test_hash_skill_md_deterministic() {
    let tmp = TempDir::new().unwrap();
    make_skill_dir(tmp.path(), "s1", "Desc", "Body content");

    let h1 = hash_skill_md(&tmp.path().join("s1")).unwrap();
    let h2 = hash_skill_md(&tmp.path().join("s1")).unwrap();
    assert_eq!(h1, h2);
}

#[test]
fn test_hash_skill_md_different_content() {
    let tmp = TempDir::new().unwrap();
    make_skill_dir(tmp.path(), "s1", "Desc1", "Body 1");
    make_skill_dir(tmp.path(), "s2", "Desc2", "Body 2");

    let h1 = hash_skill_md(&tmp.path().join("s1")).unwrap();
    let h2 = hash_skill_md(&tmp.path().join("s2")).unwrap();
    assert_ne!(h1, h2);
}

#[test]
fn test_import_backfills_metadata_for_existing_skills() {
    let tmp = TempDir::new().unwrap();
    let source_dir = tmp.path().join("claude-skills");
    let dest_dir = tmp.path().join("zag-skills");
    fs::create_dir_all(&source_dir).unwrap();
    fs::create_dir_all(&dest_dir).unwrap();

    // Simulate a previously imported skill without metadata
    make_skill_dir(&source_dir, "commit", "Commit skill", "Commit body");
    make_skill_dir(&dest_dir, "commit", "Commit skill", "Commit body");

    // No metadata yet
    assert!(read_import_metadata(&dest_dir.join("commit")).is_none());

    // Run import logic — it should backfill metadata
    // We can't call import_skills directly (it uses provider_skills_dir),
    // so replicate the backfill logic
    let source_path = source_dir.join("commit");
    let dest_path = dest_dir.join("commit");
    if dest_path.exists() && read_import_metadata(&dest_path).is_none() {
        let source_hash = hash_skill_md(&source_path).unwrap();
        write_import_metadata(&dest_path, "claude", &source_hash).unwrap();
    }

    let meta = read_import_metadata(&dest_path).unwrap();
    assert_eq!(meta.source_provider, "claude");
    assert!(!meta.source_hash.is_empty());
}