From 4bead210c634d784aff79a8f9cad39a886334fa0 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 10 Nov 2026 11:33:24 +0800 Subject: [PATCH] fix: scope image encodes to their resize policy --- src/image-rescale.ts | 2 +- tests/issue3400-image-rescale.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/image-rescale.ts b/src/image-rescale.ts index 8fedd121a5..acc495376b 100644 --- a/src/image-rescale.ts +++ b/src/image-rescale.ts @@ -150,7 +150,7 @@ async function rescaleImage(mediaType: string, b64: string, maxDim: number): Pro const dims = decodeImageDims(b64); const gated = (dims !== undefined && Math.max(dims.w, dims.h) > maxDim) || origBytes > RESCALE_TRIGGER_BYTES; if (!gated) return null; - const hash = createHash("sha256").update(b64, "utf8").digest("hex"); + const hash = createHash("sha256").update(`${mime}:${maxDim}:${b64}`, "utf8").digest("hex"); let pending = encodeCache.get(hash); const cacheHit = pending !== undefined; if (!pending) { diff --git a/tests/issue3400-image-rescale.test.ts b/tests/issue3400-image-rescale.test.ts index 1c3cec9815..3395ab0cba 100644 --- a/tests/issue3400-image-rescale.test.ts +++ b/tests/issue3400-image-rescale.test.ts @@ -137,6 +137,31 @@ test("byte-stable: same history → identical outbound bytes across cache states assert.equal(out2, out1, "two independent encodes of the same history must be byte-identical (upstream prefix-cache protection)"); }); +for (const [firstDim, nextDim] of [[1600, 800], [800, 1600]]) { + test(`encode cache respects a changed dimension limit (${firstDim} to ${nextDim})`, async () => { + const { ctx } = ctxFor(`s-cache-dim-${firstDim}`); + const input = [anthropicImageMsg("msg_dim", BIG.b64)]; + const first = await applyImageRescalePass(input, { ...ctx, maxDim: firstDim }); + const next = await applyImageRescalePass(input, { ...ctx, maxDim: nextDim }); + const imageData = (messages: BiliMessage[]) => (messages[0].rawAnthropicBlock as { source: { data: string } }).source.data; + const decoded = await Jimp.read(Buffer.from(imageData(next), "base64")); + assert.equal(decoded.width, nextDim, "cached encodes must use the current policy"); + assert.notEqual(imageData(first), imageData(next)); + assert.deepEqual(await applyImageRescalePass(input, { ...ctx, maxDim: nextDim }), next, "the same policy still reuses byte-identical output"); + assert.equal(logLines.filter((line) => line.includes("[image-rescale]")).length, 2); + }); +} + +test("a cached low-benefit result does not suppress a later stricter resize", async () => { + const { ctx } = ctxFor("s-cache-null-dim"); + const input = [anthropicImageMsg("msg_null_dim", BMP.b64, "image/bmp")]; + assert.equal((await applyImageRescalePass(input, { ...ctx, maxDim: 1600 }))[0], input[0]); + const next = await applyImageRescalePass(input, { ...ctx, maxDim: 800 }); + const block = next[0].rawAnthropicBlock as { source: { data: string } }; + const decoded = await Jimp.read(Buffer.from(block.source.data, "base64")); + assert.equal(decoded.width, 800); +}); + test("openai singular image_url converts to plural parts with inline note", async () => { const { session, ctx } = ctxFor("s-openai"); session.state.messageRefs.byRaw["msg_o"] = "m20001";