use std::collections::BTreeSet;
use std::fs;
use crate::agent_assets::AGENT_ASSETS;
use crate::agents::deployer::deploy_agents;
const READ_ONLY: &str = "Read, Bash, BashOutput, KillShell, Grep, Glob";
const READ_WRITE: &str = "Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob";
const EXPECTED_TOOLS: &[(&str, &str)] = &[
(
"api-qa",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"code-analyzer",
"Read, Write, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-memory, mcp__trusty-search",
),
(
"code-critic",
"Read, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-review, mcp__trusty-search",
),
(
"dart-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"data-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"documentation",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"dotnet-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"elixir-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
("gcp-ops", READ_WRITE),
(
"golang-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"java-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"javascript-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"local-ops",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, Skill, mcp__trusty-mpm",
),
("memory-manager", "Read, Grep, mcp__trusty-memory"),
(
"mpm-agent-manager",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-mpm",
),
(
"mpm-skills-manager",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, Skill, mcp__trusty-mpm",
),
(
"nextjs-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"phoenix-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"php-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"prompt-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"python-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"qa",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"react-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"refactoring-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"research",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, WebFetch, WebSearch, mcp__trusty-memory, mcp__trusty-search",
),
(
"ruby-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"rust-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, Skill, mcp__trusty-search",
),
("secrets-manager", READ_ONLY),
(
"security",
"Read, Bash, BashOutput, KillShell, Grep, Glob, WebFetch, WebSearch, mcp__trusty-search",
),
(
"svelte-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
(
"tauri-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
("ticketing", READ_ONLY),
(
"typescript-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
("vercel-ops", READ_WRITE),
(
"version-control",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, Skill, mcp__trusty-review",
),
(
"web-qa",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__claude-in-chrome, mcp__trusty-search",
),
(
"web-ui-engineer",
"Read, Write, Edit, Bash, BashOutput, KillShell, Grep, Glob, mcp__trusty-search",
),
];
fn deploy_roster() -> (tempfile::TempDir, tempfile::TempDir) {
let src = tempfile::tempdir().expect("source tempdir");
let tgt = tempfile::tempdir().expect("target tempdir");
for (file_name, contents) in AGENT_ASSETS {
fs::write(src.path().join(file_name), contents).expect("write roster asset");
}
let skills = std::env::temp_dir().join("tm-roster-skills");
let result = deploy_agents(src.path(), tgt.path(), &skills).expect("roster deploys");
assert!(
result.failed.is_empty(),
"every roster agent must compose and validate; failed: {:?}",
result.failed
);
(src, tgt)
}
fn deployed_tools(target: &std::path::Path, stem: &str) -> Option<String> {
let raw = fs::read_to_string(target.join(format!("{stem}.md")))
.unwrap_or_else(|e| panic!("deployed agent `{stem}.md` must exist: {e}"));
raw.lines()
.find_map(|line| line.strip_prefix("tools: ["))
.and_then(|rest| rest.strip_suffix(']'))
.map(str::to_string)
}
#[test]
fn every_roster_agent_deploys_with_its_declared_tools() {
let (_src, tgt) = deploy_roster();
let mut wrong = Vec::new();
for (stem, expected) in EXPECTED_TOOLS {
match deployed_tools(tgt.path(), stem) {
Some(actual) if actual == *expected => {}
other => wrong.push(format!(
" {stem}\n expected: {expected}\n actual: {}",
other.unwrap_or_else(|| "<no `tools:` key — all tools allowed>".to_string())
)),
}
}
assert!(
wrong.is_empty(),
"{} deployed agent(s) carry the wrong `tools:` allowlist:\n{}",
wrong.len(),
wrong.join("\n")
);
}
#[test]
fn tools_table_covers_every_dispatchable_roster_agent() {
let dispatchable: BTreeSet<String> = AGENT_ASSETS
.iter()
.map(|(file_name, _)| file_name.trim_end_matches(".md").to_string())
.filter(|stem| !stem.starts_with("BASE-"))
.collect();
let tabled: BTreeSet<String> = EXPECTED_TOOLS
.iter()
.map(|(stem, _)| (*stem).to_string())
.collect();
assert_eq!(
dispatchable, tabled,
"every dispatchable roster agent needs exactly one EXPECTED_TOOLS row"
);
}
#[test]
fn base_templates_declare_no_tools() {
let (_src, tgt) = deploy_roster();
for (file_name, _) in AGENT_ASSETS {
let stem = file_name.trim_end_matches(".md");
if !stem.starts_with("BASE-") {
continue;
}
assert_eq!(
deployed_tools(tgt.path(), stem),
None,
"`{stem}` must leave `tools:` unset so it cannot override a leaf's"
);
}
}
const UNLOADABLE_SKILL_ALLOWLIST: &[(&str, &str, Option<&str>, &str)] = &[
(
"*",
"tm-prose-style",
Some("`tm-prose-style` skill, for the agents whose allowlist carries `Skill`"),
"BASE-AGENT names it for Skill holders only; the prose rules it expands \
are resident in full beside the pointer",
),
(
"*",
"tm-workflow",
Some("see `tm-workflow`, \"Worktree Discipline\", for the exact provisioning commands"),
"the rule the agent follows (fetch, never pull) is resident in the same \
bullet; provisioning is the PM's. A Read path would make trusty-code \
embed a 42 KB tm-* skill it excludes by design",
),
(
"*",
"tm-workflow",
Some("the full gate is in `tm-workflow`"),
"the fragment gate an agent must meet is resident in the bullets below \
the pointer; the merge-side enforcement is the PM's",
),
(
"ticketing",
"tm-ticketing",
None,
"ticketing's own pointers move to a Read path in a later #7727 slice",
),
];
fn bundled_skill_names() -> BTreeSet<String> {
let dir =
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../trusty-mpm/src/assets/skills");
let names: BTreeSet<String> = fs::read_dir(&dir)
.unwrap_or_else(|e| panic!("bundled skills dir {} must exist: {e}", dir.display()))
.filter_map(|entry| {
let path = entry.ok()?.path();
let stem = path.file_stem()?.to_str()?.to_string();
(path.extension()? == "md").then_some(stem)
})
.collect();
assert!(
names.len() > 1,
"no bundled skills found in {}",
dir.display()
);
names
}
fn collapse_whitespace(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
const AMBIGUOUS_SKILL_NAMES: &[(&str, &str)] = &[("tm", "the trusty-mpm CLI binary")];
fn referenced_skills(body: &str, skills: &BTreeSet<String>) -> Vec<String> {
let mut hits: Vec<(usize, String)> = Vec::new();
for name in skills {
let token = if AMBIGUOUS_SKILL_NAMES.iter().any(|(n, _)| n == name) {
format!("`{name}` skill")
} else {
format!("`{name}`")
};
for pattern in [token, format!("Skill(skill=\"{name}\"")] {
hits.extend(
body.match_indices(&pattern)
.map(|(at, _)| (at, name.clone())),
);
}
}
hits.sort();
hits.into_iter().map(|(_, name)| name).collect()
}
#[test]
fn referenced_skills_detects_a_bare_backticked_skill_name() {
let skills = bundled_skill_names();
let body = "see `tm-workflow`, \"Worktree Discipline\"; use the `self-improvement-loop` \
skill; `Skill(skill=\"tm-ticketing\")`; run `cargo test`; the `tm` \
binary; the `tm` skill.";
assert_eq!(
referenced_skills(body, &skills),
["tm-workflow", "self-improvement-loop", "tm-ticketing", "tm"]
);
}
#[test]
fn no_skill_agent_points_at_unloadable_skill() {
use crate::agents::builder::compose_agent;
use crate::agents::metadata::agent_metadata_from_str;
use crate::agents::skill_root::SKILLS_ROOT_PLACEHOLDER;
let skills = bundled_skill_names();
let src = tempfile::tempdir().expect("source tempdir");
for (file_name, contents) in AGENT_ASSETS {
fs::write(src.path().join(file_name), contents).expect("write roster asset");
}
let mut dead = Vec::new();
let mut used = BTreeSet::new();
for (stem, tools) in EXPECTED_TOOLS {
if tools.split(", ").any(|tool| tool == "Skill") {
continue;
}
let composed = compose_agent(stem, src.path()).expect("roster agent composes");
let declared = agent_metadata_from_str(&composed).skills;
let mut scanned = collapse_whitespace(&composed);
let mut agent_wide = Vec::new();
for (row, (agent, skill, text, _)) in UNLOADABLE_SKILL_ALLOWLIST.iter().enumerate() {
assert!(
*agent != "*" || text.is_some(),
"a `*` row must name its text"
);
if *agent != "*" && agent != stem {
continue;
}
match text {
Some(text) if scanned.contains(text) => {
scanned = scanned.replace(text, "");
used.insert(row);
}
Some(_) => {}
None => agent_wide.push((row, *skill)),
}
}
for skill in referenced_skills(&scanned, &skills) {
let read_path = format!("{SKILLS_ROOT_PLACEHOLDER}/{skill}/");
if let Some((row, _)) = agent_wide.iter().find(|(_, s)| *s == skill) {
used.insert(*row);
} else if !declared.contains(&skill) && !scanned.contains(&read_path) {
dead.push(format!("{stem} -> `{skill}`"));
}
}
}
dead.dedup();
assert!(
dead.is_empty(),
"agents without `Skill` point at skills they cannot load — preload the \
skill or write `Read {SKILLS_ROOT_PLACEHOLDER}/<skill>/SKILL.md`:\n {}",
dead.join("\n ")
);
let stale: Vec<_> = (0..UNLOADABLE_SKILL_ALLOWLIST.len())
.filter(|row| !used.contains(row))
.map(|row| {
UNLOADABLE_SKILL_ALLOWLIST[row]
.2
.unwrap_or(UNLOADABLE_SKILL_ALLOWLIST[row].1)
})
.collect();
assert!(
stale.is_empty(),
"allowlist rows that match nothing: {stale:?}"
);
}
#[test]
fn roster_agents_declare_no_tcode_tools() {
let (_src, tgt) = deploy_roster();
for (file_name, _) in AGENT_ASSETS {
let raw = fs::read_to_string(tgt.path().join(file_name)).expect("deployed file");
assert!(
!raw.contains("tcode_tools:"),
"`{file_name}` must not declare `tcode_tools:` — that key is \
trusty-code's own vocabulary, and this roster deploys to Claude Code"
);
}
}