rmux 0.9.0

A local terminal multiplexer with a tmux-style CLI, daemon runtime, Rust SDK, and ratatui integration.
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
use std::path::Path;

use rmux_proto::{PaneOutputSubscriptionId, PaneOutputSubscriptionStart, Response};
use serde_json::json;

use crate::cli_args::{CollectPaneOutputArgs, StreamPaneArgs};
use crate::cli_response::tmux_cli_error_message;

use super::super::ExitFailure;
use super::common::{
    check_disabled, connect_cli, pane_process_state, pane_snapshot, resolve_pane_ref,
    sleep_poll_interval, stdout_closed, visible_lines, visible_text, write_json, write_stderr_line,
    write_stdout_bytes, write_stdout_bytes_or_broken_pipe, PaneProcessState, StdoutWrite,
    SCHEMA_VERSION,
};

const CURSOR_BATCH_EVENTS: u16 = 128;
const LINE_BUFFER_MAX: usize = 1_048_576;

pub(crate) fn run_stream_pane(
    args: StreamPaneArgs,
    socket_path: &Path,
) -> Result<i32, ExitFailure> {
    check_disabled("RMUX_DISABLE_STREAM_PANE", "stream-pane")?;
    let mut connection = connect_cli(socket_path)?;
    let target = resolve_pane_ref(&mut connection, args.target.as_ref(), "stream-pane")?;
    let subscription_id = subscribe(
        &mut connection,
        target.clone(),
        PaneOutputSubscriptionStart::Oldest,
    )?;
    let line_mode = args.lines && !args.raw;
    let mut line_buffer = Vec::new();
    let mut line_buffer_force_flushed = false;
    let mut wrote_stdout = false;
    loop {
        if stdout_closed() {
            let _ = connection.unsubscribe_pane_output(subscription_id);
            return Ok(0);
        }
        let batch = poll_output(&mut connection, subscription_id, "stream-pane")?;
        if batch.lag.is_some() {
            line_buffer.clear();
            line_buffer_force_flushed = false;
            if !wrote_stdout {
                match write_lag_snapshot_seed(&mut connection, target.clone(), line_mode)? {
                    LagSnapshotSeed::Written => wrote_stdout = true,
                    LagSnapshotSeed::BrokenPipe => {
                        let _ = connection.unsubscribe_pane_output(subscription_id);
                        return Ok(0);
                    }
                    LagSnapshotSeed::Empty => {}
                }
            }
        }
        for bytes in batch.chunks {
            if line_mode {
                if write_lines(&mut line_buffer, &mut line_buffer_force_flushed, &bytes)? {
                    let _ = connection.unsubscribe_pane_output(subscription_id);
                    return Ok(0);
                }
                wrote_stdout |= bytes.contains(&b'\n');
            } else {
                if matches!(
                    write_stdout_bytes_or_broken_pipe(&bytes)?,
                    StdoutWrite::BrokenPipe
                ) {
                    let _ = connection.unsubscribe_pane_output(subscription_id);
                    return Ok(0);
                }
                wrote_stdout = true;
            }
        }
        if batch.saw_eof {
            if line_mode && flush_line_buffer(&mut line_buffer)? {
                let _ = connection.unsubscribe_pane_output(subscription_id);
                return Ok(0);
            }
            let _ = connection.unsubscribe_pane_output(subscription_id);
            return Ok(0);
        }
        sleep_poll_interval();
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LagSnapshotSeed {
    Empty,
    Written,
    BrokenPipe,
}

fn write_lag_snapshot_seed(
    connection: &mut rmux_client::Connection,
    target: rmux_proto::PaneTargetRef,
    line_mode: bool,
) -> Result<LagSnapshotSeed, ExitFailure> {
    let snapshot = pane_snapshot(connection, target)?;
    if line_mode {
        let mut wrote = false;
        for line in visible_lines(&snapshot) {
            if line.is_empty() {
                continue;
            }
            if matches!(write_line(line.into_bytes())?, StdoutWrite::BrokenPipe) {
                return Ok(LagSnapshotSeed::BrokenPipe);
            }
            wrote = true;
        }
        return Ok(if wrote {
            LagSnapshotSeed::Written
        } else {
            LagSnapshotSeed::Empty
        });
    }

    let seed = visible_text(&snapshot);
    if seed.is_empty() {
        return Ok(LagSnapshotSeed::Empty);
    }
    if matches!(
        write_stdout_bytes_or_broken_pipe(seed.as_bytes())?,
        StdoutWrite::BrokenPipe
    ) {
        return Ok(LagSnapshotSeed::BrokenPipe);
    }
    Ok(LagSnapshotSeed::Written)
}

pub(crate) fn run_collect_pane_output(
    args: CollectPaneOutputArgs,
    socket_path: &Path,
) -> Result<i32, ExitFailure> {
    check_disabled("RMUX_DISABLE_STREAM_PANE", "collect-pane-output")?;
    let mut connection = connect_cli(socket_path)?;
    let target_ref =
        resolve_pane_ref(&mut connection, args.target.as_ref(), "collect-pane-output")?;
    let subscription_id = subscribe(
        &mut connection,
        target_ref.clone(),
        PaneOutputSubscriptionStart::Oldest,
    )?;
    let mut output = Vec::new();
    let mut total_bytes: usize = 0;
    let mut truncated = false;
    let mut pane_exit = None;
    let mut saw_eof = false;
    let mut missed_events = 0_u64;
    loop {
        let batch = poll_output(&mut connection, subscription_id, "collect-pane-output")?;
        saw_eof |= batch.saw_eof;
        if let Some(lag) = batch.lag {
            missed_events = missed_events.saturating_add(lag.missed_events);
        }
        for bytes in batch.chunks {
            total_bytes = total_bytes.saturating_add(bytes.len());
            if output.len() < args.max_bytes {
                let remaining = args.max_bytes - output.len();
                let keep = bytes.len().min(remaining);
                output.extend_from_slice(&bytes[..keep]);
                truncated |= keep < bytes.len();
            } else {
                truncated = true;
            }
        }
        if pane_exit.is_none() {
            if let PaneProcessState::Exited(value) =
                pane_process_state(&mut connection, &target_ref)?
            {
                pane_exit = Some(value);
            }
        }
        if saw_eof && pane_exit.is_some() {
            break;
        }
        sleep_poll_interval();
    }
    let _ = connection.unsubscribe_pane_output(subscription_id);
    let pane_exit = pane_exit.expect("pane exit is observed before collect-pane-output breaks");
    if missed_events > 0 {
        if args.json {
            return write_json(&json!({
                "schema_version": SCHEMA_VERSION,
                "ok": false,
                "error": "pane-output-lag",
                "bytes": total_bytes,
                "stored_bytes": output.len(),
                "truncated": truncated,
                "missed_events": missed_events,
                "pane_exit": pane_exit.json_value(),
            }))
            .map(|_| 1);
        }
        return Err(ExitFailure::new(
            1,
            format!(
                "collect-pane-output lost pane output due to lag; missed {missed_events} events"
            ),
        ));
    }
    if args.json {
        return write_json(&json!({
            "schema_version": SCHEMA_VERSION,
            "ok": true,
            "bytes": total_bytes,
            "stored_bytes": output.len(),
            "truncated": truncated,
            "output_utf8_lossy": String::from_utf8_lossy(&output),
            "pane_exit": pane_exit.json_value(),
        }));
    }
    write_stdout_bytes(&output)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct OutputLag {
    pub(super) missed_events: u64,
}

pub(super) struct OutputBatch {
    pub(super) chunks: Vec<Vec<u8>>,
    pub(super) saw_eof: bool,
    pub(super) lag: Option<OutputLag>,
}

pub(super) fn subscribe(
    connection: &mut rmux_client::Connection,
    target: rmux_proto::PaneTargetRef,
    start: PaneOutputSubscriptionStart,
) -> Result<PaneOutputSubscriptionId, ExitFailure> {
    match connection
        .subscribe_pane_output_ref(target, start)
        .map_err(ExitFailure::from_client)?
    {
        Response::SubscribePaneOutput(response) => Ok(response.subscription_id),
        Response::Error(error) => Err(ExitFailure::new(
            1,
            tmux_cli_error_message("stream-pane", &error.error),
        )),
        other => Err(ExitFailure::new(
            1,
            format!(
                "protocol error: unexpected '{}' response for stream-pane",
                other.command_name()
            ),
        )),
    }
}

pub(super) fn poll_output(
    connection: &mut rmux_client::Connection,
    subscription_id: PaneOutputSubscriptionId,
    command_name: &'static str,
) -> Result<OutputBatch, ExitFailure> {
    poll_output_inner(connection, subscription_id, command_name, true)
}

pub(super) fn poll_output_silent_lag(
    connection: &mut rmux_client::Connection,
    subscription_id: PaneOutputSubscriptionId,
    command_name: &'static str,
) -> Result<OutputBatch, ExitFailure> {
    poll_output_inner(connection, subscription_id, command_name, false)
}

fn poll_output_inner(
    connection: &mut rmux_client::Connection,
    subscription_id: PaneOutputSubscriptionId,
    command_name: &'static str,
    report_lag: bool,
) -> Result<OutputBatch, ExitFailure> {
    match connection
        .pane_output_cursor(subscription_id, Some(CURSOR_BATCH_EVENTS))
        .map_err(ExitFailure::from_client)?
    {
        Response::PaneOutputCursor(response) => {
            let mut saw_eof = false;
            let chunks = response
                .events
                .into_iter()
                .filter_map(|event| {
                    if event.bytes.is_empty() {
                        saw_eof = true;
                        None
                    } else {
                        Some(event.bytes)
                    }
                })
                .collect();
            Ok(OutputBatch {
                chunks,
                saw_eof,
                lag: None,
            })
        }
        Response::PaneOutputLag(response) => {
            if report_lag {
                write_stderr_line(&format!(
                    "{command_name}: pane output lagged; missed {} events",
                    response.lag.missed_events
                ));
            }
            Ok(OutputBatch {
                chunks: Vec::new(),
                saw_eof: false,
                lag: Some(OutputLag {
                    missed_events: response.lag.missed_events,
                }),
            })
        }
        Response::Error(error) => Err(ExitFailure::new(
            1,
            tmux_cli_error_message(command_name, &error.error),
        )),
        other => Err(ExitFailure::new(
            1,
            format!(
                "protocol error: unexpected '{}' response for {command_name}",
                other.command_name()
            ),
        )),
    }
}

fn write_lines(
    buffer: &mut Vec<u8>,
    force_flushed: &mut bool,
    bytes: &[u8],
) -> Result<bool, ExitFailure> {
    let mut broken_pipe = false;
    split_lines_bounded(buffer, force_flushed, bytes, |line| {
        if matches!(write_line(line)?, StdoutWrite::BrokenPipe) {
            broken_pipe = true;
        }
        Ok(())
    })?;
    Ok(broken_pipe)
}

fn flush_line_buffer(buffer: &mut Vec<u8>) -> Result<bool, ExitFailure> {
    let mut broken_pipe = false;
    flush_line_buffer_into(buffer, |line| {
        if matches!(write_line(line)?, StdoutWrite::BrokenPipe) {
            broken_pipe = true;
        }
        Ok(())
    })?;
    Ok(broken_pipe)
}

fn flush_line_buffer_into<F>(buffer: &mut Vec<u8>, mut emit: F) -> Result<(), ExitFailure>
where
    F: FnMut(Vec<u8>) -> Result<(), ExitFailure>,
{
    if buffer.is_empty() {
        return Ok(());
    }
    emit(std::mem::take(buffer))
}

fn write_line(mut line: Vec<u8>) -> Result<StdoutWrite, ExitFailure> {
    if line.ends_with(b"\r") {
        line.pop();
    }
    let text = String::from_utf8_lossy(&line);
    if matches!(
        write_stdout_bytes_or_broken_pipe(text.as_bytes())?,
        StdoutWrite::BrokenPipe
    ) {
        return Ok(StdoutWrite::BrokenPipe);
    }
    write_stdout_bytes_or_broken_pipe(b"\n")
}

fn split_lines_bounded<F>(
    buffer: &mut Vec<u8>,
    force_flushed: &mut bool,
    bytes: &[u8],
    mut emit: F,
) -> Result<(), ExitFailure>
where
    F: FnMut(Vec<u8>) -> Result<(), ExitFailure>,
{
    for byte in bytes {
        if *byte == b'\n' {
            if buffer.is_empty() && *force_flushed {
                *force_flushed = false;
                continue;
            }
            emit(std::mem::take(buffer))?;
            *force_flushed = false;
        } else {
            buffer.push(*byte);
            *force_flushed = false;
            if buffer.len() >= LINE_BUFFER_MAX {
                emit(std::mem::take(buffer))?;
                *force_flushed = true;
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{flush_line_buffer_into, split_lines_bounded, LINE_BUFFER_MAX};

    #[test]
    fn line_stream_buffer_is_force_flushed_at_fixed_limit() {
        let mut buffer = Vec::new();
        let mut force_flushed = false;
        let bytes = vec![b'a'; LINE_BUFFER_MAX + 5];
        let mut lines = Vec::new();

        split_lines_bounded(&mut buffer, &mut force_flushed, &bytes, |line| {
            lines.push(line);
            Ok(())
        })
        .unwrap();

        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0].len(), LINE_BUFFER_MAX);
        assert_eq!(buffer.len(), 5);
    }

    #[test]
    fn line_stream_skips_newline_after_force_flush() {
        let mut buffer = Vec::new();
        let mut force_flushed = false;
        let bytes = vec![b'a'; LINE_BUFFER_MAX];
        let mut lines = Vec::new();

        split_lines_bounded(&mut buffer, &mut force_flushed, &bytes, |line| {
            lines.push(line);
            Ok(())
        })
        .unwrap();
        split_lines_bounded(&mut buffer, &mut force_flushed, b"\n", |line| {
            lines.push(line);
            Ok(())
        })
        .unwrap();

        assert_eq!(lines.len(), 1);
        assert!(buffer.is_empty());
        assert!(!force_flushed);
    }

    #[test]
    fn line_stream_flushes_final_partial_line_on_eof() {
        let mut buffer = b"FINAL".to_vec();
        let mut lines = Vec::new();

        flush_line_buffer_into(&mut buffer, |line| {
            lines.push(line);
            Ok(())
        })
        .unwrap();

        assert_eq!(lines, vec![b"FINAL".to_vec()]);
        assert!(buffer.is_empty());
    }
}