halo-agent 0.1.1

Halo Agent and Workflow execution kernel
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
//! Concrete permission-policy integration used by the Agent runtime.
use std::{future::Future, pin::Pin, sync::Arc};

use crate::{
    Cancellation, Error,
    permission::{Behavior, Decision, Permission, Request},
};

type CheckFuture = Pin<Box<dyn Future<Output = Result<Decision, Error>> + Send + 'static>>;

trait Entry: Send + Sync {
    fn check(&self, request: Request, cancellation: Cancellation) -> CheckFuture;
}

struct Typed<P>(Arc<P>);
impl<P: Permission + 'static> Entry for Typed<P> {
    fn check(&self, request: Request, cancellation: Cancellation) -> CheckFuture {
        let policy = self.0.clone();
        Box::pin(async move { policy.check(&request, &cancellation).await })
    }
}

#[derive(Clone, Default)]
pub(crate) struct Access(Option<Arc<dyn Entry>>);

impl Access {
    pub(crate) fn set<P: Permission + 'static>(&mut self, policy: P) {
        self.0 = Some(Arc::new(Typed(Arc::new(policy))));
    }

    pub(crate) fn is_configured(&self) -> bool {
        self.0.is_some()
    }

    pub(crate) async fn check(
        &self,
        request: crate::permission::Request,
        cancellation: &Cancellation,
    ) -> Result<Decision, Error> {
        if let Some(policy) = &self.0 {
            crate::runtime::agent::hooks::cancellable(
                policy.check(request, cancellation.clone()),
                cancellation,
            )
            .await
        } else {
            Err(Error::new(
                "INVALID_STATE",
                "permission policy is not configured",
            ))
        }
    }
}

pub(crate) enum Failure {
    Policy(Error),
    Hook(Error),
}

/// Interpret a completed decision in the Agent's existing lifecycle. This
/// does not create a Message, consume a reply or grant/copy authorization.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn check_permission<K, KF>(
    access: &Access,
    hooks: &crate::runtime::agent::hooks::Hooks,
    mut metadata: serde_json::Map<String, serde_json::Value>,
    call: &crate::tool::Call,
    saved: Option<crate::runtime::agent::permission::State>,
    mut commit: K,
    cancellation: &Cancellation,
) -> Result<Decision, Failure>
where
    K: FnMut(crate::runtime::agent::permission::State) -> KF,
    KF: Future<Output = Result<(), Error>>,
{
    metadata.insert(
        "call".into(),
        serde_json::to_value(call).map_err(|_| {
            Failure::Hook(Error::new(
                "INVALID_ARGUMENTS",
                "cannot encode permission call",
            ))
        })?,
    );
    let request = Request {
        action: "call".into(),
        resource: call.name.clone(),
        metadata,
    };
    use crate::runtime::agent::permission::{State, identity};
    let mut state = match saved {
        Some(state) if state.stage != "complete" => {
            state.validate(call, hooks.len()).map_err(Failure::Hook)?;
            state
        }
        _ => State::new(request.clone()),
    };
    commit(state.clone()).await.map_err(Failure::Hook)?;
    if state.stage == "before" {
        let input = Box::pin(hooks.apply_from_async(
            crate::runtime::agent::hooks::Stage::Permission,
            true,
            state.input.clone(),
            state.idx,
            |idx, input: Request| {
                let pending = identity(&request, &input).map(|()| {
                    state.input = input;
                    state.idx = idx;
                    commit(state.clone())
                });
                async move { pending?.await }
            },
            cancellation,
        ))
        .await
        .map_err(Failure::Hook)?;
        state.input = input;
        state.stage = "check".into();
        state.idx = 0;
        commit(state.clone()).await.map_err(Failure::Hook)?;
    }
    // A saved decision is audit data, not a grant. Restoration always checks
    // current policy against the acknowledged before input, never after edits.
    let decision = access
        .check(state.input.clone(), cancellation)
        .await
        .map_err(Failure::Policy)?;
    if state.stage != "after" {
        state.stage = "after".into();
        state.idx = 0;
        state.output = Some(state.input.clone());
    }
    state.decision = Some(decision.clone());
    commit(state.clone()).await.map_err(Failure::Hook)?;
    let output = Box::pin(hooks.apply_from_async(
        crate::runtime::agent::hooks::Stage::Permission,
        false,
        state.output.clone().expect("after Request"),
        state.idx,
        |idx, output: Request| {
            let pending = identity(&state.input, &output).map(|()| {
                state.output = Some(output);
                state.idx = idx;
                commit(state.clone())
            });
            async move { pending?.await }
        },
        cancellation,
    ))
    .await
    .map_err(Failure::Hook)?;
    state.output = Some(output);
    state.stage = "complete".into();
    commit(state).await.map_err(Failure::Hook)?;
    Ok(decision)
}

/// Apply permission policy to an actual tool call and, when configured, route
/// an Ask decision through the Agent's existing reply interaction.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn check_tool<K, KF>(
    access: &Access,
    hooks: &crate::runtime::agent::hooks::Hooks,
    metadata: serde_json::Map<String, serde_json::Value>,
    call: &crate::tool::Call,
    cancellation: &Cancellation,
    interaction: Option<&crate::runtime::agent::reply::Run>,
    id_fn: Option<fn() -> i64>,
    approval: Option<&crate::tool::Call>,
    saved: Option<crate::runtime::agent::permission::State>,
    commit: K,
) -> Result<(), Failure>
where
    K: FnMut(crate::runtime::agent::permission::State) -> KF,
    KF: Future<Output = Result<(), Error>>,
{
    let decision = Box::pin(check_permission(
        access,
        hooks,
        metadata,
        call,
        saved,
        commit,
        cancellation,
    ))
    .await?;
    match decision.behavior {
        Behavior::Allow => Ok(()),
        Behavior::Deny => Err(Failure::Policy(Error::new(
            "PERMISSION_DENIED",
            decision
                .reason
                .unwrap_or_else(|| "operation denied by permission policy".into()),
        ))),
        Behavior::Ask => {
            let reason = decision
                .reason
                .unwrap_or_else(|| "operation requires user confirmation".into());
            if approval == Some(call) {
                return Ok(());
            }
            if let (Some(run), Some(id_fn)) = (interaction, id_fn) {
                run.ask(call, &reason, id_fn, cancellation)
                    .await
                    .map_err(Failure::Policy)
            } else {
                Err(Failure::Policy(Error::new("WAITING", reason)))
            }
        }
    }
}

pub(crate) fn require_policy(
    steps: &[crate::runtime::agent::node::Step],
    configured: bool,
) -> Result<(), Error> {
    if !configured
        && steps.iter().any(|step| {
            step.permission
                .as_ref()
                .is_some_and(|state| state.stage != "complete")
        })
    {
        Err(Error::new(
            "INVALID_STATE",
            "unfinished permission requires its configured policy",
        ))
    } else {
        Ok(())
    }
}

pub(crate) fn validate_scene(scene: &serde_json::Value, configured: bool) -> Result<(), Error> {
    if configured {
        return Ok(());
    }
    let steps: Vec<crate::runtime::agent::node::Step> = scene
        .get("steps")
        .cloned()
        .map(serde_json::from_value)
        .transpose()
        .map_err(|_| Error::new("INVALID_STATE", "invalid permission owner"))?
        .unwrap_or_default();
    require_policy(&steps, configured)?;
    if let Some(nodes) = scene.get("nodes").and_then(serde_json::Value::as_object) {
        for node in nodes.values() {
            if let Some(attempt) = node["execution"]["attempts"]
                .as_array()
                .and_then(|attempts| attempts.last())
            {
                validate_scene(attempt, configured)?;
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{Access, Failure, check_permission, check_tool};
    use crate::{
        Cancellation,
        permission::{Behavior, Decision, Permission, Request},
        tool::Call,
    };
    use serde_json::{Map, Value, json};
    use std::sync::{
        Arc, Mutex,
        atomic::{AtomicBool, Ordering},
    };

    #[derive(Clone)]
    struct Policy {
        decision: Decision,
        requests: Arc<Mutex<Vec<Request>>>,
    }

    impl Permission for Policy {
        async fn check(
            &self,
            request: &Request,
            _: &Cancellation,
        ) -> Result<Decision, crate::Error> {
            self.requests.lock().unwrap().push(request.clone());
            Ok(self.decision.clone())
        }
    }

    #[derive(Clone)]
    struct Hook {
        name: &'static str,
        calls: Arc<Mutex<Vec<&'static str>>>,
        change_resource: bool,
        change_after: bool,
        fail: Option<&'static str>,
    }

    impl crate::middleware::Middleware for Hook {
        async fn before_permission(
            &self,
            request: &mut Value,
            _: &Cancellation,
        ) -> Result<(), crate::Error> {
            self.calls.lock().unwrap().push(self.name);
            if self.fail == Some("before") {
                return Err(crate::Error::new("HOOK_FAILED", "before hook failed"));
            }
            if self.change_resource {
                request["resource"] = json!("different-operation");
            }
            Ok(())
        }

        async fn after_permission(
            &self,
            request: &mut Value,
            _: &Cancellation,
        ) -> Result<(), crate::Error> {
            self.calls.lock().unwrap().push("after");
            if self.fail == Some("after") {
                return Err(crate::Error::new("HOOK_FAILED", "after hook failed"));
            }
            if self.change_after {
                request["resource"] = json!("different-operation");
            }
            Ok(())
        }
    }

    fn call() -> Call {
        Call {
            call_id: "call-live-fact".into(),
            name: "lookup".into(),
            arguments: serde_json::from_value(json!({"city":"Beijing"})).unwrap(),
        }
    }

    fn policy(behavior: Behavior) -> (Access, Arc<Mutex<Vec<Request>>>) {
        let requests = Arc::new(Mutex::new(Vec::new()));
        let mut access = Access::default();
        access.set(Policy {
            decision: Decision {
                behavior,
                reason: Some("policy reason".into()),
            },
            requests: requests.clone(),
        });
        (access, requests)
    }

    #[tokio::test]
    async fn permission_checks_the_actual_call_between_ordered_hooks() {
        let (access, requests) = policy(Behavior::Allow);
        let calls = Arc::new(Mutex::new(Vec::new()));
        let mut hooks = crate::runtime::agent::hooks::Hooks::default();
        hooks.add(Hook {
            name: "before",
            calls: calls.clone(),
            change_resource: false,
            change_after: false,
            fail: None,
        });
        let mut metadata = Map::new();
        metadata.insert("chat_id".into(), json!(101));

        let decision = check_permission(
            &access,
            &hooks,
            metadata,
            &call(),
            None,
            |_| std::future::ready(Ok(())),
            &Cancellation::new(),
        )
        .await
        .unwrap_or_else(|_| panic!("permission check should complete"));

        assert_eq!(decision.behavior, Behavior::Allow);
        assert_eq!(*calls.lock().unwrap(), ["before", "after"]);
        assert_eq!(requests.lock().unwrap().as_slice(), &[Request {
            action: "call".into(),
            resource: "lookup".into(),
            metadata: serde_json::from_value(json!({
                "chat_id":101,
                "call":{"call_id":"call-live-fact","name":"lookup","arguments":{"city":"Beijing"}},
            })).unwrap(),
        }]);
    }

    #[tokio::test]
    async fn deny_is_a_normal_decision_and_policy_failure_stays_technical() {
        let (access, _) = policy(Behavior::Deny);
        let decision = check_permission(
            &access,
            &crate::runtime::agent::hooks::Hooks::default(),
            Map::new(),
            &call(),
            None,
            |_| std::future::ready(Ok(())),
            &Cancellation::new(),
        )
        .await
        .unwrap_or_else(|_| panic!("deny is a normal policy decision"));
        assert_eq!(decision.behavior, Behavior::Deny);

        struct Broken;
        impl Permission for Broken {
            async fn check(&self, _: &Request, _: &Cancellation) -> Result<Decision, crate::Error> {
                Err(crate::Error::new(
                    "UNAVAILABLE",
                    "policy backend unavailable",
                ))
            }
        }
        let mut access = Access::default();
        access.set(Broken);
        let error = check_permission(
            &access,
            &crate::runtime::agent::hooks::Hooks::default(),
            Map::new(),
            &call(),
            None,
            |_| std::future::ready(Ok(())),
            &Cancellation::new(),
        )
        .await
        .unwrap_err();
        assert!(matches!(error, Failure::Policy(error) if error.code == "UNAVAILABLE"));
    }

    #[tokio::test]
    async fn permission_hook_cannot_redirect_the_authorized_operation() {
        let (access, requests) = policy(Behavior::Allow);
        let mut hooks = crate::runtime::agent::hooks::Hooks::default();
        hooks.add(Hook {
            name: "redirect",
            calls: Arc::default(),
            change_resource: true,
            change_after: false,
            fail: None,
        });
        let error = check_permission(
            &access,
            &hooks,
            Map::new(),
            &call(),
            None,
            |_| std::future::ready(Ok(())),
            &Cancellation::new(),
        )
        .await
        .unwrap_err();
        assert!(matches!(error, Failure::Hook(error) if error.code == "INVALID_ARGUMENTS"));
        assert!(requests.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn tool_gate_distinguishes_allow_deny_ask_and_matching_approval() {
        let call = call();
        for (behavior, approval, expected) in [
            (Behavior::Allow, None, None),
            (Behavior::Deny, None, Some("PERMISSION_DENIED")),
            (Behavior::Ask, None, Some("WAITING")),
            (Behavior::Ask, Some(call.clone()), None),
        ] {
            let (access, requests) = policy(behavior);
            let result = check_tool(
                &access,
                &crate::runtime::agent::hooks::Hooks::default(),
                Map::new(),
                &call,
                &Cancellation::new(),
                None,
                None,
                approval.as_ref(),
                None,
                |_| std::future::ready(Ok(())),
            )
            .await;
            match expected {
                Some(code) => {
                    assert!(matches!(result, Err(Failure::Policy(error)) if error.code == code))
                }
                None => assert!(result.is_ok()),
            }
            assert_eq!(requests.lock().unwrap().len(), 1);
            assert_eq!(
                requests.lock().unwrap()[0].metadata["call"]["call_id"],
                call.call_id
            );
        }
    }

    #[tokio::test]
    async fn after_hook_cannot_rewrite_a_decision_into_authorizing_another_operation() {
        let (access, requests) = policy(Behavior::Allow);
        let mut hooks = crate::runtime::agent::hooks::Hooks::default();
        hooks.add(Hook {
            name: "redirect-after",
            calls: Arc::default(),
            change_resource: false,
            change_after: true,
            fail: None,
        });
        let result = check_tool(
            &access,
            &hooks,
            Map::new(),
            &call(),
            &Cancellation::new(),
            None,
            None,
            None,
            None,
            |_| std::future::ready(Ok(())),
        )
        .await;
        assert!(matches!(result, Err(Failure::Hook(error)) if error.code == "INVALID_ARGUMENTS"));
        assert_eq!(requests.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn permission_hook_failures_are_control_errors_at_their_actual_stage() {
        for stage in ["before", "after"] {
            let (access, requests) = policy(Behavior::Allow);
            let mut hooks = crate::runtime::agent::hooks::Hooks::default();
            hooks.add(Hook {
                name: "failure",
                calls: Arc::default(),
                change_resource: false,
                change_after: false,
                fail: Some(stage),
            });
            let error = check_permission(
                &access,
                &hooks,
                Map::new(),
                &call(),
                None,
                |_| std::future::ready(Ok(())),
                &Cancellation::new(),
            )
            .await
            .unwrap_err();
            assert!(matches!(error, Failure::Hook(error) if error.code == "HOOK_FAILED"));
            assert_eq!(
                requests.lock().unwrap().len(),
                usize::from(stage == "after")
            );
        }
    }

    #[tokio::test]
    async fn pending_permission_check_is_cancellable_without_retrying_policy() {
        struct Pending {
            entered: Arc<AtomicBool>,
        }
        impl Permission for Pending {
            async fn check(&self, _: &Request, _: &Cancellation) -> Result<Decision, crate::Error> {
                self.entered.store(true, Ordering::SeqCst);
                std::future::pending().await
            }
        }

        let entered = Arc::new(AtomicBool::new(false));
        let mut access = Access::default();
        access.set(Pending {
            entered: entered.clone(),
        });
        let cancellation = Cancellation::new();
        let cancel = cancellation.clone();
        let cancel_task = tokio::spawn(async move {
            while !entered.load(Ordering::SeqCst) {
                tokio::task::yield_now().await;
            }
            cancel.cancel();
        });
        let error = check_tool(
            &access,
            &crate::runtime::agent::hooks::Hooks::default(),
            Map::new(),
            &call(),
            &cancellation,
            None,
            None,
            None,
            None,
            |_| std::future::ready(Ok(())),
        )
        .await
        .unwrap_err();
        cancel_task.await.expect("cancel task");
        assert!(matches!(error, Failure::Policy(error) if error.code == "CANCELLED"));
    }

    #[tokio::test]
    async fn permission_resume_reuses_acknowledged_hooks_and_rechecks_current_policy() {
        struct WaitThenAllow {
            requests: Arc<Mutex<Vec<Request>>>,
        }
        impl Permission for WaitThenAllow {
            async fn check(
                &self,
                request: &Request,
                _: &Cancellation,
            ) -> Result<Decision, crate::Error> {
                let mut requests = self.requests.lock().unwrap();
                requests.push(request.clone());
                if requests.len() == 1 {
                    Err(crate::Error::new("WAITING", "policy pending"))
                } else {
                    Ok(Decision {
                        behavior: Behavior::Allow,
                        reason: None,
                    })
                }
            }
        }

        let requests = Arc::new(Mutex::new(Vec::new()));
        let mut access = Access::default();
        access.set(WaitThenAllow {
            requests: requests.clone(),
        });
        let calls = Arc::new(Mutex::new(Vec::new()));
        let mut hooks = crate::runtime::agent::hooks::Hooks::default();
        hooks.add(Hook {
            name: "acknowledged",
            calls: calls.clone(),
            change_resource: false,
            change_after: false,
            fail: None,
        });

        let mut saved = None;
        let first = check_permission(
            &access,
            &hooks,
            Map::new(),
            &call(),
            None,
            |state| {
                saved = Some(state);
                std::future::ready(Ok(()))
            },
            &Cancellation::new(),
        )
        .await
        .unwrap_err();
        assert!(matches!(first, Failure::Policy(error) if error.code == "WAITING"));
        let saved = saved.expect("the current permission state must be checkpointable");
        assert_eq!(saved.stage, "check");
        assert!(saved.output.is_none());
        assert_eq!(*calls.lock().unwrap(), ["acknowledged"]);

        let mut restored = None;
        let decision = check_permission(
            &access,
            &hooks,
            Map::new(),
            &call(),
            Some(saved),
            |state| {
                restored = Some(state);
                std::future::ready(Ok(()))
            },
            &Cancellation::new(),
        )
        .await
        .unwrap_or_else(|_| panic!("restored permission should recheck and allow"));
        assert_eq!(decision.behavior, Behavior::Allow);
        assert_eq!(*calls.lock().unwrap(), ["acknowledged", "after"]);
        assert_eq!(requests.lock().unwrap().len(), 2);
        assert_eq!(restored.expect("final state").stage, "complete");
    }

    #[tokio::test]
    async fn permission_is_rechecked_per_call_and_run_metadata_is_not_cached() {
        let (access, requests) = policy(Behavior::Allow);
        let call = call();
        for chat_id in [201, 202] {
            let metadata = Map::from_iter([("chat_id".into(), json!(chat_id))]);
            check_tool(
                &access,
                &crate::runtime::agent::hooks::Hooks::default(),
                metadata,
                &call,
                &Cancellation::new(),
                None,
                None,
                None,
                None,
                |_| std::future::ready(Ok(())),
            )
            .await
            .unwrap_or_else(|_| panic!("each permission request should be allowed"));
        }
        let requests = requests.lock().unwrap();
        assert_eq!(requests.len(), 2);
        assert_eq!(requests[0].metadata["chat_id"], 201);
        assert_eq!(requests[1].metadata["chat_id"], 202);
        assert_eq!(requests[0].metadata["call"], requests[1].metadata["call"]);
    }

    #[test]
    fn restored_unfinished_permission_requires_policy_at_its_nested_owner() {
        use crate::runtime::agent::permission::State;

        let mut metadata = Map::new();
        metadata.insert("call".into(), serde_json::to_value(call()).unwrap());
        let pending = State::new(Request {
            action: "call".into(),
            resource: "lookup".into(),
            metadata,
        });
        let agent_scene = agent_scene(pending);
        let nested_scene = json!({
            "nodes": {
                "agent-node": {
                    "execution": {
                        "attempts": [agent_scene],
                    },
                },
            },
        });

        for scene in [&agent_scene, &nested_scene] {
            let error = super::validate_scene(scene, false).unwrap_err();
            assert_eq!(error.code, "INVALID_STATE");
            assert!(super::validate_scene(scene, true).is_ok());
        }
    }

    #[test]
    fn completed_permission_history_does_not_require_a_policy_on_restore() {
        use crate::runtime::agent::permission::State;

        let mut metadata = Map::new();
        metadata.insert("call".into(), serde_json::to_value(call()).unwrap());
        let mut completed = State::new(Request {
            action: "call".into(),
            resource: "lookup".into(),
            metadata,
        });
        completed.stage = "complete".into();
        let scene = agent_scene(completed);

        assert!(super::validate_scene(&scene, false).is_ok());
    }

    fn agent_scene(permission: crate::runtime::agent::permission::State) -> Value {
        let step = crate::runtime::agent::node::Step {
            id: "tool-step".into(),
            kind: "tool".into(),
            status: "running".into(),
            permission: Some(Box::new(permission)),
            ..Default::default()
        };
        json!({"steps":[step]})
    }
}