🆔 fix: Atomic File Dedupe, Bedrock Tokens Fix, and Allowed MIME Types (#11675)
Some checks failed
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Has been cancelled
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Has been cancelled
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Has been cancelled
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Has been cancelled

* feat: Add support for Apache Parquet MIME types

- Introduced 'application/x-parquet' to the full MIME types list and code interpreter MIME types list.
- Updated application MIME types regex to include 'x-parquet' and 'vnd.apache.parquet'.
- Added mapping for '.parquet' files to 'application/x-parquet' in code type mapping, enhancing file format support.

* feat: Implement atomic file claiming for code execution outputs

- Added a new `claimCodeFile` function to atomically claim a file_id for code execution outputs, preventing duplicates by using a compound key of filename and conversationId.
- Updated `processCodeOutput` to utilize the new claiming mechanism, ensuring that concurrent calls for the same filename converge on a single record.
- Refactored related tests to validate the new atomic claiming behavior and its impact on file usage tracking and versioning.

* fix: Update image file handling to use cache-busting filepath

- Modified the `processCodeOutput` function to generate a cache-busting filepath for updated image files, improving browser caching behavior.
- Adjusted related tests to reflect the change from versioned filenames to cache-busted filepaths, ensuring accurate validation of image updates.

* fix: Update step handler to prevent undefined content for non-tool call types

- Modified the condition in useStepHandler to ensure that undefined content is only assigned for specific content types, enhancing the robustness of content handling.

* fix: Update bedrockOutputParser to handle maxTokens for adaptive models

- Modified the bedrockOutputParser logic to ensure that maxTokens is not set for adaptive models when neither maxTokens nor maxOutputTokens are provided, improving the handling of adaptive thinking configurations.
- Updated related tests to reflect these changes, ensuring accurate validation of the output for adaptive models.

* chore: Update @librechat/agents to version 3.1.38 in package.json and package-lock.json

* fix: Enhance file claiming and error handling in code processing

- Updated the `processCodeOutput` function to use a consistent file ID for claiming files, preventing duplicates and improving concurrency handling.
- Refactored the `createFileMethods` to include error handling for failed file claims, ensuring robust behavior when claiming files for conversations.
- These changes enhance the reliability of file management in the application.

* fix: Update adaptive thinking test for Opus 4.6 model

- Modified the test for configuring adaptive thinking to reflect that no default maxTokens should be set for the Opus 4.6 model.
- Updated assertions to ensure that maxTokens is undefined, aligning with the expected behavior for adaptive models.
This commit is contained in:
Danny Avila 2026-02-07 13:26:18 -05:00 committed by GitHub
parent a771d70b10
commit 9054ca9c15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 130 additions and 126 deletions

View file

@ -18,9 +18,9 @@ const {
getEndpointFileConfig,
} = require('librechat-data-provider');
const { filterFilesByAgentAccess } = require('~/server/services/Files/permissions');
const { createFile, getFiles, updateFile, claimCodeFile } = require('~/models');
const { getStrategyFunctions } = require('~/server/services/Files/strategies');
const { convertImage } = require('~/server/services/Files/images/convert');
const { createFile, getFiles, updateFile } = require('~/models');
const { determineFileType } = require('~/server/utils');
/**
@ -56,50 +56,6 @@ const createDownloadFallback = ({
};
};
/**
* Find an existing code-generated file by filename in the conversation.
* Used to update existing files instead of creating duplicates.
*
* ## Deduplication Strategy
*
* Files are deduplicated by `(conversationId, filename)` - NOT including `messageId`.
* This is an intentional design decision to handle iterative code development patterns:
*
* **Rationale:**
* - When users iteratively refine code (e.g., "regenerate that chart with red bars"),
* the same logical file (e.g., "chart.png") is produced multiple times
* - Without deduplication, each iteration would create a new file, leading to storage bloat
* - The latest version is what matters for re-upload to the code environment
*
* **Implications:**
* - Different messages producing files with the same name will update the same file record
* - The `messageId` field tracks which message last updated the file
* - The `usage` counter tracks how many times the file has been generated
*
* **Future Considerations:**
* - If file versioning is needed, consider adding a `versions` array or separate version collection
* - The current approach prioritizes storage efficiency over history preservation
*
* @param {string} filename - The filename to search for.
* @param {string} conversationId - The conversation ID.
* @returns {Promise<MongoFile | null>} The existing file or null.
*/
const findExistingCodeFile = async (filename, conversationId) => {
if (!filename || !conversationId) {
return null;
}
const files = await getFiles(
{
filename,
conversationId,
context: FileContext.execute_code,
},
{ createdAt: -1 },
{ text: 0 },
);
return files?.[0] ?? null;
};
/**
* Process code execution output files - downloads and saves both images and non-image files.
* All files are saved to local storage with fileIdentifier metadata for code env re-upload.
@ -170,12 +126,19 @@ const processCodeOutput = async ({
const fileIdentifier = `${session_id}/${id}`;
/**
* Check for existing file with same filename in this conversation.
* If found, we'll update it instead of creating a duplicate.
* Atomically claim a file_id for this (filename, conversationId, context) tuple.
* Uses $setOnInsert so concurrent calls for the same filename converge on
* a single record instead of creating duplicates (TOCTOU race fix).
*/
const existingFile = await findExistingCodeFile(name, conversationId);
const file_id = existingFile?.file_id ?? v4();
const isUpdate = !!existingFile;
const newFileId = v4();
const claimed = await claimCodeFile({
filename: name,
conversationId,
file_id: newFileId,
user: req.user.id,
});
const file_id = claimed.file_id;
const isUpdate = file_id !== newFileId;
if (isUpdate) {
logger.debug(
@ -184,27 +147,29 @@ const processCodeOutput = async ({
}
if (isImage) {
const usage = isUpdate ? (claimed.usage ?? 0) + 1 : 1;
const _file = await convertImage(req, buffer, 'high', `${file_id}${fileExt}`);
const filepath = usage > 1 ? `${_file.filepath}?v=${Date.now()}` : _file.filepath;
const file = {
..._file,
filepath,
file_id,
messageId,
usage: isUpdate ? (existingFile.usage ?? 0) + 1 : 1,
usage,
filename: name,
conversationId,
user: req.user.id,
type: `image/${appConfig.imageOutputType}`,
createdAt: isUpdate ? existingFile.createdAt : formattedDate,
createdAt: isUpdate ? claimed.createdAt : formattedDate,
updatedAt: formattedDate,
source: appConfig.fileStrategy,
context: FileContext.execute_code,
metadata: { fileIdentifier },
};
createFile(file, true);
await createFile(file, true);
return Object.assign(file, { messageId, toolCallId });
}
// For non-image files, save to configured storage strategy
const { saveBuffer } = getStrategyFunctions(appConfig.fileStrategy);
if (!saveBuffer) {
logger.warn(
@ -221,7 +186,6 @@ const processCodeOutput = async ({
});
}
// Determine MIME type from buffer or extension
const detectedType = await determineFileType(buffer, true);
const mimeType = detectedType?.mime || inferMimeType(name, '') || 'application/octet-stream';
@ -258,11 +222,11 @@ const processCodeOutput = async ({
metadata: { fileIdentifier },
source: appConfig.fileStrategy,
context: FileContext.execute_code,
usage: isUpdate ? (existingFile.usage ?? 0) + 1 : 1,
createdAt: isUpdate ? existingFile.createdAt : formattedDate,
usage: isUpdate ? (claimed.usage ?? 0) + 1 : 1,
createdAt: isUpdate ? claimed.createdAt : formattedDate,
};
createFile(file, true);
await createFile(file, true);
return Object.assign(file, { messageId, toolCallId });
} catch (error) {
logAxiosError({