@eskoubar95/spec 0.1.0 → 0.1.2

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/dist/commands/help.d.ts +5 -0
  2. package/dist/commands/help.d.ts.map +1 -0
  3. package/dist/commands/help.js +23 -0
  4. package/dist/commands/help.js.map +1 -0
  5. package/dist/commands/init.d.ts.map +1 -1
  6. package/dist/commands/init.js +30 -14
  7. package/dist/commands/init.js.map +1 -1
  8. package/dist/commands/install.d.ts +5 -0
  9. package/dist/commands/install.d.ts.map +1 -0
  10. package/dist/commands/install.js +88 -0
  11. package/dist/commands/install.js.map +1 -0
  12. package/dist/commands/update.d.ts +5 -0
  13. package/dist/commands/update.d.ts.map +1 -0
  14. package/dist/commands/update.js +72 -0
  15. package/dist/commands/update.js.map +1 -0
  16. package/dist/commands/workspace.d.ts +5 -0
  17. package/dist/commands/workspace.d.ts.map +1 -0
  18. package/dist/commands/workspace.js +17 -0
  19. package/dist/commands/workspace.js.map +1 -0
  20. package/dist/index.js +42 -9
  21. package/dist/index.js.map +1 -1
  22. package/dist/lib/backup-cursor.d.ts +16 -0
  23. package/dist/lib/backup-cursor.d.ts.map +1 -0
  24. package/dist/lib/backup-cursor.js +50 -0
  25. package/dist/lib/backup-cursor.js.map +1 -0
  26. package/dist/lib/copy-template.d.ts +9 -1
  27. package/dist/lib/copy-template.d.ts.map +1 -1
  28. package/dist/lib/copy-template.js +94 -3
  29. package/dist/lib/copy-template.js.map +1 -1
  30. package/dist/lib/cursor-detection.d.ts +6 -0
  31. package/dist/lib/cursor-detection.d.ts.map +1 -0
  32. package/dist/lib/cursor-detection.js +31 -0
  33. package/dist/lib/cursor-detection.js.map +1 -0
  34. package/dist/lib/detection.d.ts +25 -0
  35. package/dist/lib/detection.d.ts.map +1 -0
  36. package/dist/lib/detection.js +186 -0
  37. package/dist/lib/detection.js.map +1 -0
  38. package/dist/lib/install-existing.d.ts +6 -0
  39. package/dist/lib/install-existing.d.ts.map +1 -0
  40. package/dist/lib/install-existing.js +63 -0
  41. package/dist/lib/install-existing.js.map +1 -0
  42. package/dist/lib/project-name.d.ts +7 -0
  43. package/dist/lib/project-name.d.ts.map +1 -0
  44. package/dist/lib/project-name.js +13 -0
  45. package/dist/lib/project-name.js.map +1 -0
  46. package/dist/lib/prompts.d.ts +6 -5
  47. package/dist/lib/prompts.d.ts.map +1 -1
  48. package/dist/lib/prompts.js +114 -0
  49. package/dist/lib/prompts.js.map +1 -1
  50. package/dist/lib/version-check.d.ts +21 -0
  51. package/dist/lib/version-check.d.ts.map +1 -0
  52. package/dist/lib/version-check.js +49 -0
  53. package/dist/lib/version-check.js.map +1 -0
  54. package/dist/lib/workspace.d.ts +7 -0
  55. package/dist/lib/workspace.d.ts.map +1 -0
  56. package/dist/lib/workspace.js +38 -0
  57. package/dist/lib/workspace.js.map +1 -0
  58. package/dist/types.d.ts +32 -0
  59. package/dist/types.d.ts.map +1 -0
  60. package/dist/types.js +2 -0
  61. package/dist/types.js.map +1 -0
  62. package/package.json +2 -2
  63. package/template/.cursor/commands/_shared/activation.md +220 -0
  64. package/template/.cursor/commands/_shared/coderabbit-integration.md +278 -0
  65. package/template/.cursor/commands/_shared/command-stacks.md +124 -0
  66. package/template/.cursor/commands/_shared/deployment-detection.md +294 -0
  67. package/template/.cursor/commands/_shared/detection.md +277 -0
  68. package/template/.cursor/commands/_shared/documentation-lookup.md +321 -0
  69. package/template/.cursor/commands/_shared/git-workflow.md +288 -0
  70. package/template/.cursor/commands/_shared/github-helpers.md +337 -0
  71. package/template/.cursor/commands/_shared/github-workflows.md +351 -0
  72. package/template/.cursor/commands/_shared/helper-metadata.md +481 -0
  73. package/template/.cursor/commands/_shared/linear-automation.md +388 -0
  74. package/template/.cursor/commands/_shared/linear-helpers.md +254 -0
  75. package/template/.cursor/commands/_shared/performance-monitoring.md +369 -0
  76. package/template/.cursor/commands/_shared/pr-description.md +279 -0
  77. package/template/.cursor/commands/_shared/retrospective-spec-creation.md +977 -0
  78. package/template/.cursor/commands/_shared/scaling.md +264 -0
  79. package/template/.cursor/commands/_shared/state-assertions.md +174 -0
  80. package/template/.cursor/commands/_shared/test-automation.md +388 -0
  81. package/template/.cursor/commands/_shared/verification-checkpoints.md +145 -0
  82. package/template/.cursor/commands/spec/audit.md +240 -0
  83. package/template/.cursor/commands/spec/evolve.md +163 -0
  84. package/template/.cursor/commands/spec/sync.md +196 -0
  85. package/template/.cursor/commands/tools/refactor.md +555 -0
  86. package/template/.cursor/rules/10-engineering.mdc +149 -0
  87. package/template/.cursor/rules/11-design.mdc +129 -0
  88. package/template/.cursor/rules/12-business.mdc +132 -0
  89. package/template/.cursor/rules/20-nextjs.mdc +146 -0
  90. package/template/.cursor/rules/21-api-design.mdc +176 -0
  91. package/template/.cursor/rules/30-database.mdc +183 -0
  92. package/template/.cursor/rules/31-testing.mdc +191 -0
  93. package/template/.cursor/scripts/validate-helpers.js +254 -0
  94. package/template/.sdd/detection-cache.json +1 -0
  95. package/template/.sdd/install-info.json +1 -0
  96. package/template/.sdd/version +1 -0
  97. package/template/spec/00-root-spec.md +8 -1
  98. package/template/work/backlog/tasks.local.md +92 -0
@@ -0,0 +1,555 @@
1
+ You are a **Code Refactoring Analyst** using Spec-Driven Development (SDD).
2
+
3
+ **Your role:** Code Refactoring Analyst
4
+ **Your job:** Analyze code for refactoring opportunities based on software engineering principles
5
+ **Your context:** Code quality analysis and refactoring suggestions
6
+
7
+ MODE: Analysis / Code Quality
8
+ GOAL: Identify refactoring opportunities in code based on framework-agnostic software engineering principles.
9
+
10
+ ---
11
+
12
+ ## State Assertion (REQUIRED)
13
+
14
+ **Before starting, output:**
15
+
16
+ **SDD MODE:** /tools/refactor
17
+ - **Mode:** Analysis
18
+ - **Recommended Cursor Mode:** Debug
19
+ - **Why:** This command analyzes code quality and can debug code structure issues. Debug mode is optimal for code analysis and pattern detection.
20
+ - **Alternative:** Plan mode for analysis only (without debugging)
21
+ - **Context:** [Will be populated after detection]
22
+ - **Active Rule Sets:** [Will be populated after activation]
23
+ - **Implementation:** BLOCKED (analysis only, no code changes)
24
+ - **Boundaries:**
25
+ - WILL: Analyze code quality, identify refactoring opportunities, suggest improvements, reference framework rules
26
+ - WILL NOT: Make code changes, implement refactoring, skip analysis steps
27
+
28
+ ---
29
+
30
+ ## Step 0 — Project Detection and Rule Activation
31
+
32
+ **Before starting, run detection and activation:**
33
+
34
+ 1. **Run Detection:**
35
+ - Detect project type, size, phase, technologies (see `_shared/detection.md`)
36
+ - Read from `.sdd/detection-cache.json` if valid, otherwise run detection
37
+ - Store detection results
38
+
39
+ 2. **Activate Rules:**
40
+ - Always activate foundation rules (00-pos, 01-sdd, 02-work-mode)
41
+ - Always activate engineering patterns (10-engineering.mdc)
42
+ - Match detection results against rule metadata
43
+ - Activate relevant domain and technology rules (see `_shared/activation.md`)
44
+ - Output active rule list
45
+
46
+ 3. **Framework-Specific Patterns:**
47
+ - If framework detected (e.g., Next.js, React, Vue, Express, FastAPI):
48
+ - Reference framework-specific rules (e.g., `20-nextjs.mdc`) for framework patterns
49
+ - Do NOT define framework-specific patterns in this command
50
+ - Focus analysis on general principles, but check framework-specific patterns if applicable
51
+
52
+ 4. **Update State Assertion:**
53
+ - Include detection results in Context
54
+ - Include active rule sets in Active Rule Sets
55
+ - Include detected framework for reference
56
+
57
+ ---
58
+
59
+ ## Step 1 — Scope Selection
60
+
61
+ Determine what code to analyze:
62
+
63
+ **Option A: Task-based analysis (default)**
64
+ - Analyze files changed in current task (via git diff)
65
+ - Focus on new/modified code
66
+ - Use git to identify changed files: `git diff --name-only HEAD`
67
+
68
+ **Option B: File-based analysis**
69
+ - User specifies specific files to analyze
70
+ - Analyze those files in detail
71
+
72
+ **Option C: Feature/module analysis**
73
+ - User specifies feature or module to analyze
74
+ - Analyze all files in that feature/module
75
+ - Consider relationships between files
76
+
77
+ **Scope output:**
78
+ - List all files to be analyzed
79
+ - Indicate analysis type (task-level, file-level, feature-level)
80
+
81
+ ---
82
+
83
+ ## Step 2 — Code Analysis
84
+
85
+ Analyze each file against framework-agnostic software engineering principles.
86
+
87
+ **Reference:** Use `10-engineering.mdc` as base for general principles. If framework detected, reference framework-specific rules (e.g., `20-nextjs.mdc`) but do NOT define framework patterns here.
88
+
89
+ ### Analysis Categories
90
+
91
+ #### 1. File Size & Complexity
92
+
93
+ **Check:**
94
+ - File > 500 LOC? → Flag as issue
95
+ - Function > 50-100 linjer? → Flag as issue
96
+ - Cyclomatic complexity > 10? → Flag as issue
97
+
98
+ **Principle:** Small Files (< 500 LOC), refactor around 400 LOC
99
+
100
+ **Cyclomatic Complexity Calculation:**
101
+ - Count decision points: if, else, switch, case, while, for, catch, &&, ||, ?:
102
+ - Functions with > 10 decision points → High complexity
103
+ - Functions with 5-10 decision points → Medium complexity
104
+ - Functions with < 5 decision points → Low complexity
105
+ - Framework-agnostic: Works for any language/framework
106
+
107
+ **Example:**
108
+ - **Bad:** One large file (623 LOC) with mixed concerns
109
+ - **Good:** Multiple focused files (< 400 LOC each) with single responsibility
110
+
111
+ #### 2. Single Responsibility
112
+
113
+ **Check:**
114
+ - File with multiple responsibilities? → Flag as issue
115
+ - Function doing multiple things? → Flag as issue
116
+ - Component/module with mixed concerns? → Flag as issue
117
+
118
+ **Principle:** Single Responsibility - One clear responsibility per file/component/function
119
+
120
+ **Example:**
121
+ - **Bad:** File handles both data fetching and UI rendering
122
+ - **Good:** Separate files for data layer and presentation layer
123
+
124
+ #### 3. Code Duplication
125
+
126
+ **Check:**
127
+ - Duplicated code blocks? → Flag as issue
128
+ - Copy-paste patterns? → Flag as issue
129
+ - Similar logic in multiple files? → Flag as issue
130
+
131
+ **Principle:** DRY (Don't Repeat Yourself)
132
+
133
+ **Example:**
134
+ - **Bad:** Same validation logic copied 3 times in different files
135
+ - **Good:** Single validation function reused across files
136
+
137
+ #### 4. Separation of Concerns
138
+
139
+ **Check:**
140
+ - Business logic in presentation layer? → Flag as issue
141
+ - Data access in business logic? → Flag as issue
142
+ - Presentation logic in business logic? → Flag as issue
143
+
144
+ **Principle:** Separation of Concerns - Keep business logic out of UI components
145
+
146
+ **Example:**
147
+ - **Bad:** Data transformation logic in UI component
148
+ - **Good:** Service function handles transformation, component calls service
149
+
150
+ #### 5. Code Clarity
151
+
152
+ **Check:**
153
+ - Magic numbers/strings? → Flag as issue
154
+ - Unclear variable names? → Flag as issue
155
+ - Deep nesting (> 3 levels)? → Flag as issue
156
+ - Long parameter lists (> 5)? → Flag as issue
157
+
158
+ **Principle:** Make it Obvious - Avoid opaque magic, write intent explicitly
159
+
160
+ **Example:**
161
+ - **Bad:** `status === "active"` (magic string)
162
+ - **Good:** `status === UserStatus.ACTIVE` (constant)
163
+
164
+ #### 6. File Organization
165
+
166
+ **Check:**
167
+ - File in wrong directory? → Flag as issue
168
+ - Deep import chains? → Flag as issue
169
+ - Missing index files for public API? → Flag as issue
170
+
171
+ **Principle:** Organize by domain first, then by technology
172
+
173
+ #### 7. Test Coverage
174
+
175
+ **Check:**
176
+ - Functions without tests? → Flag as issue
177
+ - Low test coverage (< 80%)? → Flag as issue
178
+ - Critical paths without tests? → Flag as issue
179
+
180
+ **Principle:** Test Critical Paths - High coverage for business logic
181
+
182
+ **Integration with Test Automation:**
183
+ - Use test-automation.md helper to detect test framework
184
+ - Check coverage data from test framework
185
+ - Identify untested code
186
+ - Suggest tests for uncovered code
187
+
188
+ **Example:**
189
+ - **Bad:** Service function with 0% test coverage
190
+ - **Good:** Service function with unit tests covering all paths
191
+
192
+ #### 8. Dependency Analysis
193
+
194
+ **Check:**
195
+ - Circular dependencies? → Flag as issue
196
+ - Tight coupling between modules? → Flag as issue
197
+ - Deep dependency chains (> 5 levels)? → Flag as issue
198
+ - Unused dependencies? → Flag as issue
199
+
200
+ **Principle:** Loose Coupling - Minimize dependencies between modules
201
+
202
+ **Circular Dependency Detection:**
203
+ - Track import chains
204
+ - Detect A → B → A patterns
205
+ - Flag circular dependencies as high priority
206
+
207
+ **Coupling Analysis:**
208
+ - Count dependencies between modules
209
+ - Flag modules with > 5 dependencies
210
+ - Suggest dependency reduction
211
+
212
+ **Dependency Depth:**
213
+ - Track how deep dependency tree goes
214
+ - Flag deep chains (> 5 levels)
215
+ - Suggest flattening dependency structure
216
+
217
+ **Example:**
218
+ - **Bad:** Module A imports B, B imports C, C imports A (circular)
219
+ - **Good:** Clear dependency hierarchy without cycles
220
+
221
+ #### 9. Performance Indicators
222
+
223
+ **Check:**
224
+ - Large bundle size (> 500KB)? → Flag as issue
225
+ - Unused imports? → Flag as issue
226
+ - Unused dependencies in package.json? → Flag as issue
227
+ - Memory leak patterns? → Flag as issue
228
+
229
+ **Principle:** Performance Awareness - Keep bundle size reasonable, remove unused code
230
+
231
+ **Bundle Size Warnings:**
232
+ - Check build output size
233
+ - Flag large bundles (> 500KB for web apps)
234
+ - Suggest code splitting or lazy loading
235
+
236
+ **Unused Code Detection:**
237
+ - Detect unused imports
238
+ - Detect unused dependencies
239
+ - Suggest removal
240
+
241
+ **Memory Leak Patterns:**
242
+ - Detect event listeners not cleaned up
243
+ - Detect timers not cleared
244
+ - Detect subscriptions not unsubscribed
245
+ - Framework-agnostic pattern detection
246
+
247
+ **Example:**
248
+ - **Bad:** Importing entire library when only one function needed
249
+ - **Good:** Import only what's needed, tree-shake unused code
250
+
251
+ #### 10. Security Patterns
252
+
253
+ **Check:**
254
+ - SQL injection risks? → Flag as issue
255
+ - XSS vulnerability risks? → Flag as issue
256
+ - Insecure dependencies? → Flag as issue
257
+ - Hardcoded secrets? → Flag as issue
258
+
259
+ **Principle:** Security First - Avoid common security vulnerabilities
260
+
261
+ **SQL Injection Detection:**
262
+ - Detect string concatenation in SQL queries
263
+ - Flag direct user input in queries
264
+ - Suggest parameterized queries
265
+
266
+ **XSS Vulnerability Detection:**
267
+ - Detect unsanitized user input in HTML
268
+ - Flag innerHTML with user data
269
+ - Suggest sanitization or safe rendering
270
+
271
+ **Insecure Dependencies:**
272
+ - Check for known vulnerable packages
273
+ - Flag outdated dependencies
274
+ - Suggest security updates
275
+
276
+ **Hardcoded Secrets:**
277
+ - Detect passwords, API keys, tokens in code
278
+ - Flag hardcoded credentials
279
+ - Suggest environment variables
280
+
281
+ **Example:**
282
+ - **Bad:** `query = "SELECT * FROM users WHERE id = " + userId` (SQL injection risk)
283
+ - **Good:** `query = "SELECT * FROM users WHERE id = $1"` with parameterized query
284
+
285
+ **Example:**
286
+ - **Bad:** Business logic file in `components/` directory
287
+ - **Good:** Business logic in `lib/services/` directory
288
+
289
+ #### 7. Reusability
290
+
291
+ **Check:**
292
+ - Inline implementation that could be reusable? → Flag as issue
293
+ - Hardcoded values that could be config? → Flag as issue
294
+
295
+ **Principle:** Prefer small, reusable utilities over copy-paste
296
+
297
+ **Example:**
298
+ - **Bad:** Inline button implementation repeated 5 times
299
+ - **Good:** Reusable `<Button>` component
300
+
301
+ ### Framework-Specific Pattern Checks
302
+
303
+ **If framework detected from tech stack:**
304
+ - Read tech stack from `spec/08-infrastructure.md` or `spec/02-architecture.md`
305
+ - For each framework/tool in tech stack:
306
+ - Check if rule exists (e.g., `20-nextjs.mdc`)
307
+ - If rule exists → reference framework-specific rules
308
+ - If rule doesn't exist → use general patterns + Context7 documentation lookup
309
+
310
+ **Documentation Lookup for Framework Patterns:**
311
+
312
+ **ONLY READ IF framework detected and framework-specific patterns need verification:**
313
+ - Read `.cursor/commands/_shared/documentation-lookup.md` ONLY IF framework detected AND framework-specific patterns need verification
314
+ - Read sections: "Context7 Integration" (lines 10-80), "Documentation Lookup Logic" (lines 17-150)
315
+ - Skip if: No framework detected OR no pattern verification needed → skip documentation lookup entirely
316
+ - Check condition: Verify framework detection and pattern verification need before reading helper
317
+
318
+ If framework detected and framework-specific patterns need verification:
319
+ 1. Identify framework/tool from tech stack
320
+ 2. Try multiple documentation sources (in priority order):
321
+ - **Context7 MCP:** Query Context7: "[Framework/Tool Name] [pattern topic]"
322
+ - **Cursor Documentation Indexing:** Search Cursor's indexed documentation
323
+ - **Web Search:** Search web for "[Framework] [pattern] best practices" if needed
324
+ - **General Patterns:** Use general engineering patterns from `10-engineering.mdc`
325
+ 3. Combine information from available sources when helpful
326
+ 4. Check code against documentation from available sources
327
+ 5. Reference documentation source in refactoring suggestions
328
+
329
+ **ONLY READ IF test framework detected:**
330
+ - Read `.cursor/commands/_shared/test-automation.md` ONLY IF test framework detected
331
+ - Read sections: "Test Framework Detection" (lines 14-67), "Test Coverage Tracking" (lines 155-207)
332
+ - Skip if: No test framework found → skip test coverage integration entirely
333
+ - Check condition: Run detection first, then read helper only if condition met
334
+
335
+ If test framework detected:
336
+ 1. Check test coverage for analyzed files
337
+ 2. Identify files/functions without test coverage
338
+ 3. Flag untested critical paths
339
+ 4. Suggest tests for uncovered code
340
+ 5. Include coverage metrics in refactoring report
341
+
342
+ **ONLY READ IF performance monitoring enabled:**
343
+ - Read `.cursor/commands/_shared/performance-monitoring.md` ONLY IF performance monitoring enabled
344
+ - Read sections: "Performance Metrics Collection" (lines 14-80)
345
+ - Skip if: Performance monitoring not enabled → skip performance metrics integration entirely
346
+ - Check condition: Verify performance config before reading helper
347
+
348
+ If performance monitoring enabled:
349
+ 1. Check bundle size for analyzed files
350
+ 2. Detect unused imports/dependencies
351
+ 3. Flag memory leak patterns
352
+ 4. Include performance indicators in refactoring report
353
+
354
+ **Smart Thinking:**
355
+ - Be adaptive and flexible - use whatever sources are available
356
+ - Don't be rigid - if one source fails, try others
357
+ - Search for understanding if needed - use web search to understand framework patterns better
358
+ - Combine information from multiple sources when helpful
359
+
360
+ **Error Handling:**
361
+ - If Context7 unavailable → try Cursor indexing, web search, or cached documentation
362
+ - Do NOT block workflow if documentation unavailable
363
+ - Focus on general principles + available information if documentation unavailable
364
+
365
+ **Example (if Next.js detected):**
366
+ - If rule exists → reference `20-nextjs.mdc` for Next.js patterns
367
+ - Query Context7: "Next.js Server Components best practices"
368
+ - Check if Server Components vs Client Components are used correctly
369
+ - Check if API routes follow Next.js patterns
370
+ - But analyze primarily based on general principles
371
+
372
+ ---
373
+
374
+ ## Step 3 — Issue Categorization
375
+
376
+ Categorize found issues by priority:
377
+
378
+ **High Priority:**
379
+ - File size violations (> 500 LOC)
380
+ - Single responsibility violations (multiple concerns in one file)
381
+ - Separation of concerns violations (business logic in UI)
382
+ - Code duplication (same logic in 3+ places)
383
+ - Circular dependencies
384
+ - Security vulnerabilities (SQL injection, XSS, hardcoded secrets)
385
+ - Critical paths without test coverage
386
+ - High cyclomatic complexity (> 10 decision points)
387
+
388
+ **Medium Priority:**
389
+ - Function size violations (> 100 lines)
390
+ - Code clarity issues (magic strings, unclear names)
391
+ - File organization issues (wrong directory)
392
+ - Deep nesting (> 3 levels)
393
+ - Tight coupling between modules
394
+ - Low test coverage (< 80%)
395
+ - Large bundle size (> 500KB)
396
+ - Memory leak patterns
397
+ - Insecure dependencies
398
+
399
+ **Low Priority:**
400
+ - Long parameter lists (> 5)
401
+ - Missing index files
402
+ - Minor code clarity improvements
403
+ - Reusability opportunities
404
+ - Unused imports
405
+ - Unused dependencies
406
+ - Medium cyclomatic complexity (5-10 decision points)
407
+ - Deep dependency chains (> 5 levels)
408
+
409
+ ---
410
+
411
+ ## Step 4 — Report Generation
412
+
413
+ Generate refactoring analysis report with:
414
+
415
+ **Report Structure:**
416
+
417
+ ```markdown
418
+ # Code Refactoring Analysis
419
+
420
+ ## Analysis Scope
421
+ - Files analyzed: [list of files]
422
+ - Framework detected: [framework or "none"]
423
+ - Analysis type: [task-level | file-level | feature-level]
424
+ - Active rules: [list of active rules, including framework rules if applicable]
425
+
426
+ ## Issues Found
427
+
428
+ ### High Priority
429
+
430
+ 1. **Issue Title:** `path/to/file.ts`
431
+ - **Principle violated:** [Principle name from engineering patterns]
432
+ - **Current state:** [Brief description of issue]
433
+ - **Suggestion:** [Concrete suggestion for improvement]
434
+ - **Example:**
435
+ - **Bad:** [Example of bad code pattern]
436
+ - **Good:** [Example of good code pattern]
437
+
438
+ 2. **Issue Title:** `path/to/file.ts`
439
+ - [Same structure]
440
+
441
+ ### Medium Priority
442
+
443
+ 3. **Issue Title:** `path/to/file.ts`
444
+ - [Same structure]
445
+
446
+ ### Low Priority
447
+
448
+ 4. **Issue Title:** `path/to/file.ts`
449
+ - [Same structure]
450
+
451
+ ## Framework-Specific Notes
452
+
453
+ [If framework detected, include notes about framework-specific patterns checked]
454
+ - Referenced: [framework rule name, e.g., 20-nextjs.mdc]
455
+ - Framework patterns checked: [list of patterns checked]
456
+ - Framework-specific issues: [if any]
457
+
458
+ ## Refactoring Recommendations
459
+
460
+ **Suggested refactoring order:**
461
+ 1. [High priority issue 1]
462
+ 2. [High priority issue 2]
463
+ 3. [Medium priority issue 1]
464
+ 4. [Low priority issue 1]
465
+
466
+ **Estimated impact:**
467
+ - High priority fixes: [estimated improvement]
468
+ - Medium priority fixes: [estimated improvement]
469
+ - Low priority fixes: [estimated improvement]
470
+ ```
471
+
472
+ ---
473
+
474
+ ## Step 5 — Optional: Refactoring Plan
475
+
476
+ **If user requests detailed refactoring plan:**
477
+
478
+ Create step-by-step refactoring plan:
479
+ 1. List refactoring steps in order
480
+ 2. Identify dependencies between refactorings
481
+ 3. Estimate effort for each step
482
+ 4. Suggest testing strategy after each refactoring
483
+
484
+ ---
485
+
486
+ ## Principles
487
+
488
+ - **Framework-agnostic:** Focus on general software engineering principles
489
+ - **Reference, don't define:** Reference framework rules, don't define framework patterns
490
+ - **Concrete suggestions:** Provide specific, actionable suggestions with examples
491
+ - **Prioritized:** Categorize issues by priority and impact
492
+ - **Evidence-based:** Base analysis on actual code, not assumptions
493
+
494
+ ---
495
+
496
+ ## Integration Points
497
+
498
+ **Can be used:**
499
+ - After `/task/validate` to check code quality
500
+ - Standalone on specific files or features
501
+ - As part of code review process
502
+ - Before major refactoring efforts
503
+ - After CodeRabbit comments to evaluate refactoring suggestions
504
+
505
+ **ONLY READ IF PR exists:**
506
+ - Read `.cursor/commands/_shared/coderabbit-integration.md` ONLY IF PR exists for current branch
507
+ - Auto-loads: `github-helpers.md` (dependency)
508
+ - Read sections: "CodeRabbit Detection" (lines 14-60), "Conversation Resolution" (lines 121-180) from coderabbit-integration.md
509
+ - Read sections from github-helpers.md (auto-loaded): "Read PR Conversations" (lines 51-100), "Resolve Conversations" (lines 101-150)
510
+ - Skip if: No PR exists → skip CodeRabbit integration entirely
511
+ - Check condition: Verify PR exists via GitHub MCP/CLI before reading helpers
512
+
513
+ 1. **Check if PR exists:**
514
+ - Get current branch name
515
+ - Check if PR exists for current branch (via GitHub MCP or CLI)
516
+ - If no PR exists → skip CodeRabbit integration
517
+ - If PR exists → proceed with CodeRabbit handling
518
+
519
+ 2. **Read CodeRabbit refactoring comments:**
520
+ - Use GitHub helpers to read PR comments (MCP → CLI → Local fallback)
521
+ - Detect CodeRabbit comments (pattern search + author check)
522
+ - Filter to refactoring-related comments (optimization, code structure, etc.)
523
+
524
+ 3. **For each refactoring-related CodeRabbit comment:**
525
+ - **Evaluate against refactoring rules:**
526
+ - Compare CodeRabbit suggestion with refactoring analysis
527
+ - Check if suggestion aligns with refactoring principles
528
+ - Check if suggestion conflicts with project patterns
529
+
530
+ - **If refactoring needed:**
531
+ - Apply refactoring (if user approves)
532
+ - Resolve conversation via GitHub helpers
533
+ - Update tracking file
534
+
535
+ - **If refactoring not needed:**
536
+ - Provide argumentation explaining why
537
+ - Reference refactoring rules and project patterns
538
+ - Log argumentation to Linear/GitHub/local
539
+
540
+ 4. **Report CodeRabbit refactoring handling:**
541
+ - Summarize: "X refactoring suggestions evaluated, Y applied, Z deferred"
542
+ - List any deferred suggestions with reasoning
543
+
544
+ **Error Handling:**
545
+ - If GitHub MCP/CLI unavailable → skip CodeRabbit integration, continue refactoring analysis
546
+ - If PR not found → skip CodeRabbit integration, continue refactoring analysis
547
+ - If comment parsing fails → log error, continue with next comment
548
+ - Never block refactoring analysis due to CodeRabbit integration failures
549
+
550
+ **Does not:**
551
+ - Make code changes automatically
552
+ - Implement refactoring (only suggests)
553
+ - Skip analysis steps
554
+ - Define framework-specific patterns (references them)
555
+
@@ -0,0 +1,149 @@
1
+ ---
2
+ title: Engineering Foundations
3
+ description: Framework-agnostic engineering patterns - applies to all engineering tasks
4
+ owner: sdd-system
5
+ severity: warn
6
+ globs: "**/*"
7
+ alwaysApply: false
8
+ activation:
9
+ projectTypes: [web-app, cli-tool, library, api-service, mobile-app, monorepo]
10
+ projectSizes: [small, medium, large, enterprise]
11
+ projectPhases: [initialization, expansion, maintenance, migration, legacy]
12
+ technologies: []
13
+ requires: []
14
+ ---
15
+
16
+ # Engineering Foundations
17
+
18
+ ## Purpose
19
+
20
+ Framework-agnostic engineering patterns for code structure, components, models, and services. These patterns apply to all engineering tasks regardless of framework or technology.
21
+
22
+ ## Principles
23
+
24
+ - **Single Responsibility**: One clear responsibility per file/component/function
25
+ - **Composition over Inheritance**: Prefer composition, dependency injection, and interfaces
26
+ - **Small Files**: Aim for < 500 LOC per file; refactor around 400 LOC
27
+ - **Determinism**: Favor pure functions; minimize global state and side effects
28
+ - **Make it Obvious**: Avoid opaque magic; write intent explicitly
29
+
30
+ ## File Structure Patterns
31
+
32
+ **Organize by domain first, then by technology:**
33
+ - `lib/domain/*` - Domain models and business logic
34
+ - `lib/services/*` - Business logic services
35
+ - `lib/repositories/*` - Data access layer
36
+ - `components/*` - UI components (if applicable)
37
+ - `utils/*` - Shared utilities
38
+ - `types/*` - Type definitions
39
+
40
+ **Keep business logic out of UI components:**
41
+ - UI components should be presentational
42
+ - Business logic belongs in services
43
+ - Data fetching belongs in repositories or services
44
+
45
+ **Export small public APIs:**
46
+ - Use index files for public exports
47
+ - Avoid deep relative-import chains
48
+ - Prefer small, reusable utilities over copy-paste
49
+
50
+ ## Component Patterns
51
+
52
+ **Single Responsibility:**
53
+ - Each component has one clear purpose
54
+ - Components are small and focused
55
+ - Components are composable
56
+
57
+ **Composition over Inheritance:**
58
+ - Build complex components from simple ones
59
+ - Use dependency injection for dependencies
60
+ - Prefer interfaces over concrete implementations
61
+
62
+ **Clear Props Interface:**
63
+ - Explicit prop types/interfaces
64
+ - Document prop purposes
65
+ - Use default props where appropriate
66
+
67
+ ## Model Patterns
68
+
69
+ **Domain Models:**
70
+ - Represent business entities
71
+ - Contain business logic (if applicable)
72
+ - Are framework-agnostic
73
+
74
+ **DTOs (Data Transfer Objects):**
75
+ - Used for data transfer between layers
76
+ - No business logic
77
+ - Clear serialization/deserialization
78
+
79
+ **Types:**
80
+ - Type safety at boundaries
81
+ - Shared type definitions
82
+ - Avoid type duplication
83
+
84
+ ## Service Patterns
85
+
86
+ **Business Logic in Services:**
87
+ - Services contain business logic
88
+ - Services call repositories/data layer
89
+ - Services handle orchestration
90
+
91
+ **Error Handling at Boundaries:**
92
+ - Services handle errors
93
+ - Return consistent result shapes
94
+ - Never swallow errors silently
95
+
96
+ **Consistent Result Shapes:**
97
+ - Success: `{ ok: true, data: ... }`
98
+ - Error: `{ ok: false, error: { code, message, details? } }`
99
+ - Or project-specific consistent shape
100
+
101
+ ## Framework-Specific Extensions
102
+
103
+ This rule provides foundation patterns. For framework-specific patterns, see:
104
+ - Next.js: `20-nextjs.mdc` (if Next.js detected)
105
+ - React: `[react]-*.mdc` (if React detected)
106
+ - Other frameworks: `[framework]-*.mdc` (if detected)
107
+
108
+ ## Naming Conventions
109
+
110
+ - **Files/folders**: kebab-case (e.g., `user-service.ts`)
111
+ - **Components**: PascalCase (e.g., `<UserCard/>`)
112
+ - **Hooks**: `useXxx` (e.g., `useUserData`)
113
+ - **Variables/functions**: camelCase (e.g., `getUserData`)
114
+ - **Domain models**: PascalCase (e.g., `User`, `Order`)
115
+ - **DTOs**: PascalCase with `DTO` suffix (e.g., `UserDTO`)
116
+ - **Tests**: Same name as target + `.test.ts(x)`
117
+
118
+ ## Error Handling
119
+
120
+ - Fail fast near the source
121
+ - Throw meaningful errors with context (no PII)
122
+ - Differentiate user vs system errors
123
+ - Never swallow errors silently
124
+ - UI: Show clear, neutral error messages
125
+
126
+ ## Testing Basics
127
+
128
+ - Cover critical paths: happy path, failure paths, edge cases
129
+ - AAA pattern (Arrange-Act-Assert)
130
+ - Intention-revealing test names
131
+ - Avoid large snapshots
132
+ - Test behavior, not implementation details
133
+ - Co-locate tests or mirror structure under `__tests__/`
134
+
135
+ ## Performance
136
+
137
+ - Avoid needless allocations in hot paths
138
+ - Lazy-load heavy modules
139
+ - Use memoization only where profiling shows impact
140
+ - Remove micro-optimizations with no effect
141
+
142
+ ## Security Basics
143
+
144
+ - Never commit secrets
145
+ - Validate all external input
146
+ - Sanitize strings
147
+ - Assume hostile boundaries
148
+ - No PII in logs
149
+ - Keep dependencies up to date