From 377440a11b4f66cb4decaf9c016c746daa718e70 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:24:10 +0100 Subject: [PATCH] feat: agents-md makes AGENTS.md name every workspace crate in at most 150 lines --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/agents_md.rs | 53 +++++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 4 ++ .../src/rules/tests/agents_md.rs | 27 ++++++++++ .../src/rules/tests/mod.rs | 1 + 5 files changed, 86 insertions(+) create mode 100644 crates/sourceorigin-standard/src/rules/agents_md.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/agents_md.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 2225125..4fd0e5a 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -41,6 +41,7 @@ A repository adopts the standard in four steps: - `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`. +- `agents-md`: AGENTS.md names every workspace crate, by its name in backticks or its directory, and holds at most 150 lines. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/agents_md.rs b/crates/sourceorigin-standard/src/rules/agents_md.rs new file mode 100644 index 0000000..98cd5da --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/agents_md.rs @@ -0,0 +1,53 @@ +//! `agents-md`: AGENTS.md maps the workspace, briefly. + +use std::path::Path; + +use super::agent_files::AGENTS_MD; +use super::{Finding, Repository}; + +/// The most lines AGENTS.md may hold. +const LINES: usize = 150; + +impl Repository { + /// Each workspace crate AGENTS.md names neither by its name in + /// backticks nor by its directory, and an AGENTS.md over [`LINES`]. + /// Without AGENTS.md, `agent-files` fails instead. + pub(super) fn agents_md(&self) -> Vec { + let Some(agents_md) = &self.agent_files.agents_md else { + return Vec::new(); + }; + let text = &agents_md.text; + let root = &self.metadata.workspace_root; + let mut findings: Vec = self + .metadata + .packages + .iter() + .filter_map(|package| { + let dir = package.manifest_path.parent()?.strip_prefix(root).ok()?; + let named = text.contains(&format!("`{}`", package.name)) + || (dir != Path::new("") && text.contains(&*dir.to_string_lossy())); + (!named).then(|| Finding { + path: AGENTS_MD.into(), + line: 1, + message: format!( + "AGENTS.md doesn't name the crate `{}`: add it to the map of the \ + workspace's crates, with a line saying what it is", + package.name + ), + }) + }) + .collect(); + let lines = text.lines().count(); + if lines > LINES { + findings.push(Finding { + path: AGENTS_MD.into(), + line: LINES + 1, + message: format!( + "AGENTS.md holds {lines} lines, over {LINES}: keep what an agent can't infer \ + from the code, and move the rest to the docs it links" + ), + }); + } + findings + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index b4199d3..aad4ffe 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -5,6 +5,7 @@ //! their docs. mod agent_files; +mod agents_md; mod budget; mod cargo_locked; mod comments; @@ -142,6 +143,9 @@ rules! { /// is `.agents/skills//SKILL.md`, its frontmatter's `name` is /// ``, and `.claude/skills` is a symlink to `../.agents/skills`. AgentFiles = "agent-files" => agent_files, + /// AGENTS.md names every workspace crate, by its name in backticks or + /// its directory, and holds at most 150 lines. + AgentsMd = "agents-md" => agents_md, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/agents_md.rs b/crates/sourceorigin-standard/src/rules/tests/agents_md.rs new file mode 100644 index 0000000..9ed3f74 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/agents_md.rs @@ -0,0 +1,27 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn agents_md_names_every_crate_briefly() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .add_crate("store", INHERITS) + .add_crate("cli", INHERITS) + .write( + "AGENTS.md", + &format!( + "# Repository\n\n- `core`: the model.\n- crates/store: the database.\n{}", + "More.\n".repeat(150) + ), + ); + assert_eq!( + fixture.violations(Rule::AgentsMd), + [ + "AGENTS.md:1: AGENTS.md doesn't name the crate `cli`: add it to the map of the \ + workspace's crates, with a line saying what it is (agents-md)", + "AGENTS.md:151: AGENTS.md holds 154 lines, over 150: keep what an agent can't infer \ + from the code, and move the rest to the docs it links (agents-md)", + ] + ); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 87b2950..a99256d 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -1,6 +1,7 @@ //! A fixture workspace the rules check, shared by the tests of each rule. mod agent_files; +mod agents_md; mod budget; mod cargo_locked; mod comments;