a2a_protocol_server/skills/diagnostic.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! What discovery reports: [`SkillDiagnostic`], with a [`Severity`] and a
5//! [`DiagnosticKind`] a program can match on.
6
7use std::fmt;
8use std::path::{Path, PathBuf};
9
10/// How much a diagnostic matters.
11///
12/// Ordered: `Info < Warning < Error`.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[non_exhaustive]
15pub enum Severity {
16 /// Worth knowing; nothing is wrong. A skill's `scripts/` being present
17 /// and not run is the usual one.
18 Info,
19 /// The skill is loaded and deviates from the specification, or something
20 /// the operator may have expected was left out (an untrusted project
21 /// directory, a skill dropped for a duplicate name).
22 Warning,
23 /// The skill, or the directory, was skipped.
24 Error,
25}
26
27impl fmt::Display for Severity {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 f.write_str(match self {
30 Self::Info => "info",
31 Self::Warning => "warning",
32 Self::Error => "error",
33 })
34 }
35}
36
37/// What a diagnostic is about, stable enough to match on. The message says
38/// the particulars.
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40#[non_exhaustive]
41pub enum DiagnosticKind {
42 /// A configured directory could not be listed. Nothing was loaded from it.
43 SourceUnreadable,
44 /// A directory was configured and not marked trusted (a project directory
45 /// is not, until the operator says so). Nothing was read from it.
46 SourceNotTrusted,
47 /// A directory holds more entries than are examined; the rest were not.
48 ScanTruncated,
49 /// A symbolic link to a skill directory was not followed.
50 SymlinkNotFollowed,
51 /// A symbolic link in a skills directory leads nowhere usable: it dangles,
52 /// or loops.
53 BrokenSymlink,
54 /// A directory has no `SKILL.md` and was ignored.
55 NoSkillFile,
56 /// `SKILL.md` could not be read, or is not a regular file inside its
57 /// directory.
58 SkillFileUnreadable,
59 /// `SKILL.md` is over the size this module reads.
60 SkillFileTooLarge,
61 /// `SKILL.md` is not UTF-8.
62 NotUtf8,
63 /// `SKILL.md` does not start with a `---` frontmatter line.
64 FrontmatterMissing,
65 /// The frontmatter has no closing `---` line.
66 FrontmatterUnclosed,
67 /// The frontmatter is over the size this module parses; it was not read as
68 /// YAML.
69 FrontmatterTooLarge,
70 /// The frontmatter is not a YAML mapping of the shape this module reads.
71 FrontmatterInvalid,
72 /// A field's value could not be read as the kind of value the field
73 /// takes. For `name` and `description` the skill is skipped; for the
74 /// optional fields the field is ignored.
75 FieldInvalid,
76 /// `name` or `description` is absent or empty. The skill is skipped.
77 FieldMissing,
78 /// The `name` has a character a prompt cannot carry safely, or is
79 /// absurdly long. The skill is skipped.
80 NameInvalid,
81 /// The `name` deviates from the specification (uppercase, a hyphen at an
82 /// end or two together, more than 64 characters); the skill is loaded.
83 NameNotSpec,
84 /// The `name` is not the name of the skill's directory; the skill is
85 /// loaded under its `name`.
86 NameMismatch,
87 /// The `description` is longer than the specification's 1024 characters;
88 /// the skill is loaded.
89 DescriptionTooLong,
90 /// The catalog shows a shortened description; the activation result
91 /// carries the whole of it.
92 DescriptionShortened,
93 /// An optional field was empty, too long or not a string; it is ignored.
94 OptionalFieldIgnored,
95 /// A frontmatter field the specification (as of
96 /// [`SPEC_SNAPSHOT`](super::SPEC_SNAPSHOT)) does not define.
97 UnknownField,
98 /// A field appears twice; the first is used.
99 DuplicateField,
100 /// A plain value contains `: `, which a strict YAML parser refuses and
101 /// this module reads as text.
102 LenientYaml,
103 /// `allowed-tools` is recorded and never used: no skill widens the tools
104 /// the loop offers.
105 AllowedToolsIgnored,
106 /// The body is over the size this module keeps. The skill is skipped.
107 BodyTooLarge,
108 /// The body is empty.
109 BodyEmpty,
110 /// The body is longer than the specification recommends.
111 BodyLong,
112 /// A second skill has a name already taken; it was dropped.
113 DuplicateName,
114 /// More skills were found than the catalog holds; the rest were not read.
115 OverSkillCap,
116 /// The skill has a `scripts/` directory. Its files are listed to the
117 /// model as present and never run.
118 ScriptsNotRun,
119 /// A listing of a skill's files stopped at a bound.
120 ListingTruncated,
121}
122
123/// One thing discovery noticed. See
124/// [`SkillCatalog::diagnostics`](super::SkillCatalog::diagnostics).
125#[derive(Debug, Clone, PartialEq, Eq)]
126pub struct SkillDiagnostic {
127 severity: Severity,
128 kind: DiagnosticKind,
129 path: PathBuf,
130 message: String,
131}
132
133impl SkillDiagnostic {
134 pub(super) fn new(
135 severity: Severity,
136 kind: DiagnosticKind,
137 path: impl Into<PathBuf>,
138 message: impl Into<String>,
139 ) -> Self {
140 Self {
141 severity,
142 kind,
143 path: path.into(),
144 message: message.into(),
145 }
146 }
147
148 /// How much it matters.
149 #[must_use]
150 pub const fn severity(&self) -> Severity {
151 self.severity
152 }
153
154 /// What it is about.
155 #[must_use]
156 pub const fn kind(&self) -> DiagnosticKind {
157 self.kind
158 }
159
160 /// The directory or file it concerns.
161 #[must_use]
162 pub fn path(&self) -> &Path {
163 &self.path
164 }
165
166 /// What was found, in words.
167 #[must_use]
168 pub fn message(&self) -> &str {
169 &self.message
170 }
171}
172
173impl fmt::Display for SkillDiagnostic {
174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 write!(
176 f,
177 "{}: {}: {}",
178 self.severity,
179 self.path.display(),
180 self.message
181 )
182 }
183}
184
185/// A finding in a `SKILL.md` before the file's path is known.
186#[derive(Debug, Clone, PartialEq, Eq)]
187pub(super) struct Finding {
188 pub(super) severity: Severity,
189 pub(super) kind: DiagnosticKind,
190 pub(super) message: String,
191}
192
193impl Finding {
194 pub(super) fn new(
195 severity: Severity,
196 kind: DiagnosticKind,
197 message: impl Into<String>,
198 ) -> Self {
199 Self {
200 severity,
201 kind,
202 message: message.into(),
203 }
204 }
205
206 /// This finding as a diagnostic about `path`.
207 pub(super) fn at(self, path: &Path) -> SkillDiagnostic {
208 SkillDiagnostic::new(self.severity, self.kind, path, self.message)
209 }
210}