solcompat-core 0.1.0

Compatibility data model and deterministic rule engine for SolCompat
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
use crate::*;
use anyhow::{ensure, Result};
use semver::Version;

fn advice(summary: impl Into<String>) -> Remediation {
    Remediation {
        summary: summary.into(),
        location: "The bound component configuration or manifest; an application source location was not inferred.".into(),
        steps: vec![],
        example: None,
        verification: vec![],
    }
}
fn advice_at(summary: impl Into<String>, location: impl Into<String>) -> Remediation {
    let mut remediation = advice(summary);
    remediation.location = location.into();
    remediation
}

fn base(client: &Client, read: &RpcRead, data: &Dataset) -> CheckResult {
    let record = data.rpc_record();
    CheckResult {
        rule_id: record.rule_id.clone(),
        rule_name: record.rule_name.clone(),
        rule_revision: record.revision,
        subject: format!("{}/{}", client.id, read.id),
        operation: read.method.clone(),
        conditional: false,
        outcome: Outcome::Unknown,
        severity: None,
        title: "RPC transaction read needs additional evidence".into(),
        summary: "The configured RPC read is missing information required for evaluation.".into(),
        explanation: "SC201 evaluates an explicitly configured getBlock or getTransaction read. Missing request options or transaction-version requirements remain unknown and do not claim an incompatibility.".into(),
        evidence: client.evidence.clone(),
        sources: record.sources.clone(),
        remediation: Some(advice("Complete this RPC read contract in solcompat.toml.")),
        suppression: None,
    }
}

fn unknown(result: &mut CheckResult, summary: &str, action: &str) {
    result.summary = summary.into();
    result.remediation = Some(advice(action));
}

fn evaluate(client: &Client, read: &RpcRead, data: &Dataset) -> CheckResult {
    let mut result = base(client, read, data);
    let record = data.rpc_record();
    // An explicit empty requirement declares no transaction-read capability to check.
    if client
        .required_read_versions
        .as_ref()
        .is_some_and(Vec::is_empty)
    {
        result.outcome = Outcome::NotApplicable;
        result.title = "No transaction-read requirement declared".into();
        result.summary = "The client explicitly declares no required transaction formats.".into();
        result.remediation = None;
        return result;
    }
    if !record.methods.contains(&read.method) {
        unknown(
            &mut result,
            "This method has no reviewed RPC acceptance record.",
            "Select a dataset with a reviewed record for this method.",
        );
        return result;
    }
    if read.method == "getBlock" {
        match read.transaction_details.as_deref() {
            Some(details)
                if record
                    .body_free_block_details
                    .iter()
                    .any(|value| value == details) =>
            {
                result.outcome = Outcome::NotApplicable;
                result.title = "Block read does not return transaction bodies".into();
                result.summary = format!("transactionDetails={details}; the transaction-body version limit is not applicable.");
                result.remediation = None;
                return result;
            }
            Some("full") => {}
            Some(_) => {
                unknown(&mut result, "This block response mode is not reviewed in the selected dataset.", "Use a reviewed response-mode record; do not infer full-transaction requirements for this mode.");
                return result;
            }
            None => {
                unknown(
                    &mut result,
                    "The getBlock transaction_details option is unknown.",
                    "Declare the effective transaction_details value used by this read.",
                );
                return result;
            }
        }
    }
    let Some(required) = &client.required_read_versions else {
        return result;
    };
    if required
        .iter()
        .any(|version| !record.reviewed_versions.contains(version))
    {
        unknown(&mut result, "A required transaction format is outside the reviewed dataset.", "Use a reviewed compatibility record for the required format; do not assume future version support.");
        return result;
    }
    let Some(encoding) = &read.encoding else {
        unknown(
            &mut result,
            "The request encoding is unknown.",
            "Declare the effective encoding used by this RPC read.",
        );
        return result;
    };
    if !record.reviewed_encodings.contains(encoding) {
        unknown(
            &mut result,
            "The request encoding is outside the reviewed dataset.",
            "Select a reviewed record for this encoding.",
        );
        return result;
    }
    let Some(maximum) = &read.max_supported_transaction_version else {
        unknown(&mut result, "The request maximum is unknown; absence of a declaration is not RPC omission.", "Declare the effective maximum, or use the explicit string \"omitted\" if the application omits the RPC option.");
        return result;
    };
    if let RpcMaximum::Version(version) = maximum {
        if !record.reviewed_versions.contains(&format!("v{version}")) {
            unknown(&mut result, "The configured maximum is outside the reviewed dataset.", "Use a reviewed record for this maximum; increasing the number alone does not establish support.");
            return result;
        }
    }
    let highest = required
        .iter()
        .filter_map(|version| version.strip_prefix('v')?.parse::<u8>().ok())
        .max();
    let supported = match (highest, maximum) {
        (None, _) => true,
        (Some(required), RpcMaximum::Version(configured)) => *configured >= required,
        (Some(_), RpcMaximum::Omitted(_)) => false,
    };
    let required_text = highest.map_or_else(|| "legacy".into(), |v| format!("v{v}"));
    let configured = match maximum {
        RpcMaximum::Version(value) => value.to_string(),
        RpcMaximum::Omitted(_) => "omitted (legacy only)".into(),
    };
    result.summary =
        format!("Declared maximum: {configured}; highest required format: {required_text}.");
    if supported {
        result.outcome = Outcome::Pass;
        result.title = "RPC limit accepts the declared transaction versions".into();
        result.explanation = "The declared request limit meets the declared version requirement. This does not establish decoder support or prove the running application uses these options.".into();
        result.remediation = None;
    } else {
        let needed = highest.expect("an unsupported requirement is versioned");
        result.outcome = Outcome::Finding;
        result.severity = Some(Severity::Error);
        result.title = format!("RPC read limit excludes required Transaction V{needed}");
        result.explanation = if read.method == "getBlock" {
            "A full-block read containing a transaction above this maximum can fail as a whole. The configured limit does not filter out newer transactions.".into()
        } else {
            "Reading a transaction above this maximum can fail even if the transaction succeeded on-chain.".into()
        };
        let mut fix = advice(format!("Set maxSupportedTransactionVersion: {needed} in the actual {} request options, after ensuring decoder support.", read.method));
        let source_location = client.evidence.iter().find_map(|evidence| {
            evidence
                .pointer
                .strip_prefix("/source:")
                .map(|line| format!("{} at line {line}", evidence.path))
        });
        fix.location = source_location.clone().unwrap_or_else(|| {
            "The bound client's RPC read options or shared request wrapper; application source location was not detected.".into()
        });
        fix.steps = vec![
            format!("Ensure the bound decoder supports Transaction V{needed}; SC201 checks request acceptance only."),
            format!("Set maxSupportedTransactionVersion to the integer {needed} in this client's {} options or shared wrapper. Preserve its other options.", read.method),
            if source_location.is_some() {
                "Rerun SolCompat to verify the changed source call and decoder version.".into()
            } else {
                "Update the SolCompat declaration to match the changed application request. Editing solcompat.toml alone does not fix the application.".into()
            },
        ];
        fix.example = Some(format!("// Example option to merge into the application's request configuration:\n{{ maxSupportedTransactionVersion: {needed} }}"));
        fix.verification = vec![
            format!("Run the client's typecheck and tests against a known V{needed} response and the other required formats."),
            "Rerun solcompat check with the same input selection. These developer verification steps are not executed by SolCompat.".into(),
        ];
        result.remediation = Some(fix);
    }
    result
}

fn rule_result(
    data: &Dataset,
    id: &str,
    subject: &str,
    operation: &str,
    evidence: Vec<Evidence>,
) -> CheckResult {
    let record = data.rule(id);
    CheckResult {
        rule_id: id.into(),
        rule_name: record.rule_name.clone(),
        rule_revision: record.revision,
        subject: subject.into(),
        operation: operation.into(),
        conditional: false,
        outcome: Outcome::Unknown,
        severity: None,
        title: format!("{} needs additional evidence", record.rule_name),
        summary: record.notes.clone(),
        explanation: record.notes.clone(),
        evidence,
        sources: record.sources.clone(),
        remediation: None,
        suppression: None,
    }
}

fn parsed(value: Option<&String>) -> Option<Version> {
    parse_version(value?)
}
fn parse_version(value: &str) -> Option<Version> {
    let value = value.trim_start_matches('v');
    Version::parse(value)
        .ok()
        .or_else(|| Version::parse(&format!("{value}.0")).ok())
}
fn resolved(program: &Program, name: &str) -> Option<Version> {
    parsed(program.resolved_dependencies.get(name))
}
fn declares(program: &Program, name: &str) -> bool {
    program.dependencies.keys().any(|key| {
        key.rsplit('/')
            .next()
            .is_some_and(|identity| identity == name || identity.ends_with(&format!("=>{name}")))
    })
}

fn declared_requirement<'a>(program: &'a Program, name: &str) -> Option<&'a str> {
    program.dependencies.iter().find_map(|(key, requirement)| {
        key.rsplit('/').next().and_then(|identity| {
            (identity == name || identity.ends_with(&format!("=>{name}")))
                .then_some(requirement.as_str())
        })
    })
}

fn program_base_evidence(program: &Program) -> Vec<Evidence> {
    program
        .evidence
        .iter()
        .filter(|evidence| {
            !evidence.kind.starts_with("pinocchio-") && !evidence.kind.starts_with("anchor-")
        })
        .cloned()
        .collect()
}

fn program_results(program: &Program, data: &Dataset) -> Vec<CheckResult> {
    let mut out = Vec::new();
    let mut rust = rule_result(
        data,
        "SC001",
        &program.id,
        "compile program",
        program_base_evidence(program),
    );
    match (
        program.rust_version.as_deref().and_then(parse_version),
        parsed(program.sbf_rust_version.as_ref()),
    ) {
        (Some(requirement), Some(compiler)) if compiler >= requirement => {
            rust.outcome = Outcome::Pass;
            rust.title = "Selected SBF compiler satisfies the package Rust requirement".into();
            rust.summary =
                format!("Package rust-version {requirement}; selected SBF compiler {compiler}.");
            rust.remediation = None;
        }
        (Some(requirement), Some(compiler)) => {
            rust.outcome = Outcome::Finding;
            rust.severity = Some(Severity::Error);
            rust.title = "Selected SBF compiler does not satisfy rust-version".into();
            rust.summary =
                format!("Package rust-version {requirement}; selected SBF compiler {compiler}.");
            rust.remediation=Some(Remediation{summary:"Select an SBF toolchain whose compiler satisfies the package rust-version, or lower the requirement only after validating the source.".into(),location:program.manifest.clone(),steps:vec![],example:None,verification:vec!["Build the program with the selected SBF toolchain in the explicit build workflow.".into()]});
        }
        (None, _) if program.rust_version.is_none() => {
            rust.outcome = Outcome::NotApplicable;
            rust.title = "No package rust-version requirement declared".into();
            rust.summary = "Cargo does not declare a minimum Rust version for this program.".into();
        }
        _ => {
            rust.summary="A declared rust-version and an explicit SBF compiler version are both required; host rustc is not substituted.".into();
            rust.remediation=Some(advice("Set sbf_rust_version for this program from the selected platform-tools/compiler identity."));
        }
    }
    out.push(rust);

    let mut cli = rule_result(
        data,
        "SC003",
        &program.id,
        "Anchor build/IDL generation",
        program_base_evidence(program),
    );
    if program.framework != "anchor" {
        cli.outcome = Outcome::NotApplicable;
        cli.title = "Program is not identified as Anchor".into();
        cli.summary = format!("Detected framework: {}.", program.framework);
    } else {
        match (
            parsed(program.anchor_cli_version.as_ref()),
            resolved(program, "anchor-lang"),
        ) {
            (Some(a), Some(b)) if (a.major, a.minor) == (b.major, b.minor) => {
                cli.outcome = Outcome::Pass;
                cli.title = "Anchor CLI and anchor-lang release lines align".into();
                cli.summary = format!("Anchor CLI {a}; anchor-lang {b}.");
            }
            (Some(a), Some(b)) => {
                cli.outcome = Outcome::Finding;
                cli.severity = Some(Severity::Warning);
                cli.title = "Anchor CLI differs from Cargo-resolved anchor-lang".into();
                cli.summary = match declared_requirement(program, "anchor-lang") {
                    Some(requirement) => format!(
                        "Cargo accepted anchor-lang {b} for requirement {requirement}; selected Anchor CLI is {a}."
                    ),
                    None => format!("Anchor CLI {a}; resolved anchor-lang {b}."),
                };
                cli.explanation = "The resolved crate satisfies Cargo's declared version range. This establishes dependency-range compatibility, but it does not prove that the selected CLI and resolved framework release are fully aligned or that current source built successfully. Anchor release guidance updates the CLI and framework crates together, so the drift remains advisory.".into();
                let mut remediation = advice_at(
                    "Consider aligning the selected CLI and resolved Anchor packages; keep the current combination if its successful build and generated outputs are intentionally accepted.",
                    &program.manifest,
                );
                remediation.steps = vec![
                    format!("To keep resolved anchor-lang {b}, select Anchor CLI {b} and align the other Anchor packages to that release."),
                    format!("To keep Anchor CLI {a}, intentionally constrain anchor-lang to the intended {a} release, then update Cargo.lock. Use an exact requirement only when strict pinning is intended."),
                    "Run the normal Anchor build, review the regenerated IDL/client output, and rerun SolCompat.".into(),
                ];
                remediation.verification = vec![
                    "Confirm SC003 reports aligned release lines and the project build/tests pass with the selected versions.".into(),
                ];
                cli.remediation = Some(remediation);
            }
            (Some(anchor_cli), None) => {
                cli.title = "Resolved anchor-lang version is unavailable".into();
                cli.summary = match declared_requirement(program, "anchor-lang") {
                    Some(requirement) => format!(
                        "Anchor CLI {anchor_cli} is selected; anchor-lang requirement {requirement} is declared but not resolved."
                    ),
                    None => format!(
                        "Anchor CLI {anchor_cli} is selected; no resolved anchor-lang version is available."
                    ),
                };
                let mut remediation = advice_at(
                    "Resolve the program dependencies, then rerun SolCompat with Cargo.lock or captured cargo metadata.",
                    &program.manifest,
                );
                remediation.steps = vec![
                    "If Cargo.lock is absent, run anchor build for the normal project build, or run cargo generate-lockfile when only dependency resolution is needed.".into(),
                    "Rerun solcompat check --detailed; SolCompat will use an unambiguous crates.io version from Cargo.lock.".into(),
                    "If Cargo.lock contains multiple anchor-lang versions, capture cargo metadata --format-version 1 and pass the JSON file with --cargo-metadata.".into(),
                ];
                remediation.example = Some(
                    "# Full Anchor build:\nanchor build\n\n# Or dependency resolution only:\ncargo generate-lockfile\n\nsolcompat check --detailed"
                        .into(),
                );
                remediation.verification = vec![
                    "Confirm SC003 reports the selected Anchor CLI and the resolved anchor-lang version.".into(),
                ];
                cli.remediation = Some(remediation);
            }
            (None, Some(anchor_lang)) => {
                cli.title = "Anchor CLI version is unavailable".into();
                cli.summary = format!(
                    "anchor-lang {anchor_lang} is resolved; no Anchor CLI version is selected or observed."
                );
                cli.remediation = Some(advice_at(
                    "Set [toolchain].anchor_version in Anchor.toml, configure anchor_cli_version for this program, or use --probe-tools with an installed native Anchor executable.",
                    &program.manifest,
                ));
            }
            (None, None) => {
                cli.title = "Anchor CLI and resolved anchor-lang versions are unavailable".into();
                cli.summary = "SC003 requires a selected or observed Anchor CLI version and a resolved anchor-lang version.".into();
                cli.remediation = Some(advice_at(
                    "Supply the Anchor CLI version and Cargo.lock or captured cargo metadata resolving anchor-lang.",
                    &program.manifest,
                ));
            }
        }
    }
    out.push(cli);

    let mut arch = rule_result(
        data,
        "SC103",
        &program.id,
        "build sBPFv3",
        program_base_evidence(program),
    );
    if program.sbpf_arch.as_deref() != Some("v3") {
        arch.outcome = Outcome::NotApplicable;
        arch.title = "sBPFv3 was not selected".into();
        arch.summary =
            "Set sbpf_arch = \"v3\" only when evaluating an sBPFv3 build profile.".into();
    } else {
        let builder = parsed(program.cargo_build_sbf_version.as_ref());
        let platform = parsed(program.platform_tools_version.as_ref());
        let pin = resolved(program, "pinocchio");
        let syscall = resolved(program, "solana-define-syscall");
        let has_syscall = declares(program, "solana-define-syscall");
        let ok = builder
            .as_ref()
            .is_some_and(|v| *v >= Version::new(4, 2, 0))
            && platform
                .as_ref()
                .is_some_and(|v| *v >= Version::new(1, 53, 0))
            && (program.framework != "pinocchio"
                || pin.as_ref().is_some_and(|v| *v >= Version::new(0, 10, 0)))
            && (!has_syscall
                || syscall
                    .as_ref()
                    .is_some_and(|v| *v >= Version::new(2, 3, 0)));
        if builder.is_none()
            || platform.is_none()
            || (program.framework == "pinocchio" && pin.is_none())
            || (has_syscall && syscall.is_none())
        {
            arch.summary="sBPFv3 requires exact builder/platform versions and resolved framework prerequisites.".into();
            arch.remediation=Some(advice_at("Declare cargo_build_sbf_version and platform_tools_version; provide lock or metadata evidence for relevant crates.", &program.manifest));
        } else if ok {
            arch.outcome = Outcome::Pass;
            arch.title = "Reviewed sBPFv3 build prerequisites are met".into();
            arch.summary = format!(
                "cargo-build-sbf {}; platform-tools {}.",
                builder
                    .as_ref()
                    .map(ToString::to_string)
                    .unwrap_or_default(),
                platform
                    .as_ref()
                    .map(ToString::to_string)
                    .unwrap_or_default()
            );
        } else {
            arch.outcome = Outcome::Finding;
            arch.severity = Some(Severity::Error);
            arch.title = "sBPFv3 build prerequisites are not met".into();
            arch.summary =
                "One or more selected versions are below the reviewed sBPFv3 minimums.".into();
            arch.remediation=Some(advice_at("Use cargo-build-sbf 4.2+, platform-tools 1.53+, Pinocchio 0.10+ when applicable, and solana-define-syscall 2.3+ when present.", &program.manifest));
        }
    }
    out.push(arch);

    let mut crates = rule_result(
        data,
        "SC302",
        &program.id,
        "compile Anchor program",
        program_base_evidence(program),
    );
    if program.framework != "anchor" {
        crates.outcome = Outcome::NotApplicable;
        crates.title = "Program is not identified as Anchor".into();
        crates.summary = format!("Detected framework: {}.", program.framework);
    } else {
        match (
            resolved(program, "anchor-lang"),
            resolved(program, "anchor-spl"),
        ) {
            (Some(a), Some(b)) if (a.major, a.minor) == (b.major, b.minor) => {
                crates.outcome = Outcome::Pass;
                crates.title = "Anchor crate release lines align".into();
                crates.summary = format!("anchor-lang {a}; anchor-spl {b}.");
            }
            (Some(a), Some(b)) => {
                crates.outcome = Outcome::Finding;
                crates.severity = Some(Severity::Warning);
                crates.title = "Anchor crate release lines differ".into();
                crates.summary = format!("anchor-lang {a}; anchor-spl {b}.");
                crates.remediation=Some(advice_at("Align anchor-lang and anchor-spl to the same reviewed release line unless the chosen combination is explicitly supported.", &program.manifest));
            }
            (_, None) => {
                crates.outcome = Outcome::NotApplicable;
                crates.title = "anchor-spl is not a resolved dependency".into();
                crates.summary = "No anchor-spl relationship is present to compare.".into();
            }
            _ => {
                crates.summary =
                    "Resolved Anchor crate versions are required for comparison.".into();
            }
        }
    }
    out.push(crates);
    out
}

fn declared_npm_major_cap(requirement: &str, boundary: u64) -> bool {
    let requirement = requirement.trim();
    if requirement.is_empty()
        || requirement.contains("||")
        || requirement.contains('>')
        || requirement.contains('*')
    {
        return false;
    }
    let constrained = requirement.starts_with('^')
        || requirement.starts_with('~')
        || requirement
            .chars()
            .next()
            .is_some_and(|character| character.is_ascii_digit());
    if !constrained {
        return false;
    }
    requirement
        .trim_start_matches(['^', '~', '=', ' ', 'v'])
        .split('.')
        .next()
        .and_then(|major| major.parse::<u64>().ok())
        .is_some_and(|major| major < boundary)
}

fn has_source_signal(program: &Program, signal: &str) -> bool {
    program
        .evidence
        .iter()
        .any(|evidence| evidence.kind == signal)
}

fn framework_syntax_result(program: &Program, data: &Dataset) -> CheckResult {
    let mut evidence = program
        .evidence
        .iter()
        .filter(|evidence| evidence.kind.starts_with("pinocchio-"))
        .cloned()
        .collect::<Vec<_>>();
    evidence.sort_by(|a, b| (&a.path, &a.pointer).cmp(&(&b.path, &b.pointer)));
    evidence.dedup_by(|a, b| a.path == b.path && a.pointer == b.pointer);
    if evidence.is_empty() {
        evidence = program_base_evidence(program);
    }
    let mut result = rule_result(
        data,
        "SC104",
        &program.id,
        "compile Pinocchio program",
        evidence,
    );
    if program.framework != "pinocchio" {
        result.outcome = Outcome::NotApplicable;
        result.title = "Program is not identified as Pinocchio".into();
        result.summary = format!("Detected framework: {}.", program.framework);
        return result;
    }
    let Some(current) = resolved(program, "pinocchio") else {
        result.title = "Resolved Pinocchio version is unavailable".into();
        result.summary = "Source syntax was inspected, but Cargo.lock does not prove the Pinocchio release line.".into();
        result.remediation = Some(advice_at(
            "Generate Cargo.lock or provide cargo metadata, then rerun SolCompat.",
            &program.manifest,
        ));
        return result;
    };

    let legacy_types = has_source_signal(program, "pinocchio-account-info")
        || has_source_signal(program, "pinocchio-pubkey");
    let immutable_entrypoint = has_source_signal(program, "pinocchio-immutable-entrypoint");
    let mutable_entrypoint = has_source_signal(program, "pinocchio-mutable-entrypoint");
    let source_location = result.evidence.first().map_or_else(
        || program.manifest.clone(),
        |evidence| evidence.path.clone(),
    );
    let v010 = Version::new(0, 10, 0);
    let v011 = Version::new(0, 11, 0);

    if current < v010 {
        if immutable_entrypoint || mutable_entrypoint {
            result.outcome = Outcome::Finding;
            result.severity = Some(Severity::Error);
            result.title = "Pinocchio source expects the 0.10+ AccountView API".into();
            result.summary = format!(
                "Resolved Pinocchio {current}; source uses Address/AccountView entrypoint syntax introduced on the 0.10 release line."
            );
            result.remediation = Some(advice_at(
                "Upgrade Pinocchio and its companion crates to a compatible 0.10+ release, update Cargo.lock, and rebuild.",
                source_location,
            ));
        } else {
            result.outcome = Outcome::Finding;
            result.severity = Some(Severity::Warning);
            result.title = "Pinocchio 0.9 crosses two source API migrations".into();
            result.summary = if legacy_types {
                format!(
                    "Resolved Pinocchio {current}; AccountInfo/Pubkey syntax matches the 0.9 API. Migrating to 0.10 changes these to AccountView/Address, and 0.11 then requires a mutable AccountView slice."
                )
            } else {
                format!(
                    "Resolved Pinocchio {current}; upgrading crosses the 0.10 AccountInfo/Pubkey replacement and the 0.11 mutable AccountView boundary."
                )
            };
            result.remediation = Some(Remediation {
                summary: "Apply the Pinocchio 0.10 type migration before the separate 0.11 mutability migration.".into(),
                location: source_location,
                steps: vec![
                    "For 0.10, replace Pubkey with Address and AccountInfo with AccountView; update key() calls to address() and revise affected account APIs.".into(),
                    "Validate the 0.10 form with accounts: &[AccountView] before crossing the next boundary.".into(),
                    "For 0.11, change the entrypoint to accounts: &mut [AccountView], update mutating account references, and review resize traits/features.".into(),
                ],
                example: Some("0.9: (&Pubkey, &[AccountInfo]) -> 0.10: (&Address, &[AccountView]) -> 0.11: (&Address, &mut [AccountView])".into()),
                verification: vec![
                    "Update Cargo.lock, run solcompat --build at the selected target release, and run tests covering account mutation and CPI paths.".into(),
                ],
            });
        }
    } else if current < v011 {
        if legacy_types {
            result.outcome = Outcome::Finding;
            result.severity = Some(Severity::Error);
            result.title = "Pinocchio 0.10 source still uses the removed 0.9 account types".into();
            result.summary = format!(
                "Resolved Pinocchio {current}; qualified AccountInfo or Pubkey syntax from the 0.9 API was observed."
            );
            result.remediation = Some(Remediation {
                summary: "Migrate the current source to the Pinocchio 0.10 Address and AccountView API.".into(),
                location: source_location,
                steps: vec![
                    "Replace Pubkey with Address and AccountInfo with AccountView.".into(),
                    "Update key() calls to address() and revise borrow, data, owner, and CPI account access as required by the 0.10 API.".into(),
                    "Keep the entrypoint account slice immutable on 0.10; make it mutable only when upgrading to 0.11+.".into(),
                ],
                example: Some("fn process_instruction(program_id: &Address, accounts: &[AccountView], instruction_data: &[u8]) -> ProgramResult".into()),
                verification: vec!["Run solcompat --build with the resolved 0.10 toolchain and the program tests.".into()],
            });
        } else if mutable_entrypoint {
            result.outcome = Outcome::Finding;
            result.severity = Some(Severity::Error);
            result.title = "Pinocchio source expects the 0.11 mutable entrypoint API".into();
            result.summary = format!(
                "Resolved Pinocchio {current}; source uses &mut [AccountView], which belongs to the 0.11+ entrypoint contract."
            );
            result.remediation = Some(advice_at(
                "Upgrade Pinocchio and companion crates to compatible 0.11-era releases, update Cargo.lock, and rebuild.",
                source_location,
            ));
        } else {
            result.outcome = Outcome::Finding;
            result.severity = Some(Severity::Warning);
            result.title = "Pinocchio 0.11 mutable-account migration is required".into();
            result.summary = if immutable_entrypoint {
                format!(
                    "Resolved Pinocchio {current}; the 0.10 Address/AccountView API is present, and 0.11 changes accounts from &[AccountView] to &mut [AccountView]."
                )
            } else {
                format!(
                    "Resolved Pinocchio {current}; source scanning did not prove a complete 0.11 entrypoint migration."
                )
            };
            result.remediation = Some(Remediation {
                summary: "Apply the Pinocchio 0.11 mutable-account and resize migration before upgrading.".into(),
                location: source_location,
                steps: vec![
                    "Change process_instruction to accept &mut [AccountView].".into(),
                    "Update assign, close, try_borrow_mut, and mutating CPI paths to receive mutable account views.".into(),
                    "If the program resizes accounts, select account-resize or unsafe-account-resize deliberately and import the corresponding trait.".into(),
                ],
                example: Some("accounts: &mut [AccountView]".into()),
                verification: vec!["Update Cargo.lock, then run solcompat --build and mutation-focused tests.".into()],
            });
        }
    } else if legacy_types {
        result.outcome = Outcome::Finding;
        result.severity = Some(Severity::Error);
        result.title = "Pinocchio source uses legacy AccountInfo or Pubkey syntax".into();
        result.summary = format!(
            "Resolved Pinocchio {current} uses Address and AccountView, but qualified 0.9-era AccountInfo or Pubkey syntax was observed."
        );
        result.remediation = Some(Remediation {
            summary: "Migrate Pinocchio types to Address and AccountView and update affected account APIs.".into(),
            location: source_location,
            steps: vec![
                "Replace Pinocchio Pubkey usage with Address and key() access with address() where applicable.".into(),
                "Replace Pinocchio AccountInfo parameters with AccountView and update borrow/data access calls.".into(),
                "Use a mutable AccountView slice for the program entrypoint on Pinocchio 0.11+.".into(),
            ],
            example: Some("fn process_instruction(program_id: &Address, accounts: &mut [AccountView], instruction_data: &[u8]) -> ProgramResult".into()),
            verification: vec!["Run solcompat --build and the program test suite.".into()],
        });
    } else if immutable_entrypoint {
        result.outcome = Outcome::Finding;
        result.severity = Some(Severity::Error);
        result.title = "Pinocchio 0.11 entrypoint uses an immutable AccountView slice".into();
        result.summary = format!(
            "Resolved Pinocchio {current}; process_instruction still accepts &[AccountView]."
        );
        result.remediation = Some(Remediation {
            summary: "Change the entrypoint to accept &mut [AccountView] and propagate mutable references only to mutating operations.".into(),
            location: source_location,
            steps: vec![
                "Change the process_instruction accounts parameter to &mut [AccountView].".into(),
                "Update assign, close, try_borrow_mut, and mutating CPI paths to receive mutable account views.".into(),
                "If the program resizes accounts, select account-resize or unsafe-account-resize deliberately and import the corresponding trait.".into(),
            ],
            example: Some("accounts: &mut [AccountView]".into()),
            verification: vec!["Run solcompat --build and tests covering every mutating instruction.".into()],
        });
    } else if mutable_entrypoint {
        result.outcome = Outcome::Pass;
        result.title = "Pinocchio entrypoint syntax matches the resolved 0.11+ API".into();
        result.summary = format!(
            "Resolved Pinocchio {current}; process_instruction accepts &mut [AccountView], and no legacy Pinocchio type imports were observed."
        );
        result.remediation = None;
    }
    result
}

fn decoder_result(client: &Client, data: &Dataset) -> CheckResult {
    let mut r = rule_result(
        data,
        "SC200",
        &client.id,
        "decode transaction responses",
        client.evidence.clone(),
    );
    if client
        .required_read_versions
        .as_ref()
        .is_some_and(|versions| {
            versions
                .iter()
                .any(|value| !matches!(value.as_str(), "legacy" | "v0" | "v1"))
        })
    {
        r.summary = "A required transaction format is outside the reviewed decoder dataset.".into();
        return r;
    }
    if client
        .required_read_versions
        .as_ref()
        .is_some_and(Vec::is_empty)
    {
        r.outcome = Outcome::NotApplicable;
        r.title = "No transaction decoding requirement is in scope".into();
        r.summary = "The client explicitly declares no required transaction formats.".into();
        return r;
    }
    let explicit_v1_requirement = client
        .required_read_versions
        .as_ref()
        .is_some_and(|v| v.iter().any(|x| x == "v1"));
    let v1_accepting_rpc_read = client.rpc_reads.iter().any(|read| {
        matches!(
            read.max_supported_transaction_version,
            Some(RpcMaximum::Version(version)) if version >= 1
        )
    });
    let needs_v1 = explicit_v1_requirement || v1_accepting_rpc_read;
    if !needs_v1 {
        r.outcome = Outcome::NotApplicable;
        r.title = "No Transaction V1 decoding requirement is in scope".into();
        r.summary = "SC200 runs only when the client contract requires V1 reads; no V1 requirement was declared.".into();
        return r;
    }
    let Some(package) = &client.decoder_package else {
        r.summary = "V1 decoding is in scope, but no decoder_package is bound.".into();
        r.remediation = Some(advice(
            "Bind the package that decodes RPC transaction bodies for this client.",
        ));
        return r;
    };
    let Some(version) = client
        .resolved_dependencies
        .get(package)
        .and_then(|value| parse_version(value))
    else {
        let declared = client
            .dependencies
            .iter()
            .find_map(|(identity, requirement)| {
                identity
                    .split_once('/')
                    .filter(|(_, name)| *name == package)
                    .map(|_| requirement.as_str())
            });
        if package == "@solana/kit"
            && declared.is_some_and(|requirement| declared_npm_major_cap(requirement, 8))
        {
            let requirement = declared.expect("checked");
            r.outcome = Outcome::Finding;
            r.severity = Some(Severity::Error);
            r.title = "Declared decoder range excludes reviewed Transaction V1 support".into();
            r.explanation = "The declared major-version constraint cannot select a decoder release in the reviewed V1-capable line.".into();
            r.summary = format!(
                "{package} requirement {requirement} cannot resolve to the reviewed V1-capable 8.x line."
            );
            r.remediation = Some(Remediation {
                summary: "Upgrade @solana/kit to a reviewed 8.x release and regenerate the lockfile.".into(),
                location: client.manifest.clone(),
                steps: vec![
                    "Change the direct @solana/kit dependency to the intended 8.x release range.".into(),
                    "Regenerate the package-manager lockfile and rerun SolCompat so the installed version can also be verified.".into(),
                ],
                example: Some("npm install @solana/kit@^8".into()),
                verification: vec![
                    "Run client tests using known legacy, V0, and V1 responses.".into(),
                ],
            });
            return r;
        }
        r.summary = match declared {
            Some(requirement) => format!(
                "{package} requirement {requirement} does not prove the exact installed decoder version."
            ),
            None => format!("Exact resolved version for {package} is unavailable."),
        };
        r.remediation = Some(advice(
            "Generate the package-manager lockfile so SolCompat can verify the exact installed decoder version.",
        ));
        return r;
    };
    let supported = match package.as_str() {
        "@solana/web3.js" if version.major == 1 && version.minor < 99 => Some(false),
        "@solana/web3.js" if version.major == 1 && version.minor == 99 => Some(true),
        "@solana/web3.js" if version.major == 3 => {
            Some(version >= Version::parse("3.0.0-rc.3").expect("valid boundary"))
        }
        "@solana/kit" if version.major < 8 => Some(false),
        "@solana/kit" if version.major == 8 => Some(true),
        _ => None,
    };
    let reason = if explicit_v1_requirement {
        "the explicit transaction-version contract"
    } else {
        "an RPC read whose maximum accepts V1"
    };
    r.summary = format!("Bound decoder: {package} {version}; V1 is in scope from {reason}.");
    if supported == Some(true) {
        r.outcome = Outcome::Pass;
        r.title = "Resolved decoder has reviewed Transaction V1 read support".into();
    } else if supported == Some(false) {
        r.outcome = Outcome::Finding;
        r.severity = Some(Severity::Error);
        r.title = "Resolved decoder lacks reviewed Transaction V1 read support".into();
        r.remediation=Some(Remediation{summary:"Upgrade the bound decoder to a reviewed V1-capable release.".into(),location:client.manifest.clone(),steps:vec!["For the web3.js 1.x line, use @solana/web3.js 1.99.0 or a reviewed later 1.x release; for an explicit migration, @solana/kit 8.x is reviewed for V1 reads and sends.".into(),"Regenerate the lockfile with the project's package manager and rerun SolCompat.".into()],example:None,verification:vec!["Run client tests using known legacy, V0, and V1 responses.".into()]});
    } else {
        r.summary =
            format!("{package} {version} has no reviewed decoder record for Transaction V1.");
    }
    r
}

fn artifact_result(a: &Artifact, data: &Dataset, target: &AnalysisTarget) -> CheckResult {
    let mut r = rule_result(data, "SC102", &a.id, &a.operation, a.evidence.clone());
    let Some(v) = &a.sbpf_version else {
        r.summary = format!(
            "ELF e_flags={} is not a recognized sBPF version.",
            a.elf_flags
        );
        return r;
    };
    if target.id == "future-sbpfv3-deployment" && a.operation == "execute" {
        r.conditional = true;
        r.outcome = Outcome::NotApplicable;
        r.title = "Scenario does not restrict existing-program execution".into();
        r.summary = format!(
            "Conditional scenario applies to deploy/upgrade/finalize; artifact is sBPF{v}."
        );
        return r;
    }
    let Some(policy) = target
        .artifact_policies
        .iter()
        .find(|policy| policy.loader == a.loader && policy.operations.contains(&a.operation))
    else {
        r.summary = format!(
            "Artifact is sBPF{v}; target {} has no policy for {} with {}.",
            target.id, a.operation, a.loader
        );
        r.remediation = Some(advice_at(
            "Select a target or target file with a reviewed policy for this loader and operation.",
            &a.path,
        ));
        return r;
    };
    r.conditional = target.conditional;
    if policy.allowed_sbpf_versions.contains(v) {
        r.outcome = Outcome::Pass;
        r.title = "Artifact meets the selected deployment policy".into();
        r.summary = format!(
            "Artifact is sBPF{v}; the selected target allows it for {} with {}.",
            a.operation, a.loader
        );
    } else {
        r.outcome = Outcome::Finding;
        r.severity = Some(Severity::Error);
        r.title = "Artifact is ineligible under the selected deployment policy".into();
        r.summary = format!(
            "Target {} allows [{}] for {} with {}; artifact is sBPF{v}.",
            target.id,
            policy.allowed_sbpf_versions.join(", "),
            a.operation,
            a.loader
        );
        r.remediation=Some(advice_at("Rebuild explicitly for sBPFv3 and bind the newly produced artifact before reassessing deployment eligibility.", &a.path));
    }
    r
}

fn idl_result(idl: &IdlInput, project: &Project, data: &Dataset) -> CheckResult {
    let mut r = rule_result(
        data,
        "SC400",
        &idl.id,
        "read Anchor IDL",
        idl.evidence.clone(),
    );
    let Some(client) = project.clients.iter().find(|c| c.id == idl.client) else {
        return r;
    };
    let Some(version) = parsed(client.resolved_dependencies.get(&idl.reader_package)) else {
        r.summary = format!(
            "IDL schema {}; exact resolved version for {} is unavailable.",
            idl.schema, idl.reader_package
        );
        r.remediation = Some(advice(
            "Provide package-lock evidence for the explicitly bound IDL reader.",
        ));
        return r;
    };
    let supported = match (idl.schema.as_str(), idl.reader_package.as_str()) {
        ("anchor-legacy", "@coral-xyz/anchor") => version.major == 0 && version.minor < 30,
        (s, "@coral-xyz/anchor") if s.starts_with("anchor-spec-") => {
            version.major == 0 && version.minor >= 30
        }
        (s, "@anchor-lang/core") if s.starts_with("anchor-spec-") => version.major == 1,
        _ => false,
    };
    r.summary = format!(
        "IDL schema {}; reader {} {}.",
        idl.schema, idl.reader_package, version
    );
    if idl.schema == "unknown" {
        return r;
    }
    if supported {
        r.outcome = Outcome::Pass;
        r.title = "IDL schema and reader are a reviewed combination".into();
    } else if idl.schema.starts_with("anchor-spec-")
        && idl.reader_package == "@coral-xyz/anchor"
        && version.major == 0
        && version.minor < 30
    {
        r.outcome = Outcome::Finding;
        r.severity = Some(Severity::Error);
        r.title = "IDL schema and reader are not a reviewed combination".into();
        r.remediation=Some(advice_at("Use a reader generation matching the IDL schema, or convert/regenerate the IDL with a pinned Anchor toolchain.", &idl.path));
    } else {
        r.title = "IDL and reader combination is outside the reviewed matrix".into();
    }
    r
}

pub fn check(
    project: Project,
    data: &Dataset,
    policy: Policy,
    suppressions: &[Suppression],
    target: &AnalysisTarget,
) -> Result<Report> {
    let mut results = vec![];
    for program in &project.programs {
        results.extend(program_results(program, data));
        results.push(framework_syntax_result(program, data));
    }
    for artifact in &project.artifacts {
        results.push(artifact_result(artifact, data, target));
    }
    for client in &project.clients {
        results.push(decoder_result(client, data));
        for read in &client.rpc_reads {
            results.push(evaluate(client, read, data));
        }
    }
    for idl in &project.idls {
        results.push(idl_result(idl, &project, data));
    }
    for suppression in suppressions {
        let result = results.iter_mut().find(|result| {
            result.rule_id == suppression.rule && result.subject == suppression.subject
        });
        let Some(result) = result else {
            anyhow::bail!(
                "suppression does not match an evaluated rule/subject: {} {}",
                suppression.rule,
                suppression.subject
            );
        };
        ensure!(
            result.suppression.is_none(),
            "duplicate suppression for {}",
            suppression.subject
        );
        ensure!(
            matches!(result.outcome, Outcome::Finding | Outcome::Unknown),
            "suppression must target a finding or unknown: {}",
            suppression.subject
        );
        result.suppression = Some(suppression.clone());
    }
    let mut report = Report {
        schema_version: 1,
        command: "check".into(),
        analysis_mode: "static".into(),
        target: target.id.clone(),
        target_digest: target.digest.clone(),
        dataset: DatasetIdentity {
            revision: data.revision.clone(),
            digest: data.digest.clone(),
        },
        project,
        results,
        counts: Counts::default(),
        policy,
        exit_code: 0,
    };
    report.finish();
    Ok(report)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;

    fn client(maximum: Option<RpcMaximum>) -> Client {
        Client {
            id: "indexer".into(),
            name: None,
            manifest: "package.json".into(),
            dependencies: BTreeMap::new(),
            resolved_dependencies: BTreeMap::new(),
            decoder_package: None,
            required_read_versions: Some(vec!["legacy".into(), "v0".into(), "v1".into()]),
            rpc_reads: vec![RpcRead {
                id: "blocks".into(),
                method: "getBlock".into(),
                encoding: Some("json".into()),
                transaction_details: Some("full".into()),
                max_supported_transaction_version: maximum,
            }],
            evidence: vec![],
        }
    }

    fn run(client: &Client) -> CheckResult {
        evaluate(
            client,
            client.rpc_reads.first().expect("test read"),
            &Dataset::bundled().unwrap(),
        )
    }

    #[test]
    fn acceptance_boundaries_and_omission_are_distinct() {
        for (maximum, expected) in [
            (Some(RpcMaximum::Version(0)), Outcome::Finding),
            (Some(RpcMaximum::Version(1)), Outcome::Pass),
            (
                Some(RpcMaximum::Omitted(Omitted::Explicit)),
                Outcome::Finding,
            ),
            (None, Outcome::Unknown),
            (Some(RpcMaximum::Version(2)), Outcome::Unknown),
        ] {
            assert_eq!(run(&client(maximum)).outcome, expected);
        }
        let mut legacy = client(Some(RpcMaximum::Omitted(Omitted::Explicit)));
        legacy.required_read_versions = Some(vec!["legacy".into()]);
        assert_eq!(run(&legacy).outcome, Outcome::Pass);
        legacy.required_read_versions = Some(vec!["v0".into()]);
        assert_eq!(run(&legacy).outcome, Outcome::Finding);
        legacy.rpc_reads[0].max_supported_transaction_version = Some(RpcMaximum::Version(0));
        assert_eq!(run(&legacy).outcome, Outcome::Pass);
    }

    #[test]
    fn response_modes_and_unknowns_do_not_create_false_failures() {
        let mut value = client(None);
        for details in ["none", "signatures"] {
            value.rpc_reads[0].transaction_details = Some(details.into());
            assert_eq!(run(&value).outcome, Outcome::NotApplicable);
        }
        value.rpc_reads[0].transaction_details = Some("accounts".into());
        assert_eq!(run(&value).outcome, Outcome::Unknown);
        value.rpc_reads[0].method = "getTransaction".into();
        value.rpc_reads[0].transaction_details = None;
        value.rpc_reads[0].max_supported_transaction_version = Some(RpcMaximum::Version(1));
        assert_eq!(run(&value).outcome, Outcome::Pass);
        value.required_read_versions = Some(vec!["v2".into()]);
        assert_eq!(run(&value).outcome, Outcome::Unknown);
        value.required_read_versions = None;
        assert_eq!(run(&value).outcome, Outcome::Unknown);
        value.required_read_versions = Some(vec![]);
        assert_eq!(run(&value).outcome, Outcome::NotApplicable);
    }

    fn program() -> Program {
        Program {
            id: "vault".into(),
            name: "vault".into(),
            manifest: "programs/vault/Cargo.toml".into(),
            framework: "anchor".into(),
            candidate_reason: "test".into(),
            rust_version: Some("1.79".into()),
            dependencies: BTreeMap::from([("dependencies/anchor-lang".into(), "0.31.1".into())]),
            resolved_dependencies: BTreeMap::from([
                ("anchor-lang".into(), "0.31.1".into()),
                ("anchor-spl".into(), "0.31.1".into()),
            ]),
            anchor_cli_version: Some("0.31.1".into()),
            sbf_rust_version: Some("1.79.0".into()),
            sbpf_arch: Some("v3".into()),
            platform_tools_version: Some("1.53.0".into()),
            cargo_build_sbf_version: Some("4.2.0".into()),
            evidence: vec![],
        }
    }

    fn outcome(results: &[CheckResult], id: &str) -> Outcome {
        results
            .iter()
            .find(|result| result.rule_id == id)
            .unwrap()
            .outcome
    }

    #[test]
    fn program_rules_preserve_pass_finding_unknown_and_applicability() {
        let data = Dataset::bundled().unwrap();
        let baseline = program();
        let results = program_results(&baseline, &data);
        for id in ["SC001", "SC003", "SC103", "SC302"] {
            assert_eq!(outcome(&results, id), Outcome::Pass, "{id}");
        }

        let mut changed = baseline.clone();
        changed.sbf_rust_version = Some("1.78.0".into());
        changed.anchor_cli_version = Some("0.30.1".into());
        changed.platform_tools_version = Some("1.52.0".into());
        changed
            .resolved_dependencies
            .insert("anchor-spl".into(), "0.30.1".into());
        let results = program_results(&changed, &data);
        for id in ["SC001", "SC003", "SC103", "SC302"] {
            assert_eq!(outcome(&results, id), Outcome::Finding, "{id}");
        }
        let sc003 = results
            .iter()
            .find(|result| result.rule_id == "SC003")
            .unwrap();
        assert!(sc003.summary.contains("Cargo accepted anchor-lang 0.31.1"));
        assert!(sc003.summary.contains("requirement 0.31.1"));
        assert!(sc003.explanation.contains("dependency-range compatibility"));

        let mut unresolved_anchor = baseline.clone();
        unresolved_anchor
            .resolved_dependencies
            .remove("anchor-lang");
        let results = program_results(&unresolved_anchor, &data);
        let sc003 = results
            .iter()
            .find(|result| result.rule_id == "SC003")
            .unwrap();
        assert_eq!(sc003.outcome, Outcome::Unknown);
        assert_eq!(sc003.title, "Resolved anchor-lang version is unavailable");
        assert!(sc003.summary.contains("Anchor CLI 0.31.1 is selected"));
        assert!(sc003.summary.contains("requirement 0.31.1 is declared"));
        assert!(!sc003
            .remediation
            .as_ref()
            .unwrap()
            .summary
            .contains("anchor_cli_version"));
        assert!(sc003.remediation.as_ref().unwrap().steps[0].contains("anchor build"));

        let mut missing = baseline.clone();
        missing.sbf_rust_version = None;
        missing.anchor_cli_version = None;
        missing.platform_tools_version = None;
        missing.resolved_dependencies.remove("anchor-lang");
        let results = program_results(&missing, &data);
        for id in ["SC001", "SC003", "SC103"] {
            assert_eq!(outcome(&results, id), Outcome::Unknown, "{id}");
        }

        let mut native = baseline;
        native.framework = "native".into();
        native.rust_version = None;
        native.sbpf_arch = None;
        let results = program_results(&native, &data);
        for id in ["SC001", "SC003", "SC103", "SC302"] {
            assert_eq!(outcome(&results, id), Outcome::NotApplicable, "{id}");
        }
    }

    #[test]
    fn decoder_and_idl_rules_require_exact_bound_evidence() {
        let data = Dataset::bundled().unwrap();
        let mut value = client(Some(RpcMaximum::Version(1)));
        value.decoder_package = Some("@solana/web3.js".into());
        value
            .resolved_dependencies
            .insert("@solana/web3.js".into(), "1.99.0".into());
        assert_eq!(decoder_result(&value, &data).outcome, Outcome::Pass);
        value
            .resolved_dependencies
            .insert("@solana/web3.js".into(), "1.98.4".into());
        assert_eq!(decoder_result(&value, &data).outcome, Outcome::Finding);
        value.resolved_dependencies.clear();
        assert_eq!(decoder_result(&value, &data).outcome, Outcome::Unknown);
        value.required_read_versions = Some(vec![]);
        assert_eq!(
            decoder_result(&value, &data).outcome,
            Outcome::NotApplicable
        );

        // An RPC maximum that accepts V1 independently places V1 decoding in
        // scope; the compatibility contract does not need to repeat it.
        value.required_read_versions = None;
        value
            .resolved_dependencies
            .insert("@solana/kit".into(), "8.0.0".into());
        value.decoder_package = Some("@solana/kit".into());
        assert_eq!(decoder_result(&value, &data).outcome, Outcome::Pass);

        value
            .resolved_dependencies
            .insert("@coral-xyz/anchor".into(), "0.30.1".into());
        let idl = IdlInput {
            id: "vault-idl".into(),
            path: "idl/vault.json".into(),
            client: "indexer".into(),
            reader_package: "@coral-xyz/anchor".into(),
            schema: "anchor-spec-0.1.0".into(),
            evidence: vec![],
        };
        let mut project = Project {
            name: "test".into(),
            clients: vec![value.clone()],
            programs: vec![],
            idls: vec![],
            artifacts: vec![],
            tools: vec![],
        };
        assert_eq!(idl_result(&idl, &project, &data).outcome, Outcome::Pass);
        project.clients[0]
            .resolved_dependencies
            .insert("@coral-xyz/anchor".into(), "0.29.0".into());
        assert_eq!(idl_result(&idl, &project, &data).outcome, Outcome::Finding);
        project.clients[0].resolved_dependencies.clear();
        assert_eq!(idl_result(&idl, &project, &data).outcome, Outcome::Unknown);
    }
}