From c4dfc0484129c459b6833730f12d761bb568b5bd Mon Sep 17 00:00:00 2001 From: Han-wangyu <109013441+Han-wangyu@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:48:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(quoted):=20=E8=AE=A9=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E9=99=84=E4=BB=B6=E7=BB=8F=E4=BC=9A=E8=AF=9D=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E4=BC=A0=E8=BE=93=E5=B9=B6=E4=BF=9D=E7=95=99=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/remote-runner-session-tool.ts | 5 ++-- test/remote-runner-session-tool.test.ts | 32 ++++++++++++++++++---- test/riff-host-tools.test.ts | 16 ++++++++++- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/services/remote-runner-session-tool.ts b/src/services/remote-runner-session-tool.ts index 287841f95e..3f7d8fef56 100644 --- a/src/services/remote-runner-session-tool.ts +++ b/src/services/remote-runner-session-tool.ts @@ -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); @@ -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 } : {}), diff --git a/test/remote-runner-session-tool.test.ts b/test/remote-runner-session-tool.test.ts index 4774c50a78..b274425996 100644 --- a/test/remote-runner-session-tool.test.ts +++ b/test/remote-runner-session-tool.test.ts @@ -8,12 +8,13 @@ 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 { 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[] = []; @@ -23,18 +24,24 @@ afterEach(() => { }); describe('remote runner session tool host bridge', () => { - it.each([0, 1])('retains the Remote Runner attachment byte boundary with offset %s', async offset => { + it.each([0, 1])('relays a quoted video alongside an image within the aggregate byte boundary (offset %s)', async offset => { const root = mkdtempSync(join(tmpdir(), 'botmux-session-tool-limit-')); roots.push(root); const attachmentDir = join(root, 'attachments'); mkdirSync(attachmentDir); const path = join(attachmentDir, 'image.png'); const size = MAX_REMOTE_RUNNER_SESSION_TOOL_ATTACHMENT_BYTES + offset; + const video = join(attachmentDir, 'demo.mp4'); + const videoBytes = Buffer.from([0, 1, 255, 0]); + writeFileSync(video, videoBytes); writeFileSync(path, ''); - truncateSync(path, size); + truncateSync(path, size - videoBytes.length); const fixture = join(root, 'fixture.mjs'); writeFileSync(fixture, `process.stdout.write(${JSON.stringify(JSON.stringify({ - attachments: [{ path, name: 'image.png', type: 'image' }], + attachments: [ + { path, name: 'image.png', type: 'image' }, + { path: video, name: 'demo.mp4', type: 'video', mimeType: 'video/mp4' }, + ], }))});`); const result = await runRemoteRunnerSessionTool({ tool: 'quoted', messageId: 'om_boundary' }, { sessionId: 'session-1', turnId: 'turn-1', chatId: 'oc_current', cliPath: fixture, @@ -44,9 +51,22 @@ describe('remote runner session tool host bridge', () => { expect(result.outcome).toBe('completed'); if (result.outcome !== 'completed') throw new Error('Expected a successful attachment transfer'); expect(result.exitCode).toBe(0); - expect(Buffer.from(result.attachments![0]!.dataBase64, 'base64').length).toBe(size); + 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, clip] = restored.attachments; + expect(readFileSync(image.path).length).toBe(size - videoBytes.length); + expect(clip).toMatchObject({ type: 'video', name: 'demo.mp4', mimeType: 'video/mp4' }); + expect(dirname(clip.path)).toBe(localDir); + expect(readFileSync(clip.path)).toEqual(videoBytes); } else { - expect(result).toMatchObject({ outcome: 'rejected', code: 'session_tool_attachment_rejected' }); + expect(result).toMatchObject({ + outcome: 'rejected', + code: 'session_tool_attachment_rejected', + message: expect.stringContaining(`${size} bytes > ${MAX_REMOTE_RUNNER_SESSION_TOOL_ATTACHMENT_BYTES} bytes`), + }); } }); diff --git a/test/riff-host-tools.test.ts b/test/riff-host-tools.test.ts index 9bdb00961a..fd7170413a 100644 --- a/test/riff-host-tools.test.ts +++ b/test/riff-host-tools.test.ts @@ -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'); @@ -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, }); @@ -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' }] : []); }); From 4585175da12ced120a341c512b38121ce33b8e9a Mon Sep 17 00:00:00 2001 From: Han-wangyu <109013441+Han-wangyu@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:54:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test(quoted):=20=E7=8B=AC=E7=AB=8B=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E8=A7=86=E9=A2=91=E4=BC=A0=E8=BE=93=E5=8F=8A=E7=B4=AF?= =?UTF-8?q?=E8=AE=A1=E5=AD=97=E8=8A=82=E4=B8=8A=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/remote-runner-session-tool.test.ts | 69 ++++++++++++++++--------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/test/remote-runner-session-tool.test.ts b/test/remote-runner-session-tool.test.ts index b274425996..ac81d475f1 100644 --- a/test/remote-runner-session-tool.test.ts +++ b/test/remote-runner-session-tool.test.ts @@ -11,6 +11,8 @@ import { tmpdir } from 'node:os'; 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'; @@ -24,24 +26,18 @@ afterEach(() => { }); describe('remote runner session tool host bridge', () => { - it.each([0, 1])('relays a quoted video alongside an image within the aggregate byte boundary (offset %s)', async offset => { + it.each([0, 1])('retains the Remote Runner attachment byte boundary with offset %s', async offset => { const root = mkdtempSync(join(tmpdir(), 'botmux-session-tool-limit-')); roots.push(root); const attachmentDir = join(root, 'attachments'); mkdirSync(attachmentDir); const path = join(attachmentDir, 'image.png'); const size = MAX_REMOTE_RUNNER_SESSION_TOOL_ATTACHMENT_BYTES + offset; - const video = join(attachmentDir, 'demo.mp4'); - const videoBytes = Buffer.from([0, 1, 255, 0]); - writeFileSync(video, videoBytes); writeFileSync(path, ''); - truncateSync(path, size - videoBytes.length); + truncateSync(path, size); const fixture = join(root, 'fixture.mjs'); writeFileSync(fixture, `process.stdout.write(${JSON.stringify(JSON.stringify({ - attachments: [ - { path, name: 'image.png', type: 'image' }, - { path: video, name: 'demo.mp4', type: 'video', mimeType: 'video/mp4' }, - ], + attachments: [{ path, name: 'image.png', type: 'image' }], }))});`); const result = await runRemoteRunnerSessionTool({ tool: 'quoted', messageId: 'om_boundary' }, { sessionId: 'session-1', turnId: 'turn-1', chatId: 'oc_current', cliPath: fixture, @@ -51,22 +47,9 @@ describe('remote runner session tool host bridge', () => { expect(result.outcome).toBe('completed'); if (result.outcome !== 'completed') throw new Error('Expected a successful attachment transfer'); expect(result.exitCode).toBe(0); - 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, clip] = restored.attachments; - expect(readFileSync(image.path).length).toBe(size - videoBytes.length); - expect(clip).toMatchObject({ type: 'video', name: 'demo.mp4', mimeType: 'video/mp4' }); - expect(dirname(clip.path)).toBe(localDir); - expect(readFileSync(clip.path)).toEqual(videoBytes); + expect(Buffer.from(result.attachments![0]!.dataBase64, 'base64').length).toBe(size); } else { - expect(result).toMatchObject({ - outcome: 'rejected', - code: 'session_tool_attachment_rejected', - message: expect.stringContaining(`${size} bytes > ${MAX_REMOTE_RUNNER_SESSION_TOOL_ATTACHMENT_BYTES} bytes`), - }); + expect(result).toMatchObject({ outcome: 'rejected', code: 'session_tool_attachment_rejected' }); } }); @@ -250,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);