4.1 KiB
4.1 KiB
Quality Guidelines
Code quality standards for backend development.
Overview
Backend quality gates are centered around:
- Static checks (
ruff,basedpyright) via pre-commit - Pytest suites under
backend/tests - Layering and contract discipline from
backend/AGENTS.md
Evidence:
.pre-commit-config.yaml(basedpyright+ruff, scoped to^backend/)pyproject.toml(pytestconfig, test paths, live marker)backend/AGENTS.md(layering, logging, error contract, DB and env rules)
Forbidden Patterns
1) Error swallowing
- Forbidden by project rule: do not catch/log and silently continue.
- Root rule source:
AGENTS.md.
Legacy examples to avoid extending:
backend/src/v1/users/dependencies.py(_verify_user_with_supabase) catches broad exceptions and returnsNonefallback.backend/src/services/base/service_interface.py(initialize_registered_services/close_registered_services) catches broad exceptions and continues lifecycle flow.
2) Runtime print() usage
- Backend runtime must use
core.logging, notprint(). - Rule source:
backend/AGENTS.md.
3) Unstructured HTTP error contracts
- Business/API errors must not rely only on free-text
detail; use stablecode(+ optionalparams). - Sources:
backend/AGENTS.mddocs/protocols/common/http-error-codes.md
4) Reading env by ad-hoc os.getenv in runtime
- Runtime configuration must go through
core.config.settings. - Source:
backend/AGENTS.mdand implementation inbackend/src/core/config/settings.py.
Required Patterns
Layering discipline
- Use
schema -> repository -> serviceflow. - Repository: query/CRUD +
flush; no transaction ownership. - Service: authz/business rules + commit/rollback boundary.
Evidence:
backend/src/v1/todo/repository.pyvsbackend/src/v1/todo/service.pybackend/src/v1/schedule_items/repository.pyvsbackend/src/v1/schedule_items/service.pybackend/src/core/db/session.pydocs explicitly state caller (service) owns commit/rollback
Typed boundary models
- Prefer Pydantic models with strict extras handling for API/request contracts.
- Evidence across schemas:
backend/src/v1/todo/schemas.pybackend/src/v1/schedule_items/schemas.pybackend/src/v1/users/schemas.py
Protocol alignment
- If API contract changes (error codes/data formats), update corresponding
docs/protocols/**docs in same change. - Source: root
AGENTS.md+ backend rules.
Testing Requirements
Observed baseline:
- Unit tests:
backend/tests/unit/** - Integration tests:
backend/tests/integration/** - Live e2e tests separated by marker
live:backend/tests/e2e/README-live.md
Pytest setup evidence:
pyproject.tomltestpaths = ["backend/tests"]markers = ["live: ..."]
Practical expectations for backend changes:
- Add/adjust tests for changed behavior where possible (especially contract/business logic).
- Keep live tests opt-in unless task explicitly requires real dependency runs.
Code Review Checklist
Use this checklist for backend PR review:
- Layering
- Router stays thin (
backend/src/v1/*/router.py) - Service owns transaction and business decisions
- Repository does not own commit/rollback
- Router stays thin (
- Error contract
- Responses align with RFC7807 + stable
code - New/changed codes reflected in
docs/protocols/common/http-error-codes.md
- Responses align with RFC7807 + stable
- Logging
- Uses
core.logging.get_logger - No raw sensitive values in logs
- Uses
- Config/secrets
- Reads configuration via
core.config.settings - No hardcoded secrets
- Reads configuration via
- Tests and checks
- Relevant pytest scope updated if behavior changed
ruff+basedpyrightremain passing for backend scope
Uncertainties (documented, not invented)
- Backend runtime still contains some legacy fallback exception patterns in specific infra/dependency paths (for example in
backend/src/v1/users/dependencies.pyfallback helper). New code should avoid expanding these patterns unless explicitly required. - No dedicated, repository-wide automated rule currently enforces every layering guideline; enforcement is primarily pre-commit + review discipline.