Skip to main content

agentmux_srv_core/backend/
builtin_skills.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Skills that ship with AgentMux: put into the catalog once, for every agent, and then left alone.
5//!
6//! - **Once.** Each skill has a stable id. The first start that sees an id not yet recorded in
7//!   `db_builtin_seeds` creates the skill and records the id. The record stays when the user deletes the skill,
8//!   so a delete is final; a release that adds a new skill to [`BUILTINS`] seeds only that one.
9//! - **Never overwritten.** After the first write nothing here touches the row, so the user's edits stay. A
10//!   later release that improves the text does not replace it.
11//! - **Every agent.** The skill is global and reaches every agent and every agent to come (`db_skill_reach`), so it is
12//!   in each agent's skills without a bind, and the user can switch that off in Memory > Skills.
13//! - **Quiet on conflict.** If the user already has a global skill with the same name, the built-in one is
14//!   skipped and recorded as seeded, not retried at every start.
15//!
16//! Seeded into the identity store (the catalog), like the Global Memory seed. Skill text is public: it names
17//! no host, account or private system.
18
19use std::sync::Arc;
20
21use super::storage::store::Store;
22use super::storage::{Skill, StoreError};
23
24/// One skill AgentMux ships.
25pub struct Builtin {
26    /// The skill's id for ever: the seed record and the catalog row are keyed on it.
27    pub id: &'static str,
28    pub name: &'static str,
29    pub trigger: &'static str,
30    pub description: &'static str,
31    /// The skill's body (the `SKILL.md` text after the frontmatter).
32    pub content: &'static str,
33}
34
35/// The skill format that becomes `.claude/skills/<name>/SKILL.md` (`agent_config::SKILL_TYPE_AGENT_SKILL`).
36const SKILL_TYPE: &str = crate::backend::agent_config::SKILL_TYPE_AGENT_SKILL;
37
38pub const BUILTINS: &[Builtin] = &[Builtin {
39    id: "7c1e5d2a-3b64-4f0e-9a58-0d15c4b0d1a2",
40    name: "disk-cleanup",
41    trigger: "disk-cleanup",
42    description: "Free disk space safely: measure AgentMux's own leftovers (old version folders, dev homes, instances, caches), say what is safe to remove, and ask the user before deleting anything.",
43    content: include_str!("builtin_skills/disk-cleanup.md"),
44}];
45
46/// The key a skill's seed record is kept under.
47fn seed_key(id: &str) -> String {
48    format!("skill:{id}")
49}
50
51/// Seeds [`BUILTINS`] into the catalog on a start. Failures are logged and retried at the next start.
52pub fn seed_on_startup(store: &Arc<Store>) {
53    let now = agentmux_common::time::now_ms();
54    let seeded = seed(store, BUILTINS, now);
55    if seeded > 0 {
56        tracing::info!(seeded, "built-in skills: seeded");
57    }
58}
59
60/// Seeds the skills of `list` that were never seeded; returns how many were created.
61pub(crate) fn seed(store: &Store, list: &[Builtin], now_ms: i64) -> usize {
62    let mut created = 0;
63    for b in list {
64        let key = seed_key(b.id);
65        match store.builtin_seed_done(&key) {
66            Ok(true) => continue,
67            Ok(false) => {}
68            Err(e) => {
69                tracing::warn!(skill = b.name, "built-in skills: could not read the seed record: {e}");
70                continue;
71            }
72        }
73        match seed_one(store, b, now_ms) {
74            Ok(made) => {
75                if let Err(e) = store.builtin_seed_mark(&key, now_ms) {
76                    tracing::warn!(skill = b.name, "built-in skills: could not record the seed: {e}");
77                    continue;
78                }
79                created += usize::from(made);
80            }
81            Err(e) => tracing::warn!(skill = b.name, "built-in skills: not seeded, will retry at the next start: {e}"),
82        }
83    }
84    created
85}
86
87/// Creates the skill and its reach. `Ok(false)` when there was nothing to create: the row exists already, or a
88/// global skill of the same name does (both count as seeded). A store failure is an `Err`, to retry.
89fn seed_one(store: &Store, b: &Builtin, now_ms: i64) -> Result<bool, StoreError> {
90    if store.skill_get(b.id)?.is_some() {
91        // A start that wrote the skill but failed before its reach left a skill that reaches nobody, and
92        // the seed unrecorded: finish that. A reach row that exists, even one switched off, is the user's.
93        if store.skill_reach_map()?.contains_key(b.id) {
94            return Ok(false);
95        }
96        store.skill_set_reach(b.id, true, true, now_ms)?;
97        return Ok(true);
98    }
99    let skill = Skill {
100        id: b.id.to_string(),
101        name: b.name.to_string(),
102        trigger: b.trigger.to_string(),
103        skill_type: SKILL_TYPE.to_string(),
104        description: b.description.to_string(),
105        content: b.content.to_string(),
106        is_global: true,
107        created_at: now_ms,
108        updated_at: now_ms,
109    };
110    match store.skill_upsert_unique_global(&skill) {
111        Ok(()) => {}
112        Err(StoreError::Other(msg)) if msg.contains("already exists") => {
113            tracing::info!(skill = b.name, "built-in skills: a global skill with this name exists; leaving it");
114            return Ok(false);
115        }
116        Err(e) => return Err(e),
117    }
118    store.skill_set_reach(b.id, true, true, now_ms)?;
119    Ok(true)
120}
121
122#[cfg(test)]
123#[path = "builtin_skills_tests.rs"]
124mod tests;