Skip to main content

rigspark_cli/
tui_lifecycle.rs

1use crate::tui_theme::{self, Theme};
2use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
3use futures_util::{Stream, StreamExt, stream};
4use ratatui::{
5    Frame, Terminal,
6    backend::Backend,
7    layout::{Constraint, Layout},
8    style::Style,
9    text::{Line, Span},
10    widgets::{Paragraph, Wrap},
11};
12use rigspark_core::reports::strip_control;
13use rigspark_runtime::{
14    adapters::BackendError,
15    application::events::{
16        DiagnosticObserver, DiagnosticSnapshot, EVENT_CAPACITY, LifecycleEvent, LifecycleScope,
17        LifecycleStage, LifecycleStatus,
18    },
19};
20use serde_json::Value;
21use std::{
22    collections::VecDeque,
23    future::Future,
24    io::{self, Write},
25    time::Duration,
26};
27use tokio::sync::mpsc::Receiver;
28use tokio_util::sync::CancellationToken;
29
30type RuntimeResult = Result<(Value, String), BackendError>;
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33enum Phase {
34    Running,
35    Event(LifecycleEvent),
36    Redraw(i16),
37    Cancelling,
38    Restore,
39    Completed,
40    Failed,
41}
42
43enum Control {
44    Cancel(u8),
45    Redraw(i16),
46    Resize { eligible: bool },
47}
48
49pub struct Outcome {
50    pub result: RuntimeResult,
51    pub exit_code: u8,
52    pub presentation_error: Option<String>,
53    pub presentation_restored: bool,
54    pub diagnostics: Option<DiagnosticSnapshot>,
55}
56
57async fn drive(
58    operation: impl Future<Output = RuntimeResult>,
59    cancel: &CancellationToken,
60    controls: impl Stream<Item = io::Result<Control>>,
61    events: Option<Receiver<LifecycleEvent>>,
62    present: impl FnMut(Phase) -> io::Result<()>,
63) -> io::Result<Outcome> {
64    drive_with_redraw(
65        operation,
66        cancel,
67        controls,
68        events,
69        present,
70        stream::pending(),
71    )
72    .await
73}
74
75async fn drive_with_redraw(
76    operation: impl Future<Output = RuntimeResult>,
77    cancel: &CancellationToken,
78    controls: impl Stream<Item = io::Result<Control>>,
79    events: Option<Receiver<LifecycleEvent>>,
80    present: impl FnMut(Phase) -> io::Result<()>,
81    redraw: impl Stream<Item = ()>,
82) -> io::Result<Outcome> {
83    drive_with_deadline(
84        operation,
85        cancel,
86        controls,
87        events,
88        present,
89        redraw,
90        Duration::from_millis(crate::cancellation::CLEANUP_TIMEOUT_MS),
91    )
92    .await
93}
94
95async fn drive_with_deadline(
96    operation: impl Future<Output = RuntimeResult>,
97    cancel: &CancellationToken,
98    controls: impl Stream<Item = io::Result<Control>>,
99    mut events: Option<Receiver<LifecycleEvent>>,
100    mut present: impl FnMut(Phase) -> io::Result<()>,
101    redraw: impl Stream<Item = ()>,
102    cleanup_timeout: Duration,
103) -> io::Result<Outcome> {
104    present(Phase::Running)?;
105    let deadline = async {
106        cancel.cancelled().await;
107        tokio::time::sleep(cleanup_timeout).await;
108    };
109    tokio::pin!(deadline);
110    tokio::pin!(operation, controls, redraw);
111    let mut exit_code = 0;
112    let mut presentation_error = None;
113    let mut controls_done = false;
114    let mut output_failed = false;
115    let mut restored = false;
116    let mut resize_deadline = None;
117    let result = loop {
118        tokio::select! {
119            biased;
120            _ = &mut deadline, if !restored => {
121                restored = true;
122                if let Err(error) = present(Phase::Restore) {
123                    presentation_error.get_or_insert_with(|| error.to_string());
124                }
125                output_failed = true;
126            }
127            _ = async {
128                if let Some(deadline) = resize_deadline {
129                    tokio::time::sleep_until(deadline).await;
130                } else { std::future::pending::<()>().await; }
131            }, if !restored && !cancel.is_cancelled() => {
132                restored = true;
133                resize_deadline = None;
134                if let Err(error) = present(Phase::Restore) {
135                    presentation_error.get_or_insert_with(|| error.to_string());
136                }
137                output_failed = true;
138            }
139            control = controls.next(), if !controls_done => {
140                if let Some(Ok(Control::Resize { eligible })) = control {
141                    if !restored {
142                        resize_deadline = (!eligible).then(||
143                            tokio::time::Instant::now() + Duration::from_millis(50));
144                    }
145                    continue;
146                }
147                if let Some(Ok(Control::Redraw(scroll))) = control {
148                    if !output_failed && let Err(error) = present(Phase::Redraw(scroll)) {
149                        output_failed = true;
150                        presentation_error.get_or_insert_with(|| error.to_string());
151                        exit_code = 1;
152                        cancel.cancel();
153                    }
154                    continue;
155                }
156                controls_done = true;
157                match control {
158                    Some(Ok(Control::Cancel(code))) => exit_code = code,
159                    failure => {
160                        exit_code = 1;
161                        presentation_error.get_or_insert_with(|| match failure {
162                            Some(Err(error)) => error.to_string(),
163                            _ => "lifecycle control input ended".into(),
164                        });
165                    }
166                }
167                cancel.cancel();
168                if !output_failed && let Err(error) = present(Phase::Cancelling) {
169                    output_failed = true;
170                    presentation_error.get_or_insert_with(|| error.to_string());
171                }
172            }
173            result = &mut operation => break result,
174            event = async {
175                match &mut events {
176                    Some(receiver) => receiver.recv().await,
177                    None => std::future::pending().await,
178                }
179            }, if events.is_some() => {
180                match event {
181                    Some(event) if !output_failed => {
182                        if let Err(error) = present(Phase::Event(event)) {
183                            output_failed = true;
184                            presentation_error.get_or_insert_with(|| error.to_string());
185                            if exit_code == 0 {
186                                exit_code = 1;
187                            }
188                            cancel.cancel();
189                        }
190                    }
191                    Some(_) => (),
192                    None => events = None,
193                }
194            }
195            Some(()) = redraw.next(), if !output_failed => {
196                if let Err(error) = present(Phase::Redraw(0)) {
197                    output_failed = true;
198                    presentation_error.get_or_insert_with(|| error.to_string());
199                    if exit_code == 0 { exit_code = 1; }
200                    cancel.cancel();
201                }
202            }
203        }
204    };
205    if let Some(receiver) = &mut events {
206        receiver.close();
207        while let Ok(event) = receiver.try_recv() {
208            if !output_failed && let Err(error) = present(Phase::Event(event)) {
209                output_failed = true;
210                presentation_error.get_or_insert_with(|| error.to_string());
211                cancel.cancel();
212            }
213        }
214    }
215    if presentation_error.is_some() && exit_code == 0 {
216        exit_code = 1;
217    }
218    if result.is_err() && exit_code == 0 {
219        exit_code = 1;
220    }
221    if !output_failed
222        && let Err(error) = present(if result.is_ok() {
223            Phase::Completed
224        } else {
225            Phase::Failed
226        })
227    {
228        presentation_error.get_or_insert_with(|| error.to_string());
229        if exit_code == 0 {
230            exit_code = 1;
231        }
232    }
233    Ok(Outcome {
234        result,
235        exit_code,
236        presentation_error,
237        presentation_restored: restored,
238        diagnostics: None,
239    })
240}
241
242pub async fn interruption() -> io::Result<u8> {
243    crate::cancellation::TerminalSignals::new()?.recv().await
244}
245
246pub async fn run(
247    command: &str,
248    target: &str,
249    operation: impl Future<Output = RuntimeResult>,
250    cancel: &CancellationToken,
251    interactive: bool,
252) -> io::Result<Outcome> {
253    run_observed(command, target, operation, cancel, interactive, None).await
254}
255
256pub async fn run_observed(
257    command: &str,
258    target: &str,
259    operation: impl Future<Output = RuntimeResult>,
260    cancel: &CancellationToken,
261    interactive: bool,
262    events: Option<Receiver<LifecycleEvent>>,
263) -> io::Result<Outcome> {
264    let mut signals = crate::cancellation::TerminalSignals::new()?;
265    let controls = stream::once(async { signals.recv().await.map(Control::Cancel) });
266    let sizes = resize_controls(interactive);
267    let mut output = io::stderr();
268    drive(
269        operation,
270        cancel,
271        stream::select(controls, sizes),
272        events,
273        |phase| {
274            if !interactive {
275                return Ok(());
276            }
277            write_phase(&mut output, command, target, phase)
278        },
279    )
280    .await
281}
282
283pub async fn run_visual(
284    command: &str,
285    target: &str,
286    operation: impl Future<Output = RuntimeResult>,
287    cancel: &CancellationToken,
288    color: bool,
289    events: Receiver<LifecycleEvent>,
290    diagnostics: DiagnosticObserver,
291) -> io::Result<Outcome> {
292    let mut terminal_signals = crate::cancellation::TerminalSignals::new()?;
293    let (mut terminal, restore) = crate::tui_view::enter_terminal()?;
294    let mut restore = Some(restore);
295    let mut view = LifecycleView::new(command, target, color);
296    let input_owner = std::sync::Arc::new(std::sync::Mutex::new(Some(
297        crate::terminal_events::terminal_events(),
298    )));
299    let input = visual_input(input_owner.clone());
300    let signals = stream::once(async { terminal_signals.recv().await.map(Control::Cancel) });
301    let outcome = drive_visual(
302        (&mut terminal, || {
303            input_owner
304                .lock()
305                .unwrap_or_else(|error| error.into_inner())
306                .take();
307            drop(restore.take());
308        }),
309        &mut view,
310        operation,
311        cancel,
312        stream::select(signals, input),
313        events,
314        &diagnostics,
315    )
316    .await;
317    if view.started.elapsed() >= Duration::from_secs(10) {
318        // Audible cue that a long pull or start finished while the user looked away.
319        let _ = io::Write::write_all(&mut io::stderr(), b"\x07");
320    }
321    outcome
322}
323
324fn visual_input<Events>(
325    owner: std::sync::Arc<std::sync::Mutex<Option<Events>>>,
326) -> impl Stream<Item = io::Result<Control>>
327where
328    Events: Stream<Item = io::Result<Event>> + Unpin,
329{
330    stream::poll_fn(move |context| {
331        let mut owner = owner.lock().unwrap_or_else(|error| error.into_inner());
332        let Some(events) = owner.as_mut() else {
333            return std::task::Poll::Ready(None);
334        };
335        match std::pin::Pin::new(events).poll_next(context) {
336            std::task::Poll::Ready(Some(event)) => {
337                std::task::Poll::Ready(Some(event.map(visual_control)))
338            }
339            std::task::Poll::Ready(None) => {
340                owner.take();
341                std::task::Poll::Ready(Some(Err(io::Error::other("terminal input ended"))))
342            }
343            std::task::Poll::Pending => std::task::Poll::Pending,
344        }
345    })
346}
347
348fn resize_control(columns: u16, rows: u16, accessible: bool) -> Control {
349    let (minimum_columns, minimum_rows) = if accessible { (40, 10) } else { (60, 16) };
350    Control::Resize {
351        eligible: columns >= minimum_columns && rows >= minimum_rows,
352    }
353}
354
355fn resize_controls(enabled: bool) -> impl Stream<Item = io::Result<Control>> {
356    let timer = tokio::time::interval(Duration::from_millis(50));
357    stream::unfold((timer, None), move |(mut timer, mut previous)| async move {
358        if !enabled {
359            return std::future::pending().await;
360        }
361        loop {
362            timer.tick().await;
363            let size = crossterm::terminal::size().unwrap_or((0, 0));
364            if previous != Some(size) {
365                previous = Some(size);
366                return Some((Ok(resize_control(size.0, size.1, true)), (timer, previous)));
367            }
368        }
369    })
370}
371
372fn visual_control(event: Event) -> Control {
373    if let Event::Resize(columns, rows) = event {
374        return resize_control(columns, rows, false);
375    }
376    if let Event::Mouse(mouse) = event {
377        return match mouse.kind {
378            crossterm::event::MouseEventKind::ScrollUp => Control::Redraw(-3),
379            crossterm::event::MouseEventKind::ScrollDown => Control::Redraw(3),
380            _ => Control::Redraw(0),
381        };
382    }
383    if let Event::Key(key) = event
384        && key.kind != KeyEventKind::Release
385    {
386        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
387            return Control::Cancel(130);
388        }
389        return match key.code {
390            KeyCode::Esc | KeyCode::Char('q') => Control::Cancel(130),
391            KeyCode::Up => Control::Redraw(-1),
392            KeyCode::Down => Control::Redraw(1),
393            KeyCode::PageUp => Control::Redraw(-10),
394            KeyCode::PageDown => Control::Redraw(10),
395            _ => Control::Redraw(0),
396        };
397    }
398    Control::Redraw(0)
399}
400
401async fn drive_visual<B: Backend>(
402    terminal: (&mut Terminal<B>, impl FnMut()),
403    view: &mut LifecycleView,
404    operation: impl Future<Output = RuntimeResult>,
405    cancel: &CancellationToken,
406    controls: impl Stream<Item = io::Result<Control>>,
407    events: Receiver<LifecycleEvent>,
408    diagnostics: &DiagnosticObserver,
409) -> io::Result<Outcome> {
410    let (terminal, mut restore) = terminal;
411    let period = Duration::from_millis(100);
412    let mut timer = tokio::time::interval_at(tokio::time::Instant::now() + period, period);
413    timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
414    let redraw = stream::unfold(timer, |mut timer| async {
415        timer.tick().await;
416        Some(((), timer))
417    });
418    let mut outcome = drive_with_redraw(
419        operation,
420        cancel,
421        controls,
422        Some(events),
423        |phase| {
424            if phase == Phase::Restore {
425                restore();
426                return Ok(());
427            }
428            view.update(phase);
429            terminal
430                .draw(|frame| view.render(frame, &diagnostics.snapshot()))
431                .map(|_| ())
432                .map_err(|error| io::Error::other(error.to_string()))
433        },
434        redraw,
435    )
436    .await?;
437    outcome.diagnostics = Some(diagnostics.snapshot());
438    Ok(outcome)
439}
440
441struct LifecycleView {
442    title: String,
443    phase: Phase,
444    history: VecDeque<LifecycleEvent>,
445    omitted_events: usize,
446    scroll: u16,
447    color: bool,
448    started: std::time::Instant,
449    frames: usize,
450}
451
452impl LifecycleView {
453    fn new(command: &str, target: &str, color: bool) -> Self {
454        let label = |text: &str| {
455            strip_control(&text.chars().take(1024).collect::<String>())
456                .chars()
457                .take(256)
458                .collect::<String>()
459        };
460        Self {
461            title: format!("{} / {}", label(command), label(target)),
462            phase: Phase::Running,
463            history: VecDeque::new(),
464            omitted_events: 0,
465            scroll: 0,
466            color,
467            started: std::time::Instant::now(),
468            frames: 0,
469        }
470    }
471
472    fn update(&mut self, phase: Phase) {
473        match phase {
474            Phase::Event(event) => {
475                if self.history.len() == EVENT_CAPACITY {
476                    self.history.pop_front();
477                    self.omitted_events = self.omitted_events.saturating_add(1);
478                }
479                self.history.push_back(event);
480            }
481            Phase::Redraw(scroll) => self.scroll = self.scroll.saturating_add_signed(scroll),
482            phase => self.phase = phase,
483        }
484    }
485
486    fn render(&mut self, frame: &mut Frame<'_>, diagnostics: &DiagnosticSnapshot) {
487        let [header, status, current, body, footer] = Layout::vertical([
488            Constraint::Length(1),
489            Constraint::Length(2),
490            Constraint::Length(2),
491            Constraint::Min(0),
492            Constraint::Length(2),
493        ])
494        .areas(frame.area());
495        let theme = Theme::new(self.color);
496        let (badge, status_text, status_style) = match self.phase {
497            Phase::Cancelling => (
498                "cancelling",
499                "Cancellation requested. Waiting for runtime cleanup; effects not yet known.",
500                theme.warning(),
501            ),
502            Phase::Completed => (
503                "completed",
504                "Runtime returned success. Final evidence follows.",
505                theme.success(),
506            ),
507            Phase::Failed => (
508                "failed",
509                "Runtime returned an error. Cleanup success is not confirmed.",
510                theme.error(),
511            ),
512            _ => (
513                "running",
514                "Running. Observed stages are best-effort and may be incomplete.",
515                theme.accent(),
516            ),
517        };
518        self.frames = self.frames.wrapping_add(1);
519        let elapsed = self.started.elapsed().as_secs();
520        theme.header(
521            frame,
522            header,
523            &format!("rigspark / {}", self.title),
524            &format!("{badge} · {}:{:02}", elapsed / 60, elapsed % 60),
525        );
526        frame.render_widget(
527            Paragraph::new(Line::styled(format!(" {status_text}"), status_style))
528                .wrap(Wrap { trim: false }),
529            status,
530        );
531        let latest = self.history.back().copied();
532        frame.render_widget(
533            Paragraph::new(Line::from(vec![
534                Span::styled(" Latest ", theme.muted()),
535                match latest {
536                    Some(event) => Span::styled(event_text(event), event_style(event, theme)),
537                    None => Span::styled("No stage observed yet.", theme.muted()),
538                },
539            ]))
540            .wrap(Wrap { trim: false }),
541            current,
542        );
543        let inner = theme.panel(frame, body, "Stages");
544        let width = usize::from(inner.width.max(1)).saturating_sub(2).max(1);
545        let mut stages: Vec<LifecycleEvent> = Vec::new();
546        for event in &self.history {
547            match stages
548                .iter_mut()
549                .find(|seen| seen.scope == event.scope && seen.stage == event.stage)
550            {
551                Some(seen) => seen.status = event.status,
552                None => stages.push(*event),
553            }
554        }
555        const SPINNER: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
556        let running = matches!(self.phase, Phase::Running);
557        let mut lines: Vec<Line<'static>> = Vec::new();
558        for event in &stages {
559            let symbol = match event.status {
560                LifecycleStatus::Completed => "✓",
561                LifecycleStatus::Failed => "✗",
562                LifecycleStatus::Started if running => SPINNER[self.frames % SPINNER.len()],
563                // Started but never confirmed once the runtime stopped reporting.
564                LifecycleStatus::Started => "○",
565            };
566            let style = event_style(*event, theme);
567            for (index, piece) in tui_theme::wrap(&event_text(*event), width)
568                .into_iter()
569                .enumerate()
570            {
571                let lead = if index == 0 { symbol } else { " " };
572                lines.push(Line::from(vec![
573                    Span::styled(format!("{lead} "), style),
574                    Span::styled(piece, style),
575                ]));
576            }
577        }
578        if stages.is_empty() {
579            lines.push(Line::styled(
580                "Waiting for the first stage report…",
581                theme.muted(),
582            ));
583        }
584        let mut text = diagnostics.text();
585        if self.omitted_events > 0 {
586            text.push_str(&format!(
587                "{} earlier stage observations omitted\n",
588                self.omitted_events
589            ));
590        }
591        if !text.trim().is_empty() {
592            lines.push(Line::default());
593            for line in text.lines() {
594                lines.extend(
595                    tui_theme::wrap(line, width + 2)
596                        .into_iter()
597                        .map(|piece| Line::styled(piece, theme.muted())),
598                );
599            }
600        }
601        let maximum = lines.len().saturating_sub(usize::from(inner.height));
602        self.scroll = self.scroll.min(u16::try_from(maximum).unwrap_or(u16::MAX));
603        let total = lines.len();
604        frame.render_widget(Paragraph::new(lines).scroll((self.scroll, 0)), inner);
605        tui_theme::scrollbar(
606            frame,
607            body,
608            total,
609            usize::from(self.scroll),
610            usize::from(inner.height),
611            theme,
612        );
613        frame.render_widget(
614            Paragraph::new(vec![
615                Line::styled(
616                    " Final result is authoritative, not stage completion.",
617                    theme.muted(),
618                ),
619                theme.hints(
620                    &[("Ctrl-C / Esc / q", "cancel"), ("Up/Down/wheel", "scroll")],
621                    footer.width,
622                ),
623            ]),
624            footer,
625        );
626    }
627}
628
629fn event_style(event: LifecycleEvent, theme: Theme) -> Style {
630    match event.status {
631        LifecycleStatus::Started => Style::default(),
632        LifecycleStatus::Completed => theme.success(),
633        LifecycleStatus::Failed => theme.error(),
634    }
635}
636
637fn event_text(event: LifecycleEvent) -> String {
638    let scope = match event.scope {
639        LifecycleScope::Runtime => "Runtime",
640        LifecycleScope::AcquisitionDaemon => "Acquisition daemon",
641    };
642    let stage = match event.stage {
643        LifecycleStage::Acquisition => "Acquisition",
644        LifecycleStage::Verification => "Verification",
645        LifecycleStage::AcquisitionVerification => "Acquisition and verification",
646        LifecycleStage::Start => "Start/serve (may reuse a server)",
647        LifecycleStage::Attach => "Attach",
648        LifecycleStage::Activation => "Activation",
649        LifecycleStage::Readiness => "Readiness",
650        LifecycleStage::Stop => "Stop",
651    };
652    let status = match event.status {
653        LifecycleStatus::Started => "started",
654        LifecycleStatus::Completed => "completed",
655        LifecycleStatus::Failed => "failed",
656    };
657    format!("{scope} / {stage}: {status}")
658}
659
660fn write_phase(
661    output: &mut impl Write,
662    command: &str,
663    target: &str,
664    phase: Phase,
665) -> io::Result<()> {
666    match phase {
667        Phase::Running => writeln!(
668            output,
669            "{} / {}\nRunning. Observed stages are best-effort and may be incomplete.\nStage completion does not confirm command success or saved state; the final result is authoritative.\nCtrl-C cancels and waits for runtime cleanup.",
670            strip_control(command),
671            strip_control(target)
672        ),
673        Phase::Event(event) => writeln!(output, "{}", event_text(event)),
674        Phase::Redraw(_) => return Ok(()),
675        Phase::Cancelling => writeln!(
676            output,
677            "Cancellation requested. Waiting for the runtime to return; effects are not yet known."
678        ),
679        Phase::Restore => writeln!(
680            output,
681            "Terminal presentation ended; waiting for the runtime result."
682        ),
683        Phase::Completed => writeln!(output, "Runtime returned success. Final evidence follows."),
684        Phase::Failed => writeln!(output, "Runtime returned an error. Final evidence follows."),
685    }?;
686    output.flush()
687}
688
689pub fn evidence(outcome: &Outcome) -> String {
690    let mut text = match &outcome.result {
691        Ok((report, text)) => {
692            let mut lines: Vec<_> = text.lines().map(strip_control).collect();
693            for (field, label) in [
694                ("type", "Result"),
695                ("backend", "Backend"),
696                ("endpoint", "Endpoint"),
697                ("ownership", "Ownership"),
698                ("integrity", "Integrity"),
699            ] {
700                if let Some(value) = report.get(field).and_then(Value::as_str) {
701                    lines.push(format!("{label}: {}", strip_control(value)));
702                }
703            }
704            format!("{}\n", lines.join("\n"))
705        }
706        Err(error) => format!(
707            "Runtime error: {}\nEffects are unknown; rollback or cleanup success is not confirmed.\nInspect `llmup-native ls` and `llmup-native doctor` before deciding whether to retry.\nNo automatic retry or bypass was performed.\n",
708            strip_control(&error.to_string()),
709        ),
710    };
711    if [129, 130, 143].contains(&outcome.exit_code) {
712        text.push_str("\nCancellation was requested; the runtime result above is authoritative.\n");
713    }
714    if let Some(error) = &outcome.presentation_error {
715        text.push_str(&format!("\nPresentation error: {}\n", strip_control(error)));
716    }
717    if let Some(diagnostics) = &outcome.diagnostics {
718        let diagnostics = diagnostics.text();
719        if !diagnostics.is_empty() {
720            text.push_str("\nRuntime diagnostics:\n");
721            text.push_str(&diagnostics);
722        }
723    }
724    text
725}
726
727#[cfg(test)]
728mod tests {
729    use super::*;
730    use futures_util::stream;
731    use serde_json::json;
732    use std::cell::Cell;
733
734    #[tokio::test]
735    async fn visual_input_owner_drops_without_another_poll_and_eof_is_not_silenced() {
736        use std::sync::{
737            Arc, Mutex,
738            atomic::{AtomicBool, Ordering},
739        };
740        struct Input(Arc<AtomicBool>);
741        impl Stream for Input {
742            type Item = io::Result<Event>;
743            fn poll_next(
744                self: std::pin::Pin<&mut Self>,
745                _context: &mut std::task::Context<'_>,
746            ) -> std::task::Poll<Option<Self::Item>> {
747                std::task::Poll::Pending
748            }
749        }
750        impl Drop for Input {
751            fn drop(&mut self) {
752                self.0.store(true, Ordering::SeqCst);
753            }
754        }
755        let dropped = Arc::new(AtomicBool::new(false));
756        let owner = Arc::new(Mutex::new(Some(Input(dropped.clone()))));
757        let mut input = visual_input(owner.clone());
758        assert!(futures_util::poll!(input.next()).is_pending());
759        owner.lock().unwrap().take();
760        assert!(dropped.load(Ordering::SeqCst));
761        assert!(input.next().await.is_none());
762
763        let owner = Arc::new(Mutex::new(Some(stream::empty::<io::Result<Event>>())));
764        let mut input = visual_input(owner.clone());
765        assert!(matches!(input.next().await, Some(Err(_))));
766        assert!(owner.lock().unwrap().is_none());
767        assert!(input.next().await.is_none());
768    }
769
770    #[test]
771    fn resize_thresholds_preserve_visual_and_accessible_boundaries() {
772        for (accessible, width, height) in [(false, 60, 16), (true, 40, 10)] {
773            assert!(matches!(
774                resize_control(width, height, accessible),
775                Control::Resize { eligible: true }
776            ));
777            assert!(matches!(
778                resize_control(width - 1, height, accessible),
779                Control::Resize { eligible: false }
780            ));
781            assert!(matches!(
782                resize_control(width, height - 1, accessible),
783                Control::Resize { eligible: false }
784            ));
785        }
786    }
787
788    #[tokio::test]
789    async fn cleanup_deadline_restores_presentation_without_dropping_runtime() {
790        let cancel = CancellationToken::new();
791        let restored = Cell::new(false);
792        let finished = Cell::new(false);
793        let (release, released) = tokio::sync::oneshot::channel();
794        let mut release = Some(release);
795        let outcome = tokio::time::timeout(
796            Duration::from_secs(2),
797            drive_with_deadline(
798                async {
799                    cancel.cancelled().await;
800                    released.await.unwrap();
801                    assert!(restored.get());
802                    finished.set(true);
803                    Err(BackendError(
804                        "cleanup failed after terminal restoration".into(),
805                    ))
806                },
807                &cancel,
808                stream::iter([Ok(Control::Cancel(129))]),
809                None,
810                |phase| {
811                    if phase == Phase::Restore {
812                        assert!(!restored.replace(true));
813                        assert!(!finished.get());
814                        release.take().unwrap().send(()).unwrap();
815                    } else {
816                        assert!(!restored.get(), "redraw after restoration");
817                    }
818                    Ok(())
819                },
820                stream::pending(),
821                Duration::from_millis(5),
822            ),
823        )
824        .await
825        .unwrap()
826        .unwrap();
827        assert!(finished.get());
828        assert_eq!(outcome.exit_code, 129);
829        assert!(evidence(&outcome).contains("cleanup failed after terminal restoration"));
830    }
831
832    #[tokio::test]
833    async fn resize_fallback_is_debounced_and_does_not_cancel_runtime() {
834        let cancel = CancellationToken::new();
835        let (release, released) = tokio::sync::oneshot::channel();
836        let mut release = Some(release);
837        let controls = stream::iter([
838            Ok(resize_control(70, 20, false)),
839            Ok(resize_control(59, 20, false)),
840        ])
841        .chain(stream::pending());
842        let started = tokio::time::Instant::now();
843        let outcome = tokio::time::timeout(
844            Duration::from_secs(2),
845            drive(
846                async {
847                    released.await.unwrap();
848                    Ok((json!({}), "completed".into()))
849                },
850                &cancel,
851                controls,
852                None,
853                |phase| {
854                    if phase == Phase::Restore {
855                        assert!(!cancel.is_cancelled());
856                        assert!(started.elapsed() >= Duration::from_millis(50));
857                        release.take().unwrap().send(()).unwrap();
858                    }
859                    Ok(())
860                },
861            ),
862        )
863        .await
864        .unwrap()
865        .unwrap();
866        assert_eq!(outcome.exit_code, 0);
867        assert!(!cancel.is_cancelled());
868        assert!(outcome.presentation_restored);
869    }
870
871    #[tokio::test]
872    async fn recovered_size_cancels_pending_fallback_for_both_modes() {
873        for accessible in [false, true] {
874            let cancel = CancellationToken::new();
875            let controls = stream::iter([
876                Ok(resize_control(1, 1, accessible)),
877                Ok(resize_control(80, 24, accessible)),
878            ])
879            .chain(stream::pending());
880            let outcome = drive(
881                async {
882                    tokio::time::sleep(Duration::from_millis(65)).await;
883                    Ok((json!({}), String::new()))
884                },
885                &cancel,
886                controls,
887                None,
888                |phase| {
889                    assert_ne!(phase, Phase::Restore);
890                    Ok(())
891                },
892            )
893            .await
894            .unwrap();
895            assert!(!outcome.presentation_restored);
896            assert!(!cancel.is_cancelled());
897            assert_eq!(outcome.exit_code, 0);
898        }
899    }
900
901    #[tokio::test]
902    async fn signal_after_resize_restoration_still_cancels_without_second_restore() {
903        let cancel = CancellationToken::new();
904        let (notify, restored) = tokio::sync::oneshot::channel();
905        let mut notify = Some(notify);
906        let mut restoration_count = 0;
907        let controls =
908            stream::once(async { Ok(resize_control(1, 1, false)) }).chain(stream::once(async {
909                restored.await.unwrap();
910                Ok(Control::Cancel(143))
911            }));
912        let outcome = tokio::time::timeout(
913            Duration::from_secs(2),
914            drive(
915                async {
916                    cancel.cancelled().await;
917                    Err(BackendError("runtime cancelled after fallback".into()))
918                },
919                &cancel,
920                controls,
921                None,
922                |phase| {
923                    if phase == Phase::Restore {
924                        restoration_count += 1;
925                        notify.take().unwrap().send(()).unwrap();
926                    } else {
927                        assert_eq!(restoration_count, 0, "presentation after fallback");
928                    }
929                    Ok(())
930                },
931            ),
932        )
933        .await
934        .unwrap()
935        .unwrap();
936        assert_eq!(restoration_count, 1);
937        assert_eq!(outcome.exit_code, 143);
938        assert!(outcome.presentation_restored);
939        assert!(evidence(&outcome).contains("runtime cancelled after fallback"));
940    }
941
942    #[tokio::test]
943    async fn resize_during_cancellation_cannot_skip_cleanup_deadline() {
944        let cancel = CancellationToken::new();
945        let (release, released) = tokio::sync::oneshot::channel();
946        let mut release = Some(release);
947        let started = tokio::time::Instant::now();
948        let outcome = drive_with_deadline(
949            async {
950                released.await.unwrap();
951                Err(BackendError("cleanup result".into()))
952            },
953            &cancel,
954            stream::iter([Ok(Control::Cancel(143)), Ok(resize_control(1, 1, false))]),
955            None,
956            |phase| {
957                if phase == Phase::Restore {
958                    assert!(started.elapsed() >= Duration::from_millis(80));
959                    release.take().unwrap().send(()).unwrap();
960                }
961                Ok(())
962            },
963            stream::pending(),
964            Duration::from_millis(80),
965        )
966        .await
967        .unwrap();
968        assert_eq!(outcome.exit_code, 143);
969        assert!(outcome.presentation_restored);
970    }
971
972    #[tokio::test]
973    async fn visual_resize_releases_owner_and_retains_runtime_evidence() {
974        let cancel = CancellationToken::new();
975        let restored = Cell::new(false);
976        let (release, released) = tokio::sync::oneshot::channel();
977        let mut release = Some(release);
978        let (_sender, events) = tokio::sync::mpsc::channel(1);
979        let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
980        let mut view = LifecycleView::new("up", "fixture", false);
981        let outcome = drive_visual(
982            (&mut terminal, || {
983                assert!(!restored.replace(true));
984                release.take().unwrap().send(()).unwrap();
985            }),
986            &mut view,
987            async {
988                released.await.unwrap();
989                assert!(!cancel.is_cancelled());
990                Ok((json!({"model":"fixture"}), "runtime complete".into()))
991            },
992            &cancel,
993            stream::once(async { Ok(resize_control(1, 1, false)) }).chain(stream::pending()),
994            events,
995            &DiagnosticObserver::default(),
996        )
997        .await
998        .unwrap();
999        assert!(restored.get());
1000        assert!(outcome.presentation_restored);
1001        assert_eq!(outcome.exit_code, 0);
1002        assert!(evidence(&outcome).contains("runtime complete"));
1003    }
1004
1005    #[tokio::test]
1006    async fn fallback_notification_failure_keeps_runtime_alive_and_is_not_retried() {
1007        let cancel = CancellationToken::new();
1008        let (release, released) = tokio::sync::oneshot::channel();
1009        let mut release = Some(release);
1010        let outcome = drive(
1011            async {
1012                released.await.unwrap();
1013                Ok((json!({}), "complete".into()))
1014            },
1015            &cancel,
1016            stream::once(async { Ok(resize_control(1, 1, false)) }).chain(stream::pending()),
1017            None,
1018            |phase| {
1019                if phase == Phase::Restore {
1020                    release.take().unwrap().send(()).unwrap();
1021                    return Err(io::Error::other("fallback output failed"));
1022                }
1023                Ok(())
1024            },
1025        )
1026        .await
1027        .unwrap();
1028        assert!(!cancel.is_cancelled());
1029        assert!(outcome.result.is_ok());
1030        assert!(outcome.presentation_restored);
1031        assert_eq!(
1032            outcome.presentation_error.as_deref(),
1033            Some("fallback output failed")
1034        );
1035    }
1036
1037    #[test]
1038    fn visual_render_uses_only_observed_stages_and_bounds_history() {
1039        let mut view = LifecycleView::new("up\u{1b}[31m", "model\nname", false);
1040        let diagnostics = rigspark_runtime::application::events::DiagnosticObserver::default();
1041        for _ in 0..100 {
1042            view.update(Phase::Event(LifecycleEvent {
1043                scope: LifecycleScope::AcquisitionDaemon,
1044                stage: LifecycleStage::Stop,
1045                status: LifecycleStatus::Failed,
1046            }));
1047        }
1048        view.update(Phase::Cancelling);
1049        assert_eq!(view.history.len(), 64);
1050        assert_eq!(view.omitted_events, 36);
1051        for (width, height) in [(100, 28), (24, 8), (1, 1)] {
1052            let mut terminal =
1053                ratatui::Terminal::new(ratatui::backend::TestBackend::new(width, height)).unwrap();
1054            terminal
1055                .draw(|frame| view.render(frame, &diagnostics.snapshot()))
1056                .unwrap();
1057            let text: String = terminal
1058                .backend()
1059                .buffer()
1060                .content
1061                .iter()
1062                .map(|cell| cell.symbol())
1063                .collect();
1064            assert!(!text.contains("Readiness"));
1065            assert!(!text.contains("100%"));
1066            assert!(!text.contains("cleanup complete"));
1067            assert!(!text.contains('\u{1b}'));
1068            if width == 100 {
1069                assert!(text.contains("Cancellation requested"));
1070                assert!(text.contains("Acquisition daemon / Stop: failed"));
1071                assert!(text.contains("36 earlier stage observations omitted"));
1072            }
1073        }
1074    }
1075
1076    #[tokio::test]
1077    async fn visual_redraw_and_scroll_do_not_cancel_and_cleanup_is_awaited() {
1078        let cancel = CancellationToken::new();
1079        let cleaned = Cell::new(false);
1080        let mut phases = Vec::new();
1081        let outcome = drive(
1082            async {
1083                cancel.cancelled().await;
1084                tokio::task::yield_now().await;
1085                cleaned.set(true);
1086                Err(BackendError("cleanup failed".into()))
1087            },
1088            &cancel,
1089            stream::iter([Ok(Control::Redraw(1)), Ok(Control::Cancel(130))]),
1090            None,
1091            |phase| {
1092                if matches!(phase, Phase::Redraw(_)) {
1093                    assert!(!cancel.is_cancelled());
1094                }
1095                phases.push(phase);
1096                Ok(())
1097            },
1098        )
1099        .await
1100        .unwrap();
1101        assert!(cleaned.get());
1102        assert_eq!(outcome.exit_code, 130);
1103        assert_eq!(
1104            phases,
1105            [
1106                Phase::Running,
1107                Phase::Redraw(1),
1108                Phase::Cancelling,
1109                Phase::Failed
1110            ]
1111        );
1112        assert!(evidence(&outcome).contains("cleanup failed"));
1113    }
1114
1115    #[tokio::test]
1116    async fn hangup_cancels_once_waits_for_cleanup_and_normal_completion_does_not_abort() {
1117        let cancel = CancellationToken::new();
1118        let cleaned = Cell::new(false);
1119        let mut cancellations = 0;
1120        let outcome = drive(
1121            async {
1122                cancel.cancelled().await;
1123                tokio::task::yield_now().await;
1124                cleaned.set(true);
1125                Err(BackendError("cancelled after cleanup".into()))
1126            },
1127            &cancel,
1128            stream::iter([Ok(Control::Cancel(129)), Ok(Control::Cancel(143))]),
1129            None,
1130            |phase| {
1131                if phase == Phase::Cancelling {
1132                    cancellations += 1;
1133                }
1134                if phase == Phase::Failed {
1135                    assert!(cleaned.get());
1136                }
1137                Ok(())
1138            },
1139        )
1140        .await
1141        .unwrap();
1142        assert!(cleaned.get());
1143        assert_eq!(cancellations, 1);
1144        assert_eq!(outcome.exit_code, 129);
1145        assert!(evidence(&outcome).contains("Cancellation was requested"));
1146
1147        let cancel = CancellationToken::new();
1148        let outcome = drive(
1149            async { Ok((serde_json::json!({}), String::new())) },
1150            &cancel,
1151            stream::pending(),
1152            None,
1153            |_| Ok(()),
1154        )
1155        .await
1156        .unwrap();
1157        assert_eq!(outcome.exit_code, 0);
1158        assert!(!cancel.is_cancelled());
1159    }
1160
1161    #[tokio::test]
1162    async fn visual_cleanup_redraw_failure_preserves_signal_and_awaits_result() {
1163        let cancel = CancellationToken::new();
1164        let cleaned = Cell::new(false);
1165        let (acknowledge, acknowledged) = tokio::sync::oneshot::channel();
1166        let mut acknowledge = Some(acknowledge);
1167        let outcome = drive_with_redraw(
1168            async {
1169                acknowledged.await.unwrap();
1170                assert!(cancel.is_cancelled());
1171                cleaned.set(true);
1172                Err(BackendError("cleanup failed".into()))
1173            },
1174            &cancel,
1175            stream::iter([Ok(Control::Cancel(143))]),
1176            None,
1177            |phase| {
1178                if phase == Phase::Redraw(0) {
1179                    acknowledge.take().unwrap().send(()).unwrap();
1180                    Err(io::Error::other("redraw failed"))
1181                } else {
1182                    Ok(())
1183                }
1184            },
1185            stream::once(async { cancel.cancelled().await }),
1186        )
1187        .await
1188        .unwrap();
1189        assert!(cleaned.get());
1190        assert_eq!(outcome.exit_code, 143);
1191        assert!(evidence(&outcome).contains("cleanup failed"));
1192        assert!(evidence(&outcome).contains("redraw failed"));
1193    }
1194
1195    #[test]
1196    fn visual_keys_cancel_without_treating_release_resize_or_paste_as_interrupts() {
1197        use crossterm::event::{KeyEvent, KeyEventKind};
1198        for key in [
1199            KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
1200            KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE),
1201            KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE),
1202        ] {
1203            assert!(matches!(
1204                visual_control(Event::Key(key)),
1205                Control::Cancel(130)
1206            ));
1207            assert!(matches!(
1208                visual_control(Event::Key(KeyEvent {
1209                    kind: KeyEventKind::Release,
1210                    ..key
1211                })),
1212                Control::Redraw(0)
1213            ));
1214        }
1215        assert!(matches!(
1216            visual_control(Event::Resize(10, 4)),
1217            Control::Resize { eligible: false }
1218        ));
1219        assert!(matches!(
1220            visual_control(Event::Paste("q\u{3}".into())),
1221            Control::Redraw(0)
1222        ));
1223    }
1224
1225    #[tokio::test]
1226    async fn visual_driver_drains_real_events_and_retains_diagnostic_snapshot() {
1227        let (sender, receiver) = tokio::sync::mpsc::channel(64);
1228        let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(100, 28)).unwrap();
1229        let mut view = LifecycleView::new("down", "model", false);
1230        let diagnostics = DiagnosticObserver::default();
1231        let outcome = drive_visual(
1232            (&mut terminal, || {}),
1233            &mut view,
1234            async {
1235                sender
1236                    .try_send(LifecycleEvent {
1237                        scope: LifecycleScope::Runtime,
1238                        stage: LifecycleStage::Stop,
1239                        status: LifecycleStatus::Failed,
1240                    })
1241                    .unwrap();
1242                Err(BackendError("stop failed".into()))
1243            },
1244            &CancellationToken::new(),
1245            stream::pending(),
1246            receiver,
1247            &diagnostics,
1248        )
1249        .await
1250        .unwrap();
1251        assert_eq!(outcome.exit_code, 1);
1252        assert!(outcome.diagnostics.is_some());
1253        let text: String = terminal
1254            .backend()
1255            .buffer()
1256            .content
1257            .iter()
1258            .map(|cell| cell.symbol())
1259            .collect();
1260        assert!(text.contains("Runtime / Stop: failed"));
1261        assert!(text.contains("Cleanup success is not confirmed"));
1262        assert!(evidence(&outcome).contains("stop failed"));
1263    }
1264
1265    #[tokio::test]
1266    async fn completion_drains_events_in_order_without_waiting_for_held_sender() {
1267        let (sender, receiver) = tokio::sync::mpsc::channel(64);
1268        let started = LifecycleEvent {
1269            scope: LifecycleScope::Runtime,
1270            stage: LifecycleStage::Readiness,
1271            status: LifecycleStatus::Started,
1272        };
1273        let completed = LifecycleEvent {
1274            status: LifecycleStatus::Completed,
1275            ..started
1276        };
1277        sender.try_send(started).unwrap();
1278        let mut phases = Vec::new();
1279        let cancel = CancellationToken::new();
1280        let outcome = tokio::time::timeout(
1281            std::time::Duration::from_secs(1),
1282            drive(
1283                async {
1284                    sender.try_send(completed).unwrap();
1285                    Ok((json!({"type":"ready"}), "Ready.\n".into()))
1286                },
1287                &cancel,
1288                stream::pending(),
1289                Some(receiver),
1290                |phase| {
1291                    phases.push(phase);
1292                    Ok(())
1293                },
1294            ),
1295        )
1296        .await
1297        .expect("must not wait for the sender to drop")
1298        .unwrap();
1299        assert_eq!(outcome.exit_code, 0);
1300        assert_eq!(
1301            phases,
1302            [
1303                Phase::Running,
1304                Phase::Event(started),
1305                Phase::Event(completed),
1306                Phase::Completed,
1307            ]
1308        );
1309    }
1310
1311    #[test]
1312    fn labels_preserve_scope_stage_and_status_without_claiming_command_success() {
1313        for (scope, scope_label) in [
1314            (LifecycleScope::Runtime, "Runtime"),
1315            (LifecycleScope::AcquisitionDaemon, "Acquisition daemon"),
1316        ] {
1317            for (stage, stage_label) in [
1318                (LifecycleStage::Acquisition, "Acquisition"),
1319                (LifecycleStage::Verification, "Verification"),
1320                (
1321                    LifecycleStage::AcquisitionVerification,
1322                    "Acquisition and verification",
1323                ),
1324                (LifecycleStage::Start, "Start/serve (may reuse a server)"),
1325                (LifecycleStage::Attach, "Attach"),
1326                (LifecycleStage::Activation, "Activation"),
1327                (LifecycleStage::Readiness, "Readiness"),
1328                (LifecycleStage::Stop, "Stop"),
1329            ] {
1330                for (status, status_label) in [
1331                    (LifecycleStatus::Started, "started"),
1332                    (LifecycleStatus::Completed, "completed"),
1333                    (LifecycleStatus::Failed, "failed"),
1334                ] {
1335                    let mut output = Vec::new();
1336                    write_phase(
1337                        &mut output,
1338                        "up",
1339                        "model",
1340                        Phase::Event(LifecycleEvent {
1341                            scope,
1342                            stage,
1343                            status,
1344                        }),
1345                    )
1346                    .unwrap();
1347                    assert_eq!(
1348                        String::from_utf8(output).unwrap(),
1349                        format!("{scope_label} / {stage_label}: {status_label}\n")
1350                    );
1351                }
1352            }
1353        }
1354        let mut output = Vec::new();
1355        write_phase(&mut output, "up\u{1b}[31m", "model\nname", Phase::Running).unwrap();
1356        let text = String::from_utf8(output).unwrap();
1357        assert!(!text.contains('\u{1b}'));
1358        assert!(text.contains("best-effort and may be incomplete"));
1359        assert!(text.contains("does not confirm command success or saved state"));
1360    }
1361
1362    #[tokio::test]
1363    async fn events_are_presented_live_and_failure_is_drained_before_recovery() {
1364        let cancel = CancellationToken::new();
1365        let (sender, receiver) = tokio::sync::mpsc::channel(64);
1366        let (acknowledge, acknowledged) = tokio::sync::oneshot::channel();
1367        let mut acknowledge = Some(acknowledge);
1368        let started = LifecycleEvent {
1369            scope: LifecycleScope::Runtime,
1370            stage: LifecycleStage::Verification,
1371            status: LifecycleStatus::Started,
1372        };
1373        let mut output = Vec::new();
1374        let outcome = tokio::time::timeout(
1375            std::time::Duration::from_secs(1),
1376            drive(
1377                async {
1378                    sender.try_send(started).unwrap();
1379                    acknowledged.await.unwrap();
1380                    sender
1381                        .try_send(LifecycleEvent {
1382                            status: LifecycleStatus::Failed,
1383                            ..started
1384                        })
1385                        .unwrap();
1386                    Err(BackendError("verification failed".into()))
1387                },
1388                &cancel,
1389                stream::pending(),
1390                Some(receiver),
1391                |phase| {
1392                    write_phase(&mut output, "up", "model", phase)?;
1393                    if phase == Phase::Event(started) {
1394                        acknowledge.take().unwrap().send(()).unwrap();
1395                    }
1396                    Ok(())
1397                },
1398            ),
1399        )
1400        .await
1401        .expect("started must be presented before the operation completes")
1402        .unwrap();
1403        let text = String::from_utf8(output).unwrap();
1404        assert!(text.ends_with("Runtime / Verification: started\nRuntime / Verification: failed\nRuntime returned an error. Final evidence follows.\n"));
1405        assert_eq!(outcome.exit_code, 1);
1406        assert!(evidence(&outcome).contains("Effects are unknown"));
1407        assert!(!cancel.is_cancelled());
1408    }
1409
1410    #[tokio::test]
1411    async fn signal_has_priority_and_cleanup_events_remain_live_until_completion() {
1412        for code in [130, 143] {
1413            let cancel = CancellationToken::new();
1414            let (sender, receiver) = tokio::sync::mpsc::channel(64);
1415            let (acknowledge, acknowledged) = tokio::sync::oneshot::channel();
1416            let mut acknowledge = Some(acknowledge);
1417            let event = LifecycleEvent {
1418                scope: LifecycleScope::AcquisitionDaemon,
1419                stage: LifecycleStage::Stop,
1420                status: LifecycleStatus::Started,
1421            };
1422            let completed = LifecycleEvent {
1423                status: LifecycleStatus::Completed,
1424                ..event
1425            };
1426            sender.try_send(event).unwrap();
1427            let mut phases = Vec::new();
1428            let outcome = tokio::time::timeout(
1429                std::time::Duration::from_secs(1),
1430                drive(
1431                    async {
1432                        assert!(cancel.is_cancelled());
1433                        acknowledged.await.unwrap();
1434                        sender.try_send(completed).unwrap();
1435                        Ok((json!({"type":"stopped"}), "Stopped.\n".into()))
1436                    },
1437                    &cancel,
1438                    stream::iter([Ok(Control::Cancel(code))]),
1439                    Some(receiver),
1440                    |phase| {
1441                        phases.push(phase);
1442                        if phase == Phase::Event(event) {
1443                            acknowledge.take().unwrap().send(()).unwrap();
1444                        }
1445                        Ok(())
1446                    },
1447                ),
1448            )
1449            .await
1450            .expect("cleanup events must remain live after cancellation")
1451            .unwrap();
1452            assert_eq!(outcome.exit_code, code);
1453            assert!(outcome.result.is_ok());
1454            assert_eq!(
1455                phases,
1456                [
1457                    Phase::Running,
1458                    Phase::Cancelling,
1459                    Phase::Event(event),
1460                    Phase::Event(completed),
1461                    Phase::Completed
1462                ]
1463            );
1464        }
1465    }
1466
1467    #[tokio::test]
1468    async fn event_sink_failure_cancels_and_awaits_cleanup_without_masking_later_signal() {
1469        for signal in [false, true] {
1470            let cancel = CancellationToken::new();
1471            let cleaned = Cell::new(false);
1472            let writes = Cell::new(0);
1473            let (sender, receiver) = tokio::sync::mpsc::channel(64);
1474            let event = LifecycleEvent {
1475                scope: LifecycleScope::Runtime,
1476                stage: LifecycleStage::Readiness,
1477                status: LifecycleStatus::Started,
1478            };
1479            let outcome = tokio::time::timeout(
1480                std::time::Duration::from_secs(1),
1481                drive(
1482                    async {
1483                        sender.try_send(event).unwrap();
1484                        cancel.cancelled().await;
1485                        sender
1486                            .try_send(LifecycleEvent {
1487                                stage: LifecycleStage::Stop,
1488                                ..event
1489                            })
1490                            .unwrap();
1491                        tokio::task::yield_now().await;
1492                        cleaned.set(true);
1493                        Ok((json!({"type":"stopped"}), "Stopped.\n".into()))
1494                    },
1495                    &cancel,
1496                    stream::once(async {
1497                        if !signal {
1498                            std::future::pending::<()>().await;
1499                        }
1500                        cancel.cancelled().await;
1501                        Ok(Control::Cancel(143))
1502                    }),
1503                    Some(receiver),
1504                    |phase| {
1505                        writes.set(writes.get() + 1);
1506                        if matches!(phase, Phase::Event(_)) {
1507                            Err(io::Error::other("event sink failed"))
1508                        } else {
1509                            Ok(())
1510                        }
1511                    },
1512                ),
1513            )
1514            .await
1515            .expect("sink failure must cancel and await the operation")
1516            .unwrap();
1517            assert!(cleaned.get());
1518            assert!(cancel.is_cancelled());
1519            assert_eq!(writes.get(), 2);
1520            assert_eq!(outcome.exit_code, if signal { 143 } else { 1 });
1521            assert_eq!(outcome.result.as_ref().unwrap().0["type"], "stopped");
1522            assert!(evidence(&outcome).contains("Presentation error: event sink failed"));
1523        }
1524    }
1525
1526    #[test]
1527    fn closed_event_channel_does_not_spin_or_end_the_operation() {
1528        use std::task::{Context, Poll};
1529        let (observer, receiver) =
1530            rigspark_runtime::application::events::LifecycleObserver::channel();
1531        drop(observer);
1532        let cancel = CancellationToken::new();
1533        let polls = Cell::new(0);
1534        let complete = Cell::new(false);
1535        let operation = std::future::poll_fn(|_| {
1536            polls.set(polls.get() + 1);
1537            assert!(
1538                polls.get() <= 3,
1539                "closed event input must not repeatedly poll the operation"
1540            );
1541            if complete.get() {
1542                Poll::Ready(Ok((json!({}), String::new())))
1543            } else {
1544                Poll::Pending
1545            }
1546        });
1547        let mut controller = std::pin::pin!(drive(
1548            operation,
1549            &cancel,
1550            stream::pending(),
1551            Some(receiver),
1552            |_| Ok(())
1553        ));
1554        let mut context = Context::from_waker(futures_util::task::noop_waker_ref());
1555        assert!(controller.as_mut().poll(&mut context).is_pending());
1556        complete.set(true);
1557        let Poll::Ready(Ok(outcome)) = controller.as_mut().poll(&mut context) else {
1558            panic!("operation completion must end the controller");
1559        };
1560        assert_eq!(outcome.exit_code, 0);
1561    }
1562
1563    #[tokio::test]
1564    async fn cancellation_waits_for_runtime_and_preserves_signal_exit() {
1565        for code in [130, 143] {
1566            let cancel = CancellationToken::new();
1567            let cleaned = Cell::new(false);
1568            let mut phases = Vec::new();
1569            let result = drive(
1570                async {
1571                    cancel.cancelled().await;
1572                    cleaned.set(true);
1573                    Err(BackendError("runtime stopped after cleanup".into()))
1574                },
1575                &cancel,
1576                stream::iter([Ok(Control::Cancel(code))]),
1577                None,
1578                |phase| {
1579                    phases.push(phase);
1580                    Ok(())
1581                },
1582            )
1583            .await
1584            .unwrap();
1585            assert!(cleaned.get());
1586            assert_eq!(result.exit_code, code);
1587            assert!(result.result.is_err());
1588            assert_eq!(phases, [Phase::Running, Phase::Cancelling, Phase::Failed]);
1589        }
1590    }
1591
1592    #[tokio::test]
1593    async fn success_after_cancellation_is_not_reported_as_rollback() {
1594        let cancel = CancellationToken::new();
1595        let result = drive(
1596            async {
1597                cancel.cancelled().await;
1598                Ok((json!({"type":"switched"}), "Switched.\n".into()))
1599            },
1600            &cancel,
1601            stream::iter([Ok(Control::Cancel(130))]),
1602            None,
1603            |_| Ok(()),
1604        )
1605        .await
1606        .unwrap();
1607        assert_eq!(result.exit_code, 130);
1608        assert_eq!(result.result.unwrap().0["type"], "switched");
1609    }
1610
1611    #[tokio::test]
1612    async fn display_failure_before_execution_does_not_poll_runtime() {
1613        let polled = Cell::new(false);
1614        let result = drive(
1615            async {
1616                polled.set(true);
1617                Ok((json!({}), String::new()))
1618            },
1619            &CancellationToken::new(),
1620            stream::pending(),
1621            None,
1622            |_| Err(io::Error::other("display unavailable")),
1623        )
1624        .await;
1625        assert!(result.is_err());
1626        assert!(!polled.get());
1627    }
1628
1629    #[tokio::test]
1630    async fn input_failure_cancels_once_and_drains_runtime() {
1631        let cancel = CancellationToken::new();
1632        let calls = Cell::new(0);
1633        let result = drive(
1634            async {
1635                calls.set(calls.get() + 1);
1636                cancel.cancelled().await;
1637                Err(BackendError("cleanup result".into()))
1638            },
1639            &cancel,
1640            stream::iter([Err(io::Error::other("input failed"))]),
1641            None,
1642            |_| Ok(()),
1643        )
1644        .await
1645        .unwrap();
1646        assert_eq!(calls.get(), 1);
1647        assert_eq!(result.exit_code, 1);
1648        assert!(result.presentation_error.is_some());
1649    }
1650
1651    #[tokio::test]
1652    async fn final_display_failure_preserves_success_without_retry() {
1653        let calls = Cell::new(0);
1654        let cancel = CancellationToken::new();
1655        let result = drive(
1656            async {
1657                calls.set(calls.get() + 1);
1658                Ok((json!({"type":"stopped"}), "Stopped.\n".into()))
1659            },
1660            &cancel,
1661            stream::pending(),
1662            None,
1663            |phase| {
1664                if phase == Phase::Completed {
1665                    Err(io::Error::other("display failed"))
1666                } else {
1667                    Ok(())
1668                }
1669            },
1670        )
1671        .await
1672        .unwrap();
1673        assert_eq!(calls.get(), 1);
1674        assert_eq!(result.exit_code, 1);
1675        assert_eq!(result.result.as_ref().unwrap().0["type"], "stopped");
1676        assert!(evidence(&result).contains("Presentation error: display failed"));
1677        assert!(!cancel.is_cancelled());
1678    }
1679
1680    #[tokio::test]
1681    async fn unexpected_control_exit_cancels_and_waits_for_runtime_cleanup() {
1682        let cancel = CancellationToken::new();
1683        let cleaned = Cell::new(false);
1684        let result = tokio::time::timeout(
1685            Duration::from_secs(1),
1686            drive(
1687                async {
1688                    cancel.cancelled().await;
1689                    cleaned.set(true);
1690                    Err(BackendError("cleanup finished".into()))
1691                },
1692                &cancel,
1693                stream::empty(),
1694                None,
1695                |_| Ok(()),
1696            ),
1697        )
1698        .await
1699        .unwrap()
1700        .unwrap();
1701        assert!(cleaned.get());
1702        assert_eq!(result.exit_code, 1);
1703        assert_eq!(
1704            result.presentation_error.as_deref(),
1705            Some("lifecycle control input ended")
1706        );
1707        assert!(
1708            result
1709                .result
1710                .unwrap_err()
1711                .to_string()
1712                .contains("cleanup finished")
1713        );
1714    }
1715
1716    #[tokio::test]
1717    async fn cancellation_display_failure_does_not_mask_termination_or_cleanup() {
1718        let cancel = CancellationToken::new();
1719        let result = drive(
1720            async {
1721                cancel.cancelled().await;
1722                Err(BackendError("runtime cleanup failed".into()))
1723            },
1724            &cancel,
1725            stream::iter([Ok(Control::Cancel(143))]),
1726            None,
1727            |phase| {
1728                if phase == Phase::Cancelling {
1729                    Err(io::Error::other("display failed"))
1730                } else {
1731                    Ok(())
1732                }
1733            },
1734        )
1735        .await
1736        .unwrap();
1737        assert_eq!(result.exit_code, 143);
1738        assert!(evidence(&result).contains("runtime cleanup failed"));
1739        assert!(evidence(&result).contains("Effects are unknown"));
1740    }
1741
1742    #[tokio::test]
1743    async fn runtime_failure_is_not_retried_or_reclassified_as_clean_cancellation() {
1744        let mut phases = Vec::new();
1745        let result = drive(
1746            async { Err(BackendError("integrity mismatch\u{1b}[31m".into())) },
1747            &CancellationToken::new(),
1748            stream::pending(),
1749            None,
1750            |phase| {
1751                phases.push(phase);
1752                Ok(())
1753            },
1754        )
1755        .await
1756        .unwrap();
1757        assert_eq!(result.exit_code, 1);
1758        assert_eq!(phases, [Phase::Running, Phase::Failed]);
1759        let text = evidence(&result);
1760        assert!(text.contains("integrity mismatch\n"));
1761        assert!(text.contains("Effects are unknown"));
1762        assert!(text.contains("llmup-native doctor"));
1763        assert!(!text.contains('\u{1b}'));
1764        assert!(!text.contains("Cancellation was requested"));
1765    }
1766
1767    #[test]
1768    fn evidence_preserves_lines_and_only_displays_returned_fields() {
1769        let result = Outcome {
1770            result: Ok((
1771                json!({
1772                    "backend": "ollama", "ownership": "attached", "integrity": "size-only"
1773                }),
1774                "Model ready\nRuntime model: test\u{1b}[31m\n".into(),
1775            )),
1776            exit_code: 0,
1777            presentation_error: None,
1778            diagnostics: None,
1779            presentation_restored: false,
1780        };
1781        let text = evidence(&result);
1782        assert!(text.contains("Model ready\nRuntime model: test\n"));
1783        assert!(text.contains("Ownership: attached\nIntegrity: size-only\n"));
1784        assert!(!text.contains("verified"));
1785        assert!(!text.contains("Endpoint:"));
1786        assert!(!text.contains("%"));
1787        assert!(!text.contains('\u{1b}'));
1788    }
1789
1790    #[test]
1791    fn checklist_deduplicates_stages_and_never_confirms_unfinished_starts() {
1792        let started = LifecycleEvent {
1793            scope: LifecycleScope::Runtime,
1794            stage: LifecycleStage::Readiness,
1795            status: LifecycleStatus::Started,
1796        };
1797        let mut view = LifecycleView::new("up", "model", false);
1798        let diagnostics = DiagnosticObserver::default();
1799        let render = |view: &mut LifecycleView| {
1800            let mut terminal =
1801                ratatui::Terminal::new(ratatui::backend::TestBackend::new(90, 20)).unwrap();
1802            terminal
1803                .draw(|frame| view.render(frame, &diagnostics.snapshot()))
1804                .unwrap();
1805            terminal.backend().to_string()
1806        };
1807        view.update(Phase::Event(started));
1808        view.update(Phase::Event(LifecycleEvent {
1809            status: LifecycleStatus::Completed,
1810            ..started
1811        }));
1812        let done = render(&mut view);
1813        assert_eq!(done.matches("Runtime / Readiness").count(), 2, "{done}");
1814        assert!(done.contains("✓ Runtime / Readiness: completed"), "{done}");
1815        view.update(Phase::Event(LifecycleEvent {
1816            stage: LifecycleStage::Stop,
1817            ..started
1818        }));
1819        view.update(Phase::Failed);
1820        let failed = render(&mut view);
1821        assert!(failed.contains("○ Runtime / Stop: started"), "{failed}");
1822        assert!(failed.contains("failed · 0:00"), "{failed}");
1823    }
1824}