An AI-agent-friendly codebase: AGENTS.md, fast tests, machine-checked conventions
An agent codes only as well as its feedback loop: build and test commands written down in AGENTS.md, module tests under a minute, architecture conventions checked by ArchUnit instead of review comments, clear module boundaries. Which rule goes where.
On this page
1. The problem
A team gives a coding agent 60 tickets in a month. 40% of PRs are sent back: controllers calling repositories directly, LocalDateTime.now() everywhere, @Disabled on failing tests. All three conventions are things "everyone on the team knows". The suite takes 14 minutes, so the agent only runs tests for the files it changed.
2. Key idea
An agent only knows what's written down or reported by a machine. A convention that lives in a reviewer's head doesn't exist for the agent. Put each rule at the cheapest level that's still reliable enough:
| Where | Use when | Don't use when |
|---|---|---|
| AGENTS.md | build/test commands, folder structure, "for feature X, read package Y" | rules whose violation is serious |
| Automated checks (ArchUnit, Checkstyle, tests) | architectural invariants, banned APIs | rules that need judgment ("readable function names") |
| Hooks, permissions, branch protection | don't edit applied migrations, don't read .env, don't push to main |
style suggestions |
| Human review | design, business logic, trade-offs | things a machine can check |
A review comment repeated a third time becomes a check. Claude Code's docs say it plainly: CLAUDE.md is context, not enforced configuration; to block an action, use a hook. New team members hit exactly these problems; agents just surface them faster.
3. In Java
@AnalyzeClasses(packages = "com.shop", importOptions = ImportOption.DoNotIncludeTests.class)
class ArchitectureTest {
@ArchTest
static final ArchRule controllersGoThroughServices =
noClasses().that().resideInAPackage("..web..")
.should().dependOnClassesThat().resideInAPackage("..persistence..")
.because("controllers call services, not repositories (AGENTS.md: Architecture)");
@ArchTest
static final ArchRule noSystemClock =
noClasses().should().callMethod(LocalDateTime.class, "now")
.because("inject java.time.Clock so tests control time");
@ArchTest
static final ArchRule featuresAreIndependent =
slices().matching("com.shop.(*)..").should().beFreeOfCycles();
}Other languages: TypeScript uses dependency-cruiser; Python has import-linter; Go uses golangci-lint and internal/ directories.
A sample AGENTS.md and sub-60-second tests
## Commands
- Unit tests for one module: ./mvnw -q -pl order test (≈ 20 s)
- Everything before a PR: ./mvnw verify (≈ 4 min)
## Architecture
- web -> service -> persistence; ArchitectureTest checks it.
- Time comes from an injected Clock, never now().
## When a test fails
- Don't add @Disabled or retries; report back with the log.Keep the file under about 200 lines, with no secrets. Fast tests: separate unit from integration, share Testcontainers across the suite, use Clock.fixed(...) instead of Thread.sleep. Flaky tests teach agents that "red means run again".
4. Working with AI
Hand to AI: scanning the repo for build/test commands and timings, drafting AGENTS.md, turning repeated review comments into ArchUnit rules, finding flaky tests in CI history.
Check yourself in AI-written output:
- Every command in AGENTS.md runs on a clean machine, with accurate timings.
- No generic lines ("write clean code", "follow best practices").
- Every "always" or "never" rule has a matching automated check.
- ArchUnit rules have a
because(...)explaining the fix, and pass on current code (or useFreezingArchRule). - No secrets or sensitive internal URLs in context files.
5. Exercises
Exercise 1: Which lines stay in AGENTS.md
The AI's draft AGENTS.md has five lines. For each: keep, turn into an automated check, or delete?
1. Always write clean, maintainable code.
2. Run one module's unit tests: ./mvnw -q -pl order test (≈ 20 s).
3. Never call a repository from a controller.
4. Staging DB: postgres://admin:[email protected]/shop
5. Before changing the payment flow, read payment/README.md.Solution 1
- 1, delete: not specific, not verifiable, wastes context.
- 2, keep: exactly what an agent needs most, with a timing.
- 3, keep a short line and add an ArchUnit rule: "never" must be machine-checked.
- 4, delete now and rotate the password: a secret in the repo, readable by everyone. Write "get the staging connection from the vault".
- 5, keep: points to detailed context without stuffing it into the main file.
Exercise 2: Read an incident
For two weeks, 30% of agent PRs fail CI in OrderExpiryTest; a rerun passes. In the last three PRs the agent added @RetryingTest(3) or @Disabled, saying "flaky test, unrelated". The test uses LocalDateTime.now() and Thread.sleep(1000). Diagnose it, say what to do in 15 minutes, fix the root cause.
Solution 2
- The test depends on the real clock and sleep timing; on a busy CI machine one second isn't enough. The agent can't tell flakiness from a real failure, so it takes the cheapest route to green: disable the test.
- 15 minutes: reject PRs that add
@Disabled/retries; log the test in a known-flaky issue. - Root fix: inject a
Clock, useClock.fixed(...)and advance time manually, dropThread.sleep. Add thenoSystemClockrule, a CI check that fails when a PR adds@Disabledwithout an issue link, and an AGENTS.md line "report unrelated red tests, don't disable them".
Summary
- Agent quality depends on the feedback loop: context, speed, clarity of errors.
- AGENTS.md is short and specific, with commands and timings.
- "Never" rules must be machine-checked or hard-blocked.
- ArchUnit rules carry a
becausethat explains the fix. - Module tests under 60 seconds, not flaky.
Further reading: CI/CD for Spring Boot backends.
Comments
Community rules