claudetui: /usage's screen is read at the terminal's own 200 by 50 on macOS and Linux (#4988, shinnqy on X)
shinnqy on X: in the latest magpie Claude Code's usage doesn't load.
#4988 (0.1.2776, macOS, Team plan): the card said 100% / unused, and
`magpie quota refresh claude` gave one window, "7 days · Fable" 0%, while
the account's 5 hours were 29% and 7 days 17% used.
Cause: 3a74cd92e reads /usage's panel off a terminal grid (screen.go) and
prefers that look of it over the stream. screenSize() made the grid 80 by
24 everywhere but Windows, saying "a pty here is given no size", but
child_unix.go makes the pseudo-terminal 200 by 50. Claude Code draws the
panel at the foot of its 50 rows; on the 80 by 24 grid every row under the
24th was clamped onto the 24th and drawn over it, so the kept look of the
panel held one window (the last drawn), or a figure redrawn in place was
lost to the stream's older one.
Fix: one screenCols/screenRows (200 by 50) for the Unix pseudo-terminal,
Windows' pseudo console and the grid. Windows is unchanged.
Test: TestShimReadsUsageBelowTheTwentyFourthRow, a 200 by 50 panel with
three windows drawn as 2.1.316 draws (ESC[H, CR + ESC[nB, ESC[55G), then a
frame that redraws only the session's figure (29% -> 30%). fakeFrames now
splits frames at a 50-row foot (ESC[50;1H) as well as ESC[24;1H.
Run at this head (mote): gofmt -l internal/claudetui clean; go vet
./internal/claudetui with GOOS darwin, linux, windows ok; go test
./internal/claudetui ok; go test -run 'Claude|Usage|TUI' ./internal/gateway
./internal/provider ok. xiaomi-gpu (win-run): -run
'TestShimReadsUsage|TestGrid|TestShimWaits' ./internal/claudetui ok.
Broken by hand: the new test on origin/main's tui.go (80 by 24 grid) is
red, telling "Current session: 29% used" for the redrawn 30%; green with
the fix.
Not tried: a real Claude Code panel with figures at 200 by 50 on macOS
(no real-account run in a sandbox; the fixture is synthetic, in 2.1.316's
encoding). The report's "messages can't be sent" half is not explained by
this: nothing here touches the request path, and no log came with it.
This commit is contained in:
@@ -374,8 +374,10 @@ tested on the bytes Claude Code 2.1.316 drew, `testdata/usage-2.1.316.bin`),
|
||||
for `provider.parseClaudeUsage`. Claude Code 2.1.316's `-p /usage`
|
||||
printed only the session's cost there, no window, in a real run.
|
||||
The helper reads the screen as a terminal shows it (`grid`,
|
||||
[`screen.go`](../../internal/claudetui/screen.go), 200 by 50 on Windows
|
||||
as its pseudo console is made, 80 by 24 elsewhere), beside the stream:
|
||||
[`screen.go`](../../internal/claudetui/screen.go), 200 by 50, the size
|
||||
the pseudo-terminal and Windows' pseudo console are made, from one
|
||||
`screenCols`/`screenRows`; an 80 by 24 grid of the 200 by 50 Unix
|
||||
terminal drew the panel's rows over one another, #4988), beside the stream:
|
||||
Windows' pseudo console sends only the cells that changed, skipping one
|
||||
that holds the same letter ("Settin\x1b[1Cs"), and a window drawn only
|
||||
for a moment is kept as it looked (`panel`). The panel is drawn before
|
||||
|
||||
@@ -29,7 +29,7 @@ func (s *shim) start(path string, args []string) error {
|
||||
return err
|
||||
}
|
||||
// wide, so a line of Claude Code's screen is a line
|
||||
_ = unix.IoctlSetWinsize(int(tty.Fd()), unix.TIOCSWINSZ, &unix.Winsize{Row: 50, Col: 200})
|
||||
_ = unix.IoctlSetWinsize(int(tty.Fd()), unix.TIOCSWINSZ, &unix.Winsize{Row: screenRows, Col: screenCols})
|
||||
cmd := proc.Command(path, args...)
|
||||
cmd.Env = childEnv(s.env...)
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty
|
||||
|
||||
@@ -55,7 +55,7 @@ func (s *shim) start(path string, args []string) error {
|
||||
}
|
||||
var console windows.Handle
|
||||
// wide, so a line of Claude Code's screen is a line
|
||||
err := windows.CreatePseudoConsole(windows.Coord{X: 200, Y: 50}, inR, outW, 0, &console)
|
||||
err := windows.CreatePseudoConsole(windows.Coord{X: screenCols, Y: screenRows}, inR, outW, 0, &console)
|
||||
// the console has its own of these
|
||||
windows.CloseHandle(inR)
|
||||
windows.CloseHandle(outW)
|
||||
|
||||
+14
-10
@@ -30,7 +30,6 @@ import (
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -336,15 +335,20 @@ func (s *shim) clearScreen() {
|
||||
s.screenMu.Unlock()
|
||||
}
|
||||
|
||||
// screenSize is the size of Claude Code's terminal: Windows' pseudo
|
||||
// console is made 200 by 50 (child_windows.go); a pty here is given no
|
||||
// size, and Claude Code draws in 80 by 24.
|
||||
func screenSize() (cols, rows int) {
|
||||
if runtime.GOOS == "windows" {
|
||||
return 200, 50
|
||||
}
|
||||
return 80, 24
|
||||
}
|
||||
// screenCols and screenRows are the size of Claude Code's terminal, the
|
||||
// pseudo-terminal (child_unix.go) and Windows' pseudo console
|
||||
// (child_windows.go) made alike, and the grid the helper reads it on. A
|
||||
// grid of another size misreads the screen: an 80 by 24 one, on macOS and
|
||||
// Linux while their pseudo-terminal was 200 by 50, drew every row under
|
||||
// the 24th over the 24th, and /usage's panel, at the foot of the screen,
|
||||
// was told as one window or as a figure since drawn again (#4988, shinnqy
|
||||
// on X).
|
||||
const (
|
||||
screenCols = 200
|
||||
screenRows = 50
|
||||
)
|
||||
|
||||
func screenSize() (cols, rows int) { return screenCols, screenRows }
|
||||
|
||||
// looks is the screen as it is now, and the last look of it with a
|
||||
// usage window since it was cleared, while watched.
|
||||
|
||||
@@ -339,20 +339,26 @@ func fakeClaude(args []string) {
|
||||
}
|
||||
|
||||
// fakeFrames draws b a frame at a time, as Claude Code does: each frame
|
||||
// ends parking the cursor at the screen's foot (ESC[24;1H), and the next
|
||||
// comes a moment later. A terminal that renders what it is given (Windows'
|
||||
// pseudo console) passes on each frame, not only how the last one left
|
||||
// the screen.
|
||||
// ends parking the cursor at the screen's foot (ESC[24;1H in the 80 by 24
|
||||
// the captures were made in, ESC[50;1H in the helper's 200 by 50), and the
|
||||
// next comes a moment later. A terminal that renders what it is given
|
||||
// (Windows' pseudo console) passes on each frame, not only how the last
|
||||
// one left the screen.
|
||||
func fakeFrames(b []byte) {
|
||||
park := []byte("\x1b[24;1H")
|
||||
parks := [][]byte{[]byte("\x1b[24;1H"), []byte("\x1b[50;1H")}
|
||||
for len(b) > 0 {
|
||||
i := bytes.Index(b, park)
|
||||
i, n := -1, 0
|
||||
for _, park := range parks {
|
||||
if j := bytes.Index(b, park); j >= 0 && (i < 0 || j < i) {
|
||||
i, n = j, len(park)
|
||||
}
|
||||
}
|
||||
if i < 0 {
|
||||
_, _ = os.Stdout.Write(b)
|
||||
return
|
||||
}
|
||||
_, _ = os.Stdout.Write(b[:i+len(park)])
|
||||
b = b[i+len(park):]
|
||||
_, _ = os.Stdout.Write(b[:i+n])
|
||||
b = b[i+n:]
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -845,6 +851,40 @@ func TestShimReadsUsage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code draws in the terminal the helper gives it, 200 by 50 on
|
||||
// every system, so a panel with three windows sits below the 24th row.
|
||||
// Each is read: read on an 80 by 24 grid, the rows under the 24th were
|
||||
// drawn over one another, and only the last window drawn was told
|
||||
// (#4988, "7 days · Fable" alone on a Team plan; shinnqy on X).
|
||||
func TestShimReadsUsageBelowTheTwentyFourthRow(t *testing.T) {
|
||||
// as 2.1.316 draws: from the top, down by rows (CR, ESC[nB), each
|
||||
// figure placed at its column (ESC[55G)
|
||||
var b strings.Builder
|
||||
b.WriteString("\x1b[H\r\x1b[29B Settings Status Config Usage Stats\x1b[K")
|
||||
for _, w := range []struct{ name, used, reset string }{
|
||||
{"Current session", "29%", "Resets 2pm (Asia/Shanghai)"},
|
||||
{"Current week (all models)", "17%", "Resets Nov 8 at 4am (Asia/Shanghai)"},
|
||||
{"Current week (Fable)", "0%", "Resets Nov 8 at 4am (Asia/Shanghai)"},
|
||||
} {
|
||||
b.WriteString("\r\x1b[2B " + w.name + "\x1b[K")
|
||||
b.WriteString("\r\x1b[1B " + strings.Repeat("█", 10) + "\x1b[55G" + w.used + "\x1b[59Gused\x1b[K")
|
||||
b.WriteString("\r\x1b[1B " + w.reset + "\x1b[K")
|
||||
}
|
||||
b.WriteString("\r\x1b[2B Esc to cancel\x1b[K\x1b[50;1H")
|
||||
// a frame later the session's figure is drawn again, only the cells
|
||||
// that changed, as Claude Code's renderer does
|
||||
b.WriteString("\x1b[H\r\x1b[32B\x1b[55G30%\x1b[50;1H")
|
||||
panel := filepath.Join(t.TempDir(), "usage-200x50.bin")
|
||||
if err := os.WriteFile(panel, []byte(b.String()), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := runShim(t, []string{"FAKE_USAGE=" + panel}, append([]string{fakePath(t)}, pArgs...), []string{`raw:{"type":"command","command":"/usage"}`}, 2)
|
||||
want := "Current session: 30% used · resets 2pm (Asia/Shanghai)\nCurrent week (all models): 17% used · resets Nov 8 at 4am (Asia/Shanghai)\nCurrent week (Fable): 0% used · resets Nov 8 at 4am (Asia/Shanghai)"
|
||||
if r := out[1]; r["type"] != "result" || r["is_error"] != false || r["result"] != want {
|
||||
t.Fatalf("told %v", r)
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code draws its banner again before /usage's panel, and a slow
|
||||
// /usage leaves it still for seconds. It is waited past, never told as the
|
||||
// panel: read as one, the banner was an allowance's error (ikunbots on X,
|
||||
|
||||
Reference in New Issue
Block a user