This file contains critical rules for AI assistants working on the HedgeLock codebase.
- Project: HedgeLock - Event-driven trading risk management system
- Stack: Python FastAPI microservices with Apache Kafka
- Version: 2.0.0-dev
- Current Phase: System integration and production preparation
- File Size Limits: Soft limit 300 lines, hard limit 500 lines
- Refactoring Threshold: If file exceeds 600 lines (20% over hard limit), STOP and refactor immediately
- Single Responsibility: Each file manages ONE distinct aspect (API endpoints, Kafka handlers, business logic, models)
- Use type hints for ALL function parameters and returns
- Follow PEP 8 (enforced by Black formatter)
- Use async/await for ALL I/O operations
- Pydantic models for ALL data validation
- Include trace_id in ALL log messages for distributed tracing
- Branch naming:
[type]/[sprint-id]-[short-summary](e.g.,feat/HL-07-1-trade-executor) - Commit format:
[type]([sprint-id]): [description](e.g.,feat(HL-07-1): implement trade executor) - Commit types: feat, fix, docs, test, refactor, perf, chore
- Always test before commit: Run
make testandmake lint
- Create branch for task
- Update todo list with task breakdown
- For Kafka tasks: Test connectivity FIRST (
make kafka-topics) - Write integration tests BEFORE unit tests
- Include health checks and monitoring
- Add Prometheus metrics for new features
- Commit frequently with clear messages
- Push to remote after each commit
- Integration tests for ALL Kafka data flows
- Soak tests for 5+ minute continuous operation
- Performance validation: <150ms latency
- 60% minimum code coverage
- Commands:
make test,make test-integration,make test-cov
- File size exceeded: Stop, create refactoring task, split file, test, then resume
- Test failures: Fix before proceeding, never commit broken code
- Kafka errors: Check Docker (
make compose-ps), verify topics (make kafka-topics)
make compose-ps # Check Docker services
make test # Run unit tests
make lint # Check code style
make test-integration # Run integration tests
make kafka-topics # List Kafka topics
make logs-{service} # View service logs- All tests pass:
make test && make lint - Core services healthy:
curl http://localhost:800{1-6}/health - New services healthy:
curl http://localhost:80{09-12}/health - No errors in logs:
make compose-logs | grep ERROR - Kafka topics active:
make kafka-topics
src/hedgelock/
├── collector/ # Market data collection (8001)
├── funding_engine/ # Funding rate analysis (8002)
├── risk_engine/ # Risk calculations (8003)
├── hedger/ # Hedge decisions (8004)
├── treasury/ # Fund management (8005)
├── trade_executor/ # Trade execution (8006)
├── alert/ # Notifications (8007)
├── position_manager/ # Delta-neutral trading (8009) [NEW v2.0]
├── loan_manager/ # Loan tracking & repayment (8010) [NEW v2.0]
├── reserve_manager/ # Reserve deployment (8011) [NEW v2.0]
├── safety_manager/ # System safety & monitoring (8012) [NEW v2.0]
└── shared/ # Shared utilities
- Position Manager: Maintains delta-neutral positions with profit targets
- Loan Manager: Auto-repays loan from profits, monitors LTV
- Reserve Manager: Deploys USDC reserves based on risk levels
- Safety Manager: Liquidation protection, circuit breakers, dead man's switch
- Emergency docs:
docs/EMERGENCY_PROCEDURES.md - Halt trading:
curl -X POST http://localhost:8012/emergency-action/manual -d '{"action": "halt_trading"}' - Check safety:
curl http://localhost:8012/safety/state
- Always use structured logging with trace_id
- Test Kafka integration BEFORE business logic
- Update PROJECT_MEMORY.yaml after significant changes
- Follow existing patterns in src/hedgelock/
- Never commit without running tests
- Monitor Safety Manager alerts continuously in production