From ebf70826b53af32ed378fb20b4d53e66a02f6b70 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sun, 6 Sep 2026 15:31:19 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=AB=20fix:=20Surface=20Sandbox=20Artif?= =?UTF-8?q?act=20Delivery=20Failures=20(#15671)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Surface sandbox artifact delivery failures * chore: Sort artifact delivery import --- api/server/services/Files/Code/process.js | 4 +- .../services/Files/Code/process.spec.js | 42 ++++++++++++++++++ packages/api/src/agents/handlers.spec.ts | 39 +++++++++++++++++ packages/api/src/agents/handlers.ts | 9 ++++ .../src/files/code/artifactDelivery.spec.ts | 31 +++++++++++++ .../api/src/files/code/artifactDelivery.ts | 43 +++++++++++++++++++ packages/api/src/files/code/index.ts | 1 + 7 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 packages/api/src/files/code/artifactDelivery.spec.ts create mode 100644 packages/api/src/files/code/artifactDelivery.ts diff --git a/api/server/services/Files/Code/process.js b/api/server/services/Files/Code/process.js index 2b5968c117..41cc6f456c 100644 --- a/api/server/services/Files/Code/process.js +++ b/api/server/services/Files/Code/process.js @@ -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} | null>} + * @returns {Promise<{stdout?: string, stderr?: string, session_id?: string, files?: Array, 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({ diff --git a/api/server/services/Files/Code/process.spec.js b/api/server/services/Files/Code/process.spec.js index aef4233c37..cfe2dcf775 100644 --- a/api/server/services/Files/Code/process.spec.js +++ b/api/server/services/Files/Code/process.spec.js @@ -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`; diff --git a/packages/api/src/agents/handlers.spec.ts b/packages/api/src/agents/handlers.spec.ts index f3b1ca632c..028fa9829b 100644 --- a/packages/api/src/agents/handlers.spec.ts +++ b/packages/api/src/agents/handlers.spec.ts @@ -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 diff --git a/packages/api/src/agents/handlers.ts b/packages/api/src/agents/handlers.ts index b9110d1a1b..b27c4e5208 100644 --- a/packages/api/src/agents/handlers.ts +++ b/packages/api/src/agents/handlers.ts @@ -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).`; diff --git a/packages/api/src/files/code/artifactDelivery.spec.ts b/packages/api/src/files/code/artifactDelivery.spec.ts new file mode 100644 index 0000000000..facf82de22 --- /dev/null +++ b/packages/api/src/files/code/artifactDelivery.spec.ts @@ -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(); + }); +}); diff --git a/packages/api/src/files/code/artifactDelivery.ts b/packages/api/src/files/code/artifactDelivery.ts new file mode 100644 index 0000000000..95bf33b92f --- /dev/null +++ b/packages/api/src/files/code/artifactDelivery.ts @@ -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; + 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, + }; +} diff --git a/packages/api/src/files/code/index.ts b/packages/api/src/files/code/index.ts index 6573508a3f..66aef1a3dc 100644 --- a/packages/api/src/files/code/index.ts +++ b/packages/api/src/files/code/index.ts @@ -1,4 +1,5 @@ export * from './classify'; +export * from './artifactDelivery'; export * from './destinations'; export * from './errors'; export * from './extract';