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

56 lines
1.6 KiB
Go

package host
import "testing"
// The host must hand every keystroke to the agent untouched: no line editing,
// no echo, and no interpretation of Ctrl-C, which belongs to Claude Code and
// not to the scape wrapped around it. It must also put the terminal back --
// a host that exits raw leaves the user with a shell that does not echo.
func TestRawModeClearsEchoAndLineDiscipline(t *testing.T) {
p, err := openPTY()
if err != nil {
t.Fatalf("openPTY: %v", err)
}
defer p.Close()
fd := p.slave.Fd()
before, err := readTermios(fd)
if err != nil {
t.Fatalf("readTermios: %v", err)
}
if before.Lflag&lflagEcho == 0 {
t.Fatal("a fresh pty already has echo off; the test proves nothing")
}
saved, err := makeRaw(fd)
if err != nil {
t.Fatalf("makeRaw: %v", err)
}
got, err := readTermios(fd)
if err != nil {
t.Fatalf("readTermios: %v", err)
}
if got.Lflag&lflagEcho != 0 {
t.Error("echo still on in raw mode")
}
if got.Lflag&lflagCanon != 0 {
t.Error("canonical mode still on in raw mode")
}
if got.Lflag&lflagISig != 0 {
t.Error("signal generation still on: Ctrl-C would be eaten by the host, not passed to the agent")
}
if err := restoreTermios(fd, saved); err != nil {
t.Fatalf("restoreTermios: %v", err)
}
after, err := readTermios(fd)
if err != nil {
t.Fatalf("readTermios: %v", err)
}
if after.Lflag != before.Lflag || after.Iflag != before.Iflag || after.Oflag != before.Oflag {
t.Errorf("terminal not restored: lflag %x->%x, iflag %x->%x, oflag %x->%x",
before.Lflag, after.Lflag, before.Iflag, after.Iflag, before.Oflag, after.Oflag)
}
}