Skip to content

Commit 4327f5d

Browse files
committed
fix(core): stop replaying attachments the provider rejected
When a provider rejects a request because of an attachment (for example an image sent to a text-only custom model), V2 session history replayed the same attachment on every later turn, so the session could never recover. Replace attachments whose turn failed with an HTTP 4xx request error before producing any output with a short text note when replaying to the same model. Switching to a different model still sends them. Refs anomalyco#52042 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018CFR7BogLxVEwTy74UxWUE
1 parent 1188db5 commit 4327f5d

2 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎packages/core/src/session/runner/to-llm-message.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const assistant = (message: SessionMessage.Assistant, model: Model) => {
112112
]
113113
}
114114

115-
function toLLMMessage(message: SessionMessage.Message, model: Model): Message[] {
115+
function toLLMMessage(message: SessionMessage.Message, model: Model, rejected: ReadonlySet<string>): Message[] {
116116
switch (message.type) {
117117
case "agent-switched":
118118
case "model-switched":
@@ -122,7 +122,10 @@ function toLLMMessage(message: SessionMessage.Message, model: Model): Message[]
122122
Message.make({
123123
id: message.id,
124124
role: "user",
125-
content: [{ type: "text", text: message.text }, ...(message.files ?? []).map(media)],
125+
content: [
126+
{ type: "text", text: message.text },
127+
...(message.files ?? []).map((file) => (rejected.has(message.id) ? omittedMedia(file) : media(file))),
128+
],
126129
metadata: {
127130
...message.metadata,
128131
...(message.agents?.length ? { agents: message.agents } : {}),
@@ -167,5 +170,29 @@ ${message.recent}
167170
}
168171

169172
/** Translate projected V2 Session history into canonical @opencode-ai/llm context. */
170-
export const toLLMMessages = (messages: readonly SessionMessage.Message[], model: Model) =>
171-
messages.flatMap((message) => toLLMMessage(message, model))
173+
export const toLLMMessages = (messages: readonly SessionMessage.Message[], model: Model) => {
174+
const rejected = rejectedAttachments(messages, model)
175+
return messages.flatMap((message) => toLLMMessage(message, model, rejected))
176+
}
177+
178+
const omittedMedia = (file: FileAttachment): ContentPart => ({
179+
type: "text",
180+
text: `[Attachment ${file.name ? `"${file.name}" ` : ""}(${file.mime}) omitted: the provider rejected the request that included it.]`,
181+
})
182+
183+
// Replaying an attachment the provider already rejected fails every later turn the same way,
184+
// so strip attachments whose turn failed with a request error before producing any output.
185+
// Only the same model is affected, so switching to a model that accepts the attachment restores it.
186+
const rejectedAttachments = (messages: readonly SessionMessage.Message[], model: Model) =>
187+
new Set(
188+
messages.flatMap((message, index) => {
189+
if (message.type !== "user" || !message.files?.length) return []
190+
const reply = messages.slice(index + 1).find((next) => next.type === "user" || next.type === "assistant")
191+
if (reply?.type !== "assistant" || reply.error === undefined || reply.content.length > 0) return []
192+
if (String(reply.model.providerID) !== String(model.provider) || String(reply.model.id) !== String(model.id))
193+
return []
194+
return REQUEST_REJECTED.test(reply.error.message) ? [message.id] : []
195+
}),
196+
)
197+
198+
const REQUEST_REJECTED = /HTTP (400|413|415|422)\b/

‎packages/core/test/session-runner-message.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,51 @@ describe("toLLMMessages", () => {
4747
expect(messages.map((message) => message.id)).toEqual([id("text"), id("reasoning")])
4848
})
4949

50+
describe("attachments rejected by the provider", () => {
51+
const file = FileAttachment.make({ uri: "data:image/png;base64,aGVsbG8=", mime: "image/png", name: "hello.png" })
52+
const history = (error: string, modelID = "model") => [
53+
SessionMessage.User.make({ id: id("image"), type: "user", text: "Look", files: [file], time: { created } }),
54+
SessionMessage.Assistant.make({
55+
id: id("failed"),
56+
type: "assistant",
57+
agent: "build",
58+
model: { id: ModelV2.ID.make(modelID), providerID: ProviderV2.ID.make("provider") },
59+
content: [],
60+
error: { type: "unknown", message: error },
61+
time: { created, completed: created },
62+
}),
63+
SessionMessage.User.make({ id: id("next"), type: "user", text: "Hello?", time: { created } }),
64+
]
65+
const imageContent = (messages: ReturnType<typeof toLLMMessages>) =>
66+
messages.find((message) => message.id === id("image"))?.content
67+
68+
test("replaces attachments whose request the same model rejected", () => {
69+
expect(
70+
imageContent(
71+
toLLMMessages(history("Provider request failed with HTTP 400: Image input is not supported"), model),
72+
),
73+
).toEqual([
74+
{ type: "text", text: "Look" },
75+
{
76+
type: "text",
77+
text: '[Attachment "hello.png" (image/png) omitted: the provider rejected the request that included it.]',
78+
},
79+
])
80+
})
81+
82+
test("keeps attachments for a different model", () => {
83+
expect(
84+
imageContent(toLLMMessages(history("Provider request failed with HTTP 400", "other-model"), model)),
85+
).toContainEqual(expect.objectContaining({ type: "media", mediaType: "image/png" }))
86+
})
87+
88+
test("keeps attachments when the failure was not a request rejection", () => {
89+
expect(imageContent(toLLMMessages(history("Provider request failed with HTTP 503"), model))).toContainEqual(
90+
expect.objectContaining({ type: "media", mediaType: "image/png" }),
91+
)
92+
})
93+
})
94+
5095
test("maps every top-level V2 Session message type", () => {
5196
const file = FileAttachment.make({ uri: "data:image/png;base64,aGVsbG8=", mime: "image/png", name: "hello.png" })
5297
const messages = toLLMMessages(

0 commit comments

Comments
 (0)