feat(agents): scaffold AGENTS.md and CLAUDE.md, and add matcha agents
CI / installer (macos-14) (pull_request) Successful in 2s
CI / check (pull_request) Successful in 26s
CI / starter (bun) (pull_request) Successful in 31s
CI / starter (deno) (pull_request) Successful in 32s
CI / starter (node) (pull_request) Successful in 33s
CI / core-freshness (pull_request) Has been skipped
CI / installer (ubuntu-latest) (pull_request) Successful in 10s
CI / starter (edge) (pull_request) Successful in 5m38s

LLM coding agents have no training data on green-tea, so they write
Express. Every starter now ships an AGENTS.md, with a CLAUDE.md that
imports it. It states the rules that differ: a graph, not a chain;
needs declared in the signature; the graph validated at boot; reuse
existing tokens. It also points the agent at matcha
routes|tokens|explain --json and at the llms.txt inside the installed
core.

matcha agents writes the same files into an older project, with the
core version that project actually depends on. It leaves existing
files alone unless --force is given.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-30 20:35:23 -05:00
1 parent ea510c5112
commit 9641bd51a7
8 files changed
+253 -5

No files matched your search

+6
View File
@@ -48,6 +48,12 @@ pub enum Command {
},
/// Check the project for the misconfigurations that fail confusingly
Doctor,
/// Write AGENTS.md and CLAUDE.md: green-tea's rules for coding agents
Agents {
/// Overwrite the files if they exist
#[arg(long)]
force: bool,
},
/// Print the dependency graph (Mermaid, DOT, or JSON)
Graph {
#[arg(long, default_value = "mermaid", value_parser = ["mermaid", "dot", "json"])]
+122
View File
@@ -0,0 +1,122 @@
//! `matcha agents` — write `AGENTS.md` and `CLAUDE.md` into a project that was
//! scaffolded before matcha wrote them, so a coding agent opening it finds
//! green-tea's rules instead of applying Express habits.
use std::path::Path;
use crate::template::{self, AGENT_FILES, CORE_VERSION};
pub fn run(dir: &Path, force: bool) -> std::io::Result<()> {
let name = project_name(dir);
let version = core_version(dir).unwrap_or_else(|| CORE_VERSION.to_string());
for file in AGENT_FILES {
let path = dir.join(file);
if path.exists() && !force {
println!("· {file} exists, left as is (--force to overwrite)");
continue;
}
let contents = template::agent_file(file, &name, &version)
.expect("agent files are embedded in the binary");
std::fs::write(&path, contents)?;
println!("✓ {file}");
}
Ok(())
}
/// `package.json`'s `name`, else the directory's.
fn project_name(dir: &Path) -> String {
std::fs::read_to_string(dir.join("package.json"))
.ok()
.and_then(|pkg| field(&pkg, "\"name\""))
.or_else(|| {
std::fs::canonicalize(dir)
.ok()
.and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
})
.unwrap_or_else(|| "this project".to_string())
}
/// The core version the project actually depends on, so the file names the
/// version whose rules apply: `package.json` (npm, Bun, edge) or `deno.json`'s
/// `jsr:@green-tea/core@…` import.
fn core_version(dir: &Path) -> Option<String> {
if let Ok(pkg) = std::fs::read_to_string(dir.join("package.json")) {
if let Some(v) = field(&pkg, "\"@green-tea/core\"") {
return Some(v.trim_start_matches(['^', '~', '=']).to_string());
}
}
let deno = std::fs::read_to_string(dir.join("deno.json")).ok()?;
let at = deno.find("@green-tea/core@")? + "@green-tea/core@".len();
Some(
deno[at..]
.chars()
.take_while(|c| !matches!(c, '"' | '/'))
.collect(),
)
}
/// The string value after `key:` in a JSON text. Enough for the two flat
/// fields read here, without a JSON parse of a file that may not be valid.
fn field(json: &str, key: &str) -> Option<String> {
let rest = &json[json.find(key)? + key.len()..];
let rest = rest
.trim_start()
.strip_prefix(':')?
.trim_start()
.strip_prefix('"')?;
Some(rest[..rest.find('"')?].to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[test]
fn writes_both_files_with_the_projects_own_core_version() {
let d = tempdir().unwrap();
std::fs::write(
d.path().join("package.json"),
r#"{ "name": "shop", "dependencies": { "@green-tea/core": "26.9.0-beta.1" } }"#,
)
.unwrap();
run(d.path(), false).unwrap();
let agents = std::fs::read_to_string(d.path().join("AGENTS.md")).unwrap();
assert!(agents.contains("# AGENTS.md — shop"));
assert!(agents.contains("26.9.0-beta.1"));
assert_eq!(
std::fs::read_to_string(d.path().join("CLAUDE.md"))
.unwrap()
.trim(),
"@AGENTS.md"
);
}
#[test]
fn leaves_an_existing_file_alone_unless_forced() {
let d = tempdir().unwrap();
std::fs::write(d.path().join("AGENTS.md"), "mine").unwrap();
run(d.path(), false).unwrap();
assert_eq!(
std::fs::read_to_string(d.path().join("AGENTS.md")).unwrap(),
"mine"
);
run(d.path(), true).unwrap();
assert_ne!(
std::fs::read_to_string(d.path().join("AGENTS.md")).unwrap(),
"mine"
);
}
#[test]
fn reads_the_version_from_a_deno_import() {
let d = tempdir().unwrap();
std::fs::write(
d.path().join("deno.json"),
r#"{ "imports": { "@green-tea/core": "jsr:@green-tea/core@26.9.0-beta.2" } }"#,
)
.unwrap();
assert_eq!(core_version(d.path()).as_deref(), Some("26.9.0-beta.2"));
}
}
+7
View File
@@ -2,6 +2,7 @@ mod ask;
mod check;
mod cli;
mod cmd_add;
mod cmd_agents;
mod cmd_create;
mod cmd_doctor;
mod cmd_graph;
@@ -88,6 +89,12 @@ fn main() {
std::process::exit(1);
}
}
Command::Agents { force } => {
if let Err(e) = cmd_agents::run(std::path::Path::new("."), force) {
eprintln!("error: {e}");
std::process::exit(1);
}
}
Command::Doctor => {
if let Err(e) = cmd_doctor::run(std::path::Path::new(".")) {
eprintln!("error: {e}");
+45 -5
View File
@@ -50,11 +50,7 @@ fn write_dir(dir: &Dir, dest: &Path, name: &str) -> std::io::Result<()> {
std::fs::create_dir_all(parent)?;
}
let contents = std::str::from_utf8(f.contents())
.map(|s| {
s.replace("{{project_name}}", name)
.replace("{{core_version}}", CORE_VERSION)
.into_bytes()
})
.map(|s| render(s, name, CORE_VERSION).into_bytes())
.unwrap_or_else(|_| f.contents().to_vec());
std::fs::write(out, contents)?;
}
@@ -63,6 +59,28 @@ fn write_dir(dir: &Dir, dest: &Path, name: &str) -> std::io::Result<()> {
Ok(())
}
fn render(template: &str, project_name: &str, core_version: &str) -> String {
template
.replace("{{project_name}}", project_name)
.replace("{{core_version}}", core_version)
}
/// The files that tell a coding agent how this project and green-tea work:
/// `AGENTS.md` (read by Codex, OpenCode and others) and a `CLAUDE.md` that
/// imports it. `matcha new` writes them with the starter; `matcha agents`
/// writes them into a project that predates them.
pub const AGENT_FILES: [&str; 2] = ["AGENTS.md", "CLAUDE.md"];
/// Renders one of [`AGENT_FILES`] for a project already on `core_version`.
pub fn agent_file(file: &str, project_name: &str, core_version: &str) -> Option<String> {
let f = SHARED.get_file(file)?;
Some(render(
std::str::from_utf8(f.contents()).ok()?,
project_name,
core_version,
))
}
#[cfg(test)]
mod tests {
use super::*;
@@ -122,6 +140,28 @@ mod tests {
}
}
/// Every starter tells a coding agent where green-tea's rules are, with the
/// version it was scaffolded on, and points it at the introspection
/// commands rather than at guessing.
#[test]
fn every_runtime_scaffolds_the_agent_files() {
for rt in [Runtime::Node, Runtime::Deno, Runtime::Bun, Runtime::Edge] {
let d = tempdir().unwrap();
write_starter(d.path(), "my-api", rt).unwrap();
let agents = std::fs::read_to_string(d.path().join("AGENTS.md")).unwrap();
assert!(agents.contains("# AGENTS.md — my-api"), "{rt:?}");
assert!(agents.contains(CORE_VERSION), "{rt:?}");
assert!(!agents.contains("{{"), "{rt:?} left a placeholder");
assert!(agents.contains("matcha tokens --json"), "{rt:?}");
assert!(
agents.contains("node_modules/@green-tea/core/llms.txt"),
"{rt:?}"
);
let claude = std::fs::read_to_string(d.path().join("CLAUDE.md")).unwrap();
assert_eq!(claude.trim(), "@AGENTS.md", "{rt:?}");
}
}
#[test]
fn deno_writes_shared_and_deno_overlay() {
let d = tempdir().unwrap();