Skip to content
mentoor

Topic 4 of 5 · 4 min read

Enforcing architecture with automated checks

Goal: Turn a written architecture rule into an automated check that fails CI when an agent breaks it.

A rule that only lives in a document will be broken; a rule that fails the build will not.

Check yourself · 1 of 3

Why is a forbidden-import rule in CI better than a sentence in the README for guarding a boundary?

Choose one answer

Why it matters

Agents read instructions, but they also forget them, run out of context and weigh them against the task. A human reviewer misses an import on line 214 of a 600-line diff. A machine check does not get tired. Teams call such checks architectural fitness functions: an automated test that tells you whether the code still has the shape you intended. Run in CI, they give an agent immediate feedback, and an agent that sees the failure will usually fix its own change.

How to turn a rule into a check

  1. Write the rule as a testable sentence. "Routes must not import the database client." "No cycles between top-level modules." "Domain code must not import the web framework."
  2. Pick the tool for your language. JavaScript and TypeScript: dependency-cruiser or ESLint's import rules. Python: Import Linter. Java and Kotlin: ArchUnit. Go: the depguard linter or the compiler's own internal/ package rule.
  3. Encode the rule. Describe the forbidden pair of paths and a message that says what to do instead.
  4. Make it fail the build. Add it to the same CI step as tests and lint. A warning that nobody reads protects nothing.
  5. Make the message useful. The agent will read it. "Routes must not import db. Use a function from src/services/" tells it the fix.
  6. Start from the current state. If old violations exist, record them as a baseline and block new ones only, then shrink the baseline.

Before and after

Before, the rule is a sentence in a README. After:

// .dependency-cruiser.cjs (excerpt)
module.exports = {
  forbidden: [
    {
      name: "routes-not-to-db",
      comment: "Routes call services. Add or reuse a function in src/services/.",
      severity: "error",
      from: { path: "^src/routes" },
      to: { path: "^src/db" },
    },
    { name: "no-circular", severity: "error", from: {}, to: { circular: true } },
  ],
};

Then add a script such as depcruise src --config to the CI job that runs after the tests.

What goes wrong

  • Rules too broad. If half the repo fails on day one, people disable the check. Use a baseline.
  • Rules too narrow. A rule on one folder name is dodged by a new folder. Write it against the layer, not one path.
  • The agent edits the rule. An agent told to "make CI pass" may relax the config. Review any change to the rules file with extra care, and protect it with code owners.

Examples

A Python import contract

# .importlinter
[importlinter]
root_package = shop

[importlinter:contract:layers]
name = Layers
type = layers
layers =
    shop.api
    shop.services
    shop.repositories

The layers contract lets each layer import only from those below it. If an agent makes shop.repositories import shop.api, lint-imports fails and names the exact import chain.

A Java architecture test

@ArchTest
static final ArchRule domain_is_framework_free =
    noClasses().that().resideInAPackage("..domain..")
        .should().dependOnClassesThat()
        .resideInAPackage("org.springframework..");

It runs with the unit tests. A change that pulls a Spring annotation into the domain package fails with the class names listed.

Key points

  • A written rule becomes enforceable when it is encoded as a check that fails CI, such as a forbidden-import rule or an architecture test.
  • Tools exist for common stacks: dependency-cruiser for TypeScript, Import Linter for Python and ArchUnit for Java.
  • Error messages should name the fix, because the agent reads the message and often corrects its own change.
  • Adopt a rule with a baseline of known violations so it blocks new damage without failing the whole repo on day one.
  • Treat edits to the rules file as sensitive, since an agent trying to make CI pass may weaken the rule instead of fixing the code.

Common mistakes

  • Leaving the check as a warning that nobody reads.
  • Turning on a strict rule with hundreds of old violations and no baseline.
  • Writing an error message that says what is wrong but not what to do.
  • Letting an agent change the rules file in the same change that breaks the rule.

Check yourself · 2 of 3

Which tool is aimed at enforcing architecture rules in Java projects?

Choose one answer

Check yourself · 3 of 3

Your repo has 200 old violations of a new rule. How do you adopt the rule without failing every build?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic