Skip to content

Commit de87445

Browse files
committed
fix(gateway): exempt managed PDFs from staging cap
1 parent 4657a5b commit de87445

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎src/gateway/server-methods/chat.directive-tags.test.ts‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,6 +4681,57 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
46814681
]);
46824682
});
46834683

4684+
it("passes sandbox-oversized managed inbound PDFs through instead of rejecting them (#90097)", async () => {
4685+
createTranscriptFixture("openclaw-chat-send-managed-pdf-oversize-");
4686+
mockState.finalText = "ok";
4687+
mockState.sessionEntry = {
4688+
modelProvider: "test-provider",
4689+
model: "vision-model",
4690+
};
4691+
mockState.modelCatalog = [
4692+
{
4693+
provider: "test-provider",
4694+
id: "vision-model",
4695+
name: "Vision model",
4696+
input: ["text", "image"],
4697+
},
4698+
];
4699+
const managedPath = "/home/user/.openclaw/media/inbound/huge.pdf";
4700+
mockState.savedMediaResults = [{ path: managedPath, contentType: "application/pdf" }];
4701+
mockState.sandboxWorkspace = { workspaceDir: "/sandbox/workspace" };
4702+
mockState.managedInboundPaths = [managedPath];
4703+
const respond = vi.fn();
4704+
const context = createChatContext();
4705+
const oversized = Buffer.alloc(6 * 1024 * 1024);
4706+
oversized.set(Buffer.from("%PDF-1.4\n"), 0);
4707+
4708+
await runNonStreamingChatSend({
4709+
context,
4710+
respond,
4711+
idempotencyKey: "idem-managed-pdf-oversize",
4712+
message: "read this",
4713+
requestParams: {
4714+
attachments: [
4715+
{
4716+
type: "file",
4717+
mimeType: "application/pdf",
4718+
fileName: "huge.pdf",
4719+
content: oversized.toString("base64"),
4720+
},
4721+
],
4722+
},
4723+
expectBroadcast: false,
4724+
});
4725+
4726+
expect(mockState.deleteMediaBufferCalls).toEqual([]);
4727+
expect(mockState.lastDispatchCtx?.MediaPath).toBe(managedPath);
4728+
expect(mockState.lastDispatchCtx?.MediaPaths).toEqual([managedPath]);
4729+
expect(mockState.lastDispatchCtx?.MediaType).toBe("application/pdf");
4730+
expect(mockState.lastDispatchCtx?.MediaTypes).toEqual(["application/pdf"]);
4731+
expect(mockState.lastDispatchCtx?.MediaWorkspaceDir).toBeUndefined();
4732+
expect(mockState.lastDispatchCtx?.MediaStaged).toBe(true);
4733+
});
4734+
46844735
it("rejects sandbox-oversized non-image attachments as 4xx before staging", async () => {
46854736
// Regression: resolveChatAttachmentMaxBytes defaults to 20MB, but
46864737
// stageSandboxMedia caps each file at STAGED_MEDIA_MAX_BYTES (5MB) and
@@ -4703,15 +4754,18 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
47034754
},
47044755
];
47054756
mockState.savedMediaResults = [
4706-
{ path: "/home/user/.openclaw/media/inbound/huge.pdf", contentType: "application/pdf" },
4757+
{
4758+
path: "/home/user/.openclaw/media/inbound/huge.bin",
4759+
contentType: "application/octet-stream",
4760+
},
47074761
];
47084762
mockState.sandboxWorkspace = { workspaceDir: "/sandbox/workspace" };
47094763
const respond = vi.fn();
47104764
const context = createChatContext();
47114765
// 6MB buffer — above STAGED_MEDIA_MAX_BYTES (5MB) but below the 20MB parse cap.
47124766
const oversized = Buffer.alloc(6 * 1024 * 1024);
4713-
oversized.set(Buffer.from("%PDF-1.4\n"), 0);
4714-
const pdf = oversized.toString("base64");
4767+
oversized.set(Buffer.from("OPENCLAW-BINARY\n"), 0);
4768+
const oversizedPayload = oversized.toString("base64");
47154769

47164770
await runNonStreamingChatSend({
47174771
context,
@@ -4720,7 +4774,12 @@ describe("chat directive tag stripping for non-streaming final payloads", () =>
47204774
message: "read this",
47214775
requestParams: {
47224776
attachments: [
4723-
{ type: "file", mimeType: "application/pdf", fileName: "huge.pdf", content: pdf },
4777+
{
4778+
type: "file",
4779+
mimeType: "application/octet-stream",
4780+
fileName: "huge.bin",
4781+
content: oversizedPayload,
4782+
},
47244783
],
47254784
},
47264785
expectBroadcast: false,

‎src/gateway/server-methods/chat.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,14 @@ async function prestageMediaPathOffloads(params: {
12771277
// (resolveChatAttachmentMaxBytes, default 20MB) is higher, so a sandboxed
12781278
// session receiving a file between the two caps would otherwise
12791279
// pass parse, fail staging, and surface as a retryable 5xx even though
1280-
// retry cannot succeed. Reject here as a client-side 4xx instead.
1280+
// retry cannot succeed. Managed inbound PDFs are already host-readable, so
1281+
// let the managed-PDF fallback handle them before rejecting everything else.
12811282
const oversizedForSandbox = mediaPathRefs.filter((ref) => ref.sizeBytes > MEDIA_MAX_BYTES);
12821283
if (oversizedForSandbox.length > 0) {
1284+
const managedPdfFallback = await resolveManagedInboundPdfFallback(mediaPathRefs);
1285+
if (managedPdfFallback) {
1286+
return managedPdfFallback;
1287+
}
12831288
const details = oversizedForSandbox
12841289
.map((ref) => `${ref.label} (${ref.sizeBytes} bytes)`)
12851290
.join(", ");

0 commit comments

Comments
 (0)