python-hexagonal-architecture-template
AGENTS.md0 starsChanged 5 months agoMITView on GitHub
Single source of truth for coding agents (and humans) working in this repository. CLAUDE.md, .github/copilot-instructions.md, and .cursor/rules/ all point here. Do not duplicate rules; change them in this file. A hexagonal-architecture (ports and adapters) FastAPI backend template. Layer boundaries are enforced in CI by import-linter, so the architecture cannot silently rot. Python 3.13, uv, async SQLAlchemy + Alembic + Postgres. The Task resource is the canonical vertical slice; mirror it when adding a feature. Runs the full validation loop, fail-fast…
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 35,196 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository7 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/architecture.mdcCursor rule
- docs/llms.txtllms.txt
- add-featureSkill
- add-migrationSkill
- check-fixSkill
The file
We split this AGENTS.md into the rules it gives an agent
3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 62
Follow docs/adding-a-feature.md, an ordered, file-by-file runbook that mirrors the Task slice from the domain outward and ends at a green …
- Very specific: line 97
Do not weaken the gates to pass: not the coverage floor (fail_under in pyproject.toml), not the ruff rules, not the import-linter contracts.
- Very specific: line 104
Every public module, class, and function needs a Google-style docstring (ruff D).
≈ “Common” is an estimate: at least 1 in 5 of the 45,344 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AGENTS.md
Single source of truth for coding agents (and humans) working in this repository.
CLAUDE.md, .github/copilot-instructions.md, and .cursor/rules/ all point here. Do not
duplicate rules; change them in this file.
What this project is
A hexagonal-architecture (ports and adapters) FastAPI backend template. Layer boundaries are
enforced in CI by import-linter, so the architecture cannot silently rot. Python 3.13, uv, async
SQLAlchemy + Alembic + Postgres. The Task resource is the canonical vertical slice; mirror it
when adding a feature.
The one command
uv run poe checkRuns the full validation loop, fail-fast and cheapest-first:
ruff -> ty -> import-linter -> deptry -> pip-audit -> pytest (+coverage, +schemathesis)A change is not done until uv run poe check is green. Integration tests and schemathesis need
Docker (testcontainers spins up Postgres). This loop is deterministic: pinned tool versions
(uv.lock), seeded test order (pytest-randomly), and derandomized fuzzing (schemathesis) mean
the same input yields the same pass/fail on every run. Do not add nondeterminism (wall-clock
assertions, network calls, unseeded randomness) to tests.
Architecture rules (enforced)
Imports point inward only. Outer layers know inner layers; the reverse never happens.
infrastructure/ -> application/ -> domain/
(outer) (middle) (inner, zero framework imports)src/app/domain/is pure business logic. No imports fromapplication/orinfrastructure/; no FastAPI, SQLAlchemy, loguru, pydantic-settings. Entities are immutable; rule violations raise domain exceptions, not HTTP errors.src/app/application/is where use cases orchestrate domain objects.ports/input/are driving contracts (what the app can do);ports/output/are driven contracts (what the app needs: repositories, unit of work, logger). It depends ondomain/only and imports no infra libraries (see the forbidden-module lists inpyproject.toml).src/app/infrastructure/holds every concrete technology: FastAPI controllers/DTOs, SQLAlchemy repositories, Alembic migrations, logging, telemetry, config. It implements the output ports.
uv run poe arch fails the build on any inward-only violation. If you need the domain to reach
a database or an HTTP client, you have the dependency backwards: define an output port in
application/ports/output/ and implement it in infrastructure/.
The UnitOfWork is the sole commit authority; get_db_session does not commit. Mutating use
cases take (repository, uow, logger) and call await uow.commit() explicitly. See
ADR-0003.
Adding a feature
Follow docs/adding-a-feature.md, an ordered, file-by-file runbook
that mirrors the Task slice from the domain outward and ends at a green uv run poe check. The
Task slice files, in dependency order:
src/app/domain/task.py: entity +TaskStatusstate machinesrc/app/application/ports/output/task_repository.py: driven portsrc/app/application/ports/input/task_ports.py: driving portssrc/app/application/use_cases/task_use_cases.py: use casessrc/app/infrastructure/db/models/task.py+ migration: persistence modelsrc/app/infrastructure/db/repositories/task_repository.py: Postgres adaptersrc/app/infrastructure/web/schemas/task.py+controllers/tasks.py: API edge- tests at every layer (unit domain FSM, use cases with in-memory repo, contract test, integration repo + API)
Commands
| Command | Purpose |
uv run poe check | Full validation loop (see above). Run before declaring done. |
uv run poe test-quick | pytest without coverage, for fast iteration |
uv run poe format | ruff format |
uv run poe lint | ruff check src tests |
uv run poe typecheck | ty check src |
uv run poe arch | import-linter boundary check |
uv run poe dev | dev server (uvicorn --reload) |
uv run poe db-revision -m "msg" | autogenerate an Alembic migration |
uv run poe migration-lint | Squawk lock-safety lint on migration DDL |
uv run poe openapi-snapshot | regenerate the committed OpenAPI snapshot after an intentional API change |
uv run poe conventions | Semgrep rules enforcing conventions import-linter cannot express (ADR-0003) |
Constraints (off-limits)
- Do not break the layer rule to make something compile. Fix the dependency direction.
- Do not edit
uv.lockby hand. Change dependencies viauv add/uv remove, then commit the regenerated lock. - Do not weaken the gates to pass: not the coverage floor (
fail_underinpyproject.toml), not the ruff rules, not the import-linter contracts. If a gate is wrong, change it deliberately in one commit and say why. - Do not add auth, rate-limiting, a task queue, or an LLM adapter to the baseline. They are explicit non-goals; document them as recipes instead.
- Conventional Commits are enforced by commitizen (
type: subject, e.g.feat(domain): ...,fix: ...,test: ...,docs: ...,refactor: ...,chore: ...). - Every public module, class, and function needs a Google-style docstring (ruff
D). - Line length 120. Type hints on all functions and variables.
Where design decisions live
Architecture Decision Records are in docs/adr/. Read them before changing the architecture or the validation loop. They record what was decided and, more usefully, what was rejected and why.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

