🪴 fix: Recognize Live Code Files Across API Layouts (#18165)
Some checks failed
Publish `@librechat/data-schemas` to NPM / pack (push) Has been cancelled
Docker Dev Images Build / publish (push) Has been cancelled
Sync Helm Chart Tags / Ignore non-main push (push) Has been cancelled
Sync Helm Chart Tags / Sync chart tags (push) Has been cancelled
Publish `@librechat/data-schemas` to NPM / publish-npm (push) Has been cancelled

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-11-14 14:00:26 -05:00 • committed by GitHub
parent 68c436f6bc
commit 5473f91036
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 439 additions and 18 deletions

View file

@ -39,7 +39,7 @@ const {
appendCodeFileContextLine,
createCodeReuploadFailures,
openCodeFileSource,
getUploadedCodeEnvFilename,
createCodeFileUploadRef,
checkCodeFileActive: checkIfActive,
CODE_OUTPUT_PREFLIGHT_MAX_BYTES,
CODE_OUTPUT_PREFLIGHT_MAX_COUNT,
@ -896,16 +896,13 @@ const primeFiles = async (options) => {
* upload preserves the resource identity, only the storage
* pointer changes.
*/
const newRef = {
kind: sourceRef.kind,
id: sourceRef.id,
storage_session_id: uploaded.storage_session_id,
file_id: uploaded.file_id,
sandboxFilename: getUploadedCodeEnvFilename(uploaded, sandboxName),
const newRef = createCodeFileUploadRef({
sourceRef,
uploaded,
sandboxFilename: sandboxName,
executionProfile,
...(executionRouteKey !== executionProfile ? { executionRouteKey } : {}),
...(sourceRef.kind === 'skill' ? { version: sourceRef.version } : {}),
};
executionRouteKey,
});
const updatedRefs = mergeCodeEnvRef(file.metadata, newRef);

View file

@ -196,7 +196,7 @@ jest.mock('@librechat/api', () => {
codeServerHttpsAgent: jest.requireActual('@librechat/api').codeServerHttpsAgent,
selectCodeFiles: jest.requireActual('@librechat/api').selectCodeFiles,
getCodeFileInfo: jest.requireActual('@librechat/api').getCodeFileInfo,
getUploadedCodeEnvFilename: jest.requireActual('@librechat/api').getUploadedCodeEnvFilename,
createCodeFileUploadRef: jest.requireActual('@librechat/api').createCodeFileUploadRef,
checkCodeFileActive: jest.requireActual('@librechat/api').checkCodeFileActive,
};
});
@ -3556,6 +3556,7 @@ describe('Code Process', () => {
file_id: 'NEW_ID',
executionProfile: 'default',
sandboxFilename: 'sentinel.txt',
provisionedAt: expect.any(Number),
},
'metadata.codeEnvRefs.default': {
kind: 'user',
@ -3564,6 +3565,7 @@ describe('Code Process', () => {
file_id: 'NEW_ID',
executionProfile: 'default',
sandboxFilename: 'sentinel.txt',
provisionedAt: expect.any(Number),
},
}),
);

View file

@ -1,5 +1,9 @@
import FormData from 'form-data';
import { appendCodeEnvFileIdentity, buildCodeEnvDownloadQuery } from './identity';
import {
appendCodeEnvFileIdentity,
buildCodeEnvDownloadQuery,
getCodeEnvSessionFileIds,
} from './identity';
describe('appendCodeEnvFileIdentity', () => {
/* Validation rules mirror codeapi's `parseUploadSessionKeyInput`
@ -114,3 +118,82 @@ describe('buildCodeEnvDownloadQuery', () => {
);
});
});
describe('getCodeEnvSessionFileIds', () => {
const canonical = (id: string) =>
`session/.codeapi-objects/${Buffer.from(id).toString('base64url')}`;
it('indexes hosted, canonical and legacy identities without guessing dotted IDs', () => {
expect(
getCodeEnvSessionFileIds(
[
{ fileId: 'hosted', name: canonical('not-hosted') },
{ name: canonical('dotted.id') },
{ name: 'session/report.csv' },
{ name: 'session/report.csv.txt' },
{ name: 'session/extensionless' },
],
'session',
),
).toEqual(new Set(['hosted', 'dotted.id', 'report', 'report.csv', 'extensionless']));
expect(
getCodeEnvSessionFileIds([{ name: 'session/report.csv' }], 'session').has('report.csv'),
).toBe(false);
expect(
getCodeEnvSessionFileIds([{ name: canonical('report.csv') }], 'session').has('report'),
).toBe(false);
});
it.each([
null,
false,
'report',
42,
{},
{ fileId: 42 },
{ name: 42 },
{ name: 'session-other/report.csv' },
{ name: 'other/.codeapi-objects/cmVwb3J0' },
{ name: 'prefix/session/report.csv' },
{ name: 'session/nested/report.csv' },
{ name: 'session//report.csv' },
{ name: 'session/./report.csv' },
{ name: 'session/../report.csv' },
{ name: 'session/..\\report.csv' },
{ name: 'session/report\u0000.csv' },
{ name: 'session/.codeapi-objects/' },
{ name: 'session/.codeapi-objects/cmVwb3J0=' },
{ name: 'session/.codeapi-objects/cmVwb3J0/extra' },
{ name: 'session/.codeapi-objects/%63mVwb3J0' },
{ name: 'session/.codeapi-objects/_w' },
{ name: 'session/.codeapi-objects/Zh' },
])('does not derive an identity from malformed or cross-session entry %p', (entry) => {
expect(getCodeEnvSessionFileIds([entry], 'session').size).toBe(0);
});
it('does not decode URL paths or match file ID prefixes', () => {
const ids = getCodeEnvSessionFileIds(
[
{ name: 'session/report%2Ecsv.txt' },
{ name: 'session/report-extra.csv' },
{ name: canonical('report-extra') },
],
'session',
);
expect(ids).toEqual(new Set(['report%2Ecsv', 'report-extra']));
expect(ids.has('report')).toBe(false);
expect(ids.has('report.csv')).toBe(false);
});
it('reads each summary entry a constant number of times for a large session', () => {
const name = jest.fn(() => 'session/report.csv');
const summaries = Array.from({ length: 10_000 }, () => ({
get name() {
return name();
},
}));
const ids = getCodeEnvSessionFileIds(summaries, 'session');
for (let index = 0; index < 10_000; index++) expect(ids.has('report')).toBe(true);
expect(name.mock.calls.length).toBeLessThanOrEqual(30_000);
});
});

View file

@ -1,3 +1,4 @@
import path from 'node:path';
import { CODE_ENV_KINDS } from 'librechat-data-provider';
import type { CodeEnvKind } from 'librechat-data-provider';
import type FormData from 'form-data';
@ -90,3 +91,47 @@ export function buildCodeEnvDownloadQuery(identity: CodeEnvIdentity): string {
}
return `?${params.toString()}`;
}
/** Summary keys follow Code API's canonicalObjectKey/legacyObjectId storage contract. */
export function getCodeEnvSessionFileIds(
files: readonly unknown[],
sessionId: string,
): Set<string> {
const ids = new Set<string>();
const prefix = `${sessionId}/`;
const canonicalDirectory = '.codeapi-objects/';
for (const file of files) {
if (file == null || typeof file !== 'object') {
continue;
}
if ('fileId' in file && typeof file.fileId === 'string' && file.fileId !== '') {
ids.add(file.fileId);
continue;
}
if (!('name' in file) || typeof file.name !== 'string' || !file.name.startsWith(prefix)) {
continue;
}
const name = file.name.slice(prefix.length);
if (name.startsWith(canonicalDirectory)) {
const encoded = name.slice(canonicalDirectory.length);
if (!/^[A-Za-z0-9_-]+$/.test(encoded)) {
continue;
}
const id = Buffer.from(encoded, 'base64url').toString('utf8');
if (id && Buffer.from(id, 'utf8').toString('base64url') === encoded) {
ids.add(id);
}
continue;
}
if (!name || name === '.' || name === '..' || /[/\\\p{Cc}]/u.test(name)) {
continue;
}
// Legacy storage appends the filename's final extension, including for dotted IDs.
const extension = path.posix.extname(name);
const id = extension === '' ? name : name.slice(0, -extension.length);
if (id) {
ids.add(id);
}
}
return ids;
}

View file

@ -4,6 +4,7 @@ import { FileContext } from 'librechat-data-provider';
import type { CodeEnvRef, TFile } from 'librechat-data-provider';
import {
selectCodeFiles,
createCodeFileUploadRef,
getCodeFileLocation,
getCodeFileContextLine,
appendCodeFileContextLine,
@ -632,3 +633,39 @@ describe('setMissingCodeFilesContext', () => {
expect(Object.values(map)).toEqual(['files', 'note']);
});
});
describe('createCodeFileUploadRef', () => {
it('stamps only the completed upload and preserves its route, resource identity and receipt name', () => {
const before = Date.now();
const ref = createCodeFileUploadRef({
sourceRef: {
kind: 'skill',
id: 'shared-skill',
version: 7,
storage_session_id: 'old',
file_id: 'old',
provisionedAt: 1,
},
uploaded: {
storage_session_id: 'fresh-session',
file_id: 'fresh-id',
filename: 'stored.csv',
},
sandboxFilename: 'requested.csv',
executionProfile: 'stateful',
executionRouteKey: 'stateful:worker',
});
expect(ref).toMatchObject({
kind: 'skill',
id: 'shared-skill',
version: 7,
storage_session_id: 'fresh-session',
file_id: 'fresh-id',
sandboxFilename: 'stored.csv',
executionProfile: 'stateful',
executionRouteKey: 'stateful:worker',
});
expect(ref.provisionedAt).toBeGreaterThanOrEqual(before);
expect(ref.provisionedAt).toBeLessThanOrEqual(Date.now());
});
});

View file

@ -26,12 +26,40 @@ import {
createCodeApiUploadRegistry,
withCodeApiUploadSlot,
} from '~/utils/code';
import { getCodeEnvUploadFilename, getUploadedCodeEnvFilename } from './form';
import { isStorageNotFoundError } from '~/files/encode/utils';
import { codeExecutionHeaders } from '~/agents/execution';
import { buildCodeEnvDownloadQuery } from './identity';
import { getCodeEnvUploadFilename } from './form';
import { isAbortError } from '~/utils/errors';
/** Only a completed upload creates a freshness receipt; live output provenance does not. */
export function createCodeFileUploadRef({
sourceRef,
uploaded,
sandboxFilename,
executionProfile,
executionRouteKey = executionProfile,
}: {
sourceRef: CodeEnvRef | SnapshotCodeOwner;
uploaded: Pick<CodeEnvRef, 'storage_session_id' | 'file_id'> & { filename?: string };
sandboxFilename: string;
executionProfile: 'default' | 'stateful';
executionRouteKey?: string;
}): CodeEnvRef {
return {
...(sourceRef.kind === 'skill'
? { kind: sourceRef.kind, version: sourceRef.version }
: { kind: sourceRef.kind }),
id: sourceRef.id,
storage_session_id: uploaded.storage_session_id,
file_id: uploaded.file_id,
sandboxFilename: getUploadedCodeEnvFilename(uploaded, sandboxFilename),
executionProfile,
...(executionRouteKey !== executionProfile ? { executionRouteKey } : {}),
provisionedAt: Date.now(),
};
}
export interface CodeFileInfo {
lastModified?: string;
originalFilename?: string;

View file

@ -0,0 +1,200 @@
import axios from 'axios';
import { Readable } from 'node:stream';
import { createServer } from 'node:http';
import { buffer } from 'node:stream/consumers';
import { FileContext, FileSources } from 'librechat-data-provider';
import type { CodeEnvRef, TFile } from 'librechat-data-provider';
import type { AddressInfo } from 'node:net';
import type { ServerRequest } from '~/types';
import { getCodeFileInfo, selectCodeFiles } from '../code/priming';
import { createCodeApiUploadRegistry } from '~/utils/code';
import { createProvisionService } from './service';
jest.mock('~/auth/codeapi', () => ({
getCodeApiAuthHeaders: jest.fn(async () => ({})),
}));
const SESSION = 'retained-session';
const timestamp = new Date().toISOString();
function retainedFile(index: number): TFile {
return {
file_id: `local-${index}`,
user: 'owner',
filename: `report-${index}.csv`,
filepath: `/stored/report-${index}.csv`,
type: 'text/csv',
source: FileSources.local,
...(index % 2 === 0 ? { context: FileContext.execute_code } : {}),
metadata: {
codeEnvRef: {
kind: 'user',
id: 'owner',
storage_session_id: SESSION,
file_id: `remote.${index}`,
},
},
} as TFile;
}
describe('Code API summary liveness over HTTP', () => {
let summary: unknown;
let status: number;
let requests: string[];
let uploadedBodies: Buffer[];
let objectTimestamp: string;
let baseURL: string;
const server = createServer(async (request, response) => {
const url = new URL(request.url!, 'http://fixture');
requests.push(`${request.method} ${url.pathname}${url.search}`);
response.setHeader('Content-Type', 'application/json');
if (url.pathname === `/v1/files/${SESSION}`) {
response.writeHead(status).end(JSON.stringify(summary));
} else if (url.pathname.startsWith(`/v1/sessions/${SESSION}/objects/`)) {
response.end(JSON.stringify({ lastModified: objectTimestamp }));
} else if (request.method === 'POST' && url.pathname === '/v1/upload') {
uploadedBodies.push(await buffer(request));
response.end(JSON.stringify({ storage_session_id: SESSION, file_id: 'restored' }));
} else {
response.writeHead(404).end('{}');
}
});
const req = {
user: { id: 'owner' },
app: { locals: { codeApiUploadRegistry: createCodeApiUploadRegistry() } },
} as unknown as ServerRequest;
const request = axios.create({ proxy: false });
const service = createProvisionService({
getStrategyFunctions: (source) =>
source === FileSources.execute_code
? {
handleFileUpload: async ({ stream }) => {
const response = await request.post(
`${baseURL}/upload`,
await buffer(stream as Readable),
);
return response.data;
},
}
: { getDownloadStream: async () => Readable.from('immutable stored bytes') },
uploadVectors: async () => undefined,
loadAuthValues: async () => ({}),
});
const getFileInfo = (ref: CodeEnvRef) =>
getCodeFileInfo({
ref,
req,
route: { baseUrl: baseURL },
request,
getBaseURL: () => baseURL,
getAuthHeaders: async () => ({}),
});
beforeAll(async () => {
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
baseURL = `http://127.0.0.1:${(server.address() as AddressInfo).port}/v1`;
});
afterAll(async () => {
server.closeAllConnections();
await new Promise<void>((resolve, reject) =>
server.close((error) => (error ? reject(error) : resolve())),
);
});
beforeEach(() => {
status = 200;
objectTimestamp = timestamp;
summary = [];
requests = [];
uploadedBodies = [];
});
it.each(['canonical', 'legacy', 'hosted'])(
'retains 30 %s user/legacy files without uploading again',
async (layout) => {
const files = Array.from({ length: 30 }, (_, index) => retainedFile(index));
summary = files.map((file) => {
const id = file.metadata!.codeEnvRef!.file_id;
return layout === 'hosted'
? { fileId: id }
: {
name:
layout === 'canonical'
? `${SESSION}/.codeapi-objects/${Buffer.from(id).toString('base64url')}`
: `${SESSION}/${id}.csv`,
size: 22,
lastModified: timestamp,
etag: 'opaque',
};
});
const retained = await service.checkSessionsAlive({ files, req, baseURL });
for (const file of files) {
if (!retained.has(file.file_id)) {
await service.provisionToCodeEnv({ req, file, route: { baseUrl: baseURL } });
}
}
const { selected } = await selectCodeFiles({ files, routeKey: 'default', getFileInfo });
expect(uploadedBodies).toHaveLength(0);
expect(retained.size).toBe(30);
expect(selected).toHaveLength(30);
expect(selected.every((file) => file.isActive)).toBe(true);
expect(requests.filter((url) => url.startsWith('GET /v1/files/'))).toEqual([
`GET /v1/files/${SESSION}?detail=summary&kind=user&id=owner`,
]);
},
);
it.each([200, 404])(
'restores genuinely missing files after a %s response',
async (responseStatus) => {
status = responseStatus;
const file = retainedFile(1);
const retained = await service.checkSessionsAlive({ files: [file], req, baseURL });
expect(retained.has(file.file_id)).toBe(false);
const result = await service.provisionToCodeEnv({ req, file, route: { baseUrl: baseURL } });
expect(uploadedBodies).toEqual([Buffer.from('immutable stored bytes')]);
expect(result.refUpdate.ref).toMatchObject({
file_id: 'restored',
provisionedAt: expect.any(Number),
});
},
);
it.each([401, 403, 429, 500])(
'keeps unknown refs after %s without treating a response body as proof',
async (responseStatus) => {
status = responseStatus;
summary = [{ fileId: 'remote.1' }];
const file = retainedFile(1);
const retained = await service.checkSessionsAlive({ files: [file], req, baseURL });
expect(retained.has(file.file_id)).toBe(true);
expect(file.metadata!.codeEnvRef!.provisionedAt).toBeUndefined();
expect(uploadedBodies).toHaveLength(0);
},
);
it('restores an expired legacy object from stored bytes instead of trusting its path', async () => {
objectTimestamp = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
const file = retainedFile(1);
const { selected } = await selectCodeFiles({ files: [file], routeKey: 'default', getFileInfo });
expect(selected[0].isActive).toBe(false);
await service.provisionToCodeEnv({ req, file: selected[0].file, route: { baseUrl: baseURL } });
expect(uploadedBodies).toEqual([Buffer.from('immutable stored bytes')]);
});
it('never reuses a live path receipt as proof of immutable snapshot bytes', async () => {
const file = { ...retainedFile(1), context: FileContext.execute_code_snapshot };
file.metadata!.codeEnvRef!.provisionedAt = Date.now();
const getInfo = jest.fn(getFileInfo);
const { selected } = await selectCodeFiles({
files: [file],
routeKey: 'default',
getFileInfo: getInfo,
});
expect(selected[0]).toMatchObject({ ref: undefined, isActive: false });
expect(getInfo).not.toHaveBeenCalled();
await service.provisionToCodeEnv({ req, file: selected[0].file, route: { baseUrl: baseURL } });
expect(uploadedBodies).toEqual([Buffer.from('immutable stored bytes')]);
});
});

View file

@ -465,6 +465,33 @@ describe('createProvisionService', () => {
expect(alive.has('f5')).toBe(false);
});
it.each([null, {}, { files: [] }, 'not an array'])(
'keeps a malformed summary %p unverified',
async (data) => {
mockAxios.mockResolvedValue({ data });
const { service } = buildService();
expect(await service.checkSessionsAlive({ files: [staleFile('f1')], req })).toEqual(
new Set(['f1']),
);
},
);
it('starts independent session probes in parallel and probes each session once', async () => {
const pending: Array<(value: { data: unknown[] }) => void> = [];
mockAxios.mockImplementation(() => new Promise((resolve) => pending.push(resolve)));
const files = [staleFile('f1'), staleFile('f2'), staleFile('f3')];
files[2].metadata!.codeEnvRef!.storage_session_id = 'sess-2';
const { service } = buildService();
const checking = service.checkSessionsAlive({ files, req });
await new Promise(setImmediate);
expect(mockAxios).toHaveBeenCalledTimes(2);
for (const resolve of pending)
resolve({
data: [{ fileId: 'remote-f1' }, { fileId: 'remote-f2' }, { fileId: 'remote-f3' }],
});
expect(await checking).toEqual(new Set(['f1', 'f2', 'f3']));
});
describe('on an attached or stateful route', () => {
const ROUTE_KEY = 'stateful:env1';
const BYOM_URL = 'https://code-byom.test/v1';

View file

@ -27,8 +27,8 @@ import {
getCodeApiUploadOptions,
withCodeApiUploadRecovery,
} from '~/utils';
import { buildCodeEnvIdentityParams, getCodeEnvSessionFileIds } from '~/files/code/identity';
import { getCodeEnvUploadFilename, getUploadedCodeEnvFilename } from '../code/form';
import { buildCodeEnvIdentityParams } from '~/files/code/identity';
import { codeExecutionHeaders } from '~/agents/execution';
import { getCodeApiAuthHeaders } from '~/auth/codeapi';
import { resolveDownloadPath } from '~/storage/path';
@ -608,11 +608,13 @@ export function createProvisionService({
timeout: 5000,
});
const remoteFiles = response.data ?? [];
const remoteFiles: unknown = response.data;
if (!Array.isArray(remoteFiles)) {
throw new Error('Invalid Code API session summary');
}
const remoteFileIds = getCodeEnvSessionFileIds(remoteFiles, session_id);
for (const { file_id, remoteFileId } of fileEntries) {
if (
(remoteFiles as Array<{ fileId?: string }>).some((f) => f.fileId === remoteFileId)
) {
if (remoteFileIds.has(remoteFileId)) {
aliveFileIds.add(file_id);
}
}