mars-agents 0.6.2

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
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
use crate::types::managed_cmd;
use std::path::PathBuf;

/// Config-level errors
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("config file not found: {path}")]
    NotFound { path: PathBuf },

    #[error(
        "no mars.toml found from {} to filesystem root. Run `{cmd}` first.",
        start.display(),
        cmd = managed_cmd("mars init"),
    )]
    ProjectRootNotFound { start: PathBuf },

    #[error("invalid config: {message}")]
    Invalid { message: String },

    #[error("source `{name}` uses both agents/skills and exclude — pick one")]
    ConflictingFilters { name: String },

    #[error("parse error: {0}")]
    Parse(#[from] toml::de::Error),

    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

/// Lock file errors
#[derive(Debug, thiserror::Error)]
pub enum LockError {
    #[error("lock file corrupt: {message}")]
    Corrupt { message: String },

    #[error("parse error: {0}")]
    Parse(#[from] toml::de::Error),

    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

/// Resolution errors
#[derive(Debug, thiserror::Error)]
pub enum ResolutionError {
    #[error("version conflict for `{name}`: {message}")]
    VersionConflict { name: String, message: String },

    #[error(
        "version conflict for item `{item}` from package `{package}`: {existing} vs {requested} (requester chain: {chain})"
    )]
    ItemVersionConflict {
        item: String,
        package: String,
        existing: String,
        requested: String,
        chain: String,
    },

    #[error(
        "package version conflict for `{package}`: {existing} vs {requested} (requester chain: {chain})"
    )]
    PackageVersionConflict {
        package: String,
        existing: String,
        requested: String,
        chain: String,
    },

    #[error(
        "skill `{skill}` not found (required by {required_by}; searched packages: {searched:?})"
    )]
    SkillNotFound {
        skill: String,
        required_by: String,
        searched: Vec<String>,
    },

    #[error(
        "duplicate source identity: `{existing_name}` and `{duplicate_name}` both resolve to `{source_id}`"
    )]
    DuplicateSourceIdentity {
        existing_name: String,
        duplicate_name: String,
        source_id: String,
    },

    #[error(
        "source `{name}` was referenced with conflicting identities: existing `{existing}`, incoming `{incoming}`"
    )]
    SourceIdentityMismatch {
        name: String,
        existing: String,
        incoming: String,
    },

    #[error("source not found: {name}")]
    SourceNotFound { name: String },
}

/// Validation errors
#[derive(Debug, thiserror::Error)]
pub enum ValidationError {
    #[error("unresolvable skill references found")]
    UnresolvableRefs,
}

/// Top-level error type aggregating all module errors
#[derive(Debug, thiserror::Error)]
pub enum MarsError {
    #[error("config error: {0}")]
    Config(#[from] ConfigError),

    #[error("lock error: {0}")]
    Lock(#[from] LockError),

    #[error("source error: {source_name}: {message}")]
    Source {
        source_name: String,
        message: String,
    },

    #[error(
        "source error: {source_name}: subpath `{subpath}` escapes checkout root `{}`",
        checkout_root.display()
    )]
    SubpathTraversal {
        source_name: String,
        subpath: String,
        checkout_root: PathBuf,
    },

    #[error(
        "source error: {source_name}: subpath `{subpath}` does not exist under checkout root `{}`",
        checkout_root.display()
    )]
    SubpathMissing {
        source_name: String,
        subpath: String,
        checkout_root: PathBuf,
    },

    #[error(
        "source error: {source_name}: subpath `{subpath}` is not a directory under checkout root `{}`",
        checkout_root.display()
    )]
    SubpathNotDirectory {
        source_name: String,
        subpath: String,
        checkout_root: PathBuf,
    },

    #[error(
        "discovery collision in `{source_name}`: {kind} `{item_name}` found at `{}` and `{}`",
        path_a.display(),
        path_b.display()
    )]
    DiscoveryCollision {
        source_name: String,
        kind: String,
        item_name: String,
        path_a: PathBuf,
        path_b: PathBuf,
    },

    #[error(
        "source error: {source_name}: plugin manifest path `{manifest_path}` escapes package root `{}`",
        package_root.display()
    )]
    ManifestDeclaredPathEscape {
        source_name: String,
        manifest_path: String,
        package_root: PathBuf,
    },

    #[error(
        "source error: {source_name}: plugin manifest path `{manifest_path}` does not exist under package root `{}`",
        package_root.display()
    )]
    ManifestDeclaredPathMissing {
        source_name: String,
        manifest_path: String,
        package_root: PathBuf,
    },

    /// Sync refused to overwrite a file/directory not tracked in mars.lock.
    #[error("source error: {source_name}: refusing to overwrite unmanaged path `{}`", path.display())]
    UnmanagedCollision { source_name: String, path: PathBuf },

    #[error("resolution failed: {0}")]
    Resolution(#[from] ResolutionError),

    #[error("merge conflict in {path}")]
    Conflict { path: String },

    #[error("{item} is provided by both `{source_a}` and `{source_b}`")]
    Collision {
        item: String,
        source_a: String,
        source_b: String,
    },

    #[error("validation: {0}")]
    Validation(#[from] ValidationError),

    #[error("invalid request: {message}")]
    InvalidRequest { message: String },

    #[error("frozen violation: {message}")]
    FrozenViolation { message: String },

    #[error(
        "locked commit {commit} is no longer reachable in {url} — the tag may have been force-pushed"
    )]
    LockedCommitUnreachable { commit: String, url: String },

    /// Internal control-flow signal: the resolver detected that an already-resolved
    /// package would select a different ref under the full accumulated constraint set.
    /// Caught by the `resolve()` driver to trigger a fresh-context restart.
    /// Never surfaces to end users.
    #[error("(internal: resolution restart needed for `{package}`)")]
    ResolutionRestartNeeded { package: String },

    /// Link operation error — conflict, missing target, or invalid link metadata.
    #[error("link error: {target}: {message}")]
    Link { target: String, message: String },

    #[error(
        "models cache is empty and cannot be refreshed: {reason}. Run `{cmd}` to populate it.",
        cmd = managed_cmd("mars models refresh"),
    )]
    ModelCacheUnavailable { reason: String },

    #[error("{operation} failed for {}: {source}", path.display())]
    Io {
        operation: String,
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },

    #[error("HTTP error: {url} — {status}: {message}")]
    Http {
        url: String,
        status: u16,
        message: String,
    },

    #[error("git command failed: `{command}` — {message}")]
    GitCli { command: String, message: String },

    #[error("internal error: {0}")]
    Internal(String),
}

impl MarsError {
    /// Map error variants to CLI exit codes.
    ///
    /// - 1: sync completed with unresolved conflicts
    /// - 2: resolution/validation/config error
    /// - 3: source, I/O, HTTP, or git CLI error
    pub fn exit_code(&self) -> i32 {
        match self {
            MarsError::Conflict { .. } => 1,
            MarsError::Link { .. }
            | MarsError::Config(_)
            | MarsError::Lock(_)
            | MarsError::Resolution(_)
            | MarsError::Collision { .. }
            | MarsError::Validation(_)
            | MarsError::InvalidRequest { .. }
            | MarsError::FrozenViolation { .. }
            | MarsError::LockedCommitUnreachable { .. } => 2,
            MarsError::Source { .. }
            | MarsError::SubpathTraversal { .. }
            | MarsError::SubpathMissing { .. }
            | MarsError::SubpathNotDirectory { .. }
            | MarsError::DiscoveryCollision { .. }
            | MarsError::ManifestDeclaredPathEscape { .. }
            | MarsError::ManifestDeclaredPathMissing { .. }
            | MarsError::UnmanagedCollision { .. }
            | MarsError::ModelCacheUnavailable { .. }
            | MarsError::Io { .. }
            | MarsError::Http { .. }
            | MarsError::GitCli { .. }
            | MarsError::Internal(_) => 3,
            MarsError::ResolutionRestartNeeded { .. } => {
                unreachable!("ResolutionRestartNeeded is an internal signal caught by resolve()")
            }
        }
    }
}

impl From<std::io::Error> for MarsError {
    fn from(source: std::io::Error) -> Self {
        MarsError::Io {
            operation: "I/O operation".to_string(),
            path: PathBuf::from("<unknown>"),
            source,
        }
    }
}

pub type Result<T> = std::result::Result<T, MarsError>;

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

    #[test]
    fn mars_error_exit_codes_match_spec() {
        let cases = vec![
            (
                MarsError::Conflict {
                    path: "agents/reviewer.md".to_string(),
                },
                1,
            ),
            (
                MarsError::Config(ConfigError::Invalid {
                    message: "bad config".to_string(),
                }),
                2,
            ),
            (
                MarsError::Lock(LockError::Corrupt {
                    message: "bad lock".to_string(),
                }),
                2,
            ),
            (
                MarsError::Resolution(ResolutionError::SourceNotFound {
                    name: "missing".to_string(),
                }),
                2,
            ),
            (
                MarsError::Collision {
                    item: "coder".to_string(),
                    source_a: "base".to_string(),
                    source_b: "custom".to_string(),
                },
                2,
            ),
            (MarsError::Validation(ValidationError::UnresolvableRefs), 2),
            (
                MarsError::InvalidRequest {
                    message: "bad flag combination".to_string(),
                },
                2,
            ),
            (
                MarsError::FrozenViolation {
                    message: "lock file would change but --frozen is set".to_string(),
                },
                2,
            ),
            (
                MarsError::LockedCommitUnreachable {
                    commit: "abc123".to_string(),
                    url: "https://example.com/repo.git".to_string(),
                },
                2,
            ),
            (
                MarsError::Link {
                    target: ".claude".to_string(),
                    message: "conflicts found".to_string(),
                },
                2,
            ),
            (
                MarsError::Source {
                    source_name: "origin".to_string(),
                    message: "network failed".to_string(),
                },
                3,
            ),
            (
                MarsError::SubpathTraversal {
                    source_name: "origin".to_string(),
                    subpath: "../escape".to_string(),
                    checkout_root: PathBuf::from("/tmp/root"),
                },
                3,
            ),
            (
                MarsError::SubpathMissing {
                    source_name: "origin".to_string(),
                    subpath: "plugins/foo".to_string(),
                    checkout_root: PathBuf::from("/tmp/root"),
                },
                3,
            ),
            (
                MarsError::SubpathNotDirectory {
                    source_name: "origin".to_string(),
                    subpath: "plugins/foo".to_string(),
                    checkout_root: PathBuf::from("/tmp/root"),
                },
                3,
            ),
            (
                MarsError::DiscoveryCollision {
                    source_name: "origin".to_string(),
                    kind: "skill".to_string(),
                    item_name: "plan".to_string(),
                    path_a: PathBuf::from("skills/a"),
                    path_b: PathBuf::from("skills/b"),
                },
                3,
            ),
            (
                MarsError::ManifestDeclaredPathEscape {
                    source_name: "origin".to_string(),
                    manifest_path: "./../escape".to_string(),
                    package_root: PathBuf::from("/tmp/root"),
                },
                3,
            ),
            (
                MarsError::ManifestDeclaredPathMissing {
                    source_name: "origin".to_string(),
                    manifest_path: "./missing".to_string(),
                    package_root: PathBuf::from("/tmp/root"),
                },
                3,
            ),
            (
                MarsError::UnmanagedCollision {
                    source_name: "origin".to_string(),
                    path: PathBuf::from("agents/coder.md"),
                },
                3,
            ),
            (
                MarsError::ModelCacheUnavailable {
                    reason: "MARS_OFFLINE is set and no cached catalog is available".to_string(),
                },
                3,
            ),
            (
                MarsError::Io {
                    operation: "read file".to_string(),
                    path: PathBuf::from("/tmp/file"),
                    source: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied"),
                },
                3,
            ),
            (
                MarsError::Http {
                    url: "https://example.com/archive.tar.gz".to_string(),
                    status: 503,
                    message: "service unavailable".to_string(),
                },
                3,
            ),
            (
                MarsError::GitCli {
                    command: "git ls-remote --tags https://example.com/repo".to_string(),
                    message: "fatal: repository not found".to_string(),
                },
                3,
            ),
        ];

        for (err, expected) in cases {
            assert_eq!(
                err.exit_code(),
                expected,
                "unexpected exit code for error: {err}"
            );
        }
    }
}