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:
Codex
2026-10-12 13:26:58 +09:00
parent f579b4f406
commit 1167023a13
4 changed files with 198 additions and 7 deletions

View File

@@ -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 {