trusty-agents-common 0.4.0

Stable agent-plugin surface (ToolExecutor, AgentPlugin, ToolResult) shared by trusty-agents and external agent crates.
Documentation
//! Read-only projection of a deployed agent's frontmatter for display and
//! diagnostics (DOC-42, issue #2889).
//!
//! Why: `tm doctor`'s dangling-skill check (§SPEC-AGENTSKILLS-03) and the
//! `tm agent list`/`tm agent show` CLI (§SPEC-AGENTSKILLS-04) both need to
//! read an agent file's parsed frontmatter — including the new `skills:`
//! array — WITHOUT re-running full inheritance composition: a deployed file
//! is already flattened (see [`crate::agents::builder::compose_agent`]'s
//! `skills:` union-and-emit), and a source file with no `extends:` parses
//! identically either way. Duplicating [`crate::agents::builder`]'s
//! internal frontmatter grammar here would drift from the parser used at
//! compose/deploy time, so this module reuses
//! [`crate::agents::builder::split_frontmatter`] (`pub(crate)` for exactly
//! this purpose — `metadata` and `builder` are sibling modules in the same
//! crate) and projects its crate-private [`Frontmatter`] into a public,
//! stable-shaped struct external callers may read. Moved to
//! trusty-agents-common (#2892) from `trusty-mpm::core::agent_metadata`
//! alongside `agent_deployer`, which depends on it
//! (`deploy_agents_filtered` populates `DeployResult::declared_skills` via
//! [`agent_metadata_from_str`]); `trusty-mpm` re-exports every item here from
//! `core::agent_metadata` for source compatibility.
//! What: [`AgentMetadata`] plus [`agent_metadata_from_str`] (parse an
//! in-memory document — used by the co-deploy path, which already holds the
//! freshly composed string) and [`read_agent_metadata`] (read a file from
//! disk — used by doctor and the CLI, which look at already-deployed files).
//! Both spellings of an agent's declared domain are projected (#4511):
//! `role:` (this crate's composer emit, and trusty-mpm's source assets) and
//! `agent_type:` (what claude-mpm-format deployed artifacts carry instead).
//! Test: `metadata_from_str_reads_skills`, `metadata_from_missing_file_is_default`,
//! `metadata_from_str_malformed_frontmatter_is_default`,
//! `metadata_from_str_reads_agent_type` in this file.

use std::path::Path;

use super::builder::{Frontmatter, split_frontmatter};

/// A deployed agent's parsed frontmatter, projected for display/diagnostics.
///
/// Why: callers outside the compose pipeline (doctor, `tm agent`) need to
/// read an agent's declared metadata — most importantly its `skills:` list —
/// without depending on `agent_builder`'s internal, inheritance-aware
/// [`Frontmatter`] type.
/// What: a plain-old-data mirror of the frontmatter fields trusty-mpm
/// recognizes. `skills` is always populated (empty `Vec` when absent), never
/// `None`, since an agent with no declared dependencies is the common case.
/// Test: `metadata_from_str_reads_skills`, `metadata_from_str_all_fields`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AgentMetadata {
    /// The `name:` field.
    pub name: Option<String>,
    /// The `role:` field.
    pub role: Option<String>,
    /// The `agent_type:` field — the SAME declaration as [`Self::role`] under
    /// the spelling claude-mpm-format artifacts use (#4511).
    ///
    /// Why: a deployed `.claude/agents/*.md` artifact that originated from
    /// claude-mpm declares `agent_type:` and no `role:` at all, so a consumer
    /// reading one through this projection saw no domain and had to fall back
    /// to a fail-closed default. Carrying both spellings lets a consumer
    /// resolve the domain from ONE reader rather than re-scanning the file
    /// with a second, divergent grammar.
    /// What: `None` when the key is absent (the norm for trusty-mpm's own
    /// source assets and for anything this crate's composer emitted, since
    /// [`crate::agents::builder::compose_agent`] canonicalises on `role:` and
    /// never emits this key). It is a DECLARATION, not a resolved value:
    /// consumers must translate it through their own reviewed table before it
    /// reaches any authorization decision — trusty-agents does exactly that in
    /// `agents::claude_mpm_role::normalize_role`.
    /// Test: `metadata_from_str_reads_agent_type`,
    /// `metadata_from_str_agent_type_absent_is_none`.
    pub agent_type: Option<String>,
    /// The `description:` field.
    pub description: Option<String>,
    /// The `model:` field.
    pub model: Option<String>,
    /// The `extends:` field (present on source files; a fully composed
    /// deployed file never carries it — see `compose_agent`'s doc comment).
    pub extends: Option<String>,
    /// Declared skill dependencies (DOC-42), in declaration order,
    /// de-duplicated. Empty when the agent declares none.
    pub skills: Vec<String>,
    /// Maximum-output-tokens budget (#2897, epic #2892), mirroring tcode's
    /// TOML `AgentConfig`. `None` when the agent declares none — trusty-mpm
    /// agents never set this key.
    pub max_tokens: Option<u32>,
    /// Allowed tool names (#2897, epic #2892), mirroring tcode's TOML
    /// `AgentConfig`. `None` when the agent (and its whole `extends` chain)
    /// never declares a `tools:` key — trusty-mpm agents never set this key,
    /// so they always project to `None`. `Some(vec![])` is a deliberate
    /// deny-all override, distinct from `None`; the two must never be
    /// conflated (see the `Option`, not bare `Vec`, on
    /// `builder::Frontmatter::tools`). Unlike `skills`, this list is
    /// OVERRIDE-merged across an `extends` chain rather than unioned (see
    /// `builder::merge_frontmatter`).
    pub tools: Option<Vec<String>>,
}

impl From<Frontmatter> for AgentMetadata {
    fn from(fm: Frontmatter) -> Self {
        Self {
            name: fm.name,
            role: fm.role,
            agent_type: fm.agent_type,
            description: fm.description,
            model: fm.model,
            extends: fm.extends,
            skills: fm.skills,
            max_tokens: fm.max_tokens,
            tools: fm.tools,
        }
    }
}

/// Parse an in-memory agent document's frontmatter into [`AgentMetadata`].
///
/// Why: the co-deployment path (`agent_deployer::deploy_agents_filtered`)
/// already holds the freshly composed content string in memory after calling
/// `compose_agent` — re-reading it from disk would be a redundant IO round
/// trip for the exact bytes just written.
/// What: delegates to [`split_frontmatter`]; a parse failure (malformed or
/// unterminated frontmatter) yields [`AgentMetadata::default`] rather than
/// propagating an error — metadata projection is best-effort display/
/// diagnostic data, never a hard dependency.
/// Test: `metadata_from_str_reads_skills`,
/// `metadata_from_str_malformed_frontmatter_is_default`.
pub fn agent_metadata_from_str(raw: &str) -> AgentMetadata {
    match split_frontmatter(raw) {
        Ok((fm, _body)) => AgentMetadata::from(fm),
        Err(_) => AgentMetadata::default(),
    }
}

/// Read an agent file from disk and parse its frontmatter into
/// [`AgentMetadata`].
///
/// Why: doctor's dangling-skill probe and `tm agent list`/`show` both work
/// over already-deployed files on disk (`~/.claude/agents/*.md`), not
/// in-memory compose output.
/// What: reads `path`, then delegates to [`agent_metadata_from_str`]. A
/// missing or unreadable file yields [`AgentMetadata::default`] (empty
/// skills, no fields) rather than an error — callers treat "file absent" and
/// "file present with no metadata" identically for display purposes.
/// Test: `metadata_from_missing_file_is_default`, `read_metadata_from_disk`.
pub fn read_agent_metadata(path: &Path) -> AgentMetadata {
    match std::fs::read_to_string(path) {
        Ok(raw) => agent_metadata_from_str(&raw),
        Err(_) => AgentMetadata::default(),
    }
}

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

    #[test]
    fn metadata_from_str_reads_skills() {
        let doc = "---\nname: code-critic\nrole: qa\nskills: [code-review-standards, systematic-debugging]\n---\n\nBody.\n";
        let meta = agent_metadata_from_str(doc);
        assert_eq!(meta.name.as_deref(), Some("code-critic"));
        assert_eq!(meta.role.as_deref(), Some("qa"));
        assert_eq!(
            meta.skills,
            vec!["code-review-standards", "systematic-debugging"]
        );
    }

    #[test]
    fn metadata_from_str_all_fields() {
        let doc = "---\nname: rust-engineer\nrole: engineer\ndescription: Rust specialist\nmodel: sonnet\nextends: base-engineer\nskills: [toolchains-rust-core]\n---\n\nBody.\n";
        let meta = agent_metadata_from_str(doc);
        assert_eq!(meta.name.as_deref(), Some("rust-engineer"));
        assert_eq!(meta.description.as_deref(), Some("Rust specialist"));
        assert_eq!(meta.model.as_deref(), Some("sonnet"));
        assert_eq!(meta.extends.as_deref(), Some("base-engineer"));
        assert_eq!(meta.skills, vec!["toolchains-rust-core"]);
    }

    /// #4511: the claude-mpm-format artifact shape — `agent_type:` carries
    /// the domain and `role:` is absent entirely. Before this projection
    /// existed a consumer reading such a file saw NO declared domain at all.
    #[test]
    fn metadata_from_str_reads_agent_type() {
        let doc = "---\nname: aws-ops\nagent_type: ops\nversion: \"1.0.0\"\n---\n\nBody.\n";
        let meta = agent_metadata_from_str(doc);
        assert_eq!(meta.agent_type.as_deref(), Some("ops"));
        assert_eq!(meta.role, None, "this dialect declares no `role:`");
    }

    /// The two spellings are independent fields, never merged or aliased here
    /// — deciding which one wins is the CONSUMER's reviewed policy, not this
    /// read-only projection's.
    #[test]
    fn metadata_from_str_agent_type_absent_is_none() {
        let meta = agent_metadata_from_str("---\nname: plain\nrole: engineer\n---\n\nBody.\n");
        assert_eq!(meta.role.as_deref(), Some("engineer"));
        assert_eq!(meta.agent_type, None);
    }

    #[test]
    fn metadata_from_str_no_skills_is_empty_vec() {
        let doc = "---\nname: plain\nrole: engineer\n---\n\nBody.\n";
        let meta = agent_metadata_from_str(doc);
        assert!(meta.skills.is_empty());
    }

    #[test]
    fn metadata_from_str_malformed_frontmatter_is_default() {
        // Unterminated frontmatter (`split_frontmatter` errors) must degrade
        // to an empty default, never panic or propagate.
        let doc = "---\nname: broken\n\nno closing fence\n";
        let meta = agent_metadata_from_str(doc);
        assert_eq!(meta, AgentMetadata::default());
    }

    #[test]
    fn metadata_from_missing_file_is_default() {
        let meta = read_agent_metadata(Path::new("/nonexistent/agent.md"));
        assert_eq!(meta, AgentMetadata::default());
    }

    #[test]
    fn read_metadata_from_disk() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("critic.md");
        std::fs::write(
            &path,
            "---\nname: critic\nrole: qa\nskills: [code-review-standards]\n---\n\nBody.\n",
        )
        .unwrap();
        let meta = read_agent_metadata(&path);
        assert_eq!(meta.name.as_deref(), Some("critic"));
        assert_eq!(meta.skills, vec!["code-review-standards"]);
    }
}