fix: cap inline prompt documents, raise Discord upload cap to 10 MiB
joke-app finalize failed with "Prompt is too long (~1.18M tokens)": the runner inlined a 162-page, 8.3 MB PDF as a base64 document block for every [Document: ...] reference, and the prompt referenced it twice. The Claude runner now inlines a document at most once per prompt, only PDFs up to 20 pages / 10 MiB (512 KiB when the page count is unknown) with at most 40 PDF pages per prompt, and text documents up to 256 KiB. Larger ones become a path note telling the agent to use the Read tool. The same PDF was also rejected as an outbound attachment by our local 8 MiB cap, although Discord's default per-file limit is now 10 MiB. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -110,9 +110,68 @@ const TEXT_DOCUMENT_EXTENSIONS = new Set([
|
||||
'.conf',
|
||||
]);
|
||||
|
||||
// Inline-document budget for one prompt. A PDF document block costs roughly
|
||||
// 1.5-3k tokens per page (extracted text plus a page image), so inlining a
|
||||
// 162-page, 8.3 MB PDF twice pushed a finalize prompt to ~1.18M tokens
|
||||
// (limit 1M) and Claude rejected the turn with "Prompt is too long".
|
||||
// Documents over budget stay as a path the agent can open with the Read tool.
|
||||
const MAX_INLINE_PDF_PAGES = 20;
|
||||
const MAX_INLINE_PDF_PAGES_PER_PROMPT = 40;
|
||||
const MAX_INLINE_PDF_BYTES = 10 * 1024 * 1024;
|
||||
// Page objects inside compressed object streams cannot be counted without a
|
||||
// PDF parser, so an uncountable PDF is only inlined when it is small.
|
||||
const MAX_INLINE_UNCOUNTED_PDF_BYTES = 512 * 1024;
|
||||
const MAX_INLINE_TEXT_DOCUMENT_BYTES = 256 * 1024;
|
||||
|
||||
interface InlineDocumentBudget {
|
||||
inlinedPaths: Set<string>;
|
||||
pdfPages: number;
|
||||
}
|
||||
|
||||
/** Counts `/Type /Page` objects; 0 means the count is unknown. */
|
||||
export function countPdfPages(data: Buffer): number {
|
||||
return (
|
||||
data.toString('latin1').match(/\/Type\s*\/Page(?![A-Za-z])/g)?.length ?? 0
|
||||
);
|
||||
}
|
||||
|
||||
function formatMegabytes(bytes: number): string {
|
||||
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
function notInlinedCaption(
|
||||
part: Extract<PromptAttachmentPart, { type: 'attachment' }>,
|
||||
reason: string,
|
||||
): string {
|
||||
const label = part.label ? `${part.label} → ` : '';
|
||||
return `[Document not inlined: ${label}${part.path} — ${reason}]`;
|
||||
}
|
||||
|
||||
function pdfOverBudgetReason(
|
||||
size: number,
|
||||
pages: number,
|
||||
budget: InlineDocumentBudget,
|
||||
): string | null {
|
||||
const readHint =
|
||||
'open it with the Read tool and its pages parameter (e.g. pages: "1-20") if needed';
|
||||
if (pages === 0) {
|
||||
return size > MAX_INLINE_UNCOUNTED_PDF_BYTES
|
||||
? `${formatMegabytes(size)} with an unknown page count is over the inline limit; ${readHint}`
|
||||
: null;
|
||||
}
|
||||
if (pages > MAX_INLINE_PDF_PAGES || size > MAX_INLINE_PDF_BYTES) {
|
||||
return `${pages} pages, ${formatMegabytes(size)} is over the inline limit (${MAX_INLINE_PDF_PAGES} pages, ${formatMegabytes(MAX_INLINE_PDF_BYTES)}); ${readHint}`;
|
||||
}
|
||||
if (budget.pdfPages + pages > MAX_INLINE_PDF_PAGES_PER_PROMPT) {
|
||||
return `this prompt already inlines ${budget.pdfPages} PDF pages (limit ${MAX_INLINE_PDF_PAGES_PER_PROMPT}); ${readHint}`;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function loadDocumentBlock(
|
||||
part: Extract<PromptAttachmentPart, { type: 'attachment' }>,
|
||||
log: LogFn,
|
||||
budget: InlineDocumentBudget,
|
||||
): ContentBlock | string {
|
||||
try {
|
||||
if (!fs.existsSync(part.path)) {
|
||||
@@ -120,19 +179,45 @@ function loadDocumentBlock(
|
||||
return missingAttachmentCaption(part, 'file not found');
|
||||
}
|
||||
|
||||
if (budget.inlinedPaths.has(part.path)) {
|
||||
log(`Document already inlined, skipping duplicate: ${part.path}`);
|
||||
return notInlinedCaption(part, 'same file is already attached above');
|
||||
}
|
||||
|
||||
const ext = path.extname(part.path).toLowerCase();
|
||||
if (ext === '.pdf') {
|
||||
const data = fs.readFileSync(part.path).toString('base64');
|
||||
const raw = fs.readFileSync(part.path);
|
||||
const pages = countPdfPages(raw);
|
||||
const overBudget = pdfOverBudgetReason(raw.length, pages, budget);
|
||||
if (overBudget) {
|
||||
log(`Document too large to inline, skipping: ${part.path}`);
|
||||
return notInlinedCaption(part, overBudget);
|
||||
}
|
||||
budget.inlinedPaths.add(part.path);
|
||||
budget.pdfPages += pages || MAX_INLINE_PDF_PAGES;
|
||||
log(`Added document block: ${part.path} (application/pdf)`);
|
||||
return {
|
||||
type: 'document',
|
||||
title: part.label ?? path.basename(part.path),
|
||||
source: { type: 'base64', media_type: 'application/pdf', data },
|
||||
source: {
|
||||
type: 'base64',
|
||||
media_type: 'application/pdf',
|
||||
data: raw.toString('base64'),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (TEXT_DOCUMENT_EXTENSIONS.has(ext)) {
|
||||
const size = fs.statSync(part.path).size;
|
||||
if (size > MAX_INLINE_TEXT_DOCUMENT_BYTES) {
|
||||
log(`Document too large to inline, skipping: ${part.path}`);
|
||||
return notInlinedCaption(
|
||||
part,
|
||||
`${formatMegabytes(size)} is over the inline limit (${MAX_INLINE_TEXT_DOCUMENT_BYTES / 1024} KB); open it with the Read tool if needed`,
|
||||
);
|
||||
}
|
||||
const data = fs.readFileSync(part.path, 'utf8');
|
||||
budget.inlinedPaths.add(part.path);
|
||||
log(`Added document block: ${part.path} (text/plain)`);
|
||||
return {
|
||||
type: 'document',
|
||||
@@ -162,6 +247,10 @@ export function buildMultimodalContent(
|
||||
if (!parts.some((part) => part.type === 'attachment')) return text;
|
||||
|
||||
const blocks: ContentBlock[] = [];
|
||||
const documentBudget: InlineDocumentBudget = {
|
||||
inlinedPaths: new Set(),
|
||||
pdfPages: 0,
|
||||
};
|
||||
const pushText = (value: string) => {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed) blocks.push({ type: 'text', text: trimmed });
|
||||
@@ -175,7 +264,7 @@ export function buildMultimodalContent(
|
||||
|
||||
if (part.kind === 'document') {
|
||||
pushText(attachmentEvidenceCaption(part));
|
||||
const block = loadDocumentBlock(part, log);
|
||||
const block = loadDocumentBlock(part, log, documentBudget);
|
||||
if (typeof block === 'string') {
|
||||
pushText(block);
|
||||
} else {
|
||||
|
||||
@@ -4,7 +4,10 @@ import path from 'path';
|
||||
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
|
||||
import { buildMultimodalContent } from '../src/output-protocol.js';
|
||||
import {
|
||||
buildMultimodalContent,
|
||||
countPdfPages,
|
||||
} from '../src/output-protocol.js';
|
||||
|
||||
const ONE_PIXEL_PNG = Buffer.from(
|
||||
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=',
|
||||
@@ -12,6 +15,16 @@ const ONE_PIXEL_PNG = Buffer.from(
|
||||
);
|
||||
const MINIMAL_PDF = Buffer.from('%PDF-1.4\n1 0 obj\n<<>>\nendobj\n%%EOF\n');
|
||||
|
||||
function pdfWithPages(pageCount: number): Buffer {
|
||||
const pages = Array.from(
|
||||
{ length: pageCount },
|
||||
(_, i) => `${i + 3} 0 obj\n<< /Type /Page /Parent 2 0 R >>\nendobj\n`,
|
||||
).join('');
|
||||
return Buffer.from(
|
||||
`%PDF-1.4\n2 0 obj\n<< /Type /Pages /Count ${pageCount} >>\nendobj\n${pages}%%EOF\n`,
|
||||
);
|
||||
}
|
||||
|
||||
const cleanupDirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
@@ -207,3 +220,80 @@ describe('agent-runner multimodal prompts', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('agent-runner inline document budget', () => {
|
||||
it('counts PDF page objects but not the /Pages tree node', () => {
|
||||
expect(countPdfPages(pdfWithPages(3))).toBe(3);
|
||||
expect(countPdfPages(MINIMAL_PDF)).toBe(0);
|
||||
});
|
||||
|
||||
it('does not inline a PDF with too many pages', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-big-pdf-'));
|
||||
cleanupDirs.push(dir);
|
||||
const pdfPath = path.join(dir, 'book.pdf');
|
||||
fs.writeFileSync(pdfPath, pdfWithPages(162));
|
||||
|
||||
const content = buildMultimodalContent(
|
||||
`[File: book.pdf → ${pdfPath}]`,
|
||||
() => {},
|
||||
);
|
||||
|
||||
expect(Array.isArray(content)).toBe(true);
|
||||
const blocks = content as Array<{ type: string; text?: string }>;
|
||||
expect(blocks.some((block) => block.type === 'document')).toBe(false);
|
||||
expect(blocks.at(-1)?.text).toContain(
|
||||
`[Document not inlined: book.pdf → ${pdfPath} — 162 pages`,
|
||||
);
|
||||
expect(blocks.at(-1)?.text).toContain('Read tool');
|
||||
});
|
||||
|
||||
it('inlines the same PDF only once per prompt', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-dup-pdf-'));
|
||||
cleanupDirs.push(dir);
|
||||
const pdfPath = path.join(dir, 'report.pdf');
|
||||
fs.writeFileSync(pdfPath, pdfWithPages(2));
|
||||
|
||||
const content = buildMultimodalContent(
|
||||
`첫 메시지\n[File: report.pdf → ${pdfPath}]\n둘째 메시지\n[File: report.pdf → ${pdfPath}]`,
|
||||
() => {},
|
||||
);
|
||||
|
||||
const blocks = content as Array<{ type: string; text?: string }>;
|
||||
expect(blocks.filter((block) => block.type === 'document')).toHaveLength(1);
|
||||
expect(blocks.at(-1)?.text).toBe(
|
||||
`[Document not inlined: report.pdf → ${pdfPath} — same file is already attached above]`,
|
||||
);
|
||||
});
|
||||
|
||||
it('caps the total PDF pages inlined into one prompt', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-pdf-budget-'));
|
||||
cleanupDirs.push(dir);
|
||||
const paths = ['a', 'b', 'c'].map((name) => {
|
||||
const pdfPath = path.join(dir, `${name}.pdf`);
|
||||
fs.writeFileSync(pdfPath, pdfWithPages(15));
|
||||
return pdfPath;
|
||||
});
|
||||
|
||||
const content = buildMultimodalContent(
|
||||
paths.map((pdfPath) => `[File: x → ${pdfPath}]`).join('\n'),
|
||||
() => {},
|
||||
);
|
||||
|
||||
const blocks = content as Array<{ type: string; text?: string }>;
|
||||
expect(blocks.filter((block) => block.type === 'document')).toHaveLength(2);
|
||||
expect(blocks.at(-1)?.text).toContain('already inlines 30 PDF pages');
|
||||
});
|
||||
|
||||
it('does not inline an oversized text document', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-big-text-'));
|
||||
cleanupDirs.push(dir);
|
||||
const textPath = path.join(dir, 'huge.log');
|
||||
fs.writeFileSync(textPath, 'x'.repeat(300 * 1024));
|
||||
|
||||
const content = buildMultimodalContent(`MEDIA:${textPath}`, () => {});
|
||||
|
||||
const blocks = content as Array<{ type: string; text?: string }>;
|
||||
expect(blocks.some((block) => block.type === 'document')).toBe(false);
|
||||
expect(blocks.at(-1)?.text).toContain('[Document not inlined: huge.log');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -359,7 +359,13 @@ describe('validateOutboundAttachments policy checks', () => {
|
||||
const largePng = writeFile(
|
||||
dir,
|
||||
'large.png',
|
||||
Buffer.concat([ONE_PIXEL_PNG, Buffer.alloc(8 * 1024 * 1024)]),
|
||||
Buffer.concat([ONE_PIXEL_PNG, Buffer.alloc(10 * 1024 * 1024)]),
|
||||
);
|
||||
// Over the old 8 MiB cap but under Discord's 10 MiB limit.
|
||||
const mediumPng = writeFile(
|
||||
dir,
|
||||
'medium.png',
|
||||
Buffer.concat([ONE_PIXEL_PNG, Buffer.alloc(8.5 * 1024 * 1024)]),
|
||||
);
|
||||
|
||||
expect(validateOutboundAttachments([{ path: nestedDir }])).toEqual({
|
||||
@@ -370,6 +376,9 @@ describe('validateOutboundAttachments policy checks', () => {
|
||||
files: [],
|
||||
rejected: [{ path: largePng, reason: 'too-large' }],
|
||||
});
|
||||
const medium = validateOutboundAttachments([{ path: mediumPng }]);
|
||||
expect(medium.rejected).toEqual([]);
|
||||
expect(medium.files).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -16,7 +16,10 @@ export interface ValidateOutboundAttachmentsResult {
|
||||
rejected: Array<{ path: string; reason: string }>;
|
||||
}
|
||||
|
||||
const MAX_ATTACHMENT_BYTES = 8 * 1024 * 1024;
|
||||
// Discord's per-file upload limit for servers without boosts has been 10 MiB
|
||||
// since 2024 (it was 8 MiB before). An 8.3 MiB PDF was rejected locally under
|
||||
// the old 8 MiB cap even though Discord would have accepted it.
|
||||
export const MAX_ATTACHMENT_BYTES = 10 * 1024 * 1024;
|
||||
const IMAGE_EXTS = /\.(png|jpe?g|gif|webp|bmp)$/i;
|
||||
const SUPPORTED_ATTACHMENT_MIMES: Record<string, string> = {
|
||||
'.png': 'image/png',
|
||||
@@ -272,7 +275,7 @@ const REJECTION_REASON_LABELS: Record<string, string> = {
|
||||
'not-found': '파일을 찾을 수 없음',
|
||||
'not-absolute': '절대경로가 아님',
|
||||
'not-file': '파일이 아님',
|
||||
'too-large': '8MB 용량 초과',
|
||||
'too-large': `${MAX_ATTACHMENT_BYTES / (1024 * 1024)}MB 용량 초과`,
|
||||
'unsupported-extension': '지원하지 않는 형식',
|
||||
'invalid-image-signature': '이미지 형식 손상 또는 불일치',
|
||||
'invalid-file-signature': '파일 형식 손상 또는 불일치',
|
||||
|
||||
Reference in New Issue
Block a user