Files
EJClaw/runners/agent-runner/test/output-protocol.test.ts
Codex 1167023a13 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>
2026-10-12 13:26:58 +09:00

300 lines
9.4 KiB
TypeScript

import fs from 'fs';
import os from 'os';
import path from 'path';
import { afterEach, describe, expect, it } from 'vitest';
import {
buildMultimodalContent,
countPdfPages,
} from '../src/output-protocol.js';
const ONE_PIXEL_PNG = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=',
'base64',
);
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(() => {
for (const dir of cleanupDirs.splice(0)) {
fs.rmSync(dir, { force: true, recursive: true });
}
});
describe('agent-runner multimodal prompts', () => {
it('loads labeled image tags as Claude image blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-image-tag-'));
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'settings-v0.1.92-deployed-390.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const logs: string[] = [];
const content = buildMultimodalContent(
`리뷰 증거\n[Image: settings-v0.1.92-deployed-390.png → ${imagePath}]`,
(message) => logs.push(message),
);
expect(Array.isArray(content)).toBe(true);
expect(content).toEqual([
{ type: 'text', text: '리뷰 증거' },
{
type: 'text',
text: 'Image evidence: settings-v0.1.92-deployed-390.png',
},
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/png',
data: ONE_PIXEL_PNG.toString('base64'),
},
},
]);
expect(logs).toContain(`Added image block: ${imagePath} (image/png)`);
});
it('keeps missing image evidence visible in Claude prompts', () => {
const missingPath = path.join(
os.tmpdir(),
'ejclaw-missing-image-evidence.png',
);
const logs: string[] = [];
const content = buildMultimodalContent(
`리뷰 증거\n[Image: expected-render.png → ${missingPath}]`,
(message) => logs.push(message),
);
expect(content).toEqual([
{ type: 'text', text: '리뷰 증거' },
{
type: 'text',
text: `[Image unavailable: expected-render.png → ${missingPath} — file not found]`,
},
]);
expect(logs).toContain(`Image not found, skipping: ${missingPath}`);
});
it('keeps unsupported image evidence visible instead of mislabeled', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-bmp-image-'));
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'settings.bmp');
fs.writeFileSync(imagePath, Buffer.from('BMunsupported'));
const logs: string[] = [];
const content = buildMultimodalContent(
`리뷰 증거\n[Image: settings.bmp → ${imagePath}]`,
(message) => logs.push(message),
);
expect(content).toEqual([
{ type: 'text', text: '리뷰 증거' },
{
type: 'text',
text: `[Image unavailable: settings.bmp → ${imagePath} — unsupported image type .bmp]`,
},
]);
expect(logs).toContain(`Unsupported image type, skipping: ${imagePath}`);
});
it('loads MEDIA image directives as Claude image blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-media-image-'));
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'media-render.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const content = buildMultimodalContent(`증거\nMEDIA:${imagePath}`, () => {
// no-op
});
expect(content).toEqual([
{ type: 'text', text: '증거' },
{ type: 'text', text: 'Image evidence: media-render.png' },
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/png',
data: ONE_PIXEL_PNG.toString('base64'),
},
},
]);
});
it('loads markdown image links as Claude image blocks', () => {
const dir = fs.mkdtempSync(
path.join(os.tmpdir(), 'ejclaw-markdown-image-'),
);
cleanupDirs.push(dir);
const imagePath = path.join(dir, 'markdown-render.png');
fs.writeFileSync(imagePath, ONE_PIXEL_PNG);
const content = buildMultimodalContent(
`증거 ![render](${imagePath})`,
() => {
// no-op
},
);
expect(content).toEqual([
{ type: 'text', text: '증거' },
{ type: 'text', text: 'Image evidence: markdown-render.png' },
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/png',
data: ONE_PIXEL_PNG.toString('base64'),
},
},
]);
});
it('loads PDF file tags as Claude document blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-pdf-doc-'));
cleanupDirs.push(dir);
const pdfPath = path.join(dir, 'report.pdf');
fs.writeFileSync(pdfPath, MINIMAL_PDF);
const logs: string[] = [];
const content = buildMultimodalContent(
`검토 자료\n[File: report.pdf → ${pdfPath}]`,
(message) => logs.push(message),
);
expect(content).toEqual([
{ type: 'text', text: '검토 자료' },
{ type: 'text', text: 'Document evidence: report.pdf' },
{
type: 'document',
title: 'report.pdf',
source: {
type: 'base64',
media_type: 'application/pdf',
data: MINIMAL_PDF.toString('base64'),
},
},
]);
expect(logs).toContain(
`Added document block: ${pdfPath} (application/pdf)`,
);
});
it('loads text file tags as Claude text document blocks', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ejclaw-text-doc-'));
cleanupDirs.push(dir);
const textPath = path.join(dir, 'notes.md');
fs.writeFileSync(textPath, '# Notes\nEvidence text');
const content = buildMultimodalContent(
`검토 자료\nMEDIA:${textPath}`,
() => {
// no-op
},
);
expect(content).toEqual([
{ type: 'text', text: '검토 자료' },
{ type: 'text', text: 'Document evidence: notes.md' },
{
type: 'document',
title: 'notes.md',
source: {
type: 'text',
media_type: 'text/plain',
data: '# Notes\nEvidence text',
},
},
]);
});
});
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');
});
});