claude: a turn with a new image after a reply split by thinking goes on in its run

Yqihotaru on X (omp 18.20.3, magpie 0.1.4314, DEBUG on): a user turn
with an image, after a reply with no tool calls, logged "no run here has
had this conversation" and was told to a new Claude Code from a
transcript (81 messages, 4 images; the turn before had 5), all of it
written to the prompt cache again. The same turn after a reply with tool
calls, and a text-only turn after it, went on in the run.

omp keeps 5 images in a request to a provider it has no image budget for
(clampProviderContextImages, providerImageBudget's default), so each new
image takes the oldest out: the conversation's earlier images moved, and
only looseTurn.rewrote's mediaMoved could match it. mediaMoved compared
the conversation part by part (heardRuns), and the run keeps its reply as
the one text it said while the client sends it back as the text blocks
Anthropic's stream told it, one between each thinking block (#5705).
#5705 made the turn keys read a reply's words without whitespace; the
part-by-part compare still read two blocks against one text, so the
first such turn found no run.

heardRuns now reads an assistant run's texts in a row as one part, their
words with no whitespace, as messageWords does. A user's texts stay
apart: the text an image left between them is what lostFromRun reads.

TestClaudeOmpImageBudgetGoesOnInItsRun sends omp's clamped history (the
budget of 5, "[image omitted: provider image limit]" in a message left
empty) eight turns, with the reply in one block and split by thinking,
and an image with words, alone, before words, and two at once. The
subsystem page says what heardRuns reads.

Run (on mote, through mote-run, at 98955300a + this change):
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway: ok (930s)
- GOOS=linux go vet ./internal/gateway, GOOS=windows go vet
  ./internal/gateway: clean; gofmt -l: clean
- rebased onto ccae8e821, which changes internal/agent only

Broken by hand: at 98955300a without the heardRuns change the new test
is red in all four "split by its thinking" cases at turn 1, the first
turn with an image, with the reporter's line, "no run waits for this
turn: no run here has had this conversation", then "a new Claude Code
is told the whole conversation: 13 messages, 5 images". The one-block
cases pass there.

Not tried: no real omp session or Claude account; the reply's block
shape is inferred (a text-only reply, thinking on) from the report, not
read from the reporter's request. The test skips on Windows (a shell
script stands in for Claude Code). No full ./... run: the change is
confined to internal/gateway.

Reported-by: Yqihotaru on X
This commit is contained in:
yetone
2026-11-16 20:39:45 +08:00
parent ccae8e8211
commit 2896e82ccb
3 changed files with 160 additions and 5 deletions
@@ -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.
@@ -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)
}
}
})
}
}
}
+33 -5
View File
@@ -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