specweave 1.0.55 → 1.0.57

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 (42) hide show
  1. package/CLAUDE.md +703 -32
  2. package/dist/src/cli/commands/analytics.d.ts +19 -0
  3. package/dist/src/cli/commands/analytics.d.ts.map +1 -0
  4. package/dist/src/cli/commands/analytics.js +172 -0
  5. package/dist/src/cli/commands/analytics.js.map +1 -0
  6. package/dist/src/cli/helpers/init/api-docs-config.d.ts +67 -0
  7. package/dist/src/cli/helpers/init/api-docs-config.d.ts.map +1 -0
  8. package/dist/src/cli/helpers/init/api-docs-config.js +426 -0
  9. package/dist/src/cli/helpers/init/api-docs-config.js.map +1 -0
  10. package/dist/src/core/analytics/analytics-aggregator.d.ts +59 -0
  11. package/dist/src/core/analytics/analytics-aggregator.d.ts.map +1 -0
  12. package/dist/src/core/analytics/analytics-aggregator.js +276 -0
  13. package/dist/src/core/analytics/analytics-aggregator.js.map +1 -0
  14. package/dist/src/core/analytics/analytics-collector.d.ts +120 -0
  15. package/dist/src/core/analytics/analytics-collector.d.ts.map +1 -0
  16. package/dist/src/core/analytics/analytics-collector.js +286 -0
  17. package/dist/src/core/analytics/analytics-collector.js.map +1 -0
  18. package/dist/src/core/analytics/index.d.ts +9 -0
  19. package/dist/src/core/analytics/index.d.ts.map +1 -0
  20. package/dist/src/core/analytics/index.js +9 -0
  21. package/dist/src/core/analytics/index.js.map +1 -0
  22. package/dist/src/core/analytics/types.d.ts +131 -0
  23. package/dist/src/core/analytics/types.d.ts.map +1 -0
  24. package/dist/src/core/analytics/types.js +15 -0
  25. package/dist/src/core/analytics/types.js.map +1 -0
  26. package/dist/src/core/types/config.d.ts +51 -0
  27. package/dist/src/core/types/config.d.ts.map +1 -1
  28. package/dist/src/core/types/config.js +15 -0
  29. package/dist/src/core/types/config.js.map +1 -1
  30. package/package.json +1 -1
  31. package/plugins/specweave/agents/infrastructure/AGENT.md +49 -0
  32. package/plugins/specweave/commands/analytics.md +157 -0
  33. package/plugins/specweave/commands/auto.md +305 -0
  34. package/plugins/specweave/commands/do.md +59 -0
  35. package/plugins/specweave/hooks/stop-auto.sh +45 -0
  36. package/plugins/specweave/hooks/user-prompt-submit.sh +25 -0
  37. package/plugins/specweave/hooks/v2/handlers/github-sync-handler.sh +24 -3
  38. package/plugins/specweave/scripts/read-analytics.sh +284 -0
  39. package/plugins/specweave/scripts/track-analytics.sh +92 -0
  40. package/plugins/specweave/skills/auto-execute/SKILL.md +374 -0
  41. package/plugins/specweave-infrastructure/agents/devops/AGENT.md +109 -0
  42. package/src/templates/CLAUDE.md.template +467 -0
package/CLAUDE.md CHANGED
@@ -1,10 +1,10 @@
1
- <!-- SW:META template="claude" version="1.0.48" sections="header,start,autodetect,metarule,rules,workflow,context,lsp,structure,taskformat,secrets,syncing,mapping,testing,limits,troubleshooting,principles,linking,docs" -->
1
+ <!-- SW:META template="claude" version="1.0.58" sections="header,start,autodetect,metarule,rules,workflow,context,lsp,structure,taskformat,secrets,syncing,mapping,testing,api,limits,troubleshooting,principles,linking,mcp,autoexecute,auto,docs" -->
2
2
 
3
- <!-- SW:SECTION:header version="1.0.46" -->
3
+ <!-- SW:SECTION:header version="1.0.56" -->
4
4
  **Framework**: SpecWeave | **Truth**: `spec.md` + `tasks.md`
5
5
  <!-- SW:END:header -->
6
6
 
7
- <!-- SW:SECTION:start version="1.0.46" -->
7
+ <!-- SW:SECTION:start version="1.0.56" -->
8
8
  ## Getting Started
9
9
 
10
10
  **Initial increment**: `0001-project-setup` (auto-created by `specweave init`)
@@ -14,7 +14,7 @@
14
14
  2. **Customize**: Edit spec.md and use for setup tasks
15
15
  <!-- SW:END:start -->
16
16
 
17
- <!-- SW:SECTION:autodetect version="1.0.46" -->
17
+ <!-- SW:SECTION:autodetect version="1.0.56" -->
18
18
  ## Auto-Detection
19
19
 
20
20
  SpecWeave auto-detects product descriptions and routes to `/sw:increment`:
@@ -24,7 +24,7 @@ SpecWeave auto-detects product descriptions and routes to `/sw:increment`:
24
24
  **Opt-out phrases**: "Just brainstorm first" | "Don't plan yet" | "Quick discussion" | "Let's explore ideas"
25
25
  <!-- SW:END:autodetect -->
26
26
 
27
- <!-- SW:SECTION:metarule version="1.0.46" -->
27
+ <!-- SW:SECTION:metarule version="1.0.56" -->
28
28
  ## Meta-Rule: Think-Before-Act
29
29
 
30
30
  **Satisfy dependencies BEFORE dependent operations.**
@@ -35,7 +35,7 @@ SpecWeave auto-detects product descriptions and routes to `/sw:increment`:
35
35
  ```
36
36
  <!-- SW:END:metarule -->
37
37
 
38
- <!-- SW:SECTION:rules version="1.0.46" -->
38
+ <!-- SW:SECTION:rules version="1.0.56" -->
39
39
  ## Rules
40
40
 
41
41
  1. **Files** → `.specweave/increments/####-name/` (spec.md, plan.md, tasks.md at root; reports/, scripts/, logs/ subfolders)
@@ -45,7 +45,7 @@ SpecWeave auto-detects product descriptions and routes to `/sw:increment`:
45
45
  5. **Root clean**: NEVER create .md/reports/scripts in project root → use increment folders
46
46
  <!-- SW:END:rules -->
47
47
 
48
- <!-- SW:SECTION:workflow version="1.0.46" -->
48
+ <!-- SW:SECTION:workflow version="1.0.56" -->
49
49
  ## Workflow
50
50
 
51
51
  `/sw:increment "X"` → `/sw:do` → `/sw:progress` → `/sw:done 0001`
@@ -62,40 +62,92 @@ SpecWeave auto-detects product descriptions and routes to `/sw:increment`:
62
62
  **Natural language**: "Let's build X" → `/sw:increment` | "What's status?" → `/sw:progress` | "We're done" → `/sw:done`
63
63
  <!-- SW:END:workflow -->
64
64
 
65
- <!-- SW:SECTION:context version="1.0.47" -->
65
+ <!-- SW:SECTION:context version="1.0.56" -->
66
66
  ## Living Docs Context
67
67
 
68
- **Before implementing**: Check existing docs for patterns and decisions.
68
+ **Before implementing features**: Check existing docs for patterns and decisions.
69
69
 
70
70
  ```bash
71
- grep -ril "keyword" .specweave/docs/internal/ # Search for related docs
71
+ # Search for related docs
72
+ grep -ril "keyword" .specweave/docs/internal/
73
+
74
+ # Key locations
75
+ .specweave/docs/internal/specs/ # Feature specifications
76
+ .specweave/docs/internal/architecture/adr/ # Architecture decisions (ADRs)
77
+ .specweave/docs/internal/architecture/ # System design
72
78
  ```
73
79
 
74
- **Key locations**: `specs/` (features) | `architecture/adr/` (decisions) | `architecture/` (design)
80
+ **Always check ADRs** before making design decisions to avoid contradicting past choices.
75
81
 
76
- **Always check ADRs** before design decisions. Use `/sw:context <topic>` to load context.
82
+ **Use `/sw:context <topic>`** to load relevant living docs into conversation.
77
83
  <!-- SW:END:context -->
78
84
 
79
- <!-- SW:SECTION:lsp version="1.0.1" -->
85
+ <!-- SW:SECTION:lsp version="1.0.56" -->
80
86
  ## LSP-Enhanced Exploration (DEFAULT - Claude Code 2.0.74+)
81
87
 
82
- **LSP is ENABLED BY DEFAULT** - 100x faster than grep for symbol resolution.
88
+ **LSP is ENABLED BY DEFAULT** for all SpecWeave operations - 100x faster than grep for symbol resolution.
83
89
 
84
90
  **LSP Operations** (used automatically):
85
- | Operation | Purpose | Use Case |
86
- |-----------|---------|----------|
87
- | `goToDefinition` | Jump to symbol definition | Find function/class definitions |
91
+ | Operation | Purpose | Example Use |
92
+ |-----------|---------|-------------|
93
+ | `goToDefinition` | Jump to symbol definition | Find where a function/class is defined |
88
94
  | `findReferences` | All usages across codebase | Refactoring impact analysis |
89
95
  | `documentSymbol` | File structure/hierarchy | Understand module organization |
90
- | `hover` | Type info & documentation | Check inferred types |
91
- | `getDiagnostics` | Errors, warnings, hints | Real-time code quality |
96
+ | `hover` | Type info & documentation | Check inferred types, JSDoc |
97
+ | `getDiagnostics` | Errors, warnings, hints | Real-time code quality check |
98
+
99
+ **Living Docs & Init use LSP automatically**:
100
+ ```bash
101
+ # Full scan (LSP enabled by default)
102
+ /sw:living-docs --full-scan
103
+
104
+ # Init also uses LSP for accurate codebase analysis
105
+ specweave init
92
106
 
93
- **Living Docs & Init use LSP automatically**. Disable with `--no-lsp` (not recommended).
107
+ # LSP provides automatically:
108
+ # - Accurate API surface extraction (all exports, types, signatures)
109
+ # - Cross-module dependency graphs (semantic, not just imports)
110
+ # - Dead code detection (unreferenced symbols)
111
+ # - Type hierarchy and inheritance maps
94
112
 
95
- **Install Language Servers**: `npm i -g typescript-language-server` | `pip install python-lsp-server`
113
+ # Disable only if language servers unavailable (not recommended):
114
+ /sw:living-docs --full-scan --no-lsp
115
+ ```
116
+
117
+ **Install Language Servers** (required for LSP):
118
+ ```bash
119
+ # TypeScript/JavaScript (most common)
120
+ npm install -g typescript-language-server typescript
121
+
122
+ # Python
123
+ pip install python-lsp-server
124
+
125
+ # Go
126
+ go install golang.org/x/tools/gopls@latest
127
+
128
+ # Rust
129
+ rustup component add rust-analyzer
130
+ ```
131
+
132
+ **Configuration** (optional, `.lsp.json` in project root):
133
+ ```json
134
+ {
135
+ "vtsls": {
136
+ "command": "typescript-language-server",
137
+ "args": ["--stdio"],
138
+ "extensionToLanguage": { ".ts": "typescript", ".tsx": "typescriptreact", ".js": "javascript" }
139
+ }
140
+ }
141
+ ```
142
+
143
+ **Best Practices**:
144
+ - Install language servers before running `specweave init` or `/sw:living-docs`
145
+ - LSP runs automatically - no flags needed
146
+ - Use `findReferences` before refactoring to understand impact
147
+ - Combine with Explore agent for comprehensive codebase understanding
96
148
  <!-- SW:END:lsp -->
97
149
 
98
- <!-- SW:SECTION:structure version="1.0.46" -->
150
+ <!-- SW:SECTION:structure version="1.0.56" -->
99
151
  ## Structure
100
152
 
101
153
  ```
@@ -117,7 +169,7 @@ grep -ril "keyword" .specweave/docs/internal/ # Search for related docs
117
169
  **Path syntax**: `//path` = absolute | `/path` = relative to settings file | `**` = recursive | `additionalDirectories` = explicit working dirs
118
170
  <!-- SW:END:structure -->
119
171
 
120
- <!-- SW:SECTION:taskformat version="1.0.46" -->
172
+ <!-- SW:SECTION:taskformat version="1.0.56" -->
121
173
  ## Task Format
122
174
 
123
175
  ```markdown
@@ -127,7 +179,7 @@ grep -ril "keyword" .specweave/docs/internal/ # Search for related docs
127
179
  ```
128
180
  <!-- SW:END:taskformat -->
129
181
 
130
- <!-- SW:SECTION:secrets version="1.0.46" -->
182
+ <!-- SW:SECTION:secrets version="1.0.56" -->
131
183
  ## Secrets Check
132
184
 
133
185
  **BEFORE CLI tools**: Check existing config first!
@@ -138,7 +190,7 @@ gh auth status
138
190
  ```
139
191
  <!-- SW:END:secrets -->
140
192
 
141
- <!-- SW:SECTION:syncing version="1.0.46" -->
193
+ <!-- SW:SECTION:syncing version="1.0.56" -->
142
194
  ## External Sync (GitHub/JIRA/ADO)
143
195
 
144
196
  **After increment creation**: Run `/sw-github:sync {id}` to create issues!
@@ -166,7 +218,7 @@ Living docs sync ≠ External sync. They are separate:
166
218
  **Verify tokens**: `grep GITHUB_TOKEN .env` | `gh auth status`
167
219
  <!-- SW:END:syncing -->
168
220
 
169
- <!-- SW:SECTION:mapping version="1.0.46" -->
221
+ <!-- SW:SECTION:mapping version="1.0.56" -->
170
222
  ## GitHub Mapping
171
223
 
172
224
  | SpecWeave | GitHub |
@@ -176,7 +228,7 @@ Living docs sync ≠ External sync. They are separate:
176
228
  | Task T-XXX | Checkbox |
177
229
  <!-- SW:END:mapping -->
178
230
 
179
- <!-- SW:SECTION:testing version="1.0.46" -->
231
+ <!-- SW:SECTION:testing version="1.0.56" -->
180
232
  ## Testing
181
233
 
182
234
  BDD in tasks.md | Unit >80% | `.test.ts` (Vitest)
@@ -188,13 +240,86 @@ vi.mock('fs', () => ({ readFile: vi.fn() }));
188
240
  ```
189
241
  <!-- SW:END:testing -->
190
242
 
191
- <!-- SW:SECTION:limits version="1.0.46" -->
243
+ <!-- SW:SECTION:api version="1.0.58" -->
244
+ ## API Development (OpenAPI-First)
245
+
246
+ **For API projects only.** Skip this section if your project has no REST/GraphQL endpoints.
247
+
248
+ **Use OpenAPI as the source of truth for API documentation.** Postman collections are derived from OpenAPI.
249
+
250
+ ### Configuration (`.specweave/config.json`)
251
+
252
+ ```json
253
+ {
254
+ "apiDocs": {
255
+ "enabled": true,
256
+ "openApiPath": "openapi.yaml",
257
+ "generatePostman": true,
258
+ "postmanPath": "postman-collection.json",
259
+ "generateOn": "on-increment-done",
260
+ "baseUrl": "http://localhost:3000"
261
+ }
262
+ }
263
+ ```
264
+
265
+ ### OpenAPI Generation by Framework
266
+
267
+ | Framework | Auto-Generation | Setup |
268
+ |-----------|-----------------|-------|
269
+ | **NestJS** | `@nestjs/swagger` | Decorators auto-generate OpenAPI |
270
+ | **FastAPI** | Built-in | Auto-generates at `/openapi.json` |
271
+ | **Express** | `swagger-jsdoc` | JSDoc comments -> OpenAPI |
272
+ | **Spring Boot** | `springdoc-openapi` | Annotations auto-generate |
273
+ | **Go/Gin** | `swag` | Comments -> OpenAPI |
274
+
275
+ ### Workflow
276
+
277
+ ```
278
+ Code (decorators/annotations)
279
+ |
280
+ v (auto-generated or manual)
281
+ openapi.yaml (SOURCE OF TRUTH - version controlled)
282
+ |
283
+ v (derived on /sw:done or /sw:api-docs)
284
+ postman-collection.json (for manual testing)
285
+ ```
286
+
287
+ ### Commands
288
+
289
+ ```bash
290
+ # Generate/update API docs (OpenAPI + Postman)
291
+ /sw:api-docs
292
+
293
+ # Generate on increment close (automatic if enabled)
294
+ /sw:done 0001 # -> triggers API doc generation
295
+ ```
296
+
297
+ ### Manual Generation (if config disabled)
298
+
299
+ ```bash
300
+ # Generate Postman from existing OpenAPI
301
+ npx @postman/openapi-to-postmanv2 -s openapi.yaml -o postman-collection.json
302
+
303
+ # Generate OpenAPI for Express
304
+ npx swagger-jsdoc -d swagger-config.js -o openapi.yaml
305
+ ```
306
+
307
+ ### When Docs Update
308
+
309
+ | `generateOn` Setting | When API Docs Regenerate |
310
+ |---------------------|--------------------------|
311
+ | `on-increment-done` | When closing increment (recommended) |
312
+ | `on-api-change` | When API files change (hook-based) |
313
+ | `manual` | Only via `/sw:api-docs` command |
314
+ <!-- SW:END:api -->
315
+
316
+ <!-- SW:SECTION:limits version="1.0.56" -->
192
317
  ## Limits
193
318
 
194
319
  **Max 1500 lines/file** — extract before adding
195
320
  <!-- SW:END:limits -->
196
321
 
197
- <!-- SW:SECTION:troubleshooting version="1.0.46" -->
322
+ <!-- SW:SECTION:troubleshooting version="1.0.56" -->
198
323
  ## Troubleshooting
199
324
 
200
325
  | Issue | Fix |
@@ -213,7 +338,7 @@ vi.mock('fs', () => ({ readFile: vi.fn() }));
213
338
  | Path patterns not working | `//path` = absolute, `/path` = relative to settings file, `additionalDirectories` for explicit working dirs |
214
339
  <!-- SW:END:troubleshooting -->
215
340
 
216
- <!-- SW:SECTION:principles version="1.0.46" -->
341
+ <!-- SW:SECTION:principles version="1.0.56" -->
217
342
  ## Principles
218
343
 
219
344
  1. **Spec-first**: `/sw:increment` before coding
@@ -223,7 +348,7 @@ vi.mock('fs', () => ({ readFile: vi.fn() }));
223
348
  5. **Clean**: All files in increment folders
224
349
  <!-- SW:END:principles -->
225
350
 
226
- <!-- SW:SECTION:linking version="1.0.46" -->
351
+ <!-- SW:SECTION:linking version="1.0.56" -->
227
352
  ## Bidirectional Linking
228
353
 
229
354
  Tasks ↔ User Stories auto-linked via AC-IDs: `AC-US1-01` → `US-001`
@@ -231,7 +356,553 @@ Tasks ↔ User Stories auto-linked via AC-IDs: `AC-US1-01` → `US-001`
231
356
  Task format: `**AC**: AC-US1-01, AC-US1-02` (CRITICAL for linking)
232
357
  <!-- SW:END:linking -->
233
358
 
234
- <!-- SW:SECTION:docs version="1.0.46" -->
359
+ <!-- SW:SECTION:mcp version="1.0.56" -->
360
+ ## MCP Servers (External Service Integration)
361
+
362
+ **MCP servers extend Claude Code's capabilities for external services.** Install them for autonomous operations.
363
+
364
+ ### Recommended MCP Servers
365
+
366
+ | Service | Install | Purpose |
367
+ |---------|---------|---------|
368
+ | **Supabase** | `npx @anthropic/claude-code-mcp-setup add supabase` | Database, Auth, Edge Functions |
369
+ | **GitHub** | Built-in via `gh` CLI | Issues, PRs, Repos |
370
+ | **Postgres** | `npx @anthropic/claude-code-mcp-setup add postgres` | Direct DB access |
371
+
372
+ ### Supabase MCP Setup
373
+
374
+ ```bash
375
+ # Add to Claude Code MCP config
376
+ npx @anthropic/claude-code-mcp-setup add supabase
377
+
378
+ # Or manual config in .claude/settings.local.json:
379
+ {
380
+ "mcpServers": {
381
+ "supabase": {
382
+ "type": "http",
383
+ "url": "https://mcp.supabase.com/mcp"
384
+ }
385
+ }
386
+ }
387
+ ```
388
+
389
+ ### Auto-Install Rule
390
+
391
+ **If credentials exist for a service, Claude SHOULD auto-install the MCP server:**
392
+
393
+ ```bash
394
+ # If SUPABASE_URL exists → suggest/install Supabase MCP
395
+ if grep -q "SUPABASE_URL" .env 2>/dev/null; then
396
+ npx @anthropic/claude-code-mcp-setup add supabase
397
+ fi
398
+ ```
399
+ <!-- SW:END:mcp -->
400
+
401
+ <!-- SW:SECTION:autoexecute version="1.0.56" -->
402
+ ## Auto-Execute Rule (CRITICAL)
403
+
404
+ **NEVER output "Manual Step Required" or "Next Steps" when credentials are available.**
405
+
406
+ ### The Golden Rule
407
+
408
+ ```
409
+ ❌ FORBIDDEN: "Manual Step Required: Open Supabase SQL Editor..."
410
+ ❌ FORBIDDEN: "Next Steps: 1. Run wrangler deploy..."
411
+ ❌ FORBIDDEN: "Execute this SQL in your database console..."
412
+
413
+ ✅ REQUIRED: Execute commands directly using available credentials
414
+ ```
415
+
416
+ ### Decision Tree
417
+
418
+ ```
419
+ Credentials in .env? ──YES──→ EXECUTE IMMEDIATELY
420
+
421
+ NO
422
+
423
+
424
+ ASK for credentials (don't show manual steps!)
425
+
426
+
427
+ Save to .env → EXECUTE IMMEDIATELY → Continue
428
+ ```
429
+
430
+ ### Credential Lookup (MANDATORY before any external operation)
431
+
432
+ ```bash
433
+ # 1. Check .env
434
+ grep -E "(SUPABASE_|DATABASE_URL|CF_API_|GITHUB_TOKEN)" .env 2>/dev/null
435
+
436
+ # 2. Check CLI auth
437
+ supabase status 2>/dev/null # Supabase
438
+ wrangler whoami 2>/dev/null # Cloudflare
439
+ gh auth status 2>/dev/null # GitHub
440
+ aws sts get-caller-identity 2>/dev/null # AWS
441
+
442
+ # 3. Use MCP server if available
443
+ # MCP servers provide direct API access
444
+ ```
445
+
446
+ ### Auto-Execute Examples
447
+
448
+ ```bash
449
+ # Supabase SQL execution
450
+ if [ -n "$DATABASE_URL" ]; then
451
+ psql "$DATABASE_URL" -f schema.sql # ✅ Execute directly
452
+ fi
453
+
454
+ # Wrangler secrets
455
+ if wrangler whoami 2>/dev/null; then
456
+ echo "$SECRET" | wrangler secret put MY_SECRET # ✅ Execute directly
457
+ wrangler deploy # ✅ Deploy directly
458
+ fi
459
+ ```
460
+
461
+ ### If Credentials Missing → ASK, Don't Show Manual Steps
462
+
463
+ ```markdown
464
+ 🔐 **Credential Required**
465
+
466
+ I need your DATABASE_URL to execute the migration.
467
+
468
+ **Paste your connection string:**
469
+ [I will save to .env and execute automatically]
470
+ ```
471
+ <!-- SW:END:autoexecute -->
472
+
473
+ <!-- SW:SECTION:auto version="1.0.57" -->
474
+ ## Auto Mode (Autonomous Execution)
475
+
476
+ **Auto mode enables continuous autonomous execution** until all tasks are complete.
477
+
478
+ ### 🚨 CRITICAL: Zero Manual Steps in Auto Mode
479
+
480
+ **Auto mode MUST be fully autonomous. NEVER ask user to:**
481
+ - Open a web dashboard (Supabase, AWS Console, etc.)
482
+ - Copy/paste SQL into an editor
483
+ - Run commands manually
484
+ - Click buttons in UIs
485
+
486
+ **If you need external access:**
487
+ 1. Check for credentials in `.env`
488
+ 2. Use CLI tools (`supabase`, `wrangler`, `gh`, `aws`)
489
+ 3. Use MCP servers for direct API access
490
+ 4. If credentials missing → ASK for them, save to `.env`, then EXECUTE
491
+
492
+ ### 🧪 Test Execution Loop (MANDATORY)
493
+
494
+ **After EVERY implementation task, run tests in a self-healing loop:**
495
+
496
+ ```bash
497
+ # 1. Run unit/integration tests
498
+ npm test # or: npx vitest run
499
+
500
+ # 2. If UI exists, run E2E tests
501
+ npx playwright test
502
+
503
+ # 3. If tests fail → FIX → RE-RUN (max 3 attempts)
504
+ ```
505
+
506
+ **Test Loop Pattern (Ralph Loop):**
507
+ ```
508
+ ┌─────────────────────────────────────────────────────────────┐
509
+ │ IMPLEMENT → TEST → FAIL? → FIX → TEST → PASS → NEXT TASK │
510
+ │ ↑________________↓ │
511
+ │ (max 3 iterations) │
512
+ └─────────────────────────────────────────────────────────────┘
513
+ ```
514
+
515
+ **E2E Test Execution (when UI exists):**
516
+ ```bash
517
+ # Install Playwright browsers if needed
518
+ npx playwright install --with-deps chromium
519
+
520
+ # Run E2E tests with proper reporting
521
+ npx playwright test --reporter=list
522
+
523
+ # On failure, capture screenshot/trace
524
+ npx playwright test --trace on
525
+ ```
526
+
527
+ **Focus on MVP Critical Paths:**
528
+ 1. **Authentication flows** (login, logout, register)
529
+ 2. **Core business transactions** (create, update, delete)
530
+ 3. **Payment/checkout flows** (if applicable)
531
+ 4. **Data integrity scenarios**
532
+
533
+ ### ⚠️ Pragmatic Completion (NOT 100% Blindly!)
534
+
535
+ **Don't blindly follow 100% completion rules!** Reality:
536
+ - Specs have bugs, ambiguities, conflicts
537
+ - Requirements change mid-implementation
538
+ - Some planned tasks become irrelevant
539
+ - Edge cases may not be worth the effort
540
+
541
+ **Smart Completion Criteria:**
542
+ ```
543
+ ┌─────────────────────────────────────────────────────────────┐
544
+ │ MUST COMPLETE (block release): │
545
+ │ • MVP critical paths (auth, core CRUD, payments) │
546
+ │ • Security-sensitive flows │
547
+ │ • Data integrity operations │
548
+ │ • User-facing error handling │
549
+ ├─────────────────────────────────────────────────────────────┤
550
+ │ SHOULD COMPLETE (aim for, but pragmatic): │
551
+ │ • Edge case handling │
552
+ │ • Performance optimizations │
553
+ │ • Nice-to-have features │
554
+ ├─────────────────────────────────────────────────────────────┤
555
+ │ CAN SKIP/DEFER (if blocking progress): │
556
+ │ • Conflicting requirements (flag and ask user) │
557
+ │ • Over-engineered edge cases │
558
+ │ • Tasks made obsolete by other changes │
559
+ └─────────────────────────────────────────────────────────────┘
560
+ ```
561
+
562
+ **When to STOP and ask user:**
563
+ - Spec conflicts with another spec
564
+ - Task seems unnecessary given implementation
565
+ - Edge case would require major refactoring
566
+ - Requirement is ambiguous
567
+
568
+ ### 🧑‍🤝‍🧑 Smart Test User Strategy
569
+
570
+ **Create test users strategically, not blindly:**
571
+
572
+ ```typescript
573
+ // Good: Create users with specific roles/states
574
+ const testUsers = {
575
+ admin: { email: 'admin@test.com', role: 'admin' },
576
+ regularUser: { email: 'user@test.com', role: 'user' },
577
+ premiumUser: { email: 'premium@test.com', plan: 'premium' },
578
+ blockedUser: { email: 'blocked@test.com', status: 'blocked' },
579
+ };
580
+
581
+ // When to create multiple test users:
582
+ // ✅ Testing role-based access control
583
+ // ✅ Testing subscription tiers
584
+ // ✅ Testing user states (active, blocked, pending)
585
+ // ✅ Testing multi-user interactions (sharing, permissions)
586
+
587
+ // When ONE test user is enough:
588
+ // ✅ Basic CRUD operations
589
+ // ✅ Form validation
590
+ // ✅ UI component tests
591
+ // ✅ API endpoint tests (mocked auth)
592
+ ```
593
+
594
+ **E2E Test User Setup:**
595
+ ```typescript
596
+ // playwright/fixtures/users.ts
597
+ export const testUsers = {
598
+ // Seeded in database before tests
599
+ admin: { id: 'test-admin-001', email: 'admin@test.local' },
600
+ user: { id: 'test-user-001', email: 'user@test.local' },
601
+ };
602
+
603
+ // Use fixtures, don't create users per test!
604
+ test.use({ storageState: 'playwright/.auth/user.json' });
605
+ ```
606
+
607
+ ### 🔐 E2E Authentication (CRITICAL - Avoid Flaky Tests!)
608
+
609
+ **Auth is the #1 cause of flaky E2E tests. Be ULTRASMART about it:**
610
+
611
+ ```
612
+ ┌─────────────────────────────────────────────────────────────┐
613
+ │ E2E AUTH STRATEGY (in order of preference): │
614
+ ├─────────────────────────────────────────────────────────────┤
615
+ │ 1. BEST: Reuse auth state (storageState) │
616
+ │ - Login ONCE in global setup │
617
+ │ - Reuse session across all tests │
618
+ │ - 10x faster, zero flakiness │
619
+ │ │
620
+ │ 2. GOOD: API-based auth (bypass UI) │
621
+ │ - Call auth API directly │
622
+ │ - Set cookies/tokens programmatically │
623
+ │ - Faster than UI login │
624
+ │ │
625
+ │ 3. AVOID: UI login per test │
626
+ │ - Slow (3-5s per test) │
627
+ │ - Flaky (timing, captcha, rate limits) │
628
+ │ - Only for testing login flow itself │
629
+ └─────────────────────────────────────────────────────────────┘
630
+ ```
631
+
632
+ **Playwright Auth Setup (MANDATORY pattern):**
633
+
634
+ ```typescript
635
+ // playwright/auth.setup.ts - Global setup, runs ONCE
636
+ import { test as setup } from '@playwright/test';
637
+
638
+ const authFile = 'playwright/.auth/user.json';
639
+
640
+ setup('authenticate', async ({ page }) => {
641
+ // Option 1: UI login (only in setup, not per test!)
642
+ await page.goto('/login');
643
+ await page.fill('[name="email"]', 'test@example.com');
644
+ await page.fill('[name="password"]', 'testpass123');
645
+ await page.click('button[type="submit"]');
646
+ await page.waitForURL('/dashboard');
647
+
648
+ // Save auth state for reuse
649
+ await page.context().storageState({ path: authFile });
650
+ });
651
+
652
+ // Option 2: API-based auth (PREFERRED - faster, more reliable)
653
+ setup('authenticate via API', async ({ request }) => {
654
+ const response = await request.post('/api/auth/login', {
655
+ data: { email: 'test@example.com', password: 'testpass123' }
656
+ });
657
+
658
+ // Extract and save cookies/tokens
659
+ await request.storageState({ path: authFile });
660
+ });
661
+ ```
662
+
663
+ ```typescript
664
+ // playwright.config.ts
665
+ export default defineConfig({
666
+ projects: [
667
+ // Setup project - runs first
668
+ { name: 'setup', testMatch: /.*\.setup\.ts/ },
669
+
670
+ // Tests use auth state from setup
671
+ {
672
+ name: 'chromium',
673
+ use: {
674
+ storageState: 'playwright/.auth/user.json',
675
+ },
676
+ dependencies: ['setup'],
677
+ },
678
+ ],
679
+ });
680
+ ```
681
+
682
+ ```typescript
683
+ // tests/dashboard.spec.ts - NO LOGIN CODE NEEDED!
684
+ test('user can view dashboard', async ({ page }) => {
685
+ // Already authenticated via storageState!
686
+ await page.goto('/dashboard');
687
+ await expect(page.locator('h1')).toContainText('Dashboard');
688
+ });
689
+ ```
690
+
691
+ **Handling Auth Edge Cases:**
692
+
693
+ ```typescript
694
+ // For tests that need DIFFERENT users:
695
+ test.describe('admin features', () => {
696
+ test.use({ storageState: 'playwright/.auth/admin.json' });
697
+
698
+ test('admin can delete users', async ({ page }) => {
699
+ // Uses admin auth state
700
+ });
701
+ });
702
+
703
+ // For tests that need NO auth (login page testing):
704
+ test.describe('login flow', () => {
705
+ test.use({ storageState: { cookies: [], origins: [] } });
706
+
707
+ test('shows login form', async ({ page }) => {
708
+ await page.goto('/login');
709
+ // Test the actual login UI
710
+ });
711
+ });
712
+ ```
713
+
714
+ **Common Auth Flakiness Fixes:**
715
+
716
+ | Problem | Solution |
717
+ |---------|----------|
718
+ | Session expires mid-test | Increase token TTL for test env, or refresh in setup |
719
+ | Rate limited on login | Use API auth, not UI; seed test users with known creds |
720
+ | Captcha blocks tests | Disable captcha in test env, or use bypass token |
721
+ | OAuth redirect fails | Mock OAuth provider, or use test-specific flow |
722
+ | Token not persisted | Wait for storage to complete before saving state |
723
+ | Different auth per test | Use separate storageState files per user role |
724
+
725
+ **Test Database Strategy:**
726
+
727
+ ```typescript
728
+ // global-setup.ts - Seed ONCE before all tests
729
+ async function globalSetup() {
730
+ // 1. Reset test database to known state
731
+ await resetTestDatabase();
732
+
733
+ // 2. Seed test users (with KNOWN passwords!)
734
+ await seedTestUsers([
735
+ { email: 'user@test.local', password: 'Test123!', role: 'user' },
736
+ { email: 'admin@test.local', password: 'Admin123!', role: 'admin' },
737
+ ]);
738
+
739
+ // 3. Seed test data
740
+ await seedTestData();
741
+ }
742
+
743
+ // DON'T create users per test - use seeded users!
744
+ ```
745
+
746
+ **Auto Mode E2E Checklist:**
747
+ ```
748
+ Before running E2E tests in auto mode:
749
+ ✅ Test users seeded in database with known passwords
750
+ ✅ Auth state files generated (user.json, admin.json)
751
+ ✅ playwright.config.ts uses storageState
752
+ ✅ Individual tests DON'T login (except login flow tests)
753
+ ✅ Test env has relaxed rate limits
754
+ ✅ Captcha/2FA disabled or bypassed in test env
755
+ ```
756
+
757
+ ### 🔄 Continuous Refactoring (Part of Auto Loop)
758
+
759
+ **As tests grow, REFACTOR proactively:**
760
+
761
+ ```
762
+ After every 3-5 tasks:
763
+ 1. Review test organization → Extract shared fixtures
764
+ 2. Review code duplication → Extract utilities
765
+ 3. Review file sizes → Split if >300 lines
766
+ 4. Review imports → Consolidate, remove unused
767
+ ```
768
+
769
+ **Refactoring Triggers:**
770
+ - Test file > 200 lines → Split by feature
771
+ - Duplicate test setup → Extract to fixtures
772
+ - Same assertion pattern 3+ times → Create helper
773
+ - Source file > 300 lines → Extract module
774
+
775
+ ### 📊 Test Status Reporting (MANDATORY in Auto Mode)
776
+
777
+ **After EVERY task, report test status to user:**
778
+
779
+ ```markdown
780
+ ## 🧪 Test Status Report
781
+
782
+ | Type | Status | Pass/Total | Coverage |
783
+ |------|--------|------------|----------|
784
+ | Unit | ✅ | 42/42 | 87% |
785
+ | Integration | ✅ | 12/12 | - |
786
+ | E2E | ⚠️ | 8/10 | - |
787
+
788
+ **Failing tests:**
789
+ - `auth.spec.ts:45` - Login redirect not working
790
+ - `checkout.spec.ts:112` - Payment timeout
791
+
792
+ **Next:** Fixing E2E failures before continuing...
793
+ ```
794
+
795
+ ### 🏠 Local-First Development
796
+
797
+ **If no deployment instructions provided, BUILD AND TEST LOCALLY FIRST:**
798
+
799
+ ```
800
+ 1. Implement feature locally
801
+ 2. Run ALL tests (unit, integration, E2E)
802
+ 3. Verify everything works
803
+ 4. THEN ask user about deployment preferences
804
+ ```
805
+
806
+ **Don't assume deployment target!** Ask user:
807
+ ```markdown
808
+ 🚀 **Deployment Options**
809
+
810
+ Your scraper is ready and all tests pass locally.
811
+
812
+ **Where would you like to deploy?**
813
+ - Vercel Cron (serverless, free tier available)
814
+ - Railway (always-on, $5/mo)
815
+ - GitHub Actions (CI-based, free)
816
+ - Local cron (self-hosted)
817
+ - Other?
818
+ ```
819
+
820
+ ### 🔧 Infrastructure Decision-Making
821
+
822
+ **For scrapers, cron jobs, background tasks - ULTRATHINK on best approach:**
823
+
824
+ ```
825
+ ┌─────────────────────────────────────────────────────────────┐
826
+ │ INFRASTRUCTURE DECISION TREE │
827
+ ├─────────────────────────────────────────────────────────────┤
828
+ │ Scraper/Cron Job: │
829
+ │ ├─ Frequency < 1/hour → Vercel Cron, GitHub Actions │
830
+ │ ├─ Frequency ≥ 1/hour → Railway, Render, dedicated server │
831
+ │ ├─ Heavy compute → Dedicated VM, Docker container │
832
+ │ └─ Real-time → Always-on server, WebSocket │
833
+ │ │
834
+ │ Data Storage: │
835
+ │ ├─ Simple KV → Upstash Redis, Vercel KV │
836
+ │ ├─ Relational → Supabase, PlanetScale, Neon │
837
+ │ ├─ Document → MongoDB Atlas, Supabase │
838
+ │ └─ Time-series → TimescaleDB, InfluxDB │
839
+ │ │
840
+ │ File Storage: │
841
+ │ ├─ Static assets → Cloudflare R2, S3 │
842
+ │ └─ Large files → S3, GCS, Backblaze B2 │
843
+ └─────────────────────────────────────────────────────────────┘
844
+ ```
845
+
846
+ **When implementing scrapers/cron jobs:**
847
+ 1. **Ultrathink** on best hosting options given requirements
848
+ 2. **Research** rate limits, costs, reliability
849
+ 3. **Propose** 2-3 options with trade-offs
850
+ 4. **Build locally first** with tests
851
+ 5. **Deploy** only after user confirms target
852
+
853
+ ### For Claude Code Users
854
+
855
+ Auto mode uses Claude Code's Stop Hook to create a feedback loop:
856
+ - `/sw:do` continues until all tasks complete
857
+ - `/sw:auto-status` shows session progress
858
+ - `/sw:cancel-auto` stops autonomous execution
859
+
860
+ Session state stored in `.specweave/state/auto-session.json`.
861
+
862
+ ### For Non-Claude AI Systems
863
+
864
+ If using SpecWeave with other AI systems (GPT, Gemini, etc.), implement this loop pattern:
865
+
866
+ ```bash
867
+ # Bash loop for autonomous execution
868
+ while true; do
869
+ # Check if all tasks complete
870
+ TOTAL=$(grep -c "^### T-" .specweave/increments/*/tasks.md 2>/dev/null || echo "0")
871
+ DONE=$(grep -c '\[x\].*completed' .specweave/increments/*/tasks.md 2>/dev/null || echo "0")
872
+
873
+ if [ "$TOTAL" -gt 0 ] && [ "$DONE" -ge "$TOTAL" ]; then
874
+ echo "All tasks complete!"
875
+ break
876
+ fi
877
+
878
+ # Feed prompt to your AI
879
+ cat PROMPT.md | your-ai-cli
880
+
881
+ # Safety: max iterations
882
+ ITER=$((ITER + 1))
883
+ if [ "$ITER" -ge 100 ]; then
884
+ echo "Max iterations reached"
885
+ break
886
+ fi
887
+ done
888
+ ```
889
+
890
+ **Key Concepts**:
891
+ - **Completion Detection**: Check tasks.md for `[x] completed` status
892
+ - **Completion Tag**: Output `<auto-complete>DONE</auto-complete>` when finished
893
+ - **Max Iterations**: Always set a limit (default: 100)
894
+ - **Human Gates**: Pause for sensitive ops (deploy, publish, force-push)
895
+
896
+ **Human-Gated Operations** (require manual approval):
897
+ - `npm publish`, `git push --force`, `rm -rf /`
898
+ - Any `production` deployment
899
+ - API key or credential changes
900
+ - Database migrations (`drop`, `delete from`, `migrate`)
901
+
902
+ **Circuit Breaker Pattern**: If external API (GitHub, JIRA) fails 3+ times, queue operations and continue.
903
+ <!-- SW:END:auto -->
904
+
905
+ <!-- SW:SECTION:docs version="1.0.56" -->
235
906
  ## Docs
236
907
 
237
908
  [spec-weave.com](https://spec-weave.com) | `.specweave/docs/internal/`