mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 22:43:38 +00:00
🪢 fix: Keep One Code File per Sandbox Destination (#15817)
* 🪢 fix: Keep One Code File per Sandbox Destination
Codeapi mounts a by-ref input at the filename recorded on the stored
object (the file server's Content-Disposition) and only falls back to the
`name` sent in `/exec` when that header is absent. The `<stem>-<hash>`
alias `claimCodeDestination` handed to an older same-name file was
therefore remapped onto the bare name inside the sandbox, and the whole
request was rejected with "Conflicting input destinations" — on every
later turn, because nothing came back to collapse the pair. Any
conversation in which code rewrote an uploaded file in place, or the user
re-uploaded a file under its original name, lost code execution for good.
`primeFiles` now reserves each destination with `reserveCodeDestination`
in newest-content-first order and skips the files that lose, before the
freshness probe so a dropped file is never re-uploaded. This is the same
collapse `seedCodeFilesIntoSessions` and `ToolNode.updateCodeSession`
already apply. The spec stub mirrors the real directory-prefix rule so the
ancestor case is covered at this layer too.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* fix: preserve stored sandbox destinations during priming
* test: remove unused mock transport imports
* fix: recover unnamed legacy inputs and converted images safely
* fix: keep freshness decisions paired with mount destinations
* fix: align recovery destinations with upload and route boundaries
* style: format upload receipt regression fixture
* fix: distinguish stored paths from proposed recovery paths
* fix: preserve lazy provisioning destination priority and recovery names
* fix: reconcile recovery names with live code destinations
* fix: distinguish recoverable destinations from superseded scoped content
* fix: guard optional request user in recovery scope check
---------
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Danny Avila <danny@librechat.ai>
This commit is contained in:
parent
e1cc0805f2
commit
e49cb798da
18 changed files with 1246 additions and 259 deletions
|
|
@ -1,7 +1,6 @@
|
|||
import { Readable } from 'stream';
|
||||
import FormData from 'form-data';
|
||||
|
||||
import { appendCodeEnvFile, getCodeEnvFileOptions } from './form';
|
||||
import { Readable } from 'stream';
|
||||
import { appendCodeEnvFile, getCodeEnvFileOptions, getCodeEnvUploadFilename } from './form';
|
||||
|
||||
function renderMultipartDisposition(append: (form: FormData) => void): Promise<string> {
|
||||
const form = new FormData();
|
||||
|
|
@ -23,6 +22,21 @@ function renderMultipartDisposition(append: (form: FormData) => void): Promise<s
|
|||
}
|
||||
|
||||
describe('code env FormData filenames', () => {
|
||||
it.each([
|
||||
['my dir/file.csv', 'file.csv'],
|
||||
['safe/file.csv', 'safe/file.csv'],
|
||||
['分析/結果📊.csv', '分析/結果📊.csv'],
|
||||
['../../file.csv', 'file.csv'],
|
||||
])('plans the exact multipart destination for %s', async (input, expected) => {
|
||||
const destination = getCodeEnvUploadFilename(input);
|
||||
expect(destination).toBe(expected);
|
||||
expect(getCodeEnvUploadFilename(destination)).toBe(destination);
|
||||
const disposition = await renderMultipartDisposition((form) => {
|
||||
appendCodeEnvFile(form, Readable.from(['bytes']), destination);
|
||||
});
|
||||
expect(disposition).toContain(`filename="${destination}"`);
|
||||
});
|
||||
|
||||
it('uses filepath for nested filenames so form-data preserves directories', async () => {
|
||||
const disposition = await renderMultipartDisposition((form) => {
|
||||
appendCodeEnvFile(form, Readable.from(['x']), 'pptx/pptx.py');
|
||||
|
|
|
|||
|
|
@ -72,6 +72,20 @@ export function getCodeEnvFileOptions(filename: string): CodeEnvFileOptions {
|
|||
return { filename: basename, filepath: normalized };
|
||||
}
|
||||
|
||||
/** The destination encoded by the multipart adapter, including its safe-path fallback. */
|
||||
export function getCodeEnvUploadFilename(filename: string): string {
|
||||
const options = getCodeEnvFileOptions(filename);
|
||||
return options.filepath ?? options.filename;
|
||||
}
|
||||
|
||||
/** Older upload strategies may omit the receipt name; use their shared multipart normalization. */
|
||||
export function getUploadedCodeEnvFilename(
|
||||
uploaded: { filename?: string },
|
||||
requestedFilename: string,
|
||||
): string {
|
||||
return uploaded.filename || getCodeEnvUploadFilename(requestedFilename);
|
||||
}
|
||||
|
||||
export function appendCodeEnvFile(
|
||||
form: FormData,
|
||||
stream: NodeJS.ReadableStream,
|
||||
|
|
|
|||
|
|
@ -7,3 +7,4 @@ export * from './form';
|
|||
export * from './identity';
|
||||
export * from './image';
|
||||
export * from './preflight';
|
||||
export * from './priming';
|
||||
|
|
|
|||
292
packages/api/src/files/code/priming.spec.ts
Normal file
292
packages/api/src/files/code/priming.spec.ts
Normal file
|
|
@ -0,0 +1,292 @@
|
|||
import type { CodeEnvRef, TFile } from 'librechat-data-provider';
|
||||
import { selectCodeFiles } from './priming';
|
||||
|
||||
const file = (id: string, name: string, ref: Partial<CodeEnvRef> = {}, time = 1): TFile =>
|
||||
({
|
||||
file_id: id,
|
||||
filename: name,
|
||||
type: 'text/csv',
|
||||
createdAt: new Date(time),
|
||||
metadata: {
|
||||
codeEnvRef: { kind: 'user', id: 'user', storage_session_id: id, file_id: id, ...ref },
|
||||
},
|
||||
}) as TFile;
|
||||
|
||||
const fresh = { lastModified: new Date().toISOString() };
|
||||
|
||||
describe('selectCodeFiles', () => {
|
||||
it('preserves provisioned aliases across turns and reuses legacy metadata probes', async () => {
|
||||
const getFileInfo = jest.fn(async (ref: CodeEnvRef) => ({
|
||||
...fresh,
|
||||
originalFilename: ref.file_id === 'older' ? 'rows-alias.csv' : 'rows.csv',
|
||||
}));
|
||||
const result = await selectCodeFiles({
|
||||
files: [file('older', 'rows.csv'), file('newer', 'rows.csv', {}, 2)],
|
||||
routeKey: 'default',
|
||||
getFileInfo,
|
||||
});
|
||||
expect(result.selected.map((f) => f.sandboxName)).toEqual(['rows.csv', 'rows-alias.csv']);
|
||||
await Promise.all(result.selected.map((f) => f.getUploadTime()));
|
||||
expect(getFileInfo).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('keeps stored aliases when expired and selects the requested deployment reference', async () => {
|
||||
const older = file('older', 'rows.csv', { sandboxFilename: 'rows-old.csv' });
|
||||
older.metadata!.codeEnvRefs = {
|
||||
stateful: {
|
||||
kind: 'user',
|
||||
id: 'user',
|
||||
storage_session_id: 'stateful',
|
||||
file_id: 'remote',
|
||||
sandboxFilename: 'rows-stateful.csv',
|
||||
executionProfile: 'stateful',
|
||||
},
|
||||
};
|
||||
const getFileInfo = jest.fn(async () => null);
|
||||
const result = await selectCodeFiles({
|
||||
files: [older, file('newer', 'rows.csv', { sandboxFilename: 'rows.csv' }, 2)],
|
||||
routeKey: 'stateful',
|
||||
getFileInfo,
|
||||
});
|
||||
expect(result.selected.map((f) => f.sandboxName)).toEqual(['rows-stateful.csv', 'rows.csv']);
|
||||
expect(getFileInfo).toHaveBeenCalledTimes(1);
|
||||
expect(await result.selected[0].getUploadTime()).toBeUndefined();
|
||||
});
|
||||
|
||||
it('reserves a failed newest winner without reviving the superseded copy', async () => {
|
||||
const getFileInfo = jest.fn(async () => null);
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
file('older', 'rows.csv', { sandboxFilename: 'rows.csv' }),
|
||||
file('newer', 'rows.csv', { sandboxFilename: 'rows.csv' }, 2),
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo,
|
||||
});
|
||||
expect(result.selected.map((f) => f.file.file_id)).toEqual(['newer']);
|
||||
expect(result.skippedSuperseded).toBe(1);
|
||||
await result.selected[0].getUploadTime();
|
||||
expect(getFileInfo).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('lets an output supersede an aliased upload at its actual destination', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
file('upload', 'rows.csv', { sandboxFilename: 'rows-alias.csv' }),
|
||||
file('output', 'rows-alias.csv', {}, 2),
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => fresh,
|
||||
});
|
||||
expect(result.selected.map((f) => f.file.file_id)).toEqual(['output']);
|
||||
});
|
||||
|
||||
it('keeps shared content ahead of private resources and rejects ancestor conflicts', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [file('shared', 'data/rows.csv'), file('private', 'data', {}, 2)],
|
||||
privateFileIds: new Set(['private']),
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => fresh,
|
||||
});
|
||||
expect(result.selected.map((f) => f.file.file_id)).toEqual(['shared']);
|
||||
});
|
||||
|
||||
it('recovers distinct expired paths with a directory-prefix conflict', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
file('parent', 'reports', { sandboxFilename: 'reports' }),
|
||||
file('child', 'reports/data.csv', { sandboxFilename: 'reports/data.csv' }),
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => null,
|
||||
});
|
||||
expect(result.selected).toHaveLength(2);
|
||||
expect(new Set(result.selected.map((f) => f.sandboxName)).size).toBe(2);
|
||||
});
|
||||
|
||||
it('recovers every expired legacy input under a distinct persisted destination', async () => {
|
||||
const originals = [file('older', 'rows.csv'), file('newer', 'rows.csv', {}, 2)];
|
||||
const recovered = await selectCodeFiles({
|
||||
files: originals,
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => null,
|
||||
});
|
||||
expect(recovered.selected).toHaveLength(2);
|
||||
expect(new Set(recovered.selected.map((f) => f.sandboxName)).size).toBe(2);
|
||||
const restored = recovered.selected.map(({ file: original, sandboxName, sourceRef }) => ({
|
||||
...original,
|
||||
metadata: { codeEnvRef: { ...sourceRef, sandboxFilename: sandboxName } },
|
||||
}));
|
||||
const next = await selectCodeFiles({
|
||||
files: restored,
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => fresh,
|
||||
});
|
||||
expect(next.selected.map((f) => f.sandboxName)).toEqual(
|
||||
recovered.selected.map((f) => f.sandboxName),
|
||||
);
|
||||
});
|
||||
|
||||
it('allocates missing legacy inputs around confirmed live aliases', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
file('missing', 'rows.csv', {}, 3),
|
||||
file('live', 'rows.csv', { sandboxFilename: 'rows.csv' }),
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async (ref) => (ref.file_id === 'live' ? fresh : null),
|
||||
});
|
||||
expect(result.selected.find((f) => f.file.file_id === 'live')?.sandboxName).toBe('rows.csv');
|
||||
expect(result.selected.find((f) => f.file.file_id === 'missing')?.sandboxName).not.toBe(
|
||||
'rows.csv',
|
||||
);
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
'matches recovered image bytes while retaining a live remote name (expired=%s)',
|
||||
async (expired) => {
|
||||
const image = { ...file('image', 'plot.png'), type: 'image/webp' };
|
||||
const result = await selectCodeFiles({
|
||||
files: [image],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => ({
|
||||
originalFilename: 'plot-alias.png',
|
||||
lastModified: expired ? '2020-01-01' : fresh.lastModified,
|
||||
}),
|
||||
});
|
||||
expect(result.selected[0].sandboxName).toBe(expired ? 'plot-alias.webp' : 'plot-alias.png');
|
||||
},
|
||||
);
|
||||
|
||||
it('arbitrates collisions introduced by durable image conversion before uploading', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
{ ...file('image', 'plot.png', {}, 1), type: 'image/webp' },
|
||||
file('live', 'plot.webp', { sandboxFilename: 'plot.webp' }, 2),
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async (ref) =>
|
||||
ref.file_id === 'live'
|
||||
? fresh
|
||||
: { originalFilename: 'plot.png', lastModified: '2020-01-01' },
|
||||
});
|
||||
expect(result.selected).toHaveLength(2);
|
||||
expect(result.selected.find((f) => f.file.file_id === 'live')?.sandboxName).toBe('plot.webp');
|
||||
expect(result.selected.find((f) => f.file.file_id === 'image')?.sandboxName).toMatch(
|
||||
/^plot-.+\.webp$/,
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps a missing shared input independent of each agents private files', async () => {
|
||||
const shared = file('shared', 'rows.csv');
|
||||
const privateFile = file('private', 'rows.csv', { sandboxFilename: 'rows.csv' }, 2);
|
||||
const getFileInfo = async (ref: CodeEnvRef) => (ref.file_id === 'private' ? fresh : null);
|
||||
const first = await selectCodeFiles({ files: [shared], routeKey: 'default', getFileInfo });
|
||||
const second = await selectCodeFiles({
|
||||
files: [shared, privateFile],
|
||||
privateFileIds: new Set(['private']),
|
||||
routeKey: 'default',
|
||||
getFileInfo,
|
||||
});
|
||||
expect(second.selected.find((f) => f.file.file_id === 'shared')?.sandboxName).toBe(
|
||||
first.selected[0].sandboxName,
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps the freshness decision paired with the selected image destination', async () => {
|
||||
const now = Date.parse('2026-09-10T12:00:00Z');
|
||||
const clock = jest.spyOn(Date, 'now').mockReturnValue(now);
|
||||
const result = await selectCodeFiles({
|
||||
files: [{ ...file('image', 'plot.png'), type: 'image/webp' }],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => ({
|
||||
originalFilename: 'plot.png',
|
||||
lastModified: new Date(now - 23 * 3_600_000 + 1_000).toISOString(),
|
||||
}),
|
||||
});
|
||||
clock.mockReturnValue(now + 2_000);
|
||||
expect(result.selected[0]).toMatchObject({ isActive: true, sandboxName: 'plot.png' });
|
||||
clock.mockRestore();
|
||||
});
|
||||
|
||||
it('claims alternate-route names around destinations already present in the target route', async () => {
|
||||
const missing = file('missing', 'rows.csv', { sandboxFilename: 'rows.csv' }, 2);
|
||||
const live = file('live', 'rows.csv', {
|
||||
sandboxFilename: 'rows.csv',
|
||||
executionProfile: 'stateful',
|
||||
});
|
||||
const result = await selectCodeFiles({
|
||||
files: [missing, live],
|
||||
routeKey: 'stateful',
|
||||
getFileInfo: async () => fresh,
|
||||
});
|
||||
expect(result.selected).toHaveLength(2);
|
||||
expect(result.selected.find((f) => f.file.file_id === 'live')?.sandboxName).toBe('rows.csv');
|
||||
expect(result.selected.find((f) => f.file.file_id === 'missing')?.sandboxName).not.toBe(
|
||||
'rows.csv',
|
||||
);
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
'uses the multipart fallback only during recovery (expired=%s)',
|
||||
async (expired) => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [file('nested', 'my dir/file.csv')],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => ({
|
||||
originalFilename: 'my dir/file.csv',
|
||||
lastModified: expired ? '2020-01-01' : fresh.lastModified,
|
||||
}),
|
||||
});
|
||||
expect(result.selected[0].sandboxName).toBe(expired ? 'file.csv' : 'my dir/file.csv');
|
||||
},
|
||||
);
|
||||
|
||||
it('arbitrates recovery names after the multipart adapter flattens paths', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
file('older', 'my dir/file.csv'),
|
||||
file('newer', 'file.csv', { sandboxFilename: 'file.csv' }, 2),
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async (ref) =>
|
||||
ref.file_id === 'newer'
|
||||
? fresh
|
||||
: { originalFilename: 'my dir/file.csv', lastModified: '2020-01-01' },
|
||||
});
|
||||
expect(result.selected).toHaveLength(2);
|
||||
expect(result.selected.find((f) => f.file.file_id === 'newer')?.sandboxName).toBe('file.csv');
|
||||
expect(result.selected.find((f) => f.file.file_id === 'older')?.sandboxName).toMatch(
|
||||
/^file-.+\.csv$/,
|
||||
);
|
||||
});
|
||||
|
||||
it('collapses confirmed old-path duplicates before allocating converted recovery names', async () => {
|
||||
const result = await selectCodeFiles({
|
||||
files: [
|
||||
{ ...file('older', 'plot.png'), type: 'image/webp' },
|
||||
{ ...file('newer', 'plot.png', {}, 2), type: 'image/webp' },
|
||||
],
|
||||
routeKey: 'default',
|
||||
getFileInfo: async () => ({ originalFilename: 'plot.png', lastModified: '2020-01-01' }),
|
||||
});
|
||||
expect(result.selected.map((f) => f.file.file_id)).toEqual(['newer']);
|
||||
expect(result.selected[0].sandboxName).toBe('plot.webp');
|
||||
});
|
||||
|
||||
it('propagates cancellation during legacy recovery', async () => {
|
||||
const controller = new AbortController();
|
||||
const reason = new Error('cancelled');
|
||||
await expect(
|
||||
selectCodeFiles({
|
||||
files: [file('old', 'rows.csv')],
|
||||
routeKey: 'default',
|
||||
signal: controller.signal,
|
||||
getFileInfo: async () => {
|
||||
controller.abort(reason);
|
||||
return null;
|
||||
},
|
||||
}),
|
||||
).rejects.toBe(reason);
|
||||
});
|
||||
});
|
||||
201
packages/api/src/files/code/priming.ts
Normal file
201
packages/api/src/files/code/priming.ts
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
import {
|
||||
getCodeEnvRefs,
|
||||
getCodeEnvRefForProfile,
|
||||
resolveSandboxFilename,
|
||||
} from 'librechat-data-provider';
|
||||
import type { CodeEnvRef, TFile } from 'librechat-data-provider';
|
||||
import type { AxiosInstance } from 'axios';
|
||||
import type { CodeExecutionRoute } from '../provision/service';
|
||||
import type { ServerRequest } from '~/types';
|
||||
import {
|
||||
claimCodeDestination,
|
||||
createCodeDestinationSet,
|
||||
reserveCodeDestination,
|
||||
sortCodeFilesByDestinationPriority,
|
||||
} from './destinations';
|
||||
import {
|
||||
codeServerHttpAgent,
|
||||
codeServerHttpsAgent,
|
||||
createCodeApiUploadRegistry,
|
||||
withCodeApiUploadSlot,
|
||||
} from '~/utils/code';
|
||||
import { codeExecutionHeaders } from '~/agents/execution';
|
||||
import { buildCodeEnvDownloadQuery } from './identity';
|
||||
import { getCodeEnvUploadFilename } from './form';
|
||||
import { isAbortError } from '~/utils/errors';
|
||||
|
||||
export interface CodeFileInfo {
|
||||
lastModified?: string;
|
||||
originalFilename?: string;
|
||||
}
|
||||
|
||||
/** The object metadata endpoint reports the same original filename as the download header. */
|
||||
export async function getCodeFileInfo({
|
||||
ref,
|
||||
req,
|
||||
route,
|
||||
signal,
|
||||
request,
|
||||
getBaseURL,
|
||||
getAuthHeaders,
|
||||
}: {
|
||||
ref: CodeEnvRef;
|
||||
req: ServerRequest;
|
||||
route: CodeExecutionRoute & { bridgeWorkerId?: string };
|
||||
signal?: AbortSignal;
|
||||
request: AxiosInstance;
|
||||
getBaseURL: () => string;
|
||||
getAuthHeaders: (req: ServerRequest, bridgeWorkerId?: string) => Promise<Record<string, string>>;
|
||||
}): Promise<CodeFileInfo | null> {
|
||||
try {
|
||||
signal?.throwIfAborted();
|
||||
const headers = await getAuthHeaders(req, route.bridgeWorkerId);
|
||||
signal?.throwIfAborted();
|
||||
const query = buildCodeEnvDownloadQuery(ref);
|
||||
const response = await request<CodeFileInfo>({
|
||||
method: 'get',
|
||||
url: `${route.baseUrl ?? getBaseURL()}/sessions/${ref.storage_session_id}/objects/${ref.file_id}${query}`,
|
||||
headers: {
|
||||
'User-Agent': 'LibreChat/1.0',
|
||||
...headers,
|
||||
...(route.executionProfile
|
||||
? codeExecutionHeaders({
|
||||
executionProfile: route.executionProfile,
|
||||
bridgeWorkerId: route.bridgeWorkerId,
|
||||
})
|
||||
: {}),
|
||||
},
|
||||
httpAgent: codeServerHttpAgent,
|
||||
httpsAgent: codeServerHttpsAgent,
|
||||
timeout: 5000,
|
||||
signal,
|
||||
});
|
||||
signal?.throwIfAborted();
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
if (signal?.aborted && isAbortError(error)) {
|
||||
throw error;
|
||||
}
|
||||
signal?.throwIfAborted();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Matches the existing 23-hour code-storage freshness window. */
|
||||
export function checkCodeFileActive(dateString: string | undefined): boolean {
|
||||
return dateString != null && (Date.now() - new Date(dateString).getTime()) / 3_600_000 < 23;
|
||||
}
|
||||
|
||||
interface PrimedCodeFile {
|
||||
file: TFile;
|
||||
ref: CodeEnvRef | undefined;
|
||||
sourceRef: CodeEnvRef;
|
||||
sandboxName: string;
|
||||
isActive: boolean;
|
||||
getUploadTime: () => Promise<string | undefined>;
|
||||
}
|
||||
|
||||
/** Reserve actual storage destinations before recovery so a failed winner never revives stale content. */
|
||||
export async function selectCodeFiles({
|
||||
files,
|
||||
privateFileIds,
|
||||
routeKey,
|
||||
getFileInfo,
|
||||
concurrency,
|
||||
signal,
|
||||
}: {
|
||||
files: Array<TFile | null | undefined>;
|
||||
privateFileIds?: ReadonlySet<string>;
|
||||
routeKey: string;
|
||||
getFileInfo: (ref: CodeEnvRef) => Promise<CodeFileInfo | null>;
|
||||
concurrency?: number;
|
||||
signal?: AbortSignal;
|
||||
}): Promise<{ selected: PrimedCodeFile[]; skippedNoRef: number; skippedSuperseded: number }> {
|
||||
let skippedNoRef = 0;
|
||||
let skippedSuperseded = 0;
|
||||
const candidates = [];
|
||||
const registry = createCodeApiUploadRegistry();
|
||||
for (const file of sortCodeFilesByDestinationPriority(files, privateFileIds)) {
|
||||
if (!file) {
|
||||
continue;
|
||||
}
|
||||
const ref = getCodeEnvRefForProfile(file.metadata, routeKey);
|
||||
const sourceRef = ref ?? getCodeEnvRefs(file.metadata)[0]?.[1];
|
||||
if (!sourceRef) {
|
||||
skippedNoRef++;
|
||||
continue;
|
||||
}
|
||||
let probe: Promise<CodeFileInfo | null> | undefined;
|
||||
const getInfo = () =>
|
||||
(probe ??= withCodeApiUploadSlot({
|
||||
registry,
|
||||
scope: routeKey,
|
||||
concurrency,
|
||||
signal,
|
||||
task: () => getFileInfo(sourceRef),
|
||||
}));
|
||||
candidates.push({ file, ref, sourceRef, getInfo });
|
||||
}
|
||||
const resolved = await Promise.all(
|
||||
candidates.map(async (candidate) => {
|
||||
const { file, ref, sourceRef, getInfo } = candidate;
|
||||
const info = ref ? await getInfo() : null;
|
||||
signal?.throwIfAborted();
|
||||
const recovering = !ref || !checkCodeFileActive(info?.lastModified);
|
||||
const storedName = sourceRef.sandboxFilename ?? info?.originalFilename;
|
||||
const sandboxName = storedName ?? resolveSandboxFilename(file.filename, file.type);
|
||||
const destination = recovering
|
||||
? getCodeEnvUploadFilename(resolveSandboxFilename(sandboxName, file.type))
|
||||
: sandboxName;
|
||||
const assignRecoveryName = !ref || recovering;
|
||||
return {
|
||||
file,
|
||||
ref,
|
||||
sourceRef,
|
||||
isActive: !recovering,
|
||||
sandboxName: destination,
|
||||
storedName: ref ? storedName : undefined,
|
||||
assignRecoveryName,
|
||||
selectionPriority:
|
||||
(privateFileIds?.has(file.file_id) ? 2 : 0) +
|
||||
Number(!ref || (recovering && (!storedName || destination !== storedName))),
|
||||
getUploadTime: async () => info?.lastModified,
|
||||
};
|
||||
}),
|
||||
);
|
||||
/** Collapse confirmed source-path collisions before renaming recovery files.
|
||||
* Distinct old paths that normalize alike are independent inputs, not superseded copies. */
|
||||
const storedDestinations = new Set<string>();
|
||||
const surviving = resolved.filter((candidate) => {
|
||||
if (candidate.storedName && storedDestinations.has(candidate.storedName)) {
|
||||
skippedSuperseded++;
|
||||
return false;
|
||||
}
|
||||
if (candidate.storedName) storedDestinations.add(candidate.storedName);
|
||||
return true;
|
||||
});
|
||||
/** Keep shared files independent of each agent's private set. Within each
|
||||
* scope, guessed recovery names cannot displace a confirmed stored path. */
|
||||
surviving.sort((a, b) => a.selectionPriority - b.selectionPriority);
|
||||
const destinations = createCodeDestinationSet();
|
||||
const selected: PrimedCodeFile[] = [];
|
||||
for (const candidate of surviving) {
|
||||
if (candidate.assignRecoveryName) {
|
||||
selected.push({
|
||||
...candidate,
|
||||
sandboxName: claimCodeDestination(
|
||||
destinations,
|
||||
candidate.sandboxName,
|
||||
candidate.file.file_id,
|
||||
),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (!reserveCodeDestination(destinations, candidate.sandboxName)) {
|
||||
skippedSuperseded++;
|
||||
continue;
|
||||
}
|
||||
selected.push(candidate);
|
||||
}
|
||||
return { selected, skippedNoRef, skippedSuperseded };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue