cap-rs-orchestrator 0.1.0

Fleet orchestration engine for CAP (CLI Agent Protocol) — declarative YAML-driven multi-agent coordination.
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
//! One tokio task per session, owning a `Box<dyn Driver>`. Communicates only
//! over channels — no shared mutable state, no `Mutex<Driver>`.

use std::path::PathBuf;

use cap_rs::core::{
    AgentEvent, CancelScope, ClientFrame, PermissionDecision, PermissionMode, ReverseRpcResult,
    SessionConfig,
};
use cap_rs::driver::Driver;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use crate::config::{PermissionPolicy, SessionId};
use crate::event::OrchestratorEvent;

/// A live session: its inbox sender + the task handle.
#[derive(Debug)]
pub struct SessionHandle {
    pub inbox: mpsc::Sender<ClientFrame>,
    pub join: JoinHandle<()>,
}

/// Per-session config to thread from orchestrator → session actor.
#[derive(Debug, Clone, Default)]
pub struct SessionSpawnConfig {
    pub model: Option<String>,
    pub system_prompt: Option<String>,
    pub max_turns: Option<u32>,
    pub budget_usd: Option<f64>,
}

/// Spawn the actor task. Returns immediately; the task runs until its driver
/// exits, the inbox closes, or the cancel token fires.
pub fn spawn_session(
    id: SessionId,
    driver: Box<dyn Driver>,
    policy: PermissionPolicy,
    cwd: PathBuf,
    bus: mpsc::Sender<OrchestratorEvent>,
    cancel: CancellationToken,
    spawn_cfg: SessionSpawnConfig,
) -> SessionHandle {
    spawn_session_with_options(id, driver, policy, cwd, bus, cancel, false, spawn_cfg)
}

pub fn spawn_chat_session(
    id: SessionId,
    driver: Box<dyn Driver>,
    policy: PermissionPolicy,
    cwd: PathBuf,
    bus: mpsc::Sender<OrchestratorEvent>,
    cancel: CancellationToken,
    spawn_cfg: SessionSpawnConfig,
) -> SessionHandle {
    spawn_session_with_options(id, driver, policy, cwd, bus, cancel, true, spawn_cfg)
}

#[allow(clippy::too_many_arguments)]
fn spawn_session_with_options(
    id: SessionId,
    mut driver: Box<dyn Driver>,
    policy: PermissionPolicy,
    cwd: PathBuf,
    bus: mpsc::Sender<OrchestratorEvent>,
    cancel: CancellationToken,
    keep_alive_after_done: bool,
    spawn_cfg: SessionSpawnConfig,
) -> SessionHandle {
    let (inbox_tx, mut inbox_rx) = mpsc::channel::<ClientFrame>(256);

    let join = tokio::spawn(async move {
        bus_send(
            &bus,
            OrchestratorEvent::SessionStarted {
                session: id.clone(),
            },
            &cancel,
        )
        .await;

        // Wait for the first frame to drive the turn. This is the task prompt
        // supplied by the orchestrator; CAP session config is synthesized from
        // the session actor's launch context and sent first.
        let frame = tokio::select! {
            biased;
            _ = cancel.cancelled() => {
                let _ = driver.send(ClientFrame::Cancel { scope: CancelScope::Session, reason: Some("orchestrator_cancel".into()) }).await;
                let _ = driver.shutdown().await;
                return;
            }
            maybe = inbox_rx.recv() => match maybe {
                Some(f) => f,
                None => { let _ = driver.shutdown().await; return; }
            }
        };

        let permission_mode = match policy {
            PermissionPolicy::Ask => PermissionMode::Interactive,
            PermissionPolicy::Allow | PermissionPolicy::Bypass => PermissionMode::Confirm,
            PermissionPolicy::Deny => PermissionMode::None,
        };
        let mut config = SessionConfig::new(cwd);
        config.permission_mode = permission_mode;
        config.model = spawn_cfg.model;
        config.system_prompt = spawn_cfg.system_prompt;
        config.max_turns = spawn_cfg.max_turns;
        config.budget_usd = spawn_cfg.budget_usd;
        if let Err(e) = driver.send(ClientFrame::SessionConfig(config)).await {
            tracing::error!(session = %id, error = %e, "failed to send session config");
            bus_send(
                &bus,
                OrchestratorEvent::SessionFailed {
                    session: id.clone(),
                    error: e.to_string(),
                },
                &cancel,
            )
            .await;
            return;
        }

        // PTY/TUI agents need to boot to their input prompt before they can
        // receive a prompt — sending earlier loses it into a not-ready
        // terminal. Such drivers ask us to wait for the agent's `Ready`. We
        // forward any boot events to the bus meanwhile. Structured drivers
        // (claude) opt out and are prompted immediately.
        if driver.prompt_after_ready() {
            loop {
                let ev = tokio::select! {
                    biased;
                    _ = cancel.cancelled() => {
                        let _ = driver.send(ClientFrame::Cancel { scope: CancelScope::Session, reason: Some("orchestrator_cancel".into()) }).await;
                        let _ = driver.shutdown().await;
                        return;
                    }
                    ev = driver.next_event() => ev,
                };
                match ev {
                    Some(AgentEvent::Ready { .. }) => break,
                    Some(event) => {
                        bus_send(
                            &bus,
                            OrchestratorEvent::Agent {
                                session: id.clone(),
                                event,
                            },
                            &cancel,
                        )
                        .await;
                    }
                    None => {
                        bus_send(
                            &bus,
                            OrchestratorEvent::SessionFailed {
                                session: id.clone(),
                                error: "driver exited before becoming ready".into(),
                            },
                            &cancel,
                        )
                        .await;
                        return;
                    }
                }
            }
        }

        tracing::info!(session = %id, "sending prompt to driver");
        if let Err(e) = driver.send(frame).await {
            tracing::error!(session = %id, error = %e, "failed to send prompt");
            bus_send(
                &bus,
                OrchestratorEvent::SessionFailed {
                    session: id.clone(),
                    error: e.to_string(),
                },
                &cancel,
            )
            .await;
            return;
        }
        tracing::info!(session = %id, "prompt sent, entering pump_turn");

        // Pump events until this turn ends (Done or error/cancel).
        let _ = pump_turn(
            &id,
            &mut driver,
            policy,
            &bus,
            &mut inbox_rx,
            &cancel,
            keep_alive_after_done,
        )
        .await;
    });

    SessionHandle {
        inbox: inbox_tx,
        join,
    }
}

/// Send to bus with cancel awareness — never blocks if cancelled.
async fn bus_send(
    bus: &mpsc::Sender<OrchestratorEvent>,
    event: OrchestratorEvent,
    cancel: &CancellationToken,
) {
    tokio::select! {
        biased;
        _ = cancel.cancelled() => {},
        _ = bus.send(event) => {},
    }
}

/// Pump events until `Done`. The actor task exits after the turn completes,
/// regardless of whether the session ended cleanly, by error, or cancellation.
async fn pump_turn(
    id: &SessionId,
    driver: &mut Box<dyn Driver>,
    policy: PermissionPolicy,
    bus: &mpsc::Sender<OrchestratorEvent>,
    inbox_rx: &mut mpsc::Receiver<ClientFrame>,
    cancel: &CancellationToken,
    keep_alive_after_done: bool,
) {
    let mut awaiting_prompt = false;
    loop {
        tracing::debug!(session = %id, "waiting for next event");
        enum PumpInput {
            Cancelled,
            DriverEvent(Option<AgentEvent>),
            Inbox(Option<ClientFrame>),
        }
        let input = tokio::select! {
            biased;
            _ = cancel.cancelled() => PumpInput::Cancelled,
            frame = inbox_rx.recv() => PumpInput::Inbox(frame),
            ev = driver.next_event(), if !awaiting_prompt => PumpInput::DriverEvent(ev),
        };

        let ev = match input {
            PumpInput::Cancelled => {
                let _ = driver.send(ClientFrame::Cancel { scope: CancelScope::Session, reason: Some("orchestrator_cancel".into()) }).await;
                let _ = driver.shutdown().await;
                return;
            }
            PumpInput::Inbox(Some(frame)) => {
                if let ClientFrame::SessionConfig(_) = frame {
                    continue;
                }
                awaiting_prompt = false;
                if let Err(e) = driver.send(frame).await {
                    bus_send(
                        bus,
                        OrchestratorEvent::SessionFailed {
                            session: id.clone(),
                            error: e.to_string(),
                        },
                        cancel,
                    )
                    .await;
                    return;
                }
                continue;
            }
            PumpInput::Inbox(None) => {
                let _ = driver.shutdown().await;
                return;
            }
            PumpInput::DriverEvent(ev) => ev,
        };

        let Some(ev) = ev else {
            tracing::warn!(session = %id, "driver.next_event() returned None");
            bus_send(
                bus,
                OrchestratorEvent::SessionFailed {
                    session: id.clone(),
                    error: "driver exited before completing the turn".into(),
                },
                cancel,
            )
            .await;
            return;
        };
        tracing::debug!(session = %id, event = ?ev, "received event");

        match ev {
            AgentEvent::Done { stop_reason, .. } => {
                bus_send(
                    bus,
                    OrchestratorEvent::SessionDone {
                        session: id.clone(),
                        stop_reason,
                    },
                    cancel,
                )
                .await;
                if keep_alive_after_done {
                    awaiting_prompt = true;
                    continue;
                }
                return;
            }
            AgentEvent::PermissionRequest {
                ref req_id,
                ref tool,
                risk_level,
                ..
            } => {
                let req_id = req_id.clone();
                let tool = tool.clone();
                bus_send(
                    bus,
                    OrchestratorEvent::Agent {
                        session: id.clone(),
                        event: ev.clone(),
                    },
                    cancel,
                )
                .await;

                let decision = match policy {
                    PermissionPolicy::Allow | PermissionPolicy::Bypass
                        if risk_level != cap_rs::core::RiskLevel::High =>
                    {
                        PermissionDecision::AllowOnce
                    }
                    PermissionPolicy::Deny => PermissionDecision::Deny,
                    PermissionPolicy::Ask | PermissionPolicy::Allow | PermissionPolicy::Bypass => {
                        bus_send(
                            bus,
                            OrchestratorEvent::Ask {
                                session: id.clone(),
                                req_id: req_id.clone(),
                                tool,
                                risk_level,
                            },
                            cancel,
                        )
                        .await;
                        // Q7: Already cancel-safe — the nested select! below
                        // also checks cancellation before blocking on inbox_rx.
                        tokio::select! {
                            biased;
                            _ = cancel.cancelled() => {
                                let _ = driver.send(ClientFrame::Cancel { scope: CancelScope::Session, reason: Some("orchestrator_cancel".into()) }).await;
                                let _ = driver.shutdown().await;
                                return;
                            }
                            maybe = inbox_rx.recv() => match maybe {
                                Some(ClientFrame::PermissionResponse { decision, .. }) => decision,
                                _ => PermissionDecision::Deny,
                            }
                        }
                    }
                };

                if let Err(e) = driver
                    .send(ClientFrame::PermissionResponse { req_id, decision })
                    .await
                {
                    bus_send(
                        bus,
                        OrchestratorEvent::SessionFailed {
                            session: id.clone(),
                            error: e.to_string(),
                        },
                        cancel,
                    )
                    .await;
                    return;
                }
            }
            AgentEvent::ReverseRpc {
                ref rpc_id,
                ref rpc,
            } => {
                let rpc_id = rpc_id.clone();
                let rpc = rpc.clone();
                bus_send(
                    bus,
                    OrchestratorEvent::ReverseRpc {
                        session: id.clone(),
                        rpc_id: rpc_id.clone(),
                        rpc,
                    },
                    cancel,
                )
                .await;
                // Wait for the consumer to respond.
                let result = tokio::select! {
                    biased;
                    _ = cancel.cancelled() => {
                        let _ = driver.send(ClientFrame::Cancel { scope: CancelScope::Session, reason: Some("orchestrator_cancel".into()) }).await;
                        let _ = driver.shutdown().await;
                        return;
                    }
                    maybe = inbox_rx.recv() => match maybe {
                        Some(ClientFrame::ReverseRpcResult { result, .. }) => result,
                        _ => ReverseRpcResult::Success { ok: false },
                    }
                };
                if let Err(e) = driver
                    .send(ClientFrame::ReverseRpcResult { rpc_id, result })
                    .await
                {
                    bus_send(
                        bus,
                        OrchestratorEvent::SessionFailed {
                            session: id.clone(),
                            error: e.to_string(),
                        },
                        cancel,
                    )
                    .await;
                    return;
                }
            }
            AgentEvent::AskUser {
                ref ask_id,
                ref prompt,
                ref ask_kind,
                ref options,
                ..
            } => {
                let ask_id = ask_id.clone();
                bus_send(
                    bus,
                    OrchestratorEvent::AskUser {
                        session: id.clone(),
                        ask_id: ask_id.clone(),
                        prompt: prompt.clone(),
                        ask_kind: ask_kind.clone(),
                        options: options.clone(),
                    },
                    cancel,
                )
                .await;
                // Wait for the consumer to answer.
                let value = tokio::select! {
                    biased;
                    _ = cancel.cancelled() => {
                        let _ = driver.send(ClientFrame::Cancel { scope: CancelScope::Session, reason: Some("orchestrator_cancel".into()) }).await;
                        let _ = driver.shutdown().await;
                        return;
                    }
                    maybe = inbox_rx.recv() => match maybe {
                        Some(ClientFrame::AskUserAnswer { value, .. }) => value,
                        _ => serde_json::Value::Null,
                    }
                };
                if let Err(e) = driver
                    .send(ClientFrame::AskUserAnswer { ask_id, value })
                    .await
                {
                    bus_send(
                        bus,
                        OrchestratorEvent::SessionFailed {
                            session: id.clone(),
                            error: e.to_string(),
                        },
                        cancel,
                    )
                    .await;
                    return;
                }
            }
            other => {
                bus_send(
                    bus,
                    OrchestratorEvent::Agent {
                        session: id.clone(),
                        event: other,
                    },
                    cancel,
                )
                .await;
            }
        }
    }
}

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

    fn spawn_test_session(
        id: SessionId,
        driver: Box<dyn Driver>,
        policy: PermissionPolicy,
        cwd: PathBuf,
        bus: mpsc::Sender<OrchestratorEvent>,
        cancel: CancellationToken,
    ) -> SessionHandle {
        spawn_session(id, driver, policy, cwd, bus, cancel, SessionSpawnConfig::default())
    }
    use crate::event::OrchestratorEvent;
    use crate::testing::StubDriver;
    use cap_rs::core::{ClientFrame, Content, PermissionDecision, RiskLevel, StopReason};
    use tokio::sync::mpsc;
    use tokio_util::sync::CancellationToken;

    fn prompt(s: &str) -> ClientFrame {
        ClientFrame::Prompt {
            content: vec![Content::text(s)],
        }
    }

    fn test_cwd() -> std::path::PathBuf {
        std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
    }

    #[tokio::test]
    async fn pumps_events_and_signals_done() {
        let driver = Box::new(StubDriver::new("a").text("hi").done(StopReason::EndTurn));
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );

        handle.inbox.send(prompt("go")).await.unwrap();

        let mut kinds = Vec::new();
        while let Some(ev) = bus_rx.recv().await {
            match ev {
                OrchestratorEvent::SessionStarted { .. } => kinds.push("started"),
                OrchestratorEvent::Agent { .. } => kinds.push("agent"),
                OrchestratorEvent::SessionDone { stop_reason, .. } => {
                    assert_eq!(stop_reason, StopReason::EndTurn);
                    kinds.push("done");
                    break;
                }
                _ => {}
            }
        }
        assert_eq!(kinds, vec!["started", "agent", "done"]);
        handle.join.await.unwrap();
    }

    #[tokio::test]
    async fn await_ready_driver_waits_for_ready_then_prompts() {
        // A PTY-like driver: boot noise, then Ready, then the turn. The prompt
        // must be held until Ready and then delivered exactly once.
        let sink = std::sync::Arc::new(std::sync::Mutex::new(Vec::<String>::new()));
        let driver = Box::new(
            StubDriver::new("a")
                .await_ready()
                .text("booting")
                .ready()
                .text("answer")
                .done(StopReason::EndTurn)
                .capture(sink.clone()),
        );
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        let mut done = false;
        while let Some(ev) = bus_rx.recv().await {
            if let OrchestratorEvent::SessionDone { .. } = ev {
                done = true;
                break;
            }
        }
        assert!(done, "session should complete after Ready");
        assert_eq!(
            *sink.lock().unwrap(),
            vec!["go".to_string()],
            "prompt delivered once, only after Ready"
        );
        handle.join.await.unwrap();
    }

    #[tokio::test]
    async fn sends_session_config_before_prompt() {
        let frame_kinds = std::sync::Arc::new(std::sync::Mutex::new(Vec::<&'static str>::new()));
        let driver = Box::new(
            StubDriver::new("a")
                .ready()
                .done(StopReason::EndTurn)
                .capture_frame_kinds(frame_kinds.clone()),
        );
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        while let Some(ev) = bus_rx.recv().await {
            if let OrchestratorEvent::SessionDone { .. } = ev {
                break;
            }
        }

        assert_eq!(
            *frame_kinds.lock().unwrap(),
            vec!["SessionConfig", "Prompt"]
        );
        handle.join.await.unwrap();
    }

    #[tokio::test]
    async fn await_ready_driver_fails_if_never_ready() {
        // No Ready is ever scripted; the driver exits while we wait → fail loud
        // rather than send a prompt into a not-ready agent or hang forever.
        let driver = Box::new(StubDriver::new("a").await_ready().text("noise"));
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        let mut failed = false;
        while let Some(ev) = bus_rx.recv().await {
            if let OrchestratorEvent::SessionFailed { error, .. } = ev {
                assert!(error.contains("before becoming ready"), "got {error}");
                failed = true;
                break;
            }
        }
        assert!(
            failed,
            "must surface SessionFailed when Ready never arrives"
        );
        handle.join.await.unwrap();
    }

    #[tokio::test]
    async fn allow_policy_auto_approves_permission() {
        let driver = Box::new(
            StubDriver::new("a")
                .permission("Bash", RiskLevel::Medium)
                .done(StopReason::EndTurn),
        );
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        let mut saw_ask = false;
        while let Some(ev) = bus_rx.recv().await {
            match ev {
                OrchestratorEvent::Ask { .. } => saw_ask = true,
                OrchestratorEvent::SessionDone { .. } => break,
                _ => {}
            }
        }
        assert!(!saw_ask, "Allow policy must not surface an Ask");
    }

    #[tokio::test]
    async fn high_risk_is_not_auto_approved() {
        let driver = Box::new(
            StubDriver::new("a")
                .permission("Bash", RiskLevel::High)
                .done(StopReason::EndTurn),
        );
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        let mut saw_ask = false;
        loop {
            match bus_rx.recv().await.unwrap() {
                OrchestratorEvent::Ask {
                    req_id, risk_level, ..
                } => {
                    assert_eq!(risk_level, RiskLevel::High);
                    saw_ask = true;
                    handle
                        .inbox
                        .send(ClientFrame::PermissionResponse {
                            req_id,
                            decision: PermissionDecision::Deny,
                        })
                        .await
                        .unwrap();
                }
                OrchestratorEvent::SessionDone { .. } => break,
                _ => {}
            }
        }
        assert!(saw_ask, "high-risk permission must ask even under Allow");
    }

    #[tokio::test]
    async fn ask_policy_surfaces_ask_and_awaits_decision() {
        let driver = Box::new(
            StubDriver::new("a")
                .permission("Bash", RiskLevel::High)
                .done(StopReason::EndTurn),
        );
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Ask,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        loop {
            if let OrchestratorEvent::Ask { req_id, .. } = bus_rx.recv().await.unwrap() {
                handle
                    .inbox
                    .send(ClientFrame::PermissionResponse {
                        req_id,
                        decision: PermissionDecision::AllowOnce,
                    })
                    .await
                    .unwrap();
                break;
            }
        }
        let mut saw_done = false;
        while let Some(ev) = bus_rx.recv().await {
            if let OrchestratorEvent::SessionDone { .. } = ev {
                saw_done = true;
                break;
            }
        }
        assert!(saw_done);
    }

    #[tokio::test]
    async fn chat_session_waits_for_next_prompt_after_done() {
        let driver = Box::new(StubDriver::new("a").done(StopReason::EndTurn));
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_chat_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token.clone(),
            SessionSpawnConfig::default(),
        );
        handle.inbox.send(prompt("go")).await.unwrap();

        let mut saw_done = false;
        while let Some(ev) = bus_rx.recv().await {
            if let OrchestratorEvent::SessionDone { .. } = ev {
                saw_done = true;
                break;
            }
        }
        assert!(saw_done);

        let next = tokio::time::timeout(std::time::Duration::from_millis(100), bus_rx.recv()).await;
        assert!(
            next.is_err(),
            "chat session should wait silently after Done"
        );

        token.cancel();
        handle.join.await.unwrap();
    }

    #[tokio::test]
    async fn mid_turn_user_message_is_forwarded() {
        let frame_kinds = std::sync::Arc::new(std::sync::Mutex::new(Vec::<&'static str>::new()));
        let driver = Box::new(
            StubDriver::new("a")
                .delay_events(std::time::Duration::from_millis(100))
                .done(StopReason::EndTurn)
                .capture_frame_kinds(frame_kinds.clone()),
        );
        let (bus_tx, mut bus_rx) = mpsc::channel(64);
        let token = CancellationToken::new();
        let handle = spawn_test_session(
            "a".into(),
            driver,
            PermissionPolicy::Allow,
            test_cwd(),
            bus_tx,
            token,
        );
        handle.inbox.send(prompt("first")).await.unwrap();
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        handle.inbox.send(prompt("second")).await.unwrap();

        while let Some(ev) = bus_rx.recv().await {
            if let OrchestratorEvent::SessionDone { .. } = ev {
                break;
            }
        }

        assert_eq!(
            *frame_kinds.lock().unwrap(),
            vec!["SessionConfig", "Prompt", "Prompt"]
        );
        handle.join.await.unwrap();
    }
}