@vellumai/assistant 0.11.4-dev.202608192308.82015f7 → 0.11.4-dev.202608200132.719ec7f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/__tests__/compactor-retained-image-validation.test.ts +142 -0
- package/src/__tests__/image-conversion.test.ts +143 -1
- package/src/config/feature-flag-registry.json +1 -1
- package/src/context/compactor.ts +31 -2
- package/src/daemon/__tests__/provider-rejection-log-fields.test.ts +272 -0
- package/src/daemon/conversation-agent-loop-handlers.ts +4 -1
- package/src/daemon/provider-rejection-log-fields.ts +124 -0
- package/src/util/image-conversion.ts +138 -5
package/package.json
CHANGED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compaction must never bake invalid image bytes into the rebuilt history.
|
|
3
|
+
*
|
|
4
|
+
* Retained images are re-hydrated from the attachment store and re-optimized
|
|
5
|
+
* at compaction time. If that pipeline yields bytes that no longer parse as an
|
|
6
|
+
* image (e.g. a corrupt conversion-cache entry), embedding them would make the
|
|
7
|
+
* provider reject EVERY subsequent turn with a 400: the conversation wedges
|
|
8
|
+
* until the block is manually removed. `buildRetainedImageBlocks` drops such
|
|
9
|
+
* payloads instead.
|
|
10
|
+
*/
|
|
11
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
buildRetainedImageBlocks,
|
|
15
|
+
collectImageManifest,
|
|
16
|
+
} from "../context/compactor.js";
|
|
17
|
+
import { attachInlineAttachmentToMessage } from "../persistence/attachments-store.js";
|
|
18
|
+
import {
|
|
19
|
+
addMessage,
|
|
20
|
+
createConversation,
|
|
21
|
+
} from "../persistence/conversation-crud.js";
|
|
22
|
+
import { getDb } from "../persistence/db-connection.js";
|
|
23
|
+
import { initializeDb } from "../persistence/db-init.js";
|
|
24
|
+
|
|
25
|
+
await initializeDb();
|
|
26
|
+
|
|
27
|
+
// 1x1 transparent PNG.
|
|
28
|
+
const PNG_1X1_BASE64 =
|
|
29
|
+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
|
|
30
|
+
|
|
31
|
+
// Declared image/png, but the bytes are not any known image format:
|
|
32
|
+
// the stand-in for a corrupted payload surviving in the pipeline.
|
|
33
|
+
const GARBAGE_BASE64 = Buffer.from("definitely not an image").toString(
|
|
34
|
+
"base64",
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
function resetTables(): void {
|
|
38
|
+
const db = getDb();
|
|
39
|
+
db.run("DELETE FROM message_attachments");
|
|
40
|
+
db.run("DELETE FROM attachments");
|
|
41
|
+
db.run("DELETE FROM messages");
|
|
42
|
+
db.run("DELETE FROM conversations");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function addImageMessage(
|
|
46
|
+
conversationId: string,
|
|
47
|
+
filename: string,
|
|
48
|
+
dataBase64: string,
|
|
49
|
+
): Promise<void> {
|
|
50
|
+
const inserted = await addMessage(
|
|
51
|
+
conversationId,
|
|
52
|
+
"user",
|
|
53
|
+
JSON.stringify([{ type: "text", text: filename }]),
|
|
54
|
+
{
|
|
55
|
+
metadata: { provenanceTrustClass: "guardian" },
|
|
56
|
+
skipIndexing: true,
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
await attachInlineAttachmentToMessage(
|
|
60
|
+
inserted.id,
|
|
61
|
+
0,
|
|
62
|
+
filename,
|
|
63
|
+
"image/png",
|
|
64
|
+
dataBase64,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
describe("buildRetainedImageBlocks corrupt-bytes gate", () => {
|
|
69
|
+
beforeEach(resetTables);
|
|
70
|
+
|
|
71
|
+
test("valid image bytes are retained", async () => {
|
|
72
|
+
const conv = createConversation();
|
|
73
|
+
await addImageMessage(conv.id, "good.png", PNG_1X1_BASE64);
|
|
74
|
+
const manifest = collectImageManifest(conv.id, "guardian");
|
|
75
|
+
|
|
76
|
+
const { blocks, resolved, missing } = await buildRetainedImageBlocks(
|
|
77
|
+
["good.png"],
|
|
78
|
+
manifest,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
expect(resolved).toEqual(["good.png"]);
|
|
82
|
+
expect(missing).toEqual([]);
|
|
83
|
+
expect(blocks).toHaveLength(1);
|
|
84
|
+
expect(blocks[0].source.type).toBe("base64");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("bytes that do not parse as an image are dropped, not embedded", async () => {
|
|
88
|
+
const conv = createConversation();
|
|
89
|
+
await addImageMessage(conv.id, "corrupt.png", GARBAGE_BASE64);
|
|
90
|
+
await addImageMessage(conv.id, "good.png", PNG_1X1_BASE64);
|
|
91
|
+
const manifest = collectImageManifest(conv.id, "guardian");
|
|
92
|
+
|
|
93
|
+
const { blocks, resolved, missing } = await buildRetainedImageBlocks(
|
|
94
|
+
["corrupt.png", "good.png"],
|
|
95
|
+
manifest,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
// The corrupt payload is dropped (not listed as manifest-missing, since
|
|
99
|
+
// it resolved fine; its bytes were the problem) and the valid one
|
|
100
|
+
// survives.
|
|
101
|
+
expect(resolved).toEqual(["good.png"]);
|
|
102
|
+
expect(missing).toEqual([]);
|
|
103
|
+
expect(blocks).toHaveLength(1);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("a truncated JPEG (valid SOI header, no EOI) is dropped", async () => {
|
|
107
|
+
// A JPEG torn mid-write keeps its SOI header, so a format sniff alone
|
|
108
|
+
// accepts it; the gate must also require an EOI marker.
|
|
109
|
+
const tornJpeg = Buffer.concat([
|
|
110
|
+
Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10]),
|
|
111
|
+
Buffer.from("JFIF\0"),
|
|
112
|
+
Buffer.alloc(64),
|
|
113
|
+
]).toString("base64");
|
|
114
|
+
const conv = createConversation();
|
|
115
|
+
await addImageMessage(conv.id, "torn.jpg", tornJpeg);
|
|
116
|
+
const manifest = collectImageManifest(conv.id, "guardian");
|
|
117
|
+
|
|
118
|
+
const { blocks, resolved, missing } = await buildRetainedImageBlocks(
|
|
119
|
+
["torn.jpg"],
|
|
120
|
+
manifest,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
expect(resolved).toEqual([]);
|
|
124
|
+
expect(missing).toEqual([]);
|
|
125
|
+
expect(blocks).toHaveLength(0);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("unresolvable filenames still land in missing", async () => {
|
|
129
|
+
const conv = createConversation();
|
|
130
|
+
await addImageMessage(conv.id, "good.png", PNG_1X1_BASE64);
|
|
131
|
+
const manifest = collectImageManifest(conv.id, "guardian");
|
|
132
|
+
|
|
133
|
+
const { blocks, resolved, missing } = await buildRetainedImageBlocks(
|
|
134
|
+
["never-uploaded.png", "good.png"],
|
|
135
|
+
manifest,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
expect(missing).toEqual(["never-uploaded.png"]);
|
|
139
|
+
expect(resolved).toEqual(["good.png"]);
|
|
140
|
+
expect(blocks).toHaveLength(1);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
* gated on darwin.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
10
11
|
import { tmpdir } from "node:os";
|
|
11
12
|
import { join } from "node:path";
|
|
12
13
|
import { beforeAll, describe, expect, test } from "bun:test";
|
|
13
14
|
|
|
14
15
|
import {
|
|
15
16
|
convertImageToJpeg,
|
|
17
|
+
hasValidJpegStructure,
|
|
18
|
+
isCompleteJpeg,
|
|
16
19
|
isHeifImage,
|
|
17
20
|
jpegFilenameFor,
|
|
18
21
|
normalizeImageBase64,
|
|
@@ -131,6 +134,119 @@ describe("sniffImageMimeType", () => {
|
|
|
131
134
|
});
|
|
132
135
|
});
|
|
133
136
|
|
|
137
|
+
describe("isCompleteJpeg", () => {
|
|
138
|
+
test("accepts SOI...EOI framed payloads", () => {
|
|
139
|
+
expect(
|
|
140
|
+
isCompleteJpeg(Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0xff, 0xd9])),
|
|
141
|
+
).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("rejects empty, truncated, and non-JPEG payloads", () => {
|
|
145
|
+
expect(isCompleteJpeg(Buffer.alloc(0))).toBe(false);
|
|
146
|
+
// Valid head, torn tail: the poisoned-cache signature.
|
|
147
|
+
expect(isCompleteJpeg(Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00]))).toBe(
|
|
148
|
+
false,
|
|
149
|
+
);
|
|
150
|
+
expect(isCompleteJpeg(PNG_1PX_BYTES)).toBe(false);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Mirrors the converter's cache-key derivation so the test can plant a
|
|
155
|
+
// poisoned entry at the exact path a conversion will consult.
|
|
156
|
+
function cacheKeyFor(bytes: Uint8Array, quality: number): string {
|
|
157
|
+
const hash = createHash("sha256").update(bytes).digest("hex").slice(0, 16);
|
|
158
|
+
return `${hash}-full-q${quality}`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Minimal structurally valid JPEG: SOI, APP0 (len 4, 2 payload bytes),
|
|
162
|
+
// SOF0 (len 5, 3 payload bytes), SOS (len 3, 1 payload byte), entropy data
|
|
163
|
+
// with a stuffed FF, then EOI.
|
|
164
|
+
function minimalJpeg(extra: {
|
|
165
|
+
trailing?: Uint8Array;
|
|
166
|
+
dropEoi?: boolean;
|
|
167
|
+
}): Buffer {
|
|
168
|
+
const parts: Uint8Array[] = [
|
|
169
|
+
Buffer.from([0xff, 0xd8]), // SOI
|
|
170
|
+
Buffer.from([0xff, 0xe0, 0x00, 0x04, 0x4a, 0x46]), // APP0
|
|
171
|
+
Buffer.from([0xff, 0xc0, 0x00, 0x05, 0x08, 0x00, 0x01]), // SOF0
|
|
172
|
+
Buffer.from([0xff, 0xda, 0x00, 0x03, 0x01]), // SOS header
|
|
173
|
+
Buffer.from([0x12, 0xff, 0x00, 0x34]), // entropy data with stuffed FF
|
|
174
|
+
];
|
|
175
|
+
if (!extra.dropEoi) {
|
|
176
|
+
parts.push(Buffer.from([0xff, 0xd9])); // EOI
|
|
177
|
+
}
|
|
178
|
+
if (extra.trailing) {
|
|
179
|
+
parts.push(extra.trailing);
|
|
180
|
+
}
|
|
181
|
+
return Buffer.concat(parts);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
describe("hasValidJpegStructure", () => {
|
|
185
|
+
test("accepts a JPEG with trailing bytes after the EOI marker", () => {
|
|
186
|
+
// Some encoders append padding or metadata past the EOI; the structural
|
|
187
|
+
// walk tolerates it where isCompleteJpeg's exact tail framing does not.
|
|
188
|
+
const padded = minimalJpeg({ trailing: Buffer.from([0x00, 0x00]) });
|
|
189
|
+
expect(hasValidJpegStructure(padded)).toBe(true);
|
|
190
|
+
expect(isCompleteJpeg(padded)).toBe(false);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("accepts restart markers inside entropy-coded data", () => {
|
|
194
|
+
const withRst = Buffer.concat([
|
|
195
|
+
Buffer.from([0xff, 0xd8]),
|
|
196
|
+
Buffer.from([0xff, 0xc0, 0x00, 0x05, 0x08, 0x00, 0x01]), // SOF0
|
|
197
|
+
Buffer.from([0xff, 0xda, 0x00, 0x03, 0x01]),
|
|
198
|
+
Buffer.from([0x12, 0xff, 0xd0, 0x34]), // RST0 continues the scan
|
|
199
|
+
Buffer.from([0xff, 0xd9]),
|
|
200
|
+
]);
|
|
201
|
+
expect(hasValidJpegStructure(withRst)).toBe(true);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("rejects a truncated JPEG whose metadata segment contains FF D9", () => {
|
|
205
|
+
// An EXIF-style APP1 payload can embed a complete thumbnail JPEG, so an
|
|
206
|
+
// FF D9 pair appears inside the segment while the main image is torn. A
|
|
207
|
+
// raw byte search would accept this; the marker walk skips the segment
|
|
208
|
+
// payload and reports the truncation.
|
|
209
|
+
const tornWithThumbnail = Buffer.concat([
|
|
210
|
+
Buffer.from([0xff, 0xd8]), // SOI
|
|
211
|
+
Buffer.from([0xff, 0xe1, 0x00, 0x08]), // APP1, length 8
|
|
212
|
+
Buffer.from([0x45, 0x78, 0xff, 0xd9, 0x00, 0x00]), // payload with FF D9
|
|
213
|
+
Buffer.from([0xff, 0xda, 0x00, 0x03, 0x01, 0x12, 0x34]), // torn scan
|
|
214
|
+
]);
|
|
215
|
+
expect(hasValidJpegStructure(tornWithThumbnail)).toBe(false);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("rejects truncated JPEGs and non-JPEG payloads", () => {
|
|
219
|
+
expect(hasValidJpegStructure(minimalJpeg({ dropEoi: true }))).toBe(false);
|
|
220
|
+
expect(
|
|
221
|
+
hasValidJpegStructure(Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00])),
|
|
222
|
+
).toBe(false);
|
|
223
|
+
expect(hasValidJpegStructure(Buffer.alloc(0))).toBe(false);
|
|
224
|
+
expect(hasValidJpegStructure(PNG_1PX_BYTES)).toBe(false);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("rejects a top-level EOI with no frame or scan", () => {
|
|
228
|
+
// Bare SOI+EOI is structurally walkable but carries no image data;
|
|
229
|
+
// providers reject it, so the gate must too.
|
|
230
|
+
expect(hasValidJpegStructure(Buffer.from([0xff, 0xd8, 0xff, 0xd9]))).toBe(
|
|
231
|
+
false,
|
|
232
|
+
);
|
|
233
|
+
// A scan without a frame header is equally undecodable.
|
|
234
|
+
const scanNoFrame = Buffer.concat([
|
|
235
|
+
Buffer.from([0xff, 0xd8]),
|
|
236
|
+
Buffer.from([0xff, 0xda, 0x00, 0x03, 0x01, 0x12]),
|
|
237
|
+
Buffer.from([0xff, 0xd9]),
|
|
238
|
+
]);
|
|
239
|
+
expect(hasValidJpegStructure(scanNoFrame)).toBe(false);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("rejects a segment length that runs past the buffer", () => {
|
|
243
|
+
// A declared length larger than the remaining bytes is truncation, not a
|
|
244
|
+
// reason to scan beyond the buffer.
|
|
245
|
+
const overrun = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0xff, 0xff, 0x00]);
|
|
246
|
+
expect(hasValidJpegStructure(overrun)).toBe(false);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
134
250
|
describe("normalizeImageBytes passthrough", () => {
|
|
135
251
|
test("non-HEIF bytes pass through untouched", async () => {
|
|
136
252
|
const result = await normalizeImageBytes("image/png", PNG_1PX_BYTES);
|
|
@@ -205,6 +321,9 @@ describe.skipIf(process.platform !== "darwin")(
|
|
|
205
321
|
const converted = await convertImageToJpeg(heicBytes);
|
|
206
322
|
expect(converted).not.toBeNull();
|
|
207
323
|
expect(startsWithJpegMagic(converted!)).toBe(true);
|
|
324
|
+
// A real encoder's output must pass the structural walk, or the
|
|
325
|
+
// compactor gate would drop every legitimately converted image.
|
|
326
|
+
expect(hasValidJpegStructure(converted!)).toBe(true);
|
|
208
327
|
});
|
|
209
328
|
|
|
210
329
|
test("conversion options produce distinct outputs (cache key isolation)", async () => {
|
|
@@ -228,6 +347,29 @@ describe.skipIf(process.platform !== "darwin")(
|
|
|
228
347
|
expect(first!.equals(second!)).toBe(true);
|
|
229
348
|
});
|
|
230
349
|
|
|
350
|
+
test("a poisoned (truncated) cache entry is discarded, not returned", async () => {
|
|
351
|
+
// Distinct quality so this test owns its cache key.
|
|
352
|
+
const quality = 77;
|
|
353
|
+
const cacheDir = join(tmpdir(), "vellum-optimized-images");
|
|
354
|
+
mkdirSync(cacheDir, { recursive: true });
|
|
355
|
+
const poisonedPath = join(
|
|
356
|
+
cacheDir,
|
|
357
|
+
`${cacheKeyFor(heicBytes, quality)}.jpg`,
|
|
358
|
+
);
|
|
359
|
+
// A torn write: valid JPEG head, no EOI tail. readFromCache must
|
|
360
|
+
// discard it rather than return it as the converted image.
|
|
361
|
+
writeFileSync(poisonedPath, Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00]));
|
|
362
|
+
|
|
363
|
+
const converted = await convertImageToJpeg(heicBytes, { quality });
|
|
364
|
+
expect(converted).not.toBeNull();
|
|
365
|
+
expect(isCompleteJpeg(converted!)).toBe(true);
|
|
366
|
+
|
|
367
|
+
// The poisoned entry was replaced by the re-converted result.
|
|
368
|
+
expect(existsSync(poisonedPath)).toBe(true);
|
|
369
|
+
const { readFileSync } = await import("node:fs");
|
|
370
|
+
expect(isCompleteJpeg(readFileSync(poisonedPath))).toBe(true);
|
|
371
|
+
});
|
|
372
|
+
|
|
231
373
|
test("normalizeImageBytes converts to a JPEG master", async () => {
|
|
232
374
|
const result = await normalizeImageBytes("image/heic", heicBytes);
|
|
233
375
|
expect(result.converted).toBe(true);
|
|
@@ -145,7 +145,7 @@
|
|
|
145
145
|
"scope": "client",
|
|
146
146
|
"key": "fork-from-message",
|
|
147
147
|
"label": "Fork from Message",
|
|
148
|
-
"description": "Show the 'Fork from here'
|
|
148
|
+
"description": "Show the fork affordance: 'Fork from here' on messages and 'Fork Conversation' in the thread menu. Staff-only: the client also requires the viewer to be Vellum staff, so enabling this for a non-staff account still shows nothing.",
|
|
149
149
|
"defaultEnabled": false
|
|
150
150
|
},
|
|
151
151
|
{
|
package/src/context/compactor.ts
CHANGED
|
@@ -39,6 +39,10 @@ import type {
|
|
|
39
39
|
} from "../providers/types.js";
|
|
40
40
|
import { type TrustClass } from "../runtime/actor-trust-resolver.js";
|
|
41
41
|
import { resolveCapabilities } from "../runtime/capabilities.js";
|
|
42
|
+
import {
|
|
43
|
+
hasValidJpegStructure,
|
|
44
|
+
sniffImageMimeType,
|
|
45
|
+
} from "../util/image-conversion.js";
|
|
42
46
|
import { getLogger } from "../util/logger.js";
|
|
43
47
|
import { preModelCallSanitize } from "./outbound-sanitize.js";
|
|
44
48
|
import { stripInjectionsForCompaction } from "./strip-injections.js";
|
|
@@ -865,7 +869,8 @@ function resolveTailFloorIndex(messages: Message[], tailIndex: number): number {
|
|
|
865
869
|
// Retained-image hydration
|
|
866
870
|
// ---------------------------------------------------------------------------
|
|
867
871
|
|
|
868
|
-
|
|
872
|
+
/** Exported for unit testing (the corrupt-bytes drop gate). */
|
|
873
|
+
export async function buildRetainedImageBlocks(
|
|
869
874
|
filenames: string[],
|
|
870
875
|
manifest: ManifestEntry[],
|
|
871
876
|
): Promise<{ blocks: ImageContent[]; resolved: string[]; missing: string[] }> {
|
|
@@ -883,7 +888,11 @@ async function buildRetainedImageBlocks(
|
|
|
883
888
|
missing.push(name);
|
|
884
889
|
continue;
|
|
885
890
|
}
|
|
886
|
-
|
|
891
|
+
// Sniff the declared MIME from the stored bytes rather than the filename:
|
|
892
|
+
// clients derive extensions from user input, and providers reject an image
|
|
893
|
+
// whose bytes disagree with the declared media type.
|
|
894
|
+
const sourceMime =
|
|
895
|
+
sniffImageMimeType(content) ?? guessMimeFromFilename(name);
|
|
887
896
|
// Run the same downscale pass the agent uses when first sending an
|
|
888
897
|
// image. Without this, attachments that exceed the provider's per-image
|
|
889
898
|
// byte limit (Anthropic: 5 MB) crash the next turn after compaction.
|
|
@@ -891,6 +900,26 @@ async function buildRetainedImageBlocks(
|
|
|
891
900
|
content.toString("base64"),
|
|
892
901
|
sourceMime,
|
|
893
902
|
);
|
|
903
|
+
// Last-line gate before the bytes are baked into the rebuilt history: a
|
|
904
|
+
// corrupt payload would be rejected by the provider with a 400 on EVERY
|
|
905
|
+
// subsequent turn, wedging the conversation. Dropping the image loses a
|
|
906
|
+
// retained picture; shipping it loses the conversation. Format sniffing
|
|
907
|
+
// alone is not enough for JPEG: a truncated JPEG (e.g. persisted from a
|
|
908
|
+
// torn conversion-cache read) keeps its SOI header, so JPEG payloads must
|
|
909
|
+
// also walk to a terminal EOI marker.
|
|
910
|
+
const optimizedBytes = Buffer.from(optimized.data, "base64");
|
|
911
|
+
const optimizedFormat = sniffImageMimeType(optimizedBytes);
|
|
912
|
+
const bytesAreValidImage =
|
|
913
|
+
optimizedFormat != null &&
|
|
914
|
+
(optimizedFormat !== "image/jpeg" ||
|
|
915
|
+
hasValidJpegStructure(optimizedBytes));
|
|
916
|
+
if (!bytesAreValidImage) {
|
|
917
|
+
log.warn(
|
|
918
|
+
{ filename: name, attachmentId: entry.attachmentId },
|
|
919
|
+
"Retained image bytes are not a valid image after transport optimization; dropping",
|
|
920
|
+
);
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
894
923
|
blocks.push({
|
|
895
924
|
type: "image",
|
|
896
925
|
source: {
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests that an unclassified 4xx provider rejection logs the upstream
|
|
3
|
+
* response body (bounded) plus the offending request content-part coordinates,
|
|
4
|
+
* so the failing message can be identified from the daemon log alone.
|
|
5
|
+
*/
|
|
6
|
+
import { beforeEach, describe, expect, mock, test } from "bun:test";
|
|
7
|
+
|
|
8
|
+
import type { AssistantEvent } from "../../api/index.js";
|
|
9
|
+
import { ProviderError } from "../../util/errors.js";
|
|
10
|
+
import * as loggerModule from "../../util/logger.js";
|
|
11
|
+
import type {
|
|
12
|
+
EventHandlerDeps,
|
|
13
|
+
EventHandlerState,
|
|
14
|
+
} from "../conversation-agent-loop-handlers.js";
|
|
15
|
+
import {
|
|
16
|
+
buildProviderRejectionLogFields,
|
|
17
|
+
MAX_LOGGED_UPSTREAM_BODY_CHARS,
|
|
18
|
+
} from "../provider-rejection-log-fields.js";
|
|
19
|
+
|
|
20
|
+
const logRecords: Record<string, unknown>[] = [];
|
|
21
|
+
|
|
22
|
+
mock.module("../../util/logger.js", () => ({
|
|
23
|
+
...loggerModule,
|
|
24
|
+
getLogger: () => ({
|
|
25
|
+
error: (record: Record<string, unknown>) => {
|
|
26
|
+
logRecords.push(record);
|
|
27
|
+
},
|
|
28
|
+
warn: () => {},
|
|
29
|
+
info: () => {},
|
|
30
|
+
debug: () => {},
|
|
31
|
+
trace: () => {},
|
|
32
|
+
}),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
// The handlers module binds its logger at import time, so it is loaded
|
|
36
|
+
// dynamically once the logger stub is installed above.
|
|
37
|
+
const { createEventHandlerState, dispatchAgentEvent } =
|
|
38
|
+
await import("../conversation-agent-loop-handlers.js");
|
|
39
|
+
|
|
40
|
+
function createDeps(): EventHandlerDeps {
|
|
41
|
+
return {
|
|
42
|
+
ctx: {
|
|
43
|
+
conversationId: "conv-provider-rejection",
|
|
44
|
+
provider: { name: "openai" },
|
|
45
|
+
streamThinking: false,
|
|
46
|
+
emitActivityState: () => {},
|
|
47
|
+
markWorkspaceTopLevelDirty: () => {},
|
|
48
|
+
currentTurnSurfaces: [],
|
|
49
|
+
} as unknown as EventHandlerDeps["ctx"],
|
|
50
|
+
onEvent: (_msg: AssistantEvent) => {},
|
|
51
|
+
reqId: "req-provider-rejection",
|
|
52
|
+
isFirstMessage: false,
|
|
53
|
+
shouldGenerateTitle: false,
|
|
54
|
+
rlog: new Proxy({} as Record<string, unknown>, {
|
|
55
|
+
get: () => () => {},
|
|
56
|
+
}) as unknown as EventHandlerDeps["rlog"],
|
|
57
|
+
turnChannelContext: {
|
|
58
|
+
userMessageChannel: "vellum",
|
|
59
|
+
assistantMessageChannel: "vellum",
|
|
60
|
+
} as EventHandlerDeps["turnChannelContext"],
|
|
61
|
+
turnInterfaceContext: {
|
|
62
|
+
userMessageInterface: "macos",
|
|
63
|
+
assistantMessageInterface: "macos",
|
|
64
|
+
} as EventHandlerDeps["turnInterfaceContext"],
|
|
65
|
+
} as EventHandlerDeps;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
describe("buildProviderRejectionLogFields", () => {
|
|
69
|
+
test("carries the verbatim upstream body", () => {
|
|
70
|
+
// GIVEN a provider rejection that captured the upstream 400 body
|
|
71
|
+
const rawBody = JSON.stringify({
|
|
72
|
+
error: {
|
|
73
|
+
message: "Invalid body: failed to parse JSON value from the request.",
|
|
74
|
+
type: "invalid_request_error",
|
|
75
|
+
code: "invalid_body",
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
const error = new ProviderError(
|
|
79
|
+
"API error (400): Invalid body",
|
|
80
|
+
"openai",
|
|
81
|
+
400,
|
|
82
|
+
{
|
|
83
|
+
rawBody,
|
|
84
|
+
apiErrorCode: "invalid_body",
|
|
85
|
+
apiErrorType: "invalid_request_error",
|
|
86
|
+
requestId: "req_abc123",
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
// WHEN diagnostic log fields are built for it
|
|
91
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
92
|
+
|
|
93
|
+
// THEN the body and the upstream metadata ride the record
|
|
94
|
+
expect(fields.upstreamErrorBody).toBe(rawBody);
|
|
95
|
+
expect(fields.upstreamErrorBodyTruncated).toBeUndefined();
|
|
96
|
+
expect(fields.apiErrorCode).toBe("invalid_body");
|
|
97
|
+
expect(fields.apiErrorType).toBe("invalid_request_error");
|
|
98
|
+
expect(fields.requestId).toBe("req_abc123");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("bounds the body to 4 KB and flags the truncation", () => {
|
|
102
|
+
// GIVEN an upstream body larger than the logged ceiling
|
|
103
|
+
const rawBody = "x".repeat(MAX_LOGGED_UPSTREAM_BODY_CHARS + 500);
|
|
104
|
+
const error = new ProviderError("API error (400): huge", "openai", 400, {
|
|
105
|
+
rawBody,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// WHEN diagnostic log fields are built for it
|
|
109
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
110
|
+
|
|
111
|
+
// THEN the logged body stops at the ceiling
|
|
112
|
+
expect(fields.upstreamErrorBody).toHaveLength(
|
|
113
|
+
MAX_LOGGED_UPSTREAM_BODY_CHARS,
|
|
114
|
+
);
|
|
115
|
+
// AND the record says the body was cut
|
|
116
|
+
expect(fields.upstreamErrorBodyTruncated).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test("parses the offending content-part coordinates for oversized strings", () => {
|
|
120
|
+
// GIVEN an oversized-content-part rejection naming the request position
|
|
121
|
+
const error = new ProviderError(
|
|
122
|
+
"API error (400): Invalid 'input[191].content[1].text': string too long. Expected a string with maximum length 10485760, but got a string with length 11436754 instead.",
|
|
123
|
+
"openai",
|
|
124
|
+
400,
|
|
125
|
+
{ apiErrorCode: "string_above_max_length" },
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// WHEN diagnostic log fields are built for it
|
|
129
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
130
|
+
|
|
131
|
+
// THEN the message and content indices are logged for a follow-up query
|
|
132
|
+
expect(fields.offendingMessageIndex).toBe(191);
|
|
133
|
+
expect(fields.offendingContentIndex).toBe(1);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("parses coordinates out of the Chat Completions `messages[N]` shape", () => {
|
|
137
|
+
// GIVEN a rejection using the Chat Completions request shape
|
|
138
|
+
const error = new ProviderError(
|
|
139
|
+
"API error (400): Invalid 'messages[7].content[0].text': string too long.",
|
|
140
|
+
"openai",
|
|
141
|
+
400,
|
|
142
|
+
{ apiErrorCode: "string_above_max_length" },
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// WHEN diagnostic log fields are built for it
|
|
146
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
147
|
+
|
|
148
|
+
// THEN the same coordinates are extracted
|
|
149
|
+
expect(fields.offendingMessageIndex).toBe(7);
|
|
150
|
+
expect(fields.offendingContentIndex).toBe(0);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("leaves coordinates absent for codes that carry no content-part pointer", () => {
|
|
154
|
+
// GIVEN a rejection whose code is outside the narrow tap
|
|
155
|
+
const error = new ProviderError(
|
|
156
|
+
"API error (400): Invalid 'input[3].content[0].text': something else.",
|
|
157
|
+
"openai",
|
|
158
|
+
400,
|
|
159
|
+
{ apiErrorCode: "model_not_found" },
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
// WHEN diagnostic log fields are built for it
|
|
163
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
164
|
+
|
|
165
|
+
// THEN no coordinates are inferred from a coincidental match
|
|
166
|
+
expect(fields.offendingMessageIndex).toBeUndefined();
|
|
167
|
+
expect(fields.offendingContentIndex).toBeUndefined();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("scrubs secrets out of the upstream body", () => {
|
|
171
|
+
// GIVEN an upstream body that echoed back a credential
|
|
172
|
+
const projectKey = `sk-proj-${"A".repeat(48)}`;
|
|
173
|
+
const error = new ProviderError("API error (400): bad key", "openai", 400, {
|
|
174
|
+
rawBody: JSON.stringify({
|
|
175
|
+
error: { message: `Incorrect API key provided: ${projectKey}` },
|
|
176
|
+
headers: { authorization: "Bearer some-token-value" },
|
|
177
|
+
}),
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// WHEN diagnostic log fields are built for it
|
|
181
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
182
|
+
|
|
183
|
+
// THEN the credential never reaches the log record
|
|
184
|
+
expect(fields.upstreamErrorBody).not.toContain(projectKey);
|
|
185
|
+
expect(fields.upstreamErrorBody).not.toContain("some-token-value");
|
|
186
|
+
expect(fields.upstreamErrorBody).toContain("[REDACTED]");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("reports a null body for errors that are not provider errors", () => {
|
|
190
|
+
// GIVEN a non-provider error
|
|
191
|
+
const error = new Error("socket hang up");
|
|
192
|
+
|
|
193
|
+
// WHEN diagnostic log fields are built for it
|
|
194
|
+
const fields = buildProviderRejectionLogFields(error);
|
|
195
|
+
|
|
196
|
+
// THEN the body is explicitly null rather than missing
|
|
197
|
+
expect(fields.upstreamErrorBody).toBeNull();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("unclassified 4xx log line", () => {
|
|
202
|
+
let state: EventHandlerState;
|
|
203
|
+
|
|
204
|
+
beforeEach(() => {
|
|
205
|
+
logRecords.length = 0;
|
|
206
|
+
state = createEventHandlerState();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("includes the upstream response body", async () => {
|
|
210
|
+
// GIVEN an unclassified 4xx provider rejection carrying the upstream body
|
|
211
|
+
const rawBody = JSON.stringify({
|
|
212
|
+
error: {
|
|
213
|
+
message:
|
|
214
|
+
"Invalid body: failed to parse JSON value from the request. Common errors include trailing commas.",
|
|
215
|
+
type: "invalid_request_error",
|
|
216
|
+
code: "invalid_body",
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
const error = new ProviderError(
|
|
220
|
+
"API error (400): Invalid body: failed to parse JSON value from the request.",
|
|
221
|
+
"openai",
|
|
222
|
+
400,
|
|
223
|
+
{ rawBody, apiErrorCode: "invalid_body" },
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
// WHEN the agent loop dispatches the error event
|
|
227
|
+
await dispatchAgentEvent(state, createDeps(), { type: "error", error });
|
|
228
|
+
|
|
229
|
+
// THEN the log record carries the body alongside the existing fields
|
|
230
|
+
const record = logRecords.find((r) => r.upstreamErrorBody !== undefined);
|
|
231
|
+
expect(record).toBeDefined();
|
|
232
|
+
expect(record?.upstreamErrorBody).toBe(rawBody);
|
|
233
|
+
expect(record?.conversationId).toBe("conv-provider-rejection");
|
|
234
|
+
expect(record?.statusCode).toBe(400);
|
|
235
|
+
expect(record?.provider).toBe("openai");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("scrubs secrets out of the error message it logs", async () => {
|
|
239
|
+
// GIVEN a rejection whose normalized message echoes a credential
|
|
240
|
+
const error = new ProviderError(
|
|
241
|
+
"API error (400): rejected request with Authorization: Bearer some-token-value",
|
|
242
|
+
"openai",
|
|
243
|
+
400,
|
|
244
|
+
{ rawBody: "{}" },
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
// WHEN the agent loop dispatches the error event
|
|
248
|
+
await dispatchAgentEvent(state, createDeps(), { type: "error", error });
|
|
249
|
+
|
|
250
|
+
// THEN the credential is redacted out of the logged message
|
|
251
|
+
const record = logRecords.find((r) => r.upstreamErrorBody !== undefined);
|
|
252
|
+
expect(record?.errorMessage).not.toContain("some-token-value");
|
|
253
|
+
expect(record?.errorMessage).toContain("Bearer [REDACTED]");
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test("bounds a huge upstream body to 4 KB", async () => {
|
|
257
|
+
// GIVEN an unclassified 4xx rejection whose body exceeds the ceiling
|
|
258
|
+
const error = new ProviderError("API error (400): huge", "openai", 400, {
|
|
259
|
+
rawBody: "y".repeat(MAX_LOGGED_UPSTREAM_BODY_CHARS * 3),
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// WHEN the agent loop dispatches the error event
|
|
263
|
+
await dispatchAgentEvent(state, createDeps(), { type: "error", error });
|
|
264
|
+
|
|
265
|
+
// THEN the logged body is capped
|
|
266
|
+
const record = logRecords.find((r) => r.upstreamErrorBody !== undefined);
|
|
267
|
+
expect(record?.upstreamErrorBody).toHaveLength(
|
|
268
|
+
MAX_LOGGED_UPSTREAM_BODY_CHARS,
|
|
269
|
+
);
|
|
270
|
+
expect(record?.upstreamErrorBodyTruncated).toBe(true);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
@@ -89,6 +89,7 @@ import {
|
|
|
89
89
|
} from "../usage/pricing.js";
|
|
90
90
|
import { ProviderError } from "../util/errors.js";
|
|
91
91
|
import { faviconUrlForDomain } from "../util/favicon.js";
|
|
92
|
+
import { redactLogString } from "../util/log-redact.js";
|
|
92
93
|
import { getLogger } from "../util/logger.js";
|
|
93
94
|
import { withSqliteRetry } from "../util/sqlite-retry.js";
|
|
94
95
|
import type { DirectiveRequest } from "./assistant-attachments.js";
|
|
@@ -143,6 +144,7 @@ import type {
|
|
|
143
144
|
WebSearchResultItem,
|
|
144
145
|
} from "./message-types/web-activity.js";
|
|
145
146
|
import { referenceMediaBlocksForPersist } from "./persist-media-references.js";
|
|
147
|
+
import { buildProviderRejectionLogFields } from "./provider-rejection-log-fields.js";
|
|
146
148
|
import { turnOrRestingTrust } from "./trust-context-types.js";
|
|
147
149
|
import type { TurnLatencyTracker } from "./turn-latency-tracker.js";
|
|
148
150
|
|
|
@@ -2708,7 +2710,8 @@ function handleError(
|
|
|
2708
2710
|
event.error instanceof ProviderError
|
|
2709
2711
|
? event.error.provider
|
|
2710
2712
|
: undefined,
|
|
2711
|
-
errorMessage: event.error.message,
|
|
2713
|
+
errorMessage: redactLogString(event.error.message),
|
|
2714
|
+
...buildProviderRejectionLogFields(event.error),
|
|
2712
2715
|
},
|
|
2713
2716
|
"Provider rejected request with unclassified 4xx error",
|
|
2714
2717
|
);
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Log-field builder for provider rejections that carry no actionable
|
|
3
|
+
* classification of their own (unclassified 4xx).
|
|
4
|
+
*
|
|
5
|
+
* The extracted fields on `ProviderError` (`apiErrorCode`, `message`) drop the
|
|
6
|
+
* detail an operator needs to find the offending request: which request field,
|
|
7
|
+
* which message, which byte pattern. The verbatim upstream body carries it, so
|
|
8
|
+
* it rides the log record, bounded so a large provider error page cannot flood
|
|
9
|
+
* the log stream.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { ProviderError } from "../util/errors.js";
|
|
13
|
+
import { redactLogString } from "../util/log-redact.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Ceiling for the logged upstream body. Bodies are already capped at 16 KiB
|
|
17
|
+
* when captured (`providers/openai/api-error-normalization.ts`); this second,
|
|
18
|
+
* tighter bound is what a log record carries.
|
|
19
|
+
*/
|
|
20
|
+
export const MAX_LOGGED_UPSTREAM_BODY_CHARS = 4096;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Upstream error codes whose message names the offending request content part,
|
|
24
|
+
* e.g. `Invalid 'input[191].content[1].text': string too long`. Kept narrow:
|
|
25
|
+
* codes outside this set have no such pointer, so parsing their message would
|
|
26
|
+
* only produce coincidental matches.
|
|
27
|
+
*/
|
|
28
|
+
const CONTENT_PART_POINTER_CODES = new Set([
|
|
29
|
+
"string_above_max_length",
|
|
30
|
+
"invalid_body",
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Positional pointer into the request payload. `input[N]` is the Responses API
|
|
35
|
+
* shape, `messages[N]` the Chat Completions shape; both index the request's
|
|
36
|
+
* message array in send order, so `N` is an offset into the conversation
|
|
37
|
+
* history that produced the request.
|
|
38
|
+
*/
|
|
39
|
+
const CONTENT_PART_POINTER = /\b(?:input|messages)\[(\d+)\]\.content\[(\d+)\]/i;
|
|
40
|
+
|
|
41
|
+
export interface ProviderRejectionLogFields {
|
|
42
|
+
/** Verbatim upstream non-2xx body, truncated to the logged ceiling. */
|
|
43
|
+
upstreamErrorBody: string | null;
|
|
44
|
+
/** True when the body was cut to fit the ceiling. */
|
|
45
|
+
upstreamErrorBodyTruncated?: boolean;
|
|
46
|
+
apiErrorCode?: string;
|
|
47
|
+
apiErrorType?: string;
|
|
48
|
+
apiErrorParam?: string;
|
|
49
|
+
requestId?: string;
|
|
50
|
+
/** Index of the offending message within the request's message array. */
|
|
51
|
+
offendingMessageIndex?: number;
|
|
52
|
+
/** Index of the offending content part within that message. */
|
|
53
|
+
offendingContentIndex?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Extract the offending `input[N].content[M]` coordinates from an upstream
|
|
58
|
+
* error message, for the codes that carry such a pointer.
|
|
59
|
+
*/
|
|
60
|
+
function parseContentPartPointer(
|
|
61
|
+
error: ProviderError,
|
|
62
|
+
): Pick<
|
|
63
|
+
ProviderRejectionLogFields,
|
|
64
|
+
"offendingMessageIndex" | "offendingContentIndex"
|
|
65
|
+
> {
|
|
66
|
+
const code = error.apiErrorCode?.toLowerCase();
|
|
67
|
+
if (!code || !CONTENT_PART_POINTER_CODES.has(code)) {
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
const match = CONTENT_PART_POINTER.exec(
|
|
71
|
+
`${error.message} ${error.rawBody ?? ""}`,
|
|
72
|
+
);
|
|
73
|
+
if (!match) {
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
const messageIndex = Number(match[1]);
|
|
77
|
+
const contentIndex = Number(match[2]);
|
|
78
|
+
if (
|
|
79
|
+
!Number.isSafeInteger(messageIndex) ||
|
|
80
|
+
!Number.isSafeInteger(contentIndex)
|
|
81
|
+
) {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
offendingMessageIndex: messageIndex,
|
|
86
|
+
offendingContentIndex: contentIndex,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Diagnostic fields describing what the provider actually rejected. */
|
|
91
|
+
export function buildProviderRejectionLogFields(
|
|
92
|
+
error: Error,
|
|
93
|
+
): ProviderRejectionLogFields {
|
|
94
|
+
if (!(error instanceof ProviderError)) {
|
|
95
|
+
return { upstreamErrorBody: null };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// The body is arbitrary upstream text under a custom field name, so it is
|
|
99
|
+
// outside the reach of the pino `err`/`req`/`res` serializers and is scrubbed
|
|
100
|
+
// here before it reaches the record.
|
|
101
|
+
const rawBody =
|
|
102
|
+
error.rawBody === undefined ? undefined : redactLogString(error.rawBody);
|
|
103
|
+
const truncated =
|
|
104
|
+
rawBody !== undefined && rawBody.length > MAX_LOGGED_UPSTREAM_BODY_CHARS;
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
upstreamErrorBody:
|
|
108
|
+
rawBody === undefined
|
|
109
|
+
? null
|
|
110
|
+
: rawBody.slice(0, MAX_LOGGED_UPSTREAM_BODY_CHARS),
|
|
111
|
+
...(truncated ? { upstreamErrorBodyTruncated: true } : {}),
|
|
112
|
+
...(error.apiErrorCode !== undefined
|
|
113
|
+
? { apiErrorCode: error.apiErrorCode }
|
|
114
|
+
: {}),
|
|
115
|
+
...(error.apiErrorType !== undefined
|
|
116
|
+
? { apiErrorType: error.apiErrorType }
|
|
117
|
+
: {}),
|
|
118
|
+
...(error.apiErrorParam !== undefined
|
|
119
|
+
? { apiErrorParam: error.apiErrorParam }
|
|
120
|
+
: {}),
|
|
121
|
+
...(error.requestId !== undefined ? { requestId: error.requestId } : {}),
|
|
122
|
+
...parseContentPartPointer(error),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
readdirSync,
|
|
23
23
|
readFileSync,
|
|
24
24
|
readSync,
|
|
25
|
+
renameSync,
|
|
25
26
|
statSync,
|
|
26
27
|
unlinkSync,
|
|
27
28
|
writeFileSync,
|
|
@@ -42,26 +43,151 @@ function getCacheDir(): string {
|
|
|
42
43
|
return join(tmpdir(), "vellum-optimized-images");
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Structural completeness check for a JPEG payload: SOI marker (FF D8) at the
|
|
48
|
+
* head and EOI marker (FF D9) at the tail. Every cache entry and every sips
|
|
49
|
+
* output is a JPEG, so a payload failing this is truncated or empty (the
|
|
50
|
+
* signature of a torn cache write) and must never reach a provider, which
|
|
51
|
+
* rejects it with a 400 that wedges the conversation (the corrupt block is
|
|
52
|
+
* resent on every subsequent turn).
|
|
53
|
+
*/
|
|
54
|
+
export function isCompleteJpeg(bytes: Uint8Array): boolean {
|
|
55
|
+
return (
|
|
56
|
+
bytes.length >= 4 &&
|
|
57
|
+
bytes[0] === 0xff &&
|
|
58
|
+
bytes[1] === 0xd8 &&
|
|
59
|
+
bytes[bytes.length - 2] === 0xff &&
|
|
60
|
+
bytes[bytes.length - 1] === 0xd9
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Structural JPEG truncation check for payloads of arbitrary origin: walk the
|
|
66
|
+
* marker segments from SOI and report whether a terminal EOI is reached.
|
|
67
|
+
* Unlike {@link isCompleteJpeg} (exact tail framing, for sips output and
|
|
68
|
+
* cache entries, which always end on EOI), this tolerates encoders that
|
|
69
|
+
* append padding or metadata after the EOI.
|
|
70
|
+
*
|
|
71
|
+
* A raw byte search for FF D9 is not enough: length-delimited APP/COM
|
|
72
|
+
* segments (an EXIF thumbnail is itself a complete embedded JPEG) may contain
|
|
73
|
+
* FF D9, so a truncated file with an intact metadata prefix would pass.
|
|
74
|
+
* Walking segment boundaries skips those payloads entirely; only an EOI at
|
|
75
|
+
* the top level of the marker stream counts. Entropy-coded scan data is
|
|
76
|
+
* traversed byte-wise, where FF is stuffed as FF 00 and restart markers
|
|
77
|
+
* (D0-D7) continue the scan, so a marker byte there is unambiguous.
|
|
78
|
+
*
|
|
79
|
+
* The EOI only counts after at least one frame header (SOF) and one scan
|
|
80
|
+
* (SOS) have been seen: a degenerate payload such as bare SOI+EOI carries no
|
|
81
|
+
* image data and providers reject it.
|
|
82
|
+
*/
|
|
83
|
+
export function hasValidJpegStructure(bytes: Uint8Array): boolean {
|
|
84
|
+
if (bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
let sawFrame = false;
|
|
88
|
+
let sawScan = false;
|
|
89
|
+
let i = 2;
|
|
90
|
+
while (i + 1 < bytes.length) {
|
|
91
|
+
if (bytes[i] !== 0xff) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
// FF fill bytes before a marker are legal padding.
|
|
95
|
+
let j = i + 1;
|
|
96
|
+
while (j < bytes.length && bytes[j] === 0xff) {
|
|
97
|
+
j++;
|
|
98
|
+
}
|
|
99
|
+
if (j >= bytes.length) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
const marker = bytes[j];
|
|
103
|
+
i = j + 1;
|
|
104
|
+
if (marker === 0xd9) {
|
|
105
|
+
return sawFrame && sawScan;
|
|
106
|
+
}
|
|
107
|
+
// SOF0-SOF15 occupy C0-CF, excluding DHT (C4), JPG (C8), and DAC (CC).
|
|
108
|
+
if (
|
|
109
|
+
marker >= 0xc0 &&
|
|
110
|
+
marker <= 0xcf &&
|
|
111
|
+
marker !== 0xc4 &&
|
|
112
|
+
marker !== 0xc8 &&
|
|
113
|
+
marker !== 0xcc
|
|
114
|
+
) {
|
|
115
|
+
sawFrame = true;
|
|
116
|
+
}
|
|
117
|
+
// Standalone markers carry no length field: repeated SOI, TEM, RST0-7.
|
|
118
|
+
if (
|
|
119
|
+
marker === 0xd8 ||
|
|
120
|
+
marker === 0x01 ||
|
|
121
|
+
(marker >= 0xd0 && marker <= 0xd7)
|
|
122
|
+
) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (i + 1 >= bytes.length) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const segmentLength = (bytes[i] << 8) | bytes[i + 1];
|
|
129
|
+
if (segmentLength < 2) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
i += segmentLength;
|
|
133
|
+
if (marker === 0xda) {
|
|
134
|
+
sawScan = true;
|
|
135
|
+
// SOS: entropy-coded data follows the header. Scan to the next real
|
|
136
|
+
// marker; FF 00 (stuffed data byte) and FF D0-D7 (restart) stay inside
|
|
137
|
+
// the scan.
|
|
138
|
+
while (i + 1 < bytes.length) {
|
|
139
|
+
if (
|
|
140
|
+
bytes[i] === 0xff &&
|
|
141
|
+
bytes[i + 1] !== 0x00 &&
|
|
142
|
+
!(bytes[i + 1] >= 0xd0 && bytes[i + 1] <= 0xd7)
|
|
143
|
+
) {
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
i++;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
45
153
|
function readFromCache(key: string): Buffer | null {
|
|
154
|
+
const cachePath = join(getCacheDir(), `${key}.jpg`);
|
|
46
155
|
try {
|
|
47
|
-
const cachePath = join(getCacheDir(), `${key}.jpg`);
|
|
48
156
|
if (!existsSync(cachePath)) {
|
|
49
157
|
return null;
|
|
50
158
|
}
|
|
51
|
-
|
|
159
|
+
const bytes = readFileSync(cachePath) as Buffer;
|
|
160
|
+
if (!isCompleteJpeg(bytes)) {
|
|
161
|
+
// Poisoned entry (torn write, disk full, etc.): drop it so the caller
|
|
162
|
+
// re-converts and re-caches.
|
|
163
|
+
unlinkSync(cachePath);
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
return bytes;
|
|
52
167
|
} catch {
|
|
53
168
|
return null;
|
|
54
169
|
}
|
|
55
170
|
}
|
|
56
171
|
|
|
57
172
|
function writeToCache(key: string, convertedBytes: Buffer): void {
|
|
173
|
+
const dir = getCacheDir();
|
|
174
|
+
// Write-then-rename so concurrent readers (the cache dir is shared across
|
|
175
|
+
// daemon processes) never observe a partially written entry; rename within
|
|
176
|
+
// the same directory is atomic. The pid+uuid suffix keeps concurrent
|
|
177
|
+
// writers of the same key off each other's temp file.
|
|
178
|
+
const tmpPath = join(dir, `${key}.${process.pid}-${uuid().slice(0, 8)}.tmp`);
|
|
58
179
|
try {
|
|
59
|
-
const dir = getCacheDir();
|
|
60
180
|
mkdirSync(dir, { recursive: true });
|
|
61
|
-
writeFileSync(
|
|
181
|
+
writeFileSync(tmpPath, convertedBytes);
|
|
182
|
+
renameSync(tmpPath, join(dir, `${key}.jpg`));
|
|
62
183
|
evictIfNeeded(dir);
|
|
63
184
|
} catch {
|
|
64
185
|
// Cache write failure is non-fatal.
|
|
186
|
+
try {
|
|
187
|
+
unlinkSync(tmpPath);
|
|
188
|
+
} catch {
|
|
189
|
+
/* ignore */
|
|
190
|
+
}
|
|
65
191
|
}
|
|
66
192
|
}
|
|
67
193
|
|
|
@@ -141,7 +267,14 @@ async function runSips(
|
|
|
141
267
|
if (proc.exitCode !== 0) {
|
|
142
268
|
return null;
|
|
143
269
|
}
|
|
144
|
-
|
|
270
|
+
const out = readFileSync(outPath) as Buffer;
|
|
271
|
+
// A zero-exit sips can still leave a truncated file (disk full, timeout
|
|
272
|
+
// kill racing the write). Returning it would poison the cache and every
|
|
273
|
+
// downstream provider call. Treat it as a failed conversion instead.
|
|
274
|
+
if (!isCompleteJpeg(out)) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
return out;
|
|
145
278
|
} catch {
|
|
146
279
|
return null;
|
|
147
280
|
} finally {
|