@lssm/app.cli-contractspec 0.0.0-canary-20251221164004

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.
package/README.md ADDED
@@ -0,0 +1,628 @@
1
+ # @lssm/app.cli-contractspec
2
+
3
+ **Stabilize your AI-generated code** — Define contracts once, generate consistent code across API, DB, UI, and events. Safe regeneration. No lock-in.
4
+
5
+ CLI tool for creating, building, and validating contract specifications.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ bun add -D @lssm/app.cli-contractspec
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```bash
16
+ # Create a new contract spec interactively
17
+ contractspec create
18
+
19
+ # Create with AI assistance
20
+ contractspec create --ai
21
+
22
+ # Build implementation from spec
23
+ contractspec build src/contracts/mySpec.ts
24
+
25
+ # Validate a spec
26
+ contractspec validate src/contracts/mySpec.ts
27
+ ```
28
+
29
+ ## Commands
30
+
31
+ ### `contractspec list`
32
+
33
+ List all contract specifications in the project with filtering options.
34
+
35
+ **Options:**
36
+ - `--pattern <pattern>` - File pattern to search (glob)
37
+ - `--deep` - Load spec modules to extract richer metadata (executes spec modules)
38
+ - `--type <type>` - Filter by spec type (operation, event, presentation, etc.)
39
+ - `--owner <owner>` - Filter by owner
40
+ - `--tag <tag>` - Filter by tag
41
+ - `--stability <level>` - Filter by stability (experimental, beta, stable, deprecated)
42
+ - `--json` - Output as JSON for scripting
43
+
44
+ **Examples:**
45
+
46
+ ```bash
47
+ # List all specs
48
+ contractspec list
49
+
50
+ # Filter by type and stability
51
+ contractspec list --type operation --stability stable
52
+
53
+ # Find specs by owner
54
+ contractspec list --owner @team-platform
55
+
56
+ # JSON output for scripting
57
+ contractspec list --json
58
+ ```
59
+
60
+ ### `contractspec watch`
61
+
62
+ Watch contract specifications and auto-regenerate on changes.
63
+
64
+ **Options:**
65
+ - `--pattern <pattern>` - File pattern to watch (default: `**/*.contracts.ts`)
66
+ - `--build` - Auto-run build command on changes
67
+ - `--validate` - Auto-run validate command on changes
68
+ - `--on-start <mode>` - Run action on startup: none|validate|build|both (default: none)
69
+ - `--continue-on-error` - Do not exit on build/validate errors
70
+ - `--debounce <ms>` - Debounce delay in milliseconds (default: 500)
71
+
72
+ **Examples:**
73
+
74
+ ```bash
75
+ # Watch for changes and auto-build
76
+ contractspec watch --build
77
+
78
+ # Watch and auto-validate
79
+ contractspec watch --validate
80
+
81
+ # Custom pattern and debounce
82
+ contractspec watch --pattern 'src/**/*.ts' --debounce 1000
83
+ ```
84
+
85
+ ### `contractspec sync`
86
+
87
+ Sync contracts by building all discovered specs.
88
+
89
+ **Options:**
90
+ - `--pattern <pattern>` - File pattern to search (glob)
91
+ - `--buckets <buckets>` - Optional output buckets (comma-separated). Builds repeat into `./generated/<bucket>/`
92
+ - `--surfaces <surfaces>` - (deprecated) Alias for `--buckets`
93
+ - `--validate` - Validate each spec before building (spec-only)
94
+ - `--dry-run` - Show what would be synced without making changes
95
+
96
+ **Examples:**
97
+
98
+ ```bash
99
+ # Sync all surfaces
100
+ contractspec sync
101
+
102
+ # Sync specific surfaces
103
+ contractspec sync --surfaces api,ui
104
+
105
+ # Preview changes
106
+ contractspec sync --dry-run
107
+ ```
108
+
109
+ ### `contractspec clean`
110
+
111
+ Clean generated files and build artifacts.
112
+
113
+ **Options:**
114
+ - `--dry-run` - Show what would be deleted without deleting
115
+ - `--generated-only` - Only clean generated directories (generated/, dist/, .turbo/, outputDir artifacts)
116
+ - `--older-than <days>` - Only clean files older than specified days
117
+ - `--force` - Skip confirmation prompts
118
+ - `--git-clean` - Use `git clean -fdx` for comprehensive cleanup (requires confirmation or `--force`)
119
+
120
+ **Examples:**
121
+
122
+ ```bash
123
+ # Clean all generated files
124
+ contractspec clean
125
+
126
+ # Preview cleanup
127
+ contractspec clean --dry-run
128
+
129
+ # Clean only old files
130
+ contractspec clean --older-than 30
131
+
132
+ # Use git clean
133
+ contractspec clean --git-clean
134
+ ```
135
+
136
+ ### `contractspec deps`
137
+
138
+ Analyze contract dependencies and relationships.
139
+
140
+ **Options:**
141
+ - `--pattern <pattern>` - File pattern to search (glob)
142
+ - `--entry <name>` - Focus on a specific contract name
143
+ - `--format <format>` - text|json|dot (default: text)
144
+ - `--graph` - (deprecated) Same as `--format dot`
145
+ - `--circular` - Find circular dependencies
146
+ - `--missing` - Find missing dependencies
147
+ - `--json` - (deprecated) Same as `--format json`
148
+
149
+ **Examples:**
150
+
151
+ ```bash
152
+ # Show dependency overview
153
+ contractspec deps
154
+
155
+ # Analyze specific contract
156
+ contractspec deps --entry user.signup
157
+
158
+ # Find circular dependencies
159
+ contractspec deps --circular
160
+
161
+ # Generate graphviz graph
162
+ contractspec deps --format dot > deps.dot
163
+ ```
164
+
165
+ ### `contractspec diff`
166
+
167
+ Compare contract specifications and show differences.
168
+
169
+ **Arguments:**
170
+ - `<spec1> <spec2>` - Two spec files to compare
171
+
172
+ **Options:**
173
+ - `--breaking` - Only show breaking changes
174
+ - `--semantic` - Show semantic differences (not just text)
175
+ - `--json` - Output as JSON for scripting
176
+ - `--baseline <ref>` - Compare with git reference (branch/commit)
177
+
178
+ **Examples:**
179
+
180
+ ```bash
181
+ # Compare two specs
182
+ contractspec diff spec1.ts spec2.ts
183
+
184
+ # Compare with git branch
185
+ contractspec diff spec.ts spec.ts --baseline main
186
+
187
+ # Show only breaking changes
188
+ contractspec diff spec1.ts spec2.ts --breaking
189
+
190
+ # Semantic comparison
191
+ contractspec diff spec1.ts spec2.ts --semantic
192
+ ```
193
+
194
+ ### `contractspec create`
195
+
196
+ Interactive wizard to create contract specifications.
197
+
198
+ **Options:**
199
+ - `--type <type>` - Spec type: operation, event, presentation, form, feature
200
+ - `--ai` - Use AI to generate spec from description
201
+ - `--provider <provider>` - AI provider: claude, openai, ollama, custom
202
+ - `--model <model>` - AI model to use
203
+
204
+ **Examples:**
205
+
206
+ ```bash
207
+ # Interactive wizard
208
+ contractspec create
209
+
210
+ # Create operation spec with AI
211
+ contractspec create --type operation --ai
212
+
213
+ # Use local Ollama model
214
+ contractspec create --ai --provider ollama --model codellama
215
+ ```
216
+
217
+ ### `contractspec build`
218
+
219
+ Generate implementation code from contract specs using AI agents with automatic fallbacks.
220
+
221
+ **Options:**
222
+ - `--output-dir <dir>` - Output directory (default: ./generated)
223
+ - `--provider <provider>` - AI provider: claude, openai, ollama, custom
224
+ - `--model <model>` - AI model to use
225
+ - `--agent-mode <mode>` - Agent mode: simple, cursor, claude-code, openai-codex
226
+ - `--no-tests` - Skip test generation
227
+ - `--no-agent` - Disable AI (use basic templates)
228
+
229
+ > ℹ️ The build command automatically orchestrates between the selected agent and lightweight templates. If an agent becomes unavailable (missing API key, Cursor not running, etc.) the CLI falls back to deterministic templates so the build never blocks.
230
+
231
+ **Examples:**
232
+
233
+ ```bash
234
+ # Generate handler from operation spec (default: simple agent)
235
+ contractspec build src/contracts/signup.contracts.ts
236
+
237
+ # Use Claude Code agent for high-quality production code
238
+ contractspec build src/contracts/signup.contracts.ts --agent-mode claude-code
239
+
240
+ # Use OpenAI Codex for algorithmic implementations
241
+ contractspec build src/contracts/optimizer.contracts.ts --agent-mode openai-codex
242
+
243
+ # Generate without AI (basic templates only)
244
+ contractspec build src/contracts/simple.contracts.ts --no-agent
245
+ ```
246
+
247
+ ### `contractspec validate`
248
+
249
+ Validate contract specifications and optionally verify implementations against specs using AI.
250
+
251
+ **Options:**
252
+ - `--check-implementation` - Validate implementation against spec using AI
253
+ - `--implementation-path <path>` - Path to implementation (auto-detected if not specified)
254
+ - `--agent-mode <mode>` - Agent mode for validation: simple, claude-code, openai-codex
255
+ - `--check-handlers` - Verify handler implementations exist
256
+ - `--check-tests` - Verify test coverage
257
+ - `-i, --interactive` - Interactive mode - prompt for what to validate
258
+
259
+ > ℹ️ When no validation scope is provided, the CLI prompts you to choose between spec-only validation or full spec + implementation validation. In non-interactive environments it defaults to spec-only. Use `--check-implementation` to skip the prompt.
260
+
261
+ **Examples:**
262
+
263
+ ```bash
264
+ # Validate spec structure only
265
+ contractspec validate src/contracts/signup.contracts.ts
266
+
267
+ # Validate implementation against spec with AI
268
+ contractspec validate src/contracts/signup.contracts.ts --check-implementation
269
+
270
+ # Interactive validation with Claude Code agent
271
+ contractspec validate src/contracts/signup.contracts.ts -i --agent-mode claude-code
272
+
273
+ # Validate specific implementation file
274
+ contractspec validate src/contracts/signup.contracts.ts \
275
+ --check-implementation \
276
+ --implementation-path src/handlers/signup.handler.ts \
277
+ --agent-mode claude-code
278
+ ```
279
+
280
+ ### `contractspec ci`
281
+
282
+ Run all validation checks for CI/CD pipelines with machine-readable output formats.
283
+
284
+ **Options:**
285
+ - `--pattern <glob>` - Glob pattern for spec discovery
286
+ - `--format <format>` - Output format: text, json, sarif (default: text)
287
+ - `--output <file>` - Write results to file
288
+ - `--fail-on-warnings` - Exit with code 2 on warnings
289
+ - `--skip <checks>` - Skip specific checks (comma-separated)
290
+ - `--checks <checks>` - Only run specific checks (comma-separated)
291
+ - `--check-handlers` - Include handler implementation checks
292
+ - `--check-tests` - Include test coverage checks
293
+ - `--verbose` - Verbose output
294
+
295
+ **Available Checks:**
296
+ - `structure` - Spec structure validation (meta, io, policy)
297
+ - `integrity` - Contract integrity (orphaned specs, broken refs)
298
+ - `deps` - Dependency analysis (circular deps, missing refs)
299
+ - `doctor` - Installation health (skips AI in CI)
300
+ - `handlers` - Handler implementation existence
301
+ - `tests` - Test file existence
302
+
303
+ **Exit Codes:**
304
+ - `0` - All checks passed
305
+ - `1` - Errors found
306
+ - `2` - Warnings found (with `--fail-on-warnings`)
307
+
308
+ **Examples:**
309
+
310
+ ```bash
311
+ # Run all CI checks
312
+ contractspec ci
313
+
314
+ # Output as JSON for parsing
315
+ contractspec ci --format json
316
+
317
+ # Output SARIF for GitHub Code Scanning
318
+ contractspec ci --format sarif --output results.sarif
319
+
320
+ # Skip doctor checks
321
+ contractspec ci --skip doctor
322
+
323
+ # Run only structure and integrity checks
324
+ contractspec ci --checks structure,integrity
325
+
326
+ # Fail on warnings
327
+ contractspec ci --fail-on-warnings
328
+ ```
329
+
330
+ For comprehensive CI/CD integration guides (GitHub Actions, GitLab CI, Jenkins, AWS CodeBuild, etc.), see [docs/ci-cd.md](./docs/ci-cd.md).
331
+
332
+ ## Configuration
333
+
334
+ Create a `.contractsrc.json` file in your project root:
335
+
336
+ ```json
337
+ {
338
+ "aiProvider": "claude",
339
+ "aiModel": "claude-3-7-sonnet-20250219",
340
+ "agentMode": "claude-code",
341
+ "outputDir": "./src",
342
+ "conventions": {
343
+ "operations": "interactions/commands|queries",
344
+ "events": "events",
345
+ "presentations": "presentations",
346
+ "forms": "forms"
347
+ },
348
+ "defaultOwners": ["@team"],
349
+ "defaultTags": ["auto-generated"]
350
+ }
351
+ ```
352
+
353
+ ### Agent Modes
354
+
355
+ The CLI supports multiple AI agent modes for different use cases:
356
+
357
+ - **simple** (default) - Direct LLM API calls, fast and straightforward
358
+ - **cursor** - Leverages Windsurf/Cursor agentic capabilities (requires Cursor environment)
359
+ - **claude-code** - Uses Claude with extended thinking, best for production code and validation
360
+ - **openai-codex** - Uses GPT-4o/o1 models, excellent for algorithms and optimization
361
+
362
+ See [AGENT_MODES.md](./AGENT_MODES.md) for detailed comparison and usage guide.
363
+
364
+ ### AI Providers
365
+
366
+ #### Claude (Anthropic)
367
+
368
+ ```bash
369
+ export ANTHROPIC_API_KEY=your-key-here
370
+ contractspec create --ai --provider claude
371
+ ```
372
+
373
+ #### OpenAI
374
+
375
+ ```bash
376
+ export OPENAI_API_KEY=your-key-here
377
+ contractspec create --ai --provider openai --model gpt-4o
378
+ ```
379
+
380
+ #### Ollama (Local)
381
+
382
+ Install Ollama and pull a model:
383
+
384
+ ```bash
385
+ ollama pull codellama
386
+ contractspec create --ai --provider ollama --model codellama
387
+ ```
388
+
389
+ #### Custom OpenAI-Compatible Endpoint
390
+
391
+ ```bash
392
+ contractspec create --ai --provider custom \
393
+ --endpoint https://your-llm.com/v1 \
394
+ --model your-model-name
395
+ ```
396
+
397
+ Set via environment:
398
+
399
+ ```bash
400
+ export CONTRACTSPEC_LLM_ENDPOINT=https://your-llm.com/v1
401
+ export CONTRACTSPEC_LLM_API_KEY=your-key
402
+ export CONTRACTSPEC_LLM_MODEL=your-model
403
+ ```
404
+
405
+ ### Supported Local Models (Ollama)
406
+
407
+ - `codellama` - Code generation (recommended)
408
+ - `llama3.1` - General purpose
409
+ - `mistral` - Fast, good quality
410
+ - `deepseek-coder` - Specialized for code
411
+
412
+ ## Examples
413
+
414
+ ### Create a Command Spec
415
+
416
+ ```bash
417
+ contractspec create --type operation
418
+ # Follow prompts:
419
+ # - Name: user.signup
420
+ # - Kind: command
421
+ # - Description: Start user signup flow
422
+ # etc.
423
+ ```
424
+
425
+ ### Build Handler from Spec
426
+
427
+ Given a spec file `src/contracts/signup.contracts.ts`:
428
+
429
+ ```bash
430
+ contractspec build src/contracts/signup.contracts.ts
431
+ # Generates:
432
+ # - src/handlers/signup.handler.ts
433
+ # - src/handlers/signup.handler.test.ts
434
+ ```
435
+
436
+ ### Multi-Provider Workflow
437
+
438
+ ```bash
439
+ # Draft with free local model
440
+ contractspec create --ai --provider ollama
441
+
442
+ # Refine with Claude for production
443
+ contractspec build src/contracts/draft.ts --provider claude
444
+ ```
445
+
446
+ ## Advanced Usage
447
+
448
+ ### Multi-Provider and Multi-Agent Workflows
449
+
450
+ Combine different providers and agents for optimal results:
451
+
452
+ ```bash
453
+ # Draft specs with free local model
454
+ contractspec create --ai --provider ollama --model codellama
455
+
456
+ # Generate production code with Claude Code agent
457
+ contractspec build src/contracts/user-signup.contracts.ts \
458
+ --agent-mode claude-code
459
+
460
+ # Validate implementation with AI
461
+ contractspec validate src/contracts/user-signup.contracts.ts \
462
+ --check-implementation \
463
+ --agent-mode claude-code
464
+
465
+ # Use OpenAI for algorithmic code
466
+ contractspec build src/contracts/search-algorithm.contracts.ts \
467
+ --agent-mode openai-codex
468
+ ```
469
+
470
+ ### Environment Variables
471
+
472
+ ```bash
473
+ # API Keys
474
+ export ANTHROPIC_API_KEY=your-key-here
475
+ export OPENAI_API_KEY=your-key-here
476
+
477
+ # Agent configuration
478
+ export CONTRACTSPEC_AGENT_MODE=claude-code
479
+ export CONTRACTSPEC_AI_PROVIDER=claude
480
+ export CONTRACTSPEC_AI_MODEL=claude-3-7-sonnet-20250219
481
+
482
+ # Custom provider
483
+ export CONTRACTSPEC_LLM_ENDPOINT=https://your-llm.com/v1
484
+ export CONTRACTSPEC_LLM_API_KEY=your-api-key
485
+ ```
486
+
487
+ ### CI/CD Integration
488
+
489
+ ```yaml
490
+ # .github/workflows/contracts.yml
491
+ name: Validate Contracts
492
+
493
+ on: [push, pull_request]
494
+
495
+ jobs:
496
+ validate:
497
+ runs-on: ubuntu-latest
498
+ steps:
499
+ - uses: actions/checkout@v3
500
+ - uses: oven-sh/setup-bun@v1
501
+ - run: pnpm install
502
+ - run: contractspec validate 'src/contracts/**/*.ts'
503
+ ```
504
+
505
+ ### Project Structure
506
+
507
+ Recommended organization:
508
+
509
+ ```
510
+ src/
511
+ ├── contracts/ # Contract specs
512
+ │ ├── events/
513
+ │ │ └── user-created.event.ts
514
+ │ ├── interactions/
515
+ │ │ ├── commands/
516
+ │ │ │ └── user-signup.contracts.ts
517
+ │ │ └── queries/
518
+ │ │ └── user-get-profile.contracts.ts
519
+ │ └── presentations/
520
+ │ └── user-profile-card.presentation.ts
521
+ ├── handlers/ # Generated handlers
522
+ ├── components/ # Generated components
523
+ └── forms/ # Generated forms
524
+ ```
525
+
526
+ ## Troubleshooting
527
+
528
+ ### "Provider not available" error
529
+
530
+ **Claude:**
531
+ ```bash
532
+ export ANTHROPIC_API_KEY=sk-ant-...
533
+ ```
534
+
535
+ **OpenAI:**
536
+ ```bash
537
+ export OPENAI_API_KEY=sk-...
538
+ ```
539
+
540
+ **Ollama:**
541
+ ```bash
542
+ # Install Ollama from https://ollama.ai
543
+ ollama serve
544
+ ollama pull codellama
545
+ ```
546
+
547
+ ### Slow generation
548
+
549
+ For faster local generation, use smaller models:
550
+ ```bash
551
+ contractspec create --ai --provider ollama --model codellama:7b
552
+ ```
553
+
554
+ For cloud, use faster models:
555
+ ```bash
556
+ contractspec build spec.ts --provider openai --model gpt-3.5-turbo
557
+ ```
558
+
559
+ ### Import errors in generated code
560
+
561
+ Make sure `@lssm/lib.contracts` and `@lssm/lib.schema` are installed:
562
+ ```bash
563
+ pnpm add @lssm/lib.contracts @lssm/lib.schema
564
+ ```
565
+
566
+ ## Contributing
567
+
568
+ Contributions welcome! Please:
569
+
570
+ 1. Follow existing code style
571
+ 2. Add tests for new features
572
+ 3. Update documentation
573
+ 4. Ensure all tests pass: `pnpm test`
574
+
575
+ ## Agent Modes Deep Dive
576
+
577
+ ### When to Use Each Mode
578
+
579
+ **Simple Mode** - Default, good for:
580
+ - Rapid prototyping
581
+ - Basic implementations
582
+ - Quick iterations
583
+ - CI/CD pipelines (fast)
584
+
585
+ **Cursor Mode** - Best for:
586
+ - Working in Windsurf/Cursor IDE
587
+ - Complex, iterative development
588
+ - Context-aware code generation
589
+
590
+ **Claude Code Mode** - Best for:
591
+ - Production-quality implementations
592
+ - Critical business logic
593
+ - Comprehensive code validation
594
+ - Detailed code reviews
595
+
596
+ **OpenAI Codex Mode** - Best for:
597
+ - Algorithmic problems
598
+ - Performance optimization
599
+ - Mathematical computations
600
+ - Complex data transformations
601
+
602
+ For more details, see [AGENT_MODES.md](./AGENT_MODES.md).
603
+
604
+ ## Roadmap
605
+
606
+ - [x] AI-powered code generation
607
+ - [x] Multiple agent modes (simple, cursor, claude-code, openai-codex)
608
+ - [x] AI-powered implementation validation
609
+ - [x] Contract listing and discovery (`contractspec list`)
610
+ - [x] Watch mode for auto-regeneration (`contractspec watch`)
611
+ - [x] Multi-surface synchronization (`contractspec sync`)
612
+ - [x] Cleanup utilities (`contractspec clean`)
613
+ - [x] Dependency analysis (`contractspec deps`)
614
+ - [x] Contract diffing and comparison (`contractspec diff`)
615
+ - [x] CI/CD integration (`contractspec ci`) with SARIF/JSON output
616
+ - [x] GitHub Action for CI/CD
617
+ - [ ] Form spec generation
618
+ - [ ] Feature spec bundling
619
+ - [ ] Handler signature checking in validation
620
+ - [ ] Test coverage validation
621
+ - [ ] Interactive spec editing
622
+ - [ ] GraphQL schema generation
623
+ - [ ] OpenAPI/Swagger export
624
+
625
+ ## License
626
+
627
+ MIT
628
+