ssh into a VM, but the VM is a fresh declaw
sandbox created on demand.
What you’ll learn
- Putting your local TTY in raw mode so keystrokes flow without buffering
- Installing a
SIGWINCHhandler that propagates window resizes to the sandbox - Writing output bytes to local stdout via the
on_datacallback - Detecting
Ctrl-Dlocally and sending a cleanexit\n
Prerequisites
Code
Running it
Drop the snippet intopty_live.py and run it in an interactive
terminal (iTerm2, Alacritty, Kitty, Terminal.app, tmux — any real TTY
works):
bash prompt inside a fresh sandbox. Try:
ls /etc/os-release— reads a filehtop— full-screen TUI, quit withqvim /tmp/x.txt— editor loads,:wqto savestty size— prints cols rows matching your local terminal- Resize your window —
stty sizeupdates on the next probe Ctrl-D— closes the session cleanly
How it works
Sandbox.create()— new microVM, 300s default lifetime.sbx.pty.create(on_data=...)— spawns a realbash -linside the VM, opens an SSE stream, and invokeson_datawith every chunk.- Raw-mode local TTY —
tty.setraw(fd)disables line buffering and local echo so each keystroke reaches the script immediately. The remote bash echoes for us. os.read(fd, 1024)— pulls bytes the user typed. We forward them untouched withhandle.send_stdin(data).SIGWINCHhandler — callshandle.resize(...)with the new local size, which firesTIOCSWINSZinside the sandbox;htopand friends redraw for the new dimensions.Ctrl-D(EOT,\x04) — we intercept locally and sendexit\nso the remote shell exits cleanly instead of sending raw EOT and confusing bash.handle.wait()— blocks until the PTY stream emits itsexitframe, returns aPtyResult.
See also
- PTY feature overview — why and when to use a PTY.
- Python SDK: PTY reference —
create,connect,PtyHandle,PtyResult. - TypeScript SDK: PTY reference — same flow from a Node.js CLI.