Topic 1 of 5 · 4 min read
Module boundaries and dependency direction
Goal: Find a boundary violation or dependency cycle in an agent's change and say where the code should live.
A module boundary is a rule about who may import whom, and an agent will cross it the moment crossing is the shortest way to make the task pass.
Check yourself · 1 of 3
An agent adds an import so that a route file can query the database directly. What is wrong with this change?
Why it matters
Agents such as Claude Code, Codex, Cursor and Copilot optimise for the task in front of them. They see a function they need, in a file they can open, and they import it. They do not feel the cost of the arrow they just drew. Humans pay that cost later: a change in one module breaks three others, tests need half the system to start, and a cycle makes the build order unclear.
How to check a change
- Know the layers. Most services have a shape like
routes -> services -> repositories -> database. Arrows point down. Lower layers never import upper ones. - Read the import lines first. In a diff, scan only the added
importandrequirelines. Each new line is a new dependency. Ask: is this arrow allowed? - Look for reach-through. A route that queries the database directly, or a module that imports another module's private file instead of its public entry point, skips a layer.
- Look for cycles. If
billingimportsordersand the change makesordersimportbilling, you now have a cycle. Neither can be understood, tested or moved alone. - Decide where the code belongs. Put the logic in the layer that owns the concept, and let the caller go through that layer's public function.
Before and after
// routes/invoice.ts (agent's first version)
import { db } from "../db/client";
import { taxRate } from "../services/billing/internal/rates";
export async function getInvoice(id: string) {
const row = await db.invoices.findOne({ id });
return { ...row, tax: row.net * taxRate(row.country) };
}
The route talks to the database and reaches into a private folder of billing. The fixed version calls one public function:
import { invoiceWithTax } from "../services/billing";
export const getInvoice = (id: string) => invoiceWithTax(id);
The query and the tax rule now live in billing, where the next change to them will be made.
What goes wrong
- The shortcut becomes the pattern. Agents copy what they find in the repo. One route that skips the service layer teaches the next ten changes to do the same.
- Type-only imports hide cycles. A cycle that "works" at runtime today breaks when a bundler or a test loader changes the order.
- A utils folder becomes the escape hatch. When two modules cannot import each other, the agent moves the shared code to
utils, which then imports both. The cycle is hidden, not removed.
Tell the agent the rule before it starts, and check the added imports when it finishes. Section four of this skill shows how to make a tool check them for you.
Examples
The route that opened the database
A prompt says: "Add a paid-orders count to the dashboard route." The agent adds import { db } from "../db/client" to routes/dashboard.ts and writes the query inline. The test passes.
The review comment: the query belongs in the orders repository, behind a function such as countPaidOrders(). The route calls that function. The next person who changes how "paid" is defined edits one file, not every route.
The cycle created by a convenient import
orders already imports customers to read the customer name. To show an order count on the customer page, the agent makes customers import orders.
Now neither module loads without the other. The fix is to give the customer page a read-model function in a higher layer, such as customerSummary(), that imports both. Arrows stay one-directional.
Key points
- A boundary is a rule about allowed imports, and every new import line in a diff is a new dependency to approve.
- Dependencies should point one way, from upper layers such as routes to lower ones such as repositories, never back.
- Agents cross boundaries because the shortcut passes the task, so the rule must be stated up front and checked after.
- Reaching into another module's private file, or querying the database from a route, skips a layer even when it works.
- A dependency cycle is a design error; moving shared code to a utils folder usually hides the cycle without removing it.
Common mistakes
- Reviewing only the logic and never reading the added import lines.
- Accepting a new utils file as the fix for a cycle without checking what it imports.
- Never telling the agent the layer rules, then blaming it for breaking them.
- Allowing one boundary shortcut because it is small, so later changes copy it.
Check yourself · 2 of 3
Module orders imports module billing. A change makes billing import orders too. What have you created?
Check yourself · 3 of 3
A cycle appears between two modules, and the agent moves the shared code into a new utils folder that imports both modules. What is the best review response?
Practice items, not counted towards your mastery.
Start here
- Architectural Decision Records (adr.github.io) (opens in a new tab)DocsFree
Templates and examples for recording context, decision and consequences.
- Software Architecture Guide (martinfowler.com) (opens in a new tab)ArticleFree
Clear essays on layers, coupling and keeping a design healthy over time.
- Architectural fitness function (Thoughtworks Technology Radar) (opens in a new tab)ArticleFree
The idea behind automated checks that guard an architecture.