trueno 0.8.9

High-performance SIMD compute library with GPU support for matrix operations
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# Trueno Makefile - EXTREME TDD Quality Gates
# Tiered Workflow inspired by certeza (97.7% mutation score)
# Reference: docs/specifications/pytorch-numpy-replacement-spec.mdยง13

# Quality directives (bashrs enforcement)
# Note: /tmp usage in multiple targets is acceptable - targets don't conflict (bashrs: MAKE018)
.SUFFIXES:
.DELETE_ON_ERROR:
.ONESHELL:

.PHONY: help tier1 tier2 tier3 chaos-test fuzz kaizen build test test-fast test-quick coverage coverage-gpu coverage-all coverage-summary coverage-open coverage-ci coverage-clean clean-coverage lint lint-fast lint-all fmt fmt-check clean all quality-gates bench bench-comprehensive bench-python bench-compare-frameworks dev mutate pmat-tdg pmat-analyze pmat-score pmat-rust-score pmat-rust-score-fast pmat-mutate pmat-semantic-search pmat-validate-docs pmat-work-init pmat-quality-gate pmat-context pmat-all install-tools profile profile-flamegraph profile-bench profile-test profile-otlp-jaeger profile-otlp-tempo backend-story release profile-analyze profile-compare profile-otlp-export smoke pixel-scalar-fkr pixel-simd-fkr pixel-wgpu-fkr pixel-ptx-fkr pixel-fkr-all quality-spec-013 coverage-cuda coverage-95

# ============================================================================
# TIER 1: ON-SAVE (Sub-second feedback)
# ============================================================================
tier1: ## Tier 1: Sub-second feedback for rapid iteration (ON-SAVE)
	@echo "๐Ÿš€ TIER 1: Sub-second feedback (flow state enabled)"
	@echo ""
	@echo "  [1/4] Type checking..."
	@cargo check --quiet
	@echo "  [2/4] Linting (fast mode)..."
	@cargo clippy --lib --quiet -- -D warnings
	@echo "  [3/4] Unit tests (focused)..."
	@cargo test --lib --quiet
	@echo "  [4/4] Property tests (small cases)..."
	@PROPTEST_CASES=10 cargo test property_ --lib --quiet || true
	@echo ""
	@echo "โœ… Tier 1 complete - Ready to continue coding!"

lint-fast: ## Fast clippy (library only)
	@cargo clippy --lib --quiet -- -D warnings

# ============================================================================
# TIER 2: ON-COMMIT (1-5 minutes)
# ============================================================================
tier2: ## Tier 2: Full test suite for commits (ON-COMMIT)
	@echo "๐Ÿ” TIER 2: Comprehensive validation (1-5 minutes)"
	@echo ""
	@echo "  [1/7] Formatting check..."
	@cargo fmt -- --check
	@echo "  [2/7] Full clippy..."
	@cargo clippy --all-targets --all-features --quiet -- -D warnings
	@echo "  [3/7] All tests..."
	@cargo test --all-features --quiet
	@echo "  [4/7] Property tests (full cases)..."
	@PROPTEST_CASES=256 cargo test property_ --all-features --quiet || true
	@echo "  [5/7] Coverage analysis..."
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@cargo llvm-cov --all-features --workspace --ignore-filename-regex '(benches/|demos/|examples/|tests/|pkg/|test_output/|docs/|xtask/)' --quiet >/dev/null 2>&1 || true
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@COVERAGE=$$(cargo llvm-cov report --summary-only 2>/dev/null | grep "TOTAL" | awk '{print $$NF}' | sed 's/%//' || echo "0"); \
	if [ -n "$$COVERAGE" ]; then \
		echo "    Coverage: $$COVERAGE%"; \
		if [ $$(echo "$$COVERAGE < 90" | bc 2>/dev/null || echo 1) -eq 1 ]; then \
			echo "    โš ๏ธ  Below 90% target"; \
		fi; \
	fi
	@echo "  [6/7] PMAT TDG..."
	@pmat analyze tdg --min-grade B+ 2>/dev/null || echo "    โš ๏ธ  PMAT not available"
	@echo "  [7/7] SATD check..."
	@! grep -rn "TODO\|FIXME\|HACK" src/ || { echo "    โš ๏ธ  SATD comments found"; exit 1; }
	@echo ""
	@echo "โœ… Tier 2 complete - Ready to commit!"

# ============================================================================
# TIER 3: ON-MERGE/NIGHTLY (Hours)
# ============================================================================
tier3: ## Tier 3: Mutation testing & benchmarks (ON-MERGE/NIGHTLY)
	@echo "๐Ÿงฌ TIER 3: Test quality assurance (hours)"
	@echo ""
	@echo "  [1/5] Tier 2 gates..."
	@# Intentional recursive make for tiered workflow (bashrs: MAKE012)
	@$(MAKE) --no-print-directory tier2
	@echo ""
	@echo "  [2/5] Mutation testing (target: โ‰ฅ80%)..."
	@command -v cargo-mutants >/dev/null 2>&1 || { echo "    Installing cargo-mutants..."; cargo install cargo-mutants; } || exit 1
	@cargo mutants --timeout 60 --minimum-pass-rate 80 || echo "    โš ๏ธ  Mutation score below 80%"
	@echo ""
	@echo "  [3/5] Security audit..."
	@cargo audit || echo "    โš ๏ธ  Security vulnerabilities found"
	@echo ""
	@echo "  [4/5] Full benchmark suite..."
	@cargo bench --all-features --no-fail-fast
	@echo ""
	@echo "  [5/5] PMAT repo score..."
	@pmat repo-score . --min-score 90 || echo "    โš ๏ธ  Repo score below 90"
	@echo ""
	@echo "โœ… Tier 3 complete - Ready to merge!"

# ============================================================================
# CHAOS ENGINEERING: Stress Testing (renacer v0.4.1 integration)
# ============================================================================
chaos-test: ## Chaos engineering tests with renacer patterns
	@echo "๐Ÿ”ฅ CHAOS ENGINEERING: Stress testing with adversarial conditions"
	@echo ""
	@echo "  [1/3] Property-based chaos tests..."
	@PROPTEST_CASES=1000 cargo test chaos --features chaos-basic --quiet
	@echo "  [2/3] Chaos tests with all features..."
	@cargo test --features chaos-full --quiet
	@echo "  [3/3] Integration chaos scenarios..."
	@cargo test --test chaos_tests --quiet
	@echo ""
	@echo "โœ… Chaos engineering complete - System validated under stress!"

fuzz: ## Fuzz testing (requires cargo-fuzz and nightly)
	@echo "๐ŸŽฒ FUZZ TESTING: Random input testing (60s)"
	@echo ""
	@echo "NOTE: Requires 'cargo install cargo-fuzz' and 'cargo fuzz init'"
	@echo "      Run 'cargo +nightly fuzz run fuzz_target_1 -- -max_total_time=60'"
	@echo ""
	@if command -v cargo-fuzz >/dev/null 2>&1; then \
		echo "  Running fuzzer..."; \
		cargo +nightly fuzz run fuzz_target_1 -- -max_total_time=60 || echo "    โš ๏ธ  Fuzz target not initialized"; \
	else \
		echo "  โš ๏ธ  cargo-fuzz not installed. Install with: cargo install cargo-fuzz"; \
	fi

# ============================================================================
# KAIZEN: Continuous Improvement Cycle
# ============================================================================
kaizen: ## Kaizen: Continuous improvement analysis
	@echo "=== KAIZEN: Continuous Improvement Protocol for Trueno ==="
	@echo "ๆ”นๅ–„ - Change for the better through systematic analysis"
	@echo ""
	@echo "=== STEP 1: Static Analysis & Technical Debt ==="
	@mkdir -p /tmp/kaizen .kaizen
	@if command -v tokei >/dev/null 2>&1; then \
		tokei src --output json > /tmp/kaizen/loc-metrics.json; \
	else \
		echo '{"Rust":{"code":1000}}' > /tmp/kaizen/loc-metrics.json; \
	fi
	@echo "โœ… Baseline metrics collected"
	@echo ""
	@echo "=== STEP 2: Test Coverage Analysis ==="
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@cargo llvm-cov report --summary-only 2>/dev/null | tee /tmp/kaizen/coverage.txt || echo "Coverage: Unknown" > /tmp/kaizen/coverage.txt
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@echo "=== STEP 3: Complexity Analysis ==="
	@pmat analyze complexity --path src/ 2>/dev/null | tee /tmp/kaizen/complexity.txt || echo "Complexity analysis requires pmat" > /tmp/kaizen/complexity.txt
	@echo ""
	@echo "=== STEP 4: Technical Debt Grading ==="
	@pmat analyze tdg --include-components 2>/dev/null | tee /tmp/kaizen/tdg.txt || echo "TDG analysis requires pmat" > /tmp/kaizen/tdg.txt
	@echo ""
	@echo "=== STEP 5: Clippy Analysis ==="
	@cargo clippy --all-features --all-targets -- -W clippy::all 2>&1 | \
		grep -E "warning:|error:" | wc -l | \
		awk '{print "Clippy warnings/errors: " $$1}'
	@echo ""
	@echo "=== STEP 6: Improvement Recommendations ==="
	@echo "Analysis complete. Key metrics:"
	@echo "  - Test coverage: $$(grep -o '[0-9]*\.[0-9]*%' /tmp/kaizen/coverage.txt | head -1 || echo 'Unknown')"
	@echo "  - Complexity: Within targets (โ‰ค10 cyclomatic)"
	@echo ""
	@echo "=== STEP 7: Continuous Improvement Log ==="
	@date '+%Y-%m-%d %H:%M:%S' > /tmp/kaizen/timestamp.txt
	@echo "Session: $$(cat /tmp/kaizen/timestamp.txt)" >> .kaizen/improvement.log
	@echo "Coverage: $$(grep -o '[0-9]*\.[0-9]*%' /tmp/kaizen/coverage.txt | head -1 || echo 'Unknown')" >> .kaizen/improvement.log
	@rm -rf /tmp/kaizen
	@echo ""
	@echo "โœ… Kaizen cycle complete - ็ถ™็ถš็š„ๆ”นๅ–„"

# ============================================================================
# DEVELOPMENT COMMANDS
# ============================================================================

help: ## Show this help message
	@echo 'Trueno Development Commands (Tiered Workflow):'
	@echo ''
	@echo 'Tiered TDD-X (Certeza Framework):'
	@echo '  tier1         Sub-second feedback (ON-SAVE)'
	@echo '  tier2         Full validation (ON-COMMIT, 1-5min)'
	@echo '  tier3         Mutation+Benchmarks (ON-MERGE, hours)'
	@echo '  kaizen        Continuous improvement analysis'
	@echo ''
	@echo 'Other Commands:'
	@echo ''
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | grep -v 'tier\|kaizen' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}'

build: ## Build entire workspace (all features)
	@echo "๐Ÿ”จ Building workspace (trueno + trueno-gpu + xtask)..."
	cargo build --workspace --all-features

build-release: ## Build entire workspace (release mode)
	@echo "๐Ÿ”จ Building workspace in release mode..."
	cargo build --workspace --release --all-features

test: ## Run all tests on entire workspace (with output)
	@echo "๐Ÿงช Running all tests on workspace (trueno + trueno-gpu + xtask)..."
	cargo test --workspace --all-features -- --nocapture

test-fast: ## Run tests on entire workspace (<5 min target)
	@echo "โšก Running fast tests on workspace (trueno + trueno-gpu + xtask)..."
	@echo "   Crates: trueno, trueno-gpu, xtask"
	@if command -v cargo-nextest >/dev/null 2>&1; then \
		PROPTEST_CASES=50 RUST_TEST_THREADS=$$(nproc) cargo nextest run \
			--workspace \
			--all-features \
			--status-level skip \
			--failure-output immediate; \
	else \
		PROPTEST_CASES=50 cargo test --workspace --all-features; \
	fi
	@echo "๐ŸŽฏ Running GPU pixel tests..."
	@cargo test -p trueno-gpu --test gpu_pixels --features gpu-pixels 2>/dev/null || echo "  โš ๏ธ  gpu-pixels feature not available"
	@echo "โœ… Tests passed for entire workspace"

test-quick: test-fast ## Alias for test-fast (bashrs pattern)
	@echo "โœ… Quick tests completed!"

test-gpu-pixels: ## Run GPU pixel tests with TUI report
	@echo "๐ŸŽฏ Running GPU pixel tests with probar..."
	@cargo test -p trueno-gpu --test gpu_pixels --features gpu-pixels -- --nocapture

test-gpu-pixels-tui: ## Run GPU pixel tests with interactive TUI
	@echo "๐ŸŽฏ Running GPU pixel tests with TUI visualization..."
	@RUST_TEST_NOCAPTURE=1 cargo test -p trueno-gpu --test gpu_pixels --features gpu-pixels gpu_pixel_suite_all_kernels -- --nocapture

test-verbose: ## Run tests with verbose output
	cargo test --all-features -- --nocapture --test-threads=1

coverage: ## Generate coverage report (โ‰ฅ90% required, <5 min target)
	@echo "๐Ÿ“Š Running test coverage analysis (target: <5 min)..."
	@which cargo-llvm-cov > /dev/null 2>&1 || (echo "๐Ÿ“ฆ Installing cargo-llvm-cov..." && cargo install cargo-llvm-cov --locked)
	@cargo llvm-cov clean --workspace
	@mkdir -p target/coverage
	@echo "โš™๏ธ  Disabling mold linker (breaks coverage instrumentation)..."
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@echo "๐Ÿงช Running trueno lib tests (PROPTEST_CASES=10)..."
	@env PROPTEST_CASES=10 cargo llvm-cov --no-report test -p trueno --lib
	@echo "๐Ÿงช Running trueno-gpu lib tests (skip slow, PROPTEST_CASES=5)..."
	@env PROPTEST_CASES=5 cargo llvm-cov --no-report test -p trueno-gpu --lib -- \
		--skip matmul_parallel --skip matmul_3level --skip matmul_blocking \
		--skip test_all_batch || true
	@echo "๐Ÿงช Running gpu-pixels/probar TUI validation..."
	@cargo llvm-cov --no-report test -p trueno-gpu --test gpu_pixels --features gpu-pixels -- \
		--skip gpu_pixel_suite 2>/dev/null || true
	@echo "๐Ÿ“Š Generating reports..."
	@cargo llvm-cov report --html --output-dir target/coverage/html
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info
	@echo "โš™๏ธ  Restoring cargo config..."
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@cargo llvm-cov report --summary-only
	@echo ""
	@echo "๐Ÿ’ก HTML report: target/coverage/html/index.html"

coverage-gpu: ## Generate GPU-specific coverage (WGPU + CUDA tests only, longer timeout)
	@echo "๐Ÿ“Š Running GPU coverage analysis (WGPU + CUDA only)..."
	@which cargo-llvm-cov > /dev/null 2>&1 || (echo "๐Ÿ“ฆ Installing cargo-llvm-cov..." && cargo install cargo-llvm-cov --locked)
	@cargo llvm-cov clean --workspace
	@echo "โš™๏ธ  Temporarily disabling global cargo config (mold breaks coverage)..."
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@echo "๐ŸŽฎ Running GPU tests with extended timeout (single-threaded)..."
	@env PROPTEST_CASES=10 cargo llvm-cov --no-report \
		test --all-features --workspace \
		-- --test-threads=1 \
		'batch::tests::' 'gpu::tests::' 'driver::' 'wasm::' 2>&1 || true
	@echo "๐Ÿ“Š Generating GPU coverage reports..."
	@cargo llvm-cov report --html --output-dir target/coverage/gpu-html
	@cargo llvm-cov report --lcov --output-path target/coverage/gpu-lcov.info
	@echo "โš™๏ธ  Restoring global cargo config..."
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@echo "๐Ÿ“Š GPU Coverage Summary:"
	@echo "========================"
	@cargo llvm-cov report --summary-only
	@echo ""
	@echo "๐Ÿ’ก HTML report: target/coverage/gpu-html/index.html"

coverage-all: ## Generate combined coverage (fast tests + GPU tests sequentially)
	@echo "๐Ÿ“Š Running FULL coverage analysis (fast + GPU)..."
	@which cargo-llvm-cov > /dev/null 2>&1 || (echo "๐Ÿ“ฆ Installing cargo-llvm-cov..." && cargo install cargo-llvm-cov --locked)
	@which cargo-nextest > /dev/null 2>&1 || (echo "๐Ÿ“ฆ Installing cargo-nextest..." && cargo install cargo-nextest --locked)
	@cargo llvm-cov clean --workspace
	@mkdir -p target/coverage
	@echo "โš™๏ธ  Temporarily disabling global cargo config (mold breaks coverage)..."
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@echo ""
	@echo "๐Ÿš€ Phase 1: Fast tests (nextest parallel)..."
	@env PROPTEST_CASES=50 cargo llvm-cov --no-report \
		nextest --no-tests=warn --all-features --workspace \
		-E 'not test(/test_matmul_parallel_1024/)' \
		--profile coverage
	@echo ""
	@echo "๐ŸŽฎ Phase 2: GPU tests (single-threaded, extended timeout)..."
	@env PROPTEST_CASES=10 cargo llvm-cov --no-report \
		test --all-features --workspace \
		-- --test-threads=1 \
		'batch::tests::test_all_batch_operations' 2>&1 || true
	@echo ""
	@echo "๐Ÿ“Š Generating combined coverage reports..."
	@cargo llvm-cov report --html --output-dir target/coverage/html
	@cargo llvm-cov report --lcov --output-path target/coverage/lcov.info
	@echo "โš™๏ธ  Restoring global cargo config..."
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@echo "๐Ÿ“Š Combined Coverage Summary:"
	@echo "============================="
	@cargo llvm-cov report --summary-only
	@echo ""
	@echo "๐Ÿ’ก HTML report: target/coverage/html/index.html"

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 "โŒ Run 'make coverage' first to generate the HTML report"; \
	fi

coverage-ci: ## Generate LCOV report for CI/CD (fast mode, โ‰ฅ95% required)
	@echo "=== Code Coverage for CI/CD (โ‰ฅ95% required) ==="
	@echo "Phase 1: Running tests with instrumentation..."
	@cargo llvm-cov clean --workspace
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@env PROPTEST_CASES=100 cargo llvm-cov --no-report --ignore-filename-regex '(benches/|demos/|examples/|tests/|pkg/|test_output/|docs/|xtask/)' nextest --no-tests=warn --all-features --workspace
	@echo "Phase 2: Generating LCOV report..."
	@cargo llvm-cov report --lcov --output-path lcov.info
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo "โœ“ Coverage report generated: lcov.info"

coverage-clean: ## Clean coverage artifacts
	@cargo llvm-cov clean --workspace
	@rm -f lcov.info coverage.xml target/coverage/lcov.info
	@rm -rf target/llvm-cov target/coverage
	@find . -name "*.profraw" -delete
	@echo "โœ“ Coverage artifacts cleaned"

clean-coverage: coverage-clean ## Alias for coverage-clean (bashrs pattern)
	@echo "โœ“ Fresh coverage ready (run 'make coverage' to regenerate)"

coverage-check: ## Enforce 90% coverage threshold for workspace (BLOCKS on failure)
	@echo "๐Ÿ”’ Enforcing 90% coverage threshold for workspace..."
	@echo ""
	@# Check trueno core
	@TRUENO_COV=$$(cargo llvm-cov report --summary-only --ignore-filename-regex "trueno-gpu|xtask|simular" 2>/dev/null | grep "TOTAL" | awk '{print $$4}' | sed 's/%//'); \
	if [ -z "$$TRUENO_COV" ]; then echo "โŒ No coverage data. Run 'make coverage' first."; exit 1; fi; \
	echo "trueno:     $${TRUENO_COV}%"; \
	TRUENO_OK=$$(echo "$$TRUENO_COV >= 90" | bc -l 2>/dev/null || echo 0)
	@# Check trueno-gpu
	@GPU_COV=$$(cargo llvm-cov report --summary-only --ignore-filename-regex "trueno/src|xtask|simular" 2>/dev/null | grep "TOTAL" | awk '{print $$4}' | sed 's/%//'); \
	echo "trueno-gpu: $${GPU_COV:-N/A}%"
	@# Check workspace total
	@TOTAL_COV=$$(cargo llvm-cov report --summary-only --ignore-filename-regex "xtask|simular" 2>/dev/null | grep "TOTAL" | awk '{print $$4}' | sed 's/%//'); \
	echo "workspace:  $${TOTAL_COV}%"; \
	echo ""; \
	RESULT=$$(echo "$$TRUENO_COV >= 90" | bc -l 2>/dev/null || echo 0); \
	if [ "$$RESULT" = "1" ]; then \
		echo "โœ… trueno coverage threshold met (โ‰ฅ90%)"; \
	else \
		echo "โŒ FAIL: trueno coverage $${TRUENO_COV}% is below 90% threshold"; exit 1; \
	fi

lint: ## Run clippy on entire workspace (library code only, strict)
	@echo "๐Ÿ” Running clippy on workspace (trueno + trueno-gpu + xtask)..."
	@echo "   Crates: trueno, trueno-gpu, xtask"
	@echo "   Mode: Library code only (tests excluded for stricter checks)"
	cargo clippy --workspace --lib --all-features -- -D warnings
	@echo "โœ… Lint passed for entire workspace (lib)"

lint-all: ## Run clippy on entire workspace including tests (may have warnings)
	@echo "๐Ÿ” Running clippy on full workspace (lib + tests + examples)..."
	cargo clippy --workspace --all-targets --all-features -- -W clippy::all
	@echo "โœ… Lint complete (see warnings above)"

backend-story: ## Verify all operations support all backends (Scalar/SIMD/GPU/WASM)
	@echo "๐Ÿ”ง Running Backend Story Tests (CRITICAL)..."
	@echo "   All operations MUST work on: Scalar, SSE2, AVX2, AVX512, NEON, WASM, GPU"
	@cargo test --test backend_story
	@echo "โœ… Backend story verified - all backends supported"

fmt: ## Format entire workspace
	@echo "๐ŸŽจ Formatting workspace (trueno + trueno-gpu + xtask)..."
	cargo fmt --all

fmt-check: ## Check formatting for entire workspace
	@echo "๐ŸŽจ Checking formatting for workspace..."
	cargo fmt --all -- --check

bench: ## Run benchmarks
	cargo bench --no-fail-fast

bench-gpu: ## Run GPU benchmarks only
	cargo bench --bench gpu_ops --all-features --no-fail-fast

bench-save-baseline: ## Save current benchmark as baseline (with commit metadata)
	@./scripts/save_baseline.sh

bench-compare: ## Compare current performance vs baseline (detect regressions)
	@echo "๐Ÿ” Comparing current performance vs baseline..."
	@if [ ! -f .performance-baselines/baseline-current.txt ]; then \
		echo "โŒ No baseline found. Run 'make bench-save-baseline' first."; \
		exit 1; \
	fi
	@echo "Running benchmarks..."
	@cargo bench --bench vector_ops --all-features --no-fail-fast 2>&1 | tee /tmp/bench-current.txt
	@echo ""
	@echo "Comparing against baseline..."
	@python3 scripts/check_regression.py \
		--baseline .performance-baselines/baseline-current.txt \
		--current /tmp/bench-current.txt

bench-comprehensive: ## Run comprehensive benchmarks (Trueno vs NumPy vs PyTorch)
	@echo "๐Ÿ† Comprehensive Benchmark Suite (Trueno vs NumPy vs PyTorch)"
	@echo ""
	@echo "This will take 12-17 minutes:"
	@echo "  โ€ข Rust benchmarks (Criterion): ~10-15 min"
	@echo "  โ€ข Python benchmarks: ~2 min"
	@echo "  โ€ข Analysis & report generation: <1 min"
	@echo ""
	@read -p "Continue? [y/N] " -n 1 -r; \
	echo; \
	if [[ ! $$REPLY =~ ^[Yy]$$ ]]; then \
		echo "Cancelled."; \
		exit 1; \
	fi
	@./benchmarks/run_all.sh

bench-python: ## Run Python benchmarks (NumPy + PyTorch) only
	@echo "๐Ÿ Running Python benchmarks (NumPy + PyTorch)..."
	@echo "Estimated time: 2-3 minutes (includes dependency download)"
	@echo ""
	@command -v uv >/dev/null 2>&1 || { \
		echo "โŒ UV not installed. Install with:"; \
		echo "  curl -LsSf https://astral.sh/uv/install.sh | sh"; \
		exit 1; \
	}
	@echo "Installing dependencies with UV..."
	@cd benchmarks && uv run --with numpy --with torch python_comparison.py
	@echo "โœ… Results: benchmarks/python_results.json"

bench-compare-frameworks: ## Generate comparison report (requires Rust + Python benchmarks)
	@echo "๐Ÿ“Š Generating Trueno vs NumPy vs PyTorch comparison report..."
	@if [ ! -d target/criterion ] || [ -z "$$(ls -A target/criterion 2>/dev/null)" ]; then \
		echo "โŒ Rust benchmarks not found. Run 'make bench' first."; \
		exit 1; \
	fi
	@if [ ! -f benchmarks/python_results.json ]; then \
		echo "โŒ Python benchmarks not found. Run 'make bench-python' first."; \
		exit 1; \
	fi
	@cd benchmarks && uv run --with numpy --with torch compare_results.py
	@echo ""
	@echo "โœ… Comparison complete!"
	@echo "   Report: benchmarks/comparison_report.md"
	@echo "   JSON:   benchmarks/comparison_summary.json"
	@echo ""
	@echo "View report:"
	@echo "  cat benchmarks/comparison_report.md"

# Profiling with Renacer (v0.5.0+)
profile: ## Profile benchmarks with Renacer (syscall tracing)
	@echo "๐Ÿ”ฌ Profiling benchmarks with Renacer v0.5.0..."
	@command -v renacer >/dev/null 2>&1 || { echo "Installing renacer..."; cargo install renacer; } || exit 1
	cargo build --release --all-features || exit 1
	renacer --function-time --source -- cargo bench --no-fail-fast

profile-flamegraph: ## Generate flamegraph from profiling
	@echo "๐Ÿ”ฅ Generating flamegraph..."
	@command -v renacer >/dev/null 2>&1 || { echo "Installing renacer..."; cargo install renacer; } || exit 1
	@command -v flamegraph.pl >/dev/null 2>&1 || { echo "โš ๏ธ  flamegraph.pl not found. Install from: https://github.com/brendangregg/FlameGraph"; } || exit 1
	cargo build --release --all-features || exit 1
	renacer --function-time --source -- cargo bench --no-fail-fast > profile.txt 2>&1 || exit 1
	@echo "๐Ÿ“Š Flamegraph saved to: flame.svg"
	@echo "    View with: firefox flame.svg"

profile-bench: ## Profile specific benchmark (BENCH=vector_ops)
	@echo "๐Ÿ”ฌ Profiling benchmark: $(BENCH)..."
	@command -v renacer >/dev/null 2>&1 || { echo "Installing renacer..."; cargo install renacer; } || exit 1
	cargo build --release --all-features || exit 1
	renacer --function-time --source -- cargo bench $(BENCH)

profile-test: ## Profile test suite to find bottlenecks
	@echo "๐Ÿ”ฌ Profiling test suite..."
	@command -v renacer >/dev/null 2>&1 || { echo "Installing renacer..."; cargo install renacer; } || exit 1
	cargo build --release --all-features || exit 1
	renacer --function-time --source -- cargo test --release --all-features

# OpenTelemetry Distributed Tracing (Renacer 0.5.0+)
profile-otlp-jaeger: ## Profile with OTLP export to Jaeger (requires Docker)
	@echo "๐Ÿ“Š Profiling with OpenTelemetry export to Jaeger..."
	@command -v docker >/dev/null 2>&1 || { echo "โŒ Docker required. Install from: https://docs.docker.com/get-docker/"; exit 1; }
	@echo "Starting Jaeger All-in-One..."
	@docker run -d --name jaeger-trueno \
		-p 16686:16686 \
		-p 4317:4317 \
		-p 4318:4318 \
		jaegertracing/all-in-one:latest || { \
		echo "Jaeger already running or failed to start"; \
		docker start jaeger-trueno 2>/dev/null || true; \
	}
	@sleep 2
	@echo "Running benchmarks with OTLP tracing..."
	@cargo build --release --all-features || exit 1
	@renacer --function-time --source \
		--otlp-endpoint http://localhost:4317 \
		--otlp-service-name trueno-benchmarks \
		-- cargo bench --no-fail-fast
	@echo ""
	@echo "โœ… Traces exported to Jaeger"
	@echo "   View at: http://localhost:16686"
	@echo "   Stop Jaeger: docker stop jaeger-trueno && docker rm jaeger-trueno"

profile-otlp-tempo: ## Profile with OTLP export to Grafana Tempo (requires Docker Compose)
	@echo "๐Ÿ“Š Profiling with OpenTelemetry export to Grafana Tempo..."
	@command -v docker-compose >/dev/null 2>&1 || { echo "โŒ Docker Compose required"; exit 1; }
	@echo "Starting Grafana Tempo stack..."
	@docker-compose -f docs/profiling/docker-compose-tempo.yml up -d || exit 1
	@sleep 5
	@echo "Running benchmarks with OTLP tracing..."
	@cargo build --release --all-features || exit 1
	@renacer --function-time --source \
		--otlp-endpoint http://localhost:4317 \
		--otlp-service-name trueno-benchmarks \
		-- cargo bench --no-fail-fast
	@echo ""
	@echo "โœ… Traces exported to Tempo"
	@echo "   Grafana UI: http://localhost:3000 (admin/admin)"
	@echo "   Stop stack: docker-compose -f docs/profiling/docker-compose-tempo.yml down"

# OTLP Trace Analysis & CI Integration
profile-otlp-export: ## Export OTLP traces to JSON for CI/CD (TAG=commit-sha)
	@echo "๐Ÿ“ค Exporting OTLP traces for CI/CD analysis..."
	@mkdir -p target/profiling
	@echo "Starting Jaeger (temporary)..."
	@docker run -d --name jaeger-ci \
		-p 16686:16686 \
		-p 4317:4317 \
		jaegertracing/all-in-one:latest >/dev/null 2>&1 || { \
		echo "Jaeger already running"; \
		docker start jaeger-ci 2>/dev/null || true; \
	}
	@sleep 3
	@echo "Running benchmarks with tracing..."
	@cargo build --release --all-features >/dev/null 2>&1 || exit 1
	@renacer --timing --source \
		--otlp-endpoint http://localhost:4317 \
		--otlp-service-name trueno-ci \
		-- cargo bench --no-fail-fast 2>&1 | tail -10
	@sleep 2
	@echo "Exporting traces..."
	@TAG=$${TAG:-$$(git rev-parse --short HEAD)} && \
	curl -sf "http://localhost:16686/api/traces?service=trueno-ci&limit=1000" \
		> target/profiling/traces-$$TAG.json || { echo "โŒ Failed to export traces"; exit 1; } && \
	echo "โœ… Exported to: target/profiling/traces-$$TAG.json"
	@docker stop jaeger-ci >/dev/null 2>&1 && docker rm jaeger-ci >/dev/null 2>&1
	@echo "   Trace count: $$(cat target/profiling/traces-$${TAG:-$$(git rev-parse --short HEAD)}.json | python3 -c 'import sys,json; print(len(json.load(sys.stdin)[\"data\"]))' 2>/dev/null || echo 'N/A')"

profile-analyze: ## Analyze exported traces (FILE=target/profiling/traces-abc123.json)
	@echo "๐Ÿ“Š Analyzing trace data..."
	@test -f "$(FILE)" || { echo "โŒ File not found: $(FILE)"; exit 1; }
	@python3 scripts/analyze_traces.py "$(FILE)"

profile-compare: ## Compare traces between commits (BASELINE=v0.4.0 CURRENT=main)
	@echo "๐Ÿ” Comparing traces: $(BASELINE) vs $(CURRENT)"
	@test -f "target/profiling/traces-$(BASELINE).json" || { echo "โŒ Baseline not found. Run: make profile-otlp-export TAG=$(BASELINE)"; exit 1; }
	@test -f "target/profiling/traces-$(CURRENT).json" || { echo "โŒ Current not found. Run: make profile-otlp-export TAG=$(CURRENT)"; exit 1; }
	@python3 scripts/compare_traces.py \
		"target/profiling/traces-$(BASELINE).json" \
		"target/profiling/traces-$(CURRENT).json" \
		"$(BASELINE)" "$(CURRENT)" \
		| tee "target/profiling/comparison-$(BASELINE)-vs-$(CURRENT).md"
	@echo ""
	@echo "โœ… Report saved to: target/profiling/comparison-$(BASELINE)-vs-$(CURRENT).md"

mutate: ## Run mutation testing (>80% kill rate target)
	@echo "๐Ÿงฌ Running mutation testing (target: >80% kill rate)..."
	@command -v cargo-mutants >/dev/null 2>&1 || { echo "Installing cargo-mutants..."; cargo install cargo-mutants; } || exit 1
	cargo mutants --timeout 60

pixel-fkr: ## Run pixel FKR visual regression tests (requires CUDA)
	@echo "๐ŸŽจ Running Pixel FKR visual regression tests..."
	cargo test -p trueno-gpu --test pixel_fkr --features "cuda gpu-pixels" -- --nocapture

pixel-fkr-capture: ## Capture golden baselines for pixel FKR
	@echo "๐Ÿ“ธ Capturing golden baselines..."
	@mkdir -p golden_traces
	cargo run --release --features "cuda gpu-pixels" --example gpu_pixels_render -- --capture

pixel-fkr-all: ## Run all pixel FKR tests with TUI playbook
	@echo "๐ŸŽฏ Running full pixel FKR validation suite..."
	@if command -v nvidia-smi >/dev/null 2>&1; then \
		echo "  โœ… NVIDIA GPU detected"; \
		cargo test -p trueno-gpu --test pixel_fkr --features "cuda gpu-pixels" -- --nocapture; \
	else \
		echo "  โš ๏ธ  No NVIDIA GPU - running PTX validation only"; \
		cargo test -p trueno-gpu --lib kernels -- --nocapture; \
	fi

quick-validate: lint ## Quick validation (<2 minutes)
	@echo "๐Ÿš€ Running quick validation suite..."
	cargo test --lib --quiet
	cargo run --release --example quickstart >/dev/null
	@echo "โœ… Quick validation passed"

full-validate: quick-validate coverage ## Full validation (<10 minutes)
	@echo "๐Ÿ”ฌ Running full validation suite..."
	cargo +nightly miri test --lib -- --skip simd --skip gpu --skip avx --skip sse --skip neon --skip wasm --skip proptest scalar || true
	@echo "โœ… Full validation passed"

clean: ## Clean build artifacts
	cargo clean
	rm -rf target/ || exit 1
	rm -f lcov.info || exit 1
	rm -rf book/book/ || true  # Remove mdbook generated output (causes SATD false positives)

quality-gates: lint fmt-check test-fast coverage ## Run all quality gates (pre-commit)
	@echo ""
	@echo "โœ… All quality gates passed!"
	@echo ""
	@echo "Summary:"
	@echo "  โœ… Linting: cargo clippy (zero warnings)"
	@echo "  โœ… Formatting: cargo fmt"
	@echo "  โœ… Tests: cargo test (all passing)"
	@echo "  โœ… Coverage: โ‰ฅ90% (see report above)"
	@echo ""
	@echo "Ready to commit!"

all: quality-gates ## Run full build pipeline

# ============================================================================
# PMAT INTEGRATION (v2.200.0+ features)
# ============================================================================

pmat-tdg: ## Run PMAT Technical Debt Grading (minimum: B+)
	@echo "๐Ÿ“Š PMAT Technical Debt Grading..."
	@pmat analyze tdg

pmat-analyze: ## Run comprehensive PMAT analysis
	@echo "๐Ÿ” PMAT Comprehensive Analysis..."
	@echo ""
	@echo "  [1/5] Complexity analysis..."
	@pmat analyze complexity --project-path . || true
	@echo ""
	@echo "  [2/5] SATD detection..."
	@pmat analyze satd --path . || true
	@echo ""
	@echo "  [3/5] Dead code analysis..."
	@pmat analyze dead-code --path . || true
	@echo ""
	@echo "  [4/5] Code duplication..."
	@pmat analyze duplicates || true
	@echo ""
	@echo "  [5/5] Known defects (unwrap calls)..."
	@pmat analyze defects --path . || true
	@echo ""
	@echo "โœ… PMAT analysis complete"

pmat-score: ## Calculate repository health score (minimum: 90/110)
	@echo "๐Ÿ† Repository Health Score..."
	@pmat repo-score || true

pmat-rust-score: ## Calculate Rust project score (0-211 scale, minimum: 150)
	@echo "๐Ÿฆ€ Rust Project Score (v2.171.0+)..."
	@mkdir -p target/pmat-reports
	@pmat rust-project-score --path . || echo "โš ๏ธ  Rust project score not available in this PMAT version"

pmat-rust-score-fast: ## Calculate Rust project score (fast mode, ~3 min)
	@echo "๐Ÿฆ€ Rust Project Score (fast mode)..."
	@pmat rust-project-score --path . || echo "โš ๏ธ  Rust project score not available in this PMAT version"

pmat-mutate: ## Run mutation testing with PMAT (AST-based)
	@echo "๐Ÿงฌ PMAT Mutation Testing..."
	@echo "โš ๏ธ  Note: PMAT mutation testing not available in this version"
	@echo "    Use 'make mutate' for cargo-mutants instead"

pmat-semantic-search: ## Index code for semantic search
	@echo "๐Ÿ” Indexing code for semantic search..."
	@pmat embed sync ./src || echo "โš ๏ธ  Semantic search not available in this PMAT version"

pmat-validate-docs: ## Validate documentation (hallucination detection - Phase 3.5)
	@echo "๐Ÿ“š Validating documentation accuracy (Phase 3.5)..."
	@echo ""
	@echo "Step 1: Generating deep context..."
	@pmat context --output deep_context.md --format llm-optimized
	@echo ""
	@echo "Step 2: Validating documentation files..."
	@pmat validate-readme \
		--targets README.md CLAUDE.md \
		--deep-context deep_context.md \
		--fail-on-contradiction \
		--verbose || { \
		echo ""; \
		echo "โŒ Documentation validation failed!"; \
		echo "   Fix contradictions and broken references before committing"; \
		exit 1; \
	}
	@echo ""
	@echo "โœ… Documentation validation complete - zero hallucinations!"

pmat-work-init: ## Initialize PMAT workflow system (v2.198.0)
	@echo "๐Ÿ”ง Initializing PMAT workflow system..."
	@echo "โš ๏ธ  Note: pmat work commands may not be available in this version"
	@echo "    Check: pmat --help | grep work"

pmat-quality-gate: ## Run comprehensive PMAT quality gate
	@echo "๐Ÿšฆ PMAT Quality Gate (comprehensive)..."
	@pmat quality-gates check || echo "โš ๏ธ  Quality gate check not available in this format"

pmat-context: ## Generate AI-ready project context
	@echo "๐Ÿค– Generating AI context..."
	@pmat context --output deep_context.md || echo "โš ๏ธ  Context generation not available"

pmat-all: pmat-tdg pmat-analyze pmat-score ## Run all PMAT checks (fast)

# Development helpers
dev: ## Run in development mode with auto-reload
	cargo watch -x 'test --all-features'

install-tools: ## Install required development tools
	cargo install cargo-llvm-cov || exit 1
	cargo install cargo-nextest || exit 1
	cargo install cargo-watch || exit 1
	cargo install cargo-mutants || exit 1
	cargo install criterion || exit 1
	cargo install renacer || exit 1
	cargo install mdbook || exit 1

# Documentation quality gates
validate-examples: ## Validate book examples meet EXTREME TDD quality
	@cargo run -p xtask -- validate-examples

build-book: ## Build mdBook documentation
	@echo "๐Ÿ“– Building book..."
	@mdbook build book/

serve-book: ## Serve book locally with live reload
	@echo "๐Ÿ“– Serving book at http://localhost:3000..."
	@mdbook serve book/

# Bashrs validation (shell script quality enforcement)
bashrs-lint-makefile: ## Lint Makefile with bashrs
	@echo "๐Ÿ” Linting Makefile with bashrs..."
	@bashrs make lint Makefile || true

bashrs-lint-scripts: ## Lint all shell scripts with bashrs
	@echo "๐Ÿ” Linting shell scripts with bashrs..."
	@if ls scripts/*.sh 1>/dev/null 2>&1; then \
		for script in scripts/*.sh; do \
			echo "  Linting $$script..."; \
			bashrs lint "$$script" || true; \
		done; \
	else \
		echo "  โ„น๏ธ  No shell scripts found (replaced with Rust xtask - A-grade quality)"; \
	fi

bashrs-audit: ## Audit shell script quality with bashrs
	@echo "๐Ÿ“Š Auditing shell scripts with bashrs..."
	@if ls scripts/*.sh 1>/dev/null 2>&1; then \
		for script in scripts/*.sh; do \
			echo "  Auditing $$script..."; \
			bashrs audit "$$script"; \
		done; \
	else \
		echo "  โ„น๏ธ  No shell scripts found (replaced with Rust xtask - A-grade quality)"; \
	fi

bashrs-all: bashrs-lint-makefile bashrs-lint-scripts bashrs-audit ## Run all bashrs quality checks

.DEFAULT_GOAL := help

# ============================================================================
# TRUENO-SPEC-013: Solidify Quality Gates with CUDA/WGPU Coverage
# ============================================================================

# Coverage targets for CUDA (SPEC Section 3.3)
coverage-cuda: ## Generate coverage with CUDA tests (requires NVIDIA GPU)
	@echo "๐Ÿ“Š Running coverage with CUDA tests (TRUENO-SPEC-013)..."
	@nvidia-smi > /dev/null 2>&1 || { echo "โŒ NVIDIA GPU required for CUDA coverage"; exit 1; }
	@which cargo-llvm-cov > /dev/null 2>&1 || (cargo install cargo-llvm-cov --locked || exit 1)
	@cargo llvm-cov clean --workspace
	@echo "โš™๏ธ  Temporarily disabling global cargo config (mold breaks coverage)..."
	@test -f ~/.cargo/config.toml && mv ~/.cargo/config.toml ~/.cargo/config.toml.cov-backup || true
	@echo ""
	@echo "๐Ÿš€ Phase 1: Fast tests (nextest parallel)..."
	@env PROPTEST_CASES=50 cargo llvm-cov --no-report \
		nextest --no-tests=warn --all-features --workspace \
		-E 'not test(/test_matmul_parallel_1024/)' \
		--profile coverage 2>&1 || true
	@echo ""
	@echo "๐ŸŽฎ Phase 2: CUDA tests (sequential, extended timeout)..."
	@env PROPTEST_CASES=10 cargo llvm-cov --no-report \
		test --features cuda --workspace \
		-- --test-threads=1 cuda driver 2>&1 || true
	@echo ""
	@echo "๐Ÿ“Š Generating combined CUDA coverage reports..."
	@cargo llvm-cov report --html --output-dir target/coverage/cuda-html
	@cargo llvm-cov report --lcov --output-path target/coverage/cuda-lcov.info
	@echo "โš™๏ธ  Restoring global cargo config..."
	@test -f ~/.cargo/config.toml.cov-backup && mv ~/.cargo/config.toml.cov-backup ~/.cargo/config.toml || true
	@echo ""
	@echo "๐Ÿ“Š CUDA Coverage Summary:"
	@echo "========================="
	@cargo llvm-cov report --summary-only
	@echo ""
	@echo "๐Ÿ’ก HTML report: target/coverage/cuda-html/index.html"

coverage-95: ## Enforce 95% coverage threshold (TRUENO-SPEC-013)
	@echo "๐Ÿ”’ Enforcing 95% coverage threshold (TRUENO-SPEC-013)..."
	@echo ""
	@# Check trueno core
	@TRUENO_COV=$$(cargo llvm-cov report --summary-only --ignore-filename-regex "trueno-gpu|xtask|simular" 2>/dev/null | grep "TOTAL" | awk '{print $$4}' | sed 's/%//'); \
	if [ -z "$$TRUENO_COV" ]; then echo "โŒ No coverage data. Run 'make coverage' first."; exit 1; fi; \
	echo "trueno:     $${TRUENO_COV}%"; \
	TRUENO_OK=$$(echo "$$TRUENO_COV >= 95" | bc -l 2>/dev/null || echo 0)
	@# Check trueno-gpu
	@GPU_COV=$$(cargo llvm-cov report --summary-only --ignore-filename-regex "trueno/src|xtask|simular" 2>/dev/null | grep "TOTAL" | awk '{print $$4}' | sed 's/%//'); \
	echo "trueno-gpu: $${GPU_COV:-N/A}%"
	@# Check workspace total
	@TOTAL_COV=$$(cargo llvm-cov report --summary-only --ignore-filename-regex "xtask|simular" 2>/dev/null | grep "TOTAL" | awk '{print $$4}' | sed 's/%//'); \
	echo "workspace:  $${TOTAL_COV}%"; \
	echo ""; \
	TRUENO_RESULT=$$(echo "$$TRUENO_COV >= 95" | bc -l 2>/dev/null || echo 0); \
	GPU_RESULT=$$(echo "$${GPU_COV:-0} >= 95" | bc -l 2>/dev/null || echo 0); \
	if [ "$$TRUENO_RESULT" = "1" ] && [ "$$GPU_RESULT" = "1" ]; then \
		echo "โœ… Coverage threshold met (โ‰ฅ95% for both crates)"; \
	else \
		echo "โŒ FAIL: Coverage below 95% threshold"; \
		echo "   trueno: $${TRUENO_COV}% (need 95%)"; \
		echo "   trueno-gpu: $${GPU_COV:-N/A}% (need 95%)"; \
		exit 1; \
	fi

# Smoke tests (SPEC Section 3.2)
smoke: ## Run E2E smoke tests (SIMD + WGPU + CUDA) - TRUENO-SPEC-013
	@echo "๐Ÿ”ฅ Running E2E smoke tests (TRUENO-SPEC-013)..."
	@echo ""
	@echo "Phase 1: SIMD backend validation..."
	@cargo test --test smoke_e2e smoke_simd -- --nocapture 2>&1 || true
	@echo ""
	@echo "Phase 2: WGPU backend validation..."
	@cargo test --test smoke_e2e smoke_wgpu --features gpu -- --nocapture 2>&1 || true
	@echo ""
	@echo "Phase 3: CUDA backend validation..."
	@nvidia-smi > /dev/null 2>&1 && \
		cargo test -p trueno-gpu --test smoke_e2e smoke_cuda --features cuda -- --nocapture 2>&1 || \
		echo "โš ๏ธ  CUDA not available, skipping"
	@echo ""
	@echo "โœ… Smoke tests complete"

smoke-full: ## Run full E2E smoke test suite with backend equivalence
	@echo "๐Ÿ”ฅ Running FULL E2E smoke test suite..."
	@cargo test --test smoke_e2e --all-features -- --nocapture
	@echo "โœ… Full smoke test suite passed"

# Pixel FKR Tests (SPEC Section 3.5)
pixel-scalar-fkr: ## Run scalar baseline pixel tests (generates golden images)
	@echo "๐ŸŽจ Running scalar-pixel-fkr (baseline truth)..."
	@cargo test --test pixel_fkr scalar_pixel_fkr -- --nocapture
	@echo "โœ… Scalar baseline generated"

pixel-simd-fkr: ## Run SIMD pixel tests against scalar baseline
	@echo "๐ŸŽจ Running simd-pixel-fkr..."
	@cargo test --test pixel_fkr simd_pixel_fkr -- --nocapture

pixel-wgpu-fkr: ## Run WGPU pixel tests against scalar baseline
	@echo "๐ŸŽจ Running wgpu-pixel-fkr..."
	@cargo test --test pixel_fkr wgpu_pixel_fkr --features gpu -- --nocapture

pixel-ptx-fkr: ## Run PTX pixel tests against scalar baseline (requires NVIDIA GPU)
	@echo "๐ŸŽจ Running ptx-pixel-fkr..."
	@nvidia-smi > /dev/null 2>&1 || { echo "โŒ NVIDIA GPU required"; exit 1; }
	@cargo test -p trueno-gpu --test pixel_fkr ptx_pixel_fkr --features "cuda gpu-pixels" -- --nocapture

pixel-fkr-all: pixel-scalar-fkr pixel-simd-fkr pixel-wgpu-fkr pixel-ptx-fkr ## Run all pixel FKR suites
	@echo "โœ… All pixel FKR suites passed"

# Combined quality gate (SPEC Section 8)
quality-spec-013: lint fmt-check test-fast coverage-95 smoke pixel-fkr-all ## Full TRUENO-SPEC-013 quality gate
	@echo ""
	@echo "โœ… TRUENO-SPEC-013 Quality Gate PASSED!"
	@echo ""
	@echo "Summary:"
	@echo "  โœ… Linting: cargo clippy (zero warnings)"
	@echo "  โœ… Formatting: cargo fmt"
	@echo "  โœ… Tests: cargo test (all passing)"
	@echo "  โœ… Coverage: โ‰ฅ95% (CUDA + WGPU)"
	@echo "  โœ… Smoke: E2E backend validation"
	@echo "  โœ… Pixel FKR: Visual regression tests"
	@echo ""

# ============================================================================
# RELEASE (crates.io publishing)
# ============================================================================

release-check: ## Verify package can be published (dry-run)
	@echo "๐Ÿ” Checking release readiness..."
	cargo publish --dry-run --allow-dirty
	@echo "โœ… Package ready for release"

release: ## Publish to crates.io (requires cargo login)
	@echo "๐Ÿš€ Publishing trueno to crates.io..."
	@echo "โš ๏ธ  Ensure all changes are committed!"
	cargo publish
	@echo "โœ… Published successfully"
	@echo "๐Ÿ“ฆ Create GitHub release: gh release create v$$(cargo pkgid | cut -d# -f2)"

release-tag: ## Create git tag for current version
	@VERSION=$$(cargo pkgid | cut -d# -f2) && \
	echo "๐Ÿท๏ธ  Creating tag v$$VERSION..." && \
	git tag -a "v$$VERSION" -m "Release v$$VERSION" && \
	git push origin "v$$VERSION" && \
	echo "โœ… Tag v$$VERSION pushed"