mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
📫 fix: Surface Sandbox Artifact Delivery Failures (#15671)
* fix: Surface sandbox artifact delivery failures * chore: Sort artifact delivery import
This commit is contained in:
parent
deda142d58
commit
ebf70826b5
7 changed files with 168 additions and 1 deletions
|
|
@ -34,6 +34,7 @@ const {
|
|||
CODE_OUTPUT_PREFLIGHT_MAX_BYTES,
|
||||
CODE_OUTPUT_PREFLIGHT_MAX_COUNT,
|
||||
sortCodeFilesByDestinationPriority,
|
||||
normalizeArtifactDeliveryFailure,
|
||||
} = require('@librechat/api');
|
||||
const {
|
||||
Tools,
|
||||
|
|
@ -2057,7 +2058,7 @@ async function execSandboxImageChunk({
|
|||
* @param {string} [params.session_id] - Sandbox session id from the seeded context.
|
||||
* @param {Array<{id: string, name: string, session_id?: string}>} [params.files] - File refs to mount.
|
||||
* @param {ServerRequest} [params.req] - Current authenticated request, used to mint Code API auth.
|
||||
* @returns {Promise<{stdout?: string, stderr?: string, session_id?: string, files?: Array<Object>} | null>}
|
||||
* @returns {Promise<{stdout?: string, stderr?: string, session_id?: string, files?: Array<Object>, artifact_delivery?: {code: 'artifact_delivery_failed', status: 'partial' | 'failed', attempted: number, delivered: number, failed: number}} | null>}
|
||||
*/
|
||||
async function writeSandboxFile({
|
||||
file_path,
|
||||
|
|
@ -2138,6 +2139,7 @@ async function writeSandboxFile({
|
|||
stderr: result.stderr == null ? undefined : String(result.stderr),
|
||||
session_id: result.session_id,
|
||||
files: result.files,
|
||||
artifact_delivery: normalizeArtifactDeliveryFailure(result.artifact_delivery),
|
||||
};
|
||||
} catch (error) {
|
||||
logAxiosError({
|
||||
|
|
|
|||
|
|
@ -77,6 +77,16 @@ jest.mock('@librechat/api', () => {
|
|||
sanitizeArtifactPath: jest.fn((name) => name),
|
||||
flattenArtifactPath: jest.fn((name) => name.replace(/\//g, '__')),
|
||||
createAxiosInstance: jest.fn(() => mockAxios),
|
||||
normalizeArtifactDeliveryFailure: (value) =>
|
||||
value?.code === 'artifact_delivery_failed'
|
||||
? {
|
||||
code: value.code,
|
||||
status: value.status,
|
||||
attempted: value.attempted,
|
||||
delivered: value.delivered,
|
||||
failed: value.failed,
|
||||
}
|
||||
: undefined,
|
||||
executeWorkspaceTool: (...args) => mockExecuteWorkspaceTool(...args),
|
||||
getCodeApiAuthHeaders: jest.fn(async () => ({})),
|
||||
/* Windowing, sizing and rate-limit policy are real code in
|
||||
|
|
@ -2459,6 +2469,38 @@ describe('Code Process', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('forwards only a valid bounded artifact delivery failure', async () => {
|
||||
mockAxios.mockResolvedValueOnce({
|
||||
data: {
|
||||
stdout: 'WROTE 1 bytes to /mnt/data/new.txt\n',
|
||||
session_id: 'sess-new',
|
||||
files: [],
|
||||
artifact_delivery: {
|
||||
code: 'artifact_delivery_failed',
|
||||
status: 'failed',
|
||||
attempted: 1,
|
||||
delivered: 0,
|
||||
failed: 1,
|
||||
detail: 'private object storage failure',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const result = await writeSandboxFile({
|
||||
file_path: '/mnt/data/new.txt',
|
||||
content: 'x',
|
||||
});
|
||||
|
||||
expect(result.artifact_delivery).toEqual({
|
||||
code: 'artifact_delivery_failed',
|
||||
status: 'failed',
|
||||
attempted: 1,
|
||||
delivered: 0,
|
||||
failed: 1,
|
||||
});
|
||||
expect(JSON.stringify(result)).not.toContain('private object storage failure');
|
||||
});
|
||||
|
||||
it('encodes path and content in a base64 JSON payload instead of shell-interpolating them', async () => {
|
||||
mockAxios.mockResolvedValueOnce({ data: { stdout: 'ok', stderr: '', session_id: 'sess' } });
|
||||
const trickyPath = `/mnt/data/quote'$(whoami).txt`;
|
||||
|
|
|
|||
|
|
@ -3949,6 +3949,45 @@ describe('createToolExecuteHandler', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('does not report a sandbox write as durable when artifact delivery failed', async () => {
|
||||
const readSandboxFile = jest.fn(async () => {
|
||||
throw new Error('cat: /mnt/data/new.txt: No such file or directory');
|
||||
});
|
||||
const writeSandboxFile = jest.fn(async () => ({
|
||||
stdout: 'WROTE 11 bytes to /mnt/data/new.txt\n',
|
||||
session_id: 'sess-new',
|
||||
files: [],
|
||||
artifact_delivery: {
|
||||
code: 'artifact_delivery_failed' as const,
|
||||
status: 'failed' as const,
|
||||
attempted: 1,
|
||||
delivered: 0,
|
||||
failed: 1,
|
||||
},
|
||||
}));
|
||||
const handler = makeSandboxAuthoringHandler({
|
||||
readSandboxFile,
|
||||
writeSandboxFile,
|
||||
});
|
||||
|
||||
const [result] = await invokeHandler(handler, [
|
||||
{
|
||||
id: 'call_create_sandbox_delivery_failure',
|
||||
name: 'create_file',
|
||||
args: {
|
||||
path: '/mnt/data/new.txt',
|
||||
content: 'hello world',
|
||||
},
|
||||
} as unknown as ToolCallRequest,
|
||||
]);
|
||||
|
||||
expect(result.status).toBe('error');
|
||||
expect(result.errorMessage).toContain('could not be persisted');
|
||||
expect(result.errorMessage).toContain('do not retry automatically');
|
||||
expect(result.errorMessage).not.toContain('storage');
|
||||
expect(result.artifact).toBeUndefined();
|
||||
});
|
||||
|
||||
it('carries whole file refs into the next authoring call on the same path', async () => {
|
||||
/**
|
||||
* Regression: the batch-local sandbox context rebuilt each ref from
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ import type {
|
|||
BackgroundToolWakeupRegistration,
|
||||
} from './backgroundCompletion';
|
||||
import type { SkillFileRecord, PrimeSkillFilesResult } from './skillFiles';
|
||||
import type { ArtifactDeliveryFailure } from '~/files/code';
|
||||
import type { BackgroundToolResultState } from './harvest';
|
||||
import type { CodeExecutionContext } from './execution';
|
||||
import type { TextContentFragment } from '~/protection';
|
||||
|
|
@ -634,6 +635,7 @@ export interface ToolExecuteOptions {
|
|||
stderr?: string;
|
||||
session_id?: string;
|
||||
files?: SandboxFileRef[];
|
||||
artifact_delivery?: ArtifactDeliveryFailure;
|
||||
} | null>;
|
||||
}
|
||||
|
||||
|
|
@ -2837,6 +2839,13 @@ async function writeSandboxTextForAuthoring({
|
|||
if (!writeResult) {
|
||||
return errorResult(tc, `Failed to write "${filePath}" to the code-execution sandbox.`);
|
||||
}
|
||||
if (writeResult.artifact_delivery) {
|
||||
const { attempted, failed } = writeResult.artifact_delivery;
|
||||
return errorResult(
|
||||
tc,
|
||||
`Wrote "${filePath}" in the sandbox, but ${failed} of ${attempted} generated files could not be persisted. The file is not guaranteed to be available to later calls or downloadable. The execution may have had side effects; do not retry automatically.`,
|
||||
);
|
||||
}
|
||||
|
||||
const action = created ? 'Created' : 'Updated';
|
||||
const summary = `${action} ${filePath} (${content.length} chars).`;
|
||||
|
|
|
|||
31
packages/api/src/files/code/artifactDelivery.spec.ts
Normal file
31
packages/api/src/files/code/artifactDelivery.spec.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { normalizeArtifactDeliveryFailure } from './artifactDelivery';
|
||||
|
||||
describe('normalizeArtifactDeliveryFailure', () => {
|
||||
it('retains only the bounded public contract', () => {
|
||||
expect(
|
||||
normalizeArtifactDeliveryFailure({
|
||||
code: 'artifact_delivery_failed',
|
||||
status: 'partial',
|
||||
attempted: 3,
|
||||
delivered: 2,
|
||||
failed: 1,
|
||||
detail: 'private storage failure',
|
||||
}),
|
||||
).toEqual({
|
||||
code: 'artifact_delivery_failed',
|
||||
status: 'partial',
|
||||
attempted: 3,
|
||||
delivered: 2,
|
||||
failed: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
null,
|
||||
{ code: 'storage_error', status: 'failed', attempted: 1, delivered: 0, failed: 1 },
|
||||
{ code: 'artifact_delivery_failed', status: 'failed', attempted: 2, delivered: 1, failed: 1 },
|
||||
{ code: 'artifact_delivery_failed', status: 'partial', attempted: 1, delivered: 0, failed: 1 },
|
||||
])('rejects malformed external values', (value) => {
|
||||
expect(normalizeArtifactDeliveryFailure(value)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
43
packages/api/src/files/code/artifactDelivery.ts
Normal file
43
packages/api/src/files/code/artifactDelivery.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
export type ArtifactDeliveryFailure = {
|
||||
code: 'artifact_delivery_failed';
|
||||
status: 'partial' | 'failed';
|
||||
attempted: number;
|
||||
delivered: number;
|
||||
failed: number;
|
||||
};
|
||||
|
||||
export function normalizeArtifactDeliveryFailure(
|
||||
value: unknown,
|
||||
): ArtifactDeliveryFailure | undefined {
|
||||
if (value == null || typeof value !== 'object' || Array.isArray(value)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const candidate = value as Partial<ArtifactDeliveryFailure>;
|
||||
if (
|
||||
candidate.code !== 'artifact_delivery_failed' ||
|
||||
(candidate.status !== 'partial' && candidate.status !== 'failed') ||
|
||||
!Number.isSafeInteger(candidate.attempted) ||
|
||||
!Number.isSafeInteger(candidate.delivered) ||
|
||||
!Number.isSafeInteger(candidate.failed) ||
|
||||
candidate.attempted == null ||
|
||||
candidate.delivered == null ||
|
||||
candidate.failed == null ||
|
||||
candidate.attempted < 1 ||
|
||||
candidate.delivered < 0 ||
|
||||
candidate.failed < 1 ||
|
||||
candidate.attempted !== candidate.delivered + candidate.failed ||
|
||||
(candidate.status === 'failed' && candidate.delivered !== 0) ||
|
||||
(candidate.status === 'partial' && candidate.delivered === 0)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
code: candidate.code,
|
||||
status: candidate.status,
|
||||
attempted: candidate.attempted,
|
||||
delivered: candidate.delivered,
|
||||
failed: candidate.failed,
|
||||
};
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
export * from './classify';
|
||||
export * from './artifactDelivery';
|
||||
export * from './destinations';
|
||||
export * from './errors';
|
||||
export * from './extract';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue