Loading...
Loading...
Generate or sync a "Living Specification" (docs/spec.md) from source code to eliminate doc-code drift. Use when creating, updating, or reviewing architecture documentation for a directory or module. Triggers on requests like "generate spec", "create spec.md", "sync documentation", "update architecture docs", "/spec.doc", or when asked to document a codebase directory.
npx skill4agent add ymd38/dev-skills spec-doc| Signal | Scope | Output file |
|---|---|---|
| "document this directory" / path given | Single module | |
| "document the whole project" / root path | Full codebase | |
| "update the spec" / existing spec found | Sync mode | overwrite existing file |
main.*index.*app.*server.*If the codebase exceeds ~50 files, scan by layer (not file-by-file). Read representative files in each layer rather than exhaustively.
docs/spec.mdtypescriptpythonsqlgraphqlgraph TDsequenceDiagramstateDiagram-v2## Future Considerations## MaintenanceLast Updated# Specification: [Module/Feature Name]
> Last Updated: YYYY-MM-DD | Scope: [module path] | Status: [current | stale | partial]
---
## Overview
[One paragraph: what problem this solves and why it exists. Include key business invariants.]
**Non-goals:** [What this module deliberately does NOT handle.]
---
## Architecture & Data Flow
[Describe the major components and their single responsibilities.]
```mermaid
graph TD
A[Client] -->|HTTP POST /orders| B[OrderController]
B -->|validate| C[OrderValidator]
B -->|persist| D[(orders DB)]
B -->|emit| E[OrderCreatedEvent]
E -->|subscribe| F[NotificationService]
```
**Component responsibilities:**
| Component | Responsibility | Dependencies |
|-----------|---------------|--------------|
| `OrderController` | Route handling, request parsing | `OrderValidator`, `OrderRepository` |
| `OrderValidator` | Business rule enforcement | none |
| `OrderRepository` | DB persistence | PostgreSQL |
---
## Interface & Data Models
### Public API
```typescript
// POST /orders
createOrder(payload: CreateOrderRequest): Promise<Order>
// throws: ValidationError (400), AuthError (401), ConflictError (409)
// GET /orders/:id
getOrder(id: string): Promise<Order | null>
// returns null if not found (never throws 404)
```
### Data Models
```typescript
interface Order {
id: string; // UUID v4
userId: string; // FK → users.id
status: OrderStatus; // see state diagram below
items: OrderItem[];
createdAt: Date;
updatedAt: Date;
}
type OrderStatus = 'pending' | 'confirmed' | 'shipped' | 'delivered' | 'cancelled';
```
---
## State & Side Effects
```mermaid
stateDiagram-v2
[*] --> pending: createOrder()
pending --> confirmed: confirmPayment()
confirmed --> shipped: shipOrder()
shipped --> delivered: markDelivered()
pending --> cancelled: cancelOrder()
confirmed --> cancelled: cancelOrder()
```
**Side effects by transition:**
| Transition | Side Effects |
|------------|-------------|
| `pending → confirmed` | Charge payment method, emit `order.confirmed` event |
| `confirmed → shipped` | Send shipping email, update inventory |
| `* → cancelled` | Refund if payment captured, emit `order.cancelled` event |
**Persistent state:** `orders` table in PostgreSQL. No in-memory state beyond request lifetime.
---
## Development Rules & Constraints
**Hard rules (violations = bugs):**
- Never transition an order to `cancelled` after `shipped`. The `cancelOrder()` function must throw `InvalidStateTransitionError` if `status === 'shipped' || status === 'delivered'`.
- All DB writes go through `OrderRepository`. No direct SQL outside of the repository layer.
- `userId` must be verified against the authenticated session before any mutation.
**Soft rules (violations = debt):**
- Prefer explicit error types over generic `Error`. Add to `src/errors/` if missing.
- Do not add new fields to `Order` without a corresponding DB migration.
**Known gotchas:**
- `updatedAt` is set by a DB trigger, not application code. Do not set it manually.
- `items` is fetched via a JOIN; avoid N+1 by always using `OrderRepository.findWithItems()`.
---
## Maintenance
This spec was last verified against commit `[hash]` on `YYYY-MM-DD`.
To update this spec: run `/spec.doc [module path]` after significant code changes.
Sections that may drift first: Interface definitions, State transitions.