Files
xscapes/internal/host/mirror_hostscroll_test.go
donlucasandClaude Opus 5 731768ca15 The mirror follows scrolls, not resizes
The duplicated scrollback rows he has reported through six sessions. Located
in the trace on 2026-09-08 and now fixed at the source.

A SCROLL means content moved on and the row at the top is gone. A RESIZE means
only that the viewport changed. The mirror is a record of content, so it
follows the first and ignores the second. resizeScrolling handed its lost top
rows to the mirror on every shrink, and the agent repaints the whole page after
every resize -- 33 ESC[2K ESC[1B pairs then a full repaint, measured in his own
trace -- so the rows came straight back on screen and the next shrink kept them
again. Across thirteen resizes in one burst, counted inside the mirror's own
writes for lines written exactly once: "landed in THIS conversation" x5, "the
live-refresh build" x4, "right on the expected rate" x4.

This reverses TestAShrinkKeepsTheRowsTheTerminalDestroys, which asserted the
opposite deliberately. The terminal does destroy those rows; the agent does
not, and that measurement did not exist when the test was written. The old
guarantee is not lost either, which is what makes the reversal safe: content
that genuinely leaves is scrolled by the agent, and scrollUp() still keeps
every row that goes. The resize path was redundant with it for any agent that
repaints, and harmful for all of them.

The residue, and it is real: an agent that does not repaint after a resize
loses those rows from the mirrored scrollback. `xscapes inside <cmd>` can host
such a thing. Both the code and the test say so.

Scope: this is the duplication. The strikethrough merge is a different symptom
and is still open.

Two hypotheses died on the way, both killed by measurement rather than
argument. Rebind's ESC[NS was the obvious suspect and a bare SU keeps zero
rows. And the first search for duplicates in the mirror came back all zeros
because I had delimited mirror blocks as ?47h..?47l, which is backwards --
?47h ENTERS the alternate screen, so I was searching the scape-painting
periods. A zero from the wrong window looks exactly like a clean bill of
health; dumping a block before trusting the count is what caught it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015GQLYhGnSpasru9xeJbJry
2026-09-09 07:28:41 -07:00

71 lines
2.3 KiB
Go

package host
import (
"fmt"
"strings"
"testing"
)
// The host's OWN repositioning scroll must not be mirrored into scrollback.
//
// His 2026-09-08 traced session, thirteen resizes in one burst: the same
// paragraph landed in the terminal's scrollback three to six times over. The
// duplicates are in our own mirror writes, byte for byte, at different rows on
// different resizes.
//
// The loop: a resize makes Rebind emit ESC[NS to undo Terminal.app's grow-push;
// h.write feeds that to the screen model like any other byte; the model's
// scrollUp() keeps each row leaving the top, "gone for good" -- and then the
// agent repaints the whole page, so the same text is back on screen ready to be
// kept again on the next resize.
//
// scrollUp's premise is right for a scroll the AGENT causes and wrong for one
// the HOST emits: there the row is not leaving, it is being moved.
func TestAResizeIsNotMirrored(t *testing.T) {
setup := func() *screen {
s := newScreen(40, 12)
s.alt, s.capture = true, true
// Twelve rows of the agent's transcript.
var b strings.Builder
for i := 1; i <= 12; i++ {
b.WriteString(fmt.Sprintf("\x1b[%d;1Hagent row %d", i, i))
}
s.feed(b.String())
s.takeScrolled() // drain whatever the fill itself produced
return s
}
// Control: when the AGENT scrolls, the row leaving the top IS gone and must
// be kept. Without this the test could pass by never keeping anything.
t.Run("agent scroll is mirrored", func(t *testing.T) {
s := setup()
s.feed("\x1b[12;1H\n") // a newline on the last row scrolls the region
if got := s.takeScrolled(); len(got) == 0 {
t.Fatal("an agent scroll kept no rows -- the mirror would lose the transcript")
}
})
t.Run("a shrink is not mirrored", func(t *testing.T) {
s := setup()
s.resizeAlt(40, 8)
got := s.takeScrolled()
if len(got) > 0 {
var rows []string
for _, r := range got {
rows = append(rows, strings.TrimSpace(rowANSIPlain(r)))
}
t.Fatalf("a shrink kept %d rows for the mirror (%q) -- the agent repaints after a "+
"resize, so every shrink would mirror rows that are about to come straight back", len(got), rows)
}
})
}
// rowANSIPlain is the row's text without colour, for readable failures.
func rowANSIPlain(row []cell) string {
var b strings.Builder
for _, c := range row {
b.WriteRune(c.r)
}
return b.String()
}