diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 49bee4299..e1c767b29 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -1569,6 +1569,21 @@ sends a 90-message conversation five turns, the images of its earlier turns taken out and sent back in turn, both with a line in their place and with nothing. +`heardRuns` reads an assistant run's texts in a row as one part, their +words with no whitespace, as `messageWords` does for the keys (#5705): +the run keeps its reply as the one text it said, while the client sends +it back as Anthropic's stream told it, a text block for each run of +words between thinking blocks. Compared block by block, the first turn +after such a reply whose client had taken out an earlier image was none +the run had (Yqihotaru on X: omp keeps 5 images in a request to a +provider it has no budget for, so each turn with a new image takes out +the oldest; a turn after a reply with tool calls, or a turn without an +image, matched). A user's texts stay apart, as the text an image left +between them is what `lostFromRun` reads. +`TestClaudeOmpImageBudgetGoesOnInItsRun` sends omp's clamped history +eight turns, with the reply in one block and split by thinking, and an +image with words, alone, before words, and two at once. + A client that drops the images' message without adding text to a result goes on only when that message held images alone, no text. A tool result of an image alone (pi-ai's "(see attached image)") whose text is replaced by the line is not this shape, and starts anew. diff --git a/internal/gateway/claude_image_budget_test.go b/internal/gateway/claude_image_budget_test.go new file mode 100644 index 000000000..53117ef15 --- /dev/null +++ b/internal/gateway/claude_image_budget_test.go @@ -0,0 +1,112 @@ +package gateway + +import ( + "fmt" + "strings" + "testing" +) + +// ompImageGone is what omp (oh-my-pi 18.20.3, clampProviderContextImages) +// puts in a message whose every image its image budget took out. +const ompImageGone = "[image omitted: provider image limit]" + +// ompPart is a part of a message as omp keeps its history: text, a +// thinking block, or an image (neither). +type ompPart struct { + text string + think bool +} + +type ompMessage struct { + role string + parts []ompPart +} + +// ompClamp is omp's history as it sends it to a provider it has no image +// budget for (5 images, snapcompact's DEFAULT_PROVIDER_IMAGE_BUDGET): the +// oldest images past the budget taken out, and a message left with +// nothing told ompImageGone. +func ompClamp(history []ompMessage, budget int) []string { + images := 0 + for _, m := range history { + for _, p := range m.parts { + if p.text == "" && !p.think { + images++ + } + } + } + drop := images - budget + var out []string + for _, m := range history { + var parts []string + for _, p := range m.parts { + switch { + case p.think: + parts = append(parts, `{"type":"thinking","thinking":"the rest","signature":"sig"}`) + case p.text != "": + parts = append(parts, fmt.Sprintf(`{"type":"text","text":%q}`, p.text)) + case drop > 0: + drop-- + default: + parts = append(parts, `{"type":"image","source":{"type":"base64","media_type":"image/png","data":"`+onePNG+`"}}`) + } + } + if len(parts) == 0 { + parts = []string{fmt.Sprintf(`{"type":"text","text":%q}`, ompImageGone)} + } + out = append(out, fmt.Sprintf(`{"role":%q,"content":[%s]}`, m.role, strings.Join(parts, ","))) + } + return out +} + +// omp keeps 5 images in a request to a provider it has no budget for, the +// oldest taken out as a new one comes (Yqihotaru on X): each turn whose +// user pastes an image takes one out of an earlier turn. A turn after a +// reply the client keeps as several text blocks, its words split by a +// thinking block (#5705), went to a new Claude Code, told the whole +// conversation from a transcript and all of it written to the prompt +// cache again: the run kept its reply as one text, which mediaMoved +// compared block by block. Each goes on in the run that had the +// conversation. +func TestClaudeOmpImageBudgetGoesOnInItsRun(t *testing.T) { + const reply = "Looking at it:Here is the answer." + for _, r := range []struct { + name string + parts []ompPart // the reply as the client keeps it + }{ + {"a reply in one block", []ompPart{{text: reply}}}, + {"a reply split by its thinking", []ompPart{{think: true}, {text: "Looking at it:"}, {think: true}, {text: "Here is the answer."}}}, + } { + for _, c := range []struct { + name string + with func(i int) []ompPart // the user's message of turn i, with an image + }{ + {"words and an image", func(i int) []ompPart { return []ompPart{{text: fmt.Sprintf("more %d 看这个", i)}, {}} }}, + {"an image alone", func(int) []ompPart { return []ompPart{{}} }}, + {"an image and words after", func(i int) []ompPart { return []ompPart{{}, {text: fmt.Sprintf("more %d 这是什么", i)}} }}, + {"two images", func(i int) []ompPart { return []ompPart{{text: fmt.Sprintf("more %d", i)}, {}, {}} }}, + } { + t.Run(r.name+"/"+c.name, func(t *testing.T) { + h := newReplyHarness(t, reply) + var history []ompMessage + for i := range 6 { + history = append(history, + ompMessage{"user", []ompPart{{text: fmt.Sprintf("第 %d 个问题", i)}, {}}}, + ompMessage{"assistant", []ompPart{{text: fmt.Sprintf("第 %d 个回答", i)}}}) + } + for turn, image := range []bool{false, true, true, false, true, true, false, true} { + ask := []ompPart{{text: fmt.Sprintf("more %d", turn)}} + if image { + ask = c.with(turn) + } + history = append(history, ompMessage{"user", ask}) + h.send("rules", []string{"read"}, ompClamp(history, 5)...) + history = append(history, ompMessage{"assistant", r.parts}) + if n := len(h.runs()); n != 1 { + t.Fatalf("turn %d (image %t): %d runs, want the one that had the conversation", turn, image, n) + } + } + }) + } + } +} diff --git a/internal/gateway/claude_lost_media.go b/internal/gateway/claude_lost_media.go index fa8c685a8..a9e51694f 100644 --- a/internal/gateway/claude_lost_media.go +++ b/internal/gateway/claude_lost_media.go @@ -97,24 +97,52 @@ type heardRun struct { } // heardRuns is msgs as lostMedia compares them; sealed keeps no words. +// +// A reply's texts in a row are one part, their words with no whitespace, +// as messageWords reads them: the run keeps its reply as the one text it +// said, and the client as Anthropic's stream told it, a text block for +// each run of words between thinking blocks (#5705). Compared block by +// block, the next turn whose client had also taken out an earlier image +// (omp keeps 5 in a request to a provider it has no budget for) was none +// the run had, and went to a new Claude Code told all of it from a +// transcript (Yqihotaru on X). A user's texts stay apart: the text an +// image left between them is what lostFromRun reads. func heardRuns(msgs []Message, sealed bool) []heardRun { - var out []heardRun + type run struct { + role string + parts []wordPart + } + var runs []run for i, parts := range messageParts(msgs) { if len(parts) == 0 { continue } - if len(out) == 0 || out[len(out)-1].role != msgs[i].Role { - out = append(out, heardRun{role: msgs[i].Role}) + if len(runs) == 0 || runs[len(runs)-1].role != msgs[i].Role { + runs = append(runs, run{role: msgs[i].Role}) } - r := &out[len(out)-1] + r := &runs[len(runs)-1] for _, p := range parts { + if r.role == "assistant" && p.kind == Text { + p.words = strings.Join(strings.Fields(p.words), "") + if k := len(r.parts) - 1; k >= 0 && r.parts[k].kind == Text { + r.parts[k].words += p.words + continue + } + } + r.parts = append(r.parts, p) + } + } + out := make([]heardRun, len(runs)) + for i, r := range runs { + out[i].role = r.role + for _, p := range r.parts { h := heardPart{kind: p.kind, n: len(p.words), sum: sha256.Sum256([]byte(p.words)), media: p.media} if !sealed { h.words = p.words } else if p.kind == ToolResult { h.tail = p.words[max(0, len(p.words)-heardTail):] } - r.parts = append(r.parts, h) + out[i].parts = append(out[i].parts, h) } } return out