pmat 3.15.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
#![allow(clippy::expect_used, clippy::unwrap_used)]

use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;

fn main() {
    // Only watch files that actually exist - missing files cause constant rebuilds
    println!("cargo:rerun-if-changed=../scripts/install.sh");
    println!("cargo:rerun-if-changed=assets/vendor/");
    println!("cargo:rerun-if-changed=assets/demo/");
    println!("cargo:rerun-if-changed=../assets/demo/");
    println!("cargo:rerun-if-changed=templates/");
    println!("cargo:rerun-if-changed=src/schema/refactor_state.capnp");
    println!("cargo:rerun-if-env-changed=PMAT_FAST_BUILD");
    println!("cargo:rerun-if-env-changed=CARGO_LLVM_COV");
    println!("cargo:rerun-if-env-changed=SKIP_MCP_TABLES");

    // Declare custom cfg flags
    println!("cargo:rustc-check-cfg=cfg(cargo_publish)");
    println!("cargo:rustc-check-cfg=cfg(coverage)");
    println!("cargo:rustc-check-cfg=cfg(coverage_attr_stable)");
    println!("cargo:rustc-check-cfg=cfg(kani)");

    // GH-283: `coverage_attribute` was stabilized in Rust 1.94. Using
    // `#![feature(coverage_attribute)]` on 1.94+ stable triggers E0554.
    // Emit `coverage_attr_stable` so lib/bin headers can skip the feature
    // gate when the attribute is already stabilized.
    if rustc_is_at_least_1_94() {
        println!("cargo:rustc-cfg=coverage_attr_stable");
    }

    // Fast build mode for development - skip heavy operations but generate stubs
    if env::var("PMAT_FAST_BUILD").is_ok() {
        println!("cargo:warning=Fast build mode enabled - skipping heavy build operations");
        let out_dir = env::var("OUT_DIR").expect("OUT_DIR must be set");
        generate_stub_files(&out_dir);
        return;
    }

    // Emit CONTRACT_* env vars from binding.yaml for #[contract] proc macro
    emit_contract_env_vars();

    // KAIZEN-0178: Generate MCP tool schema metadata from JSON files.
    // Walks `mcp_tool_schemas/` and emits one authoritative Rust module so
    // handlers cannot silently advertise empty `inputSchema`. Missing JSON →
    // `include_str!` fails at compile time (the design constraint).
    generate_mcp_tool_schemas();

    // Verify critical dependencies at build time
    verify_dependency_versions();

    // Compress templates at build time
    compress_templates();

    // Download and compress assets for demo mode
    // Skip asset downloading during cargo publish to avoid modifying source directory
    if env::var("CARGO_FEATURE_DEMO").is_ok() && !is_publishing() {
        download_and_compress_assets();
        minify_demo_assets();
    }

    // Compile Cap'n Proto schema for MCP server
    // compile_capnp_schema(); // REMOVED: Cap'n Proto dependency eliminated (unused bloat)

    // Generate MCP discovery optimization tables
    // Skip during coverage builds to prevent hangs
    if env::var("CARGO_LLVM_COV").is_err() && env::var("SKIP_MCP_TABLES").is_err() {
        generate_mcp_discovery_tables();
    } else {
        println!("cargo:warning=Skipping MCP discovery table generation (coverage build or SKIP_MCP_TABLES set)");
        // Generate stub files to allow compilation
        let out_dir = env::var("OUT_DIR").expect("OUT_DIR must be set");
        generate_stub_files(&out_dir);
    }
}

/// GH-283: Return true when the compiler is Rust >= 1.94.
///
/// Parses `rustc --version` output (e.g. `rustc 1.94.1 (e408947bf 2026-03-25)`)
/// and compares the reported `minor` version against the 1.94 cutoff. Returns
/// false on any parse error so older compilers keep the feature gate.
fn rustc_is_at_least_1_94() -> bool {
    let rustc = env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
    let Ok(output) = std::process::Command::new(rustc).arg("--version").output() else {
        return false;
    };
    if !output.status.success() {
        return false;
    }
    let Ok(stdout) = std::str::from_utf8(&output.stdout) else {
        return false;
    };
    let Some(version) = stdout.split_whitespace().nth(1) else {
        return false;
    };
    let mut parts = version.split('.');
    let Some(major) = parts.next().and_then(|s| s.parse::<u32>().ok()) else {
        return false;
    };
    let Some(minor) = parts.next().and_then(|s| s.parse::<u32>().ok()) else {
        return false;
    };
    major > 1 || (major == 1 && minor >= 94)
}

/// Check if we're in a cargo publish context
fn is_publishing() -> bool {
    // During cargo publish, the package is extracted to a temp directory
    let is_publish = env::var("CARGO_PKG_VERSION").is_ok()
        && env::current_dir()
            .map(|dir| dir.to_string_lossy().contains("/target/package/"))
            .unwrap_or(false);

    if is_publish {
        println!("cargo:rustc-cfg=cargo_publish");
    }

    is_publish
}

/// Verifies critical dependencies exist in Cargo.lock
///
/// # Panics
///
/// Panics if Cargo.lock doesn't exist or critical dependencies are missing
fn verify_dependency_versions() {
    // In a workspace, Cargo.lock is in the parent directory
    let lock_path = if Path::new("../Cargo.lock").exists() {
        "../Cargo.lock"
    } else {
        "Cargo.lock"
    };
    let lock_content = fs::read_to_string(lock_path).expect("Cargo.lock must exist");

    // Critical dependencies for your MCP server
    let critical_deps = [
        "tokio",     // Async runtime
        "serde",     // Serialization
        "minijinja", // Template engine
    ];

    for dep in &critical_deps {
        assert!(
            lock_content.contains(&format!("name = \"{dep}\"")),
            "Critical dependency {dep} not found"
        );
    }
}

fn download_and_compress_assets() {
    setup_asset_directories();
    let assets = get_asset_definitions();
    process_assets(&assets);
    set_asset_hash_env();
}

fn setup_asset_directories() {
    let vendor_dir = Path::new("assets/vendor");
    let demo_dir = Path::new("assets/demo");
    let _ = fs::create_dir_all(vendor_dir);
    let _ = fs::create_dir_all(demo_dir);
}

const fn get_asset_definitions() -> [(&'static str, &'static str); 4] {
    [
        (
            "https://unpkg.com/gridjs@6.0.6/dist/gridjs.umd.js",
            "gridjs.min.js",
        ),
        (
            "https://unpkg.com/gridjs@6.0.6/dist/theme/mermaid.min.css",
            "gridjs-mermaid.min.css",
        ),
        (
            "https://unpkg.com/mermaid@latest/dist/mermaid.min.js",
            "mermaid.min.js",
        ),
        ("https://unpkg.com/d3@latest/dist/d3.min.js", "d3.min.js"),
    ]
}

fn process_assets(assets: &[(&str, &str)]) {
    let vendor_dir = Path::new("assets/vendor");

    for (url, filename) in assets {
        let path = vendor_dir.join(filename);
        let gz_path = vendor_dir.join(format!("{filename}.gz"));
        let hash_path = vendor_dir.join(format!("{filename}.hash"));

        if should_skip_asset(&path, &gz_path, &hash_path) {
            println!("cargo:warning=Skipping unchanged asset: {filename} (O(1) hash check)");
            continue;
        }

        ensure_asset_downloaded(&path, &gz_path, url, filename);
        compress_asset(&path, &gz_path, &hash_path, filename);
    }
}

fn should_skip_asset(source_path: &Path, gz_path: &Path, hash_path: &Path) -> bool {
    // O(1) optimization: Skip if output exists AND source hasn't changed
    if !gz_path.exists() || !source_path.exists() {
        return false;
    }

    // Hash-based check: O(1) for unchanged files
    !has_file_changed(source_path, hash_path)
}

fn ensure_asset_downloaded(path: &Path, gz_path: &Path, url: &str, filename: &str) {
    if !path.exists() {
        // Check if we're in a docs.rs build environment
        if env::var("DOCS_RS").is_ok() {
            println!("cargo:warning=Skipping asset download in docs.rs environment: {filename}");
            // Create a placeholder file for docs.rs builds
            let _ = fs::write(path, b"/* Asset skipped in docs.rs build */");
            // Also create an empty gzipped placeholder to satisfy include_bytes!
            let _ = fs::write(gz_path, b"");
        } else {
            download_asset(url, path, filename);
        }
    }
}

fn download_asset(url: &str, path: &Path, filename: &str) {
    println!("cargo:warning=Downloading {filename} from {url}");

    match ureq::get(url).call() {
        Ok(mut response) => match response.body_mut().read_to_vec() {
            Ok(content) => {
                if let Err(e) = fs::write(path, &content) {
                    println!("cargo:warning=Failed to write {filename}: {e}");
                }
            }
            Err(e) => {
                println!("cargo:warning=Failed to read {filename}: {e}");
                let _ = fs::write(path, b"/* Asset download failed during build */");
            }
        },
        Err(e) => {
            handle_download_failure(&e, path, filename);
        }
    }
}

fn handle_download_failure(e: &ureq::Error, path: &Path, filename: &str) {
    println!("cargo:warning=Failed to download {filename}: {e}. Using placeholder.");
    // Create a placeholder file
    let _ = fs::write(path, b"/* Asset download failed during build */");
}

fn compress_asset(path: &Path, gz_path: &Path, hash_path: &Path, filename: &str) {
    if !path.exists() {
        return;
    }

    let Ok(input) = fs::read(path) else { return };

    let Some(compressed) = create_compressed_data(&input) else {
        return;
    };

    write_compressed_file(gz_path, &compressed, filename, input.len());

    // Save hash for O(1) skip detection on next build
    if let Some(hash) = calculate_file_hash(path) {
        let _ = write_hash_file(hash_path, &hash);
    }
}

fn create_compressed_data(input: &[u8]) -> Option<Vec<u8>> {
    use flate2::write::GzEncoder;
    use flate2::Compression;

    let mut encoder = GzEncoder::new(Vec::new(), Compression::best());
    encoder.write_all(input).ok()?;
    encoder.finish().ok()
}

fn write_compressed_file(gz_path: &Path, compressed: &[u8], filename: &str, original_size: usize) {
    if fs::write(gz_path, compressed).is_ok() {
        if let Ok(metadata) = fs::metadata(gz_path) {
            println!(
                "cargo:warning=Compressed {} ({} -> {} bytes)",
                filename,
                original_size,
                metadata.len()
            );
        }
    }
}

fn set_asset_hash_env() {
    let hash = calculate_asset_hash();
    println!("cargo:rustc-env=ASSET_HASH={hash}");
}

/// Compresses template files at build time
///
/// # Panics
///
/// Panics if `OUT_DIR` environment variable is not set
fn compress_templates() {
    let templates_dir = Path::new("templates");

    if !validate_templates_directory(templates_dir) {
        return;
    }

    // O(1) optimization: Skip if templates haven't changed
    let out_dir = env::var("OUT_DIR").expect("OUT_DIR must be set");
    let hash_path = Path::new(&out_dir).join("templates.hash");

    let current_hash = calculate_templates_hash(templates_dir);

    // O(1) skip when available (standard-deps enabled and hash matches)
    if let Some(h) = &current_hash {
        if let Some(stored_hash) = read_stored_hash(&hash_path) {
            if *h == stored_hash {
                println!("cargo:warning=Skipping unchanged templates (O(1) hash check)");
                return;
            }
        }
    }

    let (templates, total_original) = load_all_templates(templates_dir);

    if templates.is_empty() {
        println!("cargo:warning=No templates found for compression");
        return;
    }

    compress_and_save_templates(&templates, total_original);

    // Save hash for O(1) skip detection on next build (best-effort)
    if let Some(h) = current_hash {
        let _ = write_hash_file(&hash_path, &h);
    }
}

/// Calculate combined hash of all template files.
///
/// Returns `None` under `--no-default-features` (no `sha2` in build-deps);
/// callers fall back to unconditional recompression, losing only the O(1)
/// skip optimization.
#[cfg(feature = "standard-deps")]
fn calculate_templates_hash(templates_dir: &Path) -> Option<String> {
    use sha2::{Digest, Sha256};

    let mut hasher = Sha256::new();
    let mut file_count = 0;

    if let Ok(files) = collect_template_files(templates_dir) {
        // Sort files for deterministic hashing
        let mut sorted_files = files;
        sorted_files.sort();

        for file in sorted_files {
            if let Ok(content) = fs::read(&file) {
                // Include filename in hash for renames
                hasher.update(file.to_string_lossy().as_bytes());
                hasher.update(&content);
                file_count += 1;
            }
        }
    }

    if file_count == 0 {
        return None;
    }

    Some(format!("{:x}", hasher.finalize()))
}

#[cfg(not(feature = "standard-deps"))]
fn calculate_templates_hash(_templates_dir: &Path) -> Option<String> {
    None
}

/// Validate templates directory exists (cognitive complexity ≤2)
fn validate_templates_directory(templates_dir: &Path) -> bool {
    if templates_dir.exists() {
        true
    } else {
        println!("cargo:warning=Templates directory not found, skipping compression");
        false
    }
}

/// Load all template files and return templates map with total size (cognitive complexity ≤6)
fn load_all_templates(templates_dir: &Path) -> (std::collections::HashMap<String, String>, usize) {
    use std::collections::HashMap;
    let mut templates = HashMap::new();
    let mut total_original = 0usize;

    if let Ok(entries) = collect_template_files(templates_dir) {
        for entry in entries {
            if let Some((name, content)) = read_template_file(&entry) {
                total_original += content.len();
                templates.insert(name, content);
            }
        }
    }

    (templates, total_original)
}

/// Compress templates and save to output file (cognitive complexity ≤8)
fn compress_and_save_templates(
    templates: &std::collections::HashMap<String, String>,
    total_original: usize,
) {
    let serialized = serde_json_to_string(templates);

    if let Some(compressed) = create_compressed_data(serialized.as_bytes()) {
        let total_compressed = compressed.len();
        let template_code = generate_template_output(&compressed, templates.len());

        write_compressed_templates_file(&template_code);
        print_compression_stats(templates.len(), total_original, total_compressed);
    }
}

/// Generate template output code (cognitive complexity ≤2)
fn generate_template_output(compressed: &[u8], template_count: usize) -> String {
    let compressed_hex = generate_hex_string(compressed);
    generate_template_code(&compressed_hex, template_count)
}

/// Write compressed templates to output file (cognitive complexity ≤2)
fn write_compressed_templates_file(template_code: &str) {
    let out_dir = env::var("OUT_DIR")
        .expect("OUT_DIR environment variable must be set by Cargo during build");
    let dest_path = Path::new(&out_dir).join("compressed_templates.rs");
    let _ = write_if_changed(&dest_path, template_code);
}

/// Print compression statistics (cognitive complexity ≤3)
fn print_compression_stats(template_count: usize, total_original: usize, total_compressed: usize) {
    #[allow(clippy::cast_precision_loss)]
    let reduction_percent = (1.0 - total_compressed as f64 / total_original as f64) * 100.0;

    println!(
        "cargo:warning=Compressed {template_count} templates ({total_original} -> {total_compressed} bytes, {reduction_percent:.1}% reduction)"
    );
}

/// Collects template files from directory
///
/// # Errors
///
/// Returns error if directory cannot be read
fn collect_template_files(dir: &Path) -> Result<Vec<std::path::PathBuf>, std::io::Error> {
    let mut files = Vec::new();
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            files.extend(collect_template_files(&path)?);
        } else if path
            .extension()
            .is_some_and(|ext| ext == "hbs" || ext == "json")
        {
            files.push(path);
        }
    }
    Ok(files)
}

fn read_template_file(path: &Path) -> Option<(String, String)> {
    let name = path
        .strip_prefix("templates")
        .ok()?
        .to_string_lossy()
        .to_string();
    let content = fs::read_to_string(path).ok()?;
    Some((name, content))
}

fn serde_json_to_string<T: serde::Serialize>(value: &T) -> String {
    serde_json::to_string(value).unwrap_or_else(|_| "{}".to_string())
}

fn generate_hex_string(data: &[u8]) -> String {
    data.iter().fold(String::new(), |mut acc, b| {
        use std::fmt::Write;
        let _ = write!(acc, "{b:02x}");
        acc
    })
}

fn generate_template_code(hex: &str, count: usize) -> String {
    format!(
        r#"// Auto-generated compressed templates
use std::collections::HashMap;
use std::sync::LazyLock;

const COMPRESSED_TEMPLATES: &str = "{hex}";

fn hex_decode_templates(s: &str) -> Vec<u8> {{
    (0..s.len())
        .step_by(2)
        .filter_map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok())
        .collect()
}}

pub static TEMPLATES: LazyLock<HashMap<String, String>> = LazyLock::new(|| {{
    use flate2::read::GzDecoder;
    use std::io::Read;

    let compressed = hex_decode_templates(COMPRESSED_TEMPLATES);
    let mut decoder = GzDecoder::new(&compressed[..]);
    let mut decompressed = String::new();
    decoder.read_to_string(&mut decompressed).expect("Decompression failed");

    serde_json::from_str(&decompressed).expect("Valid JSON")
}});

// Template count: {count}
"#
    )
}

fn minify_demo_assets() {
    println!("cargo:warning=Minifying demo assets...");

    let demo_dir = Path::new("../assets/demo");
    let output_dir = Path::new("assets/demo");
    let _ = fs::create_dir_all(output_dir);

    // Minify JavaScript
    minify_js_file(&demo_dir.join("app.js"), &output_dir.join("app.min.js"));

    // Minify CSS
    minify_css_file(
        &demo_dir.join("style.css"),
        &output_dir.join("style.min.css"),
    );

    // Copy other demo assets as-is
    copy_demo_asset(
        &demo_dir.join("favicon.ico"),
        &output_dir.join("favicon.ico"),
    );
}

fn minify_js_file(input_path: &Path, output_path: &Path) {
    if !input_path.exists() {
        println!(
            "cargo:warning=JavaScript file not found: {}",
            input_path.display()
        );
        return;
    }

    // O(1) optimization: Skip if unchanged
    let Some(file_name) = output_path.file_name() else {
        println!("cargo:warning=Invalid output path: no file name");
        return;
    };
    let hash_path = output_path.with_file_name(format!("{}.hash", file_name.to_string_lossy()));
    if output_path.exists() && !has_file_changed(input_path, &hash_path) {
        println!(
            "cargo:warning=Skipping unchanged JavaScript: {} (O(1) hash check)",
            input_path.display()
        );
        return;
    }

    let content = match fs::read_to_string(input_path) {
        Ok(content) => content,
        Err(e) => {
            println!("cargo:warning=Failed to read JS file: {e}");
            return;
        }
    };

    let minified = simple_js_minify(&content);

    if let Err(e) = fs::write(output_path, &minified) {
        println!("cargo:warning=Failed to write minified JS: {e}");
        return;
    }

    // Save hash for O(1) skip detection on next build
    if let Some(hash) = calculate_file_hash(input_path) {
        let _ = write_hash_file(&hash_path, &hash);
    }

    #[allow(clippy::cast_precision_loss)]
    let reduction = (1.0 - minified.len() as f64 / content.len() as f64) * 100.0;
    println!(
        "cargo:warning=Minified JavaScript: {} -> {} bytes ({:.1}% reduction)",
        content.len(),
        minified.len(),
        reduction
    );
}

fn minify_css_file(input_path: &Path, output_path: &Path) {
    if !input_path.exists() {
        println!("cargo:warning=CSS file not found: {}", input_path.display());
        return;
    }

    // O(1) optimization: Skip if unchanged
    let Some(file_name) = output_path.file_name() else {
        println!("cargo:warning=Invalid output path: no file name");
        return;
    };
    let hash_path = output_path.with_file_name(format!("{}.hash", file_name.to_string_lossy()));
    if output_path.exists() && !has_file_changed(input_path, &hash_path) {
        println!(
            "cargo:warning=Skipping unchanged CSS: {} (O(1) hash check)",
            input_path.display()
        );
        return;
    }

    let content = match fs::read_to_string(input_path) {
        Ok(content) => content,
        Err(e) => {
            println!("cargo:warning=Failed to read CSS file: {e}");
            return;
        }
    };

    let minified = simple_css_minify(&content);

    if let Err(e) = fs::write(output_path, &minified) {
        println!("cargo:warning=Failed to write minified CSS: {e}");
        return;
    }

    // Save hash for O(1) skip detection on next build
    if let Some(hash) = calculate_file_hash(input_path) {
        let _ = write_hash_file(&hash_path, &hash);
    }

    #[allow(clippy::cast_precision_loss)]
    let reduction = (1.0 - minified.len() as f64 / content.len() as f64) * 100.0;
    println!(
        "cargo:warning=Minified CSS: {} -> {} bytes ({:.1}% reduction)",
        content.len(),
        minified.len(),
        reduction
    );
}

fn copy_demo_asset(input_path: &Path, output_path: &Path) {
    if input_path.exists() {
        let _ = fs::copy(input_path, output_path);
    }
}

fn simple_js_minify(content: &str) -> String {
    content
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty() && !line.starts_with("//"))
        .collect::<Vec<_>>()
        .join(" ")
        .replace("; ", ";")
        .replace(", ", ",")
        .replace(" = ", "=")
        .replace(" + ", "+")
        .replace(" { ", "{")
        .replace(" } ", "}")
        .replace("{ ", "{")
        .replace(" }", "}")
}

fn simple_css_minify(content: &str) -> String {
    content
        .lines()
        .map(|line| {
            let line = line.trim();
            // Remove CSS comments
            if line.starts_with("/*") && line.ends_with("*/") {
                ""
            } else {
                line
            }
        })
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join("")
        .replace("; ", ";")
        .replace(": ", ":")
        .replace(", ", ",")
        .replace(" { ", "{")
        .replace(" } ", "}")
        .replace("{ ", "{")
        .replace(" }", "}")
}

fn calculate_asset_hash() -> String {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();

    // Hash all asset files
    if let Ok(entries) = fs::read_dir("assets/vendor") {
        for entry in entries.filter_map(Result::ok) {
            if let Ok(content) = fs::read(entry.path()) {
                content.hash(&mut hasher);
            }
        }
    }

    // Also hash demo assets
    if let Ok(entries) = fs::read_dir("assets/demo") {
        for entry in entries.filter_map(Result::ok) {
            if let Ok(content) = fs::read(entry.path()) {
                content.hash(&mut hasher);
            }
        }
    }

    format!("{:x}", hasher.finish())
}

/// Calculate SHA256 hash of a file for change detection.
///
/// Returns `None` under `--no-default-features` (no `sha2` in build-deps);
/// callers treat this as "changed" and reprocess unconditionally.
#[cfg(feature = "standard-deps")]
fn calculate_file_hash(path: &Path) -> Option<String> {
    use sha2::{Digest, Sha256};

    let content = fs::read(path).ok()?;
    let mut hasher = Sha256::new();
    hasher.update(&content);
    Some(format!("{:x}", hasher.finalize()))
}

#[cfg(not(feature = "standard-deps"))]
fn calculate_file_hash(_path: &Path) -> Option<String> {
    None
}

/// Read stored hash from .hash file
fn read_stored_hash(hash_path: &Path) -> Option<String> {
    fs::read_to_string(hash_path)
        .ok()
        .map(|s| s.trim().to_string())
}

/// Write hash to .hash file
fn write_hash_file(hash_path: &Path, hash: &str) -> bool {
    fs::write(hash_path, hash).is_ok()
}

/// Check if source file has changed by comparing hashes
fn has_file_changed(source_path: &Path, hash_path: &Path) -> bool {
    // If hash file doesn't exist, file has "changed" (needs processing)
    if !hash_path.exists() {
        return true;
    }

    // Calculate current hash
    let Some(current_hash) = calculate_file_hash(source_path) else {
        return true; // Can't read source, assume changed
    };

    // Compare with stored hash
    let Some(stored_hash) = read_stored_hash(hash_path) else {
        return true; // Can't read stored hash, assume changed
    };

    current_hash != stored_hash
}

/// Compiles Cap'n Proto schema for MCP server
/// DISABLED: Cap'n Proto removed (unused bloat dependency)
#[allow(dead_code)]
fn compile_capnp_schema() {
    // REMOVED: capnpc dependency eliminated (unused)
    // Reason: 0 references found in codebase, removing bloat
    println!("cargo:warning=Cap'n Proto schema compilation skipped (dependency removed)");
}

/// Generate MCP discovery optimization tables for <10ms initialization
fn generate_mcp_discovery_tables() {
    println!("cargo:warning=Generating MCP discovery optimization tables");

    let out_dir = env::var("OUT_DIR").expect("OUT_DIR must be set");

    // Generate tool registry
    generate_tool_registry(&out_dir);

    // Generate alias table
    generate_alias_table(&out_dir);

    // Generate trigram index
    generate_trigram_index(&out_dir);
}

/// Generate static PHF map of all MCP tools for zero-copy initialization
fn generate_tool_registry(out_dir: &str) {
    let dest_path = Path::new(out_dir).join("tool_registry.rs");

    // Tool definitions from the current MCP server
    let tools = vec![
        (
            "analyze_complexity",
            "Analyze code complexity metrics (cyclomatic, cognitive)",
            vec!["complexity", "analyze", "metrics"],
        ),
        (
            "analyze_satd",
            "Find self-admitted technical debt in comments",
            vec!["satd", "debt", "todo", "fixme"],
        ),
        (
            "analyze_dead_code",
            "Detect unused functions and variables",
            vec!["dead", "unused", "code"],
        ),
        (
            "analyze_dag",
            "Generate dependency graphs and visualizations",
            vec!["dependency", "graph", "dag", "architecture"],
        ),
        (
            "analyze_deep_context",
            "Generate comprehensive codebase context",
            vec!["context", "summary", "analysis"],
        ),
        (
            "analyze_big_o",
            "Analyze algorithmic complexity",
            vec!["big-o", "algorithm", "performance"],
        ),
        (
            "refactor.start",
            "Begin refactoring workflow",
            vec!["refactor", "start", "begin"],
        ),
        (
            "refactor.nextIteration",
            "Continue refactoring process",
            vec!["refactor", "next", "continue"],
        ),
        (
            "refactor.getState",
            "Get current refactoring state",
            vec!["refactor", "state", "status"],
        ),
        (
            "refactor.stop",
            "End refactoring workflow",
            vec!["refactor", "stop", "end"],
        ),
        (
            "quality_gate",
            "Run comprehensive quality analysis",
            vec!["quality", "gate", "check"],
        ),
        (
            "quality_proxy",
            "Intercept and validate code changes",
            vec!["quality", "proxy", "validate"],
        ),
        (
            "git_operation",
            "Execute git operations",
            vec!["git", "version", "control"],
        ),
        (
            "generate_context",
            "Generate AI-optimized context",
            vec!["generate", "context", "ai"],
        ),
        (
            "scaffold_project",
            "Create project scaffolding",
            vec!["scaffold", "create", "generate", "project"],
        ),
        // Sprint 31: TDG System MCP Tools
        (
            "tdg_system_diagnostics",
            "Get comprehensive TDG system diagnostics and health monitoring",
            vec!["tdg", "diagnostics", "health", "monitoring", "system"],
        ),
        (
            "tdg_storage_management",
            "Manage TDG storage operations (stats, cleanup, flush, migrate)",
            vec!["tdg", "storage", "management", "cleanup", "migrate"],
        ),
        (
            "tdg_analyze_with_storage",
            "Analyze files using transactional TDG storage with caching",
            vec!["tdg", "analyze", "storage", "transactional", "cache"],
        ),
        (
            "tdg_performance_metrics",
            "Get real-time TDG performance metrics and adaptive thresholds",
            vec!["tdg", "performance", "metrics", "adaptive", "thresholds"],
        ),
        (
            "tdg_configure_storage",
            "Configure and validate TDG storage backends",
            vec!["tdg", "configure", "storage", "backend", "sled", "rocksdb"],
        ),
        (
            "tdg_health_check",
            "Comprehensive TDG system health check with recommendations",
            vec!["tdg", "health", "check", "recommendations", "status"],
        ),
        // Phase 4: Organizational Intelligence Integration
        (
            "generate_defect_aware_prompt",
            "Generate context-aware AI prompts from organizational intelligence and defect patterns",
            vec!["prompt", "defect", "organizational", "intelligence", "oip", "ai"],
        ),
    ];

    let mut registry_code = String::from(
        "// Auto-generated tool registry for zero-copy MCP initialization\n\n\
         #[derive(Debug, Clone)]\n\
         pub struct ToolMeta {\n\
             pub name: &'static str,\n\
             pub description: &'static str,\n\
             pub keywords: &'static [&'static str],\n\
         }\n\n\
         pub static TOOL_REGISTRY: std::sync::LazyLock<std::collections::HashMap<&'static str, ToolMeta>> = std::sync::LazyLock::new(|| {\n\
             let mut m = std::collections::HashMap::new();\n"
    );

    for (name, desc, keywords) in &tools {
        registry_code.push_str(&format!(
            "    m.insert(\"{name}\", ToolMeta {{\n\
                 name: \"{name}\",\n\
                 description: \"{desc}\",\n\
                 keywords: &{keywords:?},\n\
             }});\n"
        ));
    }

    registry_code.push_str("    m\n});\n");

    if let Err(e) = write_if_changed(&dest_path, &registry_code) {
        println!("cargo:warning=Failed to write tool registry: {e}");
    }
}

/// Generate alias dispatch table from empirical usage patterns
fn generate_alias_table(out_dir: &str) {
    let dest_path = Path::new(out_dir).join("alias_table.rs");

    let aliases = vec![
        (
            "analyze_complexity",
            vec![
                "complexity",
                "cyclomatic",
                "cognitive",
                "analyze code",
                "code complexity",
                "mccabe",
                "sonar",
                "analyze",
                "metrics",
            ],
        ),
        (
            "analyze_satd",
            vec![
                "debt",
                "technical debt",
                "todo",
                "fixme",
                "hack",
                "satd",
                "find debt",
                "find todo",
                "self admitted",
                "admitted debt",
            ],
        ),
        (
            "analyze_dag",
            vec![
                "dependency",
                "dependencies",
                "graph",
                "visualize",
                "diagram",
                "show dependencies",
                "dependency graph",
                "architecture",
                "dag",
            ],
        ),
        (
            "scaffold_project",
            vec![
                "scaffold",
                "create",
                "generate",
                "make",
                "new",
                "init",
                "create project",
                "generate project",
                "new project",
                "project template",
            ],
        ),
        (
            "generate_context",
            vec![
                "context",
                "summary",
                "generate context",
                "ai context",
                "codebase context",
                "analyze codebase",
                "understand code",
            ],
        ),
        (
            "quality_gate",
            vec![
                "quality",
                "check quality",
                "quality check",
                "gate",
                "validate",
                "quality analysis",
                "code quality",
                "standards",
            ],
        ),
        (
            "refactor.start",
            vec![
                "refactor",
                "refactoring",
                "start refactor",
                "begin refactor",
                "improve code",
                "clean code",
                "restructure",
            ],
        ),
        (
            "git_operation",
            vec![
                "git",
                "version control",
                "commit",
                "branch",
                "merge",
                "git command",
                "source control",
            ],
        ),
        // Sprint 31: TDG System MCP Tool Aliases
        (
            "tdg_system_diagnostics",
            vec![
                "tdg diagnostics",
                "system health",
                "tdg health",
                "monitoring",
                "system status",
                "tdg status",
                "diagnostics",
                "health check",
            ],
        ),
        (
            "tdg_storage_management",
            vec![
                "tdg storage",
                "storage stats",
                "cleanup storage",
                "storage cleanup",
                "flush storage",
                "migrate storage",
                "storage migrate",
                "tdg cache",
            ],
        ),
        (
            "tdg_analyze_with_storage",
            vec![
                "tdg analyze",
                "analyze tdg",
                "transactional analysis",
                "cached analysis",
                "storage analysis",
                "tdg file",
            ],
        ),
        (
            "tdg_performance_metrics",
            vec![
                "tdg performance",
                "performance metrics",
                "adaptive thresholds",
                "tdg metrics",
                "system performance",
                "threshold management",
            ],
        ),
        (
            "tdg_configure_storage",
            vec![
                "configure tdg",
                "storage config",
                "backend config",
                "tdg backend",
                "sled config",
                "rocksdb config",
                "storage backend",
            ],
        ),
        (
            "tdg_health_check",
            vec![
                "tdg health",
                "system health",
                "health status",
                "tdg check",
                "system check",
                "health recommendations",
            ],
        ),
        // Phase 4: Organizational Intelligence Integration
        (
            "generate_defect_aware_prompt",
            vec![
                "defect aware",
                "ai prompt",
                "organizational intelligence",
                "oip prompt",
                "context prompt",
                "defect patterns",
                "quality prompt",
                "intelligent prompt",
                "org intelligence",
                "prompt generation",
            ],
        ),
    ];

    let mut alias_code = String::from(
        "// Auto-generated alias table for MCP tool discovery\n\n\
         pub static ALIAS_TABLE: std::sync::LazyLock<std::collections::HashMap<&'static str, Vec<&'static str>>> = std::sync::LazyLock::new(|| {\n\
             let mut m = std::collections::HashMap::new();\n"
    );

    for (tool, tool_aliases) in &aliases {
        alias_code.push_str(&format!(
            "    m.insert(\"{tool}\", vec!{tool_aliases:?});\n"
        ));
    }

    alias_code.push_str("    m\n});\n");

    if let Err(e) = write_if_changed(&dest_path, &alias_code) {
        println!("cargo:warning=Failed to write alias table: {e}");
    }
}

/// Generate trigram index for fuzzy matching
fn generate_trigram_index(out_dir: &str) {
    let dest_path = Path::new(out_dir).join("trigram_index.rs");

    let trigram_code = r#"// Auto-generated trigram index for fuzzy matching
pub struct TrigramIndex;

impl TrigramIndex {
    #[inline(always)]
    pub fn pack_trigram(s: &[u8]) -> u32 {
        if s.len() < 3 { return 0; }
        (s[0] as u32) | ((s[1] as u32) << 8) | ((s[2] as u32) << 16)
    }
    
    pub fn similarity_score(&self, query: &str, candidate: &str) -> f32 {
        let q_bytes = query.to_lowercase().into_bytes();
        let c_bytes = candidate.to_lowercase().into_bytes();
        
        if q_bytes.len() < 3 || c_bytes.len() < 3 {
            return 0.0;
        }
        
        // Collect query trigrams
        let mut q_trigrams = Vec::with_capacity(q_bytes.len().saturating_sub(2));
        for i in 0..q_bytes.len().saturating_sub(2) {
            q_trigrams.push(Self::pack_trigram(&q_bytes[i..i+3]));
        }
        
        // Collect candidate trigrams
        let mut c_trigrams = Vec::with_capacity(c_bytes.len().saturating_sub(2));
        for i in 0..c_bytes.len().saturating_sub(2) {
            c_trigrams.push(Self::pack_trigram(&c_bytes[i..i+3]));
        }
        
        // Count matches
        let mut matches = 0;
        for q_tri in &q_trigrams {
            if c_trigrams.contains(q_tri) {
                matches += 1;
            }
        }
        
        // Jaccard similarity coefficient
        let union_size = q_trigrams.len() + c_trigrams.len() - matches;
        if union_size == 0 { return 0.0; }
        
        matches as f32 / union_size as f32
    }
    
    pub fn find_best_match<'a>(&self, query: &str, candidates: &[(&'a str, &str)]) -> Option<(&'a str, f32)> {
        let mut best_match = ("", 0.0f32);
        
        for (name, description) in candidates {
            // Check both name and description
            let name_score = self.similarity_score(query, name);
            let desc_score = self.similarity_score(query, description) * 0.7; // Weight description lower
            let combined = name_score.max(desc_score);
            
            if combined > best_match.1 {
                best_match = (name, combined);
            }
        }
        
        if best_match.1 > 0.4 {  // Empirically determined threshold
            Some(best_match)
        } else {
            None
        }
    }
}
"#;

    if let Err(e) = write_if_changed(&dest_path, trigram_code) {
        println!("cargo:warning=Failed to write trigram index: {e}");
    }
}

/// Generate stub files for coverage builds to avoid compilation errors
/// Write file only if content changed (avoids triggering recompilation)
fn write_if_changed(path: &Path, content: &str) -> Result<(), std::io::Error> {
    if path.exists() {
        if let Ok(existing) = fs::read_to_string(path) {
            if existing == content {
                // Content unchanged - skip write to preserve mtime
                return Ok(());
            }
        }
    }
    // Content changed or file doesn't exist - write it
    println!("cargo:warning=Writing changed file: {}", path.display());
    fs::write(path, content)
}

fn write_stub(out_dir: &str, filename: &str, content: &str) {
    let dest = Path::new(out_dir).join(filename);
    if let Err(e) = write_if_changed(&dest, content) {
        println!("cargo:warning=Failed to write {filename} stub: {e}");
    }
}

fn generate_stub_files(out_dir: &str) {
    generate_stub_tool_registry(out_dir);
    generate_stub_alias_table(out_dir);
    generate_stub_trigram_index(out_dir);
    generate_stub_compressed_templates(out_dir);
}

fn generate_stub_tool_registry(out_dir: &str) {
    let tool_registry = r#"
// Functional tool registry for coverage builds
use std::collections::HashMap;
use std::sync::LazyLock;

pub struct ToolMeta {
    pub name: &'static str,
    pub description: &'static str,
    pub keywords: &'static [&'static str],
}

pub static TOOL_REGISTRY: LazyLock<HashMap<&'static str, ToolMeta>> =
    LazyLock::new(|| {
        let mut m = HashMap::new();
        m.insert("analyze_complexity", ToolMeta {
            name: "analyze_complexity",
            description: "Analyze code complexity metrics (cyclomatic, cognitive)",
            keywords: &["complexity", "analyze", "metrics"],
        });
        m.insert("quality_gate", ToolMeta {
            name: "quality_gate",
            description: "Run comprehensive quality analysis",
            keywords: &["quality", "gate", "check"],
        });
        m.insert("scaffold_project", ToolMeta {
            name: "scaffold_project",
            description: "Create project scaffolding",
            keywords: &["scaffold", "create", "generate", "project"],
        });
        m.insert("analyze_satd", ToolMeta {
            name: "analyze_satd",
            description: "Find self-admitted technical debt in comments",
            keywords: &["satd", "debt", "todo", "fixme"],
        });
        m.insert("analyze_dag", ToolMeta {
            name: "analyze_dag",
            description: "Generate dependency graphs and visualizations",
            keywords: &["dependency", "graph", "dag", "architecture"],
        });
        m.insert("generate_context", ToolMeta {
            name: "generate_context",
            description: "Generate AI-optimized context",
            keywords: &["generate", "context", "ai"],
        });
        m.insert("refactor.start", ToolMeta {
            name: "refactor.start",
            description: "Begin refactoring workflow",
            keywords: &["refactor", "start", "begin"],
        });
        m.insert("git_operation", ToolMeta {
            name: "git_operation",
            description: "Execute git operations",
            keywords: &["git", "version", "control"],
        });
        m.insert("analyze_dead_code", ToolMeta {
            name: "analyze_dead_code",
            description: "Detect unused functions and variables",
            keywords: &["dead", "unused", "code"],
        });
        m.insert("analyze_big_o", ToolMeta {
            name: "analyze_big_o",
            description: "Analyze algorithmic complexity",
            keywords: &["big-o", "algorithm", "performance"],
        });
        m.insert("analyze_deep_context", ToolMeta {
            name: "analyze_deep_context",
            description: "Generate comprehensive codebase context",
            keywords: &["context", "summary", "analysis"],
        });
        m.insert("refactor.nextIteration", ToolMeta {
            name: "refactor.nextIteration",
            description: "Continue refactoring process",
            keywords: &["refactor", "next", "continue"],
        });
        m.insert("refactor.getState", ToolMeta {
            name: "refactor.getState",
            description: "Get current refactoring state",
            keywords: &["refactor", "state", "status"],
        });
        m.insert("refactor.stop", ToolMeta {
            name: "refactor.stop",
            description: "End refactoring workflow",
            keywords: &["refactor", "stop", "end"],
        });
        m.insert("quality_proxy", ToolMeta {
            name: "quality_proxy",
            description: "Intercept and validate code changes",
            keywords: &["quality", "proxy", "validate"],
        });
        m
    });
"#;

    write_stub(out_dir, "tool_registry.rs", tool_registry);
}

fn generate_stub_alias_table(out_dir: &str) {
    let alias_table = r#"
// Functional alias table for coverage builds
pub static ALIAS_TABLE: std::sync::LazyLock<std::collections::HashMap<&'static str, Vec<&'static str>>> =
    std::sync::LazyLock::new(|| {
        let mut m = std::collections::HashMap::new();
        m.insert("analyze_complexity", vec!["complexity", "analyze", "metrics", "complxity", "complx"]);
        m.insert("analyze_satd", vec!["debt", "technical debt", "todo", "fixme"]);
        m.insert("analyze_dag", vec!["dependency", "dependencies", "graph", "show dependencies", "dependency graph"]);
        m.insert("scaffold_project", vec!["scaffold", "create", "generate", "create project", "scafold"]);
        m.insert("generate_context", vec!["context", "generate context", "ai context"]);
        m.insert("quality_gate", vec!["quality", "check quality", "quality check", "qualit"]);
        m.insert("refactor.start", vec!["refactor", "start refactor", "refactr"]);
        m.insert("git_operation", vec!["git", "version control"]);
        m
    });
"#;

    write_stub(out_dir, "alias_table.rs", alias_table);
}

fn generate_stub_trigram_index(out_dir: &str) {
    write_stub(out_dir, "trigram_index.rs", TRIGRAM_INDEX_STUB);
}

const TRIGRAM_INDEX_STUB: &str = r#"
// Functional trigram index for coverage builds
pub struct TrigramIndex;

impl TrigramIndex {
    #[inline(always)]
    pub fn pack_trigram(s: &[u8]) -> u32 {
        if s.len() < 3 { return 0; }
        (s[0] as u32) | ((s[1] as u32) << 8) | ((s[2] as u32) << 16)
    }

    fn collect_trigrams(bytes: &[u8]) -> Vec<u32> {
        (0..bytes.len().saturating_sub(2))
            .map(|i| Self::pack_trigram(&bytes[i..i+3]))
            .collect()
    }

    fn jaccard_trigram_score(q: &[u32], c: &[u32]) -> f32 {
        let matches = q.iter().filter(|t| c.contains(t)).count();
        let union = q.len() + c.len() - matches;
        if union == 0 { 0.0 } else { matches as f32 / union as f32 }
    }

    pub fn similarity_score(&self, query: &str, candidate: &str) -> f32 {
        let q_lower = query.to_lowercase();
        let c_lower = candidate.to_lowercase();
        if q_lower == c_lower { return 1.0; }
        if c_lower.contains(&q_lower) { return 0.8; }
        let q_bytes = q_lower.into_bytes();
        let c_bytes = c_lower.into_bytes();
        if q_bytes.len() < 3 || c_bytes.len() < 3 { return 0.0; }
        Self::jaccard_trigram_score(&Self::collect_trigrams(&q_bytes), &Self::collect_trigrams(&c_bytes))
    }

    pub fn find_best_match<'a>(&self, query: &str, candidates: &[(&'a str, &str)]) -> Option<(&'a str, f32)> {
        candidates.iter()
            .map(|(name, desc)| {
                let score = self.similarity_score(query, name)
                    .max(self.similarity_score(query, desc) * 0.7);
                (*name, score)
            })
            .filter(|(_, s)| *s > 0.4)
            .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
    }
}
"#;

fn generate_stub_compressed_templates(out_dir: &str) {
    let compressed_templates = r#"
// Stub compressed templates for fast/coverage builds
// Real compression happens in full build mode via compress_templates()

use std::sync::LazyLock;
use std::collections::HashMap;

pub static COMPRESSED_TEMPLATES: LazyLock<HashMap<&'static str, Vec<u8>>> = LazyLock::new(|| {
    let mut m = HashMap::new();
    // Stub template data - "stub test data" as raw bytes
    m.insert("context.md.tera", b"stub test data".to_vec());
    m.insert("satd.md.tera", b"stub test data".to_vec());
    m
});
"#;

    write_stub(out_dir, "compressed_templates.rs", compressed_templates);

    // ── provable-contracts binding enforcement (AllImplemented) ──
    {
        let binding_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap_or(std::path::Path::new("."))
            .parent()
            .unwrap_or(std::path::Path::new("."))
            .join("provable-contracts/contracts/pmat/binding.yaml");

        println!("cargo:rerun-if-changed={}", binding_path.display());

        // provable-contracts validation requires serde_yaml_ng, which is
        // gated behind `standard-deps`. Under --no-default-features this
        // enforcement is skipped (contract YAML is irrelevant for minimal
        // builds that don't ship those subsystems).
        #[cfg(feature = "standard-deps")]
        if binding_path.exists() {
            #[derive(serde::Deserialize)]
            struct BF {
                #[allow(dead_code)]
                version: String,
                bindings: Vec<B>,
            }
            #[derive(serde::Deserialize)]
            struct B {
                contract: String,
                equation: String,
                status: String,
            }

            if let Ok(yaml) = std::fs::read_to_string(&binding_path) {
                if let Ok(bf) = serde_yaml_ng::from_str::<BF>(&yaml) {
                    let (mut imp, mut gaps) = (0u32, Vec::new());
                    for b in &bf.bindings {
                        let var = format!(
                            "CONTRACT_{}_{}",
                            b.contract
                                .trim_end_matches(".yaml")
                                .to_uppercase()
                                .replace('-', "_"),
                            b.equation.to_uppercase().replace('-', "_")
                        );
                        println!("cargo:rustc-env={var}={}", b.status);
                        if b.status == "implemented" {
                            imp += 1;
                        } else {
                            gaps.push(var.clone());
                        }
                    }
                    let total = bf.bindings.len() as u32;
                    println!("cargo:warning=[contract] AllImplemented: {imp}/{total} implemented, {} gaps", gaps.len());
                    if !gaps.is_empty() {
                        for g in &gaps {
                            println!("cargo:warning=[contract] UNALLOWED GAP: {g}");
                        }
                        panic!(
                            "[contract] AllImplemented: {} gap(s). Fix bindings or update status.",
                            gaps.len()
                        );
                    }
                }
            }
        }
    }
}

/// Emit CONTRACT_* env vars from contracts/binding.yaml.
///
/// Each binding with status=implemented generates a
/// `CONTRACT_<CONTRACT>_<EQUATION>=bound` env var that the
/// `#[contract]` proc macro reads at compile time.
fn emit_contract_env_vars() {
    let binding_path = Path::new("contracts/binding.yaml");
    println!("cargo:rerun-if-changed=contracts/binding.yaml");

    if !binding_path.exists() {
        return;
    }

    let content = match fs::read_to_string(binding_path) {
        Ok(c) => c,
        Err(_) => return,
    };

    // Parse YAML manually (no serde_yaml in build.rs)
    let mut current_contract = String::new();
    let mut current_equation = String::new();
    let mut current_status = String::new();

    for line in content.lines() {
        let t = line.trim();
        if t.starts_with("- contract:") {
            // Emit previous if complete
            if !current_contract.is_empty()
                && !current_equation.is_empty()
                && current_status == "implemented"
            {
                let key = make_contract_env_key(&current_contract, &current_equation);
                println!("cargo:rustc-env={key}=bound");
            }
            current_contract = t.trim_start_matches("- contract:").trim().to_string();
            current_equation.clear();
            current_status.clear();
        } else if t.starts_with("equation:") {
            current_equation = t.trim_start_matches("equation:").trim().to_string();
        } else if t.starts_with("status:") {
            current_status = t.trim_start_matches("status:").trim().to_string();
        }
    }
    // Emit last
    if !current_contract.is_empty()
        && !current_equation.is_empty()
        && current_status == "implemented"
    {
        let key = make_contract_env_key(&current_contract, &current_equation);
        println!("cargo:rustc-env={key}=bound");
    }
}

/// KAIZEN-0178: Build-time MCP tool schema code generation.
///
/// Reads every `mcp_tool_schemas/*.json` file, validates that it parses as
/// JSON with `{name, description, inputSchema}`, and emits
/// `$OUT_DIR/mcp_tool_schemas_gen.rs`. The emitted code exposes:
///
/// * `pub static MCP_TOOL_SCHEMAS: &[(&str, &str)]` — slice of
///   `(tool_name, raw_json_string)` for every schema file found.
/// * Per-tool `include_str!` constants so the macro layer and any
///   registration audit can reference them by tool name.
///
/// If a schema file referenced by handler code is deleted, the `include_str!`
/// it expanded to will fail with `E0432`/`E0433` style errors at compile time
/// — satisfying the "missing schema = compile error" acceptance constraint.
fn generate_mcp_tool_schemas() {
    let schema_dir = Path::new("mcp_tool_schemas");
    println!("cargo:rerun-if-changed=mcp_tool_schemas");

    if !schema_dir.exists() {
        println!("cargo:warning=mcp_tool_schemas/ not found — skipping codegen");
        return;
    }

    let out_dir = env::var("OUT_DIR").expect("OUT_DIR must be set");
    let dest = Path::new(&out_dir).join("mcp_tool_schemas_gen.rs");

    let mut entries: Vec<(String, std::path::PathBuf)> = Vec::new();

    let Ok(read_dir) = fs::read_dir(schema_dir) else {
        println!("cargo:warning=Failed to read mcp_tool_schemas/");
        return;
    };

    for entry in read_dir.flatten() {
        let path = entry.path();
        if path.extension().is_some_and(|e| e == "json") {
            let stem = path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("")
                .to_string();
            if stem.is_empty() {
                continue;
            }
            println!("cargo:rerun-if-changed={}", path.display());
            validate_tool_schema(&path, &stem);
            entries.push((stem, path));
        }
    }

    entries.sort_by(|a, b| a.0.cmp(&b.0));

    let code = render_tool_schema_module(&entries);
    if let Err(e) = write_if_changed(&dest, &code) {
        panic!("KAIZEN-0178: failed to write generated tool schema module: {e}");
    }
    println!(
        "cargo:warning=KAIZEN-0178: generated {} MCP tool schema(s)",
        entries.len()
    );
}

/// Validate a single schema JSON file at build time.
///
/// Enforces the invariant that every schema declares `name`,
/// `description`, and `inputSchema.type == "object"`. Mismatches panic the
/// build — the whole point is to catch bad schemas before they reach
/// runtime `tools/list`.
fn validate_tool_schema(path: &Path, stem: &str) {
    let content =
        fs::read_to_string(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
    let parsed: serde_json::Value = serde_json::from_str(&content)
        .unwrap_or_else(|e| panic!("{} is not valid JSON: {e}", path.display()));

    let name = parsed
        .get("name")
        .and_then(|v| v.as_str())
        .unwrap_or_else(|| panic!("{}: missing `name`", path.display()));
    assert_eq!(
        name,
        stem,
        "{}: `name` field ({name}) must match file stem ({stem})",
        path.display()
    );
    assert!(
        parsed
            .get("description")
            .and_then(|v| v.as_str())
            .is_some_and(|s| !s.is_empty()),
        "{}: `description` must be a non-empty string",
        path.display()
    );
    let input_schema = parsed
        .get("inputSchema")
        .unwrap_or_else(|| panic!("{}: missing `inputSchema`", path.display()));
    let ty = input_schema.get("type").and_then(|v| v.as_str());
    assert_eq!(
        ty,
        Some("object"),
        "{}: `inputSchema.type` must be \"object\"",
        path.display()
    );
}

/// Render the generated module source for a sorted list of schemas.
///
/// Emits two things:
///
/// 1. `MCP_TOOL_SCHEMAS: &[(&str, &str)]` — iterable registry.
/// 2. A `schema_const!` `macro_rules!` that maps a literal tool name to
///    its per-tool `include_str!("…/<name>.json")` constant. Referencing an
///    unknown tool name from a handler via `schema_const!("missing")`
///    expands to a non-existent arm → **compile error**, which satisfies
///    the KAIZEN-0178 acceptance constraint (removing a JSON file must
///    break the build, not degrade silently).
fn render_tool_schema_module(entries: &[(String, std::path::PathBuf)]) -> String {
    let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR set by cargo");
    let abs_path = |path: &std::path::PathBuf| {
        path.canonicalize()
            .unwrap_or_else(|_| Path::new(&manifest_dir).join(path))
    };
    let mut out = String::new();
    out.push_str(
        "// AUTO-GENERATED by build.rs (KAIZEN-0178). Do not edit.\n\
         //\n\
         // Source of truth: `mcp_tool_schemas/<tool_name>.json`.\n\
         // Deleting a referenced schema JSON triggers a compile error via `include_str!`.\n\n",
    );
    out.push_str("/// Raw JSON contents of every MCP tool schema, keyed by tool name.\n");
    out.push_str("///\n");
    out.push_str("/// Slice entries are sorted by tool name for deterministic iteration.\n");
    out.push_str("pub static MCP_TOOL_SCHEMAS: &[(&str, &str)] = &[\n");
    for (name, path) in entries {
        out.push_str(&format!(
            "    (\"{name}\", include_str!(r\"{}\")),\n",
            abs_path(path).display()
        ));
    }
    out.push_str("];\n\n");

    // Per-tool literal lookup macro. Referencing an unknown name fails to
    // match any arm → compile error ("no rules expected the token …").
    out.push_str("/// Compile-time tool schema lookup by literal tool name.\n");
    out.push_str("///\n");
    out.push_str("/// See [`tool_metadata!`] for the public macro that wraps this.\n");
    out.push_str("#[macro_export]\n");
    out.push_str("macro_rules! __kaizen0178_schema_const {\n");
    for (name, path) in entries {
        out.push_str(&format!(
            "    (\"{name}\") => {{ include_str!(r\"{}\") }};\n",
            abs_path(path).display()
        ));
    }
    out.push_str("}\n");
    out
}

fn make_contract_env_key(contract: &str, equation: &str) -> String {
    let c = contract.to_uppercase().replace(['-', '.', ' '], "_");
    let e = equation.to_uppercase().replace(['-', '.', ' '], "_");
    // Strip .yaml suffix
    let c = c.trim_end_matches("_YAML");
    format!("CONTRACT_{c}_{e}")
}