# Agent-first code principles

Fellow applications are written and changed by people and coding agents together. For an agent, the cost of a
change is dominated by three things: correction cycles (try, fail, read the error, retry), the files it must read to
understand one behaviour, and ambiguity (several valid ways to do the same thing, all of which it explores). These
principles minimise those costs. Classic principles (DRY, SOLID, KISS, YAGNI, Clean Code) are kept where they serve
that goal and reinterpreted where they work against it. The rules marked with an id are enforced by
`fellow check`; see [`fellow docs adr/0014-agent-first-code-principles`](/docs/adr/0014-agent-first-code-principles) for the reasoning.

## 1. One way to do each thing (KISS)

There is one place and one shape for every kind of code: a route per file in `routes/`, business rules in
`services/`, background work in `jobs/` and `workflows/`, contracts written with Zod. Generators produce that shape;
use them instead of inventing a variation. Fewer concepts mean shorter docs and skills, and less exploration.

## 2. Keep behaviour local

Everything that defines a behaviour lives together, inside its module: contract, validation, permission, service and
test. A reader should understand a feature from one module, mostly from two or three files.

- Files stay small and single-purpose (**FW-CODE-001**, warning above 400 lines). This is the useful part of the
  single-responsibility principle: an agent reads a whole file to change any part of it.
- Only `public.ts` re-exports, as the module boundary. Barrels elsewhere add a hop (**FW-CODE-004**).
- Do not wrap a function just to forward its arguments (**FW-CODE-005**). Every layer must add a responsibility.

## 3. Be explicit, not clever

Where code comes from must be visible where it is used.

- No decorators (**FW-CODE-002**): they attach behaviour that is not visible at the call site. Use `defineRoute`,
  `defineJob`, middleware lists and plain functions.
- No dependency-injection containers (**FW-CODE-003**): import implementations explicitly. Tests pass dependencies
  as parameters or use the framework test doubles (`memoryChannel()` with `configureNotifications`, `createTestApp`).
- Prefer a little duplication over a generic abstraction with options. This is how DRY applies here:
  **one source of truth for knowledge** (a contract generates types, validation, OpenAPI and the client; a
  permission is declared once in the manifest), **but not for code shape**. Generated code that repeats a pattern is
  cheap to read and to change; an abstraction shared by unrelated features is not.
- Open/closed and dependency inversion are applied at module boundaries (events, `public.ts`), not inside modules.

## 4. Build only what was asked (YAGNI)

Agents tend to generate more than the task needs. The task plan states the scope (`fellow task:new`), and the
Evidence Report lists files changed outside it. Do not add options, hooks or layers for hypothetical future needs;
add them when a task needs them.

## 5. Make it verifiable

- Every route has a test (**FW-TEST-001**), and every service is reached by a test, directly or through a tested
  route (**FW-CODE-006**). Tests sit in the module's `tests/` directory, next to the code they prove.
- Failures must be cheap to fix: diagnostics carry an id, a file, a line and a suggestion, so a correction takes one
  step instead of an investigation.

## 6. Names and comments

- Names are descriptive and searchable: the context engine finds code by its words (`archiveDocument`, not
  `handle2`). Use the domain vocabulary of the module consistently.
- Comments explain **why** (a decision, a constraint, a trap), not **what** the next line does.
- Do not split functions into fragments to make them short: a 30-line function read once beats six 5-line
  functions spread across a file.

## Measuring

These rules are a hypothesis about agent efficiency. The agent benchmark (`benchmarks/agent`) records tool calls,
correction cycles and out-of-scope changes per task; real-agent runs with and without the rules decide whether they
stay as they are.
