cflx 0.6.327

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
//! The local run scheduler behind [`RunSchedulerPort`].
//!
//! The TUI used to own the orchestrator task handle in its event loop, which
//! meant only a keypress could start or cancel a run. The supervisor owns it
//! instead, so the TUI adapter and the `/api/v2` adapter drive one object: a
//! remote start really spawns the run, and a remote force stop really cancels
//! the task a keypress would have cancelled.
//!
//! Everything here is process-local and dropped on exit. The supervisor holds no
//! workflow decision state — it starts what it is told to start and reports what
//! actually happened.

use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use async_trait::async_trait;
use tokio_util::sync::CancellationToken;
use tracing::{info, warn};

use crate::ai_command_runner::RunCommandScope;
use crate::config::OrchestratorConfig;
use crate::error::Result;
use crate::events::EventDispatcher;
use crate::orchestration::run_control::{RunPermit, RunSchedulerPort};
use crate::orchestration::state::OrchestratorState;
use crate::parallel::PostArchiveAction;
use crate::tui::orchestrator::run_orchestrator_parallel;
use crate::tui::queue::DynamicQueue;
use crate::tui::stop_classification::{collect_stop_activity_snapshot, StopActivitySnapshot};

/// Immutable launch context for a local orchestrator run.
#[derive(Clone)]
struct LaunchContext {
    repo_root: PathBuf,
    config: OrchestratorConfig,
    /// The process-lifetime authoritative dispatch owner every run publishes to.
    ///
    /// A spawned run does not build a dispatcher of its own: runner-local
    /// producers, accepted command outcomes, and orchestration runs share one
    /// owner, so one internal event is one reducer transition, one core mode
    /// transition, and one delivery per frontend regardless of who raised it.
    dispatcher: Arc<EventDispatcher>,
    dynamic_queue: DynamicQueue,
    shared_state: Arc<tokio::sync::RwLock<OrchestratorState>>,
    manual_resolve_counter: Arc<std::sync::atomic::AtomicUsize>,
    post_archive_action: PostArchiveAction,
    upstream_runtime: Option<crate::upstream::UpstreamRuntime>,
}

/// The live run's task, cancellation token, and command scope.
///
/// Shared behind an `Arc` so a prepared launch can install them at *activation*
/// time without borrowing the supervisor: preparation must leave no observable
/// trace, which includes not claiming the run slots for a launch that may still
/// be rolled back.
#[derive(Default)]
struct RunSlots {
    handle: Mutex<Option<tokio::task::JoinHandle<Result<()>>>>,
    cancel: Mutex<Option<CancellationToken>>,
    /// The live run's command scope, retained *outside* the spawned
    /// orchestrator task.
    ///
    /// Aborting that task destroys everything it owns, so a scope reachable
    /// only from inside it would take the run's process identities with it.
    /// This clone is what still lets local shutdown force-clean them.
    scope: Mutex<Option<RunCommandScope>>,
}

/// Bounded budget for proving quiescence after the run task has been drained.
///
/// It runs *after* the orchestrator shutdown grace, so it covers only the
/// escalation of identities that grace could not prove — not the run's own
/// cancellation window.
const EXTERNAL_SHUTDOWN_QUIESCENCE_BUDGET: Duration = Duration::from_secs(15);

/// What a bounded TUI shutdown proved about the run it was asked to stop.
///
/// The distinction that matters is the last variant: a TUI that cannot prove
/// its owned process groups are empty must not report a clean stop, because
/// detached agent, shell, or test descendants would keep mutating the managed
/// worktree after the operator's terminal came back.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RunShutdownOutcome {
    /// No run was live, so there was nothing to cancel and nothing to clean.
    NoActiveRun,
    /// The run was drained and every owned process group is proven empty.
    Quiescent {
        /// Owned identities that only managed escalation could prove quiescent.
        escalated: usize,
    },
    /// Bounded cleanup expired or left members behind.
    CleanupUnconfirmed {
        /// One bounded, actionable operator-facing summary.
        diagnostics: String,
    },
}

impl RunShutdownOutcome {
    /// Whether the process may exit reporting a clean stop.
    #[allow(dead_code)] // Read by shutdown-outcome coverage; the binary uses `into_exit_result`.
    pub fn is_clean(&self) -> bool {
        !matches!(self, Self::CleanupUnconfirmed { .. })
    }

    /// Classify one bounded cleanup barrier result.
    ///
    /// Kept as its own function because it is the step that decides what the
    /// operator is told, and it must be exercisable over every verdict —
    /// including the ones a real process only reaches by outliving SIGKILL.
    pub(crate) fn from_cleanup(cleanup: &crate::ai_command_runner::RunCommandScopeCleanup) -> Self {
        if cleanup.is_quiescent() {
            Self::Quiescent {
                escalated: cleanup.escalated,
            }
        } else {
            Self::CleanupUnconfirmed {
                diagnostics: cleanup.diagnostics(),
            }
        }
    }

    /// The process-level result a TUI exit reports for this outcome.
    ///
    /// Cleanup that could not be proven means owned descendants may still be
    /// mutating the managed worktree, so the process must exit non-zero with
    /// the diagnostics rather than let the operator believe the run stopped.
    pub(crate) fn into_exit_result(self) -> crate::error::Result<()> {
        match self {
            Self::CleanupUnconfirmed { diagnostics } => {
                Err(crate::error::OrchestratorError::AgentCommand(format!(
                    "TUI shutdown could not prove that run-owned processes stopped: {diagnostics}. \
                     Workspace contents were left in place; terminate the surviving processes \
                     before starting another run."
                )))
            }
            Self::NoActiveRun | Self::Quiescent { .. } => Ok(()),
        }
    }
}

/// An external SIGINT/SIGTERM asking this TUI process to stop.
///
/// The TUI owns the terminal in raw mode, so a keyboard Ctrl+C arrives as a key
/// event and never as a signal; this exists for the external `kill -INT` /
/// `kill -TERM` case, which nothing in the event loop would otherwise observe.
///
/// The watcher only *records* the request. It never exits the process, because
/// exiting from a signal handler is exactly what leaves detached agent, shell,
/// and test descendants behind: the request is drained by the event loop, which
/// then leaves through [`TuiRunSupervisor::shutdown_run`] like every other exit.
#[derive(Clone, Default)]
pub struct ExternalShutdownRequest {
    requested: Arc<AtomicBool>,
}

impl ExternalShutdownRequest {
    pub fn new() -> Self {
        Self::default()
    }

    /// Whether an external signal has asked this process to stop.
    pub fn is_requested(&self) -> bool {
        self.requested.load(Ordering::SeqCst)
    }

    /// Record the request. Idempotent: a second signal adds nothing to observe.
    pub fn request(&self) {
        self.requested.store(true, Ordering::SeqCst);
    }

    /// Install the process-wide SIGINT and SIGTERM watchers.
    ///
    /// Failing to install a handler is not fatal: the TUI still runs, and the
    /// default disposition stays in effect for that one signal. Refusing to
    /// start over it would be a worse trade than losing the graceful path.
    pub fn install(&self) {
        #[cfg(unix)]
        {
            let request = self.clone();
            tokio::spawn(async move {
                match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
                    Ok(mut sigterm) => {
                        sigterm.recv().await;
                        info!("Received SIGTERM; routing the TUI through run shutdown");
                        request.request();
                    }
                    Err(err) => warn!(error = %err, "Could not install the TUI SIGTERM handler"),
                }
            });
        }

        let request = self.clone();
        tokio::spawn(async move {
            if tokio::signal::ctrl_c().await.is_ok() {
                info!("Received SIGINT; routing the TUI through run shutdown");
                request.request();
            }
        });
    }
}

/// Owns the local orchestrator task, its cancellation token, and the flags that
/// steer it.
pub struct TuiRunSupervisor {
    launch: LaunchContext,
    slots: Arc<RunSlots>,
    graceful_stop: Arc<AtomicBool>,
}

impl TuiRunSupervisor {
    /// Build a supervisor for the current TUI invocation.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        repo_root: PathBuf,
        config: OrchestratorConfig,
        dispatcher: Arc<EventDispatcher>,
        dynamic_queue: DynamicQueue,
        shared_state: Arc<tokio::sync::RwLock<OrchestratorState>>,
        manual_resolve_counter: Arc<std::sync::atomic::AtomicUsize>,
        post_archive_action: PostArchiveAction,
        upstream_runtime: Option<crate::upstream::UpstreamRuntime>,
        graceful_stop: Arc<AtomicBool>,
    ) -> Self {
        Self {
            launch: LaunchContext {
                repo_root,
                config,
                dispatcher,
                dynamic_queue,
                shared_state,
                manual_resolve_counter,
                post_archive_action,
                upstream_runtime,
            },
            slots: Arc::new(RunSlots::default()),
            graceful_stop,
        }
    }

    /// Take the current run task, cancellation token, and command scope for shutdown.
    pub fn take_run(
        &self,
    ) -> (
        Option<tokio::task::JoinHandle<Result<()>>>,
        Option<CancellationToken>,
        Option<RunCommandScope>,
    ) {
        let handle = lock(&self.slots.handle).take();
        let cancel = lock(&self.slots.cancel).take();
        let scope = lock(&self.slots.scope).take();
        (handle, cancel, scope)
    }

    /// The command scope of the live run, if any.
    ///
    /// The managed ownership graph a targeted force-stop signals through, read
    /// per call rather than captured: a scope belongs to one run, so a caller
    /// holding an old one would be addressing PGIDs that were reaped runs ago.
    /// `None` between runs means this process owns no managed process at all.
    pub fn run_command_scope(&self) -> Option<RunCommandScope> {
        lock(&self.slots.scope).clone()
    }

    /// Install run slots directly.
    ///
    /// Shutdown coverage needs a run whose scope it controls; spawning the real
    /// orchestrator would give it a scope with nothing registered in it, which
    /// is the one case shutdown has nothing to prove about.
    #[cfg(test)]
    pub(crate) fn install_run_for_test(
        &self,
        handle: Option<tokio::task::JoinHandle<Result<()>>>,
        cancel: Option<CancellationToken>,
        scope: Option<RunCommandScope>,
    ) {
        *lock(&self.slots.handle) = handle;
        *lock(&self.slots.cancel) = cancel;
        *lock(&self.slots.scope) = scope;
    }

    /// The cancellation token of the live run, if any.
    pub fn cancel_token(&self) -> Option<CancellationToken> {
        lock(&self.slots.cancel).clone()
    }

    /// The one bounded shutdown boundary every TUI exit path goes through.
    ///
    /// Keyboard quit, an operator force stop, and an external SIGINT/SIGTERM all
    /// call this rather than exiting on their own. A signal that terminated the
    /// process directly would skip three things that only this sequence does:
    /// closing command admission, running the graceful-then-forceful cleanup of
    /// owned process groups, and — through the run cancellation it propagates —
    /// letting an in-flight Apply preserve its dirty worktree before the run ends.
    ///
    /// The sequence is ordered: admission closes first so no command can start
    /// while the process is already leaving, then the run is cancelled and
    /// drained, and only then is quiescence proven. A failure is returned, never
    /// swallowed: the caller decides the exit status, but it cannot be told the
    /// stop was clean when it was not.
    pub async fn shutdown_run(&self, grace_period: Duration) -> RunShutdownOutcome {
        self.shutdown_run_with_budget(grace_period, EXTERNAL_SHUTDOWN_QUIESCENCE_BUDGET)
            .await
    }

    /// [`Self::shutdown_run`] with an explicit quiescence budget.
    ///
    /// The budget is a parameter only so coverage can drive the unprovable-cleanup
    /// branch without waiting out the production window; production always uses
    /// [`EXTERNAL_SHUTDOWN_QUIESCENCE_BUDGET`].
    pub(crate) async fn shutdown_run_with_budget(
        &self,
        grace_period: Duration,
        quiescence_budget: Duration,
    ) -> RunShutdownOutcome {
        let (handle, cancel, scope) = self.take_run();
        if handle.is_none() && cancel.is_none() && scope.is_none() {
            return RunShutdownOutcome::NoActiveRun;
        }

        // Admission closes before cancellation rather than after the grace
        // period, so a command cannot be admitted into a run that is already
        // being torn down.
        if let Some(scope) = &scope {
            scope.close();
        }

        crate::tui::runner::shutdown_local_orchestrator_task(
            handle,
            cancel,
            scope.clone(),
            grace_period,
        )
        .await;

        // A run with no scope owns no commands, so there is no process identity
        // left that could still hold the managed worktree.
        let Some(scope) = scope else {
            return RunShutdownOutcome::Quiescent { escalated: 0 };
        };

        let cleanup = scope.wait_quiescent(quiescence_budget).await;
        let outcome = RunShutdownOutcome::from_cleanup(&cleanup);
        match &outcome {
            RunShutdownOutcome::CleanupUnconfirmed { diagnostics } => warn!(
                "TUI shutdown could not prove run-owned process cleanup: {}",
                diagnostics
            ),
            _ => info!(
                escalated = cleanup.escalated,
                "TUI shutdown proved every owned process group quiescent"
            ),
        }
        outcome
    }

    /// Build the closure that actually launches the run.
    ///
    /// Everything it needs is cloned here; running it is a pure `spawn` with no
    /// fallible step left, which is what makes activation infallible.
    fn launcher(&self, targets: Vec<String>, explicit_retry: bool) -> impl FnOnce() + Send {
        let launch = self.launch.clone();
        let slots = self.slots.clone();
        let graceful_stop = self.graceful_stop.clone();

        move || {
            let cancel = CancellationToken::new();
            // One fresh scope per run start. A closed scope is never reused, so
            // a restarted run always begins with open admission.
            let scope = RunCommandScope::new();
            scope.link_cancellation(cancel.clone());

            // Every launch — selected targets and scheduler-owned resolve alike
            // — dispatches the cumulative worktree scheduler, so an opted-in
            // upstream session always reaches the path its publication contract
            // is defined on.
            graceful_stop.store(false, Ordering::SeqCst);

            let run_cancel = cancel.clone();
            let run_scope = scope.clone();
            let run_graceful_stop = graceful_stop.clone();
            let handle = tokio::spawn(async move {
                run_orchestrator_parallel(
                    targets,
                    explicit_retry,
                    launch.repo_root,
                    launch.config,
                    launch.dispatcher,
                    run_cancel,
                    run_scope,
                    launch.dynamic_queue,
                    launch.shared_state,
                    launch.manual_resolve_counter,
                    launch.post_archive_action,
                    launch.upstream_runtime,
                    run_graceful_stop,
                )
                .await
            });

            *lock(&slots.handle) = Some(handle);
            *lock(&slots.cancel) = Some(cancel);
            *lock(&slots.scope) = Some(scope);
        }
    }
}

fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

#[async_trait]
impl RunSchedulerPort for TuiRunSupervisor {
    fn is_running(&self) -> bool {
        lock(&self.slots.handle)
            .as_ref()
            .is_some_and(|handle| !handle.is_finished())
    }

    /// Reserve a launch without spawning it.
    ///
    /// There is no fallible step: the orchestrator task is spawned by the
    /// returned permit, so nothing exists to emit an event, claim the run slots,
    /// or be observed as running until the accepted command outcome has already
    /// been published.
    async fn prepare_run(
        &self,
        targets: Vec<String>,
        explicit_retry: bool,
    ) -> std::result::Result<RunPermit, String> {
        Ok(RunPermit::new(self.launcher(targets, explicit_retry)))
    }

    async fn notify_scheduler(&self) {
        self.launch.dynamic_queue.notify_scheduler();
    }

    async fn cancel_run(&self) {
        if let Some(cancel) = self.cancel_token() {
            cancel.cancel();
        }
    }

    fn set_graceful_stop(&self, requested: bool) {
        self.graceful_stop.store(requested, Ordering::SeqCst);
    }

    async fn stop_activity(&self) -> StopActivitySnapshot {
        collect_stop_activity_snapshot(&self.launch.dynamic_queue, &self.launch.shared_state).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::events::ExecutionEvent;
    use crate::orchestration::state::ReducerCommand;
    use crate::tui::events::OrchestratorEvent;
    use tokio::sync::mpsc;

    /// Marker the worktree orchestrator emits before it touches anything else,
    /// which is what makes the dispatched path observable from outside.
    const PARALLEL_START_MARKER: &str = "Starting parallel processing";

    fn shared_state(change_ids: &[&str]) -> Arc<tokio::sync::RwLock<OrchestratorState>> {
        Arc::new(tokio::sync::RwLock::new(OrchestratorState::new(
            change_ids.iter().map(|id| id.to_string()).collect(),
            10,
        )))
    }

    /// The process-lifetime dispatch owner a spawned run publishes through.
    ///
    /// Built here rather than inside the supervisor because production builds
    /// exactly one for the whole process and hands it in; a supervisor that made
    /// its own would be the per-run dispatcher this change removed.
    fn dispatcher(
        tx: mpsc::Sender<OrchestratorEvent>,
        state: Arc<tokio::sync::RwLock<OrchestratorState>>,
    ) -> Arc<EventDispatcher> {
        Arc::new(EventDispatcher::new(
            state,
            vec![Arc::new(crate::tui::events::TuiEventSink::new(tx))],
        ))
    }

    fn supervisor(
        repo_root: PathBuf,
        tx: mpsc::Sender<OrchestratorEvent>,
        state: Arc<tokio::sync::RwLock<OrchestratorState>>,
        upstream_runtime: Option<crate::upstream::UpstreamRuntime>,
    ) -> TuiRunSupervisor {
        TuiRunSupervisor::new(
            repo_root,
            OrchestratorConfig::default(),
            dispatcher(tx, state.clone()),
            DynamicQueue::new(),
            state,
            Arc::new(std::sync::atomic::AtomicUsize::new(0)),
            PostArchiveAction::MergeToBase,
            upstream_runtime,
            Arc::new(AtomicBool::new(false)),
        )
    }

    /// Record reducer-owned `ResolveWait` for `change_id`, the way the shared
    /// run-control service does before it dispatches a manual resolve.
    async fn reserve_resolve_wait(
        state: &Arc<tokio::sync::RwLock<OrchestratorState>>,
        change_id: &str,
    ) {
        let mut guard = state.write().await;
        guard.apply_execution_event(&ExecutionEvent::MergeDeferred {
            change_id: change_id.to_string(),
            reason: "manual resolution required".to_string(),
            auto_resumable: false,
        });
        guard.apply_command(ReducerCommand::ResolveMerge(change_id.to_string()));
        assert_eq!(
            guard.resolve_wait_change_ids(),
            vec![change_id.to_string()],
            "arrangement must record the intent the dispatch exists to serve"
        );
    }

    /// Every launch reaches the cumulative worktree scheduler: a selected target
    /// and a scheduler-owned resolve alike, with no mode flag anywhere.
    #[tokio::test]
    async fn every_launch_dispatches_the_worktree_scheduler() {
        for targets in [Vec::new(), vec!["alpha".to_string()]] {
            let dir = tempfile::tempdir().expect("tempdir");
            let (tx, mut rx) = mpsc::channel(64);
            let state = shared_state(&["alpha"]);
            if targets.is_empty() {
                reserve_resolve_wait(&state, "alpha").await;
            }

            let supervisor = supervisor(dir.path().to_path_buf(), tx, state.clone(), None);

            supervisor
                .start_run(targets.clone(), false)
                .await
                .expect("the launch must dispatch");

            let started = tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv())
                .await
                .expect("the dispatched run must announce itself")
                .expect("the dispatched run must emit its first event");
            match started {
                ExecutionEvent::Log(entry) => assert!(
                    entry.message.contains(PARALLEL_START_MARKER),
                    "every launch must reach the worktree orchestrator, got: {}",
                    entry.message
                ),
                other => panic!("expected the worktree startup log, got: {other:?}"),
            }

            let (handle, cancel, _scope) = supervisor.take_run();
            if let Some(cancel) = cancel {
                cancel.cancel();
            }
            if let Some(handle) = handle {
                handle.abort();
                let _ = handle.await;
            }

            if targets.is_empty() {
                assert_eq!(
                    state.read().await.resolve_wait_change_ids(),
                    vec!["alpha".to_string()],
                    "the dispatched run must not wipe the intent it was started to consume"
                );
            }
        }
    }

    /// Where an externally-signalled TUI exit has to end up.
    ///
    /// A signal handler that exits the process directly is the failure this
    /// boundary exists to prevent: the terminal comes back, the operator
    /// believes the run stopped, and detached agent, shell, and test
    /// descendants keep mutating the managed worktree. So the contract is not
    /// "we sent a signal" but "admission is closed, the run is cancelled, and
    /// the owned groups are *proven* empty" — and when that proof cannot be
    /// produced, the process must say so instead of reporting a clean stop.
    mod external_shutdown {
        use super::*;
        use crate::ai_command_runner::{AiCommandRunner, RunCommandScope, RunCommandScopeCleanup};

        /// Long enough that nothing but shutdown can end these commands.
        const NEVER_ENDS: &str = "sleep 300";

        fn test_supervisor() -> TuiRunSupervisor {
            let dir = tempfile::tempdir().expect("tempdir");
            let (tx, _rx) = mpsc::channel(64);
            let state = shared_state(&["alpha"]);
            let supervisor = supervisor(dir.path().to_path_buf(), tx, state, None);
            // The temp dir only has to outlive construction: nothing in these
            // tests dispatches a run that would read from it.
            std::mem::forget(dir);
            supervisor
        }

        fn scoped_runner(scope: &RunCommandScope) -> AiCommandRunner {
            let config = OrchestratorConfig {
                command_queue_stagger_delay_ms: Some(0),
                command_queue_max_retries: Some(1),
                command_queue_retry_delay_ms: Some(0),
                // Only the shutdown path may end these commands.
                command_inactivity_timeout_secs: Some(0),
                command_inactivity_timeout_max_retries: Some(0),
                command_max_runtime_secs: Some(0),
                ..OrchestratorConfig::default()
            };
            AiCommandRunner::for_run(
                &config,
                Arc::new(tokio::sync::Mutex::new(None)),
                scope.clone(),
            )
        }

        /// A task that never finishes on its own, standing in for an
        /// orchestrator that has stopped cooperating.
        fn uncooperative_run() -> tokio::task::JoinHandle<Result<()>> {
            tokio::spawn(async {
                std::future::pending::<()>().await;
                Ok(())
            })
        }

        /// A signal is a *request*, never an exit. Recording it is all the
        /// handler may do, because the event loop is what carries it into the
        /// bounded shutdown boundary.
        #[test]
        fn an_external_signal_is_recorded_as_a_request() {
            let request = ExternalShutdownRequest::new();
            assert!(
                !request.is_requested(),
                "a fresh process has not been asked to stop"
            );

            request.request();
            assert!(request.is_requested());

            // A second signal adds nothing new to observe, so escalation cannot
            // be built on "we saw it twice" by accident.
            request.request();
            assert!(request.is_requested());

            // The watcher shares one flag with the loop that drains it.
            let observer = request.clone();
            assert!(observer.is_requested());
        }

        /// A remote TUI owns no local run, so closing it must send no stop.
        #[tokio::test]
        async fn a_supervisor_with_no_run_reports_no_active_run() {
            let supervisor = test_supervisor();
            assert_eq!(
                supervisor.shutdown_run(Duration::from_millis(50)).await,
                RunShutdownOutcome::NoActiveRun
            );
        }

        /// The whole ordered sequence, against a real owned command: admission
        /// closes, the run is cancelled, the registration drains, and no owned
        /// process identity is left behind.
        #[tokio::test]
        async fn external_shutdown_closes_admission_drains_and_proves_quiescence() {
            let supervisor = test_supervisor();
            let scope = RunCommandScope::new();
            let cancel = CancellationToken::new();
            scope.link_cancellation(cancel.clone());
            let runner = scoped_runner(&scope);

            let (handle, mut rx) = runner
                .execute_streaming_with_retry(NEVER_ENDS, None, Some("apply"), Some("alpha"))
                .await
                .expect("an open scope admits the command");
            // Drain so the runner task is never blocked on its output channel.
            let drain = tokio::spawn(async move { while rx.recv().await.is_some() {} });

            let deadline = std::time::Instant::now() + Duration::from_secs(5);
            while scope.active_executions() == 0 {
                assert!(
                    std::time::Instant::now() < deadline,
                    "arrangement failed: the command never registered"
                );
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
            // The workspace future that held the handle is gone, exactly as
            // `JoinSet::abort_all` leaves it.
            drop(handle);

            supervisor.install_run_for_test(
                Some(uncooperative_run()),
                Some(cancel.clone()),
                Some(scope.clone()),
            );

            let outcome = supervisor
                .shutdown_run_with_budget(Duration::from_millis(50), Duration::from_secs(30))
                .await;
            drain.await.expect("the drain task joins");

            assert!(
                matches!(outcome, RunShutdownOutcome::Quiescent { .. }),
                "the owned group must be proven empty: {outcome:?}"
            );
            assert!(outcome.is_clean());
            assert!(
                scope.is_closed(),
                "command admission must be closed by shutdown"
            );
            assert!(
                cancel.is_cancelled(),
                "the run must be cancelled, which is what lets an in-flight \
                 Apply preserve its worktree before the run ends"
            );
            assert_eq!(
                scope.active_executions(),
                0,
                "every registered execution must have drained"
            );
            assert!(
                scope.retained_process_ids().is_empty(),
                "no owned process identity may remain after a clean shutdown"
            );

            // Retry and spawn admission stay closed: nothing may start into a
            // run that is already gone.
            let (mut refused, mut refused_rx) = runner
                .execute_streaming_with_retry("echo never", None, Some("apply"), Some("beta"))
                .await
                .expect("the call returns a refusal rather than launching");
            while refused_rx.recv().await.is_some() {}
            assert!(
                !refused.wait().await.expect("status").success(),
                "a command admitted after shutdown would outlive the process"
            );

            // The slots are consumed, so a second shutdown has nothing to stop.
            assert_eq!(
                supervisor.shutdown_run(Duration::from_millis(10)).await,
                RunShutdownOutcome::NoActiveRun
            );
        }

        /// Cleanup that cannot be proven is never rounded up to a clean stop.
        ///
        /// This is the branch that decides what the operator is told, and both
        /// of its failure shapes matter: a barrier that expired with a runner
        /// still active, and a barrier that finished but left an owned identity
        /// it could not verify. Neither can be arranged with a real process —
        /// force-kill escalation eventually wins against anything a test can
        /// spawn — so the verdicts are driven directly.
        #[test]
        fn an_unproven_barrier_is_never_reported_as_a_clean_stop() {
            let expired = RunCommandScopeCleanup {
                escalated: 0,
                unconfirmed: Vec::new(),
                timed_out: true,
            };
            let outcome = RunShutdownOutcome::from_cleanup(&expired);
            let RunShutdownOutcome::CleanupUnconfirmed { diagnostics } = &outcome else {
                panic!("an expired barrier is not proof of quiescence: {outcome:?}");
            };
            assert!(!outcome.is_clean());
            assert!(
                diagnostics.contains("unconfirmed") && diagnostics.contains("timed_out=true"),
                "the operator needs actionable diagnostics: {diagnostics}"
            );

            let survivors = RunCommandScopeCleanup {
                escalated: 1,
                unconfirmed: vec!["apply/alpha pgid=4242 members remain".to_string()],
                timed_out: false,
            };
            let outcome = RunShutdownOutcome::from_cleanup(&survivors);
            let RunShutdownOutcome::CleanupUnconfirmed { diagnostics } = &outcome else {
                panic!("a surviving owned identity is not a clean stop: {outcome:?}");
            };
            assert!(!outcome.is_clean());
            assert!(
                diagnostics.contains("apply/alpha"),
                "the surviving identity must be named: {diagnostics}"
            );
        }

        #[test]
        fn a_proven_barrier_is_a_clean_stop_even_after_escalation() {
            let escalated = RunCommandScopeCleanup {
                escalated: 2,
                unconfirmed: Vec::new(),
                timed_out: false,
            };
            assert_eq!(
                RunShutdownOutcome::from_cleanup(&escalated),
                RunShutdownOutcome::Quiescent { escalated: 2 },
                "forced-but-verified cleanup is still verified cleanup"
            );
            assert!(RunShutdownOutcome::from_cleanup(&escalated).is_clean());
        }

        /// The exit status is the operator-visible half of the contract: a
        /// shutdown that could not prove cleanup must fail the process, and a
        /// proven one must not.
        #[test]
        fn unproven_cleanup_exits_non_zero_with_its_diagnostics() {
            let error = RunShutdownOutcome::CleanupUnconfirmed {
                diagnostics: "run command cleanup unconfirmed (timed_out=true): apply/alpha"
                    .to_string(),
            }
            .into_exit_result()
            .expect_err("an unproven shutdown must not report success");
            let rendered = error.to_string();
            assert!(
                rendered.contains("could not prove that run-owned processes stopped"),
                "the failure must say what could not be proven: {rendered}"
            );
            assert!(
                rendered.contains("apply/alpha"),
                "the barrier diagnostics must reach the operator: {rendered}"
            );
            assert!(
                rendered.contains("Workspace contents were left in place"),
                "recovery must stay possible and be stated: {rendered}"
            );

            assert!(RunShutdownOutcome::NoActiveRun.into_exit_result().is_ok());
            assert!(RunShutdownOutcome::Quiescent { escalated: 0 }
                .into_exit_result()
                .is_ok());
            assert!(RunShutdownOutcome::Quiescent { escalated: 3 }
                .into_exit_result()
                .is_ok());
        }
    }

    /// An opted-in upstream session is never refused: there is no dispatch path
    /// left that carries no upstream runtime.
    #[tokio::test]
    async fn an_opted_in_upstream_session_is_never_refused() {
        let dir = tempfile::tempdir().expect("tempdir");
        let (tx, _rx) = mpsc::channel(64);
        let state = shared_state(&["alpha"]);
        reserve_resolve_wait(&state, "alpha").await;

        let supervisor = supervisor(
            dir.path().to_path_buf(),
            tx,
            state,
            Some(crate::upstream::UpstreamRuntime {
                config: crate::upstream::UpstreamIntegrationConfig::new("origin", "exit 0"),
                branch: "main".to_string(),
            }),
        );

        supervisor
            .start_run(Vec::new(), false)
            .await
            .expect("the launch keeps the publication contract");

        let (handle, cancel, _scope) = supervisor.take_run();
        if let Some(cancel) = cancel {
            cancel.cancel();
        }
        if let Some(handle) = handle {
            handle.abort();
            let _ = handle.await;
        }
    }
}