mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
His first run in Ghostty. The sun differed because Ghostty took the truecolor profile and painted the palette's raw blend, a flat tan, where Terminal.app's quantiser lands on peach over cream: DetectProfile now returns the 256 cube on every terminal and COLORTERM is not consulted; XSCAPES_COLOR=truecolor opts in. The layout broke on a resize because Ghostty 1.3.1's alternate screen, read from its source, keeps content at the top on a grow and moves the cursor with its row on a shrink, and restores DECRC verbatim, where Terminal.app is bottom-anchored with the cursor unmoved: host.Rules, picked by RulesFor on TERM_PROGRAM, keeps Terminal.app's sequences byte for byte and gives everything else no SU on a grow and RebindShrinkAltFollow on a shrink. Red first in ghostty_resize_test.go, Ghostty's rules in the screen model (resizeGhostty, screen.restoreAbsolute), eight geometries. notes/sunprobe renders the noon sun under both profiles; the screenshot pixels also showed Terminal.app's U+2580 glyph starting 5px below the cell top, the hairline at every half-block edge that Ghostty does not draw. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V8uiSPv8yHadvci8xGpjyK
437 lines
15 KiB
Go
437 lines
15 KiB
Go
package host
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// scapeRow is a recognisable full-width row: the row index repeated, so a stale
|
|
// row from a previous frame or a previous SIZE is obvious in the model.
|
|
func scapeRow(cols, i int, tag rune) string {
|
|
b := make([]rune, cols)
|
|
for x := range b {
|
|
b[x] = tag
|
|
}
|
|
label := fmt.Sprintf("%d", i)
|
|
copy(b, []rune(label))
|
|
return string(b)
|
|
}
|
|
|
|
// runHosted drives a real Host through a size change and returns the screen as
|
|
// the terminal would have it.
|
|
func runHosted(t *testing.T, w1, h1, w2, h2 int, mode string) *screen {
|
|
t.Helper()
|
|
return runHostedChild(t, w1, h1, w2, h2, mode, "sleep 0.8")
|
|
}
|
|
|
|
// runHostedChild is runHosted with the hosted command spelled out, so a test
|
|
// can put the terminal into a state -- a background colour left set, say --
|
|
// that the host then has to survive.
|
|
func runHostedChild(t *testing.T, w1, h1, w2, h2 int, mode, child string) *screen {
|
|
t.Helper()
|
|
return runHostedAlt(t, w1, h1, w2, h2, mode, child, false)
|
|
}
|
|
|
|
// runHostedAlt is runHostedChild with the screen buffer chosen, because the two
|
|
// resize DIFFERENTLY and the host has to know which one it is on.
|
|
func runHostedAlt(t *testing.T, w1, h1, w2, h2 int, mode, child string, alt bool) *screen {
|
|
t.Helper()
|
|
return runHostedOpts(t, w1, h1, w2, h2, mode, child, alt, false)
|
|
}
|
|
|
|
// runHostedOpts is runHostedAlt with the history mirror switchable. The
|
|
// snapshot's model keeps both buffers, so a test can read the mirrored rows
|
|
// out of its main buffer the way the terminal would hold them.
|
|
func runHostedOpts(t *testing.T, w1, h1, w2, h2 int, mode, child string, alt, history bool) *screen {
|
|
t.Helper()
|
|
pr, pw, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// In has to be a real tty: the host puts it in raw mode, and a pipe
|
|
// cannot do that. A pty's slave is a tty, which is exactly what the host
|
|
// will be handed in production.
|
|
tty, err := openPTY()
|
|
if err != nil {
|
|
t.Skipf("no pty available: %v", err)
|
|
}
|
|
defer tty.Close()
|
|
|
|
sc := newScreen(w1, h1)
|
|
var raw strings.Builder
|
|
nbytes := 0
|
|
var mu sync.Mutex
|
|
done := make(chan struct{})
|
|
answered := false
|
|
go func() {
|
|
defer close(done)
|
|
buf := make([]byte, 4096)
|
|
for {
|
|
n, err := pr.Read(buf)
|
|
if n > 0 {
|
|
mu.Lock()
|
|
nbytes += n
|
|
raw.Write(buf[:n])
|
|
sc.feed(string(buf[:n]))
|
|
// Play the terminal: the host asks where the shell's cursor
|
|
// is, and a terminal answers. Row 7, so a test can see the
|
|
// transcript begin there and not at the bottom.
|
|
if history && !answered && strings.Contains(raw.String(), "\x1b[6n") {
|
|
answered = true
|
|
tty.master.Write([]byte("\x1b[7;1R"))
|
|
}
|
|
mu.Unlock()
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
var sz struct {
|
|
sync.Mutex
|
|
w, h int
|
|
tag rune
|
|
}
|
|
sz.w, sz.h, sz.tag = w1, h1, 'A'
|
|
|
|
h := &Host{
|
|
Cmd: exec.Command("sh", "-c", child),
|
|
FPS: 60,
|
|
AltScreen: alt,
|
|
// The harness models Terminal.app's screen (resizeAlt and friends),
|
|
// so the host gets Terminal.app's rules; RulesFor does that in production.
|
|
Rules: AppleTerminalRules,
|
|
History: history,
|
|
In: tty.slave,
|
|
Out: pw,
|
|
Size: func() (int, int) {
|
|
sz.Lock()
|
|
defer sz.Unlock()
|
|
return sz.w, sz.h
|
|
},
|
|
Paint: func(cols, rows int) []string {
|
|
sz.Lock()
|
|
tag := sz.tag
|
|
sz.Unlock()
|
|
out := make([]string, rows)
|
|
for i := range out {
|
|
out[i] = scapeRow(cols, i, tag)
|
|
}
|
|
return out
|
|
},
|
|
}
|
|
// Snapshot BEFORE the host exits. Its close path hands the terminal back
|
|
// and blanks the band, so a screen inspected after Run() is legitimately
|
|
// empty -- which the first version of this test read as "the host paints
|
|
// nothing" and blamed the host for.
|
|
var snap *screen
|
|
snapAt := 600 * time.Millisecond
|
|
if history {
|
|
snapAt += 100 * time.Millisecond // the cursor query's round trip
|
|
}
|
|
go func() {
|
|
time.Sleep(snapAt)
|
|
mu.Lock()
|
|
snap = sc.clone()
|
|
mu.Unlock()
|
|
}()
|
|
|
|
go func() {
|
|
time.Sleep(snapAt / 2)
|
|
if w1 == w2 && h1 == h2 {
|
|
// No resize: a terminal does nothing, and neither may the model.
|
|
// Resizing it to the same size would reset its scroll region,
|
|
// which the host re-pins only on a real size change.
|
|
return
|
|
}
|
|
// The model has to learn about the resize too, the way a real terminal
|
|
// would: it keeps what fits and exposes blank rows where it grew.
|
|
mu.Lock()
|
|
switch mode {
|
|
case "scroll":
|
|
sc.resizeScrolling(w2, h2)
|
|
case "anchor":
|
|
sc.resizeAnchoredBottom(w2, h2)
|
|
case "alt":
|
|
sc.resizeAlt(w2, h2)
|
|
default:
|
|
sc.resize(w2, h2)
|
|
}
|
|
mu.Unlock()
|
|
sz.Lock()
|
|
sz.w, sz.h, sz.tag = w2, h2, 'B'
|
|
sz.Unlock()
|
|
}()
|
|
|
|
if err := h.Run(); err != nil && err != io.EOF {
|
|
t.Fatalf("host: %v", err)
|
|
}
|
|
pw.Close()
|
|
<-done
|
|
if snap == nil {
|
|
t.Fatal("no snapshot taken before the host exited")
|
|
}
|
|
sc = snap
|
|
if os.Getenv("DBG") != "" {
|
|
fmt.Printf("--- captured %d bytes; screen %dx%d ---\n", nbytes, sc.w, sc.h)
|
|
fmt.Printf("first 500 bytes: %q\n", trunc500(raw.String()))
|
|
for y := 0; y < sc.h; y++ {
|
|
if r := sc.rowAt(y); r != "" {
|
|
fmt.Printf("row %2d: %q\n", y, trunc(r))
|
|
}
|
|
}
|
|
}
|
|
return sc
|
|
}
|
|
|
|
// After a resize, every row of the scape's band must be the NEW frame. A row
|
|
// left over from before the resize is the defect he photographed: a fragment of
|
|
// the old scape sitting above the band, and a strip of it down the right edge.
|
|
func TestEveryBandRowIsRepaintedAfterAResize(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
w1, h1, w2, h2 int
|
|
mode string
|
|
}{
|
|
{"wider", 100, 40, 120, 40, ""},
|
|
{"narrower", 120, 40, 100, 40, ""},
|
|
{"taller", 100, 40, 100, 50, ""},
|
|
{"shorter", 100, 50, 100, 40, ""},
|
|
{"wider and shorter", 100, 50, 128, 44, ""},
|
|
{"his window", 120, 46, 128, 51, ""},
|
|
// The ones that matter: a terminal that keeps the BOTTOM when it
|
|
// shrinks, which is what Terminal.app does.
|
|
{"shorter, terminal scrolls", 100, 50, 100, 40, "scroll"},
|
|
{"his window, shrunk", 128, 51, 120, 44, "scroll"},
|
|
{"one row shorter", 128, 51, 128, 50, "scroll"},
|
|
// The terminal pushing content DOWN when it grows. The scape must
|
|
// still be right; the agent's own screen is not the host's to fix.
|
|
{"taller, terminal anchors to the bottom", 100, 40, 100, 50, "anchor"},
|
|
{"his window, grown", 120, 62, 120, 66, "anchor"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
sc := runHosted(t, tc.w1, tc.h1, tc.w2, tc.h2, tc.mode)
|
|
agent, scape := BandWith(tc.h2, 0)
|
|
if scape <= 0 {
|
|
t.Skip("no scape at this size")
|
|
}
|
|
for i := 0; i < scape; i++ {
|
|
row := agent + i // 0-based screen row
|
|
got := sc.rowAt(row)
|
|
want := strings.TrimRight(scapeRow(tc.w2, i, 'B'), " ")
|
|
if got != want {
|
|
t.Errorf("screen row %d (scape row %d of %d):\n got %q\n want %q",
|
|
row, i, scape, trunc(got), trunc(want))
|
|
}
|
|
}
|
|
// And nothing of the scape may be left ABOVE the band.
|
|
for row := 0; row < agent; row++ {
|
|
if r := sc.rowAt(row); strings.ContainsRune(r, 'A') || strings.ContainsRune(r, 'B') {
|
|
t.Errorf("screen row %d is inside the agent's band and still holds scape: %q",
|
|
row, trunc(r))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func trunc(s string) string {
|
|
if len(s) > 60 {
|
|
return s[:60] + "..."
|
|
}
|
|
return s
|
|
}
|
|
|
|
func trunc500(s string) string {
|
|
if len(s) > 500 {
|
|
return s[:500]
|
|
}
|
|
return s
|
|
}
|
|
|
|
// An erase does not write spaces. EL and ED fill with the CURRENT background
|
|
// (BCE), so any row the host clears while some other colour is in force comes
|
|
// out as a solid band of that colour.
|
|
//
|
|
// The host clears rows on a resize, and the resize is driven by a TIMER that
|
|
// fires whenever the window changed -- with no relation to where the agent is
|
|
// in its output. Claude Code paints backgrounds constantly (its input box, the
|
|
// context bar, selected text), so "no colour is set right now" is not something
|
|
// the host is entitled to assume. It has to state the background it wants.
|
|
//
|
|
// This is the class the old model could not see: it stored runes only, so a row
|
|
// filled with blue read as blank and the host was recorded innocent.
|
|
func TestResizeClearDoesNotPaintTheAgentsBandWithAStrayBackground(t *testing.T) {
|
|
// The child sets a background and leaves it set, which is the state the
|
|
// resize tick can legitimately land in.
|
|
sc := runHostedChild(t, 80, 40, 80, 30, "scroll", `printf '\033[44mheld'; sleep 0.8`)
|
|
|
|
agentRows, _ := Band(30)
|
|
if painted := sc.paintedRows(1, agentRows); len(painted) > 0 {
|
|
t.Errorf("the host erased %d row(s) of the agent's band into a stray background: rows %v\n"+
|
|
"an erase fills with the current background, so these are solid colour on the real terminal",
|
|
len(painted), painted)
|
|
}
|
|
}
|
|
|
|
// Same defect, the other resize direction. Growing clears a different range.
|
|
func TestGrowClearDoesNotPaintTheAgentsBandWithAStrayBackground(t *testing.T) {
|
|
sc := runHostedChild(t, 80, 30, 80, 46, "", `printf '\033[41mheld'; sleep 0.8`)
|
|
|
|
agentRows, _ := Band(46)
|
|
if painted := sc.paintedRows(1, agentRows); len(painted) > 0 {
|
|
t.Errorf("the host erased %d row(s) of the agent's band into a stray background: rows %v",
|
|
len(painted), painted)
|
|
}
|
|
}
|
|
|
|
// The positive control. With the model's background tracking working, a child
|
|
// that fills a row itself MUST be seen -- otherwise the two tests above pass
|
|
// because the model reports every row as default, which is the exact blindness
|
|
// they exist to remove.
|
|
func TestTheModelCanSeeABackgroundAtAll(t *testing.T) {
|
|
sc := newScreen(20, 4)
|
|
sc.feed("\x1b[44m\x1b[2;1H\x1b[2K")
|
|
if painted := sc.paintedRows(2, 2); len(painted) != 1 {
|
|
t.Fatalf("the model cannot see an erase-to-background; every other test here is blind. got %v", painted)
|
|
}
|
|
if painted := sc.paintedRows(1, 1); len(painted) != 0 {
|
|
t.Errorf("row 1 was not erased and must not be reported as painted: %v", painted)
|
|
}
|
|
// And a reset must actually clear it, or "painted" would just mean "erased".
|
|
sc.feed("\x1b[0m\x1b[2;1H\x1b[2K")
|
|
if painted := sc.paintedRows(2, 2); len(painted) != 0 {
|
|
t.Errorf("an erase after a reset still reads as painted: %v", painted)
|
|
}
|
|
}
|
|
|
|
// agentLines is a child that fills the band with numbered, recognisable rows,
|
|
// then holds the terminal open while the resize happens.
|
|
const agentLines = `i=1; while [ $i -le 24 ]; do printf 'AGENTLINE%02d\r\n' $i; i=$((i+1)); done; sleep 0.8`
|
|
|
|
// countAgentLines reports how many rows in [from,to] still hold the child's text.
|
|
func countAgentLines(sc *screen, from, to int) int {
|
|
n := 0
|
|
for y := from - 1; y <= to-1 && y < sc.h; y++ {
|
|
if y >= 0 && strings.Contains(sc.rowAt(y), "AGENTLINE") {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
// On an alternate-screen SHRINK the terminal pulls every row UP by the rows
|
|
// lost and the top ones are gone -- measured by eye with notes/contentprobe,
|
|
// 2026-09-03. The host cannot save those; they were destroyed before it ran.
|
|
//
|
|
// What it IS responsible for is the two halves of the aftermath: the scape
|
|
// slides up into the band and must be cleared, and the agent rows that SURVIVED
|
|
// the slide must not be blanked along with it. That is what `drop` computes,
|
|
// and it is why `drop` belongs on this screen after all -- it was made
|
|
// main-screen-only earlier the same day on the strength of a cursor reading
|
|
// that did not describe the content.
|
|
func TestAltScreenShrinkClearsTheScapeButKeepsWhatSurvived(t *testing.T) {
|
|
sc := runHostedAlt(t, 80, 44, 80, 22, "alt", agentLines, true)
|
|
|
|
newAgent, _ := Band(22)
|
|
|
|
// The scape rows carry a repeated tag; none may remain inside the band.
|
|
for row := 1; row <= newAgent; row++ {
|
|
if r := sc.rowAt(row - 1); strings.Count(r, "AAAA") > 0 || strings.Count(r, "BBBB") > 0 {
|
|
t.Errorf("row %d is inside the band and still holds scape: %q", row, r)
|
|
}
|
|
}
|
|
// And the agent rows the terminal did NOT destroy must still be there.
|
|
if got := countAgentLines(sc, 1, newAgent); got == 0 {
|
|
t.Error("every surviving row of the agent's text was blanked by the host")
|
|
} else {
|
|
t.Logf("%d agent row(s) survived the shrink and were left alone", got)
|
|
}
|
|
}
|
|
|
|
// The main screen is the control, and it must keep working: there a shrink
|
|
// really does slide the scape up into the band, and those rows must be cleared.
|
|
func TestMainScreenShrinkStillClearsTheScapeThatSlidUp(t *testing.T) {
|
|
sc := runHosted(t, 80, 44, 80, 22, "scroll")
|
|
|
|
agentRows, _ := Band(22)
|
|
for row := 1; row <= agentRows; row++ {
|
|
if r := sc.rowAt(row - 1); strings.ContainsRune(r, 'A') || strings.ContainsRune(r, 'B') {
|
|
t.Errorf("row %d is inside the agent's band and still holds scape: %q", row, r)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A GROW on the alternate screen. Measured 2026-09-03 with notes/anchorprobe:
|
|
// the alternate screen is ANCHORED TOP in BOTH directions -- on a grow the
|
|
// content stays exactly where it is and the blank rows appear at the BOTTOM.
|
|
// So after growing, the agent's text must still be at the top of the band,
|
|
// untouched, and the band is simply taller.
|
|
//
|
|
// He photographed the opposite: a fresh session's banner sitting at the BOTTOM
|
|
// of the band with everything above it blank, after nothing but a stretch.
|
|
func TestAltScreenGrowLeavesTheAgentsTextWhereItWas(t *testing.T) {
|
|
sc := runHostedAlt(t, 80, 30, 80, 59, "alt", agentLines, true)
|
|
|
|
newAgent, _ := Band(59)
|
|
oldAgent, _ := Band(30)
|
|
if got := countAgentLines(sc, 1, oldAgent); got == 0 {
|
|
t.Fatalf("no agent text anywhere in the old band (rows 1..%d) after a grow", oldAgent)
|
|
}
|
|
// Every line the child wrote must still be inside the band, not below it
|
|
// where the scape paints.
|
|
below := countAgentLines(sc, newAgent+1, sc.h)
|
|
if below > 0 {
|
|
t.Errorf("%d row(s) of the agent's text ended up BELOW the band (row >%d), where the scape paints over them",
|
|
below, newAgent)
|
|
}
|
|
t.Logf("agent rows in old band 1..%d: %d | in new band 1..%d: %d | below band: %d",
|
|
oldAgent, countAgentLines(sc, 1, oldAgent), newAgent, countAgentLines(sc, 1, newAgent), below)
|
|
}
|
|
|
|
// The defect he photographed, stated directly.
|
|
//
|
|
// Measured 2026-09-03 with notes/contentprobe: an alternate-screen GROW pushes
|
|
// the whole screen DOWN by the delta. So the agent's text slides out of the top
|
|
// of the band, and whatever crosses the band's bottom edge is painted over by
|
|
// the scape on the very next frame. A small grow leaves the startup banner
|
|
// stranded just above the scape; a big one leaves nothing at all.
|
|
//
|
|
// The host has to undo the push before it paints. It can: a scroll of the whole
|
|
// screen moves rows, and moving rows needs no model of the UI living in them.
|
|
func TestAltScreenGrowRestoresTheAgentsTextToTheTopOfTheBand(t *testing.T) {
|
|
sc := runHostedAlt(t, 80, 30, 80, 59, "alt", agentLines, true)
|
|
|
|
newAgent, _ := Band(59)
|
|
|
|
// The child writes AGENTLINE01 first, so it is the topmost row of its
|
|
// output and the one a downward push loses first.
|
|
top := 0
|
|
for y := 0; y < sc.h; y++ {
|
|
if strings.Contains(sc.rowAt(y), "AGENTLINE") {
|
|
top = y + 1
|
|
break
|
|
}
|
|
}
|
|
if top == 0 {
|
|
t.Fatal("none of the agent's text is on screen at all after a grow")
|
|
}
|
|
if top > 4 {
|
|
t.Errorf("the agent's first row is at screen row %d after a grow; the terminal pushed it down"+
|
|
" and the host did not undo it", top)
|
|
}
|
|
if below := countAgentLines(sc, newAgent+1, sc.h); below > 0 {
|
|
t.Errorf("%d row(s) of the agent's text are BELOW the band (row >%d), where the scape paints over them",
|
|
below, newAgent)
|
|
}
|
|
t.Logf("agent text starts at screen row %d; %d rows inside the band, %d below it",
|
|
top, countAgentLines(sc, 1, newAgent), countAgentLines(sc, newAgent+1, sc.h))
|
|
}
|