proa_source_tools 0.1.1

Source formatting, linting, and inspection engines for Proa
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
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! `proa rsjs`: inspection tools for rsjs compiler artifacts.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize)]
pub struct StrategyExplanation {
    pub source: StrategySource,
    pub strategy_mode: String,
    pub render_strategy: String,
    /// Whether this component may originate browser behavior. Absent only for
    /// legacy strategy sidecars written before the permission field existed.
    pub client_origin_permission: Option<String>,
    pub hydration_strategy: Option<ExplainedHydrationStrategy>,
    pub declared_client_modules: Vec<String>,
    pub client_modules: Vec<String>,
    pub unused_client_modules: Vec<String>,
    pub component_fragment_imports: Vec<String>,
    pub runtime_features: Vec<String>,
    pub shared_state_dependencies: Vec<StrategySharedStateDependency>,
    pub server_surfaces: Vec<ExplainedServerSurface>,
    pub crossings: Vec<ExplainedClientCrossing>,
    pub dead_primitives: Vec<ExplainedDeadPrimitive>,
    pub summary: String,
    pub roots: Vec<ExplainedRoot>,
    pub emitted: ExplainedEmission,
    pub next_actions: Vec<String>,
    pub sidecar_path: String,
    /// Set when the anchored source no longer matches the sidecar's input
    /// fingerprint (or cannot be found), meaning this analysis is outdated.
    pub staleness: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct StrategySource {
    pub analysis_id: String,
    pub runtime_asset_id: Option<String>,
    pub struct_name: String,
    pub struct_path: String,
    pub input_fingerprint: Option<String>,
    pub source_anchor: Option<StrategySourceAnchor>,
}

#[derive(Debug, Clone, Serialize)]
pub struct StrategySourceAnchor {
    pub file: Option<String>,
    pub impl_start_line: Option<usize>,
    pub impl_start_column: Option<usize>,
    pub render_start_line: Option<usize>,
    pub render_start_column: Option<usize>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExplainedRoot {
    pub kind: String,
    pub detail: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExplainedServerSurface {
    pub kind: String,
    pub source: Option<String>,
    pub source_anchor: Option<StrategyServerSurfaceAnchor>,
    pub detail: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExplainedClientCrossing {
    pub kind: String,
    pub label: Option<String>,
    pub module: Option<String>,
    pub name: Option<String>,
    pub arg_index: Option<u64>,
    pub asset_id: Option<String>,
    pub prop: Option<String>,
    pub root: Option<u64>,
    pub child_signal: Option<u64>,
    pub parent_signal: Option<u64>,
    pub signal: Option<u64>,
    pub query: Option<u64>,
    pub mutation: Option<u64>,
    pub invalidation_index: Option<u64>,
    pub node: Option<u64>,
    pub attr: Option<String>,
    pub surface: Option<String>,
    pub requirement: Option<String>,
    pub source_anchor: Option<StrategyClientCrossingAnchor>,
    pub detail: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExplainedDeadPrimitive {
    pub kind: String,
    pub label: Option<String>,
    pub signal: Option<u64>,
    pub handler: Option<u64>,
    pub node_ref: Option<u64>,
    pub query: Option<u64>,
    pub mutation: Option<u64>,
    pub source_anchor: Option<StrategyDeadPrimitiveAnchor>,
    pub detail: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExplainedHydrationStrategy {
    pub kind: String,
    pub label: String,
    pub ignored: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct ExplainedEmission {
    pub wrapper: bool,
    pub seed_script: bool,
    pub manifest_registration: bool,
    pub js_asset: bool,
    pub component_fragment_asset: bool,
    pub uses_queries: bool,
    pub uses_mutations: bool,
    pub uses_shared_state: bool,
}

#[derive(Debug, Clone)]
pub struct StrategySidecarSummary {
    pub analysis_id: String,
    pub runtime_asset_id: Option<String>,
    pub struct_name: String,
    pub struct_path: String,
    pub input_fingerprint: Option<String>,
    pub source_anchor: Option<StrategySourceAnchor>,
    pub render_strategy: String,
    pub client_origin_permission: Option<String>,
    pub hydration_strategy_kind: Option<String>,
    pub hydration_strategy_label: Option<String>,
    pub declared_client_modules: Vec<String>,
    pub client_modules: Vec<String>,
    pub unused_client_modules: Vec<String>,
    pub component_fragment_imports: Vec<String>,
    pub runtime_features: Vec<String>,
    pub shared_state_dependencies: Vec<StrategySharedStateDependency>,
    pub server_surfaces: Vec<StrategyServerSurfaceSummary>,
    pub crossings: Vec<StrategyClientCrossingSummary>,
    pub dead_primitives: Vec<StrategyDeadPrimitiveSummary>,
    pub component_fragment_mounts: Vec<StrategyFragmentMountSummary>,
    pub sidecar_path: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StrategyFragmentMountSummary {
    pub root: u64,
    pub asset_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrategySharedStateDependency {
    pub store: u64,
    pub rust_type: String,
    pub type_key: Option<String>,
    /// Fields this island actually subscribes to.
    pub fields: Vec<String>,
    /// Every field serialized by the provider snapshot, including fields
    /// this island never reads.
    pub serialized_fields: Vec<String>,
    pub provider_serializes_full_snapshot: bool,
    pub source_anchor: Option<StrategyClientCrossingAnchor>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StrategyServerSurfaceSummary {
    pub kind: String,
    pub source: Option<String>,
    pub node: Option<u64>,
    pub attr: Option<String>,
    pub reason: Option<String>,
    pub source_anchor: Option<StrategyServerSurfaceAnchor>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrategyServerSurfaceAnchor {
    pub start_line: usize,
    pub start_column: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrategyClientCrossingSummary {
    pub kind: String,
    pub label: Option<String>,
    pub module: Option<String>,
    pub name: Option<String>,
    pub arg_index: Option<u64>,
    pub asset_id: Option<String>,
    pub prop: Option<String>,
    pub root: Option<u64>,
    pub child_signal: Option<u64>,
    pub parent_signal: Option<u64>,
    pub signal: Option<u64>,
    pub query: Option<u64>,
    pub mutation: Option<u64>,
    pub invalidation_index: Option<u64>,
    pub node: Option<u64>,
    pub attr: Option<String>,
    pub surface: Option<String>,
    pub requirement: Option<String>,
    pub source_anchor: Option<StrategyClientCrossingAnchor>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StrategyDeadPrimitiveSummary {
    pub kind: String,
    pub label: Option<String>,
    pub signal: Option<u64>,
    pub handler: Option<u64>,
    pub node_ref: Option<u64>,
    pub query: Option<u64>,
    pub mutation: Option<u64>,
    pub source_anchor: Option<StrategyDeadPrimitiveAnchor>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CrossingReport {
    pub root: String,
    pub selector: Option<String>,
    pub sidecar_count: usize,
    pub crossing_count: usize,
    pub crossings: Vec<CrossingReportEntry>,
}

#[derive(Debug, Clone, Serialize)]
pub struct CrossingReportEntry {
    pub sidecar_path: String,
    pub analysis_id: String,
    pub runtime_asset_id: Option<String>,
    pub struct_name: String,
    pub struct_path: String,
    pub render_strategy: String,
    #[serde(flatten)]
    pub crossing: StrategyClientCrossingSummary,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrategyClientCrossingAnchor {
    pub start_line: usize,
    pub start_column: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrategyDeadPrimitiveAnchor {
    pub start_line: usize,
    pub start_column: usize,
}

#[derive(Debug, Deserialize)]
struct StrategySidecar {
    format: String,
    strategy_version: u64,
    strategy_mode: String,
    analysis_id: String,
    #[serde(default)]
    runtime_asset_id: Option<String>,
    struct_name: String,
    struct_path: String,
    #[serde(default)]
    input_fingerprint: Option<String>,
    #[serde(default)]
    source: Option<StrategySourceSidecar>,
    render_strategy: String,
    #[serde(default)]
    client_origin_permission: Option<String>,
    #[serde(default)]
    hydration_strategy: Option<HydrationStrategySidecar>,
    #[serde(default)]
    declared_client_modules: Vec<String>,
    #[serde(default)]
    client_modules: Vec<String>,
    #[serde(default)]
    component_fragment_imports: Vec<String>,
    #[serde(default)]
    runtime_features: Vec<String>,
    #[serde(default)]
    shared_state_dependencies: Vec<SharedStateDependencySidecar>,
    #[serde(default)]
    server_surfaces: Vec<ServerSurfaceSidecar>,
    #[serde(default)]
    crossings: Vec<ClientCrossingSidecar>,
    #[serde(default)]
    dead_primitives: Vec<DeadPrimitiveSidecar>,
    #[serde(default)]
    roots: Vec<StrategyRoot>,
    #[serde(default)]
    emitted: EmittedSidecar,
}

#[derive(Debug, Clone, Deserialize)]
struct StrategySourceSidecar {
    #[serde(default)]
    file: Option<String>,
    #[serde(default)]
    impl_start_line: Option<usize>,
    #[serde(default)]
    impl_start_column: Option<usize>,
    #[serde(default)]
    render_start_line: Option<usize>,
    #[serde(default)]
    render_start_column: Option<usize>,
}

#[derive(Debug, Clone, Deserialize)]
struct SharedStateDependencySidecar {
    store: u64,
    rust_type: String,
    #[serde(default)]
    type_key: Option<String>,
    #[serde(default)]
    fields: Vec<String>,
    #[serde(default)]
    serialized_fields: Vec<String>,
    #[serde(default)]
    provider_serializes_full_snapshot: bool,
    #[serde(default)]
    source: Option<ClientCrossingAnchorSidecar>,
}

#[derive(Debug, Clone, Deserialize)]
struct HydrationStrategySidecar {
    kind: String,
    #[serde(default)]
    label: Option<String>,
    #[serde(default)]
    event: Option<String>,
    #[serde(default)]
    query: Option<String>,
}

#[derive(Debug, Deserialize)]
struct StrategyRoot {
    kind: String,
    #[serde(default)]
    node: Option<u64>,
    #[serde(default)]
    parent: Option<u64>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    module: Option<String>,
    #[serde(default)]
    event: Option<String>,
    #[serde(default)]
    dom_event: Option<String>,
    #[serde(default)]
    node_ref: Option<u64>,
    #[serde(default)]
    id: Option<u64>,
    #[serde(default)]
    index: Option<u64>,
    #[serde(default)]
    container: Option<u64>,
    #[serde(default)]
    root: Option<u64>,
    #[serde(default)]
    asset_id: Option<String>,
    #[serde(default)]
    read: Option<String>,
    #[serde(default)]
    effect: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ServerSurfaceSidecar {
    kind: String,
    #[serde(default)]
    source: Option<String>,
    #[serde(default)]
    node: Option<u64>,
    #[serde(default)]
    attr: Option<String>,
    #[serde(default)]
    reason: Option<String>,
    #[serde(default)]
    source_anchor: Option<ServerSurfaceAnchorSidecar>,
}

#[derive(Debug, Clone, Deserialize)]
struct ServerSurfaceAnchorSidecar {
    start_line: usize,
    start_column: usize,
}

#[derive(Debug, Deserialize)]
struct ClientCrossingSidecar {
    kind: String,
    #[serde(default)]
    label: Option<String>,
    #[serde(default)]
    module: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    arg_index: Option<u64>,
    #[serde(default)]
    asset_id: Option<String>,
    #[serde(default)]
    prop: Option<String>,
    #[serde(default)]
    root: Option<u64>,
    #[serde(default)]
    child_signal: Option<u64>,
    #[serde(default)]
    parent_signal: Option<u64>,
    #[serde(default)]
    signal: Option<u64>,
    #[serde(default)]
    query: Option<u64>,
    #[serde(default)]
    mutation: Option<u64>,
    #[serde(default)]
    invalidation_index: Option<u64>,
    #[serde(default)]
    node: Option<u64>,
    #[serde(default)]
    attr: Option<String>,
    #[serde(default)]
    surface: Option<String>,
    #[serde(default)]
    requirement: Option<String>,
    #[serde(default)]
    source_anchor: Option<ClientCrossingAnchorSidecar>,
}

#[derive(Debug, Clone, Deserialize)]
struct ClientCrossingAnchorSidecar {
    start_line: usize,
    start_column: usize,
}

#[derive(Debug, Deserialize)]
struct DeadPrimitiveSidecar {
    kind: String,
    #[serde(default)]
    label: Option<String>,
    #[serde(default)]
    signal: Option<u64>,
    #[serde(default)]
    handler: Option<u64>,
    #[serde(default)]
    node_ref: Option<u64>,
    #[serde(default)]
    query: Option<u64>,
    #[serde(default)]
    mutation: Option<u64>,
    #[serde(default)]
    source_anchor: Option<DeadPrimitiveAnchorSidecar>,
}

#[derive(Debug, Clone, Deserialize)]
struct DeadPrimitiveAnchorSidecar {
    start_line: usize,
    start_column: usize,
}

#[derive(Debug, Default, Deserialize)]
struct EmittedSidecar {
    #[serde(default)]
    wrapper: bool,
    #[serde(default)]
    seed_script: bool,
    #[serde(default)]
    manifest_registration: bool,
    #[serde(default)]
    js_asset: bool,
    #[serde(default)]
    component_fragment_asset: bool,
    #[serde(default)]
    uses_queries: bool,
    #[serde(default)]
    uses_mutations: bool,
    #[serde(default)]
    uses_shared_state: bool,
}

pub fn explain_strategy(root: &Path, selector: &str) -> Result<StrategyExplanation> {
    let sidecar_path = find_strategy_sidecar(root, selector)?;
    let text = std::fs::read_to_string(&sidecar_path)
        .with_context(|| format!("read {}", sidecar_path.display()))?;
    let sidecar: StrategySidecar =
        serde_json::from_str(&text).with_context(|| format!("parse {}", sidecar_path.display()))?;
    validate_sidecar(&sidecar, &sidecar_path)?;
    let staleness = sidecar_staleness(root, &sidecar);

    let roots: Vec<_> = sidecar.roots.iter().map(explain_root).collect();
    let render_strategy = human_strategy(&sidecar.render_strategy).to_string();
    let hydration_strategy = sidecar.hydration_strategy.as_ref().map(|strategy| {
        explain_hydration_strategy(strategy, sidecar.render_strategy == "server_only")
    });
    let declared_client_modules = sidecar.declared_client_modules.clone();
    let client_modules = sidecar.client_modules.clone();
    let unused_client_modules = unused_client_modules(&declared_client_modules, &client_modules);
    let component_fragment_imports = sidecar_component_fragment_imports(&sidecar);
    let runtime_features = sidecar_runtime_features(&sidecar);
    let shared_state_dependencies = sidecar
        .shared_state_dependencies
        .iter()
        .map(strategy_shared_state_dependency)
        .collect();
    let server_surfaces: Vec<_> = sidecar
        .server_surfaces
        .iter()
        .map(explain_server_surface)
        .collect();
    let crossings: Vec<_> = sidecar
        .crossings
        .iter()
        .map(explain_client_crossing)
        .collect();
    let dead_primitives: Vec<_> = sidecar
        .dead_primitives
        .iter()
        .map(explain_dead_primitive)
        .collect();
    let summary = if sidecar.render_strategy == "server_only" {
        let mut summary = format!(
            "{} is ServerOnly: no reachable client roots were recorded, so no island JS should be emitted.",
            sidecar.struct_name
        );
        if !declared_client_modules.is_empty() {
            summary.push_str(
                " Client helper grouping metadata is unreachable here; grouping never forces hydration.",
            );
        }
        if !dead_primitives.is_empty() {
            summary.push_str(
                " Declared client primitives are dead here; declarations alone do not force hydration.",
            );
        }
        summary
    } else {
        format!(
            "{} is {}: {} reachable client root(s) require browser JS.",
            sidecar.struct_name,
            render_strategy,
            roots.len()
        )
    };
    let mut next_actions = if sidecar.render_strategy == "server_only" {
        let mut actions = vec![
            "keep #[rsjs(component)] when this type must remain compiler-composable; use an undecorated render trait only for intentionally opaque SSR"
                .to_string(),
        ];
        if sidecar.client_origin_permission.as_deref() == Some("participant_only") {
            actions.push(
                "change to #[rsjs(component, client)] before adding a local signal, handler, query, mutation, shared-state acquisition, or browser capability"
                    .to_string(),
            );
        } else {
            actions.push(
                "add a reachable signal/event/bind/query/ref primitive if browser behavior is intended"
                    .to_string(),
            );
        }
        if hydration_strategy
            .as_ref()
            .is_some_and(|strategy| strategy.ignored)
        {
            actions.push(
                "remove the `strategy = ...` argument because no browser hydration is emitted"
                    .to_string(),
            );
        }
        if !declared_client_modules.is_empty() {
            actions.push(
                "remove obsolete component helper allowlists; annotate each portable origin-free helper once with #[rsjs::client] and call it from reachable Client IR if browser computation is intended"
                    .to_string(),
            );
        }
        if !dead_primitives.is_empty() {
            actions.push(
                "remove dead client primitive declarations or connect them to a reachable template binding/handler"
                    .to_string(),
            );
        }
        actions
    } else {
        vec![
            "review reachable roots to confirm each browser dependency is intentional".to_string(),
            "move opaque SSR-only work behind rsjs::server_only(...) or rsjs::server_attr(...)"
                .to_string(),
        ]
    };
    next_actions.sort();
    next_actions.dedup();

    Ok(StrategyExplanation {
        source: StrategySource {
            analysis_id: sidecar.analysis_id,
            runtime_asset_id: sidecar.runtime_asset_id,
            struct_name: sidecar.struct_name,
            struct_path: sidecar.struct_path,
            input_fingerprint: sidecar.input_fingerprint,
            source_anchor: sidecar.source.map(strategy_source_anchor),
        },
        strategy_mode: sidecar.strategy_mode,
        render_strategy,
        client_origin_permission: sidecar.client_origin_permission,
        hydration_strategy,
        declared_client_modules,
        client_modules,
        unused_client_modules,
        component_fragment_imports,
        runtime_features,
        shared_state_dependencies,
        server_surfaces,
        crossings,
        dead_primitives,
        summary,
        roots,
        emitted: ExplainedEmission {
            wrapper: sidecar.emitted.wrapper,
            seed_script: sidecar.emitted.seed_script,
            manifest_registration: sidecar.emitted.manifest_registration,
            js_asset: sidecar.emitted.js_asset,
            component_fragment_asset: sidecar.emitted.component_fragment_asset,
            uses_queries: sidecar.emitted.uses_queries,
            uses_mutations: sidecar.emitted.uses_mutations,
            uses_shared_state: sidecar.emitted.uses_shared_state,
        },
        next_actions,
        sidecar_path: sidecar_path.display().to_string(),
        staleness,
    })
}

/// Compare the sidecar's recorded input fingerprint against the current
/// source of the anchored file. A mismatch means the analysis predates the
/// latest edit and should not be presented as ground truth.
fn sidecar_staleness(root: &Path, sidecar: &StrategySidecar) -> Option<String> {
    let fingerprint = sidecar.input_fingerprint.as_deref()?;
    let anchor = sidecar.source.as_ref()?;
    let file = anchor.file.as_deref()?;

    let direct = PathBuf::from(file);
    let source_path = if direct.is_absolute() {
        direct
    } else {
        let joined = root.join(file);
        if joined.exists() {
            joined
        } else {
            direct
        }
    };
    if !source_path.exists() {
        return Some(format!(
            "anchored source file `{file}` was not found; re-run the build to refresh target/rsjs/strategy"
        ));
    }

    let source = std::fs::read_to_string(&source_path).ok()?;
    let parsed = syn::parse_file(&source).ok()?;
    let mut fingerprints = Vec::new();
    rsjs_impl_fingerprints(&parsed.items, &sidecar.struct_name, &mut fingerprints);
    if fingerprints.is_empty() {
        return Some(format!(
            "no `#[rsjs(component)]` or `#[rsjs(component, client)]` render impl for `{}` found in `{file}`; re-run the build to refresh target/rsjs/strategy",
            sidecar.struct_name
        ));
    }
    if fingerprints.iter().any(|current| current == fingerprint) {
        return None;
    }
    Some(format!(
        "`{file}` changed since this sidecar was written; re-run the build to refresh target/rsjs/strategy"
    ))
}

fn rsjs_impl_fingerprints(items: &[syn::Item], struct_name: &str, out: &mut Vec<String>) {
    for item in items {
        match item {
            syn::Item::Impl(item_impl) => {
                let is_rsjs = item_impl.attrs.iter().any(|attr| {
                    attr.path()
                        .segments
                        .last()
                        .is_some_and(|segment| segment.ident == "rsjs")
                });
                if !is_rsjs {
                    continue;
                }
                let matches_struct = match item_impl.self_ty.as_ref() {
                    syn::Type::Path(path) => path
                        .path
                        .segments
                        .last()
                        .is_some_and(|segment| segment.ident == struct_name),
                    _ => false,
                };
                if matches_struct {
                    if let Some(fingerprint) =
                        crate::lint::rsjs_input_fingerprint_for_impl(item_impl)
                    {
                        out.push(fingerprint);
                    }
                }
            }
            syn::Item::Mod(module) => {
                if let Some((_, items)) = &module.content {
                    rsjs_impl_fingerprints(items, struct_name, out);
                }
            }
            _ => {}
        }
    }
}

pub fn strategy_sidecar_summaries(root: &Path) -> Vec<StrategySidecarSummary> {
    let mut summaries = Vec::new();
    for dir in strategy_dirs(root) {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
                continue;
            }
            if let Ok(summary) = read_strategy_sidecar_summary(&path) {
                summaries.push(summary);
            }
        }
    }
    summaries.sort_by(|left, right| left.sidecar_path.cmp(&right.sidecar_path));
    summaries
}

pub fn crossing_report(root: &Path, selector: Option<&str>) -> Result<CrossingReport> {
    let summaries = if let Some(selector) = selector {
        vec![read_strategy_sidecar_summary(&find_strategy_sidecar(
            root, selector,
        )?)?]
    } else {
        strategy_sidecar_summaries(root)
    };
    let sidecar_count = summaries.len();
    let mut crossings = Vec::new();
    for summary in summaries {
        for crossing in summary.crossings {
            crossings.push(CrossingReportEntry {
                sidecar_path: summary.sidecar_path.display().to_string(),
                analysis_id: summary.analysis_id.clone(),
                runtime_asset_id: summary.runtime_asset_id.clone(),
                struct_name: summary.struct_name.clone(),
                struct_path: summary.struct_path.clone(),
                render_strategy: summary.render_strategy.clone(),
                crossing,
            });
        }
    }
    let crossing_count = crossings.len();
    Ok(CrossingReport {
        root: root.display().to_string(),
        selector: selector.map(ToString::to_string),
        sidecar_count,
        crossing_count,
        crossings,
    })
}

fn read_strategy_sidecar_summary(path: &Path) -> Result<StrategySidecarSummary> {
    let text = std::fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;
    let sidecar: StrategySidecar =
        serde_json::from_str(&text).with_context(|| format!("parse {}", path.display()))?;
    validate_sidecar(&sidecar, path)?;
    Ok(strategy_sidecar_summary(sidecar, path.to_path_buf()))
}

fn strategy_sidecar_summary(sidecar: StrategySidecar, path: PathBuf) -> StrategySidecarSummary {
    let runtime_features = sidecar_runtime_features(&sidecar);
    let component_fragment_imports = sidecar_component_fragment_imports(&sidecar);
    let shared_state_dependencies = sidecar
        .shared_state_dependencies
        .iter()
        .map(strategy_shared_state_dependency)
        .collect();
    let unused_client_modules =
        unused_client_modules(&sidecar.declared_client_modules, &sidecar.client_modules);
    let server_surfaces = sidecar
        .server_surfaces
        .iter()
        .map(strategy_server_surface_summary)
        .collect();
    let crossings = sidecar
        .crossings
        .iter()
        .map(strategy_client_crossing_summary)
        .collect();
    let dead_primitives = sidecar
        .dead_primitives
        .iter()
        .map(strategy_dead_primitive_summary)
        .collect();
    let component_fragment_mounts = sidecar
        .roots
        .iter()
        .filter_map(strategy_fragment_mount_summary)
        .collect();
    StrategySidecarSummary {
        analysis_id: sidecar.analysis_id,
        runtime_asset_id: sidecar.runtime_asset_id,
        struct_name: sidecar.struct_name,
        struct_path: sidecar.struct_path,
        input_fingerprint: sidecar.input_fingerprint,
        source_anchor: sidecar.source.map(strategy_source_anchor),
        render_strategy: sidecar.render_strategy,
        client_origin_permission: sidecar.client_origin_permission,
        hydration_strategy_kind: sidecar
            .hydration_strategy
            .as_ref()
            .map(|strategy| strategy.kind.clone()),
        hydration_strategy_label: sidecar
            .hydration_strategy
            .as_ref()
            .map(hydration_strategy_label),
        declared_client_modules: sidecar.declared_client_modules,
        client_modules: sidecar.client_modules,
        unused_client_modules,
        component_fragment_imports,
        runtime_features,
        shared_state_dependencies,
        server_surfaces,
        crossings,
        dead_primitives,
        component_fragment_mounts,
        sidecar_path: path,
    }
}

fn strategy_fragment_mount_summary(root: &StrategyRoot) -> Option<StrategyFragmentMountSummary> {
    if root.kind != "component_fragment_mount" {
        return None;
    }
    Some(StrategyFragmentMountSummary {
        root: root.root.unwrap_or_default(),
        asset_id: root
            .asset_id
            .clone()
            .unwrap_or_else(|| "<unknown>".to_string()),
    })
}

pub fn print_strategy_explanation(explanation: &StrategyExplanation, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string_pretty(explanation)?);
        return Ok(());
    }

    if let Some(staleness) = &explanation.staleness {
        eprintln!("warning: {staleness}");
    }
    println!("RSJS strategy: {}", explanation.render_strategy);
    println!("component: {}", explanation.source.struct_path);
    println!(
        "client origin permission: {}",
        explanation
            .client_origin_permission
            .as_deref()
            .unwrap_or("unknown (legacy sidecar)")
    );
    if let Some(anchor) = &explanation.source.source_anchor {
        match (
            anchor.file.as_deref(),
            anchor.impl_start_line,
            anchor.impl_start_column,
        ) {
            (Some(file), Some(line), Some(column)) => println!("source: {file}:{line}:{column}"),
            (None, Some(line), Some(column)) => println!("source: {line}:{column}"),
            _ => {}
        }
    }
    println!("analysis id: {}", explanation.source.analysis_id);
    if let Some(fingerprint) = &explanation.source.input_fingerprint {
        println!("input fingerprint: {fingerprint}");
    }
    match &explanation.source.runtime_asset_id {
        Some(id) => println!("runtime asset id: {id}"),
        None => println!("runtime asset id: none"),
    }
    if let Some(strategy) = &explanation.hydration_strategy {
        if strategy.ignored {
            println!(
                "hydration strategy: {} (ignored; ServerOnly emitted no JS)",
                strategy.label
            );
        } else {
            println!("hydration strategy: {}", strategy.label);
        }
    }
    println!("mode: {}", explanation.strategy_mode);
    println!("{}", explanation.summary);
    if explanation.declared_client_modules.is_empty() {
        println!("declared client modules: none");
    } else {
        println!("declared client modules:");
        for module in &explanation.declared_client_modules {
            println!("- {module}");
        }
    }
    if explanation.client_modules.is_empty() {
        println!("client modules: none");
    } else {
        println!("client modules:");
        for module in &explanation.client_modules {
            println!("- {module}");
        }
    }
    if !explanation.unused_client_modules.is_empty() {
        println!("unused client module declarations:");
        for module in &explanation.unused_client_modules {
            println!("- {module}");
        }
    }
    if explanation.component_fragment_imports.is_empty() {
        println!("component fragments: none");
    } else {
        println!("component fragments:");
        for asset_id in &explanation.component_fragment_imports {
            println!("- {asset_id}");
        }
    }
    if explanation.runtime_features.is_empty() {
        println!("runtime features: none");
    } else {
        println!("runtime features:");
        for feature in &explanation.runtime_features {
            println!("- {feature}");
        }
    }
    if explanation.shared_state_dependencies.is_empty() {
        println!("shared state: none");
    } else {
        println!("shared state:");
        for dependency in &explanation.shared_state_dependencies {
            let used_fields = if dependency.fields.is_empty() {
                "<no reachable fields>".to_string()
            } else {
                dependency.fields.join(", ")
            };
            // The macro cannot resolve a shared state's Rust type (see
            // `Analyzer::shared_field_client_type`), so the sidecar records the
            // store's Rust type as written and the fields this island reads.
            // The provider's full serialized field list is a property of the
            // type and is only known to the running provider.
            let serialized_fields = if dependency.serialized_fields.is_empty() {
                "<resolved by the provider at run time>".to_string()
            } else {
                dependency.serialized_fields.join(", ")
            };
            let identity = dependency
                .type_key
                .as_deref()
                .unwrap_or(&dependency.rust_type);
            if let Some(anchor) = &dependency.source_anchor {
                println!(
                    "- store {} {} @ {}:{}: used [{}]; provider serializes [{}]",
                    dependency.store,
                    identity,
                    anchor.start_line,
                    anchor.start_column,
                    used_fields,
                    serialized_fields
                );
            } else {
                println!(
                    "- store {} {}: used [{}]; provider serializes [{}]",
                    dependency.store, identity, used_fields, serialized_fields
                );
            }
        }
    }
    if explanation.server_surfaces.is_empty() {
        println!("server surfaces: none");
    } else {
        println!("server surfaces:");
        for surface in &explanation.server_surfaces {
            if let Some(anchor) = &surface.source_anchor {
                println!(
                    "- {} @ {}:{}: {}",
                    surface.kind, anchor.start_line, anchor.start_column, surface.detail
                );
            } else {
                println!("- {}: {}", surface.kind, surface.detail);
            }
        }
    }
    if explanation.crossings.is_empty() {
        println!("client crossings: none");
    } else {
        println!("client crossings:");
        for crossing in &explanation.crossings {
            if let Some(anchor) = &crossing.source_anchor {
                println!(
                    "- {} @ {}:{}: {}",
                    crossing.kind, anchor.start_line, anchor.start_column, crossing.detail
                );
            } else {
                println!("- {}: {}", crossing.kind, crossing.detail);
            }
        }
    }
    if explanation.dead_primitives.is_empty() {
        println!("dead client primitives: none");
    } else {
        println!("dead client primitives:");
        for primitive in &explanation.dead_primitives {
            if let Some(anchor) = &primitive.source_anchor {
                println!(
                    "- {} @ {}:{}: {}",
                    primitive.kind, anchor.start_line, anchor.start_column, primitive.detail
                );
            } else {
                println!("- {}: {}", primitive.kind, primitive.detail);
            }
        }
    }

    if explanation.roots.is_empty() {
        println!("why: no reachable client roots");
    } else {
        println!("why:");
        for root in &explanation.roots {
            println!("- {}: {}", root.kind, root.detail);
        }
    }

    println!("emitted:");
    println!("- wrapper: {}", yes_no(explanation.emitted.wrapper));
    println!("- seed script: {}", yes_no(explanation.emitted.seed_script));
    println!(
        "- manifest registration: {}",
        yes_no(explanation.emitted.manifest_registration)
    );
    println!("- JS asset: {}", yes_no(explanation.emitted.js_asset));
    println!(
        "- component fragment asset: {}",
        yes_no(explanation.emitted.component_fragment_asset)
    );

    println!("next actions:");
    for action in &explanation.next_actions {
        println!("- {action}");
    }

    Ok(())
}

pub fn print_crossing_report(report: &CrossingReport, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string_pretty(report)?);
        return Ok(());
    }

    println!("RSJS client crossings: {}", report.crossing_count);
    println!("strategy sidecars: {}", report.sidecar_count);
    if let Some(selector) = &report.selector {
        println!("selector: {selector}");
    }
    if report.crossings.is_empty() {
        println!("client crossings: none");
        return Ok(());
    }
    for crossing in &report.crossings {
        let label = crossing.crossing.label.as_deref().unwrap_or("<unlabeled>");
        let surface = crossing
            .crossing
            .surface
            .as_deref()
            .unwrap_or("browser_state");
        let requirement = crossing
            .crossing
            .requirement
            .as_deref()
            .unwrap_or("client_encode");
        if let Some(anchor) = &crossing.crossing.source_anchor {
            println!(
                "- {} {} `{label}` -> `{surface}` ({requirement}) @ {}:{}",
                crossing.struct_path,
                crossing.crossing.kind,
                anchor.start_line,
                anchor.start_column
            );
        } else {
            println!(
                "- {} {} `{label}` -> `{surface}` ({requirement})",
                crossing.struct_path, crossing.crossing.kind
            );
        }
    }
    Ok(())
}

fn find_strategy_sidecar(root: &Path, selector: &str) -> Result<PathBuf> {
    let direct = PathBuf::from(selector);
    if direct.exists() {
        return Ok(direct);
    }

    let mut candidates = Vec::new();
    for dir in strategy_dirs(root) {
        let exact = dir.join(format!("{selector}.json"));
        if exact.exists() {
            return Ok(exact);
        }
        candidates.push(dir);
    }

    let mut matches: Vec<(PathBuf, String, String)> = Vec::new();
    for dir in candidates {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
                continue;
            }
            let Ok(text) = std::fs::read_to_string(&path) else {
                continue;
            };
            let Ok(sidecar) = serde_json::from_str::<StrategySidecar>(&text) else {
                continue;
            };
            if sidecar_matches(&sidecar, selector)
                && !matches
                    .iter()
                    .any(|(_, analysis_id, _)| *analysis_id == sidecar.analysis_id)
            {
                matches.push((path, sidecar.analysis_id, sidecar.struct_path));
            }
        }
    }

    match matches.len() {
        0 => bail!(
            "could not find rsjs strategy sidecar for `{selector}` under {}",
            root.display()
        ),
        1 => Ok(matches.remove(0).0),
        _ => {
            let mut listing = String::new();
            for (path, analysis_id, struct_path) in &matches {
                listing.push_str(&format!(
                    "\n  {struct_path} (analysis id {analysis_id}, {})",
                    path.display()
                ));
            }
            bail!(
                "selector `{selector}` matches {} rsjs strategy sidecars; disambiguate with the struct path or analysis id:{listing}",
                matches.len()
            )
        }
    }
}

fn strategy_dirs(root: &Path) -> Vec<PathBuf> {
    let root_local = root.join("target").join("rsjs").join("strategy");
    if let Ok(current) = std::env::current_dir() {
        let same_workspace = current == root || current.starts_with(root);
        if !same_workspace && root_local.exists() {
            // An explicit foreign workspace (including isolated test
            // fixtures) must not be contaminated by this process's own
            // CARGO_TARGET_DIR or current working directory artifacts.
            return vec![root_local];
        }
    }

    let mut dirs = Vec::new();
    if let Ok(target) = std::env::var("CARGO_TARGET_DIR") {
        dirs.push(PathBuf::from(target).join("rsjs").join("strategy"));
    }
    dirs.push(root_local);
    if let Some(parent) = root.parent() {
        dirs.push(parent.join("target").join("rsjs").join("strategy"));
    }
    if let Ok(current) = std::env::current_dir() {
        dirs.push(current.join("target").join("rsjs").join("strategy"));
    }

    let mut seen = BTreeSet::new();
    dirs.into_iter()
        .filter(|dir| seen.insert(dir.clone()))
        .collect()
}

fn validate_sidecar(sidecar: &StrategySidecar, path: &Path) -> Result<()> {
    if sidecar.format != "rsjs.strategy" {
        bail!(
            "{} is not an rsjs strategy sidecar (format={:?})",
            path.display(),
            sidecar.format
        );
    }
    if sidecar.strategy_version != 1 {
        bail!(
            "{} uses unsupported rsjs strategy_version {}; this CLI supports 1",
            path.display(),
            sidecar.strategy_version
        );
    }
    Ok(())
}

fn sidecar_matches(sidecar: &StrategySidecar, selector: &str) -> bool {
    sidecar.format == "rsjs.strategy"
        && (sidecar.analysis_id == selector
            || sidecar.runtime_asset_id.as_deref() == Some(selector)
            || sidecar.struct_name == selector
            || sidecar.struct_path == selector)
}

fn explain_hydration_strategy(
    strategy: &HydrationStrategySidecar,
    server_only: bool,
) -> ExplainedHydrationStrategy {
    let label = hydration_strategy_label(strategy);
    ExplainedHydrationStrategy {
        kind: strategy.kind.clone(),
        ignored: server_only && strategy.kind != "immediate",
        label,
    }
}

fn hydration_strategy_label(strategy: &HydrationStrategySidecar) -> String {
    if let Some(label) = &strategy.label {
        return label.clone();
    }
    match strategy.kind.as_str() {
        "interaction" => strategy
            .event
            .as_ref()
            .map(|event| format!("interaction:{event}"))
            .unwrap_or_else(|| "interaction".to_string()),
        "media" => strategy
            .query
            .as_ref()
            .map(|query| format!("media:{query}"))
            .unwrap_or_else(|| "media:<unknown>".to_string()),
        other => other.to_string(),
    }
}

fn sidecar_runtime_features(sidecar: &StrategySidecar) -> Vec<String> {
    let mut features = if sidecar.runtime_features.is_empty() {
        let mut derived = Vec::new();
        if sidecar.emitted.uses_queries {
            derived.push("query".to_string());
        }
        if sidecar.emitted.uses_mutations {
            derived.push("mutation".to_string());
        }
        if sidecar.emitted.uses_shared_state {
            derived.push("shared_state".to_string());
        }
        derived
    } else {
        sidecar.runtime_features.clone()
    };

    let mut seen = BTreeSet::new();
    features.retain(|feature| seen.insert(feature.clone()));
    features
}

/// Sidecar `rust_type` strings are recorded from proc-macro token streams,
/// whose Display form inserts spaces between every token: `state::WorkbenchState`
/// arrives as "state :: WorkbenchState" and `Vec<Item>` as "Vec < Item >".
/// Collapse that spacing back to the canonical source spelling so `explain`
/// prints what the author wrote and the same store type spelled through the
/// same path compares equal across islands in `--json` output. A space is
/// kept only where removing it would glue two word tokens together
/// (`dyn Trait`, `impl Trait`).
fn normalize_rust_type_tokens(raw: &str) -> String {
    fn is_word_char(ch: char) -> bool {
        ch.is_alphanumeric() || ch == '_'
    }

    let mut out = String::with_capacity(raw.len());
    let mut chars = raw.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch.is_whitespace() {
            while chars.peek().is_some_and(|next| next.is_whitespace()) {
                chars.next();
            }
            let previous = out.chars().next_back();
            let next = chars.peek().copied();
            if let (Some(previous), Some(next)) = (previous, next) {
                if is_word_char(previous) && is_word_char(next) {
                    out.push(' ');
                }
            }
            continue;
        }
        out.push(ch);
    }
    out
}

fn strategy_shared_state_dependency(
    dependency: &SharedStateDependencySidecar,
) -> StrategySharedStateDependency {
    StrategySharedStateDependency {
        store: dependency.store,
        rust_type: normalize_rust_type_tokens(&dependency.rust_type),
        type_key: dependency.type_key.clone(),
        fields: dependency.fields.clone(),
        serialized_fields: dependency.serialized_fields.clone(),
        provider_serializes_full_snapshot: dependency.provider_serializes_full_snapshot,
        source_anchor: dependency
            .source
            .as_ref()
            .map(|anchor| StrategyClientCrossingAnchor {
                start_line: anchor.start_line,
                start_column: anchor.start_column,
            }),
    }
}

fn sidecar_component_fragment_imports(sidecar: &StrategySidecar) -> Vec<String> {
    let mut imports = sidecar.component_fragment_imports.clone();
    for root in &sidecar.roots {
        if root.kind != "component_fragment_mount" {
            continue;
        }
        if let Some(asset_id) = &root.asset_id {
            imports.push(asset_id.clone());
        }
    }

    imports.sort();
    imports.dedup();
    imports
}

fn unused_client_modules(declared: &[String], client_modules: &[String]) -> Vec<String> {
    let shipped: BTreeSet<_> = client_modules.iter().map(String::as_str).collect();
    let mut seen = BTreeSet::new();
    let mut unused = Vec::new();
    for module in declared {
        if !shipped.contains(module.as_str()) && seen.insert(module.as_str()) {
            unused.push(module.clone());
        }
    }
    unused
}

fn explain_root(root: &StrategyRoot) -> ExplainedRoot {
    ExplainedRoot {
        kind: root.kind.clone(),
        detail: root_detail(root),
    }
}

fn explain_server_surface(surface: &ServerSurfaceSidecar) -> ExplainedServerSurface {
    ExplainedServerSurface {
        kind: surface.kind.clone(),
        source: surface.source.clone(),
        source_anchor: surface.source_anchor.as_ref().map(strategy_surface_anchor),
        detail: server_surface_detail(surface),
    }
}

fn explain_client_crossing(crossing: &ClientCrossingSidecar) -> ExplainedClientCrossing {
    ExplainedClientCrossing {
        kind: crossing.kind.clone(),
        label: crossing.label.clone(),
        module: crossing.module.clone(),
        name: crossing.name.clone(),
        arg_index: crossing.arg_index,
        asset_id: crossing.asset_id.clone(),
        prop: crossing.prop.clone(),
        root: crossing.root,
        child_signal: crossing.child_signal,
        parent_signal: crossing.parent_signal,
        signal: crossing.signal,
        query: crossing.query,
        mutation: crossing.mutation,
        invalidation_index: crossing.invalidation_index,
        node: crossing.node,
        attr: crossing.attr.clone(),
        surface: crossing.surface.clone(),
        requirement: crossing.requirement.clone(),
        source_anchor: crossing
            .source_anchor
            .as_ref()
            .map(strategy_client_crossing_anchor),
        detail: client_crossing_detail(crossing),
    }
}

fn explain_dead_primitive(primitive: &DeadPrimitiveSidecar) -> ExplainedDeadPrimitive {
    ExplainedDeadPrimitive {
        kind: primitive.kind.clone(),
        label: primitive.label.clone(),
        signal: primitive.signal,
        handler: primitive.handler,
        node_ref: primitive.node_ref,
        query: primitive.query,
        mutation: primitive.mutation,
        source_anchor: primitive
            .source_anchor
            .as_ref()
            .map(strategy_dead_primitive_anchor),
        detail: dead_primitive_detail(primitive),
    }
}

fn strategy_server_surface_summary(surface: &ServerSurfaceSidecar) -> StrategyServerSurfaceSummary {
    StrategyServerSurfaceSummary {
        kind: surface.kind.clone(),
        source: surface.source.clone(),
        node: surface.node,
        attr: surface.attr.clone(),
        reason: surface.reason.clone(),
        source_anchor: surface.source_anchor.as_ref().map(strategy_surface_anchor),
    }
}

fn strategy_client_crossing_summary(
    crossing: &ClientCrossingSidecar,
) -> StrategyClientCrossingSummary {
    StrategyClientCrossingSummary {
        kind: crossing.kind.clone(),
        label: crossing.label.clone(),
        module: crossing.module.clone(),
        name: crossing.name.clone(),
        arg_index: crossing.arg_index,
        asset_id: crossing.asset_id.clone(),
        prop: crossing.prop.clone(),
        root: crossing.root,
        child_signal: crossing.child_signal,
        parent_signal: crossing.parent_signal,
        signal: crossing.signal,
        query: crossing.query,
        mutation: crossing.mutation,
        invalidation_index: crossing.invalidation_index,
        node: crossing.node,
        attr: crossing.attr.clone(),
        surface: crossing.surface.clone(),
        requirement: crossing.requirement.clone(),
        source_anchor: crossing
            .source_anchor
            .as_ref()
            .map(strategy_client_crossing_anchor),
    }
}

fn strategy_dead_primitive_summary(
    primitive: &DeadPrimitiveSidecar,
) -> StrategyDeadPrimitiveSummary {
    StrategyDeadPrimitiveSummary {
        kind: primitive.kind.clone(),
        label: primitive.label.clone(),
        signal: primitive.signal,
        handler: primitive.handler,
        node_ref: primitive.node_ref,
        query: primitive.query,
        mutation: primitive.mutation,
        source_anchor: primitive
            .source_anchor
            .as_ref()
            .map(strategy_dead_primitive_anchor),
    }
}

fn strategy_surface_anchor(anchor: &ServerSurfaceAnchorSidecar) -> StrategyServerSurfaceAnchor {
    StrategyServerSurfaceAnchor {
        start_line: anchor.start_line,
        start_column: anchor.start_column,
    }
}

fn strategy_client_crossing_anchor(
    anchor: &ClientCrossingAnchorSidecar,
) -> StrategyClientCrossingAnchor {
    StrategyClientCrossingAnchor {
        start_line: anchor.start_line,
        start_column: anchor.start_column,
    }
}

fn strategy_dead_primitive_anchor(
    anchor: &DeadPrimitiveAnchorSidecar,
) -> StrategyDeadPrimitiveAnchor {
    StrategyDeadPrimitiveAnchor {
        start_line: anchor.start_line,
        start_column: anchor.start_column,
    }
}

// proa-perf-ok(scope): terminal strategy diagnostics format plain text outside the render path
fn server_surface_detail(surface: &ServerSurfaceSidecar) -> String {
    let reason = surface
        .reason
        .as_deref()
        .or_else(|| surface.source.as_deref().map(server_surface_source_label))
        .unwrap_or("server rendered");
    match surface.kind.as_str() {
        "server_attr" => format!(
            "node {} attr `{}` rendered on server ({reason})",
            surface.node.unwrap_or_default(),
            surface.attr.as_deref().unwrap_or("<unknown>")
        ),
        "server_slot" => format!(
            "node {} server-rendered slot ({reason})",
            surface.node.unwrap_or_default()
        ),
        _ => format!(
            "node {} server surface ({reason})",
            surface.node.unwrap_or_default()
        ),
    }
}

fn dead_primitive_detail(primitive: &DeadPrimitiveSidecar) -> String {
    let label = primitive.label.as_deref().unwrap_or("<unlabeled>");
    match primitive.kind.as_str() {
        "signal" => format!(
            "`{label}` signal {} was declared but no reachable client root reads or writes it",
            primitive.signal.unwrap_or_default()
        ),
        "memo" => format!(
            "`{label}` memo signal {} was declared but no reachable client root reads it",
            primitive.signal.unwrap_or_default()
        ),
        "handler" => format!(
            "`{label}` handler {} was declared but no reachable event binding uses it",
            primitive.handler.unwrap_or_default()
        ),
        "node_ref" => format!(
            "`{label}` node_ref {} was declared but no reachable ref binding, handler, or expression uses it",
            primitive.node_ref.unwrap_or_default()
        ),
        "query" => format!(
            "`{label}` query {} was declared but no reachable client root observes, refetches, or invalidates it",
            primitive.query.unwrap_or_default()
        ),
        "mutation" => format!(
            "`{label}` mutation {} was declared but no reachable client root reads or runs it",
            primitive.mutation.unwrap_or_default()
        ),
        kind => format!("`{label}` {kind} declaration was not reachable from client roots"),
    }
}

fn client_crossing_detail(crossing: &ClientCrossingSidecar) -> String {
    client_crossing_summary_detail(&strategy_client_crossing_summary(crossing))
}

// proa-perf-ok(scope): terminal strategy diagnostics format plain text outside the render path
pub(crate) fn client_crossing_summary_detail(crossing: &StrategyClientCrossingSummary) -> String {
    let label = crossing.label.as_deref().unwrap_or("<unlabeled>");
    let surface = crossing.surface.as_deref().unwrap_or("browser_state");
    let requirement = crossing.requirement.as_deref().unwrap_or("client_encode");
    match crossing.kind.as_str() {
        "signal_initial_value" => format!(
            "`{label}` signal initial value crosses to `{surface}` and requires `{requirement}`"
        ),
        "handler_payload" => format!(
            "`{label}` handler payload crosses to `{surface}` on node {} attr `{}` and requires `{requirement}`",
            crossing.node.unwrap_or_default(),
            crossing.attr.as_deref().unwrap_or("<unknown>")
        ),
        "node_payload" => format!(
            "`{label}` node payload crosses to `{surface}` on node {} attr `{}` and requires `{requirement}`",
            crossing.node.unwrap_or_default(),
            crossing.attr.as_deref().unwrap_or("<unknown>")
        ),
        "query_key" => format!(
            "`{label}` query key crosses to `{surface}` for query {} and requires `{requirement}`",
            crossing.query.unwrap_or_default()
        ),
        "query_fetcher" => format!(
            "`{label}` query fetcher config crosses to `{surface}` for query {} and requires `{requirement}`",
            crossing.query.unwrap_or_default()
        ),
        "query_initial_value" => format!(
            "`{label}` query initial value crosses to `{surface}` for query {} and requires `{requirement}`",
            crossing.query.unwrap_or_default()
        ),
        "mutation_key" => format!(
            "`{label}` mutation key crosses to `{surface}` for mutation {} and requires `{requirement}`",
            crossing.mutation.unwrap_or_default()
        ),
        "mutation_runner" => format!(
            "`{label}` mutation runner config crosses to `{surface}` for mutation {} and requires `{requirement}`",
            crossing.mutation.unwrap_or_default()
        ),
        "mutation_invalidation_key" => format!(
            "`{label}` mutation invalidation key {} crosses to `{surface}` for mutation {} and requires `{requirement}`",
            crossing.invalidation_index.unwrap_or_default(),
            crossing.mutation.unwrap_or_default()
        ),
        // Compatibility for older experimental sidecars. Current v2
        // sidecars report client helper calls as roots/import edges; any
        // public data is recorded by the underlying signal/payload/query/
        // mutation/descriptor crossing surface instead.
        "client_helper_argument" => format!(
            "`{label}` client helper argument {} crosses to `{surface}` for `{}::{}` and requires `{requirement}`",
            crossing.arg_index.unwrap_or_default(),
            crossing.module.as_deref().unwrap_or("<unknown>"),
            crossing.name.as_deref().unwrap_or("<unknown>")
        ),
        "descriptor_reactive_prop" => format!(
            "`{label}` descriptor reactive prop `{}` crosses to `{surface}` for fragment `{}` at root {} (child signal {}, parent signal {}) and requires `{requirement}`",
            crossing.prop.as_deref().unwrap_or("<unknown>"),
            crossing.asset_id.as_deref().unwrap_or("<unknown>"),
            crossing.root.unwrap_or_default(),
            crossing.child_signal.unwrap_or_default(),
            crossing.parent_signal.unwrap_or_default()
        ),
        kind => {
            format!("`{label}` crosses to `{surface}` as `{kind}` and requires `{requirement}`")
        }
    }
}

fn server_surface_source_label(source: &str) -> &'static str {
    match source {
        "explicit_boundary" => "explicit SSR boundary",
        "explicit_simple_ssr_boundary" => "explicit wrapper around simple SSR projection",
        "inferred_simple_ssr" => "inferred simple SSR projection",
        "keyed_list_item" => "keyed-list item SSR template",
        "explicit_island_boundary" => "explicit island boundary",
        _ => "server rendered",
    }
}

// proa-perf-ok(scope): terminal strategy diagnostics format plain text outside the render path
fn root_detail(root: &StrategyRoot) -> String {
    match root.kind.as_str() {
        "text_binding" => format_node("text binding", root.node),
        "attr_binding" => format!(
            "node {} dynamic attribute `{}`",
            root.node.unwrap_or_default(),
            root.name.as_deref().unwrap_or("<unknown>")
        ),
        "event_binding" => format!(
            "node {} event `{}` ({})",
            root.node.unwrap_or_default(),
            root.event.as_deref().unwrap_or("<unknown>"),
            root.dom_event.as_deref().unwrap_or("unknown DOM event")
        ),
        "conditional_section" => format_node("conditional section parent", root.parent),
        "input_binding" => format_node("input binding", root.node),
        "node_ref_binding" => format!(
            "node {} bound to node_ref {}",
            root.node.unwrap_or_default(),
            root.node_ref.unwrap_or_default()
        ),
        "node_ref" => format!("node_ref {}", root.id.unwrap_or_default()),
        "signal" => format!("signal {}", root.id.unwrap_or_default()),
        "query" => format!("query {}", root.id.unwrap_or_default()),
        "mutation" => format!("mutation {}", root.id.unwrap_or_default()),
        "browser_read" => format!(
            "browser read `{}`",
            root.read.as_deref().unwrap_or("<unknown>")
        ),
        "browser_effect" => format!(
            "browser effect `{}`",
            root.effect.as_deref().unwrap_or("<unknown>")
        ),
        "client_module_call" => format!(
            "client module `{}` helper `{}`",
            root.module.as_deref().unwrap_or("<unknown>"),
            root.name.as_deref().unwrap_or("<unknown>")
        ),
        "interval" => format!("interval {}", root.index.unwrap_or_default()),
        "keyed_list" => format_node("keyed list container", root.container),
        "component_fragment_mount" => format!(
            "component fragment `{}` mounted at root {} through the schema-v4 parent-mounted fragment path",
            root.asset_id.as_deref().unwrap_or("<unknown>"),
            root.root.unwrap_or_default()
        ),
        _ => "unknown client reachability root".to_string(),
    }
}

fn format_node(label: &str, node: Option<u64>) -> String {
    format!("{label} node {}", node.unwrap_or_default())
}

fn strategy_source_anchor(source: StrategySourceSidecar) -> StrategySourceAnchor {
    StrategySourceAnchor {
        file: source.file,
        impl_start_line: source.impl_start_line,
        impl_start_column: source.impl_start_column,
        render_start_line: source.render_start_line,
        render_start_column: source.render_start_column,
    }
}

fn human_strategy(strategy: &str) -> &str {
    match strategy {
        "server_only" => "ServerOnly",
        "ssr_hydrate" => "SSR + Hydrate",
        "client_only_explicit" => "ClientOnlyExplicit",
        other => other,
    }
}

fn yes_no(value: bool) -> &'static str {
    if value {
        "yes"
    } else {
        "no"
    }
}

#[cfg(test)]
mod tests;