Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/services/remote-runner-session-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function quotedAttachments(
const path = typeof item.path === 'string' ? item.path : '';
const name = typeof item.name === 'string' ? basename(item.name).slice(0, 255) : '';
const type = item.type;
if (!path || !name || (type !== 'image' && type !== 'file')) {
if (!path || !name || (type !== 'image' && type !== 'file' && type !== 'video')) {
throw new Error('quoted returned an invalid attachment');
}
const info = lstatSync(path);
Expand All @@ -207,7 +207,8 @@ export function quotedAttachments(
transferred.push({
placeholder,
name,
type,
// Send video bytes as a file for existing runners; the quoted message keeps its video type.
type: type === 'video' ? 'file' : type,
...(typeof item.mimeType === 'string' && item.mimeType.length <= 256
? { mimeType: item.mimeType }
: {}),
Expand Down
43 changes: 42 additions & 1 deletion test/remote-runner-session-tool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import {
quotedAttachments,
QuotedAttachmentLimitError,
remoteRunnerSessionToolArgs,
runRemoteRunnerSessionTool,
} from '../src/services/remote-runner-session-tool.js';
import { materializeRelayedAttachments } from '../src/cli/relay-session-tool.js';
import { MAX_REMOTE_RUNNER_SESSION_TOOL_ATTACHMENT_BYTES } from '../src/adapters/backend/remote-runner-protocol.js';

const roots: string[] = [];
Expand Down Expand Up @@ -230,6 +233,44 @@ process.stdout.write(JSON.stringify({
});
});

it('transfers video as a file while preserving its quoted media type and charging its bytes', async () => {
const root = mkdtempSync(join(tmpdir(), 'botmux-session-tool-video-'));
roots.push(root);
const attachmentDir = join(root, 'attachments');
mkdirSync(attachmentDir);
const imagePath = join(attachmentDir, 'chart.png');
const videoPath = join(attachmentDir, 'demo.mp4');
const videoBytes = Buffer.from([0, 1, 255, 0]);
const limit = MAX_REMOTE_RUNNER_SESSION_TOOL_ATTACHMENT_BYTES;
writeFileSync(imagePath, Buffer.alloc(limit - videoBytes.length, 0xa5));
writeFileSync(videoPath, videoBytes);
const stdout = JSON.stringify({ attachments: [
{ path: imagePath, type: 'image', name: 'chart.png' },
{ path: videoPath, type: 'video', name: 'demo.mp4', mimeType: 'video/mp4' },
] });
const fixture = join(root, 'quoted.mjs');
writeFileSync(fixture, `process.stdout.write(${JSON.stringify(stdout)});`);
const env = { PATH: process.env.PATH, SESSION_DATA_DIR: root };
const result = await runRemoteRunnerSessionTool({ tool: 'quoted', messageId: 'om_video' }, {
sessionId: 'session-1', turnId: 'turn-1', chatId: 'oc_current', cliPath: fixture, env,
});
expect(result).toMatchObject({ outcome: 'completed', exitCode: 0 });
if (result.outcome !== 'completed') throw new Error('Expected a successful attachment transfer');
expect(result.attachments?.map(item => item.type)).toEqual(['image', 'file']);
expect(result.attachments?.[1]?.mimeType).toBe('video/mp4');
expect(result.stdout).not.toContain(attachmentDir);
const localDir = join(root, 'restored');
const restored = JSON.parse(materializeRelayedAttachments(result.stdout, result.attachments, localDir));
const [image, video] = restored.attachments;
expect(readFileSync(image.path).equals(readFileSync(imagePath))).toBe(true);
expect(video).toMatchObject({ type: 'video', name: 'demo.mp4', mimeType: 'video/mp4' });
expect(dirname(video.path)).toBe(localDir);
expect(readFileSync(video.path)).toEqual(videoBytes);

truncateSync(videoPath, videoBytes.length + 1);
expect(() => quotedAttachments(stdout, env)).toThrow(new QuotedAttachmentLimitError(limit + 1, limit));
});

it('rejects quoted attachment paths outside the BotMux attachment root', async () => {
const root = mkdtempSync(join(tmpdir(), 'botmux-session-tool-escape-test-'));
roots.push(root);
Expand Down
16 changes: 15 additions & 1 deletion test/riff-host-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ describe('Riff host boundary', () => {
expect(packaged.stdout).not.toContain(dataDir);
});

it('rejects an unknown attachment kind even when the file exists', () => {
const { ctx, paths } = quotedFixture([1]);
const stdout = JSON.stringify({ attachments: [{ path: paths[0], name: 'audio.mp3', type: 'audio' }] });
expect(() => quotedAttachments(stdout, { SESSION_DATA_DIR: ctx.dataDir })).toThrow('invalid attachment');
});

it('rejects batch attachments outside the attachment root and caps the count across messages', () => {
const dataDir = root();
const attachmentDir = join(dataDir, 'attachments');
Expand Down Expand Up @@ -566,10 +572,15 @@ describe('Riff host boundary', () => {
mkdirSync(attachmentDir, { recursive: true });
const hostPath = join(attachmentDir, 'chart.png');
writeFileSync(hostPath, 'chart bytes');
const videoPath = join(attachmentDir, 'clip.mp4');
writeFileSync(videoPath, Buffer.from([255, 0, 42]));
// Same envelope cmdQuoted prints for several ids: per-id errors exit 1.
const stdout = JSON.stringify({
sessionId: 'session-1',
messages: [{ messageId: 'om_one', attachments: [{ path: hostPath, name: 'chart.png', type: 'image' }] }],
messages: [{ messageId: 'om_one', attachments: [
{ path: hostPath, name: 'chart.png', type: 'image' },
{ path: videoPath, name: 'clip.mp4', type: 'video' },
] }],
errors: exitCode ? [{ messageId: 'om_gone', error: 'not found' }] : [],
requestedCount: 2, total: 1, incomplete: exitCode !== 0,
});
Expand All @@ -589,6 +600,9 @@ describe('Riff host boundary', () => {
expect(path).not.toContain('botmux-session-tool://');
roots.push(join(path, '..'));
expect(readFileSync(path, 'utf8')).toBe('chart bytes');
const clip = restored.messages[0].attachments[1];
expect(clip).toMatchObject({ name: 'clip.mp4', type: 'video' });
expect(readFileSync(clip.path)).toEqual(Buffer.from([255, 0, 42]));
expect(restored.errors).toEqual(exitCode ? [{ messageId: 'om_gone', error: 'not found' }] : []);
});

Expand Down