Part 4 of 15 · The House That Diligence BuiltNext: Give Your Agent Fewer Tools →

Deep Modules Are How Agents Find Their Way

How a codebase is shaped sets how much an agent can do in it. Few deep modules, small interfaces, fast tests at the boundary, and one shared vocabulary.

AdminPublished 8 min read

The foundation is how the codebase is shaped. I've written about what agents copy from it. This piece is about the other half: how its shape decides whether an agent can find its way around at all.

There's a popular idea that code is cheap now. Write a spec, let the model compile it into code, and when something's wrong, change the spec and regenerate without reading the code. I've watched that loop produce worse code on every pass. Each regeneration thinks only about the change, never about the whole design, and the codebase decays with every edit. That's old-fashioned software entropy, running faster.

So I think the opposite is true. Bad code is the most expensive it's ever been. A codebase that's hard to change can't use most of what agents offer, and a well-shaped one gets a lot from them. Studies of teams using AI on real work show the same split. Simple greenfield work gains 30 to 40%. Complex brownfield work gains 0 to 10%. Most of us work in brownfield code.

The fundamentals that make code easy for people to change are the same ones that make it easy for agents. The difference is that agents punish their absence faster.

Deep modules, small interfaces

The most useful idea I know for this is the deep module. A deep module hides a lot of behavior behind a small, simple interface. A shallow module exposes a wide interface for very little behavior behind it.

Agents tend to write shallow modules: a helper here, a tiny wrapper there, each one a file with three exports and a dozen callers. A codebase made of those is a web. To change anything, the agent has to walk every edge, and it often doesn't. It misses the module that already does the job, or a dependency two hops away, and writes a second version next to the first.

The same code grouped into a few deep modules is a different place to work. The agent finds the module by its name, reads its interface, and makes its change inside the boundary. It doesn't need to understand the rest of the system to do that safely.

Lodestone, the task list my sessions work from, is a small example. Its rules live in one core module: parsing an action, ranking tasks, working out the order to do them in. It's pure functions behind a handful of exports like parseAction(body, byClint), and it doesn't touch the database or the network. Storage lives in a separate module, and the CLI is a thin layer over both. When a session changes how answers or ranks work, it changes the core and runs the core's tests. It never needs to read the CLI.

Design the interface, delegate the inside

Deep modules also fix a problem that has nothing to do with the agent: my own attention. Agents produce more code than I can read line by line. I can't keep the whole system in my head at that rate, and I shouldn't try.

What I can do is treat each deep module as a gray box. I design its interface: the names, the inputs, the outputs, what it promises. The agent writes the inside. I review the interface and the tests at the boundary closely, and the implementation lightly. The exceptions are areas where a subtle bug costs money or trust, such as billing, auth and anything that sends messages to people. Those get read in full.

This changes how plans are written. A plan for a change should name which modules change and how their interfaces change. If a plan doesn't mention the module map, the agent will improvise one, and improvised architecture is where shallow modules come from.

Tests at the boundary, and fast

A good codebase is easy to test. That isn't a coincidence. It's mostly a statement about module shape.

With shallow modules there's no good place to put a test. You end up testing each tiny function alone, mocking everything around it, and the tests pass while the feature is broken. With a deep module there's one obvious boundary. Wrap a test around the interface and you cover a lot of behavior at once, in the way the rest of the system actually uses it.

Speed matters more than it used to. An agent will run your tests a hundred or a thousand times more often than you will. A suite that takes ten minutes is fine for a person who runs it twice a day. For an agent, it's a wall. The agent either waits, skips it, or writes a large block of code before it checks anything.

That last failure is common. The model writes two hundred lines and only then thinks to run the type checker. The rate of feedback is the speed limit. Test-first work helps because it forces small steps: one test, make it pass, clean up, repeat. But it only works if each step's feedback comes back in seconds.

So every module gets one test command that runs in seconds, and sessions run that first, before the full suite. The bots I run follow this pattern. Each script that does real work has a test file beside it, such as dedup.mjs with dedup.test.mjs, and each one runs on its own.

Make the structure say what it means

An agent starts every session as a new hire with no one to show it around. The directory tree is the first thing it reads, so the tree should explain the system.

  • Name folders for the boundary. If two parts of the system must never import from each other, put them in two top-level folders named for that boundary, and add a check that fails on the import. The structure carries the rule, and the check enforces it.
  • Keep state where you can find it. In MedHook, my integration engine, module-level state has to live in one place. A test fails when it shows up anywhere else, and the few known exceptions are listed in an allowlist file. The agent can see the rule and the exceptions in one place, and the list can't grow quietly.
  • Keep files small. My repos cap files at about 500 lines, and new logic goes in new files. A 3,000-line file is a shallow module wearing a deep module's coat.
  • Put guidance next to the code. A short instructions file at the root and smaller ones in subfolders, pulled in when the agent works there. One big onboarding doc either grows forever or leaves things out.

One vocabulary

A lot of agent verbosity is people and the model talking past each other. I say "the inbox," the code says triage, and the model invents a third word. Its reasoning gets long because it's translating.

The fix is old: a shared language between the people, the code and the model. I keep a short glossary per repo, a table of each domain term, what it means, the name the code uses for it, and where it lives. An agent can generate the first draft by scanning the code. A person reviews it. The instructions file points to it rather than pasting it in, so it loads when the agent plans, not on every turn.

With a glossary in the plan, the model's reasoning gets shorter and the build stays closer to the plan. It also exposes naming drift. When the glossary needs two code names for one term, that's a refactor waiting to happen.

Tangled code needs a seed

Some changes cut across hundreds of files, like replacing an auth check that's woven through business logic, or moving every caller off an old client. An agent asked to do that from a description tends to spiral. Every file looks a little different, and it can't tell the essential complexity from the accidental.

What works is doing one by hand. Migrate one representative file yourself, carefully, and link that pull request as the example. The agent copies a real, reviewed change much better than it follows a paragraph describing one.

Improve the shape a little every day

None of this is a rewrite. It's small, steady work: map the modules you have, pick one cluster of related shallow code, and wrap it in a deep module with a designed interface and tests at its edge. Then do the next one. An agent is good at the search part. Ask it to find code that belongs together and propose the boundary. Keep the interface decision yourself.

Agents are tactical. They work well inside a boundary someone drew. Drawing the boundaries is still the engineer's job, and the old books on design describe how to do it better than anything written this year.

How you know it's working

  • There's a module map. Every repo has a short list of its main modules and their interfaces, and plans refer to it.
  • Each module has a fast test. One command per module that finishes in seconds, run before the full suite.
  • Agents find the existing module. Fewer duplicate helpers show up in review. A second version of something that exists means a boundary is unclear.
  • There's a glossary, and it's used. Domain terms map to one code name each. New terms get added when they're introduced, not a quarter later.
  • Wide migrations start from a seed. Any change that repeats across many files links one hand-done example.
  • Brownfield work speeds up. Time from task to merged change drops in the modules you've deepened, compared with the ones you haven't touched yet.
Next in The House That Diligence Built · Part 5 of 15Give Your Agent Fewer Tools →