anda_engine 0.16.6

Agents engine for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs.
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! File-backed skills with bounded discovery, immutable catalogs and package resource access.
//!
//! Register [`SkillManager::tools`] for `skills_manager`, `skills_list`, and `skills_read`.
//! Skills default to inline execution; only `execution: subagent` creates `SA_` callables.
//! Register the same manager as a [`SubAgentSet`] when delegated skills are wanted.
//! Bundled text resources are read through `skills_read` without granting general file or shell
//! access. Script execution remains subject to the host's workspace and shell permissions.
//!
//! [`SkillManager::load`] explicitly reloads membership. Hosts can call [`SkillManager::invalidate`]
//! after filesystem notifications; the next async read/list coalesces a refresh. Known reads
//! revalidate their file and metadata, while name misses also refresh. Synchronous catalogs and
//! callable lookups always use the last published snapshot, never perform filesystem I/O.

use crate::{
    context::BaseCtx,
    extension::{hooked_call, tool_definition},
    hook::DynToolHook,
    subagent::{SubAgent, SubAgentSet},
};
use anda_core::{
    Agent, BoxError, FunctionDefinition, Resource, Tool, ToolOutput, ToolSet, select_resources,
};
use parking_lot::RwLock;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
    any::Any,
    collections::BTreeMap,
    path::{Path, PathBuf},
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
};

mod catalog;
mod discovery;
mod tools;
mod types;
pub use catalog::*;
use catalog::{Catalog, Entry, digest, truncate};
#[cfg(test)]
use discovery::MAX_SKILL_FILE_BYTES;
pub use tools::*;
pub use types::*;
/// Arguments for reading a skill via the [`SkillManager`] tool.
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SkillArgs {
    /// Skill name in kebab-case (e.g. 'pdf-processing'). Returns the matching SKILL.md content. When `execution` is `inline`, follow that content yourself; when it is `subagent`, call the returned `callable` with a self-contained prompt instead.
    pub name: String,
}

/// Typed hook for skills-manager tool calls.
pub type SkillToolHook = DynToolHook<SkillArgs, SkillContentOutput>;

/// Content returned by the [`SkillManager`] tool.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct SkillContentOutput {
    /// Skill name from the SKILL.md frontmatter.
    pub name: String,
    /// Skill description from the SKILL.md frontmatter.
    pub description: String,
    /// How this skill runs: `inline` (follow `content` yourself) or `subagent` (delegate).
    pub execution: SkillExecution,
    /// Callable name for `subagent` skills, absent for `inline` ones.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub callable: Option<String>,
    /// Base directory of the skill, for resolving the bundled files `content` references.
    pub base_dir: String,
    /// Path to SKILL.md, relative to a configured skills directory when possible.
    pub path: String,
    /// Full SKILL.md content including YAML frontmatter and Markdown body.
    pub content: String,
}

/// Host admission predicate. Rejected identities are absent from catalogs, reads and callables.
/// The callback executes under internal locks and must not call back into this manager.
pub type SkillFilter = Arc<dyn Fn(&Skill) -> bool + Send + Sync>;

#[derive(Default)]
struct Registry {
    catalog: Arc<Catalog>,
    subagents: BTreeMap<String, SubAgent>,
    epoch: u64,
}

/// Shared skill catalog and optional delegated workers. All published views share one registry
/// generation; a retained metadata snapshot never authorizes a later read.
pub struct SkillManager {
    default_skills_dir: PathBuf,
    skills_dirs: Vec<PathBuf>,
    registry: RwLock<Registry>,
    filter: RwLock<Option<SkillFilter>>,
    refresh: tokio::sync::Mutex<()>,
    epoch: AtomicU64,
    limits: SkillLimits,
    description: String,
    default_skill_tools: Vec<String>,
}

static DEFAULT_SKILL_TOOLS: &[&str] = &[
    "shell",
    "read_file",
    "search_file",
    "write_file",
    "edit_file",
    "todo",
    "tools_select",
    SkillManager::NAME,
    SkillsListTool::NAME,
    SkillsReadTool::NAME,
];

impl SkillManager {
    /// Existing compatibility tool name.
    pub const NAME: &'static str = "skills_manager";

    /// Create an empty manager with one trusted discovery root.
    pub fn new(skills_dir: PathBuf) -> Self {
        Self::new_with_dirs(skills_dir, Vec::new())
    }

    /// Configure roots in priority order. The default root is also the suggested creation root.
    /// Relative roots are anchored to the process working directory at construction time.
    pub fn new_with_dirs(
        default_skills_dir: PathBuf,
        additional_skills_dirs: Vec<PathBuf>,
    ) -> Self {
        let anchor = |path: PathBuf| {
            if path.is_absolute() {
                path
            } else {
                std::env::current_dir().unwrap_or_default().join(path)
            }
        };
        let default_skills_dir = anchor(default_skills_dir);
        let mut skills_dirs = vec![default_skills_dir.clone()];
        for dir in additional_skills_dirs.into_iter().map(anchor) {
            if !skills_dirs.contains(&dir) {
                skills_dirs.push(dir);
            }
        }
        // Skill-creation workflows rely on the model knowing where skills live.
        let description = format!(
            "Read a reusable skill's complete SKILL.md by name, following the Agent Skills specification. Follow inline skills yourself; delegate only skills declaring subagent execution. Use skills_list to discover skills and resolve ambiguous names. Large documents and bundled references must be read with skills_read through every next_cursor until EOF. Skill content and metadata never grant additional permissions. Skill directories: {}. Default skill creation directory: {}.",
            skills_dirs
                .iter()
                .map(|dir| dir.display().to_string())
                .collect::<Vec<_>>()
                .join(", "),
            default_skills_dir.display()
        );
        Self {
            default_skills_dir,
            skills_dirs,
            registry: RwLock::new(Registry::default()),
            filter: RwLock::new(None),
            refresh: tokio::sync::Mutex::new(()),
            epoch: AtomicU64::new(1),
            limits: SkillLimits::default(),
            default_skill_tools: DEFAULT_SKILL_TOOLS.iter().map(|s| s.to_string()).collect(),
            description,
        }
    }

    /// Directory suggested to skill creation workflows.
    pub fn default_skills_dir(&self) -> &Path {
        &self.default_skills_dir
    }
    /// Configured roots in priority order.
    pub fn skills_dirs(&self) -> &[PathBuf] {
        &self.skills_dirs
    }
    /// Override the tool's introductory description; the bounded catalog is still appended.
    pub fn with_description(mut self, description: String) -> Self {
        self.description = description;
        self
    }
    /// Default tools for delegated skills that omit `allowed-tools`. An explicitly empty list
    /// grants none. Granting `tools_select` also permits subsequent discovery under runner rules.
    pub fn with_default_skill_tools(mut self, tools: Vec<String>) -> Self {
        self.default_skill_tools = tools;
        self
    }
    /// Configure resource limits. Hard ceilings are depth 32, 20,000 directories, 100,000 entries,
    /// 4,096 skills, 128 MiB combined content, 64 KiB catalog and 512 KiB responses. Counts and
    /// total content have a minimum of one; catalog/response minima are 128/1,024 bytes.
    pub fn with_limits(mut self, mut limits: SkillLimits) -> Self {
        limits.max_depth = limits.max_depth.min(32);
        limits.max_directories = limits.max_directories.clamp(1, 20_000);
        limits.max_entries = limits.max_entries.clamp(1, 100_000);
        limits.max_skills = limits.max_skills.clamp(1, 4_096);
        limits.max_total_bytes = limits.max_total_bytes.clamp(1, 128 * 1024 * 1024);
        limits.catalog_bytes = limits.catalog_bytes.clamp(128, 64 * 1024);
        limits.response_bytes = limits.response_bytes.clamp(1_024, 512 * 1024);
        self.limits = limits;
        self
    }
    /// Register all three static local tools over this manager's shared state.
    pub fn tools(self: &Arc<Self>) -> Result<ToolSet<BaseCtx>, BoxError> {
        let mut tools = ToolSet::new();
        tools.add(self.clone())?;
        tools.add(Arc::new(SkillsListTool::new(self.clone())))?;
        tools.add(Arc::new(SkillsReadTool::new(self.clone())))?;
        Ok(tools)
    }
    /// Invalidate membership after a filesystem or host configuration change. No I/O is done here.
    pub fn invalidate(&self) {
        self.epoch.fetch_add(1, Ordering::AcqRel);
    }

    /// Replace the host admission policy and immediately remove rejected identities. Newly
    /// admitted files are discovered on the next async access or explicit reload.
    pub fn set_skill_filter(&self, filter: Option<SkillFilter>) {
        let mut current = self.filter.write();
        *current = filter;
        let mut registry = self.registry.write();
        let entries = registry.catalog.entries.values().cloned().collect();
        let report = SkillLoadReport::default();
        self.publish(&mut registry, entries, report, current.as_ref());
        self.invalidate();
    }
    /// Last published immutable metadata and diagnostics, without filesystem I/O.
    pub fn catalog(&self) -> Arc<SkillCatalogSnapshot> {
        Arc::new(self.registry.read().catalog.snapshot())
    }
    /// Compatibility reload API. Inspect [`Self::catalog`] for per-file failures and truncation.
    pub async fn load(&self) -> Result<(), BoxError> {
        let report = self.reload().await?;
        for diagnostic in &report.diagnostics {
            log::warn!(
                "skill {} at {}: {}",
                diagnostic.kind,
                diagnostic.path.display(),
                diagnostic.message
            );
        }
        Ok(())
    }
    /// Rescan and publish all roots atomically. Missing roots become empty; invalid or unreadable
    /// files are omitted with diagnostics. No stale entries survive a successful publication.
    pub async fn reload(&self) -> Result<SkillLoadReport, BoxError> {
        let _guard = self.refresh.lock().await;
        self.scan_and_publish().await
    }
    async fn ensure_loaded(&self) -> Result<(), BoxError> {
        if self.registry.read().epoch == self.epoch.load(Ordering::Acquire) {
            return Ok(());
        }
        let _guard = self.refresh.lock().await;
        if self.registry.read().epoch != self.epoch.load(Ordering::Acquire) {
            self.scan_and_publish().await?;
        }
        Ok(())
    }
    async fn scan_and_publish(&self) -> Result<SkillLoadReport, BoxError> {
        let epoch = self.epoch.load(Ordering::Acquire);
        let mut entries = Vec::new();
        let mut report = SkillLoadReport::default();
        let mut bytes = 0usize;
        let mut visited = 0;
        'roots: for (rank, root) in self.skills_dirs.iter().enumerate() {
            let scan = discovery::scan_files(root, &self.limits).await;
            report.rejected += scan.report.rejected;
            report.truncated |= scan.report.truncated;
            for diagnostic in scan.report.diagnostics {
                report.note(&diagnostic.kind, diagnostic.path, diagnostic.message);
            }
            for path in scan.files {
                if visited == self.limits.max_skills {
                    report.truncated = true;
                    report.note("limit", root.clone(), "Global skill count limit reached");
                    break 'roots;
                }
                visited += 1;
                match discovery::read_entry(root, &path, rank).await {
                    Ok((entry, _, size)) => {
                        bytes = bytes.saturating_add(size);
                        if bytes > self.limits.max_total_bytes {
                            report.truncated = true;
                            report.note("limit", path, "Combined skill content limit reached");
                            break 'roots;
                        }
                        entries.push(entry);
                    }
                    Err(error) => {
                        report.rejected += 1;
                        report.note("invalid", path, error);
                    }
                }
            }
        }
        let filter = self.filter.read();
        let mut registry = self.registry.write();
        self.publish(&mut registry, entries, report, filter.as_ref());
        registry.epoch = epoch;
        Ok(registry.catalog.report.clone())
    }

    fn publish(
        &self,
        registry: &mut Registry,
        entries: Vec<Entry>,
        mut report: SkillLoadReport,
        filter: Option<&SkillFilter>,
    ) {
        let mut catalog = Catalog::default();
        for entry in entries {
            if filter.is_some_and(|filter| !filter(&entry.skill)) {
                report.rejected += 1;
                continue;
            }
            // A nested/repeated root cannot create a second identity for the same file.
            catalog.entries.entry(entry.id.clone()).or_insert(entry);
        }
        let mut by_name: BTreeMap<String, Vec<&Entry>> = BTreeMap::new();
        for entry in catalog.entries.values() {
            by_name
                .entry(entry.skill.agent_name.clone())
                .or_default()
                .push(entry);
        }
        for (name, mut copies) in by_name {
            copies.sort_by_key(|entry| (entry.rank, &entry.skill.base_dir));
            let first = copies[0];
            // Callable hash collisions must never route one frontmatter name to another.
            let collision = copies
                .iter()
                .any(|entry| entry.skill.frontmatter.name != first.skill.frontmatter.name);
            if collision || copies.get(1).is_some_and(|entry| entry.rank == first.rank) {
                report.note(
                    "conflict",
                    first.skill.base_dir.clone(),
                    format!(
                        "multiple skills named {:?}; select by ID",
                        first.skill.frontmatter.name
                    ),
                );
            } else {
                catalog.winners.insert(name, first.id.clone());
            }
        }
        report.loaded = catalog.entries.len();
        let identities = catalog
            .entries
            .values()
            .map(|entry| (&entry.id, &entry.fingerprint, entry.rank))
            .collect::<Vec<_>>();
        catalog.fingerprint =
            digest(&serde_json::to_vec(&(identities, &report)).expect("catalog data serializes"));
        report.generation = registry.catalog.report.generation
            + u64::from(catalog.fingerprint != registry.catalog.fingerprint);
        catalog.report = report;
        let mut agents = BTreeMap::new();
        for (name, id) in &catalog.winners {
            let entry = &catalog.entries[id];
            if !entry.skill.is_subagent() {
                continue;
            }
            let mut agent = SubAgent::from(entry.skill.as_ref());
            if !entry.skill.declares_tools() {
                agent.tools = self.default_skill_tools.clone();
            }
            if registry.catalog.winners.get(name) == Some(id)
                && let Some(old) = registry.subagents.get(name)
            {
                agent.subsessions = old.subsessions.clone();
            }
            agents.insert(name.clone(), agent);
        }
        registry.subagents = agents;
        registry.catalog = Arc::new(catalog);
    }

    async fn read_selected(
        &self,
        selector: &str,
    ) -> Result<(SkillSummary, String, Entry), BoxError> {
        if !selector.starts_with("skill://") {
            validate_skill_name(selector)?;
        }
        if selector.len() > 128 {
            return Err("Invalid skill identity".into());
        }
        self.ensure_loaded().await?;
        let mut last_error = None;
        for attempt in 0..2 {
            let entry = self.registry.read().catalog.resolve(selector)?.cloned();
            if let Some(entry) = entry {
                let path = entry.skill.base_dir.join("SKILL.md");
                match discovery::read_entry(&entry.root, &path, entry.rank).await {
                    Ok((fresh, content, _))
                        if fresh.id == entry.id && fresh.fingerprint == entry.fingerprint =>
                    {
                        let summary = self.current_summary(&fresh)?;
                        if !selector.starts_with("skill://") && !summary.active {
                            return Err(
                                "Skill name changed precedence during read; retry or select by ID"
                                    .into(),
                            );
                        }
                        return Ok((summary, content, fresh));
                    }
                    Ok(_) => (),
                    Err(error) => last_error = Some(error),
                }
            }
            if attempt == 0 {
                self.reload().await?;
            }
        }
        if let Some(error) = last_error {
            return Err(error);
        }
        let registry = self.registry.read();
        if let Some(entry) = registry.catalog.entries.values().find(|entry| {
            entry
                .skill
                .base_dir
                .file_name()
                .is_some_and(|name| name == selector)
        }) {
            return Err(format!(
                "SKILL.md frontmatter name {:?} must match requested skill name {selector:?}",
                entry.skill.frontmatter.name
            )
            .into());
        }
        if let Some(diagnostic) = registry
            .catalog
            .report
            .diagnostics
            .iter()
            .find(|diagnostic| {
                diagnostic
                    .path
                    .parent()
                    .and_then(Path::file_name)
                    .is_some_and(|name| name == selector)
            })
        {
            return Err(diagnostic.message.clone().into());
        }
        Err(format!("skill {selector:?} not found; reload after changing skill files").into())
    }

    fn current_summary(&self, entry: &Entry) -> Result<SkillSummary, BoxError> {
        let filter = self.filter.read();
        if filter.as_ref().is_some_and(|filter| !filter(&entry.skill)) {
            return Err("Skill is no longer available".into());
        }
        let registry = self.registry.read();
        let current = registry
            .catalog
            .entries
            .get(&entry.id)
            .ok_or("Skill is no longer available")?;
        if current.fingerprint != entry.fingerprint {
            return Err("Skill changed during read; retry".into());
        }
        Ok(registry.catalog.summary(current))
    }

    async fn read_skill_action(&self, args: SkillArgs) -> Result<SkillContentOutput, BoxError> {
        // Preserve the existing name-only wire contract; IDs are accepted by skills_read.
        validate_skill_name(&args.name)?;
        let (summary, content, _) = self.read_selected(&args.name).await?;
        let target = summary.base_dir.join("SKILL.md");
        let path = self
            .skills_dirs
            .iter()
            .find_map(|root| target.strip_prefix(root).ok())
            .map(crate::extension::fs::normalize_relative_path)
            .unwrap_or_else(|| target.display().to_string());
        let output = SkillContentOutput {
            name: summary.name,
            description: summary.description,
            execution: summary.execution,
            callable: summary.callable,
            base_dir: summary.base_dir.display().to_string(),
            path,
            content,
        };
        if serde_json::to_vec(&output)?.len() > self.limits.response_bytes {
            return Err(format!("Skill exceeds inline response budget; read the complete document with skills_read using skill {:?} and follow next_cursor until EOF", summary.id).into());
        }
        Ok(output)
    }

    /// Retrieve the winning skill by its normalized callable name, without I/O.
    pub fn get_skill(&self, lowercase_name: &str) -> Option<Skill> {
        let registry = self.registry.read();
        let id = registry.catalog.winners.get(lowercase_name)?;
        Some(registry.catalog.entries.get(id)?.skill.as_ref().clone())
    }
    /// All unambiguous delegated workers, including explicit-only skills, for host inspection.
    pub fn subagents(&self) -> Vec<SubAgent> {
        self.registry.read().subagents.values().cloned().collect()
    }
    /// Compatibility view of unambiguous winning skills, keyed by normalized callable name.
    pub fn list(&self) -> BTreeMap<String, Skill> {
        let registry = self.registry.read();
        registry
            .catalog
            .winners
            .iter()
            .map(|(name, id)| {
                (
                    name.clone(),
                    registry.catalog.entries[id].skill.as_ref().clone(),
                )
            })
            .collect()
    }
    fn skills_catalog(&self) -> String {
        let registry = self.registry.read();
        let mut entries = registry
            .catalog
            .entries
            .values()
            .filter(|entry| entry.metadata.policy.allow_implicit_invocation)
            .collect::<Vec<_>>();
        entries.sort_by_key(|entry| (&entry.skill.frontmatter.name, entry.rank, &entry.id));
        if entries.is_empty() {
            return String::new();
        }
        let header = "\nLoaded skills (name, execution, description):";
        let lines = entries
            .into_iter()
            .map(|entry| {
                let description = entry
                    .metadata
                    .interface
                    .short_description
                    .as_deref()
                    .unwrap_or(&entry.skill.frontmatter.description)
                    .split_whitespace()
                    .collect::<Vec<_>>()
                    .join(" ");
                (
                    format!(
                        "\n- {} [{}]: ",
                        entry.skill.frontmatter.name, entry.skill.execution
                    ),
                    description,
                )
            })
            .collect::<Vec<_>>();
        let full_bytes = header.len()
            + lines
                .iter()
                .map(|(prefix, description)| prefix.len() + description.len())
                .sum::<usize>();
        if full_bytes <= self.limits.catalog_bytes {
            return format!(
                "{header}{}",
                lines
                    .into_iter()
                    .map(|(prefix, description)| prefix + &description)
                    .collect::<String>()
            );
        }
        // Allocate names first, then divide remaining bytes fairly between descriptions.
        let mut output = header.to_string();
        let mut names_bytes = 0;
        let mut count = 0;
        for (prefix, _) in &lines {
            if header.len() + names_bytes + prefix.len() + 80 > self.limits.catalog_bytes {
                break;
            }
            names_bytes += prefix.len();
            count += 1;
        }
        let description_budget = self
            .limits
            .catalog_bytes
            .saturating_sub(header.len() + names_bytes + 80)
            / count.max(1);
        for (prefix, description) in lines.iter().take(count) {
            output.push_str(prefix);
            output.push_str(truncate(description, description_budget));
        }
        let omitted = lines.len() - count;
        if omitted > 0 {
            output.push_str(&format!(
                "\n{omitted} additional skills omitted; use skills_list to discover them."
            ));
        } else {
            output
                .push_str("\nDescriptions shortened; use skills_list and skills_read for details.");
        }
        truncate(&output, self.limits.catalog_bytes).into()
    }
}

impl SubAgentSet for SkillManager {
    fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
        self
    }
    fn contains_lowercase(&self, name: &str) -> bool {
        self.registry.read().subagents.contains_key(name)
    }
    fn get_lowercase(&self, name: &str) -> Option<SubAgent> {
        self.registry.read().subagents.get(name).cloned()
    }
    fn definitions(&self, names: Option<&[String]>) -> Vec<FunctionDefinition> {
        let registry = self.registry.read();
        registry
            .subagents
            .iter()
            .filter(|(name, _)| match names {
                Some(names) => names
                    .iter()
                    .any(|requested| requested.eq_ignore_ascii_case(name)),
                None => registry
                    .catalog
                    .winners
                    .get(*name)
                    .and_then(|id| registry.catalog.entries.get(id))
                    .is_some_and(|entry| entry.metadata.policy.allow_implicit_invocation),
            })
            .map(|(_, agent)| agent.definition())
            .collect()
    }
    fn select_resources(&self, name: &str, resources: &mut Vec<Resource>) -> Vec<Resource> {
        self.get_lowercase(&name.to_ascii_lowercase())
            .map(|agent| select_resources(resources, &agent.supported_resource_tags()))
            .unwrap_or_default()
    }
}
impl Tool<BaseCtx> for SkillManager {
    type Args = SkillArgs;
    type Output = SkillContentOutput;
    fn name(&self) -> String {
        Self::NAME.into()
    }
    fn description(&self) -> String {
        format!("{}{}", self.description, self.skills_catalog())
    }
    fn definition(&self) -> FunctionDefinition {
        let mut definition = tool_definition::<Self::Args>(self.name(), self.description());
        definition.parameters["description"] = "Read a reusable skill's SKILL.md file content by skill name. Create or update skills with separately authorized file tools, then reload or invalidate the manager. Use skills_read for paginated documents and package resources.".into();
        definition
    }
    fn group(&self) -> Option<anda_core::ToolGroupInfo> {
        Some(tools::skill_group())
    }
    async fn call(
        &self,
        ctx: BaseCtx,
        args: Self::Args,
        _resources: Vec<Resource>,
    ) -> Result<ToolOutput<Self::Output>, BoxError> {
        use anda_core::StateFeatures;
        hooked_call(&ctx, args, |args| async {
            let cancellation = ctx.cancellation_token();
            tokio::select! {
                _ = cancellation.cancelled() => Err("call was cancelled".into()),
                result = self.read_skill_action(args) => result.map(ToolOutput::new),
            }
        })
        .await
    }
}

#[cfg(test)]
mod catalog_tests;
#[cfg(test)]
mod tests;