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
//! Boot-time wiring and per-tick drains.
//!
//! Submodule of [`super::app`]; sees its parent's private fields
//! directly. Splitting these out keeps `app.rs` focused on App's
//! identity (fields + the common state mutations) — the
//! background-task plumbing (spawn the worker, kick off the
//! discovery / diff-preload / watcher-spawn tasks, drain their
//! channels, poke the worker for fresh polls) all lives here.
use super::{App, DiffPreload, WatcherSpawnResult};
use crate::config::model::UiPane;
use crate::tui::services::ServicePane;
use crate::tui::views::terminals::state::TmuxWindow;
use crate::tui::watchers::WatcherPane;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};
impl App {
/// Spawn the three boot tasks (service discovery, diff preload,
/// watcher panes) plus the background state worker. Returns
/// immediately; results land via the channels stored on
/// `self.boot` and are folded into App state by
/// [`Self::drain_boot_results`].
///
/// The render loop can paint its first frame before any of these
/// finish — boot time is no longer gated on a slow Docker daemon
/// or a cold git cache.
pub fn spawn_boot_tasks(&mut self, project_root: &Path) {
// 0. Spawn the background state worker if we have a backend.
// Seeded with whatever services the config already declared
// so the first poll tick has work to do without waiting for
// discovery to land. Also seeds the tmux session name so the
// worker can poll #{window_bell_flag} for the Terminals view.
if let Some(backend) = self.backend.as_ref().map(Arc::clone) {
let initial = self.services.keys().cloned().collect();
let handle = crate::tui::worker::spawn(backend, initial);
let _ = handle
.cmd_tx
.send(crate::tui::worker::WorkerCommand::SetTmuxSession(Some(
self.terminals.session_name.clone(),
)));
self.worker = Some(handle);
}
// 1. Service discovery — one-shot.
if let Some(backend) = self.backend.as_ref().map(Arc::clone) {
let (tx, rx) = oneshot::channel();
tokio::spawn(async move {
// Empty Vec on failure: caller treats discovery as
// best-effort (the auto-discovered group just stays
// empty). Matches the pre-refactor `discover_services`
// behaviour where a `list_services` Err was a no-op.
let names = backend.list_services().await.unwrap_or_default();
let _ = tx.send(names);
});
self.boot.discover_rx = Some(rx);
}
// 2. Diff preload — one-shot.
let project_root_owned = project_root.to_path_buf();
let configured_base = self.config.diff.base.clone();
let (diff_tx, diff_rx) = oneshot::channel();
tokio::spawn(async move {
let trunk =
crate::runtime::detect_trunk(&project_root_owned, configured_base.as_deref()).await;
let anchor = match trunk.as_deref() {
Some(t) => crate::runtime::merge_base(&project_root_owned, t).await,
None => None,
};
let branch = crate::tui::views::diff::git::current_branch(&project_root_owned).await;
let anchor_short = anchor
.as_deref()
.map(|sha| sha.chars().take(7).collect::<String>());
let lazygit_available = crate::tui::lazygit::is_available();
let files = crate::tui::views::diff::git::load_diff_files(
&project_root_owned,
anchor.as_deref(),
)
.await;
let _ = diff_tx.send(DiffPreload {
trunk,
anchor,
anchor_short,
branch,
lazygit_available,
files,
});
});
self.boot.diff_rx = Some(diff_rx);
// 3. Watcher panes — mpsc, one item per pane. Spawning is sync
// (globset compile + notify init) so we hop to `spawn_blocking`;
// each pane streams back independently so a slow pane (notify
// init on a deep tree) doesn't hold up the others.
let watcher_specs: Vec<(usize, String, Vec<String>, u64)> = self
.config
.ui
.panes
.iter()
.enumerate()
.filter_map(|(idx, pane)| match pane {
UiPane::Watcher {
glob,
on_change,
debounce_ms,
..
} => Some((idx, on_change.clone(), glob.clone(), *debounce_ms)),
_ => None,
})
.collect();
if !watcher_specs.is_empty() {
let (w_tx, w_rx) = mpsc::unbounded_channel();
let project_root_for_watchers = project_root.to_path_buf();
tokio::task::spawn_blocking(move || {
// Replicate `unique_watcher_name` locally — we don't
// hold a reference to App from this thread, so we
// dedupe against names we've already emitted.
let mut emitted: std::collections::BTreeSet<String> =
std::collections::BTreeSet::new();
for (idx, on_change, glob, debounce_ms) in watcher_specs {
let primary = format!("watch:{on_change}");
let name = if emitted.insert(primary.clone()) {
primary
} else {
let fallback = format!("watch:{on_change}:{idx}");
emitted.insert(fallback.clone());
fallback
};
let pane = WatcherPane::spawn(
name.clone(),
on_change,
glob,
Duration::from_millis(debounce_ms),
&project_root_for_watchers,
);
if w_tx.send(WatcherSpawnResult { name, pane }).is_err() {
// Receiver dropped — TUI exited mid-spawn.
return;
}
}
});
self.boot.watcher_rx = Some(w_rx);
}
}
/// Non-blocking drain of any boot-task results that have landed
/// since the last call. Called once per loop iteration in the
/// pre-render hooks so the sidebar / header fill in as soon as
/// each task completes — no extra wake needed.
pub fn drain_boot_results(&mut self) {
// Service discovery.
if let Some(rx) = self.boot.discover_rx.as_mut()
&& let Ok(names) = rx.try_recv()
{
self.boot.discover_rx = None;
let mut added = false;
for name in names {
if !self.services.contains_key(&name) {
self.services
.insert(name.clone(), ServicePane::new(name.clone()));
added = true;
}
}
if added {
self.items = super::build_items_from(&self.config, &self.services, &self.watchers);
// Refresh the worker's service set so the auto-
// discovered rows start receiving status updates.
if let Some(w) = self.worker.as_ref() {
let _ = w
.cmd_tx
.send(crate::tui::worker::WorkerCommand::SetServices(
self.services.keys().cloned().collect(),
));
}
}
}
// Diff preload.
if let Some(rx) = self.boot.diff_rx.as_mut()
&& let Ok(preload) = rx.try_recv()
{
self.boot.diff_rx = None;
self.apply_diff_preload(preload);
}
// Watcher panes — drain everything that arrived this tick.
if let Some(rx) = self.boot.watcher_rx.as_mut() {
let mut added = false;
let mut disconnected = false;
loop {
match rx.try_recv() {
Ok(result) => match result.pane {
Ok(pane) => {
self.watchers.insert(result.name, pane);
added = true;
}
Err(e) => {
tracing::warn!("watcher pane `{}` failed to start: {e}", result.name);
}
},
Err(mpsc::error::TryRecvError::Empty) => break,
Err(mpsc::error::TryRecvError::Disconnected) => {
disconnected = true;
break;
}
}
}
if disconnected {
self.boot.watcher_rx = None;
}
if added {
self.items = super::build_items_from(&self.config, &self.services, &self.watchers);
}
}
}
pub(crate) fn apply_diff_preload(&mut self, preload: DiffPreload) {
self.diff.set_anchor(
preload.trunk,
preload.anchor,
preload.branch,
preload.anchor_short,
);
self.diff.set_lazygit_available(preload.lazygit_available);
match preload.files {
Ok(files) => self.diff.set_files(files),
Err(msg) => self.diff.set_error(msg),
}
}
/// Apply any worker snapshots that have arrived since the last
/// call. Non-blocking — replaces the inline
/// `refresh_service_status` await that used to shell out to
/// compose for each service on every pre-render tick.
pub fn drain_worker_snapshots(&mut self) {
// Coalesce tmux-window snapshots: only the most recent
// reflects the current state, and applying older ones first
// would briefly flicker stale data on the next render.
let mut latest_tmux: Option<Vec<TmuxWindow>> = None;
let Some(w) = self.worker.as_mut() else {
return;
};
loop {
match w.snap_rx.try_recv() {
Ok(crate::tui::worker::WorkerSnapshot::ServiceStatus { name, status }) => {
if let Some(pane) = self.services.get_mut(&name) {
pane.status = Some(status);
}
}
Ok(crate::tui::worker::WorkerSnapshot::TmuxWindows(windows)) => {
latest_tmux = Some(windows);
}
Err(mpsc::error::TryRecvError::Empty) => break,
Err(mpsc::error::TryRecvError::Disconnected) => {
self.worker = None;
break;
}
}
}
if let Some(windows) = latest_tmux {
self.terminals_set_windows(windows);
}
}
/// Signal the worker that service-affecting state just changed
/// (e.g. compose `up`/`down`/`restart`) so it polls immediately
/// rather than waiting for the next interval tick. Best-effort —
/// the next tick would still pick the change up within ~2 s.
pub fn poke_worker_status(&self) {
if let Some(w) = self.worker.as_ref() {
let _ = w
.cmd_tx
.send(crate::tui::worker::WorkerCommand::PokeServiceStatus);
}
}
/// Signal the worker to refresh the tmux window list now —
/// after an attach return, kill, or view entry, so the row list
/// (and bell flags) feel reactive instead of waiting for the
/// 1 s tick.
pub fn poke_worker_tmux_windows(&self) {
if let Some(w) = self.worker.as_ref() {
let _ = w
.cmd_tx
.send(crate::tui::worker::WorkerCommand::PokeTmuxWindows);
}
}
/// Spawn tail processes for every service pane that doesn't
/// already have one. Called once at startup; idempotent if
/// called again.
pub async fn spawn_service_tails(&mut self) {
let Some(backend) = self.backend.as_ref().map(Arc::clone) else {
return;
};
for pane in self.services.values_mut() {
pane.ensure_tailing(&backend).await;
}
}
/// Spawn watcher panes from `[[ui.pane]] type = "watcher"`. Spawn
/// failures (bad globs, notify init failures) are logged and the
/// pane is omitted; the rest of the dashboard still works.
pub fn spawn_watcher_panes(&mut self, project_root: &Path) {
let mut added = false;
for (idx, pane) in self.config.ui.panes.iter().enumerate() {
if let UiPane::Watcher {
glob,
on_change,
debounce_ms,
..
} = pane
{
let name = super::unique_watcher_name(on_change, idx, &self.watchers);
match WatcherPane::spawn(
name.clone(),
on_change.clone(),
glob.clone(),
Duration::from_millis(*debounce_ms),
project_root,
) {
Ok(p) => {
self.watchers.insert(name, p);
added = true;
}
Err(e) => {
tracing::warn!("watcher pane `{on_change}` failed to start: {e}");
}
}
}
}
if added {
self.items = super::build_items_from(&self.config, &self.services, &self.watchers);
}
}
}