dixscript 1.0.0

Config, code, and encryption in one file — a data interchange format with compile-time functions, AES-256/ChaCha20 built-in, and cross-platform FFI
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
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
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
//! AST-level merging of two or more DixScript databases.
//!
//! Works directly on `DixScript` AST nodes — no JSON round-trip, no information
//! loss through type flattening.  All DixScript types (Long, Float, Double,
//! ScientificNotation, EnumValue, etc.) survive the merge exactly as-is.
//!
//! ## Conflict resolution
//!
//! When two sources define the same key, the configured `MdixMergeStrategy`
//! picks the winner:
//!
//! - `WeightedPriority` (default) — each source carries a `weight` in `[0.0, 1.0]`;
//!   higher weight wins.  Equal-weight ties fall back to primary (lower index) wins.
//! - `PrimaryWins` — the source with the lower index always wins.
//! - `SecondaryWins` — the source with the higher index always wins.
//! - `ThrowOnConflict` — any conflict returns `Err`.
//!
//! ## Two-tier ordering guarantee
//!
//! The merged `@DATA` section always places:
//!   - Tier 1 (`SimpleProperty` + `ObjectProperty`) before
//!   - Tier 2 (`TableProperty` + `GroupArray`)
//!
//! This invariant is enforced by construction, regardless of input ordering.
//!
//! ## Deep merging
//!
//! `TableProperty` entries sharing the same path are deep-merged at the
//! property level (field-by-field conflict resolution).  `SecurityEntry` blocks
//! sharing the same block key are deep-merged at the field level.  `EnumDeclaration`
//! sharing the same name are deep-merged at the field level.
//!
//! ## Array deduplication
//!
//! `GroupArray` entries sharing the same path are combined according to
//! `ArrayMergeStrategy` (default: `ConcatDedup` — winner's items first,
//! exact-duplicate primitive values removed).

use std::collections::HashMap;

use crate::Compiler::AST::{
    ConfigEntry, ConfigSection,
    DataEntry, DataSection,
    DixScript,
    DLMModule, DLMModuleType, DLMSection,
    EnumDeclaration, EnumField, EnumsSection,
    ImportDeclaration, ImportsSection,
    Position,
    PropertyAssignment,
    QuickFunction, QuickFuncsSection,
    SecurityEntry, SecurityField, SecuritySection,
    TablePath,
    Value,
};

// ── Strategy enums ────────────────────────────────────────────────────────────

/// How to pick the winner when two sources define the same key.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default)]
pub enum MdixMergeStrategy {
    /// The source with the highest `weight` wins.  Equal-weight ties go to
    /// the source with the lower index (primary).
    #[default]
    WeightedPriority,
    /// The source with the lower index (earliest added) always wins.
    PrimaryWins,
    /// The source with the higher index (latest added) always wins.
    SecondaryWins,
    /// Any key present in more than one source produces an error.
    ThrowOnConflict,
}


/// How to combine two `GroupArray` (or array-valued) entries that share a path.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default)]
pub enum ArrayMergeStrategy {
    /// Winner's array entirely replaces the loser's.
    Replace,
    /// Both arrays are concatenated — winner's items first.
    Concat,
    /// Both arrays are concatenated (winner first); exact-duplicate primitive
    /// values are removed.  Complex values (Object, Array) are never deduped.
    #[default]
    ConcatDedup,
}


// ── Input / output types ──────────────────────────────────────────────────────

/// A single AST to be merged, together with its priority weight.
///
/// Higher `weight` beats lower `weight` under `WeightedPriority`.
/// `weight` is ignored for `PrimaryWins` and `SecondaryWins`.
#[derive(Debug, Clone)]
pub struct MdixMergeInput {
    pub ast: DixScript,
    /// Priority in `[0.0, 1.0]`.  Values outside the range are clamped.
    pub weight: f64,
    /// Optional human-readable label used in conflict reports and error messages.
    pub label: Option<String>,
}

impl MdixMergeInput {
    /// Create a new input with default weight `0.5`.
    pub fn new(ast: DixScript) -> Self {
        MdixMergeInput { ast, weight: 0.5, label: None }
    }

    /// Set the priority weight (clamped to `[0.0, 1.0]`).
    pub fn with_weight(mut self, weight: f64) -> Self {
        self.weight = weight.clamp(0.0, 1.0);
        self
    }

    /// Attach a human-readable label (used in conflict reports).
    pub fn with_label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }
}

/// A recorded conflict and which source won.
#[derive(Debug, Clone)]
pub struct MergeConflict {
    /// Dotted path of the conflicting key, e.g. `"DATA.server.host"`.
    pub path: String,
    /// 0-based index of the winning source in the input slice.
    pub winning_source: usize,
    /// Label of the winning source (if one was provided).
    pub winning_label: Option<String>,
}

impl std::fmt::Display for MergeConflict {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.winning_label {
            Some(label) => write!(
                f,
                "[Conflict] '{}' → source[{}] ('{}') won",
                self.path, self.winning_source, label
            ),
            None => write!(
                f,
                "[Conflict] '{}' → source[{}] won",
                self.path, self.winning_source
            ),
        }
    }
}

/// The result of a merge operation.
#[derive(Debug)]
pub struct MdixMergeResult {
    /// The merged AST.
    pub merged_ast: DixScript,
    /// Every conflict that was resolved during the merge.
    pub conflicts: Vec<MergeConflict>,
    /// `true` when no fatal errors occurred.
    pub is_success: bool,
    /// Fatal error messages (non-empty only under `ThrowOnConflict` or bad inputs).
    pub errors: Vec<String>,
}

impl MdixMergeResult {
    /// Unwrap the merged AST, panicking with all errors joined if the merge failed.
    pub fn unwrap(self) -> DixScript {
        if !self.is_success {
            panic!("MdixMerge failed: {}", self.errors.join("; "));
        }
        self.merged_ast
    }

    /// Convert to `Result`, mapping errors to a joined string.
    pub fn into_result(self) -> Result<(DixScript, Vec<MergeConflict>), String> {
        if self.is_success {
            Ok((self.merged_ast, self.conflicts))
        } else {
            Err(self.errors.join("; "))
        }
    }
}

// ── MdixMerger ────────────────────────────────────────────────────────────────

/// AST-level merger for DixScript databases.
///
/// ```rust,ignore
/// use dixscript::Runtime::merge::{MdixMerger, MdixMergeInput, MdixMergeStrategy};
///
/// let result = MdixMerger::new()
///     .with_strategy(MdixMergeStrategy::WeightedPriority)
///     .merge_all(vec![
///         MdixMergeInput::new(ast_base).with_weight(1.0).with_label("base"),
///         MdixMergeInput::new(ast_patch).with_weight(0.8).with_label("patch"),
///     ]);
///
/// if result.is_success {
///     println!("Merged with {} conflict(s)", result.conflicts.len());
/// }
/// ```
pub struct MdixMerger {
    strategy:       MdixMergeStrategy,
    array_strategy: ArrayMergeStrategy,
}

impl MdixMerger {
    pub fn new() -> Self {
        MdixMerger {
            strategy:       MdixMergeStrategy::default(),
            array_strategy: ArrayMergeStrategy::default(),
        }
    }

    /// Set the conflict resolution strategy.
    pub fn with_strategy(mut self, strategy: MdixMergeStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Set the array merge strategy (applies to `GroupArray` entries and
    /// array-valued `SimpleProperty` entries that share a key).
    pub fn with_array_strategy(mut self, strategy: ArrayMergeStrategy) -> Self {
        self.array_strategy = strategy;
        self
    }

    // ── Public entry points ───────────────────────────────────────────────────

    /// Merge exactly two ASTs.
    pub fn merge(&self, primary: MdixMergeInput, secondary: MdixMergeInput) -> MdixMergeResult {
        self.merge_all(vec![primary, secondary])
    }

    /// Merge two or more ASTs.
    ///
    /// Sources are processed in order; index 0 is always "primary" for
    /// tie-breaking when weights are equal under `WeightedPriority`, and
    /// is always the winner under `PrimaryWins`.
    pub fn merge_all(&self, sources: Vec<MdixMergeInput>) -> MdixMergeResult {
        if sources.is_empty() {
            return MdixMergeResult {
                merged_ast: DixScript::new(),
                conflicts:  vec![],
                is_success: false,
                errors:     vec!["merge_all: no sources provided".to_string()],
            };
        }
        if sources.len() == 1 {
            let s = sources.into_iter().next().unwrap();
            return MdixMergeResult {
                merged_ast: s.ast,
                conflicts:  vec![],
                is_success: true,
                errors:     vec![],
            };
        }

        let mut conflicts = Vec::new();
        let mut errors    = Vec::new();

        let merged_ast = self.do_merge(&sources, &mut conflicts, &mut errors);

        let is_success = errors.is_empty();
        MdixMergeResult { merged_ast, conflicts, is_success, errors }
    }

    // ── Core orchestrator ─────────────────────────────────────────────────────

    fn do_merge(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> DixScript {
        DixScript {
            config:          self.merge_config(sources, conflicts, errors),
            imports:         self.merge_imports(sources, conflicts, errors),
            dlm:             self.merge_dlm(sources),
            enums:           self.merge_enums(sources, conflicts, errors),
            quick_functions: self.merge_quickfuncs(sources, conflicts, errors),
            data:            self.merge_data(sources, conflicts, errors),
            security:        self.merge_security(sources, conflicts, errors),
        }
    }

    // ── @CONFIG ───────────────────────────────────────────────────────────────

    fn merge_config(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> Option<ConfigSection> {
        let present: Vec<(usize, &ConfigSection)> = sources
            .iter()
            .enumerate()
            .filter_map(|(i, s)| s.ast.config.as_ref().map(|c| (i, c)))
            .collect();

        if present.is_empty() {
            return None;
        }

        // key → (winning_source_idx, ConfigEntry)
        let mut key_map: HashMap<String, (usize, ConfigEntry)> = HashMap::new();
        // Preserve insertion order so output matches primary source order.
        let mut key_order: Vec<String> = Vec::new();

        for (src_idx, section) in &present {
            for entry in &section.entries {
                let key = entry.key.clone();
                if let Some((existing_idx, existing_entry)) = key_map.get(&key) {
                    // Identical value on both sides is not a real conflict — every
                    // parsed file gets the same auto-populated minimal-config
                    // defaults (version, debug_mode, ...) when the user never wrote
                    // an explicit @CONFIG block, so this key collides on *every*
                    // multi-source merge regardless of @DATA content. Without this
                    // guard, ThrowOnConflict raised unconditionally on every merge.
                    if existing_entry.value == entry.value {
                        continue;
                    }
                    let existing_idx = *existing_idx;
                    if let Some(winner) = self.resolve_conflict(
                        format!("CONFIG.{}", key),
                        *src_idx, existing_idx, sources, conflicts, errors,
                    ) {
                        if winner == *src_idx {
                            key_map.insert(key, (*src_idx, entry.clone()));
                        }
                    }
                } else {
                    key_order.push(key.clone());
                    key_map.insert(key, (*src_idx, entry.clone()));
                }
            }
        }

        if !errors.is_empty() {
            return None;
        }

        let mut entries = Vec::with_capacity(key_order.len());
        for key in &key_order {
            if let Some((_, entry)) = key_map.get(key) {
                entries.push(entry.clone());
            }
        }

        Some(ConfigSection { entries, position: Position::UNKNOWN })
    }

    // ── @IMPORTS ──────────────────────────────────────────────────────────────

    fn merge_imports(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> Option<ImportsSection> {
        let present: Vec<(usize, &ImportsSection)> = sources
            .iter()
            .enumerate()
            .filter_map(|(i, s)| s.ast.imports.as_ref().map(|imp| (i, imp)))
            .collect();

        if present.is_empty() {
            return None;
        }

        // alias → (source_idx, ImportDeclaration)
        let mut alias_map: HashMap<String, (usize, ImportDeclaration)> = HashMap::new();
        let mut alias_order: Vec<String> = Vec::new();

        for (src_idx, section) in &present {
            for import in &section.imports {
                let alias = import.alias.clone();
                if let Some((existing_idx, existing_import)) = alias_map.get(&alias) {
                    // Identical alias + identical path + same cloud flag → harmless dup, skip.
                    if existing_import.path == import.path
                        && existing_import.is_cloud_import == import.is_cloud_import
                    {
                        continue;
                    }
                    // Different target — real conflict.
                    let existing_idx = *existing_idx;
                    if let Some(winner) = self.resolve_conflict(
                        format!("IMPORTS.{}", alias),
                        *src_idx, existing_idx, sources, conflicts, errors,
                    ) {
                        if winner == *src_idx {
                            alias_map.insert(alias, (*src_idx, import.clone()));
                        }
                    }
                } else {
                    alias_order.push(alias.clone());
                    alias_map.insert(alias, (*src_idx, import.clone()));
                }
            }
        }

        if !errors.is_empty() || alias_map.is_empty() {
            return if errors.is_empty() { None } else { None };
        }

        let mut imports = Vec::with_capacity(alias_order.len());
        for alias in &alias_order {
            if let Some((_, imp)) = alias_map.get(alias) {
                imports.push(imp.clone());
            }
        }

        Some(ImportsSection { imports, position: Position::UNKNOWN })
    }

    // ── @DLM ─────────────────────────────────────────────────────────────────

    fn merge_dlm(&self, sources: &[MdixMergeInput]) -> Option<DLMSection> {
        let present: Vec<&DLMSection> = sources
            .iter()
            .filter_map(|s| s.ast.dlm.as_ref())
            .collect();

        if present.is_empty() {
            return None;
        }

        // One module per module-type category; first occurrence (primary) wins.
        let mut type_map: HashMap<u8, DLMModule> = HashMap::new();

        for section in &present {
            for module in &section.modules {
                let key = dlm_module_type_key(module.module_type);
                type_map.entry(key).or_insert_with(|| module.clone());
            }
        }

        if type_map.is_empty() {
            return None;
        }

        // Canonical pipeline order: compress → audit → encrypt.
        let mut modules: Vec<DLMModule> = type_map.into_values().collect();
        modules.sort_by_key(|m| match m.module_type {
            DLMModuleType::DCompressor => 0u8,
            DLMModuleType::DAuditor    => 1,
            DLMModuleType::DEncryptor  => 2,
            DLMModuleType::ParseError  => 3,
        });

        Some(DLMSection { modules, position: Position::UNKNOWN })
    }

    // ── @ENUMS ────────────────────────────────────────────────────────────────

    fn merge_enums(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> Option<EnumsSection> {
        let present: Vec<(usize, &EnumsSection)> = sources
            .iter()
            .enumerate()
            .filter_map(|(i, s)| s.ast.enums.as_ref().map(|e| (i, e)))
            .collect();

        if present.is_empty() {
            return None;
        }

        // name → (winning_source_idx, EnumDeclaration)
        let mut enum_map: HashMap<String, (usize, EnumDeclaration)> = HashMap::new();
        let mut enum_order: Vec<String> = Vec::new();

        for (src_idx, section) in &present {
            for decl in &section.enums {
                if enum_map.contains_key(&decl.name) {
                    let existing_src_idx = enum_map[&decl.name].0;
                    let existing_fields  = enum_map[&decl.name].1.fields.clone();

                    // Same enum name, and every field already carries the same
                    // value on both sides — not a real conflict. Compare only
                    // `.value` (like merge_enum_fields already does below), not
                    // the whole EnumField, since each field's `position` will
                    // always differ across two independently-parsed sources.
                    let fields_identical = existing_fields.len() == decl.fields.len()
                        && existing_fields.iter().all(|ef| {
                            decl.fields.iter().any(|df| df.name == ef.name && df.value == ef.value)
                        });

                    if fields_identical {
                        continue;
                    }

                    // Deep-merge: same enum name, merge fields.
                    let Some(winner) = self.resolve_conflict(
                        format!("ENUMS.{}", decl.name),
                        *src_idx, existing_src_idx, sources, conflicts, errors,
                    ) else { continue; };

                    let merged_fields = self.merge_enum_fields(
                        &existing_fields, existing_src_idx,
                        &decl.fields, *src_idx,
                        winner, sources, conflicts, errors,
                        &decl.name,
                    );

                    let entry = enum_map.get_mut(&decl.name).unwrap();
                    entry.0 = winner;
                    entry.1.fields = merged_fields;
                } else {
                    enum_order.push(decl.name.clone());
                    enum_map.insert(decl.name.clone(), (*src_idx, decl.clone()));
                }
            }
        }

        if !errors.is_empty() || enum_map.is_empty() {
            return None;
        }

        let mut enums = Vec::with_capacity(enum_order.len());
        for name in &enum_order {
            if let Some((_, decl)) = enum_map.get(name) {
                enums.push(decl.clone());
            }
        }

        Some(EnumsSection { enums, position: Position::UNKNOWN })
    }

    fn merge_enum_fields(
        &self,
        primary_fields:   &[EnumField],
        primary_src:      usize,
        secondary_fields: &[EnumField],
        secondary_src:    usize,
        winner:           usize,
        sources:          &[MdixMergeInput],
        conflicts:        &mut Vec<MergeConflict>,
        errors:           &mut Vec<String>,
        enum_name:        &str,
    ) -> Vec<EnumField> {
        // name → (source_idx, EnumField)
        let mut field_map: HashMap<String, (usize, EnumField)> = HashMap::new();
        let mut field_order: Vec<String> = Vec::new();

        for field in primary_fields {
            field_order.push(field.name.clone());
            field_map.insert(field.name.clone(), (primary_src, field.clone()));
        }

        for field in secondary_fields {
            if let Some((existing_src, existing_field)) = field_map.get(&field.name) {
                // Only a conflict if the integer values differ.
                if existing_field.value != field.value {
                    let existing_src = *existing_src;
                    if let Some(field_winner) = self.resolve_conflict(
                        format!("ENUMS.{}.{}", enum_name, field.name),
                        secondary_src, existing_src, sources, conflicts, errors,
                    ) {
                        if field_winner == secondary_src {
                            field_map.insert(field.name.clone(), (secondary_src, field.clone()));
                        }
                    }
                }
                // Identical value → silent dedup, no conflict recorded.
            } else {
                // New field only in secondary → always add.
                field_order.push(field.name.clone());
                field_map.insert(field.name.clone(), (secondary_src, field.clone()));
            }
        }

        let mut result = Vec::with_capacity(field_order.len());
        for name in &field_order {
            if let Some((_, f)) = field_map.get(name) {
                result.push(f.clone());
            }
        }
        result
    }

    // ── @QUICKFUNCS ───────────────────────────────────────────────────────────

    fn merge_quickfuncs(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> Option<QuickFuncsSection> {
        let present: Vec<(usize, &QuickFuncsSection)> = sources
            .iter()
            .enumerate()
            .filter_map(|(i, s)| s.ast.quick_functions.as_ref().map(|q| (i, q)))
            .collect();

        if present.is_empty() {
            return None;
        }

        // name → (winning_source_idx, QuickFunction)
        let mut func_map: HashMap<String, (usize, QuickFunction)> = HashMap::new();
        let mut func_order: Vec<String> = Vec::new();

        for (src_idx, section) in &present {
            for func in &section.functions {
                if let Some((existing_idx, existing_func)) = func_map.get(&func.name) {
                    // Identical signature + body on both sides is not a real
                    // conflict. NOTE: unlike CONFIG/ENUMS above, this comparison
                    // includes each QuickFuncStatement's own `position`, so in
                    // practice this only dedups trivially-identical (e.g. empty
                    // body) functions parsed from otherwise-different source
                    // text — a real identical function body from two distinct
                    // source files will still (correctly) be flagged, since we
                    // can't cheaply prove text-identity without positions here.
                    let identical = existing_func.return_type == func.return_type
                        && existing_func.scope_list == func.scope_list
                        && existing_func.parameters == func.parameters
                        && existing_func.body == func.body;
                    if identical {
                        continue;
                    }
                    match self.resolve_conflict(
                        format!("QUICKFUNCS.{}", func.name),
                        *src_idx, *existing_idx, sources, conflicts, errors,
                    ) {
                        Some(winner) if winner == *src_idx => {
                            func_map.insert(func.name.clone(), (*src_idx, func.clone()));
                        }
                        _ => {}
                    }
                } else {
                    func_order.push(func.name.clone());
                    func_map.insert(func.name.clone(), (*src_idx, func.clone()));
                }
            }
        }

        if !errors.is_empty() || func_map.is_empty() {
            return None;
        }

        let mut functions = Vec::with_capacity(func_order.len());
        for name in &func_order {
            if let Some((_, func)) = func_map.get(name) {
                functions.push(func.clone());
            }
        }

        Some(QuickFuncsSection { functions, position: Position::UNKNOWN })
    }

    // ── @DATA ─────────────────────────────────────────────────────────────────

    fn merge_data(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> Option<DataSection> {
        let present: Vec<(usize, &DataSection)> = sources
            .iter()
            .enumerate()
            .filter_map(|(i, s)| s.ast.data.as_ref().map(|d| (i, d)))
            .collect();

        if present.is_empty() {
            return None;
        }

        // ── Per-tier accumulators ─────────────────────────────────────────────
        // Tier 1: flat (SimpleProperty, ObjectProperty)
        let mut simple_map:   HashMap<String, (usize, DataEntry)> = HashMap::new();
        let mut simple_order: Vec<String>                          = Vec::new();
        let mut object_map:   HashMap<String, (usize, DataEntry)> = HashMap::new();
        let mut object_order: Vec<String>                          = Vec::new();

        // Tier 2: grouped (TableProperty, GroupArray)
        let mut table_map:   HashMap<String, (usize, Vec<PropertyAssignment>)> = HashMap::new();
        let mut table_order: Vec<String>                                        = Vec::new();
        let mut array_map:   HashMap<String, (usize, Vec<Value>)>              = HashMap::new();
        let mut array_order: Vec<String>                                        = Vec::new();

        for (src_idx, section) in &present {
            for entry in &section.entries {
                match entry {
                    DataEntry::SimpleProperty { name, .. } => {
                        self.upsert_unique_entry(
                            name.clone(), *src_idx, entry.clone(),
                            &mut simple_map, &mut simple_order,
                            "DATA", conflicts, errors, sources,
                        );
                    }

                    DataEntry::ObjectProperty { name, .. } => {
                        self.upsert_unique_entry(
                            name.clone(), *src_idx, entry.clone(),
                            &mut object_map, &mut object_order,
                            "DATA", conflicts, errors, sources,
                        );
                    }

                    DataEntry::TableProperty { path, properties, .. } => {
                        let key = path.to_string();
                        if table_map.contains_key(&key) {
                            let existing_src = table_map[&key].0;
                            let existing_props = table_map[&key].1.clone();
                            // Same key existing on both sides isn't a conflict
                            // by itself -- only differing content is. Without
                            // this, re-diffing/re-merging identical content
                            // (e.g. `mdix diff file.mdix file.mdix`) reported
                            // every TableProperty as conflicting purely
                            // because the key was present twice.
                            if table_props_are_equal(&existing_props, properties) {
                                continue;
                            }
                            let Some(winner) = self.resolve_conflict(
                                format!("DATA.{}", key),
                                *src_idx, existing_src, sources, conflicts, errors,
                            ) else { continue; };
                            let merged = self.merge_table_props(
                                &existing_props, existing_src,
                                properties, *src_idx,
                                winner, sources, conflicts, errors,
                                &format!("DATA.{}", key),
                            );
                            let slot = table_map.get_mut(&key).unwrap();
                            slot.0 = winner;
                            slot.1 = merged;
                        } else {
                            table_order.push(key.clone());
                            table_map.insert(key, (*src_idx, properties.clone()));
                        }
                    }

                    DataEntry::GroupArray { path, items, .. } => {
                        let key = path.to_string();
                        if array_map.contains_key(&key) {
                            let existing_src = array_map[&key].0;
                            let existing_items = array_map[&key].1.clone();
                            // Same guard as TableProperty above -- identical
                            // items on both sides isn't a real conflict.
                            if array_items_are_equal(&existing_items, items) {
                                continue;
                            }
                            let Some(winner) = self.resolve_conflict(
                                format!("DATA.{}", key),
                                *src_idx, existing_src, sources, conflicts, errors,
                            ) else { continue; };
                            let merged_items = self.merge_array_items(
                                &existing_items, existing_src,
                                items, *src_idx,
                                winner,
                            );
                            let slot = array_map.get_mut(&key).unwrap();
                            slot.0 = winner;
                            slot.1 = merged_items;
                        } else {
                            array_order.push(key.clone());
                            array_map.insert(key, (*src_idx, items.clone()));
                        }
                    }
                }
            }
        }

        if !errors.is_empty() {
            return None;
        }

        // ── Assemble with two-tier ordering enforced ──────────────────────────
        let mut entries = Vec::new();

        // Tier 1a — SimpleProperty
        for key in &simple_order {
            if let Some((_, entry)) = simple_map.get(key) {
                entries.push(entry.clone());
            }
        }
        // Tier 1b — ObjectProperty
        for key in &object_order {
            if let Some((_, entry)) = object_map.get(key) {
                entries.push(entry.clone());
            }
        }
        // Tier 2a — TableProperty
        for key in &table_order {
            if let Some((_, props)) = table_map.get(key) {
                let segments = key.split('.').map(String::from).collect();
                entries.push(DataEntry::TableProperty {
                    path:       TablePath { segments },
                    properties: props.clone(),
                    position:   Position::UNKNOWN,
                });
            }
        }
        // Tier 2b — GroupArray
        for key in &array_order {
            if let Some((_, items)) = array_map.get(key) {
                let segments = key.split('.').map(String::from).collect();
                entries.push(DataEntry::GroupArray {
                    path:     TablePath { segments },
                    items:    items.clone(),
                    position: Position::UNKNOWN,
                });
            }
        }

        if entries.is_empty() { None } else { Some(DataSection { entries, position: Position::UNKNOWN }) }
    }

    /// Insert or update a single-key entry (SimpleProperty / ObjectProperty).
    fn upsert_unique_entry(
        &self,
        key:       String,
        src_idx:   usize,
        entry:     DataEntry,
        map:       &mut HashMap<String, (usize, DataEntry)>,
        order:     &mut Vec<String>,
        section:   &str,
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
        sources:   &[MdixMergeInput],
    ) {
        if let Some((existing_idx, existing_entry)) = map.get(&key) {
            // Identical value on both sides is not a real conflict. Only
            // SimpleProperty is compared in full (via values_are_equal,
            // Position-blind) — TableProperty/GroupArray/ObjectProperty fall
            // through to "always a conflict" the same way values_are_equal's
            // own catch-all treats complex types, since a full position-blind
            // structural walk of those isn't worth the complexity here.
            if data_entries_are_equal(existing_entry, &entry) {
                return;
            }
            let existing_idx = *existing_idx;
            if let Some(winner) = self.resolve_conflict(
                format!("{}.{}", section, key),
                src_idx, existing_idx, sources, conflicts, errors,
            ) {
                if winner == src_idx {
                    map.insert(key, (src_idx, entry));
                    // key is already in `order`
                }
            }
        } else {
            order.push(key.clone());
            map.insert(key, (src_idx, entry));
        }
    }

    /// Deep-merge two TableProperty property lists (field-by-field conflict resolution).
    fn merge_table_props(
        &self,
        primary_props:   &[PropertyAssignment],
        primary_src:     usize,
        secondary_props: &[PropertyAssignment],
        secondary_src:   usize,
        winner:          usize,
        sources:         &[MdixMergeInput],
        conflicts:       &mut Vec<MergeConflict>,
        errors:          &mut Vec<String>,
        path_label:      &str,
    ) -> Vec<PropertyAssignment> {
        let mut prop_map:   HashMap<String, (usize, PropertyAssignment)> = HashMap::new();
        let mut prop_order: Vec<String>                                   = Vec::new();

        for prop in primary_props {
            prop_order.push(prop.name.clone());
            prop_map.insert(prop.name.clone(), (primary_src, prop.clone()));
        }

        for prop in secondary_props {
            if let Some((existing_src, existing_prop)) = prop_map.get(&prop.name) {
                // Identical value on both sides is not a real conflict. Uses
                // values_are_equal (already defined below for merge_array_items'
                // ConcatDedup strategy) rather than raw `==`, since Value's
                // compound variants (Array/Object) embed their own per-source
                // Position and would never derive-equal even when the content
                // matches; values_are_equal already treats those conservatively.
                if values_are_equal(&existing_prop.value, &prop.value) {
                    continue;
                }
                let existing_src = *existing_src;
                if let Some(prop_winner) = self.resolve_conflict(
                    format!("{}.{}", path_label, prop.name),
                    secondary_src, existing_src, sources, conflicts, errors,
                ) {
                    if prop_winner == secondary_src {
                        prop_map.insert(prop.name.clone(), (secondary_src, prop.clone()));
                    }
                }
            } else {
                prop_order.push(prop.name.clone());
                prop_map.insert(prop.name.clone(), (secondary_src, prop.clone()));
            }
        }

        let mut result = Vec::with_capacity(prop_order.len());
        for name in &prop_order {
            if let Some((_, prop)) = prop_map.get(name) {
                result.push(prop.clone());
            }
        }
        result
    }

    /// Combine two GroupArray item lists per the configured `ArrayMergeStrategy`.
    fn merge_array_items(
        &self,
        primary_items:  &[Value],
        primary_src:    usize,
        secondary_items: &[Value],
        secondary_src:  usize,
        winner:         usize,
    ) -> Vec<Value> {
        let (winner_items, loser_items) = if winner == primary_src {
            (primary_items, secondary_items)
        } else {
            (secondary_items, primary_items)
        };

        match self.array_strategy {
            ArrayMergeStrategy::Replace => winner_items.to_vec(),

            ArrayMergeStrategy::Concat => {
                let mut result = winner_items.to_vec();
                result.extend_from_slice(loser_items);
                result
            }

            ArrayMergeStrategy::ConcatDedup => {
                let mut result = winner_items.to_vec();
                for item in loser_items {
                    if !result.iter().any(|existing| values_are_equal(existing, item)) {
                        result.push(item.clone());
                    }
                }
                result
            }
        }
    }

    // ── @SECURITY ─────────────────────────────────────────────────────────────

    fn merge_security(
        &self,
        sources:   &[MdixMergeInput],
        conflicts: &mut Vec<MergeConflict>,
        errors:    &mut Vec<String>,
    ) -> Option<SecuritySection> {
        let present: Vec<(usize, &SecuritySection)> = sources
            .iter()
            .enumerate()
            .filter_map(|(i, s)| s.ast.security.as_ref().map(|sec| (i, sec)))
            .collect();

        if present.is_empty() {
            return None;
        }

        // block_key → (winning_source_idx, SecurityEntry)
        let mut entry_map:   HashMap<String, (usize, SecurityEntry)> = HashMap::new();
        let mut entry_order: Vec<String>                              = Vec::new();

        for (src_idx, section) in &present {
            for entry in &section.entries {
                let key = entry.block_key.clone();
                if entry_map.contains_key(&key) {
                    let existing_src    = entry_map[&key].0;
                    let existing_fields = entry_map[&key].1.fields.clone();

                    // Same block key, and every field already carries the same
                    // value on both sides (via values_are_equal, see
                    // merge_table_props above for why not raw `==`) — not a
                    // real conflict.
                    let fields_identical = existing_fields.len() == entry.fields.len()
                        && existing_fields.iter().all(|ef| {
                            entry.fields.iter().any(|df| {
                                df.key == ef.key && values_are_equal(&df.value, &ef.value)
                            })
                        });
                    if fields_identical {
                        continue;
                    }

                    let Some(winner) = self.resolve_conflict(
                        format!("SECURITY.{}", key),
                        *src_idx, existing_src, sources, conflicts, errors,
                    ) else { continue; };
                    let merged_fields = self.merge_security_fields(
                        &existing_fields, existing_src,
                        &entry.fields, *src_idx,
                        winner, sources, conflicts, errors,
                        &format!("SECURITY.{}", key),
                    );
                    let slot = entry_map.get_mut(&key).unwrap();
                    slot.0 = winner;
                    slot.1.fields = merged_fields;
                } else {
                    entry_order.push(key.clone());
                    entry_map.insert(key, (*src_idx, entry.clone()));
                }
            }
        }

        if !errors.is_empty() || entry_map.is_empty() {
            return None;
        }

        let mut entries = Vec::with_capacity(entry_order.len());
        for key in &entry_order {
            if let Some((_, entry)) = entry_map.get(key) {
                entries.push(entry.clone());
            }
        }

        Some(SecuritySection { entries, position: Position::UNKNOWN })
    }

    fn merge_security_fields(
        &self,
        primary_fields:   &[SecurityField],
        primary_src:      usize,
        secondary_fields: &[SecurityField],
        secondary_src:    usize,
        winner:           usize,
        sources:          &[MdixMergeInput],
        conflicts:        &mut Vec<MergeConflict>,
        errors:           &mut Vec<String>,
        block_label:      &str,
    ) -> Vec<SecurityField> {
        let mut field_map:   HashMap<String, (usize, SecurityField)> = HashMap::new();
        let mut field_order: Vec<String>                              = Vec::new();

        for field in primary_fields {
            field_order.push(field.key.clone());
            field_map.insert(field.key.clone(), (primary_src, field.clone()));
        }

        for field in secondary_fields {
            if let Some((existing_src, existing_field)) = field_map.get(&field.key) {
                // Identical value on both sides is not a real conflict.
                if values_are_equal(&existing_field.value, &field.value) {
                    continue;
                }
                let existing_src = *existing_src;
                if let Some(field_winner) = self.resolve_conflict(
                    format!("{}.{}", block_label, field.key),
                    secondary_src, existing_src, sources, conflicts, errors,
                ) {
                    if field_winner == secondary_src {
                        field_map.insert(field.key.clone(), (secondary_src, field.clone()));
                    }
                }
            } else {
                field_order.push(field.key.clone());
                field_map.insert(field.key.clone(), (secondary_src, field.clone()));
            }
        }

        let mut result = Vec::with_capacity(field_order.len());
        for key in &field_order {
            if let Some((_, f)) = field_map.get(key) {
                result.push(f.clone());
            }
        }
        result
    }

    // ── Conflict resolution ───────────────────────────────────────────────────

    /// Resolve a conflict between `challenger` (new) and `existing` (already
    /// stored) for a given reporting `path`, and — unlike calling
    /// `pick_winner` directly — always record a `MergeConflict` for it,
    /// regardless of whether a winner could actually be picked.
    ///
    /// Every call site used to push to `conflicts` only from inside
    /// `pick_winner`'s `Ok` arm, which `ThrowOnConflict` never returns (it's
    /// `Err` unconditionally, by design — see `pick_winner`). That meant
    /// `result.conflicts` came back empty under `ThrowOnConflict` even when
    /// real, detected disagreements existed, which silently broke `mdix
    /// diff` (`mdix-cli/src/services/diff_service.rs`): it runs merges
    /// under `ThrowOnConflict` specifically to *list* every disagreement
    /// without picking a winner, so an always-empty `conflicts` made every
    /// diff report "no conflicts" no matter what.
    ///
    /// Under `ThrowOnConflict` there's no real winner to apply to the
    /// merged output — `winning_source`/`winning_label` on that conflict
    /// describe `challenger` purely as a reference point for display, the
    /// same way `SecondaryWins` would report it. Nothing is actually
    /// applied to the merged AST for it either way, since the caller's
    /// `errors` (pushed here too, exactly as `pick_winner` already did) is
    /// non-empty afterward, which already makes the overall merge
    /// `is_success: false` — callers that only care about a successful
    /// merge (e.g. `mdix merge`) bail out on that before ever looking at
    /// `conflicts`, so this doesn't change their behavior.
    ///
    /// Returns `Some(winner)` when a winner was actually selected and
    /// should be applied to the merged output, or `None` when the strategy
    /// refused (the error has already been pushed to `errors`).
    fn resolve_conflict(
        &self,
        path:       String,
        challenger: usize,
        existing:   usize,
        sources:    &[MdixMergeInput],
        conflicts:  &mut Vec<MergeConflict>,
        errors:     &mut Vec<String>,
    ) -> Option<usize> {
        match self.pick_winner(challenger, existing, sources) {
            Ok(winner) => {
                conflicts.push(MergeConflict {
                    path,
                    winning_source: winner,
                    winning_label: sources[winner].label.clone(),
                });
                Some(winner)
            }
            Err(e) => {
                conflicts.push(MergeConflict {
                    path,
                    winning_source: challenger,
                    winning_label: sources.get(challenger).and_then(|s| s.label.clone()),
                });
                errors.push(e);
                None
            }
        }
    }

    /// Returns the index of the winner between `challenger` (new) and `existing`
    /// (already stored).  Returns `Err` under `ThrowOnConflict`.
    fn pick_winner(
        &self,
        challenger: usize,
        existing:   usize,
        sources:    &[MdixMergeInput],
    ) -> Result<usize, String> {
        match self.strategy {
            MdixMergeStrategy::ThrowOnConflict => Err(format!(
                "Conflict between source[{}]{} and source[{}]{} (ThrowOnConflict)",
                existing,
                sources.get(existing)
                    .and_then(|s| s.label.as_ref())
                    .map(|l| format!(" ('{}')", l))
                    .unwrap_or_default(),
                challenger,
                sources.get(challenger)
                    .and_then(|s| s.label.as_ref())
                    .map(|l| format!(" ('{}')", l))
                    .unwrap_or_default(),
            )),

            MdixMergeStrategy::PrimaryWins => {
                // Lower index = primary = wins.
                Ok(existing.min(challenger))
            }

            MdixMergeStrategy::SecondaryWins => {
                // Higher index = latest = wins.
                Ok(existing.max(challenger))
            }

            MdixMergeStrategy::WeightedPriority => {
                let w_existing    = sources.get(existing).map(|s| s.weight).unwrap_or(0.0);
                let w_challenger  = sources.get(challenger).map(|s| s.weight).unwrap_or(0.0);
                // Higher weight wins; tie → lower index (primary) wins.
                if w_challenger > w_existing {
                    Ok(challenger)
                } else if w_existing > w_challenger {
                    Ok(existing)
                } else {
                    Ok(existing.min(challenger))
                }
            }
        }
    }
}

// ── File-loading convenience ──────────────────────────────────────────────────

impl MdixMerger {
    /// Load, merge, and return a fully resolved `DixData` from a list of `.mdix`
    /// file paths.
    ///
    /// Files are assigned weights in descending order so the first path has the
    /// highest weight (`1.0`) and the last path has the lowest (approaching `0.0`).
    /// Use `merge_files_weighted` to supply explicit weights.
    ///
    /// Each file is compiled through the full pipeline (tokenize → parse →
    /// semantic → enhance → value-resolve) before merging, so QuickFunc
    /// calls are already inlined into @DATA values.
    pub fn merge_files(
        &self,
        file_paths: &[impl AsRef<str>],
    ) -> Result<super::dix_data::DixData, String> {
        let n = file_paths.len();
        if n == 0 {
            return Err("merge_files: no file paths provided".to_string());
        }

        let weights: Vec<f64> = (0..n)
            .map(|i| if n == 1 { 1.0 } else { 1.0 - (i as f64 / (n - 1) as f64) })
            .collect();

        let weighted: Vec<(&str, f64)> = file_paths
            .iter()
            .zip(weights.iter())
            .map(|(p, &w)| (p.as_ref(), w))
            .collect();

        self.merge_files_weighted(&weighted)
    }

    /// Load, merge, and return a `DixData` from `(path, weight)` pairs.
    ///
    /// Higher weight = higher priority under `WeightedPriority`.
    pub fn merge_files_weighted(
        &self,
        paths_and_weights: &[(&str, f64)],
    ) -> Result<super::dix_data::DixData, String> {
        if paths_and_weights.is_empty() {
            return Err("merge_files_weighted: no paths provided".to_string());
        }

        let loader = super::loader::DixLoader::new();

        let sources: Vec<MdixMergeInput> = paths_and_weights
            .iter()
            .map(|&(path, weight)| {
                loader
                    .compile_to_resolved_ast(path)
                    .map(|ast| {
                        MdixMergeInput::new(ast)
                            .with_weight(weight)
                            .with_label(path.to_string())
                    })
            })
            .collect::<Result<_, _>>()?;

        let result = self.merge_all(sources);

        if !result.is_success {
            return Err(result.errors.join("; "));
        }

        Ok(super::dix_data::DixData::from_ast(
            result.merged_ast,
            "1.0.0".to_string(),
            chrono::Utc::now(),
            false,
            false,
            vec![],
        ))
    }
}

// ── DixData extension ─────────────────────────────────────────────────────────

impl super::dix_data::DixData {
    /// Convenience: merge two DixData objects at the AST level by re-loading
    /// them from their source paths.
    ///
    /// If you already have `DixScript` ASTs, use `MdixMerger::merge` directly
    /// instead — it avoids the file I/O.
    pub fn merge_files(
        primary_path:   &str,
        secondary_path: &str,
        strategy:       MdixMergeStrategy,
    ) -> Result<super::dix_data::DixData, String> {
        MdixMerger::new()
            .with_strategy(strategy)
            .merge_files(&[primary_path, secondary_path])
    }
}

// ── Private helpers ───────────────────────────────────────────────────────────

/// Numeric key for a `DLMModuleType` — used as the deduplication key in the
/// DLM section merge so only one module of each type survives.
#[inline]
fn dlm_module_type_key(t: DLMModuleType) -> u8 {
    match t {
        DLMModuleType::DCompressor => 0,
        DLMModuleType::DAuditor    => 1,
        DLMModuleType::DEncryptor  => 2,
        DLMModuleType::ParseError  => 255,
    }
}

/// Shallow structural equality for `Value` — used by `ConcatDedup`.
///
/// Only primitive / leaf variants are compared.  Collection types (`Array`,
/// `Object`, `NestedArray`, `PrefixedConstructor`) are always treated as
/// non-equal so they are never silently dropped from a merged group array.
/// Semantic equality for two top-level `@DATA` entries, ignoring Position.
/// See the comment at its call site in `upsert_unique_entry` for scope.
/// Whether two `TableProperty` property lists are equal enough that
/// there's no real conflict to report at all -- same length, and every
/// side-a property has a same-named, `values_are_equal` counterpart on
/// side b. Table properties aren't semantically ordered (`host = ...,
/// port = ...` means the same thing in any order), so this compares as an
/// unordered set of (name, value) pairs, not positionally.
///
/// Mirrors the per-property short-circuit `merge_table_props` already does
/// once it's inside a table conflict (`values_are_equal(&existing_prop.
/// value, &prop.value)`) -- this is that same check applied one level up,
/// so a `TableProperty` whose key merely repeats across sources with
/// identical contents (e.g. diffing a file against itself) never gets
/// treated as a conflict in the first place.
fn table_props_are_equal(a: &[PropertyAssignment], b: &[PropertyAssignment]) -> bool {
    a.len() == b.len()
        && a.iter().all(|pa| {
            b.iter().any(|pb| pa.name == pb.name && values_are_equal(&pa.value, &pb.value))
        })
}

/// Whether two `GroupArray` item lists are equal enough that there's no
/// real conflict to report. Unlike table properties, array items *are*
/// ordered, so this compares positionally rather than as a set.
fn array_items_are_equal(a: &[Value], b: &[Value]) -> bool {
    a.len() == b.len() && a.iter().zip(b).all(|(x, y)| values_are_equal(x, y))
}

fn data_entries_are_equal(a: &DataEntry, b: &DataEntry) -> bool {
    match (a, b) {
        (DataEntry::SimpleProperty { value: v1, .. }, DataEntry::SimpleProperty { value: v2, .. }) => {
            values_are_equal(v1, v2)
        }
        _ => false,
    }
}

fn values_are_equal(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Null { .. },      Value::Null { .. })      => true,
        (Value::Boolean { value: v1, .. }, Value::Boolean { value: v2, .. }) => v1 == v2,
        (Value::Integer { value: v1, .. }, Value::Integer { value: v2, .. }) => v1 == v2,
        (Value::Long    { value: v1, .. }, Value::Long    { value: v2, .. }) => v1 == v2,
        // Float and Double use bit-equal comparison — NaN != NaN as per IEEE 754.
        (Value::Float   { value: v1, .. }, Value::Float   { value: v2, .. }) => v1.to_bits() == v2.to_bits(),
        (Value::Double  { value: v1, .. }, Value::Double  { value: v2, .. }) => v1.to_bits() == v2.to_bits(),
        (Value::ScientificNotation { value: v1, .. }, Value::ScientificNotation { value: v2, .. }) => {
            v1.to_bits() == v2.to_bits()
        }
        (Value::String    { value: v1, .. }, Value::String    { value: v2, .. }) => v1 == v2,
        (Value::HexColor  { value: v1, .. }, Value::HexColor  { value: v2, .. }) => v1 == v2,
        (Value::Date      { value: v1, .. }, Value::Date      { value: v2, .. }) => v1 == v2,
        (Value::Timestamp { value: v1, .. }, Value::Timestamp { value: v2, .. }) => v1 == v2,
        (Value::EnumValue { enum_name: e1, value: v1, .. },
         Value::EnumValue { enum_name: e2, value: v2, .. }) => e1 == e2 && v1 == v2,
        // Complex types — never equal for dedup purposes.
        _ => false,
    }
}

// ── Default ───────────────────────────────────────────────────────────────────

impl Default for MdixMerger {
    fn default() -> Self {
        Self::new()
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Compiler::AST::*;

    // ── helpers ───────────────────────────────────────────────────────────────

    fn int_val(n: i32) -> Value {
        Value::Integer { value: n, position: Position::UNKNOWN }
    }
    fn str_val(s: &str) -> Value {
        Value::String { value: s.into(), position: Position::UNKNOWN }
    }
    fn bool_val(b: bool) -> Value {
        Value::Boolean { value: b, position: Position::UNKNOWN }
    }
    fn long_val(l: i64) -> Value {
        Value::Long { value: l, position: Position::UNKNOWN }
    }

    fn simple_prop(name: &str, value: Value) -> DataEntry {
        DataEntry::SimpleProperty {
            name: name.into(), data_type: None, value, position: Position::UNKNOWN,
        }
    }
    fn table_prop(path: &str, props: Vec<(&str, Value)>) -> DataEntry {
        let segments = path.split('.').map(String::from).collect();
        let properties = props.into_iter().map(|(k, v)| PropertyAssignment {
            name: k.into(), data_type: None, value: v, position: Position::UNKNOWN,
        }).collect();
        DataEntry::TableProperty { path: TablePath { segments }, properties, position: Position::UNKNOWN }
    }
    fn group_array(path: &str, items: Vec<Value>) -> DataEntry {
        let segments = path.split('.').map(String::from).collect();
        DataEntry::GroupArray { path: TablePath { segments }, items, position: Position::UNKNOWN }
    }
    fn config_entry(key: &str, val: &str) -> ConfigEntry {
        ConfigEntry { key: key.into(), value: ConfigValue::String(val.into()), position: Position::UNKNOWN }
    }

    fn ast_with_data(entries: Vec<DataEntry>) -> DixScript {
        let mut s = DixScript::new();
        s.data = Some(DataSection { entries, position: Position::UNKNOWN });
        s
    }
    fn ast_with_config(entries: Vec<ConfigEntry>) -> DixScript {
        let mut s = DixScript::new();
        s.config = Some(ConfigSection { entries, position: Position::UNKNOWN });
        s
    }

    // ── basic merge ───────────────────────────────────────────────────────────

    #[test]
    fn merge_disjoint_simple_properties() {
        let a = ast_with_data(vec![simple_prop("x", int_val(1))]);
        let b = ast_with_data(vec![simple_prop("y", int_val(2))]);

        let result = MdixMerger::new().merge(
            MdixMergeInput::new(a),
            MdixMergeInput::new(b),
        );
        assert!(result.is_success);
        assert!(result.conflicts.is_empty());

        let entries = &result.merged_ast.data.unwrap().entries;
        assert_eq!(entries.len(), 2);
    }

    #[test]
    fn merge_conflicting_simple_property_primary_wins_by_default() {
        let a = ast_with_data(vec![simple_prop("x", int_val(1))]);
        let b = ast_with_data(vec![simple_prop("x", int_val(99))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .merge(
                MdixMergeInput::new(a),
                MdixMergeInput::new(b),
            );

        assert!(result.is_success);
        assert_eq!(result.conflicts.len(), 1);
        assert_eq!(result.conflicts[0].winning_source, 0);

        let entries = &result.merged_ast.data.unwrap().entries;
        assert_eq!(entries.len(), 1);
        if let DataEntry::SimpleProperty { value: Value::Integer { value, .. }, .. } = &entries[0] {
            assert_eq!(*value, 1);
        } else {
            panic!("expected SimpleProperty(int)");
        }
    }

    #[test]
    fn merge_conflicting_simple_property_secondary_wins() {
        let a = ast_with_data(vec![simple_prop("x", int_val(1))]);
        let b = ast_with_data(vec![simple_prop("x", int_val(99))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::SecondaryWins)
            .merge(
                MdixMergeInput::new(a),
                MdixMergeInput::new(b),
            );

        if let DataEntry::SimpleProperty { value: Value::Integer { value, .. }, .. } =
            &result.merged_ast.data.unwrap().entries[0]
        {
            assert_eq!(*value, 99);
        } else {
            panic!("expected 99");
        }
    }

    #[test]
    fn merge_conflicting_simple_property_higher_weight_wins() {
        let a = ast_with_data(vec![simple_prop("x", int_val(1))]);
        let b = ast_with_data(vec![simple_prop("x", int_val(99))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::WeightedPriority)
            .merge(
                MdixMergeInput::new(a).with_weight(0.3),
                MdixMergeInput::new(b).with_weight(0.9),
            );

        if let DataEntry::SimpleProperty { value: Value::Integer { value, .. }, .. } =
            &result.merged_ast.data.unwrap().entries[0]
        {
            assert_eq!(*value, 99, "higher-weight source should win");
        } else {
            panic!("expected SimpleProperty");
        }
    }

    #[test]
    fn throw_on_conflict_produces_error() {
        let a = ast_with_data(vec![simple_prop("x", int_val(1))]);
        let b = ast_with_data(vec![simple_prop("x", int_val(2))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::ThrowOnConflict)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        assert!(!result.is_success);
        assert!(!result.errors.is_empty());
    }

    // ── two-tier ordering ─────────────────────────────────────────────────────

    #[test]
    fn two_tier_ordering_maintained_after_merge() {
        // Source A: has a table property first, then a simple property
        let mut a = DixScript::new();
        a.data = Some(DataSection {
            entries: vec![
                // Deliberately put in wrong order — merge must fix it.
                table_prop("server", vec![("host", str_val("a.local"))]),
                simple_prop("version", str_val("1.0")),
            ],
            position: Position::UNKNOWN,
        });

        // Source B: just a simple property
        let b = ast_with_data(vec![simple_prop("debug", bool_val(true))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        assert!(result.is_success, "{:?}", result.errors);

        let entries = result.merged_ast.data.unwrap().entries;
        // Tier 1 (flat) must come before tier 2 (grouped).
        let flat_positions: Vec<usize> = entries.iter().enumerate()
            .filter(|(_, e)| matches!(e,
                DataEntry::SimpleProperty { .. } | DataEntry::ObjectProperty { .. }))
            .map(|(i, _)| i)
            .collect();
        let grouped_positions: Vec<usize> = entries.iter().enumerate()
            .filter(|(_, e)| matches!(e,
                DataEntry::TableProperty { .. } | DataEntry::GroupArray { .. }))
            .map(|(i, _)| i)
            .collect();

        if !flat_positions.is_empty() && !grouped_positions.is_empty() {
            let last_flat  = *flat_positions.last().unwrap();
            let first_grouped = *grouped_positions.first().unwrap();
            assert!(
                last_flat < first_grouped,
                "flat entries (last at {}) must precede grouped entries (first at {})",
                last_flat, first_grouped
            );
        }
    }

    // ── TableProperty deep-merge ──────────────────────────────────────────────

    #[test]
    fn table_property_same_path_deep_merged() {
        let a = ast_with_data(vec![
            table_prop("server", vec![("host", str_val("a.local")), ("port", int_val(8080))]),
        ]);
        let b = ast_with_data(vec![
            table_prop("server", vec![("port", int_val(9090)), ("ssl", bool_val(true))]),
        ]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        assert!(result.is_success);
        let entries = result.merged_ast.data.unwrap().entries;
        assert_eq!(entries.len(), 1, "should be one merged table entry");

        if let DataEntry::TableProperty { properties, .. } = &entries[0] {
            assert_eq!(properties.len(), 3, "host + port (primary wins) + ssl from secondary");
            let host = properties.iter().find(|p| p.name == "host").unwrap();
            assert!(matches!(&host.value, Value::String { value, .. } if *value == "a.local"));
            let port = properties.iter().find(|p| p.name == "port").unwrap();
            assert!(matches!(&port.value, Value::Integer { value: 8080, .. }));
            let ssl = properties.iter().find(|p| p.name == "ssl").unwrap();
            assert!(matches!(&ssl.value, Value::Boolean { value: true, .. }));
        } else {
            panic!("expected TableProperty");
        }
    }

    // ── GroupArray merge strategies ───────────────────────────────────────────

    #[test]
    fn group_array_concat_dedup_removes_duplicates() {
        let a = ast_with_data(vec![group_array("tags", vec![str_val("alpha"), str_val("beta")])]);
        let b = ast_with_data(vec![group_array("tags", vec![str_val("beta"), str_val("gamma")])]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .with_array_strategy(ArrayMergeStrategy::ConcatDedup)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        assert!(result.is_success);
        if let DataEntry::GroupArray { items, .. } =
            &result.merged_ast.data.unwrap().entries[0]
        {
            assert_eq!(items.len(), 3, "alpha, beta, gamma — beta deduped");
        }
    }

    #[test]
    fn group_array_replace_uses_winner_only() {
        let a = ast_with_data(vec![group_array("tags", vec![str_val("alpha")])]);
        let b = ast_with_data(vec![group_array("tags", vec![str_val("omega")])]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::SecondaryWins)
            .with_array_strategy(ArrayMergeStrategy::Replace)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        if let DataEntry::GroupArray { items, .. } =
            &result.merged_ast.data.unwrap().entries[0]
        {
            assert_eq!(items.len(), 1);
            assert!(matches!(&items[0], Value::String { value, .. } if *value == "omega"));
        }
    }

    // ── Config merge ──────────────────────────────────────────────────────────

    #[test]
    fn config_sections_merged_with_dedup() {
        let a = ast_with_config(vec![
            config_entry("version", "1.0.0"),
            config_entry("author", "Alice"),
        ]);
        let b = ast_with_config(vec![
            config_entry("version", "2.0.0"),
            config_entry("debug", "off"),
        ]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        assert!(result.is_success);
        let cfg = result.merged_ast.config.unwrap();
        assert_eq!(cfg.entries.len(), 3); // version(primary), author, debug

        let version = cfg.entries.iter().find(|e| e.key == "version").unwrap();
        assert!(matches!(&version.value, ConfigValue::String(s) if *s == "1.0.0"));
    }

    // ── Enum deep-merge ───────────────────────────────────────────────────────

    #[test]
    fn enum_same_name_fields_are_deep_merged() {
        let make_enum = |name: &str, fields: Vec<(&str, Option<i32>)>| {
            let mut s = DixScript::new();
            s.enums = Some(EnumsSection {
                enums: vec![EnumDeclaration {
                    name: name.into(),
                    fields: fields.into_iter().map(|(n, v)| EnumField {
                        name: n.into(), value: v, position: Position::UNKNOWN,
                    }).collect(),
                    position: Position::UNKNOWN,
                }],
                position: Position::UNKNOWN,
            });
            s
        };

        let a = make_enum("Status", vec![("ACTIVE", Some(0)), ("INACTIVE", Some(1))]);
        let b = make_enum("Status", vec![("INACTIVE", Some(99)), ("PENDING", Some(2))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .merge(MdixMergeInput::new(a), MdixMergeInput::new(b));

        assert!(result.is_success);
        let decl = &result.merged_ast.enums.unwrap().enums[0];
        assert_eq!(decl.fields.len(), 3); // ACTIVE, INACTIVE (primary=1), PENDING

        let inactive = decl.fields.iter().find(|f| f.name == "INACTIVE").unwrap();
        assert_eq!(inactive.value, Some(1), "primary wins on field conflict");

        assert!(decl.fields.iter().any(|f| f.name == "PENDING"), "new field from secondary added");
    }

    // ── Multi-source merge ────────────────────────────────────────────────────

    #[test]
    fn merge_all_three_sources() {
        let a = ast_with_data(vec![simple_prop("a", int_val(1))]);
        let b = ast_with_data(vec![simple_prop("b", int_val(2))]);
        let c = ast_with_data(vec![simple_prop("c", int_val(3)), simple_prop("a", int_val(99))]);

        let result = MdixMerger::new()
            .with_strategy(MdixMergeStrategy::PrimaryWins)
            .merge_all(vec![
                MdixMergeInput::new(a).with_label("A"),
                MdixMergeInput::new(b).with_label("B"),
                MdixMergeInput::new(c).with_label("C"),
            ]);

        assert!(result.is_success);
        assert_eq!(result.conflicts.len(), 1, "only 'a' conflicts");
        assert_eq!(result.conflicts[0].winning_source, 0); // source A wins

        let entries = result.merged_ast.data.unwrap().entries;
        assert_eq!(entries.len(), 3, "a, b, c");

        if let DataEntry::SimpleProperty { name, value: Value::Integer { value, .. }, .. } =
            entries.iter().find(|e| matches!(e, DataEntry::SimpleProperty { name, .. } if *name == "a")).unwrap()
        {
            assert_eq!(*value, 1);
        }
    }

    // ── Empty / single source ─────────────────────────────────────────────────

    #[test]
    fn single_source_returns_as_is() {
        let ast = ast_with_data(vec![simple_prop("x", long_val(42))]);
        let result = MdixMerger::new().merge_all(vec![MdixMergeInput::new(ast.clone())]);
        assert!(result.is_success);
        assert!(result.conflicts.is_empty());
    }

    #[test]
    fn empty_sources_fails_gracefully() {
        let result = MdixMerger::new().merge_all(vec![]);
        assert!(!result.is_success);
        assert!(!result.errors.is_empty());
    }

    // ── Long / typed value survival ───────────────────────────────────────────

    #[test]
    fn long_values_survive_merge() {
        let a = ast_with_data(vec![simple_prop("big", long_val(9_000_000_000))]);
        let b = ast_with_data(vec![simple_prop("small", int_val(42))]);

        let result = MdixMerger::new().merge(
            MdixMergeInput::new(a),
            MdixMergeInput::new(b),
        );

        assert!(result.is_success);
        let entries = result.merged_ast.data.unwrap().entries;
        assert!(entries.iter().any(|e| matches!(e,
            DataEntry::SimpleProperty { value: Value::Long { value: 9_000_000_000, .. }, .. }
        )));
    }

    // ── values_are_equal ─────────────────────────────────────────────────────

    #[test]
    fn values_are_equal_primitives() {
        assert!(values_are_equal(&int_val(42), &int_val(42)));
        assert!(!values_are_equal(&int_val(42), &int_val(43)));
        assert!(values_are_equal(&str_val("x"), &str_val("x")));
        assert!(!values_are_equal(&str_val("x"), &str_val("y")));
        // Complex types are never equal
        let arr = Value::Array { values: vec![], position: Position::UNKNOWN };
        assert!(!values_are_equal(&arr, &arr));
    }
            }