trible 0.41.2

A knowledge graph and meta file system for object stores.
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
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
use anyhow::Result;
use clap::Parser;
use std::collections::HashMap;
use std::convert::TryInto;
use std::io::{Read, Seek, SeekFrom};
use std::path::PathBuf;

// DEFAULT_MAX_PILE_SIZE removed; the new Pile API no longer uses a size const generic

use triblespace::prelude::blobencodings::SimpleArchive;
use triblespace::prelude::BlobStore;
use triblespace::prelude::BlobStoreGet;
use triblespace::prelude::BlobStorePut;
use triblespace::prelude::BranchStore;
use triblespace::prelude::View;
use triblespace_core::blob::encodings::longstring::LongString;
use triblespace_core::blob::IntoBlob;
use triblespace_core::id::id_hex;
use triblespace_core::id::Id;
use triblespace_core::repo::pile::Pile;
use triblespace_core::repo::Repository;
use triblespace_core::trible::TribleSet;
use triblespace_core::inline::encodings::hash::{Blake3, Handle, Hash};
use triblespace_core::inline::Inline;

use super::signing::load_signing_key;
use triblespace_core::repo::BlobStoreMeta;

type BranchNameHandle = Inline<Handle<LongString>>;

// These markers are part of the stable on-disk pile format (see
// triblespace-rs/book/src/pile-format.md). Copy them exactly; do not invent.
#[allow(non_upper_case_globals)]
const MAGIC_MARKER_BLOB: Id = id_hex!("1E08B022FF2F47B6EBACF1D68EB35D96");
#[allow(non_upper_case_globals)]
const MAGIC_MARKER_BRANCH: Id = id_hex!("2BC991A7F5D5D2A3A468C53B0AA03504");
#[allow(non_upper_case_globals)]
const MAGIC_MARKER_BRANCH_TOMBSTONE: Id = id_hex!("E888CC787202D2AE4C654BFE9699C430");

const RECORD_LEN: u64 = 64;

#[derive(Parser)]
pub enum Command {
    /// List branches in a pile file (id + head + name).
    List {
        /// Path to the pile file to inspect
        path: PathBuf,
        /// Include all branches ever seen (scans raw pile records, including deleted)
        #[arg(long)]
        all: bool,
        /// Only show deleted/tombstoned branches (implies --all)
        #[arg(long)]
        deleted: bool,
    },
    /// Create a new branch in a pile file.
    Create {
        /// Path to the pile file to modify
        pile: PathBuf,
        /// Name of the branch to create
        name: String,
        /// Optional signing key path. The file should contain a 64-char hex seed.
        #[arg(long)]
        signing_key: Option<PathBuf>,
    },
    /// Inspect a branch in a pile and print its id, name, and current head handle.
    Inspect {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Branch identifier to inspect (hex encoded)
        branch: String,
    },
    /// Delete a branch in a pile (writes a tombstone).
    Delete {
        /// Path to the pile file to modify
        pile: PathBuf,
        /// Branch identifier to delete (hex encoded)
        branch: String,
    },
    /// Set the branch metadata handle for a branch in a pile (CAS update).
    ///
    /// This updates the branch store head to point at the provided branch
    /// metadata blob handle. The pile does not verify that the referenced blob
    /// exists (head-only piles are allowed).
    Set {
        /// Path to the pile file to modify
        pile: PathBuf,
        /// Branch identifier to set (hex encoded)
        branch: String,
        /// Branch metadata blob handle (64 hex chars, optionally prefixed with `blake3:`)
        meta: String,
        /// Expected current branch metadata blob handle (CAS). Uses current head when omitted.
        #[arg(long)]
        expected: Option<String>,
    },
    /// Show a reflog-like history of branch head updates stored in the pile.
    ///
    /// This scans the pile file for branch update and tombstone records and
    /// prints the most recent entries for a branch (latest first).
    Reflog {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Branch identifier to inspect (hex encoded)
        branch: String,
        /// Maximum results to print
        #[arg(long, default_value_t = 50)]
        limit: usize,
    },
    /// Export a branch from one pile into another, copying reachable blobs.
    ///
    /// This transfers all blobs reachable from the source branch metadata into
    /// the destination pile and sets the destination branch head to the same
    /// branch metadata handle (preserving the branch id).
    Export {
        /// Path to the source pile file
        #[arg(long)]
        from_pile: PathBuf,
        /// Branch identifier to export (hex encoded)
        #[arg(long)]
        branch: String,
        /// Path to the destination pile file
        #[arg(long)]
        to_pile: PathBuf,
    },
    /// Show statistics for a branch.
    Stats {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Branch identifier to inspect (hex encoded)
        branch: String,
        /// Also compute unique triples/entities/attributes by materializing commit content.
        #[arg(long, default_value_t = false)]
        full: bool,
    },
    /// Import reachable blobs from a source branch into a target pile and
    /// attach them to the target branch via a single merge commit.
    MergeImport {
        /// Path to the source pile file
        #[arg(long)]
        from_pile: PathBuf,
        /// Source branch identifier (hex)
        #[arg(long)]
        from_id: String,

        /// Path to the destination pile file
        #[arg(long)]
        to_pile: PathBuf,
        /// Destination branch identifier (hex)
        #[arg(long)]
        to_id: String,
        /// Optional signing key path. The file should contain a 64-char hex seed.
        #[arg(long)]
        signing_key: Option<PathBuf>,
    },
    /// Consolidate multiple branches into a single new branch.
    Consolidate {
        /// Path to the pile file to modify
        pile: PathBuf,
        /// Branch identifier(s) to consolidate (hex encoded).
        /// Ignored when --include-deleted is set.
        #[arg(num_args = 0..)]
        branches: Vec<String>,
        /// Optional name for the newly created consolidated branch
        #[arg(long)]
        out_name: Option<String>,
        /// Dry run: show what would be done without making changes
        #[arg(long)]
        dry_run: bool,
        /// Delete (tombstone) the source branches after consolidation
        #[arg(long)]
        delete_sources: bool,
        /// Group active branches by name and consolidate each group with
        /// subsumption detection. `branches` list is ignored.
        #[arg(long)]
        by_name: bool,
        /// Like --by-name but also includes tombstoned/historical branches
        /// by scanning the raw pile file.
        #[arg(long, conflicts_with = "by_name")]
        by_name_include_deleted: bool,
        /// Optional signing key path. The file should contain a 64-char hex seed.
        #[arg(long)]
        signing_key: Option<PathBuf>,
    },
    /// Walk the commit history of a branch (newest first).
    Log {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Branch identifier (hex encoded)
        branch: String,
        /// Maximum commits to print
        #[arg(long, default_value_t = 50)]
        limit: usize,
        /// Compact one-line-per-commit format
        #[arg(long)]
        oneline: bool,
    },
    /// Census attribute IDs across all commits in a branch.
    Describe {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Branch identifier (hex encoded)
        branch: String,
        /// Also show per-entity breakdown
        #[arg(long)]
        entities: bool,
    },
    /// Display a single commit's structure.
    Show {
        /// Path to the pile file to inspect
        pile: PathBuf,
        /// Commit handle (blake3:... or raw 64-char hex)
        commit: String,
    },
    /// Rename a branch (creates a new branch with the new name pointing
    /// to the same commit, then deletes the old one).
    Rename {
        /// Path to the pile file to modify
        pile: PathBuf,
        /// Branch to rename (name or hex id)
        branch: String,
        /// New name for the branch
        new_name: String,
        /// Optional signing key path. The file should contain a 64-char hex seed.
        #[arg(long)]
        signing_key: Option<PathBuf>,
    },
}

pub fn run(cmd: Command) -> Result<()> {
    match cmd {
        Command::List { path, all, deleted } => {
            use triblespace_core::repo::pile::Pile;

            if all || deleted {
                // Raw pile scan mode (absorbs former `journal` command).
                let mut pile: Pile = Pile::open(&path)?;
                let res = (|| -> Result<(), anyhow::Error> {
                    pile.refresh()?;
                    let reader = pile
                        .reader()
                        .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                    let records = scan_pile_records(&path)?;
                    let states = collapse_branch_states(&records);

                    let mut rows: Vec<(Id, &BranchState)> = states.iter().map(|(id, s)| (*id, s)).collect();
                    rows.sort_by_key(|(id, _)| *id);

                    for (id, state) in rows {
                        if deleted && state.kind != RecordKind::Tombstone {
                            continue;
                        }

                        let meta_handle = match state.kind {
                            RecordKind::Set => state.meta,
                            RecordKind::Tombstone => state.last_set,
                        };

                        let kind = match state.kind {
                            RecordKind::Set => "set",
                            RecordKind::Tombstone => "delete",
                        };

                        let mut name = "-".to_string();
                        let mut head_str = "-".to_string();

                        if let Some(mh) = meta_handle {
                            if reader.metadata(mh)?.is_some() {
                                if let Ok(meta_set) = reader.get::<TribleSet, _>(mh) {
                                    if let Ok(Some(n)) = load_branch_name(&reader, &meta_set) {
                                        name = n;
                                    }
                                    if let Some(h) = extract_repo_head(&meta_set) {
                                        head_str = format!("blake3:{}", hex::encode(h.raw));
                                    }
                                }
                            }
                        }

                        println!("{id:X}\t{kind}\t{head_str}\t{name}");
                    }
                    Ok(())
                })();
                let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
                res.and(close_res)?;
            } else {
                // Default mode: list active branches via pile.branches().
                let mut pile: Pile = Pile::open(&path)?;
                let res = (|| -> Result<(), anyhow::Error> {
                    pile.refresh()?;
                    let reader = pile
                        .reader()
                        .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;
                    let iter = pile.branches()?;
                    let head_attr = triblespace_core::repo::head.id();
                    let mut rows: Vec<(String, Id, String)> = Vec::new();
                    for branch in iter {
                        let id = branch?;
                        let meta_handle = match pile.head(id)? {
                            Some(handle) => handle,
                            None => {
                                rows.push(("<deleted>".to_string(), id, "-".to_string()));
                                continue;
                            }
                        };

                        let (name, head) = match reader.get::<TribleSet, _>(meta_handle) {
                            Ok(meta) => {
                                let name_attr = triblespace_core::metadata::name.id();
                                let mut name_handle: Option<BranchNameHandle> = None;
                                let mut head_handle: Option<Inline<Handle<SimpleArchive>>> =
                                    None;
                                for t in meta.iter() {
                                    if t.a() == &name_attr {
                                        let h: BranchNameHandle = *t.v();
                                        if name_handle.replace(h).is_some() {
                                            name_handle = None;
                                            break;
                                        }
                                    } else if t.a() == &head_attr {
                                        let h: Inline<Handle<SimpleArchive>> = *t.v();
                                        if head_handle.replace(h).is_some() {
                                            head_handle = None;
                                        }
                                    }
                                }

                                let name = match name_handle {
                                    None => "<unnamed>".to_string(),
                                    Some(handle) => match reader.get::<View<str>, _>(handle) {
                                        Ok(view) => view.as_ref().to_string(),
                                        Err(_) => format!(
                                            "<name blob missing ({})>",
                                            hex::encode_upper(&handle.raw[..4])
                                        ),
                                    },
                                };

                                let head = match head_handle {
                                    None => "-".to_string(),
                                    Some(handle) => format!("blake3:{}", hex::encode(handle.raw)),
                                };

                                (name, head)
                            }
                            Err(_) => (
                                format!(
                                    "<metadata blob missing ({})>",
                                    hex::encode_upper(&meta_handle.raw[..4])
                                ),
                                "-".to_string(),
                            ),
                        };

                        rows.push((name, id, head));
                    }

                    rows.sort_by(|(a_name, a_id, _), (b_name, b_id, _)| {
                        a_name.cmp(b_name).then_with(|| a_id.cmp(b_id))
                    });

                    for (name, id, head) in rows {
                        println!("{id:X}\t{head}\t{name}");
                    }
                    Ok(())
                })();
                let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
                res.and(close_res)?;
            }
        }
        Command::Create {
            pile,
            name,
            signing_key,
        } => {
            use triblespace_core::repo::pile::Pile;
            use triblespace_core::repo::Repository;
            
            let pile: Pile = Pile::open(&pile)?;
            let key = load_signing_key(&signing_key)?;
            let mut repo = Repository::new(pile, key, TribleSet::new())?;

            let res = (|| -> Result<(), anyhow::Error> {
                let branch_id = repo
                    .create_branch(&name, None)
                    .map_err(|e| anyhow::anyhow!("{e:?}"))?;
                println!("{:#X}", *branch_id);
                Ok(())
            })();

            // Ensure the underlying pile is closed whether the command succeeds or fails.
            let close_res = repo
                .into_storage()
                .close()
                .map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Inspect { pile, branch } => {
            use triblespace::prelude::blobencodings::SimpleArchive;
            use triblespace::prelude::inlineencodings::Handle;

            use triblespace_core::repo::pile::Pile;
            use triblespace_core::trible::TribleSet;
            use triblespace_core::inline::encodings::hash::Blake3;
            use triblespace_core::inline::encodings::hash::Hash;
            use triblespace_core::inline::Inline;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                let branch_id = parse_branch_id_hex(&branch)?;

                let meta_handle = pile
                    .head(branch_id)?
                    .ok_or_else(|| anyhow::anyhow!("branch not found"))?;
                let reader = pile
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;
                let meta_present = reader.metadata(meta_handle)?.is_some();
                let (name_val, head_val, head_err): (
                    Option<String>,
                    Option<Inline<Handle<SimpleArchive>>>,
                    Option<String>,
                ) = if meta_present {
                    match reader.get::<TribleSet, SimpleArchive>(meta_handle) {
                        Ok(meta) => {
                            let mut head_val: Option<Inline<Handle<SimpleArchive>>> = None;
                            let repo_head_attr = triblespace_core::repo::head.id();
                            for t in meta.iter() {
                                if t.a() == &repo_head_attr {
                                    let h = *t.v::<Handle<SimpleArchive>>();
                                    head_val = Some(h);
                                }
                            }
                            let name_val = load_branch_name(&reader, &meta)?;
                            (name_val, head_val, None)
                        }
                        Err(e) => (None, None, Some(format!("decode failed: {e:?}"))),
                    }
                } else {
                    (None, None, None)
                };

                let id_hex = format!("{branch_id:X}");
                let meta_hash: Inline<Hash<Blake3>> = Handle::to_hash(meta_handle);
                let meta_hex: String = meta_hash.from_inline();

                println!("Id:        {id_hex}");
                if let Some(nstr) = name_val.clone() {
                    println!("Name:      {nstr}");
                }
                println!(
                    "Meta:      {meta_hex} [{}]{}",
                    if meta_present { "present" } else { "missing" },
                    head_err
                        .as_deref()
                        .map(|e| format!(" ({e})"))
                        .unwrap_or_default()
                );
                if let Some(h) = head_val {
                    let head_hash: Inline<Hash<Blake3>> = Handle::to_hash(h);
                    let head_hex: String = head_hash.from_inline();
                    let present = reader.metadata(h)?.is_some();
                    println!(
                        "Head:      {head_hex} [{}]",
                        if present { "present" } else { "missing" }
                    );
                }
                Ok(())
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Delete { pile, branch } => {
            use triblespace_core::repo::pile::Pile;
            

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                let branch_id = parse_branch_id_hex(&branch)?;

                let old = pile
                    .head(branch_id)?
                    .ok_or_else(|| anyhow::anyhow!("branch not found"))?;

                match pile.update(branch_id, Some(old), None)? {
                    triblespace_core::repo::PushResult::Success() => {
                        println!("deleted branch {branch_id:X}");
                        Ok(())
                    }
                    triblespace_core::repo::PushResult::Conflict(_) => {
                        anyhow::bail!("branch {branch_id:X} advanced concurrently; rerun delete")
                    }
                }
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Set {
            pile,
            branch,
            meta,
            expected,
        } => {
            use triblespace::prelude::blobencodings::SimpleArchive;
            use triblespace::prelude::inlineencodings::Handle;
            use triblespace_core::repo::pile::Pile;
            
            use triblespace_core::inline::Inline;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                let branch_id = parse_branch_id_hex(&branch)?;
                let new_meta: Inline<Handle<SimpleArchive>> = parse_blake3_handle(&meta)?;

                let expected_old: Option<Inline<Handle<SimpleArchive>>> = match expected {
                    Some(s) => parse_blake3_handle_opt(&s)?,
                    None => pile.head(branch_id)?,
                };

                match pile.update(branch_id, expected_old, Some(new_meta))? {
                    triblespace_core::repo::PushResult::Success() => {
                        println!(
                            "set branch {bid:X} meta blake3:{meta}",
                            bid = branch_id,
                            meta = hex::encode(new_meta.raw)
                        );
                        Ok(())
                    }
                    triblespace_core::repo::PushResult::Conflict(existing) => {
                        let got = existing
                            .map(|h| format!("blake3:{}", hex::encode(h.raw)))
                            .unwrap_or_else(|| "-".to_string());
                        anyhow::bail!("branch head changed concurrently; current={got}")
                    }
                }
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Reflog {
            pile,
            branch,
            limit,
        } => {
            use triblespace_core::repo::pile::Pile;

            let branch_id = parse_branch_id_hex(&branch)?;

            let mut pile_reader: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                pile_reader.refresh()?;
                let reader = pile_reader
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                let all_records = scan_pile_records(&pile)?;

                // Filter to this branch, keep last `limit` entries.
                let branch_records: Vec<&RawBranchRecord> = all_records
                    .iter()
                    .filter(|r| r.branch_id == branch_id)
                    .collect();
                let start = branch_records.len().saturating_sub(limit);
                let tail = &branch_records[start..];

                // Print latest first, like git's reflog.
                for (idx, rec) in tail.iter().rev().enumerate() {
                    let offset = rec.offset;
                    let kind = match rec.kind {
                        RecordKind::Set => "set",
                        RecordKind::Tombstone => "delete",
                    };

                    let meta = match rec.meta_handle {
                        None => "-".to_string(),
                        Some(h) => format!("blake3:{}", hex::encode(h.raw)),
                    };

                    let mut head_str = "-".to_string();
                    let mut head_state = "-";
                    let mut name: Option<String> = None;
                    let meta_state;

                    if let Some(mh) = rec.meta_handle {
                        let present = reader.metadata(mh)?.is_some();
                        meta_state = if present { "present" } else { "missing" };
                        if present {
                            if let Ok(meta_set) = reader.get::<TribleSet, _>(mh) {
                                name = load_branch_name(&reader, &meta_set).ok().flatten();
                                if let Some(h) = extract_repo_head(&meta_set) {
                                    head_str = format!("blake3:{}", hex::encode(h.raw));
                                    head_state = if reader.metadata(h)?.is_some() {
                                        "present"
                                    } else {
                                        "missing"
                                    };
                                }
                            }
                        }
                    } else {
                        meta_state = "-";
                    }

                    let name = name.as_deref().unwrap_or("-");
                    println!(
                        "{idx}\toffset={offset}\t{kind}\tmeta={meta}\tmeta[{meta_state}]\thead={head_str}\thead[{head_state}]\tname={name}"
                    );
                }
                Ok(())
            })();

            let close_res = pile_reader
                .close()
                .map_err(|e| anyhow::anyhow!("close pile: {e:?}"));
            res.and(close_res)?;
        }
        Command::Export {
            from_pile,
            branch,
            to_pile,
        } => {
            use triblespace_core::repo;
            use triblespace_core::repo::pile::Pile;
            
            use triblespace_core::inline::encodings::hash::Handle;
            use triblespace_core::inline::Inline;

            let bid = parse_branch_id_hex(&branch)?;

            let mut src: Pile = Pile::open(&from_pile)?;
            let mut dst: Pile = match Pile::open(&to_pile) {
                Ok(pile) => pile,
                Err(err) => {
                    let _ = src.close();
                    return Err(err.into());
                }
            };

            let res = (|| -> Result<(), anyhow::Error> {
                // Obtain the source branch metadata handle (root) and ensure it exists.
                let src_meta = src
                    .head(bid)?
                    .ok_or_else(|| anyhow::anyhow!("source branch head not found"))?;

                // Prepare a mapping from source handle raw -> destination handle for later lookup.
                use std::collections::HashMap;
                use triblespace_core::inline::INLINE_LEN;
                let mut mapping: HashMap<[u8; INLINE_LEN], Inline<Handle<_>>> =
                    HashMap::new();

                let src_reader = src
                    .reader()
                    .map_err(|e| anyhow::anyhow!("src pile reader error: {e:?}"))?;
                let handles = repo::reachable(&src_reader, std::iter::once(src_meta.transmute()));

                let mut visited: usize = 0;
                let mut stored: usize = 0;
                for r in repo::transfer(&src_reader, &mut dst, handles) {
                    match r {
                        Ok((src_h, dst_h)) => {
                            visited += 1;
                            stored += 1;
                            mapping.insert(src_h.raw, dst_h);
                        }
                        Err(e) => return Err(anyhow::anyhow!("transfer failed: {e}")),
                    }
                }

                // Find the destination handle corresponding to the source branch meta.
                let dst_meta = mapping
                    .get(&src_meta.raw)
                    .ok_or_else(|| {
                        anyhow::anyhow!("destination meta handle not found after transfer")
                    })?
                    .clone();

                // Update the destination pile branch pointer to the copied meta handle.
                let old = dst.head(bid)?;
                let res = dst
                    .update(bid, old, Some(dst_meta.transmute()))
                    .map_err(|e| anyhow::anyhow!("destination branch update failed: {e:?}"))?;
                match res {
                    triblespace_core::repo::PushResult::Success() => {
                        println!(
                            "export: copied visited={} stored={} and set branch {:#X}",
                            visited, stored, bid
                        );
                    }
                    triblespace_core::repo::PushResult::Conflict(existing) => {
                        println!("export: copied visited={} stored={} but branch update conflicted: existing={:?}", visited, stored, existing);
                    }
                }
                Ok(())
            })();

            let close_src = src.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            let close_dst = dst.close().map_err(|e| anyhow::anyhow!("{e:?}"));

            match res {
                Ok(()) => {
                    close_src?;
                    close_dst?;
                    Ok(())
                }
                Err(err) => {
                    if let Err(close_err) = close_src {
                        eprintln!("warning: failed to close source pile cleanly: {close_err:#}");
                    }
                    if let Err(close_err) = close_dst {
                        eprintln!(
                            "warning: failed to close destination pile cleanly: {close_err:#}"
                        );
                    }
                    Err(err)
                }
            }?;
        }
        Command::Stats { pile, branch, full } => {
            use std::collections::{BTreeSet, HashSet};
            use triblespace::prelude::blobencodings::SimpleArchive;
            use triblespace::prelude::inlineencodings::Handle;

            use triblespace_core::repo::pile::Pile;
            use triblespace_core::trible::TribleSet;
            use triblespace_core::inline::encodings::hash::Blake3;
            use triblespace_core::inline::encodings::hash::Hash;
            use triblespace_core::inline::Inline;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                // Ensure indices are loaded before scanning
                pile.refresh()?;
                let reader = pile
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                let branch_id = parse_branch_id_hex(&branch)?;

                // Traversal attributes
                let repo_parent_attr = triblespace_core::repo::parent.id();
                let repo_content_attr = triblespace_core::repo::content.id();

                // Resolve branch head
                let meta_handle = pile
                    .head(branch_id)?
                    .ok_or_else(|| anyhow::anyhow!("branch not found"))?;

                let mut head_opt: Option<Inline<Handle<SimpleArchive>>> = None;
                if reader.metadata(meta_handle)?.is_some() {
                    if let Ok(meta) = reader.get::<TribleSet, SimpleArchive>(meta_handle) {
                        let repo_head_attr = triblespace_core::repo::head.id();
                        for t in meta.iter() {
                            if t.a() == &repo_head_attr {
                                head_opt = Some(*t.v::<Handle<SimpleArchive>>());
                                break;
                            }
                        }
                    }
                }

                let head = head_opt.ok_or_else(|| anyhow::anyhow!("branch has no head set"))?;

                // Traverse commit graph, union content tribles
                let mut visited: BTreeSet<String> = BTreeSet::new();
                let mut stack: Vec<Inline<Handle<SimpleArchive>>> = vec![head];
                let mut commit_count: usize = 0;
                let mut total_triples_accum: usize = 0;
                let mut content_blob_count: usize = 0;
                let mut content_bytes_total: u64 = 0;
                let mut content_misaligned_count: usize = 0;
                let mut unioned = TribleSet::new();

                while let Some(h) = stack.pop() {
                    let hh: Inline<Hash<Blake3>> = Handle::to_hash(h);
                    let hex: String = hh.from_inline();
                    if !visited.insert(hex.clone()) {
                        continue;
                    }
                    commit_count += 1;

                    if reader.metadata(h)?.is_none() {
                        continue;
                    }

                    let meta: TribleSet = match reader.get::<TribleSet, SimpleArchive>(h) {
                        Ok(m) => m,
                        Err(_) => continue,
                    };

                    let mut parents: Vec<Inline<Handle<SimpleArchive>>> = Vec::new();
                    let mut content_handles: Vec<Inline<Handle<SimpleArchive>>> = Vec::new();
                    for t in meta.iter() {
                        if t.a() == &repo_content_attr {
                            let c = *t.v::<Handle<SimpleArchive>>();
                            content_handles.push(c);
                        } else if t.a() == &repo_parent_attr {
                            parents.push(*t.v::<Handle<SimpleArchive>>());
                        }
                    }

                    for c in content_handles {
                        let Some(content_meta) = reader.metadata(c)? else {
                            continue;
                        };
                        content_blob_count = content_blob_count.saturating_add(1);
                        content_bytes_total =
                            content_bytes_total.saturating_add(content_meta.length);
                        let triples_from_length =
                            (content_meta.length / 64).try_into().unwrap_or(usize::MAX);
                        total_triples_accum =
                            total_triples_accum.saturating_add(triples_from_length);
                        if content_meta.length % 64 != 0 {
                            content_misaligned_count = content_misaligned_count.saturating_add(1);
                        }
                        if full {
                            let content: TribleSet = match reader.get::<TribleSet, SimpleArchive>(c)
                            {
                                Ok(s) => s,
                                Err(_) => continue,
                            };
                            unioned += content;
                        }
                    }

                    for p in parents {
                        stack.push(p);
                    }
                }

                println!("Branch: {branch_id:X}");
                println!("Commits: {commit_count}");
                println!("Content blobs (accum): {content_blob_count}");
                println!("Content bytes (accum): {content_bytes_total}");
                println!("Triples (accum): {total_triples_accum}");
                if content_misaligned_count > 0 {
                    println!("Warning: {content_misaligned_count} content blob(s) had non-64-byte-aligned length.");
                }
                if full {
                    // Count unique triples, entities, and attributes only when explicitly requested.
                    let unique_triples = unioned.len();
                    let mut entities: HashSet<Id> = HashSet::new();
                    let mut attributes: HashSet<Id> = HashSet::new();
                    for t in unioned.iter() {
                        entities.insert(*t.e());
                        attributes.insert(*t.a());
                    }
                    println!("Triples (unique): {unique_triples}");
                    println!("Entities: {}", entities.len());
                    println!("Attributes: {}", attributes.len());
                }

                Ok(())
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::MergeImport {
            from_pile,
            from_id,
            to_pile,
            to_id,
            signing_key,
        } => {
            use triblespace::prelude::blobencodings::SimpleArchive;
            use triblespace_core::repo;
            use triblespace_core::repo::pile::Pile;
            use triblespace_core::repo::Repository;
            
            use triblespace_core::inline::encodings::hash::Handle;
            use triblespace_core::inline::Inline;

            struct CopyStats {
                visited: usize,
                stored: usize,
            }

            let src_bid = parse_branch_id_hex(&from_id)?;
            let dst_bid = parse_branch_id_hex(&to_id)?;
            let key = load_signing_key(&signing_key)?;

            let mut src: Pile = Pile::open(&from_pile)?;
            let dst_pile: Pile = match Pile::open(&to_pile) {
                Ok(pile) => pile,
                Err(err) => {
                    let _ = src.close();
                    return Err(err.into());
                }
            };

            let mut repo = Repository::new(dst_pile, key, TribleSet::new())?;
            let result = (|| -> Result<CopyStats, anyhow::Error> {
                let src_head: Inline<Handle<SimpleArchive>> = src
                    .head(src_bid)?
                    .ok_or_else(|| anyhow::anyhow!("source branch head not found"))?;

                let src_reader = src
                    .reader()
                    .map_err(|e| anyhow::anyhow!("src pile reader error: {e:?}"))?;

                let handles = repo::reachable(&src_reader, std::iter::once(src_head.transmute()));
                let mut visited: usize = 0;
                let mut stored: usize = 0;
                for r in repo::transfer(&src_reader, repo.storage_mut(), handles) {
                    match r {
                        Ok((_src_h, _dst_h)) => {
                            visited += 1;
                            stored += 1;
                        }
                        Err(e) => return Err(anyhow::anyhow!("transfer failed: {e}")),
                    }
                }

                let mut ws = repo
                    .pull(dst_bid)
                    .map_err(|e| anyhow::anyhow!("failed to open destination branch: {e:?}"))?;
                ws.merge_commit(src_head)
                    .map_err(|e| anyhow::anyhow!("merge failed: {e:?}"))?;

                while let Some(mut incoming) = repo
                    .try_push(&mut ws)
                    .map_err(|e| anyhow::anyhow!("push failed: {e:?}"))?
                {
                    incoming
                        .merge(&mut ws)
                        .map_err(|e| anyhow::anyhow!("merge conflict: {e:?}"))?;
                    ws = incoming;
                }

                Ok(CopyStats { visited, stored })
            })();

            let close_src = src.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            let close_dst = repo
                .into_storage()
                .close()
                .map_err(|e| anyhow::anyhow!("{e:?}"));

            match result {
                Ok(stats) => {
                    close_src?;
                    close_dst?;
                    println!(
                        "merge-import: copied visited={} stored={} and attached source head to destination branch",
                        stats.visited, stats.stored
                    );
                    Ok(())
                }
                Err(err) => {
                    if let Err(close_err) = close_src {
                        eprintln!("warning: failed to close source pile cleanly: {close_err:#}");
                    }
                    if let Err(close_err) = close_dst {
                        eprintln!(
                            "warning: failed to close destination pile cleanly: {close_err:#}"
                        );
                    }
                    Err(err)
                }
            }?;
        }
        Command::Consolidate {
            pile,
            branches,
            out_name,
            dry_run,
            delete_sources,
            by_name_include_deleted,
            by_name,
            signing_key,
        } => {
            use std::collections::{BTreeMap, HashSet};

            let key = load_signing_key(&signing_key)?;

            if by_name_include_deleted {
                if out_name.is_some() {
                    eprintln!("warning: --out-name is ignored when --by-name-include-deleted is set");
                }

                let pile_path = pile;
                let pile_store: Pile = Pile::open(&pile_path)?;
                let mut repo = Repository::new(pile_store, key.clone(), TribleSet::new())?;

                let res = (|| -> Result<(), anyhow::Error> {
                    repo.storage_mut().refresh()?;
                    let reader = repo
                        .storage_mut()
                        .reader()
                        .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                    // --- Phase 1: Raw pile scan ---
                    let records = scan_pile_records(&pile_path)?;
                    let states = collapse_branch_states(&records);

                    let n_active = states.values().filter(|s| s.kind == RecordKind::Set).count();
                    let n_deleted = states.values().filter(|s| s.kind == RecordKind::Tombstone).count();
                    println!(
                        "scanning pile: found {} unique branch IDs ({} active, {} tombstoned)",
                        states.len(), n_active, n_deleted
                    );

                    // --- Phase 2: Name resolution & grouping ---
                    let mut groups: BTreeMap<String, Vec<(Id, Option<Inline<Handle<SimpleArchive>>>)>> = BTreeMap::new();

                    for (bid, state) in &states {
                        let meta_handle = match state.kind {
                            RecordKind::Set => state.meta,
                            RecordKind::Tombstone => state.last_set,
                        };

                        let Some(mh) = meta_handle else {
                            groups.entry("<unnamed>".to_string()).or_default().push((*bid, None));
                            continue;
                        };

                        if reader.metadata(mh)?.is_none() {
                            eprintln!("warning: metadata blob missing for branch {bid:X}");
                            groups.entry("<unnamed>".to_string()).or_default().push((*bid, None));
                            continue;
                        }

                        let meta_set = match reader.get::<TribleSet, SimpleArchive>(mh) {
                            Ok(ms) => ms,
                            Err(_) => {
                                eprintln!("warning: failed to read metadata for branch {bid:X}");
                                groups.entry("<unnamed>".to_string()).or_default().push((*bid, None));
                                continue;
                            }
                        };

                        let name = load_branch_name(&reader, &meta_set)
                            .ok()
                            .flatten()
                            .unwrap_or_else(|| "<unnamed>".to_string());

                        let head = extract_repo_head(&meta_set);
                        groups.entry(name).or_default().push((*bid, head));
                    }

                    // --- Phase 3: Subsumption + merge per name group ---
                    let statuses: HashMap<Id, &str> = states.iter().map(|(id, s)| {
                        let label = match s.kind {
                            RecordKind::Set => "active",
                            RecordKind::Tombstone => "deleted",
                        };
                        (*id, label)
                    }).collect();
                    let created_count = consolidate_groups(
                        &groups, &statuses, &reader, &mut repo, &key,
                        dry_run, delete_sources,
                    )?;

                    if dry_run {
                        println!("\ndry-run: no changes were made");
                    } else {
                        println!("\ncreated {created_count} consolidated branch(es)");
                    }

                    Ok(())
                })();

                let close_res = repo
                    .into_storage()
                    .close()
                    .map_err(|e| anyhow::anyhow!("{e:?}"));
                res.and(close_res)?;
            } else if by_name {
                if out_name.is_some() {
                    eprintln!("warning: --out-name is ignored when --by-name is set");
                }

                let pile_store: Pile = Pile::open(&pile)?;
                let mut repo = Repository::new(pile_store, key.clone(), TribleSet::new())?;

                let res = (|| -> Result<(), anyhow::Error> {
                    repo.storage_mut().refresh()?;
                    let reader = repo
                        .storage_mut()
                        .reader()
                        .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                    // Iterate active branches, resolve names, group.
                    let mut groups: std::collections::BTreeMap<String, Vec<(Id, Option<Inline<Handle<SimpleArchive>>>)>> = std::collections::BTreeMap::new();

                    let branch_ids: Vec<Id> = repo.storage_mut().branches()?
                        .collect::<Result<Vec<_>, _>>()?;

                    println!("found {} active branch(es)", branch_ids.len());

                    for bid in &branch_ids {
                        let Some(mh) = repo.storage_mut().head(*bid)? else {
                            continue;
                        };

                        if reader.metadata(mh)?.is_none() {
                            eprintln!("warning: metadata blob missing for branch {bid:X}");
                            groups.entry("<unnamed>".to_string()).or_default().push((*bid, None));
                            continue;
                        }

                        let meta_set = match reader.get::<TribleSet, SimpleArchive>(mh) {
                            Ok(ms) => ms,
                            Err(_) => {
                                eprintln!("warning: failed to read metadata for branch {bid:X}");
                                groups.entry("<unnamed>".to_string()).or_default().push((*bid, None));
                                continue;
                            }
                        };

                        let name = load_branch_name(&reader, &meta_set)
                            .ok()
                            .flatten()
                            .unwrap_or_else(|| "<unnamed>".to_string());

                        let head = extract_repo_head(&meta_set);
                        groups.entry(name).or_default().push((*bid, head));
                    }

                    let statuses: HashMap<Id, &str> = branch_ids.iter()
                        .map(|bid| (*bid, "active"))
                        .collect();
                    let created_count = consolidate_groups(
                        &groups, &statuses, &reader, &mut repo, &key,
                        dry_run, delete_sources,
                    )?;

                    if dry_run {
                        println!("\ndry-run: no changes were made");
                    } else {
                        println!("\ncreated {created_count} consolidated branch(es)");
                    }

                    Ok(())
                })();

                let close_res = repo
                    .into_storage()
                    .close()
                    .map_err(|e| anyhow::anyhow!("{e:?}"));
                res.and(close_res)?;
            } else {
                // Original explicit-branch-IDs path.
                // Parse branch ids before opening the pile so CLI errors don't leave files open.
                let mut seen: HashSet<Id> = HashSet::new();
                let mut branch_ids: Vec<Id> = Vec::new();
                for raw in branches {
                    let bid = parse_branch_id_hex(&raw)?;
                    if seen.insert(bid) {
                        branch_ids.push(bid);
                    }
                }

                let pile: Pile = Pile::open(&pile)?;
                let mut repo = Repository::new(pile, key.clone(), TribleSet::new())?;

                let res = (|| -> Result<(), anyhow::Error> {
                    // Ensure in-memory indices are populated.
                    repo.storage_mut().refresh()?;
                    let reader = repo
                        .storage_mut()
                        .reader()
                        .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                    // Attribute ids used in branch metadata.
                    let repo_head_attr = triblespace_core::repo::head.id();

                    // Collect all branch ids and their current heads.
                    let mut candidates: Vec<(Id, Option<Inline<Handle<SimpleArchive>>>)> =
                        Vec::new();
                    for bid in branch_ids {
                        let meta_handle = repo
                            .storage_mut()
                            .head(bid)?
                            .ok_or_else(|| anyhow::anyhow!("branch not found: {bid:X}"))?;

                        let mut head_val: Option<Inline<Handle<SimpleArchive>>> = None;
                        if reader.metadata(meta_handle)?.is_some() {
                            if let Ok(meta) = reader.get::<TribleSet, SimpleArchive>(meta_handle) {
                                for t in meta.iter() {
                                    if t.a() == &repo_head_attr {
                                        head_val = Some(*t.v::<Handle<SimpleArchive>>());
                                        break;
                                    }
                                }
                            }
                        }

                        candidates.push((bid, head_val));
                    }

                    println!("found {} branch(es)", candidates.len());
                    for (bid, head) in &candidates {
                        let id_hex = format!("{bid:X}");
                        if let Some(h) = head {
                            let hh: Inline<Hash<Blake3>> = Handle::to_hash(*h);
                            let hex: String = hh.from_inline();
                            println!("- {id_hex} -> commit {hex}");
                        } else {
                            println!("- {id_hex} -> <no head>");
                        }
                    }

                    if dry_run {
                        println!("dry-run: no changes will be made");
                        return Ok(());
                    }

                    if candidates.len() == 1 {
                        println!("only one branch present; nothing to consolidate");
                        return Ok(());
                    }

                    // Collect parent commit handles (skip branches without a head).
                    let parents: Vec<Inline<Handle<SimpleArchive>>> =
                        candidates.iter().filter_map(|(_, h)| *h).collect();
                    if parents.is_empty() {
                        anyhow::bail!("no branch heads available to attach");
                    }

                    // Create a single merge commit that has all branch heads as parents.
                    let commit_set = triblespace_core::repo::commit::commit_metadata(
                        &key,
                        parents.clone(),
                        None,
                        None,
                        None,
                    );
                    let commit_handle = repo
                        .storage_mut()
                        .put(commit_set.to_blob())
                        .map_err(|e| anyhow::anyhow!("failed to put commit blob: {e:?}"))?;

                    // Decide output branch name.
                    let out = out_name.unwrap_or_else(|| "consolidated".to_string());

                    let new_id = *repo
                        .create_branch_with_key(&out, Some(commit_handle), key.clone())
                        .map_err(|e| anyhow::anyhow!("failed to create consolidated branch: {e:?}"))?;
                    println!("created consolidated branch '{out}' with id {new_id:X}");

                    if delete_sources {
                        for (bid, _) in &candidates {
                            if let Some(old) = repo.storage_mut().head(*bid)? {
                                match repo.storage_mut().update(*bid, Some(old), None)? {
                                    triblespace_core::repo::PushResult::Success() => {
                                        println!("deleted source branch {bid:X}");
                                    }
                                    triblespace_core::repo::PushResult::Conflict(_) => {
                                        eprintln!("warning: branch {bid:X} advanced concurrently; skipping delete");
                                    }
                                }
                            }
                        }
                    }
                    Ok(())
                })();

                let close_res = repo
                    .into_storage()
                    .close()
                    .map_err(|e| anyhow::anyhow!("{e:?}"));
                res.and(close_res)?;
            }
        }
        Command::Log {
            pile,
            branch,
            limit,
            oneline,
        } => {
            use std::collections::HashSet;
            use triblespace_core::repo::pile::Pile;

            let branch_id = parse_branch_id_hex(&branch)?;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                pile.refresh()?;
                let reader = pile
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                // Resolve branch head commit.
                let branch_meta = pile
                    .head(branch_id)?
                    .ok_or_else(|| anyhow::anyhow!("branch not found"))?;
                let branch_meta_set: TribleSet = reader
                    .get(branch_meta)
                    .map_err(|e| anyhow::anyhow!("read branch metadata: {e:?}"))?;
                let commit_head = extract_repo_head(&branch_meta_set)
                    .ok_or_else(|| anyhow::anyhow!("branch has no commit head"))?;

                // BFS from commit head, newest first.
                let mut queue: std::collections::VecDeque<Inline<Handle<SimpleArchive>>> =
                    std::collections::VecDeque::new();
                let mut visited: HashSet<[u8; 32]> = HashSet::new();
                queue.push_back(commit_head);
                let mut printed = 0usize;

                while let Some(current) = queue.pop_front() {
                    if !visited.insert(current.raw) {
                        continue;
                    }
                    if printed >= limit {
                        break;
                    }

                    let commit_set: TribleSet = match reader.get(current) {
                        Ok(c) => c,
                        Err(_) => {
                            let hash: Inline<Hash<Blake3>> = Handle::to_hash(current);
                            let hex: String = hash.from_inline();
                            println!("{hex}  <missing blob>");
                            printed += 1;
                            continue;
                        }
                    };

                    let info = read_commit_fields(&commit_set);
                    let hash: Inline<Hash<Blake3>> = Handle::to_hash(current);
                    let hex: String = hash.from_inline();

                    let msg = if let Some(sm) = &info.short_message {
                        sm.clone()
                    } else if let Some(mh) = info.message {
                        match reader.get::<View<str>, _>(mh) {
                            Ok(v) => {
                                let s = v.as_ref();
                                if s.len() > 72 {
                                    format!("{}...", &s[..72])
                                } else {
                                    s.to_string()
                                }
                            }
                            Err(_) => "<message blob missing>".to_string(),
                        }
                    } else {
                        "<no message>".to_string()
                    };

                    let content_count = if let Some(ch) = info.content {
                        match reader.get::<TribleSet, _>(ch) {
                            Ok(ts) => format!("{}", ts.len()),
                            Err(_) => "?".to_string(),
                        }
                    } else {
                        "0".to_string()
                    };

                    let ts_str = if let Some(ts_val) = info.timestamp {
                        use triblespace_core::inline::encodings::time::Lower;
                        let lower: Lower = ts_val.try_from_inline().unwrap_or(Lower(0));
                        let epoch = hifitime::Epoch::from_tai_duration(
                            hifitime::Duration::from_total_nanoseconds(lower.0));
                        hifitime::efmt::Formatter::new(
                            epoch,
                            hifitime::efmt::consts::ISO8601,
                        ).to_string()
                    } else {
                        "?".to_string()
                    };

                    if oneline {
                        println!(
                            "\x1b[33m{short}\x1b[0m  {ts_str}  {msg}",
                            short = &hex[..16],
                        );
                    } else {
                        println!("\x1b[33mcommit {hex}\x1b[0m");
                        if let Some(pk) = &info.signed_by {
                            println!("Signed: {}", hex::encode(&pk[..8]));
                        }
                        println!("Date:   {ts_str}");
                        if !info.parents.is_empty() {
                            let parent_strs: Vec<String> = info.parents.iter().map(|p| {
                                let ph: Inline<Hash<Blake3>> = Handle::to_hash(*p);
                                let phex: String = ph.from_inline();
                                phex[..16].to_string()
                            }).collect();
                            let label = if info.parents.len() > 1 { "Merge: " } else { "Parent:" };
                            println!("{label} {}", parent_strs.join(" "));
                        }
                        println!();
                        println!("    {msg}");
                        println!();
                        println!("    {content_count} tribles");
                        println!();
                    }
                    printed += 1;

                    for p in &info.parents {
                        queue.push_back(*p);
                    }
                }
                Ok(())
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Show { pile, commit } => {
            use triblespace_core::repo::pile::Pile;

            let commit_handle: Inline<Handle<SimpleArchive>> =
                parse_blake3_handle(&commit)?;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                pile.refresh()?;
                let reader = pile
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                let commit_set: TribleSet = reader
                    .get(commit_handle)
                    .map_err(|e| anyhow::anyhow!("read commit blob: {e:?}"))?;

                let info = read_commit_fields(&commit_set);
                let hash: Inline<Hash<Blake3>> = Handle::to_hash(commit_handle);
                let hex: String = hash.from_inline();
                println!("Commit: {hex}");

                // Message
                if let Some(sm) = &info.short_message {
                    println!("Short message: {sm}");
                }
                if let Some(mh) = info.message {
                    match reader.get::<View<str>, _>(mh) {
                        Ok(v) => println!("Message: {}", v.as_ref()),
                        Err(_) => println!("Message: <blob missing>"),
                    }
                }

                // Signer
                if let Some(pk) = &info.signed_by {
                    println!("Signed by: {}", hex::encode(pk));
                }

                // Parents
                if info.parents.is_empty() {
                    println!("Parents: (none)");
                } else {
                    println!("Parents:");
                    for p in &info.parents {
                        let ph: Inline<Hash<Blake3>> = Handle::to_hash(*p);
                        let phex: String = ph.from_inline();
                        let present = reader.metadata(*p)?.is_some();
                        println!(
                            "  {phex} [{}]",
                            if present { "present" } else { "missing" }
                        );
                    }
                }

                // Content
                if let Some(ch) = info.content {
                    let ch_hash: Inline<Hash<Blake3>> = Handle::to_hash(ch);
                    let ch_hex: String = ch_hash.from_inline();
                    let present = reader.metadata(ch)?.is_some();
                    print!("Content: {ch_hex} [{}]", if present { "present" } else { "missing" });
                    if present {
                        if let Ok(ts) = reader.get::<TribleSet, _>(ch) {
                            use std::collections::HashSet;
                            let mut entities: HashSet<Id> = HashSet::new();
                            let mut attributes: HashSet<Id> = HashSet::new();
                            for t in ts.iter() {
                                entities.insert(*t.e());
                                attributes.insert(*t.a());
                            }
                            print!(
                                " ({} tribles, {} entities, {} attributes)",
                                ts.len(),
                                entities.len(),
                                attributes.len()
                            );
                        }
                    }
                    println!();
                } else {
                    println!("Content: (none)");
                }

                // Metadata
                if let Some(mh) = info.metadata {
                    let mh_hash: Inline<Hash<Blake3>> = Handle::to_hash(mh);
                    let mh_hex: String = mh_hash.from_inline();
                    let present = reader.metadata(mh)?.is_some();
                    println!(
                        "Metadata: {mh_hex} [{}]",
                        if present { "present" } else { "missing" }
                    );
                } else {
                    println!("Metadata: (none)");
                }

                // Total tribles in commit TribleSet
                println!("Commit tribles: {}", commit_set.len());

                Ok(())
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Describe {
            pile,
            branch,
            entities,
        } => {
            use std::collections::HashSet;
            use triblespace_core::repo::pile::Pile;

            let branch_id = parse_branch_id_hex(&branch)?;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                pile.refresh()?;
                let reader = pile
                    .reader()
                    .map_err(|e| anyhow::anyhow!("pile reader error: {e:?}"))?;

                // Resolve branch head commit.
                let branch_meta = pile
                    .head(branch_id)?
                    .ok_or_else(|| anyhow::anyhow!("branch not found"))?;
                let branch_meta_set: TribleSet = reader
                    .get(branch_meta)
                    .map_err(|e| anyhow::anyhow!("read branch metadata: {e:?}"))?;
                let commit_head = extract_repo_head(&branch_meta_set)
                    .ok_or_else(|| anyhow::anyhow!("branch has no commit head"))?;

                // Walk full commit DAG, collect attribute tallies.
                struct AttrTally {
                    trible_count: usize,
                    entity_ids: HashSet<Id>,
                }

                let mut tallies: HashMap<Id, AttrTally> = HashMap::new();
                let mut attr_names: HashMap<Id, String> = HashMap::new();
                let mut visited: HashSet<[u8; 32]> = HashSet::new();
                let mut stack: Vec<Inline<Handle<SimpleArchive>>> = vec![commit_head];
                let mut commit_count = 0usize;

                let tag_attr = triblespace_core::metadata::tag.id();
                let attr_attr = triblespace_core::metadata::attribute.id();
                let name_attr = triblespace_core::metadata::name.id();

                while let Some(current) = stack.pop() {
                    if !visited.insert(current.raw) {
                        continue;
                    }

                    let commit_set: TribleSet = match reader.get(current) {
                        Ok(c) => c,
                        Err(_) => continue,
                    };
                    commit_count += 1;

                    let info = read_commit_fields(&commit_set);

                    // Tally content attributes.
                    if let Some(ch) = info.content {
                        if let Ok(content) = reader.get::<TribleSet, _>(ch) {
                            for t in content.iter() {
                                let entry = tallies.entry(*t.a()).or_insert_with(|| AttrTally {
                                    trible_count: 0,
                                    entity_ids: HashSet::new(),
                                });
                                entry.trible_count += 1;
                                entry.entity_ids.insert(*t.e());
                            }
                        }
                    }

                    // Resolve attribute names from metadata.
                    if let Some(mh) = info.metadata {
                        if let Ok(meta_set) = reader.get::<TribleSet, _>(mh) {
                            // Find entities tagged with KIND_ATTRIBUTE_USAGE.
                            let kind_id = triblespace_core::metadata::KIND_ATTRIBUTE_USAGE;
                            let mut usage_entities: HashSet<Id> = HashSet::new();
                            for t in meta_set.iter() {
                                if t.a() == &tag_attr {
                                    let v: Inline<triblespace::prelude::inlineencodings::GenId> =
                                        *t.v();
                                    if let Ok(gid) =
                                        v.try_from_inline::<triblespace_core::id::Id>()
                                    {
                                        if gid == kind_id {
                                            usage_entities.insert(*t.e());
                                        }
                                    }
                                }
                            }

                            // For each usage entity, read attribute + name.
                            for t in meta_set.iter() {
                                if !usage_entities.contains(t.e()) {
                                    continue;
                                }
                                if t.a() == &attr_attr {
                                    let v: Inline<triblespace::prelude::inlineencodings::GenId> =
                                        *t.v();
                                    if let Ok(described_id) =
                                        v.try_from_inline::<triblespace_core::id::Id>()
                                    {
                                        // Now find the name for this entity.
                                        for t2 in meta_set.iter() {
                                            if t2.e() == t.e() && t2.a() == &name_attr {
                                                let nh: Inline<
                                                    Handle<LongString>,
                                                > = *t2.v();
                                                if let Ok(view) =
                                                    reader.get::<View<str>, _>(nh)
                                                {
                                                    attr_names.entry(described_id).or_insert_with(
                                                        || view.as_ref().to_string(),
                                                    );
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    for p in &info.parents {
                        stack.push(*p);
                    }
                }

                println!("Commits: {commit_count}");
                println!("Attributes: {}", tallies.len());
                println!();

                // Sort by trible count descending.
                let mut sorted: Vec<(Id, &AttrTally)> =
                    tallies.iter().map(|(id, t)| (*id, t)).collect();
                sorted.sort_by(|a, b| b.1.trible_count.cmp(&a.1.trible_count));

                for (attr_id, tally) in &sorted {
                    let name = attr_names
                        .get(attr_id)
                        .map(|s| s.as_str())
                        .unwrap_or("-");
                    if entities {
                        println!(
                            "{attr_id:X}  tribles={tc}  entities={ec}  {name}",
                            tc = tally.trible_count,
                            ec = tally.entity_ids.len(),
                        );
                    } else {
                        println!(
                            "{attr_id:X}  tribles={tc}  {name}",
                            tc = tally.trible_count,
                        );
                    }
                }

                Ok(())
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
        Command::Rename {
            pile,
            branch,
            new_name,
            signing_key,
        } => {
            use triblespace_core::repo::pile::Pile;
            use triblespace_core::repo::branch as branch_mod;
            use triblespace_core::query::find;
            use triblespace_core::macros::pattern;

            let branch_id = parse_branch_id_hex(&branch)?;
            let key = load_signing_key(&signing_key)?;

            let mut pile: Pile = Pile::open(&pile)?;
            let res = (|| -> Result<(), anyhow::Error> {
                pile.refresh()?;

                let mut current_meta_handle = pile
                    .head(branch_id)?
                    .ok_or_else(|| anyhow::anyhow!("branch {branch} not found"))?;

                loop {
                    // Load current branch metadata.
                    let reader = pile.reader()
                        .map_err(|e| anyhow::anyhow!("reader: {e:?}"))?;
                    let meta: TribleSet = reader.get(current_meta_handle)
                        .map_err(|e| anyhow::anyhow!("read branch meta: {e:?}"))?;

                    // Extract current commit head from metadata.
                    let head_handle: Option<Inline<Handle<SimpleArchive>>> =
                        find!((h: Inline<_>), pattern!(&meta, [{ triblespace_core::repo::head: ?h }]))
                            .next()
                            .map(|(h,)| h);

                    // Build the commit head blob for re-signing (branch_metadata needs it).
                    let commit_blob = if let Some(h) = head_handle {
                        let commit_set: TribleSet = reader.get(h)
                            .map_err(|e| anyhow::anyhow!("read commit: {e:?}"))?;
                        Some(commit_set.to_blob())
                    } else {
                        None
                    };

                    // Store the new name as a LongString blob.
                    let name_handle: BranchNameHandle = pile
                        .put(new_name.clone().to_blob())
                        .map_err(|e| anyhow::anyhow!("put name blob: {e:?}"))?;

                    // Build new branch metadata with the new name. Rename
                    // doesn't touch rollup state; if the existing metadata
                    // had one, we'd need to carry it forward — for now
                    // rename drops any existing rollup (readers fall back
                    // to checkout).
                    let new_meta = branch_mod::branch_metadata(
                        &key,
                        branch_id,
                        name_handle,
                        commit_blob,
                        None,
                    );

                    let new_meta_handle = pile
                        .put(new_meta)
                        .map_err(|e| anyhow::anyhow!("put branch meta: {e:?}"))?;

                    // CAS: swap old metadata for new.
                    match pile.update(branch_id, Some(current_meta_handle), Some(new_meta_handle))? {
                        triblespace_core::repo::PushResult::Success() => {
                            println!("renamed {branch_id:X}\"{new_name}\"");
                            return Ok(());
                        }
                        triblespace_core::repo::PushResult::Conflict(conflict) => {
                            let conflict = conflict
                                .ok_or_else(|| anyhow::anyhow!("branch deleted concurrently"))?;
                            eprintln!("CAS conflict, retrying...");
                            current_meta_handle = conflict;
                            // loop back and retry with the new handle
                        }
                    }
                }
            })();
            let close_res = pile.close().map_err(|e| anyhow::anyhow!("{e:?}"));
            res.and(close_res)?;
        }
    }
    Ok(())
}

// ───────────── Shared helpers ─────────────

/// Kind of raw branch record in a pile file.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum RecordKind {
    Set,
    Tombstone,
}

/// A single branch record read from the raw pile file.
#[derive(Clone, Debug)]
struct RawBranchRecord {
    offset: u64,
    branch_id: Id,
    kind: RecordKind,
    /// Branch metadata handle (only when kind == Set).
    meta_handle: Option<Inline<Handle<SimpleArchive>>>,
}

/// Collapsed final state per branch from a raw pile scan.
#[derive(Clone, Debug)]
struct BranchState {
    kind: RecordKind,
    /// Current metadata handle (only when kind == Set).
    meta: Option<Inline<Handle<SimpleArchive>>>,
    /// Most recent Set metadata handle (kept even after tombstone).
    last_set: Option<Inline<Handle<SimpleArchive>>>,
}

/// Scan the raw pile file for all branch update/tombstone records.
fn scan_pile_records(path: &std::path::Path) -> Result<Vec<RawBranchRecord>> {
    let mut file = std::fs::File::open(path)?;
    let file_len = file.metadata()?.len();
    let mut records = Vec::new();
    let mut offset: u64 = 0;
    let mut buf = [0u8; RECORD_LEN as usize];

    while offset + RECORD_LEN <= file_len {
        file.seek(SeekFrom::Start(offset))?;
        if file.read_exact(&mut buf).is_err() {
            break;
        }

        let magic: [u8; 16] = buf[0..16].try_into().unwrap();
        if magic == MAGIC_MARKER_BLOB.raw() {
            let len = u64::from_ne_bytes(buf[24..32].try_into().unwrap());
            let pad = blob_padding(len);
            offset = offset
                .checked_add(RECORD_LEN)
                .and_then(|o| o.checked_add(len))
                .and_then(|o| o.checked_add(pad))
                .ok_or_else(|| anyhow::anyhow!("pile too large"))?;
            continue;
        }

        if magic == MAGIC_MARKER_BRANCH.raw() {
            let raw_id: [u8; 16] = buf[16..32].try_into().unwrap();
            let Some(id) = Id::new(raw_id) else { break };
            let raw_handle: [u8; 32] = buf[32..64].try_into().unwrap();
            let meta: Inline<Handle<SimpleArchive>> = Inline::new(raw_handle);
            records.push(RawBranchRecord {
                offset,
                branch_id: id,
                kind: RecordKind::Set,
                meta_handle: Some(meta),
            });
            offset += RECORD_LEN;
            continue;
        }

        if magic == MAGIC_MARKER_BRANCH_TOMBSTONE.raw() {
            let raw_id: [u8; 16] = buf[16..32].try_into().unwrap();
            let Some(id) = Id::new(raw_id) else { break };
            records.push(RawBranchRecord {
                offset,
                branch_id: id,
                kind: RecordKind::Tombstone,
                meta_handle: None,
            });
            offset += RECORD_LEN;
            continue;
        }

        break;
    }

    Ok(records)
}

/// Collapse raw records into final state per branch.
fn collapse_branch_states(records: &[RawBranchRecord]) -> HashMap<Id, BranchState> {
    let mut states: HashMap<Id, BranchState> = HashMap::new();
    for rec in records {
        let entry = states.entry(rec.branch_id).or_insert(BranchState {
            kind: rec.kind,
            meta: rec.meta_handle,
            last_set: if rec.kind == RecordKind::Set {
                rec.meta_handle
            } else {
                None
            },
        });
        entry.kind = rec.kind;
        match rec.kind {
            RecordKind::Set => {
                entry.meta = rec.meta_handle;
                entry.last_set = rec.meta_handle;
            }
            RecordKind::Tombstone => {
                entry.meta = None;
            }
        }
    }
    states
}

/// Parsed commit fields from a commit TribleSet.
#[derive(Clone, Debug)]
struct CommitInfo {
    parents: Vec<Inline<Handle<SimpleArchive>>>,
    content: Option<Inline<Handle<SimpleArchive>>>,
    metadata: Option<Inline<Handle<SimpleArchive>>>,
    message: Option<Inline<Handle<LongString>>>,
    short_message: Option<String>,
    timestamp: Option<Inline<triblespace_core::inline::encodings::time::NsTAIInterval>>,
    signed_by: Option<[u8; 32]>,
}

/// Parse a commit TribleSet into structured fields.
fn read_commit_fields(commit: &TribleSet) -> CommitInfo {
    use triblespace_core::repo;
    use triblespace_core::inline::encodings::ed25519 as ed;
    use triblespace_core::inline::encodings::shortstring::ShortString;
    use triblespace_core::inline::encodings::time::NsTAIInterval;

    let content_attr = repo::content.id();
    let metadata_attr = repo::metadata.id();
    let parent_attr = repo::parent.id();
    let message_attr = repo::message.id();
    let short_message_attr = repo::short_message.id();
    let created_at_attr = triblespace_core::metadata::created_at.id();
    let signed_by_attr = repo::signed_by.id();

    let mut info = CommitInfo {
        parents: Vec::new(),
        content: None,
        metadata: None,
        message: None,
        short_message: None,
        timestamp: None,
        signed_by: None,
    };

    for t in commit.iter() {
        let a = *t.a();
        if a == parent_attr {
            info.parents
                .push(*t.v::<Handle<SimpleArchive>>());
        } else if a == content_attr {
            info.content = Some(*t.v::<Handle<SimpleArchive>>());
        } else if a == metadata_attr {
            info.metadata = Some(*t.v::<Handle<SimpleArchive>>());
        } else if a == message_attr {
            info.message = Some(*t.v::<Handle<LongString>>());
        } else if a == short_message_attr {
            let v: Inline<ShortString> = *t.v();
            info.short_message = v.try_from_inline().ok();
        } else if a == created_at_attr {
            info.timestamp = Some(*t.v::<NsTAIInterval>());
        } else if a == signed_by_attr {
            let v: Inline<ed::ED25519PublicKey> = *t.v();
            info.signed_by = Some(v.raw);
        }
    }

    info
}

fn blob_padding(len: u64) -> u64 {
    // The pile stores blobs padded so the next record begins on a 64-byte boundary.
    let rem = len % RECORD_LEN;
    if rem == 0 {
        0
    } else {
        RECORD_LEN - rem
    }
}

fn extract_repo_head(meta: &TribleSet) -> Option<Inline<Handle<SimpleArchive>>> {
    use triblespace::prelude::blobencodings::SimpleArchive;
    use triblespace::prelude::inlineencodings::Handle;
    use triblespace_core::repo;
    
    use triblespace_core::inline::Inline;

    let head_attr = repo::head.id();
    let mut head_handle: Option<Inline<Handle<SimpleArchive>>> = None;
    for t in meta.iter() {
        if t.a() == &head_attr {
            let h: Inline<Handle<SimpleArchive>> = *t.v();
            if head_handle.replace(h).is_some() {
                // Multiple heads -> ambiguous.
                return None;
            }
        }
    }
    head_handle
}

fn parse_branch_id_hex(s: &str) -> Result<Id> {
    let raw = hex::decode(s).map_err(|e| anyhow::anyhow!("branch id hex decode failed: {e}"))?;
    let raw: [u8; 16] = raw
        .as_slice()
        .try_into()
        .map_err(|_| anyhow::anyhow!("branch id must be 16 bytes (32 hex chars)"))?;
    Id::new(raw).ok_or_else(|| anyhow::anyhow!("branch id cannot be nil"))
}

fn parse_blake3_handle(s: &str) -> Result<Inline<Handle<SimpleArchive>>> {
    let s = s.trim();
    let hex = match s.split_once(':') {
        Some((proto, rest)) => {
            if proto.eq_ignore_ascii_case("blake3") {
                rest
            } else {
                return Err(anyhow::anyhow!("unsupported handle protocol: {proto}"));
            }
        }
        None => s,
    };

    let raw = hex::decode(hex).map_err(|e| anyhow::anyhow!("handle hex decode failed: {e}"))?;
    let raw: [u8; 32] = raw
        .as_slice()
        .try_into()
        .map_err(|_| anyhow::anyhow!("handle must be 32 bytes (64 hex chars)"))?;
    Ok(Inline::new(raw))
}

fn parse_blake3_handle_opt(s: &str) -> Result<Option<Inline<Handle<SimpleArchive>>>> {
    let s = s.trim();
    if s == "-" || s.eq_ignore_ascii_case("none") {
        return Ok(None);
    }
    Ok(Some(parse_blake3_handle(s)?))
}

/// Check whether `ancestor` is reachable from `descendant` by walking the
/// commit parent chain.
/// Consolidate named groups: compute subsumption, merge non-subsumed heads,
/// create new branches. Returns the number of branches created.
///
/// `statuses` maps branch IDs to display labels (e.g. "active"/"deleted").
fn consolidate_groups(
    groups: &std::collections::BTreeMap<String, Vec<(Id, Option<Inline<Handle<SimpleArchive>>>)>>,
    statuses: &HashMap<Id, &str>,
    reader: &triblespace_core::repo::pile::PileReader,
    repo: &mut Repository<Pile>,
    key: &ed25519_dalek::SigningKey,
    dry_run: bool,
    delete_sources: bool,
) -> Result<usize> {
    use std::collections::HashSet;

    let parent_attr = triblespace_core::repo::parent.id();
    let mut created_count: usize = 0;

    for (name, members) in groups {
        let heads: Vec<Inline<Handle<SimpleArchive>>> =
            members.iter().filter_map(|(_, h)| *h).collect();

        if heads.is_empty() {
            if !dry_run && delete_sources {
                let cleaned = tombstone_branches(repo, members, None)?;
                if cleaned > 0 {
                    println!("\nname group \"{name}\" ({} branches): all empty, cleaned up {cleaned} branch(es)", members.len());
                } else {
                    println!("\nname group \"{name}\" ({} branches): all empty, skipping", members.len());
                }
            } else {
                println!("\nname group \"{name}\" ({} branches): all empty, skipping", members.len());
            }
            continue;
        }

        println!("\nname group \"{name}\" ({} branches, {} with heads):", members.len(), heads.len());
        for (bid, head) in members {
            let status = statuses.get(bid).copied().unwrap_or("?");
            if let Some(h) = head {
                let hh: Inline<Hash<Blake3>> = Handle::to_hash(*h);
                let hex: String = hh.from_inline();
                println!("  - {bid:X} [{status}] head={}", &hex[..23]);
            } else {
                println!("  - {bid:X} [{status}] <no head>");
            }
        }

        // Deduplicate heads (same commit on multiple branch IDs).
        let unique_heads: Vec<Inline<Handle<SimpleArchive>>> = {
            let mut seen: HashSet<[u8; 32]> = HashSet::new();
            heads.iter().copied().filter(|h| seen.insert(h.raw)).collect()
        };

        // Compute subsumption: a head is subsumed if another head
        // has it as an ancestor.
        let mut subsumed: HashSet<[u8; 32]> = HashSet::new();
        if unique_heads.len() > 1 {
            for i in 0..unique_heads.len() {
                if subsumed.contains(&unique_heads[i].raw) { continue; }
                for j in 0..unique_heads.len() {
                    if i == j { continue; }
                    if subsumed.contains(&unique_heads[j].raw) { continue; }
                    match is_ancestor_of(unique_heads[i], unique_heads[j], reader, &parent_attr) {
                        Ok(true) => {
                            subsumed.insert(unique_heads[i].raw);
                            let hh: Inline<Hash<Blake3>> = Handle::to_hash(unique_heads[i]);
                            let hex: String = hh.from_inline();
                            println!("  ({}... subsumed)", &hex[..23]);
                            break;
                        }
                        Ok(false) => {}
                        Err(e) => {
                            eprintln!("  warning: ancestry check failed: {e:#}");
                        }
                    }
                }
            }
        }

        let non_subsumed: Vec<Inline<Handle<SimpleArchive>>> = unique_heads
            .iter()
            .copied()
            .filter(|h| !subsumed.contains(&h.raw))
            .collect();

        if non_subsumed.is_empty() {
            println!("  -> all heads subsumed, skipping");
            continue;
        }

        // Check if a single active branch already has the right head — skip if so.
        if non_subsumed.len() == 1 {
            let dominated_head = non_subsumed[0];
            let already_active = members.iter().any(|(bid, head)| {
                head.as_ref() == Some(&dominated_head)
                    && statuses.get(bid).copied() == Some("active")
            });
            if already_active {
                if dry_run {
                    println!("  -> already consolidated (active branch has the sole non-subsumed head)");
                } else if delete_sources {
                    let keeper = members.iter().find(|(bid, head)| {
                        head.as_ref() == Some(&dominated_head)
                            && statuses.get(bid).copied() == Some("active")
                    }).map(|(b, _)| *b);
                    let cleaned = tombstone_branches(repo, members, keeper)?;
                    if cleaned > 0 {
                        println!("  -> already consolidated, cleaned up {cleaned} redundant branch(es)");
                    } else {
                        println!("  -> already consolidated, skipping");
                    }
                } else {
                    println!("  -> already consolidated, skipping");
                }
                continue;
            }
        }

        if dry_run {
            println!("  -> would merge {} non-subsumed head(s) into \"{name}\"", non_subsumed.len());
            continue;
        }

        let commit_handle = if non_subsumed.len() == 1 {
            println!("  -> single non-subsumed head, creating branch directly");
            non_subsumed[0]
        } else {
            println!("  -> merging {} non-subsumed heads", non_subsumed.len());
            let commit_set = triblespace_core::repo::commit::commit_metadata(
                key,
                non_subsumed.clone(),
                None,
                None,
                None,
            );
            repo.storage_mut()
                .put(commit_set.to_blob())
                .map_err(|e| anyhow::anyhow!("failed to put commit blob: {e:?}"))?
        };

        let new_id = *repo
            .create_branch_with_key(name, Some(commit_handle), key.clone())
            .map_err(|e| anyhow::anyhow!("failed to create branch '{name}': {e:?}"))?;
        println!("  created branch '{name}' with id {new_id:X}");
        created_count += 1;

        if delete_sources {
            let cleaned = tombstone_branches(repo, members, Some(new_id))?;
            println!("  deleted {cleaned} source branch(es)");
        }
    }

    Ok(created_count)
}

/// Tombstone all branches in `members` except `keeper`. Returns the number tombstoned.
fn tombstone_branches(
    repo: &mut Repository<Pile>,
    members: &[(Id, Option<Inline<Handle<SimpleArchive>>>)],
    keeper: Option<Id>,
) -> Result<usize> {
    let mut count = 0;
    for (bid, _) in members {
        if Some(*bid) == keeper { continue; }
        let old = repo.storage_mut().head(*bid)?;
        match repo.storage_mut().update(*bid, old, None)? {
            triblespace_core::repo::PushResult::Success() => { count += 1; }
            triblespace_core::repo::PushResult::Conflict(_) => {
                eprintln!("  warning: branch {bid:X} advanced concurrently; skipping delete");
            }
        }
    }
    Ok(count)
}

fn is_ancestor_of(
    ancestor: Inline<Handle<SimpleArchive>>,
    descendant: Inline<Handle<SimpleArchive>>,
    reader: &impl BlobStoreGet,
    parent_attr: &Id,
) -> Result<bool> {
    use std::collections::HashSet;

    let mut visited: HashSet<[u8; 32]> = HashSet::new();
    let mut stack: Vec<Inline<Handle<SimpleArchive>>> = vec![descendant];

    while let Some(current) = stack.pop() {
        if current.raw == ancestor.raw {
            return Ok(true);
        }
        if !visited.insert(current.raw) {
            continue;
        }
        let commit: TribleSet = match reader.get(current) {
            Ok(c) => c,
            Err(_) => continue, // Missing blob — stop traversal on this branch.
        };
        for t in commit.iter() {
            if t.a() == parent_attr {
                stack.push(*t.v::<Handle<SimpleArchive>>());
            }
        }
    }
    Ok(false)
}

fn load_branch_name(
    reader: &impl BlobStoreGet,
    meta: &TribleSet,
) -> Result<Option<String>> {
    let name_attr = triblespace_core::metadata::name.id();
    let mut handle_opt: Option<BranchNameHandle> = None;
    for t in meta.iter() {
        if t.a() == &name_attr {
            let h: BranchNameHandle = *t.v();
            if handle_opt.replace(h).is_some() {
                return Ok(None);
            }
        }
    }

    let Some(handle) = handle_opt else {
        return Ok(None);
    };

    let view: View<str> = reader
        .get(handle)
        .map_err(|err| anyhow::anyhow!("read branch name blob: {err:?}"))?;
    Ok(Some(view.as_ref().to_string()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn parse_signing_key_hex_and_file() {
        // File containing hex
        let mut seed = [0u8; 32];
        for i in 0..32 {
            seed[i] = i as u8;
        }
        let hex = hex::encode(seed);
        let mut f = NamedTempFile::new().expect("tmpfile");
        writeln!(f, "{}", hex).expect("write");
        let path = f.path().to_path_buf();
        let key = load_signing_key(&Some(path)).expect("parse file");
        let expected = ed25519_dalek::SigningKey::from_bytes(&seed);
        assert_eq!(key.to_bytes(), expected.to_bytes());
    }
}