Skip to content

Commit f0b4077

Browse files
lonexrebvincentkoc
andauthored
fix(agents): sign the resize log byte delta from its value (#123411)
* fix(agents): sign resize log byte deltas Co-authored-by: Shubhankar Tripathy <[email protected]> Punchcard-Session: brisk-cedar-valley-dv * test(agents): cover resize log delta signs Co-authored-by: Shubhankar Tripathy <[email protected]> Punchcard-Session: brisk-cedar-valley-dv --------- Co-authored-by: Vincent Koc <[email protected]>
1 parent 5a0a96f commit f0b4077

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎src/agents/tool-images.log.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
44
import { createSolidPngBuffer } from "../../test/helpers/image-fixtures.js";
55

6-
const { infoMock, warnMock } = vi.hoisted(() => ({
6+
const { infoMock, resizeToJpegMock, warnMock } = vi.hoisted(() => ({
77
infoMock: vi.fn(),
8+
resizeToJpegMock: vi.fn(),
89
warnMock: vi.fn(),
910
}));
1011

@@ -24,6 +25,16 @@ vi.mock("../logging/subsystem.js", () => {
2425
return { createSubsystemLogger: () => makeLogger() };
2526
});
2627

28+
vi.mock("../media/media-services.js", () => ({
29+
buildImageResizeSideGrid: () => [2000],
30+
getImageMetadata: vi.fn(),
31+
IMAGE_REDUCE_QUALITY_STEPS: [85],
32+
isImageProcessorUnavailableError: () => false,
33+
MAX_IMAGE_INPUT_PIXELS: 25_000_000,
34+
readImageMetadataFromHeader: () => ({ width: 2001, height: 8 }),
35+
resizeToJpeg: resizeToJpegMock,
36+
}));
37+
2738
import { sanitizeContentBlocksImages } from "./tool-images.js";
2839

2940
async function createLargePng(): Promise<Buffer> {
@@ -39,6 +50,8 @@ describe("tool-images log context", () => {
3950

4051
beforeEach(() => {
4152
infoMock.mockClear();
53+
resizeToJpegMock.mockReset();
54+
resizeToJpegMock.mockResolvedValue(Buffer.alloc(100, 2));
4255
warnMock.mockClear();
4356
});
4457

@@ -50,4 +63,31 @@ describe("tool-images log context", () => {
5063
const messages = infoMock.mock.calls.map((call) => String(call[0] ?? ""));
5164
expect(messages.join("\n")).toContain("sample-diagram.png");
5265
});
66+
67+
it.each([
68+
{ sourceBytes: 200, outputBytes: 150, pct: 25, label: "-25%" },
69+
{ sourceBytes: 200, outputBytes: 200, pct: 0, label: "0%" },
70+
{ sourceBytes: 200, outputBytes: 250, pct: -25, label: "+25%" },
71+
{ sourceBytes: 3, outputBytes: 2, pct: 33.3, label: "-33.3%" },
72+
])("logs the signed byte delta for $label", async ({ sourceBytes, outputBytes, pct, label }) => {
73+
resizeToJpegMock.mockResolvedValue(Buffer.alloc(outputBytes, 2));
74+
const blocks = [
75+
{
76+
type: "image" as const,
77+
data: Buffer.alloc(sourceBytes, 1).toString("base64"),
78+
mimeType: "image/png",
79+
},
80+
];
81+
await sanitizeContentBlocksImages(blocks, "read:/tmp/images/sample-diagram.png");
82+
const [message, context] = infoMock.mock.calls.at(-1) ?? [];
83+
expect(message).toContain(`sample-diagram.png 2001x8px`);
84+
expect(message).toContain(`(${label})`);
85+
expect(message).not.toContain("(--");
86+
expect(context).toMatchObject({
87+
fileName: "sample-diagram.png",
88+
sourceBytes,
89+
outputBytes,
90+
byteReductionPct: pct,
91+
});
92+
});
5393
});

‎src/agents/tool-images.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ async function resizeImageBase64IfNeeded(params: {
249249
? Number((((buf.byteLength - out.byteLength) / buf.byteLength) * 100).toFixed(1))
250250
: 0;
251251
log.info(
252-
`Image resized to fit limits: ${sourceWithFile} ${formatBytesShort(buf.byteLength)} -> ${formatBytesShort(out.byteLength)} (-${byteReductionPct}%)`,
252+
`Image resized to fit limits: ${sourceWithFile} ${formatBytesShort(buf.byteLength)} -> ${formatBytesShort(out.byteLength)} (${byteReductionPct < 0 ? "+" : ""}${-byteReductionPct}%)`,
253253
{
254254
label: params.label,
255255
fileName: params.fileName,

0 commit comments

Comments
 (0)