Chapter 6: Spec-Driven Methodology
What is Spec-Driven Development?
Spec-Driven Development is a methodology where you write detailed specifications before implementation. When combined with AI assistants, specifications become executable blueprints that guide code generation.
Traditional vs. Spec-Driven
Traditional Development:
Idea → Code → Debug → Refactor → Document
Spec-Driven Development:
Idea → Specification → AI Generation → Review → Refinement
Benefits of Spec-Driven Development
1. Clarity Before Implementation
Writing specs forces you to:
- Define requirements precisely
- Identify edge cases early
- Make architectural decisions upfront
- Align stakeholders
2. Better AI Output
Detailed specs enable AI to generate:
- More accurate implementations
- Proper error handling
- Comprehensive test coverage
- Consistent code style
3. Living Documentation
Specs serve as:
- Single source of truth
- Onboarding material
- API documentation
- Validation criteria
4. Reduced Technical Debt
By planning ahead, you avoid:
- Rushed implementations
- Unclear requirements
- Misaligned components
- Costly refactoring
Writing Effective Specifications
The Spec-Kit Plus Approach
Spec-Kit Plus is a comprehensive framework for writing AI-friendly specifications. It includes:
- Project Overview: High-level goals and context
- Architecture: System design and component relationships
- Features: Detailed functionality descriptions
- Data Models: Schemas and relationships
- API Contracts: Endpoints, requests, and responses
- UI/UX Specifications: User interface designs
- Testing Requirements: Test scenarios and coverage
- Deployment: Infrastructure and deployment strategy
Specification Template
# Project Specification: [Project Name]
## Overview
### Purpose
[What problem does this solve?]
### Goals
- [Primary goal]
- [Secondary goal]
- [Additional goals]
### Success Criteria
- [Measurable criterion 1]
- [Measurable criterion 2]
## Architecture
### Technology Stack
- Frontend: [Technologies]
- Backend: [Technologies]
- Database: [Technologies]
- Infrastructure: [Technologies]
### System Components
[Component diagram or description]
## Features
### Feature 1: [Name]
**Description:** [Detailed description]
**User Story:** As a [role], I want [action] so that [benefit]
**Acceptance Criteria:**
- [ ] Criterion 1
- [ ] Criterion 2
- [ ] Criterion 3
**Technical Requirements:**
- Implementation details
- Performance requirements
- Security considerations
## Data Models
### [Model Name]
```typescript
interface ModelName {
id: string;
field1: Type;
field2: Type;
createdAt: Date;
updatedAt: Date;
}
Relationships:
- Relationship description
Validation Rules:
- Field validation requirements
API Contracts
Endpoint: [Method] /path
Description: [What this endpoint does]
Request:
{
"field": "value"
}
Response (200):
{
"data": "value"
}
Error Responses:
- 400: Bad Request
- 401: Unauthorized
- 404: Not Found
- 500: Server Error
Testing Requirements
Unit Tests
- Test scenario 1
- Test scenario 2
Integration Tests
- Test scenario 1
E2E Tests
- User flow 1
Deployment
Environment Variables
VAR_NAME: Description
Infrastructure
- Hosting platform
- Database setup
- CDN configuration
## Real-World Example: Task Management API
Let's write a complete specification:
```markdown
# Project Specification: Task Management API
## Overview
### Purpose
Build a RESTful API for managing tasks in team projects, enabling task
creation, assignment, tracking, and collaboration.
### Goals
- Enable teams to organize work efficiently
- Provide real-time task updates
- Support task dependencies and priorities
- Scale to 10,000+ concurrent users
### Success Criteria
- API response time < 200ms (p95)
- 99.9% uptime
- Complete test coverage (>90%)
- Comprehensive API documentation
## Architecture
### Technology Stack
- Runtime: Node.js v20
- Framework: Express.js with TypeScript
- Database: PostgreSQL 15
- Cache: Redis
- API Documentation: OpenAPI 3.0
- Testing: Jest + Supertest
- Deployment: Docker + Kubernetes
### System Components
1. **API Gateway**: Request routing and rate limiting
2. **Application Server**: Business logic and data processing
3. **Database Layer**: PostgreSQL with connection pooling
4. **Cache Layer**: Redis for frequently accessed data
5. **Background Jobs**: Task notifications and cleanup
## Features
### Feature 1: Task Management
**Description:** CRUD operations for tasks with rich metadata
**User Story:** As a team member, I want to create and manage tasks
so that I can organize my work effectively.
**Acceptance Criteria:**
- [ ] Create tasks with title, description, priority, and due date
- [ ] Update task status (todo, in-progress, done)
- [ ] Assign tasks to team members
- [ ] Add tags and categories
- [ ] Filter and search tasks
- [ ] Sort by various fields
**Technical Requirements:**
- Validate all inputs
- Prevent SQL injection
- Implement pagination (20 items per page)
- Support full-text search
- Cache frequently accessed tasks
- Log all mutations for audit trail
### Feature 2: Task Dependencies
**Description:** Define relationships between tasks
**User Story:** As a project manager, I want to set task dependencies
so that work happens in the correct order.
**Acceptance Criteria:**
- [ ] Define "blocks" and "blocked by" relationships
- [ ] Prevent circular dependencies
- [ ] Automatically update status when dependencies complete
- [ ] Visualize dependency graph
**Technical Requirements:**
- Use adjacency list for dependency graph
- Implement cycle detection algorithm
- Maintain referential integrity
- Update status via event system
## Data Models
### Task
```typescript
interface Task {
id: string; // UUID v4
title: string; // 1-200 characters
description: string | null; // Max 5000 characters
status: 'todo' | 'in_progress' | 'done' | 'archived';
priority: 'low' | 'medium' | 'high' | 'urgent';
assigneeId: string | null; // User ID
projectId: string; // Project ID
dueDate: Date | null;
tags: string[]; // Max 10 tags
createdAt: Date;
updatedAt: Date;
createdBy: string; // User ID
}
Relationships:
- Task belongs to Project (many-to-one)
- Task assigned to User (many-to-one)
- Task has many Dependencies (many-to-many)
- Task has many Comments (one-to-many)
Validation Rules:
- title: Required, 1-200 chars, no special characters
- description: Optional, max 5000 chars
- status: Must be valid enum value
- priority: Must be valid enum value
- assigneeId: Must reference existing user
- projectId: Required, must reference existing project
- dueDate: Must be future date
- tags: Array of 0-10 strings, each 1-50 chars
Database Schema:
CREATE TABLE tasks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title VARCHAR(200) NOT NULL,
description TEXT,
status VARCHAR(20) NOT NULL DEFAULT 'todo',
priority VARCHAR(20) NOT NULL DEFAULT 'medium',
assignee_id UUID REFERENCES users(id),
project_id UUID NOT NULL REFERENCES projects(id),
due_date TIMESTAMP,
tags TEXT[],
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
created_by UUID NOT NULL REFERENCES users(id)
);
CREATE INDEX idx_tasks_project ON tasks(project_id);
CREATE INDEX idx_tasks_assignee ON tasks(assignee_id);
CREATE INDEX idx_tasks_status ON tasks(status);
CREATE INDEX idx_tasks_due_date ON tasks(due_date);
API Contracts
Create Task
Endpoint: POST /api/v1/tasks
Description: Create a new task
Request:
{
"title": "Implement user authentication",
"description": "Add JWT-based authentication system",
"status": "todo",
"priority": "high",
"assigneeId": "550e8400-e29b-41d4-a716-446655440000",
"projectId": "660e8400-e29b-41d4-a716-446655440000",
"dueDate": "2024-12-31T23:59:59Z",
"tags": ["backend", "security"]
}
Response (201):
{
"success": true,
"data": {
"id": "770e8400-e29b-41d4-a716-446655440000",
"title": "Implement user authentication",
"description": "Add JWT-based authentication system",
"status": "todo",
"priority": "high",
"assigneeId": "550e8400-e29b-41d4-a716-446655440000",
"projectId": "660e8400-e29b-41d4-a716-446655440000",
"dueDate": "2024-12-31T23:59:59Z",
"tags": ["backend", "security"],
"createdAt": "2024-01-15T10:30:00Z",
"updatedAt": "2024-01-15T10:30:00Z",
"createdBy": "550e8400-e29b-41d4-a716-446655440000"
}
}
Error Responses:
- 400: Invalid input (missing required fields, invalid format)
- 401: Unauthorized (missing or invalid auth token)
- 403: Forbidden (no permission to create in this project)
- 404: Project or assignee not found
- 500: Internal server error
Validation:
- All fields must pass data model validation
- assigneeId must exist and be project member
- projectId must exist and user must have access
- Authentication required
List Tasks
Endpoint: GET /api/v1/tasks
Description: Retrieve paginated list of tasks with filtering
Query Parameters:
- projectId: string (required)
- status: string (optional)
- assigneeId: string (optional)
- priority: string (optional)
- search: string (optional)
- page: number (optional, default: 1)
- limit: number (optional, default: 20, max: 100)
- sortBy: string (optional, default: 'createdAt')
- sortOrder: 'asc' | 'desc' (optional, default: 'desc')
Response (200):
{
"success": true,
"data": {
"tasks": [...],
"pagination": {
"page": 1,
"limit": 20,
"total": 150,
"pages": 8
}
}
}
Testing Requirements
Unit Tests
- Task model validation
- Task creation logic
- Task update logic
- Dependency cycle detection
- Permission checking
- Input sanitization
Integration Tests
- Create task via API
- Update task via API
- Delete task via API
- List tasks with filters
- Dependency creation and validation
- Authentication and authorization
E2E Tests
- Complete task lifecycle (create → update → complete → archive)
- Task assignment workflow
- Dependency chain resolution
- Search and filter operations
Performance Tests
- 1000 concurrent task creations
- List endpoint with 10,000+ tasks
- Search performance with large datasets
Deployment
Environment Variables
DATABASE_URL: PostgreSQL connection stringREDIS_URL: Redis connection stringJWT_SECRET: Secret for token signingPORT: API server port (default: 3000)NODE_ENV: Environment (development/production)LOG_LEVEL: Logging verbosity
Infrastructure
- Hosting: AWS ECS (Elastic Container Service)
- Database: AWS RDS PostgreSQL with Multi-AZ
- Cache: AWS ElastiCache Redis
- Load Balancer: AWS ALB
- Monitoring: CloudWatch + DataDog
- Logging: CloudWatch Logs + Elasticsearch
Deployment Process
- Run tests in CI pipeline
- Build Docker image
- Push to ECR
- Deploy to staging environment
- Run smoke tests
- Deploy to production with blue-green deployment
- Monitor metrics and logs
## Using Specs with AI Assistants
### Prompt Pattern
I have the following specification for a Task Management API:
[Paste specification]
Please implement the following based on this specification:
- TypeScript type definitions for all data models
- Express.js route handlers for the Task endpoints
- Input validation middleware using Joi
- Unit tests for the task creation logic
Ensure the implementation follows the specification exactly, including all validation rules and error handling.
### Iterative Refinement
1. **Generate initial implementation** from spec
2. **Review generated code** for accuracy
3. **Update specification** based on learnings
4. **Regenerate affected components**
5. **Repeat** until complete
## Spec Maintenance
### Version Control
Track specifications in Git alongside code:
project/ ├── specs/ │ ├── architecture.md │ ├── api-contracts.md │ ├── data-models.md │ └── testing.md ├── src/ └── tests/
### Keep Specs Updated
- Update specs when requirements change
- Review specs during code reviews
- Use specs as PR templates
- Link commits to spec sections
## Summary
In this chapter, you learned:
- The Spec-Driven Development methodology
- How to write comprehensive specifications
- The Spec-Kit Plus framework
- Real-world specification examples
- How to use specs with AI assistants
**Next Chapter:** We'll explore Claude Code and advanced AI development workflows.
---
## 📋 Spec-Driven Development Process
<InteractiveDiagram
title="Spec-Driven Development Workflow"
diagram={Diagrams.specDriven}
caption="The complete workflow of Spec-Driven Development from requirements to validation."
/>
\n## 🎴 Test Your Knowledge
import Flashcards, { ChapterFlashcards } from '@site/src/components/Flashcards';
<Flashcards cards={ChapterFlashcards.ch2} title="Chapter Flashcards" />
---
## 📝 Chapter Quiz
import MCQ, { ChapterMCQ } from '@site/src/components/MCQ';
<MCQ questions={ChapterMCQ.ch6} title="Chapter 6 Quiz" />
---