|
1 | | -# General Rules |
| 1 | +# AI Agent Code Guidelines |
| 2 | + |
| 3 | +This document provides coding guidelines for AI agents working on this Python project. |
2 | 4 |
|
3 | 5 | ## Environment and Dependencies |
4 | | -- **Tooling**: Use `uv` for all package management. |
| 6 | + |
| 7 | +- **Tooling**: Use `uv` for all package management |
| 8 | +- **Python Version**: 3.13+ |
| 9 | +- **Package Manager**: uv (fast, modern dependency management) |
5 | 10 |
|
6 | 11 | ## Code Style |
7 | 12 |
|
8 | | -**Format**: 2-space indent, single quotes, `snake_case` vars/functions, `PascalCase` classes, `is_/has_/should_` booleans |
| 13 | +**Format**: 2-space indentation, single quotes, 88-character lines, `snake_case` variables/functions, `PascalCase` classes, boolean flags prefixed with `is_/has_/should_`. Pre-commit hooks ensure code quality on commit. |
| 14 | + |
| 15 | +**Docstrings**: |
| 16 | +- Use simple one-line docstrings for most functions - be concise and descriptive |
| 17 | +- Type hints replace Args/Returns documentation - don't duplicate what's in the signature |
| 18 | +- No Examples section - type hints + function name should be self-explanatory |
| 19 | +- Exception: Top-level user-facing APIs may include brief usage notes if necessary |
| 20 | +- Rationale: Modern Python with PEP 484 type hints makes verbose docstrings redundant |
| 21 | + |
| 22 | +## Architecture Principles |
| 23 | + |
| 24 | +**Classes for state and lifecycle**, pure functions for transformations. Use classes when managing resources, coordinating operations, or defining interfaces. |
| 25 | + |
| 26 | +**Protocols over inheritance**: Define interfaces with `typing.Protocol` for flexible, duck-typed implementations. |
| 27 | + |
| 28 | +**Immutable configs**: Frozen dataclasses with `__post_init__` validation and factory classmethods (`from_uri()`, `from_dict()`). |
| 29 | + |
| 30 | +**Factory functions**: Standalone functions (`get_backend()`, `resolve_config()`) that map configurations to implementations. |
| 31 | + |
| 32 | +**Resource management**: |
| 33 | +- Reference counting for expensive resources (connections, clients) with acquire/release semantics |
| 34 | +- Context managers (`__enter__`/`__exit__`) for automatic cleanup |
| 35 | +- Track ownership with boolean flags to avoid closing borrowed resources |
| 36 | + |
| 37 | +**Streaming over bulk**: Use `Iterator[T]` to yield chunks for large datasets. Implement retry logic within generators for resilience. |
| 38 | + |
| 39 | +**Separation by lifecycle**: Split read and write operations into distinct classes, even when working with the same backend (e.g., `SessionStoreReader`, `SessionStoreWriter`). |
| 40 | + |
| 41 | +**Domain exceptions**: Create specific exception types rather than generic `RuntimeError`. |
| 42 | + |
| 43 | +**Performance**: Use `@dataclass(slots=True)` for frequently-instantiated objects and `TypeVar` for type-safe generic protocols. |
| 44 | + |
| 45 | +**Resilience**: Exponential backoff for network operations, graceful empty returns over exceptions, debug logging for key metrics. |
| 46 | + |
| 47 | +## Best Practices |
9 | 48 |
|
10 | | -**Architecture**: |
11 | | -- Pure functions preferred; classes for stateful operations (3+ shared params), data structures, framework requirements |
12 | 49 | - RORO pattern: service functions receive/return single Pydantic objects |
13 | | -- Single responsibility: break 150+ line functions into focused 15-25 line methods, early returns, no magic values, explicit dependencies |
14 | | -- DRY principle: extract utility functions to eliminate code duplication when applicable |
| 50 | +- Single responsibility: break 150+ line functions into focused 15-25 line methods |
| 51 | +- Early returns, no magic values, explicit dependencies |
| 52 | +- DRY principle: extract utility functions to eliminate duplication |
15 | 53 | - Pipeline pattern: chain transformations with clear inputs/outputs |
16 | 54 | - No excessive error handling or low-value comments |
17 | 55 |
|
| 56 | +## Pydantic Commitment |
| 57 | + |
| 58 | +- All interface models use Pydantic `BaseModel` (not dataclasses) |
| 59 | +- Validation, `.model_dump()`, JSON serialization built-in |
| 60 | +- Reserve `@dataclass(slots=True)` only for inner performance-critical helpers (graph traversal caches, etc.) |
| 61 | + |
18 | 62 | ## Codex Commands |
19 | 63 |
|
20 | 64 | Conventions: |
|
0 commit comments