ruchy 1.41.0

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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
.PHONY: help all build test lint format clean coverage examples bench install doc ci prepare-publish quality-gate test-examples test-fuzz test-fuzz-quick tdg-dashboard tdg-stop tdg-status tdg-restart

# Default target
help:
	@echo "Ruchy Language - Development Commands"
	@echo ""
	@echo "Core Commands:"
	@echo "  make build       - Build the project in release mode"
	@echo "  make test        - Run main test suite (lib + property + doc + examples + fuzz tests)"
	@echo "  make test-all    - Run ALL tests including slow ones"
	@echo "  make test-property - Run property-based tests"
	@echo "  make test-doc    - Run documentation tests"
	@echo "  make test-examples - Run all examples (Rust examples + Ruchy scripts)"
	@echo "  make test-fuzz   - Run comprehensive fuzz tests (65+ seconds)"
	@echo "  make test-fuzz-quick - Run quick fuzz tests (5 seconds)"
	@echo "  make test-repl   - Run ALL REPL tests (unit, property, fuzz, examples, coverage)"
	@echo "  make test-nextest - Run tests with nextest (better output)"
	@echo "  make lint        - Run clippy linter"
	@echo "  make format      - Format code with rustfmt"
	@echo "  make clean       - Clean build artifacts"
	@echo ""
	@echo "Quality Commands:"
	@echo "  make coverage    - Generate comprehensive coverage report (Toyota Way)"
	@echo "  make coverage-quick - Quick coverage check for development"
	@echo "  make coverage-open - Generate and open coverage report in browser"
	@echo "  make test-coverage-quality - Show coverage & TDG quality per component"
	@echo "  make quality-gate - Run PMAT quality checks"
	@echo "  make ci          - Run full CI pipeline"
	@echo ""
	@echo "TDG Dashboard Commands:"
	@echo "  make tdg-dashboard - Start real-time TDG quality dashboard"
	@echo "  make tdg-stop    - Stop the TDG dashboard"
	@echo "  make tdg-status  - Check TDG dashboard status"
	@echo "  make tdg-restart - Restart the TDG dashboard"
	@echo ""
	@echo "Development Commands:"
	@echo "  make examples    - Run all examples"
	@echo "  make bench       - Run benchmarks"
	@echo "  make doc         - Generate documentation"
	@echo "  make install     - Install ruchy locally"
	@echo ""
	@echo "Language Compatibility:"
	@echo "  make compatibility - Run comprehensive language feature compatibility tests"
	@echo ""
	@echo "Publishing:"
	@echo "  make prepare-publish - Prepare for crates.io publication"
	@echo "  make pre-release-checks - Run all pre-release quality checks"
	@echo "  make release-patch - Create patch release (bug fixes)"
	@echo "  make release-minor - Create minor release (new features)"
	@echo "  make release-major - Create major release (breaking changes)"
	@echo "  make release-auto - Auto-detect version bump type"
	@echo "  make crate-release - Publish to crates.io"

# Build project
build:
	@echo "Building Ruchy..."
	@cargo build --release
	@echo "✓ Build complete"

# Execution Testing Targets
test-execution: test-cli test-oneliner test-repl-integration
	@echo "✓ All execution modes validated"

test-cli:
	@echo "Testing CLI commands..."
	@cargo test --test cli_integration 2>/dev/null || true
	@echo "✓ CLI tests complete"

test-oneliner:
	@echo "Testing one-liners..."
	@./tests/oneliner/suite.sh
	@echo "✓ One-liner tests complete"

test-repl-integration:
	@echo "Testing REPL integration..."
	@cargo test --test repl_integration 2>/dev/null || true
	@echo "✓ REPL integration tests complete"

test-properties:
	@echo "Running property-based tests..."
	@cargo test --test property_tests --features proptest
	@echo "✓ Property tests complete"

bench-execution:
	@echo "Running execution benchmarks..."
	@cargo bench --bench execution_bench
	@echo "✓ Benchmarks complete"

validate-performance:
	@echo "Validating performance targets..."
	@cargo run --release --bin validate
	@echo "✓ Performance validated"

# Run tests (default - includes property, doc, examples, and fuzz tests as key testing pathway)
test:
	@echo "Running main test suite (lib + property + doc + examples + fuzz tests)..."
	@cargo test --lib --quiet -- --test-threads=4
	@echo "Running property-based tests..."
	@cargo test property_ --lib --release --quiet -- --nocapture
	@cargo test proptest --lib --release --quiet -- --nocapture
	@cargo test quickcheck --lib --release --quiet -- --nocapture
	@cargo test --lib --features testing testing::properties --release --quiet -- --nocapture
	@echo "Running documentation tests..."
	-@cargo test --doc --quiet
	@echo "Running examples tests..."
	@$(MAKE) test-examples --quiet
	@echo "Running quick fuzz tests..."
	@$(MAKE) test-fuzz-quick --quiet
	@echo "✓ Main test suite completed (lib + property + doc + examples + fuzz tests)"

# Run tests with nextest (will recompile, but has better output)
test-nextest:
	@echo "Running tests with nextest..."
	@cargo nextest run --lib --profile quick
	@echo "✓ Nextest tests passed"

# Run all tests comprehensively (including ignored/slow tests, doc tests)
test-all:
	@echo "Running all tests comprehensively (including slow/ignored tests)..."
	@cargo test --all-features --workspace -- --include-ignored
	@cargo test --doc
	@echo "✓ All tests passed"

# Run property-based tests specifically
test-property:
	@echo "Running property-based tests..."
	@cargo test property_ --lib --release -- --nocapture
	@cargo test proptest --lib --release -- --nocapture
	@cargo test quickcheck --lib --release -- --nocapture
	@cargo test --lib --features testing testing::properties --release -- --nocapture
	@echo "✓ Property tests passed"

# Run documentation tests specifically
test-doc:
	@echo "Running documentation tests..."
	@echo "Note: Some doc tests may fail due to Ruchy syntax examples being interpreted as Rust"
	-@cargo test --doc
	@echo "✓ Documentation tests completed (some may have failed - this is expected)"

# Comprehensive REPL testing - ALL test types for REPL
test-repl:
	@echo "════════════════════════════════════════════════════════════════════"
	@echo "   COMPREHENSIVE REPL TESTING SUITE"
	@echo "════════════════════════════════════════════════════════════════════"
	@echo ""
	@echo "1️⃣  Running REPL unit tests..."
	@cargo test repl --lib --quiet || (echo "❌ REPL unit tests failed" && exit 1)
	@echo "✅ REPL unit tests passed"
	@echo ""
	@echo "2️⃣  Running REPL integration tests..."
	@cargo test --test repl_commands_test --quiet || (echo "❌ REPL integration tests failed" && exit 1)
	@cargo test --test cli_oneliner_tests --quiet || (echo "❌ CLI oneliner tests failed" && exit 1)
	@echo "✅ REPL integration tests passed"
	@echo ""
	@echo "3️⃣  Running REPL property tests..."
	@cargo test repl_function_tests::property --lib --release --quiet || (echo "❌ REPL property tests failed" && exit 1)
	@echo "✅ REPL property tests passed"
	@echo ""
	@echo "4️⃣  Running REPL doctests..."
	@cargo test --doc runtime::repl --quiet || (echo "❌ REPL doctests failed" && exit 1)
	@echo "✅ REPL doctests passed"
	@echo ""
	@echo "5️⃣  Running REPL examples..."
	@cargo run --example repl_demo --quiet || (echo "❌ REPL demo example failed" && exit 1)
	@cargo run --example debug_repl --quiet || (echo "❌ Debug REPL example failed" && exit 1)
	@echo "✅ REPL examples passed"
	@echo ""
	@echo "6️⃣  Running REPL fuzz tests (5 seconds)..."
	@cargo +nightly fuzz run repl_input -- -max_total_time=5 2>/dev/null || true
	@echo "✅ REPL fuzz test completed"
	@echo ""
	@echo "7️⃣  Generating REPL coverage report..."
	@cargo llvm-cov test repl --lib --quiet --no-report
	@cargo llvm-cov report --lib --ignore-filename-regex="tests/|benches/|examples/" 2>&1 | grep -E "src/runtime/repl" || true
	@echo ""
	@echo "════════════════════════════════════════════════════════════════════"
	@echo "   ✅ ALL REPL TESTS COMPLETED SUCCESSFULLY!"
	@echo "════════════════════════════════════════════════════════════════════"


# Run linter
lint:
	@echo "Running clippy..."
	@cargo clippy --lib --bins -- -D warnings
	@echo "✓ Linting complete"

# Run linter on all targets including tests (use with caution - test code may have warnings)
lint-all:
	@echo "Running clippy on all targets..."
	@cargo clippy --all-targets --all-features -- -D warnings
	@echo "✓ Linting complete"

# Format code
format:
	@echo "Formatting code..."
	@cargo fmt --all
	@echo "✓ Formatting complete"

# Check formatting (for CI)
format-check:
	@echo "Checking formatting..."
	@cargo fmt --all -- --check
	@echo "✓ Format check complete"

# Clean build artifacts
clean:
	@echo "Cleaning..."
	@cargo clean
	@rm -rf target/
	@rm -rf ~/.ruchy/cache/
	@echo "✓ Clean complete"

# Generate comprehensive test coverage using cargo-llvm-cov (Toyota Way)
coverage:
	@echo "🧪 Running comprehensive coverage analysis..."
	@./scripts/coverage.sh

# Quick coverage check for development workflow  
coverage-quick:
	@./scripts/quick-coverage.sh

# Open coverage report in browser
coverage-open:
	@./scripts/coverage.sh --open

# Test coverage and quality per component (parser, interpreter, repl)
test-coverage-quality:
	@echo "📊 Component Coverage & Quality Analysis"
	@echo "========================================="
	@echo ""
	@echo "🔍 Parser Component:"
	@echo "-------------------"
	@cargo llvm-cov test --lib --no-report 2>/dev/null || true
	@cargo llvm-cov report --ignore-filename-regex "(?!.*parser)" 2>/dev/null | grep -E "TOTAL|parser" | head -5 || echo "Coverage data collection in progress..."
	@echo ""
	@echo "TDG Quality Score:"
	@pmat tdg src/frontend/parser --include-components 2>/dev/null | grep -E "Overall Score|Grade" | head -2 || echo "TDG analysis pending..."
	@echo ""
	@echo "🧠 Interpreter Component:"
	@echo "------------------------"
	@cargo llvm-cov report --ignore-filename-regex "(?!.*interpreter)" 2>/dev/null | grep -E "TOTAL|interpreter" | head -5 || echo "Coverage data collection in progress..."
	@echo ""
	@echo "TDG Quality Score:"
	@pmat tdg src/runtime/interpreter.rs --include-components 2>/dev/null | grep -E "Overall Score|Grade" | head -2 || echo "TDG analysis pending..."
	@echo ""
	@echo "💻 REPL Component:"
	@echo "-----------------"
	@cargo llvm-cov report --ignore-filename-regex "(?!.*repl)" 2>/dev/null | grep -E "TOTAL|repl" | head -5 || echo "Coverage data collection in progress..."
	@echo ""
	@echo "TDG Quality Score:"
	@pmat tdg src/runtime/repl.rs --include-components 2>/dev/null | grep -E "Overall Score|Grade" | head -2 || echo "TDG analysis pending..."
	@echo ""
	@echo "🎯 Target Goals:"
	@echo "---------------"
	@echo "• Parser: 80% coverage, TDG A grade (≥90)"
	@echo "• Interpreter: 70% coverage, TDG B+ grade (≥85)"
	@echo "• REPL: 60% coverage, TDG B grade (≥80)"
	@echo ""
	@echo "Run 'make coverage' for detailed report"

# Legacy coverage for CI compatibility
coverage-legacy:
	@echo "Generating coverage report with cargo-llvm-cov..."
	@cargo install cargo-llvm-cov 2>/dev/null || true
	@cargo llvm-cov clean --workspace
	@cargo llvm-cov --all-features --workspace --html --output-dir target/coverage/html --ignore-filename-regex "tests/|benches/|examples/"
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info
	@echo "✓ Coverage report generated in target/coverage/html/index.html"
	@echo "✓ LCOV report generated in target/coverage/lcov.info"
	@echo "Coverage summary:"
	@cargo llvm-cov report --summary-only 2>&1 | tail -1

# Generate coverage with tarpaulin (alternative)
coverage-tarpaulin:
	@echo "Generating coverage report with tarpaulin..."
	@cargo install cargo-tarpaulin 2>/dev/null || true
	@cargo tarpaulin --config tarpaulin.toml
	@echo "✓ Coverage report generated in target/coverage/"

# CI coverage check with minimum threshold
coverage-ci:
	@echo "Running coverage check for CI (80% minimum)..."
	@cargo tarpaulin --fail-under 80 --print-summary

# CLI Testing Infrastructure (SPEC-CLI-TEST-001)
test-ruchy-commands: test-cli-integration test-cli-properties test-cli-fuzz test-cli-examples
	@echo "🎯 All CLI command testing complete!"

# Integration tests for CLI commands
test-cli-integration:
	@echo "🧪 Running CLI integration tests..."
	@cargo test --test cli_integration -- --test-threads=4
	@echo "✅ CLI integration tests complete"

# Property-based tests for CLI commands
test-cli-properties:
	@echo "🔬 Running CLI property tests..."
	@cargo test --test cli_properties -- --test-threads=4
	@echo "✅ CLI property tests complete"

# Fuzz testing for CLI commands  
test-cli-fuzz:
	@echo "🎲 Running CLI fuzz tests..."
	@if command -v cargo-fuzz >/dev/null 2>&1; then \
		for target in fmt check lint; do \
			echo "Fuzzing $$target for 30s..."; \
			timeout 30s cargo fuzz run fuzz_$$target || echo "Fuzz $$target completed"; \
		done; \
	else \
		echo "⚠️  cargo-fuzz not installed, skipping fuzz tests"; \
	fi
	@echo "✅ CLI fuzz tests complete"

# CLI command examples
test-cli-examples:
	@echo "📋 Running CLI command examples..."
	@for example in examples/cli/*.rs; do \
		if [ -f "$$example" ]; then \
			echo "Running $$example..."; \
			cargo run --example $$(basename $$example .rs) --quiet || echo "Example failed"; \
		fi; \
	done
	@echo "✅ CLI examples complete"

# CLI command coverage reporting
test-cli-coverage:
	@echo "📊 Running comprehensive CLI coverage analysis..."
	@./scripts/cli_coverage.sh

# CLI performance benchmarking
test-cli-performance:
	@echo "⚡ Benchmarking CLI command performance..."
	@if command -v hyperfine >/dev/null 2>&1; then \
		hyperfine --warmup 2 --runs 5 'make test-ruchy-commands' --export-markdown target/cli-performance.md; \
		echo "✅ Performance report saved to target/cli-performance.md"; \
	else \
		echo "⚠️  hyperfine not installed, install with: cargo install hyperfine"; \
	fi

# Run all examples
examples:
	@echo "Running examples..."
	@echo ""
	@echo "=== Parser Demo ==="
	@cargo run --example parser_demo --quiet
	@echo ""
	@echo "=== Transpiler Demo ==="
	@cargo run --example transpiler_demo --quiet
	@echo ""
	@echo "✓ All examples complete"

# Run example scripts
example-scripts:
	@echo "Testing Ruchy scripts..."
	@cargo run --bin ruchy -- transpile examples/fibonacci.ruchy
	@cargo run --bin ruchy -- transpile examples/marco_polo.ruchy
	@echo "✓ Script examples complete"

# Run benchmarks
bench:
	@echo "Running benchmarks..."
	@cargo bench --workspace
	@echo "✓ Benchmarks complete"

# Run snapshot tests
test-snapshot:
	@echo "Running snapshot tests..."
	@cargo test snapshot_ --lib -- --nocapture
	@echo "✓ Snapshot tests complete"

# Run mutation tests
test-mutation:
	@echo "Running mutation tests with cargo-mutants..."
	@cargo install cargo-mutants 2>/dev/null || true
	@cargo mutants --timeout 30 --jobs 4
	@echo "✓ Mutation tests complete"

# Run fuzz tests with comprehensive coverage
test-fuzz:
	@echo "Running comprehensive fuzz tests..."
	@echo ""
	@echo "1️⃣  Installing cargo-fuzz if needed..."
	@cargo +nightly install cargo-fuzz 2>/dev/null || echo "  ✅ cargo-fuzz already installed"
	@echo ""
	@echo "2️⃣  Fuzz testing parser (20 seconds)..."
	@cargo +nightly fuzz run parser -- -max_total_time=20 2>/dev/null || echo "  ⚠️  Parser fuzz completed with potential issues"
	@echo "✅ Parser fuzz testing completed"
	@echo ""
	@echo "3️⃣  Fuzz testing transpiler (20 seconds)..."
	@cargo +nightly fuzz run transpiler -- -max_total_time=20 2>/dev/null || echo "  ⚠️  Transpiler fuzz completed with potential issues"
	@echo "✅ Transpiler fuzz testing completed"
	@echo ""
	@echo "4️⃣  Fuzz testing REPL input handling (15 seconds)..."
	@cargo +nightly fuzz run repl_input -- -max_total_time=15 2>/dev/null || echo "  ⚠️  REPL fuzz completed with potential issues"
	@echo "✅ REPL fuzz testing completed"
	@echo ""
	@echo "5️⃣  Fuzz testing full pipeline (10 seconds)..."
	@cargo +nightly fuzz run full_pipeline -- -max_total_time=10 2>/dev/null || echo "  ⚠️  Full pipeline fuzz completed with potential issues"
	@echo "✅ Full pipeline fuzz testing completed"
	@echo ""
	@echo "✅ All fuzz tests completed successfully!"

# Quick fuzz tests (for integration into main test suite)
test-fuzz-quick:
	@echo "Running quick fuzz tests (5 seconds total)..."
	@cargo +nightly install cargo-fuzz 2>/dev/null || true
	@cargo +nightly fuzz run parser -- -max_total_time=2 2>/dev/null || true
	@cargo +nightly fuzz run transpiler -- -max_total_time=2 2>/dev/null || true
	@cargo +nightly fuzz run repl_input -- -max_total_time=1 2>/dev/null || true
	@echo "✅ Quick fuzz tests completed"

# Test all examples (Rust examples + Ruchy scripts)
test-examples:
	@echo "Running all examples tests..."
	@echo ""
	@echo "1️⃣  Running Rust examples..."
	@cargo run --example parser_demo --quiet
	@cargo run --example transpiler_demo --quiet
	@echo "✅ Rust examples passed"
	@echo ""
	@echo "2️⃣  Running Ruchy script transpilation tests..."
	@cargo run --bin ruchy -- transpile examples/fibonacci.ruchy > /dev/null
	@cargo run --bin ruchy -- transpile examples/marco_polo.ruchy > /dev/null
	@echo "✅ Ruchy script transpilation passed"
	@echo ""
	@echo "3️⃣  Running working Ruchy script execution tests..."
	@echo "Testing fibonacci.ruchy..."
	@echo 'fibonacci(10)' | cargo run --bin ruchy -- run examples/fibonacci.ruchy > /dev/null 2>&1 || true
	@echo "Testing marco_polo.ruchy..."
	@echo '' | cargo run --bin ruchy -- run examples/marco_polo.ruchy > /dev/null 2>&1 || true
	@echo "✅ Working Ruchy scripts tested"
	@echo ""
	@echo "4️⃣  Checking problematic examples (expected to fail)..."
	@echo "Note: Some .ruchy files may fail due to unsupported syntax (comments, features)"
	@for example in examples/*.ruchy; do \
		case "$$example" in \
			*fibonacci*|*marco_polo.ruchy) ;; \
			*) echo "Checking $$example (may fail - expected)..."; \
			   cargo run --bin ruchy -- run $$example 2>/dev/null || echo "  ⚠️  Failed as expected (unsupported syntax)"; ;; \
		esac \
	done
	@echo ""
	@echo "✅ All examples testing completed"

# Binary validation tests (legacy - kept for compatibility)
test-binary:
	@echo "Running binary validation tests..."
	@for example in examples/*.ruchy; do \
		echo "Testing $$example..."; \
		cargo run --bin ruchy -- run $$example || exit 1; \
	done
	@echo "✓ Binary validation complete"

# Generate documentation
doc:
	@echo "Generating documentation..."
	@cargo doc --no-deps --workspace --all-features
	@echo "✓ Documentation generated in target/doc"

# Install locally
install:
	@echo "Installing ruchy..."
	@cargo install --path ruchy-cli --force
	@echo "✓ Ruchy installed to ~/.cargo/bin/ruchy"

# Run PMAT quality gates
quality-gate:
	@echo "Running PMAT quality checks..."
	@~/.local/bin/pmat quality-gate || true
	@echo "Checking complexity..."
	@~/.local/bin/pmat analyze --metrics complexity src/ || true
	@echo "✓ Quality check complete"

# TDG Dashboard Management
tdg-dashboard:
	@echo "🚀 Starting TDG Real-Time Dashboard..."
	@./scripts/tdg_dashboard.sh start --open

tdg-stop:
	@echo "🛑 Stopping TDG Dashboard..."
	@./scripts/tdg_dashboard.sh stop

tdg-status:
	@echo "📊 TDG Dashboard Status:"
	@./scripts/tdg_dashboard.sh status

tdg-restart:
	@echo "🔄 Restarting TDG Dashboard..."
	@./scripts/tdg_dashboard.sh restart

# CI pipeline
ci: format-check lint test-all coverage quality-gate
	@echo "✓ CI pipeline complete"

# Prepare for crates.io publication
prepare-publish:
	@echo "Preparing for crates.io publication..."
	@echo "Checking package metadata..."
	@cargo publish --dry-run --package ruchy
	@cargo publish --dry-run --package ruchy-cli
	@echo ""
	@echo "Checklist for publication:"
	@echo "  [ ] Version numbers updated in Cargo.toml"
	@echo "  [ ] CHANGELOG.md updated"
	@echo "  [ ] README.md complete with examples"
	@echo "  [ ] Documentation complete"
	@echo "  [ ] All tests passing"
	@echo "  [ ] Coverage > 80%"
	@echo "  [ ] No clippy warnings"
	@echo "  [ ] PMAT quality gates passing"
	@echo ""
	@echo "To publish:"
	@echo "  cargo publish --package ruchy"
	@echo "  cargo publish --package ruchy-cli"

# Documentation enforcement targets
.PHONY: check-docs commit sprint-close

# Ensure documentation is current
check-docs:
	@echo "📋 Checking documentation currency..."
	@if [ $$(git diff --name-only | grep -cE '\.(rs|ruchy)$$') -gt 0 ] && \
	    [ $$(git diff --name-only | grep -cE 'docs/|CHANGELOG.md') -eq 0 ]; then \
	    echo "❌ Documentation update required!"; \
	    echo "Update one of:"; \
	    echo "  - docs/execution/roadmap.md"; \
	    echo "  - docs/execution/quality-gates.md"; \
	    echo "  - CHANGELOG.md"; \
	    exit 1; \
	fi

# Development workflow with quality checks
dev: check-docs format lint test
	@echo "✅ Ready for development"

# Quality-enforced commit
commit: check-docs lint
	@echo "📝 Creating quality-enforced commit..."
	@read -p "Task ID (RUCHY-XXXX): " task_id; \
	read -p "Commit message: " msg; \
	git add -A && \
	git commit -m "$$task_id: $$msg"

# Sprint close verification
sprint-close: check-docs
	@echo "🏁 Sprint Close Quality Gate"
	@if command -v pmat >/dev/null 2>&1; then \
	    pmat quality-gate --fail-on-violation; \
	    echo "📊 Generating quality report..."; \
	    pmat analyze complexity . --format markdown > docs/quality/sprint-report.md; \
	fi
	@echo "✅ Sprint ready for close"

# Test optimization commands
.PHONY: test-quick test-memory test-heavy find-heavy-tests

# Quick smoke tests only
test-quick:
	@echo "Running quick smoke tests..."
	@PROPTEST_CASES=5 cargo test --lib -- --test-threads=2 --skip property_
	@echo "✓ Quick tests complete"

# Test memory usage
test-memory:
	@echo "Running resource verification tests..."
	@cargo test --test resource_check -- --test-threads=1
	@echo "✓ Memory tests complete"

# Run heavy tests (normally ignored)
test-heavy:
	@echo "Running heavy tests (this may take a while)..."
	@cargo test -- --ignored --test-threads=1 --nocapture
	@echo "✓ Heavy tests complete"

# Find memory-intensive tests
find-heavy-tests:
	@echo "Identifying memory-intensive tests..."
	@./scripts/find-heavy-tests.sh

# Full validation
all: clean build test-all lint format coverage examples bench doc quality-gate
	@echo "✓ Full validation complete"

# ============================================================================
# RELEASE MANAGEMENT - Based on paiml-mcp-agent-toolkit patterns
# ============================================================================

.PHONY: install-release-tools pre-release-checks release-patch release-minor release-major release-auto release-dry crate-release release-verify

# Install required release tools
install-release-tools:
	@echo "📦 Installing release tools..."
	@cargo install cargo-release --locked 2>/dev/null || echo "cargo-release already installed"
	@cargo install cargo-semver-checks --locked 2>/dev/null || echo "cargo-semver-checks already installed"
	@cargo install cargo-audit --locked 2>/dev/null || echo "cargo-audit already installed"
	@cargo install cargo-outdated --locked 2>/dev/null || echo "cargo-outdated already installed"
	@echo "✅ Release tools installed"

# Pre-release quality gates
pre-release-checks:
	@echo "🔍 Running pre-release checks..."
	@echo ""
	@echo "1️⃣ Version consistency check..."
	@MAIN_VERSION=$$(grep -m1 '^version = ' Cargo.toml | cut -d'"' -f2); \
	CLI_VERSION=$$(grep -m1 '^version = ' ruchy-cli/Cargo.toml | cut -d'"' -f2 || echo $$MAIN_VERSION); \
	if [ "$$MAIN_VERSION" != "$$CLI_VERSION" ] && [ -n "$$CLI_VERSION" ]; then \
		echo "❌ Version mismatch: ruchy=$$MAIN_VERSION, ruchy-cli=$$CLI_VERSION"; \
		exit 1; \
	fi; \
	echo "✅ Versions consistent: $$MAIN_VERSION"
	@echo ""
	@echo "2️⃣ Running tests..."
	@$(MAKE) test-all
	@echo ""
	@echo "3️⃣ Checking formatting and lints..."
	@$(MAKE) format-check
	@$(MAKE) lint
	@echo ""
	@echo "4️⃣ Security audit..."
	@cargo audit || echo "⚠️  Some vulnerabilities found (review before release)"
	@echo ""
	@echo "5️⃣ Checking outdated dependencies..."
	@cargo outdated || echo "⚠️  Some dependencies outdated (review before release)"
	@echo ""
	@echo "6️⃣ Documentation check..."
	@cargo doc --no-deps --workspace --all-features --quiet
	@echo "✅ Documentation builds successfully"
	@echo ""
	@echo "7️⃣ Dry-run publish check..."
	@cargo publish --dry-run --package ruchy --quiet
	@echo "✅ Package ruchy ready for publication"
	@cargo publish --dry-run --quiet 2>/dev/null || echo "⚠️  ruchy-cli may need separate publication"
	@echo ""
	@echo "✅ All pre-release checks completed!"

# Patch release (x.y.Z) - bug fixes only
release-patch: install-release-tools pre-release-checks
	@echo "🔖 Creating PATCH release (bug fixes only)..."
	@cargo release patch --execute --no-confirm

# Minor release (x.Y.z) - new features, backward compatible
release-minor: install-release-tools pre-release-checks
	@echo "🔖 Creating MINOR release (new features, backward compatible)..."
	@cargo release minor --execute --no-confirm

# Major release (X.y.z) - breaking changes
release-major: install-release-tools pre-release-checks
	@echo "🔖 Creating MAJOR release (breaking changes)..."
	@cargo release major --execute --no-confirm

# Auto-determine version bump based on conventional commits
release-auto: install-release-tools pre-release-checks
	@echo "🤖 Auto-determining version bump type..."
	@if git log --oneline $$(git describe --tags --abbrev=0 2>/dev/null || echo HEAD~10)..HEAD | grep -qE '^[a-f0-9]+ (feat!|fix!|refactor!|BREAKING)'; then \
		echo "💥 Breaking changes detected - MAJOR release"; \
		$(MAKE) release-major; \
	elif git log --oneline $$(git describe --tags --abbrev=0 2>/dev/null || echo HEAD~10)..HEAD | grep -qE '^[a-f0-9]+ feat:'; then \
		echo "✨ New features detected - MINOR release"; \
		$(MAKE) release-minor; \
	else \
		echo "🐛 Bug fixes/patches only - PATCH release"; \
		$(MAKE) release-patch; \
	fi

# Dry run for release (no actual changes)
release-dry:
	@echo "🧪 Dry run for release..."
	@cargo release patch --dry-run

# Publish to crates.io (interactive)
crate-release:
	@echo "📦 Publishing to crates.io..."
	@echo "Current version: $$(grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)"
	@echo ""
	@echo "Pre-publish checklist:"
	@echo "  ✓ Version bumped in Cargo.toml"
	@echo "  ✓ CHANGELOG.md updated"
	@echo "  ✓ All tests passing"
	@echo "  ✓ Documentation builds"
	@echo ""
	@printf "Continue with publish? [y/N] "; \
	read REPLY; \
	case "$$REPLY" in \
		[yY]*) \
			echo "Publishing ruchy..."; \
			cargo publish --package ruchy; \
			echo "Waiting 30 seconds for crates.io to index..."; \
			sleep 30; \
			echo "Publishing ruchy-cli..."; \
			cargo publish || echo "ruchy-cli may already be published or needs manual intervention"; \
			;; \
		*) echo "❌ Publish cancelled" ;; \
	esac

# Verify release was successful
release-verify:
	@echo "🔍 Verifying release..."
	@LATEST_TAG=$$(git describe --tags --abbrev=0); \
	echo "Latest tag: $$LATEST_TAG"; \
	CRATE_VERSION=$$(cargo search ruchy | head -1 | cut -d'"' -f2); \
	echo "Crates.io version: $$CRATE_VERSION"; \
	echo ""; \
	echo "📦 Testing installation from crates.io..."; \
	cargo install ruchy --force && ruchy --version; \
	echo "✅ Release verification complete!"

# Run comprehensive language feature compatibility tests
compatibility:
	@echo "🔍 RUCHY LANGUAGE COMPATIBILITY TEST SUITE"
	@echo $$(printf '=%.0s' $$(seq 1 60))
	@echo ""
	@echo "Running comprehensive compatibility tests based on:"
	@echo "  • Rust, Python, Elixir, Ruby, SQLite, Haskell, JS/Deno best practices"
	@echo "  • Performance regression detection (SQLite standard)"
	@echo "  • Property-based testing (Haskell QuickCheck style)"
	@echo ""
	@cargo test compatibility_report --test compatibility_suite -- --nocapture --ignored
	@echo ""
	@echo "✅ Language compatibility verification complete!"
	@echo "📊 Use results to prioritize development for maximum compatibility improvement"