bmux_cli 0.0.1-alpha.1

Command-line interface for bmux terminal multiplexer
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
use crate::connection::{
    ConnectionContext, ConnectionPolicyScope, ServerRuntimeMetadata, connect,
    connect_if_running_with_context, connect_raw, connect_raw_with_context, connect_with_context,
    current_cli_build_id, expand_bmux_target_if_needed, map_client_connect_error,
    read_server_runtime_metadata, remove_server_runtime_metadata_file,
    write_server_runtime_metadata,
};
use crate::input::{InputProcessor, Keymap, RuntimeAction};
use anyhow::{Context, Result};
use bmux_cli_schema::{
    RecordingCursorBlinkMode, RecordingCursorMode, RecordingCursorPaintMode,
    RecordingCursorProfile, RecordingCursorShape, RecordingCursorTextMode, RecordingEventKindArg,
    RecordingExportFormat, RecordingListOrderArg, RecordingListSortArg, RecordingListStatusArg,
    RecordingPaletteSource, RecordingProfileArg, RecordingRenderMode, RecordingReplayMode,
};
use bmux_client::BmuxClient;
use bmux_config::{BmuxConfig, ConfigPaths, push_process_config_overrides};
use bmux_recording_protocol::{RecordingEventKind, RecordingStatus, RecordingSummary};
use bmux_snapshot_plugin_api::offline_snapshot::offline_kill_sessions;
use crossterm::terminal;
use gif::{Encoder as GifEncoder, Frame as GifFrame, Repeat};
use std::io::{self, BufWriter, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::Command as ProcessCommand;
use std::time::{Duration, Instant};
use uuid::Uuid;

type RecordingEventEnvelope =
    bmux_recording_protocol::RecordingEventEnvelope<bmux_ipc::Event, bmux_ipc::ErrorCode>;

mod access_cli;
mod action_dispatch;
pub mod attach;
mod bootstrap;
mod built_in_commands;
mod cli_parse;
mod config_cli;
mod dispatch;
mod doctor_cli;
mod hosted_output;
mod kiosk_cli;
mod logs_cli;
mod logs_watch;
mod perf_cli;
mod playbook_cli;
mod plugin_commands;
mod plugin_host;
mod plugin_kernel;
mod plugin_runtime;
mod prompt;
mod recording;
mod recording_cli;
mod remote_cli;
mod sandbox_cli;
mod server_commands;
mod server_runtime;
mod session_cli;
mod session_follow;
pub mod slot;
mod slot_cli;
mod terminal_doctor;
mod terminal_protocol;

use self::logs_watch::{
    active_log_file_path, run_logs_profiles_delete, run_logs_profiles_list,
    run_logs_profiles_rename, run_logs_profiles_show, run_logs_watch,
};
use access_cli::{
    run_access_add, run_access_disable, run_access_enable, run_access_init, run_access_list,
    run_access_remove, run_access_status,
};
pub use action_dispatch::{ActionDispatchError, ActionDispatchRequest};
pub use attach::runtime::AttachRunOutcome;
use attach::runtime::run_session_attach_with_client;
pub use attach::state::AttachExitReason;
use bootstrap::{
    DefaultAttachOptions, LogProcessScope, init_logging, map_attach_client_error,
    map_cli_client_error, run_default_server_attach, run_server_start, run_session_attach,
};
use built_in_commands::{BuiltInHandlerId, built_in_command_by_handler};
use cli_parse::{
    ParsedRuntimeCli, parse_runtime_cli, resolve_log_level, tracing_level,
    validate_record_bootstrap_flags,
};
use config_cli::{
    run_config_get, run_config_path, run_config_profiles_diff, run_config_profiles_evaluate,
    run_config_profiles_explain, run_config_profiles_lint, run_config_profiles_list,
    run_config_profiles_resolve, run_config_profiles_set_active_at_path, run_config_profiles_show,
    run_config_profiles_switch, run_config_set, run_config_show,
};
use dispatch::run_command;
use doctor_cli::run_doctor;
use kiosk_cli::{
    run_kiosk_attach, run_kiosk_init, run_kiosk_issue_token, run_kiosk_revoke_token,
    run_kiosk_ssh_print_config, run_kiosk_status,
};
use logs_cli::{run_logs_level, run_logs_path, run_logs_tail};
use perf_cli::{run_perf_off, run_perf_on, run_perf_status};
use playbook_cli::{
    run_playbook_cleanup, run_playbook_diff, run_playbook_dry_run, run_playbook_from_recording,
    run_playbook_interactive, run_playbook_run, run_playbook_validate,
};
use plugin_kernel::{
    EFFECTIVE_LOG_LEVEL, KernelClientFactory, LOG_CONTROL, available_capability_providers,
    available_service_descriptors, core_provided_capabilities, enter_host_kernel_client_factory,
    enter_host_kernel_connection, host_kernel_bridge, register_plugin_service_handlers,
    service_descriptors_from_declarations,
};
use plugin_runtime::{
    activate_loaded_plugins, build_runtime_command_state,
    bundled_plugin_root as bundled_plugin_roots, command_accepts_repeat, deactivate_loaded_plugins,
    discover_bundled_plugin_ids, dispatch_loaded_plugin_event, effective_enabled_plugins,
    load_enabled_plugins, load_plugin, plugin_command_policy_hints, plugin_event_bridge_loop,
    plugin_host_metadata, plugin_system_event, registered_plugin_entry_exists,
    resolve_plugin_search_paths, run_external_plugin_command, run_plugin_command,
    run_plugin_command_with_state, run_plugin_keybinding_command,
    run_plugin_keybinding_command_with_active_bindings, run_plugin_keybinding_command_with_state,
    scan_available_plugins, validate_enabled_plugins,
};
pub use prompt::{
    PromptEvent, PromptField, PromptOption, PromptPolicy, PromptRequest, PromptResponse,
    PromptSubmitError, PromptValidation, PromptValue, PromptWidth,
};
pub use recording::recording_plugin_error;
use recording_cli::{
    recording_event_kind_name, replay_interactive, replay_verify, replay_watch,
    run_recording_analyze, run_recording_cut, run_recording_delete, run_recording_delete_all,
    run_recording_export, run_recording_inspect, run_recording_list, run_recording_path,
    run_recording_replay, run_recording_start, run_recording_status, run_recording_stop,
    run_recording_verify_smoke, verify_recording_report,
};
use remote_cli::{
    SSH_RECONNECT_MAX_ATTEMPTS, connect_attach_target_with_kernel,
    maybe_run_cluster_plugin_command_via_gateway, reconnect_backoff_ms, run_auth_login,
    run_auth_logout, run_auth_status, run_connect, run_host, run_hosts, run_join,
    run_remote_complete_sessions, run_remote_complete_targets, run_remote_doctor, run_remote_init,
    run_remote_install_server, run_remote_list, run_remote_test, run_remote_upgrade, run_setup,
    run_share, run_target_proxy_from_current_argv, run_unshare, should_proxy_to_target,
};
use sandbox_cli::{
    BundleIncludeOptions, BundleSandboxOptions, InspectTargetOptions, RerunSandboxOptions,
    RunSandboxOptions, TriageBundleOptions, TriageSandboxOptions, run_sandbox_bundle,
    run_sandbox_cleanup, run_sandbox_doctor, run_sandbox_inspect, run_sandbox_list,
    run_sandbox_open, run_sandbox_rebuild_index, run_sandbox_rerun, run_sandbox_run,
    run_sandbox_status, run_sandbox_tail, run_sandbox_triage, run_sandbox_verify_bundle,
};
use server_commands::{
    run_server_bridge, run_server_gateway, run_server_recording_clear, run_server_recording_path,
    run_server_recording_start, run_server_recording_status, run_server_recording_stop,
    run_server_restore, run_server_save, run_server_status, run_server_stop,
    run_server_whoami_principal, server_event_name,
};
use server_runtime::{
    cleanup_stale_pid_file, fetch_server_status, is_pid_running, parse_pid_content,
    read_server_pid_file, remove_server_pid_file, server_is_running, try_kill_pid,
    wait_for_process_exit, wait_for_server_running, wait_until_server_stopped,
    write_server_pid_file,
};

/// Re-export of [`server_runtime::is_pid_running`] for cross-module callers
/// (e.g. `crate::connection`).
pub fn is_pid_running_crate(pid: u32) -> anyhow::Result<bool> {
    server_runtime::is_pid_running(pid)
}
use session_cli::{
    attach_quit_failure_status, run_client_list, run_session_kill, run_session_kill_all,
    run_session_list, run_session_new,
};
use session_follow::{
    parse_session_selector, parse_uuid_value, run_follow, run_session_detach, run_unfollow,
};
use terminal_doctor::{
    check_terminfo_available, merged_runtime_keybindings, resolve_pane_term, run_keymap_doctor,
    run_keymap_explain, run_terminal_doctor, run_terminal_install_terminfo, terminal_profile_name,
};
use terminal_protocol::{
    ProtocolDirection, ProtocolProfile, ProtocolTraceEvent, primary_da_for_profile,
    protocol_profile_name, secondary_da_for_profile, supported_query_names,
};

const SERVER_POLL_INTERVAL: Duration = Duration::from_millis(200);
const SERVER_START_TIMEOUT: Duration = Duration::from_secs(5);
const SERVER_STATUS_TIMEOUT: Duration = Duration::from_secs(1);
const SERVER_STOP_TIMEOUT: Duration = Duration::from_secs(5);
const VERIFY_SERVER_START_TIMEOUT_DEFAULT: Duration = Duration::from_secs(30);
const ATTACH_SCROLLBACK_UNAVAILABLE_STATUS: &str = "scrollback unavailable for focused pane";
const ATTACH_SELECTION_STARTED_STATUS: &str = "selection started";
const ATTACH_SELECTION_CLEARED_STATUS: &str = "selection cleared";
const ATTACH_SELECTION_COPIED_STATUS: &str = "selection copied";
const ATTACH_SELECTION_EMPTY_STATUS: &str = "no selection";
const ATTACH_TRANSIENT_STATUS_TTL: Duration = Duration::from_millis(1800);
const ATTACH_WELCOME_STATUS_TTL: Duration = Duration::from_millis(2600);
const HELP_OVERLAY_SURFACE_ID: Uuid = Uuid::from_u128(1);

pub fn active_runtime_name() -> String {
    std::env::var("BMUX_RUNTIME_NAME")
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty())
        .unwrap_or_else(|| "default".to_string())
}

pub fn append_runtime_arg(command: &mut ProcessCommand) {
    command.arg("--runtime").arg(active_runtime_name());
}

pub async fn run_attach_with_client(
    client: BmuxClient,
    target: Option<&str>,
    follow: Option<&str>,
    global: bool,
) -> Result<AttachRunOutcome> {
    run_session_attach_with_client(client, target, follow, global, None).await
}

pub fn submit_prompt_request(
    request: PromptRequest,
) -> std::result::Result<tokio::sync::oneshot::Receiver<PromptResponse>, PromptSubmitError> {
    prompt::submit(request)
}

pub fn submit_prompt_request_with_events(
    request: PromptRequest,
) -> std::result::Result<
    (
        tokio::sync::oneshot::Receiver<PromptResponse>,
        tokio::sync::mpsc::UnboundedReceiver<PromptEvent>,
    ),
    PromptSubmitError,
> {
    prompt::submit_with_events(request)
}

pub async fn request_prompt_response(
    request: PromptRequest,
) -> std::result::Result<PromptResponse, PromptSubmitError> {
    prompt::request(request).await
}

pub async fn request_prompt_response_with_events(
    request: PromptRequest,
) -> std::result::Result<
    (
        PromptResponse,
        tokio::sync::mpsc::UnboundedReceiver<PromptEvent>,
    ),
    PromptSubmitError,
> {
    let (response_rx, event_rx) = prompt::request_with_events(request)?;
    let response = response_rx
        .await
        .map_err(|_| PromptSubmitError::HostDisconnected)?;
    Ok((response, event_rx))
}

pub fn dispatch_action(action: impl Into<String>) -> std::result::Result<(), ActionDispatchError> {
    action_dispatch::dispatch(action)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TerminalProfile {
    Bmux256Color,
    Screen256Color,
    Xterm256Color,
    Conservative,
}

/// Returns `true` for commands that enter raw terminal mode (TUI attach
/// sessions).  When `command` is `None` the default attach path is taken,
/// which always enters raw mode.
const fn logging_scope_for_command(command: Option<&bmux_cli_schema::Command>) -> LogProcessScope {
    use bmux_cli_schema::{Command, KioskCommand, ServerCommand, SessionCommand};
    let Some(cmd) = command else {
        return LogProcessScope::Client;
    };
    match cmd {
        Command::Attach { .. }
        | Command::Connect { .. }
        | Command::Join { .. }
        | Command::Host { .. }
        | Command::Kiosk {
            command: KioskCommand::Attach { .. },
        }
        | Command::Session {
            command: SessionCommand::Attach { .. },
        } => LogProcessScope::Client,
        Command::Server {
            command: ServerCommand::Start { .. } | ServerCommand::Gateway { .. },
        } => LogProcessScope::Server,
        _ => LogProcessScope::Command,
    }
}

pub fn set_logging_client_id(client_id: impl Into<String>) {
    if let Some(control) = LOG_CONTROL.get() {
        control.set_subject_id(client_id);
    }
}

const fn command_enters_raw_mode(command: Option<&bmux_cli_schema::Command>) -> bool {
    use bmux_cli_schema::{
        Command, LogsCommand, PlaybookCommand, RecordingCommand, SessionCommand,
    };
    let Some(cmd) = command else {
        // Default path (no subcommand) = attach to server.
        return true;
    };
    matches!(
        cmd,
        Command::Attach { .. }
            | Command::Connect { .. }
            | Command::Join { .. }
            | Command::Host { .. }
            | Command::Session {
                command: SessionCommand::Attach { .. }
            }
            | Command::Recording {
                command: RecordingCommand::Replay { .. }
            }
            | Command::Playbook {
                command: PlaybookCommand::Run { .. } | PlaybookCommand::Interactive { .. }
            }
            | Command::Logs {
                command: LogsCommand::Watch { .. }
            }
    )
}

/// Print a startup banner on stderr when:
/// - a slot is active,
/// - the slot differs from the manifest's presentational default,
/// - the command enters raw-mode (interactive attach-like flows),
/// - `BMUX_SLOT_BANNER` is not "off",
/// - stderr is a TTY.
fn maybe_print_slot_startup_banner(command: Option<&bmux_cli_schema::Command>) {
    use std::io::IsTerminal as _;
    if std::env::var("BMUX_SLOT_BANNER")
        .ok()
        .is_some_and(|v| v.trim().eq_ignore_ascii_case("off"))
    {
        return;
    }
    if !command_enters_raw_mode(command) {
        return;
    }
    if !std::io::stderr().is_terminal() {
        return;
    }
    let state = slot::active_slot();
    let slot::ActiveSlotState::Resolved { slot, manifest, .. } = state else {
        return;
    };
    let default_name = manifest.resolved_default().map_or("", |s| s.name.as_str());
    if slot.name == default_name {
        return;
    }
    eprintln!("[bmux slot={} @ {}]", slot.name, env!("CARGO_PKG_VERSION"));
}

pub async fn run() -> Result<u8> {
    match parse_runtime_cli()? {
        ParsedRuntimeCli::BuiltIn {
            cli,
            log_level,
            verbose,
            config_overrides,
        } => {
            let _config_override_guard = push_process_config_overrides(config_overrides);
            let file_only = command_enters_raw_mode(cli.command.as_ref()) || cli.core_builtins_only;
            let logging_scope = logging_scope_for_command(cli.command.as_ref());
            let _logging_guard = init_logging(verbose, Some(log_level), file_only, logging_scope);
            tracing::info!(
                scope = ?logging_scope,
                file_only,
                has_command = cli.command.is_some(),
                core_builtins_only = cli.core_builtins_only,
                "runtime.cli.builtin_start"
            );
            validate_record_bootstrap_flags(&cli)?;
            maybe_print_slot_startup_banner(cli.command.as_ref());
            let connection_context = ConnectionContext::new(cli.target.as_deref());
            if should_proxy_to_target(&cli).await? {
                return run_target_proxy_from_current_argv(&cli).await;
            }

            if let Some(command) = &cli.command {
                return Box::pin(run_command(command, connection_context)).await;
            }

            let options = DefaultAttachOptions {
                record: cli.record,
                capture_input: !cli.no_capture_input,
                profile: cli.record_profile,
                name: cli.record_name.clone(),
                event_kinds: cli.record_event_kind.clone(),
                recording_id_file: cli.recording_id_file.clone(),
                stop_server_on_exit: cli.stop_server_on_exit,
            };
            run_default_server_attach(options, connection_context).await
        }
        ParsedRuntimeCli::Plugin {
            log_level,
            plugin_id,
            command_name,
            arguments,
            command_state,
            config_overrides,
        } => {
            let _config_override_guard = push_process_config_overrides(config_overrides);
            let _logging_guard =
                init_logging(false, Some(log_level), false, LogProcessScope::Command);
            tracing::info!(plugin_id, command_name, "runtime.cli.plugin_start");
            if let Some(status) =
                maybe_run_cluster_plugin_command_via_gateway(&plugin_id, &command_name, &arguments)
                    .await?
            {
                return Ok(status);
            }
            if let Some(command_state) = command_state.as_ref() {
                run_plugin_command_with_state(command_state, &plugin_id, &command_name, &arguments)
                    .await
            } else {
                run_plugin_command(&plugin_id, &command_name, &arguments).await
            }
        }
        ParsedRuntimeCli::ImmediateExit {
            code,
            output,
            stderr,
        } => {
            if stderr {
                eprint!("{output}");
            } else {
                print!("{output}");
            }
            Ok(code)
        }
    }
}

// ── Playbook commands ────────────────────────────────────────────────────────