nmbrs-tui 0.7.0

Terminal UI for nmbrs: live metrics dashboard, progress, op throughput
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! `SinkSupervisor` — coordinates the active
//! [`crate::display_sink::DisplaySink`] and the
//! [`crate::key_watcher::KeyWatcher`] across a `tui=terminal`
//! run.
//!
//! ## Why
//!
//! `tui=terminal` is interactive: the operator can hit Ctrl-T
//! at any moment to swap from line-mode rendering
//! (`LogOnlySink`) up to the full TUI (`TuiSink`), and Ctrl-T
//! again (or `q`) inside the TUI to swap back. The supervisor
//! is the one component that watches for those signals and
//! drives the sink lifecycle.
//!
//! ## State machine
//!
//! ```text
//!   ┌─ Terminal ──── Ctrl-T ──→  TUI ───────┐
//!   │   • LogOnlySink                       │
//!   │   • KeyWatcher (raw stdin)            │
//!   │                                       │
//!   └────────── Ctrl-T or q ←───────────────┘
//!                              (App writes
//!                              `yielded_to_terminal`)
//! ```
//!
//! Stdin ownership is exclusive: only one of {KeyWatcher, App}
//! has raw-mode read access at a time. The transition function
//! always tears the outgoing side down before bringing the
//! incoming side up.
//!
//! ## Lifetime
//!
//! Owns its own OS thread (not async). The runner's async
//! future runs in parallel; when it completes, the runner
//! drops a sentinel into the supervisor's "run done" channel
//! and the supervisor tears the active sink + watcher down
//! before exiting.

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc;
use std::thread::JoinHandle;
use std::time::Duration;

use crate::display_sink::{DisplayInputs, DisplaySink, SinkHandle};
use crate::key_watcher::{KeyWatcher, WatcherSignal};
use crate::log_only_observer::LogOnlyObserver;
use crate::log_only_sink::{LogOnlySink, fresh_resume_cursor};
use crate::run_state_actor::RunStateHandle;
use crate::tui_sink::{TuiSink, TuiSinkSync};

/// Handle held by the runner-side caller. The supervisor runs
/// on its own OS thread; [`Self::shutdown`] is called once
/// the runner future completes.
pub struct SinkSupervisor {
    /// Sentinel channel: drop the sender (in `shutdown`) and
    /// the supervisor's `try_recv` returns `Disconnected`,
    /// breaking the loop.
    done_tx: Option<mpsc::Sender<()>>,
    join: Option<JoinHandle<()>>,
}

impl SinkSupervisor {
    /// Spawn the supervisor. Initial state is the terminal sink
    /// (`LogOnlySink` + `KeyWatcher`). If stdin isn't a TTY,
    /// the watcher refuses to spawn and the supervisor exits
    /// immediately — leaving the observer's synchronous
    /// stderr write path active (legacy `tui=off` behaviour).
    pub fn spawn(
        observer: Arc<LogOnlyObserver>,
        state: RunStateHandle,
        runtime: Option<tokio::runtime::Handle>,
    ) -> Self {
        // SRD-93 M7 — arm the console-loss demote before any sink
        // touches the terminal, so a SIGHUP at any point in the
        // supervised lifetime lands on the flag the loop polls.
        install_console_loss_hook();
        let (done_tx, done_rx) = mpsc::channel::<()>();
        let join = std::thread::Builder::new()
            .name("sink-supervisor".into())
            .spawn(move || run_supervision(observer, state, done_rx, runtime))
            .expect("spawn sink-supervisor thread");
        Self {
            done_tx: Some(done_tx),
            join: Some(join),
        }
    }

    /// Tell the supervisor the run has finished. Tears down
    /// the active sink + watcher and joins. Idempotent.
    pub fn shutdown(mut self) {
        drop(self.done_tx.take());
        if let Some(join) = self.join.take() {
            let _ = join.join();
        }
    }
}

/// What the supervisor is currently driving. Tear-down + bring-
/// up between modes is sequential — never side-by-side stdin.
enum ActiveSink {
    Terminal {
        sink_handle: Box<dyn SinkHandle>,
        /// `None` when stdin isn't a TTY — the LogOnlySink is
        /// still rendering to the stderr terminal, just without
        /// interactive key handling / prompt. Auto-degraded path
        /// for piped-stdin invocations.
        watcher: Option<KeyWatcher>,
        signal_rx: mpsc::Receiver<WatcherSignal>,
        /// Forwarder for prompt-bound signals. The supervisor's
        /// match arms route `Key` / `GrowPrompt` / `ShrinkPrompt`
        /// / `ToggleHelp` here; the `LogOnlySink` owns the
        /// receiver and drives its embedded `PromptState`.
        /// Unused in the degraded (no-watcher) mode — kept on
        /// the struct so the enum variant doesn't need
        /// per-mode shape, but no traffic ever flows.
        prompt_tx: mpsc::Sender<WatcherSignal>,
    },
    Tui {
        sink_handle: Box<dyn SinkHandle>,
        sync: TuiSinkSync,
    },
}

/// SRD-93 M7 — set by the console-loss hook (SIGHUP received by the
/// signal dispatcher: the controlling pty/ssh is GONE). The
/// supervisor polls it each tick and demotes to headless: the run
/// continues, only the display dies. Process-global and one-way,
/// like the console itself.
static CONSOLE_LOST: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Install the M7 console-loss hook. Idempotent (first install
/// wins in `session_signals`); called at supervisor spawn so every
/// `tui=terminal` run demotes cleanly instead of rendering to a
/// dead terminal. The hook runs on the signal-dispatch thread —
/// one relaxed store, nothing blocking.
pub fn install_console_loss_hook() {
    nmbrs_runtime::session_signals::set_console_loss_hook(Box::new(|| {
        CONSOLE_LOST.store(true, std::sync::atomic::Ordering::Relaxed);
    }));
}

fn console_lost() -> bool {
    CONSOLE_LOST.load(std::sync::atomic::Ordering::Relaxed)
}

fn run_supervision(
    observer: Arc<LogOnlyObserver>,
    state: RunStateHandle,
    done_rx: mpsc::Receiver<()>,
    runtime: Option<tokio::runtime::Handle>,
) {
    // Two distinct flags the supervisor manages:
    //   • sink_active     — observer's synchronous-stderr
    //                       suppression. Held high while *any*
    //                       sink is rendering (LogOnlySink
    //                       sets/clears it on its own; the
    //                       supervisor re-asserts during the
    //                       TUI swap).
    //   • inline_suppress — activity's inline-status thread
    //                       suppression. Held high *only*
    //                       while an alt-screen TUI owns the
    //                       terminal — i.e. during the TuiSink
    //                       window. Cleared in plain
    //                       `tui=terminal` so the per-cycle
    //                       status line keeps rendering
    //                       alongside the LogOnlySink's log
    //                       drain (they share stderr without
    //                       conflict).
    let sink_active_flag = observer.sink_active_flag();
    let inline_suppress = observer.inline_suppress_flag();
    // Cross-swap log cursor: each LogOnlySink writes its final
    // `last_seen` here on shutdown and the next one (after a TUI
    // swap) resumes from it, so the lines that scrolled under the
    // alternate screen are re-emitted into the restored scrollback.
    // Starts `RESUME_FRESH` so the very first sink seeds from the
    // live `log_seq_total` instead.
    let resume_from = fresh_resume_cursor();
    let mut active = match start_terminal(&observer, &state, runtime.clone(), &resume_from) {
        Some(a) => a,
        None => {
            // No TTY — KeyWatcher refused. Fall through:
            // the observer's synchronous stderr-write path is
            // already active (sink_active stays false), so the
            // operator gets the same output the legacy
            // `tui=off` mode delivered.
            wait_for_done(&done_rx);
            return;
        }
    };

    const TICK: Duration = Duration::from_millis(100);

    loop {
        // Runner finished?
        match done_rx.try_recv() {
            Ok(()) | Err(mpsc::TryRecvError::Disconnected) => break,
            Err(mpsc::TryRecvError::Empty) => {}
        }

        // SRD-93 M7 — console lost (SIGHUP): the pty/ssh is gone and
        // is never coming back. Demote to headless: permanently
        // suppress every terminal-bound write (logs continue through
        // the session.log sink), tear the active sink down
        // best-effort, restore the (dead) terminal's modes for
        // hygiene, and hold until the RUNNER — which is unaffected —
        // finishes. Before this seam existed, a pty-spawned run
        // survived SIGHUP per the M7 contract but kept rendering to
        // the dead terminal; the PTY test harnesses then deadlocked
        // behind a child that never exited (observed 2026-08-04:
        // 39-minute orphaned test runs at 100% CPU).
        if console_lost() {
            sink_active_flag.store(true, std::sync::atomic::Ordering::SeqCst);
            inline_suppress.store(true, std::sync::atomic::Ordering::SeqCst);
            let _ = crossterm::terminal::disable_raw_mode();
            match active {
                ActiveSink::Terminal { sink_handle, .. } => sink_handle.shutdown(),
                ActiveSink::Tui { sink_handle, .. } => sink_handle.shutdown(),
            }
            wait_for_done(&done_rx);
            return;
        }

        // Per-state handling. Each branch may swap `active`
        // and `continue`-loop, restarting the polling cycle.
        let mut swap_to: Option<Transition> = None;
        match &active {
            ActiveSink::Terminal {
                signal_rx,
                prompt_tx,
                ..
            } => {
                while let Ok(sig) = signal_rx.try_recv() {
                    match sig {
                        WatcherSignal::ToggleTui => {
                            swap_to = Some(Transition::TerminalToTui);
                            break;
                        }
                        WatcherSignal::Interrupt => {
                            // In raw mode the terminal's Ctrl-C→SIGINT
                            // translation is OFF, so this keystroke is NOT a
                            // signal. Drive the shutdown LADDER directly
                            // rather than re-raising SIGINT: the TUI's
                            // `install_signal_terminal_restore` sigaction
                            // handler intercepts SIGINT and hard-terminates
                            // (restoring the terminal) before the runtime's
                            // graceful handler can run — so a re-raise here
                            // loses both the operator notice and the metrics
                            // flush. Each keystroke advances one rung:
                            // graceful → cancel in-flight ops (process
                            // cleanup continues) → force-exit. Only the
                            // force-exit rung is handled here (it owns the
                            // terminal restore); the first two rungs are the
                            // shared `escalate_shutdown` ladder.
                            if nmbrs_runtime::session_signals::shutdown_level() >= 2 {
                                let _ = crossterm::terminal::disable_raw_mode();
                                std::process::exit(130);
                            } else {
                                nmbrs_runtime::session_signals::escalate_shutdown(
                                    nmbrs_runtime::session_signals::ShutdownOrigin::CtrlC,
                                );
                            }
                        }
                        WatcherSignal::Suspend => {
                            // Honour the user's expectation that
                            // Ctrl-Z suspends the process, just
                            // as a cooked-mode shell would. We
                            // briefly drop raw mode so the
                            // foreground/background dance behaves
                            // sensibly, raise SIGTSTP, and re-
                            // enable raw mode after the OS
                            // resumes us via `fg`.
                            // Job control is a Unix concept —
                            // on Windows Ctrl-Z is just a byte
                            // (EOF in cooked mode); ignore it.
                            #[cfg(unix)]
                            {
                                let _ = crossterm::terminal::disable_raw_mode();
                                unsafe {
                                    libc::raise(libc::SIGTSTP);
                                }
                                // Execution resumes here when `fg`
                                // delivers SIGCONT. Re-arm the
                                // terminal for keystroke detection.
                                let _ = crossterm::terminal::enable_raw_mode();
                            }
                        }
                        WatcherSignal::Redraw => {
                            // Standard cooked-terminal Ctrl-L:
                            // clear the screen and park the
                            // cursor at the home position. The
                            // LogOnlySink will continue printing
                            // new lines on its normal cadence —
                            // no replay of historical buffer
                            // (matches what users see from a
                            // cooked-shell `clear`).
                            use std::io::Write;
                            let mut err = std::io::stderr();
                            let _ = err.write_all(b"\x1b[2J\x1b[H");
                            let _ = err.flush();
                        }
                        WatcherSignal::ExplainPulse => {
                            // `?` keystroke: toggle the global
                            // explainer overlay. First press
                            // turns it on with a 10 s auto-
                            // revert deadline; second press
                            // while on turns it off. Auto-
                            // repeat-safe via the debounce
                            // inside `toggle_explain`.
                            nmbrs_runtime::observer::toggle_explain();
                        }
                        WatcherSignal::ReplToggleBar => {
                            // `~` keystroke: cycle REPL
                            // visibility (Hidden ↔ Bar; any
                            // visible state → Hidden). The
                            // global state has its own
                            // auto-repeat debounce, so holding
                            // the key doesn't strobe.
                            crate::repl_state::toggle_bar();
                        }
                        WatcherSignal::ReplToggleWindow => {
                            // `Ctrl-~` keystroke: open or close
                            // the full-screen REPL window.
                            crate::repl_state::toggle_window();
                        }
                        // Prompt-bound signals. The
                        // `LogOnlySink` owns the receiver and
                        // applies these directly to its
                        // `PromptState`. A disconnected
                        // receiver (sink shutting down) is
                        // not fatal here — we just drop the
                        // signal; the sink will catch up on
                        // its next start.
                        sig @ (WatcherSignal::Key(_)
                        | WatcherSignal::GrowPrompt
                        | WatcherSignal::ShrinkPrompt
                        | WatcherSignal::ToggleHelp) => {
                            let _ = prompt_tx.send(sig);
                        }
                    }
                }
            }
            ActiveSink::Tui { sync, .. } => {
                if sync.yielded.load(Ordering::Acquire) {
                    swap_to = Some(Transition::TuiToTerminal);
                }
            }
        }

        if let Some(t) = swap_to {
            active = match t {
                Transition::TerminalToTui => {
                    swap_to_tui(&observer, &state, active, runtime.clone(), &resume_from)
                }
                Transition::TuiToTerminal => {
                    // Wait for the App thread to fully exit and
                    // restore the terminal before bringing the
                    // KeyWatcher back up (otherwise both might
                    // claim raw mode at once).
                    teardown(active);
                    // Release the inline-status suppression now
                    // that the alt-screen is gone — the
                    // LogOnlySink's log drain and the inline
                    // status line coexist on stderr without
                    // conflict, so the per-cycle status should
                    // be visible again. `sink_active` is
                    // re-asserted by `start_terminal` →
                    // `LogOnlySink::start`.
                    observer
                        .inline_suppress_flag()
                        .store(false, Ordering::Release);
                    match start_terminal(&observer, &state, runtime.clone(), &resume_from) {
                        Some(a) => a,
                        None => {
                            // Lost the TTY (unexpected). Fall
                            // through to no-supervisor; runner
                            // continues with synchronous stderr.
                            return;
                        }
                    }
                }
            };
            continue;
        }

        std::thread::sleep(TICK);
    }

    teardown(active);
    // Final clear: whichever sink we tore down might or might
    // not have left the flag set (LogOnlySink clears
    // sink_active on shutdown; TuiSink doesn't touch it).
    // After supervisor exit there's nothing rendering, so the
    // inline-status thread (if still alive) and any straggler
    // synchronous-stderr writes from the observer should be
    // unsuppressed.
    sink_active_flag.store(false, Ordering::Release);
    inline_suppress.store(false, Ordering::Release);
}

fn wait_for_done(done_rx: &mpsc::Receiver<()>) {
    // No supervised state — just block until the runner
    // signals completion.
    let _ = done_rx.recv();
}

enum Transition {
    TerminalToTui,
    TuiToTerminal,
}

fn start_terminal(
    observer: &Arc<LogOnlyObserver>,
    state: &RunStateHandle,
    runtime: Option<tokio::runtime::Handle>,
    resume_from: &Arc<AtomicU64>,
) -> Option<ActiveSink> {
    // Stderr-only TTY case (stdin is piped/redirected — common
    // for `nmbrs run ... 2>&1 | tee log.txt` or invocation under
    // a parent process that captures stdin): KeyWatcher refuses
    // to spawn (no raw-mode access), but the stderr terminal can
    // still render the status block via absolute positioning. We
    // start LogOnlySink without key plumbing — same readouts,
    // same in-place updates, just no interactive keys/prompt.
    let (signal_tx, signal_rx) = mpsc::channel::<WatcherSignal>();
    let watcher = KeyWatcher::spawn(signal_tx);

    // Prompt-bound channel: only wired when KeyWatcher is up.
    // The supervisor's match forwards prompt-bound signals
    // here; in the watcher-less degraded mode this channel
    // never carries traffic, so `prompt_tx` is `None` and
    // the sink is constructed without a key receiver.
    let (prompt_tx, prompt_rx) = mpsc::channel::<WatcherSignal>();

    let min_level = observer.min_level();
    let sink_active = observer.sink_active_flag();
    let mut sink = LogOnlySink::new(min_level, sink_active).with_resume(resume_from.clone());
    if watcher.is_some() {
        sink = sink.with_keys(prompt_rx, runtime);
    }
    let sink_handle = Box::new(sink).start(DisplayInputs {
        state: state.clone(),
        frame_rx: None,
        metrics_query: None,
    });

    Some(ActiveSink::Terminal {
        sink_handle,
        watcher,
        signal_rx,
        prompt_tx,
    })
}

fn swap_to_tui(
    observer: &Arc<LogOnlyObserver>,
    state: &RunStateHandle,
    active: ActiveSink,
    runtime: Option<tokio::runtime::Handle>,
    resume_from: &Arc<AtomicU64>,
) -> ActiveSink {
    // Tear down terminal mode first — the watcher disables
    // raw mode + releases stdin so the App can claim it.
    if let ActiveSink::Terminal {
        sink_handle,
        watcher,
        prompt_tx,
        ..
    } = active
    {
        // Dropping `prompt_tx` disconnects the prompt receiver
        // inside the sink so its drain loop exits cleanly; the
        // sink_handle.shutdown() that follows then joins.
        drop(prompt_tx);
        sink_handle.shutdown();
        if let Some(w) = watcher {
            w.shutdown();
        }
    } else {
        unreachable!("swap_to_tui called outside Terminal state");
    }

    let frame_rx = observer.subscribe_frames();
    let query = match observer.metrics_query() {
        Some(q) => q,
        None => {
            // Cadence reporter not yet wired by the runner —
            // this can only happen if the user hits Ctrl-T
            // before the first phase fires. Fall back to
            // terminal mode with a one-line notice.
            let _ = std::io::Write::write_all(
                &mut std::io::stderr(),
                b"Ctrl-T: TUI not yet ready (metrics scheduler pending) - retry once the run is underway\r\n",
            );
            return start_terminal(observer, state, runtime, resume_from)
                .expect("re-entering terminal mode after deferred swap");
        }
    };

    // Two flags now flip on the way into TUI mode:
    //   • sink_active — re-asserted because LogOnlySink's
    //     shutdown cleared it, and the TUI also "renders log
    //     lines" (inside its own panel) so the observer must
    //     stay quiet on stderr.
    //   • inline_suppress — first becomes true here. The TUI
    //     owns the alt-screen; the inline-status thread's
    //     `\r\x1b[K…` would otherwise overwrite the TUI's
    //     bottom-row content.
    observer.sink_active_flag().store(true, Ordering::Release);
    observer
        .inline_suppress_flag()
        .store(true, Ordering::Release);

    let sync = TuiSinkSync::default();
    let sink = Box::new(TuiSink::new(frame_rx, query, sync.clone()));
    let sink_handle = sink.start(DisplayInputs {
        state: state.clone(),
        frame_rx: None,
        metrics_query: None,
    });

    ActiveSink::Tui { sink_handle, sync }
}

fn teardown(active: ActiveSink) {
    match active {
        ActiveSink::Terminal {
            sink_handle,
            watcher,
            ..
        } => {
            sink_handle.shutdown();
            if let Some(w) = watcher {
                w.shutdown();
            }
        }
        ActiveSink::Tui { sink_handle, .. } => {
            sink_handle.shutdown();
        }
    }
}