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
//! Deterministic state machine. Owns the registry + audit log; interprets the
//! DSL to drive fan-out, joins, and routing. Runs in its own task; the consumer
//! reads `OrchestratorEvent`s from the returned channel.

use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};

use cap_rs::core::{AgentEvent, ClientFrame, Content, PermissionDecision, StopReason, TextChannel};
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use tracing::debug;

use crate::OrchestratorError;
use crate::audit::AuditLog;
use crate::config::{DriverKind, FleetSpec, PermissionPolicy, SessionId};
use crate::event::{OrchestratorControl, OrchestratorEvent};
use crate::factory::DriverFactory;
use crate::registry::SessionRegistry;
use crate::session::SessionSpawnConfig;
use crate::routing::{RouteDecision, RoutingContext, RoutingStrategy, StaticRouting};
use crate::worktree::WorktreeManager;

/// A handle to a running fleet: query the audit log, answer asks, cancel.
#[derive(Debug)]
pub struct ExecutorHandle {
    cancel: CancellationToken,
    control: mpsc::Sender<OrchestratorControl>,
    audit: Arc<Mutex<AuditLog>>,
}

impl ExecutorHandle {
    /// Snapshot the audit log as `(from, to)` pairs in order. Readable even
    /// after the fleet completes — the log is shared, not message-passed.
    pub fn audit_pairs(&self) -> Vec<(SessionId, SessionId)> {
        use crate::audit::AuditEvent;
        self.audit
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .records()
            .iter()
            .filter_map(|r| match &r.event {
                AuditEvent::Route { from, to } => Some((from.clone(), to.clone())),
                _ => None,
            })
            .collect()
    }

    /// Answer an [`OrchestratorEvent::Ask`] (only needed under `ask` policy).
    pub async fn decide(&self, session: SessionId, req_id: String, allow: bool) {
        let _ = self
            .control
            .send(OrchestratorControl::Decision {
                session,
                req_id,
                allow,
            })
            .await;
    }

    /// A cloneable control sender — e.g. for a Ctrl-C task to send `Cancel`.
    pub fn control_sender(&self) -> mpsc::Sender<OrchestratorControl> {
        self.control.clone()
    }

    /// Hard-cancel the whole fleet.
    pub fn cancel(&self) {
        self.cancel.cancel();
    }
}

/// Build the initial prompt frame for a session.
fn task_prompt(task: &str) -> ClientFrame {
    ClientFrame::Prompt {
        content: vec![Content::text(task)],
    }
}

#[derive(Debug)]
pub struct Executor;

impl Executor {
    /// Start the fleet with the default static YAML routing strategy.
    pub async fn start<F, W>(
        spec: FleetSpec,
        factory: F,
        worktree: W,
        task: &str,
    ) -> Result<(ExecutorHandle, mpsc::Receiver<OrchestratorEvent>), OrchestratorError>
    where
        F: DriverFactory + 'static,
        W: WorktreeManager + 'static,
    {
        let strategy = StaticRouting::new(spec.fleet.routes.clone());
        Self::start_with_mode(spec, factory, worktree, task, strategy, false).await
    }

    pub async fn start_chat<F, W>(
        spec: FleetSpec,
        factory: F,
        worktree: W,
        task: &str,
    ) -> Result<(ExecutorHandle, mpsc::Receiver<OrchestratorEvent>), OrchestratorError>
    where
        F: DriverFactory + 'static,
        W: WorktreeManager + 'static,
    {
        let strategy = StaticRouting::new(spec.fleet.routes.clone());
        Self::start_with_mode(spec, factory, worktree, task, strategy, true).await
    }

    /// Start the fleet with a custom routing strategy.
    pub async fn start_with_strategy<F, W, S>(
        spec: FleetSpec,
        factory: F,
        worktree: W,
        task: &str,
        strategy: S,
    ) -> Result<(ExecutorHandle, mpsc::Receiver<OrchestratorEvent>), OrchestratorError>
    where
        F: DriverFactory + 'static,
        W: WorktreeManager + 'static,
        S: RoutingStrategy,
    {
        Self::start_with_mode(spec, factory, worktree, task, strategy, false).await
    }

    async fn start_with_mode<F, W, S>(
        spec: FleetSpec,
        factory: F,
        worktree: W,
        task: &str,
        strategy: S,
        chat_mode: bool,
    ) -> Result<(ExecutorHandle, mpsc::Receiver<OrchestratorEvent>), OrchestratorError>
    where
        F: DriverFactory + 'static,
        W: WorktreeManager + 'static,
        S: RoutingStrategy,
    {
        spec.validate()?;

        let (out_tx, out_rx) = mpsc::channel::<OrchestratorEvent>(256);
        let (bus_tx, bus_rx) = mpsc::channel::<OrchestratorEvent>(256);
        let (control_tx, control_rx) = mpsc::channel::<OrchestratorControl>(32);
        let cancel = CancellationToken::new();
        let audit = Arc::new(Mutex::new(AuditLog::new()));

        let handle = ExecutorHandle {
            cancel: cancel.clone(),
            control: control_tx,
            audit: Arc::clone(&audit),
        };

        let task = task.to_string();
        tokio::spawn(async move {
            let mut run = Run {
                spec,
                strategy: Box::new(strategy),
                factory,
                worktree,
                task,
                registry: SessionRegistry::new(),
                audit,
                done: HashSet::new(),
                spawned: HashSet::new(),
                failed: HashSet::new(),
                usage_cost_usd: 0.0,
                buffers: HashMap::new(),
                out: out_tx,
                bus_tx,
                cancel,
                chat_mode,
            };
            run.drive(bus_rx, control_rx).await;
        });

        Ok((handle, out_rx))
    }
}

struct Run<F: DriverFactory, W: WorktreeManager> {
    spec: FleetSpec,
    strategy: Box<dyn RoutingStrategy>,
    factory: F,
    worktree: W,
    task: String,
    registry: SessionRegistry,
    audit: Arc<Mutex<AuditLog>>,
    done: HashSet<SessionId>,
    /// Sessions successfully spawned (so we know what must still settle).
    spawned: HashSet<SessionId>,
    /// Sessions that failed (driver crash, or reported SessionFailed).
    failed: HashSet<SessionId>,
    /// Aggregated reported usage cost across all sessions.
    usage_cost_usd: f64,
    /// Accumulated assistant text per session, used to parse `by_subtask` blocks.
    buffers: HashMap<SessionId, String>,
    out: mpsc::Sender<OrchestratorEvent>,
    bus_tx: mpsc::Sender<OrchestratorEvent>,
    cancel: CancellationToken,
    chat_mode: bool,
}

impl<F: DriverFactory, W: WorktreeManager> Run<F, W> {
    /// Effective permission policy for a session (per-session override or fleet default).
    fn policy_for(&self, id: &str) -> Result<PermissionPolicy, OrchestratorError> {
        let s = self.spec.fleet.sessions.get(id).ok_or_else(|| {
            OrchestratorError::Config(format!("session '{id}' not found in fleet spec"))
        })?;
        Ok(s.permissions.unwrap_or(self.spec.fleet.permissions))
    }

    async fn spawn(&mut self, id: &SessionId) -> bool {
        let kind = match self.spec.fleet.sessions.get(id) {
            Some(s) => match s.driver_kind() {
                Some(k) => k,
                None => {
                    let _ = self
                        .out
                        .send(OrchestratorEvent::SessionFailed {
                            session: id.clone(),
                            error: format!(
                                "session '{id}' has no driver, agent, or manifest configured"
                            ),
                        })
                        .await;
                    return false;
                }
            },
            None => return false,
        };
        let policy = match self.policy_for(id) {
            Ok(p) => p,
            Err(e) => {
                let _ = self
                    .out
                    .send(OrchestratorEvent::SessionFailed {
                        session: id.clone(),
                        error: e.to_string(),
                    })
                    .await;
                return false;
            }
        };
        self.do_spawn(id, &kind, policy).await
    }

    /// Spawn a dynamic session (not in the YAML spec) with explicit driver kind
    /// and permission policy.
    async fn spawn_dynamic(
        &mut self,
        id: &SessionId,
        kind: &DriverKind,
        policy: PermissionPolicy,
    ) -> bool {
        self.do_spawn(id, kind, policy).await
    }

    /// Shared spawn logic used by both static and dynamic paths.
    async fn do_spawn(
        &mut self,
        id: &SessionId,
        kind: &DriverKind,
        policy: PermissionPolicy,
    ) -> bool {
        let base = &self.spec.fleet.base_branch;
        // Build per-session config from session spec + fleet-level budget.
        let spawn_cfg = self
            .spec
            .fleet
            .sessions
            .get(id)
            .map(|s| SessionSpawnConfig {
                model: s.model.clone(),
                system_prompt: s.system_prompt.clone(),
                max_turns: s.max_turns,
                budget_usd: self.spec.fleet.budget_usd,
            })
            .unwrap_or_default();
        let spawn_result = if self.chat_mode {
            self.registry
                .spawn_chat(
                    id.clone(),
                    kind,
                    policy,
                    base,
                    &self.factory,
                    &self.worktree,
                    &self.bus_tx,
                    &self.cancel,
                    spawn_cfg,
                )
                .await
        } else {
            self.registry
                .spawn(
                    id.clone(),
                    kind,
                    policy,
                    base,
                    &self.factory,
                    &self.worktree,
                    &self.bus_tx,
                    &self.cancel,
                    spawn_cfg,
                )
                .await
        };
        match spawn_result {
            Ok(()) => {
                self.spawned.insert(id.clone());
                true
            }
            Err(e) => {
                let _ = self
                    .out
                    .send(OrchestratorEvent::SessionFailed {
                        session: id.clone(),
                        error: e.to_string(),
                    })
                    .await;
                false
            }
        }
    }

    /// Deliver the initial task prompt to a freshly spawned session.
    async fn kick(&self, id: &SessionId) {
        let _ = self.registry.route(id, task_prompt(&self.task)).await;
    }

    async fn drive(
        &mut self,
        mut bus_rx: mpsc::Receiver<OrchestratorEvent>,
        mut control_rx: mpsc::Receiver<OrchestratorControl>,
    ) {
        // Spawn + kick the start sessions.
        for id in self.spec.fleet.start.sessions() {
            if self.spawn(&id).await {
                self.kick(&id).await;
            }
        }

        // If nothing is pending (e.g. all start sessions failed to spawn), finish now.
        if !self.chat_mode && self.fleet_complete() {
            let _ = self.out.send(OrchestratorEvent::FleetComplete).await;
            self.registry.shutdown().await;
            return;
        }

        // Clone the token so the select! arm does NOT borrow `self`, leaving the
        // handlers free to take `&mut self`. (Required for borrowck.)
        let cancel = self.cancel.clone();

        loop {
            tokio::select! {
                biased;
                _ = cancel.cancelled() => break,
                Some(ctrl) = control_rx.recv() => self.on_control(ctrl).await,
                maybe = bus_rx.recv() => {
                    let Some(ev) = maybe else { break };

                    // Accumulate assistant text so `by_subtask` can parse the lead's output.
                    if let OrchestratorEvent::Agent {
                        session,
                        event: AgentEvent::TextChunk { text, channel: TextChannel::Assistant, .. },
                    } = &ev
                    {
                        self.buffers.entry(session.clone()).or_default().push_str(text);
                    }
                    if let OrchestratorEvent::Agent {
                        session,
                        event: AgentEvent::Usage { usage },
                    } = &ev
                        && let Some(cost) = usage.cost_usd_estimate
                    {
                        self.usage_cost_usd += cost;
                        if let Some(limit) = self.spec.fleet.budget_usd
                            && self.usage_cost_usd > limit
                        {
                            self.failed.insert(session.clone());
                            let _ = self
                                .out
                                .send(OrchestratorEvent::SessionFailed {
                                    session: session.clone(),
                                    error: format!(
                                        "budget exceeded: ${:.4} > ${:.4}",
                                        self.usage_cost_usd, limit
                                    ),
                                })
                                .await;
                            self.cancel.cancel();
                            let _ = self.out.send(OrchestratorEvent::FleetComplete).await;
                            break;
                        }
                    }

                    // Record failed sessions BEFORE checking fleet_complete,
                    // otherwise the completion check won't see the failure and
                    // the fleet will hang waiting for a session that already died.
                    if let OrchestratorEvent::SessionFailed { ref session, .. } = ev {
                        self.failed.insert(session.clone());
                    }
                    match ev {
                        OrchestratorEvent::SessionDone { session, stop_reason } => {
                            // Don't forward SessionDone before routing — the consumer
                            // may see SessionFailed instead if routing (e.g. by_subtask)
                            // fails. on_session_done sends the appropriate event.
                            if self.on_session_done(&session, stop_reason).await {
                                if self.chat_mode {
                                    continue;
                                }
                                let _ = self.out.send(OrchestratorEvent::FleetComplete).await;
                                break;
                            }
                        }
                        ev @ OrchestratorEvent::SessionFailed { .. } => {
                            let _ = self.out.send(ev).await;
                            if self.fleet_complete() {
                                if self.chat_mode {
                                    continue;
                                }
                                let _ = self.out.send(OrchestratorEvent::FleetComplete).await;
                                break;
                            }
                        }
                        other => {
                            let _ = self.out.send(other).await;
                        }
                    }
                }
            }
        }

        // Ensure all session actors see the cancellation and stop promptly.
        self.cancel.cancel();

        self.registry.shutdown().await;

        // Always clean up worktrees. On cancellation the user can still inspect
        // the audit log and event stream; orphaned git worktrees are worse than
        // losing the filesystem state of a cancelled run.
        for id in self.spawned.drain() {
            if let Err(e) = self.worktree.cleanup(&id) {
                tracing::warn!(session = %id, error = %e, "worktree cleanup failed");
            }
        }
    }

    /// Handle a control message from the consumer (decision / cancel / select).
    async fn on_control(&mut self, ctrl: OrchestratorControl) {
        match ctrl {
            OrchestratorControl::Decision {
                session,
                req_id,
                allow,
            } => {
                let decision = if allow {
                    PermissionDecision::AllowOnce
                } else {
                    PermissionDecision::Deny
                };
                let _ = self
                    .registry
                    .route(
                        &session,
                        ClientFrame::PermissionResponse { req_id, decision },
                    )
                    .await;
            }
            OrchestratorControl::AskUserResponse {
                session,
                ask_id,
                value,
            } => {
                let _ = self
                    .registry
                    .route(&session, ClientFrame::AskUserAnswer { ask_id, value })
                    .await;
            }
            OrchestratorControl::Cancel => self.cancel.cancel(),
            OrchestratorControl::ReverseRpcResult {
                session,
                rpc_id,
                result,
            } => {
                let _ = self
                    .registry
                    .route(&session, ClientFrame::ReverseRpcResult { rpc_id, result })
                    .await;
            }
            // v1: selection is informational; the human merges the chosen worktree.
            OrchestratorControl::Select { .. } => {}
            OrchestratorControl::UserMessage { session, text } => {
                let _ = self
                    .registry
                    .route(
                        &session,
                        ClientFrame::Prompt {
                            content: vec![Content::text(&text)],
                        },
                    )
                    .await;
            }
        }
    }

    /// React to a session finishing. Returns `true` when the fleet is complete.
    /// Delegates routing decisions to the [`RoutingStrategy`].
    async fn on_session_done(&mut self, session: &SessionId, stop: StopReason) -> bool {
        debug!(%session, ?stop, "session done");
        self.done.insert(session.clone());

        let ctx = RoutingContext {
            spec: &self.spec,
            done: &self.done,
            failed: &self.failed,
            spawned: &self.spawned,
            buffers: &self.buffers,
            task: &self.task,
        };

        let decisions = self.strategy.on_session_done(&ctx, session, stop).await;

        enum Fire {
            Route(SessionId, ClientFrame),
            Dynamic {
                id: SessionId,
                frame: ClientFrame,
                kind: DriverKind,
                permissions: PermissionPolicy,
            },
            Select(Vec<SessionId>),
        }

        let mut fires: Vec<Fire> = Vec::new();
        let mut routing_failed = false;

        for d in decisions {
            match d {
                RouteDecision::Route { target, payload } => {
                    fires.push(Fire::Route(target, task_prompt(&payload)));
                }
                RouteDecision::DynamicRoute {
                    target,
                    payload,
                    driver,
                    permissions,
                } => {
                    fires.push(Fire::Dynamic {
                        id: target,
                        frame: task_prompt(&payload),
                        kind: driver,
                        permissions,
                    });
                }
                RouteDecision::FanOut { targets } => {
                    for (target, payload) in targets {
                        fires.push(Fire::Route(target, task_prompt(&payload)));
                    }
                }
                RouteDecision::Select { candidates } => {
                    fires.push(Fire::Select(candidates));
                }
                RouteDecision::Error(_) => {
                    routing_failed = true;
                }
                RouteDecision::None => {}
            }
        }

        // Forward SessionDone before processing routes (done → routed ordering).
        // When routing fails, send SessionFailed instead (no done).
        let from = session.clone();
        if routing_failed {
            self.done.remove(&from);
            self.failed.insert(from.clone());
            let _ = self
                .out
                .send(OrchestratorEvent::SessionFailed {
                    session: from.clone(),
                    error: "fan_out by_subtask: lead emitted no parseable \
                             cap-subtasks JSON-array block"
                        .into(),
                })
                .await;
        } else {
            let _ = self
                .out
                .send(OrchestratorEvent::SessionDone {
                    session: from.clone(),
                    stop_reason: stop,
                })
                .await;
        }

        for fire in fires {
            match fire {
                Fire::Route(to, frame) => {
                    if !self.registry.is_live(&to) && !self.spawn(&to).await {
                        continue;
                    }
                    self.audit
                        .lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .record_route(&from, &to);
                    let _ = self
                        .out
                        .send(OrchestratorEvent::Routed {
                            from: from.clone(),
                            to: to.clone(),
                        })
                        .await;
                    let _ = self.registry.route(&to, frame).await;
                }
                Fire::Dynamic {
                    id,
                    frame,
                    kind,
                    permissions,
                } => {
                    if !self.registry.is_live(&id)
                        && !self.spawn_dynamic(&id, &kind, permissions).await
                    {
                        continue;
                    }
                    self.audit
                        .lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .record_route(&from, &id);
                    let _ = self
                        .out
                        .send(OrchestratorEvent::Routed {
                            from: from.clone(),
                            to: id.clone(),
                        })
                        .await;
                    let _ = self.registry.route(&id, frame).await;
                }
                Fire::Select(candidates) => {
                    let _ = self
                        .out
                        .send(OrchestratorEvent::AwaitSelection { candidates })
                        .await;
                }
            }
        }

        self.fleet_complete()
    }

    /// The fleet is complete once every spawned session has settled — i.e.
    /// completed (`done`) or failed. Sessions that were never spawned (e.g. the
    /// targets of a route that never fired because its lead failed) do not block
    /// completion; this is how failed branches terminate without hanging while
    /// sibling branches still finish.
    fn fleet_complete(&self) -> bool {
        self.spawned
            .iter()
            .all(|s| self.done.contains(s) || self.failed.contains(s))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::FleetSpec;
    use crate::testing::{StubDriver, StubDriverFactory};
    use crate::worktree::NoopWorktreeManager;

    #[tokio::test]
    async fn chat_mode_keeps_single_session_alive_after_done() {
        let spec = FleetSpec::from_yaml(
            r#"
fleet:
  base_branch: main
  sessions:
    agent: { driver: claude, permissions: allow }
  start: agent
"#,
        )
        .unwrap();
        let factory = StubDriverFactory::new().with(
            "agent",
            StubDriver::new("agent").done(cap_rs::core::StopReason::EndTurn),
        );
        let (_handle, mut events) =
            Executor::start_chat(spec, factory, NoopWorktreeManager::new(), "say READY")
                .await
                .unwrap();

        let mut saw_done = false;
        while let Some(ev) = events.recv().await {
            match ev {
                OrchestratorEvent::SessionDone { session, .. } => {
                    saw_done = session == "agent";
                    break;
                }
                OrchestratorEvent::FleetComplete => {
                    panic!("chat mode should not complete after first turn")
                }
                _ => {}
            }
        }
        assert!(saw_done);
    }

    #[tokio::test]
    async fn budget_exceeded_cancels_fleet() {
        let spec = FleetSpec::from_yaml(
            r#"
fleet:
  base_branch: main
  budget_usd: 0.01
  sessions:
    a: { driver: claude, permissions: allow }
  start: a
"#,
        )
        .unwrap();
        let factory = StubDriverFactory::new().with(
            "a",
            StubDriver::new("a")
                .usage_cost(0.02)
                .done(cap_rs::core::StopReason::EndTurn),
        );
        let (handle, mut events) = Executor::start(spec, factory, NoopWorktreeManager::new(), "go")
            .await
            .unwrap();

        let mut saw_budget_failure = false;
        while let Some(ev) = events.recv().await {
            match ev {
                OrchestratorEvent::SessionFailed { error, .. } => {
                    if error.contains("budget exceeded") {
                        saw_budget_failure = true;
                    }
                }
                OrchestratorEvent::FleetComplete => break,
                _ => {}
            }
        }
        handle.cancel();
        assert!(saw_budget_failure);
    }
}