ruchy 4.2.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# Ruchy Notebook - EXTREME Quality Gates (wasm-labs inspired)

## 🎯 Vision: Jupyter-Level UX with Rust-Level Quality

**Goal**: Create a notebook experience that **empirically proves** all 41 language features work interactively.

---

## 🚦 3-Level Quality System (wasm-labs pattern)

### Level 1: quality-fast (<30s) - Pre-Commit
**Run**: `make notebook-quality-fast`

```makefile
notebook-quality-fast:
    @cargo fmt -- --check
    @cargo clippy -- -D warnings
    @cargo test --test notebook_core
    @echo "✅ Fast quality gate passed"
```

**Gates**:
- ✅ Format check
- ✅ Clippy warnings = errors
- ✅ Core notebook tests (20-30 tests)
- ❌ NO coverage (too slow)
- ❌ NO mutation (too slow)

---

### Level 2: quality-complete (~5min) - Pre-Push
**Run**: `make notebook-quality-complete`

```makefile
notebook-quality-complete: notebook-quality-fast
    @cargo test --features notebook --all
    @cargo llvm-cov --features notebook --fail-under-lines 85
    @cargo llvm-cov --features notebook --branch --fail-under-branches 90
    @echo "✅ Complete quality gate passed"
```

**Gates**:
- ✅ All fast checks
- ✅ All notebook tests
- ✅ Line coverage ≥85%
- ✅ Branch coverage ≥90%
- ❌ NO mutation (takes 10+ min)

---

### Level 3: quality-extreme (~10-15min) - Pre-Deploy
**Run**: `make notebook-quality-extreme`

```makefile
notebook-quality-extreme: notebook-quality-complete
    @cargo mutants --features notebook --file src/notebook/*.rs
    @# Verify mutation score ≥90%
    @echo "✅ Extreme quality gate passed"
```

**Gates**:
- ✅ All complete checks
- ✅ Mutation testing ≥90% score
- ✅ E2E tests with real browser
- ✅ WASM size check (<500KB)
- ✅ Zero WASI imports

---

## 📊 Coverage Requirements (wasm-labs standards)

```yaml
coverage:
  line_coverage:
    minimum: 85%
    target: 90%
    enforcement: "BLOCKING - CI fails below threshold"

  branch_coverage:
    minimum: 90%
    target: 95%
    enforcement: "BLOCKING - CI fails below threshold"
    note: "Branch coverage proves decision paths tested"

  mutation_coverage:
    minimum: 90%
    target: 95%
    enforcement: "BLOCKING - Pre-deploy only"
    note: "Mutation testing proves tests catch real bugs"
```

**Why Branch Coverage ≥90%?**
- Line coverage measures execution
- Branch coverage measures decisions
- Mutation coverage measures effectiveness

**Example**:
```rust
// This has 100% line coverage but only 50% branch coverage
if x > 0 { /* tested */ } else { /* NOT tested */ }
```

---

## 🧬 Mutation Testing Requirements

**Target**: ≥90% mutation score (wasm-labs standard)

**Fast Mutation Testing** (for development):
```bash
# Only test recent changes
cargo mutants --features notebook --in-diff HEAD~1
# Runtime: ~2-3 minutes
```

**Full Mutation Testing** (for CI/deploy):
```bash
# Test all notebook code
cargo mutants --features notebook --file src/notebook/*.rs
# Runtime: ~10-15 minutes
```

**Mutation Score Calculation**:
```
mutation_score = caught_mutants / (caught_mutants + missed_mutants)
Target: ≥90%
```

---

## 🎭 E2E Testing with Playwright (wasm-labs pattern)

### Test Structure
```
tests/
├── e2e/
│   ├── notebook-basic.spec.ts     # Basic notebook operations
│   ├── notebook-features.spec.ts  # All 41 language features
│   ├── notebook-error.spec.ts     # Error handling
│   └── notebook-wasm.spec.ts      # WASM-specific tests
```

### Test Scenarios (Minimum)
```typescript
test('Notebook loads and runs code', async ({ page }) => {
  await page.goto('http://localhost:8000/notebook.html');
  await page.fill('textarea', '1 + 1');
  await page.click('button.run');
  await expect(page.locator('.output')).toContainText('2');
});

test('All 41 language features work', async ({ page }) => {
  // Load feature test suite
  const features = await loadFeatureTests();

  for (const feature of features) {
    await testFeature(page, feature);
  }

  // All 41 must pass
  expect(passedFeatures).toBe(41);
});
```

### Browser Matrix
```yaml
browsers:
  - Chrome (latest)
  - Firefox (latest)
  - Safari (latest - MacOS only)

matrix_tests: 41 features × 3 browsers = 123 test runs
runtime: ~5-10 minutes
```

---

## 📦 WASM Quality Gates

### Size Requirements
```yaml
wasm_size:
  maximum: 500KB     # Hard limit
  target: 300KB      # Ideal
  current: TBD       # Measure with `make wasm-size`
```

### Purity Requirements
```yaml
wasm_purity:
  wasi_imports: 0    # Pure WASM only
  js_glue: "minimal" # Only essentials
  verification: "wasm-objdump -x notebook.wasm | grep -c wasi_"
```

### Validation Commands
```bash
# Check WASM size
make wasm-size

# Validate WASM structure
make wasm-check

# Deep inspection with PMAT
make pmat-wasm-notebook
```

---

## 🔬 Notebook-Specific Testing

### 1. Cell Execution Tests
```rust
#[test]
fn test_cell_executes_expression() {
    let mut notebook = Notebook::new();
    let result = notebook.execute_cell("1 + 1");
    assert_eq!(result, "2");
}

#[test]
fn test_cell_preserves_state() {
    let mut notebook = Notebook::new();
    notebook.execute_cell("let x = 10");
    let result = notebook.execute_cell("x + 5");
    assert_eq!(result, "15");
}
```

### 2. Output Formatting Tests
```rust
#[test]
fn test_rich_output_dataframe() {
    let mut notebook = Notebook::new();
    let result = notebook.execute_cell("df![[1, 2], [3, 4]]");
    assert!(result.contains("<table>"));
}

#[test]
fn test_rich_output_error() {
    let mut notebook = Notebook::new();
    let result = notebook.execute_cell("undefined_var");
    assert!(result.contains("Error:"));
    assert!(result.contains("undefined_var"));
}
```

### 3. WASM Integration Tests
```rust
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
fn test_wasm_notebook_init() {
    let notebook = Notebook::new();
    assert!(notebook.is_ready());
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
fn test_wasm_cell_execution() {
    let mut notebook = Notebook::new();
    let result = notebook.execute_cell("2 + 2");
    assert_eq!(result, "4");
}
```

### 4. Property Tests (10,000+ iterations)
```rust
#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn notebook_never_panics(code: String) {
            let mut notebook = Notebook::new();
            let _ = notebook.execute_cell(&code);
            // Should not panic, even on invalid input
        }

        #[test]
        fn state_isolation_property(
            var_name in "[a-z]+",
            value in any::<i64>()
        ) {
            let mut nb1 = Notebook::new();
            let mut nb2 = Notebook::new();

            nb1.execute_cell(&format!("let {} = {}", var_name, value));

            // nb2 should NOT see nb1's variables
            let result = nb2.execute_cell(&var_name);
            assert!(result.contains("Error"));
        }
    }
}
```

---

## 📚 The MD Book: Language Proof via Notebook

**Structure**:
```
docs/notebook/book/
├── src/
│   ├── SUMMARY.md                 # Table of contents
│   ├── 00-introduction.md         # Why this book exists
│   │
│   ├── 01-basic-syntax/
│   │   ├── README.md
│   │   ├── 01-literals.md         # Feature 1/41
│   │   ├── 02-variables.md        # Feature 2/41
│   │   ├── 03-comments.md         # Feature 3/41
│   │   └── proof.ruchy            # Runnable proof
│   │
│   ├── 02-operators/
│   │   ├── README.md
│   │   ├── 01-arithmetic.md       # Feature 4/41
│   │   ├── 02-comparison.md       # Feature 5/41
│   │   └── proof.ruchy
│   │
│   ├── 03-control-flow/
│   │   ├── README.md
│   │   ├── 01-if-else.md          # Feature 6/41
│   │   ├── 02-match.md            # Feature 7/41
│   │   ├── 03-for-loops.md        # Feature 8/41
│   │   └── proof.ruchy
│   │
│   ├── ... (remaining 33 features)
│   │
│   └── 15-validation/
│       ├── README.md
│       ├── 01-coverage-report.md  # Test coverage proof
│       ├── 02-mutation-report.md  # Mutation testing proof
│       └── 03-e2e-report.md       # Playwright test proof
│
└── book.toml                      # mdBook configuration
```

### Book Philosophy

**Every Chapter = Empirical Proof**:
1. **Feature Description**: What it does
2. **Notebook Code**: Copy-paste into notebook
3. **Expected Output**: What you should see
4. **Test Proof**: Link to automated test
5. **Coverage Proof**: Link to coverage report
6. **Mutation Proof**: Link to mutation test

**Example Chapter** (01-basic-syntax/01-literals.md):

```markdown
# Literals - Feature 1/41

## Description
Ruchy supports literals for integers, floats, strings, booleans, and nil.

## Try it in Notebook

Open ruchy notebook and run:

\`\`\`ruchy
# Integer literal
42

# Float literal
3.14

# String literal
"hello"

# Boolean literals
true
false

# Nil literal
nil
\`\`\`

## Expected Output

\`\`\`
42
3.14
"hello"
true
false
nil
\`\`\`

## Proof

✅ **Test**: `tests/lang_comp/operators.rs::test_literals`
✅ **Coverage**: 100% (5/5 lines)
✅ **Mutation**: 100% (3/3 mutants caught)
✅ **E2E**: `tests/e2e/notebook-features.spec.ts::test_literals`
```

---

## 🎯 Implementation Plan

### Phase 4A: Notebook Core (Week 1)
**Goal**: Basic REPL-style notebook with state persistence

```yaml
tasks:
  - id: "NOTEBOOK-001"
    title: "Notebook core infrastructure"
    tests: "30 unit tests"
    coverage: "≥85% line, ≥90% branch"

  - id: "NOTEBOOK-002"
    title: "Cell execution engine"
    tests: "20 unit tests + 100 property tests"
    coverage: "≥85% line, ≥90% branch"

  - id: "NOTEBOOK-003"
    title: "State persistence across cells"
    tests: "15 unit tests + 50 property tests"
    coverage: "≥85% line, ≥90% branch"
```

### Phase 4B: Rich Output (Week 2)
**Goal**: HTML tables, syntax highlighting, error formatting

```yaml
tasks:
  - id: "NOTEBOOK-004"
    title: "Rich output formatting"
    tests: "25 unit tests"
    coverage: "≥85% line, ≥90% branch"

  - id: "NOTEBOOK-005"
    title: "DataFrame HTML rendering"
    tests: "15 unit tests"
    coverage: "≥85% line, ≥90% branch"
```

### Phase 4C: WASM Integration (Week 3)
**Goal**: Run in browser with full WASM compilation

```yaml
tasks:
  - id: "NOTEBOOK-006"
    title: "WASM notebook runtime"
    tests: "20 WASM tests"
    wasm_size: "<500KB"
    wasm_purity: "0 WASI imports"

  - id: "NOTEBOOK-007"
    title: "Browser integration"
    tests: "30 E2E tests (Playwright)"
    browsers: "Chrome, Firefox, Safari"
```

### Phase 4D: The Book (Week 4)
**Goal**: 41-chapter MD book proving all features work

```yaml
tasks:
  - id: "NOTEBOOK-008"
    title: "MD book structure"
    chapters: 41
    proof_type: "Automated test + coverage + mutation"

  - id: "NOTEBOOK-009"
    title: "Automated proof generation"
    description: "Script that extracts test results into book"
```

---

## 🚀 Makefile Targets

```makefile
# ============================================================================
# Notebook Quality Gates (3-Level System)
# ============================================================================

notebook-quality-fast: fmt clippy test-notebook-core
	@echo "✅ Notebook fast quality gate passed (<30s)"

notebook-quality-complete: notebook-quality-fast test-notebook-all coverage-notebook
	@echo "✅ Notebook complete quality gate passed (~5min)"

notebook-quality-extreme: notebook-quality-complete mutants-notebook e2e-notebook
	@echo "✅ Notebook extreme quality gate passed (~10-15min)"

# ============================================================================
# Notebook Testing
# ============================================================================

test-notebook-core:
	@cargo test --features notebook --test notebook_core

test-notebook-all:
	@cargo test --features notebook --all

# ============================================================================
# Notebook Coverage
# ============================================================================

coverage-notebook:
	@cargo llvm-cov --features notebook --branch --html
	@# Verify thresholds
	@cargo llvm-cov --features notebook --fail-under-lines 85
	@# Branch coverage check
	@echo "Checking branch coverage ≥90%..."

# ============================================================================
# Notebook Mutation Testing
# ============================================================================

mutants-notebook:
	@cargo mutants --features notebook --file src/notebook/*.rs

mutants-notebook-fast:
	@cargo mutants --features notebook --in-diff HEAD~1

# ============================================================================
# Notebook E2E Testing
# ============================================================================

e2e-notebook:
	@npx playwright test tests/e2e/notebook*.spec.ts

e2e-notebook-ui:
	@npx playwright test tests/e2e/notebook*.spec.ts --ui

# ============================================================================
# Notebook WASM
# ============================================================================

wasm-notebook:
	@cargo build --features notebook --target wasm32-unknown-unknown --release
	@wasm-bindgen target/wasm32-unknown-unknown/release/ruchy.wasm \
		--out-dir dist/notebook --target web

wasm-notebook-size:
	@stat -c%s dist/notebook/ruchy_bg.wasm | awk '{print "WASM size: " $$1/1024 "KB"}'

wasm-notebook-check:
	@# Verify size <500KB
	@# Verify 0 WASI imports

# ============================================================================
# The Book
# ============================================================================

book-build:
	@mdbook build docs/notebook/book

book-serve:
	@mdbook serve docs/notebook/book --open

book-proof-generate:
	@# Extract test results, coverage, mutation into book chapters
	@python3 scripts/generate_book_proofs.py
```

---

## ✅ Success Criteria

**Notebook is production-ready when**:
1. ✅ All 41 language features work in notebook
2. ✅ Line coverage ≥85%, branch coverage ≥90%
3. ✅ Mutation score ≥90%
4. ✅ E2E tests pass on 3 browsers
5. ✅ WASM binary <500KB with 0 WASI imports
6. ✅ MD book with 41 chapters of empirical proof
7. ✅ All quality gates pass (fast/complete/extreme)

**Result**: A notebook that **empirically proves** Ruchy is production-ready.