Seagull fries (haioushutiao, Discord #help; magpie 0.1.2704, Claude Code
2.1.316, Windows 11, Claude Pro): the Claude card said 暂时无法获取额度,
"Claude Code's /usage told no allowance: ❯ /usage ─── Settin s Status
Config Usage …". Reproduced with a real Claude Code 2.1.316 under
Windows' pseudo console in a sandbox: /usage's panel is drawn first with
"Loading usage data…" while Claude Code asks Anthropic for the figures
(5s timeout, retried only on a 401), and the helper took 3s of a still
screen as the panel drawn whole. On a slow answer it told the panel's
frame as the result, and parseClaudeUsage called any text with no window
"told no allowance". A failed load ("Error: Failed to load usage data",
"r to retry") went the same way.
claudetui:
- screen.go: a small terminal grid (CUP/CUU..CHA/VPA, EL, ED, ECH, CR/LF
with scrolling, OSC/DCS skipped, an escape or UTF-8 character cut
between reads kept), 200x50 on Windows as the pseudo console is made,
80x24 elsewhere. The pseudo console sends only the cells that changed,
skipping a cell that holds the same letter ("Settin\x1b[1Cs"), so the
stream read split words and kept a redrawn digit beside the old one.
While /usage is read, each look of the screen with a window is kept
(panel), and usageText reads that before the stream.
- command(), built on b4073c06a's wait for the panel (ikunbots; its
usageDrawn and "hadn't drawn" error kept, now also judged on the grid,
since the pseudo console's split "Settin s" never matched its tabs):
"Loading usage data" on screen is waited out (to
commandWait, s.doing "was still loading /usage's figures" for the
deadline message); "Failed to load usage data" gets one "r"; a panel
still with no figure and loading or failed on screen is an
error_during_execution result, "/usage couldn't load the plan's usage:
<screen>".
- the fake draws the panel a frame at a time (fakeFrames), as Claude Code
does: with one write, the pseudo console passed on only the last
frame, which no longer shows the windows. TestShimReadsUsage now passes
on Windows (#4820, the fake's side; a real panel with figures was not
seen on Windows, see below).
provider:
- parseClaudeUsage: a panel with no figure is "Claude Code's /usage
showed no usage figures: …", not "told no allowance" (kept for
claudeUsageDenied's refusals).
- claudeUsageUnknown: Claude Code's "temporarily unavailable" and the
helper's "couldn't load the plan's usage" keep the account's reported
reading (quota_last), as the first did alone before. Other no-figure
text is still shown as an error (TestClaudeHeaderSurvivesUnclassified-
UsageFailure, TestClaudeUsageReadingAloneDoesNotHideUnclassifiedFailure
hold that).
Fixtures are the reporter's case as the pseudo console passed it on for
the real 2.1.316 (a temporary dump of the helper's screen bytes, sandbox config,
proxy that never answers / refuses): testdata/usage-loading-win-2.1.316.bin
and usage-failed-win-2.1.316.bin.
Run at the pushed head:
- mote: go test ./internal/claudetui ./internal/provider ok; go test
-run 'Claude|Usage|TUI' ./internal/gateway ok; GOOS=darwin/linux/
windows go vet ./internal/claudetui ./internal/provider ok; gofmt -l
clean.
- xiaomi-gpu (win-run): go test ./internal/claudetui: all pass except
TestShimFirstMessageWaitsForTools, red on Windows at main (#4810).
- xiaomi-gpu, real Claude Code 2.1.316 (npm), sandbox CLAUDE_CONFIG_DIR
with a fake token, magpie.exe built from this tree, helper run as the
gateway runs it: a proxy that never answers gives is_error "/usage
couldn't load the plan's usage: … Settings Status … Loading usage data…"
after the 20s wait (was a success with the chrome); a refused proxy
shows "Failed to load usage data", "r" is pressed, and the same error
comes back in 6.6s. "Settings" is whole now.
Broken by hand (before the rebase onto b4073c06a; the merged loop is
re-checked below):
- old tui.go: TestShimWaitsForUsageFigures slow, retried and failed again
all red, each telling the reporter's chrome as success.
- the "r" not written: TestShimWaitsForUsageFigures/retried red.
- old parse message: TestClaudeUsageWithoutFiguresIsUnknown red ("told
no allowance"); old quota_last check: its couldn't-load cases red.
- after the rebase, on the merged loop: the "r" not written,
TestShimWaitsForUsageFigures/retried red; the old "anything still 3s"
in place of usageDrawn on the grid, TestShimWaitsPastTheBanner red.
Not tried: a real account (the box has none of the owner's), so a real
2.1.316 panel with figures was never seen through the pseudo console;
whether Claude Code's frames that show the windows reach the pseudo
console's output before it renders the next one is shown only by the
fake. Not taken: asking Anthropic's /api/oauth/usage, which the owner
ruled out (9bf6d4352).
258 lines
5.3 KiB
Go
258 lines
5.3 KiB
Go
package claudetui
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"golang.org/x/text/width"
|
|
)
|
|
|
|
// grid is Claude Code's screen as a terminal shows it: what it draws is
|
|
// put in its cells, where the cursor is moved to. Read as a stream, with
|
|
// its escapes taken out (plain), a screen drawn again only where it
|
|
// changed is misread: Windows' pseudo console sends only the cells that
|
|
// changed, a cell left as it was skipped by a move of the cursor
|
|
// ("Settin\x1b[1Cs" for "Settings", whose g was there already), and a
|
|
// number redrawn in place is a digit or two after the old one, which the
|
|
// stream still holds. The grid holds the screen as it is.
|
|
type grid struct {
|
|
cells [][]rune
|
|
cols, rows int
|
|
row, col int
|
|
// pending is the start of an escape or a character a read ended in
|
|
// the middle of
|
|
pending []byte
|
|
}
|
|
|
|
func newGrid(cols, rows int) *grid {
|
|
g := &grid{cols: cols, rows: rows}
|
|
g.cells = make([][]rune, rows)
|
|
for i := range g.cells {
|
|
g.cells[i] = blankRow(cols)
|
|
}
|
|
return g
|
|
}
|
|
|
|
func blankRow(cols int) []rune {
|
|
r := make([]rune, cols)
|
|
for i := range r {
|
|
r[i] = ' '
|
|
}
|
|
return r
|
|
}
|
|
|
|
// text is the screen, a line a row, each without its trailing spaces.
|
|
func (g *grid) text() string {
|
|
var b strings.Builder
|
|
for i, r := range g.cells {
|
|
if i > 0 {
|
|
b.WriteByte('\n')
|
|
}
|
|
var line []rune
|
|
for _, c := range r {
|
|
if c != 0 { // the second cell of a wide character
|
|
line = append(line, c)
|
|
}
|
|
}
|
|
b.WriteString(strings.TrimRight(string(line), " "))
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (g *grid) clamp() {
|
|
g.row = min(max(g.row, 0), g.rows-1)
|
|
g.col = min(max(g.col, 0), g.cols)
|
|
}
|
|
|
|
func (g *grid) lineFeed() {
|
|
if g.row < g.rows-1 {
|
|
g.row++
|
|
return
|
|
}
|
|
copy(g.cells, g.cells[1:])
|
|
g.cells[g.rows-1] = blankRow(g.cols)
|
|
}
|
|
|
|
func (g *grid) put(r rune) {
|
|
w := 1
|
|
if k := width.LookupRune(r).Kind(); k == width.EastAsianWide || k == width.EastAsianFullwidth {
|
|
w = 2
|
|
}
|
|
if g.col+w > g.cols {
|
|
g.col = 0
|
|
g.lineFeed()
|
|
}
|
|
g.cells[g.row][g.col] = r
|
|
if w == 2 && g.col+1 < g.cols {
|
|
g.cells[g.row][g.col+1] = 0
|
|
}
|
|
g.col += w
|
|
}
|
|
|
|
// erase blanks the cells from..to (not included) of row.
|
|
func (g *grid) erase(row, from, to int) {
|
|
for i := max(from, 0); i < min(to, g.cols); i++ {
|
|
g.cells[row][i] = ' '
|
|
}
|
|
}
|
|
|
|
// write draws b. An escape or character cut off at its end is kept for
|
|
// the next write.
|
|
func (g *grid) write(b []byte) {
|
|
if len(g.pending) > 0 {
|
|
b = append(g.pending, b...)
|
|
g.pending = nil
|
|
}
|
|
for i := 0; i < len(b); {
|
|
c := b[i]
|
|
switch {
|
|
case c == 0x1b:
|
|
n := g.escape(b[i:])
|
|
if n == 0 { // cut off
|
|
if len(b)-i < 4096 {
|
|
g.pending = append([]byte(nil), b[i:]...)
|
|
}
|
|
return
|
|
}
|
|
i += n
|
|
continue
|
|
case c == '\r':
|
|
g.col = 0
|
|
case c == '\n', c == '\v', c == '\f':
|
|
g.lineFeed()
|
|
case c == '\b':
|
|
g.col = max(g.col-1, 0)
|
|
case c == '\t':
|
|
g.col = min((g.col/8+1)*8, g.cols-1)
|
|
case c < 0x20 || c == 0x7f:
|
|
default:
|
|
if !utf8.FullRune(b[i:]) {
|
|
g.pending = append([]byte(nil), b[i:]...)
|
|
return
|
|
}
|
|
r, n := utf8.DecodeRune(b[i:])
|
|
g.put(r)
|
|
i += n
|
|
continue
|
|
}
|
|
i++
|
|
}
|
|
}
|
|
|
|
// escape acts on the escape at the start of b and is its length, 0 when
|
|
// b ends before it does.
|
|
func (g *grid) escape(b []byte) int {
|
|
if len(b) < 2 {
|
|
return 0
|
|
}
|
|
switch b[1] {
|
|
case '[':
|
|
i := 2
|
|
for i < len(b) && b[i] >= 0x20 && b[i] <= 0x3f {
|
|
i++
|
|
}
|
|
params := string(b[2:i])
|
|
for i < len(b) && b[i] >= 0x20 && b[i] <= 0x2f {
|
|
i++
|
|
}
|
|
if i >= len(b) {
|
|
return 0
|
|
}
|
|
g.csi(params, b[i])
|
|
return i + 1
|
|
case ']', 'P', '^', '_':
|
|
// a string, ended by BEL (an OSC's) or ST
|
|
for i := 2; i < len(b); i++ {
|
|
if b[i] == 0x07 && b[1] == ']' {
|
|
return i + 1
|
|
}
|
|
if b[i] == 0x1b && i+1 < len(b) && b[i+1] == '\\' {
|
|
return i + 2
|
|
}
|
|
}
|
|
return 0
|
|
case '(', ')', '*', '+', '#', '%':
|
|
if len(b) < 3 {
|
|
return 0
|
|
}
|
|
return 3
|
|
case 'D':
|
|
g.lineFeed()
|
|
case 'E':
|
|
g.col = 0
|
|
g.lineFeed()
|
|
case 'M':
|
|
g.row = max(g.row-1, 0)
|
|
}
|
|
return 2
|
|
}
|
|
|
|
func (g *grid) csi(params string, final byte) {
|
|
if strings.HasPrefix(params, "?") || strings.HasPrefix(params, ">") || strings.HasPrefix(params, "<") || strings.HasPrefix(params, "=") {
|
|
return // modes, keyboard protocols: nothing drawn
|
|
}
|
|
var ps []int
|
|
for _, p := range strings.Split(params, ";") {
|
|
n, _ := strconv.Atoi(p)
|
|
ps = append(ps, n)
|
|
}
|
|
arg := func(i, def int) int {
|
|
if i < len(ps) && ps[i] > 0 {
|
|
return ps[i]
|
|
}
|
|
return def
|
|
}
|
|
switch final {
|
|
case 'A':
|
|
g.row -= arg(0, 1)
|
|
case 'B', 'e':
|
|
g.row += arg(0, 1)
|
|
case 'C', 'a':
|
|
g.col += arg(0, 1)
|
|
case 'D':
|
|
g.col -= arg(0, 1)
|
|
case 'E':
|
|
g.row, g.col = g.row+arg(0, 1), 0
|
|
case 'F':
|
|
g.row, g.col = g.row-arg(0, 1), 0
|
|
case 'G', '`':
|
|
g.col = arg(0, 1) - 1
|
|
case 'd':
|
|
g.row = arg(0, 1) - 1
|
|
case 'H', 'f':
|
|
g.row, g.col = arg(0, 1)-1, arg(1, 1)-1
|
|
case 'K':
|
|
if g.row >= 0 && g.row < g.rows {
|
|
switch arg(0, 0) {
|
|
case 0:
|
|
g.erase(g.row, g.col, g.cols)
|
|
case 1:
|
|
g.erase(g.row, 0, g.col+1)
|
|
case 2:
|
|
g.erase(g.row, 0, g.cols)
|
|
}
|
|
}
|
|
case 'J':
|
|
switch arg(0, 0) {
|
|
case 0:
|
|
g.erase(g.row, g.col, g.cols)
|
|
for r := g.row + 1; r < g.rows; r++ {
|
|
g.erase(r, 0, g.cols)
|
|
}
|
|
case 1:
|
|
for r := 0; r < g.row; r++ {
|
|
g.erase(r, 0, g.cols)
|
|
}
|
|
g.erase(g.row, 0, g.col+1)
|
|
case 2, 3:
|
|
for r := range g.rows {
|
|
g.erase(r, 0, g.cols)
|
|
}
|
|
}
|
|
case 'X':
|
|
g.erase(g.row, g.col, g.col+arg(0, 1))
|
|
}
|
|
g.clamp()
|
|
}
|