rmux-server 0.9.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Silence alert timer synchronization and expiry handling.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::time::Duration;

use rmux_core::WINDOW_SILENCE;
use rmux_proto::{types::OptionScopeSelector, SessionId, SessionName, WindowId, WindowTarget};

use super::super::RequestHandler;
use super::{monitor_silence_seconds, SilenceTimerState};
use crate::pane_terminals::HandlerState;

#[path = "silence_timers/deadline_fanout.rs"]
mod deadline_fanout;
#[path = "silence_timers/new_session.rs"]
mod new_session;
#[path = "silence_timers/window_mutations.rs"]
mod window_mutations;

pub(in crate::handler) use deadline_fanout::SilenceTimerDeadlineFanout;

#[derive(Clone)]
struct DesiredSilenceTimer {
    target: WindowTarget,
    session_id: SessionId,
    window_id: WindowId,
    seconds: u64,
}

impl RequestHandler {
    pub(in crate::handler) async fn sync_session_silence_timers(&self, session_name: &SessionName) {
        let state = self.state.lock().await;
        let Some(session) = state.sessions.session(session_name) else {
            return;
        };
        let targets = session
            .windows()
            .keys()
            .copied()
            .map(|window_index| WindowTarget::with_window(session_name.clone(), window_index))
            .collect::<Vec<_>>();
        let desired = desired_silence_timers(&state, &targets);
        self.reconcile_silence_timers_for_session(session_name, desired);
        drop(state);
    }

    pub(in crate::handler) async fn sync_all_silence_timers(&self) {
        let state = self.state.lock().await;
        let targets = state
            .sessions
            .iter()
            .flat_map(|(session_name, session)| {
                session.windows().keys().copied().map(|window_index| {
                    WindowTarget::with_window(session_name.clone(), window_index)
                })
            })
            .collect::<Vec<_>>();
        let desired = desired_silence_timers(&state, &targets);

        let existing = {
            let timers = self
                .silence_timers
                .lock()
                .expect("silence timer mutex must not be poisoned");
            timers.keys().cloned().collect::<Vec<_>>()
        };
        for target in existing {
            if !desired.iter().any(|candidate| candidate.target == target) {
                self.remove_silence_timer(&target);
            }
        }
        for desired in desired {
            self.configure_silence_timer(desired);
        }
        drop(state);
    }

    pub(in crate::handler) async fn cancel_session_silence_timers(
        &self,
        session_name: &SessionName,
    ) {
        // Serialize cancellation with every state-derived timer producer.
        let state = self.state.lock().await;
        let current_session_id = state
            .sessions
            .session(session_name)
            .map(|session| session.id());
        let existing = {
            let timers = self
                .silence_timers
                .lock()
                .expect("silence timer mutex must not be poisoned");
            timers
                .iter()
                .filter(|(target, timer)| {
                    target.session_name() == session_name
                        && current_session_id != Some(timer.session_id)
                })
                .map(|(target, _)| target.clone())
                .collect::<Vec<_>>()
        };
        for target in existing {
            self.remove_silence_timer(&target);
        }
        drop(state);
    }

    pub(in crate::handler) async fn sync_alert_timers_for_option_scope(
        &self,
        scope: &OptionScopeSelector,
    ) {
        match scope {
            OptionScopeSelector::Session(session_name) => {
                self.sync_session_silence_timers(session_name).await;
            }
            OptionScopeSelector::Window(target) => {
                self.sync_window_family_silence_timers(
                    target.session_name(),
                    target.window_index(),
                )
                .await;
            }
            OptionScopeSelector::Pane(target) => {
                self.sync_window_family_silence_timers(
                    target.session_name(),
                    target.window_index(),
                )
                .await;
            }
            OptionScopeSelector::ServerGlobal
            | OptionScopeSelector::SessionGlobal
            | OptionScopeSelector::WindowGlobal => {
                self.sync_all_silence_timers().await;
            }
        }
    }

    async fn sync_window_family_silence_timers(
        &self,
        session_name: &SessionName,
        window_index: u32,
    ) {
        let state = self.state.lock().await;
        let targets = state.window_linked_window_targets(session_name, window_index);
        self.sync_window_silence_timers_locked(&state, targets);
        drop(state);
    }

    pub(in crate::handler) fn sync_window_silence_timers_locked(
        &self,
        state: &HandlerState,
        targets: Vec<WindowTarget>,
    ) {
        let desired = desired_silence_timers(state, &targets);
        for target in targets {
            if !desired.iter().any(|candidate| candidate.target == target) {
                self.remove_silence_timer(&target);
            }
        }
        for desired in desired {
            self.configure_silence_timer(desired);
        }
    }

    pub(in crate::handler) fn sync_inserted_window_silence_timers_locked(
        &self,
        state: &HandlerState,
        destination_targets: Vec<WindowTarget>,
        reindexed_session_names: Vec<SessionName>,
        index_map: BTreeMap<u32, u32>,
        deadline_fanout: Option<SilenceTimerDeadlineFanout>,
    ) {
        let desired_destinations = desired_silence_timers(state, &destination_targets);

        #[cfg(test)]
        self.pause_before_silence_timer_apply();

        let mut seen_sessions = HashSet::new();
        let mut reindexed_targets = Vec::new();
        for session_name in reindexed_session_names {
            if !seen_sessions.insert(session_name.clone()) {
                continue;
            }
            reindexed_targets.extend(
                index_map
                    .iter()
                    .filter(|(source_index, target_index)| source_index != target_index)
                    .map(|(source_index, target_index)| {
                        (
                            WindowTarget::with_window(session_name.clone(), *source_index),
                            WindowTarget::with_window(session_name.clone(), *target_index),
                        )
                    }),
            );
        }

        self.apply_silence_timer_reindex(
            state,
            reindexed_targets,
            destination_targets,
            desired_destinations,
            deadline_fanout,
        );
    }

    pub(in crate::handler) fn rekey_session_silence_timers_locked(
        &self,
        state: &HandlerState,
        previous_name: &SessionName,
        new_name: &SessionName,
        session_id: SessionId,
    ) {
        let reindexed_targets = {
            let timers = self
                .silence_timers
                .lock()
                .expect("silence timer mutex must not be poisoned");
            timers
                .iter()
                .filter(|(target, timer)| {
                    target.session_name() == previous_name && timer.session_id == session_id
                })
                .map(|(target, _)| {
                    (
                        target.clone(),
                        WindowTarget::with_window(new_name.clone(), target.window_index()),
                    )
                })
                .collect::<Vec<_>>()
        };
        self.apply_silence_timer_reindex(state, reindexed_targets, Vec::new(), Vec::new(), None);
    }

    fn apply_silence_timer_reindex(
        &self,
        state: &HandlerState,
        reindexed_targets: Vec<(WindowTarget, WindowTarget)>,
        removed_targets: Vec<WindowTarget>,
        desired_destinations: Vec<DesiredSilenceTimer>,
        deadline_fanout: Option<SilenceTimerDeadlineFanout>,
    ) {
        let mut touched_targets = removed_targets.into_iter().collect::<HashSet<_>>();
        touched_targets.extend(
            desired_destinations
                .iter()
                .map(|desired| desired.target.clone()),
        );
        for (source, destination) in &reindexed_targets {
            let _ = touched_targets.insert(source.clone());
            let _ = touched_targets.insert(destination.clone());
        }

        let mut timers = self
            .silence_timers
            .lock()
            .expect("silence timer mutex must not be poisoned");
        // Extract every source and destination before inserting any moved
        // timer. Adjacent index moves overlap, so incremental rekeying would
        // otherwise overwrite the next timer in the chain.
        let mut extracted = HashMap::new();
        let mut original_generations = HashMap::new();
        for target in touched_targets {
            if let Some(timer) = timers.remove(&target) {
                timer.task.abort();
                let _ = original_generations.insert(target.clone(), timer.generation);
                let _ = extracted.insert(target, timer);
            }
        }

        let mut moved_timers = Vec::new();
        for (source, destination) in reindexed_targets {
            let Some(timer) = extracted.remove(&source) else {
                continue;
            };
            let generation = timer
                .generation
                .max(original_generations.get(&destination).copied().unwrap_or(0))
                .saturating_add(1);
            // The absolute deadline follows the winlink. Bump past both old
            // generations so an aborted task already waiting on this mutex
            // cannot expire the replacement at its new key.
            let session_id = timer.session_id;
            let window_id = timer.window_id;
            let identity_matches = state
                .sessions
                .session(destination.session_name())
                .filter(|session| session.id() == session_id)
                .and_then(|session| session.window_at(destination.window_index()))
                .is_some_and(|window| window.id() == window_id);
            if !identity_matches {
                continue;
            }
            let deadline = timer.deadline;
            let task = self.spawn_silence_timer_task(
                destination.clone(),
                session_id,
                window_id,
                generation,
                deadline,
            );
            moved_timers.push((
                destination,
                SilenceTimerState {
                    session_id,
                    window_id,
                    generation,
                    deadline,
                    task,
                },
            ));
        }
        for (target, timer) in moved_timers {
            if let Some(previous) = timers.insert(target, timer) {
                previous.task.abort();
                debug_assert!(false, "silence timer rekey must be injective");
            }
        }

        for desired in desired_destinations {
            let target = desired.target;
            let generation = original_generations
                .get(&target)
                .copied()
                .unwrap_or(0)
                .saturating_add(1);
            if desired.seconds == 0 {
                continue;
            }
            let deadline = match deadline_fanout
                .filter(|fanout| fanout.window_id == desired.window_id)
                .map(|fanout| fanout.deadline)
            {
                Some(Some(deadline)) => deadline,
                Some(None) => continue,
                None => tokio::time::Instant::now() + Duration::from_secs(desired.seconds),
            };
            let task = self.spawn_silence_timer_task(
                target.clone(),
                desired.session_id,
                desired.window_id,
                generation,
                deadline,
            );
            if let Some(previous) = timers.insert(
                target,
                SilenceTimerState {
                    session_id: desired.session_id,
                    window_id: desired.window_id,
                    generation,
                    deadline,
                    task,
                },
            ) {
                previous.task.abort();
                debug_assert!(
                    false,
                    "new link destination must not collide with a shifted timer"
                );
            }
        }
    }

    pub(super) fn configure_silence_timer_locked(
        &self,
        state: &HandlerState,
        target: WindowTarget,
        seconds: u64,
    ) {
        let Some(desired) = desired_silence_timer(state, target.clone(), seconds) else {
            self.remove_silence_timer(&target);
            return;
        };
        self.configure_silence_timer(desired);
    }

    fn configure_silence_timer(&self, desired: DesiredSilenceTimer) {
        let target = desired.target;
        let mut timers = self
            .silence_timers
            .lock()
            .expect("silence timer mutex must not be poisoned");
        let generation = timers
            .get(&target)
            .map_or(1, |state| state.generation.saturating_add(1));
        if let Some(previous) = timers.remove(&target) {
            previous.task.abort();
        }
        if desired.seconds == 0 {
            return;
        }

        let deadline = tokio::time::Instant::now() + Duration::from_secs(desired.seconds);
        let task = self.spawn_silence_timer_task(
            target.clone(),
            desired.session_id,
            desired.window_id,
            generation,
            deadline,
        );
        timers.insert(
            target,
            SilenceTimerState {
                session_id: desired.session_id,
                window_id: desired.window_id,
                generation,
                deadline,
                task,
            },
        );
    }

    #[cfg(test)]
    pub(in crate::handler) fn silence_timer_generation_for_test(
        &self,
        target: &WindowTarget,
    ) -> Option<u64> {
        self.silence_timers
            .lock()
            .expect("silence timer mutex must not be poisoned")
            .get(target)
            .map(|state| state.generation)
    }

    #[cfg(test)]
    pub(in crate::handler) fn silence_timer_snapshot_for_test(
        &self,
        target: &WindowTarget,
    ) -> Option<(u64, tokio::time::Instant)> {
        self.silence_timers
            .lock()
            .expect("silence timer mutex must not be poisoned")
            .get(target)
            .map(|state| (state.generation, state.deadline))
    }

    #[cfg(test)]
    pub(in crate::handler) fn silence_timer_identity_for_test(
        &self,
        target: &WindowTarget,
    ) -> Option<(SessionId, WindowId, u64)> {
        self.silence_timers
            .lock()
            .expect("silence timer mutex must not be poisoned")
            .get(target)
            .map(|state| (state.session_id, state.window_id, state.generation))
    }

    #[cfg(test)]
    pub(in crate::handler) async fn expire_silence_timer_for_test(
        &self,
        target: WindowTarget,
        session_id: SessionId,
        window_id: WindowId,
        generation: u64,
    ) {
        self.handle_silence_timer_expired(target, session_id, window_id, generation)
            .await;
    }

    fn spawn_silence_timer_task(
        &self,
        target: WindowTarget,
        session_id: SessionId,
        window_id: WindowId,
        generation: u64,
        deadline: tokio::time::Instant,
    ) -> tokio::task::JoinHandle<()> {
        let handler = self.clone();
        tokio::spawn(async move {
            tokio::time::sleep_until(deadline).await;
            handler
                .handle_silence_timer_expired(target, session_id, window_id, generation)
                .await;
        })
    }

    fn remove_silence_timer(&self, target: &WindowTarget) {
        let mut timers = self
            .silence_timers
            .lock()
            .expect("silence timer mutex must not be poisoned");
        if let Some(previous) = timers.remove(target) {
            previous.task.abort();
        }
    }

    async fn handle_silence_timer_expired(
        &self,
        target: WindowTarget,
        session_id: SessionId,
        window_id: WindowId,
        generation: u64,
    ) {
        let attached_count = self.attached_count(target.session_name()).await;
        let plans = {
            let mut state = self.state.lock().await;
            #[cfg(test)]
            self.pause_before_silence_timer_apply();
            let target_identity_matches = state
                .sessions
                .session(target.session_name())
                .filter(|session| session.id() == session_id)
                .and_then(|session| session.window_at(target.window_index()))
                .is_some_and(|window| window.id() == window_id);
            let should_fire = target_identity_matches && {
                let mut timers = self
                    .silence_timers
                    .lock()
                    .expect("silence timer mutex must not be poisoned");
                match timers.get(&target) {
                    Some(timer)
                        if timer.session_id == session_id
                            && timer.window_id == window_id
                            && timer.generation == generation =>
                    {
                        // Remove without aborting — we are inside the expired task itself.
                        timers.remove(&target);
                        true
                    }
                    _ => false,
                }
            };
            if should_fire {
                self.alerts_queue_window_locked(&mut state, target, WINDOW_SILENCE, attached_count)
            } else {
                Vec::new()
            }
        };
        self.execute_alert_plans(plans).await;
    }

    fn reconcile_silence_timers_for_session(
        &self,
        session_name: &SessionName,
        desired: Vec<DesiredSilenceTimer>,
    ) {
        let existing = {
            let timers = self
                .silence_timers
                .lock()
                .expect("silence timer mutex must not be poisoned");
            timers
                .keys()
                .filter(|target| target.session_name() == session_name)
                .cloned()
                .collect::<Vec<_>>()
        };
        for target in existing {
            if !desired.iter().any(|candidate| candidate.target == target) {
                self.remove_silence_timer(&target);
            }
        }
        for desired in desired {
            self.configure_silence_timer(desired);
        }
    }
}

fn desired_silence_timers(
    state: &HandlerState,
    targets: &[WindowTarget],
) -> Vec<DesiredSilenceTimer> {
    targets
        .iter()
        .filter_map(|target| {
            desired_silence_timer(
                state,
                target.clone(),
                monitor_silence_seconds(
                    &state.options,
                    target.session_name(),
                    target.window_index(),
                ),
            )
        })
        .collect()
}

fn desired_silence_timer(
    state: &HandlerState,
    target: WindowTarget,
    seconds: u64,
) -> Option<DesiredSilenceTimer> {
    let session = state.sessions.session(target.session_name())?;
    let window = session.window_at(target.window_index())?;
    Some(DesiredSilenceTimer {
        target,
        session_id: session.id(),
        window_id: window.id(),
        seconds,
    })
}