diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index d912c6399..dd3040a31 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -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 diff --git a/internal/claudetui/testdata/paste-images-2.1.319.bin b/internal/claudetui/testdata/paste-images-2.1.319.bin new file mode 100644 index 000000000..8c11e04b6 --- /dev/null +++ b/internal/claudetui/testdata/paste-images-2.1.319.bin @@ -0,0 +1 @@ +78[?25h[?2004h[?2031h[?1004h[>4;2m]0;✳ Claude Code\[?1049h[>4;2m[?1000h[?1002h[?1003h[?1006h[?25l[?25l[?2026h  ▐β–›β–ˆβ–ˆβ–ˆβ–›β–ˆClaude Codev2.1.319 β–β–œβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–€Fable 5.1 Β· Claude API  ▝▝ ▝▝ /home/user/wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww/work ● high Β· /effort ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ Try "fix lint errors" ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ⏸ manual mode on Β· ? for shortcuts Β· ← for agents[?25h[?2026l[>0q[?u]7501;?\[?2026h[?25l ctrl+g to edit in Vim ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ Syntheticimage1.   [?25h[?2026l[?2026h[?25l Pasting…[?25h[?2026l[?2026h[?25l [Image#1] ⏸ manual mode on[?25h[?2026l[?2026h[?25l [Pastedtext#2+4lines] paste gain to expand[?25h[?2026l[?2026h[?25l [Image#3] Ping…[?25h[?2026l[?2026h[?25l [Pastedtext#4+4lines] pe again to expand[?25h[?2026l[?2026h[?25l [Image#5][?25h[?2026l[?2026h[?25l [Pastedtext#6+4lines][?25h[?2026l[?2026h[?25l [Image#7][?25h[?2026l[?2026h[?25l [Pastedtext#8+4lines][?25h[?2026l[?2026h[?25l [Image#9][?25h[?2026l[?2026h[?25l [Pastedtext#10+4lines][?25h[?2026l[?2026h[?25l [Image#11][?25h[?2026l[?2026h[?25l ctrl+g to edit in Vim ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ Synthetic image 1.  [Image#1][Pastedtext#2+4lines][Image#3][Pastedtext#4+4lines][Image#5][Pastedtext#6+4lines][Image#7][Pastedtext#8+4lines][Image#9][Pastedtext#10+4lines][Image#11][Pasted text #12 +4 lins][?25h[?2026l[?2026h[?25l  ●high Β· /effort[?25h[?2026l[?2026h[?25l ⏸ manul mode on[?25h[?2026l(B[?2004h[>4;2m[?1000h[?1002h[?1003h[?1006h[?2026h[?25l [Image#13][?25h[?2026l[?2026h[?25l [Pastedtext#14+4lines] paste gain to expand[?25h[?2026l[?2026h[?25l [Image#15][?25h[?2026l[?2026h[?25l [Pastedtext#16+4lines][?25h[?2026l[?2026h[?25l [Image#17][?25h[?2026l[?2026h[?25l [Pastedtext#18+4lines][?25h[?2026l[?2026h[?25l [Image#19][?25h[?2026l[?2026h[?25l [Pastedtext#20+4lines][?25h[?2026l[?2026h[?25l [Image#21][?25h[?2026l[?2026h[?25l ● high Β· /effort ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ Synthetic image 1.  [Image#1][Pastedtext#2+4lines][Image#3][Pastedtext#4+4lines][Image#5][Pastedtext#6+4lines][Image#7][Pastedtext#8+4lines][Image#9][Pastedtext#10+4lines][Image#11][Pasted text #12 +4 lins][Image #13][Pasted text14 +4 lines][Image #15][Pasted txt #16 +4 lins][Image#17][Pasted text #18 +4lines][Image #19Pastd text #20 +4 lines][Image #21][Psted text #22 +4 lines][?25h[?2026l[?2026h[?25l [?25h[?2026l[?2026h[?25l ⏸ manul mode on[?25h[?2026l(B[?2004h[>4;2m[?1000h[?1002h[?1003h[?1006h[?2026h[?25l [Image#23][?25h[?2026l[?2026h[?25l [Pastedtext#24+4lines] paste gain to expand[?25h[?2026l[?2026h[?25l [Image#25] Ping…[?25h[?2026l[?2026h[?25l [Pastedtext#26+4lines] pe again to expand[?25h[?2026l[?2026h[?25l [Image#27][?25h[?2026l[?2026h[?25l [Pastedtext#28+4lines][?25h[?2026l[?2026h[?25l [Image#29][?25h[?2026l[?2026h[?25l [Pastedtext#30+4lines][?25h[?2026l[?2026h[?25l [Image#31][?25h[?2026l[?2026h[?25l [Pastedtext#32+4lines][?25h[?2026l[?2026h[?25l ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ❯ Synthetic image 1.  [Image#1][Pastedtext#2+4lines][Image#3][Pastedtext#4+4lines][Image#5][Pastedtext#6+4lines][Image#7][Pastedtext#8+4lines][Image#9][Pastedtext#10+4lines][Image#11][Pasted text #12 +4 lins][Image #13][Pasted text14 +4 lines][Image #15][Pasted txt #16 +4 lins][Image#17][Pasted text #18 +4lines][Image #19Pastd text #20 +4 lines][Image #21][Psted text #22 +4 lines][Image #23][Pastedtext #24 +4lines][Image #25][Pastedtext #26 +4lines][Image #27][Pastedtext #28 +4lines][Image #29][Pastedtext #30 +4lines][Image #31][Pastedtext #32 +4 3[?25h[?2026l[?2026h[?25l ⏸ manul mode on[?25h[?2026l \ No newline at end of file diff --git a/internal/claudetui/tui.go b/internal/claudetui/tui.go index 3b1d18df9..f002bbee4 100644 --- a/internal/claudetui/tui.go +++ b/internal/claudetui/tui.go @@ -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 #" 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 #" 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")) diff --git a/internal/claudetui/tui_test.go b/internal/claudetui/tui_test.go index ad9afc887..40699298c 100644 --- a/internal/claudetui/tui_test.go +++ b/internal/claudetui/tui_test.go @@ -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) {