mars-agents 0.2.4

Agent package manager for .agents/ directories
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
mod common;

use assert_fs::TempDir;
use assert_fs::prelude::*;
use predicates::prelude::*;
use std::fs;
use toml::Value;

use common::*;

#[test]
fn full_pipeline_with_local_package_and_custom_target() {
    let dir = TempDir::new().unwrap();
    let project = dir.child("project");
    project.create_dir_all().unwrap();

    // 1. Init with custom target (.claude)
    mars()
        .args([
            "init",
            ".claude",
            "--root",
            project.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // 2. Verify mars.toml has [dependencies], not [sources], no init marker
    let config_content = fs::read_to_string(project.child("mars.toml").path()).unwrap();
    assert!(
        config_content.contains("[dependencies]"),
        "should have [dependencies]: {config_content}"
    );
    assert!(
        !config_content.contains("[sources]"),
        "should not have [sources]: {config_content}"
    );
    assert!(
        !config_content.contains("# created by mars"),
        "should not have init marker"
    );

    // 3. Verify init does not modify .gitignore.
    assert!(!project.child(".gitignore").exists());

    // 4. Verify settings.managed_root persisted
    let config: Value = toml::from_str(&config_content).unwrap();
    assert_eq!(
        config["settings"]["managed_root"].as_str(),
        Some(".claude"),
        "managed_root should be persisted"
    );

    // 5. Add [package] section and create local items
    let mut config_str = config_content.clone();
    config_str.push_str("\n[package]\nname = \"test-project\"\nversion = \"1.0.0\"\n");
    fs::write(project.child("mars.toml").path(), &config_str).unwrap();

    // Create local agent and skill
    let local_agents = project.child("agents");
    local_agents.create_dir_all().unwrap();
    fs::write(
        local_agents.child("local-agent.md").path(),
        "# Local Agent\nThis is a local agent.",
    )
    .unwrap();

    let local_skill = project.child("skills").child("local-skill");
    fs::create_dir_all(local_skill.path()).unwrap();
    fs::write(
        local_skill.child("SKILL.md").path(),
        "# Local Skill\nThis is a local skill.",
    )
    .unwrap();

    // 6. Add external dependency
    let source = create_source(
        &dir,
        "ext-source",
        &[("external-agent", "# External Agent")],
        &[],
    );
    mars()
        .args([
            "add",
            source.to_str().unwrap(),
            "--root",
            project.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // 7-10. Verify items exist in target and .mars/ canonical store
    let managed = project.child(".claude");
    let local_agent_target = managed.child("agents").child("local-agent.md");
    let local_skill_target = managed.child("skills").child("local-skill");
    let external_agent = managed.child("agents").child("external-agent.md");

    assert!(
        local_agent_target.path().exists(),
        "local agent should exist"
    );
    assert!(
        local_skill_target.path().exists(),
        "local skill should exist"
    );
    assert!(
        external_agent.path().exists(),
        "external agent should exist"
    );

    // In .mars/ canonical store, local items are regular copied content
    let mars_dir = project.child(".mars");
    let mars_local_agent = mars_dir.child("agents").child("local-agent.md");
    assert!(
        !mars_local_agent
            .path()
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink(),
        "local agent in .mars/ should be a regular file copy"
    );

    // In target directories, ALL items are regular file copies (D26)
    assert!(
        !local_agent_target
            .path()
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink(),
        "local agent in target should be a regular file copy, not a symlink"
    );
    assert!(
        !external_agent
            .path()
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink(),
        "external agent should not be a symlink"
    );

    // 11. Verify lock file has _self entries
    let lock_content = fs::read_to_string(project.child("mars.lock").path()).unwrap();
    assert!(
        lock_content.contains("[dependencies._self]"),
        "lock should have _self dependency: {lock_content}"
    );

    // 12. Verify lock file uses [dependencies.xxx] not [sources.xxx]
    assert!(
        !lock_content.contains("[sources."),
        "lock should not have [sources.]: {lock_content}"
    );

    // 13. Re-run sync — verify idempotent
    mars()
        .args(["sync", "--root", project.path().to_str().unwrap()])
        .assert()
        .success();
    let lock_content_after_resync = fs::read_to_string(project.child("mars.lock").path()).unwrap();
    assert!(
        lock_content_after_resync.contains("[dependencies._self]"),
        "lock should retain _self dependency after re-sync: {lock_content_after_resync}"
    );
    assert!(
        lock_content_after_resync.contains("[items.\"agent/local-agent\"]"),
        "lock should retain local _self agent after re-sync: {lock_content_after_resync}"
    );
    assert!(
        lock_content_after_resync.contains("[items.\"skill/local-skill\"]"),
        "lock should retain local _self skill after re-sync: {lock_content_after_resync}"
    );

    // 14. Re-run init — should be idempotent
    mars()
        .args([
            "init",
            ".claude",
            "--root",
            project.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    // 15. Verify init on package-only mars.toml succeeds
    let pkg_dir = dir.child("pkg-only");
    pkg_dir.create_dir_all().unwrap();
    fs::write(
        pkg_dir.child("mars.toml").path(),
        "[package]\nname = \"pkg\"\nversion = \"1.0.0\"\n",
    )
    .unwrap();
    mars()
        .args(["init", "--root", pkg_dir.path().to_str().unwrap()])
        .assert()
        .success();
}

#[test]
fn sync_prefers_mars_src_local_items_over_repo_root() {
    let dir = TempDir::new().unwrap();
    let project = dir.child("project");
    project.create_dir_all().unwrap();

    mars()
        .args(["init", "--root", project.path().to_str().unwrap()])
        .assert()
        .success();

    fs::write(
        project.child("mars.toml").path(),
        "[dependencies]\n\n[package]\nname = \"pkg\"\nversion = \"1.0.0\"\n",
    )
    .unwrap();

    let legacy_skill = project.child("skills").child("planning");
    legacy_skill.create_dir_all().unwrap();
    legacy_skill
        .child("SKILL.md")
        .write_str("# Legacy")
        .unwrap();

    let preferred_skill = project.child(".mars-src").child("skills").child("planning");
    preferred_skill.create_dir_all().unwrap();
    preferred_skill
        .child("SKILL.md")
        .write_str("# Preferred")
        .unwrap();

    mars()
        .args(["sync", "--root", project.path().to_str().unwrap()])
        .assert()
        .success()
        .stderr(predicate::str::contains("defined in both"))
        .stderr(predicate::str::contains(".mars-src"));

    assert_eq!(
        fs::read_to_string(
            project
                .child(".mars")
                .child("skills")
                .child("planning")
                .child("SKILL.md")
                .path()
        )
        .unwrap(),
        "# Preferred"
    );
}

#[test]
fn adopt_moves_skill_into_mars_src_and_syncs_targets() {
    let dir = TempDir::new().unwrap();
    let project = dir.child("project");
    project.create_dir_all().unwrap();

    mars()
        .args([
            "init",
            ".claude",
            "--link",
            ".agents",
            "--root",
            project.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    let unmanaged_skill = project
        .child(".claude")
        .child("skills")
        .child("local-skill");
    unmanaged_skill.create_dir_all().unwrap();
    unmanaged_skill
        .child("SKILL.md")
        .write_str("# Local skill")
        .unwrap();

    mars()
        .args([
            "adopt",
            unmanaged_skill.path().to_str().unwrap(),
            "--root",
            project.path().to_str().unwrap(),
        ])
        .assert()
        .success()
        .stdout(predicate::str::contains("adopted skill `local-skill`"));

    let local_source_skill = project
        .child(".mars-src")
        .child("skills")
        .child("local-skill")
        .child("SKILL.md");
    assert!(local_source_skill.exists());
    assert_eq!(
        fs::read_to_string(local_source_skill.path()).unwrap(),
        "# Local skill"
    );

    assert!(
        project
            .child(".claude")
            .child("skills")
            .child("local-skill")
            .child("SKILL.md")
            .exists()
    );
    assert!(
        project
            .child(".agents")
            .child("skills")
            .child("local-skill")
            .child("SKILL.md")
            .exists()
    );
    assert!(
        project
            .child(".mars")
            .child("skills")
            .child("local-skill")
            .child("SKILL.md")
            .exists()
    );

    let lock_content = fs::read_to_string(project.child("mars.lock").path()).unwrap();
    assert!(lock_content.contains("[dependencies._self]"));
    assert!(lock_content.contains("[items.\"skill/local-skill\"]"));
}

#[test]
fn sync_reads_mars_src_local_items_without_package_section() {
    let dir = TempDir::new().unwrap();
    let project = dir.child("project");
    project.create_dir_all().unwrap();

    mars()
        .args([
            "init",
            ".claude",
            "--link",
            ".agents",
            "--root",
            project.path().to_str().unwrap(),
        ])
        .assert()
        .success();

    let local_skill = project
        .child(".mars-src")
        .child("skills")
        .child("local-only");
    local_skill.create_dir_all().unwrap();
    local_skill
        .child("SKILL.md")
        .write_str("# Local only")
        .unwrap();

    mars()
        .args(["sync", "--root", project.path().to_str().unwrap()])
        .assert()
        .success();

    assert!(
        project
            .child(".claude")
            .child("skills")
            .child("local-only")
            .child("SKILL.md")
            .exists()
    );
    assert!(
        project
            .child(".agents")
            .child("skills")
            .child("local-only")
            .child("SKILL.md")
            .exists()
    );
    let lock_content = fs::read_to_string(project.child("mars.lock").path()).unwrap();
    assert!(lock_content.contains("[dependencies._self]"));
    assert!(lock_content.contains("[items.\"skill/local-only\"]"));
}

#[test]
fn sync_ignores_repo_root_local_items_without_package_section() {
    let dir = TempDir::new().unwrap();
    let project = dir.child("project");
    project.create_dir_all().unwrap();

    mars()
        .args(["init", "--root", project.path().to_str().unwrap()])
        .assert()
        .success();

    let legacy_skill = project.child("skills").child("legacy-only");
    legacy_skill.create_dir_all().unwrap();
    legacy_skill
        .child("SKILL.md")
        .write_str("# Legacy only")
        .unwrap();

    mars()
        .args(["sync", "--root", project.path().to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::contains("already up to date"));

    assert!(
        !project
            .child(".agents")
            .child("skills")
            .child("legacy-only")
            .child("SKILL.md")
            .exists()
    );
}