Skip to content

feat: preserve outer TUI enhanced keyboard state across PTY sessions #236

Description

@inureyes

Problem / Background

#234 restores ordinary local-shell input after a remote PTY leaks Kitty keyboard
progressive-enhancement state. Its best-effort teardown intentionally drains the
Kitty mode stacks and clears the active flags on both the current and main
screens.

That fallback is correct for the common case where bssh is launched from an
ordinary shell, but it cannot preserve an enhanced keyboard mode already owned
by an outer local TUI. If that TUI launches bssh, returning from the SSH
session may leave the outer application expecting CSI-u input while the terminal
has been reset to legacy keyboard reporting.

This limitation is documented in src/pty/terminal.rs and
docs/architecture/interactive-mode.md. It was explicitly deferred by #234
because crossterm 0.29 exposes support detection but not the active Kitty
keyboard flags, and a naïve /dev/tty query can race the PTY input reader.

Goal

Preserve and restore the local terminal's pre-existing enhanced-keyboard state
across an interactive bssh PTY session without racing or consuming user input.

Proposed Investigation

  • Determine whether a newer crossterm API can expose the active Kitty keyboard
    flags, or whether the necessary query support should be contributed upstream.
  • If bssh must issue CSI ? u itself, design a bounded query/response phase
    before the PTY input task starts so the response cannot race ordinary input.
  • Record the state independently for the main and alternate screens where
    required by the Kitty protocol.
  • Restore the captured flags during both normal RAII teardown and forced
    cleanup, while retaining fix: restore enhanced keyboard modes after PTY disconnect #234's legacy-baseline reset as a fallback when
    querying is unsupported or fails.
  • Assess equivalent state preservation for xterm modifyOtherKeys.

Acceptance Criteria

  • Launching bssh from an outer Kitty-keyboard-aware TUI does not change the
    outer application's effective keyboard protocol after bssh exits.
  • Pre-existing Kitty enhancement flags are restored on the applicable main
    and alternate screens.
  • Query responses cannot be consumed by, or confused with, user input or
    remote PTY traffic.
  • Unsupported terminals and failed/time-limited queries fall back safely to
    the ordinary-shell cleanup introduced by fix: restore enhanced keyboard modes after PTY disconnect #234.
  • Normal EOF, abrupt transport loss, local ~., input-send failure, and
    forced cleanup paths preserve the captured state consistently.
  • Automated tests cover successful queries, timeouts/malformed replies,
    unsupported terminals, exact restore bytes, and the legacy fallback.
  • A real-TTY test covers an outer TUI in Ghostty and at least one additional
    Kitty-keyboard-compatible terminal.

Implementation Note

The implementation captures the effective Kitty flags on each screen. The Kitty protocol does not expose the contents of an application's private mode stack, so cleanup drains remote-owned stack entries and restores the captured effective values.

DEC 1049 has a separate protocol limitation: setting it clears the alternate buffer and overwrites a single saved-cursor slot. When bssh starts inside an outer alternate-screen TUI and the remote resets 1049, cleanup therefore reselects the preserved buffer with non-clearing mode 47 instead of claiming it can reconstruct the 1049 mode bit, cursor slot, and outer contents simultaneously.

Related

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions