rmux-server 0.10.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
use std::fs;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use super::control_support::{with_control_queue_identity, ControlClientIdentity};
use super::RequestHandler;
use crate::control::{ControlModeUpgrade, ControlServerEvent, CONTROL_SERVER_EVENT_CAPACITY};
use crate::handler::scripting_support::QueueExecutionContext;
use crate::hook_runtime::with_hook_execution;
use crate::outer_terminal::OuterTerminalContext;
use crate::server_access::AccessMode;
use rmux_core::command_parser::CommandParser;
use rmux_core::TargetFindContext;
use rmux_proto::{
    encode_internal_runtime_command_arguments, BreakPaneRequest, DisplayMessageRequest, HookName,
    IfShellRequest, KillSessionRequest, KillWindowRequest, LastWindowRequest, LinkWindowRequest,
    NewSessionExtRequest, NewSessionRequest, NewWindowRequest, NextWindowRequest, OptionName,
    OptionScopeSelector, PaneTarget, PreviousWindowRequest, Request, RespawnPaneRequest,
    RespawnWindowRequest, Response, RotateWindowDirection, RotateWindowRequest,
    RunShellDelaySeconds, RunShellRequest, RunShellResponse, ScopeSelector, SelectPaneRequest,
    SessionName, SetEnvironmentRequest, SetOptionMode, SetOptionRequest, ShowBufferRequest,
    ShowEnvironmentRequest, ShowOptionsRequest, SourceFileRequest, SplitDirection,
    SplitWindowRequest, SplitWindowTarget, SwapPaneDirection, SwapPaneRequest, Target,
    TerminalSize, WaitForMode, WaitForRequest, WaitForResponse, WindowTarget,
    INTERNAL_CANONICAL_COMMAND_EXECUTION_PATH, INTERNAL_PARSE_TIME_ASSIGNMENTS_PATH,
    INTERNAL_RUNTIME_COMMAND_EXPANSION_PATH,
};

fn session_name(value: &str) -> SessionName {
    SessionName::new(value).expect("valid session name")
}

fn wait_for(channel: &str, mode: WaitForMode) -> Request {
    Request::WaitFor(WaitForRequest {
        channel: channel.to_owned(),
        mode,
    })
}

fn run_shell(command: &str, background: bool) -> Request {
    Request::RunShell(Box::new(RunShellRequest {
        command: command.to_owned(),
        arguments: Vec::new(),
        background,
        as_commands: false,
        show_stderr: false,
        delay_seconds: None,
        start_directory: None,
        target: None,
        source_depth: None,
    }))
}

fn source_file_request(paths: Vec<String>, cwd: Option<PathBuf>) -> Request {
    Request::SourceFile(Box::new(SourceFileRequest {
        paths,
        quiet: false,
        parse_only: false,
        verbose: false,
        expand_paths: false,
        target: None,
        caller_cwd: cwd,
        stdin: None,
    }))
}

fn source_file_stdout_failure(response: Response) -> String {
    let Response::SourceFile(response) = response else {
        panic!("expected source-file failure response, got {response:?}");
    };
    assert_eq!(response.exit_status(), Some(1));
    assert!(
        response.stderr().is_empty(),
        "source-file parse diagnostics must stay on stdout: {:?}",
        response.stderr()
    );
    String::from_utf8(
        response
            .command_output()
            .expect("source-file failure should include stdout diagnostics")
            .stdout()
            .to_vec(),
    )
    .expect("source-file stdout diagnostic is UTF-8")
}

fn temp_root(label: &str) -> PathBuf {
    let unique = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time after epoch")
        .as_nanos();
    std::env::temp_dir().join(format!(
        "rmux-source-file-{label}-{}-{unique}",
        std::process::id()
    ))
}

fn write_config(path: &Path, contents: &str) {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).expect("config parent directory");
    }
    fs::write(path, contents).expect("write config");
}

fn write_executable_script(path: &Path, contents: &str) {
    write_config(path, contents);
    #[cfg(unix)]
    {
        let mut permissions = fs::metadata(path).expect("script metadata").permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(path, permissions).expect("script permissions");
    }
}

fn shell_quote(path: &Path) -> String {
    format!("'{}'", path.display().to_string().replace('\'', "'\\''"))
}

fn command_quote(command: &str) -> String {
    crate::test_shell::command_quote(command)
}

async fn use_platform_test_shell(handler: &RequestHandler) {
    #[cfg(not(windows))]
    let _ = handler;

    #[cfg(windows)]
    {
        let powershell = std::env::var_os("SystemRoot")
            .map(PathBuf::from)
            .map(|root| {
                root.join("System32")
                    .join("WindowsPowerShell")
                    .join("v1.0")
                    .join("powershell.exe")
            })
            .unwrap_or_else(|| PathBuf::from("powershell.exe"));

        assert!(matches!(
            handler
                .handle(Request::SetOption(SetOptionRequest {
                    scope: ScopeSelector::Global,
                    option: OptionName::DefaultShell,
                    value: powershell.to_string_lossy().into_owned(),
                    mode: SetOptionMode::Replace,
                }))
                .await,
            Response::SetOption(_)
        ));
    }
}

async fn wait_for_named_buffer(handler: &RequestHandler, name: &str, expected: &[u8]) {
    tokio::time::timeout(background_shell_test_timeout(), async {
        loop {
            if let Some(output) = handler
                .handle(Request::ShowBuffer(ShowBufferRequest {
                    name: Some(name.to_owned()),
                }))
                .await
                .command_output()
            {
                if output.stdout() == expected {
                    return;
                }
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
    })
    .await
    .unwrap_or_else(|_| panic!("buffer {name:?} did not become {expected:?}"));
}

async fn wait_for_detached_request_count(handler: &RequestHandler, expected: usize) {
    tokio::time::timeout(background_shell_test_timeout(), async {
        loop {
            let active = handler
                .active_detached_requests
                .load(std::sync::atomic::Ordering::SeqCst);
            if active == expected {
                return;
            }
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }
    })
    .await
    .unwrap_or_else(|_| panic!("detached request count did not become {expected}"));
}

fn background_shell_test_timeout() -> std::time::Duration {
    #[cfg(windows)]
    {
        // A cold Windows PowerShell process can start slowly when hosted CI is
        // compiling and running several test shards. This is a liveness bound,
        // not a process-start latency assertion.
        std::time::Duration::from_secs(30)
    }
    #[cfg(not(windows))]
    {
        // Background shell startup competes with thousands of async tests in
        // the full server suite. Keep this as a bounded liveness budget, not a
        // scheduler-latency assertion.
        std::time::Duration::from_secs(8)
    }
}

async fn register_control_for_session(
    handler: &RequestHandler,
    requester_pid: u32,
    session_name: SessionName,
) -> (u64, tokio::sync::mpsc::Receiver<ControlServerEvent>) {
    let (event_tx, event_rx) =
        tokio::sync::mpsc::channel::<ControlServerEvent>(CONTROL_SERVER_EVENT_CAPACITY);
    let control_id = handler
        .register_control_with_closing(
            requester_pid,
            ControlModeUpgrade {
                initial_command_count: 0,
                mode: rmux_proto::ControlMode::Plain,
                terminal_context: OuterTerminalContext::default(),
            },
            event_tx,
            Arc::new(AtomicBool::new(false)),
        )
        .await;
    handler
        .set_control_session(requester_pid, Some(session_name))
        .await
        .expect("control session binds");
    (control_id, event_rx)
}

async fn create_background_identity_session(handler: &RequestHandler, session_name: SessionName) {
    let response = handler
        .handle(Request::NewSession(NewSessionRequest {
            session_name,
            detached: true,
            size: Some(TerminalSize { cols: 80, rows: 24 }),
            environment: None,
        }))
        .await;
    assert!(matches!(response, Response::NewSession(_)), "{response:?}");
}

async fn replace_background_identity_session(handler: &RequestHandler, session_name: SessionName) {
    let response = handler
        .handle(Request::KillSession(KillSessionRequest {
            target: session_name.clone(),
            kill_all_except_target: false,
            clear_alerts: false,
            kill_group: false,
        }))
        .await;
    assert!(matches!(response, Response::KillSession(_)), "{response:?}");
    create_background_identity_session(handler, session_name).await;
}

async fn wait_for_active_window_name(
    handler: &RequestHandler,
    session_name: &SessionName,
    expected: &str,
) {
    tokio::time::timeout(background_shell_test_timeout(), async {
        loop {
            let matches = {
                let state = handler.state.lock().await;
                state
                    .sessions
                    .session(session_name)
                    .and_then(|session| session.window_at(session.active_window_index()))
                    .and_then(rmux_core::Window::name)
                    == Some(expected)
            };
            if matches {
                return;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("background command follows the active attached session");
}

async fn wait_for_background_waiter(handler: &RequestHandler, channel: &str) {
    tokio::time::timeout(background_shell_test_timeout(), async {
        loop {
            if handler.wait_for_counts(channel).0 == 1 {
                return;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("background command reaches its wait-for seam");
}

async fn release_background_waiter(handler: &RequestHandler, channel: &str) {
    let response = handler.handle(wait_for(channel, WaitForMode::Signal)).await;
    assert_eq!(response, Response::WaitFor(WaitForResponse));
}

async fn assert_sessions_survive_background_control_reuse(
    handler: &RequestHandler,
    original: &SessionName,
    replacement: &SessionName,
) {
    wait_for_detached_request_count(handler, 0).await;
    let state = handler.state.lock().await;
    assert!(
        state.sessions.contains_session(original),
        "the stale background command must not mutate the original session"
    );
    assert!(
        state.sessions.contains_session(replacement),
        "the stale background command must not jump to the replacement registration"
    );
}

#[cfg(unix)]
fn delayed_true_shell_condition() -> String {
    "sleep 0.05; true".to_owned()
}

#[cfg(windows)]
fn delayed_true_shell_condition() -> String {
    "Start-Sleep -Milliseconds 50; exit 0".to_owned()
}

#[cfg(unix)]
fn shell_print_command(text: &str) -> String {
    format!("printf {}", command_quote(text))
}

#[cfg(windows)]
fn shell_print_command(text: &str) -> String {
    format!(
        "[Console]::Out.Write({})",
        crate::test_shell::powershell_quote(text)
    )
}

#[cfg(unix)]
fn shell_print_then_exit_command(text: &str, code: u8) -> String {
    format!("printf {}; exit {code}", command_quote(text))
}

#[cfg(windows)]
fn shell_print_then_exit_command(text: &str, code: u8) -> String {
    format!(
        "[Console]::Out.Write({}); exit {code}",
        crate::test_shell::powershell_quote(text)
    )
}

#[cfg(unix)]
fn shell_stderr_command(text: &str) -> String {
    format!("printf {} >&2", command_quote(text))
}

#[cfg(windows)]
fn shell_stderr_command(text: &str) -> String {
    format!(
        "[Console]::Error.Write({})",
        crate::test_shell::powershell_quote(text)
    )
}

#[cfg(unix)]
fn shell_success_command() -> String {
    "true".to_owned()
}

#[cfg(windows)]
fn shell_success_command() -> String {
    crate::test_shell::powershell_encoded_command("exit 0")
}

#[path = "handler_scripting_tests/run_shell.rs"]
mod run_shell;

#[path = "handler_scripting_tests/source_file_core.rs"]
mod source_file_core;

#[path = "handler_scripting_tests/source_file_conditions.rs"]
mod source_file_conditions;

#[path = "handler_scripting_tests/if_shell.rs"]
mod if_shell;

#[path = "handler_scripting_tests/parsed_queue_core.rs"]
mod parsed_queue_core;

#[path = "handler_scripting_tests/detached_access.rs"]
mod detached_access;

#[path = "handler_scripting_tests/parsed_queue_cwd.rs"]
mod parsed_queue_cwd;

#[path = "handler_scripting_tests/queued_inventory.rs"]
mod queued_inventory;

#[path = "handler_scripting_tests/queue_exact_target.rs"]
mod queue_exact_target;

#[path = "handler_scripting_tests/parsed_queue_split.rs"]
mod parsed_queue_split;

#[path = "handler_scripting_tests/parsed_queue_targets.rs"]
mod parsed_queue_targets;

#[path = "handler_scripting_tests/parsed_queue_swap_window.rs"]
mod parsed_queue_swap_window;

#[path = "handler_scripting_tests/parsed_queue_windows_mouse.rs"]
mod parsed_queue_windows_mouse;

#[path = "handler_scripting_tests/parsed_queue_move_window_current.rs"]
mod parsed_queue_move_window_current;

#[path = "handler_scripting_tests/parsed_queue_select_zoom.rs"]
mod parsed_queue_select_zoom;

#[path = "handler_scripting_tests/parsed_queue_resize_trim.rs"]
mod parsed_queue_resize_trim;

#[path = "handler_scripting_tests/mouse_origin_copy_mode.rs"]
mod mouse_origin_copy_mode;

#[path = "handler_scripting_tests/prompt_mouse_origin.rs"]
mod prompt_mouse_origin;

#[path = "handler_scripting_tests/control_hooks_wait.rs"]
mod control_hooks_wait;

#[path = "handler_scripting_tests/list_windows_all.rs"]
mod list_windows_all;

#[path = "handler_scripting_tests/command_alias.rs"]
mod command_alias;

#[path = "handler_scripting_tests/command_blocks.rs"]
mod command_blocks;

#[path = "handler_scripting_tests/parser_flags.rs"]
mod parser_flags;

#[path = "handler_scripting_tests/parser_option_flags.rs"]
mod parser_option_flags;

#[path = "handler_scripting_tests/select_layout_flags.rs"]
mod select_layout_flags;