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
pub type JsonValue = serde_json::Value;

pub mod annotation;
pub mod cedar;
mod constants;
pub use constants::reserved_reason;
#[cfg(feature = "default-dispatchers")]
pub mod dispatchers;
// AGT D1: effects are no longer part of the public verdict surface. The
// module stays compiled so its types remain available to internal callers
// during the M2 sunset, but it is not exported. See
// `policy-engine/spec/SPECIFICATION.md` ยง14.
pub(crate) mod effects;
pub mod error;
pub mod ffi;
pub mod intervention_point;
pub mod limits;
pub mod manifest;
// AGT M2.S5 D7: the OPA CLI dispatcher lives behind the `opa` feature so
// hosts that never use Rego do not see the Rego dispatcher surface. The
// `PolicyConfig::Rego` manifest grammar stays compiled unconditionally so
// manifest validation still accepts `type: rego` even when the bundled
// dispatcher is absent; hosts that need Rego evaluation under those
// conditions must wire their own `PolicyDispatcher` implementation.
#[cfg(feature = "opa")]
pub mod opa;
pub mod paths;
pub mod perf_telemetry;
pub mod policy;
pub mod policy_input;
pub mod runtime;
pub mod telemetry;
pub mod tool_projection;
pub mod verdict;

pub use annotation::{
    AnnotationConfig, AnnotatorConfig, AnnotatorDispatcher, AnnotatorInvocation, AnnotatorType,
};
#[cfg(feature = "cedar")]
pub use cedar::CedarBuiltinDispatcher;
pub use cedar::{
    build_cedar_request, translate_advice, CedarEntity, CedarPolicyDispatcher, CedarRequest,
    CedarTestDispatcher,
};
#[cfg(feature = "default-dispatchers")]
pub use dispatchers::{
    ClassifierAnnotator, DefaultAnnotatorDispatcher, EndpointAnnotator, LlmAnnotator,
};
pub use error::RuntimeError;
pub use intervention_point::{EnforcementMode, InterventionPoint};
pub use limits::Limits;
pub use manifest::{
    ApprovalOnTimeout, ApprovalResolverConfig, ApprovalSection, InterventionPointConfig, Manifest,
    ToolConfig,
};
#[cfg(feature = "opa")]
pub use opa::{OpaPolicyDispatcher, OpaRegoRunner};
pub use paths::{JsonPath, PathEnv, PathParseError, PathRoot, PathSegment};
pub use perf_telemetry::PerfTelemetry;
pub use policy::{
    CedarPolicyConfig, CedarPolicyInvocation, CustomPolicyConfig, CustomPolicyInvocation,
    PolicyBinding, PolicyConfig, PreparedPolicyInvocation, RegoPolicyConfig, RegoPolicyInvocation,
    TestPolicyConfig, TestPolicyInvocation,
};
pub use policy_input::{action_identity, build_policy_input, canonical_json};
pub use runtime::{InterventionPointRequest, InterventionPointResult, PolicyDispatcher, Runtime};
pub use telemetry::{NoopTelemetrySink, TelemetryEvent, TelemetryEventType, TelemetrySink};
pub use verdict::{normalize_policy_output, Decision, Evidence, Transform, Verdict};

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex,
    };

    #[derive(Default)]
    struct StaticAnnotator {
        output: JsonValue,
        seen: Mutex<Vec<JsonValue>>,
        error: Option<RuntimeError>,
    }

    impl AnnotatorDispatcher for StaticAnnotator {
        fn dispatch(
            &self,
            _annotator_name: &str,
            _annotator: &AnnotatorInvocation,
            preliminary_policy_input: &JsonValue,
        ) -> Result<JsonValue, RuntimeError> {
            self.seen
                .lock()
                .unwrap()
                .push(preliminary_policy_input.clone());
            if let Some(error) = &self.error {
                Err(error.clone())
            } else {
                Ok(self.output.clone())
            }
        }
    }

    struct StaticPolicy {
        output: JsonValue,
        seen: Mutex<Vec<JsonValue>>,
        error: Option<RuntimeError>,
    }

    impl StaticPolicy {
        fn allow() -> Self {
            Self {
                output: json!({"decision": "allow"}),
                seen: Mutex::new(Vec::new()),
                error: None,
            }
        }

        fn with_output(output: JsonValue) -> Self {
            Self {
                output,
                seen: Mutex::new(Vec::new()),
                error: None,
            }
        }
    }

    impl PolicyDispatcher for StaticPolicy {
        fn evaluate(
            &self,
            invocation: &PreparedPolicyInvocation,
        ) -> Result<JsonValue, RuntimeError> {
            let final_policy_input = invocation.policy_input().unwrap();
            self.seen.lock().unwrap().push(final_policy_input.clone());
            if let Some(error) = &self.error {
                Err(error.clone())
            } else {
                Ok(self.output.clone())
            }
        }
    }

    struct OncePanicAnnotator {
        panicked: AtomicBool,
    }

    impl OncePanicAnnotator {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                panicked: AtomicBool::new(false),
            })
        }
    }

    impl AnnotatorDispatcher for OncePanicAnnotator {
        fn dispatch(
            &self,
            _annotator_name: &str,
            _annotator: &AnnotatorInvocation,
            _preliminary_policy_input: &JsonValue,
        ) -> Result<JsonValue, RuntimeError> {
            if !self.panicked.swap(true, Ordering::SeqCst) {
                panic!("annotator boom");
            }
            Ok(json!({"label": "safe"}))
        }
    }

    struct OncePanicPolicy {
        panicked: AtomicBool,
    }

    impl OncePanicPolicy {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                panicked: AtomicBool::new(false),
            })
        }
    }

    impl PolicyDispatcher for OncePanicPolicy {
        fn evaluate(
            &self,
            _invocation: &PreparedPolicyInvocation,
        ) -> Result<JsonValue, RuntimeError> {
            if !self.panicked.swap(true, Ordering::SeqCst) {
                panic!("policy boom");
            }
            Ok(json!({"decision": "allow"}))
        }
    }

    fn no_annotations() -> Arc<StaticAnnotator> {
        Arc::new(StaticAnnotator {
            output: JsonValue::Null,
            seen: Mutex::new(Vec::new()),
            error: None,
        })
    }

    fn manifest(input: &str) -> Manifest {
        Manifest::from_yaml_str(input).unwrap()
    }

    fn runtime(
        manifest: Manifest,
        annotations: Arc<StaticAnnotator>,
        policy: Arc<StaticPolicy>,
    ) -> Runtime {
        Runtime::new(manifest, annotations, policy).unwrap()
    }

    #[test]
    fn parses_and_resolves_deterministic_paths() {
        let snapshot = json!({
            "a": {"b": [{"c": 7}], "x.y": 11},
            "array": ["zero"]
        });

        let path = JsonPath::parse("$snap.a.b[0].c").unwrap();
        assert_eq!(
            path.resolve(&PathEnv::with_snap(&snapshot)).unwrap(),
            json!(7)
        );

        let literal = JsonPath::parse("$snap.a[\"x.y\"]").unwrap();
        assert_eq!(
            literal.resolve(&PathEnv::with_snap(&snapshot)).unwrap(),
            json!(11)
        );

        assert_eq!(
            JsonPath::parse("$snap.array[-1]").unwrap_err(),
            PathParseError::NegativeIndex
        );

        let missing = JsonPath::parse("$snap.array[2]").unwrap();
        assert_eq!(
            missing
                .resolve(&PathEnv::with_snap(&snapshot))
                .unwrap_err()
                .reason(),
            "runtime_error:path_missing"
        );
    }

    #[test]
    fn validates_closed_intervention_point_names_and_tool_intervention_point_constraints() {
        let unknown_intervention_point = r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  state:
    policy_target_kind: state
    policy:
      id: test_policy
    policy_target: $snap.state"#;
        assert_eq!(
            Manifest::from_yaml_str(unknown_intervention_point)
                .unwrap_err()
                .reason(),
            "runtime_error:manifest_invalid"
        );

        let invalid_tool_name_from = r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  input:
    policy_target_kind: user_input
    tool_name_from: $snap.tool.name
    policy:
      id: test_policy
    policy_target: $snap.input"#;
        assert_eq!(
            Manifest::from_yaml_str(invalid_tool_name_from)
                .unwrap_err()
                .reason(),
            "runtime_error:manifest_invalid"
        );
    }

    #[test]
    fn unknown_tool_fails_closed() {
        let manifest = manifest(
            r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  pre_tool_call:
    policy_target_kind: tool_args
    tool_name_from: $snap.tool_call.name
    policy:
      id: test_policy
    policy_target: $snap.tool_call.args"#,
        );
        let policy = Arc::new(StaticPolicy::allow());
        let runtime = runtime(manifest, no_annotations(), policy.clone());

        let result = runtime.evaluate_intervention_point(InterventionPointRequest {
            intervention_point: InterventionPoint::PreToolCall,
            snapshot: json!({"tool_call": {"name": "missing_tool", "args": {}}}),
            mode: EnforcementMode::Enforce,
        });

        assert_eq!(result.verdict.decision, Decision::Deny);
        assert_eq!(
            result.verdict.reason.as_deref(),
            Some("runtime_error:tool_unknown")
        );
        assert!(policy.seen.lock().unwrap().is_empty());
    }

    #[test]
    fn normalizes_policy_verdicts_and_rejects_reserved_reasons() {
        let verdict = normalize_policy_output(json!({
            "decision": "warn",
            "reason": "needs_review",
            "message": "Proceeding with warning"
        }))
        .unwrap();
        assert_eq!(verdict.decision, Decision::Warn);
        assert_eq!(verdict.reason.as_deref(), Some("needs_review"));

        let error = normalize_policy_output(json!({
            "decision": "allow",
            "reason": "runtime_error:path_missing"
        }))
        .unwrap_err();
        assert_eq!(error.reason(), "runtime_error:policy_output_invalid");
    }

    #[test]
    fn evaluate_only_validates_transform_without_applying_it() {
        // AGT D1 migration: the legacy effects-based evaluate-only test now
        // exercises the transform decision per `SPECIFICATION.md` ยง14
        // and ยง5. Effects are removed from the verdict surface.
        let manifest = manifest(
            r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  input:
    policy_target_kind: user_input
    policy:
      id: test_policy
    policy_target: $snap.input"#,
        );
        let policy = Arc::new(StaticPolicy::with_output(json!({
            "decision": "transform",
            "transform": {"path": "$policy_target.message", "value": "changed"}
        })));
        let runtime = runtime(manifest, no_annotations(), policy);

        let result = runtime.evaluate_intervention_point(InterventionPointRequest {
            intervention_point: InterventionPoint::Input,
            snapshot: json!({"input": {"message": "original"}}),
            mode: EnforcementMode::EvaluateOnly,
        });

        assert_eq!(result.verdict.decision, Decision::Transform);
        assert_eq!(result.transformed_policy_target, None);
        assert_eq!(
            result.policy_input.unwrap()["policy_target"]["value"],
            json!({"message": "original"})
        );
    }

    #[test]
    fn enforce_applies_policy_target_only_transform() {
        // AGT D1 migration: replaced multi-effect (append + prepend + redact
        // + replace) on a single warn verdict with a single transform that
        // rewrites the entire policy target object. Multi-step rewriting is
        // moving to annotators per D1.3.
        let manifest = manifest(
            r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  output:
    policy_target_kind: assistant_output
    policy:
      id: test_policy
    policy_target: $snap.output"#,
        );
        let policy = Arc::new(StaticPolicy::with_output(json!({
            "decision": "transform",
            "transform": {
                "path": "$policy_target",
                "value": {
                    "items": [0, 1, 2],
                    "content": "hello [x] world",
                    "flag": true
                }
            }
        })));
        let runtime = runtime(manifest, no_annotations(), policy);

        let result = runtime.evaluate_intervention_point(InterventionPointRequest {
            intervention_point: InterventionPoint::Output,
            snapshot: json!({
                "output": {
                    "items": [1],
                    "content": "hello secret world",
                    "flag": false
                }
            }),
            mode: EnforcementMode::Enforce,
        });

        assert_eq!(result.verdict.decision, Decision::Transform);
        assert_eq!(
            result.transformed_policy_target.unwrap(),
            json!({
                "items": [0, 1, 2],
                "content": "hello [x] world",
                "flag": true
            })
        );
    }

    #[test]
    fn annotation_dispatch_runs_before_policy_and_finalizes_policy_input() {
        let manifest = manifest(
            r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  input:
    policy_target_kind: user_input
    policy:
      id: test_policy
    policy_target: $snap.input
    annotations:
      prompt_classifier:
        from: $policy_target.text
annotators:
  prompt_classifier:
    type: classifier"#,
        );
        let annotations = Arc::new(StaticAnnotator {
            output: json!({"risk": "low"}),
            seen: Mutex::new(Vec::new()),
            error: None,
        });
        let policy = Arc::new(StaticPolicy::allow());
        let runtime = runtime(manifest, annotations.clone(), policy.clone());

        let result = runtime.evaluate_intervention_point(InterventionPointRequest {
            intervention_point: InterventionPoint::Input,
            snapshot: json!({"input": {"text": "hello"}}),
            mode: EnforcementMode::Enforce,
        });

        assert_eq!(result.verdict.decision, Decision::Allow);
        let preliminary = annotations.seen.lock().unwrap()[0].clone();
        assert_eq!(preliminary["annotations"], json!({}));
        assert_eq!(
            preliminary["policy_target"]["value"],
            json!({"text": "hello"})
        );

        let final_input = policy.seen.lock().unwrap()[0].clone();
        assert_eq!(
            final_input["annotations"]["prompt_classifier"],
            json!({"risk": "low"})
        );
        assert_eq!(result.policy_input.unwrap(), final_input);
    }

    #[test]
    fn dispatcher_panics_fail_closed_without_poisoning_runtime_reuse() {
        let annotated_manifest = manifest(
            r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  input:
    policy:
      id: test_policy
    policy_target: $snap.input
    annotations:
      prompt_classifier:
        from: $policy_target.text
annotators:
  prompt_classifier:
    type: classifier"#,
        );
        let annotation_runtime = Runtime::new(
            annotated_manifest,
            OncePanicAnnotator::new(),
            Arc::new(StaticPolicy::allow()),
        )
        .unwrap();

        let first_annotation =
            annotation_runtime.evaluate_intervention_point(InterventionPointRequest {
                intervention_point: InterventionPoint::Input,
                snapshot: json!({"input": {"text": "hello"}}),
                mode: EnforcementMode::Enforce,
            });
        assert_eq!(first_annotation.verdict.decision, Decision::Deny);
        assert_eq!(
            first_annotation.verdict.reason.as_deref(),
            Some("runtime_error:annotation_failed")
        );

        let second_annotation =
            annotation_runtime.evaluate_intervention_point(InterventionPointRequest {
                intervention_point: InterventionPoint::Input,
                snapshot: json!({"input": {"text": "hello"}}),
                mode: EnforcementMode::Enforce,
            });
        assert_eq!(second_annotation.verdict.decision, Decision::Allow);

        let policy_manifest = manifest(
            r#"agent_control_specification_version: 0.3.1-beta
policies:
  test_policy:
    type: test
intervention_points:
  input:
    policy:
      id: test_policy
    policy_target: $snap.input"#,
        );
        let policy_runtime =
            Runtime::new(policy_manifest, no_annotations(), OncePanicPolicy::new()).unwrap();

        let first_policy = policy_runtime.evaluate_intervention_point(InterventionPointRequest {
            intervention_point: InterventionPoint::Input,
            snapshot: json!({"input": "hello"}),
            mode: EnforcementMode::Enforce,
        });
        assert_eq!(first_policy.verdict.decision, Decision::Deny);
        assert_eq!(
            first_policy.verdict.reason.as_deref(),
            Some("runtime_error:policy_invocation_failed")
        );

        let second_policy = policy_runtime.evaluate_intervention_point(InterventionPointRequest {
            intervention_point: InterventionPoint::Input,
            snapshot: json!({"input": "hello"}),
            mode: EnforcementMode::Enforce,
        });
        assert_eq!(second_policy.verdict.decision, Decision::Allow);
    }

    #[test]
    fn canonical_serialization_sorts_keys_without_pretty_whitespace() {
        let value = json!({"b": 1, "a": {"d": 4, "c": 3}});
        assert_eq!(
            canonical_json(&value).unwrap(),
            r#"{"a":{"c":3,"d":4},"b":1}"#
        );
    }
}