Skip to main content

a2a_protocol_server/skills/
config.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Tom F. <[email protected]> (https://github.com/tomtom215)
3
4//! What to scan and how much to keep: [`SkillsConfig`] and [`SkillSource`].
5
6use std::path::{Path, PathBuf};
7
8/// The most skills a catalog holds when no other bound is set.
9///
10/// Thirty-two descriptions of [`DEFAULT_MAX_DESCRIPTION_CHARS`] are at most
11/// 12,800 characters of prompt, about 3,200 tokens at four characters a token
12/// (an estimate, not a count).
13pub const DEFAULT_MAX_SKILLS: usize = 32;
14
15/// The most characters of a description the prompt's catalog shows when no
16/// other bound is set.
17///
18/// The specification sizes the catalog at about 100 tokens a skill, and a
19/// description is most of that: 400 characters is about 100 tokens at four a
20/// token. The whole description is in the activation result.
21pub const DEFAULT_MAX_DESCRIPTION_CHARS: usize = 400;
22
23/// The largest `SKILL.md` body kept when no other bound is set: 64 KiB.
24///
25/// That is about 16,000 tokens at four bytes a token, over three times the
26/// specification's recommendation of 5,000. A body over the bound is not
27/// truncated, because half a procedure is worse than none: the skill is
28/// skipped.
29pub const DEFAULT_MAX_BODY_BYTES: usize = 64 * 1024;
30
31/// The largest file `read_skill_file` returns when no other bound is set:
32/// 64 KiB. A larger file is refused, not truncated.
33pub const DEFAULT_MAX_RESOURCE_BYTES: usize = 64 * 1024;
34
35/// Where a skills directory comes from, which decides how far it is trusted.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37#[non_exhaustive]
38pub enum SkillScope {
39    /// A directory the operator configured: their own text.
40    Operator,
41    /// A directory of a project the process was pointed at, such as a
42    /// repository's `.agents/skills`: text anyone who can commit to it
43    /// wrote. See [`SkillSource::project`].
44    Project,
45}
46
47/// One directory whose subdirectories are skills.
48///
49/// Only the directory's own children are examined (`<dir>/<name>/SKILL.md`),
50/// not deeper; names that start with a dot are skipped.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct SkillSource {
53    pub(super) path: PathBuf,
54    pub(super) scope: SkillScope,
55    pub(super) trusted: bool,
56    pub(super) follow_symlinks: bool,
57}
58
59impl SkillSource {
60    /// A directory the operator configured. Trusted: its skills' text is the
61    /// operator's own.
62    #[must_use]
63    pub fn operator(path: impl Into<PathBuf>) -> Self {
64        Self {
65            path: path.into(),
66            scope: SkillScope::Operator,
67            trusted: true,
68            follow_symlinks: false,
69        }
70    }
71
72    /// A project's skills directory.
73    ///
74    /// Not read until the operator says so with
75    /// [`with_trusted(true)`](Self::with_trusted): a skill's description goes
76    /// into the model's system prompt and its body into the conversation, so a
77    /// directory anyone can write to is an instruction channel
78    /// (`docs/injection-containment.md`). Marking it trusted is the operator's
79    /// statement that they accept that. Even then a project's skill never
80    /// replaces an operator's of the same name, its description is marked as
81    /// the project's in the prompt, and it gets no capability the operator's
82    /// skills lack: `allowed-tools` is never honoured and `scripts/` are
83    /// never run.
84    #[must_use]
85    pub fn project(path: impl Into<PathBuf>) -> Self {
86        Self {
87            path: path.into(),
88            scope: SkillScope::Project,
89            trusted: false,
90            follow_symlinks: false,
91        }
92    }
93
94    /// Whether this directory is read. Operator directories are trusted from
95    /// the start and project directories are not.
96    #[must_use]
97    pub const fn with_trusted(mut self, trusted: bool) -> Self {
98        self.trusted = trusted;
99        self
100    }
101
102    /// Whether a symbolic link in this directory that leads to a skill
103    /// directory is followed. Off by default: a link is how a directory
104    /// someone else controls gets into this one. A followed link still
105    /// cannot take a file read outside the skill's own directory.
106    #[must_use]
107    pub const fn with_follow_symlinks(mut self, follow: bool) -> Self {
108        self.follow_symlinks = follow;
109        self
110    }
111
112    /// The directory.
113    #[must_use]
114    pub fn path(&self) -> &Path {
115        &self.path
116    }
117
118    /// Where it comes from.
119    #[must_use]
120    pub const fn scope(&self) -> SkillScope {
121        self.scope
122    }
123}
124
125/// What [`SkillCatalog::discover`](super::SkillCatalog::discover) scans and
126/// the bounds it keeps to.
127///
128/// Sources are read operator directories first, then project directories,
129/// each in the order given and each directory's skills in name order, so the
130/// catalog does not depend on the file system's listing order. When two skills
131/// share a name the first is kept.
132#[derive(Debug, Clone, PartialEq, Eq)]
133pub struct SkillsConfig {
134    pub(super) sources: Vec<SkillSource>,
135    pub(super) max_skills: usize,
136    pub(super) max_description_chars: usize,
137    pub(super) max_body_bytes: usize,
138    pub(super) max_resource_bytes: usize,
139}
140
141impl Default for SkillsConfig {
142    fn default() -> Self {
143        Self::new()
144    }
145}
146
147impl SkillsConfig {
148    /// No sources, and the default bounds.
149    #[must_use]
150    pub const fn new() -> Self {
151        Self {
152            sources: Vec::new(),
153            max_skills: DEFAULT_MAX_SKILLS,
154            max_description_chars: DEFAULT_MAX_DESCRIPTION_CHARS,
155            max_body_bytes: DEFAULT_MAX_BODY_BYTES,
156            max_resource_bytes: DEFAULT_MAX_RESOURCE_BYTES,
157        }
158    }
159
160    /// Adds a directory to scan.
161    #[must_use]
162    pub fn with_source(mut self, source: SkillSource) -> Self {
163        self.sources.push(source);
164        self
165    }
166
167    /// Sets how many skills the catalog holds. Skills past it are not read
168    /// and one diagnostic says how many were left. Default
169    /// [`DEFAULT_MAX_SKILLS`].
170    #[must_use]
171    pub const fn with_max_skills(mut self, max: usize) -> Self {
172        self.max_skills = max;
173        self
174    }
175
176    /// Sets how many characters of a description the prompt's catalog shows.
177    /// `0` is taken as `1`. Default [`DEFAULT_MAX_DESCRIPTION_CHARS`].
178    #[must_use]
179    pub const fn with_max_description_chars(mut self, max: usize) -> Self {
180        self.max_description_chars = if max == 0 { 1 } else { max };
181        self
182    }
183
184    /// Sets the largest body kept, in bytes; a skill with a larger one is
185    /// skipped. Default [`DEFAULT_MAX_BODY_BYTES`].
186    #[must_use]
187    pub const fn with_max_body_bytes(mut self, max: usize) -> Self {
188        self.max_body_bytes = max;
189        self
190    }
191
192    /// Sets the largest file `read_skill_file` returns, in bytes. Default
193    /// [`DEFAULT_MAX_RESOURCE_BYTES`].
194    #[must_use]
195    pub const fn with_max_resource_bytes(mut self, max: usize) -> Self {
196        self.max_resource_bytes = max;
197        self
198    }
199}