Teaching AI agents to generate robust, maintainable code through aggressive proximity patterns and decision archaeology.
This repository provides patterns and templates that help AI agents (Claude, GPT-4, Copilot, etc.) generate code with:
- Decision archaeology - Every choice documented at its point of implementation
- Test colocation - Tests next to code they test
- Rich error context - Debugging information where errors occur
- Security markers - Threat models at enforcement points
- Performance rationale - Benchmarks with optimization decisions
Result: AI-generated code that's production-ready, not just functionally correct.
When generating code, follow aggressive proximity patterns:
1. Document decisions WHERE they happen with π§ DECISION comments
2. Place tests next to implementation files
3. Include error context at error sites with π¨ ERROR comments
4. Mark security decisions with π‘οΈ SECURITY comments
5. Document performance choices with β‘ PERFORMANCE comments
See examples: https://github.com/aggressive-proximity-patterns
Generate code following aggressive proximity patterns:
- Add π§ DECISION comments explaining non-obvious choices
- Include WHY in comments, not WHAT
- Colocate tests with implementation
- Provide rich error context
- Document security and performance decisions at enforcement points
π― Real Example from Production (spiral-core)
This pattern from a production AI orchestration system shows perfect proximity:
// From https://github.com/SaintPepsi/spiral-core/blob/main/src/constants.rs
/// β±οΈ TASK POLLING INTERVAL: Balance between responsiveness and CPU usage
/// Why: 100ms provides near-real-time feel without excessive CPU overhead
/// Alternative: 50ms (rejected: 2x CPU usage), 500ms (rejected: sluggish UX)
/// Calculation: ~10 polls/second = reasonable for human-perceived responsiveness
pub const TASK_POLL_INTERVAL_MS: u64 = 100;
/// π¦ MAX QUEUE SIZE: Memory protection for 8GB VPS deployment
/// Why: 1000 tasks β 1MB RAM (1KB avg task) provides safety margin
/// Calculation: 8GB total - 2GB OS - 4GB app = 2GB buffer Γ· 1KB = 2M tasks theoretical
/// Conservative: 1K tasks allows for larger tasks and system overhead
/// Alternative: 10K (rejected: potential OOM), 100 (rejected: too restrictive)
pub const MAX_QUEUE_SIZE: usize = 1000;
What makes this excellent:
- Specific reasoning with measurements
- Alternatives considered and rejected with reasons
- Deployment context included (8GB VPS)
- Calculations shown for verification
Every significant decision should be documented WHERE it happens, not in separate documentation.
# π§ DECISION: Using Redis for session storage
# Why: Need atomic operations for concurrent session updates
# Alternative: In-memory store - rejected due to multi-server deployment
# Impact: Adds Redis dependency but ensures session consistency
session_store = RedisSessionStore(connection_pool)
Don't abstract on first or second occurrence. Wait for the third strike.
// Strike 1: UserController validation
// Strike 2: AdminController validation
// Strike 3: ApiController validation β EXTRACTED to ValidationUtils
const ValidationUtils = require('./utils/validation');
Tests live next to the code they test, not in separate test directories.
src/
βββ user_service.py
βββ user_service_test.py # Colocated test
βββ auth/
β βββ authenticator.rs
β βββ authenticator_test.rs # Colocated test
Related code should be physically close, even if it breaks traditional hierarchies.
Comments explain WHY, not WHAT. They capture context that code cannot express.
- Rust - Module system, lifetime annotations, trait proximity
- Python - Package structure, type hints, docstring patterns
- JavaScript/TypeScript - ES modules, JSDoc, component colocation
- Go - Package organization, interface proximity, test files
- Java - Package-private visibility, inner classes, annotation proximity
- C# - Partial classes, regions, namespace organization
- Ruby - Module inclusion, method visibility, spec colocation
- Swift - Extensions, protocol conformance, access control
- Audit Checkpoints - Security and compliance verification points
- Decision Templates - Standardized decision documentation
- Error Proximity - Error handling near error sources
- Configuration Colocation - Config next to usage
- Validation Proximity - Validation logic near data structures
Your code's proximity score is calculated based on:
- Decision Coverage (25%) - Percentage of significant decisions documented
- Test Colocation (25%) - Tests living next to implementation
- Abstraction Timing (20%) - Following 3-strikes rule
- Comment Quality (15%) - Context-rich over redundant
- File Organization (15%) - Related code proximity
Task queue and authentication system with complete proximity patterns.
Rate limiter and auth module demonstrating decision archaeology.
API gateway with circuit breaker and rate limiting patterns.
Worker pool with concurrent processing and metrics.
- Read Core Principles
- Review language-specific guide for your stack
- Study spiral-core examples
- Pick one pattern to implement
- Apply to your codebase
- Gradually adopt more patterns
- Share your results!
"Reduced debugging time by 60% after implementing decision archaeology" - TechCorp
"New developers onboard 3x faster with proximity patterns" - StartupXYZ
"Eliminated 'why was this done?' questions in code reviews" - EnterpriseCo
We welcome contributions! See CONTRIBUTING.md for guidelines.
- Add Language Support - Template for new language patterns
- Share Patterns - Document your proximity discoveries
- Improve Tools - Enhance linter or audit generator
- Case Studies - Share your implementation story
- Spiral-Core Repository - Production example with 92% proximity score
- Original Kent C. Dodds Article
- Spiral-Core constants.rs - Gold standard for decision documentation
- Start: Basic proximity principles
- Practice: Implement in small project
- Advance: Apply to production code
- Master: Create custom patterns
- Teach: Share with community
MIT - See LICENSE file
- Kent C. Dodds for the original colocation principles
- The Rust community for compile-time safety patterns
- Contributors who've added language-specific implementations
Remember: The best documentation is the code that doesn't need documentation because its context is self-evident through proximity.