mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 22:14:27 +00:00
🩻 fix: Preserve Workspace Tool HTTP Diagnostics (#15723)
* fix: preserve workspace tool HTTP diagnostics * fix: filter raw workspace diagnostics before logging * fix: preserve workspace cancellation and exact body limits
This commit is contained in:
parent
f50c40e2f5
commit
1b51ec49ee
4 changed files with 305 additions and 62 deletions
|
|
@ -273,7 +273,7 @@ describe('executeWorkspaceTool', () => {
|
|||
).rejects.toMatchObject({ reason: 'invalid' });
|
||||
});
|
||||
|
||||
test('surfaces bounded upstream failures without returning their body', async () => {
|
||||
test('preserves the upstream status when the error body stalls', async () => {
|
||||
const cancel = jest.fn();
|
||||
const fetchImpl = jest.fn().mockResolvedValue(
|
||||
new Response(
|
||||
|
|
@ -296,10 +296,105 @@ describe('executeWorkspaceTool', () => {
|
|||
},
|
||||
fetchImpl,
|
||||
}),
|
||||
).rejects.toMatchObject({ reason: 'rejected', upstreamStatus: 503 });
|
||||
).rejects.toMatchObject({
|
||||
reason: 'rejected',
|
||||
upstreamStatus: 503,
|
||||
upstreamBodyTruncated: true,
|
||||
});
|
||||
expect(cancel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test.each([400, 409, 503, 504])('preserves HTTP %i and its diagnostic body', async (status) => {
|
||||
const body = JSON.stringify({ code: 'ASSIGNMENT_EXPIRED', error: 'Assignment expired' });
|
||||
await expect(
|
||||
executeWorkspaceTool({
|
||||
baseURL: 'https://code.example.com',
|
||||
authHeaders: {},
|
||||
request: { protocolVersion: 1, operation: 'list_files', workspaceId: 'primary' },
|
||||
fetchImpl: jest.fn(async () => new Response(body, { status })),
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
reason: 'rejected',
|
||||
upstreamStatus: status,
|
||||
upstreamBody: body,
|
||||
upstreamBodyTruncated: false,
|
||||
message: expect.stringContaining(`upstreamStatus: ${status}`),
|
||||
});
|
||||
});
|
||||
|
||||
test.each([4095, 4096, 4097])(
|
||||
'reports truncation correctly for a %i-byte error body',
|
||||
async (size) => {
|
||||
await expect(
|
||||
executeWorkspaceTool({
|
||||
baseURL: 'https://code.example.com',
|
||||
authHeaders: {},
|
||||
request: { protocolVersion: 1, operation: 'list_files', workspaceId: 'primary' },
|
||||
fetchImpl: jest.fn(async () => new Response('x'.repeat(size), { status: 503 })),
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
upstreamStatus: 503,
|
||||
upstreamBody: 'x'.repeat(Math.min(size, 4096)),
|
||||
upstreamBodyTruncated: size > 4096,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
test('preserves caller cancellation during a rejected response body read', async () => {
|
||||
const controller = new AbortController();
|
||||
const cancel = jest.fn();
|
||||
const request = executeWorkspaceTool({
|
||||
baseURL: 'https://code.example.com',
|
||||
authHeaders: {},
|
||||
signal: controller.signal,
|
||||
request: { protocolVersion: 1, operation: 'list_files', workspaceId: 'primary' },
|
||||
fetchImpl: jest.fn(async () => new Response(new ReadableStream({ cancel }), { status: 503 })),
|
||||
});
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
controller.abort();
|
||||
await expect(request).rejects.toBe(controller.signal.reason);
|
||||
expect(cancel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('bounds a streaming error body and cancels the unread remainder', async () => {
|
||||
const cancel = jest.fn();
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(new TextEncoder().encode('x'.repeat(10_000)));
|
||||
},
|
||||
cancel,
|
||||
});
|
||||
await expect(
|
||||
executeWorkspaceTool({
|
||||
baseURL: 'https://code.example.com',
|
||||
authHeaders: {},
|
||||
request: { protocolVersion: 1, operation: 'list_files', workspaceId: 'primary' },
|
||||
fetchImpl: jest.fn(async () => new Response(body, { status: 504 })),
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
upstreamStatus: 504,
|
||||
upstreamBody: 'x'.repeat(4096),
|
||||
upstreamBodyTruncated: true,
|
||||
});
|
||||
expect(cancel).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('retains HTTP status when reading the error body fails', async () => {
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.error(new Error('socket closed'));
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
executeWorkspaceTool({
|
||||
baseURL: 'https://code.example.com',
|
||||
authHeaders: {},
|
||||
request: { protocolVersion: 1, operation: 'list_files', workspaceId: 'primary' },
|
||||
fetchImpl: jest.fn(async () => new Response(body, { status: 503 })),
|
||||
}),
|
||||
).rejects.toMatchObject({ upstreamStatus: 503, upstreamBodyTruncated: true });
|
||||
});
|
||||
|
||||
test('validates bounded search matches before returning them', async () => {
|
||||
const fetchImpl = jest.fn().mockResolvedValue(
|
||||
new Response(
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ const MAX_COMMAND_OUTPUT_BYTES = 1024 * 1024;
|
|||
const MAX_COMMAND_SIGNAL_LENGTH = 32;
|
||||
const WORKSPACE_COMMAND_TRANSPORT_GRACE_MS = 5_000;
|
||||
const MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
||||
const MAX_ERROR_BODY_BYTES = 4096;
|
||||
const ERROR_BODY_TIMEOUT_MS = 1000;
|
||||
const READ_RESULT_KEYS = new Set([
|
||||
'protocolVersion',
|
||||
'operation',
|
||||
|
|
@ -246,12 +248,64 @@ export class WorkspaceToolHttpError extends Error {
|
|||
constructor(
|
||||
public readonly reason: 'rejected' | 'invalid' | 'timeout' | 'failed',
|
||||
public readonly upstreamStatus?: number,
|
||||
public readonly upstreamBody?: string,
|
||||
public readonly upstreamBodyTruncated = false,
|
||||
) {
|
||||
super(`Workspace tool request ${reason}`);
|
||||
super(
|
||||
`Workspace tool request ${reason}` +
|
||||
(upstreamStatus == null ? '' : ` (upstreamStatus: ${upstreamStatus})`) +
|
||||
(upstreamBody ? `; upstreamBody: ${JSON.stringify(upstreamBody)}` : '') +
|
||||
(upstreamBodyTruncated ? ' [body truncated or incomplete]' : ''),
|
||||
);
|
||||
this.name = 'WorkspaceToolHttpError';
|
||||
}
|
||||
}
|
||||
|
||||
/** Keep a received HTTP status even if reading its diagnostic body fails or stalls. */
|
||||
async function readErrorBody(
|
||||
response: Response,
|
||||
signal: AbortSignal,
|
||||
): Promise<{
|
||||
body: string;
|
||||
truncated: boolean;
|
||||
}> {
|
||||
if (!response.body) return { body: '', truncated: false };
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let body = '';
|
||||
let bytes = 0;
|
||||
let complete = false;
|
||||
let interrupted = false;
|
||||
const cancel = () => {
|
||||
interrupted = true;
|
||||
void reader.cancel().catch(() => undefined);
|
||||
};
|
||||
const timer = setTimeout(cancel, ERROR_BODY_TIMEOUT_MS);
|
||||
signal.addEventListener('abort', cancel, { once: true });
|
||||
try {
|
||||
if (signal.aborted) return { body, truncated: true };
|
||||
while (bytes <= MAX_ERROR_BODY_BYTES) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
complete = !interrupted;
|
||||
body += decoder.decode();
|
||||
break;
|
||||
}
|
||||
const remaining = MAX_ERROR_BODY_BYTES - bytes;
|
||||
body += decoder.decode(value.subarray(0, remaining), { stream: true });
|
||||
bytes += value.byteLength;
|
||||
}
|
||||
} catch {
|
||||
complete = false;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
signal.removeEventListener('abort', cancel);
|
||||
cancel();
|
||||
reader.releaseLock();
|
||||
}
|
||||
return { body, truncated: !complete || signal.aborted };
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value != null;
|
||||
}
|
||||
|
|
@ -639,8 +693,9 @@ export async function executeWorkspaceTool({
|
|||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
await response.body?.cancel().catch(() => undefined);
|
||||
throw new WorkspaceToolHttpError('rejected', response.status);
|
||||
const { body, truncated } = await readErrorBody(response, requestSignal);
|
||||
signal?.throwIfAborted();
|
||||
throw new WorkspaceToolHttpError('rejected', response.status, body, truncated);
|
||||
}
|
||||
const result = await readBoundedJson(response, requestSignal);
|
||||
if (!isValidResult(request, result)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue