claude: an interactive run takes many images between words (#5654)

Chapin2018's message of nineteen images between words (the issue's
script, Claude Code 2.1.319, claude-fable-5-1) ended the interactive
helper "after 13s (exit status 1)" and the client got a 502, while -p
answered. The cause is how the helper saw a paste taken. It looked for
"[Image #N]" and "[Pasted text #N" only in what Claude Code drew after
the paste. Once the prompt runs past a row, Claude Code draws it again
only where it changed and moves the cursor past cells that are already
right. After the 33rd piece the drawing held "\r\x1b[16C\x1b[1B3": one
digit of "[Image #33]". The helper waited out pasteWait (10s) and ended.
Text marks came out in part too ("[Pasted t xt #16"), so several word
pieces each sat out their own 10s before that. There is no limit in
Claude Code's TUI: the same paste is on its screen whole, so no -p
fallback is needed.

Each check now also reads the prompt as the 200x50 screen grid shows
it: the rows between its last two rules, words one space apart (a mark
can wrap onto the next row). The mark that ends it counts (lastMark),
an image's [Image #N] or a whole [Pasted text #N +k lines], numbered
past the piece before, and so do words the prompt ends with. The
stream check stays first.

Run:
- Real, macOS arm64, this Mac, sandbox HOME and CLAUDE_CONFIG_DIR
  (token by env, nothing rotated, DISABLE_AUTOUPDATER=1), the issue's
  script unchanged, --images 19, Claude Code 2.1.319:
  - origin/main a46190e1f: exit 1 after ~32s, stderr as reported.
    2.1.316 failed the same way. -p: success, 5.8s.
  - This change: exit 0 in 5.3 to 12.1s on 5 runs, subtype success.
    The transcript's user row has all 19 image blocks and the words in
    order, and the model answered. The script's answer_ok stays false
    because the helper's result line has no "result" text (-p's does).
    The gateway reads the assistant events, not that field.
- TestShimSeesPastesOnAPromptDrawnInPart feeds the real run's bytes
  (testdata/paste-images-2.1.319.bin, folder name replaced at the same
  length). Each of the 33 pastes must be seen taken.
- Broken by hand: drop the prompt checks from imageTaken and textTaken
  and it goes red on text 12, text 22 and image 33, the pieces whose
  marks were drawn in part.
- WSL magpie-desktop (Ubuntu 24.04 amd64, go1.26.8), because mote is
  down:
  - gofmt -l . is clean.
  - go vet -tags nogui ./... and GOOS=windows go vet ./... pass. Plain
    vet there lacks gtk4 headers.
  - go test ./internal/claudetui/ is ok (120s).
  - The darwin build used for the real runs above.

Not tried: Linux arm64 (the reporter's system) with a real account, the
gateway's /v1/messages path end to end with 19 images, and the full
suite (not run at push).

Refs #5654: kept open for the reporter to confirm.

Reported-by: Chapin2018
This commit is contained in:
yetone
2026-11-08 18:24:16 +08:00
parent f418edffc3
commit 8fa3c60f80
4 changed files with 166 additions and 16 deletions
@@ -202,7 +202,15 @@ talks to it as it talks to `-p`: stream-json in and out. The helper:
show or a `[Pasted text #N` with N past the paste before (`shown`):
images and pasted text are numbered together, and the prompt drawn
again shows the earlier ones (`TestShimWaitsForWordsNamingImages`,
`TestShimPastesImages`). The transcript holds real image blocks, and
`TestShimPastesImages`). Both are looked for in what was drawn since
the paste and at the end of the prompt as the screen shows it, the rows
between its last two rules (`prompt`, `imageTaken`, `textTaken`): a
prompt grown past a row is drawn again only where it changed, so the
drawing after a paste can hold one digit of its mark. With nineteen
images between words, 2.1.319 drew "3" of `[Image #33]`, the helper
waited out `pasteWait` and ended with exit status 1, a 502 to the
client (#5654, `TestShimSeesPastesOnAPromptDrawnInPart`, a real run's
bytes). The transcript holds real image blocks, and
Claude Code notes each one's path to the model (`[Image: source: …]`).
The folder is removed once its message is sent (Claude Code has read
the files by then), and when the helper ends, on SIGINT, SIGTERM and
File diff suppressed because one or more lines are too long
+99 -15
View File
@@ -1267,6 +1267,55 @@ func (s *shim) send(pieces []piece) error {
var imageShown = regexp.MustCompile(`\[Image #(\d+)\]`)
// promptRule is a row of the rules Claude Code draws over and under its
// prompt.
var promptRule = regexp.MustCompile(`^\s*─{20,}\s*$`)
// prompt is the prompt as the screen shows it, its words one space apart:
// the rows between the screen's last two rules, "" with no such rows. A
// prompt grown past a row is drawn again only where it changed, the cells
// already right skipped by a move of the cursor: what was drawn since a
// paste (plain) held "3" of the "[Image #33]" it took, and "[Pasted t xt
// #16" of a paste taken before (#5654, Claude Code 2.1.319). Its words
// wrap where the row ends, so a mark can start on one row and end on the
// next.
func (s *shim) prompt() string {
s.screenMu.Lock()
var t string
if s.grid != nil {
t = s.grid.text()
}
s.screenMu.Unlock()
rows := strings.Split(t, "\n")
end := -1
for i := len(rows) - 1; i >= 0; i-- {
if !promptRule.MatchString(rows[i]) {
continue
}
if end < 0 {
end = i
continue
}
return strings.Join(strings.Fields(strings.Join(rows[i+1:end], " ")), " ")
}
return ""
}
// lastMark is the number of the mark re matches at the end of the prompt
// as the screen shows it (prompt), 0 with none: the paste taken last, as
// a paste goes where the prompt's words end.
func lastMark(re *regexp.Regexp, prompt string) int {
m := re.FindAllStringSubmatchIndex(prompt, -1)
if len(m) == 0 || m[len(m)-1][1] != len(prompt) {
return 0
}
n, _ := strconv.Atoi(prompt[m[len(m)-1][2]:m[len(m)-1][3]])
return n
}
// textLast is Claude Code's whole mark of a long paste.
var textLast = regexp.MustCompile(`\[Pasted text #(\d+) \+\d+ lines\]`)
// path is where Claude Code reads a file of the helper's at path: one in
// WSL under the distro's mount of the drive.
func (s *shim) path(path string) string {
@@ -1278,7 +1327,8 @@ func (s *shim) path(path string) string {
// pasteImage pastes an image file's path, and waits for the prompt to show
// it taken as an image: one numbered past after, the message's paste
// before it (Claude Code numbers images and long pasted text alike). It
// before it (Claude Code numbers images and long pasted text alike), drawn
// since the paste or ending the prompt as the screen shows it (prompt). It
// returns the number shown.
func (s *shim) pasteImage(path string, after int) (int, error) {
s.clearScreen()
@@ -1287,10 +1337,8 @@ func (s *shim) pasteImage(path string, after int) (int, error) {
}
by := time.After(pasteWait)
for {
for _, m := range imageShown.FindAllStringSubmatch(s.plain(), -1) {
if n, _ := strconv.Atoi(m[1]); n > after {
return n, nil
}
if n := s.imageTaken(after); n > 0 {
return n, nil
}
select {
case <-s.changed:
@@ -1303,6 +1351,21 @@ func (s *shim) pasteImage(path string, after int) (int, error) {
}
}
// imageTaken is the number of the image the prompt shows taken, numbered
// past after: drawn since the paste, or ending the prompt as the screen
// shows it (prompt). 0 while it shows none.
func (s *shim) imageTaken(after int) int {
for _, m := range imageShown.FindAllStringSubmatch(s.plain(), -1) {
if n, _ := strconv.Atoi(m[1]); n > after {
return n
}
}
if n := lastMark(imageShown, s.prompt()); n > after {
return n
}
return 0
}
// shownWait is how long a paste of n bytes may take to show: Claude Code
// takes a long one in at about 100KB a second (a real 3MB paste showed
// "[Pasted text" after 35s), so pasteWait and a second for every 64KB.
@@ -1315,8 +1378,10 @@ var textShown = regexp.MustCompile(`\[Pasted text #(\d+)`)
// shown waits for the prompt to show pasted text: its last words, or
// Claude Code's "[Pasted text #<n>" for a long one, numbered past after,
// the message's paste before it: the prompt drawn again shows those
// before. It returns the number shown, or after where the words show
// as themselves. Text "" waits for nothing.
// before. They are looked for in what was drawn since the paste, and at
// the end of the prompt as the screen shows it (prompt). It returns the
// number shown, or after where the words show as themselves. Text ""
// waits for nothing.
func (s *shim) shown(text string, after int) (int, error) {
if text == "" {
return after, nil
@@ -1330,14 +1395,8 @@ func (s *shim) shown(text string, after int) (int, error) {
}
shownBy := time.After(shownWait(len(text)))
for {
p := s.plain()
for _, m := range textShown.FindAllStringSubmatch(p, -1) {
if n, _ := strconv.Atoi(m[1]); n > after {
return n, nil
}
}
if tail != "" && strings.Contains(strings.Join(strings.Fields(p), " "), strings.Join(strings.Fields(tail), " ")) {
return after, nil
if n, ok := s.textTaken(tail, after); ok {
return n, nil
}
select {
case <-s.changed:
@@ -1350,6 +1409,31 @@ func (s *shim) shown(text string, after int) (int, error) {
}
}
// textTaken says whether the prompt shows pasted words taken: a "[Pasted
// text #<n>" numbered past after, or the words' last, tail, drawn since
// the paste or ending the prompt as the screen shows it (prompt). It is
// the number shown, or after where the words show as themselves.
func (s *shim) textTaken(tail string, after int) (int, bool) {
p := s.plain()
for _, m := range textShown.FindAllStringSubmatch(p, -1) {
if n, _ := strconv.Atoi(m[1]); n > after {
return n, true
}
}
words := strings.Join(strings.Fields(tail), " ")
if words != "" && strings.Contains(strings.Join(strings.Fields(p), " "), words) {
return after, true
}
on := s.prompt()
if n := lastMark(textLast, on); n > after {
return n, true
}
if words != "" && strings.HasSuffix(on, words) {
return after, true
}
return 0, false
}
// transcript is the session's transcript, "" before Claude Code writes it.
func (s *shim) transcript() string {
m, _ := filepath.Glob(filepath.Join(s.projects, "*", s.sid+".jsonl"))
+57
View File
@@ -704,6 +704,63 @@ func TestShimWaitsForWordsNamingImages(t *testing.T) {
}
}
// Claude Code 2.1.319 draws a prompt grown past a row again only where
// it changed, moving the cursor past the cells already right, so what is
// drawn after a paste can hold a digit of the mark it took: after the
// 33rd piece of the reporter's message, nineteen images between words,
// the stream held "3" of "[Image #33]", and the helper waited out
// pasteWait and ended (#5654, Chapin2018: "ended after 13s (exit status
// 1)"). Each piece is seen taken on the screen, as Claude Code drew it.
// The bytes are a real run's, its folder's name changed.
func TestShimSeesPastesOnAPromptDrawnInPart(t *testing.T) {
b, err := os.ReadFile(filepath.Join("testdata", "paste-images-2.1.319.bin"))
if err != nil {
t.Fatal(err)
}
// where the helper cleared the screen, before each paste, and where
// the run ended
clears := []int{1994, 2794, 3007, 3163, 3281, 3434, 3505, 3605, 3679, 3783, 3857, 3962, 4037, 5372, 5504, 5661, 5733, 5832, 5906, 6011, 6086, 6191, 6266, 7774, 7905, 8062, 8181, 8335, 8407, 8511, 8586, 8691, 8766, 8871, len(b)}
// the pieces from the first image on: an image, then words long
// enough to be a "[Pasted text", 33 marks in all
var images, texts, inPart int
for i := 1; i < len(clears); i++ {
s := &shim{screen: b[clears[i-1]:clears[i]], grid: newGrid(screenSize())}
s.grid.write(b[:clears[i]])
on := s.prompt()
img, txt := lastMark(imageShown, on), lastMark(textLast, on)
switch {
case img > 0:
images++
if n := s.imageTaken(img - 1); n != img {
t.Errorf("the paste before %d: image %d not seen taken (%d) on %q", clears[i], img, n, on)
}
if !strings.Contains(s.plain(), fmt.Sprintf("[Image #%d]", img)) {
inPart++
}
case txt > 0:
texts++
if n, ok := s.textTaken("", txt-1); !ok || n != txt {
t.Errorf("the paste before %d: text %d not seen taken (%d, %v) on %q", clears[i], txt, n, ok, on)
}
if !textShown.MatchString(s.plain()) {
inPart++
}
}
}
if images != 17 || texts != 16 {
t.Fatalf("read %d images and %d texts (%d in part), want 17 and 16", images, texts, inPart)
}
// the case this guards: the stream alone misses a mark
if inPart == 0 {
t.Fatal("every mark was drawn whole: the fixture no longer shows #5654")
}
s := &shim{screen: b[8871:], grid: newGrid(screenSize())}
s.grid.write(b)
if n := s.imageTaken(32); n != 33 {
t.Fatalf("the last image was seen as %d, want 33, on %q", n, s.prompt())
}
}
// A folder of images or a tap's a killed helper left is removed by a
// later one after imagesLeft, and one in use isn't.
func TestDropLeft(t *testing.T) {