From bee183e6a9550fc57136b8ab8fa0491130145824 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 10 Sep 2026 15:16:20 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=BC=20feat:=20Expose=20BYOM=20Cancella?= =?UTF-8?q?tion=20Diagnostics=20(#15810)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Trace BYOM command cancellation at the HTTP boundary * 🧹 chore: Sort Command Module Imports The Static Checks job fails on this branch at the "Import sorting" step: `node scripts/sort-imports.mts --check` reports `packages/api/src/code/command.ts` as out of order. The `logger` import added for the BYOM command diagnostics was placed after `import { tool } from '@librechat/agents/langchain/tools'`, but package imports sort shortest to longest line, and the data-schemas import is the shorter of the two. Reordered by running the repo's own sorter, which is also what the pre-commit lint-staged hook applies. --- packages/api/src/agents/handlers.spec.ts | 56 +++++++++++++++++++++++ packages/api/src/code/command.spec.ts | 44 ++++++++++++++++++ packages/api/src/code/command.ts | 57 ++++++++++++++++-------- 3 files changed, 139 insertions(+), 18 deletions(-) diff --git a/packages/api/src/agents/handlers.spec.ts b/packages/api/src/agents/handlers.spec.ts index f7b8f317a9..4b75a67a23 100644 --- a/packages/api/src/agents/handlers.spec.ts +++ b/packages/api/src/agents/handlers.spec.ts @@ -3,6 +3,9 @@ jest.mock('./prewarm', () => ({ })); import { Readable } from 'stream'; +import { once } from 'node:events'; +import { createServer } from 'node:http'; +import type { AddressInfo } from 'node:net'; import { Constants } from '@librechat/agents'; import { logger } from '@librechat/data-schemas'; import type { @@ -20,6 +23,7 @@ import { import { markSandboxReady } from './prewarm'; import { ContentFilterError } from '../middleware/contentFilter'; import { WorkspaceToolHttpError } from '../code/workspace'; +import { createAttachedWorkspaceBashTool } from '../code/command'; import { createCodeApiUploadRegistry } from '~/utils'; function createMockTool( @@ -464,6 +468,58 @@ describe('createToolExecuteHandler', () => { expect(results[0].status).toBe('error'); }); + it('closes the real command HTTP connection on foreground host cancellation', async () => { + let markStarted!: () => void; + let markDisconnected!: () => void; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const disconnected = new Promise((resolve) => { + markDisconnected = resolve; + }); + const server = createServer(async (req, res) => { + for await (const _chunk of req) { + /* Wait for the full command request. */ + } + res.once('close', () => { + if (!res.writableEnded) markDisconnected(); + }); + markStarted(); + }); + server.listen(0, '127.0.0.1'); + await once(server, 'listening'); + const { port } = server.address() as AddressInfo; + const tool = createAttachedWorkspaceBashTool({ + baseUrl: `http://127.0.0.1:${port}/v1`, + authHeaders: () => ({}), + workspaceId: 'project-a', + }); + const controller = new AbortController(); + const handler = createToolExecuteHandler({ + loadTools: async () => ({ loadedTools: [tool] }), + runSignal: controller.signal, + foregroundRunId: 'foreground-run', + }); + try { + const result = new Promise((resolve, reject) => { + handler.handle('on_tool_execute', { + toolCalls: [{ id: 'call-http', name: tool.name, args: { command: 'sleep 30' } }], + metadata: { run_id: 'foreground-run' }, + resolve, + reject, + } as ToolExecuteBatchRequest); + }); + await started; + controller.abort(); + expect((await result)[0].status).toBe('error'); + await disconnected; + } finally { + server.closeAllConnections(); + server.close(); + await once(server, 'close'); + } + }); + it('composes host cancellation with an SDK event circuit-breaker signal', async () => { const controller = new AbortController(); const eventController = new AbortController(); diff --git a/packages/api/src/code/command.spec.ts b/packages/api/src/code/command.spec.ts index fcf5420116..d265f5a754 100644 --- a/packages/api/src/code/command.spec.ts +++ b/packages/api/src/code/command.spec.ts @@ -66,6 +66,50 @@ function commandResponse(overrides: Record = {}): Response { } describe('createAttachedWorkspaceBashTool', () => { + test('disconnects the actual HTTP request when an invoked command is cancelled', async () => { + let markStarted!: () => void; + let markDisconnected!: () => void; + const started = new Promise((resolve) => { + markStarted = resolve; + }); + const disconnected = new Promise((resolve) => { + markDisconnected = resolve; + }); + const server = createServer(async (req, res) => { + for await (const _chunk of req) { + /* Consume the complete request before cancellation. */ + } + res.once('close', () => { + if (!res.writableEnded) markDisconnected(); + }); + markStarted(); + }); + server.listen(0, '127.0.0.1'); + await once(server, 'listening'); + const { port } = server.address() as AddressInfo; + const bashTool = createAttachedWorkspaceBashTool({ + baseUrl: `http://127.0.0.1:${port}/v1`, + authHeaders: () => ({}), + workspaceId: 'project-a', + }); + const controller = new AbortController(); + try { + const invocation = bashTool.invoke({ command: 'sleep 30' }, { signal: controller.signal }); + const settled = invocation.then( + () => ({ rejected: false }), + () => ({ rejected: true }), + ); + await started; + controller.abort(); + expect((await settled).rejected).toBe(true); + await disconnected; + } finally { + server.closeAllConnections(); + server.close(); + await once(server, 'close'); + } + }); + test('executes in the selected workspace and relative working directory', async () => { const fetchImpl: CodeBridgeFetch = jest.fn(async () => commandResponse()); const authHeaders = jest.fn().mockResolvedValue({ diff --git a/packages/api/src/code/command.ts b/packages/api/src/code/command.ts index 20b3122e11..25c3123378 100644 --- a/packages/api/src/code/command.ts +++ b/packages/api/src/code/command.ts @@ -1,3 +1,4 @@ +import { logger } from '@librechat/data-schemas'; import { tool } from '@librechat/agents/langchain/tools'; import { BashExecutionToolDefinition, @@ -212,25 +213,45 @@ export function createAttachedWorkspaceBashTool({ ); const timeoutMs = rawInput.timeoutMs ?? Math.min(WORKSPACE_COMMAND_DEFAULT_TIMEOUT_MS, effectiveMaxTimeoutMs); - const result = await executeWorkspaceTool({ - baseURL: baseUrl, - authHeaders: await authHeaders(), - request: { - protocolVersion: 1, - operation: 'execute_command', - workspaceId, - command, - ...(rawInput.cwd ? { cwd: rawInput.cwd } : {}), - timeoutMs, - maxOutputBytes: DEFAULT_OUTPUT_BYTES, - }, - signal: config?.signal, - fetchImpl, - }); - if (result.operation !== 'execute_command') { - throw new Error('Attached workspace returned an unexpected command result.'); + const signal = config?.signal; + const trace = { + runId: config?.metadata?.run_id, + workspaceId, + signalPresent: signal != null, + }; + const onAbort = (): void => { + logger.debug('[BYOMCommand] invocation signal aborted', trace); + }; + signal?.addEventListener('abort', onAbort, { once: true }); + logger.debug('[BYOMCommand] dispatch', { ...trace, aborted: signal?.aborted === true }); + try { + const result = await executeWorkspaceTool({ + baseURL: baseUrl, + authHeaders: await authHeaders(), + request: { + protocolVersion: 1, + operation: 'execute_command', + workspaceId, + command, + ...(rawInput.cwd ? { cwd: rawInput.cwd } : {}), + timeoutMs, + maxOutputBytes: DEFAULT_OUTPUT_BYTES, + }, + signal, + fetchImpl, + }); + if (result.operation !== 'execute_command') { + throw new Error('Attached workspace returned an unexpected command result.'); + } + logger.debug('[BYOMCommand] transport completed', trace); + return [formatCommandResult(result), {}]; + } finally { + signal?.removeEventListener('abort', onAbort); + logger.debug('[BYOMCommand] transport settled', { + ...trace, + aborted: signal?.aborted === true, + }); } - return [formatCommandResult(result), {}]; }, { name: BashExecutionToolDefinition.name,