From f0336ae61d64aa1b38f9f4a45a492d5cc6934a62 Mon Sep 17 00:00:00 2001 From: Afloat Date: Sun, 15 Nov 2026 23:28:44 +0800 Subject: [PATCH 1/3] fix: recover each Responses image from its own ref Use split image sidecars instead of expanding the whole carrier under every image ref; preserve unsplit and tool-result image groups. Fixes #6851 (cherry picked from commit f5aeb2827cd828a7562afd8d98424639c8b7d99a) --- src/image-restore.ts | 9 ++++++ tests/image-restore.test.ts | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/image-restore.ts b/src/image-restore.ts index 5c59fe4d40..9ce61045a4 100644 --- a/src/image-restore.ts +++ b/src/image-restore.ts @@ -125,6 +125,15 @@ export function messageImageBytes(m: CoreMessage): RestorableImage[] { // there, so the index must too (#1995 nested-tool-image parity). const ri = mm.rawResponsesItem; if (isObj(ri)) { + // A split Responses image core still points at the WHOLE wire item. + // Its imageBase64 sidecar identifies this occurrence's own pixels; + // walking the carrier would bind every sibling image to every ref. + // A URL-only occurrence has no carried bytes, even if a sibling does. + if (mm.imagePart === true) { + return typeof mm.imageBase64 === "string" + ? [{ mediaType: mm.imageMediaType ?? "image/png", b64: mm.imageBase64, bytes: b64Bytes(mm.imageBase64) }] + : []; + } const arr = ri.type === "function_call_output" || ri.type === "custom_tool_call_output" ? (Array.isArray(ri.output) ? ri.output : undefined) : (Array.isArray(ri.content) ? ri.content : undefined); diff --git a/tests/image-restore.test.ts b/tests/image-restore.test.ts index 5dc702d26d..5e54494152 100644 --- a/tests/image-restore.test.ts +++ b/tests/image-restore.test.ts @@ -26,6 +26,13 @@ const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYPhfDwA const pngImg: RestorableImage = { mediaType: "image/png", b64: PNG, bytes: Buffer.byteLength(PNG, "base64") }; const dataUrl = `data:image/png;base64,${PNG}`; +const RED_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAE0lEQVR4AWP8z8DwnwEImBigAAAfFwICgH3ifwAAAABJRU5ErkJggg=="; +const BLUE_PNG = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAE0lEQVR4AWNkYPj/nwEImBigAAAdGQICxBbJ1AAAAABJRU5ErkJggg=="; + +function responseImage(b64: string) { + return { type: "input_image", image_url: `data:image/png;base64,${b64}` }; +} + // Each wire converter has a distinct concrete signature; the test drives them // uniformly on already-shaped bodies, so they are widened at the single call // site below rather than forced into one callable type here. @@ -343,3 +350,57 @@ test("buildIncomingImageIndex: a Gemini $ref without inline bytes adds no image" assert.deepEqual(imagePlaceholdersForSummary(message, "m00001"), []); assert.equal(buildIndex("google", body).size, 0); }); + +test("Responses split image refs restore only their own pixels", () => { + const index = buildIndex("responses", { model: "gpt", input: [{ type: "message", role: "user", content: [ + { type: "input_text", text: "Compare these two screenshots." }, responseImage(RED_PNG), responseImage(BLUE_PNG), + ] }] }, false, "responses-split-own-image"); + const entries = [...index.entries()]; + assert.equal(entries.length, 2); + assert.deepEqual(entries.map(([, images]) => images.map(image => readFileSync(image.path).toString("base64"))), [[RED_PNG], [BLUE_PNG]]); + const [blueRef, blueImages] = entries[1]; + const receipt = resolveDecompress({ imageRef: blueRef }, ctxWith(index)); + assert.equal(receipt.outcome, "success"); + assert.match(receipt.text, /Restored 1 image\(s\)/); + assert.ok(receipt.text.includes(blueImages[0].path)); + assert.ok(!receipt.text.includes(entries[0][1][0].path), "retrieving the second image must not return its sibling"); +}); + +test("Responses remote image refs do not borrow a carried sibling image", () => { + const body = { model: "gpt", input: [{ type: "message", role: "user", content: [ + { type: "input_image", image_url: "https://example.invalid/remote.png" }, responseImage(BLUE_PNG), + ] }] }; + const index = buildIndex("responses", body, false, "responses-remote-own-image"); + assert.deepEqual([...index.keys()], ["m00002"], "the remote-only ref has no carried pixels"); + assert.deepEqual(readFileSync(index.get("m00002")![0].path), Buffer.from(BLUE_PNG, "base64")); + assert.equal(resolveDecompress({ imageRef: "m00001" }, ctxWith(index)).outcome, "failure"); +}); + +test("Responses repeated identical images remain distinct one-image occurrences", () => { + const index = buildIndex("responses", { model: "gpt", input: [{ type: "message", role: "user", content: [responseImage(RED_PNG), responseImage(RED_PNG)] }] }, false, "responses-repeated-own-image"); + const images = [...index.values()]; + assert.deepEqual(images.map(value => value.length), [1, 1]); + assert.notEqual(images[0][0].path, images[1][0].path); + for (const value of images) assert.deepEqual(readFileSync(value[0].path), Buffer.from(RED_PNG, "base64")); +}); + +test("Responses image recovery creates one spill per split image, not a carrier cross-product", () => { + const content = Array.from({ length: 20 }, (_, i) => responseImage(i % 2 === 0 ? RED_PNG : BLUE_PNG)); + const index = buildIndex("responses", { model: "gpt", input: [{ type: "message", role: "user", content }] }, false, "responses-twenty-own-images"); + assert.equal(index.size, 20); + const files = [...index.values()].flatMap(images => images.map(image => image.path)); + assert.equal(files.length, 20); + assert.equal(new Set(files).size, 20); + assert.equal(files.reduce((total, file) => total + statSync(file).size, 0), 20 * Buffer.byteLength(RED_PNG, "base64")); +}); + +test("Responses unsplit carriers keep all images, including tool-result carriers", () => { + for (const [label, item] of [ + ["unknown-part", { type: "message", role: "user", content: [{ type: "input_text", text: "keep opaque carrier" }, { type: "future_part", payload: "opaque" }, responseImage(RED_PNG), responseImage(BLUE_PNG)] }], + ["tool-output", { type: "function_call_output", call_id: "tool-images", output: [responseImage(RED_PNG), responseImage(BLUE_PNG)] }], + ] as const) { + const index = buildIndex("responses", { model: "gpt", input: [item] }, false, `responses-unsplit-${label}`); + assert.equal(index.size, 1, label); + assert.deepEqual([...index.values()][0].map(image => readFileSync(image.path).toString("base64")), [RED_PNG, BLUE_PNG], label); + } +}); From bf479805e79bb585d86193931884e6587b316fb5 Mon Sep 17 00:00:00 2001 From: Saim Shafique Date: Mon, 16 Nov 2026 02:36:34 +0000 Subject: [PATCH 2/3] kernel: recognize DSH attachment refs as recoverable media (#6875) DSH read_image results carry {type:"image", attachment:{attachmentId, ...}} in tool results. Two gaps stacked: the codec reduced tool results to text so the part never reached a CoreMessage, and extractMediaRefs never enumerated the shape so the message pinned forever. This lands both halves together: the tool branch preserves attachment-carrying image parts (and re-emits them), the classifier reads attachmentId as a durable re-access handle, and the drop detector stops counting preserved parts as dropped. Already-BLOCKED history migrates via the existing #2607 mechanism on the next compress. (cherry picked from commit 0e8b7794d40e714ab7d3d34db63d241cd037594b) --- kernel/package-lock.json | 4 +- kernel/package.json | 2 +- kernel/src/media-refs.ts | 20 +- kernel/src/wire/openai.ts | 60 ++++- .../issue6875-dsh-attachment-refs.test.ts | 217 ++++++++++++++++++ .../issue6875-tool-result-sidecar.test.ts | 169 ++++++++++++++ package-lock.json | 2 +- src/wire-drop-warn.ts | 23 +- tests/fix-1205-file-parts.test.ts | 15 ++ 9 files changed, 497 insertions(+), 15 deletions(-) create mode 100644 kernel/tests/issue6875-dsh-attachment-refs.test.ts create mode 100644 kernel/tests/issue6875-tool-result-sidecar.test.ts diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 9437e48cf1..3e25df6ca0 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index a58d31360b..f65ce78fdd 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/media-refs.ts b/kernel/src/media-refs.ts index 44411611de..03310dbb44 100644 --- a/kernel/src/media-refs.ts +++ b/kernel/src/media-refs.ts @@ -11,7 +11,8 @@ * * The fix splits the class: a media message is FOLDABLE when it exposes at * least one extractable re-access handle (a non-data: URL, a file id, a - * fileUri). Folding then loses nothing unrecoverable — the handles ride the + * fileUri, a DSH attachment id — #6875). Folding then loses nothing + * unrecoverable — the handles ride the * stored summary verbatim (collectFoldMediaRefs appends them mechanically at * apply time, below), and decompress restores the original message * losslessly regardless. Only media with NO extractable ref stays pinned, @@ -88,6 +89,19 @@ export function looksEphemeralSignedUrl(ref: string): boolean { return false; } +/** #6875: a DSH image part's re-access handle is its attachment's + * content-addressed id — push it verbatim (bare-id parallel to a DeepSeek + * file_id; pushRef's inline-data/ephemeral guards do not apply to it). + * An attachment with no usable id pushes nothing, so the message keeps + * pinning (conservative). */ +function pushDshAttachmentRef( + refs: string[], + part: Record, +): void { + if (part.type !== "image") return; + pushRef(refs, asObj(part.attachment)?.attachmentId); +} + function pushRef(refs: string[], ref: unknown): void { if (typeof ref !== "string" || ref.length === 0 || isInlineDataUrl(ref)) return; // #5865-P2: an expiring handle is not a re-access handle — keep the @@ -111,7 +125,7 @@ export function extractMediaRefs(message: CoreMessage): string[] { else if (roc.type === "file") { pushRef(refs, roc.file_id); pushRef(refs, roc.url); - } + } else if (roc.type === "image") pushDshAttachmentRef(refs, roc); } if (Array.isArray(m.rawOpenaiContentParts)) { for (const p of m.rawOpenaiContentParts) { @@ -121,7 +135,7 @@ export function extractMediaRefs(message: CoreMessage): string[] { else if (part.type === "file") { pushRef(refs, part.file_id); pushRef(refs, part.url); - } + } else if (part.type === "image") pushDshAttachmentRef(refs, part); } } diff --git a/kernel/src/wire/openai.ts b/kernel/src/wire/openai.ts index a398bbf74f..04183688ae 100644 --- a/kernel/src/wire/openai.ts +++ b/kernel/src/wire/openai.ts @@ -28,6 +28,25 @@ function imageFingerprintsOfParts(parts: unknown[]): string[] { return out; } +/** #6875: a DSH image part — `{type:"image", + * attachment:{attachmentId, ...}}` — is the one tool-result shape the + * codec preserves into a sidecar. The attachmentId is a durable + * content-addressed handle (media-refs.ts enumerates it), so carrying it + * through is what makes the message foldable instead of invisibly + * media-carrying. Any other opaque tool-result part keeps the legacy + * text-only reduction: a sidecar the classifier does not recognize would + * newly pin the message (the #6875 trap — codec and enumeration must land + * together). */ +function isDshAttachmentPart(part: unknown): part is OpenAIContentPart { + if (!part || typeof part !== "object") return false; + const p = part as { type?: unknown; attachment?: unknown }; + if (p.type !== "image") return false; + const attachment = p.attachment; + if (!attachment || typeof attachment !== "object") return false; + const id = (attachment as { attachmentId?: unknown }).attachmentId; + return typeof id === "string" && id.length > 0; +} + export type OpenAIContentPart = | { type: "text"; text: string } | { type: "image_url"; image_url: { url: string } } @@ -326,6 +345,17 @@ export function openaiToCore(body: OpenAIRequestBody): Flat { { toolName: m.name || toolNames.get(m.tool_call_id ?? "") }, ); const name = m.name || toolNames.get(m.tool_call_id ?? ""); + // #6875: a DSH read_image result carries its image as + // `{type:"image", attachment:{attachmentId, ...}}` in the content + // array. stringContent() below drops every non-text part, so + // without a sidecar the attachment never reaches CoreMessage and + // extractMediaRefs can never see it — the message looks like plain + // text while its media is invisible. Preserve ONLY the + // attachment-carrying image parts (see isDshAttachmentPart): any + // other opaque part keeps the legacy text-only reduction. + const attachmentParts = Array.isArray(m.content) + ? m.content.filter(isDshAttachmentPart) + : []; msgs.push({ id: clusters.next(base), role: "tool", @@ -333,6 +363,9 @@ export function openaiToCore(body: OpenAIRequestBody): Flat { toolCallId: m.tool_call_id ?? "", text: stringContent(m.content), ...(name ? { toolName: name } : {}), + ...(attachmentParts.length > 0 + ? { rawOpenaiContentParts: attachmentParts } + : {}), }); break; } @@ -529,11 +562,28 @@ export function coreToOpenai(messages: BiliMessage[]): OpenAIMessage[] { } userRun.push(m); } else if (m.role === "tool") { - out.push({ - role: "tool", - tool_call_id: m.toolCallId ?? "", - content: m.text ?? "", - }); + // #6875: a tool result carrying preserved DSH attachment parts + // re-emits them after its text part (mirror of flushUserRun) so + // the parse→rebuild round trip keeps the attachment; text-only + // results keep the legacy plain-string shape byte-identically. + const parts = m.rawOpenaiContentParts; + if (Array.isArray(parts) && parts.length > 0) { + const content: OpenAIContentPart[] = []; + if (m.text) content.push({ type: "text", text: m.text }); + for (const part of parts) + content.push(part as OpenAIContentPart); + out.push({ + role: "tool", + tool_call_id: m.toolCallId ?? "", + content, + }); + } else { + out.push({ + role: "tool", + tool_call_id: m.toolCallId ?? "", + content: m.text ?? "", + }); + } } } } diff --git a/kernel/tests/issue6875-dsh-attachment-refs.test.ts b/kernel/tests/issue6875-dsh-attachment-refs.test.ts new file mode 100644 index 0000000000..f23cbf9e63 --- /dev/null +++ b/kernel/tests/issue6875-dsh-attachment-refs.test.ts @@ -0,0 +1,217 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + extractMediaRefs, + hasRefRecoverableMediaPayload, + MEDIA_REFS_MARKER, +} from "../src/media-refs.js"; +import { hasUnrecoverableMediaPayload } from "../src/protected.js"; +import { assignRefs, BLOCKED_REF } from "../src/refs.js"; +import { createCore } from "../src/compress.js"; +import { createInitialState } from "../src/state.js"; +import type { Config, CoreMessage } from "../src/types.js"; + +// #6875: DSH read_image / screenshot results carry the image as +// { type: "image", attachment: { attachmentId, mediaType, bytes, ... } } — +// a local blob in the DSH attachment store, addressable by attachmentId +// (blobs verified present by the reporter: 504 attachments, missing = 0). +// The id is a durable re-access handle in the #5620 sense (same class as a +// DeepSeek file_id: a bare id, durable by definition), but extractMediaRefs +// never enumerated the shape, so every such message classified as +// unrecoverable media, pinned forever, and its refs went BLOCKED. +const DSH_ID = "sha256:a08caa79deadbeef"; +const DSH_PART = { + type: "image", + attachment: { + attachmentId: DSH_ID, + mediaType: "image/png", + bytes: 62368, + width: 896, + height: 651, + name: "printwindow_flag0.png", + }, +}; + +function textMsg( + id: string, + role: CoreMessage["role"], + text: string, +): CoreMessage { + return { id, role, contentType: "text", text }; +} + +function sidecarMsg(id: string, extra: object): CoreMessage { + return Object.assign( + { id, role: "user" as const, contentType: "text" as const, text: "" }, + extra, + ); +} + +function config(): Config { + return { + tiers: { enabled: true, tier2Trigger: 5, tier3Trigger: 10 }, + nudge: { + maxContextLimitPct: 0.55, + minContextLimitPct: 0.45, + frequency: 5, + iterationThreshold: 15, + force: "soft", + growthRatio: 0.05, + growthFloor: 6000, + growthCap: 50000, + minGrowthFloor: 5000, + minGrowthRatio: 0.45, + emergencyThresholdPct: 0.98, + tier2GrowthMultiplier: 1.5, + }, + promotionThreshold: 5, + truncate: { threshold: 1 }, + compress: { minCompressRange: 0, maxSummaryLength: 0, minSummaryLength: 0 }, + protectedTools: [], + preserveRecentMessages: 0, + preserveRecentTokens: 0, + modelContextLimit: 100000, + }; +} + +test("extractMediaRefs reads the DSH attachment id from every sidecar family", () => { + assert.deepEqual( + extractMediaRefs(sidecarMsg("a", { rawOpenaiContentParts: [DSH_PART] })), + [DSH_ID], + "openai plural — the #6875 wire shape (unknown opaque part, #1205)", + ); + assert.deepEqual( + extractMediaRefs(sidecarMsg("b", { rawOpenaiContent: DSH_PART })), + [DSH_ID], + "openai singular", + ); + assert.deepEqual( + extractMediaRefs( + sidecarMsg("c", { + rawOpenaiContentParts: [{ type: "text", text: "screenshot" }, DSH_PART], + }), + ), + [DSH_ID], + "mixed text + attachment parts — only the handle", + ); +}); + +test("DSH attachment media is ref-recoverable, not unrecoverable (#6875)", () => { + const openai = sidecarMsg("o", { rawOpenaiContentParts: [DSH_PART] }); + assert.equal(hasRefRecoverableMediaPayload(openai), true); + assert.equal( + hasUnrecoverableMediaPayload(openai), + false, + "folds like #5620 media", + ); + const lone = sidecarMsg("p", { rawOpenaiContent: DSH_PART }); + assert.equal(hasRefRecoverableMediaPayload(lone), true); + assert.equal( + hasUnrecoverableMediaPayload(lone), + false, + "folds like #5620 media", + ); +}); + +test("an attachment without an id still pins (conservative, #6875)", () => { + const noId = sidecarMsg("x", { + rawOpenaiContentParts: [ + { type: "image", attachment: { mediaType: "image/png", bytes: 100 } }, + ], + }); + assert.deepEqual(extractMediaRefs(noId), []); + assert.equal(hasRefRecoverableMediaPayload(noId), false); + assert.equal( + hasUnrecoverableMediaPayload(noId), + true, + "no handle — pre-#5620 pin", + ); + const emptyId = sidecarMsg("y", { + rawOpenaiContentParts: [ + { type: "image", attachment: { attachmentId: "" } }, + ], + }); + assert.deepEqual(extractMediaRefs(emptyId), []); + assert.equal(hasUnrecoverableMediaPayload(emptyId), true); + const nonObject = sidecarMsg("z", { + rawOpenaiContentParts: [{ type: "image", attachment: "sha256:abc" }], + }); + assert.deepEqual(extractMediaRefs(nonObject), []); + assert.equal(hasUnrecoverableMediaPayload(nonObject), true); +}); + +test("a DSH image message gets a numeric ref, never BLOCKED (#6875)", () => { + const messages = [ + textMsg("a", "user", "alpha"), + sidecarMsg("dshimg", { rawOpenaiContentParts: [DSH_PART] }), + textMsg("b", "assistant", "beta"), + ]; + const state = createInitialState(); + const res = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + isProtected: hasUnrecoverableMediaPayload, + }); + assert.equal(res.map.byRaw["dshimg"], "m00002"); +}); + +// #6875 Q2: already-BLOCKED history needs no migration code — protection is +// re-evaluated every compress pass, so a fixed classifier lifts the pin and +// the #2607 migration mints a fresh number on the next pass. +test("legacy BLOCKED ref on DSH attachment media migrates to a fresh number (#6875)", () => { + const messages = [ + textMsg("a", "user", "alpha"), + sidecarMsg("dshimg", { rawOpenaiContentParts: [DSH_PART] }), + textMsg("b", "assistant", "beta"), + ]; + const state = createInitialState(); + state.messageRefs.byRaw["a"] = "m00001"; + state.messageRefs.byRaw["dshimg"] = BLOCKED_REF; + state.messageRefs.byRaw["b"] = "m00002"; + const res = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 3, + isProtected: hasUnrecoverableMediaPayload, + }); + assert.equal( + res.map.byRaw["dshimg"], + "m00003", + "fresh number — never a reuse", + ); +}); + +test("applyCompression records the DSH attachment id in the block summary (#6875)", () => { + const core = createCore(); + const state = createInitialState(); + const messages = [ + textMsg("u", "user", "the task"), + sidecarMsg("img", { rawOpenaiContentParts: [DSH_PART] }), + textMsg("t", "assistant", "analyzing"), + ]; + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + }).map; + + const result = core.applyCompression({ + ranges: [ + { + startRef: "m00001", + endRef: "m00003", + summary: "folded work", + topic: "t", + }, + ], + messages, + state, + config: config(), + }); + assert.equal( + result.result.errors.length, + 0, + JSON.stringify(result.result.errors), + ); + const block = result.state.blocks[0]!; + assert.ok(block.summary.includes(MEDIA_REFS_MARKER)); + assert.ok(block.summary.includes(`- ${DSH_ID}`)); +}); diff --git a/kernel/tests/issue6875-tool-result-sidecar.test.ts b/kernel/tests/issue6875-tool-result-sidecar.test.ts new file mode 100644 index 0000000000..6f158cea8e --- /dev/null +++ b/kernel/tests/issue6875-tool-result-sidecar.test.ts @@ -0,0 +1,169 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + coreToOpenai, + openaiToCore, + type OpenAIRequestBody, +} from "../src/wire/openai.js"; +import { hasUnrecoverableMediaPayload } from "../src/protected.js"; +import { assignRefs, BLOCKED_REF } from "../src/refs.js"; +import { createInitialState } from "../src/state.js"; + +// #6875, codec half (maintainer triage): openaiToCore case "tool" only kept +// stringContent, so a DSH read_image result {type:"image", +// attachment:{attachmentId}} was dropped at parse time and never reached +// extractMediaRefs. The tool branch now retains non-text parts on +// rawOpenaiContentParts (#1205 parity with the user branch) and the rebuild +// re-emits them verbatim; the media-refs half (companion file) recognizes +// the attachmentId as a recoverable handle. Either half alone is wrong: +// retention without recognition would newly pin the message. +const DSH_ID = "sha256:a08caa79deadbeef"; +const DSH_PART = { + type: "image", + attachment: { + attachmentId: DSH_ID, + mediaType: "image/png", + bytes: 62368, + width: 896, + height: 651, + name: "printwindow_flag0.png", + }, +}; + +function toolBody(content: unknown): OpenAIRequestBody { + return { + messages: [ + { + role: "tool", + tool_call_id: "call_1", + name: "read_image", + content: content as never, + }, + ], + }; +} + +test("tool results keep their non-text parts on rawOpenaiContentParts (#6875)", () => { + const parsed = openaiToCore( + toolBody([{ type: "text", text: "shot" }, DSH_PART]), + ); + assert.equal(parsed.msgs.length, 1); + const core = parsed.msgs[0]!; + assert.equal(core.role, "tool"); + assert.equal(core.contentType, "tool-result"); + // stringContent joins every part with "\n" and non-text parts + // contribute "" — the trailing newline is the pre-existing join + // artifact for multi-part tool results, not something this fix adds. + assert.equal(core.text, "shot\n"); + assert.equal(core.toolName, "read_image"); + assert.deepEqual( + (core as { rawOpenaiContentParts?: unknown[] }).rawOpenaiContentParts, + [DSH_PART], + ); +}); + +test("plain tool results keep the legacy shape — no sidecar (#6875 negative)", () => { + const str = openaiToCore(toolBody("plain result")); + assert.equal(str.msgs.length, 1); + assert.equal(str.msgs[0]!.text, "plain result"); + assert.equal( + (str.msgs[0] as { rawOpenaiContentParts?: unknown[] }) + .rawOpenaiContentParts, + undefined, + ); + const textOnly = openaiToCore( + toolBody([ + { type: "text", text: "a" }, + { type: "text", text: "b" }, + ]), + ); + assert.equal(textOnly.msgs[0]!.text, "a\nb"); + assert.equal( + (textOnly.msgs[0] as { rawOpenaiContentParts?: unknown[] }) + .rawOpenaiContentParts, + undefined, + ); +}); + +test("a retained tool attachment round-trips verbatim; plain stays a string (#6875)", () => { + const parsed = openaiToCore( + toolBody([{ type: "text", text: "shot" }, DSH_PART]), + ); + const rebuilt = coreToOpenai(parsed.msgs); + assert.equal(rebuilt.length, 1); + assert.equal(rebuilt[0]!.role, "tool"); + assert.equal(rebuilt[0]!.tool_call_id, "call_1"); + assert.deepEqual(rebuilt[0]!.content, [ + { type: "text", text: "shot\n" }, + DSH_PART, + ]); + const plain = coreToOpenai(openaiToCore(toolBody("plain result")).msgs); + assert.deepEqual(plain, [ + { role: "tool", tool_call_id: "call_1", content: "plain result" }, + ]); +}); + +test("an image-only DSH tool result keeps its attachment, empty text (#6875)", () => { + const parsed = openaiToCore(toolBody([DSH_PART])); + assert.equal(parsed.msgs.length, 1); + const core = parsed.msgs[0]!; + assert.equal(core.text, ""); + assert.deepEqual( + (core as { rawOpenaiContentParts?: unknown[] }).rawOpenaiContentParts, + [DSH_PART], + ); + const rebuilt = coreToOpenai(parsed.msgs); + assert.deepEqual(rebuilt[0]!.content, [DSH_PART]); +}); + +test("non-attachment opaque tool parts keep the legacy text-only reduction (#6875)", () => { + const parsed = openaiToCore( + toolBody([ + { type: "text", text: "out" }, + { type: "image_url", image_url: { url: "https://example.com/i.png" } }, + { type: "image" }, + { type: "image", attachment: { mediaType: "image/png" } }, + ]), + ); + const core = parsed.msgs[0]!; + assert.equal( + (core as { rawOpenaiContentParts?: unknown[] }).rawOpenaiContentParts, + undefined, + "no sidecar — none of these parts is a DSH attachment part", + ); + const rebuilt = coreToOpenai(parsed.msgs); + assert.equal( + typeof rebuilt[0]!.content, + "string", + "legacy string-content shape, no parts array", + ); +}); + +test("a parsed DSH tool result is not unrecoverable media (#6875)", () => { + const parsed = openaiToCore( + toolBody([{ type: "text", text: "shot" }, DSH_PART]), + ); + assert.equal(hasUnrecoverableMediaPayload(parsed.msgs[0]!), false); +}); + +// Directed repro the maintainer asked for: with both halves in place, a +// stale BLOCKED binding on a DSH tool-result message migrates to a fresh +// number on the next assignRefs pass — no delete-the-library step. +test("stale BLOCKED on a parsed DSH tool result migrates to a fresh number (#6875)", () => { + const parsed = openaiToCore( + toolBody([{ type: "text", text: "shot" }, DSH_PART]), + ); + const core = parsed.msgs[0]!; + const state = createInitialState(); + state.messageRefs.byRaw[core.id] = BLOCKED_REF; + const res = assignRefs([core], { + existing: state.messageRefs, + nextIndex: 1, + isProtected: hasUnrecoverableMediaPayload, + }); + assert.equal( + res.map.byRaw[core.id], + "m00001", + "fresh number — never a reuse", + ); +}); diff --git a/package-lock.json b/package-lock.json index d12b556eb1..006bda1e14 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.198", + "version": "0.0.199", "dev": true, "license": "MIT", "devDependencies": { diff --git a/src/wire-drop-warn.ts b/src/wire-drop-warn.ts index 8f37e1375e..2b72c231f4 100644 --- a/src/wire-drop-warn.ts +++ b/src/wire-drop-warn.ts @@ -10,14 +10,28 @@ // (stringContent), so parts riding them (assistant-carried // images/attachments some clients emit) still vanish. Mid-conversation // system/developer messages ride verbatim via the rawOpenaiMessage sidecar -// since #4166 and no longer belong to the drop class. This detector scans -// the RAW body for that surviving drop class so the proxy can log a one-time -// warn instead of staying silent. +// since #4166 and no longer belong to the drop class. One exception: +// #6875 carries DSH read_image attachment parts ({type:"image", +// attachment:{attachmentId}}) on TOOL results through the +// rawOpenaiContentParts sidecar, so those are preserved, not dropped. +// This detector scans the RAW body for that surviving drop class so the +// proxy can log a one-time warn instead of staying silent. function isObj(v: unknown): v is Record { return typeof v === "object" && v !== null; } +// #6875: mirror of the codec's isDshAttachmentPart (kernel/src/wire/openai.ts) +// — a {type:"image", attachment:{attachmentId}} part rides the tool-result +// sidecar instead of being dropped, so the detector must not count it. +function isDshAttachmentPart(p: Record): boolean { + if (p.type !== "image") return false; + const attachment = p.attachment; + if (!isObj(attachment)) return false; + const id = attachment.attachmentId; + return typeof id === "string" && id.length > 0; +} + interface DroppedOpenaiPartsReport { count: number; types: string[]; @@ -47,6 +61,9 @@ export function droppedOpenaiParts(body: unknown): DroppedOpenaiPartsReport | nu if (m.role === "user") continue; for (const p of m.content) { if (!isObj(p) || DEFAULT_PRESERVED.has(typeof p.type === "string" ? p.type : "")) continue; + // #6875: a DSH attachment part on a tool result is preserved by + // the codec (rawOpenaiContentParts sidecar) — not dropped. + if (isDshAttachmentPart(p)) continue; count++; types.add(typeof p.type === "string" ? p.type : ""); if (firstIndex < 0) firstIndex = i; diff --git a/tests/fix-1205-file-parts.test.ts b/tests/fix-1205-file-parts.test.ts index 579204b3d1..e1069da679 100644 --- a/tests/fix-1205-file-parts.test.ts +++ b/tests/fix-1205-file-parts.test.ts @@ -92,6 +92,21 @@ test("droppedOpenaiParts applies the text-only preserved set to non-user roles", assert.deepEqual(droppedOpenaiParts(body), { count: 1, types: ["image_url"], firstIndex: 1 }); }); +// #6875: DSH read_image attachment parts on tool results are preserved by +// the codec (rawOpenaiContentParts sidecar), so the drop detector stays +// silent for them — but a bare {type:"image"} part with no attachment is +// still dropped and still reported. +test("droppedOpenaiParts stays silent for DSH attachment parts on tool results (#6875)", () => { + const dshPart = { type: "image", attachment: { attachmentId: "sha256:abc", mediaType: "image/png", bytes: 123 } }; + const body = openaiBody([{ role: "tool", tool_call_id: "c1", content: [{ type: "text", text: "shot" }, dshPart] }]); + assert.equal(droppedOpenaiParts(body), null); +}); + +test("droppedOpenaiParts still reports a bare image part with no attachment (#6875)", () => { + const body = openaiBody([{ role: "tool", tool_call_id: "c1", content: [{ type: "image", attachment: { mediaType: "image/png" } }] }]); + assert.deepEqual(droppedOpenaiParts(body), { count: 1, types: ["image"], firstIndex: 0 }); +}); + test("droppedOpenaiParts unions types across non-user messages and keeps the earliest index", () => { const body = openaiBody([ { role: "user", content: [{ type: "input_audio", input_audio: {} }, { type: "text", text: "ok" }] }, From 0bf0e2492ca2c7a3cda78f82d8c1fb4a7f286f9d Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 16:59:58 +0800 Subject: [PATCH 3/3] ci: re-trigger main test lanes (concurrency-group cancelled the opened run) The main CI workflow's runs were cancelled by the pull_request concurrency group before any jobs started (0 jobs), so the full test lanes never ran. This empty commit triggers a fresh synchronize event to re-run them.