agentleFS
MatthiasEgMatthiasEg/python-hexagonal-architecture-template/AGENTS.md

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.

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
  1. Very specific:

    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 …

    line 62
  2. Very specific:

    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.

    line 97
  3. Very specific:

    Every public module, class, and function needs a Google-style docstring (ruff D).

    line 104

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

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 check

Runs 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 from application/ or infrastructure/; 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 on domain/ only and imports no infra libraries (see the forbidden-module lists in pyproject.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:

  1. src/app/domain/task.py: entity + TaskStatus state machine
  2. src/app/application/ports/output/task_repository.py: driven port
  3. src/app/application/ports/input/task_ports.py: driving ports
  4. src/app/application/use_cases/task_use_cases.py: use cases
  5. src/app/infrastructure/db/models/task.py + migration: persistence model
  6. src/app/infrastructure/db/repositories/task_repository.py: Postgres adapter
  7. src/app/infrastructure/web/schemas/task.py + controllers/tasks.py: API edge
  8. tests at every layer (unit domain FSM, use cases with in-memory repo, contract test, integration repo + API)

Commands

CommandPurpose
uv run poe checkFull validation loop (see above). Run before declaring done.
uv run poe test-quickpytest without coverage, for fast iteration
uv run poe formatruff format
uv run poe lintruff check src tests
uv run poe typecheckty check src
uv run poe archimport-linter boundary check
uv run poe devdev server (uvicorn --reload)
uv run poe db-revision -m "msg"autogenerate an Alembic migration
uv run poe migration-lintSquawk lock-safety lint on migration DDL
uv run poe openapi-snapshotregenerate the committed OpenAPI snapshot after an intentional API change
uv run poe conventionsSemgrep 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.lock by hand. Change dependencies via uv add / uv remove, then commit the regenerated lock.
  • 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. 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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.