mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 23:13:35 +00:00
🥼 feat: Expose BYOM Cancellation Diagnostics (#15810)
* 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.
This commit is contained in:
parent
e49cb798da
commit
bee183e6a9
3 changed files with 139 additions and 18 deletions
|
|
@ -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<void>((resolve) => {
|
||||
markStarted = resolve;
|
||||
});
|
||||
const disconnected = new Promise<void>((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<ToolExecuteResult[]>((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();
|
||||
|
|
|
|||
|
|
@ -66,6 +66,50 @@ function commandResponse(overrides: Record<string, unknown> = {}): 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<void>((resolve) => {
|
||||
markStarted = resolve;
|
||||
});
|
||||
const disconnected = new Promise<void>((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({
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue