Files
donlucas 610c8909e9 Host the agent inside the scape: a PTY band pinned by the scroll region
The agent runs in the top rows of one window and the scape paints the rows
below it. Not a terminal emulator: the agent's bytes reach the terminal
untouched but for the one three-byte sequence that would free it from its
band, so nothing the host fails to understand about Claude Code's output can
be corrupted by it.

internal/host carries the pieces, each measured rather than assumed:
- Filter removes DECSTBM from the child's stream. Claude emits ESC[r once at
  startup, which resets the region to the whole screen and would let its next
  scroll walk over the scape. Handles the sequence arriving split across reads.
- Band splits the window. The agent's rows must start at row 1 -- lines
  scrolled out of a region reach the scrollback only when the region is
  top-anchored, measured in Terminal.app: rows 1-10 keeps every line, rows
  5-14 keeps none. So the scape reads downward from the agent instead of
  upward from the bottom.
- EnterBand/BeginPaint/EndPaint carry the ordering rules. The cursor is saved
  before the region is touched because DECSTBM homes it; origin mode is on so
  the agent's resize-time ESC[H lands in the band rather than on the screen.
- openPTY and makeRaw, on stdlib syscalls, so the project keeps its no-
  dependency rule. Darwin and Linux.

main gains 'xscapes inside [command]'. frames.go extracts the frame producer
that the live pane and the band now share, so the two cannot drift.

The old tmux launcher is untouched and still works.
2026-09-01 12:09:39 -07:00

65 lines
1.9 KiB
Go

package host
import (
"syscall"
"unsafe"
)
// Raw mode on the host's own terminal.
//
// The host is a pass-through: every byte the user types goes to the agent
// exactly as typed. That means no echo (the agent draws its own input line),
// no line buffering (Claude reacts per keystroke), and no signal generation --
// Ctrl-C belongs to Claude Code, which uses it to interrupt a turn, and a host
// that turned it into a SIGINT would kill the scape instead.
//
// The terminal's replies to the agent's startup queries (device attributes,
// the kitty keyboard protocol, synchronized output) arrive on the same path,
// so it has to stay byte-transparent in both directions.
const (
lflagEcho = syscall.ECHO
lflagCanon = syscall.ICANON
lflagISig = syscall.ISIG
)
func readTermios(fd uintptr) (*syscall.Termios, error) {
var t syscall.Termios
if err := ioctl(fd, ioctlReadTermios, uintptr(unsafe.Pointer(&t))); err != nil {
return nil, err
}
return &t, nil
}
func writeTermios(fd uintptr, t *syscall.Termios) error {
return ioctl(fd, ioctlWriteTermios, uintptr(unsafe.Pointer(t)))
}
// makeRaw switches the terminal to raw mode and returns what it was, for
// restoreTermios. The flag set is the classic cfmakeraw one.
func makeRaw(fd uintptr) (*syscall.Termios, error) {
old, err := readTermios(fd)
if err != nil {
return nil, err
}
raw := *old
raw.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP |
syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON
raw.Oflag &^= syscall.OPOST
raw.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN
raw.Cflag &^= syscall.CSIZE | syscall.PARENB
raw.Cflag |= syscall.CS8
raw.Cc[syscall.VMIN] = 1
raw.Cc[syscall.VTIME] = 0
if err := writeTermios(fd, &raw); err != nil {
return nil, err
}
return old, nil
}
func restoreTermios(fd uintptr, t *syscall.Termios) error {
if t == nil {
return nil
}
return writeTermios(fd, t)
}