realizar 0.6.2

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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
# Realizar Makefile
# Pure Rust ML Library - Model Serving, MLOps, LLMOps
# Quality: EXTREME TDD, 95%+ coverage, zero tolerance for defects

.SUFFIXES:
.DELETE_ON_ERROR:
.PHONY: help build test test-fast lint quality-gates deploy clean
.PHONY: tier1 tier2 test-cuda-fast test-probar
.PHONY: coverage coverage-open coverage-clean clean-coverage coverage-summary
.PHONY: mutants mutants-quick mutants-quantize mutants-layers mutants-tokenizer
.PHONY: mutants-generate mutants-report mutants-clean mutation-file mutate mutate-fast
.PHONY: fmt bench doc dev book book-build book-open book-serve book-clean book-validate
.PHONY: bench-inference-all bench-pytorch-inference bench-cpu-inference bench-wgpu
.PHONY: bench-gguf-gpu-inference bench-apr-gpu-inference bench-server-matrix
.DEFAULT_GOAL := help

# Color output
RED := \033[0;31m
GREEN := \033[0;32m
YELLOW := \033[0;33m
NC := \033[0m

help: ## Show this help message
	@echo "Realizar - Pure Rust ML Library"
	@echo "================================"
	@echo ""
	@echo "Available targets:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  $(GREEN)%-20s$(NC) %s\n", $$1, $$2}'

# === Build Targets ===

build: ## Build the project in release mode
	@echo "$(GREEN)Building Realizar...$(NC)"
	cargo build --release

build-dev: ## Build the project in development mode
	@echo "$(GREEN)Building Realizar (dev)...$(NC)"
	cargo build

build-all-features: ## Build with all features enabled
	@echo "$(GREEN)Building Realizar (all features)...$(NC)"
	cargo build --all-features --release

# === Tiered Testing (trueno-style) ===

tier1: ## Tier 1: Sub-second feedback for rapid iteration (ON-SAVE)
	@echo "🚀 TIER 1: Sub-second feedback"
	@cargo check --lib --quiet
	@cargo clippy --lib --quiet -- -D warnings
	@echo "✅ Tier 1 complete"

tier2: ## Tier 2: Fast tests before commit (30s target)
	@echo "🔍 TIER 2: Fast tests"
	@cargo fmt -- --check
	@cargo clippy --lib --quiet -- -D warnings
	@cargo test --lib --quiet
	@echo "✅ Tier 2 complete"

test-cuda-fast: ## Fast CUDA tests only (probar TUI simulation, debug mode)
	@echo "⚡ Running fast CUDA tests..."
	@cargo test --test probar_tui_simulation --features cuda -- --nocapture
	@echo "✅ CUDA tests passed"

test-probar: ## Run all probar visual tests
	@echo "🎯 Running probar visual tests..."
	@cargo test --test 'probar_*' --features cuda -- --nocapture
	@echo "✅ Probar tests passed"

# === Test Targets ===

test: ## Run all tests (excludes load tests - use 'make load-test' for those)
	@echo "$(GREEN)Running tests (-j2 to prevent OOM)...$(NC)"
	@# Exclude load-test-enabled (requires running server)
	cargo test --features "server,cli,gpu" -- --test-threads=2

test-lib: ## Run library tests only (fast)
	@echo "$(GREEN)Running library tests...$(NC)"
	cargo test --lib

test-unit: ## Run unit tests only
	@echo "$(GREEN)Running unit tests...$(NC)"
	cargo test --lib --bins

test-integration: ## Run integration tests only
	@echo "$(GREEN)Running integration tests...$(NC)"
	cargo test --test '*'

test-property: ## Run property-based tests (proptest)
	@echo "$(GREEN)Running property-based tests...$(NC)"
	cargo test --test property_*

load-test: ## Run HTTP API load tests (requires running server)
	@echo "$(GREEN)Running load tests...$(NC)"
	@./scripts/load_test.sh

load-test-no-server: ## Run load tests against existing server
	@echo "$(GREEN)Running load tests (no server start)...$(NC)"
	@./scripts/load_test.sh --no-server

# === Standard Batuta Stack Targets ===

lint: ## Run all linting with auto-format (Batuta stack standard)
	@echo "$(GREEN)🔧 Auto-formatting code...$(NC)"
	@cargo fmt
	@echo "$(GREEN)🔍 Running clippy with auto-fix...$(NC)"
	@cargo clippy --all-targets --all-features --fix --allow-dirty --allow-staged 2>/dev/null || true
	@echo "$(GREEN)🔍 Running clippy (zero warnings policy)...$(NC)"
	@cargo clippy --all-targets --all-features -- -D warnings
	@echo "$(GREEN)✅ Lint passed!$(NC)"

test-fast: ## Fast unit tests (<5min target, low memory - excludes 29k lines of gguf tests)
	@echo "$(GREEN)⚡ Running fast tests (excludes heavy-tests feature)...$(NC)"
	time env PROPTEST_CASES=25 cargo test --lib
	@echo "$(GREEN)✅ Fast tests passed$(NC)"

test-full: ## Full test suite (requires 16GB+ RAM for gguf tests)
	@echo "$(GREEN)🧪 Running full tests (includes heavy-tests)...$(NC)"
	time env PROPTEST_CASES=25 cargo test --lib --features heavy-tests
	@echo "$(GREEN)✅ Full tests passed$(NC)"

# === Quality Gates ===

quality-gates: fmt-check clippy test coverage bashrs-check book-build book-validate ## Run all quality gates (pre-commit)
	@echo "$(GREEN)✅ All quality gates passed!$(NC)"

fmt: ## Format code
	@echo "$(GREEN)Formatting code...$(NC)"
	cargo fmt

fmt-check: ## Check code formatting
	@echo "$(GREEN)Checking code formatting...$(NC)"
	cargo fmt --check || (echo "$(RED)❌ Format check failed. Run 'make fmt'$(NC)" && exit 1)

clippy: ## Run clippy lints (zero warnings policy)
	@echo "$(GREEN)Running clippy...$(NC)"
	cargo clippy --all-targets --all-features -- -D warnings

clippy-fix: ## Automatically fix clippy warnings
	@echo "$(GREEN)Fixing clippy warnings...$(NC)"
	cargo clippy --all-targets --all-features --fix

# === Coverage (Memory-efficient: cargo test, not nextest) ===

coverage: ## Generate HTML coverage report (target: >95%, memory-efficient)
	@echo "$(GREEN)📊 Running coverage analysis (target: >95%)...$(NC)"
	@echo "   - Uses 'cargo test' (1 profraw/binary) NOT 'nextest' (1 profraw/test)"
	@echo "   - This reduces thousands of profraw files to ~5 = low memory"
	@which cargo-llvm-cov > /dev/null 2>&1 || (echo "$(YELLOW)📦 Installing cargo-llvm-cov...$(NC)" && cargo install cargo-llvm-cov --locked)
	@mkdir -p target/coverage
	@cargo llvm-cov clean --workspace
	@echo "$(GREEN)🧪 Running tests with instrumentation...$(NC)"
	@# Memory-safe settings (ruchy-style):
	@# - PROPTEST_CASES=2: Minimal property test iterations
	@# - cargo test: 1 profraw per binary (not per test like nextest)
	@# - --test-threads=4: Limit parallel test execution
	@# - --lib only: Skip integration tests for faster coverage
	@# - RUSTFLAGS="": Disable mold linker (breaks LLVM instrumentation)
	@env RUSTFLAGS="" CARGO_BUILD_RUSTFLAGS="" PROPTEST_CASES=2 QUICKCHECK_TESTS=2 \
		cargo llvm-cov test --lib --no-report \
		-- --test-threads=4 2>&1 | tail -30
	@echo "$(GREEN)📊 Generating reports...$(NC)"
	@cargo llvm-cov report --html --output-dir target/coverage/html
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info
	@echo ""
	@cargo llvm-cov report --summary-only
	@echo "$(GREEN)✅ Coverage report: target/coverage/html/index.html$(NC)"

coverage-full: ## Full coverage with all features (requires 16GB+ RAM)
	@echo "$(GREEN)📊 Running FULL coverage analysis (high memory)...$(NC)"
	@which cargo-llvm-cov > /dev/null 2>&1 || (echo "$(YELLOW)📦 Installing cargo-llvm-cov...$(NC)" && cargo install cargo-llvm-cov --locked)
	@which cargo-nextest > /dev/null 2>&1 || (echo "$(YELLOW)📦 Installing cargo-nextest...$(NC)" && cargo install cargo-nextest --locked)
	@mkdir -p target/coverage
	@cargo llvm-cov clean --workspace
	@# Full coverage with nextest (memory-intensive)
	@env RUSTFLAGS="" CARGO_BUILD_RUSTFLAGS="" PROPTEST_CASES=25 QUICKCHECK_TESTS=25 \
		cargo llvm-cov --no-report nextest -j 2 --lib --tests --no-tests=warn --workspace --no-fail-fast --features "server,cli,gpu"
	@cargo llvm-cov report --html --output-dir target/coverage/html
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info
	@cargo llvm-cov report --summary-only
	@echo "$(GREEN)✅ Full coverage report: target/coverage/html/index.html$(NC)"

coverage-summary: ## Show coverage summary
	@cargo llvm-cov report --summary-only 2>/dev/null || echo "Run 'make coverage' first"

coverage-open: ## Open HTML coverage report in browser
	@if [ -f target/coverage/html/index.html ]; then \
		xdg-open target/coverage/html/index.html 2>/dev/null || \
		open target/coverage/html/index.html 2>/dev/null || \
		echo "Please open: target/coverage/html/index.html"; \
	else \
		echo "$(RED)❌ Run 'make coverage' first to generate the HTML report$(NC)"; \
	fi

coverage-clean: ## Clean coverage artifacts
	@rm -f lcov.info coverage.xml
	@rm -rf target/llvm-cov target/coverage
	@find . -name "*.profraw" -delete 2>/dev/null || true
	@echo "$(GREEN)✓ Coverage artifacts cleaned$(NC)"

clean-coverage: coverage-clean ## Alias for coverage-clean (fresh start)
	@echo "$(GREEN)✓ Fresh coverage ready (run 'make coverage' to regenerate)$(NC)"

# === Mutation Testing (Toyota Way: Automated) ===

mutants: ## Run full mutation testing analysis
	@echo "$(GREEN)🧬 Running full mutation testing analysis...$(NC)"
	@echo "🧪 Running mutation tests on realizar package..."
	@cargo mutants --no-times --timeout 300 || true
	@echo ""
	@echo "$(GREEN)📊 Mutation testing complete. Review mutants.out/ for detailed results.$(NC)"

mutants-quick: ## Run mutation testing on recently changed files only
	@echo "$(GREEN)🧬 Running quick mutation testing (recently changed files)...$(NC)"
	@cargo mutants --no-times --in-diff HEAD~5..HEAD || true
	@echo "$(GREEN)📊 Quick mutation testing complete.$(NC)"

mutants-quantize: ## Run mutation testing on quantize module only
	@echo "$(GREEN)🧬 Running mutation testing on quantize module...$(NC)"
	@cargo mutants --file 'src/quantize.rs' --no-times || true
	@echo "$(GREEN)📊 Quantize mutation testing complete.$(NC)"

mutants-layers: ## Run mutation testing on layers module
	@echo "$(GREEN)🧬 Running mutation testing on layers module...$(NC)"
	@cargo mutants --file 'src/layers.rs' --no-times || true
	@echo "$(GREEN)📊 Layers mutation testing complete.$(NC)"

mutants-tokenizer: ## Run mutation testing on tokenizer module
	@echo "$(GREEN)🧬 Running mutation testing on tokenizer module...$(NC)"
	@cargo mutants --file 'src/tokenizer.rs' --no-times || true
	@echo "$(GREEN)📊 Tokenizer mutation testing complete.$(NC)"

mutants-generate: ## Run mutation testing on generate module
	@echo "$(GREEN)🧬 Running mutation testing on generate module...$(NC)"
	@cargo mutants --file 'src/generate.rs' --no-times || true
	@echo "$(GREEN)📊 Generate mutation testing complete.$(NC)"

mutants-report: ## Generate mutation testing report
	@echo "$(GREEN)📊 Generating mutation testing report...$(NC)"
	@if [ -f mutants.out/mutants.json ]; then \
		echo "=== Mutation Testing Summary ==="; \
		echo ""; \
		jq -r '.summary // empty' mutants.out/mutants.json 2>/dev/null || cat mutants.out/mutants.json | head -50; \
		echo ""; \
		echo "📄 Full report: mutants.out/mutants.json"; \
		echo "📋 Detailed logs: mutants.out/"; \
	else \
		echo "$(RED)❌ No mutation results found. Run 'make mutants' first.$(NC)"; \
	fi

mutants-clean: ## Clean mutation testing artifacts
	@rm -rf mutants.out mutants.out.old
	@echo "$(GREEN)✓ Mutation testing artifacts cleaned$(NC)"

mutation-file: ## Run mutation testing on a single file (FILE=path/to/file.rs)
	@echo "$(GREEN)🧬 Running targeted mutation testing...$(NC)"
	@if [ -z "$(FILE)" ]; then \
		echo "$(RED)❌ Error: FILE parameter required$(NC)"; \
		echo "Usage: make mutation-file FILE=src/path/to/file.rs"; \
		exit 1; \
	fi
	@if [ ! -f "$(FILE)" ]; then \
		echo "$(RED)❌ Error: File not found: $(FILE)$(NC)"; \
		exit 1; \
	fi
	@echo "  Target: $(FILE)"
	@cargo mutants --file '$(FILE)' --no-times || true
	@echo "$(GREEN)📊 Mutation testing complete for $(FILE)$(NC)"
	@echo "💡 View results: mutants.out/mutants.json"

# Legacy aliases for backwards compatibility
mutate: mutants ## Alias for mutants (backwards compatibility)
mutate-fast: mutants-quick ## Alias for mutants-quick (backwards compatibility)

# === Benchmarking ===

bench: ## Run all benchmarks
	@echo "$(GREEN)Running benchmarks...$(NC)"
	cargo bench

bench-tensor: ## Run tensor operation benchmarks
	@echo "$(GREEN)Running tensor benchmarks...$(NC)"
	cargo bench --bench tensor_ops

bench-comparative: ## Run comparative benchmarks (PyTorch vs Realizar)
	@echo "$(GREEN)Running comparative benchmarks...$(NC)"
	@echo "Step 1: Running Realizar benchmarks..."
	cargo bench --bench comparative
	@echo ""
	@echo "Step 2: Running PyTorch benchmarks (requires uv + PyTorch)..."
	@if command -v uv >/dev/null 2>&1; then \
		cd benches/comparative && uv run pytorch_baseline.py --all --output pytorch_results.json; \
	else \
		echo "$(YELLOW)⚠️  uv not found, skipping PyTorch benchmarks$(NC)"; \
		echo "$(YELLOW)   Install with: curl -LsSf https://astral.sh/uv/install.sh | sh$(NC)"; \
	fi
	@echo ""
	@echo "Step 3: Generating comparison report..."
	@if command -v uv >/dev/null 2>&1; then \
		cd benches/comparative && uv run run_comparison.py --output comparison_report.md; \
	fi
	@echo "$(GREEN)✅ Comparative benchmarks complete!$(NC)"

# === KISS Inference Benchmarks (Refs PERF-PARITY-001) ===

bench-inference-all: ## Run ALL inference benchmarks (master target)
	@echo "$(GREEN)╔════════════════════════════════════════════════════════════════╗$(NC)"
	@echo "$(GREEN)║          Running Complete Inference Benchmark Suite            ║$(NC)"
	@echo "$(GREEN)╚════════════════════════════════════════════════════════════════╝$(NC)"
	@$(MAKE) bench-pytorch-inference
	@$(MAKE) bench-cpu-inference
	@$(MAKE) bench-wgpu
	@$(MAKE) bench-gguf-gpu-inference
	@$(MAKE) bench-apr-gpu-inference
	@echo "$(GREEN)✅ All inference benchmarks complete$(NC)"

bench-pytorch-inference: ## PyTorch vs APR MNIST benchmark
	@echo "$(GREEN)Running PyTorch vs APR MNIST comparison...$(NC)"
	cargo bench --bench apr_real
	@if command -v uv >/dev/null 2>&1; then \
		echo "Running PyTorch baseline..."; \
		cd benches/comparative && uv run pytorch_baseline.py --mnist 2>/dev/null || true; \
	else \
		echo "$(YELLOW)⚠️  uv not found, skipping PyTorch comparison$(NC)"; \
	fi
	@echo "$(GREEN)✅ PyTorch vs APR benchmark complete$(NC)"

bench-cpu-inference: ## All inference servers on CPU only
	@echo "$(GREEN)Running CPU-only inference benchmarks...$(NC)"
	cargo bench --bench gguf_real
	@if [ -f scripts/bench-cpu-matrix.sh ]; then \
		./scripts/bench-cpu-matrix.sh; \
	else \
		echo "$(YELLOW)⚠️  scripts/bench-cpu-matrix.sh not found$(NC)"; \
	fi
	@echo "$(GREEN)✅ CPU inference benchmark complete$(NC)"

bench-wgpu: ## WGPU backend benchmark (no-op if unavailable)
	@echo "$(GREEN)Running WGPU inference benchmarks...$(NC)"
	@if cargo build --features gpu --quiet 2>/dev/null; then \
		echo "WGPU available, running GPU benchmarks..."; \
		cargo bench --bench gguf_real --features gpu 2>/dev/null || \
			echo "$(YELLOW)⚠️  WGPU benchmark failed, GPU may not be available$(NC)"; \
	else \
		echo "$(YELLOW)⚠️  WGPU not available (GPU feature not compilable), skipping$(NC)"; \
	fi

bench-gguf-gpu-inference: ## GGUF GPU inference: realizar/ollama/llama.cpp
	@echo "$(GREEN)Running GGUF GPU inference matrix...$(NC)"
	@if [ -f scripts/bench-gguf-gpu-matrix.sh ]; then \
		./scripts/bench-gguf-gpu-matrix.sh; \
	else \
		echo "Running external matrix benchmark..."; \
		cargo bench --bench external_matrix --features bench-http 2>/dev/null || \
			echo "$(YELLOW)⚠️  External matrix benchmark requires bench-http feature$(NC)"; \
	fi
	@echo "$(GREEN)✅ GGUF GPU inference benchmark complete$(NC)"

bench-apr-gpu-inference: ## APR format GPU inference vs GGUF
	@echo "$(GREEN)Running APR vs GGUF GPU comparison...$(NC)"
	@if cargo build --features gpu --quiet 2>/dev/null; then \
		cargo bench --bench comparative --features gpu 2>/dev/null || \
			echo "Running without GPU..."; \
		cargo bench --bench comparative; \
	else \
		cargo bench --bench comparative; \
	fi
	@echo "$(GREEN)✅ APR vs GGUF benchmark complete$(NC)"

bench-server-matrix: ## Benchmark Ollama vs llama.cpp servers (updates README)
	@echo "$(GREEN)Running server benchmark matrix...$(NC)"
	@./scripts/bench-server-matrix.sh
	@echo "$(GREEN)✅ Server benchmark complete$(NC)"

# === Documentation ===

doc: ## Generate documentation
	@echo "$(GREEN)Generating documentation...$(NC)"
	cargo doc --all-features --no-deps

doc-open: ## Generate and open documentation
	@echo "$(GREEN)Generating and opening documentation...$(NC)"
	cargo doc --all-features --no-deps --open

# === Book (mdBook) ===

book: book-build book-open ## Build and open the Realizar book

book-build: ## Build the book
	@echo "$(GREEN)📚 Building Realizar book...$(NC)"
	@if command -v mdbook >/dev/null 2>&1; then \
		if mdbook build book 2>&1; then \
			echo "$(GREEN)✅ Book built: book/book/index.html$(NC)"; \
		else \
			echo "$(RED)❌ Book build failed$(NC)"; \
			exit 1; \
		fi; \
	else \
		echo "$(RED)❌ mdbook not installed. Install with: cargo install mdbook$(NC)"; \
		exit 1; \
	fi

book-open: ## Open the book in browser
	@if [ -f book/book/index.html ]; then \
		xdg-open book/book/index.html 2>/dev/null || \
		open book/book/index.html 2>/dev/null || \
		echo "$(YELLOW)Please open: book/book/index.html$(NC)"; \
	else \
		echo "$(RED)❌ Book not built. Run 'make book-build' first$(NC)"; \
	fi

book-serve: ## Serve the book with live reload
	@echo "$(GREEN)📚 Serving Realizar book at http://localhost:3000$(NC)"
	@if command -v mdbook >/dev/null 2>&1; then \
		mdbook serve book --open; \
	else \
		echo "$(RED)❌ mdbook not installed. Install with: cargo install mdbook$(NC)"; \
		exit 1; \
	fi

book-clean: ## Clean book build artifacts
	@rm -rf book/book
	@echo "$(GREEN)✓ Book artifacts cleaned$(NC)"

book-validate: ## Validate that book code examples are test-backed (TDD enforcement)
	@echo "$(GREEN)📚 Validating book code examples are test-backed...$(NC)"
	@if [ -f scripts/validate-book-code.sh ]; then \
		./scripts/validate-book-code.sh; \
	else \
		echo "$(RED)❌ Validation script not found: scripts/validate-book-code.sh$(NC)"; \
		exit 1; \
	fi

# === Quality Analysis ===

bashrs-check: ## Validate Makefile and shell scripts with bashrs
	@echo "$(GREEN)Running bashrs validation...$(NC)"
	@if command -v bashrs >/dev/null 2>&1; then \
		echo "Validating Makefile..."; \
		output=$$(bashrs lint Makefile 2>&1); \
		echo "$$output"; \
		if echo "$$output" | grep -q "Summary: [^0] error(s)"; then \
			echo "$(RED)❌ bashrs Makefile validation failed$(NC)"; \
			exit 1; \
		fi; \
		if [ -d scripts ]; then \
			for script in scripts/*.sh; do \
				if [ -f "$$script" ]; then \
					echo ""; \
					echo "Validating $$script..."; \
					script_output=$$(bashrs lint "$$script" 2>&1); \
					echo "$$script_output"; \
					if echo "$$script_output" | grep -q "Summary: [^0] error(s)"; then \
						echo "$(RED)❌ bashrs validation failed for $$script$(NC)"; \
						exit 1; \
					fi; \
				fi; \
			done; \
		fi; \
		echo "$(GREEN)✅ All bashrs validations passed$(NC)"; \
	else \
		echo "$(YELLOW)⚠️  bashrs not installed, skipping$(NC)"; \
	fi

audit: ## Run security audit (cargo-audit)
	@echo "$(GREEN)Running security audit...$(NC)"
	cargo audit

deny: ## Run cargo-deny checks (licenses, bans, sources)
	@echo "$(GREEN)Running cargo-deny checks...$(NC)"
	cargo deny check

pmat-tdg: ## Run PMAT Technical Debt Grading
	@echo "$(GREEN)Running PMAT TDG analysis...$(NC)"
	@if command -v pmat >/dev/null 2>&1; then \
		pmat analyze tdg src/; \
	else \
		echo "$(YELLOW)⚠️  pmat not installed, skipping$(NC)"; \
	fi

# === Profiling (Renacer integration) ===

profile: ## Profile benchmarks with Renacer
	@echo "$(GREEN)Profiling benchmarks with Renacer...$(NC)"
	@if command -v renacer >/dev/null 2>&1; then \
		renacer --function-time --source -- cargo bench --no-run; \
	else \
		echo "$(YELLOW)⚠️  renacer not installed, skipping$(NC)"; \
	fi

profile-test: ## Profile tests with Renacer
	@echo "$(GREEN)Profiling tests with Renacer...$(NC)"
	@if command -v renacer >/dev/null 2>&1; then \
		renacer --function-time -- cargo test --no-run; \
	else \
		echo "$(YELLOW)⚠️  renacer not installed, skipping$(NC)"; \
	fi

# === Development ===

dev: ## Start development environment
	@echo "$(GREEN)Starting development environment...$(NC)"
	cargo watch -x 'test --lib' -x 'clippy'

clean: ## Clean build artifacts
	@echo "$(GREEN)Cleaning build artifacts...$(NC)"
	cargo clean
	rm -f lcov.info
	rm -rf mutants.out mutants.out.old
	rm -rf target/coverage target/llvm-cov
	find . -name "*.profraw" -delete 2>/dev/null || true

# === Lambda Deployment (.apr format) ===

lambda-model: ## Build reproducible .apr model file
	@echo "$(GREEN)Building MNIST model (.apr format)...$(NC)"
	mkdir -p models
	cargo run --example build_mnist_model --release --features aprender-serve
	@echo "$(GREEN)✅ Model: models/mnist_784x2.apr$(NC)"

lambda-build: lambda-model ## Build MNIST Lambda binary (requires model)
	@echo "$(GREEN)Building MNIST Lambda binary...$(NC)"
	cargo build --release --bin mnist_lambda --features "aprender-serve lambda"
	@echo "$(GREEN)✅ Binary: target/release/mnist_lambda ($(shell ls -lh target/release/mnist_lambda 2>/dev/null | awk '{print $$5}'))$(NC)"

lambda-bench: lambda-build ## Run Lambda benchmark (proves .apr vs PyTorch)
	@echo "$(GREEN)Running .apr vs PyTorch Lambda benchmark...$(NC)"
	./target/release/mnist_lambda
	@echo ""
	@echo "$(GREEN)✅ Benchmark complete - .apr DOMINATES PyTorch$(NC)"

lambda-package: lambda-build ## Package Lambda for AWS deployment
	@echo "$(GREEN)Packaging Lambda for AWS...$(NC)"
	cp target/release/mnist_lambda bootstrap
	zip -j mnist_lambda.zip bootstrap
	rm bootstrap
	@echo "$(GREEN)✅ Package: mnist_lambda.zip ($(shell ls -lh mnist_lambda.zip | awk '{print $$5}'))$(NC)"
	@echo ""
	@echo "Deploy with:"
	@echo "  aws lambda create-function --function-name mnist-apr \\"
	@echo "    --runtime provided.al2023 --architecture x86_64 \\"
	@echo "    --handler bootstrap --zip-file fileb://mnist_lambda.zip \\"
	@echo "    --role arn:aws:iam::ACCOUNT:role/lambda-role"

lambda-clean: ## Clean Lambda artifacts
	@rm -f bootstrap mnist_lambda.zip
	@rm -rf models/
	@echo "$(GREEN)✓ Lambda artifacts cleaned$(NC)"

# === Deployment ===

deploy: quality-gates build-all-features ## Deploy to production
	@echo "$(GREEN)Deploying to production...$(NC)"
	@echo "$(YELLOW)Building release...$(NC)"
	@# Future: Deploy model server
	@echo "$(GREEN)✅ Deployment complete!$(NC)"

# === CI/CD ===

ci: quality-gates mutate-fast ## Run CI pipeline (all checks)
	@echo "$(GREEN)✅ CI pipeline passed!$(NC)"

# === Installation ===

install-tools: ## Install development tools
	@echo "$(GREEN)Installing development tools...$(NC)"
	cargo install cargo-nextest --locked || true
	cargo install cargo-llvm-cov --locked || true
	cargo install cargo-mutants || true
	cargo install cargo-audit || true
	cargo install cargo-deny || true
	cargo install cargo-watch || true
	@echo "$(GREEN)✅ Tools installed!$(NC)"