tsift-cli 0.1.63

CLI dispatch layer for tsift — clap types, command handlers, and output formatting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
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
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fs;
use std::io::Read as _;
use std::path::{Path, PathBuf};
use std::time::Instant;

use anyhow::{Context, Result, bail};
use serde::Serialize;
use substrate::{
    ConvexGraphStore as SubstrateConvexGraphStore, ConvexProjectionRows, ConvexRowsGraphClient,
    GraphStore, SqliteGraphStore,
};
use tsift_index::init;
use tsift_quality::lint;
use tsift_sqlite as substrate;
use tsift_status::status;

use crate::cli::{GraphDbBackend, GraphDbQuery};
use crate::output::{OutputFormat, ToolEnvelopeSummary};
use crate::{
    ConvexHttpTransport, ConvexSyncOptions, EditBatch, EditResult, EditStatus,
    GRAPH_DB_BACKEND_EVAL_DIRECT_PATH_HOPS, GRAPH_DB_BACKEND_EVAL_EXTENDED_PATH_HOPS,
    GRAPH_DB_BACKEND_EVAL_NORMALIZATION_ROW_UNIT, GRAPH_DB_BACKEND_EVAL_PATH_MAX_HOPS,
    GRAPH_PROJECTION_VERSION, GraphDbBackendEvalConfig, GraphDbBackendEvalOptions,
    GraphDbBackendEvalPhaseTiming, GraphDbBackendEvalReport, GraphDbCompactionReport,
    GraphDbDoctorReport, GraphDbDriftInput, GraphDbEvidenceInput, GraphDbExperimentalBackend,
    GraphDbRefreshSummary, append_convex_snapshot_doctor_checks,
    append_graph_db_backend_eval_normalized_duration_metric,
    append_graph_db_backend_eval_phase_metrics, append_sqlite_graph_doctor_checks,
    append_tokensave_graph_doctor_checks, apply_edit_plan_atomically, apply_rewrite_output_format,
    apply_status_fixes, autoindex_missing_workspace_scopes, build_convex_sync_report_with_snapshot,
    build_edit_plan, classify_task, convex_graph_freshness, convex_rows_from_graph_store,
    dedupe_preserve_order, envelope_metric, execute_query, execute_rewritten_command,
    graph_db_backend_eval_cached_refresh, graph_db_backend_eval_dataset,
    graph_db_backend_eval_full_projection_with_profile, graph_db_backend_eval_graph_rows,
    graph_db_backend_eval_metric_digest_command, graph_db_backend_eval_metrics,
    graph_db_backend_eval_performance_gate, graph_db_backend_eval_phase_timing,
    graph_db_backend_eval_promotion, graph_db_backend_eval_refresh_operation,
    graph_db_backend_eval_refresh_total_micros, graph_db_backend_eval_refresh_with_profile,
    graph_db_backend_eval_reused_cached_projection, graph_db_backend_eval_synthetic_projection,
    graph_db_backend_eval_targets, graph_db_backend_eval_timed_phase,
    graph_db_backend_eval_update_source_watermark, graph_db_compaction_policy,
    graph_db_drift_report, graph_db_evidence_report_from_store, graph_db_operator_report_from_disk,
    graph_db_operator_status_warnings, graph_db_read_recovery_diagnostic,
    graph_db_report_from_store, graph_db_resolve_evidence_target, graph_db_scope_arg,
    graph_projection_content_hash, graph_substrate_db_path, load_convex_projection_rows,
    load_convex_projection_snapshot_value, no_rewrite_message, open_db,
    prepare_conflict_matrix_inputs, print_convex_sync_human, print_graph_db_backend_eval_human,
    print_graph_db_compaction_human, print_graph_db_doctor_human, print_graph_db_drift_human,
    print_graph_db_evidence_report, print_graph_db_human, print_graph_db_operator_report,
    print_json_or_envelope, rewrite_command, schema_overview, shell_quote,
    sqlite_convex_rows_from_conn, sqlite_graph_freshness, status_missing_workspace_scopes,
    table_columns, to_json_schema, tokensave_graph_freshness, traversal_source_watermark,
    truncate_for_compact, validate_convex_projection_rows, write_traversal_graph_store,
    write_traversal_graph_store_with_options,
};
use tsift_tokensave::TokensaveDb;

pub(crate) fn cmd_route(task: &str, id_only: bool) -> Result<()> {
    let (tier, model_id) = classify_task(task);
    if id_only {
        println!("{}", model_id);
    } else {
        println!("tier:  {}", tier);
        println!("model: {}", model_id);
        println!("task:  {}", task);
    }
    Ok(())
}

pub(crate) fn cmd_edit(
    dry_run: bool,
    file: Option<PathBuf>,
    compact: bool,
    pretty: bool,
    terse: bool,
    schema: bool,
) -> Result<()> {
    let input = match file {
        Some(path) => fs::read_to_string(&path)
            .with_context(|| format!("reading edit file: {}", path.display()))?,
        None => {
            let mut buf = String::new();
            std::io::stdin()
                .read_to_string(&mut buf)
                .context("reading edits from stdin")?;
            buf
        }
    };
    let batch: EditBatch = serde_json::from_str(&input).context("parsing edit JSON")?;

    if batch.edits.is_empty() {
        println!("No edits provided.");
        return Ok(());
    }

    let plan = build_edit_plan(&batch)?;
    let results: Vec<EditResult> = if dry_run {
        plan.iter()
            .map(|entry| EditResult {
                file: entry.file.clone(),
                status: EditStatus::Skipped,
                error: Some("dry run".into()),
                replacements: Some(entry.replacements),
            })
            .collect()
    } else {
        apply_edit_plan_atomically(plan)?
    };

    // Summary output
    let ok_count = results
        .iter()
        .filter(|r| matches!(r.status, EditStatus::Ok))
        .count();
    let skip_count = results
        .iter()
        .filter(|r| matches!(r.status, EditStatus::Skipped))
        .count();
    let err_count = 0usize;

    if compact {
        println!(
            "applied:{} skipped:{} errors:{}",
            ok_count, skip_count, err_count
        );
    } else {
        println!(
            "{}",
            to_json_schema(
                &serde_json::json!({
                    "applied": ok_count,
                    "skipped": skip_count,
                    "errors": err_count,
                    "results": results,
                }),
                pretty,
                terse,
                false,
                schema
            )?
        );
    }

    if err_count > 0 {
        bail!("{} edit(s) failed", err_count);
    }
    Ok(())
}

pub(crate) fn cmd_convex_sync(options: ConvexSyncOptions<'_>, format: OutputFormat) -> Result<()> {
    let transport = if options.remote_snapshot || options.apply {
        Some(ConvexHttpTransport::from_options(
            options.endpoint,
            options.auth_token_env,
        )?)
    } else {
        None
    };
    let mut snapshot_diagnostics = Vec::new();
    let snapshot_rows = if options.remote_snapshot {
        let local_report = build_convex_sync_report_with_snapshot(
            options.path,
            options.scope,
            None,
            options.chunk_size,
            true,
        )?;
        let local_rows = ConvexProjectionRows {
            nodes: local_report.node_upserts.clone(),
            edges: local_report.edge_upserts.clone(),
        };
        let (rows, diagnostics) = transport
            .as_ref()
            .expect("transport is initialized when remote_snapshot is set")
            .fetch_snapshot(
                GRAPH_PROJECTION_VERSION,
                options.scope,
                local_report.projection_hash.as_deref(),
                Some(&local_rows),
            )?;
        snapshot_diagnostics = diagnostics;
        Some(rows)
    } else {
        options
            .snapshot
            .map(load_convex_projection_rows)
            .transpose()?
    };
    let mut report = build_convex_sync_report_with_snapshot(
        options.path,
        options.scope,
        snapshot_rows,
        options.chunk_size,
        !options.apply,
    )?;
    report.diagnostics.extend(snapshot_diagnostics);
    if let Some(transport) = &transport {
        let mut receipts = Vec::new();
        if options.apply {
            for chunk in &report.chunks {
                receipts.push(transport.apply_chunk(&report, chunk)?);
            }
        }
        report.transport = Some(transport.summary(options.remote_snapshot, receipts.len()));
        report.receipts = receipts;
        if options.apply {
            report
                .diagnostics
                .push("live Convex transport completed all planned chunks".to_string());
        } else if options.remote_snapshot {
            report
                .diagnostics
                .push("remote Convex snapshot was pulled before diffing".to_string());
        }
    }
    if format.json_output {
        print_json_or_envelope(
            &report,
            &format,
            "convex-sync",
            if report.dry_run { "dry-run" } else { "apply" },
            ToolEnvelopeSummary {
                text: format!(
                    "Convex graph sync {}: {} node upserts, {} edge upserts, {} chunks, freshness {}",
                    if report.dry_run { "plan" } else { "apply" },
                    report.node_upserts.len(),
                    report.edge_upserts.len(),
                    report.chunks.len(),
                    report.freshness.status
                ),
                metrics: vec![
                    envelope_metric("node_upserts", report.node_upserts.len()),
                    envelope_metric("edge_upserts", report.edge_upserts.len()),
                    envelope_metric("chunks", report.chunks.len()),
                    envelope_metric("freshness", &report.freshness.status),
                ],
            },
            report.freshness.fail_closed,
            vec![
                "Apply the planned chunks in order, then rerun with --snapshot to verify freshness"
                    .to_string(),
            ],
        )
    } else {
        print_convex_sync_human(&report, format.compact);
        Ok(())
    }
}

pub(crate) fn cmd_graph_db_status(
    root: &Path,
    scope: Option<&str>,
    format: OutputFormat,
) -> Result<()> {
    let graph_db = graph_substrate_db_path(root, scope);
    let report =
        graph_db_operator_report_from_disk(root, scope, &graph_db, "status", None, Vec::new())?;
    print_graph_db_operator_report(&report, format)
}

pub(crate) fn cmd_graph_db_refresh(
    root: &Path,
    path: &Path,
    scope: Option<&str>,
    format: OutputFormat,
) -> Result<()> {
    let source_watermark = traversal_source_watermark(root, path, scope, false)?;
    let cached_refresh =
        graph_db_backend_eval_cached_refresh(root, scope, source_watermark.as_deref())?;
    let (mode, mut warnings, refresh, phase_timings) =
        if let Some((_graph, refresh, phase_timings)) = cached_refresh {
            (
                "cached_source_watermark_reuse".to_string(),
                Vec::new(),
                refresh,
                phase_timings,
            )
        } else {
            let (graph, refresh) = write_traversal_graph_store(root, path, scope)?;
            let phase_timings = refresh
                .phase_timings
                .iter()
                .map(|phase| GraphDbBackendEvalPhaseTiming {
                    name: phase.name.clone(),
                    duration_micros: phase.duration_micros,
                    detail: phase.detail.clone(),
                })
                .collect::<Vec<_>>();
            (
                "cold_source_graph_rebuild".to_string(),
                graph.warnings,
                refresh,
                phase_timings,
            )
        };
    let graph_db = graph_substrate_db_path(root, scope);
    warnings.extend(graph_db_operator_status_warnings(root, scope));
    let warnings = dedupe_preserve_order(warnings);
    let refresh = GraphDbRefreshSummary {
        scope: refresh.scope,
        projection_version: refresh.projection_version,
        mode,
        source_watermark: refresh.source_watermark,
        tombstoned_nodes: refresh.tombstoned_nodes.len(),
        tombstoned_edges: refresh.tombstoned_edges.len(),
        upserted_nodes: refresh.upserted_nodes,
        upserted_edges: refresh.upserted_edges,
        unchanged_nodes: refresh.unchanged_nodes,
        unchanged_edges: refresh.unchanged_edges,
        upserted_properties: refresh.upserted_properties,
        unchanged_properties: refresh.unchanged_properties,
        deleted_properties: refresh.deleted_properties,
        deleted_nodes: refresh.deleted_nodes,
        deleted_edges: refresh.deleted_edges,
        pruned_tombstones: refresh.pruned_tombstones,
        file_size_bytes_before: refresh.file_size_bytes_before,
        file_size_bytes_after: refresh.file_size_bytes_after,
        phase_timings,
    };
    let report = graph_db_operator_report_from_disk(
        root,
        scope,
        &graph_db,
        "refresh",
        Some(refresh),
        warnings,
    )?;
    print_graph_db_operator_report(&report, format)
}

pub(crate) fn cmd_graph_db_doctor(
    root: &Path,
    scope: Option<&str>,
    backend: GraphDbBackend,
    convex_snapshot: Option<&Path>,
    format: OutputFormat,
) -> Result<()> {
    let graph_db = graph_substrate_db_path(root, scope);
    let backend_name = match backend {
        GraphDbBackend::Sqlite => "sqlite",
        GraphDbBackend::ConvexSnapshot => "convex-snapshot",
        GraphDbBackend::Tokensave => "tokensave",
    };
    let doctor_path = if backend == GraphDbBackend::Tokensave {
        root.join(".tokensave").join("tokensave.db")
    } else {
        graph_db.clone()
    };
    let mut report =
        GraphDbDoctorReport::new(root, scope, backend_name, &doctor_path, convex_snapshot);
    let conn = if backend == GraphDbBackend::Tokensave {
        append_tokensave_graph_doctor_checks(&mut report, root);
        None
    } else {
        append_sqlite_graph_doctor_checks(&mut report, root, scope, &graph_db)
    };
    let local_rows = conn
        .as_ref()
        .and_then(|conn| sqlite_convex_rows_from_conn(conn.conn()).ok());
    if backend == GraphDbBackend::ConvexSnapshot {
        append_convex_snapshot_doctor_checks(
            &mut report,
            root,
            scope,
            local_rows.as_ref(),
            convex_snapshot,
        );
    }
    report.finalize();

    let fail_closed = report.fail_closed;
    let summary = report.summary();
    if format.json_output {
        print_json_or_envelope(
            &report,
            &format,
            "graph-db",
            "doctor",
            ToolEnvelopeSummary {
                text: format!(
                    "Graph DB doctor {} for {} backend with {} check(s)",
                    report.status,
                    report.backend,
                    report.checks.len()
                ),
                metrics: vec![
                    envelope_metric("backend", &report.backend),
                    envelope_metric("status", &report.status),
                    envelope_metric("checks", report.checks.len()),
                ],
            },
            false,
            report.repair_commands.clone(),
        )?;
    } else {
        print_graph_db_doctor_human(&report);
    }

    if fail_closed {
        bail!(
            "graph-db doctor failed closed for {} backend: {}",
            backend_name,
            if summary.is_empty() {
                "see diagnostics".to_string()
            } else {
                summary
            }
        );
    }
    Ok(())
}

pub(crate) fn cmd_graph_db_drift(
    root: &Path,
    path: &Path,
    scope: Option<&str>,
    convex_snapshot: Option<&Path>,
    format: OutputFormat,
) -> Result<()> {
    let snapshot_path =
        convex_snapshot.context("graph-db drift requires --convex-snapshot <rows.json>")?;
    let (graph, _refresh) = write_traversal_graph_store(root, path, scope)?;
    let graph_db = graph_substrate_db_path(root, scope);
    let store = SqliteGraphStore::open_read_only_resilient(&graph_db)?;
    let local = convex_rows_from_graph_store(&store)?;
    let (snapshot, snapshot_value) = load_convex_projection_snapshot_value(snapshot_path)?;
    let mut warnings = graph.warnings;
    if let Some(recovery) = store.read_only_recovery() {
        warnings.push(graph_db_read_recovery_diagnostic(recovery));
    }
    let report = graph_db_drift_report(GraphDbDriftInput {
        root,
        scope,
        graph_db: &graph_db,
        snapshot_path,
        local: &local,
        snapshot: &snapshot,
        snapshot_value: &snapshot_value,
        warnings,
    });

    if format.json_output {
        print_json_or_envelope(
            &report,
            &format,
            "graph-db",
            "drift",
            ToolEnvelopeSummary {
                text: format!(
                    "Graph DB drift status {} with {} node upsert(s), {} edge upsert(s), {} node tombstone(s), {} edge tombstone(s)",
                    report.status,
                    report.summary.node_upserts,
                    report.summary.edge_upserts,
                    report.summary.node_tombstones,
                    report.summary.edge_tombstones
                ),
                metrics: vec![
                    envelope_metric("status", &report.status),
                    envelope_metric("node_upserts", report.summary.node_upserts),
                    envelope_metric("edge_upserts", report.summary.edge_upserts),
                    envelope_metric("node_tombstones", report.summary.node_tombstones),
                    envelope_metric("edge_tombstones", report.summary.edge_tombstones),
                ],
            },
            false,
            report.next_commands.clone(),
        )
    } else {
        print_graph_db_drift_human(&report);
        Ok(())
    }
}

pub(crate) fn cmd_graph_db_compact(
    root: &Path,
    scope: Option<&str>,
    apply: bool,
    prune_tombstones: bool,
    confirmed_convex_reconciled: bool,
    format: OutputFormat,
) -> Result<()> {
    if prune_tombstones && !confirmed_convex_reconciled {
        bail!(
            "graph-db compact --prune-tombstones requires --confirmed-convex-reconciled after Convex deletion reconciliation has completed"
        );
    }
    let graph_db = graph_substrate_db_path(root, scope);
    if apply && !graph_db.exists() {
        bail!("graph-db compact --apply requires an existing graph.db; run graph-db refresh first");
    }
    let before =
        graph_db_operator_report_from_disk(root, scope, &graph_db, "compact", None, Vec::new())?;
    let mut warnings = before.warnings.clone();
    let mut pruned_tombstones = 0usize;
    if apply {
        let mut store = SqliteGraphStore::open(&graph_db)?;
        pruned_tombstones = store.compact_storage(scope.unwrap_or("root"), prune_tombstones)?;
        if prune_tombstones {
            warnings.push(format!(
                "pruned {pruned_tombstones} retained tombstone row(s) after explicit Convex reconciliation confirmation"
            ));
        }
    }
    let after = graph_db_operator_report_from_disk(
        root,
        scope,
        &graph_db,
        "compact",
        None,
        warnings.clone(),
    )?;
    let before_file = before.counts.file_size_bytes.unwrap_or(0) as i64;
    let after_file = after.counts.file_size_bytes.unwrap_or(0) as i64;
    let mut next_commands = after.compaction.recommendations.clone();
    next_commands.push(format!(
        "tsift graph-db --path {}{} status --json",
        shell_quote(root.to_string_lossy().as_ref()),
        graph_db_scope_arg(scope)
    ));
    next_commands.push(format!(
        "tsift graph-db --path {}{} doctor --json",
        shell_quote(root.to_string_lossy().as_ref()),
        graph_db_scope_arg(scope)
    ));
    let compaction_after =
        graph_db_compaction_policy(root, scope, &after.counts, confirmed_convex_reconciled);
    let report = GraphDbCompactionReport {
        root: root.to_string_lossy().to_string(),
        scope: scope.map(str::to_string),
        graph_db: graph_db.to_string_lossy().to_string(),
        applied: apply,
        pruned_tombstones,
        counts_before: before.counts,
        counts_after: after.counts,
        compaction_before: before.compaction,
        compaction_after,
        reclaimed_bytes: before_file - after_file,
        next_commands: dedupe_preserve_order(next_commands),
        warnings,
    };

    if format.json_output {
        print_json_or_envelope(
            &report,
            &format,
            "graph-db",
            "compact",
            ToolEnvelopeSummary {
                text: format!(
                    "Graph DB compact {} with {} reclaimed byte(s) and {} tombstone row(s) pruned",
                    if apply { "applied" } else { "dry-run" },
                    report.reclaimed_bytes,
                    report.pruned_tombstones
                ),
                metrics: vec![
                    envelope_metric("applied", report.applied),
                    envelope_metric("reclaimed_bytes", report.reclaimed_bytes),
                    envelope_metric("pruned_tombstones", report.pruned_tombstones),
                    envelope_metric("tombstones_after", report.counts_after.tombstones.total),
                ],
            },
            false,
            report.next_commands.clone(),
        )
    } else {
        print_graph_db_compaction_human(&report);
        Ok(())
    }
}

pub(crate) fn cmd_graph_db_backend_eval(
    options: GraphDbBackendEvalOptions<'_>,
    format: OutputFormat,
) -> Result<()> {
    let GraphDbBackendEvalOptions {
        path,
        scope,
        candidates,
        targets,
        full_projection,
    } = options;
    let root = lint::resolve_project_root_or_canonical_path(path)?;
    let candidates = if candidates.is_empty() {
        vec![
            GraphDbExperimentalBackend::DuckdbDuckpgq,
            GraphDbExperimentalBackend::Falkordb,
            GraphDbExperimentalBackend::Ladybug,
            GraphDbExperimentalBackend::Kuzu,
            GraphDbExperimentalBackend::Surrealdb,
        ]
    } else {
        candidates
            .iter()
            .map(|candidate| GraphDbExperimentalBackend::parse(candidate))
            .collect::<Result<Vec<_>>>()?
    };
    let high_degree_nodes = 128;
    let high_degree_fanout = 8;
    let deep_chain_nodes = 640;
    let deep_chain_fanout = 1;
    let depth = 3;
    let limit = 8;
    let impact_limit = 20;

    let (graph, _refresh, mut phase_timings) =
        graph_db_backend_eval_refresh_with_profile(&root, path, scope)
            .with_context(|| format!("refreshing graph-db projection for {}", root.display()))?;
    let refresh_micros = graph_db_backend_eval_refresh_total_micros(&phase_timings);
    let reused_cached_projection = graph_db_backend_eval_reused_cached_projection(&phase_timings);
    let prepared = graph_db_backend_eval_timed_phase(
        &mut phase_timings,
        "conflict_matrix_preparation",
        "context-pack, cached diff digest, and cached impact inputs shared by conflict-matrix and dispatch-trace measurements",
        || prepare_conflict_matrix_inputs(&root, path, scope, impact_limit),
    )?;
    phase_timings.extend(prepared.preparation_timings.iter().map(|phase| {
        GraphDbBackendEvalPhaseTiming {
            name: format!("conflict_matrix_preparation.{}", phase.name),
            duration_micros: phase.duration_micros,
            detail: phase.detail.clone(),
        }
    }));
    if !reused_cached_projection {
        graph_db_backend_eval_update_source_watermark(&root, path, scope)?;
    }
    let graph_db = graph_substrate_db_path(&root, scope);
    let real_store = SqliteGraphStore::open_read_only_resilient(&graph_db)
        .with_context(|| format!("opening graph-db projection: {}", graph_db.display()))?;
    let real_freshness = sqlite_graph_freshness(&real_store, scope.unwrap_or("root"))?;
    let real_targets = graph_db_backend_eval_targets(&real_store, targets)?;
    let real_rows = convex_rows_from_graph_store(&real_store)?;
    let real_refresh = graph_db_backend_eval_refresh_operation(
        refresh_micros,
        real_rows.nodes.len() + real_rows.edges.len(),
        serde_json::json!({
            "nodes": real_rows.nodes.len(),
            "edges": real_rows.edges.len(),
        }),
    );
    let mut real_warnings = graph.warnings;
    if let Some(recovery) = real_store.read_only_recovery() {
        real_warnings.push(graph_db_read_recovery_diagnostic(recovery));
    }
    let real_dataset = graph_db_backend_eval_dataset(
        "real",
        &root,
        path,
        scope,
        &real_targets,
        depth,
        limit,
        impact_limit,
        &candidates,
        &real_store,
        real_freshness,
        real_refresh,
        real_rows,
        real_warnings,
        &prepared,
    )?;

    let high_degree_projection =
        graph_db_backend_eval_synthetic_projection(high_degree_nodes, high_degree_fanout);
    let high_degree_started = Instant::now();
    let mut high_degree_store = SqliteGraphStore::in_memory()?;
    let _high_degree_refresh = high_degree_store.replace_projection_with_version(
        "root",
        &high_degree_projection,
        Some(GRAPH_PROJECTION_VERSION),
        Some(format!(
            "synthetic-high-degree:{high_degree_nodes}:{high_degree_fanout}"
        )),
    )?;
    let high_degree_micros = high_degree_started.elapsed().as_micros();
    let high_degree_freshness = sqlite_graph_freshness(&high_degree_store, "root")?;
    let high_degree_targets = graph_db_backend_eval_targets(&high_degree_store, &[])?;
    let high_degree_rows = convex_rows_from_graph_store(&high_degree_store)?;
    let high_degree_refresh = graph_db_backend_eval_refresh_operation(
        high_degree_micros,
        high_degree_rows.nodes.len() + high_degree_rows.edges.len(),
        serde_json::json!({
            "nodes": high_degree_rows.nodes.len(),
            "edges": high_degree_rows.edges.len(),
        }),
    );
    let high_degree_dataset = graph_db_backend_eval_dataset(
        "synthetic_high_degree",
        &root,
        path,
        scope,
        &high_degree_targets,
        depth,
        limit,
        impact_limit,
        &candidates,
        &high_degree_store,
        high_degree_freshness,
        high_degree_refresh,
        high_degree_rows,
        Vec::new(),
        &prepared,
    )?;

    let deep_chain_projection =
        graph_db_backend_eval_synthetic_projection(deep_chain_nodes, deep_chain_fanout);
    let deep_chain_started = Instant::now();
    let mut deep_chain_store = SqliteGraphStore::in_memory()?;
    let _deep_chain_refresh = deep_chain_store.replace_projection_with_version(
        "root",
        &deep_chain_projection,
        Some(GRAPH_PROJECTION_VERSION),
        Some(format!(
            "synthetic-deep-chain:{deep_chain_nodes}:{deep_chain_fanout}"
        )),
    )?;
    let deep_chain_micros = deep_chain_started.elapsed().as_micros();
    let deep_chain_freshness = sqlite_graph_freshness(&deep_chain_store, "root")?;
    let deep_chain_targets = graph_db_backend_eval_targets(&deep_chain_store, &[])?;
    let deep_chain_rows = convex_rows_from_graph_store(&deep_chain_store)?;
    let deep_chain_refresh = graph_db_backend_eval_refresh_operation(
        deep_chain_micros,
        deep_chain_rows.nodes.len() + deep_chain_rows.edges.len(),
        serde_json::json!({
            "nodes": deep_chain_rows.nodes.len(),
            "edges": deep_chain_rows.edges.len(),
        }),
    );
    let deep_chain_dataset = graph_db_backend_eval_dataset(
        "synthetic_deep_chain",
        &root,
        path,
        scope,
        &deep_chain_targets,
        depth,
        limit,
        impact_limit,
        &candidates,
        &deep_chain_store,
        deep_chain_freshness,
        deep_chain_refresh,
        deep_chain_rows,
        Vec::new(),
        &prepared,
    )?;

    let mut all_targets = real_targets
        .iter()
        .chain(high_degree_targets.iter())
        .chain(deep_chain_targets.iter())
        .cloned()
        .collect::<BTreeSet<_>>();
    let mut datasets = vec![real_dataset, high_degree_dataset, deep_chain_dataset];
    let mut full_projection_phase_timings = Vec::new();
    let mut full_projection_cache_stats = None;
    if full_projection {
        let (full_projection_rows, full_projection_warnings, mut full_phases, cache_stats) =
            graph_db_backend_eval_full_projection_with_profile(&root, scope)?;
        full_projection_cache_stats = Some(cache_stats);
        let sqlite_open_started = Instant::now();
        let mut full_store = SqliteGraphStore::in_memory()?;
        let sqlite_open_micros = sqlite_open_started.elapsed().as_micros();
        full_phases.push(graph_db_backend_eval_phase_timing(
            "full_projection.sqlite.in_memory_open",
            sqlite_open_micros,
            "open in-memory SQLite graph store for the opt-in full-project projection write",
        ));
        let sqlite_replace_started = Instant::now();
        let full_refresh_op = full_store.replace_projection_with_version(
            scope.unwrap_or("root"),
            &full_projection_rows,
            Some(GRAPH_PROJECTION_VERSION),
            graph_projection_content_hash(&full_projection_rows),
        )?;
        let sqlite_replace_micros = sqlite_replace_started.elapsed().as_micros();
        full_phases.push(graph_db_backend_eval_phase_timing(
            "full_projection.sqlite.replace_projection_total",
            sqlite_replace_micros,
            "wall-clock total of the SQLite replace_projection_with_version write (see sub-phases below)",
        ));
        for sub_phase in full_refresh_op.phase_timings.iter() {
            full_phases.push(GraphDbBackendEvalPhaseTiming {
                name: format!("full_projection.sqlite.{}", sub_phase.name),
                duration_micros: sub_phase.duration_micros,
                detail: sub_phase.detail.clone(),
            });
        }
        let post_write_started = Instant::now();
        let full_freshness = sqlite_graph_freshness(&full_store, scope.unwrap_or("root"))?;
        let full_targets = graph_db_backend_eval_targets(&full_store, targets)?;
        all_targets.extend(full_targets.iter().cloned());
        let full_rows = convex_rows_from_graph_store(&full_store)?;
        let post_write_micros = post_write_started.elapsed().as_micros();
        full_phases.push(graph_db_backend_eval_phase_timing(
            "full_projection.sqlite.post_write_reads",
            post_write_micros,
            "post-write freshness, target resolution, and convex row materialization reads",
        ));
        let full_projection_micros = sqlite_open_micros + sqlite_replace_micros + post_write_micros;
        let full_refresh = graph_db_backend_eval_refresh_operation(
            full_projection_micros,
            full_rows.nodes.len() + full_rows.edges.len(),
            serde_json::json!({
                "nodes": full_rows.nodes.len(),
                "edges": full_rows.edges.len(),
            }),
        );
        phase_timings.extend(full_phases.clone());
        full_projection_phase_timings = full_phases;
        datasets.push(graph_db_backend_eval_dataset(
            "full_projection",
            &root,
            path,
            scope,
            &full_targets,
            depth,
            limit,
            impact_limit,
            &candidates,
            &full_store,
            full_freshness,
            full_refresh,
            full_rows,
            full_projection_warnings,
            &prepared,
        )?);
    }
    let targets = all_targets.into_iter().collect::<Vec<_>>();
    let promotion = graph_db_backend_eval_promotion(&datasets, &candidates);
    let mut metrics = graph_db_backend_eval_metrics(&datasets);
    if let Some(cache_stats) = &full_projection_cache_stats {
        metrics.insert(
            "full_projection.cache.disk_bytes".to_string(),
            cache_stats.disk_bytes as f64,
        );
        metrics.insert(
            "full_projection.cache.json_bytes".to_string(),
            cache_stats.json_bytes as f64,
        );
        metrics.insert(
            "full_projection.cache.compression_ratio".to_string(),
            if cache_stats.json_bytes == 0 {
                0.0
            } else {
                cache_stats.disk_bytes as f64 / cache_stats.json_bytes as f64
            },
        );
        metrics.insert(
            "full_projection.cache.hit".to_string(),
            if cache_stats.hit { 1.0 } else { 0.0 },
        );
        metrics.insert(
            "full_projection.cache.pruned_files".to_string(),
            cache_stats.pruned_files as f64,
        );
        metrics.insert(
            "full_projection.cache.pruned_bytes".to_string(),
            cache_stats.pruned_bytes as f64,
        );
    }
    if let Some(real_dataset) = datasets.iter().find(|dataset| dataset.name == "real") {
        let real_phase_timings = phase_timings
            .iter()
            .filter(|phase| !phase.name.starts_with("full_projection."))
            .cloned()
            .collect::<Vec<_>>();
        append_graph_db_backend_eval_phase_metrics(
            &mut metrics,
            "real",
            graph_db_backend_eval_graph_rows(real_dataset),
            &real_phase_timings,
        );
    }
    if let Some(full_dataset) = datasets
        .iter()
        .find(|dataset| dataset.name == "full_projection")
    {
        let normalized_full_projection_phases = full_projection_phase_timings
            .iter()
            .filter_map(|phase| {
                Some(GraphDbBackendEvalPhaseTiming {
                    name: phase.name.strip_prefix("full_projection.")?.to_string(),
                    duration_micros: phase.duration_micros,
                    detail: phase.detail.clone(),
                })
            })
            .collect::<Vec<_>>();
        append_graph_db_backend_eval_phase_metrics(
            &mut metrics,
            "full_projection",
            graph_db_backend_eval_graph_rows(full_dataset),
            &normalized_full_projection_phases,
        );
        for phase in &full_projection_phase_timings {
            if let Some(sqlite_phase) = phase.name.strip_prefix("full_projection.sqlite.") {
                metrics.insert(
                    format!("full_projection.sqlite.{sqlite_phase}.duration_micros"),
                    phase.duration_micros as f64,
                );
                append_graph_db_backend_eval_normalized_duration_metric(
                    &mut metrics,
                    &format!(
                        "full_projection.sqlite.{sqlite_phase}.duration_micros_per_1k_graph_rows"
                    ),
                    phase.duration_micros,
                    graph_db_backend_eval_graph_rows(full_dataset),
                );
            }
        }
    }
    let report = GraphDbBackendEvalReport {
        root: root.to_string_lossy().to_string(),
        scope: scope.map(str::to_string),
        label: "graph-db backend-eval".to_string(),
        baseline_backend: "sqlite".to_string(),
        candidates: candidates
            .iter()
            .map(|candidate| candidate.name().to_string())
            .collect(),
        targets,
        config: GraphDbBackendEvalConfig {
            high_degree_nodes,
            high_degree_fanout,
            deep_chain_nodes,
            deep_chain_fanout,
            depth,
            limit,
            impact_limit,
            path_max_hops: GRAPH_DB_BACKEND_EVAL_PATH_MAX_HOPS,
            path_direct_hop_budget: GRAPH_DB_BACKEND_EVAL_DIRECT_PATH_HOPS,
            path_deep_chain_hop_budget: GRAPH_DB_BACKEND_EVAL_PATH_MAX_HOPS,
            path_extended_hop_budgets: GRAPH_DB_BACKEND_EVAL_EXTENDED_PATH_HOPS.to_vec(),
            path_hop_policy:
                "default path reads stay capped at 64 hops; 128/256/512-hop probes are opt-in benchmark evidence until real and synthetic regression gates pass"
                    .to_string(),
            path_probe_strategy:
                "adaptive: use one-hop direct probes for high-degree/direct edges, 64-hop deep-chain default coverage, and measured 128/256/512-hop tiers without raising user-facing defaults"
                    .to_string(),
            path_query_plan_checks: vec![
                "SQLite bounded path probes must continue using idx_graph_edges_from_kind for frontier expansion".to_string(),
                "128/256/512-hop tiers are benchmark fixtures until repeated samples and query-plan checks pass".to_string(),
            ],
            full_projection_enabled: full_projection,
            full_projection_profile: if full_projection {
                "included opt-in full_projection dataset built from the project root".to_string()
            } else {
                "disabled by default; pass --full-projection to add the full-project dataset"
                    .to_string()
            },
            normalization_row_unit: GRAPH_DB_BACKEND_EVAL_NORMALIZATION_ROW_UNIT as usize,
        },
        phase_timings,
        datasets,
        promotion,
        performance_gate: graph_db_backend_eval_performance_gate(&root, scope, full_projection),
        metrics,
        metric_digest_command: graph_db_backend_eval_metric_digest_command(
            &root,
            scope,
            full_projection,
        ),
        warnings: Vec::new(),
    };

    if format.json_output {
        print_json_or_envelope(
            &report,
            &format,
            "graph-db",
            "backend-eval",
            ToolEnvelopeSummary {
                text: format!(
                    "Graph DB backend evaluation ran {} dataset(s) against {} candidate(s)",
                    report.datasets.len(),
                    report.candidates.len()
                ),
                metrics: vec![
                    envelope_metric("datasets", report.datasets.len()),
                    envelope_metric("candidates", report.candidates.len()),
                ],
            },
            false,
            vec![
                format!(
                    "Re-run with `tsift graph-db --path {} --json backend-eval --target <id>` after adding a production candidate adapter",
                    shell_quote(root.to_string_lossy().as_ref())
                ),
                report.metric_digest_command.clone(),
            ],
        )
    } else {
        print_graph_db_backend_eval_human(&report);
        Ok(())
    }
}

/// A trusted, fresh authored finding attached to a map node (community / hub /
/// focus) because it `concerns` a member of that node (#trt1p3 graph menu). The
/// graph store is the source of truth; this is the compact projection the map
/// overview carries so an agent can drill systems-overview → community → the
/// finding explaining the coupling.
#[derive(Serialize, Clone)]
struct GraphDbMapFindingRef {
    id: String,
    kind: String,
    title: String,
    about: String,
    anchor_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    confidence: Option<f64>,
}

#[derive(Serialize)]
struct GraphDbMapCommunitySummary {
    id: usize,
    size: usize,
    top_members: Vec<String>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    findings: Vec<GraphDbMapFindingRef>,
}

#[derive(Serialize)]
struct GraphDbMapHub {
    id: String,
    kind: String,
    label: String,
    degree: usize,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    findings: Vec<GraphDbMapFindingRef>,
}

#[derive(Serialize, Clone)]
struct GraphDbMapModuleEntry {
    module: String,
    node_count: usize,
    kinds: BTreeMap<String, usize>,
}

#[derive(Serialize)]
struct GraphDbMapOverview {
    node_count: usize,
    edge_count: usize,
    community_count: usize,
    communities: Vec<GraphDbMapCommunitySummary>,
    top_hubs: Vec<GraphDbMapHub>,
    edge_kind_histogram: BTreeMap<String, usize>,
    modules: Vec<GraphDbMapModuleEntry>,
}

#[derive(Serialize)]
struct GraphDbMapReport {
    root: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    scope: Option<String>,
    backend: String,
    overview: GraphDbMapOverview,
    #[serde(skip_serializing_if = "Option::is_none")]
    focus: Option<GraphDbMapFocusReport>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    warnings: Vec<String>,
}

#[derive(Serialize)]
struct GraphDbMapFocusReport {
    symbol: String,
    node_id: String,
    node_kind: String,
    node_label: String,
    degree: usize,
    community_id: Option<usize>,
    neighbor_count: usize,
    neighbor_kinds: BTreeMap<String, usize>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    findings: Vec<GraphDbMapFindingRef>,
}

/// Collect trusted, fresh findings concerning any of `about_keys`, bucketed by
/// the `about` anchor, for map annotation (#trt1p3 graph menu). Reuses the
/// Phase 2 trusted+fresh injection contract (`collect_injectable_findings`).
/// Fails open to an empty map when the findings store is absent or unreadable.
fn collect_map_findings_by_about(
    root: &Path,
    scope: Option<&str>,
    about_keys: &BTreeSet<String>,
) -> BTreeMap<String, Vec<GraphDbMapFindingRef>> {
    let findings =
        match crate::commands::finding::collect_injectable_findings(root, about_keys, scope) {
            Ok(findings) => findings,
            Err(_) => return BTreeMap::new(),
        };
    let mut by_about: BTreeMap<String, Vec<GraphDbMapFindingRef>> = BTreeMap::new();
    for finding in findings {
        by_about
            .entry(finding.about.clone())
            .or_default()
            .push(GraphDbMapFindingRef {
                id: finding.id,
                kind: finding.kind,
                title: finding.title,
                anchor_kind: finding.anchor_kind,
                confidence: finding.confidence,
                about: finding.about,
            });
    }
    by_about
}

/// Gather the distinct findings attached to any of `keys` (dedup by finding id,
/// ordered by id) for a single map-node annotation.
fn map_findings_for_keys<'a>(
    by_about: &BTreeMap<String, Vec<GraphDbMapFindingRef>>,
    keys: impl Iterator<Item = &'a String>,
) -> Vec<GraphDbMapFindingRef> {
    let mut seen: BTreeSet<String> = BTreeSet::new();
    let mut out: Vec<GraphDbMapFindingRef> = Vec::new();
    for key in keys {
        if let Some(findings) = by_about.get(key) {
            for finding in findings {
                if seen.insert(finding.id.clone()) {
                    out.push(finding.clone());
                }
            }
        }
    }
    out.sort_by(|a, b| a.id.cmp(&b.id));
    out
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn cmd_graph_db_map(
    root: &Path,
    scope: Option<&str>,
    backend: &str,
    store: &impl GraphStore,
    focus: Option<&str>,
    top_hubs_limit: usize,
    community_limit: usize,
    _focus_depth: usize,
    format: OutputFormat,
    map_format: Option<crate::cli::MapFormat>,
    warnings: Vec<String>,
) -> Result<()> {
    let nodes = store.all_nodes()?;
    let edges = store.all_edges()?;

    let mut degree: HashMap<&str, usize> = HashMap::new();
    let mut edge_kind_histogram: BTreeMap<String, usize> = BTreeMap::new();
    for edge in &edges {
        *degree.entry(&edge.from_id).or_default() += 1;
        *degree.entry(&edge.to_id).or_default() += 1;
        *edge_kind_histogram.entry(edge.kind.clone()).or_default() += 1;
    }

    let node_by_id: HashMap<&str, &substrate::GraphNode> =
        nodes.iter().map(|n| (n.id.as_str(), n)).collect();

    let mut hubs: Vec<GraphDbMapHub> = degree
        .iter()
        .filter_map(|(&id, &deg)| {
            node_by_id.get(id).map(|node| GraphDbMapHub {
                id: id.to_string(),
                kind: node.kind.clone(),
                label: node.label.clone(),
                degree: deg,
                findings: Vec::new(),
            })
        })
        .collect();
    hubs.sort_by_key(|b| std::cmp::Reverse(b.degree));
    if top_hubs_limit > 0 && hubs.len() > top_hubs_limit {
        hubs.truncate(top_hubs_limit);
    }

    let mut modules: BTreeMap<String, GraphDbMapModuleEntry> = BTreeMap::new();
    for node in &nodes {
        let module = match node.properties.get("file") {
            Some(f) => {
                let path = Path::new(f);
                path.parent()
                    .and_then(|p| p.to_str())
                    .unwrap_or("unknown")
                    .to_string()
            }
            None => "unknown".to_string(),
        };
        let module_name = module.clone();
        let entry = modules.entry(module).or_insert_with(|| GraphDbMapModuleEntry {
            module: module_name,
            node_count: 0,
            kinds: BTreeMap::new(),
        });
        entry.node_count += 1;
        *entry.kinds.entry(node.kind.clone()).or_default() += 1;
    }
    let modules: Vec<GraphDbMapModuleEntry> = modules.into_values().collect();

    let mut communities: Vec<GraphDbMapCommunitySummary> = Vec::new();
    {
        let mut from_map: HashMap<&str, Vec<&str>> = HashMap::new();
        for edge in &edges {
            from_map
                .entry(&edge.from_id)
                .or_default()
                .push(&edge.to_id);
        }
        let edge_pairs: Vec<(String, String)> = edges
            .iter()
            .map(|e| (e.from_id.clone(), e.to_id.clone()))
            .collect();
        let comm_result = tsift_graph::detect_communities(&edge_pairs);
        let mut comm_list: Vec<(usize, usize, Vec<String>)> = comm_result
            .communities
            .iter()
            .enumerate()
            .filter(|(_, c)| c.members.len() >= 2)
            .map(|(i, c)| {
                let mut names: Vec<String> =
                    c.members.iter().map(|m| m.name.clone()).collect();
                names.sort();
                (i, c.members.len(), names)
            })
            .collect();
        comm_list.sort_by_key(|b| std::cmp::Reverse(b.1));
        if community_limit > 0 && comm_list.len() > community_limit {
            comm_list.truncate(community_limit);
        }
        for (i, size, members) in comm_list {
            let top_members: Vec<String> = members.into_iter().take(10).collect();
            communities.push(GraphDbMapCommunitySummary {
                id: i + 1,
                size,
                top_members,
                findings: Vec::new(),
            });
        }
    }

    // #trt1p3 graph menu: annotate communities and hubs with the trusted, fresh
    // authored findings that concern their members, so the overview drills
    // systems → community → the finding explaining the coupling. Anchored by the
    // displayed member names / hub labels (bounded), reusing the Phase 2
    // trusted+fresh injection contract. Fails open to no annotations.
    let mut finding_about_keys: BTreeSet<String> = BTreeSet::new();
    for community in &communities {
        for member in &community.top_members {
            finding_about_keys.insert(member.clone());
        }
    }
    for hub in &hubs {
        finding_about_keys.insert(hub.label.clone());
    }
    if let Some(symbol) = focus {
        finding_about_keys.insert(symbol.to_string());
    }
    let findings_by_about = collect_map_findings_by_about(root, scope, &finding_about_keys);
    if !findings_by_about.is_empty() {
        for community in &mut communities {
            community.findings =
                map_findings_for_keys(&findings_by_about, community.top_members.iter());
        }
        for hub in &mut hubs {
            hub.findings =
                map_findings_for_keys(&findings_by_about, std::iter::once(&hub.label));
        }
    }

    let overview = GraphDbMapOverview {
        node_count: nodes.len(),
        edge_count: edges.len(),
        community_count: communities.len(),
        communities,
        top_hubs: hubs,
        edge_kind_histogram,
        modules,
    };

    let focus_report = if let Some(symbol) = focus {
        let matches: Vec<&substrate::GraphNode> = nodes
            .iter()
            .filter(|n| n.label == symbol || n.id.contains(symbol))
            .collect();
        match matches.first() {
            Some(focus_node) => {
                let node_deg = degree.get(focus_node.id.as_str()).copied().unwrap_or(0);
                let incident = store.incident_edges(&focus_node.id, None)?;
                let mut neighbor_kinds: BTreeMap<String, usize> = BTreeMap::new();
                for edge in &incident {
                    let neighbor_id = if edge.from_id == focus_node.id {
                        &edge.to_id
                    } else {
                        &edge.from_id
                    };
                    if let Some(neighbor) = node_by_id.get(neighbor_id.as_str()) {
                        *neighbor_kinds
                            .entry(neighbor.kind.clone())
                            .or_default() += 1;
                    }
                }
                let comm_id = {
                    let edge_pairs: Vec<(String, String)> = edges
                        .iter()
                        .map(|e| (e.from_id.clone(), e.to_id.clone()))
                        .collect();
                    let comm_result = tsift_graph::detect_communities(&edge_pairs);
                    comm_result
                        .communities
                        .iter()
                        .position(|c| c.members.iter().any(|m| m.name == symbol))
                };
                let focus_findings = map_findings_for_keys(
                    &findings_by_about,
                    [symbol.to_string(), focus_node.label.clone()].iter(),
                );
                Some(GraphDbMapFocusReport {
                    symbol: symbol.to_string(),
                    node_id: focus_node.id.clone(),
                    node_kind: focus_node.kind.clone(),
                    node_label: focus_node.label.clone(),
                    degree: node_deg,
                    community_id: comm_id.map(|i| i + 1),
                    neighbor_count: incident.len(),
                    neighbor_kinds,
                    findings: focus_findings,
                })
            }
            None => None,
        }
    } else {
        None
    };

    let report = GraphDbMapReport {
        root: root.to_string_lossy().to_string(),
        scope: scope.map(str::to_string),
        backend: backend.to_string(),
        overview,
        focus: focus_report,
        warnings,
    };

    // #trt1p3 on-demand projection: md (greppable, commit-friendly) / html
    // (interactive human view) rendered from the same report the JSON view
    // serializes — the graph store stays the single source of truth.
    if let Some(map_format) = map_format {
        let rendered = match map_format {
            crate::cli::MapFormat::Md => render_graph_db_map_markdown(&report),
            crate::cli::MapFormat::Html => render_graph_db_map_html(&report),
        };
        println!("{rendered}");
        return Ok(());
    }

    if format.json_output {
        print_json_or_envelope(
            &report,
            &format,
            "graph-db",
            "map",
            ToolEnvelopeSummary {
                text: format!(
                    "Graph map: {} nodes, {} edges, {} communities, {} hubs",
                    report.overview.node_count,
                    report.overview.edge_count,
                    report.overview.community_count,
                    report.overview.top_hubs.len(),
                ),
                metrics: vec![
                    envelope_metric("nodes", report.overview.node_count),
                    envelope_metric("edges", report.overview.edge_count),
                    envelope_metric("communities", report.overview.community_count),
                    envelope_metric("hubs", report.overview.top_hubs.len()),
                    envelope_metric("modules", report.overview.modules.len()),
                    envelope_metric("edge_kinds", report.overview.edge_kind_histogram.len()),
                ],
            },
            false,
            if report.focus.is_some() {
                vec![]
            } else {
                vec![
                    "Use --focus <symbol> to add a focused deep-dive tier".to_string(),
                ]
            },
        )
    } else {
        print_graph_db_map_human(&report, format.compact);
        Ok(())
    }
}

fn print_graph_db_map_human(report: &GraphDbMapReport, compact: bool) {
    let ov = &report.overview;
    if compact {
        println!(
            "map n:{} e:{} com:{} hubs:{} mods:{} ek:{}",
            ov.node_count,
            ov.edge_count,
            ov.community_count,
            ov.top_hubs.len(),
            ov.modules.len(),
            ov.edge_kind_histogram.len(),
        );
        for hub in &ov.top_hubs {
            println!("  hub {} [{}] deg:{}", hub.label, hub.kind, hub.degree);
        }
        return;
    }

    println!("Graph Map ({}, {})", report.backend, report.root);
    if let Some(scope) = &report.scope {
        println!("scope: {}", scope);
    }
    println!(
        "Overview: {} nodes, {} edges, {} communities",
        ov.node_count, ov.edge_count, ov.community_count
    );

    if !ov.edge_kind_histogram.is_empty() {
        println!();
        println!("Edge kinds:");
        let mut kinds: Vec<_> = ov.edge_kind_histogram.iter().collect();
        kinds.sort_by(|a, b| b.1.cmp(a.1));
        for (kind, count) in kinds {
            println!("  {}: {}", kind, count);
        }
    }

    if !ov.top_hubs.is_empty() {
        println!();
        println!("Top hubs by degree:");
        for hub in &ov.top_hubs {
            println!("  {} [{}] degree={}", hub.label, hub.kind, hub.degree);
            for finding in &hub.findings {
                println!(
                    "    finding [{}] {} (about {})",
                    finding.kind, finding.title, finding.about
                );
            }
        }
    }

    if !ov.communities.is_empty() {
        println!();
        println!("Communities:");
        for comm in &ov.communities {
            let members_display = if comm.top_members.len() < comm.size {
                format!(
                    "{} ... (+{} more)",
                    comm.top_members.join(", "),
                    comm.size - comm.top_members.len()
                )
            } else {
                comm.top_members.join(", ")
            };
            println!(
                "  [{}] {} members: {}",
                comm.id, comm.size, members_display
            );
            for finding in &comm.findings {
                println!(
                    "    finding [{}] {} (about {})",
                    finding.kind, finding.title, finding.about
                );
            }
        }
    }

    if !ov.modules.is_empty() {
        println!();
        let mut modules = ov.modules.clone();
        modules.sort_by_key(|b| std::cmp::Reverse(b.node_count));
        let display_modules: Vec<_> = if modules.len() > 15 {
            modules[..15].to_vec()
        } else {
            modules.clone()
        };
        println!("Modules (top {}):", display_modules.len());
        for module in display_modules {
            println!("  {}: {} nodes", module.module, module.node_count);
        }
    }

    if let Some(focus) = &report.focus {
        println!();
        println!("Focus: {} [{}]", focus.node_label, focus.node_kind);
        println!("  id: {}", focus.node_id);
        println!("  degree: {}", focus.degree);
        if let Some(comm_id) = focus.community_id {
            println!("  community: {}", comm_id);
        }
        println!("  neighbors: {}", focus.neighbor_count);
        if !focus.neighbor_kinds.is_empty() {
            print!("  neighbor kinds:");
            for (kind, count) in &focus.neighbor_kinds {
                print!(" {}={}", kind, count);
            }
            println!();
        }
        for finding in &focus.findings {
            println!(
                "  finding [{}] {} (about {})",
                finding.kind, finding.title, finding.about
            );
        }
    }

    for warning in &report.warnings {
        println!("warning: {}", warning);
    }
}

/// Render the map overview + attached findings as Markdown (#trt1p3). Greppable,
/// commit-friendly projection; the graph store remains the source of truth.
fn render_graph_db_map_markdown(report: &GraphDbMapReport) -> String {
    let ov = &report.overview;
    let mut out = String::new();
    out.push_str(&format!("# Graph Map — {}\n\n", report.backend));
    out.push_str(&format!("Root: `{}`\n", report.root));
    if let Some(scope) = &report.scope {
        out.push_str(&format!("Scope: `{scope}`\n"));
    }
    out.push_str(&format!(
        "\n**Overview:** {} nodes, {} edges, {} communities\n",
        ov.node_count, ov.edge_count, ov.community_count
    ));

    if !ov.edge_kind_histogram.is_empty() {
        out.push_str("\n## Edge kinds\n\n");
        let mut kinds: Vec<_> = ov.edge_kind_histogram.iter().collect();
        kinds.sort_by(|a, b| b.1.cmp(a.1).then(a.0.cmp(b.0)));
        for (kind, count) in kinds {
            out.push_str(&format!("- `{kind}`: {count}\n"));
        }
    }

    if !ov.top_hubs.is_empty() {
        out.push_str("\n## Top hubs by degree\n\n");
        for hub in &ov.top_hubs {
            out.push_str(&format!(
                "- **{}** [{}] degree={}\n",
                hub.label, hub.kind, hub.degree
            ));
            for finding in &hub.findings {
                out.push_str(&map_finding_markdown_line(finding));
            }
        }
    }

    if !ov.communities.is_empty() {
        out.push_str("\n## Communities\n");
        for comm in &ov.communities {
            out.push_str(&format!(
                "\n### Community {} ({} members)\n\n",
                comm.id, comm.size
            ));
            let members = if comm.top_members.len() < comm.size {
                format!(
                    "{} … (+{} more)",
                    comm.top_members.join(", "),
                    comm.size - comm.top_members.len()
                )
            } else {
                comm.top_members.join(", ")
            };
            out.push_str(&format!("{members}\n"));
            for finding in &comm.findings {
                out.push_str(&map_finding_markdown_line(finding));
            }
        }
    }

    if !ov.modules.is_empty() {
        let mut modules = ov.modules.clone();
        modules.sort_by_key(|b| std::cmp::Reverse(b.node_count));
        out.push_str("\n## Modules\n\n");
        for module in modules.iter().take(15) {
            out.push_str(&format!("- `{}`: {} nodes\n", module.module, module.node_count));
        }
    }

    if let Some(focus) = &report.focus {
        out.push_str(&format!(
            "\n## Focus: {} [{}]\n\n",
            focus.node_label, focus.node_kind
        ));
        out.push_str(&format!("- id: `{}`\n", focus.node_id));
        out.push_str(&format!("- degree: {}\n", focus.degree));
        if let Some(community_id) = focus.community_id {
            out.push_str(&format!("- community: {community_id}\n"));
        }
        out.push_str(&format!("- neighbors: {}\n", focus.neighbor_count));
        for finding in &focus.findings {
            out.push_str(&map_finding_markdown_line(finding));
        }
    }

    out
}

fn map_finding_markdown_line(finding: &GraphDbMapFindingRef) -> String {
    let confidence = finding
        .confidence
        .map(|value| format!(", confidence {value}"))
        .unwrap_or_default();
    format!(
        "- 📌 {}: {} (about `{}`{})\n",
        finding.kind, finding.title, finding.about, confidence
    )
}

fn map_html_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Render the map overview + attached findings as a self-contained HTML page
/// (#trt1p3) — an interactive human view of a large graph. Same data as the
/// JSON/Markdown projections; the graph store remains the source of truth.
fn render_graph_db_map_html(report: &GraphDbMapReport) -> String {
    let ov = &report.overview;
    let mut out = String::new();
    out.push_str("<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n");
    out.push_str(&format!(
        "<title>Graph Map — {}</title>\n",
        map_html_escape(&report.backend)
    ));
    out.push_str("<style>body{font-family:system-ui,-apple-system,sans-serif;margin:2rem;max-width:60rem;color:#1a1a1a}h1{margin-bottom:.2rem}h2{margin-top:1.6rem;border-bottom:1px solid #ddd;padding-bottom:.2rem}h3{margin-bottom:.2rem}code{background:#f4f4f4;padding:.1rem .3rem;border-radius:3px}ul{margin-top:.3rem}.finding{color:#7a3e00;background:#fff7ec;border-left:3px solid #d98c2b;padding:.2rem .5rem;margin:.2rem 0;list-style:none}.meta{color:#666;font-size:.9rem}</style>\n");
    out.push_str("</head>\n<body>\n");
    out.push_str(&format!(
        "<h1>Graph Map — {}</h1>\n",
        map_html_escape(&report.backend)
    ));
    out.push_str(&format!(
        "<p class=\"meta\">Root: <code>{}</code>",
        map_html_escape(&report.root)
    ));
    if let Some(scope) = &report.scope {
        out.push_str(&format!(" · scope: <code>{}</code>", map_html_escape(scope)));
    }
    out.push_str("</p>\n");
    out.push_str(&format!(
        "<p><strong>Overview:</strong> {} nodes, {} edges, {} communities</p>\n",
        ov.node_count, ov.edge_count, ov.community_count
    ));

    if !ov.edge_kind_histogram.is_empty() {
        out.push_str("<h2>Edge kinds</h2>\n<ul>\n");
        let mut kinds: Vec<_> = ov.edge_kind_histogram.iter().collect();
        kinds.sort_by(|a, b| b.1.cmp(a.1).then(a.0.cmp(b.0)));
        for (kind, count) in kinds {
            out.push_str(&format!(
                "<li><code>{}</code>: {}</li>\n",
                map_html_escape(kind),
                count
            ));
        }
        out.push_str("</ul>\n");
    }

    if !ov.top_hubs.is_empty() {
        out.push_str("<h2>Top hubs by degree</h2>\n<ul>\n");
        for hub in &ov.top_hubs {
            out.push_str(&format!(
                "<li><strong>{}</strong> [{}] degree={}",
                map_html_escape(&hub.label),
                map_html_escape(&hub.kind),
                hub.degree
            ));
            let findings = map_findings_html_list(&hub.findings);
            if !findings.is_empty() {
                out.push_str(&format!("<ul>{findings}</ul>"));
            }
            out.push_str("</li>\n");
        }
        out.push_str("</ul>\n");
    }

    if !ov.communities.is_empty() {
        out.push_str("<h2>Communities</h2>\n");
        for comm in &ov.communities {
            out.push_str(&format!(
                "<h3>Community {} ({} members)</h3>\n",
                comm.id, comm.size
            ));
            let members = if comm.top_members.len() < comm.size {
                format!(
                    "{} … (+{} more)",
                    comm.top_members.join(", "),
                    comm.size - comm.top_members.len()
                )
            } else {
                comm.top_members.join(", ")
            };
            out.push_str(&format!("<p>{}</p>\n", map_html_escape(&members)));
            let findings = map_findings_html_list(&comm.findings);
            if !findings.is_empty() {
                out.push_str(&format!("<ul>{findings}</ul>\n"));
            }
        }
    }

    if !ov.modules.is_empty() {
        let mut modules = ov.modules.clone();
        modules.sort_by_key(|b| std::cmp::Reverse(b.node_count));
        out.push_str("<h2>Modules</h2>\n<ul>\n");
        for module in modules.iter().take(15) {
            out.push_str(&format!(
                "<li><code>{}</code>: {} nodes</li>\n",
                map_html_escape(&module.module),
                module.node_count
            ));
        }
        out.push_str("</ul>\n");
    }

    if let Some(focus) = &report.focus {
        out.push_str(&format!(
            "<h2>Focus: {} [{}]</h2>\n<ul>\n",
            map_html_escape(&focus.node_label),
            map_html_escape(&focus.node_kind)
        ));
        out.push_str(&format!(
            "<li>id: <code>{}</code></li>\n",
            map_html_escape(&focus.node_id)
        ));
        out.push_str(&format!("<li>degree: {}</li>\n", focus.degree));
        if let Some(community_id) = focus.community_id {
            out.push_str(&format!("<li>community: {community_id}</li>\n"));
        }
        out.push_str(&format!("<li>neighbors: {}</li>\n", focus.neighbor_count));
        out.push_str("</ul>\n");
        let findings = map_findings_html_list(&focus.findings);
        if !findings.is_empty() {
            out.push_str(&format!("<ul>{findings}</ul>\n"));
        }
    }

    out.push_str("</body>\n</html>\n");
    out
}

/// Render attached findings as `<li class="finding">` rows (no wrapping
/// `<ul>` — the caller wraps so hub/community/focus contexts control nesting).
fn map_findings_html_list(findings: &[GraphDbMapFindingRef]) -> String {
    let mut out = String::new();
    for finding in findings {
        let confidence = finding
            .confidence
            .map(|value| format!(" · confidence {value}"))
            .unwrap_or_default();
        out.push_str(&format!(
            "<li class=\"finding\">📌 {}: {} (about <code>{}</code>{})</li>",
            map_html_escape(&finding.kind),
            map_html_escape(&finding.title),
            map_html_escape(&finding.about),
            confidence
        ));
    }
    out
}

pub(crate) fn cmd_graph_db(
    path: &Path,
    scope: Option<&str>,
    backend: GraphDbBackend,
    convex_snapshot: Option<&Path>,
    query: GraphDbQuery,
    format: OutputFormat,
) -> Result<()> {
    let root = lint::resolve_project_root_or_canonical_path(path)?;
    match &query {
        GraphDbQuery::Refresh => {
            return cmd_graph_db_refresh(&root, path, scope, format);
        }
        GraphDbQuery::Status => {
            return cmd_graph_db_status(&root, scope, format);
        }
        GraphDbQuery::Doctor => {
            return cmd_graph_db_doctor(&root, scope, backend, convex_snapshot, format);
        }
        GraphDbQuery::Drift => {
            return cmd_graph_db_drift(&root, path, scope, convex_snapshot, format);
        }
        GraphDbQuery::Compact {
            apply,
            prune_tombstones,
            confirmed_convex_reconciled,
        } => {
            return cmd_graph_db_compact(
                &root,
                scope,
                *apply,
                *prune_tombstones,
                *confirmed_convex_reconciled,
                format,
            );
        }
        GraphDbQuery::BackendEval {
            candidates,
            targets,
            full_projection,
        } => {
            return cmd_graph_db_backend_eval(
                GraphDbBackendEvalOptions {
                    path,
                    scope,
                    candidates,
                    targets,
                    full_projection: *full_projection,
                },
                format,
            );
        }
        _ => {}
    }
    let graph_db = graph_substrate_db_path(&root, scope);
    let mut warnings = Vec::new();
    if matches!(
        backend,
        GraphDbBackend::Sqlite | GraphDbBackend::ConvexSnapshot
    ) && let GraphDbQuery::Evidence { target, .. } = &query
    {
        let needs_refresh = if graph_db.exists() {
            let store = SqliteGraphStore::open_read_only_resilient(&graph_db)?;
            sqlite_graph_freshness(&store, scope.unwrap_or("root"))?.fail_closed
                || graph_db_resolve_evidence_target(&store, target)?.is_none()
        } else {
            true
        };
        if needs_refresh {
            let (graph, _refresh) =
                write_traversal_graph_store_with_options(&root, path, scope, true)?;
            warnings = graph.warnings;
        }
    }
    let report = match backend {
        GraphDbBackend::Sqlite => {
            let store = SqliteGraphStore::open_read_only_resilient(&graph_db)?;
            if let Some(recovery) = store.read_only_recovery() {
                warnings.push(graph_db_read_recovery_diagnostic(recovery));
            }
            let freshness = sqlite_graph_freshness(&store, scope.unwrap_or("root"))?;
            if let GraphDbQuery::Evidence {
                target,
                depth,
                limit,
                cursor,
            } = &query
            {
                let report = graph_db_evidence_report_from_store(GraphDbEvidenceInput {
                    root: &root,
                    scope,
                    backend: "sqlite",
                    target,
                    depth: *depth,
                    limit: *limit,
                    cursor: cursor.as_deref(),
                    store: &store,
                    freshness,
                    warnings,
                })?;
                return print_graph_db_evidence_report(&report, format);
            }
            if let GraphDbQuery::Map {
                focus,
                top_hubs,
                community_limit,
                focus_depth,
                format: map_format,
            } = &query
            {
                return cmd_graph_db_map(
                    &root,
                    scope,
                    "sqlite",
                    &store,
                    focus.as_deref(),
                    *top_hubs,
                    *community_limit,
                    *focus_depth,
                    format,
                    *map_format,
                    warnings,
                );
            }
            graph_db_report_from_store(&root, scope, "sqlite", query, &store, freshness, warnings)?
        }
        GraphDbBackend::ConvexSnapshot => {
            let snapshot_path = convex_snapshot
                .context("--backend convex-snapshot requires --convex-snapshot <rows.json>")?;
            let local_store = SqliteGraphStore::open_read_only_resilient(&graph_db)?;
            if let Some(recovery) = local_store.read_only_recovery() {
                warnings.push(graph_db_read_recovery_diagnostic(recovery));
            }
            let local = convex_rows_from_graph_store(&local_store)?;
            let snapshot = load_convex_projection_rows(snapshot_path)?;
            validate_convex_projection_rows(&snapshot)?;
            let freshness = convex_graph_freshness(&local, &snapshot, scope);
            let client = ConvexRowsGraphClient::from_rows(snapshot);
            let store = SubstrateConvexGraphStore::new(client);
            if let GraphDbQuery::Evidence {
                target,
                depth,
                limit,
                cursor,
            } = &query
            {
                let report = graph_db_evidence_report_from_store(GraphDbEvidenceInput {
                    root: &root,
                    scope,
                    backend: "convex-snapshot",
                    target,
                    depth: *depth,
                    limit: *limit,
                    cursor: cursor.as_deref(),
                    store: &store,
                    freshness,
                    warnings,
                })?;
                return print_graph_db_evidence_report(&report, format);
            }
            if let GraphDbQuery::Map {
                focus,
                top_hubs,
                community_limit,
                focus_depth,
                format: map_format,
            } = &query
            {
                return cmd_graph_db_map(
                    &root,
                    scope,
                    "convex-snapshot",
                    &store,
                    focus.as_deref(),
                    *top_hubs,
                    *community_limit,
                    *focus_depth,
                    format,
                    *map_format,
                    warnings,
                );
            }
            graph_db_report_from_store(
                &root,
                scope,
                "convex-snapshot",
                query,
                &store,
                freshness,
                warnings,
            )?
        }
        GraphDbBackend::Tokensave => {
            let store = TokensaveDb::discover(&root)?.with_context(|| {
                format!(
                    "--backend tokensave requires {}",
                    root.join(".tokensave").join("tokensave.db").display()
                )
            })?;
            let freshness = tokensave_graph_freshness(&store)?;
            if let GraphDbQuery::Evidence { .. } = &query {
                bail!(
                    "graph-db evidence is not supported for --backend tokensave; use node, kind, edges, incident, neighborhood, or path queries"
                );
            }
            graph_db_report_from_store(
                &root,
                scope,
                "tokensave",
                query,
                &store,
                freshness,
                warnings,
            )?
        }
    };

    if format.json_output {
        let mut metrics = vec![
            envelope_metric("backend", &report.backend),
            envelope_metric(
                "nodes",
                report.nodes.len() + usize::from(report.node.is_some()),
            ),
            envelope_metric("edges", report.edges.len()),
            envelope_metric("freshness", &report.freshness.status),
        ];
        let mut next_commands = Vec::new();
        if let Some(readiness) = &report.readiness {
            metrics.push(envelope_metric("readiness", &readiness.status));
            next_commands.extend(readiness.next_commands.clone());
        }
        next_commands.push(format!(
            "Use tsift convex-sync {} --json to inspect or refresh Convex projection rows",
            shell_quote(root.to_string_lossy().as_ref())
        ));
        print_json_or_envelope(
            &report,
            &format,
            "graph-db",
            "query",
            ToolEnvelopeSummary {
                text: format!(
                    "Graph DB {} query returned {} node(s), {} edge(s), freshness {}",
                    report.backend,
                    report.nodes.len() + usize::from(report.node.is_some()),
                    report.edges.len(),
                    report.freshness.status
                ),
                metrics,
            },
            false,
            next_commands,
        )
    } else {
        print_graph_db_human(&report, format.compact);
        Ok(())
    }
}

fn status_index_needs_auto_fix(report: &status::StatusReport) -> bool {
    match &report.index {
        status::IndexStatus::Fresh { .. } => false,
        status::IndexStatus::Stale { recovery: None, .. } => true,
        status::IndexStatus::Stale { recovery: Some(_), .. } => false,
        status::IndexStatus::Missing { .. } => true,
    }
}

pub(crate) struct StatusCommandOptions {
    pub fix: bool,
    pub no_fix: bool,
    pub json_output: bool,
    pub compact: bool,
    pub pretty: bool,
    pub terse: bool,
    pub schema: bool,
}

pub(crate) fn cmd_status(
    path: &std::path::Path,
    options: StatusCommandOptions,
) -> Result<()> {
    if options.fix {
        eprintln!("warning: --fix is deprecated; auto-fix is now the default. Use --no-fix to skip.");
    }
    let auto_fix = !options.no_fix;
    let root = lint::resolve_project_root_or_canonical_path(path)?;
    let status_cache = status::StatusCheckCache::new();
    let mut report = status::check_status_with_cache(&root, &status_cache)?;
    if status_missing_workspace_scopes(&report) {
        autoindex_missing_workspace_scopes(&root, &report)?;
        status_cache.invalidate_all();
        report = status::check_status_with_cache(&root, &status_cache)?;
    }
    if auto_fix && status_index_needs_auto_fix(&report) {
        apply_status_fixes(&root, &report)?;
        status_cache.invalidate_all();
        report = status::check_status_with_cache(&root, &status_cache)?;
        if status_missing_workspace_scopes(&report) {
            autoindex_missing_workspace_scopes(&root, &report)?;
            status_cache.invalidate_all();
            report = status::check_status_with_cache(&root, &status_cache)?;
        }
    }
    if options.json_output {
        println!(
            "{}",
            to_json_schema(&report, options.pretty, options.terse, false, options.schema)?
        );
    } else {
        print!("{}", status::format_human(&report, options.compact));
    }
    Ok(())
}

pub(crate) fn cmd_locks(
    path: &std::path::Path,
    scope: Option<&str>,
    json_output: bool,
    compact: bool,
    pretty: bool,
    terse: bool,
    schema: bool,
) -> Result<()> {
    let root = lint::resolve_project_root_or_canonical_path(path)?;
    let report = status::check_locks(&root, Some(path), scope)?;
    if json_output {
        println!("{}", to_json_schema(&report, pretty, terse, false, schema)?);
    } else {
        print!("{}", status::format_locks_human(&report, compact));
    }
    Ok(())
}

pub(crate) fn cmd_init(
    path: &std::path::Path,
    codex: bool,
    opencode: bool,
    workspace: bool,
) -> Result<()> {
    let resolved = if workspace {
        init::resolve_workspace_dir(path)?
    } else {
        init::resolve_project_dir(path)?
    };
    if resolved != path {
        println!("resolved: {}{}", path.display(), resolved.display());
    }
    let codex_workspace = codex && (workspace || init::has_submodules(&resolved)?);
    let result = init::init_with_integrations(&resolved, codex, codex_workspace, opencode)?;
    for update in result.updates {
        println!(
            "{}: {} ({})",
            update.file.display(),
            update.action,
            match update.action {
                init::InitAction::Created => "tsift Code Navigation section added",
                init::InitAction::Updated => "tsift Code Navigation section updated to latest",
                init::InitAction::AlreadyPresent => "no changes needed",
            }
        );
    }
    if result.gitignore_added {
        println!(".gitignore: added .tsift/");
    }
    if let Some(codex_result) = &result.codex_hooks {
        let scope_label = match codex_result.scope {
            init::CodexHookScope::Project => "project",
            init::CodexHookScope::Workspace => "workspace",
        };
        match codex_result.action {
            init::CodexHookAction::Added => {
                println!(
                    ".codex/hooks.json: tsift {} auto-reindex hook added",
                    scope_label
                );
            }
            init::CodexHookAction::Updated => {
                println!(
                    ".codex/hooks.json: tsift {} auto-reindex hook updated",
                    scope_label
                );
            }
            init::CodexHookAction::AlreadyPresent => {
                println!(
                    ".codex/hooks.json: tsift {} hook already present",
                    scope_label
                );
            }
            init::CodexHookAction::Created => {
                println!(
                    ".codex/hooks.json: created with tsift {} auto-reindex hook",
                    scope_label
                );
            }
        }
    }
    if let Some(opencode_commands) = &result.opencode_commands {
        for update in opencode_commands {
            println!(
                "{}: {} (OpenCode /{} tsift shortcut)",
                update.file.display(),
                update.action,
                update.command_name
            );
        }
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn cmd_sql(
    db_path: &std::path::Path,
    query: Option<String>,
    table: Option<String>,
    json_output: bool,
    compact: bool,
    pretty: bool,
    terse: bool,
    schema: bool,
) -> Result<()> {
    let conn = open_db(db_path)?;

    match (query, table) {
        (Some(sql), _) => {
            let (columns, rows) = execute_query(&conn, &sql)?;
            if json_output {
                let json_rows: Vec<serde_json::Value> = rows
                    .iter()
                    .map(|row| {
                        let obj: serde_json::Map<String, serde_json::Value> = columns
                            .iter()
                            .zip(row.iter())
                            .map(|(k, v)| (k.clone(), v.clone()))
                            .collect();
                        serde_json::Value::Object(obj)
                    })
                    .collect();
                println!("{}", to_json_schema(&json_rows, pretty, terse, false, schema)?);
            } else if compact {
                println!("rows:{} cols:{}", rows.len(), columns.len());
                for row in &rows {
                    let cells: Vec<String> = row
                        .iter()
                        .map(|v| match v {
                            serde_json::Value::Null => "NULL".to_string(),
                            serde_json::Value::String(s) => truncate_for_compact(s, 40),
                            other => other.to_string(),
                        })
                        .collect();
                    println!("  {}", cells.join(" | "));
                }
            } else {
                // Tabular output
                if columns.is_empty() {
                    println!("Query returned no columns.");
                    return Ok(());
                }
                // Header
                println!("{}", columns.join(" | "));
                println!(
                    "{}",
                    columns
                        .iter()
                        .map(|c| "-".repeat(c.len().max(4)))
                        .collect::<Vec<_>>()
                        .join("-+-")
                );
                for row in &rows {
                    let cells: Vec<String> = row
                        .iter()
                        .map(|v| match v {
                            serde_json::Value::Null => "NULL".to_string(),
                            serde_json::Value::String(s) => s.clone(),
                            other => other.to_string(),
                        })
                        .collect();
                    println!("{}", cells.join(" | "));
                }
                println!("\n{} row(s)", rows.len());
            }
        }
        (None, Some(tbl)) => {
            let cols = table_columns(&conn, &tbl)?;
            if cols.is_empty() {
                bail!("table '{}' not found or has no columns", tbl);
            }
            if json_output {
                println!("{}", to_json_schema(&cols, pretty, terse, false, schema)?);
            } else if compact {
                println!("table:{} columns:{}", tbl, cols.len());
                for col in &cols {
                    println!("  {} {}", col.name, col.col_type);
                }
            } else {
                println!("Table: {}", tbl);
                println!("{:<20} {:<12} {:<8} PK", "Column", "Type", "NotNull");
                println!("{}", "-".repeat(50));
                for col in &cols {
                    println!(
                        "{:<20} {:<12} {:<8} {}",
                        col.name,
                        col.col_type,
                        col.notnull,
                        if col.pk { "PK" } else { "" }
                    );
                }
            }
        }
        (None, None) => {
            let tables = schema_overview(&conn)?;
            if json_output {
                println!("{}", to_json_schema(&tables, pretty, terse, false, schema)?);
            } else if compact {
                println!("tables:{}", tables.len());
                for tbl in &tables {
                    println!(
                        "  {} rows:{} cols:{}",
                        tbl.name,
                        tbl.row_count,
                        tbl.columns.len()
                    );
                }
            } else {
                println!("Database: {}", db_path.display());
                println!("{} table(s)\n", tables.len());
                for tbl in &tables {
                    println!("  {} ({} rows)", tbl.name, tbl.row_count);
                    for col in &tbl.columns {
                        let flags = [
                            if col.pk { "PK" } else { "" },
                            if col.notnull { "NOT NULL" } else { "" },
                        ]
                        .iter()
                        .filter(|s| !s.is_empty())
                        .cloned()
                        .collect::<Vec<_>>()
                        .join(", ");
                        let suffix = if flags.is_empty() {
                            String::new()
                        } else {
                            format!(" [{}]", flags)
                        };
                        println!("    {} {}{}", col.name, col.col_type, suffix);
                    }
                    println!();
                }
            }
        }
    }
    Ok(())
}

/// Exit codes for `tsift rewrite` (matches rtk protocol):
///   0 + stdout → rewrite found, auto-allow
///   1 + stderr → no tsift equivalent, pass through
pub(crate) fn cmd_rewrite(command: &str, run: bool, format: OutputFormat) -> Result<()> {
    let rewritten = match rewrite_command(command) {
        Some(rewritten) => rewritten,
        None => {
            eprintln!("{}", no_rewrite_message(command, run));
            std::process::exit(1);
        }
    };
    let rewritten = apply_rewrite_output_format(&rewritten, format);

    if !run {
        print!("{}", rewritten);
        return Ok(());
    }

    let status_code = execute_rewritten_command(&rewritten)?;
    std::process::exit(status_code);
}