ggen 1.2.0

ggen is a deterministic, language-agnostic code generation framework that treats software artifacts as projections of knowledge graphs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Ggen Dogfooding Guide: Using Ggen to Fix Ggen

## 🎯 Philosophy: Eating Our Own Dog Food

**Core Principle:** Use ggen's own marketplace, lifecycle, templates, and AI tools to solve ggen's problems.

This ensures:
- **Tools work in practice** - We experience what users experience
- **Quality feedback loop** - Issues get fixed faster
- **Credibility** - We trust our own tools for production
- **Innovation** - We push boundaries by using our own features

## 🚨 The Problem: 403 Panic Points

**Current State (October 2025):**
- ❌ 72 `.expect()` calls in production code
- ❌ 331 `.unwrap()` calls in production code
- ❌ Total: 403 potential crash points

**Solution:** Use ggen's own tools to fix this systematically.

## 🛠️ Dogfooding Tools Created

### 1. Automatic Panic Point Fixer
**Location:** `scripts/fix-panic-points.rs`

**What it does:**
- Scans Rust code for `.expect()` and `.unwrap()` calls
- Automatically replaces with safe error handling
- Generates proper `Result<T>` patterns
- Preserves safe patterns (`.unwrap_or()`, `.unwrap_or_else()`)

**Usage:**
```bash
# Dry run (see what would be fixed)
cargo script scripts/fix-panic-points.rs --dry-run

# Fix all panic points
cargo script scripts/fix-panic-points.rs

# Fix specific directories
cargo script scripts/fix-panic-points.rs cli/src ggen-core/src
```

### 2. Safe Error Handling Template
**Location:** `templates/safe-error-handling.tmpl`

**What it does:**
- Generates safe error handling patterns
- Provides template for production-ready code
- Includes common safe patterns library

**Usage:**
```bash
# Generate safe error handling module
ggen template generate templates/safe-error-handling.tmpl \
  --output src/safe_error_handling.rs

# Generate with custom function
ggen template generate templates/safe-error-handling.tmpl \
  --vars function_name=load_config \
  --vars return_type=Config \
  --output src/config_loader.rs
```

### 3. Pre-Commit Hook
**Location:** `.githooks/pre-commit`

**What it does:**
- Blocks commits containing panic points
- Provides fix suggestions
- Enforces production safety standards

**Setup:**
```bash
# Install hooks (run once)
ggen lifecycle run setup-git-hooks

# Or manually
mkdir -p .git/hooks
cp .githooks/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
```

### 4. Panic Point Detection Script
**Location:** `scripts/check-no-panic-points.sh`

**What it does:**
- Scans production code for panic points
- Reports detailed statistics
- Exits with error if panic points found

**Usage:**
```bash
# Check entire codebase
./scripts/check-no-panic-points.sh

# Integrate with CI/CD
ggen lifecycle run validate-safety
```

### 5. Production Lifecycle
**Location:** `make.toml`

**What it provides:**
- Automated safety validation
- Integrated testing pipeline
- Dogfooding phases

**Usage:**
```bash
# Run safety validation
ggen lifecycle run validate-safety

# Run full production pipeline
ggen lifecycle run production-validate

# Use dogfooding pipeline
ggen lifecycle run dogfood
```

## 📋 Complete Dogfooding Workflow

### Step 1: Identify Problems
```bash
# Scan for panic points
./scripts/check-no-panic-points.sh

# Output shows:
# 🔍 Scanning for panic points in production code...
# Results:
#   .expect() calls: 72
#   .unwrap() calls: 331
#   Total panic points: 403
```

### Step 2: Analyze Impact
```bash
# See which files have most panic points
./scripts/check-no-panic-points.sh | grep "Top 10"

# Top 10 offenders:
# cli/src/cmds/remove.rs:58
# ggen-core/src/lifecycle/dag.rs:57
# ggen-core/src/lifecycle/exec.rs:142
# ...
```

### Step 3: Fix Automatically
```bash
# Preview fixes (dry run)
cargo script scripts/fix-panic-points.rs --dry-run --verbose

# Apply fixes to critical paths first (80/20 rule)
cargo script scripts/fix-panic-points.rs cli/src/cmds ggen-core/src/lifecycle

# Verify fixes
./scripts/check-no-panic-points.sh

# Build and test
cargo build --release
cargo test --all-features
```

### Step 4: Use Templates for New Code
```bash
# Generate safe error handling for new module
ggen template generate templates/safe-error-handling.tmpl \
  --vars function_name=process_marketplace_package \
  --vars return_type=Package \
  --vars parameters='[{"name":"path","type":"&Path"},{"name":"validate","type":"bool"}]' \
  --output src/marketplace/safe_package_loader.rs
```

### Step 5: Prevent Future Panic Points
```bash
# Install pre-commit hooks
ggen lifecycle run setup-git-hooks

# Try to commit code with panic points
echo 'fn bad() { Some(1).unwrap(); }' > test.rs
git add test.rs
git commit -m "test"

# Output:
# ❌ COMMIT BLOCKED: Found 1 panic point(s)
# Production code must not contain .expect() or .unwrap() calls.
```

### Step 6: Validate Production Readiness
```bash
# Run complete validation
ggen lifecycle run production-validate

# Checks:
# ✅ No panic points
# ✅ Clippy warnings = 0
# ✅ Tests passing
# ✅ Security audit clean
```

## 🎯 80/20 Dogfooding Strategy

### Phase 1: Critical Safety (Week 1) - 80% Impact

**Focus:** Fix 20% of files with 80% of panic points

```bash
# 1. Identify critical paths
./scripts/check-no-panic-points.sh > panic-report.txt
grep -A 5 "Top 10 offenders" panic-report.txt

# 2. Fix critical files first
cargo script scripts/fix-panic-points.rs \
  cli/src/main.rs \
  cli/src/cmds/mod.rs \
  ggen-core/src/lifecycle/exec.rs \
  ggen-core/src/template.rs

# 3. Validate
./scripts/check-no-panic-points.sh
cargo test --all-features
```

### Phase 2: Automation (Week 2) - 15% Impact

**Focus:** Prevent future panic points

```bash
# 1. Install pre-commit hooks
ggen lifecycle run setup-git-hooks

# 2. Add lifecycle validation
ggen lifecycle run validate-safety

# 3. Update CI/CD
cat >> .github/workflows/ci.yml <<EOF
      - name: Check Panic Points
        run: ./scripts/check-no-panic-points.sh
EOF
```

### Phase 3: Templates (Week 3) - 4% Impact

**Focus:** Generate safe code patterns

```bash
# 1. Use safe error handling template for new code
ggen template generate templates/safe-error-handling.tmpl

# 2. Create project-specific templates
ggen ai generate \
  --description "Safe marketplace package loader" \
  --output templates/marketplace-loader.tmpl

# 3. Document patterns
ggen template generate templates/safe-error-handling.tmpl \
  --output docs/SAFE_PATTERNS.md
```

### Phase 4: Continuous Improvement (Week 4) - 1% Impact

**Focus:** Monitor and improve

```bash
# 1. Regular checks
ggen lifecycle run dogfood

# 2. Metrics tracking
./scripts/check-no-panic-points.sh | tee metrics/panic-points-$(date +%Y%m%d).txt

# 3. Update templates based on learnings
ggen ai generate \
  --description "Update safe error handling template based on common patterns"
```

## 📊 Success Metrics

### Before Dogfooding
- ❌ 403 panic points
- ❌ No automated validation
- ❌ Manual code review required
- ❌ Inconsistent error handling

### After Dogfooding (Target)
- ✅ 0-5 panic points (with SAFE comments)
- ✅ Automated validation in CI/CD
- ✅ Pre-commit hooks prevent new panic points
- ✅ Consistent safe error handling patterns
- ✅ Templates generate safe code by default

## 🔄 Continuous Dogfooding Practices

### Daily Development
```bash
# Before starting work
ggen lifecycle run validate-safety

# During development (use templates)
ggen template generate templates/safe-error-handling.tmpl

# Before committing (automatic via hook)
git commit -m "feat: add marketplace sync"
# → Pre-commit hook validates automatically

# After commit
ggen lifecycle run test
```

### Weekly Reviews
```bash
# Check progress
./scripts/check-no-panic-points.sh > weekly-report.txt

# Compare with previous week
diff weekly-report.txt previous-week-report.txt

# Update templates if patterns emerge
ggen ai generate \
  --description "Update templates based on weekly patterns"
```

### Monthly Improvements
```bash
# Full dogfooding pipeline
ggen lifecycle run dogfood

# Generate improvements
ggen ai generate \
  --description "Suggest improvements to panic point prevention"

# Update tooling
cargo script scripts/fix-panic-points.rs --update-patterns
```

## 🚀 Advanced Dogfooding Techniques

### 1. AI-Powered Pattern Learning
```bash
# Analyze existing safe patterns
ggen ai analyze \
  --input "src/**/*.rs" \
  --filter "safe error handling" \
  --output patterns/learned-safe-patterns.json

# Generate template from learned patterns
ggen template generate \
  --from-patterns patterns/learned-safe-patterns.json \
  --output templates/learned-safe-handling.tmpl
```

### 2. SPARQL-Driven Code Analysis
```bash
# Create knowledge graph of code structure
ggen graph analyze src/ --output code-graph.ttl

# Query for panic-prone patterns
ggen graph query \
  --sparql "SELECT ?function WHERE { ?function :hasPanicPoint true }" \
  --graph code-graph.ttl

# Generate fixes using graph data
ggen ai generate \
  --from-graph code-graph.ttl \
  --description "Generate safe alternatives for panic-prone functions"
```

### 3. Marketplace Integration
```bash
# Package safe error handling as marketplace item
ggen market package \
  --name "safe-error-handling" \
  --templates templates/safe-error-handling.tmpl \
  --scripts scripts/fix-panic-points.rs \
  --output marketplace/packages/safe-error-handling

# Publish to marketplace
ggen market publish safe-error-handling

# Install in other projects
ggen market add safe-error-handling
```

## 🎓 Learning From Dogfooding

### What We Learned

1. **Automation is Critical**
   - Manual code review misses panic points
   - Pre-commit hooks catch issues immediately
   - Automated fixers save hours of work

2. **Templates Ensure Consistency**
   - Developers copy-paste existing patterns
   - Templates encode best practices
   - AI generates safe code from templates

3. **Lifecycle Integration Works**
   - Validation becomes automatic
   - No extra steps for developers
   - Continuous assurance of quality

4. **80/20 Rule Applies**
   - 20% of files have 80% of panic points
   - Fixing critical paths first = fast progress
   - Templates prevent issues in new code

### Best Practices Discovered

1. **Use `?` operator instead of `.expect()`**
2. **Use `.unwrap_or_default()` for Options**
3. **Add context with `.with_context()`**
4. **Document truly safe `.unwrap()` with `// SAFE:`**
5. **Generate safe patterns with templates**

## 📚 Resources

### Documentation
- `docs/PRODUCTION_READINESS_8020.md` - Production checklist
- `docs/DOGFOODING_EXAMPLES.md` - More examples
- `templates/safe-error-handling.tmpl` - Safe patterns template

### Scripts
- `scripts/fix-panic-points.rs` - Automatic fixer
- `scripts/check-no-panic-points.sh` - Validation script
- `.githooks/pre-commit` - Git hook for prevention

### Lifecycle
- `make.toml` - Complete lifecycle configuration
- Run `ggen lifecycle list` to see all phases
- Run `ggen lifecycle run dogfood` for complete workflow

## 🔥 Quick Start

**5-Minute Dogfooding Setup:**

```bash
# 1. Install hooks
ggen lifecycle run setup-git-hooks

# 2. Scan current state
./scripts/check-no-panic-points.sh

# 3. Fix critical paths
cargo script scripts/fix-panic-points.rs cli/src

# 4. Validate
ggen lifecycle run validate-safety

# 5. Done! Panic points prevented automatically
```

---

**Remember: The best way to prove ggen works is to use it to make ggen better.**

## 🎯 Next Steps

1. Run `./scripts/check-no-panic-points.sh` to see current state
2. Fix critical paths with `cargo script scripts/fix-panic-points.rs`
3. Install hooks with `ggen lifecycle run setup-git-hooks`
4. Use templates for new code
5. Monitor progress weekly
6. Share improvements via marketplace

**Target: 0 panic points in production code by Q1 2026**