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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0
//! `DisplaySink` — uniform consumer interface for the run-state
//! snapshot stream.
//!
//! ## Architecture
//!
//! The runtime publishes state through one canonical channel:
//!
//! ```text
//! RunObserver (lifecycle callbacks)
//! │
//! ▼ RunStateCmd
//! RunState actor (mpsc → ArcSwap<Arc<RunState>>)
//! │
//! ▼ snapshot() / mpsc::Receiver<MetricSet>
//! DisplaySink ←── the trait this module defines
//! ```
//!
//! Every consumer that wants to render the live run — terminal
//! TUI, line-mode "fake TUI" emulation, log-only stderr, future
//! external dashboards, the inspector socket — implements this
//! trait. The runtime stays oblivious to which sink is wired in;
//! it only knows about the actor.
//!
//! Three impls are planned:
//!
//! | Impl | Surface | Owns terminal? |
//! |---------------------|---------|----------------|
//! | `TuiSink` (Phase 2) | `app::App` raw-mode + alt-screen | **yes** |
//! | `FakeTuiSink` (P2) | line-mode emulation, stderr | no |
//! | `LogOnlySink` | stderr line-stream (current `tui=off` shape) | no |
//!
//! Subsystems that *don't* consume the live stream (plotter,
//! post-run summary reports — they read `metrics.db` after the
//! run finishes) sit outside this trait. The trait is for
//! live-stream consumers only.
//!
//! ## Lifecycle
//!
//! ```text
//! sink = SomeSink::new(inputs);
//! handle = sink.start(); // takes ownership; spawns thread(s)
//! // ... runtime fires events into the actor ...
//! handle.shutdown(); // graceful drain + teardown
//! ```
//!
//! `shutdown` is the cooperative-stop signal; the sink thread
//! flushes any pending buffered output, restores any terminal
//! state it modified (raw mode, alt-screen), and exits. The
//! returned thread join-handle on `start` lets the host wait for
//! a clean teardown.
//!
//! ## Why a trait, not an enum
//!
//! - Each sink lives in its own file, with its own dependencies.
//! `TuiSink` needs `crossterm` + `ratatui`; `LogOnlySink`
//! doesn't.
//! - Future external sinks (web dashboard, IPC bridge) can be
//! added without touching every existing impl.
//! - The runner-side wiring code switches on `tui=` mode once at
//! startup and hands the trait object to a single executor —
//! one polymorphic dispatch on shutdown, none on the hot path.
use Arc;
use mpsc;
use MetricsQuery;
use MetricSet;
use crateRunStateHandle;
/// Inputs every sink consumes. Cheap to construct: every field
/// is either an `Arc` clone or an `mpsc::Receiver` (single-
/// consumer, owned by the sink that takes it).
///
/// The two non-state fields are optional: a [`LogOnlySink`]
/// that just renders log lines doesn't need either; the eventual
/// [`crate::app::App`]-backed `TuiSink` and the future
/// `FakeTuiSink` need both. `None` means "this sink doesn't
/// consume frames / query the cadence store" — the runner-side
/// wiring is responsible for not registering a reporter when no
/// sink will drain it.
///
/// [`LogOnlySink`]: crate::log_only_sink::LogOnlySink
/// Sink for the run-state snapshot stream.
///
/// Implementors live on their own thread (or group of threads).
/// The trait surface is intentionally tiny: construct, start,
/// shut down. The runtime never reaches into the sink for
/// per-event delivery — events flow through the actor; the sink
/// drains.
/// Handle returned by [`DisplaySink::start`]. The runner uses it
/// to signal shutdown and wait for the rendering thread to exit.
/// Holding this handle alive keeps the thread running; dropping
/// it without `shutdown` is a coding bug — the thread may leave
/// the terminal in raw mode or fail to flush its output.