claude: a turn's images say they are attached, so the file notes after them are not taken for the images

Claude Code notes each image it is given ([Image: source: .../images/N.jpg])
in a note after the message, with a path in the gateway's temp folder. With
no history before the turn, nothing said what they are: the model took the
paths for the images, said it could not see them and quoted the paths as on
another computer (sinsoul1647 on Discord, v0.1.2862, 7 tool-result photos).
The prompt (renderClaudePrompt) and a saved session's turn (renderClaudeTurn)
now say the images, tool results' too, are in the message.

Tried: real claude 2.1.316 on mote against a fake Anthropic server, with
-p user images, MCP tool-result images, a url source, the TUI paste and a
resumed transcript: every one reached the API as image blocks, so the loss
is the model's reading of the notes, not a dropped image.
Not tried: the reporter's client; whether the model stops quoting paths.
Broken by hand: with the note left out, TestClaudePromptSaysToolResultImagesAreAttached is red.

Run at the pushed head (rebased on 8e27369bd): gofmt -l clean; go vet gateway linux and windows ok; go test ./internal/gateway -run Claude|Subscription|Prompt|Image ok (not the full gateway suite: another full suite was on mote).
This commit is contained in:
yetone
2026-11-04 00:14:37 +08:00
parent 8e27369bd6
commit 8468afecb3
3 changed files with 79 additions and 0 deletions
@@ -724,6 +724,13 @@ history to the 1h cache at 2×.
are the history's, or the history's first and then the turn's
(`currentTurnOpen`, #1751). Told nothing, the model said a text-only
turn had an image attached and offered to open the path.
When the images are all the turn's, as a tool's seven photos in a
first turn (no history to open the notes' owner), `imageNotesNote` ends
the turn: the images, tool results' too, are in the message, and the
notes are Claude Code's record of where it keeps them. Told nothing,
the model took the paths for the images and said it could not see them
(a Discord report, v0.1.2862). A turn told to a saved session says the
same (`renderClaudeTurn`).
A Claude Code that ends before it answers a control request
(`cliControl`: `initialize`, the safety context) is reported with its
@@ -123,3 +123,58 @@ func TestClaudePromptSaysWhoseImageNotesAre(t *testing.T) {
t.Fatalf("a history with no image:\n%s", now)
}
}
// A tool that returns images (seven photos, as sinsoul1647 on Discord,
// v0.1.2862) leaves Claude Code's notes after the message with paths in the
// gateway's temp folder ("[Image: source: …/images/1.jpg]"). With no history
// before the turn there was nothing to say what they are: the model took the
// paths for the images, said it could not see them and quoted them as on
// another computer. The turn says the images are in it, in a run told the
// whole conversation and in a turn told to a saved session.
func TestClaudePromptSaysToolResultImagesAreAttached(t *testing.T) {
img := Part{MediaType: "image/png", Data: "iVBORw0KGgo="}
calls := make([]Part, 7)
results := make([]Part, 7)
for i := range calls {
id := "call_" + string(rune('a'+i))
calls[i] = Part{Kind: ToolCall, ID: id, Name: "view", Args: json.RawMessage(`{}`)}
results[i] = Part{Kind: ToolResult, CallID: id, Text: "Read image file [image/png]", Images: []Part{img}}
}
msgs := []Message{
{Role: "user", Parts: []Part{{Kind: Text, Text: "look at the seven pictures"}}},
{Role: "assistant", Parts: calls},
{Role: "user", Parts: results},
}
text := func(blocks []map[string]any) string {
var all strings.Builder
images := 0
for _, b := range blocks {
if b["type"] == "image" {
images++
continue
}
s, _ := b["text"].(string)
all.WriteString(s)
}
if images != 7 {
t.Fatalf("%d image blocks, want 7", images)
}
return all.String()
}
tools := []Tool{{Name: "view"}}
blocks, err := renderClaudePrompt(&Request{Messages: msgs, Tools: tools})
if err != nil {
t.Fatal(err)
}
if got := text(blocks); !strings.Contains(got, imageNotesNote) {
t.Fatalf("the prompt doesn't say the images are attached:\n%s", got)
}
if got := text(renderClaudeTurn(msgs[2:], tools)); !strings.Contains(got, imageNotesNote) {
t.Fatalf("the saved session's turn doesn't say the images are attached:\n%s", got)
}
// no image, no note
blocks, _ = renderClaudePrompt(&Request{Messages: msgs[:1]})
if got, _ := json.Marshal(blocks); strings.Contains(string(got), "image content") {
t.Fatalf("a turn with no image has the note: %s", got)
}
}
+17
View File
@@ -4093,6 +4093,9 @@ func renderClaudePrompt(req *Request) ([]map[string]any, error) {
chunk = len(blocks) - 1
}
}
if countImages(req.Messages[split:]) > 0 && (split == 0 || countImages(req.Messages[:split]) == 0) {
text.WriteString(imageNotesNote + "\n\n")
}
if slices.ContainsFunc(req.Messages, callsTool) {
text.WriteString(pastCallsNote + "\n\n")
}
@@ -4262,6 +4265,17 @@ func currentTurnOpen(msgs []Message, split int) string {
"the images above in their order: the conversation history's first, then this turn's.\n\n"
}
// imageNotesNote ends a turn whose images (a tool result's among them) all
// belong to it, where currentTurnOpen has no history to say whose the notes
// are. Claude Code notes each image it is given ("[Image: source:
// …/images/3.jpg]") in a note after the message, with a path in the
// gateway's temp folder. Told nothing of them, the model took the notes for
// the images, said it could not see them and quoted the paths as on
// another computer (a Discord report, v0.1.2862).
const imageNotesNote = "(The images above, those in tool results too, are attached to this message as image content you can see. " +
"Any \"[Image: …]\" notes after it are Claude Code's record of where it keeps them, on its own computer: " +
"a path in one is not something to open, and the image itself is in the message.)"
// countImages is how many images msgs carry, their tool results' among them.
func countImages(msgs []Message) int {
n := 0
@@ -4493,6 +4507,9 @@ func renderClaudeTurn(msgs []Message, tools []Tool) []map[string]any {
}
blocks = renderParts(blocks, &text, m.Parts, offered)
}
if countImages(msgs) > 0 {
text.WriteString("\n\n" + imageNotesNote)
}
if slices.ContainsFunc(msgs, callsTool) {
text.WriteString("\n\n" + pastCallsNote)
}