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

85 lines
2.0 KiB
Go

package host
import (
"bytes"
"io"
"os/exec"
"strings"
"syscall"
"testing"
"time"
)
func TestPTYCarriesAChildsOutput(t *testing.T) {
p, err := openPTY()
if err != nil {
t.Fatalf("openPTY: %v", err)
}
defer p.Close()
cmd := exec.Command("sh", "-c", "echo marker-9f3a")
cmd.Stdin, cmd.Stdout, cmd.Stderr = p.slave, p.slave, p.slave
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
if err := cmd.Start(); err != nil {
t.Fatalf("start: %v", err)
}
p.slave.Close() // the child owns it now; keeping it open hides EOF
defer cmd.Wait()
var got bytes.Buffer
done := make(chan struct{})
go func() {
io.Copy(&got, p.master)
close(done)
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("timed out reading the pty")
}
if !strings.Contains(got.String(), "marker-9f3a") {
t.Errorf("pty output %q does not contain the child's line", got.String())
}
}
// The whole design rests on the child believing the window is band-sized: size
// the pty to (rows - scape) and Claude lays itself out inside the band with no
// further help. If the size does not reach the child, nothing else works.
func TestPTYReportsTheSizeTheHostSets(t *testing.T) {
p, err := openPTY()
if err != nil {
t.Fatalf("openPTY: %v", err)
}
defer p.Close()
if err := p.SetSize(101, 21); err != nil {
t.Fatalf("SetSize: %v", err)
}
cols, rows, ok := ptySize(p.slave.Fd())
if !ok {
t.Fatal("could not read the size back from the slave side")
}
if cols != 101 || rows != 21 {
t.Errorf("child sees %dx%d, want 101x21", cols, rows)
}
}
func TestPTYResizeReachesTheChild(t *testing.T) {
p, err := openPTY()
if err != nil {
t.Fatalf("openPTY: %v", err)
}
defer p.Close()
if err := p.SetSize(80, 24); err != nil {
t.Fatalf("SetSize: %v", err)
}
if err := p.SetSize(120, 33); err != nil {
t.Fatalf("resize: %v", err)
}
cols, rows, _ := ptySize(p.slave.Fd())
if cols != 120 || rows != 33 {
t.Errorf("after resize the child sees %dx%d, want 120x33", cols, rows)
}
}