contextos-agents 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/.agents/adapters/aider/export.js +2 -2
  2. package/.agents/adapters/claude/export.js +53 -2
  3. package/.agents/adapters/drift-detector.js +6 -3
  4. package/.agents/adapters/pure-compiler.js +18 -6
  5. package/.agents/ctx.js +4 -4
  6. package/.agents/plugins.js +100 -4
  7. package/.agents/profiles.js +32 -11
  8. package/README.md +2 -2
  9. package/bin/commands/hook.js +50 -12
  10. package/bin/commands/scan.js +10 -3
  11. package/bin/index.js +8 -2
  12. package/bin/lib/git-snapshot.js +70 -43
  13. package/bin/lib/scan.js +108 -27
  14. package/catalog/skills/adapters/EXAMPLES.md +19 -0
  15. package/catalog/skills/adapters/SKILL.md +101 -0
  16. package/catalog/skills/adapters/TROUBLESHOOTING.md +7 -0
  17. package/catalog/skills/adapters/VALIDATION.json +12 -0
  18. package/catalog/skills/adapters/skill.yaml +13 -0
  19. package/catalog/skills/api-design/EXAMPLES.md +91 -0
  20. package/catalog/skills/api-design/SKILL.md +63 -0
  21. package/catalog/skills/api-design/TROUBLESHOOTING.md +54 -0
  22. package/catalog/skills/api-design/VALIDATION.json +11 -0
  23. package/catalog/skills/api-design/skill.yaml +14 -0
  24. package/catalog/skills/architecture-diagrams/SKILL.md +108 -0
  25. package/catalog/skills/architecture-diagrams/VALIDATION.json +12 -0
  26. package/catalog/skills/architecture-diagrams/skill.yaml +9 -0
  27. package/catalog/skills/brutalist-design/EXAMPLES.md +59 -0
  28. package/catalog/skills/brutalist-design/SKILL.md +150 -0
  29. package/catalog/skills/brutalist-design/VALIDATION.json +12 -0
  30. package/catalog/skills/brutalist-design/skill.yaml +10 -0
  31. package/catalog/skills/ci-cd/EXAMPLES.md +79 -0
  32. package/catalog/skills/ci-cd/SKILL.md +69 -0
  33. package/catalog/skills/ci-cd/TROUBLESHOOTING.md +52 -0
  34. package/catalog/skills/ci-cd/VALIDATION.json +11 -0
  35. package/catalog/skills/ci-cd/skill.yaml +13 -0
  36. package/catalog/skills/database/EXAMPLES.md +74 -0
  37. package/catalog/skills/database/SKILL.md +101 -0
  38. package/catalog/skills/database/TROUBLESHOOTING.md +18 -0
  39. package/catalog/skills/database/VALIDATION.json +11 -0
  40. package/catalog/skills/database/skill.yaml +14 -0
  41. package/catalog/skills/ddd/EXAMPLES.md +42 -0
  42. package/catalog/skills/ddd/SKILL.md +247 -0
  43. package/catalog/skills/ddd/TROUBLESHOOTING.md +19 -0
  44. package/catalog/skills/ddd/VALIDATION.json +12 -0
  45. package/catalog/skills/ddd/skill.yaml +14 -0
  46. package/catalog/skills/decisions/EXAMPLES.md +35 -0
  47. package/catalog/skills/decisions/SKILL.md +90 -0
  48. package/catalog/skills/decisions/TROUBLESHOOTING.md +13 -0
  49. package/catalog/skills/decisions/VALIDATION.json +12 -0
  50. package/catalog/skills/decisions/skill.yaml +13 -0
  51. package/catalog/skills/docker/EXAMPLES.md +56 -0
  52. package/catalog/skills/docker/SKILL.md +169 -0
  53. package/catalog/skills/docker/TROUBLESHOOTING.md +18 -0
  54. package/catalog/skills/docker/VALIDATION.json +11 -0
  55. package/catalog/skills/docker/skill.yaml +13 -0
  56. package/catalog/skills/fastapi/EXAMPLES.md +36 -0
  57. package/catalog/skills/fastapi/SKILL.md +171 -0
  58. package/catalog/skills/fastapi/TROUBLESHOOTING.md +19 -0
  59. package/catalog/skills/fastapi/VALIDATION.json +12 -0
  60. package/catalog/skills/fastapi/skill.yaml +14 -0
  61. package/catalog/skills/generators/EXAMPLES.md +19 -0
  62. package/catalog/skills/generators/SKILL.md +110 -0
  63. package/catalog/skills/generators/TROUBLESHOOTING.md +7 -0
  64. package/catalog/skills/generators/VALIDATION.json +12 -0
  65. package/catalog/skills/generators/skill.yaml +22 -0
  66. package/catalog/skills/generators/templates/API.md +77 -0
  67. package/catalog/skills/generators/templates/ARCHITECTURE.md +70 -0
  68. package/catalog/skills/generators/templates/DATABASE.md +42 -0
  69. package/catalog/skills/generators/templates/DECISION.md +46 -0
  70. package/catalog/skills/generators/templates/PRD.md +67 -0
  71. package/catalog/skills/generators/templates/PROJECT_GRAPH.md +56 -0
  72. package/catalog/skills/generators/templates/ROADMAP.md +51 -0
  73. package/catalog/skills/generators/templates/TASKS.md +43 -0
  74. package/catalog/skills/generators/templates/UI.md +73 -0
  75. package/catalog/skills/graphify/EXAMPLES.md +73 -0
  76. package/catalog/skills/graphify/SKILL.md +130 -0
  77. package/catalog/skills/graphify/VALIDATION.json +12 -0
  78. package/catalog/skills/graphify/skill.yaml +13 -0
  79. package/catalog/skills/impeccable-design/EXAMPLES.md +26 -0
  80. package/catalog/skills/impeccable-design/SKILL.md +201 -0
  81. package/catalog/skills/impeccable-design/TROUBLESHOOTING.md +19 -0
  82. package/catalog/skills/impeccable-design/VALIDATION.json +12 -0
  83. package/catalog/skills/impeccable-design/skill.yaml +15 -0
  84. package/catalog/skills/interview-me/SKILL.md +97 -0
  85. package/catalog/skills/interview-me/VALIDATION.json +12 -0
  86. package/catalog/skills/interview-me/skill.yaml +9 -0
  87. package/catalog/skills/microservices/EXAMPLES.md +38 -0
  88. package/catalog/skills/microservices/SKILL.md +164 -0
  89. package/catalog/skills/microservices/TROUBLESHOOTING.md +19 -0
  90. package/catalog/skills/microservices/VALIDATION.json +12 -0
  91. package/catalog/skills/microservices/skill.yaml +14 -0
  92. package/catalog/skills/minimalist-design/EXAMPLES.md +58 -0
  93. package/catalog/skills/minimalist-design/SKILL.md +113 -0
  94. package/catalog/skills/minimalist-design/VALIDATION.json +12 -0
  95. package/catalog/skills/minimalist-design/skill.yaml +10 -0
  96. package/catalog/skills/nestjs/EXAMPLES.md +40 -0
  97. package/catalog/skills/nestjs/SKILL.md +139 -0
  98. package/catalog/skills/nestjs/TROUBLESHOOTING.md +19 -0
  99. package/catalog/skills/nestjs/VALIDATION.json +12 -0
  100. package/catalog/skills/nestjs/skill.yaml +14 -0
  101. package/catalog/skills/nextjs/EXAMPLES.md +40 -0
  102. package/catalog/skills/nextjs/SKILL.md +163 -0
  103. package/catalog/skills/nextjs/TROUBLESHOOTING.md +19 -0
  104. package/catalog/skills/nextjs/VALIDATION.json +12 -0
  105. package/catalog/skills/nextjs/skill.yaml +14 -0
  106. package/catalog/skills/node/EXAMPLES.md +80 -0
  107. package/catalog/skills/node/SKILL.md +128 -0
  108. package/catalog/skills/node/TROUBLESHOOTING.md +19 -0
  109. package/catalog/skills/node/VALIDATION.json +12 -0
  110. package/catalog/skills/node/skill.yaml +14 -0
  111. package/catalog/skills/performance/EXAMPLES.md +30 -0
  112. package/catalog/skills/performance/SKILL.md +75 -0
  113. package/catalog/skills/performance/TROUBLESHOOTING.md +19 -0
  114. package/catalog/skills/performance/VALIDATION.json +12 -0
  115. package/catalog/skills/performance/skill.yaml +14 -0
  116. package/catalog/skills/react/EXAMPLES.md +79 -0
  117. package/catalog/skills/react/SKILL.md +132 -0
  118. package/catalog/skills/react/TROUBLESHOOTING.md +19 -0
  119. package/catalog/skills/react/VALIDATION.json +12 -0
  120. package/catalog/skills/react/skill.yaml +14 -0
  121. package/catalog/skills/react-best-practices/SKILL.md +158 -0
  122. package/catalog/skills/react-best-practices/VALIDATION.json +12 -0
  123. package/catalog/skills/react-best-practices/skill.yaml +13 -0
  124. package/catalog/skills/redesign-audit/SKILL.md +117 -0
  125. package/catalog/skills/redesign-audit/VALIDATION.json +12 -0
  126. package/catalog/skills/redesign-audit/skill.yaml +9 -0
  127. package/catalog/skills/security-audit/EXAMPLES.md +79 -0
  128. package/catalog/skills/security-audit/SKILL.md +91 -0
  129. package/catalog/skills/security-audit/TROUBLESHOOTING.md +46 -0
  130. package/catalog/skills/security-audit/VALIDATION.json +11 -0
  131. package/catalog/skills/security-audit/skill.yaml +14 -0
  132. package/catalog/skills/soft-design/EXAMPLES.md +51 -0
  133. package/catalog/skills/soft-design/SKILL.md +108 -0
  134. package/catalog/skills/soft-design/VALIDATION.json +12 -0
  135. package/catalog/skills/soft-design/skill.yaml +10 -0
  136. package/catalog/skills/state-management/EXAMPLES.md +56 -0
  137. package/catalog/skills/state-management/SKILL.md +168 -0
  138. package/catalog/skills/state-management/TROUBLESHOOTING.md +18 -0
  139. package/catalog/skills/state-management/VALIDATION.json +11 -0
  140. package/catalog/skills/state-management/skill.yaml +14 -0
  141. package/catalog/skills/subagent-orchestrator/SKILL.md +117 -0
  142. package/catalog/skills/subagent-orchestrator/VALIDATION.json +12 -0
  143. package/catalog/skills/subagent-orchestrator/skill.yaml +9 -0
  144. package/catalog/skills/system-design/EXAMPLES.md +75 -0
  145. package/catalog/skills/system-design/SKILL.md +419 -0
  146. package/catalog/skills/system-design/TROUBLESHOOTING.md +19 -0
  147. package/catalog/skills/system-design/VALIDATION.json +12 -0
  148. package/catalog/skills/system-design/skill.yaml +14 -0
  149. package/catalog/skills/terraform/EXAMPLES.md +74 -0
  150. package/catalog/skills/terraform/SKILL.md +55 -0
  151. package/catalog/skills/terraform/TROUBLESHOOTING.md +53 -0
  152. package/catalog/skills/terraform/VALIDATION.json +11 -0
  153. package/catalog/skills/terraform/skill.yaml +14 -0
  154. package/catalog/skills/testing/EXAMPLES.md +122 -0
  155. package/catalog/skills/testing/SKILL.md +70 -0
  156. package/catalog/skills/testing/TROUBLESHOOTING.md +18 -0
  157. package/catalog/skills/testing/VALIDATION.json +11 -0
  158. package/catalog/skills/testing/skill.yaml +14 -0
  159. package/catalog/skills/typescript/EXAMPLES.md +64 -0
  160. package/catalog/skills/typescript/SKILL.md +112 -0
  161. package/catalog/skills/typescript/TROUBLESHOOTING.md +19 -0
  162. package/catalog/skills/typescript/VALIDATION.json +12 -0
  163. package/catalog/skills/typescript/skill.yaml +14 -0
  164. package/catalog/skills/ui-design/EXAMPLES.md +21 -0
  165. package/catalog/skills/ui-design/SKILL.md +124 -0
  166. package/catalog/skills/ui-design/TROUBLESHOOTING.md +19 -0
  167. package/catalog/skills/ui-design/VALIDATION.json +12 -0
  168. package/catalog/skills/ui-design/skill.yaml +16 -0
  169. package/catalog/skills/ui-ux-pro/EXAMPLES.md +62 -0
  170. package/catalog/skills/ui-ux-pro/SKILL.md +418 -0
  171. package/catalog/skills/ui-ux-pro/TROUBLESHOOTING.md +19 -0
  172. package/catalog/skills/ui-ux-pro/VALIDATION.json +12 -0
  173. package/catalog/skills/ui-ux-pro/skill.yaml +14 -0
  174. package/catalog/skills/ux-design/EXAMPLES.md +36 -0
  175. package/catalog/skills/ux-design/SKILL.md +116 -0
  176. package/catalog/skills/ux-design/TROUBLESHOOTING.md +19 -0
  177. package/catalog/skills/ux-design/VALIDATION.json +12 -0
  178. package/catalog/skills/ux-design/skill.yaml +16 -0
  179. package/catalog/skills/vercel-optimize/SKILL.md +83 -0
  180. package/catalog/skills/vercel-optimize/VALIDATION.json +12 -0
  181. package/catalog/skills/vercel-optimize/scripts/collect-signals.mjs +131 -0
  182. package/catalog/skills/vercel-optimize/scripts/gate-investigations.mjs +142 -0
  183. package/catalog/skills/vercel-optimize/scripts/merge-signals.mjs +143 -0
  184. package/catalog/skills/vercel-optimize/scripts/scan-codebase.mjs +174 -0
  185. package/catalog/skills/vercel-optimize/skill.yaml +15 -0
  186. package/catalog/skills/web-accessibility/EXAMPLES.md +39 -0
  187. package/catalog/skills/web-accessibility/SKILL.md +151 -0
  188. package/catalog/skills/web-accessibility/TROUBLESHOOTING.md +19 -0
  189. package/catalog/skills/web-accessibility/VALIDATION.json +12 -0
  190. package/catalog/skills/web-accessibility/skill.yaml +14 -0
  191. package/package.json +3 -2
@@ -0,0 +1,117 @@
1
+ ---
2
+ name: subagent-orchestrator
3
+ description: >
4
+ Subagent delegation and parallel coordination skill. Implements task decomposition,
5
+ context hand-offs, blast-radius boundary isolation, and conflict-free merge synthesis.
6
+ ---
7
+
8
+ # subagent-orchestrator
9
+
10
+ ## Overview
11
+
12
+ Multi-agent coordination protocol inspired by [obra/superpowers](https://github.com/obra/superpowers). Enables a primary orchestrating agent to decompose complex workflows into isolated, parallel sub-tasks, delegate them with precise context boundaries, monitor execution, and synthesize outputs with zero merge conflicts.
13
+
14
+ ## When to Use
15
+
16
+ Activate whenever:
17
+
18
+ - A task can be parallelized across distinct modules, services, or test suites.
19
+ - Long-running exploratory research or multi-file refactoring exceeds single-context budget.
20
+ - Running autonomous subagent workers for specialized roles (e.g. specialized QA tester, Security auditor, Docs generator).
21
+
22
+ ## Rules & Patterns
23
+
24
+ ### 1. The Blast Radius Boundary Rule
25
+
26
+ Before delegating any subagent task:
27
+
28
+ - **Zero File Overlap**: Each subagent MUST have a mutually exclusive list of target files. Two subagents must never be instructed to edit the same file concurrently.
29
+ - **Explicit Inputs & Outputs**: Provide only the minimal schema, contract, or mock that the subagent needs. Do not dump the entire workspace into subagent prompts.
30
+
31
+ ### 2. The 4-Step Delegation Lifecycle
32
+
33
+ ```
34
+ [ Orchestrator ]
35
+ │
36
+ ├─▶ 1. DECOMPOSE: Break into orthogonal tasks with non-overlapping file sets
37
+ │
38
+ ├─▶ 2. DISPATCH: Launch subagent with precise goal, constraints, and finish criteria
39
+ │
40
+ ├─▶ 3. AWAIT & VERIFY: Validate subagent output against its individual quality gate
41
+ │
42
+ └─▶ 4. SYNTHESIZE: Merge subagent results into the main branch and run global regression suite
43
+ ```
44
+
45
+ ### 3. Context Hand-off Specification
46
+
47
+ Every subagent dispatch must be backed by an immutable `TaskBrief`:
48
+
49
+ ```ts
50
+ type TaskBrief = Readonly<{
51
+ taskId: string;
52
+ baseSha: string;
53
+ objective: string;
54
+ writeScope: readonly string[];
55
+ testCommand: string;
56
+ expectedResult: string;
57
+ maxAttempts: number;
58
+ }>;
59
+ ```
60
+
61
+ Create the brief once before dispatch. Agents and retry loops must reuse it without
62
+ changing the base commit, scope, verification command, or acceptance result.
63
+ Every subagent dispatch prompt must contain:
64
+
65
+ 1. **Target Objective**: Single, verifiable deliverable.
66
+ 2. **Read-Only Context**: Files to consult as reference without modifying.
67
+ 3. **Write Scope**: Exact file paths the subagent is permitted to create or modify.
68
+ 4. **Completion Signal**: Explicit instruction to report `DONE` with test evidence or `BLOCKED` with reason.
69
+
70
+ ---
71
+
72
+ ## Code Examples
73
+
74
+ ### Orchestrator Task Dispatch Template
75
+
76
+ ```markdown
77
+ **Subagent Task: Order Validation Service**
78
+
79
+ - **Role**: `[ROLE: Senior Developer]`
80
+ - **Goal**: Implement Zod validation schema and unit tests for order payloads.
81
+ - **Write Scope**:
82
+ - `src/services/order/validation.ts`
83
+ - `tests/services/order/validation.test.ts`
84
+ - **Read-Only Reference**:
85
+ - `src/types/order.ts`
86
+ - **Quality Gate**:
87
+ - Run `npx vitest run tests/services/order/validation.test.ts`
88
+ - All tests must pass with 100% coverage of validation rules.
89
+ - **Finish Criteria**:
90
+ - Report exact test output and finish with `DONE`.
91
+ ```
92
+
93
+ ---
94
+
95
+ ## Validation Checklist
96
+
97
+ - [ ] All delegated tasks have disjoint, non-overlapping file sets.
98
+ - [ ] Every worker received an immutable `TaskBrief` tied to the original `baseSha`.
99
+ - [ ] Every subagent prompt has explicit read vs write boundaries.
100
+ - [ ] Subagent results verified individually before merging.
101
+ - [ ] Global regression suite executed across the entire repository after all subagents finish.
102
+
103
+ ---
104
+
105
+ ## Common Mistakes
106
+
107
+ - **Concurrent file collisions**: Assigning two subagents to modify the same route handler or lockfile.
108
+ - **Unbounded delegation**: Asking a subagent to "improve the codebase" without specific file limits.
109
+ - **Trusting without verification**: Assuming subagent code works without executing the test gate in the parent context.
110
+
111
+ ---
112
+
113
+ ## Integration Notes
114
+
115
+ - Integrates with `engineering-workflow` during the PLAN and BUILD phases.
116
+ - Works directly with `gstack-roles` to assign specific specialist personas to each subagent.
117
+ - Employs `ponytail-mindset` to keep subagent implementations minimal.
@@ -0,0 +1,12 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "rules_followed": {
6
+ "type": "boolean"
7
+ }
8
+ },
9
+ "required": [
10
+ "rules_followed"
11
+ ]
12
+ }
@@ -0,0 +1,9 @@
1
+ schemaVersion: 2
2
+ name: subagent-orchestrator
3
+ category: engineering
4
+ type: runtime
5
+ description: Subagent delegation and parallel coordination skill. Implements task decomposition, context hand-offs, blast-radius boundary isolation, and conflict-free merge synthesis.
6
+ version: 1.0.0
7
+ resources:
8
+ - SKILL.md
9
+ - VALIDATION.json
@@ -0,0 +1,75 @@
1
+ # system-design Examples - Anti-patterns vs ContextOS Standard
2
+
3
+ ## Example 1: Database Caching Strategy
4
+
5
+ ### Anti-pattern: Cache-Aside with Unbounded Thundering Herd
6
+
7
+ ```typescript
8
+ // BAD: When cache expires, 10,000 concurrent requests hit PostgreSQL simultaneously
9
+ async function getUserProfile(id: string) {
10
+ const cached = await redis.get(`user:${id}`);
11
+ if (cached) return JSON.parse(cached);
12
+ const user = await db.user.findUnique({ where: { id } });
13
+ await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 300);
14
+ return user;
15
+ }
16
+ ```
17
+
18
+ ### Best practice: ContextOS Standard (Mutex Lock / Single-Flight Pattern)
19
+
20
+ ```typescript
21
+ // GOOD: Only one worker fetches from DB on cache miss; others wait
22
+ import { singleflight } from './singleflight';
23
+
24
+ async function getUserProfile(id: string) {
25
+ const cached = await redis.get(`user:${id}`);
26
+ if (cached) return JSON.parse(cached);
27
+
28
+ return singleflight.do(`user:${id}`, async () => {
29
+ const fresh = await redis.get(`user:${id}`);
30
+ if (fresh) return JSON.parse(fresh);
31
+
32
+ const user = await db.user.findUnique({ where: { id } });
33
+ if (user) {
34
+ await redis.set(`user:${id}`, JSON.stringify(user), 'EX', 300);
35
+ }
36
+ return user;
37
+ });
38
+ }
39
+ ```
40
+
41
+ ---
42
+
43
+ ## Example 2: Outbox Pattern for Distributed Consistency
44
+
45
+ ### Anti-pattern: Dual-Write Anti-pattern (Direct DB write + Kafka publish)
46
+
47
+ ```typescript
48
+ // BAD: If Kafka publish fails, DB change is committed but event is lost forever
49
+ async function createOrder(data: OrderInput) {
50
+ const order = await db.order.create({ data });
51
+ await kafkaProducer.send({ topic: 'orders', messages: [{ value: JSON.stringify(order) }] });
52
+ return order;
53
+ }
54
+ ```
55
+
56
+ ### Best practice: ContextOS Standard (Transactional Outbox)
57
+
58
+ ```typescript
59
+ // GOOD: Order and Outbox record committed in a single atomic DB transaction
60
+ async function createOrder(data: OrderInput) {
61
+ return await db.$transaction(async (tx) => {
62
+ const order = await tx.order.create({ data });
63
+ await tx.outbox.create({
64
+ data: {
65
+ aggregateType: 'Order',
66
+ aggregateId: order.id,
67
+ eventType: 'OrderCreated',
68
+ payload: JSON.stringify(order),
69
+ status: 'PENDING',
70
+ },
71
+ });
72
+ return order;
73
+ });
74
+ }
75
+ ```
@@ -0,0 +1,419 @@
1
+ ---
2
+ name: system-design
3
+ description: >
4
+ Scalable architecture skill based on the System Design Primer.
5
+ Before designing any backend, reason about load balancers, caching,
6
+ DB partitioning, CAP theorem, and microservices trade-offs.
7
+ Adapted for Serverless/Edge, Next.js App Router, BFF, and DDD patterns.
8
+ ---
9
+
10
+ # system-design
11
+
12
+ ## Overview
13
+
14
+ Scalable system architecture blueprint based on the System Design Primer and DDIA. Enforces load balancing, multi-tier caching (Redis, CDN), database partitioning, CAP theorem tradeoffs, and rate limiting before code is written.
15
+
16
+ ## When to Use
17
+
18
+ Activate during the PLAN phase of any backend service, API design, database schema creation, or scalability optimization.
19
+
20
+ ## Rules & Patterns
21
+
22
+ Based on [donnemartin/system-design-primer](https://github.com/donnemartin/system-design-primer) - the most starred system design resource on GitHub.
23
+
24
+ ## Core Principle
25
+
26
+ > **Everything is a trade-off.** Before writing a single line of backend code, reason through the system at scale. A flat monolith that works now fails at 10× load.
27
+
28
+ ---
29
+
30
+ ## Mandatory Pre-Design Checklist
31
+
32
+ Before architecting any backend system, answer these questions:
33
+
34
+ 1. **Scale**: What is the expected QPS (queries per second)? Peak vs average?
35
+ 2. **Data volume**: How much data? Growth rate? 1GB? 1TB? 1PB?
36
+ 3. **Consistency vs Availability**: Can we tolerate eventual consistency? (CAP theorem)
37
+ 4. **Read/Write ratio**: Is it read-heavy (cache it!) or write-heavy (shard it!)?
38
+ 5. **Latency requirements**: Real-time (<100ms)? Near-real-time (<1s)? Batch?
39
+ 6. **Global distribution**: Single region or multi-region?
40
+ 7. **Deployment model**: Traditional servers, Serverless, or Edge functions?
41
+
42
+ ---
43
+
44
+ ## Core Architecture Patterns
45
+
46
+ ### Load Balancing
47
+
48
+ ```
49
+ Clients → Load Balancer → [App Server 1, App Server 2, App Server N]
50
+ ```
51
+
52
+ - Use **Round Robin** for stateless services
53
+ - Use **Least Connections** for varying request times
54
+ - Use **IP Hash** for session affinity (or move sessions to Redis)
55
+ - Always add **health checks** - remove unhealthy nodes automatically
56
+
57
+ **Rule**: Any service expecting > 1000 RPS needs a load balancer. No exceptions.
58
+
59
+ ### Caching Strategy
60
+
61
+ ```
62
+ App → [Cache Layer: Redis/Memcached] → Database
63
+ ```
64
+
65
+ Cache decision ladder (check in order):
66
+
67
+ 1. Is it read > write? → Cache it
68
+ 2. Is it expensive to compute? → Cache it
69
+ 3. Is it user-specific? → Cache with user key
70
+ 4. Is it global? → Shared cache, shorter TTL
71
+
72
+ **Cache patterns**:
73
+
74
+ - **Cache-aside** (lazy loading): check cache → miss → load DB → write cache
75
+ - **Write-through**: write to DB AND cache simultaneously (consistency > performance)
76
+ - **Write-behind**: write to cache → async flush to DB (performance > consistency)
77
+
78
+ **Invalidation**: Use TTL + event-driven invalidation. Never stale-forever.
79
+
80
+ **Modern framework-native caching (Next.js App Router)**:
81
+ Before spinning up a dedicated Redis instance for caching API responses, check if Next.js built-in mechanisms are sufficient:
82
+
83
+ - `revalidatePath('/dashboard')` - invalidate all cache for a route
84
+ - `revalidateTag('user-profile')` - fine-grained tagged cache invalidation
85
+ - `unstable_cache()` - server-side data caching with TTL
86
+
87
+ ```typescript
88
+ // [GOOD] Use Next.js native caching first
89
+ import { revalidateTag } from 'next/cache'
90
+
91
+ const getUser = unstable_cache(
92
+ async (id: string) => db.users.findById(id),
93
+ ['user'],
94
+ { tags: ['user-profile'], revalidate: 3600 }
95
+ )
96
+
97
+ // Invalidate on mutation:
98
+ await db.users.update(id, data)
99
+ revalidateTag('user-profile')
100
+
101
+ // [BAD] Don't add Redis for simple SSR caching when Next.js handles it
102
+ ```
103
+
104
+ ### Database Architecture
105
+
106
+ #### When to use SQL vs NoSQL
107
+
108
+ | Scenario | Use SQL | Use NoSQL |
109
+ | ---------- | --------- | ----------- |
110
+ | Complex joins, ACID transactions | [PASS] | [FAIL] |
111
+ | Flexible/evolving schema | [FAIL] | [PASS] |
112
+ | Horizontal scaling needed | Careful | [PASS] |
113
+ | Simple key-value lookup | Overkill | [PASS] |
114
+ | Full-text search | Use Elasticsearch | Use Elasticsearch |
115
+ | Time-series data | TimescaleDB | InfluxDB |
116
+
117
+ #### Scaling Databases
118
+
119
+ **Vertical scaling**: Bigger machine. Easy but has ceiling.
120
+ **Read replicas**: Route SELECT to replicas, writes to primary.
121
+ **Sharding (horizontal partitioning)**:
122
+
123
+ - Hash sharding: `user_id % N` - even distribution, hard to rebalance
124
+ - Range sharding: user_id 1-1M on shard 1 - easy range queries, hotspots risk
125
+ - Directory-based: lookup table maps key → shard - flexible, but lookup is overhead
126
+
127
+ **Denormalization**: For read-heavy systems, duplicate data to avoid joins.
128
+ **Rule**: Don't shard until you've maxed out read replicas.
129
+
130
+ ### Message Queues & Async Processing
131
+
132
+ ```
133
+ Producer → [Queue: Redis/RabbitMQ/Kafka] → Consumer Workers
134
+ ```
135
+
136
+ Use queues when:
137
+
138
+ - Operation takes > 200ms (email, PDF generation, ML inference)
139
+ - You need retry logic on failure
140
+ - You need to decouple services
141
+ - Traffic spikes need to be absorbed
142
+
143
+ **Kafka** = durability + replay + high throughput (events/analytics)
144
+ **Redis Queue** = simplicity + low latency (jobs/tasks)
145
+ **RabbitMQ** = complex routing + acknowledgements
146
+
147
+ ### Microservices vs Monolith
148
+
149
+ **Start with a monolith** unless you have > 10 engineers or proven scale need.
150
+
151
+ When to split into microservices:
152
+
153
+ - Independent deployment cycles needed
154
+ - Different scaling requirements per service
155
+ - Team autonomy (Conway's Law)
156
+ - Clear service boundaries (DDD bounded contexts)
157
+
158
+ **Rule**: A microservice should be able to be rewritten in 2 weeks by 2 engineers.
159
+
160
+ Service communication:
161
+
162
+ - **Sync (REST/gRPC)**: when caller needs immediate response
163
+ - **Async (events/queue)**: when caller can tolerate delay, or decoupling is needed
164
+ - **BFF / Server Actions**: for web apps, prefer typed client-server contracts (see below)
165
+
166
+ ---
167
+
168
+ ## Modern Stack Patterns (Serverless, Edge, Next.js)
169
+
170
+ ### Serverless & Edge Architecture
171
+
172
+ When deploying to serverless (Vercel Functions, AWS Lambda) or edge (Vercel Edge, Cloudflare Workers), the classical "App Server + Load Balancer" model changes:
173
+
174
+ **Cold Start Problem**:
175
+
176
+ - Serverless functions spin up from zero on first request - this can add 100-1000ms
177
+ - **Never** do heavy initialization at module level (DB connections, config loading, crypto keys)
178
+ - **Always** initialize lazily inside the handler, or use a connection pooling service
179
+
180
+ ```typescript
181
+ // [BAD] Wrong: Module-level initialization (runs on cold start, hangs the function)
182
+ const db = new DatabaseClient({ ... }) // top of file
183
+
184
+ // [GOOD] Correct: Lazy initialization with caching
185
+ let db: DatabaseClient | null = null
186
+ function getDb() {
187
+ if (!db) db = new DatabaseClient({ ... })
188
+ return db
189
+ }
190
+ ```
191
+
192
+ **DB Connection Pooling in Serverless**:
193
+
194
+ - Traditional in-process pools (pg-pool, knex) do NOT work in serverless - each invocation is ephemeral
195
+ - Use **Prisma Accelerate**, **PlanetScale**, **Neon** pooling, or **Supabase** - they handle pooling at the infrastructure level
196
+ - Rule: If deploying to Vercel/serverless, NEVER assume `max_connections` is managed by your app process
197
+
198
+ **Edge Functions limitations**:
199
+
200
+ - No Node.js APIs (no `fs`, no `crypto.randomBytes`, limited DNS)
201
+ - Latency must be < 50ms - no heavy DB queries
202
+ - Use edge for: auth token verification, A/B testing, geo-routing, lightweight transformations
203
+
204
+ ### BFF Pattern & Server Actions (Type-Safe Client-Server)
205
+
206
+ When building web apps, prefer typed client-server communication over generic REST endpoints:
207
+
208
+ **Option 1: Server Actions (Next.js App Router)**
209
+ For mutations that touch the database directly, skip the API route entirely:
210
+
211
+ ```typescript
212
+ // [GOOD] Server Action: No API route needed, fully type-safe
213
+ "use server"
214
+ export async function updateUser(id: string, data: UpdateUserInput) {
215
+ // Input validation (always!)
216
+ const validated = UpdateUserSchema.parse(data)
217
+
218
+ // Auth check (always before data access!)
219
+ const session = await getSession()
220
+ if (session.userId !== id && !session.isAdmin) {
221
+ throw new Error("Forbidden")
222
+ }
223
+
224
+ return db.users.update(id, validated)
225
+ }
226
+
227
+ // [BAD] Over-engineering: Don't create /api/users/[id] + fetch wrapper for simple mutations
228
+ ```
229
+
230
+ **Option 2: tRPC (Full-stack type safety)**
231
+ For complex APIs with many routes, use tRPC to get end-to-end type safety from DB to UI without code generation.
232
+
233
+ **Option 3: REST (When appropriate)**
234
+ When building a public API consumed by external clients or mobile apps - use REST with OpenAPI spec.
235
+
236
+ **Decision rule**:
237
+
238
+ - Internal web-to-DB mutation → **Server Action**
239
+ - Internal complex API → **tRPC**
240
+ - Public/mobile API → **REST + OpenAPI**
241
+
242
+ ### Domain-Driven Design (DDD) - Business Logic Isolation
243
+
244
+ **Rule**: NEVER write business logic inside API route handlers, Server Actions, or controllers. Always extract to dedicated services/use-cases.
245
+
246
+ ```
247
+ [FAIL] Wrong structure:
248
+ app/api/orders/route.ts ← contains: validation + auth + business logic + DB query
249
+
250
+ [PASS] Correct structure:
251
+ app/api/orders/route.ts ← only: parse request, call service, return response
252
+ src/services/order.service.ts ← all business logic, testable without HTTP context
253
+ src/repositories/order.repo.ts ← all DB queries
254
+ ```
255
+
256
+ Example:
257
+
258
+ ```typescript
259
+ // [BAD] Business logic in route (untestable, bloated)
260
+ export async function POST(req: Request) {
261
+ const data = await req.json()
262
+ if (data.quantity <= 0) return new Response("Invalid", { status: 400 })
263
+ const inventory = await db.inventory.findById(data.productId)
264
+ if (inventory.stock < data.quantity) return new Response("Out of stock", { status: 400 })
265
+ const total = inventory.price * data.quantity
266
+ // ... 40 more lines
267
+ }
268
+
269
+ // [GOOD] Thin route, fat service
270
+ export async function POST(req: Request) {
271
+ const data = await req.json()
272
+ const result = await orderService.createOrder(data)
273
+ return Response.json(result)
274
+ }
275
+
276
+ // orderService.createOrder() - pure function, fully unit-testable without HTTP
277
+ ```
278
+
279
+ ---
280
+
281
+ ## Scalability Design Patterns
282
+
283
+ ### CDN (Content Delivery Network)
284
+
285
+ - Serve static assets (JS, CSS, images) from CDN edge nodes
286
+ - Cache API responses that don't change per-user
287
+ - Reduce origin server load by 80%+
288
+
289
+ ### Rate Limiting
290
+
291
+ Always implement for public APIs:
292
+
293
+ ```
294
+ - Token bucket: smooth bursts, allows brief spikes
295
+ - Leaky bucket: strict rate, no bursts
296
+ - Fixed window: simple, vulnerable to boundary spikes
297
+ - Sliding window: most accurate, slightly more complex
298
+ ```
299
+
300
+ Store rate limit state in Redis (not in-process - it doesn't survive restarts).
301
+
302
+ ### Circuit Breaker
303
+
304
+ Prevent cascade failures:
305
+
306
+ ```
307
+ CLOSED (normal) → [failures > threshold] → OPEN (fail fast)
308
+ ↑ ↓
309
+ └────── [timeout] ← HALF-OPEN (test request) ──┘
310
+ ```
311
+
312
+ ### Database Connection Pooling
313
+
314
+ - **Traditional servers**: Use pg-pool, knex, Prisma connection pool
315
+ - **Serverless**: Use Prisma Accelerate, PlanetScale, Neon, or Supabase pooling - NOT in-process pools
316
+
317
+ ---
318
+
319
+ ## CAP Theorem in Practice
320
+
321
+ **You can only guarantee 2 of 3**: Consistency, Availability, Partition Tolerance
322
+
323
+ | System | Chooses | Example |
324
+ | -------- | --------- | --------- |
325
+ | Traditional SQL | CP | PostgreSQL |
326
+ | Distributed NoSQL | AP | DynamoDB, Cassandra |
327
+ | Cache | AP (tunable) | Redis with replication |
328
+
329
+ **For most apps**: Choose AP. Accept eventual consistency. Use optimistic locking for critical writes.
330
+
331
+ ---
332
+
333
+ ## Designing Data-Intensive Applications (DDIA) Patterns
334
+
335
+ Based on _Designing Data-Intensive Applications_ (Martin Kleppmann) and [ciembor/agent-rules-books](https://github.com/ciembor/agent-rules-books).
336
+
337
+ ### 1. The Dual-Write Problem & Transactional Outbox
338
+
339
+ **The Anti-Pattern**: Updating the database and sending a message to a broker (Kafka, RabbitMQ, SQS) in two separate operations. If one fails, the system enters an inconsistent state.
340
+
341
+ **The Solution**: Write the business entity AND an event record to an `outbox` table in the SAME database transaction:
342
+
343
+ ```sql
344
+ BEGIN TRANSACTION;
345
+ UPDATE orders SET status = 'PAID' WHERE id = 'ord_123';
346
+ INSERT INTO outbox_events (id, aggregate_type, aggregate_id, event_type, payload, created_at)
347
+ VALUES ('evt_456', 'Order', 'ord_123', 'OrderPaid', '{"amount": 99.00}', NOW());
348
+ COMMIT;
349
+ ```
350
+
351
+ A background process (polling worker or Debezium CDC) reads `outbox_events`, delivers them to the message broker, and marks them as published.
352
+
353
+ ### 2. Idempotency Invariant for Mutations
354
+
355
+ All write operations exposed over HTTP or queues MUST support deduplication:
356
+
357
+ - Accept an `Idempotency-Key` header (UUID or client-generated hash).
358
+ - Store key with status in Redis or DB with a TTL (e.g., 24 hours).
359
+ - If the key is already `COMPLETED`, return the cached response immediately without re-executing.
360
+ - If `IN_PROGRESS`, return HTTP `409 Conflict` or queue retry.
361
+
362
+ ### 3. Read-Your-Own-Writes Consistency
363
+
364
+ When using read replicas, replication lag (even 50ms) causes users to not see their own changes immediately after saving:
365
+
366
+ - **Rule**: Route user reads to the primary database for `N` seconds (e.g., 5s) following any mutation by that user.
367
+ - Route all other queries and background jobs to read replicas.
368
+
369
+ ---
370
+
371
+ ## Architecture Decision Template
372
+
373
+ When proposing any backend architecture, include:
374
+
375
+ ```markdown
376
+ ## System Design Decision
377
+
378
+ **Scale Target**: [X RPS, Y GB data, Z users]
379
+ **Deployment Model**: [Traditional servers | Serverless | Edge]
380
+ **CAP Choice**: [CP/AP] because [reason]
381
+ **Read/Write Ratio**: [X:Y]
382
+
383
+ ### Components
384
+ - **API Layer**: [REST/tRPC/Server Actions] - [why this choice]
385
+ - **Cache**: [Next.js native | Redis] for [what] with [TTL/tags strategy]
386
+ - **Database**: [SQL/NoSQL] - [pooling solution for serverless if applicable]
387
+ - **Async**: [Queue tech] for [what operations]
388
+
389
+ ### Business Logic Isolation
390
+ - Services: [list key service files]
391
+ - Repositories: [list key repo files]
392
+ - Routes/Actions: [thin handlers only]
393
+
394
+ ### Trade-offs Accepted
395
+ - [Trade-off 1]: [Why acceptable]
396
+ - [Trade-off 2]: [Why acceptable]
397
+
398
+ ### Scaling Path
399
+ 1. Now (MVP): [simple setup]
400
+ 2. At 10× load: [first scaling step]
401
+ 3. At 100× load: [next scaling step]
402
+ ```
403
+
404
+
405
+ ## Code Examples
406
+
407
+ See `EXAMPLES.md` for detailed code examples.
408
+
409
+ ## Validation Checklist
410
+
411
+ What to verify during the review phase before completing the task.
412
+
413
+ ## Common Mistakes
414
+
415
+ Anti-patterns and things to explicitly avoid. See `TROUBLESHOOTING.md`.
416
+
417
+ ## Integration Notes
418
+
419
+ How this skill interacts with other skills.
@@ -0,0 +1,19 @@
1
+ # system-design Troubleshooting & Common Mistakes
2
+
3
+ ## 1. Serverless Connection Exhaustion
4
+
5
+ - **Symptom**: "FATAL: remaining connection slots are reserved for non-replication superuser connections" under modest traffic.
6
+ - **Root Cause**: Serverless/Edge functions opening new DB connection pools per invoked instance.
7
+ - **Fix**: Use a connection pooler like PgBouncer or managed pooling (Supabase connection pool, AWS RDS Proxy, Prisma Accelerate).
8
+
9
+ ## 2. Cache Invalidation Drift
10
+
11
+ - **Symptom**: Users see stale, outdated data after making updates.
12
+ - **Root Cause**: Updates to database do not invalidate related cache keys, or TTLs are set to infinite.
13
+ - **Fix**: Invalidate cache keys explicitly on write in the same transactional flow, and always set defensive TTLs.
14
+
15
+ ## 3. Lack of Rate Limiting and Backpressure
16
+
17
+ - **Symptom**: Backend crashes or slows to a crawl during traffic spikes or bot scraping.
18
+ - **Root Cause**: Unthrottled public endpoints without token-bucket or sliding-window rate limiting.
19
+ - **Fix**: Add rate-limiting middleware (Redis-backed sliding window) at the API gateway / Edge layer.
@@ -0,0 +1,12 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "rules_followed": {
6
+ "type": "boolean"
7
+ }
8
+ },
9
+ "required": [
10
+ "rules_followed"
11
+ ]
12
+ }
@@ -0,0 +1,14 @@
1
+ schemaVersion: 2
2
+ name: system-design
3
+ category: architecture
4
+ type: instruction-only
5
+ description: >
6
+ Architecture and system design skill based on donnemartin's System Design Primer.
7
+ Teaches scalable architecture thinking: load balancers, caching, DB partitioning,
8
+ microservices, CAP theorem, and trade-off analysis before writing backend code.
9
+ version: 1.0.0
10
+ resources:
11
+ - EXAMPLES.md
12
+ - SKILL.md
13
+ - TROUBLESHOOTING.md
14
+ - VALIDATION.json