From 8284fe4bf454ce484df3e42478b6ddb252375e6a Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:22:55 +0100 Subject: [PATCH] feat: agent-files keeps agents' instructions in AGENTS.md and skills under .agents/skills --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/agent_files.rs | 128 ++++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 9 ++ .../src/rules/tests/agent_files.rs | 47 +++++++ .../src/rules/tests/mod.rs | 1 + 5 files changed, 186 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/agent_files.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/agent_files.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index c8053f2..2225125 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -40,6 +40,7 @@ A repository adopts the standard in four steps: - `workspace-manifest`: The root manifest's `[workspace]` sets `resolver = "3"`, its `[workspace.package]` sets `edition = "2024"`, and every workspace crate inherits it with `edition.workspace = true`. - `layout`: Every workspace crate lives under `crates/`, `apps/` or `tools/`, the `xtask` crate in `tools/xtask`, and `.cargo/config.toml` aliases `cargo xtask` to `run --locked --package xtask --`. - `cargo-locked`: Every `cargo build`, `check`, `clippy`, `doc`, `llvm-cov`, `nextest`, `publish`, `run` and `test` in a justfile (and `*.just`), `.github` YAML file or `t3.json` passes `--locked` (or `--frozen`). +- `agent-files`: `AGENTS.md` sits at the root, and no `CLAUDE.md` exists. Each skill is `.agents/skills//SKILL.md`, its frontmatter's `name` is ``, and `.claude/skills` is a symlink to `../.agents/skills`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/agent_files.rs b/crates/sourceorigin-standard/src/rules/agent_files.rs new file mode 100644 index 0000000..5a178e6 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/agent_files.rs @@ -0,0 +1,128 @@ +//! `agent-files`: where agents find their instructions and skills. + +use std::path::{Path, PathBuf}; + +use super::text_file::TextFile; +use super::{Error, Finding, Repository}; +use crate::workspace::Workspace; + +/// The agents' instructions, from the repository's root. +pub(super) const AGENTS_MD: &str = "AGENTS.md"; + +/// The skills' directory, and the symlink Claude Code reads it through. +const SKILLS: &str = ".agents/skills"; +const CLAUDE_SKILLS: &str = ".claude/skills"; + +/// The files agents read. +pub(super) struct AgentFiles { + /// `AGENTS.md`, if there is one. + pub(super) agents_md: Option, + /// Every `CLAUDE.md`, from the root. + claude_md: Vec, + /// Every `SKILL.md` under `.agents/skills`. + skills: Vec, + /// What `.claude/skills` links to, if it is a symlink. + claude_skills: Option, + /// The files under `.claude/skills` when it is a directory. + claude_skill_files: Vec, +} + +impl AgentFiles { + /// The agents' files under `root`. + pub(super) fn load(root: &Path) -> Result { + let workspace = Workspace::new(root); + Ok(Self { + agents_md: TextFile::load_all(root, &[AGENTS_MD])?.pop(), + claude_md: workspace + .files(&[":(glob)**/CLAUDE.md"])? + .into_iter() + .collect(), + skills: TextFile::load_all(root, &[":(glob).agents/skills/*/SKILL.md"])?, + claude_skills: std::fs::read_link(root.join(CLAUDE_SKILLS)).ok(), + claude_skill_files: workspace + .files(&[":(glob).claude/skills/**"])? + .into_iter() + .filter(|path| path != CLAUDE_SKILLS) + .collect(), + }) + } +} + +impl TextFile { + /// The `name` of a skill's frontmatter, between its opening `---` + /// lines. + fn skill_name(&self) -> Option<&str> { + let mut lines = self.text.lines(); + if lines.next()?.trim_end() != "---" { + return None; + } + lines + .take_while(|line| line.trim_end() != "---") + .find_map(|line| line.strip_prefix("name:")) + .map(|name| name.trim().trim_matches(['"', '\''])) + } +} + +impl Repository { + /// A missing `AGENTS.md`, each `CLAUDE.md`, each skill whose + /// frontmatter names another, and a `.claude/skills` that isn't the + /// symlink to `.agents/skills`. + pub(super) fn agent_files(&self) -> Vec { + let files = &self.agent_files; + let mut findings = Vec::new(); + if files.agents_md.is_none() { + findings.push(Finding { + path: AGENTS_MD.into(), + line: 1, + message: "no AGENTS.md: write the agents' instructions there".to_owned(), + }); + } + findings.extend(files.claude_md.iter().map(|path| { + Finding { + path: path.into(), + line: 1, + message: + "a CLAUDE.md: move what it says to AGENTS.md, which Claude Code reads too, \ + and delete it" + .to_owned(), + } + })); + for skill in &files.skills { + let directory = skill + .path + .parent() + .and_then(Path::file_name) + .map(|name| name.to_string_lossy()); + let name = skill.skill_name(); + if name.is_none() || name != directory.as_deref() { + findings.push(Finding { + path: skill.path.clone(), + line: 1, + message: format!( + "the skill's frontmatter names it `{}`: start the file with `---`, \ + `name: {}`, `---`", + name.unwrap_or_default(), + directory.unwrap_or_default() + ), + }); + } + } + findings.extend(files.claude_skill_files.iter().map(|path| Finding { + path: path.into(), + line: 1, + message: format!("a skill under {CLAUDE_SKILLS}: move it to {SKILLS}"), + })); + let linked = files.claude_skills.as_deref() == Some(Path::new("../.agents/skills")); + if !files.skills.is_empty() && !linked { + findings.push(Finding { + path: CLAUDE_SKILLS.into(), + line: 1, + message: format!( + "{CLAUDE_SKILLS} isn't a symlink to `../{SKILLS}`: make it one, with \ + `ln -s ../{SKILLS} {CLAUDE_SKILLS}`" + ), + }); + } + findings + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 3670991..b4199d3 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -4,6 +4,7 @@ //! rules `standard.toml` deviates from. [`describe`] lists the rules with //! their docs. +mod agent_files; mod budget; mod cargo_locked; mod comments; @@ -30,6 +31,7 @@ use std::path::{Path, PathBuf}; use crate::config::{self, Config, ConfigError, Stale}; use crate::metadata::{Metadata, MetadataError}; use crate::workspace::Workspace; +use agent_files::AgentFiles; use budget::Budget; use config_comments::Commented; use fix::Checked; @@ -136,6 +138,10 @@ rules! { /// `nextest`, `publish`, `run` and `test` in a justfile (and `*.just`), /// `.github` YAML file or `t3.json` passes `--locked` (or `--frozen`). CargoLocked = "cargo-locked" => cargo_locked, + /// `AGENTS.md` sits at the root, and no `CLAUDE.md` exists. Each skill + /// is `.agents/skills//SKILL.md`, its frontmatter's `name` is + /// ``, and `.claude/skills` is a symlink to `../.agents/skills`. + AgentFiles = "agent-files" => agent_files, } /// Why the rules couldn't be checked. @@ -294,6 +300,8 @@ struct Repository { modules: Modules, /// Each crate's ceilings of exceptions. budget: Budget, + /// `AGENTS.md`, any `CLAUDE.md`, and the skills. + agent_files: AgentFiles, /// The justfiles, workflows and `t3.json`, whose commands the rules /// read. scripts: Vec, @@ -318,6 +326,7 @@ impl Repository { budget: Budget::load(root)?, commented: Commented::load_all(root)?, scripts: TextFile::load_all(root, &cargo_locked::FILES)?, + agent_files: AgentFiles::load(root)?, metadata, sources, unparsed, diff --git a/crates/sourceorigin-standard/src/rules/tests/agent_files.rs b/crates/sourceorigin-standard/src/rules/tests/agent_files.rs new file mode 100644 index 0000000..153393a --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/agent_files.rs @@ -0,0 +1,47 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +/// A skill's file, its frontmatter naming it `name`. +fn skill(name: &str) -> String { + format!("---\nname: {name}\ndescription: Does a thing.\n---\n\n# Steps\n") +} + +#[test] +fn agents_read_agents_md_and_skills_under_agents() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write("crates/core/CLAUDE.md", "@AGENTS.md\n") + .write(".agents/skills/review/SKILL.md", &skill("review")) + .write(".agents/skills/deploy/SKILL.md", &skill("ship")) + .write(".claude/skills/debug/SKILL.md", &skill("debug")); + assert_eq!( + fixture.violations(Rule::AgentFiles), + [ + ".agents/skills/deploy/SKILL.md:1: the skill's frontmatter names it `ship`: start \ + the file with `---`, `name: deploy`, `---` (agent-files)", + ".claude/skills:1: .claude/skills isn't a symlink to `../.agents/skills`: make it \ + one, with `ln -s ../.agents/skills .claude/skills` (agent-files)", + ".claude/skills/debug/SKILL.md:1: a skill under .claude/skills: move it to \ + .agents/skills (agent-files)", + "AGENTS.md:1: no AGENTS.md: write the agents' instructions there (agent-files)", + "crates/core/CLAUDE.md:1: a CLAUDE.md: move what it says to AGENTS.md, which Claude \ + Code reads too, and delete it (agent-files)", + ] + ); +} + +#[cfg(unix)] +#[test] +fn claude_code_reads_the_skills_through_a_symlink() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write("AGENTS.md", "# Repository\n") + .write(".agents/skills/review/SKILL.md", &skill("review")); + let claude = fixture.0.root().join(".claude"); + std::fs::create_dir(&claude).expect("the directory is created"); + std::os::unix::fs::symlink("../.agents/skills", claude.join("skills")) + .expect("the symlink is created"); + assert_eq!(fixture.violations(Rule::AgentFiles), Vec::::new()); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 3bd0508..87b2950 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -1,5 +1,6 @@ //! A fixture workspace the rules check, shared by the tests of each rule. +mod agent_files; mod budget; mod cargo_locked; mod comments;