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

64 lines
1.9 KiB
Go

package host
import (
"os"
"syscall"
"unsafe"
)
// pty is a pseudo-terminal pair. The host writes the agent's input to the
// master and reads its output from it; the agent gets the slave as its
// controlling terminal and cannot tell it apart from a real one.
//
// Opened with raw ioctls rather than a dependency, so the project stays on the
// standard library. Everything needed is already in package syscall on both
// platforms it runs on.
type pty struct{ master, slave *os.File }
// SetSize tells the child how big its window is.
//
// This is the hinge of the whole design: the child is sized to the band rather
// than the window, so Claude Code lays itself out inside the band without
// being asked to, and its text can never collide with the scape below. The
// kernel sends the child a SIGWINCH, and Claude repaints from the top -- the
// one moment it uses absolute cursor positioning, which is why the host also
// turns on origin mode.
func (p *pty) SetSize(cols, rows int) error {
ws := struct{ rows, cols, x, y uint16 }{uint16(rows), uint16(cols), 0, 0}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, p.master.Fd(),
uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(&ws)))
if errno != 0 {
return errno
}
return nil
}
func (p *pty) Close() error {
if p.slave != nil {
p.slave.Close()
}
if p.master != nil {
return p.master.Close()
}
return nil
}
// ptySize reads a terminal's size back out of the kernel, for the tests and
// for the resize loop.
func ptySize(fd uintptr) (cols, rows int, ok bool) {
var ws struct{ rows, cols, x, y uint16 }
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd,
uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&ws)))
if errno != 0 {
return 0, 0, false
}
return int(ws.cols), int(ws.rows), true
}
func ioctl(fd, req, arg uintptr) error {
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, req, arg); errno != 0 {
return errno
}
return nil
}