Skip to content

Latest commit

 

History

History
129 lines (106 loc) · 6.27 KB

README.md

File metadata and controls

129 lines (106 loc) · 6.27 KB

SnapTTY

Zero-Friction Multi-Node tmux Session Handoff, Dynamic Viewport Resizing & SIGHUP-Safe Lifecycle Appliance (POSIX Shell & Zero-Pip Python 3)

Python: 3.8+ Zero-Pip Shell: POSIX / Bash Multiplexer: tmux 2.1+ License: MIT


1. The Multi-Computer Roaming Problem

Engineers and systems operators who move fluidly across multiple physical machines—a 4K workstation, a Linux laptop, a low-power netbook, and a mobile SSH terminal—hit four recurring operational bottlenecks with standard tmux workflows:

  1. Smallest-Screen Viewport Clamping ("The Dotted Border Problem"): Standard tmux attach leaves previous SSH clients attached in the background. When you switch from a small laptop or mobile screen to a 4K desktop monitor, tmux clamps your terminal dimensions to the smallest connected client.
  2. Prefix-Matching Session Collisions: By default, tmux has-session -t dev and tmux attach -t dev match prefixes, meaning an existing dev-sandbox session will hijack commands intended for dev.
  3. Nested Multiplexer Collisions ($TMUX Recursion): Invoking a session launcher from inside an already-attached tmux pane spawns a broken nested tmux-inside-tmux session instead of switching the client cleanly.
  4. Self-Terminating Daemon / AI Agent SIGHUP Truncation: When an automated workflow or CLI AI agent running inside a tmux session attempts to close its own session on completion (tmux kill-session), it immediately sends SIGHUP to its own PTY before flushing its final stdout response.

2. How SnapTTY Solves It

  [Host A: Netbook 1024x600] ──(Auto-Detached)──┐
  [Host B: Mobile SSH Client] ──(Auto-Detached)──┼──► [SnapTTY Engine] ──► Exact Match (-t "=session")
  [Host C: 4K Workstation]   ══(Atomic Snap-To)═╝          │
                                                           ├── Outside $TMUX : attach-session -d + window-size latest
                                                           ├── Inside  $TMUX : switch-client -t "=session"
                                                           └── Self-Close    : setsid Detached Reaper (--delay 3s)
  • Exclusive Atomic Takeover (attach-session -d + window-size latest): Detaches stale/ghost SSH clients from previous computers in a single atomic operation and forces the PTY geometry to adapt immediately to your current display.
  • Exact Target Matching (-t "=session"): Eliminates tmux prefix-matching ambiguity across all status, attach, switch, and teardown operations.
  • Context-Aware $TMUX Routing: Automatically detects if you are already inside tmux and executes tmux switch-client -t "=session" instead of nesting multiplexers.
  • Single-Word Shortcut Linker (snap link <alias> <cmd...>): Generates standalone executable wrappers in ~/.local/bin/<alias> so typing a single word (e.g., ai, dev, btop) upon SSH login immediately snaps you into that persistent workspace.
  • Double-Forked SIGHUP-Safe Reaper (snap close --delay N): Spawns an injection-proof, setsid-detached background timer (start_new_session=True, zero shell=True) that waits $N$ seconds for the calling process to flush stdout before dissolving the session.

3. Repository Structure

SnapTTY/
├── bin/
│   └── snap                 # Fast-path POSIX CLI wrapper (attach, link, list, status, close, reset, kill)
├── src/
│   └── snap_ctl.py          # Zero-Pip Python 3 telemetry, exact-match router & setsid delayed reaper
├── conf/
│   └── tmux.snap.conf       # Ergonomics profile (window-size latest, SGR-1006 mouse, 24-bit TrueColor)
├── tests/
│   └── test_snaptty.py      # Deterministic unittest suite (prefix isolation, injection guard, reaper)
└── install.sh               # Rootless installer to ~/.local/bin/snap and ~/.local/share/snaptty/

4. Quick Start & Usage

Installation (Zero External Python Dependencies)

git clone https://github.com/Tethtibis/SnapTTY.git
cd SnapTTY
./install.sh

1. Instant Session Roaming Across Computers

On Computer 1, launch or snap into a persistent workspace:

snap dev

Walk over to Computer 2 (or connect from mobile SSH) and run the exact same command:

snap dev

Computer 1 is cleanly detached, Computer 2 takes over at full native screen resolution, and your cursor/process state is preserved without interruption.

2. Create Single-Word SSH Shortcuts (snap link)

Bind any CLI tool, TUI dashboard, or AI assistant to a single-word command in ~/.local/bin/:

# Create `~/.local/bin/monitor` that always snaps to a persistent btop session
snap link monitor btop

# Now from any SSH client, simply type:
monitor

Generated shortcuts also inherit lifecycle subcommands automatically:

monitor status          # Inspect PID, window count, and attached client telemetry
monitor close --delay 3 # Arm SIGHUP-safe 3-second delayed session teardown
monitor reset           # Kill and restart a fresh instance

3. Machine-Readable JSON Telemetry & Schema Introspection

Every operation in snap_ctl.py supports --json for integration with system orchestrators and autonomous agents:

$ snap status --session dev --json
{
  "timestamp": "2026-10-17T18:00:00.000000+00:00",
  "origin": "workstation-node",
  "module": "snaptty",
  "action": "status",
  "status": "SUCCESS",
  "exit_code": 0,
  "telemetry": {
    "session_exists": true,
    "session_name": "dev",
    "attached_clients": 1,
    "windows": 1,
    "pane_pid": 41892,
    "pane_command": "bash"
  },
  "diagnostics": {
    "error_code": null,
    "message": "Session 'dev' active with 1 client(s)."
  }
}

4. Run the Verification Test Suite

python3 -m unittest discover -s tests -v

5. License

Released under the MIT License. Engineered by Bill Fullen (@Tethtibis).