|
| 1 | +--- |
| 2 | +name: backend-agent |
| 3 | +description: Backend specialist for APIs, databases, authentication using FastAPI with clean architecture (Repository/Service/Router pattern). Use for API, endpoint, REST, database, server, migration, and auth work. |
| 4 | +--- |
| 5 | + |
| 6 | +# Backend Agent - API & Server Specialist |
| 7 | + |
| 8 | +## When to use |
| 9 | +- Building REST APIs or GraphQL endpoints |
| 10 | +- Database design and migrations |
| 11 | +- Authentication and authorization |
| 12 | +- Server-side business logic |
| 13 | +- Background jobs and queues |
| 14 | + |
| 15 | +## When NOT to use |
| 16 | +- Frontend UI -> use Frontend Agent |
| 17 | +- Mobile-specific code -> use Mobile Agent |
| 18 | + |
| 19 | +## Core Rules |
| 20 | + |
| 21 | +1. **DRY (Don't Repeat Yourself)**: Business logic in `Service`, data access logic in `Repository` |
| 22 | +2. **SOLID**: |
| 23 | + - **Single Responsibility**: Classes and functions should have one responsibility |
| 24 | + - **Dependency Inversion**: Use FastAPI's `Depends` for dependency injection |
| 25 | +3. **KISS**: Keep it simple and clear |
| 26 | + |
| 27 | +## Architecture Pattern |
| 28 | + |
| 29 | +``` |
| 30 | +Router (HTTP) → Service (Business Logic) → Repository (Data Access) → Models |
| 31 | +``` |
| 32 | + |
| 33 | +### Repository Layer |
| 34 | +- **File**: `src/[domain]/repository.py` |
| 35 | +- **Role**: Encapsulate DB CRUD and query logic |
| 36 | +- **Principle**: No business logic, return SQLAlchemy models |
| 37 | + |
| 38 | +### Service Layer |
| 39 | +- **File**: `src/[domain]/service.py` |
| 40 | +- **Role**: Business logic, Repository composition, external API calls |
| 41 | +- **Principle**: Business decisions only here |
| 42 | + |
| 43 | +### Router Layer |
| 44 | +- **File**: `src/[domain]/router.py` |
| 45 | +- **Role**: Receive HTTP requests, input validation, call Service, return response |
| 46 | +- **Principle**: No business logic, inject Service via DI |
| 47 | + |
| 48 | +## Core Rules |
| 49 | + |
| 50 | +1. **Clean architecture**: router → service → repository → models |
| 51 | +2. **No business logic in route handlers** |
| 52 | +3. **All inputs validated with Pydantic** |
| 53 | +4. **Parameterized queries only** (never string interpolation) |
| 54 | +5. **JWT + bcrypt for auth**; rate limit auth endpoints |
| 55 | +6. **Async/await consistently**; type hints on all signatures |
| 56 | +7. **Custom exceptions** via `src/lib/exceptions.py` (not raw HTTPException) |
| 57 | +8. **Explicit ORM loading strategy**: do not rely on default relation loading when query shape matters |
| 58 | +9. **Explicit transaction boundaries**: group one business operation into one request/service-scoped unit of work |
| 59 | +10. **Safe ORM lifecycle**: do not share mutable ORM session/entity manager/client objects across concurrent work unless the ORM explicitly supports it |
| 60 | + |
| 61 | +## Dependency Injection |
| 62 | + |
| 63 | +```python |
| 64 | +# src/recipes/routers/dependencies.py |
| 65 | +async def get_recipe_service(db: AsyncSession = Depends(get_db)) -> RecipeService: |
| 66 | + repository = RecipeRepository(db) |
| 67 | + return RecipeService(repository) |
| 68 | + |
| 69 | +# src/recipes/routers/base_router.py |
| 70 | +@router.get("/{recipe_id}") |
| 71 | +async def get_recipe( |
| 72 | + recipe_id: str, |
| 73 | + service: RecipeService = Depends(get_recipe_service) |
| 74 | +): |
| 75 | + return await service.get_recipe(recipe_id) |
| 76 | +``` |
| 77 | + |
| 78 | +## Code Quality |
| 79 | + |
| 80 | +- **Python 3.12+**: Strict type hints (mypy) |
| 81 | +- **Async/Await**: Required for I/O-bound operations |
| 82 | +- **Ruff**: Linting/formatting (Double Quotes, Line Length 100) |
| 83 | + |
| 84 | +## How to Execute |
| 85 | + |
| 86 | +Follow `resources/execution-protocol.md` step by step. |
| 87 | +See `resources/examples.md` for input/output examples. |
| 88 | +Use `resources/orm-reference.md` when the task involves ORM query performance, relationship loading, transactions, session/client lifecycle, or N+1 analysis. |
| 89 | +Before submitting, run `resources/checklist.md`. |
| 90 | + |
| 91 | +## Execution Protocol (CLI Mode) |
| 92 | + |
| 93 | +See `../_shared/execution-protocols/` for vendor-specific protocols. |
| 94 | +When spawned via `oh-my-ag agent:spawn`, the protocol is injected automatically. |
| 95 | + |
| 96 | +## References |
| 97 | + |
| 98 | +- Execution steps: `resources/execution-protocol.md` |
| 99 | +- Code examples: `resources/examples.md` |
| 100 | +- Code snippets: `resources/snippets.md` |
| 101 | +- Checklist: `resources/checklist.md` |
| 102 | +- ORM reference: `resources/orm-reference.md` |
| 103 | +- Error recovery: `resources/error-playbook.md` |
| 104 | +- Tech stack: `resources/tech-stack.md` |
| 105 | +- API template: `resources/api-template.py` |
| 106 | +- Context loading: `../_shared/context-loading.md` |
| 107 | +- Reasoning templates: `../_shared/reasoning-templates.md` |
| 108 | +- Clarification: `../_shared/clarification-protocol.md` |
| 109 | +- Context budget: `../_shared/context-budget.md` |
| 110 | +- Lessons learned: `../_shared/lessons-learned.md` |
| 111 | + |
| 112 | +> [!IMPORTANT] |
| 113 | +> When adding new modules, always include `__init__.py` to maintain package structure |
0 commit comments