rtango 0.4.0

Package manager for AI agent skills, agents, and system instruction files
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
use std::fs;
use std::path::{Path, PathBuf};

use tempfile::TempDir;

use rtango::engine::hash_content;
use rtango::spec::io::{load_lock, save_lock};
use rtango::spec::{
    AgentName, Defaults, Deployment, Lock, OnTargetModified, Rule, RuleKind, Source, Spec,
};

// ── Helpers ──────────────────────────────────────────────────────────

fn setup_copilot_skill(root: &Path, name: &str, body: &str) {
    let dir = root.join(format!(".github/skills/{}", name));
    fs::create_dir_all(&dir).unwrap();
    fs::write(dir.join("SKILL.md"), body).unwrap();
}

fn write_spec(root: &Path, spec: &Spec) {
    let yaml = serde_yml::to_string(spec).unwrap();
    fs::create_dir_all(root.join(".rtango")).unwrap();
    fs::write(root.join(".rtango/spec.yaml"), yaml).unwrap();
}

fn make_spec(agents: Vec<&str>, rules: Vec<Rule>) -> Spec {
    make_spec_with_defaults(agents, Defaults::default(), rules)
}

fn make_spec_with_defaults(agents: Vec<&str>, defaults: Defaults, rules: Vec<Rule>) -> Spec {
    Spec {
        version: 1,
        agents: agents.into_iter().map(AgentName::new).collect(),
        defaults,
        rules,
    }
}

fn skill_set_rule(id: &str, path: &str, schema: &str) -> Rule {
    Rule {
        id: id.to_string(),
        source: Source::Local(PathBuf::from(path)),
        schema_agent: AgentName::new(schema),
        on_target_modified: None,
        kind: RuleKind::skill_set(),
    }
}

fn single_skill_rule(id: &str, path: &str, schema: &str) -> Rule {
    Rule {
        id: id.to_string(),
        source: Source::Local(PathBuf::from(path)),
        schema_agent: AgentName::new(schema),
        on_target_modified: None,
        kind: RuleKind::skill(),
    }
}

fn empty_lock() -> Lock {
    Lock {
        version: 1,
        tracked_agents: vec![],
        owners: vec![],
        deployments: vec![],
    }
}

// ── Tests ────────────────────────────────────────────────────────────

#[test]
fn basic_sync_creates_files_and_updates_lock() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec(
        vec!["claude-code"],
        vec![skill_set_rule("skills", ".github/skills", "copilot")],
    );
    write_spec(root, &spec);

    // No lock file yet
    let result = rtango::cmd::sync::exec(root, false, false, None, false);
    assert!(result.is_ok(), "sync failed: {:?}", result.err());

    // Verify target file was created
    let target = root.join(".claude/skills/deploy/SKILL.md");
    assert!(target.exists());
    let content = fs::read_to_string(&target).unwrap();
    assert!(content.contains("Deploy instructions"));

    // Verify lock was written
    let lock = load_lock(root).unwrap();
    assert_eq!(lock.deployments.len(), 1);
    assert_eq!(lock.deployments[0].rule_id, "skills");
    assert_eq!(lock.deployments[0].agent, AgentName::new("claude-code"));
    assert_eq!(lock.tracked_agents, vec![AgentName::new("claude-code")]);
}

#[test]
fn check_mode_does_not_write_files_and_errors_when_not_clean() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec(
        vec!["claude-code"],
        vec![skill_set_rule("skills", ".github/skills", "copilot")],
    );
    write_spec(root, &spec);

    let result = rtango::cmd::sync::exec(root, true, false, None, false);
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("not in sync"));

    // Verify no files were created
    let target = root.join(".claude/skills/deploy/SKILL.md");
    assert!(!target.exists());

    // Verify no lock was written
    assert!(!root.join(".rtango/lock.yaml").exists());
}

#[test]
fn check_mode_returns_ok_when_already_synced() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec(
        vec!["claude-code"],
        vec![skill_set_rule("skills", ".github/skills", "copilot")],
    );
    write_spec(root, &spec);

    // First, do a real sync
    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();

    // Now check mode should succeed
    let result = rtango::cmd::sync::exec(root, true, false, None, false);
    assert!(
        result.is_ok(),
        "check mode failed when synced: {:?}",
        result.err()
    );
}

#[test]
fn force_mode_resolves_conflicts() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec(
        vec!["claude-code"],
        vec![skill_set_rule("skills", ".github/skills", "copilot")],
    );
    write_spec(root, &spec);

    // Pre-create the target file to cause a conflict (no lock entry + file exists)
    let target_dir = root.join(".claude/skills/deploy");
    fs::create_dir_all(&target_dir).unwrap();
    fs::write(target_dir.join("SKILL.md"), "existing content").unwrap();

    // Without force, should fail due to conflict
    let result = rtango::cmd::sync::exec(root, false, false, None, false);
    assert!(result.is_err());

    // With force, should succeed
    let result = rtango::cmd::sync::exec(root, false, true, None, false);
    assert!(result.is_ok(), "force sync failed: {:?}", result.err());

    // Verify the file was overwritten with rendered content
    let content = fs::read_to_string(root.join(".claude/skills/deploy/SKILL.md")).unwrap();
    assert!(content.contains("Deploy instructions"));
    assert!(!content.contains("existing content"));
}

#[test]
fn rule_filter_only_processes_matching_rule() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "alpha", "Alpha body");
    setup_copilot_skill(root, "beta", "Beta body");

    let spec = make_spec(
        vec!["claude-code"],
        vec![
            single_skill_rule("rule-alpha", ".github/skills/alpha", "copilot"),
            single_skill_rule("rule-beta", ".github/skills/beta", "copilot"),
        ],
    );
    write_spec(root, &spec);

    // First, sync everything
    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();

    let lock = load_lock(root).unwrap();
    assert_eq!(lock.deployments.len(), 2);

    // Now modify alpha source
    setup_copilot_skill(root, "alpha", "Alpha updated");

    // Sync only rule-alpha
    rtango::cmd::sync::exec(root, false, false, Some("rule-alpha".into()), false).unwrap();

    // Verify alpha was updated
    let alpha_content = fs::read_to_string(root.join(".claude/skills/alpha/SKILL.md")).unwrap();
    assert!(alpha_content.contains("Alpha updated"));

    // Verify lock still has both entries
    let lock = load_lock(root).unwrap();
    assert_eq!(lock.deployments.len(), 2);

    let has_alpha = lock.deployments.iter().any(|d| d.rule_id == "rule-alpha");
    let has_beta = lock.deployments.iter().any(|d| d.rule_id == "rule-beta");
    assert!(has_alpha, "lock should contain rule-alpha");
    assert!(has_beta, "lock should contain rule-beta");
}

fn system_rule(id: &str, path: &str, schema: &str) -> Rule {
    Rule {
        id: id.to_string(),
        source: Source::Local(PathBuf::from(path)),
        schema_agent: AgentName::new(schema),
        on_target_modified: None,
        kind: RuleKind::System,
    }
}

#[test]
fn system_file_syncs_per_agent_convention_paths() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    // Source is a single markdown file with no frontmatter.
    fs::create_dir_all(root.join("docs")).unwrap();
    fs::write(
        root.join("docs/INSTRUCTIONS.md"),
        "# House rules\n\nBe terse.\n",
    )
    .unwrap();

    let spec = make_spec(
        vec!["claude-code", "codex", "copilot"],
        vec![system_rule(
            "instructions",
            "docs/INSTRUCTIONS.md",
            "claude-code",
        )],
    );
    write_spec(root, &spec);

    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();

    // Each agent gets its own convention path with verbatim content.
    let claude = fs::read_to_string(root.join("CLAUDE.md")).unwrap();
    let codex = fs::read_to_string(root.join("AGENTS.md")).unwrap();
    let copilot = fs::read_to_string(root.join(".github/copilot-instructions.md")).unwrap();

    let expected = "# House rules\n\nBe terse.\n";
    assert_eq!(claude, expected);
    assert_eq!(codex, expected);
    assert_eq!(copilot, expected);

    // No frontmatter was injected.
    assert!(!claude.starts_with("---"));
    assert!(!codex.starts_with("---"));
    assert!(!copilot.starts_with("---"));

    let lock = load_lock(root).unwrap();
    assert_eq!(lock.deployments.len(), 3);
}

#[test]
fn system_file_handles_agents_sharing_target_path() {
    // codex and opencode both write to AGENTS.md — must not conflict.
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    fs::write(root.join("source.md"), "shared body\n").unwrap();
    let spec = make_spec(
        vec!["codex", "opencode"],
        vec![system_rule("sys", "source.md", "codex")],
    );
    write_spec(root, &spec);

    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();
    assert_eq!(
        fs::read_to_string(root.join("AGENTS.md")).unwrap(),
        "shared body\n"
    );

    // Re-sync stays clean despite two lock entries pointing at the same path.
    rtango::cmd::sync::exec(root, true, false, None, false).unwrap();

    let lock = load_lock(root).unwrap();
    assert_eq!(lock.deployments.len(), 2);
}

#[test]
fn system_file_resync_is_clean_after_first_sync() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    fs::write(root.join("source.md"), "body\n").unwrap();
    let spec = make_spec(
        vec!["claude-code"],
        vec![system_rule("sys", "source.md", "claude-code")],
    );
    write_spec(root, &spec);

    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();
    // Re-running in --check mode must succeed (idempotent).
    rtango::cmd::sync::exec(root, true, false, None, false).unwrap();
}

#[test]
fn adopt_mode_adopts_existing_files() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec(
        vec!["claude-code"],
        vec![skill_set_rule("skills", ".github/skills", "copilot")],
    );
    write_spec(root, &spec);

    // Pre-create the target file (untracked — no lock entry)
    let target_dir = root.join(".claude/skills/deploy");
    fs::create_dir_all(&target_dir).unwrap();
    fs::write(target_dir.join("SKILL.md"), "existing content").unwrap();

    // Without adopt or force, should fail (conflict)
    let result = rtango::cmd::sync::exec(root, false, false, None, false);
    assert!(result.is_err());

    // With adopt, should succeed
    let result = rtango::cmd::sync::exec(root, false, false, None, true);
    assert!(result.is_ok(), "adopt sync failed: {:?}", result.err());

    // Verify the file was overwritten
    let content = fs::read_to_string(root.join(".claude/skills/deploy/SKILL.md")).unwrap();
    assert!(content.contains("Deploy instructions"));

    // Verify lock was created
    let lock = load_lock(root).unwrap();
    assert_eq!(lock.deployments.len(), 1);
}

#[test]
fn sync_updates_gitignore_precisely_when_enabled() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec_with_defaults(
        vec!["copilot", "claude-code"],
        Defaults {
            gitignore_targets: true,
            ..Defaults::default()
        },
        vec![single_skill_rule(
            "deploy",
            ".github/skills/deploy",
            "copilot",
        )],
    );
    write_spec(root, &spec);

    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();

    let gitignore = fs::read_to_string(root.join(".gitignore")).unwrap();
    assert!(gitignore.contains("# >>> rtango managed targets >>>"));
    assert!(gitignore.contains(".claude/skills/deploy/"));
    assert!(gitignore.contains(".claude/skills/rtango/"));
    assert!(gitignore.contains(".github/skills/rtango/"));
    assert!(!gitignore.contains(".claude/\n"));
    assert!(!gitignore.contains(".claude/skills/\n"));
    assert!(!gitignore.contains(".github/skills/deploy/"));
}

#[test]
fn check_mode_fails_when_managed_gitignore_is_out_of_date() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "deploy", "Deploy instructions");

    let spec = make_spec_with_defaults(
        vec!["copilot", "claude-code"],
        Defaults {
            gitignore_targets: true,
            ..Defaults::default()
        },
        vec![single_skill_rule(
            "deploy",
            ".github/skills/deploy",
            "copilot",
        )],
    );
    write_spec(root, &spec);

    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();
    fs::write(root.join(".gitignore"), "").unwrap();

    let err = rtango::cmd::sync::exec(root, true, false, None, false).unwrap_err();
    assert!(err.to_string().contains("not in sync"));
}

#[test]
fn rule_filtered_check_ignores_unrelated_gitignore_changes() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "alpha", "Alpha body");

    let initial_spec = make_spec_with_defaults(
        vec!["copilot", "claude-code"],
        Defaults {
            gitignore_targets: true,
            ..Defaults::default()
        },
        vec![single_skill_rule(
            "rule-alpha",
            ".github/skills/alpha",
            "copilot",
        )],
    );
    write_spec(root, &initial_spec);
    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();

    setup_copilot_skill(root, "beta", "Beta body");
    let updated_spec = make_spec_with_defaults(
        vec!["copilot", "claude-code"],
        Defaults {
            gitignore_targets: true,
            ..Defaults::default()
        },
        vec![
            single_skill_rule("rule-alpha", ".github/skills/alpha", "copilot"),
            single_skill_rule("rule-beta", ".github/skills/beta", "copilot"),
        ],
    );
    write_spec(root, &updated_spec);

    rtango::cmd::sync::exec(root, true, false, Some("rule-alpha".into()), false).unwrap();
}

#[test]
fn rule_filtered_sync_does_not_write_gitignore_entries_for_other_rules() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();

    setup_copilot_skill(root, "alpha", "Alpha body");

    let initial_spec = make_spec_with_defaults(
        vec!["copilot", "claude-code"],
        Defaults {
            gitignore_targets: true,
            ..Defaults::default()
        },
        vec![single_skill_rule(
            "rule-alpha",
            ".github/skills/alpha",
            "copilot",
        )],
    );
    write_spec(root, &initial_spec);
    rtango::cmd::sync::exec(root, false, false, None, false).unwrap();

    let gitignore_before = fs::read_to_string(root.join(".gitignore")).unwrap();

    setup_copilot_skill(root, "beta", "Beta body");
    let updated_spec = make_spec_with_defaults(
        vec!["copilot", "claude-code"],
        Defaults {
            gitignore_targets: true,
            ..Defaults::default()
        },
        vec![
            single_skill_rule("rule-alpha", ".github/skills/alpha", "copilot"),
            single_skill_rule("rule-beta", ".github/skills/beta", "copilot"),
        ],
    );
    write_spec(root, &updated_spec);

    rtango::cmd::sync::exec(root, false, false, Some("rule-alpha".into()), false).unwrap();

    let gitignore_after = fs::read_to_string(root.join(".gitignore")).unwrap();
    assert_eq!(gitignore_after, gitignore_before);
    assert!(!root.join(".claude/skills/beta/SKILL.md").exists());
}