mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
Seven sessions. It was the first line of screen.feed.
r := []rune(s.pending + in)
pending held a trailing partial ESCAPE and never a trailing partial RUNE, and
[]rune turns the orphaned bytes of a split multi-byte character into U+FFFD
with no way back. The agent's output arrives from a pty read, which splits
wherever it likes, and Claude Code's interface is full of multi-byte glyphs --
its full-width rule is U+2500, three bytes at a time. Split one and a single
cell becomes THREE: every cell after it on the row shifts two columns, and the
overflow wraps onto the row below. That is the merge.
The terminal never saw any of it. It gets the exact bytes and draws them
correctly, which is exactly why his parallel session with no xscapes stayed
clean, and why the live screen is clean. Only the MODEL was wrong -- and
Host.mirror writes the model's rows into the terminal's scrollback, which is
the one place he has ever seen this. Every screenshot in seven sessions was of
scrolled-back content, and nobody asked why.
His test is what found it. Bare Claude at the same window height stayed clean,
which eliminated the agent, and eliminated height, and left only something we
do to bytes nobody else touches.
Four hypotheses died first and all four are recorded as dead, because each cost
a session: Rebind's ESC[NS (a bare SU keeps zero rows), an off-by-one between
the scroll region and the pty (34<->34, 21<->21, 29<->29, 33<->33), the shared
cursor-save slot (1054053 of 1054059 saves are ours; the agent never uses it),
and our band paint disturbing the agent (966 paints replayed, every one
invisible to it). The one that found it was feeding the same bytes in different
chunk sizes and getting different screens.
The fix holds back an incomplete trailing sequence the way pending already held
a partial escape. The two cannot both be pending -- an escape's final byte is
ASCII, so a partial rune is never inside one -- but they are appended rather
than assigned so that stays true by construction.
Tested across every split point of a string, across every pair of split points
with escapes between the runes, and checked to FAIL without the fix.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015GQLYhGnSpasru9xeJbJry
81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package host
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A multi-byte rune split across two writes must survive whole.
|
|
//
|
|
// ⭐ This is the strikethrough he reported through seven sessions, and it was
|
|
// ours. The agent's output comes off a pty read, which splits wherever it
|
|
// likes, and Claude Code's interface is full of multi-byte glyphs -- its
|
|
// full-width rule is U+2500, three bytes at a time. Split one and []rune turns
|
|
// the orphaned bytes into U+FFFD: a single cell becomes THREE, every cell after
|
|
// it shifts two columns, and the overflow wraps onto the row below.
|
|
//
|
|
// The terminal never sees any of it. It gets the exact bytes and draws them
|
|
// correctly, which is why a bare agent is clean and why the live screen is
|
|
// clean. Only the MODEL was wrong -- and Host.mirror writes the model's rows
|
|
// into the terminal's scrollback, which is the one place he ever saw it.
|
|
func TestASplitRuneSurvivesTheWriteBoundary(t *testing.T) {
|
|
const whole = "──── done ────"
|
|
want := func() string {
|
|
s := newScreen(40, 4)
|
|
s.feed("\x1b[?47h\x1b[1;1H" + whole)
|
|
return s.rowAt(0)
|
|
}()
|
|
if want != whole {
|
|
t.Fatalf("unsplit baseline is already wrong: %q", want)
|
|
}
|
|
b := []byte(whole)
|
|
for cut := 1; cut < len(b); cut++ {
|
|
s := newScreen(40, 4)
|
|
s.feed("\x1b[?47h\x1b[1;1H")
|
|
s.feed(string(b[:cut]))
|
|
s.feed(string(b[cut:]))
|
|
if got := s.rowAt(0); got != want {
|
|
t.Errorf("split after %d of %d bytes -> %q, want %q", cut, len(b), got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Three-way splits, and a rune split with an escape sequence either side of it,
|
|
// because pending has to carry a rune and an escape without losing either.
|
|
func TestSplitRunesAndEscapesTogether(t *testing.T) {
|
|
in := "\x1b[1;1H─\x1b[38;5;210m─\x1b[0m─X"
|
|
full := func() string {
|
|
s := newScreen(20, 3)
|
|
s.feed("\x1b[?47h" + in)
|
|
return s.rowAt(0)
|
|
}()
|
|
if !strings.HasPrefix(full, "───X") {
|
|
t.Fatalf("baseline %q, want to start ───X", full)
|
|
}
|
|
b := []byte(in)
|
|
for i := 1; i < len(b)-1; i++ {
|
|
for j := i + 1; j < len(b); j++ {
|
|
s := newScreen(20, 3)
|
|
s.feed("\x1b[?47h")
|
|
s.feed(string(b[:i]))
|
|
s.feed(string(b[i:j]))
|
|
s.feed(string(b[j:]))
|
|
if got := s.rowAt(0); got != full {
|
|
t.Fatalf("split at %d,%d -> %q, want %q", i, j, got, full)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// A lone trailing ESC still waits for its sequence: the rune fix must not have
|
|
// broken what pending already did.
|
|
func TestATrailingEscapeStillWaits(t *testing.T) {
|
|
s := newScreen(20, 3)
|
|
s.feed("\x1b[?47h\x1b[1;1HAB")
|
|
s.feed("\x1b")
|
|
s.feed("[1;1HCD")
|
|
if got := s.rowAt(0); got != "CD" {
|
|
t.Errorf("row %q, want CD -- a split escape was not held", got)
|
|
}
|
|
}
|