mars-schema 0.34.5

Pure schema reader and editor for Mars agent packages
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
//! What is wrong with a package, from its files and its dependencies: `mars check` and the WASM
//! `check` share this one function. It has no host I/O, and it reports what is true of the package
//! wherever it runs. A finding that exists only because a harness target or an older Mars cannot
//! take something belongs where that target or version exists (the binary).
//!
//! A level says what the author loses: `Error`, the file or package won't load or install;
//! `Warning`, something written won't take effect; `Info`, nothing written is lost.
//!
//! Per path, a result lists `prepare`'s findings, then the three kinds of change translation made
//! that an author should see ([`CHANGE_CODES`]), then the check's own rules in the order they run
//! below, stably sorted by level. Dropping the codes of those rules and changes from a path's list
//! leaves what `prepare` returns for it, so the two never disagree about a file.

use std::collections::{BTreeMap, BTreeSet, VecDeque};

use indexmap::IndexMap;
use serde::Serialize;

use crate::claude::translate::REPAIRED;
use crate::diagnostic::{PathSegment, SchemaDiagnostic, Severity, key_path};
use crate::discover::{Boundary, basename, holds_file, join, kept_skill_containers};
use crate::item::{DefinitionKind, ItemKind};
use crate::prepare::{Classification, Prepared, PreparedDefinition};
use crate::read::Reading;
use crate::selection::{
    GraphView, MissingSkill, PackageView, SelectionFacts, SelectionResult, duplicate_slugs, select,
};
use crate::set_aside::{EXECUTION_IGNORED, RESTRICTION_LOST};
use crate::tree::TreeSource;

/// The codes `check` takes from `Prepared::changes`.
const CHANGE_CODES: [&str; 3] = [RESTRICTION_LOST, EXECUTION_IGNORED, REPAIRED];

const MANIFEST: &str = "mars.toml";

/// What is wrong with a package.
#[derive(Debug, Clone, Serialize)]
pub struct Check {
    /// Findings in the root's own files, by source path: what `prepare` reported for the file,
    /// then the check's own. `mars.toml` holds what applies to the whole package and comes first,
    /// then the paths in lexical order.
    pub diagnostics: IndexMap<String, Vec<SchemaDiagnostic>>,
    /// `skill-ref.dependency` only: a dependency's own Agent names a skill no package ships. By
    /// the dependency's source name, then the Agent's output path (`agents/<slug>.md`).
    pub dependency_diagnostics: BTreeMap<String, BTreeMap<String, Vec<SchemaDiagnostic>>>,
    /// Every `[dependencies]` name reachable from the root that `dependencies` lacks. While it
    /// is not empty the result is not a verdict: no skill ref was judged.
    pub missing_dependencies: Vec<MissingDependency>,
    /// Every dependency reachable from the root whose `selection.unreadable` is set: an invalid
    /// or newer-schema manifest. It may be the one that ships a skill, so while this is not empty
    /// the result is not a verdict either. Same shape as `missing_dependencies`.
    pub unreadable_dependencies: Vec<MissingDependency>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MissingDependency {
    pub name: String,
    /// The package whose `mars.toml` declares it; `None` for the root.
    pub declared_in: Option<String>,
}

/// What is wrong with the package that `prepared` (with [`crate::links::package`] applied) is
/// the result of preparing from `source`. `dependencies` is the whole closure: every package
/// reachable from the root through any `[dependencies]` entry, filtered or not, by the name the
/// declaring table gives its source.
pub fn check(
    source: &impl TreeSource,
    prepared: &Prepared,
    dependencies: &BTreeMap<String, SelectionFacts>,
) -> Check {
    let mut found = Found::default();
    let mut dependency_diagnostics = BTreeMap::new();
    let mut missing_dependencies = Vec::new();
    let mut unreadable_dependencies = Vec::new();
    if matches!(
        prepared.classification,
        Classification::Schema1 | Classification::Foreign(_)
    ) {
        found.changes(prepared);
        found.requirement(prepared);
        found.definition_names(prepared);
        let graph = Graph::of(prepared, dependencies);
        found.selection_duplicates(prepared, &graph.selection);
        if graph.missing.is_empty() && graph.unreadable.is_empty() {
            found.skill_refs(prepared, &graph);
            dependency_diagnostics = graph.dependency_findings(dependencies);
        }
        if matches!(prepared.classification, Classification::Schema1) {
            found.skill_folders(source, prepared);
        }
        missing_dependencies = graph.missing;
        unreadable_dependencies = graph.unreadable;
    }
    Check {
        diagnostics: found.merge(prepared),
        dependency_diagnostics,
        missing_dependencies,
        unreadable_dependencies,
    }
}

/// The check's own findings by source path, in the order the rules run.
#[derive(Default)]
struct Found {
    changes: BTreeMap<String, Vec<SchemaDiagnostic>>,
    rules: BTreeMap<String, Vec<SchemaDiagnostic>>,
}

impl Found {
    fn rule(&mut self, path: &str, diagnostic: SchemaDiagnostic) {
        self.rules
            .entry(path.to_owned())
            .or_default()
            .push(diagnostic);
    }

    fn merge(self, prepared: &Prepared) -> IndexMap<String, Vec<SchemaDiagnostic>> {
        let mut by_path: BTreeMap<String, Vec<SchemaDiagnostic>> = prepared
            .all_diagnostics()
            .into_iter()
            .map(|(path, found)| (path.to_owned(), found.into_iter().cloned().collect()))
            .collect();
        for (path, found) in self.changes.into_iter().chain(self.rules) {
            by_path.entry(path).or_default().extend(found);
        }
        let mut merged = IndexMap::new();
        if let Some(found) = by_path.remove(MANIFEST) {
            merged.insert(MANIFEST.to_owned(), found);
        }
        merged.extend(by_path);
        for found in merged.values_mut() {
            // `Severity` is declared most severe first.
            found.sort_by_key(|diagnostic| diagnostic.severity as u8);
        }
        merged
    }

    /// The changes translation made that the author can act on.
    fn changes(&mut self, prepared: &Prepared) {
        for (path, changes) in &prepared.changes {
            for change in changes
                .iter()
                .filter(|change| CHANGE_CODES.contains(&change.code.as_str()))
            {
                self.changes
                    .entry(path.clone())
                    .or_default()
                    .push(change.clone());
            }
        }
    }

    /// `package.requirement`.
    fn requirement(&mut self, prepared: &Prepared) {
        // A generated manifest has no `requires-*` to read.
        let Some(package) = prepared
            .manifest
            .as_ref()
            .filter(|manifest| manifest.source_path.is_some())
            .and_then(|manifest| manifest.reading.package.fields.as_ref())
        else {
            return;
        };
        for (key, message) in crate::package::requirement_errors(package) {
            self.rule(
                MANIFEST,
                SchemaDiagnostic::new(
                    "package.requirement",
                    Severity::Error,
                    vec![
                        PathSegment::Key("package".into()),
                        PathSegment::Key(key.into()),
                    ],
                    message,
                ),
            );
        }
    }

    /// `agent.description-missing`, `agent.name-missing`, `definition.name-mismatch`
    /// and `definition.duplicate-name`.
    fn definition_names(&mut self, prepared: &Prepared) {
        let items = prepared
            .definitions
            .iter()
            .filter_map(Named::of)
            .collect::<Vec<_>>();
        let agents = || items.iter().filter(|item| item.agent && !item.unfenced);

        for item in agents().filter(|item| !item.described) {
            self.rule(
                &item.definition.source_path,
                SchemaDiagnostic::new(
                    "agent.description-missing",
                    Severity::Info,
                    key_path("description"),
                    format!(
                        "agent `{}` has no `description`; Claude does not load an Agent without one",
                        item.name()
                    ),
                ),
            );
        }
        for item in agents().filter(|item| item.declared.is_none()) {
            self.rule(
                &item.definition.source_path,
                SchemaDiagnostic::new(
                    "agent.name-missing",
                    Severity::Info,
                    key_path("name"),
                    format!(
                        "agent `{}` has no `name`; the file name is used",
                        item.definition.slug
                    ),
                ),
            );
        }
        for item in items.iter().filter(|item| {
            item.declared
                .is_some_and(|name| name != item.definition.slug)
        }) {
            let (noun, file) = if item.agent {
                ("agent file", format!("{}.md", item.definition.slug))
            } else {
                ("skill folder", item.definition.slug.clone())
            };
            let consequence = if item.agent {
                "the item loads under both".to_string()
            } else {
                format!(
                    "Claude lists it as `{}`, but skill refs and install use the folder name `{file}`",
                    item.name()
                )
            };
            self.rule(
                &item.definition.source_path,
                SchemaDiagnostic::new(
                    "definition.name-mismatch",
                    Severity::Info,
                    key_path("name"),
                    format!(
                        "{noun} `{file}` doesn't match its `name` `{}`; {consequence}",
                        item.name()
                    ),
                ),
            );
        }

        let mut by_name: BTreeMap<(DefinitionKind, &str), Vec<&Named>> = BTreeMap::new();
        for item in &items {
            by_name
                .entry((item.definition.kind, item.name()))
                .or_default()
                .push(item);
        }
        let shared = |item: &Named| by_name[&(item.definition.kind, item.name())].len() > 1;
        for item in items.iter().filter(|item| shared(item)) {
            let others = by_name[&(item.definition.kind, item.name())]
                .iter()
                .filter(|other| other.definition.source_path != item.definition.source_path)
                .map(|other| format!("`{}`", other.definition.source_path))
                .collect::<Vec<_>>()
                .join(", ");
            self.rule(
                &item.definition.source_path,
                SchemaDiagnostic::new(
                    "definition.duplicate-name",
                    Severity::Error,
                    key_path("name"),
                    format!(
                        "{} name `{}` is also the name of {others}; `mars sync` installs both, and one native file replaces the other",
                        item.definition.kind,
                        item.name()
                    ),
                ),
            );
        }
    }

    /// `selection.duplicate-slug`: a root item over a dependency's is only information, a slug
    /// two dependencies ship is a dependency the author declared installing less than it ships.
    fn selection_duplicates(&mut self, prepared: &Prepared, selection: &SelectionResult) {
        for duplicate in duplicate_slugs(&selection.items) {
            // Two files of the root's own are `import.duplicate-slug`, not two packages.
            if duplicate.packages.iter().flatten().next().is_none() {
                continue;
            }
            let names = duplicate
                .packages
                .iter()
                .flatten()
                .map(|name| format!("`{name}`"))
                .collect::<Vec<_>>();
            let dependencies = names.join(", ");
            if duplicate.packages.contains(&None) {
                let Some(root) = prepared.definitions.iter().find(|definition| {
                    ItemKind::from(definition.kind) == duplicate.kind
                        && definition.slug == duplicate.slug
                }) else {
                    continue;
                };
                self.rule(
                    &root.source_path,
                    SchemaDiagnostic::new(
                        "selection.duplicate-slug",
                        Severity::Info,
                        Vec::new(),
                        format!(
                            "{} `{}` is also shipped by {dependencies}; this package's is the one installed",
                            duplicate.kind, duplicate.slug
                        ),
                    ),
                );
            } else {
                self.rule(
                    MANIFEST,
                    SchemaDiagnostic::new(
                        "selection.duplicate-slug",
                        Severity::Warning,
                        key_path("dependencies"),
                        format!(
                            "{} `{}` is shipped by {}; one is installed",
                            duplicate.kind,
                            duplicate.slug,
                            both(&names)
                        ),
                    ),
                );
            }
        }
    }

    /// `skill-ref.missing` and `skill-ref.provider`, for the root's Agents.
    fn skill_refs(&mut self, prepared: &Prepared, graph: &Graph) {
        for missing in graph
            .selection
            .missing_skills
            .iter()
            .filter(|missing| missing.package.is_none())
        {
            let Some(agent) = prepared.definitions.iter().find(|definition| {
                definition.kind == DefinitionKind::Agent && definition.slug == missing.agent
            }) else {
                continue;
            };
            let code = match &missing.hint {
                Some(_) if graph.has_package => "skill-ref.provider",
                _ => "skill-ref.missing",
            };
            let message = missing_message(missing);
            self.rule(
                &agent.source_path,
                SchemaDiagnostic::new(code, Severity::Error, key_path("skills"), message),
            );
        }
    }

    /// `skill.no-definition`: a folder directly under a `skills/` that discovery read, holding
    /// files and no `SKILL.md`.
    fn skill_folders(&mut self, source: &impl TreeSource, prepared: &Prepared) {
        let boundary = Boundary::of(source);
        let containers = kept_skill_containers(
            source,
            prepared
                .definitions
                .iter()
                .map(|definition| definition.source_path.as_str()),
        );
        for container in containers {
            for dir in source.children(&container) {
                if basename(&dir).starts_with('.')
                    || !source.is_dir(&dir)
                    || source.is_file(&join(&dir, "SKILL.md"))
                    || !boundary.admits(source, &dir, &mut Vec::new())
                    || !holds_file(source, &boundary, &dir)
                {
                    continue;
                }
                self.rule(
                    &dir,
                    SchemaDiagnostic::new(
                        "skill.no-definition",
                        Severity::Warning,
                        Vec::new(),
                        format!("`{dir}` has files and no `SKILL.md`, so none of it is installed"),
                    ),
                );
            }
        }
    }
}

/// An Agent or skill of the root that was read, with what its check rules need.
struct Named<'a> {
    definition: &'a PreparedDefinition,
    agent: bool,
    /// The author's `name`.
    declared: Option<&'a str>,
    described: bool,
    /// An Agent with no opening `---`: `frontmatter.missing` already says it has no fields.
    unfenced: bool,
}

impl<'a> Named<'a> {
    fn of(definition: &'a PreparedDefinition) -> Option<Self> {
        let (declared, described, unfenced) = match &definition.reading {
            Reading::Agent(reading) => {
                let profile = reading.fields.as_ref()?;
                (
                    profile.name.as_deref(),
                    profile.description.is_some(),
                    reading
                        .diagnostics
                        .iter()
                        .any(|diagnostic| diagnostic.code == "frontmatter.missing"),
                )
            }
            Reading::Skill(reading) => {
                let profile = reading.fields.as_ref()?;
                (
                    profile.name.as_deref(),
                    profile.description.is_some(),
                    false,
                )
            }
            Reading::Manifest(_) => return None,
        };
        Some(Self {
            agent: definition.kind == DefinitionKind::Agent,
            definition,
            declared,
            described,
            unfenced,
        })
    }

    /// The name the item goes by: its `name`, else the file or folder name.
    fn name(&self) -> &str {
        self.declared.unwrap_or(&self.definition.slug)
    }
}

/// Selection over the root and its dependencies, whole, as a sync of the package alone selects.
struct Graph {
    selection: SelectionResult,
    /// The root has `[package]`.
    has_package: bool,
    /// Dependencies a ref could resolve through that the caller left out.
    missing: Vec<MissingDependency>,
    /// Dependencies a ref could resolve through that could not be read; their own errors say
    /// so, and a ref that only they could satisfy is not the root's to report.
    unreadable: Vec<MissingDependency>,
}

impl Graph {
    fn of(prepared: &Prepared, dependencies: &BTreeMap<String, SelectionFacts>) -> Self {
        let has_package = prepared
            .manifest
            .as_ref()
            .is_some_and(|manifest| manifest.reading.package.fields.is_some());
        let has_dependencies = has_package && !prepared.selection.edges.is_empty();
        let mut root = prepared.selection.clone();
        let mut packages = BTreeMap::new();
        if has_dependencies {
            packages = dependencies.clone();
        } else {
            root.edges.clear();
        }

        let mut missing = Vec::new();
        let mut unreadable = Vec::new();
        let mut seen = BTreeSet::new();
        let mut queue: VecDeque<(Option<&str>, &SelectionFacts)> = VecDeque::from([(None, &root)]);
        while let Some((declared_in, facts)) = queue.pop_front() {
            for edge in &facts.edges {
                if !seen.insert(edge.name.as_str()) {
                    continue;
                }
                match packages.get(&edge.name) {
                    Some(package) => {
                        if package.unreadable {
                            unreadable.push(MissingDependency {
                                name: edge.name.clone(),
                                declared_in: declared_in.map(str::to_owned),
                            });
                        }
                        queue.push_back((Some(edge.name.as_str()), package));
                    }
                    None => missing.push(MissingDependency {
                        name: edge.name.clone(),
                        declared_in: declared_in.map(str::to_owned),
                    }),
                }
            }
        }

        let view = |facts| PackageView {
            facts,
            extra_items: Vec::new(),
        };
        let selection = select(&GraphView {
            requests: Vec::new(),
            packages: packages
                .into_iter()
                .map(|(name, facts)| (name, view(facts)))
                .collect(),
            root: Some(view(root)),
        });
        Self {
            selection,
            has_package,
            missing,
            unreadable,
        }
    }

    /// `skill-ref.dependency`: a dependency's own Agent names a skill no package ships.
    fn dependency_findings(
        &self,
        dependencies: &BTreeMap<String, SelectionFacts>,
    ) -> BTreeMap<String, BTreeMap<String, Vec<SchemaDiagnostic>>> {
        let mut found: BTreeMap<String, BTreeMap<String, Vec<SchemaDiagnostic>>> = BTreeMap::new();
        for missing in &self.selection.missing_skills {
            let Some(package) = missing.package.as_deref() else {
                continue;
            };
            let Some(agent) = dependencies.get(package).and_then(|facts| {
                facts.definitions.iter().find(|definition| {
                    definition.kind == DefinitionKind::Agent && definition.slug == missing.agent
                })
            }) else {
                continue;
            };
            found
                .entry(package.to_owned())
                .or_default()
                .entry(agent.output_path.clone())
                .or_default()
                .push(SchemaDiagnostic::new(
                    "skill-ref.dependency",
                    Severity::Error,
                    key_path("skills"),
                    missing_message(missing),
                ));
        }
        found
    }
}

/// The error for a ref no package ships, in the words `mars check` has always used.
fn missing_message(missing: &MissingSkill) -> String {
    let skill = &missing.skill;
    // An Agent of a dependency is that package's to fix, and the author's to work around.
    let agent = match &missing.package {
        Some(package) => format!("agent `{}` of dependency `{package}`", missing.agent),
        None => format!("agent `{}`", missing.agent),
    };
    if let Some(hint) = &missing.hint {
        return format!(
            "{agent} references skill `{skill}`, which is not in this package or its dependencies; {hint}"
        );
    }
    let remedy = match &missing.package {
        Some(package) => {
            format!("use a version of `{package}` that ships it, or add a dependency that does")
        }
        None => "add the skill's source package as a dependency, or remove the skill reference"
            .to_string(),
    };
    let own = match &missing.package {
        Some(package) => package.as_str(),
        None => "(this package)",
    };
    let searched = std::iter::once(own)
        .chain(
            missing
                .searched
                .iter()
                .map(String::as_str)
                .filter(|name| *name != own),
        )
        .collect::<Vec<_>>()
        .join(", ");
    format!(
        "{agent} references skill `{skill}` not found in local package or dependencies\n  searched: {searched}\n  hint: {remedy}"
    )
}

/// "both `a` and `b`", or "`a`, `b` and `c`" for more.
fn both(names: &[String]) -> String {
    match names {
        [first, second] => format!("both {first} and {second}"),
        [init @ .., last] => format!("{} and {last}", init.join(", ")),
        [] => String::new(),
    }
}