tribunal-kit 4.3.0 → 4.3.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 (98) hide show
  1. package/.agent/scripts/case_law_manager.js +684 -684
  2. package/.agent/scripts/graph_builder.js +199 -0
  3. package/.agent/scripts/graph_zoom.js +154 -0
  4. package/.agent/skills/agent-organizer/SKILL.md +9 -1
  5. package/.agent/skills/agentic-patterns/SKILL.md +9 -1
  6. package/.agent/skills/ai-prompt-injection-defense/SKILL.md +9 -1
  7. package/.agent/skills/api-patterns/SKILL.md +206 -198
  8. package/.agent/skills/api-security-auditor/SKILL.md +9 -1
  9. package/.agent/skills/app-builder/SKILL.md +9 -1
  10. package/.agent/skills/app-builder/templates/SKILL.md +77 -69
  11. package/.agent/skills/appflow-wireframe/SKILL.md +9 -1
  12. package/.agent/skills/architecture/SKILL.md +9 -1
  13. package/.agent/skills/authentication-best-practices/SKILL.md +9 -1
  14. package/.agent/skills/bash-linux/SKILL.md +9 -1
  15. package/.agent/skills/behavioral-modes/SKILL.md +9 -1
  16. package/.agent/skills/brainstorming/SKILL.md +9 -1
  17. package/.agent/skills/building-native-ui/SKILL.md +9 -1
  18. package/.agent/skills/clean-code/SKILL.md +9 -1
  19. package/.agent/skills/code-review-checklist/SKILL.md +9 -1
  20. package/.agent/skills/config-validator/SKILL.md +9 -1
  21. package/.agent/skills/csharp-developer/SKILL.md +9 -1
  22. package/.agent/skills/data-validation-schemas/SKILL.md +287 -279
  23. package/.agent/skills/database-design/SKILL.md +199 -191
  24. package/.agent/skills/deployment-procedures/SKILL.md +9 -1
  25. package/.agent/skills/devops-engineer/SKILL.md +9 -1
  26. package/.agent/skills/devops-incident-responder/SKILL.md +9 -1
  27. package/.agent/skills/documentation-templates/SKILL.md +9 -1
  28. package/.agent/skills/edge-computing/SKILL.md +9 -1
  29. package/.agent/skills/error-resilience/SKILL.md +387 -379
  30. package/.agent/skills/extract-design-system/SKILL.md +9 -1
  31. package/.agent/skills/framer-motion-expert/SKILL.md +203 -195
  32. package/.agent/skills/frontend-design/SKILL.md +160 -152
  33. package/.agent/skills/game-design-expert/SKILL.md +9 -1
  34. package/.agent/skills/game-engineering-expert/SKILL.md +9 -1
  35. package/.agent/skills/geo-fundamentals/SKILL.md +9 -1
  36. package/.agent/skills/github-operations/SKILL.md +9 -1
  37. package/.agent/skills/gsap-core/SKILL.md +54 -46
  38. package/.agent/skills/gsap-frameworks/SKILL.md +54 -46
  39. package/.agent/skills/gsap-performance/SKILL.md +54 -46
  40. package/.agent/skills/gsap-plugins/SKILL.md +54 -46
  41. package/.agent/skills/gsap-react/SKILL.md +54 -46
  42. package/.agent/skills/gsap-scrolltrigger/SKILL.md +54 -46
  43. package/.agent/skills/gsap-timeline/SKILL.md +54 -46
  44. package/.agent/skills/gsap-utils/SKILL.md +54 -46
  45. package/.agent/skills/i18n-localization/SKILL.md +9 -1
  46. package/.agent/skills/intelligent-routing/SKILL.md +38 -30
  47. package/.agent/skills/knowledge-graph/SKILL.md +36 -0
  48. package/.agent/skills/lint-and-validate/SKILL.md +9 -1
  49. package/.agent/skills/llm-engineering/SKILL.md +9 -1
  50. package/.agent/skills/local-first/SKILL.md +9 -1
  51. package/.agent/skills/mcp-builder/SKILL.md +9 -1
  52. package/.agent/skills/mobile-design/SKILL.md +222 -214
  53. package/.agent/skills/monorepo-management/SKILL.md +293 -285
  54. package/.agent/skills/motion-engineering/SKILL.md +193 -185
  55. package/.agent/skills/nextjs-react-expert/SKILL.md +193 -185
  56. package/.agent/skills/nodejs-best-practices/SKILL.md +9 -1
  57. package/.agent/skills/observability/SKILL.md +9 -1
  58. package/.agent/skills/parallel-agents/SKILL.md +9 -1
  59. package/.agent/skills/performance-profiling/SKILL.md +9 -1
  60. package/.agent/skills/plan-writing/SKILL.md +9 -1
  61. package/.agent/skills/platform-engineer/SKILL.md +9 -1
  62. package/.agent/skills/playwright-best-practices/SKILL.md +9 -1
  63. package/.agent/skills/powershell-windows/SKILL.md +9 -1
  64. package/.agent/skills/project-idioms/SKILL.md +9 -1
  65. package/.agent/skills/python-patterns/SKILL.md +9 -1
  66. package/.agent/skills/python-pro/SKILL.md +282 -274
  67. package/.agent/skills/react-specialist/SKILL.md +236 -228
  68. package/.agent/skills/readme-builder/SKILL.md +9 -1
  69. package/.agent/skills/realtime-patterns/SKILL.md +9 -1
  70. package/.agent/skills/red-team-tactics/SKILL.md +9 -1
  71. package/.agent/skills/rust-pro/SKILL.md +9 -1
  72. package/.agent/skills/seo-fundamentals/SKILL.md +9 -1
  73. package/.agent/skills/server-management/SKILL.md +9 -1
  74. package/.agent/skills/shadcn-ui-expert/SKILL.md +9 -1
  75. package/.agent/skills/skill-creator/SKILL.md +9 -1
  76. package/.agent/skills/sql-pro/SKILL.md +9 -1
  77. package/.agent/skills/supabase-postgres-best-practices/SKILL.md +9 -1
  78. package/.agent/skills/swiftui-expert/SKILL.md +9 -1
  79. package/.agent/skills/systematic-debugging/SKILL.md +9 -1
  80. package/.agent/skills/tailwind-patterns/SKILL.md +9 -1
  81. package/.agent/skills/tdd-workflow/SKILL.md +9 -1
  82. package/.agent/skills/test-result-analyzer/SKILL.md +9 -1
  83. package/.agent/skills/testing-patterns/SKILL.md +9 -1
  84. package/.agent/skills/trend-researcher/SKILL.md +9 -1
  85. package/.agent/skills/typescript-advanced/SKILL.md +294 -286
  86. package/.agent/skills/ui-ux-pro-max/SKILL.md +9 -1
  87. package/.agent/skills/ui-ux-researcher/SKILL.md +9 -1
  88. package/.agent/skills/vue-expert/SKILL.md +234 -226
  89. package/.agent/skills/vulnerability-scanner/SKILL.md +9 -1
  90. package/.agent/skills/web-accessibility-auditor/SKILL.md +9 -1
  91. package/.agent/skills/web-design-guidelines/SKILL.md +9 -1
  92. package/.agent/skills/webapp-testing/SKILL.md +9 -1
  93. package/.agent/skills/whimsy-injector/SKILL.md +9 -1
  94. package/.agent/skills/workflow-optimizer/SKILL.md +9 -1
  95. package/README.md +242 -242
  96. package/bin/tribunal-kit.js +30 -22
  97. package/package.json +81 -80
  98. package/scripts/validate-payload.js +73 -0
@@ -1,381 +1,381 @@
1
- ---
2
- name: error-resilience
3
- description: Error resilience and fault tolerance mastery. Retry strategies (exponential backoff with jitter), circuit breakers, bulkheads, graceful degradation, React error boundaries, dead letter queues, timeout patterns, fallback chains, and idempotent error recovery. Use when building fault-tolerant systems, handling flaky external services, or preventing cascading failures.
4
- allowed-tools: Read, Write, Edit, Glob, Grep
5
- version: 1.0.0
6
- last-updated: 2026-04-17
7
- applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
8
- ---
9
-
10
- ## Hallucination Traps (Read First)
11
- - ❌ Retrying non-idempotent operations (POST payments) without idempotency keys -> ✅ Always require idempotency keys for non-safe HTTP methods before retrying
12
- - ❌ Using fixed delay retries instead of exponential backoff -> ✅ Exponential backoff with jitter prevents thundering herd
13
- - ❌ Catching all errors and swallowing them -> ✅ Only catch recoverable (operational) errors; let programmer errors crash
14
-
15
- ---
16
-
17
- # Error Resilience — Fault-Tolerant Systems
18
-
19
- ---
20
-
21
- ## Error Classification (Always Do This First)
22
-
23
- ```typescript
24
- // ✅ CRITICAL: Classify errors before handling them
25
- // Operational errors = expected failures you CAN recover from
26
- // Programmer errors = bugs you CANNOT recover from
27
-
28
- class OperationalError extends Error {
29
- constructor(message: string, public readonly isRetryable: boolean = false) {
30
- super(message);
31
- this.name = "OperationalError";
32
- }
33
- }
34
-
35
- // ❌ BAD: Treating all errors the same
36
- catch (e) { console.log(e); }
37
-
38
- // ✅ GOOD: Classifying and routing
39
- catch (error) {
40
- if (error instanceof OperationalError && error.isRetryable) {
41
- return retry(operation);
42
- }
43
- if (error instanceof OperationalError) {
44
- return fallback(error);
45
- }
46
- // Programmer error — crash fast, fix the bug
47
- throw error;
48
- }
49
- ```
50
-
51
- ```
52
- ┌─────────────────────────────────────────────────────────────┐
53
- │ Operational (recoverable) │
54
- ├─────────────────────────────────────────────────────────────┤
55
- │ Network timeout → Retry with backoff │
56
- │ Rate limited (429) → Retry after Retry-After header │
57
- │ Service unavailable (503) → Circuit breaker + fallback │
58
- │ Database connection lost → Reconnect with pool │
59
- │ Input validation failed → Return 400 to client │
60
- │ File not found → Return 404 or create default │
61
- ├─────────────────────────────────────────────────────────────┤
62
- │ Programmer (crash immediately) │
63
- ├─────────────────────────────────────────────────────────────┤
64
- │ TypeError / ReferenceError → Bug — fix the code │
65
- │ Assertion failure → Invariant violated — fix logic │
66
- │ Undefined is not a function → Missing import or typo │
67
- │ Stack overflow → Infinite recursion — fix logic │
68
- └─────────────────────────────────────────────────────────────┘
69
- ```
70
-
71
- ---
72
-
73
- ## Retry with Exponential Backoff + Jitter
74
-
75
- ```typescript
76
- interface RetryOptions {
77
- maxRetries: number;
78
- baseDelayMs: number;
79
- maxDelayMs: number;
80
- retryableErrors?: (error: unknown) => boolean;
81
- }
82
-
83
- const DEFAULT_RETRY: RetryOptions = {
84
- maxRetries: 3,
85
- baseDelayMs: 500,
86
- maxDelayMs: 15_000,
87
- retryableErrors: (err) =>
88
- err instanceof Error && (
89
- err.message.includes("ECONNRESET") ||
90
- err.message.includes("ETIMEDOUT") ||
91
- err.message.includes("503") ||
92
- err.message.includes("429")
93
- ),
94
- };
95
-
96
- async function withRetry<T>(
97
- fn: () => Promise<T>,
98
- options: Partial<RetryOptions> = {},
99
- ): Promise<T> {
100
- const opts = { ...DEFAULT_RETRY, ...options };
101
-
102
- for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
103
- try {
104
- return await fn();
105
- } catch (error) {
106
- const isLast = attempt === opts.maxRetries;
107
- const isRetryable = opts.retryableErrors?.(error) ?? true;
108
-
109
- if (isLast || !isRetryable) throw error;
110
-
111
- // Exponential backoff with full jitter
112
- const exponential = opts.baseDelayMs * 2 ** attempt;
113
- const capped = Math.min(exponential, opts.maxDelayMs);
114
- const jitter = Math.random() * capped;
115
-
116
- console.warn(
117
- `[RETRY] Attempt ${attempt + 1}/${opts.maxRetries} failed. ` +
118
- `Retrying in ${Math.round(jitter)}ms...`
119
- );
120
-
121
- await sleep(jitter);
122
- }
123
- }
124
- throw new Error("Unreachable");
125
- }
126
-
127
- function sleep(ms: number): Promise<void> {
128
- return new Promise((resolve) => setTimeout(resolve, ms));
129
- }
130
-
131
- // ❌ NEVER retry non-idempotent operations without idempotency keys
132
- // ❌ withRetry(() => createPayment(order)); // could charge twice!
133
- // ✅ withRetry(() => createPayment(order, { idempotencyKey: order.id }));
134
- ```
135
-
136
- ---
137
-
138
- ## Circuit Breaker
139
-
140
- ```typescript
141
- enum CircuitState {
142
- CLOSED = "CLOSED", // Normal — requests pass through
143
- OPEN = "OPEN", // Tripped — requests fail immediately
144
- HALF_OPEN = "HALF_OPEN", // Testing — one request allowed
145
- }
146
-
147
- class CircuitBreaker {
148
- private state = CircuitState.CLOSED;
149
- private failureCount = 0;
150
- private lastFailureTime = 0;
151
- private successCount = 0;
152
-
153
- constructor(
154
- private readonly threshold: number = 5,
155
- private readonly resetTimeoutMs: number = 30_000,
156
- private readonly halfOpenMax: number = 3,
157
- ) {}
158
-
159
- async execute<T>(fn: () => Promise<T>, fallback?: () => T): Promise<T> {
160
- if (this.state === CircuitState.OPEN) {
161
- if (Date.now() - this.lastFailureTime > this.resetTimeoutMs) {
162
- this.state = CircuitState.HALF_OPEN;
163
- this.successCount = 0;
164
- } else {
165
- if (fallback) return fallback();
166
- throw new Error(`Circuit OPEN — service unavailable`);
167
- }
168
- }
169
-
170
- try {
171
- const result = await fn();
172
- this.onSuccess();
173
- return result;
174
- } catch (error) {
175
- this.onFailure();
176
- if (fallback) return fallback();
177
- throw error;
178
- }
179
- }
180
-
181
- private onSuccess(): void {
182
- if (this.state === CircuitState.HALF_OPEN) {
183
- this.successCount++;
184
- if (this.successCount >= this.halfOpenMax) {
185
- this.state = CircuitState.CLOSED; // Recovery confirmed
186
- this.failureCount = 0;
187
- }
188
- } else {
189
- this.failureCount = 0;
190
- }
191
- }
192
-
193
- private onFailure(): void {
194
- this.failureCount++;
195
- this.lastFailureTime = Date.now();
196
- if (this.failureCount >= this.threshold) {
197
- this.state = CircuitState.OPEN;
198
- }
199
- }
200
- }
201
-
202
- // Usage
203
- const paymentCircuit = new CircuitBreaker(5, 30_000);
204
-
205
- const result = await paymentCircuit.execute(
206
- () => paymentGateway.charge(amount),
207
- () => ({ status: "deferred", message: "Payment queued for retry" }),
208
- );
209
- ```
210
-
211
- ---
212
-
213
- ## React Error Boundaries
214
-
215
- ```tsx
216
- // ✅ Error boundary with recovery
217
- import { Component, type ReactNode } from "react";
218
-
219
- interface Props {
220
- children: ReactNode;
221
- fallback?: ReactNode;
222
- onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
223
- }
224
-
225
- interface State {
226
- hasError: boolean;
227
- error: Error | null;
228
- }
229
-
230
- class ErrorBoundary extends Component<Props, State> {
231
- state: State = { hasError: false, error: null };
232
-
233
- static getDerivedStateFromError(error: Error): State {
234
- return { hasError: true, error };
235
- }
236
-
237
- componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
238
- this.props.onError?.(error, errorInfo);
239
- // Send to error tracking (Sentry, etc.)
240
- }
241
-
242
- render() {
243
- if (this.state.hasError) {
244
- return this.props.fallback ?? (
245
- <div role="alert">
246
- <h2>Something went wrong</h2>
247
- <button onClick={() => this.setState({ hasError: false, error: null })}>
248
- Try Again
249
- </button>
250
- </div>
251
- );
252
- }
253
- return this.props.children;
254
- }
255
- }
256
-
257
- // ❌ TRAP: Error boundaries do NOT catch:
258
- // - Event handlers (use try/catch inside handlers)
259
- // - Async code (use Promise.catch or error state)
260
- // - Server-side rendering errors
261
- // - Errors in the error boundary itself
262
- ```
263
-
264
- ---
265
-
266
- ## Timeout Patterns
267
-
268
- ```typescript
269
- // ✅ AbortController-based timeout (modern, cancellable)
270
- async function withTimeout<T>(
271
- fn: (signal: AbortSignal) => Promise<T>,
272
- timeoutMs: number,
273
- ): Promise<T> {
274
- const controller = new AbortController();
275
- const timer = setTimeout(() => controller.abort(), timeoutMs);
276
-
277
- try {
278
- return await fn(controller.signal);
279
- } catch (error) {
280
- if (controller.signal.aborted) {
281
- throw new OperationalError(`Operation timed out after ${timeoutMs}ms`, true);
282
- }
283
- throw error;
284
- } finally {
285
- clearTimeout(timer);
286
- }
287
- }
288
-
289
- // Usage
290
- const data = await withTimeout(
291
- (signal) => fetch("https://api.example.com/data", { signal }),
292
- 5000,
293
- );
294
- ```
295
-
296
- ---
297
-
298
- ## Graceful Degradation (Fallback Chains)
299
-
300
- ```typescript
301
- // ✅ Layered fallback: primary → cache → stale → default
302
- async function getUserProfile(userId: string): Promise<UserProfile> {
303
- // Layer 1: Primary source
304
- try {
305
- return await api.getUser(userId);
306
- } catch { /* fall through */ }
307
-
308
- // Layer 2: Cache
309
- try {
310
- const cached = await cache.get(`user:${userId}`);
311
- if (cached) return { ...cached, _stale: true };
312
- } catch { /* fall through */ }
313
-
314
- // Layer 3: Default
315
- return {
316
- id: userId,
317
- name: "Unknown User",
318
- avatar: "/default-avatar.png",
319
- _stale: true,
320
- _default: true,
321
- };
322
- }
323
-
324
- // ❌ BAD: Crash the whole page because one API is down
325
- // ✅ GOOD: Show stale/partial data with a warning banner
326
- ```
327
-
328
- ---
329
-
330
- ## Dead Letter Queue Pattern
331
-
332
- ```typescript
333
- // When a message/job fails after all retries, don't lose it
334
- interface DeadLetter<T> {
335
- payload: T;
336
- error: string;
337
- failedAt: string;
338
- attempts: number;
339
- originalQueue: string;
340
- }
341
-
342
- async function processWithDLQ<T>(
343
- payload: T,
344
- processor: (item: T) => Promise<void>,
345
- dlqStore: { push: (item: DeadLetter<T>) => Promise<void> },
346
- ): Promise<void> {
347
- try {
348
- await withRetry(() => processor(payload), { maxRetries: 3 });
349
- } catch (error) {
350
- // Exhausted retries — park in dead letter queue
351
- await dlqStore.push({
352
- payload,
353
- error: error instanceof Error ? error.message : String(error),
354
- failedAt: new Date().toISOString(),
355
- attempts: 4,
356
- originalQueue: "main",
357
- });
358
- // Don't throw — the message is preserved for manual review
359
- }
360
- }
361
- ```
362
-
363
- ---
364
-
365
- ## Anti-Patterns (Never Do These)
366
-
367
- ```
368
- ❌ Swallowing errors silently: catch (e) { /* empty */ }
369
- ❌ Retrying infinitely without a max — causes resource exhaustion
370
- ❌ Retrying POST/DELETE without idempotency keys
371
- ❌ Using fixed-delay retries — causes thundering herd
372
- ❌ Catching Error base class when you mean a specific subclass
373
- ❌ Logging error.message but not error.stack
374
- ❌ Returning null to indicate failure (use Result type or throw)
375
- ❌ Wrapping synchronous code in try/catch when it can't fail
376
- ```
377
-
378
- ---
1
+ ---
2
+ name: error-resilience
3
+ description: Error resilience and fault tolerance mastery. Retry strategies (exponential backoff with jitter), circuit breakers, bulkheads, graceful degradation, React error boundaries, dead letter queues, timeout patterns, fallback chains, and idempotent error recovery. Use when building fault-tolerant systems, handling flaky external services, or preventing cascading failures.
4
+ allowed-tools: Read, Write, Edit, Glob, Grep
5
+ version: 1.0.0
6
+ last-updated: 2026-04-17
7
+ applies-to-model: gemini-2.5-pro, claude-3-7-sonnet
8
+ ---
9
+
10
+ ## Hallucination Traps (Read First)
11
+ - ❌ Retrying non-idempotent operations (POST payments) without idempotency keys -> ✅ Always require idempotency keys for non-safe HTTP methods before retrying
12
+ - ❌ Using fixed delay retries instead of exponential backoff -> ✅ Exponential backoff with jitter prevents thundering herd
13
+ - ❌ Catching all errors and swallowing them -> ✅ Only catch recoverable (operational) errors; let programmer errors crash
14
+
15
+ ---
16
+
17
+ # Error Resilience — Fault-Tolerant Systems
18
+
19
+ ---
20
+
21
+ ## Error Classification (Always Do This First)
22
+
23
+ ```typescript
24
+ // ✅ CRITICAL: Classify errors before handling them
25
+ // Operational errors = expected failures you CAN recover from
26
+ // Programmer errors = bugs you CANNOT recover from
27
+
28
+ class OperationalError extends Error {
29
+ constructor(message: string, public readonly isRetryable: boolean = false) {
30
+ super(message);
31
+ this.name = "OperationalError";
32
+ }
33
+ }
34
+
35
+ // ❌ BAD: Treating all errors the same
36
+ catch (e) { console.log(e); }
37
+
38
+ // ✅ GOOD: Classifying and routing
39
+ catch (error) {
40
+ if (error instanceof OperationalError && error.isRetryable) {
41
+ return retry(operation);
42
+ }
43
+ if (error instanceof OperationalError) {
44
+ return fallback(error);
45
+ }
46
+ // Programmer error — crash fast, fix the bug
47
+ throw error;
48
+ }
49
+ ```
50
+
51
+ ```
52
+ ┌─────────────────────────────────────────────────────────────┐
53
+ │ Operational (recoverable) │
54
+ ├─────────────────────────────────────────────────────────────┤
55
+ │ Network timeout → Retry with backoff │
56
+ │ Rate limited (429) → Retry after Retry-After header │
57
+ │ Service unavailable (503) → Circuit breaker + fallback │
58
+ │ Database connection lost → Reconnect with pool │
59
+ │ Input validation failed → Return 400 to client │
60
+ │ File not found → Return 404 or create default │
61
+ ├─────────────────────────────────────────────────────────────┤
62
+ │ Programmer (crash immediately) │
63
+ ├─────────────────────────────────────────────────────────────┤
64
+ │ TypeError / ReferenceError → Bug — fix the code │
65
+ │ Assertion failure → Invariant violated — fix logic │
66
+ │ Undefined is not a function → Missing import or typo │
67
+ │ Stack overflow → Infinite recursion — fix logic │
68
+ └─────────────────────────────────────────────────────────────┘
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Retry with Exponential Backoff + Jitter
74
+
75
+ ```typescript
76
+ interface RetryOptions {
77
+ maxRetries: number;
78
+ baseDelayMs: number;
79
+ maxDelayMs: number;
80
+ retryableErrors?: (error: unknown) => boolean;
81
+ }
82
+
83
+ const DEFAULT_RETRY: RetryOptions = {
84
+ maxRetries: 3,
85
+ baseDelayMs: 500,
86
+ maxDelayMs: 15_000,
87
+ retryableErrors: (err) =>
88
+ err instanceof Error && (
89
+ err.message.includes("ECONNRESET") ||
90
+ err.message.includes("ETIMEDOUT") ||
91
+ err.message.includes("503") ||
92
+ err.message.includes("429")
93
+ ),
94
+ };
95
+
96
+ async function withRetry<T>(
97
+ fn: () => Promise<T>,
98
+ options: Partial<RetryOptions> = {},
99
+ ): Promise<T> {
100
+ const opts = { ...DEFAULT_RETRY, ...options };
101
+
102
+ for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
103
+ try {
104
+ return await fn();
105
+ } catch (error) {
106
+ const isLast = attempt === opts.maxRetries;
107
+ const isRetryable = opts.retryableErrors?.(error) ?? true;
108
+
109
+ if (isLast || !isRetryable) throw error;
110
+
111
+ // Exponential backoff with full jitter
112
+ const exponential = opts.baseDelayMs * 2 ** attempt;
113
+ const capped = Math.min(exponential, opts.maxDelayMs);
114
+ const jitter = Math.random() * capped;
115
+
116
+ console.warn(
117
+ `[RETRY] Attempt ${attempt + 1}/${opts.maxRetries} failed. ` +
118
+ `Retrying in ${Math.round(jitter)}ms...`
119
+ );
120
+
121
+ await sleep(jitter);
122
+ }
123
+ }
124
+ throw new Error("Unreachable");
125
+ }
126
+
127
+ function sleep(ms: number): Promise<void> {
128
+ return new Promise((resolve) => setTimeout(resolve, ms));
129
+ }
130
+
131
+ // ❌ NEVER retry non-idempotent operations without idempotency keys
132
+ // ❌ withRetry(() => createPayment(order)); // could charge twice!
133
+ // ✅ withRetry(() => createPayment(order, { idempotencyKey: order.id }));
134
+ ```
135
+
136
+ ---
137
+
138
+ ## Circuit Breaker
139
+
140
+ ```typescript
141
+ enum CircuitState {
142
+ CLOSED = "CLOSED", // Normal — requests pass through
143
+ OPEN = "OPEN", // Tripped — requests fail immediately
144
+ HALF_OPEN = "HALF_OPEN", // Testing — one request allowed
145
+ }
146
+
147
+ class CircuitBreaker {
148
+ private state = CircuitState.CLOSED;
149
+ private failureCount = 0;
150
+ private lastFailureTime = 0;
151
+ private successCount = 0;
152
+
153
+ constructor(
154
+ private readonly threshold: number = 5,
155
+ private readonly resetTimeoutMs: number = 30_000,
156
+ private readonly halfOpenMax: number = 3,
157
+ ) {}
158
+
159
+ async execute<T>(fn: () => Promise<T>, fallback?: () => T): Promise<T> {
160
+ if (this.state === CircuitState.OPEN) {
161
+ if (Date.now() - this.lastFailureTime > this.resetTimeoutMs) {
162
+ this.state = CircuitState.HALF_OPEN;
163
+ this.successCount = 0;
164
+ } else {
165
+ if (fallback) return fallback();
166
+ throw new Error(`Circuit OPEN — service unavailable`);
167
+ }
168
+ }
169
+
170
+ try {
171
+ const result = await fn();
172
+ this.onSuccess();
173
+ return result;
174
+ } catch (error) {
175
+ this.onFailure();
176
+ if (fallback) return fallback();
177
+ throw error;
178
+ }
179
+ }
180
+
181
+ private onSuccess(): void {
182
+ if (this.state === CircuitState.HALF_OPEN) {
183
+ this.successCount++;
184
+ if (this.successCount >= this.halfOpenMax) {
185
+ this.state = CircuitState.CLOSED; // Recovery confirmed
186
+ this.failureCount = 0;
187
+ }
188
+ } else {
189
+ this.failureCount = 0;
190
+ }
191
+ }
192
+
193
+ private onFailure(): void {
194
+ this.failureCount++;
195
+ this.lastFailureTime = Date.now();
196
+ if (this.failureCount >= this.threshold) {
197
+ this.state = CircuitState.OPEN;
198
+ }
199
+ }
200
+ }
201
+
202
+ // Usage
203
+ const paymentCircuit = new CircuitBreaker(5, 30_000);
204
+
205
+ const result = await paymentCircuit.execute(
206
+ () => paymentGateway.charge(amount),
207
+ () => ({ status: "deferred", message: "Payment queued for retry" }),
208
+ );
209
+ ```
210
+
211
+ ---
212
+
213
+ ## React Error Boundaries
214
+
215
+ ```tsx
216
+ // ✅ Error boundary with recovery
217
+ import { Component, type ReactNode } from "react";
218
+
219
+ interface Props {
220
+ children: ReactNode;
221
+ fallback?: ReactNode;
222
+ onError?: (error: Error, errorInfo: React.ErrorInfo) => void;
223
+ }
224
+
225
+ interface State {
226
+ hasError: boolean;
227
+ error: Error | null;
228
+ }
229
+
230
+ class ErrorBoundary extends Component<Props, State> {
231
+ state: State = { hasError: false, error: null };
232
+
233
+ static getDerivedStateFromError(error: Error): State {
234
+ return { hasError: true, error };
235
+ }
236
+
237
+ componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
238
+ this.props.onError?.(error, errorInfo);
239
+ // Send to error tracking (Sentry, etc.)
240
+ }
241
+
242
+ render() {
243
+ if (this.state.hasError) {
244
+ return this.props.fallback ?? (
245
+ <div role="alert">
246
+ <h2>Something went wrong</h2>
247
+ <button onClick={() => this.setState({ hasError: false, error: null })}>
248
+ Try Again
249
+ </button>
250
+ </div>
251
+ );
252
+ }
253
+ return this.props.children;
254
+ }
255
+ }
256
+
257
+ // ❌ TRAP: Error boundaries do NOT catch:
258
+ // - Event handlers (use try/catch inside handlers)
259
+ // - Async code (use Promise.catch or error state)
260
+ // - Server-side rendering errors
261
+ // - Errors in the error boundary itself
262
+ ```
263
+
264
+ ---
265
+
266
+ ## Timeout Patterns
267
+
268
+ ```typescript
269
+ // ✅ AbortController-based timeout (modern, cancellable)
270
+ async function withTimeout<T>(
271
+ fn: (signal: AbortSignal) => Promise<T>,
272
+ timeoutMs: number,
273
+ ): Promise<T> {
274
+ const controller = new AbortController();
275
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
276
+
277
+ try {
278
+ return await fn(controller.signal);
279
+ } catch (error) {
280
+ if (controller.signal.aborted) {
281
+ throw new OperationalError(`Operation timed out after ${timeoutMs}ms`, true);
282
+ }
283
+ throw error;
284
+ } finally {
285
+ clearTimeout(timer);
286
+ }
287
+ }
288
+
289
+ // Usage
290
+ const data = await withTimeout(
291
+ (signal) => fetch("https://api.example.com/data", { signal }),
292
+ 5000,
293
+ );
294
+ ```
295
+
296
+ ---
297
+
298
+ ## Graceful Degradation (Fallback Chains)
299
+
300
+ ```typescript
301
+ // ✅ Layered fallback: primary → cache → stale → default
302
+ async function getUserProfile(userId: string): Promise<UserProfile> {
303
+ // Layer 1: Primary source
304
+ try {
305
+ return await api.getUser(userId);
306
+ } catch { /* fall through */ }
307
+
308
+ // Layer 2: Cache
309
+ try {
310
+ const cached = await cache.get(`user:${userId}`);
311
+ if (cached) return { ...cached, _stale: true };
312
+ } catch { /* fall through */ }
313
+
314
+ // Layer 3: Default
315
+ return {
316
+ id: userId,
317
+ name: "Unknown User",
318
+ avatar: "/default-avatar.png",
319
+ _stale: true,
320
+ _default: true,
321
+ };
322
+ }
323
+
324
+ // ❌ BAD: Crash the whole page because one API is down
325
+ // ✅ GOOD: Show stale/partial data with a warning banner
326
+ ```
327
+
328
+ ---
329
+
330
+ ## Dead Letter Queue Pattern
331
+
332
+ ```typescript
333
+ // When a message/job fails after all retries, don't lose it
334
+ interface DeadLetter<T> {
335
+ payload: T;
336
+ error: string;
337
+ failedAt: string;
338
+ attempts: number;
339
+ originalQueue: string;
340
+ }
341
+
342
+ async function processWithDLQ<T>(
343
+ payload: T,
344
+ processor: (item: T) => Promise<void>,
345
+ dlqStore: { push: (item: DeadLetter<T>) => Promise<void> },
346
+ ): Promise<void> {
347
+ try {
348
+ await withRetry(() => processor(payload), { maxRetries: 3 });
349
+ } catch (error) {
350
+ // Exhausted retries — park in dead letter queue
351
+ await dlqStore.push({
352
+ payload,
353
+ error: error instanceof Error ? error.message : String(error),
354
+ failedAt: new Date().toISOString(),
355
+ attempts: 4,
356
+ originalQueue: "main",
357
+ });
358
+ // Don't throw — the message is preserved for manual review
359
+ }
360
+ }
361
+ ```
362
+
363
+ ---
364
+
365
+ ## Anti-Patterns (Never Do These)
366
+
367
+ ```
368
+ ❌ Swallowing errors silently: catch (e) { /* empty */ }
369
+ ❌ Retrying infinitely without a max — causes resource exhaustion
370
+ ❌ Retrying POST/DELETE without idempotency keys
371
+ ❌ Using fixed-delay retries — causes thundering herd
372
+ ❌ Catching Error base class when you mean a specific subclass
373
+ ❌ Logging error.message but not error.stack
374
+ ❌ Returning null to indicate failure (use Result type or throw)
375
+ ❌ Wrapping synchronous code in try/catch when it can't fail
376
+ ```
377
+
378
+ ---
379
379
 
380
380
 
381
381
  ---
@@ -417,4 +417,12 @@ Review these questions before confirming output:
417
417
 
418
418
  **CRITICAL:** You must follow a strict "evidence-based closeout" state machine.
419
419
  - ❌ **Forbidden:** Declaring a task complete because the output "looks correct."
420
- - ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.
420
+ - ✅ **Required:** You are explicitly forbidden from finalizing any task without providing **concrete evidence** (terminal output, passing tests, compile success, or equivalent proof) that your output works as intended.
421
+
422
+
423
+ ## Pre-Flight Checklist
424
+ - [ ] Have I reviewed the user's specific constraints and requests?
425
+ - [ ] Have I checked the environment for relevant existing implementations?
426
+
427
+ ## VBC Protocol (Verification-Before-Completion)
428
+ You MUST verify existing code signatures and variables before attempting to modify or call them. No hallucination is permitted.