Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<name>/SKILL.md`, its frontmatter's `name` is `<name>`, and `.claude/skills` is a symlink to `../.agents/skills`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
128 changes: 128 additions & 0 deletions crates/sourceorigin-standard/src/rules/agent_files.rs
Original file line number Diff line number Diff line change
@@ -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<TextFile>,
/// Every `CLAUDE.md`, from the root.
claude_md: Vec<String>,
/// Every `SKILL.md` under `.agents/skills`.
skills: Vec<TextFile>,
/// What `.claude/skills` links to, if it is a symlink.
claude_skills: Option<PathBuf>,
/// The files under `.claude/skills` when it is a directory.
claude_skill_files: Vec<String>,
}

impl AgentFiles {
/// The agents' files under `root`.
pub(super) fn load(root: &Path) -> Result<Self, Error> {
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<Finding> {
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
}
}
9 changes: 9 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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/<name>/SKILL.md`, its frontmatter's `name` is
/// `<name>`, and `.claude/skills` is a symlink to `../.agents/skills`.
AgentFiles = "agent-files" => agent_files,
}

/// Why the rules couldn't be checked.
Expand Down Expand Up @@ -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<TextFile>,
Expand All @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/agent_files.rs
Original file line number Diff line number Diff line change
@@ -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::<String>::new());
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down