Protect monorepo package boundaries from agents

In a monorepo, the easiest “fix” is often the most damaging one: import across package boundaries because it compiles.

Published 2026-11-09 · Translation: VI

In a monorepo, the easiest “fix” is often the most damaging one: import across package boundaries because it compiles. Agents do this constantly unless you give them hard gates.

The trap is relying on an architecture diagram or a README. Humans forget. Large diffs hide new edges. CI does not.

Pick hard gates, not soft guidelines

When this fits vs when it does not

When this fits vs when it does not

You need two layers:

  • Rules: which packages may depend on which, and what counts as “public API”.
  • Enforcement: lint and CI checks that fail on violations.

Then add a third layer for agent behavior:

  • Repair playbook: the three allowed fixes when a boundary rule fires.

When a boundary rule fails, force one of three fixes

Teach reviewers and agents the allowed moves:

  1. Move code to the correct package. If the code logically belongs with the dependency, move it.
  2. Extract to an approved shared package. Use a small shared package with a clear purpose, not a dumping ground.
  3. Add a public API wrapper. Expose a stable function/type from the owning package, then import the wrapper, not internals.

If an agent proposes “just disable the rule”, treat that as a design change. Require an ADR and a time box. Most of the time it is a shortcut hiding a real dependency leak.

Three boundary failures that compound fast

Failure modes to catch early

Failure modes to catch early

  • Mega-shared dumping ground. Everything lands in shared and boundaries vanish. Fix: limit shared packages to one purpose, and review additions.
  • Lint disables for green CI. A single disable spreads. Fix: disallow boundary rule suppressions without an ADR.
  • Widened allowlists. Cycles are “fixed” by allowing more edges. Fix: treat cycles as architecture work, not lint work.

A weekly loop that keeps boundaries healthy

Fix Flow

Fix Flow

  • Every PR: boundary checks run in CI and fail on violations.
  • Weekly: review new edges and shared package changes. If an edge is surprising, require explanation.
  • Monthly: regenerate a dependency graph artifact and scan for new cycles or “shared” sprawl.

Bottom line

Agents will take the shortest path to green tests. Your job is to make the shortest path the correct architecture. Encode boundaries as hard gates, and standardize the legal fixes when a gate fires.

Related: Multi-agent code review that catches real bugs and Migrate a codebase with agents without big-bang risk.

Share

X Facebook LinkedIn
Educational content only. Rankings reflect stated criteria at the update date—not investment advice or permanent performance guarantees.