aither-agent 0.25.0

AI agent framework with planning, memory, and tool execution for aither
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
//! Subagent definitions.
//!
//! A subagent is a TOML file named after the subagent it defines:
//!
//! ```toml
//! name = "explore"
//! description = "Investigates a codebase and returns its conclusion."
//! tier = "fast"            # advanced, balanced or fast; absent: the fast model routes the brief
//! access = "read_only"     # or "worktrees": writes the worktrees its caller grants
//! prompt = '''
//! You investigate the workspace for the task in the brief.
//! '''
//! ```
//!
//! aither bundles none: the embedder supplies its definitions to the
//! `SubagentTool`, and `subagent <name>` looks a name up among them first,
//! then as `<name>.toml` in `agents/` under the calling terminal's working
//! directory; an embedder's definition shadows a local one of the same name.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use async_fs as fs;
use futures_lite::stream::StreamExt;
use serde::Deserialize;

use crate::model_group::ModelTier;

/// The names `subagent` gives its own forms; no definition can take one.
pub const RESERVED_SUBAGENT_NAMES: [&str; 3] = ["fork", "resume", "external"];

/// The directory under a working directory that holds its subagents.
pub const LOCAL_SUBAGENTS_DIR: &str = "agents";

/// What a subagent may write besides its own working directory.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SubagentAccess {
    /// Nothing: it reads the workspace and writes only its own space.
    ReadOnly,
    /// The worktrees its caller grants, within the caller's own authority.
    Worktrees,
}

/// A subagent definition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubagentDefinition {
    /// The name `subagent <name>` runs it by, which its file is named after.
    pub name: String,
    /// When to use it, as the delegating model reads it.
    pub description: String,
    /// Its role prompt; the caller's brief is its first message.
    pub prompt: String,
    /// The model tier it runs on; `None` lets the fast model route each
    /// brief to a tier.
    pub tier: Option<ModelTier>,
    /// What it may write besides its own working directory.
    pub access: SubagentAccess,
}

/// The fields of a definition file.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct DefinitionFile {
    name: String,
    description: String,
    prompt: String,
    #[serde(default)]
    tier: Option<ModelTier>,
    access: SubagentAccess,
}

/// Why a definition is invalid.
#[derive(Debug, thiserror::Error)]
pub enum DefinitionError {
    /// The file is not a definition in TOML.
    #[error("{0}")]
    Toml(#[from] basic_toml::Error),
    /// A text field is blank.
    #[error("`{0}` must not be empty")]
    Empty(&'static str),
    /// The name is one `subagent` gives its own forms.
    #[error("the name '{0}' is reserved for `subagent {0}`")]
    Reserved(String),
    /// The name cannot be a file name.
    #[error("the name '{0}' may hold only ASCII letters, digits, `-` and `_`")]
    InvalidName(String),
}

/// Checks that `name` can name a subagent: a file name of ASCII letters,
/// digits, `-` and `_` that `subagent` does not reserve.
///
/// # Errors
///
/// Returns why `name` cannot name a subagent.
pub fn validate_subagent_name(name: &str) -> Result<(), DefinitionError> {
    if name.is_empty()
        || !name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
    {
        return Err(DefinitionError::InvalidName(name.to_string()));
    }
    if RESERVED_SUBAGENT_NAMES.contains(&name) {
        return Err(DefinitionError::Reserved(name.to_string()));
    }
    Ok(())
}

impl SubagentDefinition {
    /// Parses a definition from its TOML source.
    ///
    /// # Errors
    ///
    /// Returns an error when the source is not a definition, a field is
    /// blank, or the name cannot name a subagent.
    pub fn parse(source: &str) -> Result<Self, DefinitionError> {
        let file: DefinitionFile = basic_toml::from_str(source)?;
        validate_subagent_name(&file.name)?;
        for (field, value) in [("description", &file.description), ("prompt", &file.prompt)] {
            if value.trim().is_empty() {
                return Err(DefinitionError::Empty(field));
            }
        }
        Ok(Self {
            name: file.name,
            description: file.description.trim().to_string(),
            prompt: file.prompt.trim().to_string(),
            tier: file.tier,
            access: file.access,
        })
    }
}

fn definition_path(dir: &Path, name: &str) -> PathBuf {
    let mut file_name = String::with_capacity(name.len() + 5);
    file_name.push_str(name);
    file_name.push_str(".toml");
    dir.join(file_name)
}

/// Why `subagent <name>` resolved no definition.
#[derive(Debug, thiserror::Error)]
pub enum ResolveError {
    /// The name cannot name a subagent.
    #[error(transparent)]
    Name(#[from] DefinitionError),
    /// Neither the embedder's definitions nor the working directory hold the name.
    #[error(
        "no subagent '{name}': the embedder defines none of that name and {} holds no {name}.toml",
        local.display()
    )]
    NotFound {
        /// The name looked up.
        name: String,
        /// The working directory's `agents/`.
        local: PathBuf,
    },
    /// A definition file cannot be read.
    #[error("cannot read the subagent definition {}: {error}", path.display())]
    Read {
        /// The file.
        path: PathBuf,
        /// What failed.
        error: std::io::Error,
    },
    /// A definition file is invalid.
    #[error("the subagent definition {} is invalid: {error}", path.display())]
    Invalid {
        /// The file.
        path: PathBuf,
        /// What is wrong with it.
        error: DefinitionError,
    },
    /// A definition file names another subagent than its file name does.
    #[error("the subagent definition {} names '{declared}'; its file is named after the subagent it defines", path.display())]
    Misnamed {
        /// The file.
        path: PathBuf,
        /// The name the file declares.
        declared: String,
    },
}

/// The two places a subagent name resolves in.
#[derive(Debug, Clone, Copy)]
pub struct SubagentLookup<'a> {
    /// The definitions the embedder supplied, by name.
    pub definitions: &'a BTreeMap<String, SubagentDefinition>,
    /// The working directory whose `agents/` is looked in second.
    pub working_dir: &'a Path,
}

/// A subagent a name resolves to, as the listing of resolvable names shows
/// it: its description, or why its definition is invalid.
pub struct Listed {
    /// The subagent's name.
    pub name: String,
    /// Its description, or why its definition is invalid.
    pub description: Result<String, String>,
}

impl SubagentLookup<'_> {
    fn local(&self) -> PathBuf {
        self.working_dir.join(LOCAL_SUBAGENTS_DIR)
    }

    /// The definition `name` resolves to: the embedder's, else the working
    /// directory's.
    pub async fn resolve(&self, name: &str) -> Result<SubagentDefinition, ResolveError> {
        validate_subagent_name(name)?;
        if let Some(definition) = self.definitions.get(name) {
            return Ok(definition.clone());
        }
        if let Some(definition) = load(&definition_path(&self.local(), name), name).await? {
            return Ok(definition);
        }
        Err(ResolveError::NotFound {
            name: name.to_string(),
            local: self.local(),
        })
    }

    /// Every name that resolves, by name, with what each resolves to.
    pub async fn resolvable(&self) -> Vec<Listed> {
        let local_dir = self.local();
        let local = names_in(&local_dir).await;
        let unshadowed: Vec<String> = local
            .into_iter()
            .filter(|name| !self.definitions.contains_key(name))
            .collect();
        let mut names: Vec<String> = self.definitions.keys().cloned().collect();
        names.extend(unshadowed);
        names.sort();
        futures::future::join_all(names.into_iter().map(|name| async move {
            let description = self
                .resolve(&name)
                .await
                .map(|definition| definition.description)
                .map_err(|error| error.to_string());
            Listed { name, description }
        }))
        .await
    }
}

/// Loads the definition at `path`, which `name` was looked up as; `None`
/// when there is no such file.
async fn load(path: &Path, name: &str) -> Result<Option<SubagentDefinition>, ResolveError> {
    let source = match fs::read_to_string(path).await {
        Ok(source) => source,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(ResolveError::Read {
                path: path.to_path_buf(),
                error,
            });
        }
    };
    let definition = SubagentDefinition::parse(&source).map_err(|error| ResolveError::Invalid {
        path: path.to_path_buf(),
        error,
    })?;
    if definition.name != name {
        return Err(ResolveError::Misnamed {
            path: path.to_path_buf(),
            declared: definition.name,
        });
    }
    Ok(Some(definition))
}

/// The subagent names `dir` holds definitions for; none when it does not
/// exist or cannot be read.
async fn names_in(dir: &Path) -> Vec<String> {
    let Ok(mut entries) = fs::read_dir(dir).await else {
        return Vec::new();
    };
    let mut names = Vec::new();
    while let Some(Ok(entry)) = entries.next().await {
        let path = entry.path();
        if path
            .extension()
            .is_some_and(|extension| extension == "toml")
            && let Some(name) = path.file_stem().and_then(|stem| stem.to_str())
            && validate_subagent_name(name).is_ok()
        {
            names.push(name.to_string());
        }
    }
    names
}

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

    const DEFINITION: &str = r#"
name = "reviewer"
description = "Reviews a change."
tier = "balanced"
access = "worktrees"
prompt = '''
You review.
'''
"#;

    #[test]
    fn a_definition_parses_with_its_tier_and_access() {
        let definition = SubagentDefinition::parse(DEFINITION).unwrap();
        assert_eq!(
            definition,
            SubagentDefinition {
                name: "reviewer".to_string(),
                description: "Reviews a change.".to_string(),
                prompt: "You review.".to_string(),
                tier: Some(ModelTier::Balanced),
                access: SubagentAccess::Worktrees,
            }
        );
        let routed =
            SubagentDefinition::parse(&DEFINITION.replace("tier = \"balanced\"\n", "")).unwrap();
        assert_eq!(routed.tier, None);
    }

    #[test]
    fn an_invalid_definition_is_refused_with_why() {
        let cases = [
            (DEFINITION.replace("access = \"worktrees\"\n", ""), "access"),
            (
                DEFINITION.replace("tier = \"balanced\"", "tier = \"huge\""),
                "huge",
            ),
            (format!("{DEFINITION}model = \"fast\"\n"), "model"),
            (
                DEFINITION.replace("Reviews a change.", "  "),
                "`description` must not be empty",
            ),
            (
                DEFINITION.replace("\"reviewer\"", "\"fork\""),
                "reserved for `subagent fork`",
            ),
            (
                DEFINITION.replace("\"reviewer\"", "\"../up\""),
                "may hold only ASCII letters",
            ),
        ];
        for (source, expected) in cases {
            let error = SubagentDefinition::parse(&source).expect_err(&source);
            assert!(error.to_string().contains(expected), "{error}");
        }
    }

    fn definitions<'a>(
        named: impl IntoIterator<Item = (&'a str, &'a str)>,
    ) -> BTreeMap<String, SubagentDefinition> {
        named
            .into_iter()
            .map(|(name, description)| {
                let definition = SubagentDefinition::parse(
                    &DEFINITION
                        .replace("\"reviewer\"", &format!("\"{name}\""))
                        .replace("Reviews a change.", description),
                )
                .unwrap();
                (name.to_string(), definition)
            })
            .collect()
    }

    fn write(dir: &Path, name: &str, description: &str) {
        std::fs::create_dir_all(dir).unwrap();
        std::fs::write(
            definition_path(dir, name),
            DEFINITION
                .replace("\"reviewer\"", &format!("\"{name}\""))
                .replace("Reviews a change.", description),
        )
        .unwrap();
    }

    #[tokio::test]
    async fn an_embedder_name_shadows_the_working_directorys_and_the_lookup_says_where_it_looked() {
        let work = tempfile::tempdir().unwrap();
        let local = work.path().join(LOCAL_SUBAGENTS_DIR);
        let definitions =
            definitions([("shared", "the embedder one"), ("only-embedder", "defined")]);
        write(&local, "shared", "the local one");
        write(&local, "only-local", "the local one");
        let lookup = SubagentLookup {
            definitions: &definitions,
            working_dir: work.path(),
        };

        assert_eq!(
            lookup.resolve("shared").await.unwrap().description,
            "the embedder one"
        );
        assert_eq!(
            lookup.resolve("only-local").await.unwrap().description,
            "the local one"
        );
        let missing = lookup.resolve("missing").await.unwrap_err().to_string();
        assert_eq!(
            missing,
            format!(
                "no subagent 'missing': the embedder defines none of that name and {} holds no missing.toml",
                local.display()
            )
        );
        assert!(matches!(
            lookup.resolve("../shared").await,
            Err(ResolveError::Name(DefinitionError::InvalidName(_)))
        ));

        let listed: Vec<(String, String)> = lookup
            .resolvable()
            .await
            .into_iter()
            .map(|listed| (listed.name, listed.description.unwrap()))
            .collect();
        assert_eq!(
            listed,
            [
                ("only-embedder".to_string(), "defined".to_string()),
                ("only-local".to_string(), "the local one".to_string()),
                ("shared".to_string(), "the embedder one".to_string()),
            ]
        );
    }

    #[tokio::test]
    async fn a_definition_file_named_unlike_its_file_is_refused() {
        let work = tempfile::tempdir().unwrap();
        let local = work.path().join(LOCAL_SUBAGENTS_DIR);
        write(&local, "inner", "described");
        std::fs::rename(
            definition_path(&local, "inner"),
            definition_path(&local, "outer"),
        )
        .unwrap();
        let none = BTreeMap::new();
        let lookup = SubagentLookup {
            definitions: &none,
            working_dir: work.path(),
        };
        let error = lookup.resolve("outer").await.unwrap_err();
        assert!(
            matches!(error, ResolveError::Misnamed { ref declared, .. } if declared == "inner")
        );
    }

    #[tokio::test]
    async fn no_definitions_resolve_and_list_nothing() {
        let work = tempfile::tempdir().unwrap();
        let none = BTreeMap::new();
        let lookup = SubagentLookup {
            definitions: &none,
            working_dir: work.path(),
        };
        assert!(lookup.resolvable().await.is_empty());
        for bundled_name in ["explore", "research", "audit", "review", "plan", "general"] {
            assert!(matches!(
                lookup.resolve(bundled_name).await,
                Err(ResolveError::NotFound { .. })
            ));
        }
    }
}