pmcp-code-mode 0.5.0

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

use crate::config::CodeModeConfig;
#[cfg(feature = "openapi-code-mode")]
use crate::config::OperationRegistry;
use crate::explanation::{ExplanationGenerator, TemplateExplanationGenerator};
use crate::graphql::{GraphQLQueryInfo, GraphQLValidator};
use crate::policy::{OperationEntity, PolicyEvaluator};
use crate::token::{compute_context_hash, HmacTokenGenerator, TokenGenerator, TokenSecret};
use crate::types::{
    PolicyViolation, TokenError, UnifiedAction, ValidationError, ValidationMetadata,
    ValidationResult,
};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Instant;

#[cfg(feature = "openapi-code-mode")]
use crate::javascript::{JavaScriptCodeInfo, JavaScriptValidator};

/// Static flag to ensure the "no policy evaluator" warning is only logged once per process.
static NO_POLICY_WARNING_LOGGED: AtomicBool = AtomicBool::new(false);

/// Build the `Vec<PolicyViolation>` for a denied authorization decision.
///
/// Flows all available diagnostic information from the decision into violations
/// so the client sees *why* a request was denied:
///
/// - Each `determining_policies` entry becomes a `policy` violation naming that policy
/// - Each `decision.errors` entry becomes a `policy_error` violation surfacing the
///   underlying Cedar/AVP error (e.g., "entity does not conform to schema")
/// - If both lists are empty (the canonical "default-deny: no Permit matched" case)
///   a synthetic `default_deny` violation is injected with context about the
///   server_id and action, so the client has *something* to debug against
///
/// Without this, default-deny would surface as `valid:false, violations:[]` —
/// impossible to debug from the client.
fn build_policy_violations(
    decision: &crate::policy::AuthorizationDecision,
    server_id: &str,
    action: impl std::fmt::Display,
    denied_subject: &str,
) -> Vec<PolicyViolation> {
    let capacity = decision.determining_policies.len() + decision.errors.len() + 1;
    let mut violations: Vec<PolicyViolation> = Vec::with_capacity(capacity);

    for policy_id in &decision.determining_policies {
        violations.push(PolicyViolation::new(
            "policy",
            policy_id.clone(),
            format!("Policy denied the {}", denied_subject),
        ));
    }

    for err in &decision.errors {
        violations.push(PolicyViolation::new(
            "policy_error",
            "evaluation_error",
            err.clone(),
        ));
    }

    if violations.is_empty() {
        violations.push(PolicyViolation::new(
            "policy",
            "default_deny",
            format!(
                "Authorization default-deny: no Permit policy matched for \
                 server_id={server_id} action={action}. Check that Cedar \
                 policies exist for this server and that server_id is set correctly."
            ),
        ));
    }

    violations
}

/// Log a warning when Code Mode is enabled without a policy evaluator.
fn warn_no_policy_configured() {
    if !NO_POLICY_WARNING_LOGGED.swap(true, Ordering::SeqCst) {
        tracing::warn!(
            target: "code_mode",
            "CODE MODE SECURITY WARNING: Code Mode is enabled but no policy evaluator \
            is configured. Only basic config checks (allow_mutations, max_depth, etc.) will be \
            performed. This provides NO real authorization policy evaluation. \
            For production deployments, configure a policy evaluator (AVP or local Cedar)."
        );
    }
}

/// Context for validation (user, session, schema).
#[derive(Debug, Clone)]
pub struct ValidationContext {
    /// User ID from access token
    pub user_id: String,

    /// MCP session ID
    pub session_id: String,

    /// Schema hash for context binding
    pub schema_hash: String,

    /// Permissions hash for context binding
    pub permissions_hash: String,
}

impl ValidationContext {
    /// Create a new validation context.
    pub fn new(
        user_id: impl Into<String>,
        session_id: impl Into<String>,
        schema_hash: impl Into<String>,
        permissions_hash: impl Into<String>,
    ) -> Self {
        Self {
            user_id: user_id.into(),
            session_id: session_id.into(),
            schema_hash: schema_hash.into(),
            permissions_hash: permissions_hash.into(),
        }
    }

    /// Compute the combined context hash.
    pub fn context_hash(&self) -> String {
        compute_context_hash(&self.schema_hash, &self.permissions_hash)
    }
}

/// The validation pipeline that orchestrates all validation stages.
pub struct ValidationPipeline<
    T: TokenGenerator = HmacTokenGenerator,
    E: ExplanationGenerator = TemplateExplanationGenerator,
> {
    config: CodeModeConfig,
    graphql_validator: GraphQLValidator,
    #[cfg(feature = "openapi-code-mode")]
    javascript_validator: JavaScriptValidator,
    #[cfg(feature = "openapi-code-mode")]
    operation_registry: OperationRegistry,
    token_generator: T,
    explanation_generator: E,
    policy_evaluator: Option<Arc<dyn PolicyEvaluator>>,
}

impl ValidationPipeline<HmacTokenGenerator, TemplateExplanationGenerator> {
    /// Create a new validation pipeline with default generators.
    ///
    /// **Warning**: This constructor does not configure a policy evaluator.
    /// Only basic config checks will be performed.
    ///
    /// # Errors
    ///
    /// Returns [`TokenError::SecretTooShort`] if the token secret is shorter
    /// than [`HmacTokenGenerator::MIN_SECRET_LEN`] (16 bytes).
    pub fn new(
        mut config: CodeModeConfig,
        token_secret: impl Into<Vec<u8>>,
    ) -> Result<Self, TokenError> {
        if config.enabled {
            warn_no_policy_configured();
        }

        config.resolve_server_id();

        #[cfg(feature = "openapi-code-mode")]
        let operation_registry = OperationRegistry::from_entries(&config.operations);

        Ok(Self {
            graphql_validator: GraphQLValidator::default(),
            #[cfg(feature = "openapi-code-mode")]
            javascript_validator: JavaScriptValidator::default()
                .with_sdk_operations(config.sdk_operations.clone()),
            #[cfg(feature = "openapi-code-mode")]
            operation_registry,
            token_generator: HmacTokenGenerator::new_from_bytes(token_secret)?,
            explanation_generator: TemplateExplanationGenerator::new(),
            policy_evaluator: None,
            config,
        })
    }

    /// Create a new validation pipeline from a [`TokenSecret`].
    ///
    /// Convenience constructor for production callers and derive macro generated
    /// code. Callers never need to call `expose_secret()` directly.
    ///
    /// **Security note**: Internally this creates an intermediate `Vec<u8>` copy
    /// of the secret bytes that is **not** zeroized on drop. For maximum security,
    /// prefer [`TokenSecret::from_env`] which minimizes secret copies. This
    /// limitation will be addressed in a future version by adding a
    /// `HmacTokenGenerator::from_secret_ref` constructor.
    ///
    /// **Warning**: This constructor does not configure a policy evaluator.
    /// Only basic config checks will be performed.
    ///
    /// # Errors
    ///
    /// Returns [`TokenError::SecretTooShort`] if the token secret is shorter
    /// than [`HmacTokenGenerator::MIN_SECRET_LEN`] (16 bytes).
    pub fn from_token_secret(
        config: CodeModeConfig,
        secret: &TokenSecret,
    ) -> Result<Self, TokenError> {
        Self::new(config, secret.expose_secret().to_vec())
    }

    /// Create a new validation pipeline with a policy evaluator.
    ///
    /// # Errors
    ///
    /// Returns [`TokenError::SecretTooShort`] if the token secret is shorter
    /// than [`HmacTokenGenerator::MIN_SECRET_LEN`] (16 bytes).
    pub fn with_policy_evaluator(
        mut config: CodeModeConfig,
        token_secret: impl Into<Vec<u8>>,
        evaluator: Arc<dyn PolicyEvaluator>,
    ) -> Result<Self, TokenError> {
        config.resolve_server_id();
        if config.server_id.is_none() {
            tracing::warn!(
                target: "code_mode",
                "CodeModeConfig.server_id is not set — AVP/Cedar authorization will use 'unknown' \
                 as the resource entity ID and will likely default-deny silently. \
                 Set server_id in config.toml, or the PMCP_SERVER_ID or AWS_LAMBDA_FUNCTION_NAME env var."
            );
        }

        #[cfg(feature = "openapi-code-mode")]
        let operation_registry = OperationRegistry::from_entries(&config.operations);

        Ok(Self {
            graphql_validator: GraphQLValidator::default(),
            #[cfg(feature = "openapi-code-mode")]
            javascript_validator: JavaScriptValidator::default()
                .with_sdk_operations(config.sdk_operations.clone()),
            #[cfg(feature = "openapi-code-mode")]
            operation_registry,
            token_generator: HmacTokenGenerator::new_from_bytes(token_secret)?,
            explanation_generator: TemplateExplanationGenerator::new(),
            policy_evaluator: Some(evaluator),
            config,
        })
    }

    /// Create a pipeline from a [`TokenSecret`] with an `Arc` policy evaluator.
    ///
    /// Used by derive macro generated code where the policy evaluator is
    /// stored as `Arc<dyn PolicyEvaluator>` on the parent struct.
    ///
    /// # Errors
    ///
    /// Returns [`TokenError::SecretTooShort`] if the token secret is shorter
    /// than [`HmacTokenGenerator::MIN_SECRET_LEN`] (16 bytes).
    pub fn from_token_secret_with_policy(
        config: CodeModeConfig,
        secret: &TokenSecret,
        evaluator: Arc<dyn PolicyEvaluator>,
    ) -> Result<Self, TokenError> {
        Self::with_policy_evaluator(config, secret.expose_secret().to_vec(), evaluator)
    }
}

impl<T: TokenGenerator, E: ExplanationGenerator> ValidationPipeline<T, E> {
    /// Create a pipeline with custom generators.
    pub fn with_generators(
        mut config: CodeModeConfig,
        token_generator: T,
        explanation_generator: E,
    ) -> Self {
        config.resolve_server_id();

        #[cfg(feature = "openapi-code-mode")]
        let operation_registry = OperationRegistry::from_entries(&config.operations);

        Self {
            graphql_validator: GraphQLValidator::default(),
            #[cfg(feature = "openapi-code-mode")]
            javascript_validator: JavaScriptValidator::default()
                .with_sdk_operations(config.sdk_operations.clone()),
            #[cfg(feature = "openapi-code-mode")]
            operation_registry,
            token_generator,
            explanation_generator,
            policy_evaluator: None,
            config,
        }
    }

    /// Set the policy evaluator for this pipeline.
    pub fn set_policy_evaluator(&mut self, evaluator: Arc<dyn PolicyEvaluator>) {
        self.policy_evaluator = Some(evaluator);
    }

    /// Check if a policy evaluator is configured.
    pub fn has_policy_evaluator(&self) -> bool {
        self.policy_evaluator.is_some()
    }

    /// Check mutation and query authorization against config (blocklists, allowlists).
    ///
    /// This is the authorization logic shared between the sync and async validation paths.
    /// It uses the already-parsed `query_info` to avoid re-parsing.
    fn check_config_authorization(
        &self,
        query_info: &GraphQLQueryInfo,
        start: Instant,
    ) -> Option<ValidationResult> {
        // Mutation authorization checks
        if !query_info.operation_type.is_read_only() {
            let mutation_name = query_info.root_fields.first().cloned().unwrap_or_default();

            if !self.config.blocked_mutations.is_empty()
                && self.config.blocked_mutations.contains(&mutation_name)
            {
                return Some(ValidationResult::failure(
                    vec![PolicyViolation::new(
                        "code_mode",
                        "blocked_mutation",
                        &format!("Mutation '{}' is blocked for this server", mutation_name),
                    )
                    .with_suggestion("This mutation is in the blocklist and cannot be executed")],
                    self.build_metadata(query_info, start.elapsed().as_millis() as u64),
                ));
            }

            if !self.config.allowed_mutations.is_empty() {
                if !self.config.allowed_mutations.contains(&mutation_name) {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "mutation_not_allowed",
                            &format!("Mutation '{}' is not in the allowlist", mutation_name),
                        )
                        .with_suggestion(&format!(
                            "Only these mutations are allowed: {}",
                            self.config
                                .allowed_mutations
                                .iter()
                                .cloned()
                                .collect::<Vec<_>>()
                                .join(", ")
                        ))],
                        self.build_metadata(query_info, start.elapsed().as_millis() as u64),
                    ));
                }
            } else if !self.config.allow_mutations {
                return Some(ValidationResult::failure(
                    vec![PolicyViolation::new(
                        "code_mode",
                        "allow_mutations",
                        "Mutations are not enabled for this server",
                    )
                    .with_suggestion("Only read-only queries are allowed")],
                    self.build_metadata(query_info, start.elapsed().as_millis() as u64),
                ));
            }
        }

        // Query (read) authorization checks -- mirrors mutation enforcement above
        if query_info.operation_type.is_read_only() {
            let query_name = query_info.root_fields.first().cloned().unwrap_or_default();

            if !self.config.blocked_queries.is_empty()
                && self.config.blocked_queries.contains(&query_name)
            {
                return Some(ValidationResult::failure(
                    vec![PolicyViolation::new(
                        "code_mode",
                        "blocked_query",
                        &format!("Query '{}' is blocked for this server", query_name),
                    )
                    .with_suggestion("This query is in the blocklist and cannot be executed")],
                    self.build_metadata(query_info, start.elapsed().as_millis() as u64),
                ));
            }

            if !self.config.allowed_queries.is_empty()
                && !self.config.allowed_queries.contains(&query_name)
            {
                return Some(ValidationResult::failure(
                    vec![PolicyViolation::new(
                        "code_mode",
                        "query_not_allowed",
                        &format!("Query '{}' is not in the allowlist", query_name),
                    )
                    .with_suggestion(&format!(
                        "Only these queries are allowed: {}",
                        self.config
                            .allowed_queries
                            .iter()
                            .cloned()
                            .collect::<Vec<_>>()
                            .join(", ")
                    ))],
                    self.build_metadata(query_info, start.elapsed().as_millis() as u64),
                ));
            }
        }

        None
    }

    /// Validate a GraphQL query using basic config checks only.
    pub fn validate_graphql_query(
        &self,
        query: &str,
        context: &ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        let start = Instant::now();

        if !self.config.enabled {
            return Err(ValidationError::ConfigError(
                "Code Mode is not enabled for this server".into(),
            ));
        }

        if query.len() > self.config.max_query_length {
            return Err(ValidationError::SecurityError {
                message: format!(
                    "Query length {} exceeds maximum {}",
                    query.len(),
                    self.config.max_query_length
                ),
                issue: crate::types::SecurityIssueType::HighComplexity,
            });
        }

        let query_info = self.graphql_validator.validate(query)?;

        // Config-based authorization checks (mutation blocklist/allowlist, query blocklist/allowlist)
        if let Some(failure) = self.check_config_authorization(&query_info, start) {
            return Ok(failure);
        }

        self.complete_validation(query, &query_info, context, start)
    }

    /// Validate a GraphQL query using a policy evaluator (async).
    pub async fn validate_graphql_query_async(
        &self,
        query: &str,
        context: &ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        let start = Instant::now();

        if !self.config.enabled {
            return Err(ValidationError::ConfigError(
                "Code Mode is not enabled for this server".into(),
            ));
        }

        if query.len() > self.config.max_query_length {
            return Err(ValidationError::SecurityError {
                message: format!(
                    "Query length {} exceeds maximum {}",
                    query.len(),
                    self.config.max_query_length
                ),
                issue: crate::types::SecurityIssueType::HighComplexity,
            });
        }

        let query_info = self.graphql_validator.validate(query)?;

        // Policy evaluation via trait
        if let Some(ref evaluator) = self.policy_evaluator {
            let operation_entity = OperationEntity::from_query_info(&query_info);
            let server_config = self.config.to_server_config_entity();

            let decision = evaluator
                .evaluate_operation(&operation_entity, &server_config)
                .await
                .map_err(|e| {
                    ValidationError::InternalError(format!("Policy evaluation error: {}", e))
                })?;

            if !decision.allowed {
                let op_type_str = format!("{:?}", query_info.operation_type);
                let action =
                    UnifiedAction::from_graphql(&op_type_str, query_info.operation_name.as_deref());
                let violations = build_policy_violations(
                    &decision,
                    self.config.server_id(),
                    action,
                    "operation",
                );

                return Ok(ValidationResult::failure(
                    violations,
                    self.build_metadata(&query_info, start.elapsed().as_millis() as u64),
                ));
            }
        } else {
            warn_no_policy_configured();
            tracing::debug!(
                target: "code_mode",
                "Falling back to basic config checks (no policy evaluator configured)"
            );
            // Reuse already-parsed query_info instead of re-parsing via validate_graphql_query
            if let Some(failure) = self.check_config_authorization(&query_info, start) {
                return Ok(failure);
            }
        }

        self.complete_validation(query, &query_info, context, start)
    }

    /// Complete validation after policy check passes.
    fn complete_validation(
        &self,
        query: &str,
        query_info: &GraphQLQueryInfo,
        context: &ValidationContext,
        start: Instant,
    ) -> Result<ValidationResult, ValidationError> {
        let security_analysis = self.graphql_validator.analyze_security(query_info);
        let risk_level = security_analysis.assess_risk();

        if security_analysis
            .potential_issues
            .iter()
            .any(|i| i.is_critical())
        {
            let violations: Vec<PolicyViolation> = security_analysis
                .potential_issues
                .iter()
                .filter(|i| i.is_critical())
                .map(|i| {
                    PolicyViolation::new("security", format!("{:?}", i.issue_type), &i.message)
                })
                .collect();

            return Ok(ValidationResult::failure(
                violations,
                self.build_metadata(query_info, start.elapsed().as_millis() as u64),
            ));
        }

        let explanation = self
            .explanation_generator
            .explain_graphql(query_info, &security_analysis);

        let context_hash = context.context_hash();
        let token = self.token_generator.generate(
            query,
            &context.user_id,
            &context.session_id,
            self.config.server_id(),
            &context_hash,
            risk_level,
            self.config.token_ttl_seconds,
        );

        let token_string = token.encode().map_err(|e| {
            ValidationError::InternalError(format!("Failed to encode token: {}", e))
        })?;

        let operation_type_str = format!("{:?}", query_info.operation_type).to_lowercase();
        let mutation_name = query_info.operation_name.as_deref();
        let inferred_action = UnifiedAction::from_graphql(&operation_type_str, mutation_name);
        let action = UnifiedAction::resolve(
            inferred_action,
            &self.config.action_tags,
            query_info.operation_name.as_deref().unwrap_or(""),
        );

        let metadata = ValidationMetadata {
            is_read_only: query_info.operation_type.is_read_only(),
            estimated_rows: security_analysis.estimated_rows,
            accessed_types: security_analysis.tables_accessed.iter().cloned().collect(),
            accessed_fields: security_analysis.fields_accessed.iter().cloned().collect(),
            has_aggregation: security_analysis.has_aggregation,
            code_type: Some(self.graphql_validator.to_code_type(query_info)),
            action: Some(action),
            validation_time_ms: start.elapsed().as_millis() as u64,
        };

        let mut result = ValidationResult::success(explanation, risk_level, token_string, metadata);

        for issue in &security_analysis.potential_issues {
            if !issue.is_critical() {
                result.warnings.push(issue.message.clone());
            }
        }

        Ok(result)
    }

    /// Build metadata from query info.
    fn build_metadata(
        &self,
        query_info: &GraphQLQueryInfo,
        validation_time_ms: u64,
    ) -> ValidationMetadata {
        let operation_type_str = format!("{:?}", query_info.operation_type).to_lowercase();
        let mutation_name = query_info.operation_name.as_deref();
        let inferred_action = UnifiedAction::from_graphql(&operation_type_str, mutation_name);
        let action = UnifiedAction::resolve(
            inferred_action,
            &self.config.action_tags,
            query_info.operation_name.as_deref().unwrap_or(""),
        );

        ValidationMetadata {
            is_read_only: query_info.operation_type.is_read_only(),
            estimated_rows: None,
            accessed_types: query_info.types_accessed.iter().cloned().collect(),
            accessed_fields: query_info.fields_accessed.iter().cloned().collect(),
            has_aggregation: false,
            code_type: Some(self.graphql_validator.to_code_type(query_info)),
            action: Some(action),
            validation_time_ms,
        }
    }

    /// Validate JavaScript code for OpenAPI Code Mode (sync, no policy evaluation).
    ///
    /// Runs config-level checks only. For policy evaluation (Cedar/AVP), use
    /// [`validate_javascript_code_async`] instead. Retained for backward
    /// compatibility with callers that don't need policy enforcement.
    #[cfg(feature = "openapi-code-mode")]
    pub fn validate_javascript_code(
        &self,
        code: &str,
        context: &ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        let start = Instant::now();
        let code_info = self.validate_js_preamble(code)?;
        if let Some(failure) = self.check_js_config_authorization(&code_info, start) {
            return Ok(failure);
        }
        self.complete_js_validation(code, &code_info, context, start)
    }

    /// Validate JavaScript code with async policy evaluation.
    ///
    /// Mirrors [`validate_graphql_query_async`] but for JavaScript/OpenAPI:
    /// 1. Parse JS via SWC + config-level checks (shared with sync version)
    /// 2. Policy evaluation via [`PolicyEvaluator::evaluate_script`] (async, fail-closed)
    /// 3. Security analysis + token generation
    ///
    /// When no policy evaluator is configured, falls back to config-only checks.
    #[cfg(feature = "openapi-code-mode")]
    pub async fn validate_javascript_code_async(
        &self,
        code: &str,
        context: &ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        use crate::policy::types::ScriptEntity;

        let start = Instant::now();
        let code_info = self.validate_js_preamble(code)?;
        if let Some(failure) = self.check_js_config_authorization(&code_info, start) {
            return Ok(failure);
        }

        // Policy evaluation via evaluate_script (mirrors GraphQL's evaluate_operation)
        if let Some(ref evaluator) = self.policy_evaluator {
            let sensitive_patterns: Vec<String> =
                self.config.openapi_blocked_paths.iter().cloned().collect();
            let registry_ref = if self.operation_registry.is_empty() {
                None
            } else {
                Some(&self.operation_registry)
            };
            let script_entity =
                ScriptEntity::from_javascript_info(&code_info, &sensitive_patterns, registry_ref);
            let server_entity = self.config.to_openapi_server_entity();

            let decision = evaluator
                .evaluate_script(&script_entity, &server_entity)
                .await
                .map_err(|e| {
                    ValidationError::InternalError(format!("Policy evaluation error: {}", e))
                })?;

            if !decision.allowed {
                let violations = build_policy_violations(
                    &decision,
                    self.config.server_id(),
                    script_entity.action(),
                    "script",
                );

                return Ok(ValidationResult::failure(
                    violations,
                    self.build_js_metadata(&code_info, start.elapsed().as_millis() as u64),
                ));
            }
        }

        self.complete_js_validation(code, &code_info, context, start)
    }

    /// Shared JavaScript preamble: enabled check, length check, parse.
    #[cfg(feature = "openapi-code-mode")]
    fn validate_js_preamble(&self, code: &str) -> Result<JavaScriptCodeInfo, ValidationError> {
        if !self.config.enabled {
            return Err(ValidationError::ConfigError(
                "Code Mode is not enabled for this server".into(),
            ));
        }

        if code.len() > self.config.max_query_length {
            return Err(ValidationError::SecurityError {
                message: format!(
                    "Code length {} exceeds maximum {}",
                    code.len(),
                    self.config.max_query_length
                ),
                issue: crate::types::SecurityIssueType::HighComplexity,
            });
        }

        Ok(self.javascript_validator.validate(code)?)
    }

    /// Config-level authorization checks for JavaScript code.
    ///
    /// Returns `Some(failure)` if a config check denied the code,
    /// `None` if all checks pass. Shared between sync and async paths
    /// (mirrors `check_config_authorization` for GraphQL).
    #[cfg(feature = "openapi-code-mode")]
    fn check_js_config_authorization(
        &self,
        code_info: &JavaScriptCodeInfo,
        start: Instant,
    ) -> Option<ValidationResult> {
        if code_info.is_read_only {
            return None;
        }

        for method in &code_info.methods_used {
            if !self.config.openapi_blocked_writes.is_empty()
                && self.config.openapi_blocked_writes.contains(method)
            {
                return Some(ValidationResult::failure(
                    vec![PolicyViolation::new(
                        "code_mode",
                        "blocked_method",
                        &format!("HTTP method '{}' is blocked for this server", method),
                    )
                    .with_suggestion("This method is in the blocklist and cannot be used")],
                    self.build_js_metadata(code_info, start.elapsed().as_millis() as u64),
                ));
            }
        }

        if !self.config.openapi_allowed_writes.is_empty() {
            tracing::debug!(
                target: "code_mode",
                "Skipping method-level check - policy evaluator will check operation allowlist ({} entries)",
                self.config.openapi_allowed_writes.len()
            );
        } else if !self.config.openapi_allow_writes {
            return Some(ValidationResult::failure(
                vec![PolicyViolation::new(
                    "code_mode",
                    "allow_mutations",
                    "Write HTTP methods (POST, PUT, DELETE, PATCH) are not enabled for this server",
                )
                .with_suggestion("Only read-only methods (GET, HEAD, OPTIONS) are allowed. Contact your administrator to enable write operations.")],
                self.build_js_metadata(code_info, start.elapsed().as_millis() as u64),
            ));
        }

        None
    }

    /// Complete JavaScript validation after policy checks pass.
    #[cfg(feature = "openapi-code-mode")]
    fn complete_js_validation(
        &self,
        code: &str,
        code_info: &JavaScriptCodeInfo,
        context: &ValidationContext,
        start: Instant,
    ) -> Result<ValidationResult, ValidationError> {
        let security_analysis = self.javascript_validator.analyze_security(code_info);
        let risk_level = security_analysis.assess_risk();

        if security_analysis
            .potential_issues
            .iter()
            .any(|i| i.is_critical())
        {
            let violations: Vec<PolicyViolation> = security_analysis
                .potential_issues
                .iter()
                .filter(|i| i.is_critical())
                .map(|i| {
                    PolicyViolation::new("security", format!("{:?}", i.issue_type), &i.message)
                })
                .collect();

            return Ok(ValidationResult::failure(
                violations,
                self.build_js_metadata(code_info, start.elapsed().as_millis() as u64),
            ));
        }

        let explanation = self.generate_js_explanation(code_info, &security_analysis);

        let context_hash = context.context_hash();
        let token = self.token_generator.generate(
            code,
            &context.user_id,
            &context.session_id,
            self.config.server_id(),
            &context_hash,
            risk_level,
            self.config.token_ttl_seconds,
        );

        let token_string = token.encode().map_err(|e| {
            ValidationError::InternalError(format!("Failed to encode token: {}", e))
        })?;

        let metadata = self.build_js_metadata(code_info, start.elapsed().as_millis() as u64);

        let mut result = ValidationResult::success(explanation, risk_level, token_string, metadata);

        for issue in &security_analysis.potential_issues {
            if !issue.is_critical() {
                result.warnings.push(issue.message.clone());
            }
        }

        Ok(result)
    }

    /// Build metadata from JavaScript code info.
    #[cfg(feature = "openapi-code-mode")]
    fn build_js_metadata(
        &self,
        code_info: &JavaScriptCodeInfo,
        validation_time_ms: u64,
    ) -> ValidationMetadata {
        let action = if !code_info.api_calls.is_empty() {
            let mut max_action = UnifiedAction::Read;
            for call in &code_info.api_calls {
                let method_str = format!("{:?}", call.method);
                let inferred = UnifiedAction::from_http_method(&method_str);
                match (&max_action, &inferred) {
                    (UnifiedAction::Read, _) => max_action = inferred,
                    (UnifiedAction::Write, UnifiedAction::Delete | UnifiedAction::Admin) => {
                        max_action = inferred
                    },
                    (UnifiedAction::Delete, UnifiedAction::Admin) => max_action = inferred,
                    _ => {},
                }
            }
            Some(max_action)
        } else if code_info.is_read_only {
            Some(UnifiedAction::Read)
        } else {
            Some(UnifiedAction::Write)
        };

        ValidationMetadata {
            is_read_only: code_info.is_read_only,
            estimated_rows: None,
            accessed_types: code_info.endpoints_accessed.iter().cloned().collect(),
            accessed_fields: code_info.methods_used.iter().cloned().collect(),
            has_aggregation: false,
            code_type: Some(self.javascript_validator.to_code_type(code_info)),
            action,
            validation_time_ms,
        }
    }

    /// Generate a human-readable explanation for JavaScript code.
    #[cfg(feature = "openapi-code-mode")]
    fn generate_js_explanation(
        &self,
        code_info: &JavaScriptCodeInfo,
        security_analysis: &crate::types::SecurityAnalysis,
    ) -> String {
        let mut parts = Vec::new();

        if code_info.is_read_only {
            parts.push("This code will perform read-only API requests.".to_string());
        } else {
            parts.push("This code will perform API requests that may modify data.".to_string());
        }

        if !code_info.api_calls.is_empty() {
            let call_descriptions: Vec<String> = code_info
                .api_calls
                .iter()
                .map(|call| format!("{:?} {}", call.method, call.path))
                .collect();

            if call_descriptions.len() <= 3 {
                parts.push(format!("API calls: {}", call_descriptions.join(", ")));
            } else {
                parts.push(format!(
                    "API calls: {} and {} more",
                    call_descriptions[..2].join(", "),
                    call_descriptions.len() - 2
                ));
            }
        }

        if code_info.loop_count > 0 {
            if code_info.all_loops_bounded {
                parts.push(format!(
                    "Contains {} bounded loop(s).",
                    code_info.loop_count
                ));
            } else {
                parts.push(format!(
                    "Contains {} loop(s) - ensure they are properly bounded.",
                    code_info.loop_count
                ));
            }
        }

        let risk = security_analysis.assess_risk();
        parts.push(format!("Risk: {}", risk));

        parts.join(" ")
    }

    /// Validate a SQL statement using basic config checks only (no policy evaluator).
    ///
    /// For policy evaluation (Cedar/AVP), use [`validate_sql_query_async`] instead.
    #[cfg(feature = "sql-code-mode")]
    pub fn validate_sql_query(
        &self,
        sql: &str,
        context: &ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        let start = Instant::now();
        let info = self.validate_sql_preamble(sql)?;
        if let Some(failure) = self.check_sql_config_authorization(&info, start) {
            return Ok(failure);
        }
        self.complete_sql_validation(sql, &info, context, start)
    }

    /// Validate a SQL statement with async policy evaluation.
    ///
    /// Mirrors [`validate_graphql_query_async`] and [`validate_javascript_code_async`]:
    /// 1. Parse SQL via sqlparser + config-level checks (shared with sync version)
    /// 2. Policy evaluation via [`PolicyEvaluator::evaluate_statement`] (async, fail-closed)
    /// 3. Security analysis + token generation
    ///
    /// When no policy evaluator is configured, falls back to config-only checks.
    #[cfg(feature = "sql-code-mode")]
    pub async fn validate_sql_query_async(
        &self,
        sql: &str,
        context: &ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        use crate::policy::StatementEntity;

        let start = Instant::now();
        let info = self.validate_sql_preamble(sql)?;
        if let Some(failure) = self.check_sql_config_authorization(&info, start) {
            return Ok(failure);
        }

        if let Some(ref evaluator) = self.policy_evaluator {
            let statement_entity = StatementEntity::from_sql_info(&info);
            let server_entity = self.config.to_sql_server_entity();

            let decision = evaluator
                .evaluate_statement(&statement_entity, &server_entity)
                .await
                .map_err(|e| {
                    ValidationError::InternalError(format!("Policy evaluation error: {}", e))
                })?;

            if !decision.allowed {
                let violations = build_policy_violations(
                    &decision,
                    self.config.server_id(),
                    statement_entity.action(),
                    "SQL statement",
                );

                return Ok(ValidationResult::failure(
                    violations,
                    self.build_sql_metadata(&info, start.elapsed().as_millis() as u64),
                ));
            }
        } else {
            warn_no_policy_configured();
        }

        self.complete_sql_validation(sql, &info, context, start)
    }

    /// Shared SQL preamble: enabled check, length check, parse.
    #[cfg(feature = "sql-code-mode")]
    fn validate_sql_preamble(
        &self,
        sql: &str,
    ) -> Result<crate::sql::SqlStatementInfo, ValidationError> {
        if !self.config.enabled {
            return Err(ValidationError::ConfigError(
                "Code Mode is not enabled for this server".into(),
            ));
        }

        if sql.len() > self.config.max_query_length {
            return Err(ValidationError::SecurityError {
                message: format!(
                    "SQL length {} exceeds maximum {}",
                    sql.len(),
                    self.config.max_query_length
                ),
                issue: crate::types::SecurityIssueType::HighComplexity,
            });
        }

        let validator = crate::sql::SqlValidator::new();
        validator.validate(sql)
    }

    /// Config-level authorization checks for SQL.
    ///
    /// Returns `Some(failure)` if a config check denied the statement,
    /// `None` if all checks pass.
    #[cfg(feature = "sql-code-mode")]
    fn check_sql_config_authorization(
        &self,
        info: &crate::sql::SqlStatementInfo,
        start: Instant,
    ) -> Option<ValidationResult> {
        use crate::sql::SqlStatementType;

        let stype = info.statement_type.as_str();

        // Statement-type blocklist
        if self.config.sql_blocked_statements.contains(stype) {
            return Some(ValidationResult::failure(
                vec![PolicyViolation::new(
                    "code_mode",
                    "blocked_statement",
                    format!("Statement type '{}' is blocked for this server", stype),
                )],
                self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
            ));
        }

        // Statement-type allowlist
        if !self.config.sql_allowed_statements.is_empty()
            && !self.config.sql_allowed_statements.contains(stype)
        {
            return Some(ValidationResult::failure(
                vec![PolicyViolation::new(
                    "code_mode",
                    "statement_not_allowed",
                    format!("Statement type '{}' is not in the allowlist", stype),
                )],
                self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
            ));
        }

        // Global action flags
        match info.statement_type {
            SqlStatementType::Select => {
                if !self.config.sql_reads_enabled {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "reads_disabled",
                            "SELECT statements are not enabled for this server",
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            },
            SqlStatementType::Insert | SqlStatementType::Update => {
                if !self.config.sql_allow_writes {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "writes_disabled",
                            "INSERT/UPDATE statements are not enabled for this server",
                        )
                        .with_suggestion("Contact your administrator to enable sql_allow_writes.")],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
                // WHERE requirement applies to UPDATE only — INSERTs never have WHERE.
                if matches!(info.statement_type, SqlStatementType::Update)
                    && self.config.sql_require_where_on_writes
                    && !info.has_where
                {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "missing_where",
                            format!("{} without WHERE clause is not allowed", info.verb),
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            },
            SqlStatementType::Delete => {
                if !self.config.sql_allow_deletes {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "deletes_disabled",
                            "DELETE statements are not enabled for this server",
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
                if self.config.sql_require_where_on_writes && !info.has_where {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "missing_where",
                            "DELETE without WHERE clause is not allowed",
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            },
            SqlStatementType::Ddl => {
                if !self.config.sql_allow_ddl {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "ddl_disabled",
                            "DDL (CREATE/ALTER/DROP/GRANT/REVOKE) is not enabled for this server",
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            },
            SqlStatementType::Other => {
                return Some(ValidationResult::failure(
                    vec![PolicyViolation::new(
                        "code_mode",
                        "unsupported_statement",
                        format!("Statement type '{}' is not supported", info.verb),
                    )],
                    self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                ));
            },
        }

        // Table-level blocklist
        if !self.config.sql_blocked_tables.is_empty() {
            for table in &info.tables {
                if self.config.sql_blocked_tables.contains(table) {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "blocked_table",
                            format!("Table '{}' is blocked for this server", table),
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            }
        }

        // Table-level allowlist
        if !self.config.sql_allowed_tables.is_empty() {
            for table in &info.tables {
                if !self.config.sql_allowed_tables.contains(table) {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "table_not_allowed",
                            format!("Table '{}' is not in the allowlist", table),
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            }
        }

        // Column-level blocklist
        if !self.config.sql_blocked_columns.is_empty() {
            for col in &info.columns {
                if self.config.sql_blocked_columns.contains(col) {
                    return Some(ValidationResult::failure(
                        vec![PolicyViolation::new(
                            "code_mode",
                            "blocked_column",
                            format!("Column '{}' is blocked for this server", col),
                        )],
                        self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
                    ));
                }
            }
        }

        // Structural limits
        if info.join_count > self.config.sql_max_joins {
            return Some(ValidationResult::failure(
                vec![PolicyViolation::new(
                    "code_mode",
                    "excessive_joins",
                    format!(
                        "Query has {} JOINs, exceeds limit of {}",
                        info.join_count, self.config.sql_max_joins
                    ),
                )],
                self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
            ));
        }

        if info.estimated_rows > self.config.sql_max_rows {
            return Some(ValidationResult::failure(
                vec![PolicyViolation::new(
                    "code_mode",
                    "excessive_rows",
                    format!(
                        "Estimated rows {} exceeds limit of {}",
                        info.estimated_rows, self.config.sql_max_rows
                    ),
                )],
                self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
            ));
        }

        None
    }

    /// Complete SQL validation after config/policy checks pass.
    #[cfg(feature = "sql-code-mode")]
    fn complete_sql_validation(
        &self,
        sql: &str,
        info: &crate::sql::SqlStatementInfo,
        context: &ValidationContext,
        start: Instant,
    ) -> Result<ValidationResult, ValidationError> {
        let validator = crate::sql::SqlValidator::new();
        let security_analysis = validator.analyze_security(info);
        let risk_level = security_analysis.assess_risk();

        if security_analysis
            .potential_issues
            .iter()
            .any(|i| i.is_critical())
        {
            let violations: Vec<PolicyViolation> = security_analysis
                .potential_issues
                .iter()
                .filter(|i| i.is_critical())
                .map(|i| {
                    PolicyViolation::new("security", format!("{:?}", i.issue_type), &i.message)
                })
                .collect();

            return Ok(ValidationResult::failure(
                violations,
                self.build_sql_metadata(info, start.elapsed().as_millis() as u64),
            ));
        }

        let context_hash = context.context_hash();
        let token = self.token_generator.generate(
            sql,
            &context.user_id,
            &context.session_id,
            self.config.server_id(),
            &context_hash,
            risk_level,
            self.config.token_ttl_seconds,
        );

        let token_string = token.encode().map_err(|e| {
            ValidationError::InternalError(format!("Failed to encode token: {}", e))
        })?;

        let explanation = self.generate_sql_explanation(info, &security_analysis);
        let metadata = self.build_sql_metadata(info, start.elapsed().as_millis() as u64);

        let mut result = ValidationResult::success(explanation, risk_level, token_string, metadata);

        for issue in &security_analysis.potential_issues {
            if !issue.is_critical() {
                result.warnings.push(issue.message.clone());
            }
        }

        Ok(result)
    }

    /// Build metadata from SQL statement info.
    #[cfg(feature = "sql-code-mode")]
    fn build_sql_metadata(
        &self,
        info: &crate::sql::SqlStatementInfo,
        validation_time_ms: u64,
    ) -> ValidationMetadata {
        let inferred = UnifiedAction::from_sql(info.statement_type.as_str());
        let action = UnifiedAction::resolve(inferred, &self.config.action_tags, &info.verb);

        ValidationMetadata {
            is_read_only: info.statement_type.is_read_only(),
            estimated_rows: Some(info.estimated_rows),
            accessed_types: info.tables.iter().cloned().collect(),
            accessed_fields: info.columns.iter().cloned().collect(),
            has_aggregation: info.has_aggregation,
            code_type: Some(if info.statement_type.is_read_only() {
                crate::types::CodeType::SqlQuery
            } else {
                crate::types::CodeType::SqlMutation
            }),
            action: Some(action),
            validation_time_ms,
        }
    }

    /// Generate a human-readable explanation for a SQL statement.
    #[cfg(feature = "sql-code-mode")]
    fn generate_sql_explanation(
        &self,
        info: &crate::sql::SqlStatementInfo,
        security_analysis: &crate::types::SecurityAnalysis,
    ) -> String {
        let mut parts = Vec::new();

        let verb_phrase = match info.statement_type.as_str() {
            "SELECT" => "This query reads data",
            "INSERT" => "This statement inserts rows",
            "UPDATE" => "This statement updates rows",
            "DELETE" => "This statement deletes rows",
            "DDL" => "This statement changes schema or permissions",
            _ => "This statement",
        };

        let tables_phrase = if info.tables.is_empty() {
            String::new()
        } else {
            let mut ts: Vec<&String> = info.tables.iter().collect();
            ts.sort();
            format!(
                " in table(s): {}",
                ts.into_iter().cloned().collect::<Vec<_>>().join(", ")
            )
        };

        parts.push(format!("{}{}.", verb_phrase, tables_phrase));

        if info.has_where {
            parts.push("Filtered with WHERE clause.".to_string());
        }
        if info.has_limit {
            parts.push(format!("Limited to {} rows.", info.estimated_rows));
        }
        if info.join_count > 0 {
            parts.push(format!("Uses {} JOIN(s).", info.join_count));
        }
        if info.subquery_count > 0 {
            parts.push(format!("Contains {} subquer(ies).", info.subquery_count));
        }

        let risk = security_analysis.assess_risk();
        parts.push(format!("Risk: {}", risk));

        parts.join(" ")
    }

    /// Check if a validation result should be auto-approved.
    pub fn should_auto_approve(&self, result: &ValidationResult) -> bool {
        result.is_valid && self.config.should_auto_approve(result.risk_level)
    }

    /// Get the config.
    pub fn config(&self) -> &CodeModeConfig {
        &self.config
    }

    /// Get the token generator.
    pub fn token_generator(&self) -> &T {
        &self.token_generator
    }
}

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

    fn test_pipeline() -> ValidationPipeline {
        ValidationPipeline::new(CodeModeConfig::enabled(), b"test-secret-key!".to_vec()).unwrap()
    }

    fn test_context() -> ValidationContext {
        ValidationContext::new("user-123", "session-456", "schema-hash", "perms-hash")
    }

    #[test]
    fn test_simple_query_validation() {
        let pipeline = test_pipeline();
        let ctx = test_context();

        let result = pipeline
            .validate_graphql_query("query { users { id name } }", &ctx)
            .unwrap();

        assert!(result.is_valid);
        assert!(result.approval_token.is_some());
        assert_eq!(result.risk_level, RiskLevel::Low);
        assert!(result.explanation.contains("read"));
    }

    #[test]
    fn test_mutation_blocked() {
        let mut config = CodeModeConfig::enabled();
        config.allow_mutations = false;

        let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
        let ctx = test_context();

        let result = pipeline
            .validate_graphql_query("mutation { createUser(name: \"test\") { id } }", &ctx)
            .unwrap();

        assert!(!result.is_valid);
        assert!(result
            .violations
            .iter()
            .any(|v| v.rule == "allow_mutations"));
    }

    #[test]
    fn test_disabled_code_mode() {
        let config = CodeModeConfig::default();
        let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
        let ctx = test_context();

        let result = pipeline.validate_graphql_query("query { users { id } }", &ctx);

        assert!(matches!(result, Err(ValidationError::ConfigError(_))));
    }

    #[test]
    fn test_auto_approve_low_risk() {
        let pipeline = test_pipeline();
        let ctx = test_context();

        let result = pipeline
            .validate_graphql_query("query { users { id } }", &ctx)
            .unwrap();

        assert!(pipeline.should_auto_approve(&result));
    }

    #[test]
    fn test_context_hash() {
        let ctx = test_context();
        let hash1 = ctx.context_hash();

        let ctx2 =
            ValidationContext::new("user-123", "session-456", "different-schema", "perms-hash");
        let hash2 = ctx2.context_hash();

        assert_ne!(hash1, hash2);
    }

    #[test]
    fn test_blocked_query_rejected() {
        let mut config = CodeModeConfig::enabled();
        config.blocked_queries.insert("users".to_string());

        let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
        let ctx = test_context();

        let result = pipeline
            .validate_graphql_query("query { users { id } }", &ctx)
            .unwrap();

        assert!(!result.is_valid);
        assert!(result.violations.iter().any(|v| v.rule == "blocked_query"));
    }

    #[test]
    fn test_allowed_queries_enforced() {
        let mut config = CodeModeConfig::enabled();
        config.allowed_queries.insert("orders".to_string());

        let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
        let ctx = test_context();

        // "users" is not in the allowlist -- should be rejected
        let result = pipeline
            .validate_graphql_query("query { users { id } }", &ctx)
            .unwrap();

        assert!(!result.is_valid);
        assert!(result
            .violations
            .iter()
            .any(|v| v.rule == "query_not_allowed"));
    }

    // ============================================================================
    // SQL Code Mode tests
    // ============================================================================

    #[cfg(feature = "sql-code-mode")]
    mod sql_tests {
        use super::*;

        fn sql_pipeline() -> ValidationPipeline {
            ValidationPipeline::new(CodeModeConfig::enabled(), b"test-secret-key!".to_vec())
                .unwrap()
        }

        #[test]
        fn validates_select() {
            let pipeline = sql_pipeline();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("SELECT id, name FROM users LIMIT 10", &ctx)
                .unwrap();

            assert!(result.is_valid);
            assert!(result.approval_token.is_some());
        }

        #[test]
        fn rejects_insert_when_writes_disabled() {
            let pipeline = sql_pipeline();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("INSERT INTO users (id, name) VALUES (1, 'Alice')", &ctx)
                .unwrap();

            assert!(!result.is_valid);
            assert!(result
                .violations
                .iter()
                .any(|v| v.rule == "writes_disabled"));
        }

        #[test]
        fn permits_insert_when_writes_enabled() {
            let mut config = CodeModeConfig::enabled();
            config.sql_allow_writes = true;
            let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("INSERT INTO users (id, name) VALUES (1, 'Alice')", &ctx)
                .unwrap();

            assert!(result.is_valid);
        }

        #[test]
        fn rejects_update_without_where_by_default() {
            let mut config = CodeModeConfig::enabled();
            config.sql_allow_writes = true;
            let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("UPDATE users SET active = 0", &ctx)
                .unwrap();

            assert!(!result.is_valid);
            assert!(result.violations.iter().any(|v| v.rule == "missing_where"));
        }

        #[test]
        fn rejects_blocked_table() {
            let mut config = CodeModeConfig::enabled();
            config.sql_blocked_tables.insert("secrets".to_string());
            let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("SELECT * FROM secrets LIMIT 10", &ctx)
                .unwrap();

            assert!(!result.is_valid);
            assert!(result.violations.iter().any(|v| v.rule == "blocked_table"));
        }

        #[test]
        fn rejects_non_allowlisted_table() {
            let mut config = CodeModeConfig::enabled();
            config.sql_allowed_tables.insert("users".to_string());
            let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
            let ctx = test_context();

            // "orders" is not in the allowlist
            let result = pipeline
                .validate_sql_query("SELECT id FROM orders LIMIT 10", &ctx)
                .unwrap();

            assert!(!result.is_valid);
            assert!(result
                .violations
                .iter()
                .any(|v| v.rule == "table_not_allowed"));
        }

        #[test]
        fn rejects_blocked_column() {
            let mut config = CodeModeConfig::enabled();
            config.sql_blocked_columns.insert("password".to_string());
            let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("SELECT id, password FROM users LIMIT 10", &ctx)
                .unwrap();

            assert!(!result.is_valid);
            assert!(result.violations.iter().any(|v| v.rule == "blocked_column"));
        }

        #[test]
        fn rejects_ddl_by_default() {
            let pipeline = sql_pipeline();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query("CREATE TABLE foo (id INT)", &ctx)
                .unwrap();

            assert!(!result.is_valid);
            assert!(result.violations.iter().any(|v| v.rule == "ddl_disabled"));
        }

        #[test]
        fn rejects_syntax_error() {
            let pipeline = sql_pipeline();
            let ctx = test_context();

            let result = pipeline.validate_sql_query("SELEC id FRM users", &ctx);

            assert!(matches!(result, Err(ValidationError::ParseError { .. })));
        }

        struct FixedDenyEvaluator {
            errors: Vec<String>,
        }

        #[async_trait::async_trait]
        impl PolicyEvaluator for FixedDenyEvaluator {
            async fn evaluate_operation(
                &self,
                _op: &crate::policy::OperationEntity,
                _cfg: &crate::policy::ServerConfigEntity,
            ) -> Result<crate::policy::AuthorizationDecision, crate::policy::PolicyEvaluationError>
            {
                Ok(crate::policy::AuthorizationDecision {
                    allowed: false,
                    determining_policies: vec![],
                    errors: self.errors.clone(),
                })
            }

            #[cfg(feature = "sql-code-mode")]
            async fn evaluate_statement(
                &self,
                _stmt: &crate::policy::StatementEntity,
                _server: &crate::policy::SqlServerEntity,
            ) -> Result<crate::policy::AuthorizationDecision, crate::policy::PolicyEvaluationError>
            {
                Ok(crate::policy::AuthorizationDecision {
                    allowed: false,
                    determining_policies: vec![],
                    errors: self.errors.clone(),
                })
            }

            fn name(&self) -> &str {
                "fixed-deny-test"
            }
        }

        fn sql_pipeline_with_evaluator(evaluator: Arc<dyn PolicyEvaluator>) -> ValidationPipeline {
            let mut config = CodeModeConfig::enabled();
            config.server_id = Some("test-server".to_string());
            ValidationPipeline::with_policy_evaluator(
                config,
                b"test-secret-key!".to_vec(),
                evaluator,
            )
            .unwrap()
        }

        #[tokio::test]
        async fn default_deny_produces_synthetic_violation() {
            let evaluator =
                Arc::new(FixedDenyEvaluator { errors: vec![] }) as Arc<dyn PolicyEvaluator>;
            let pipeline = sql_pipeline_with_evaluator(evaluator);
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query_async("SELECT id FROM users LIMIT 10", &ctx)
                .await
                .unwrap();

            assert!(!result.is_valid);
            let default_deny = result
                .violations
                .iter()
                .find(|v| v.rule == "default_deny")
                .expect("expected a synthetic default_deny violation");
            assert!(default_deny.message.contains("test-server"));
            assert!(default_deny.message.contains("Read"));
        }

        #[tokio::test]
        async fn policy_errors_flow_to_violations() {
            let evaluator = Arc::new(FixedDenyEvaluator {
                errors: vec!["schema validation: missing required attribute X".to_string()],
            }) as Arc<dyn PolicyEvaluator>;
            let pipeline = sql_pipeline_with_evaluator(evaluator);
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query_async("SELECT id FROM users LIMIT 10", &ctx)
                .await
                .unwrap();

            assert!(!result.is_valid);
            let policy_error = result
                .violations
                .iter()
                .find(|v| v.rule == "evaluation_error")
                .expect("expected a policy_error violation");
            assert!(policy_error.message.contains("schema validation"));
        }

        #[test]
        fn rejects_excessive_joins() {
            let mut config = CodeModeConfig::enabled();
            config.sql_max_joins = 1;
            let pipeline = ValidationPipeline::new(config, b"test-secret-key!".to_vec()).unwrap();
            let ctx = test_context();

            let result = pipeline
                .validate_sql_query(
                    "SELECT u.id FROM users u \
                     JOIN orders o ON u.id = o.user_id \
                     JOIN items i ON o.id = i.order_id LIMIT 10",
                    &ctx,
                )
                .unwrap();

            assert!(!result.is_valid);
            assert!(result
                .violations
                .iter()
                .any(|v| v.rule == "excessive_joins"));
        }
    }
}