agent_control_specification_core 0.3.1-beta.0

Stateless Rust core for Agent Control Specification
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
//! AGT D3 cedar dispatcher surface.
//!
//! See `policy-engine/spec/SPECIFICATION.md` ยง12.4 for the normative
//! contract. This module provides three pieces:
//!
//! 1. [`CedarPolicyDispatcher`] is the trait a host implements to evaluate a
//!    [`CedarPolicyInvocation`]. It is parallel to the rego dispatcher path
//!    that lives in [`crate::opa`]; both ultimately satisfy the runtime
//!    [`crate::PolicyDispatcher`] trait so the [`crate::Runtime`] can call
//!    into either backend uniformly.
//! 2. [`CedarTestDispatcher`] is a deterministic test double, always
//!    compiled, that parses a small JSON pseudo-cedar policy set, builds a
//!    cedar [`CedarRequest`] from the policy input per D3.2, and emits an
//!    `allow`, `deny`, or advice-translated verdict per D3.3.
//! 3. [`CedarBuiltinDispatcher`] is the AGT M2.S5 D7 feature-gated bundled
//!    dispatcher backed by the upstream `cedar-policy` crate. It is gated
//!    behind the `cedar` Cargo feature so callers that do not want the
//!    heavyweight cedar dep can opt out at build time.
//!
//! The dispatcher returns a verdict-shaped `JsonValue` exactly like the OPA
//! dispatcher does, and the runtime then normalizes the value via
//! [`crate::normalize_policy_output`]. Errors fail closed with the matching
//! reserved reason from `RuntimeError`.

use crate::{
    constants::policy_input as pi_key, runtime::PolicyDispatcher, CedarPolicyInvocation, JsonValue,
    PreparedPolicyInvocation, RuntimeError,
};
use serde::Deserialize;
use serde_json::{json, Map};

/// Cedar dispatcher contract. Implementations evaluate a prepared cedar
/// invocation and return a verdict-shaped `JsonValue` that the runtime feeds
/// to [`crate::normalize_policy_output`]. Errors fail closed with
/// `runtime_error:policy_invocation_failed` or `runtime_error:policy_output_invalid`.
pub trait CedarPolicyDispatcher: Send + Sync {
    fn evaluate_cedar(&self, invocation: &CedarPolicyInvocation)
        -> Result<JsonValue, RuntimeError>;
}

/// Cedar request derived from the policy input per AGT D3.2 default mapping.
/// The dispatcher is responsible for translating this into the cedar crate's
/// native `Request` type when evaluating against the upstream engine.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CedarRequest {
    pub principal: CedarEntity,
    pub action: CedarEntity,
    pub resource: CedarEntity,
    pub context_keys: Vec<String>,
}

/// Cedar entity reference of the form `Type::"id"`. The test dispatcher uses
/// this string form directly; the builtin dispatcher parses it into the
/// upstream cedar crate's `EntityUid` when evaluating.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CedarEntity {
    pub kind: String,
    pub id: String,
}

impl CedarEntity {
    pub fn new(kind: impl Into<String>, id: impl Into<String>) -> Self {
        Self {
            kind: kind.into(),
            id: id.into(),
        }
    }

    pub fn as_display(&self) -> String {
        format!("{}::\"{}\"", self.kind, self.id)
    }
}

/// Build the cedar request per AGT D3.2 default mapping. Returns
/// `runtime_error:policy_invocation_failed` when the input is missing the
/// envelope identifiers required by [`spec/agt/AGT-SNAPSHOT-1.0.md`] ยง1.
pub fn build_cedar_request(policy_input: &JsonValue) -> Result<CedarRequest, RuntimeError> {
    let object = policy_input.as_object().ok_or_else(|| {
        RuntimeError::PolicyInvocationFailed(
            "cedar dispatcher received non-object policy input".to_string(),
        )
    })?;

    let snapshot = object
        .get(pi_key::SNAPSHOT)
        .and_then(JsonValue::as_object)
        .ok_or_else(|| {
            RuntimeError::PolicyInvocationFailed(
                "cedar policy input is missing snapshot object".to_string(),
            )
        })?;

    let envelope = snapshot
        .get("envelope")
        .and_then(JsonValue::as_object)
        .ok_or_else(|| {
            RuntimeError::PolicyInvocationFailed(
                "cedar policy input snapshot is missing the AGT envelope".to_string(),
            )
        })?;

    let agent_id = envelope
        .get("agent")
        .and_then(JsonValue::as_object)
        .and_then(|agent| agent.get("id"))
        .and_then(JsonValue::as_str)
        .ok_or_else(|| {
            RuntimeError::PolicyInvocationFailed(
                "cedar policy input envelope is missing agent.id".to_string(),
            )
        })?;

    let intervention_point = object
        .get(pi_key::INTERVENTION_POINT)
        .and_then(JsonValue::as_str)
        .ok_or_else(|| {
            RuntimeError::PolicyInvocationFailed(
                "cedar policy input is missing intervention_point".to_string(),
            )
        })?;

    let resource = resource_entity(object);

    let mut context_keys: Vec<String> = snapshot
        .keys()
        .filter(|key| *key != "envelope")
        .cloned()
        .collect();
    if let Some(JsonValue::Object(annotations)) = object.get(pi_key::ANNOTATIONS) {
        for key in annotations.keys() {
            let key = format!("annotations.{key}");
            if !context_keys.contains(&key) {
                context_keys.push(key);
            }
        }
    }
    context_keys.sort();

    Ok(CedarRequest {
        principal: CedarEntity::new("Agent", agent_id),
        action: CedarEntity::new("Action", intervention_point),
        resource,
        context_keys,
    })
}

fn resource_entity(policy_input: &Map<String, JsonValue>) -> CedarEntity {
    if let Some(JsonValue::Object(tool)) = policy_input.get(pi_key::TOOL) {
        if let Some(name) = tool.get("name").and_then(JsonValue::as_str) {
            return CedarEntity::new("Tool", name);
        }
    }
    let kind = policy_input
        .get(pi_key::POLICY_TARGET)
        .and_then(JsonValue::as_object)
        .and_then(|target| target.get(pi_key::KIND))
        .and_then(JsonValue::as_str)
        .unwrap_or("unspecified");
    CedarEntity::new("PolicyTarget", kind)
}

/// Deterministic cedar test dispatcher. The dispatcher parses
/// [`CedarPolicyInvocation::policy_set`] as a small JSON pseudo-cedar
/// document, builds a [`CedarRequest`] from the policy input per
/// [`build_cedar_request`], and applies the rules with a simple equality
/// match. This is the test double tests can drive without linking the
/// upstream cedar crate. It satisfies the AGT M2.S2 D3.3 contract for
/// allow / deny / advice translation.
///
/// The pseudo-cedar JSON shape is:
///
/// ```jsonc
/// {
///   "rules": [
///     { "effect": "forbid", "principal": "any", "action": "Action::\"pre_tool_call\"", "resource": "Tool::\"banned\"" },
///     { "effect": "permit", "principal": "any", "action": "any", "resource": "any" },
///     { "effect": "permit", "principal": "Agent::\"alice\"", "action": "Action::\"output\"", "resource": "PolicyTarget::\"assistant_output\"",
///       "advice": { "verdict": "warn", "reason": "needs_review" } }
///   ]
/// }
/// ```
///
/// Rules are scanned in declared order; the first `forbid` match wins.
/// Otherwise the first `permit` match wins. A permit rule MAY carry an
/// `advice` object, which is validated against the AGT D3.3 cedar advice
/// shape and translated into the corresponding verdict.
#[derive(Debug, Clone, Default)]
pub struct CedarTestDispatcher;

impl CedarTestDispatcher {
    pub fn new() -> Self {
        Self
    }
}

impl CedarPolicyDispatcher for CedarTestDispatcher {
    fn evaluate_cedar(
        &self,
        invocation: &CedarPolicyInvocation,
    ) -> Result<JsonValue, RuntimeError> {
        let policy_set_text = invocation.policy_set.as_deref().ok_or_else(|| {
            RuntimeError::PolicyInvocationFailed(
                "cedar test dispatcher requires an inline policy_set; policy_path is reserved for the builtin dispatcher".to_string(),
            )
        })?;
        let policy_set = parse_test_policy_set(policy_set_text)?;
        let request = build_cedar_request(&invocation.input)?;
        match policy_set.decide(&request) {
            TestDecision::Forbid(reason) => Ok(json!({
                "decision": "deny",
                "reason": reason,
            })),
            TestDecision::Permit { advice: None } => Ok(json!({ "decision": "allow" })),
            TestDecision::Permit {
                advice: Some(advice),
            } => translate_advice(advice),
            TestDecision::NoMatch => Ok(json!({
                "decision": "deny",
                "reason": "no_matching_policy",
            })),
        }
    }
}

impl PolicyDispatcher for CedarTestDispatcher {
    fn evaluate(&self, invocation: &PreparedPolicyInvocation) -> Result<JsonValue, RuntimeError> {
        match invocation {
            PreparedPolicyInvocation::Cedar(invocation) => self.evaluate_cedar(invocation),
            other => Err(RuntimeError::PolicyInvocationFailed(format!(
                "cedar test dispatcher only supports Cedar invocations; received {} invocation",
                other.engine_type()
            ))),
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
struct TestPolicySetDoc {
    #[serde(default)]
    rules: Vec<TestRuleDoc>,
}

#[derive(Debug, Clone, Deserialize)]
struct TestRuleDoc {
    effect: TestEffectDoc,
    #[serde(default)]
    principal: Option<String>,
    #[serde(default)]
    action: Option<String>,
    #[serde(default)]
    resource: Option<String>,
    #[serde(default)]
    reason: Option<String>,
    #[serde(default)]
    advice: Option<JsonValue>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
enum TestEffectDoc {
    Permit,
    Forbid,
}

#[derive(Debug, Clone)]
struct TestPolicySet {
    rules: Vec<TestRule>,
}

#[derive(Debug, Clone)]
struct TestRule {
    effect: TestEffectDoc,
    principal: Option<CedarEntity>,
    action: Option<CedarEntity>,
    resource: Option<CedarEntity>,
    reason: Option<String>,
    advice: Option<JsonValue>,
}

#[derive(Debug)]
enum TestDecision {
    Forbid(String),
    Permit { advice: Option<JsonValue> },
    NoMatch,
}

impl TestPolicySet {
    fn decide(&self, request: &CedarRequest) -> TestDecision {
        let mut permit: Option<&TestRule> = None;
        for rule in &self.rules {
            if !rule.matches(request) {
                continue;
            }
            match rule.effect {
                TestEffectDoc::Forbid => {
                    return TestDecision::Forbid(
                        rule.reason
                            .clone()
                            .unwrap_or_else(|| "forbid_rule_matched".to_string()),
                    );
                }
                TestEffectDoc::Permit if permit.is_none() => {
                    permit = Some(rule);
                }
                TestEffectDoc::Permit => {}
            }
        }
        match permit {
            Some(rule) => TestDecision::Permit {
                advice: rule.advice.clone(),
            },
            None => TestDecision::NoMatch,
        }
    }
}

impl TestRule {
    fn matches(&self, request: &CedarRequest) -> bool {
        entity_matches(self.principal.as_ref(), &request.principal)
            && entity_matches(self.action.as_ref(), &request.action)
            && entity_matches(self.resource.as_ref(), &request.resource)
    }
}

fn entity_matches(pattern: Option<&CedarEntity>, actual: &CedarEntity) -> bool {
    match pattern {
        None => true,
        Some(entity) => entity == actual,
    }
}

fn parse_test_policy_set(text: &str) -> Result<TestPolicySet, RuntimeError> {
    let doc: TestPolicySetDoc = serde_json::from_str(text).map_err(|err| {
        RuntimeError::PolicyInvocationFailed(format!(
            "cedar test dispatcher failed to parse policy_set as JSON: {err}"
        ))
    })?;
    let mut rules = Vec::with_capacity(doc.rules.len());
    for (index, rule) in doc.rules.into_iter().enumerate() {
        rules.push(TestRule {
            effect: rule.effect,
            principal: parse_entity_pattern("principal", index, rule.principal.as_deref())?,
            action: parse_entity_pattern("action", index, rule.action.as_deref())?,
            resource: parse_entity_pattern("resource", index, rule.resource.as_deref())?,
            reason: rule.reason,
            advice: rule.advice,
        });
    }
    Ok(TestPolicySet { rules })
}

fn parse_entity_pattern(
    field: &str,
    index: usize,
    text: Option<&str>,
) -> Result<Option<CedarEntity>, RuntimeError> {
    let raw = match text {
        None => return Ok(None),
        Some(value) => value.trim(),
    };
    if raw.is_empty() || raw.eq_ignore_ascii_case("any") || raw == "*" {
        return Ok(None);
    }
    let Some((kind, rest)) = raw.split_once("::") else {
        return Err(RuntimeError::PolicyInvocationFailed(format!(
            "cedar test policy rule {index} field '{field}' must be 'any' or 'Type::\"id\"', got '{raw}'"
        )));
    };
    let id = rest
        .trim_start_matches('"')
        .trim_end_matches('"')
        .to_string();
    if kind.trim().is_empty() || id.is_empty() {
        return Err(RuntimeError::PolicyInvocationFailed(format!(
            "cedar test policy rule {index} field '{field}' is missing a type or id: '{raw}'"
        )));
    }
    Ok(Some(CedarEntity::new(kind.trim(), id)))
}

/// Translate AGT D3.3 cedar advice into a verdict-shaped `JsonValue` ready
/// for [`crate::normalize_policy_output`]. Advice missing the `verdict`
/// field, advice with an unknown verdict value, or transform advice missing
/// its body fail closed with `runtime_error:policy_output_invalid`. Path
/// validation (rooted at `$policy_target`) is delegated to
/// [`crate::verdict::Transform::from_value`] inside `normalize_policy_output`,
/// which produces `runtime_error:transform_target_forbidden` for an
/// out-of-target path.
pub fn translate_advice(advice: JsonValue) -> Result<JsonValue, RuntimeError> {
    let object = advice.as_object().ok_or_else(|| {
        RuntimeError::PolicyOutputInvalid("cedar advice must be a JSON object".to_string())
    })?;

    let verdict = object
        .get("verdict")
        .and_then(JsonValue::as_str)
        .ok_or_else(|| {
            RuntimeError::PolicyOutputInvalid(
                "cedar advice is missing the required 'verdict' field".to_string(),
            )
        })?;
    if !matches!(verdict, "warn" | "escalate" | "transform") {
        return Err(RuntimeError::PolicyOutputInvalid(format!(
            "cedar advice 'verdict' must be one of warn, escalate, transform; got '{verdict}'"
        )));
    }

    let mut out = Map::new();
    out.insert(
        "decision".to_string(),
        JsonValue::String(verdict.to_string()),
    );

    if let Some(reason) = object.get("reason") {
        match reason {
            JsonValue::Null => {}
            JsonValue::String(_) => {
                out.insert("reason".to_string(), reason.clone());
            }
            _ => {
                return Err(RuntimeError::PolicyOutputInvalid(
                    "cedar advice 'reason' must be a string".to_string(),
                ))
            }
        }
    }
    if let Some(message) = object.get("message") {
        match message {
            JsonValue::Null => {}
            JsonValue::String(_) => {
                out.insert("message".to_string(), message.clone());
            }
            _ => {
                return Err(RuntimeError::PolicyOutputInvalid(
                    "cedar advice 'message' must be a string".to_string(),
                ))
            }
        }
    }

    if verdict == "transform" {
        let transform = object.get("transform").ok_or_else(|| {
            RuntimeError::PolicyOutputInvalid(
                "cedar advice with verdict 'transform' requires a transform object".to_string(),
            )
        })?;
        if !transform.is_object() {
            return Err(RuntimeError::PolicyOutputInvalid(
                "cedar advice 'transform' must be a JSON object".to_string(),
            ));
        }
        out.insert("transform".to_string(), transform.clone());
    } else if object.contains_key("transform") {
        return Err(RuntimeError::PolicyOutputInvalid(
            "cedar advice 'transform' is only permitted when verdict is 'transform'".to_string(),
        ));
    }

    Ok(JsonValue::Object(out))
}

/// AGT M2.S5 D7 bundled cedar dispatcher backed by the upstream
/// `cedar-policy` crate. Gated behind the `cedar` Cargo feature so that
/// hosts that never need real cedar evaluation do not have to compile the
/// cedar runtime. The dispatcher parses the inline `policy_set` text (or
/// the file pointed to by `policy_path`), builds a `cedar_policy::Request`
/// from the [`CedarRequest`] produced by [`build_cedar_request`], and runs
/// the upstream authorizer. The result is translated into the verdict
/// JSON the runtime feeds to [`crate::normalize_policy_output`]: a cedar
/// `Allow` becomes `{"decision":"allow"}` and a cedar `Deny` becomes
/// `{"decision":"deny","reason":"no_matching_policy"}` (or
/// `runtime_error:policy_invocation_failed` when the authorizer surfaces a
/// hard error).
///
/// Cedar policy annotations and richer advice translation remain the job
/// of the host-facing [`CedarPolicyDispatcher`] implementations; the
/// builtin restricts itself to the standard allow / deny contract that the
/// upstream `Authorizer::is_authorized` exposes.
#[cfg(feature = "cedar")]
#[derive(Debug, Clone, Default)]
pub struct CedarBuiltinDispatcher;

#[cfg(feature = "cedar")]
impl CedarBuiltinDispatcher {
    pub fn new() -> Self {
        Self
    }
}

#[cfg(feature = "cedar")]
impl CedarPolicyDispatcher for CedarBuiltinDispatcher {
    fn evaluate_cedar(
        &self,
        invocation: &CedarPolicyInvocation,
    ) -> Result<JsonValue, RuntimeError> {
        builtin::evaluate(invocation)
    }
}

#[cfg(feature = "cedar")]
impl PolicyDispatcher for CedarBuiltinDispatcher {
    fn evaluate(&self, invocation: &PreparedPolicyInvocation) -> Result<JsonValue, RuntimeError> {
        match invocation {
            PreparedPolicyInvocation::Cedar(invocation) => self.evaluate_cedar(invocation),
            other => Err(RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher only supports Cedar invocations; received {} invocation",
                other.engine_type()
            ))),
        }
    }
}

#[cfg(feature = "cedar")]
mod builtin {
    //! Upstream-cedar evaluation helpers for [`super::CedarBuiltinDispatcher`].
    //!
    //! Kept in a private module so the cedar crate imports never leak into
    //! the public API surface even when the `cedar` feature is enabled.

    use super::{build_cedar_request, CedarEntity, CedarRequest};
    use crate::{CedarPolicyInvocation, JsonValue, RuntimeError};
    use cedar_policy::{
        Authorizer, Context, Decision, Entities, EntityUid, PolicySet, Request, Schema,
        ValidationMode, Validator,
    };
    use serde_json::json;
    use std::{fs, str::FromStr};

    pub(super) fn evaluate(invocation: &CedarPolicyInvocation) -> Result<JsonValue, RuntimeError> {
        let policy_text = load_policy_text(invocation)?;
        let policy_set = PolicySet::from_str(&policy_text).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to parse policy_set: {err}"
            ))
        })?;
        let schema = load_schema(invocation.schema_path.as_deref())?;
        validate_policy_set(&policy_set, schema.as_ref())?;
        let entities = load_entities(invocation.entities_path.as_deref(), schema.as_ref())?;
        let request = build_cedar_request(&invocation.input)?;
        let cedar_request = build_authorizer_request(&request, schema.as_ref())?;

        let authorizer = Authorizer::new();
        let answer = authorizer.is_authorized(&cedar_request, &policy_set, &entities);
        let hard_errors = answer.diagnostics().errors().cloned().collect::<Vec<_>>();
        if !hard_errors.is_empty() {
            let detail = hard_errors
                .iter()
                .map(|err| err.to_string())
                .collect::<Vec<_>>()
                .join("; ");
            return Err(RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher authorizer reported errors: {detail}"
            )));
        }

        match answer.decision() {
            Decision::Allow => Ok(json!({ "decision": "allow" })),
            Decision::Deny => Ok(json!({
                "decision": "deny",
                "reason": "no_matching_policy",
            })),
        }
    }

    fn load_policy_text(invocation: &CedarPolicyInvocation) -> Result<String, RuntimeError> {
        match (
            invocation.policy_set.as_deref(),
            invocation.policy_path.as_deref(),
        ) {
            (Some(text), None) => Ok(text.to_string()),
            (None, Some(path)) => fs::read_to_string(path).map_err(|err| {
                RuntimeError::PolicyInvocationFailed(format!(
                    "cedar builtin dispatcher failed to read policy_path '{path}': {err}"
                ))
            }),
            (Some(_), Some(_)) => Err(RuntimeError::PolicyInvocationFailed(
                "cedar builtin dispatcher received both policy_set and policy_path; manifest validation must reject this earlier".to_string(),
            )),
            (None, None) => Err(RuntimeError::PolicyInvocationFailed(
                "cedar builtin dispatcher requires either policy_set or policy_path".to_string(),
            )),
        }
    }

    fn load_schema(path: Option<&str>) -> Result<Option<Schema>, RuntimeError> {
        let Some(path) = path else {
            return Ok(None);
        };
        let json_text = fs::read_to_string(path).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to read schema_path '{path}': {err}"
            ))
        })?;
        let schema = Schema::from_json_str(&json_text).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to parse schema_path '{path}': {err}"
            ))
        })?;
        Ok(Some(schema))
    }

    fn validate_policy_set(
        policy_set: &PolicySet,
        schema: Option<&Schema>,
    ) -> Result<(), RuntimeError> {
        let Some(schema) = schema else {
            return Ok(());
        };
        let result = Validator::new(schema.clone()).validate(policy_set, ValidationMode::Strict);
        if result.validation_passed() {
            Ok(())
        } else {
            Err(RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher policy_set failed schema validation: {result}"
            )))
        }
    }

    fn load_entities(
        path: Option<&str>,
        schema: Option<&Schema>,
    ) -> Result<Entities, RuntimeError> {
        let Some(path) = path else {
            return Ok(Entities::empty());
        };
        let json_text = fs::read_to_string(path).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to read entities_path '{path}': {err}"
            ))
        })?;
        Entities::from_json_str(&json_text, schema).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to parse entities at '{path}': {err}"
            ))
        })
    }

    fn build_authorizer_request(
        request: &CedarRequest,
        schema: Option<&Schema>,
    ) -> Result<Request, RuntimeError> {
        let principal = entity_uid(&request.principal, "principal")?;
        let action = entity_uid(&request.action, "action")?;
        let resource = entity_uid(&request.resource, "resource")?;
        Request::new(principal, action, resource, Context::empty(), schema).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to build authorizer request: {err}"
            ))
        })
    }

    fn entity_uid(entity: &CedarEntity, field: &str) -> Result<EntityUid, RuntimeError> {
        EntityUid::from_str(&entity.as_display()).map_err(|err| {
            RuntimeError::PolicyInvocationFailed(format!(
                "cedar builtin dispatcher failed to parse {field} entity '{}': {err}",
                entity.as_display()
            ))
        })
    }
}

#[cfg(test)]
mod tests {
    //! AGT M2.S2 D3.3 dispatcher behaviour tests. Each test drives the
    //! [`CedarTestDispatcher`] against a hand-crafted policy input that
    //! mirrors the AGT snapshot shape from `spec/agt/AGT-SNAPSHOT-1.0.md` ยง1
    //! and asserts the verdict the runtime would emit after normalizing the
    //! dispatcher's JsonValue through [`crate::normalize_policy_output`].

    use super::*;
    use crate::{normalize_policy_output, Decision};
    use serde_json::json;
    use std::{
        fs,
        path::{Path, PathBuf},
    };

    fn invocation(policy_set: &str, input: JsonValue) -> CedarPolicyInvocation {
        CedarPolicyInvocation {
            policy_set: Some(policy_set.to_string()),
            policy_path: None,
            entities_path: None,
            schema_path: None,
            query: None,
            input: input.clone(),
            canonical_input: serde_json::to_string(&input).unwrap(),
        }
    }

    fn tool_input(agent_id: &str, tool_name: &str) -> JsonValue {
        json!({
            "intervention_point": "pre_tool_call",
            "policy_target": {
                "kind": "tool_args",
                "path": "$snap.tool_call.args",
                "value": {"q": "hello"}
            },
            "snapshot": {
                "envelope": {
                    "agent": {"id": agent_id, "version": "1.0", "name": agent_id},
                    "session": {"id": "sess-1", "started_at": "2026-01-01T00:00:00Z"},
                    "intervention_point": "pre_tool_call",
                    "timestamp": "2026-01-01T00:00:01Z",
                    "budgets": {"tool_call_count": 0, "token_count": 0, "elapsed_seconds": 0.0, "cost_usd": 0.0}
                },
                "tool_call": {"name": tool_name, "args": {"q": "hello"}, "id": "call-1"}
            },
            "annotations": {},
            "tool": {"name": tool_name}
        })
    }

    #[cfg(feature = "cedar")]
    fn cedar_test_dir(name: &str) -> PathBuf {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("target")
            .join("cedar-dispatcher-tests")
            .join(name);
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&root).unwrap();
        root
    }

    #[cfg(feature = "cedar")]
    fn write_cedar_test_file(dir: &Path, name: &str, content: &str) -> String {
        let path = dir.join(name);
        fs::write(&path, content).unwrap();
        path.display().to_string()
    }

    #[cfg(feature = "cedar")]
    fn schema_for_tool_resource() -> &'static str {
        r#"{
            "": {
                "entityTypes": {
                    "Agent": {"shape": {"type": "Record", "attributes": {}}},
                    "Tool": {"shape": {"type": "Record", "attributes": {}}},
                    "PolicyTarget": {"shape": {"type": "Record", "attributes": {}}}
                },
                "actions": {
                    "pre_tool_call": {
                        "appliesTo": {
                            "principalTypes": ["Agent"],
                            "resourceTypes": ["Tool"]
                        }
                    }
                }
            }
        }"#
    }

    #[cfg(feature = "cedar")]
    fn schema_for_policy_target_resource() -> &'static str {
        r#"{
            "": {
                "entityTypes": {
                    "Agent": {"shape": {"type": "Record", "attributes": {}}},
                    "Tool": {"shape": {"type": "Record", "attributes": {}}},
                    "PolicyTarget": {"shape": {"type": "Record", "attributes": {}}}
                },
                "actions": {
                    "pre_tool_call": {
                        "appliesTo": {
                            "principalTypes": ["Agent"],
                            "resourceTypes": ["PolicyTarget"]
                        }
                    }
                }
            }
        }"#
    }

    // โ”€โ”€ D3.2 request mapping โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[test]
    fn build_cedar_request_maps_principal_action_resource_per_d32() {
        let input = tool_input("agent-x", "hello");
        let request = build_cedar_request(&input).expect("request built");
        assert_eq!(request.principal, CedarEntity::new("Agent", "agent-x"));
        assert_eq!(request.action, CedarEntity::new("Action", "pre_tool_call"));
        assert_eq!(request.resource, CedarEntity::new("Tool", "hello"));
        assert!(request.context_keys.contains(&"tool_call".to_string()));
    }

    #[test]
    fn build_cedar_request_uses_policy_target_kind_when_no_tool() {
        let input = json!({
            "intervention_point": "output",
            "policy_target": {"kind": "assistant_output", "path": "$snap.response", "value": {}},
            "snapshot": {
                "envelope": {
                    "agent": {"id": "agent-y"},
                    "session": {"id": "s"},
                    "intervention_point": "output",
                    "timestamp": "t",
                    "budgets": {}
                },
                "response": {"content": ""}
            },
            "annotations": {},
            "tool": null
        });
        let request = build_cedar_request(&input).expect("request built");
        assert_eq!(request.principal, CedarEntity::new("Agent", "agent-y"));
        assert_eq!(request.action, CedarEntity::new("Action", "output"));
        assert_eq!(
            request.resource,
            CedarEntity::new("PolicyTarget", "assistant_output")
        );
    }

    #[test]
    fn build_cedar_request_fails_closed_when_envelope_missing_agent_id() {
        let input = json!({
            "intervention_point": "input",
            "policy_target": {"kind": "user_input", "path": "$snap.input", "value": {}},
            "snapshot": {"envelope": {"agent": {}}},
            "annotations": {},
            "tool": null
        });
        let error = build_cedar_request(&input).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
    }

    // โ”€โ”€ D3.3 allow / deny โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[test]
    fn test_dispatcher_allow_path() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any"}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Allow);
    }

    #[test]
    fn test_dispatcher_deny_path() {
        let policy_set = r#"{
            "rules": [
                {"effect": "forbid", "principal": "any", "action": "Action::\"pre_tool_call\"", "resource": "Tool::\"banned\"", "reason": "tool_banned"},
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any"}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "banned"));
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Deny);
        assert_eq!(verdict.reason.as_deref(), Some("tool_banned"));
    }

    #[test]
    fn test_dispatcher_no_matching_rule_denies() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "Agent::\"alice\"", "action": "any", "resource": "any"}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("bob", "hello"));
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Deny);
        assert_eq!(verdict.reason.as_deref(), Some("no_matching_policy"));
    }

    // โ”€โ”€ D3.3 advice translation โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[test]
    fn test_dispatcher_advice_translates_to_transform() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "transform", "reason": "scrub_pii",
                            "transform": {"path": "$policy_target.value.q", "value": "[REDACTED]"}}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Transform);
        let transform = verdict.transform.as_ref().expect("transform present");
        assert_eq!(transform.path, "$policy_target.value.q");
        assert_eq!(transform.value, json!("[REDACTED]"));
        assert_eq!(verdict.reason.as_deref(), Some("scrub_pii"));
    }

    #[test]
    fn test_dispatcher_advice_translates_to_escalate() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "escalate", "reason": "human_review", "message": "needs sign-off"}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Escalate);
        assert_eq!(verdict.reason.as_deref(), Some("human_review"));
        assert_eq!(verdict.message.as_deref(), Some("needs sign-off"));
    }

    #[test]
    fn test_dispatcher_advice_translates_to_warn() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "warn", "reason": "low_confidence"}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Warn);
        assert_eq!(verdict.reason.as_deref(), Some("low_confidence"));
    }

    // โ”€โ”€ D3.3 malformed advice โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[test]
    fn test_dispatcher_malformed_advice_missing_verdict_fails_closed() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"reason": "no_verdict_field"}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let error = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    #[test]
    fn test_dispatcher_malformed_advice_unknown_verdict_fails_closed() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "approve"}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let error = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    #[test]
    fn test_dispatcher_transform_advice_without_body_fails_closed() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "transform"}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let error = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    #[test]
    fn test_dispatcher_warn_advice_with_transform_body_fails_closed() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "warn",
                            "transform": {"path": "$policy_target.value", "value": "x"}}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let error = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    // โ”€โ”€ D1.1 transform target confinement โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[test]
    fn test_dispatcher_transform_path_outside_policy_target_fails_closed() {
        let policy_set = r#"{
            "rules": [
                {"effect": "permit", "principal": "any", "action": "any", "resource": "any",
                 "advice": {"verdict": "transform",
                            "transform": {"path": "$snap.tool_call.args.q", "value": "[REDACTED]"}}}
            ]
        }"#;
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        // The dispatcher emits the verdict JSON verbatim; the runtime's
        // normalize_policy_output is what enforces $policy_target confinement
        // per AGT D1.1, returning runtime_error:transform_target_forbidden.
        let output = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap();
        let error = normalize_policy_output(output).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:transform_target_forbidden");
    }

    // โ”€โ”€ Dispatcher error paths โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[test]
    fn test_dispatcher_requires_inline_policy_set() {
        let inv = CedarPolicyInvocation {
            policy_set: None,
            policy_path: Some("/no/such/file.cedar".to_string()),
            entities_path: None,
            schema_path: None,
            query: None,
            input: tool_input("agent-1", "hello"),
            canonical_input: "{}".to_string(),
        };
        let error = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
    }

    #[test]
    fn test_dispatcher_invalid_policy_set_json_fails_closed() {
        let inv = invocation("not json", tool_input("agent-1", "hello"));
        let error = CedarTestDispatcher::new().evaluate_cedar(&inv).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
    }

    #[test]
    fn test_dispatcher_rejects_non_cedar_invocation_through_policy_dispatcher() {
        use crate::{PolicyDispatcher, PreparedPolicyInvocation, TestPolicyInvocation};

        let other = PreparedPolicyInvocation::Test(TestPolicyInvocation {
            adapter_config: Default::default(),
            input: json!({}),
            canonical_input: "{}".to_string(),
        });
        let error = CedarTestDispatcher::new().evaluate(&other).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
    }

    // โ”€โ”€ translate_advice unit checks (independent of the dispatcher) โ”€โ”€

    #[test]
    fn translate_advice_rejects_non_object() {
        let error = translate_advice(json!("warn")).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    #[test]
    fn translate_advice_rejects_non_string_reason() {
        let error = translate_advice(json!({"verdict": "warn", "reason": 7})).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    #[test]
    fn translate_advice_round_trips_warn() {
        let value = translate_advice(json!({"verdict": "warn"})).unwrap();
        assert_eq!(value["decision"], json!("warn"));
    }

    // โ”€โ”€ M2.S5 D7 builtin dispatcher (feature `cedar`) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_allows_trivial_permit_all_policy() {
        let policy_set = "permit(principal, action, resource);";
        let inv = invocation(policy_set, tool_input("agent-1", "hello"));
        let output = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .expect("builtin cedar dispatcher returns ok for permit-all");
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Allow);
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_denies_when_no_policy_matches() {
        let policy_set = "permit(principal == Agent::\"alice\", action, resource);";
        let inv = invocation(policy_set, tool_input("bob", "hello"));
        let output = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .expect("builtin cedar dispatcher returns ok for deny");
        let verdict = normalize_policy_output(output).unwrap();
        assert_eq!(verdict.decision, Decision::Deny);
        assert_eq!(verdict.reason.as_deref(), Some("no_matching_policy"));
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_with_valid_schema_accepts_conformant_request() {
        let dir = cedar_test_dir("valid-schema-accepts");
        let schema_path = write_cedar_test_file(&dir, "schema.json", schema_for_tool_resource());
        let mut inv = invocation(
            "permit(principal, action == Action::\"pre_tool_call\", resource == Tool::\"hello\");",
            tool_input("agent-1", "hello"),
        );
        inv.schema_path = Some(schema_path);

        let output = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .expect("schema-conformant cedar request should evaluate");
        let verdict = normalize_policy_output(output).unwrap();

        assert_eq!(verdict.decision, Decision::Allow);
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_with_valid_schema_rejects_nonconformant_request() {
        let dir = cedar_test_dir("valid-schema-rejects");
        let schema_path =
            write_cedar_test_file(&dir, "schema.json", schema_for_policy_target_resource());
        let mut inv = invocation(
            "permit(principal, action == Action::\"pre_tool_call\", resource);",
            tool_input("agent-1", "hello"),
        );
        inv.schema_path = Some(schema_path);

        let error = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .unwrap_err();

        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
        assert!(
            error.detail().contains("authorizer request")
                || error.detail().contains("failed schema validation"),
            "{}",
            error.detail()
        );
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_fails_closed_when_schema_path_is_missing() {
        let missing_schema = cedar_test_dir("missing-schema").join("missing-schema.json");
        let mut inv = invocation(
            "permit(principal, action, resource);",
            tool_input("agent-1", "hello"),
        );
        inv.schema_path = Some(missing_schema.display().to_string());
        let error = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
        assert!(error.detail().contains("schema_path"));
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_fails_closed_when_schema_path_is_malformed() {
        let dir = cedar_test_dir("malformed-schema");
        let schema_path = write_cedar_test_file(&dir, "schema.json", "{not valid schema json");
        let mut inv = invocation(
            "permit(principal, action, resource);",
            tool_input("agent-1", "hello"),
        );
        inv.schema_path = Some(schema_path);

        let error = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .unwrap_err();

        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
        assert!(error.detail().contains("parse schema_path"));
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_rejects_non_cedar_invocation_through_policy_dispatcher() {
        use crate::{PolicyDispatcher, PreparedPolicyInvocation, TestPolicyInvocation};

        let other = PreparedPolicyInvocation::Test(TestPolicyInvocation {
            adapter_config: Default::default(),
            input: json!({}),
            canonical_input: "{}".to_string(),
        });
        let error = CedarBuiltinDispatcher::new().evaluate(&other).unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
    }

    #[cfg(feature = "cedar")]
    #[test]
    fn builtin_dispatcher_surfaces_parser_errors_as_policy_invocation_failed() {
        let inv = invocation("not a valid cedar policy", tool_input("agent-1", "hello"));
        let error = CedarBuiltinDispatcher::new()
            .evaluate_cedar(&inv)
            .unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_invocation_failed");
    }
}